aboutsummaryrefslogtreecommitdiff
path: root/lib/std/testing.zig
diff options
context:
space:
mode:
authorRyan Liptak <squeek502@hotmail.com>2023-08-04 18:00:52 -0700
committerAndrew Kelley <andrew@ziglang.org>2023-08-05 11:54:26 -0700
commit90fde14c5f4406c284ec030186172b3db4697258 (patch)
tree1a5f2ae17ce10105b1d9c97b069f6703cf72a196 /lib/std/testing.zig
parenta91a8df6791b6fe228ad616acd9fbbde46451651 (diff)
downloadzig-90fde14c5f4406c284ec030186172b3db4697258.tar.gz
zig-90fde14c5f4406c284ec030186172b3db4697258.zip
std.testing.expectEqualSlices: On failure, print address for pointer types
When comparing slice elements, `std.meta.eql` is used which only compares pointer address and length to determine equality for pointer types. This previously led to confusing results where `expectEqualSlices` would appear to fail on seemingly equal slices (judging by the output of `expectEqualSlices`. For example: try testing.expectEqualSlices( []const i64, &[_][]const i64{ &[_]i64{ 1, 2, 3 }, &[_]i64{ 5, 5, 5 } }, &[_][]const i64{ &[_]i64{ 1, 2, 3 }, &[_]i64{ 5, 5, 5 } }, ); Previously, this would result in: ============ expected this output: ============= len: 2 (0x2) [0]: { 1, 2, 3 } [1]: { 5, 5, 5 } ============= instead found this: ============== len: 2 (0x2) [0]: { 1, 2, 3 } [1]: { 5, 5, 5 } ================================================ After this commit, it will result in: ============ expected this output: ============= len: 2 (0x2) [0]i64@7ff7e2773758: { 1, 2, 3 } [1]i64@7ff7e2773770: { 5, 5, 5 } ============= instead found this: ============== len: 2 (0x2) [0]i64@7ff7e2773788: { 1, 2, 3 } [1]i64@7ff7e27737a0: { 5, 5, 5 } ================================================
Diffstat (limited to 'lib/std/testing.zig')
-rw-r--r--lib/std/testing.zig6
1 files changed, 5 insertions, 1 deletions
diff --git a/lib/std/testing.zig b/lib/std/testing.zig
index 0590d76e57..40a95a450b 100644
--- a/lib/std/testing.zig
+++ b/lib/std/testing.zig
@@ -394,7 +394,11 @@ fn SliceDiffer(comptime T: type) type {
var full_index = self.start_index + i;
const diff = if (i < self.actual.len) !std.meta.eql(self.actual[i], value) else true;
if (diff) try self.ttyconf.setColor(writer, .red);
- try writer.print("[{}]: {any}\n", .{ full_index, value });
+ if (@typeInfo(T) == .Pointer) {
+ try writer.print("[{}]{*}: {any}\n", .{ full_index, value, value });
+ } else {
+ try writer.print("[{}]: {any}\n", .{ full_index, value });
+ }
if (diff) try self.ttyconf.setColor(writer, .reset);
}
}