1
use super::{Arm, arm_of_form, in_runtime_branch, progn_value_arm};
2
use crate::ast::Fraction;
3
use crate::ast::{Expr, Number, WasmType};
4
use crate::error::Error;
5
use crate::runtime::SymbolTable;
6

            
7
#[test]
8
1
fn in_runtime_branch_marks_only_the_closure() {
9
1
    let mut symbols = SymbolTable::new();
10
1
    let inside = in_runtime_branch(&mut symbols, |symbols| {
11
1
        Ok(symbols.is_under_any_runtime_guard())
12
1
    });
13
1
    assert!(inside.unwrap());
14
1
    assert!(!symbols.is_under_any_runtime_guard());
15
1
}
16

            
17
#[test]
18
1
fn in_runtime_branch_prefers_the_closure_error() {
19
1
    let mut symbols = SymbolTable::new();
20
1
    let result: crate::error::Result<()> = in_runtime_branch(&mut symbols, |symbols| {
21
1
        symbols.exit_runtime_branch()?;
22
1
        Err(Error::Compile("closure failed".to_string()))
23
1
    });
24
1
    assert!(result.unwrap_err().to_string().contains("closure failed"));
25
1
}
26

            
27
#[test]
28
1
fn in_runtime_branch_reports_an_exit_failure_after_a_successful_closure() {
29
1
    let mut symbols = SymbolTable::new();
30
1
    let result = in_runtime_branch(&mut symbols, |symbols| symbols.exit_runtime_branch());
31
1
    assert!(
32
1
        result
33
1
            .unwrap_err()
34
1
            .to_string()
35
1
            .contains("exit_runtime_branch()")
36
    );
37
1
}
38

            
39
8
fn progn_ending_in(head: &str, value: Expr) -> Expr {
40
8
    Expr::List(vec![
41
8
        Expr::Symbol(head.into()),
42
8
        Expr::Number(Number::Index(1.into())),
43
8
        value,
44
8
    ])
45
8
}
46

            
47
3
fn progn_nil(head: &str) -> Expr {
48
3
    progn_ending_in(head, Expr::Nil)
49
3
}
50

            
51
#[test]
52
1
fn a_progn_ending_in_nil_or_a_number_has_that_arm() {
53
1
    assert_eq!(progn_value_arm(&progn_nil("PROGN")), Some(Arm::Nil));
54
1
    assert_eq!(progn_value_arm(&progn_nil("LIST")), None);
55
1
    let ratio = Expr::Number(Number::Scalar(Fraction::new(1, 2)));
56
1
    assert_eq!(
57
1
        progn_value_arm(&progn_ending_in("PROGN", ratio)),
58
1
        Some(Arm::NumberLiteral(WasmType::Ratio))
59
    );
60
1
    let nested = progn_ending_in(
61
1
        "PROGN",
62
1
        progn_ending_in("PROGN", Expr::Number(Number::Index(2.into()))),
63
1
    );
64
1
    assert_eq!(
65
1
        progn_value_arm(&nested),
66
1
        Some(Arm::NumberLiteral(WasmType::I32))
67
    );
68
1
    let call = Expr::List(vec![Expr::Symbol("F".into())]);
69
1
    assert_eq!(progn_value_arm(&progn_ending_in("PROGN", call)), None);
70
1
    assert_eq!(
71
1
        progn_value_arm(&Expr::Number(Number::Index(2.into()))),
72
1
        None
73
    );
74
1
}
75

            
76
#[test]
77
1
fn arm_of_form_defers_a_progn_ending_in_nil() {
78
1
    let value = Expr::Number(Number::Index(5.into()));
79
1
    let arm = arm_of_form(&progn_nil("PROGN"), &value).unwrap();
80
1
    assert!(matches!(arm, Arm::Nil));
81
1
}
82

            
83
#[test]
84
1
fn arm_of_form_keeps_the_literal_a_progn_ends_in() {
85
1
    let runtime = Expr::WasmRuntime(WasmType::I32);
86
1
    let arm = arm_of_form(
87
1
        &progn_ending_in("PROGN", Expr::Number(Number::Index(2.into()))),
88
1
        &runtime,
89
1
    )
90
1
    .unwrap();
91
1
    assert_eq!(arm, Arm::NumberLiteral(WasmType::I32));
92
1
}
93

            
94
#[test]
95
1
fn a_value_with_no_stack_type_is_a_structured_error() {
96
1
    let cons = Expr::Cons(
97
1
        Box::new(Expr::Number(Number::Index(1.into()))),
98
1
        Box::new(Expr::Number(Number::Index(2.into()))),
99
1
    );
100
1
    let err = arm_of_form(&cons, &cons).unwrap_err();
101
1
    assert!(err.to_string().contains("no runtime value type"), "{err}");
102
1
}
103

            
104
#[test]
105
1
fn a_quoted_symbol_arm_is_a_symbol() {
106
1
    let quoted = Expr::Quote(Box::new(Expr::Symbol("FOO".into())));
107
1
    assert_eq!(
108
1
        arm_of_form(&quoted, &quoted).unwrap(),
109
1
        Arm::Value(WasmType::SymbolRef)
110
    );
111
1
}