authorgravatar for srazin123@gmail.comThisPC <srazin123@gmail.com> 2024-10-09 02:44:41+08:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-01-29 09:19:07+01:00
loge528ab4709a3703b74de3a0006a9c07f056d3600
tree8f3380fae9ef7b7bae3f010ce7b5b9154b8ec689
parent78b7a446f0dd67f1d4dcb730065558bd93399957

std.testing.expectEqual: {any} in print and move tests


1 files changed, 29 insertions(+), 22 deletions(-)

lib/std/testing.zig+29-22
...@@ -125,7 +125,7 @@ fn expectEqualInner(comptime T: type, expected: T, actual: T) !void {...@@ -125,7 +125,7 @@ fn expectEqualInner(comptime T: type, expected: T, actual: T) !void {
125 var i: usize = 0;125 var i: usize = 0;
126 while (i < info.len) : (i += 1) {126 while (i < info.len) : (i += 1) {
127 if (!std.meta.eql(expected[i], actual[i])) {127 if (!std.meta.eql(expected[i], actual[i])) {
128 print("index {} incorrect. expected {}, found {}\n", .{128 print("index {d} incorrect. expected {any}, found {any}\n", .{
129 i, expected[i], actual[i],129 i, expected[i], actual[i],
130 });130 });
131 return error.TestExpectedEqual;131 return error.TestExpectedEqual;
...@@ -214,6 +214,34 @@ test "expectEqual union with comptime-only field" {...@@ -214,6 +214,34 @@ test "expectEqual union with comptime-only field" {
214 try expectEqual(U{ .a = {} }, .a);214 try expectEqual(U{ .a = {} }, .a);
215}215}
216216
217test "expectEqual nested array" {
218 const a = [2][2]f32{
219 [_]f32{ 1.0, 0.0 },
220 [_]f32{ 0.0, 1.0 },
221 };
222
223 const b = [2][2]f32{
224 [_]f32{ 1.0, 0.0 },
225 [_]f32{ 0.0, 1.0 },
226 };
227
228 try expectEqual(a, b);
229}
230
231test "expectEqual vector" {
232 const a: @Vector(4, u32) = @splat(4);
233 const b: @Vector(4, u32) = @splat(4);
234
235 try expectEqual(a, b);
236}
237
238test "expectEqual null" {
239 const a = .{null};
240 const b = @Vector(1, ?*u8){null};
241
242 try expectEqual(a, b);
243}
244
217/// This function is intended to be used only in tests. When the formatted result of the template245/// This function is intended to be used only in tests. When the formatted result of the template
218/// and its arguments does not equal the expected text, it prints diagnostics to stderr to show how246/// and its arguments does not equal the expected text, it prints diagnostics to stderr to show how
219/// they are not equal, then returns an error. It depends on `expectEqualStrings()` for printing247/// they are not equal, then returns an error. It depends on `expectEqualStrings()` for printing
...@@ -584,27 +612,6 @@ pub fn tmpDir(opts: std.fs.Dir.OpenOptions) TmpDir {...@@ -584,27 +612,6 @@ pub fn tmpDir(opts: std.fs.Dir.OpenOptions) TmpDir {
584 };612 };
585}613}
586614
587test "expectEqual nested array" {
588 const a = [2][2]f32{
589 [_]f32{ 1.0, 0.0 },
590 [_]f32{ 0.0, 1.0 },
591 };
592
593 const b = [2][2]f32{
594 [_]f32{ 1.0, 0.0 },
595 [_]f32{ 0.0, 1.0 },
596 };
597
598 try expectEqual(a, b);
599}
600
601test "expectEqual vector" {
602 const a: @Vector(4, u32) = @splat(4);
603 const b: @Vector(4, u32) = @splat(4);
604
605 try expectEqual(a, b);
606}
607
608pub fn expectEqualStrings(expected: []const u8, actual: []const u8) !void {615pub fn expectEqualStrings(expected: []const u8, actual: []const u8) !void {
609 if (std.mem.indexOfDiff(u8, actual, expected)) |diff_index| {616 if (std.mem.indexOfDiff(u8, actual, expected)) |diff_index| {
610 print("\n====== expected this output: =========\n", .{});617 print("\n====== expected this output: =========\n", .{});