Polish a few tests in switch.zig

- Return `error.TestFailed` instead of panicing
- Use `comptime assert` for type checks so that errors surface at
  compile time
This commit is contained in:
John Schmidt 2024-02-08 23:40:03 +01:00
parent 0d1baf0c61
commit 7cb227ab67

View File

@ -410,8 +410,8 @@ test "switch on integer with else capturing expr" {
var x: i32 = 5; var x: i32 = 5;
_ = &x; _ = &x;
switch (x + 10) { switch (x + 10) {
14 => @panic("fail"), 14 => return error.TestFailed,
16 => @panic("fail"), 16 => return error.TestFailed,
else => |e| try expect(e == 15), else => |e| try expect(e == 15),
} }
} }
@ -522,7 +522,7 @@ test "switch with null and T peer types and inferred result location type" {
else => null, else => null,
}) |v| { }) |v| {
_ = v; _ = v;
@panic("fail"); return error.TestFailed;
} }
} }
}; };
@ -548,12 +548,12 @@ test "switch prongs with cases with identical payload types" {
fn doTheSwitch1(u: Union) !void { fn doTheSwitch1(u: Union) !void {
switch (u) { switch (u) {
.A, .C => |e| { .A, .C => |e| {
try expect(@TypeOf(e) == usize); comptime assert(@TypeOf(e) == usize);
try expect(e == 8); try expect(e == 8);
}, },
.B => |e| { .B => |e| {
_ = e; _ = e;
@panic("fail"); return error.TestFailed;
}, },
} }
} }
@ -561,10 +561,10 @@ test "switch prongs with cases with identical payload types" {
switch (u) { switch (u) {
.A, .C => |e| { .A, .C => |e| {
_ = e; _ = e;
@panic("fail"); return error.TestFailed;
}, },
.B => |e| { .B => |e| {
try expect(@TypeOf(e) == isize); comptime assert(@TypeOf(e) == isize);
try expect(e == -8); try expect(e == -8);
}, },
} }