1
use crate::ast::{Expr, KeyParam, Number};
2
use crate::runtime::Value;
3

            
4
use super::{format_expr, format_form, format_runtime_value, not_callable};
5

            
6
10
fn sym(name: &str) -> Expr {
7
10
    Expr::Symbol(name.into())
8
10
}
9

            
10
#[test]
11
1
fn nil_and_the_empty_list_print_as_nil() {
12
1
    assert_eq!(format_expr(&Expr::Nil), "nil");
13
1
    assert_eq!(format_expr(&Expr::List(vec![])), "nil");
14
1
    assert_eq!(format_runtime_value(&Value::Nil), "nil");
15
1
}
16

            
17
#[test]
18
1
fn a_struct_value_prints_as_common_lisp_does() {
19
1
    let value = Value::Struct {
20
1
        name: "PT".to_string(),
21
1
        fields: vec![("X".to_string(), Value::Number(Number::Index(8)))],
22
1
    };
23
1
    assert_eq!(format_runtime_value(&value), "#S(PT :X 8)");
24
1
}
25

            
26
#[test]
27
1
fn a_cons_chain_ending_in_nil_prints_as_a_proper_list() {
28
1
    assert_eq!(format_expr(&Expr::cons(sym("A"), Expr::Nil)), "(A)");
29
1
    assert_eq!(
30
1
        format_expr(&Expr::cons(sym("A"), Expr::cons(sym("B"), Expr::Nil))),
31
1
        "(A B)"
32
    );
33
1
}
34

            
35
#[test]
36
1
fn a_cons_chain_ending_in_an_atom_keeps_its_dot() {
37
1
    assert_eq!(format_expr(&Expr::cons(sym("A"), sym("B"))), "(A . B)");
38
1
    assert_eq!(
39
1
        format_expr(&Expr::cons(sym("A"), Expr::cons(sym("B"), sym("C")))),
40
1
        "(A B . C)"
41
    );
42
1
}
43

            
44
#[test]
45
1
fn not_callable_names_the_head_as_the_reader_writes_it() {
46
1
    let quoted = Expr::Quote(Box::new(Expr::List(vec![
47
1
        Expr::Number(Number::Index(1.into())),
48
1
        Expr::Number(Number::Index(2.into())),
49
1
    ])));
50
1
    assert_eq!(
51
1
        not_callable(&quoted).to_string(),
52
1
        "Compile error: not callable: '(1 2)"
53
    );
54
1
    assert_eq!(
55
1
        not_callable(&Expr::Number(Number::Index(5.into()))).to_string(),
56
1
        "Compile error: not callable: 5"
57
    );
58
1
}
59

            
60
#[test]
61
1
fn not_callable_tells_a_quoted_symbol_head_to_use_funcall() {
62
1
    let quoted = Expr::Quote(Box::new(Expr::symbol("CAR")));
63
1
    assert_eq!(
64
1
        not_callable(&quoted).to_string(),
65
1
        "Compile error: illegal function call: ('CAR …); use (funcall 'car …)"
66
    );
67
1
}
68

            
69
10
fn text(s: &str) -> Expr {
70
10
    Expr::String(s.into())
71
10
}
72

            
73
#[test]
74
1
fn a_form_keeps_string_quotes_at_any_depth() {
75
1
    assert_eq!(format_form(&text("5")), "\"5\"");
76
1
    let nested = Expr::Quote(Box::new(Expr::List(vec![
77
1
        Expr::Number(Number::Index(1.into())),
78
1
        Expr::List(vec![text("2")]),
79
1
    ])));
80
1
    assert_eq!(format_form(&nested), "'(1 (\"2\"))");
81
1
}
82

            
83
#[test]
84
1
fn a_form_escapes_quotes_and_backslashes() {
85
1
    assert_eq!(format_form(&text("a\"b\\c")), "\"a\\\"b\\\\c\"");
86
1
}
87

            
88
#[test]
89
1
fn a_form_prints_a_dotted_pair_with_quoted_strings() {
90
1
    let pair = Expr::cons(text("a"), text("b"));
91
1
    assert_eq!(format_form(&pair), "(\"a\" . \"b\")");
92
1
}
93

            
94
#[test]
95
1
fn a_form_quotes_strings_inside_lambda_defaults() {
96
1
    let params = crate::ast::LambdaParams {
97
1
        optional: vec![("X".into(), Some(text("d")))],
98
1
        ..crate::ast::LambdaParams::simple(vec![])
99
1
    };
100
1
    let lambda = Expr::Lambda(params, Box::new(sym("X")));
101
1
    assert_eq!(format_form(&lambda), "(LAMBDA (&optional (X \"d\")) X)");
102
1
    assert_eq!(format_expr(&lambda), "(LAMBDA (&optional (X d)) X)");
103
1
}
104

            
105
#[test]
106
1
fn princ_printing_leaves_strings_bare() {
107
1
    assert_eq!(format_expr(&text("a\"b")), "a\"b");
108
1
    assert_eq!(
109
1
        format_expr(&Expr::List(vec![
110
1
            Expr::Number(Number::Index(1.into())),
111
1
            text("2")
112
1
        ])),
113
1
        "(1 2)"
114
    );
115
1
}
116

            
117
#[test]
118
1
fn not_callable_quotes_a_string_head() {
119
1
    assert_eq!(
120
1
        not_callable(&text("5")).to_string(),
121
1
        "Compile error: not callable: \"5\""
122
    );
123
1
}
124

            
125
#[test]
126
1
fn a_labels_alias_prints_as_the_function_the_user_wrote() {
127
1
    let alias = Expr::List(vec![
128
1
        Expr::Symbol("M (LABELS 7)".to_string()),
129
1
        Expr::Symbol("Y".to_string()),
130
1
    ]);
131
1
    assert_eq!(format_form(&alias), "(M Y)");
132
1
}
133

            
134
#[test]
135
1
fn a_form_spells_a_renamed_key_parameter_with_its_keyword() {
136
1
    let mut params = crate::ast::LambdaParams::simple(vec![]);
137
1
    params.key = vec![
138
1
        KeyParam {
139
1
            name: "FMT".into(),
140
1
            keyword: "FORMAT".into(),
141
1
            default: Some(text("spec")),
142
1
        },
143
1
        KeyParam::new("W", None),
144
1
    ];
145
1
    let lambda = Expr::Lambda(params, Box::new(sym("FMT")));
146
1
    assert_eq!(
147
1
        format_form(&lambda),
148
1
        "(LAMBDA (&key ((:format FMT) \"spec\") W) FMT)"
149
    );
150
1
}
151

            
152
#[test]
153
1
fn a_whole_scalar_prints_with_its_denominator() {
154
1
    let whole = Expr::Number(Number::Scalar(2.into()));
155
1
    assert_eq!(format_expr(&whole), "2/1");
156
1
    assert_eq!(format_form(&whole), "2/1");
157
1
    assert_eq!(format_expr(&Expr::Number(Number::Index(2.into()))), "2");
158
1
}