1
use super::*;
2
use crate::compiler::expr::format_form;
3
use crate::reader::Reader;
4

            
5
5
fn parse(source: &str) -> Expr {
6
5
    Reader::parse(source).expect("parses").exprs.remove(0)
7
5
}
8

            
9
4
fn source(fields: &[(&str, WasmType)]) -> ClosureSource {
10
4
    let fields = fields
11
4
        .iter()
12
1
        .map(|(name, ty)| (name.to_string(), *ty))
13
        .collect();
14
4
    ClosureSource::new(
15
4
        &LambdaParams::simple(vec!["X".to_string()]),
16
4
        &parse("(* x r)"),
17
4
        fields,
18
4
    )
19
4
}
20

            
21
2
fn lambda_text(lambda: &Expr) -> String {
22
2
    match lambda {
23
2
        Expr::Lambda(params, body) => format!("{:?} {}", params.required, format_form(body)),
24
        other => panic!("not a lambda: {other:?}"),
25
    }
26
2
}
27

            
28
#[test]
29
1
fn a_closure_with_nothing_captured_is_its_lambda() {
30
1
    let lambda = source(&[]).lambda(Vec::new());
31
1
    assert_eq!(lambda_text(&lambda), r#"["X"] (* X R)"#);
32
1
}
33

            
34
#[test]
35
1
fn the_values_of_the_captured_variables_are_bound_around_the_body() {
36
1
    let lambda = source(&[("R", WasmType::Ratio)]).lambda(vec![parse("1/10")]);
37
1
    assert_eq!(lambda_text(&lambda), r#"["X"] (LET ((R 1/10)) (* X R))"#);
38
1
}
39

            
40
#[test]
41
1
fn a_site_names_a_source_of_the_last_unit_only() {
42
1
    let mut symbols = SymbolTable::new();
43
1
    assert_eq!(symbols.closure_source(0), None);
44
1
    symbols.finish_unit(vec![source(&[])]);
45
1
    assert_eq!(symbols.closure_source(0), Some(&source(&[])));
46
1
    assert_eq!(symbols.closure_source(1), None);
47
1
    assert_eq!(symbols.closure_source(-1), None);
48
1
}