Commit 64061cc1bf

alexander <justin.b.alexander1@gmail.com>
2018-12-27 20:46:32
Test cases for compiler error and working behavior for switching on booleans
1 parent 4a1f0e1
Changed files (2)
test/cases/switch.zig
@@ -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,
+    };
+}
test/compile_errors.zig
@@ -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 {