1
// Skipped under Miri: these tests run the nms binary, which compiles and runs
2
// wasm via wasmtime, whose Cranelift backend refuses to run under Miri.
3
#![cfg(not(miri))]
4

            
5
//! PERSIST rows: PRINT, DISPLAY and DEBUG show a struct instance as Common
6
//! Lisp prints it, however the argument reached the call.
7

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

            
11
use stdout::Run;
12

            
13
const SETUP: &[&str] = &[
14
    "(defstruct pt x)",
15
    "(defstruct box p)",
16
    "(defvar *p* (make-pt :x 1))",
17
    "(defvar *b* (make-box :p *p*))",
18
    "(defvar *m* (list 1 *p* \"s\"))",
19
];
20

            
21
17
fn session(units: &[&str]) -> Run {
22
17
    let all: Vec<&str> = SETUP.iter().chain(units).copied().collect();
23
17
    stdout::session(&all)
24
17
}
25

            
26
15
fn assert_logs(unit: &str, text: &str) {
27
15
    let run = session(&[unit]);
28
15
    assert!(
29
15
        run.stdout.contains(&format!("> {text}\n")),
30
        "{unit}: {}",
31
        run.stdout
32
    );
33
15
    assert!(!run.stdout.contains("<anyref>"), "{unit}: {}", run.stdout);
34
15
    assert!(!run.stdout.contains("#<instance"), "{unit}: {}", run.stdout);
35
15
}
36

            
37
#[test]
38
1
fn a_carried_struct_prints_as_common_lisp() {
39
1
    assert_logs("(print *p*)", "#S(PT :X 1)");
40
1
    assert_logs("(display *p*)", "#S(PT :X 1)");
41
1
    assert_logs("(debug *p*)", "#S(PT :X 1)");
42
1
}
43

            
44
#[test]
45
1
fn a_struct_read_out_of_a_carried_list_prints_as_common_lisp() {
46
1
    assert_logs("(print (car (cdr *m*)))", "#S(PT :X 1)");
47
1
    assert_logs("(display (car (cdr *m*)))", "#S(PT :X 1)");
48
1
    assert_logs("(debug (car (cdr *m*)))", "#S(PT :X 1)");
49
1
}
50

            
51
#[test]
52
1
fn a_struct_among_several_arguments_prints_in_place() {
53
1
    assert_logs("(print 1 *p* \"a\")", "1 #S(PT :X 1) a");
54
1
    assert_logs("(print 1 (car (cdr *m*)) \"a\")", "1 #S(PT :X 1) a");
55
1
}
56

            
57
#[test]
58
1
fn a_struct_printed_inside_a_handler_prints_as_common_lisp() {
59
1
    assert_logs(
60
1
        "(handler-case (print (car (cdr *m*))) (error (e) 0))",
61
1
        "#S(PT :X 1)",
62
1
    );
63
1
}
64

            
65
#[test]
66
1
fn a_let_bound_struct_prints_as_common_lisp() {
67
1
    assert_logs("(let ((q (car (cdr *m*)))) (print q))", "#S(PT :X 1)");
68
1
}
69

            
70
#[test]
71
1
fn a_struct_prints_the_value_a_same_unit_mutation_left() {
72
1
    assert_logs(
73
1
        "(progn (setf (pt-x *p*) 9) (print (car (cdr *m*))))",
74
1
        "#S(PT :X 9)",
75
1
    );
76
1
}
77

            
78
#[test]
79
1
fn a_nested_struct_prints_with_its_inner_struct() {
80
1
    assert_logs("(print *b*)", "#S(BOX :P #S(PT :X 1))");
81
1
    assert_logs("(print (box-p *b*))", "#S(PT :X 1)");
82
1
}
83

            
84
#[test]
85
1
fn a_struct_in_a_list_prints_inside_the_list() {
86
1
    assert_logs("(print (list *p* 2))", "(#S(PT :X 1) 2)");
87
1
    assert_logs("(print *m*)", "(1 #S(PT :X 1) s)");
88
1
}
89

            
90
#[test]
91
1
fn a_struct_holding_itself_prints_up_to_the_depth_placeholder() {
92
1
    let run = session(&[
93
1
        "(defstruct node next)",
94
1
        "(defvar *n* (make-node))",
95
1
        "(setf (node-next *n*) *n*)",
96
1
        "(print 7 *n*)",
97
1
    ]);
98
1
    assert!(!run.stderr.contains("not printable"), "{}", run.stderr);
99
1
    assert!(
100
1
        run.stdout.contains("> 7 #S(NODE :NEXT #S(NODE"),
101
        "{}",
102
        run.stdout
103
    );
104
1
    assert!(run.stdout.contains("#<...>"), "{}", run.stdout);
105
1
}
106

            
107
#[test]
108
1
fn a_chain_past_the_depth_cap_prints_the_placeholder() {
109
1
    let run = session(&[
110
1
        "(defvar *k* 0)",
111
1
        "(defun chain (n) (if (= n 0) (make-pt :x 0) (make-pt :x (chain (- n 1)))))",
112
1
        "(defvar *q* (progn (setf *k* 70) (chain *k*)))",
113
1
        "(print *q*)",
114
1
    ]);
115
1
    assert!(!run.stderr.contains("not printable"), "{}", run.stderr);
116
1
    assert!(run.stdout.contains("#<...>"), "{}", run.stdout);
117
1
}