aboutsummaryrefslogtreecommitdiff
path: root/lib/std/testing.zig
diff options
context:
space:
mode:
authorrohlem <rohlemF@gmail.com>2024-06-18 19:01:05 +0200
committerrohlem <rohlemF@gmail.com>2024-06-18 19:01:05 +0200
commit17ce3e5a17e617bd83d1cf2f5f464fb098b92ec7 (patch)
treee8d7beee0a50b2c68b374627635ee89e92e6a265 /lib/std/testing.zig
parent04e08ea883f94d2de7a91daee72ccc9613a18a43 (diff)
downloadzig-17ce3e5a17e617bd83d1cf2f5f464fb098b92ec7.tar.gz
zig-17ce3e5a17e617bd83d1cf2f5f464fb098b92ec7.zip
fix std.testing.expectEqual for comptime-only union
switch from `inline for` with `std.mem.eql` to `inline else` and tag comparison; expectEqualDeep(Inner) was already doing this. add a previously-failing test case.
Diffstat (limited to 'lib/std/testing.zig')
-rw-r--r--lib/std/testing.zig26
1 files changed, 14 insertions, 12 deletions
diff --git a/lib/std/testing.zig b/lib/std/testing.zig
index 051b6becee..341161a64b 100644
--- a/lib/std/testing.zig
+++ b/lib/std/testing.zig
@@ -147,18 +147,10 @@ fn expectEqualInner(comptime T: type, expected: T, actual: T) !void {
try expectEqual(expectedTag, actualTag);
- // we only reach this loop if the tags are equal
- inline for (std.meta.fields(@TypeOf(actual))) |fld| {
- if (std.mem.eql(u8, fld.name, @tagName(actualTag))) {
- try expectEqual(@field(expected, fld.name), @field(actual, fld.name));
- return;
- }
+ // we only reach this switch if the tags are equal
+ switch (expected) {
+ inline else => |val, tag| try expectEqual(val, @field(actual, @tagName(tag))),
}
-
- // we iterate over *all* union fields
- // => we should never get here as the loop above is
- // including all possible values.
- unreachable;
},
.Optional => {
@@ -208,6 +200,16 @@ test "expectEqual.union(enum)" {
try expectEqual(a10, a10);
}
+test "expectEqual union with comptime-only field" {
+ const U = union(enum) {
+ a: void,
+ b: void,
+ c: comptime_int,
+ };
+
+ try expectEqual(U{ .a = {} }, .a);
+}
+
/// This function is intended to be used only in tests. When the formatted result of the template
/// and its arguments does not equal the expected text, it prints diagnostics to stderr to show how
/// they are not equal, then returns an error. It depends on `expectEqualStrings()` for printing
@@ -809,7 +811,7 @@ fn expectEqualDeepInner(comptime T: type, expected: T, actual: T) error{TestExpe
try expectEqual(expectedTag, actualTag);
- // we only reach this loop if the tags are equal
+ // we only reach this switch if the tags are equal
switch (expected) {
inline else => |val, tag| {
try expectEqualDeep(val, @field(actual, @tagName(tag)));