mirror of
https://github.com/ziglang/zig.git
synced 2026-02-13 04:48:20 +00:00
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.
This commit is contained in:
parent
04e08ea883
commit
17ce3e5a17
@ -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)));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user