| ... | @@ -89,7 +89,26 @@ pub fn expectEqual(expected: var, actual: @typeOf(expected)) void { | ... | @@ -89,7 +89,26 @@ pub fn expectEqual(expected: var, actual: @typeOf(expected)) void { |
| 89 | if (union_info.tag_type == null) { | 89 | if (union_info.tag_type == null) { |
| 90 | @compileError("Unable to compare untagged union values"); | 90 | @compileError("Unable to compare untagged union values"); |
| 91 | } | 91 | } |
| 92 | @compileError("TODO implement testing.expectEqual for tagged unions"); | 92 | |
| | 93 | const TagType = @TagType(@typeOf(expected)); |
| | 94 | |
| | 95 | const expectedTag = @as(TagType, expected); |
| | 96 | const actualTag = @as(TagType, actual); |
| | 97 | |
| | 98 | expectEqual(expectedTag, actualTag); |
| | 99 | |
| | 100 | // we only reach this loop if the tags are equal |
| | 101 | inline for (std.meta.fields(@typeOf(actual))) |fld| { |
| | 102 | if (std.mem.eql(u8, fld.name, @tagName(actualTag))) { |
| | 103 | expectEqual(@field(expected, fld.name), @field(actual, fld.name)); |
| | 104 | return; |
| | 105 | } |
| | 106 | } |
| | 107 | |
| | 108 | // we iterate over *all* union fields |
| | 109 | // => we should never get here as the loop above is |
| | 110 | // including all possible values. |
| | 111 | unreachable; |
| 93 | }, | 112 | }, |
| 94 | | 113 | |
| 95 | .Optional => { | 114 | .Optional => { |
| ... | @@ -124,6 +143,19 @@ pub fn expectEqual(expected: var, actual: @typeOf(expected)) void { | ... | @@ -124,6 +143,19 @@ pub fn expectEqual(expected: var, actual: @typeOf(expected)) void { |
| 124 | } | 143 | } |
| 125 | } | 144 | } |
| 126 | | 145 | |
| | 146 | test "expectEqual.union(enum)" |
| | 147 | { |
| | 148 | const T = union(enum) { |
| | 149 | a: i32, |
| | 150 | b: f32, |
| | 151 | }; |
| | 152 | |
| | 153 | const a10 = T { .a = 10 }; |
| | 154 | const a20 = T { .a = 20 }; |
| | 155 | |
| | 156 | expectEqual(a10, a10); |
| | 157 | } |
| | 158 | |
| 127 | /// This function is intended to be used only in tests. When the two slices are not | 159 | /// This function is intended to be used only in tests. When the two slices are not |
| 128 | /// equal, prints diagnostics to stderr to show exactly how they are not equal, | 160 | /// equal, prints diagnostics to stderr to show exactly how they are not equal, |
| 129 | /// then aborts. | 161 | /// then aborts. |