1
use super::{
2
    Arm, NEVER_RETURNS, NilRepr, literal_fits, nil_repr, reconcile_arms, reconcile_arms_expecting,
3
    rides,
4
};
5
use crate::ast::{PairElement, StructId, WasmType};
6

            
7
#[test]
8
1
fn a_literal_adopts_a_scalar_value() {
9
1
    let arms = [
10
1
        Arm::Value(WasmType::Ratio),
11
1
        Arm::NumberLiteral(WasmType::I32),
12
1
    ];
13
1
    assert_eq!(reconcile_arms(&arms), WasmType::Ratio);
14
1
}
15

            
16
#[test]
17
1
fn a_fractional_literal_beside_an_index_value_widens() {
18
1
    let arms = [
19
1
        Arm::Value(WasmType::I32),
20
1
        Arm::NumberLiteral(WasmType::Ratio),
21
1
    ];
22
1
    assert_eq!(reconcile_arms(&arms), WasmType::AnyRef);
23
1
}
24

            
25
#[test]
26
1
fn a_nil_beside_an_index_widens() {
27
1
    assert_eq!(
28
1
        reconcile_arms(&[Arm::Value(WasmType::I32), Arm::Nil]),
29
1
        WasmType::AnyRef
30
    );
31
1
    assert_eq!(
32
1
        reconcile_arms(&[Arm::NumberLiteral(WasmType::I32), Arm::Nil]),
33
1
        WasmType::AnyRef
34
    );
35
1
}
36

            
37
#[test]
38
1
fn a_nil_beside_a_list_is_the_null_list() {
39
1
    let pair = WasmType::PairRef(PairElement::I32);
40
1
    assert_eq!(reconcile_arms(&[Arm::Value(pair), Arm::Nil]), pair);
41
1
    assert_eq!(
42
1
        reconcile_arms(&[Arm::Nil, Arm::ConstantList(PairElement::I32)]),
43
1
        pair
44
    );
45
1
}
46

            
47
#[test]
48
1
fn pair_elements_widen_and_other_types_box_to_anyref() {
49
1
    let pairs = [
50
1
        Arm::ConstantList(PairElement::I32),
51
1
        Arm::Value(WasmType::PairRef(PairElement::StringRef)),
52
1
    ];
53
1
    assert_eq!(
54
1
        reconcile_arms(&pairs),
55
1
        WasmType::PairRef(PairElement::AnyRef)
56
    );
57
1
    assert_eq!(
58
1
        reconcile_arms(&[Arm::Value(WasmType::I32), Arm::Value(WasmType::StringRef)]),
59
1
        WasmType::AnyRef
60
    );
61
1
    assert_eq!(
62
1
        reconcile_arms(&[
63
1
            Arm::Value(WasmType::StringRef),
64
1
            Arm::NumberLiteral(WasmType::I32)
65
1
        ]),
66
1
        WasmType::AnyRef
67
    );
68
1
}
69

            
70
#[test]
71
1
fn no_reachable_arm_never_returns_and_a_lone_nil_is_bool() {
72
1
    assert_eq!(reconcile_arms(&[]), NEVER_RETURNS);
73
1
    assert_eq!(reconcile_arms(&[Arm::Nil]), WasmType::Bool);
74
1
}
75

            
76
#[test]
77
1
fn only_an_i32_repr_value_needs_a_box_to_stand_as_anyref() {
78
1
    let pair = WasmType::PairRef(PairElement::I32);
79
1
    assert!(rides(WasmType::Ratio, WasmType::Ratio));
80
1
    assert!(rides(pair, WasmType::PairRef(PairElement::AnyRef)));
81
1
    assert!(rides(WasmType::StringRef, WasmType::AnyRef));
82
1
    assert!(rides(WasmType::Ratio, WasmType::AnyRef));
83
1
    assert!(!rides(WasmType::I32, WasmType::AnyRef));
84
1
    assert!(!rides(WasmType::Bool, WasmType::AnyRef));
85
1
    assert!(!rides(WasmType::Bool, pair));
86
1
    assert!(!rides(WasmType::I32, WasmType::Ratio));
87
1
    assert!(!rides(WasmType::StringRef, pair));
88
1
}
89

            
90
#[test]
91
1
fn a_structure_is_nullable_and_widens_against_anything_else() {
92
1
    let (pt, other) = (
93
1
        WasmType::StructRef(StructId(0)),
94
1
        WasmType::StructRef(StructId(1)),
95
1
    );
96
1
    assert_eq!(nil_repr(&pt), NilRepr::Null);
97
1
    assert_eq!(reconcile_arms(&[Arm::Value(pt), Arm::Nil]), pt);
98
1
    assert_eq!(reconcile_arms(&[Arm::Value(pt), Arm::Value(pt)]), pt);
99
1
    assert_eq!(
100
1
        reconcile_arms(&[Arm::Value(pt), Arm::Value(other)]),
101
1
        WasmType::AnyRef
102
    );
103
1
    assert_eq!(
104
1
        reconcile_arms(&[Arm::Value(pt), Arm::Value(WasmType::I32)]),
105
1
        WasmType::AnyRef
106
    );
107
1
    assert!(rides(pt, WasmType::AnyRef));
108
1
    assert!(!rides(pt, other));
109
1
}
110

            
111
#[test]
112
1
fn nil_repr_and_literal_fitting() {
113
1
    assert_eq!(nil_repr(&WasmType::StringRef), NilRepr::Null);
114
1
    assert_eq!(nil_repr(&WasmType::Bool), NilRepr::I32Zero);
115
1
    assert_eq!(nil_repr(&WasmType::I32), NilRepr::Never);
116
1
    assert!(literal_fits(WasmType::Ratio, WasmType::I32));
117
1
    assert!(!literal_fits(WasmType::I32, WasmType::Ratio));
118
1
    assert!(!literal_fits(WasmType::Commodity, WasmType::I32));
119
1
}
120

            
121
#[test]
122
1
fn arms_order_canonically() {
123
1
    let mut arms = [
124
1
        Arm::Nil,
125
1
        Arm::NumberLiteral(WasmType::Ratio),
126
1
        Arm::Value(WasmType::I32),
127
1
    ];
128
1
    arms.sort();
129
1
    assert_eq!(arms[0], Arm::Value(WasmType::I32));
130
1
    assert_eq!(arms[2], Arm::Nil);
131
1
}
132

            
133
#[test]
134
1
fn literal_arms_adopt_an_expected_type_they_fit() {
135
1
    let literals = [
136
1
        Arm::NumberLiteral(WasmType::I32),
137
1
        Arm::NumberLiteral(WasmType::I32),
138
1
    ];
139
1
    assert_eq!(
140
1
        reconcile_arms_expecting(&literals, Some(WasmType::Ratio)),
141
1
        WasmType::Ratio
142
    );
143
1
    assert_eq!(reconcile_arms_expecting(&literals, None), WasmType::I32);
144
1
    assert_eq!(
145
1
        reconcile_arms_expecting(&[], Some(WasmType::Ratio)),
146
1
        WasmType::Ratio
147
    );
148
1
    assert_eq!(
149
1
        reconcile_arms_expecting(&[Arm::NumberLiteral(WasmType::Ratio)], Some(WasmType::I32)),
150
1
        WasmType::Ratio
151
    );
152
1
}
153

            
154
#[test]
155
1
fn a_computed_or_nil_arm_keeps_its_own_join_under_an_expected_type() {
156
1
    let expected = Some(WasmType::Ratio);
157
1
    let computed = [Arm::Value(WasmType::I32), Arm::NumberLiteral(WasmType::I32)];
158
1
    assert_eq!(reconcile_arms_expecting(&computed, expected), WasmType::I32);
159
1
    let nil = [Arm::NumberLiteral(WasmType::I32), Arm::Nil];
160
1
    assert_eq!(reconcile_arms_expecting(&nil, expected), WasmType::AnyRef);
161
1
}