Skip to content

Commit 64061cc

Browse files
Test cases for compiler error and working behavior for switching on booleans
1 parent 4a1f0e1 commit 64061cc

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

test/cases/switch.zig

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,40 @@ test "capture value of switch with all unreachable prongs" {
232232
};
233233
assert(x == 1);
234234
}
235+
236+
test "switching on booleans" {
237+
testSwitchOnBools();
238+
comptime testSwitchOnBools();
239+
}
240+
241+
fn testSwitchOnBools() void {
242+
assert(testSwitchOnBoolsTrueAndFalse(true) == false);
243+
assert(testSwitchOnBoolsTrueAndFalse(false) == true);
244+
245+
assert(testSwitchOnBoolsTrueWithElse(true) == false);
246+
assert(testSwitchOnBoolsTrueWithElse(false) == true);
247+
248+
assert(testSwitchOnBoolsFalseWithElse(true) == false);
249+
assert(testSwitchOnBoolsFalseWithElse(false) == true);
250+
}
251+
252+
fn testSwitchOnBoolsTrueAndFalse(x: bool) bool {
253+
return switch (x) {
254+
true => false,
255+
false => true,
256+
};
257+
}
258+
259+
fn testSwitchOnBoolsTrueWithElse(x: bool) bool {
260+
return switch (x) {
261+
true => false,
262+
else => true,
263+
};
264+
}
265+
266+
fn testSwitchOnBoolsFalseWithElse(x: bool) bool {
267+
return switch (x) {
268+
false => true,
269+
else => false,
270+
};
271+
}

test/compile_errors.zig

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,44 @@
11
const tests = @import("tests.zig");
22

33
pub fn addCases(cases: *tests.CompileErrorContext) void {
4+
cases.add(
5+
"duplicate boolean switch value",
6+
\\comptime {
7+
\\ const x = switch (true) {
8+
\\ true => false,
9+
\\ false => true,
10+
\\ true => false,
11+
\\ };
12+
\\}
13+
\\comptime {
14+
\\ const x = switch (true) {
15+
\\ false => true,
16+
\\ true => false,
17+
\\ false => true,
18+
\\ };
19+
\\}
20+
,
21+
".tmp_source.zig:5:9: error: duplicate switch value",
22+
".tmp_source.zig:12:9: error: duplicate switch value",
23+
);
24+
25+
cases.add(
26+
"missing boolean switch value",
27+
\\comptime {
28+
\\ const x = switch (true) {
29+
\\ true => false,
30+
\\ };
31+
\\}
32+
\\comptime {
33+
\\ const x = switch (true) {
34+
\\ false => true,
35+
\\ };
36+
\\}
37+
,
38+
".tmp_source.zig:2:15: error: switch must handle all possibilities",
39+
".tmp_source.zig:7:15: error: switch must handle all possibilities",
40+
);
41+
442
cases.add(
543
"reading past end of pointer casted array",
644
\\comptime {

0 commit comments

Comments
 (0)