Files
@ 392c59600687
Branch filter:
Location: CSY/reowolf/src/protocol/tests/parser_validation.rs
392c59600687
8.9 KiB
application/rls-services+xml
Test non-ascii string literals and recursion
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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | /// parser_validation.rs
///
/// Simple tests for the validation phase
use super::*;
#[test]
fn test_correct_struct_instance() {
Tester::new_single_source_expect_ok(
"single field",
"
struct Foo { s32 a }
func bar(s32 arg) -> Foo { return Foo{ a: arg }; }
"
);
Tester::new_single_source_expect_ok(
"multiple fields",
"
struct Foo { s32 a, s32 b }
func bar(s32 arg) -> Foo { return Foo{ a: arg, b: arg }; }
"
);
Tester::new_single_source_expect_ok(
"single field, explicit polymorph",
"
struct Foo<T>{ T field }
func bar(s32 arg) -> Foo<s32> { return Foo<s32>{ field: arg }; }
"
);
Tester::new_single_source_expect_ok(
"single field, implicit polymorph",
"
struct Foo<T>{ T field }
func bar(s32 arg) -> s32 {
auto thingo = Foo{ field: arg };
return arg;
}
"
);
Tester::new_single_source_expect_ok(
"multiple fields, same explicit polymorph",
"
struct Pair<T1, T2>{ T1 first, T2 second }
func bar(s32 arg) -> s32 {
auto qux = Pair<s32, s32>{ first: arg, second: arg };
return arg;
}
"
);
Tester::new_single_source_expect_ok(
"multiple fields, same implicit polymorph",
"
struct Pair<T1, T2>{ T1 first, T2 second }
func bar(s32 arg) -> s32 {
auto wup = Pair{ first: arg, second: arg };
return arg;
}
"
);
Tester::new_single_source_expect_ok(
"multiple fields, different explicit polymorph",
"
struct Pair<T1, T2>{ T1 first, T2 second }
func bar(s32 arg1, s8 arg2) -> s32 {
auto shoo = Pair<s32, s8>{ first: arg1, second: arg2 };
return arg1;
}
"
);
Tester::new_single_source_expect_ok(
"multiple fields, different implicit polymorph",
"
struct Pair<T1, T2>{ T1 first, T2 second }
func bar(s32 arg1, s8 arg2) -> s32 {
auto shrubbery = Pair{ first: arg1, second: arg2 };
return arg1;
}
"
);
}
#[test]
fn test_incorrect_struct_instance() {
Tester::new_single_source_expect_err(
"reused field in definition",
"struct Foo{ s32 a, s8 a }"
).error(|e| { e
.assert_num(2)
.assert_occurs_at(0, "a }")
.assert_msg_has(0, "defined more than once")
.assert_occurs_at(1, "a, ")
.assert_msg_has(1, "other struct field");
});
Tester::new_single_source_expect_err(
"reused field in instance",
"
struct Foo{ s32 a, s32 b }
func bar() -> s32 {
auto foo = Foo{ a: 5, a: 3 };
return 0;
}
"
).error(|e| { e
.assert_occurs_at(0, "a: 3")
.assert_msg_has(0, "field is specified more than once");
});
Tester::new_single_source_expect_err(
"missing field",
"
struct Foo { s32 a, s32 b }
func bar() -> s32 {
auto foo = Foo{ a: 2 };
return 0;
}
"
).error(|e| { e
.assert_occurs_at(0, "Foo{")
.assert_msg_has(0, "'b' is missing");
});
Tester::new_single_source_expect_err(
"missing fields",
"
struct Foo { s32 a, s32 b, s32 c }
func bar() -> s32 {
auto foo = Foo{ a: 2 };
return 0;
}
"
).error(|e| { e
.assert_occurs_at(0, "Foo{")
.assert_msg_has(0, "[b, c] are missing");
});
}
#[test]
fn test_correct_enum_instance() {
Tester::new_single_source_expect_ok(
"single variant",
"
enum Foo { A }
func bar() -> Foo { return Foo::A; }
"
);
Tester::new_single_source_expect_ok(
"multiple variants",
"
enum Foo { A=15, B = 0xF }
func bar() -> Foo { auto a = Foo::A; return Foo::B; }
"
);
Tester::new_single_source_expect_ok(
"explicit single polymorph",
"
enum Foo<T>{ A }
func bar() -> Foo<s32> { return Foo::A; }
"
);
Tester::new_single_source_expect_ok(
"explicit multi-polymorph",
"
enum Foo<A, B>{ A, B }
func bar() -> Foo<s8, s32> { return Foo::B; }
"
);
}
#[test]
fn test_incorrect_enum_instance() {
Tester::new_single_source_expect_err(
"variant name reuse",
"
enum Foo { A, A }
func bar() -> Foo { return Foo::A; }
"
).error(|e| { e
.assert_num(2)
.assert_occurs_at(0, "A }")
.assert_msg_has(0, "defined more than once")
.assert_occurs_at(1, "A, ")
.assert_msg_has(1, "other enum variant is defined here");
});
Tester::new_single_source_expect_err(
"undefined variant",
"
enum Foo { A }
func bar() -> Foo { return Foo::B; }
"
).error(|e| { e
.assert_num(1)
.assert_msg_has(0, "variant 'B' does not exist on the enum 'Foo'");
});
}
#[test]
fn test_correct_union_instance() {
Tester::new_single_source_expect_ok(
"single tag",
"
union Foo { A }
func bar() -> Foo { return Foo::A; }
"
);
Tester::new_single_source_expect_ok(
"multiple tags",
"
union Foo { A, B }
func bar() -> Foo { return Foo::B; }
"
);
Tester::new_single_source_expect_ok(
"single embedded",
"
union Foo { A(s32) }
func bar() -> Foo { return Foo::A(5); }
"
);
Tester::new_single_source_expect_ok(
"multiple embedded",
"
union Foo { A(s32), B(s8) }
func bar() -> Foo { return Foo::B(2); }
"
);
Tester::new_single_source_expect_ok(
"multiple values in embedded",
"
union Foo { A(s32, s8) }
func bar() -> Foo { return Foo::A(0, 2); }
"
);
Tester::new_single_source_expect_ok(
"mixed tag/embedded",
"
union OptionInt { None, Some(s32) }
func bar() -> OptionInt { return OptionInt::Some(3); }
"
);
Tester::new_single_source_expect_ok(
"single polymorphic var",
"
union Option<T> { None, Some(T) }
func bar() -> Option<s32> { return Option::Some(3); }"
);
Tester::new_single_source_expect_ok(
"multiple polymorphic vars",
"
union Result<T, E> { Ok(T), Err(E), }
func bar() -> Result<s32, s8> { return Result::Ok(3); }
"
);
Tester::new_single_source_expect_ok(
"multiple polymorphic in one variant",
"
union MaybePair<T1, T2>{ None, Some(T1, T2) }
func bar() -> MaybePair<s8, s32> { return MaybePair::Some(1, 2); }
"
);
}
#[test]
fn test_incorrect_union_instance() {
Tester::new_single_source_expect_err(
"tag-variant name reuse",
"
union Foo{ A, A }
"
).error(|e| { e
.assert_num(2)
.assert_occurs_at(0, "A }")
.assert_msg_has(0, "union variant is defined more than once")
.assert_occurs_at(1, "A, ")
.assert_msg_has(1, "other union variant");
});
Tester::new_single_source_expect_err(
"embedded-variant name reuse",
"
union Foo{ A(s32), A(s8) }
"
).error(|e| { e
.assert_num(2)
.assert_occurs_at(0, "A(s8)")
.assert_msg_has(0, "union variant is defined more than once")
.assert_occurs_at(1, "A(s32)")
.assert_msg_has(1, "other union variant");
});
Tester::new_single_source_expect_err(
"undefined variant",
"
union Silly{ Thing(s8) }
func bar() -> Silly { return Silly::Undefined(5); }
"
).error(|e| { e
.assert_msg_has(0, "variant 'Undefined' does not exist on the union 'Silly'");
});
Tester::new_single_source_expect_err(
"using tag instead of embedded",
"
union Foo{ A(s32) }
func bar() -> Foo { return Foo::A; }
"
).error(|e| { e
.assert_msg_has(0, "variant 'A' of union 'Foo' expects 1 embedded values, but 0 were");
});
Tester::new_single_source_expect_err(
"using embedded instead of tag",
"
union Foo{ A }
func bar() -> Foo { return Foo::A(3); }
"
).error(|e| { e
.assert_msg_has(0, "The variant 'A' of union 'Foo' expects 0");
});
Tester::new_single_source_expect_err(
"wrong embedded value",
"
union Foo{ A(s32) }
func bar() -> Foo { return Foo::A(false); }
"
).error(|e| { e
.assert_occurs_at(0, "Foo::A")
.assert_msg_has(0, "failed to fully resolve")
.assert_occurs_at(1, "false")
.assert_msg_has(1, "has been resolved to 's32'")
.assert_msg_has(1, "has been resolved to 'bool'");
});
}
|