1
//! Acceptance rows tagged phase 4 in `doc/ops/0010-common-lisp-booleans.org`:
2
//! `if` reconciling a `Never` runtime test to always-true, `and` / `or`
3
//! returning values, a bodyless `cond` clause, `filter` accepting a
4
//! nil-or-value predicate result, and `catch-each` cells staying two
5
//! elements. `v` stands for `(rpc-protocol-version)`, which is 1.
6

            
7
use super::*;
8

            
9
#[tokio::test]
10
1
async fn a_never_typed_runtime_test_always_folds_true() {
11
1
    assert_values(&[
12
1
        ("(if 0 1 2)", "1"),
13
1
        ("(if (* 0 (rpc-protocol-version)) 1 2)", "1"),
14
1
    ])
15
1
    .await;
16
1
}
17

            
18
#[tokio::test]
19
1
async fn null_of_a_count_is_nil() {
20
1
    assert_values(&[("(null? 0)", "nil")]).await;
21
1
}
22

            
23
#[tokio::test]
24
1
async fn when_false_and_unless_true_are_nil() {
25
1
    assert_values(&[
26
1
        ("(when (> (rpc-protocol-version) 5) 1)", "nil"),
27
1
        ("(unless t 1)", "nil"),
28
1
    ])
29
1
    .await;
30
1
}
31

            
32
#[tokio::test]
33
1
async fn a_false_if_beside_a_list_then_branch_is_nil() {
34
1
    assert_values(&[(
35
1
        "(if (> (rpc-protocol-version) 5) (list (rpc-protocol-version)))",
36
1
        "nil",
37
1
    )])
38
1
    .await;
39
1
}
40

            
41
#[tokio::test]
42
1
async fn arithmetic_over_a_nil_arm_is_a_compile_error() {
43
1
    assert_error_code(
44
1
        "(+ (if (> (rpc-protocol-version) 5) (rpc-protocol-version)) 1)",
45
1
        "compile",
46
1
    )
47
1
    .await;
48
1
    assert_error_code("(+ (if (> (rpc-protocol-version) 5) 1/2) 1)", "compile").await;
49
1
}
50

            
51
#[tokio::test]
52
1
async fn and_or_identities_and_short_circuit_values() {
53
1
    assert_values(&[
54
1
        ("(and)", "t"),
55
1
        ("(or)", "nil"),
56
1
        ("(or nil nil)", "nil"),
57
1
        ("(and 1 2)", "2"),
58
1
        ("(or nil 5)", "5"),
59
1
    ])
60
1
    .await;
61
1
}
62

            
63
#[tokio::test]
64
1
async fn and_or_return_the_deciding_operand_value() {
65
1
    assert_values(&[
66
1
        (r#"(and (> (rpc-protocol-version) 0) "x")"#, "\"x\""),
67
1
        ("(and (or nil t) (and t (> (rpc-protocol-version) 0)))", "t"),
68
1
    ])
69
1
    .await;
70
1
}
71

            
72
#[tokio::test]
73
1
async fn and_or_never_evaluate_past_a_decided_result() {
74
    // The first operand decides the result, so the `(error ...)` operand must
75
    // never run — a bug here would surface as an `:error` frame instead of
76
    // the expected value.
77
1
    assert_values(&[
78
1
        ("(and (> (rpc-protocol-version) 5) (error 'x \"m\"))", "nil"),
79
1
        ("(or (> (rpc-protocol-version) 0) (error 'x \"m\"))", "t"),
80
1
    ])
81
1
    .await;
82
1
}
83

            
84
#[tokio::test]
85
1
async fn bodyless_cond_clause_returns_its_test_value_evaluated_once() {
86
1
    assert_values(&[("(cond ((> (rpc-protocol-version) 0)))", "t")]).await;
87
1
}
88

            
89
#[tokio::test]
90
1
async fn bodyless_cond_capture_hygiene() {
91
    // A bodyless-clause capture must not be a reader-reachable symbol name —
92
    // `%COND-TEST%` was: this form referenced it and silently compiled by
93
    // capturing the bound test value instead of raising "Undefined symbol".
94
1
    let response = eval_value_of("(cond ((> (rpc-protocol-version) 5)) (t %COND-TEST%))").await;
95
1
    assert!(
96
1
        response.contains(":code compile") && response.to_lowercase().contains("undefined"),
97
1
        "expected an undefined-symbol compile error, got: {response}"
98
1
    );
99
1
}
100

            
101
#[tokio::test]
102
1
async fn or_folds_a_constant_prefix_like_the_eval_mirror() {
103
1
    assert_values(&[
104
1
        (r#"(or "x" 1)"#, "\"x\""),
105
1
        (r#"(or (list 1) 2)"#, "(1)"),
106
1
        (r#"(or nil "x" 1)"#, "\"x\""),
107
1
    ])
108
1
    .await;
109
1
}
110

            
111
#[tokio::test]
112
1
async fn or_runs_each_runtime_operand_at_most_once() {
113
1
    let form = "(let ((c 0)) (or (progn (setf c (+ c 1)) nil) c))";
114
1
    assert_values(&[(form, "1")]).await;
115
1
}
116

            
117
#[tokio::test]
118
1
async fn not_of_a_runtime_pair_result_is_a_bool() {
119
1
    let response = eval_value_of("(not (cons 1 nil))").await;
120
1
    assert!(
121
1
        response.contains(":value nil"),
122
1
        "expected `(not <non-nil pair>)` to be nil, got: {response}"
123
1
    );
124
1
}
125

            
126
#[tokio::test]
127
1
async fn filter_keeps_a_string_or_nil_predicate_result() {
128
1
    assert_values(&[(
129
1
        r#"(filter (lambda (s) (if (string= s "a") s)) (list "a" "b"))"#,
130
1
        "(\"a\")",
131
1
    )])
132
1
    .await;
133
1
}
134

            
135
#[tokio::test]
136
1
async fn catch_each_ok_cell_is_always_two_elements() {
137
1
    assert_values(&[
138
1
        (
139
1
            "(null? (car (cdr (car (catch-each (list 1 2) x (> x 1))))))",
140
1
            "t",
141
1
        ),
142
1
        (
143
1
            "(eq? t (car (cdr (car (cdr (catch-each (list 1 2) x (> x 1)))))))",
144
1
            "t",
145
1
        ), // (ok . (t)) is the 2nd cell; its cdr's car is t
146
1
        ("(length (car (catch-each (list 1 2) x (> x 1))))", "2"),
147
1
    ])
148
1
    .await;
149
1
}
150

            
151
#[tokio::test]
152
1
async fn mapcar_over_a_catch_each_ok_cell_sees_the_nil_value() {
153
1
    assert_values(&[(
154
1
        "(mapcar (lambda (x) (null? x)) (car (catch-each (list 1) x (> x 1))))",
155
1
        "(nil t)",
156
1
    )])
157
1
    .await;
158
1
}
159

            
160
#[tokio::test]
161
1
async fn car_of_a_non_list_anyref_raises_a_catchable_type_error() {
162
1
    assert_values(&[(
163
1
        r#"(handler-case (car (if (> (rpc-protocol-version) 0) 1 "a")) (type-error (e) 7))"#,
164
1
        "7",
165
1
    )])
166
1
    .await;
167
1
}
168

            
169
#[tokio::test]
170
1
async fn length_of_a_nil_widened_if_result_is_zero() {
171
1
    assert_values(&[("(length (if (> (rpc-protocol-version) 5) (list 1)))", "0")]).await;
172
1
}
173

            
174
#[tokio::test]
175
1
async fn length_of_a_non_list_anyref_raises_a_catchable_type_error() {
176
1
    assert_values(&[(
177
1
        "(handler-case (length (if (> (rpc-protocol-version) 0) 1)) (type-error (e) 7))",
178
1
        "7",
179
1
    )])
180
1
    .await;
181
1
}
182

            
183
/// Plan 0011 row R2: `AND`'s `Void`-flow step (`PRINT` has no value) is
184
/// `nil` in value position, not skipped — the enclosing `PROGN` still
185
/// returns the next form's value, and the print runs exactly once (the
186
/// chain's join constraint reuses the same lowered `PRINT` node rather than
187
/// re-elaborating its effect and its value separately).
188
#[tokio::test]
189
1
async fn and_over_a_void_print_step_logs_once_and_returns_the_next_form() {
190
1
    let form = "(progn (and (> (rpc-protocol-version) 0) (print \"x\")) 1)";
191
1
    assert_values(&[(form, "1")]).await;
192
1
    let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
193
1
    let outcome = session.handle_request(form).await;
194
1
    assert_eq!(
195
1
        outcome.output.matches('x').count(),
196
1
        1,
197
1
        "expected \"x\" logged exactly once, got: {:?}",
198
1
        outcome.output
199
1
    );
200
1
}