1
//! IF / COND / AND / OR / PROGN consumed in value position (not just
2
//! effect position). Each is wrapped in arithmetic or comparison so
3
//! `compile_*_for_stack` fires and the value lands on the wasm stack.
4

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

            
7
#[test]
8
1
fn if_for_stack_ratio_result() {
9
    // Both branches return X (runtime Ratio) so the outer `+` sees a
10
    // Ratio. Locks in the `compile_if_for_stack` path for ratio-valued
11
    // IFs.
12
1
    compile_and_validate(&wrap_with_runtime_ratio("(+ (if (= X 0/1) X X) X)"));
13
1
}
14

            
15
#[test]
16
1
fn if_numeric_literal_branch_coerces_to_ratio() {
17
    // ADR-0028: an integer-literal IF arm coerces to the other arm's numeric
18
    // type (Scalar here) instead of widening to AnyRef. `(if (= X 0) 1 X)` is a
19
    // Ratio IF, so the outer `+ … X` composes. This is the mechanism that lets
20
    // a recursive `(if base 1 (* n (fact …)))` keep a consistent Ratio result.
21
1
    compile_and_validate(&wrap_with_runtime_ratio("(+ (if (= X 0/1) 1 X) X)"));
22
1
}
23

            
24
#[test]
25
1
fn an_index_literal_test_beside_a_runtime_scalar_is_a_mix() {
26
1
    let err = compile_expect_error(&wrap_with_runtime_ratio("(+ (if (= X 0) 1/1 X) X)"));
27
1
    assert!(
28
1
        err.contains("= cannot mix an index (count) with a scalar"),
29
        "{err}"
30
    );
31
1
}
32

            
33
#[test]
34
1
fn if_for_stack_no_else_branch() {
35
    // No-else IF in value position; the absent branch defaults to nil
36
    // and the codegen path emits a typed-result block. Consumed as the
37
    // last program form so it lands on the value stack.
38
1
    compile_and_validate(&wrap_with_runtime_ratio("(if (= X 0/1) (+ X 1/1))"));
39
1
}
40

            
41
#[test]
42
1
fn runtime_if_with_string_branches_let_bound() {
43
    // A runtime-test IF whose branches are StringRef literals, bound in a
44
    // LET*. The binder sizes its local from the eval-time type, which must
45
    // match the StringRef the codegen actually pushes — previously eval
46
    // collapsed both-literal branches to I32, mis-sizing the local and
47
    // producing an invalid module.
48
1
    compile_and_validate(&wrap_with_runtime_ratio(
49
1
        r#"(let* ((s (if (= X 0/1) "a" "b"))) s)"#,
50
1
    ));
51
1
}
52

            
53
#[test]
54
1
fn runtime_if_with_ratio_branches_let_bound() {
55
1
    compile_and_validate(&wrap_with_runtime_ratio(
56
1
        "(let* ((r (if (= X 0/1) 11/10 12/10))) r)",
57
1
    ));
58
1
}
59

            
60
#[test]
61
1
fn cond_runtime_at_effect_position() {
62
    // COND at effect position routes through `compile_cond_for_effect`.
63
    // The cond is a prefix form; the program's last form is X so the
64
    // overall return is Ratio.
65
1
    compile_and_validate(&wrap_with_runtime_ratio(
66
1
        "(cond ((= X 0/1) (debug \"zero\")) ((> X 0/1) (debug \"positive\"))) X",
67
1
    ));
68
1
}
69

            
70
#[test]
71
1
fn and_for_stack() {
72
1
    compile_and_validate(&wrap_with_runtime_ratio(
73
1
        "(if (and (> X 0/1) (< X 10/1)) 1 0)",
74
1
    ));
75
1
}
76

            
77
#[test]
78
1
fn or_for_stack() {
79
1
    compile_and_validate(&wrap_with_runtime_ratio(
80
1
        "(if (or (= X 0/1) (= X 1/1)) 1 0)",
81
1
    ));
82
1
}
83

            
84
#[test]
85
1
fn progn_for_stack() {
86
    // `progn` at value position returns the last form's value.
87
1
    compile_and_validate(&wrap_with_runtime_ratio(
88
1
        "(+ (progn (debug \"step\") X) 1/1)",
89
1
    ));
90
1
}
91

            
92
#[test]
93
1
fn quote_compiles_for_value_position() {
94
1
    compile_and_validate("'a");
95
1
}
96

            
97
#[test]
98
1
fn quote_constant_list_compiles() {
99
1
    compile_and_validate("'(1 2 3)");
100
1
}