| ... | @@ -147,18 +147,10 @@ fn expectEqualInner(comptime T: type, expected: T, actual: T) !void { | ... | @@ -147,18 +147,10 @@ fn expectEqualInner(comptime T: type, expected: T, actual: T) !void { |
| 147 | | 147 | |
| 148 | try expectEqual(expectedTag, actualTag); | 148 | try expectEqual(expectedTag, actualTag); |
| 149 | | 149 | |
| 150 | // we only reach this loop if the tags are equal | 150 | // we only reach this switch if the tags are equal |
| 151 | inline for (std.meta.fields(@TypeOf(actual))) |fld| { | 151 | switch (expected) { |
| 152 | if (std.mem.eql(u8, fld.name, @tagName(actualTag))) { | 152 | inline else => |val, tag| try expectEqual(val, @field(actual, @tagName(tag))), |
| 153 | try expectEqual(@field(expected, fld.name), @field(actual, fld.name)); | | |
| 154 | return; | | |
| 155 | } | | |
| 156 | } | 153 | } |
| 157 | | | |
| 158 | // we iterate over *all* union fields | | |
| 159 | // => we should never get here as the loop above is | | |
| 160 | // including all possible values. | | |
| 161 | unreachable; | | |
| 162 | }, | 154 | }, |
| 163 | | 155 | |
| 164 | .Optional => { | 156 | .Optional => { |
| ... | @@ -208,6 +200,16 @@ test "expectEqual.union(enum)" { | ... | @@ -208,6 +200,16 @@ test "expectEqual.union(enum)" { |
| 208 | try expectEqual(a10, a10); | 200 | try expectEqual(a10, a10); |
| 209 | } | 201 | } |
| 210 | | 202 | |
| | 203 | test "expectEqual union with comptime-only field" { |
| | 204 | const U = union(enum) { |
| | 205 | a: void, |
| | 206 | b: void, |
| | 207 | c: comptime_int, |
| | 208 | }; |
| | 209 | |
| | 210 | try expectEqual(U{ .a = {} }, .a); |
| | 211 | } |
| | 212 | |
| 211 | /// This function is intended to be used only in tests. When the formatted result of the template | 213 | /// This function is intended to be used only in tests. When the formatted result of the template |
| 212 | /// and its arguments does not equal the expected text, it prints diagnostics to stderr to show how | 214 | /// and its arguments does not equal the expected text, it prints diagnostics to stderr to show how |
| 213 | /// they are not equal, then returns an error. It depends on `expectEqualStrings()` for printing | 215 | /// 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 | ... | @@ -809,7 +811,7 @@ fn expectEqualDeepInner(comptime T: type, expected: T, actual: T) error{TestExpe |
| 809 | | 811 | |
| 810 | try expectEqual(expectedTag, actualTag); | 812 | try expectEqual(expectedTag, actualTag); |
| 811 | | 813 | |
| 812 | // we only reach this loop if the tags are equal | 814 | // we only reach this switch if the tags are equal |
| 813 | switch (expected) { | 815 | switch (expected) { |
| 814 | inline else => |val, tag| { | 816 | inline else => |val, tag| { |
| 815 | try expectEqualDeep(val, @field(actual, @tagName(tag))); | 817 | try expectEqualDeep(val, @field(actual, @tagName(tag))); |