1
//! HANDLER-CASE — try_table-based condition recovery. Compile + validate
2
//! surface; runtime evaluation (the catch actually firing and clauses
3
//! returning values) is covered in `nms/tests/handler_case_runtime.rs`.
4

            
5
use super::common::{compile_and_validate, compile_expect_error, wrap_with_runtime_i32};
6

            
7
#[test]
8
1
fn handler_case_single_clause_validates() {
9
1
    compile_and_validate(r#"(handler-case (error 'oops "x") (oops (e) 7))"#);
10
1
}
11

            
12
#[test]
13
1
fn handler_case_no_throw_body_validates() {
14
1
    compile_and_validate("(handler-case 42 (oops (e) 7))");
15
1
}
16

            
17
#[test]
18
1
fn handler_case_multi_clause_validates() {
19
1
    compile_and_validate(r#"(handler-case (error 'two "x") (one (e) 1) (two (e) 2))"#);
20
1
}
21

            
22
#[test]
23
1
fn handler_case_catch_all_validates() {
24
1
    compile_and_validate(r#"(handler-case (error 'whatever "x") (t (e) 99))"#);
25
1
}
26

            
27
#[test]
28
1
fn handler_case_unmatched_propagates_validates() {
29
    // No matching clause + no `t`: re-raises. Still a valid module.
30
1
    compile_and_validate(r#"(handler-case (error 'nope "x") (other (e) 1))"#);
31
1
}
32

            
33
#[test]
34
1
fn handler_case_error_code_accessor_validates() {
35
1
    compile_and_validate(r#"(handler-case (error 'x "m") (x (e) (error-code e)))"#);
36
1
}
37

            
38
#[test]
39
1
fn handler_case_error_message_accessor_validates() {
40
1
    compile_and_validate(r#"(handler-case (error 'x "m") (x (e) (error-message e)))"#);
41
1
}
42

            
43
#[test]
44
1
fn handler_case_no_clauses_validates() {
45
    // Zero clauses degenerates to "evaluate body; any raise propagates".
46
1
    compile_and_validate(r#"(handler-case (error 'x "m"))"#);
47
1
}
48

            
49
#[test]
50
1
fn handler_case_disagreeing_body_and_clause_widen() {
51
    // A Ratio body beside a StringRef clause joins as AnyRef.
52
1
    compile_and_validate(&wrap_with_runtime_i32(
53
1
        r#"(handler-case (if (= IDX 0) 11/10 12/10) (oops (e) "str"))"#,
54
1
    ));
55
1
}
56

            
57
#[test]
58
1
fn handler_case_catch_all_must_be_last() {
59
1
    let err = compile_expect_error(r#"(handler-case (error 'x "m") (t (e) 1) (x (e) 2))"#);
60
1
    assert!(
61
1
        err.contains("handler-case") && err.contains("must be last"),
62
        "expected a t-must-be-last error, got: {err}"
63
    );
64
1
}
65

            
66
#[test]
67
1
fn handler_case_clause_binding_must_be_single_var() {
68
1
    let err = compile_expect_error(r#"(handler-case (error 'x "m") (x (a b) 1))"#);
69
1
    assert!(
70
1
        err.contains("handler-case") && err.contains("single (var)"),
71
        "expected a clause-binding-shape error, got: {err}"
72
    );
73
1
}
74

            
75
#[test]
76
1
fn error_code_rejects_non_condition_arg() {
77
    // The accessor is typed: a Ratio is not an EntityRef(Condition).
78
1
    let err = compile_expect_error("(error-code 5)");
79
1
    assert!(
80
1
        err.contains("ERROR-CODE") || err.contains("condition"),
81
        "expected a type error for ERROR-CODE on a non-condition, got: {err}"
82
    );
83
1
}
84

            
85
#[test]
86
1
fn return_from_inside_handler_case_body_resolves() {
87
    // A `(return-from)` inside a handler-case body must resolve its relative
88
    // `br` target through the +3 wrapper frames the handler-case opens.
89
1
    compile_and_validate(r#"(block done (handler-case (return-from done 1) (x (e) 2)))"#);
90
1
}
91

            
92
/// COND reconciles all its arms at once, exactly like a two-arm IF, so a list
93
/// arm beside a nil arm types `pair<i32>`, which widens to AnyRef beside the
94
/// i32 handler in both spellings.
95
#[test]
96
1
fn a_list_or_nil_cond_widens_beside_an_i32_handler_like_the_if_equivalent() {
97
1
    compile_and_validate(
98
1
        "(handler-case (cond ((= (entity-count) 0) (list (entity-count))) \
99
1
         ((= (entity-count) 1) nil)) (boom (e) 55))",
100
1
    );
101
1
    compile_and_validate(
102
1
        "(handler-case (if (= (entity-count) 0) (list (entity-count)) nil) (boom (e) 55))",
103
1
    );
104
1
}