1
use super::{compile_keywordp_to_stack, compile_symbol_name_to_stack, compile_symbolp_to_stack};
2
use super::{keywordp, symbol_name, symbolp};
3
use crate::ast::{Expr, WasmType};
4
use crate::compiler::context::CompileContext;
5
use crate::compiler::emit::FunctionEmitter;
6
use crate::error::{Error, Result};
7
use crate::reader::Reader;
8
use crate::runtime::SymbolTable;
9

            
10
type Eval = fn(&mut SymbolTable, &[Expr]) -> Result<Expr>;
11

            
12
21
fn eval(native: Eval, arg: &str) -> Result<Expr> {
13
21
    let mut symbols = SymbolTable::with_builtins();
14
21
    native(&mut symbols, &[Reader::parse_expr(arg)?])
15
21
}
16

            
17
#[test]
18
1
fn symbolp_is_true_for_every_kind_of_symbol() {
19
1
    for arg in ["'a", ":a", "nil", "t"] {
20
4
        assert_eq!(eval(symbolp, arg).unwrap(), Expr::T, "{arg}");
21
    }
22
1
}
23

            
24
#[test]
25
1
fn symbolp_is_false_for_other_values() {
26
1
    for arg in ["1", "\"a\"", "'(a)", "1/2"] {
27
4
        assert_eq!(eval(symbolp, arg).unwrap(), Expr::Nil, "{arg}");
28
    }
29
1
}
30

            
31
#[test]
32
1
fn keywordp_is_true_only_for_keywords() {
33
1
    assert_eq!(eval(keywordp, ":a").unwrap(), Expr::T);
34
1
    for arg in ["'a", "nil", "t", "\"a\"", "1"] {
35
5
        assert_eq!(eval(keywordp, arg).unwrap(), Expr::Nil, "{arg}");
36
    }
37
1
}
38

            
39
#[test]
40
1
fn symbol_name_drops_the_keyword_colon() {
41
1
    for (arg, name) in [(":foo", "FOO"), ("'foo", "FOO"), ("nil", "NIL"), ("t", "T")] {
42
4
        assert_eq!(eval(symbol_name, arg).unwrap(), Expr::String(name.into()));
43
    }
44
1
}
45

            
46
#[test]
47
1
fn symbol_name_of_a_non_symbol_is_a_compile_error() {
48
1
    for arg in ["\"x\"", "1", "'(a)"] {
49
3
        let err = eval(symbol_name, arg);
50
3
        assert!(matches!(err, Err(Error::Compile(ref m)) if m.contains("SYMBOL-NAME")));
51
    }
52
1
}
53

            
54
#[test]
55
1
fn a_runtime_operand_is_an_unfolded_placeholder() {
56
1
    let mut symbols = SymbolTable::with_builtins();
57
1
    let runtime = [Expr::WasmRuntime(WasmType::AnyRef)];
58
1
    assert_eq!(
59
1
        symbolp(&mut symbols, &runtime).unwrap(),
60
1
        Expr::WasmRuntime(WasmType::Bool)
61
    );
62
1
    assert_eq!(
63
1
        symbol_name(&mut symbols, &runtime).unwrap(),
64
1
        Expr::WasmRuntime(WasmType::StringRef)
65
    );
66
1
    let string = [Expr::WasmRuntime(WasmType::StringRef)];
67
1
    assert!(symbol_name(&mut symbols, &string).is_err());
68
1
}
69

            
70
#[test]
71
1
fn a_wrong_arity_is_an_arity_error() {
72
1
    let mut symbols = SymbolTable::with_builtins();
73
1
    for native in [symbolp, keywordp, symbol_name] {
74
3
        assert!(matches!(
75
3
            native(&mut symbols, &[]),
76
            Err(Error::Arity { expected: 1, .. })
77
        ));
78
    }
79
1
}
80

            
81
type Compile =
82
    fn(&mut CompileContext, &mut FunctionEmitter, &mut SymbolTable, &[Expr]) -> Result<WasmType>;
83

            
84
10
fn lower(native: Compile, arg: &str) -> Result<WasmType> {
85
10
    let mut ctx = CompileContext::new_with_host_fns(&[])?;
86
10
    let mut emit = FunctionEmitter::new();
87
10
    let mut symbols = SymbolTable::with_builtins();
88
10
    let ty = native(
89
10
        &mut ctx,
90
10
        &mut emit,
91
10
        &mut symbols,
92
10
        &[Reader::parse_expr(arg)?],
93
1
    )?;
94
9
    emit.finish(&[])?;
95
9
    Ok(ty)
96
10
}
97

            
98
#[test]
99
1
fn predicates_lower_to_a_truth_value() {
100
1
    for arg in ["'a", "(progn (print 1) 'a)", "(progn (print 1) 1)"] {
101
3
        assert_eq!(
102
3
            lower(compile_symbolp_to_stack, arg).unwrap(),
103
3
            WasmType::Bool
104
        );
105
3
        assert_eq!(
106
3
            lower(compile_keywordp_to_stack, arg).unwrap(),
107
3
            WasmType::Bool
108
        );
109
    }
110
1
}
111

            
112
#[test]
113
1
fn symbol_name_lowers_to_a_string() {
114
1
    for arg in [":a", "(progn (print 1) 'a)", "(progn (print 1) nil)"] {
115
3
        assert_eq!(
116
3
            lower(compile_symbol_name_to_stack, arg).unwrap(),
117
3
            WasmType::StringRef
118
        );
119
    }
120
1
}
121

            
122
#[test]
123
1
fn symbol_name_of_a_runtime_non_symbol_is_refused() {
124
1
    let err = lower(compile_symbol_name_to_stack, "(progn (print 1) 1)");
125
1
    assert!(matches!(err, Err(Error::Compile(ref m)) if m.contains("SYMBOL-NAME")));
126
1
}