1
use super::{SCRATCH_SIZE, scratch_pieces};
2

            
3
#[test]
4
1
fn a_short_message_is_one_piece() {
5
1
    assert_eq!(scratch_pieces("hello"), vec!["hello"]);
6
1
}
7

            
8
#[test]
9
1
fn an_empty_message_still_reaches_the_host() {
10
1
    assert_eq!(scratch_pieces(""), vec![""]);
11
1
}
12

            
13
#[test]
14
1
fn a_long_message_is_staged_in_pieces_that_fit() {
15
1
    let text = "A".repeat(2 * SCRATCH_SIZE + 104);
16
1
    let pieces = scratch_pieces(&text);
17
1
    assert_eq!(
18
3
        pieces.iter().map(|p| p.len()).collect::<Vec<_>>(),
19
1
        vec![SCRATCH_SIZE, SCRATCH_SIZE, 104]
20
    );
21
1
    assert_eq!(pieces.concat(), text);
22
1
}
23

            
24
#[test]
25
1
fn a_piece_never_splits_a_character() {
26
1
    let text = format!("{}é{}", "a".repeat(SCRATCH_SIZE - 1), "b".repeat(10));
27
1
    let pieces = scratch_pieces(&text);
28
1
    assert_eq!(pieces[0].len(), SCRATCH_SIZE - 1);
29
2
    assert!(pieces.iter().all(|p| p.len() <= SCRATCH_SIZE));
30
1
    assert_eq!(pieces.concat(), text);
31
1
}
32

            
33
19
fn compile_eval_error(src: &str) -> String {
34
19
    let program = crate::reader::Reader::parse(src).expect("source parses");
35
19
    let mut compiler = crate::compiler::Compiler::new();
36
19
    let mut symbols = crate::runtime::SymbolTable::with_builtins();
37
19
    match compiler.compile_eval_with_type(&program, &mut symbols) {
38
18
        Ok(_) => String::new(),
39
1
        Err(err) => err.to_string(),
40
    }
41
19
}
42

            
43
#[test]
44
1
fn printing_a_closure_compiles_on_every_surface() {
45
1
    for name in ["print", "display", "debug"] {
46
3
        assert_eq!(
47
3
            compile_eval_error(&format!("(let ((f (lambda (x) x))) ({name} f))")),
48
3
            ""
49
        );
50
    }
51
1
}
52

            
53
#[test]
54
1
fn printing_a_condition_compiles_on_every_surface() {
55
1
    for name in ["print", "display", "debug"] {
56
3
        assert_eq!(
57
3
            compile_eval_error(&format!(
58
3
                "({name} (handler-case (error 'boom \"m\") (t (e) e)))"
59
3
            )),
60
3
            ""
61
        );
62
    }
63
1
}
64

            
65
#[test]
66
1
fn printing_a_folded_closure_list_compiles() {
67
1
    assert_eq!(
68
1
        compile_eval_error("(let ((n 0)) (print (list (lambda () (setf n (+ n 1)) n))))"),
69
1
        ""
70
    );
71
1
}
72

            
73
#[test]
74
1
fn printing_a_runtime_value_compiles() {
75
1
    assert_eq!(
76
1
        compile_eval_error("(defvar g 0) (setf g 3) (print \"n=\" g)"),
77
1
        ""
78
    );
79
1
}
80

            
81
#[test]
82
1
fn printing_a_folded_bare_lambda_compiles() {
83
1
    for source in [
84
1
        "(print (lambda (x) x))",
85
1
        "(print 1 (lambda (x) x))",
86
1
        "(print (if t (lambda (x) x) nil))",
87
1
        "(let ((f (print (lambda (x) (+ x 1))))) (funcall f 2))",
88
1
        "(defvar *h* (print (lambda (x) (+ x 1))))",
89
1
        "(defparameter *f* (print (lambda (x) (+ x 1))))",
90
1
        "(defun mk () (print (lambda (x) (+ x 1)))) (defvar *k* (mk))",
91
1
        "(defvar *g* (lambda (x) x)) (defvar *h* (print (lambda (x y) y)))",
92
1
        "(length (list (print (lambda (x) x))))",
93
1
    ] {
94
9
        assert_eq!(compile_eval_error(source), "", "{source}");
95
    }
96
1
}
97

            
98
#[test]
99
1
fn a_printed_constant_list_beyond_the_index_range_is_a_static_error() {
100
1
    let message = compile_eval_error("(print (list (+ 1073741823 1) 1))");
101
1
    assert!(message.contains("outside the index range"), "{message}");
102
1
    assert_eq!(compile_eval_error("(print (quote (1073741823 1)))"), "");
103
1
}