1
use super::*;
2
use crate::compiler::context::CompileContext;
3
use crate::compiler::emit::FunctionEmitter;
4
use crate::compiler::special::try_emit_lambda_for_value;
5

            
6
1
fn required_lambda() -> Expr {
7
1
    Expr::Lambda(
8
1
        LambdaParams::simple(vec!["a".to_string()]),
9
1
        Box::new(Expr::Nil),
10
1
    )
11
1
}
12

            
13
2
fn optional_lambda() -> Expr {
14
2
    let mut params = LambdaParams::simple(vec!["a".to_string()]);
15
2
    params.optional.push(("b".to_string(), None));
16
2
    Expr::Lambda(params, Box::new(Expr::Nil))
17
2
}
18

            
19
#[test]
20
1
fn a_required_only_lambda_passes_the_gate() {
21
1
    assert!(reject_non_v1_lambda(&required_lambda()).is_ok());
22
1
}
23

            
24
#[test]
25
1
fn a_non_lambda_passes_the_gate() {
26
1
    assert!(reject_non_v1_lambda(&Expr::Nil).is_ok());
27
1
}
28

            
29
#[test]
30
1
fn an_optional_lambda_is_the_structured_error() {
31
1
    let err = reject_non_v1_lambda(&optional_lambda()).expect_err("non-v1 rejected");
32
1
    assert_eq!(err.to_string(), non_v1_lambda_error().to_string());
33
1
    assert!(
34
1
        err.to_string()
35
1
            .contains("not yet supported as a first-class wasm closure")
36
    );
37
1
}
38

            
39
#[test]
40
1
fn only_the_v1_shape_is_emitted_as_a_value() {
41
1
    let mut ctx = CompileContext::new_with_host_fns(&[]).expect("ctx");
42
1
    let mut emit = FunctionEmitter::new();
43
1
    let mut symbols = SymbolTable::with_builtins_for_wasm();
44
1
    let Expr::Lambda(params, body) = optional_lambda() else {
45
        return;
46
    };
47
1
    let emitted = try_emit_lambda_for_value(&mut ctx, &mut emit, &mut symbols, &params, &body);
48
1
    assert!(matches!(emitted, Ok(None)));
49
1
}