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
//! PERSIST rows: a struct accessor applied to a value that is not a struct
6
//! names the accessor and the operand's type, not the internal natives.
7

            
8
#[path = "persist/rows.rs"]
9
mod persist;
10

            
11
use nms::interpreter::Interpreter;
12
use persist::{Row, assert_pinned, row};
13
use scripting::nomiscript::{Number, Value};
14

            
15
const PT: &str = "(defstruct pt x)";
16

            
17
1
fn rows() -> Vec<Row> {
18
1
    vec![
19
1
        row("S1", &[PT, "(pt-x (entity-count))"]),
20
1
        row("S2", &[PT, "(setf (pt-x (entity-count)) 3)"]),
21
1
        row("S3", &[PT, "(pt-x 5)"]),
22
1
        row("S4", &[PT, "(let ((n (entity-count))) (pt-x n))"]),
23
1
        row("S5", &[PT, "(pt-x nil)"]),
24
1
        row("S6", &[PT, "(setf (pt-x 'a) 3)"]),
25
1
        row("S7", &[PT, "(pt-x (make-pt :x 1))"]),
26
1
        row(
27
1
            "S8",
28
1
            &[PT, "(let ((p (make-pt :x 1))) (setf (pt-x p) 3) (pt-x p))"],
29
1
        ),
30
    ]
31
1
}
32

            
33
const S1: &str = "err: Compile error: PT-X: expected a PT, got an i";
34
const S2: &str = "err: Compile error: (SETF PT-X): expected a PT, g";
35

            
36
/// CL: every refused row is an error at run time, S7 is 1 and S8 is 3. The
37
/// refusal is a compile error because the operand's type is known.
38
const CL: &[(&str, &[&str])] = &[
39
    ("S1", &["pt", S1]),
40
    ("S2", &["pt", S2]),
41
    ("S3", &["pt", S1]),
42
    ("S4", &["pt", S1]),
43
    (
44
        "S5",
45
        &["pt", "err: Compile error: PT-X: expected a PT, got a bo"],
46
    ),
47
    ("S6", &["pt", S2]),
48
    ("S7", &["pt", "1"]),
49
    ("S8", &["pt", "3"]),
50
];
51

            
52
#[test]
53
1
fn every_row_reads_as_common_lisp() {
54
1
    assert_pinned(&rows(), CL);
55
1
}
56

            
57
15
fn error_of(source: &str) -> String {
58
15
    let mut interp = Interpreter::new(false).expect("interpreter");
59
15
    interp.eval(PT).expect("defstruct");
60
15
    interp.eval(source).expect_err("refused").to_string()
61
15
}
62

            
63
#[test]
64
1
fn the_refusal_names_the_accessor_and_the_type() {
65
1
    assert_eq!(
66
1
        error_of("(pt-x (entity-count))"),
67
1
        "Compile error: PT-X: expected a PT, got an i32"
68
    );
69
1
    assert_eq!(
70
1
        error_of("(setf (pt-x (entity-count)) 3)"),
71
1
        "Compile error: (SETF PT-X): expected a PT, got an i32"
72
    );
73
1
}
74

            
75
#[test]
76
1
fn a_struct_of_another_layout_is_named_by_its_layout() {
77
1
    let mut interp = Interpreter::new(false).expect("interpreter");
78
1
    interp.eval(PT).expect("defstruct");
79
1
    interp.eval("(defstruct box p)").expect("defstruct");
80
1
    let err = interp.eval("(pt-x (make-box :p 1))").expect_err("refused");
81
1
    assert_eq!(
82
1
        err.to_string(),
83
1
        "Compile error: PT-X: expected a PT, got a BOX"
84
    );
85
1
}
86

            
87
/// CL: each of these is an error at run time.
88
#[test]
89
1
fn the_refusal_reads_the_same_on_every_path() {
90
1
    let plain = "Compile error: PT-X: expected a PT, got an i32";
91
1
    for source in [
92
1
        "(funcall (function pt-x) 5)",
93
1
        "(apply (function pt-x) (list 5))",
94
1
        "(mapcar (function pt-x) (list 5 6))",
95
1
        "(handler-case (pt-x 5) (error (e) 0))",
96
1
        "(pt-x 5)",
97
1
    ] {
98
5
        assert_eq!(error_of(source), plain, "{source}");
99
    }
100
1
}
101

            
102
#[test]
103
1
fn an_operand_that_fails_is_refused_by_its_own_error() {
104
1
    assert_eq!(
105
1
        error_of("(pt-x (car 5))"),
106
1
        "Compile error: CAR expects a list, got 5"
107
    );
108
1
    assert_eq!(
109
1
        error_of("(pt-x (make-pt :x (+ 1 \"a\")))"),
110
1
        "Compile error: + expects numeric arguments, got a string value"
111
    );
112
1
}
113

            
114
#[test]
115
1
fn an_operand_that_raises_raises_its_own_error() {
116
1
    assert_eq!(error_of("(pt-x (error 'boom \"bad\"))"), "BOOM: bad");
117
1
}
118

            
119
#[test]
120
1
fn a_runtime_field_is_held_by_the_instance() {
121
1
    let mut interp = Interpreter::new(false).expect("interpreter");
122
1
    interp.eval(PT).expect("defstruct");
123
1
    let values = interp
124
1
        .eval("(pt-x (make-pt :x (entity-count)))")
125
1
        .expect("reads the field");
126
1
    assert_eq!(values.last(), Some(&Value::Number(Number::Index(0))));
127
1
}
128

            
129
#[test]
130
1
fn an_accessor_takes_exactly_its_operand() {
131
1
    assert_eq!(
132
1
        error_of("(let ((p (make-pt :x 1))) (pt-x p p))"),
133
1
        "Arity error: lambda expects 1 arguments, got 2"
134
    );
135
1
}
136

            
137
#[test]
138
1
fn a_runtime_operand_is_named_by_its_type() {
139
1
    assert_eq!(
140
1
        error_of("(funcall (function pt-x) (entity-count))"),
141
1
        "Compile error: PT-X: expected a PT, got an i32"
142
    );
143
1
    assert_eq!(
144
1
        error_of("(pt-x (+ 1 (entity-count)))"),
145
1
        "Compile error: PT-X: expected a PT, got an i32"
146
    );
147
1
}
148

            
149
#[test]
150
1
fn a_struct_in_a_type_message_is_named_by_its_structure() {
151
1
    assert_eq!(
152
1
        error_of("(+ (make-pt :x 1) 1)"),
153
1
        "Compile error: + expects numeric arguments, got a PT value"
154
    );
155
    // A parameter has no home, so the store is refused; SBCL 5.
156
1
    assert_eq!(
157
1
        error_of("(defun f (p) (setf p 5) p) (f (make-pt :x 1))"),
158
1
        "Compile error: type mismatch: expected PT, got i32"
159
    );
160
1
}
161

            
162
#[test]
163
1
fn a_dolist_variable_over_structs_takes_a_store() {
164
1
    let mut interp = Interpreter::new(false).expect("interpreter");
165
1
    interp.eval(PT).expect("defstruct");
166
1
    let values = interp
167
1
        .eval("(dolist (p (list (make-pt :x 1))) (setf p 5))")
168
1
        .expect("stores");
169
1
    assert_eq!(values.last(), Some(&Value::Nil));
170
1
}
171

            
172
2
fn error_after(definition: &str, source: &str) -> String {
173
2
    let mut interp = Interpreter::new(false).expect("interpreter");
174
2
    interp.eval(PT).expect("defstruct");
175
2
    interp.eval(definition).expect("defstruct");
176
2
    interp.eval(source).expect_err("refused").to_string()
177
2
}
178

            
179
#[test]
180
1
fn the_article_before_a_struct_name_agrees_with_it() {
181
1
    assert_eq!(
182
1
        error_after("(defstruct apple x)", "(+ (make-apple :x 1) 1)"),
183
1
        "Compile error: + expects numeric arguments, got an APPLE value"
184
    );
185
1
    assert_eq!(
186
1
        error_after("(defstruct box y)", "(box-y (make-pt :x 1))"),
187
1
        "Compile error: BOX-Y: expected a BOX, got a PT"
188
    );
189
1
}
190

            
191
#[test]
192
1
fn a_repeated_slot_name_is_refused() {
193
1
    let mut interp = Interpreter::new(false).expect("interpreter");
194
1
    let err = interp.eval("(defstruct dup x x)").expect_err("refused");
195
1
    assert_eq!(
196
1
        err.to_string(),
197
1
        "Compile error: DEFSTRUCT DUP: duplicate slot name X"
198
    );
199
1
}