Files
@ 2c1fa43903ac
Branch filter:
Location: CSY/reowolf/src/protocol/tests/parser_binding.rs
2c1fa43903ac
6.8 KiB
application/rls-services+xml
Several unfinished attempts at introducing polling
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | use super::*;
#[test]
fn test_correct_binding() {
Tester::new_single_source_expect_ok("binding bare", "
enum TestEnum{ A, B }
union TestUnion{ A(u32), B }
struct TestStruct{ u32 field }
func foo() -> u32 {
auto lit_enum_a = TestEnum::A;
auto lit_enum_b = TestEnum::B;
auto lit_union_a = TestUnion::A(0);
auto lit_union_b = TestUnion::B;
auto lit_struct = TestStruct{ field: 0 };
if (let test_enum_a = lit_enum_a) { auto can_use = test_enum_a; }
if (let test_enum_b = lit_enum_b) { auto can_use = test_enum_b; }
if (let test_union_a = lit_union_a) { auto can_use = test_union_a; }
if (let test_union_b = lit_union_b) { auto can_use = test_union_b; }
if (let test_struct = lit_struct) { auto can_use = test_struct; }
return 0;
}
").for_function("foo", |f| { f
.for_variable("test_enum_a", |v| { v.assert_concrete_type("TestEnum"); })
.for_variable("test_enum_b", |v| { v.assert_concrete_type("TestEnum"); })
.for_variable("test_union_a", |v| { v.assert_concrete_type("TestUnion"); })
.for_variable("test_union_b", |v| { v.assert_concrete_type("TestUnion"); })
.for_variable("test_struct", |v| { v.assert_concrete_type("TestStruct"); });
});
}
#[test]
fn test_incorrect_binding() {
Tester::new_single_source_expect_err("binding at statement level", "
func foo() -> bool {
return let a = 5;
}
").error(|e| { e
.assert_num(1)
.assert_occurs_at(0, "let")
.assert_msg_has(0, "only be used inside the testing expression");
});
Tester::new_single_source_expect_err("nested bindings", "
struct Struct{ bool field }
func foo() -> bool {
if (let Struct{ field: let a = Struct{ field: false } } = Struct{ field: true }) {
return test;
}
return false;
}
").error(|e| { e
.assert_num(2)
.assert_occurs_at(0, "let a = ")
.assert_msg_has(0, "nested binding")
.assert_occurs_at(1, "let Struct")
.assert_msg_has(1, "outer binding");
});
}
#[test]
fn test_incorrect_binding_variable() {
// Note: if variable already exists then it is interpreted at the binding
// expression as value. So the case where "the variable is already defined"
// results in no binding variable.
Tester::new_single_source_expect_err("binding var in next scope", "
union Option<T>{ Some(T), None }
func foo() -> bool {
auto opt = Option::Some(false);
if (let Option::Some(var) = opt) {
auto var = true; // should mismatch against binding 'var'
return var;
}
return false;
}
").error(|e| { e
.assert_num(2)
.assert_msg_has(0, "variable name conflicts")
.assert_occurs_at(0, "var = true;")
.assert_occurs_at(1, "var) = opt");
});
Tester::new_single_source_expect_err("binding var in nested scope", "
union LR<A, B>{ L(A), R(B) }
func foo() -> u32 {
LR<auto, u32> x = LR::L(5);
if (let LR::L(y) = x) {
if (true) {
auto y = 5;
return y;
}
}
}
").error(|e| { e
.assert_num(2)
.assert_msg_has(0, "variable name conflicts")
.assert_occurs_at(0, "y = 5")
.assert_occurs_at(1, "y) = x");
});
}
#[test]
fn test_boolean_ops_on_binding() {
Tester::new_single_source_expect_ok("apply && to binding result", "
union TestUnion{ Two(u16), Four(u32), Eight(u64) }
func foo() -> u32 {
auto lit_2 = TestUnion::Two(2);
auto lit_4 = TestUnion::Four(4);
auto lit_8 = TestUnion::Eight(8);
// Testing combined forms of bindings
if (
let TestUnion::Two(test_2) = lit_2 &&
let TestUnion::Four(test_4) = lit_4 &&
let TestUnion::Eight(test_8) = lit_8
) {
auto valid_2 = test_2;
auto valid_4 = test_4;
auto valid_8 = test_8;
}
// Testing in combination with regular expressions, and to the correct
// literals
if (let TestUnion::Two(inter_a) = lit_2 && 5 + 2 == 7) { inter_a = 0; }
if (5 + 2 == 7 && let TestUnion::Two(inter_b) = lit_2) { inter_b = 0; }
if (2 + 2 == 4 && let TestUnion::Two(inter_c) = lit_2 && 3 + 3 == 8) { inter_c = 0; }
// Testing with the 'incorrect' target union
if (let TestUnion::Four(nope) = lit_2 && let TestUnion::Two(zilch) = lit_8) { }
return 0;
}
").for_function("foo", |f| { f
.for_variable("valid_2", |v| { v.assert_concrete_type("u16"); })
.for_variable("valid_4", |v| { v.assert_concrete_type("u32"); })
.for_variable("valid_8", |v| { v.assert_concrete_type("u64"); })
.for_variable("inter_a", |v| { v.assert_concrete_type("u16"); })
.for_variable("inter_b", |v| { v.assert_concrete_type("u16"); })
.for_variable("inter_c", |v| { v.assert_concrete_type("u16"); });
});
Tester::new_single_source_expect_err("apply || before binding", "
enum Test{ A, B }
func foo() -> u32 {
if (let a = Test::A || 5 + 2 == 7) {
auto mission_impossible = 5;
}
return 0;
}
").error(|e| { e
.assert_num(2)
.assert_occurs_at(0, "let a")
.assert_msg_has(0, "only the logical-and operator")
.assert_occurs_at(1, "||")
.assert_msg_has(1, "disallowed operation");
});
Tester::new_single_source_expect_err("apply || after binding", "
enum Test{ A, B }
func foo() -> u32 {
if (5 + 2 == 7 || let b = Test::B) {
auto magic_number = 7;
}
return 0;
}
").error(|e| { e
.assert_num(2)
.assert_occurs_at(0, "let b")
.assert_msg_has(0, "only the logical-and operator")
.assert_occurs_at(1, "||")
.assert_msg_has(1, "disallowed operation");
});
Tester::new_single_source_expect_err("apply || before and after binding", "
enum Test{ A, B }
func foo() -> u32 {
if (1 + 2 == 3 || (let a = Test::A && let b = Test::B) || (2 + 2 == 4)) {
auto darth_vader_says = \"Noooooooooo\";
}
return 0;
}
").error(|e| { e
.assert_num(2)
.assert_occurs_at(0, "let a")
.assert_msg_has(0, "only the logical-and operator")
.assert_occurs_at(1, "|| (let a")
.assert_msg_has(1, "disallowed operation");
});
}
|