Test cases for compiler error and working behavior for switching on booleans

master
alexander 2018-12-27 13:46:32 -06:00
parent 4a1f0e1418
commit 64061cc1bf
2 changed files with 75 additions and 0 deletions

View File

@ -232,3 +232,40 @@ test "capture value of switch with all unreachable prongs" {
};
assert(x == 1);
}
test "switching on booleans" {
testSwitchOnBools();
comptime testSwitchOnBools();
}
fn testSwitchOnBools() void {
assert(testSwitchOnBoolsTrueAndFalse(true) == false);
assert(testSwitchOnBoolsTrueAndFalse(false) == true);
assert(testSwitchOnBoolsTrueWithElse(true) == false);
assert(testSwitchOnBoolsTrueWithElse(false) == true);
assert(testSwitchOnBoolsFalseWithElse(true) == false);
assert(testSwitchOnBoolsFalseWithElse(false) == true);
}
fn testSwitchOnBoolsTrueAndFalse(x: bool) bool {
return switch (x) {
true => false,
false => true,
};
}
fn testSwitchOnBoolsTrueWithElse(x: bool) bool {
return switch (x) {
true => false,
else => true,
};
}
fn testSwitchOnBoolsFalseWithElse(x: bool) bool {
return switch (x) {
false => true,
else => false,
};
}

View File

@ -1,6 +1,44 @@
const tests = @import("tests.zig");
pub fn addCases(cases: *tests.CompileErrorContext) void {
cases.add(
"duplicate boolean switch value",
\\comptime {
\\ const x = switch (true) {
\\ true => false,
\\ false => true,
\\ true => false,
\\ };
\\}
\\comptime {
\\ const x = switch (true) {
\\ false => true,
\\ true => false,
\\ false => true,
\\ };
\\}
,
".tmp_source.zig:5:9: error: duplicate switch value",
".tmp_source.zig:12:9: error: duplicate switch value",
);
cases.add(
"missing boolean switch value",
\\comptime {
\\ const x = switch (true) {
\\ true => false,
\\ };
\\}
\\comptime {
\\ const x = switch (true) {
\\ false => true,
\\ };
\\}
,
".tmp_source.zig:2:15: error: switch must handle all possibilities",
".tmp_source.zig:7:15: error: switch must handle all possibilities",
);
cases.add(
"reading past end of pointer casted array",
\\comptime {