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: a printed value nests as deep as a result does, and the
6
//! depth placeholder replaces the first level past `MAX_WIRE_LIST_DEPTH`.
7

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

            
11
use stdout::session;
12

            
13
const CAP: usize = 62;
14

            
15
/// The `(print 7 ...)` log line, which the REPL's own value echo cannot be
16
/// mistaken for.
17
7
fn logged_line(units: &[&str]) -> String {
18
7
    let run = session(units);
19
7
    assert!(!run.stderr.contains("panicked"), "{}", run.stderr);
20
7
    run.stdout
21
7
        .lines()
22
37
        .find(|line| line.starts_with("> 7 "))
23
        .unwrap_or_default()
24
        .to_string()
25
7
}
26

            
27
7
fn depth_counts(line: &str, opener: &str) -> String {
28
7
    format!(
29
        "{}/{}",
30
7
        line.matches(opener).count(),
31
7
        line.matches("#<...>").count()
32
    )
33
7
}
34

            
35
3
fn struct_chain(levels: usize) -> String {
36
3
    let line = logged_line(&[
37
3
        "(defstruct pt x)",
38
3
        "(defvar *k* 0)",
39
3
        "(defun chain (n) (if (= n 0) (make-pt :x 0) (make-pt :x (chain (- n 1)))))",
40
3
        &format!("(defvar *q* (progn (setf *k* {}) (chain *k*)))", levels - 1),
41
3
        "(print 7 *q*)",
42
3
    ]);
43
3
    depth_counts(&line, "#S(PT")
44
3
}
45

            
46
3
fn list_nest(levels: usize) -> String {
47
3
    let steps = vec!["1"; levels].join(" ");
48
3
    let line = logged_line(&[&format!(
49
3
        "(print 7 (let ((l nil)) (dolist (i '({steps})) (setf l (list l))) l))"
50
3
    )]);
51
3
    depth_counts(&line, "(")
52
3
}
53

            
54
#[test]
55
1
fn a_struct_chain_prints_every_level_to_the_cap() {
56
1
    assert_eq!(struct_chain(CAP - 1), format!("{}/0", CAP - 1));
57
1
    assert_eq!(struct_chain(CAP), format!("{CAP}/0"));
58
1
}
59

            
60
#[test]
61
1
fn a_struct_chain_one_past_the_cap_ends_in_the_placeholder() {
62
1
    assert_eq!(struct_chain(CAP + 1), format!("{CAP}/1"));
63
1
}
64

            
65
#[test]
66
1
fn a_list_prints_every_level_to_the_cap() {
67
1
    assert_eq!(list_nest(CAP - 1), format!("{}/0", CAP - 1));
68
1
    assert_eq!(list_nest(CAP), format!("{CAP}/0"));
69
1
}
70

            
71
#[test]
72
1
fn a_list_one_past_the_cap_ends_in_the_placeholder() {
73
1
    assert_eq!(list_nest(CAP + 1), format!("{CAP}/1"));
74
1
}
75

            
76
#[test]
77
1
fn a_cyclic_struct_prints_to_the_cap_then_the_placeholder() {
78
1
    let line = logged_line(&[
79
1
        "(defstruct node next)",
80
1
        "(defvar *n* (make-node))",
81
1
        "(setf (node-next *n*) *n*)",
82
1
        "(print 7 *n*)",
83
1
    ]);
84
1
    assert_eq!(depth_counts(&line, "#S(NODE"), format!("{CAP}/1"), "{line}");
85
1
}