1
use super::{compound_setf_call, expand_macro_places};
2
use crate::ast::{Expr, LambdaParams};
3
use crate::reader::Reader;
4
use crate::runtime::{Symbol, SymbolKind, SymbolTable};
5

            
6
24
fn form(source: &str) -> Expr {
7
24
    Reader::parse_expr(source).expect("form parses")
8
24
}
9

            
10
6
fn table() -> SymbolTable {
11
6
    let mut symbols = SymbolTable::with_builtins_for_wasm();
12
6
    let place = Expr::Lambda(
13
6
        LambdaParams::simple(vec!["P".to_string()]),
14
6
        Box::new(form("`(pt-x ,p)")),
15
6
    );
16
6
    symbols.define(Symbol::new("PX", SymbolKind::Macro).with_function(place));
17
6
    symbols
18
6
}
19

            
20
#[test]
21
1
fn a_macro_place_is_replaced_by_its_expansion() {
22
1
    let args = [form("(px q)"), form("(px 1)")];
23
1
    let expanded = expand_macro_places(&table(), &args).expect("expands");
24
1
    assert_eq!(expanded, vec![form("(pt-x q)"), form("(px 1)")]);
25
1
}
26

            
27
#[test]
28
1
fn a_plain_place_is_kept() {
29
1
    let args = [form("(pt-x q)"), form("3"), form("n"), form("4")];
30
1
    let expanded = expand_macro_places(&table(), &args).expect("expands");
31
1
    assert_eq!(expanded, args.to_vec());
32
1
}
33

            
34
4
fn with_setter() -> SymbolTable {
35
4
    let mut symbols = table();
36
4
    let setter = Expr::Lambda(LambdaParams::simple(Vec::new()), Box::new(Expr::Nil));
37
4
    symbols.define(Symbol::new("(SETF PT-X)", SymbolKind::Function).with_function(setter));
38
4
    symbols
39
4
}
40

            
41
#[test]
42
1
fn a_compound_place_stores_through_its_setter_with_the_value_last() {
43
1
    let call = compound_setf_call(&with_setter(), &form("(pt-x (car l))"), &form("v"))
44
1
        .expect("a setter exists");
45
1
    assert_eq!(
46
1
        call,
47
1
        Some(Expr::List(vec![
48
1
            Expr::Symbol("(SETF PT-X)".to_string()),
49
1
            form("(car l)"),
50
1
            form("v"),
51
1
        ]))
52
    );
53
1
}
54

            
55
#[test]
56
1
fn a_symbol_place_has_no_setter_call() {
57
1
    let call = compound_setf_call(&with_setter(), &form("n"), &form("v")).expect("no error");
58
1
    assert_eq!(call, None);
59
1
}
60

            
61
#[test]
62
1
fn a_compound_place_without_a_setter_is_an_error() {
63
    let err =
64
1
        compound_setf_call(&with_setter(), &form("(pt-y q)"), &form("v")).expect_err("no setter");
65
1
    assert_eq!(
66
1
        err.to_string(),
67
1
        "Compile error: No setf method defined for accessor 'PT-Y'"
68
    );
69
1
}
70

            
71
#[test]
72
1
fn a_place_headed_by_a_non_symbol_is_an_error() {
73
1
    let err = compound_setf_call(&with_setter(), &form("(1 q)"), &form("v")).expect_err("no head");
74
1
    assert!(err.to_string().contains("expected function call"), "{err}");
75
1
}