1
// Skipped under Miri: these tests compile+run wasm via wasmtime, whose
2
// Cranelift backend refuses to run under Miri.
3
#![cfg(not(miri))]
4

            
5
//! A runtime list written as a script's result decodes element by element,
6
//! nested lists and mixed element types included, instead of a `t` marker.
7

            
8
use nms::interpreter::Interpreter;
9
use scripting::nomiscript::{Number, Value, format_value};
10

            
11
13
fn printed(src: &str) -> String {
12
13
    let mut interp = Interpreter::new(false).unwrap();
13
13
    let results = interp
14
13
        .eval(src)
15
        .unwrap_or_else(|e| panic!("eval {src:?}: {e}"));
16
13
    let value = results
17
13
        .into_iter()
18
13
        .next_back()
19
        .unwrap_or_else(|| panic!("eval {src:?} produced no value"));
20
13
    format_value(&value)
21
13
}
22

            
23
4
fn assert_prints(cases: &[(&str, &str)]) {
24
4
    for (src, expected) in cases {
25
13
        assert_eq!(printed(src), *expected, "{src}");
26
    }
27
4
}
28

            
29
#[test]
30
1
fn a_runtime_list_prints_its_elements() {
31
1
    assert_prints(&[
32
1
        ("(list (entity-count) 2)", "(0 2)"),
33
1
        ("(cons (entity-count) nil)", "(0)"),
34
1
        ("(cdr (list (entity-count) 2))", "(2)"),
35
1
        // A runtime test: `(if (entity-count) …)` would fold, because an
36
1
        // index is never nil, and so would not exercise a runtime list.
37
1
        ("(if (> (entity-count) 5) '(1 2) '(3 4))", "(3 4)"),
38
1
    ]);
39
1
}
40

            
41
#[test]
42
1
fn an_accumulated_local_prints_as_a_list() {
43
1
    assert_prints(&[
44
1
        ("(let ((acc nil)) (setf acc (cons 1 acc)) acc)", "(1)"),
45
1
        ("(let ((r (list 0))) (setf r (cons 1 r)) r)", "(1 0)"),
46
1
        ("(let ((acc nil)) (setf acc (cons 1 acc)))", "(1)"),
47
1
    ]);
48
1
}
49

            
50
#[test]
51
1
fn mixed_elements_print_by_their_runtime_shape() {
52
1
    assert_prints(&[
53
1
        (
54
1
            "(list \"a\" 1/2 t nil (entity-count))",
55
1
            "(\"a\" 1/2 t nil 0)",
56
1
        ),
57
1
        ("(cons 1/2 (cons (entity-count) nil))", "(1/2 0)"),
58
1
    ]);
59
1
}
60

            
61
#[test]
62
1
fn a_list_in_a_dynamic_cell_prints_nested() {
63
1
    assert_prints(&[
64
1
        (
65
1
            "(if (> (entity-count) 5) \"s\" (list (entity-count) 2))",
66
1
            "(0 2)",
67
1
        ),
68
1
        (
69
1
            "(list (if (> (entity-count) 5) \"s\" (list (entity-count) 2)) 3)",
70
1
            "((0 2) 3)",
71
1
        ),
72
1
        (
73
1
            "(list 1 (if (> (entity-count) 5) \"s\" (list (entity-count) (if (> (entity-count) 5) \"t\" (list 7)))) 3)",
74
1
            "(1 (0 (7)) 3)",
75
1
        ),
76
1
        ("(or (> (transaction-tag-count 0) 0) (list 1))", "(1)"),
77
1
    ]);
78
1
}
79

            
80
#[test]
81
1
fn an_empty_runtime_list_is_nil() {
82
1
    let mut interp = Interpreter::new(false).unwrap();
83
1
    let results = interp
84
1
        .eval("(cdr (list (entity-count)))")
85
        .unwrap_or_else(|e| panic!("{e}"));
86
1
    assert_eq!(results.last(), Some(&Value::Nil));
87
1
    let mut interp = Interpreter::new(false).unwrap();
88
1
    let results = interp
89
1
        .eval("(car (list (entity-count)))")
90
        .unwrap_or_else(|e| panic!("{e}"));
91
1
    assert_eq!(results.last(), Some(&Value::Number(Number::Index(0))));
92
1
}