Test cases for compiler error and working behavior for switching on booleans
parent
4a1f0e1418
commit
64061cc1bf
|
@ -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,
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue