Lines
100 %
Functions
Branches
//! Acceptance rows tagged phase 4 in `doc/ops/0010-common-lisp-booleans.org`:
//! `if` reconciling a `Never` runtime test to always-true, `and` / `or`
//! returning values, a bodyless `cond` clause, `filter` accepting a
//! nil-or-value predicate result, and `catch-each` cells staying two
//! elements. `v` stands for `(rpc-protocol-version)`, which is 1.
use super::*;
#[tokio::test]
async fn a_never_typed_runtime_test_always_folds_true() {
assert_values(&[
("(if 0 1 2)", "1"),
("(if (* 0 (rpc-protocol-version)) 1 2)", "1"),
])
.await;
}
async fn null_of_a_count_is_nil() {
assert_values(&[("(null? 0)", "nil")]).await;
async fn when_false_and_unless_true_are_nil() {
("(when (> (rpc-protocol-version) 5) 1)", "nil"),
("(unless t 1)", "nil"),
async fn a_false_if_beside_a_list_then_branch_is_nil() {
assert_values(&[(
"(if (> (rpc-protocol-version) 5) (list (rpc-protocol-version)))",
"nil",
)])
async fn arithmetic_over_a_nil_arm_is_a_compile_error() {
assert_error_code(
"(+ (if (> (rpc-protocol-version) 5) (rpc-protocol-version)) 1)",
"compile",
)
assert_error_code("(+ (if (> (rpc-protocol-version) 5) 1/2) 1)", "compile").await;
async fn and_or_identities_and_short_circuit_values() {
("(and)", "t"),
("(or)", "nil"),
("(or nil nil)", "nil"),
("(and 1 2)", "2"),
("(or nil 5)", "5"),
async fn and_or_return_the_deciding_operand_value() {
(r#"(and (> (rpc-protocol-version) 0) "x")"#, "\"x\""),
("(and (or nil t) (and t (> (rpc-protocol-version) 0)))", "t"),
async fn and_or_never_evaluate_past_a_decided_result() {
// The first operand decides the result, so the `(error ...)` operand must
// never run — a bug here would surface as an `:error` frame instead of
// the expected value.
("(and (> (rpc-protocol-version) 5) (error 'x \"m\"))", "nil"),
("(or (> (rpc-protocol-version) 0) (error 'x \"m\"))", "t"),
async fn bodyless_cond_clause_returns_its_test_value_evaluated_once() {
assert_values(&[("(cond ((> (rpc-protocol-version) 0)))", "t")]).await;
async fn bodyless_cond_capture_hygiene() {
// A bodyless-clause capture must not be a reader-reachable symbol name —
// `%COND-TEST%` was: this form referenced it and silently compiled by
// capturing the bound test value instead of raising "Undefined symbol".
let response = eval_value_of("(cond ((> (rpc-protocol-version) 5)) (t %COND-TEST%))").await;
assert!(
response.contains(":code compile") && response.to_lowercase().contains("undefined"),
"expected an undefined-symbol compile error, got: {response}"
);
async fn or_folds_a_constant_prefix_like_the_eval_mirror() {
(r#"(or "x" 1)"#, "\"x\""),
(r#"(or (list 1) 2)"#, "(1)"),
(r#"(or nil "x" 1)"#, "\"x\""),
async fn or_runs_each_runtime_operand_at_most_once() {
let form = "(let ((c 0)) (or (progn (setf c (+ c 1)) nil) c))";
assert_values(&[(form, "1")]).await;
async fn not_of_a_runtime_pair_result_is_a_bool() {
let response = eval_value_of("(not (cons 1 nil))").await;
response.contains(":value nil"),
"expected `(not <non-nil pair>)` to be nil, got: {response}"
async fn filter_keeps_a_string_or_nil_predicate_result() {
r#"(filter (lambda (s) (if (string= s "a") s)) (list "a" "b"))"#,
"(\"a\")",
async fn catch_each_ok_cell_is_always_two_elements() {
(
"(null? (car (cdr (car (catch-each (list 1 2) x (> x 1))))))",
"t",
),
"(eq? t (car (cdr (car (cdr (catch-each (list 1 2) x (> x 1)))))))",
), // (ok . (t)) is the 2nd cell; its cdr's car is t
("(length (car (catch-each (list 1 2) x (> x 1))))", "2"),
async fn mapcar_over_a_catch_each_ok_cell_sees_the_nil_value() {
"(mapcar (lambda (x) (null? x)) (car (catch-each (list 1) x (> x 1))))",
"(nil t)",
async fn car_of_a_non_list_anyref_raises_a_catchable_type_error() {
r#"(handler-case (car (if (> (rpc-protocol-version) 0) 1 "a")) (type-error (e) 7))"#,
"7",
async fn length_of_a_nil_widened_if_result_is_zero() {
assert_values(&[("(length (if (> (rpc-protocol-version) 5) (list 1)))", "0")]).await;
async fn length_of_a_non_list_anyref_raises_a_catchable_type_error() {
"(handler-case (length (if (> (rpc-protocol-version) 0) 1)) (type-error (e) 7))",
/// Plan 0011 row R2: `AND`'s `Void`-flow step (`PRINT` has no value) is
/// `nil` in value position, not skipped — the enclosing `PROGN` still
/// returns the next form's value, and the print runs exactly once (the
/// chain's join constraint reuses the same lowered `PRINT` node rather than
/// re-elaborating its effect and its value separately).
async fn and_over_a_void_print_step_logs_once_and_returns_the_next_form() {
let form = "(progn (and (> (rpc-protocol-version) 0) (print \"x\")) 1)";
let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
let outcome = session.handle_request(form).await;
assert_eq!(
outcome.output.matches('x').count(),
1,
"expected \"x\" logged exactly once, got: {:?}",
outcome.output