1
use super::*;
2
use crate::compiler::expr::{compile_for_effect, compile_for_stack};
3
use crate::reader::Reader;
4

            
5
5
fn form(source: &str) -> Expr {
6
5
    Reader::parse(source)
7
5
        .expect("source parses")
8
5
        .exprs
9
5
        .into_iter()
10
5
        .next()
11
5
        .expect("one form")
12
5
}
13

            
14
3
fn stack_type(source: &str) -> Result<WasmType> {
15
3
    let mut ctx = CompileContext::new_with_host_fns(&[])?;
16
3
    let mut emit = FunctionEmitter::new();
17
3
    let mut symbols = SymbolTable::with_builtins_for_wasm();
18
3
    compile_for_stack(&mut ctx, &mut emit, &mut symbols, &form(source))
19
3
}
20

            
21
#[test]
22
1
fn a_lambda_form_pushes_a_closure_value() {
23
1
    assert!(matches!(
24
1
        stack_type("(lambda (x) x)"),
25
        Ok(WasmType::Closure(_))
26
    ));
27
1
}
28

            
29
#[test]
30
1
fn a_function_form_of_a_lambda_pushes_a_closure_value() {
31
1
    assert!(matches!(
32
1
        stack_type("(function (lambda (x) x))"),
33
        Ok(WasmType::Closure(_))
34
    ));
35
1
}
36

            
37
#[test]
38
1
fn a_non_v1_lambda_in_stack_position_is_the_structured_error() {
39
1
    let err = stack_type("(lambda (&optional x) x)").expect_err("non-v1 lambda");
40
1
    assert_eq!(err.to_string(), non_v1_lambda_error().to_string());
41
1
}
42

            
43
#[test]
44
1
fn a_lambda_statement_emits_no_value() {
45
1
    let mut ctx = CompileContext::new_with_host_fns(&[]).expect("ctx");
46
1
    let mut emit = FunctionEmitter::new();
47
1
    let mut symbols = SymbolTable::with_builtins_for_wasm();
48
1
    let result = compile_for_effect(&mut ctx, &mut emit, &mut symbols, &form("(lambda (x) x)"));
49
1
    assert!(result.is_ok());
50
1
}
51

            
52
#[test]
53
1
fn a_malformed_lambda_statement_is_still_an_error() {
54
1
    let mut ctx = CompileContext::new_with_host_fns(&[]).expect("ctx");
55
1
    let mut emit = FunctionEmitter::new();
56
1
    let mut symbols = SymbolTable::with_builtins_for_wasm();
57
1
    let result = compile_for_effect(&mut ctx, &mut emit, &mut symbols, &form("(lambda (x))"));
58
1
    assert!(result.is_err());
59
1
}