authorgravatar for r00ster91@proton.meWooster <r00ster91@proton.me> 2023-08-03 09:42:04+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-08 00:13:21-07:00
logad7a09d95a3867ac9d8230c4b9694f711d09390e
tree742ea0943af722c97e2a7336d8c32a631619e89f
parentdfc4d618dd31c00e339344962a839db5208ade8f

std.testing.expectEqualSlices: some improvements

This mainly replaces ChunkIterator with std.mem.window and also prints \n, \r, \t using Unicode symbols instead of periods because they're common non-printable characters. This same code exists in std.debug.hexdump. At some point maybe this code could be exposed through a public function. Then we could reuse the code in both places.

1 files changed, 36 insertions(+), 33 deletions(-)

lib/std/testing.zig+36-33
......@@ -339,7 +339,8 @@ pub fn expectEqualSlices(comptime T: type, expected: []const T, actual: []const
339339 const actual_window = actual[window_start..@min(actual.len, window_start + max_window_size)];
340340 const actual_truncated = window_start + actual_window.len < actual.len;
341341
342 const ttyconf = std.io.tty.detectConfig(std.io.getStdErr());
342 const stderr = std.io.getStdErr();
343 const ttyconf = std.io.tty.detectConfig(stderr);
343344 var differ = if (T == u8) BytesDiffer{
344345 .expected = expected_window,
345346 .actual = actual_window,
......@@ -350,7 +351,6 @@ pub fn expectEqualSlices(comptime T: type, expected: []const T, actual: []const
350351 .actual = actual_window,
351352 .ttyconf = ttyconf,
352353 };
353 const stderr = std.io.getStdErr();
354354
355355 // Print indexes as hex for slices of u8 since it's more likely to be binary data where
356356 // that is usually useful.
......@@ -432,16 +432,17 @@ const BytesDiffer = struct {
432432 ttyconf: std.io.tty.Config,
433433
434434 pub fn write(self: BytesDiffer, writer: anytype) !void {
435 var expected_iterator = ChunkIterator{ .bytes = self.expected };
435 var expected_iterator = std.mem.window(u8, self.expected, 16, 16);
436 var row: usize = 0;
436437 while (expected_iterator.next()) |chunk| {
437438 // to avoid having to calculate diffs twice per chunk
438439 var diffs: std.bit_set.IntegerBitSet(16) = .{ .mask = 0 };
439 for (chunk, 0..) |byte, i| {
440 const absolute_byte_index = (expected_iterator.index - chunk.len) + i;
440 for (chunk, 0..) |byte, col| {
441 const absolute_byte_index = col + row * 16;
441442 const diff = if (absolute_byte_index < self.actual.len) self.actual[absolute_byte_index] != byte else true;
442 if (diff) diffs.set(i);
443 try self.writeByteDiff(writer, "{X:0>2} ", byte, diff);
444 if (i == 7) try writer.writeByte(' ');
443 if (diff) diffs.set(col);
444 try self.writeDiff(writer, "{X:0>2} ", .{byte}, diff);
445 if (col == 7) try writer.writeByte(' ');
445446 }
446447 try writer.writeByte(' ');
447448 if (chunk.len < 16) {
......@@ -449,33 +450,38 @@ const BytesDiffer = struct {
449450 if (chunk.len < 8) missing_columns += 1;
450451 try writer.writeByteNTimes(' ', missing_columns);
451452 }
452 for (chunk, 0..) |byte, i| {
453 const byte_to_print = if (std.ascii.isPrint(byte)) byte else '.';
454 try self.writeByteDiff(writer, "{c}", byte_to_print, diffs.isSet(i));
453 for (chunk, 0..) |byte, col| {
454 const diff = diffs.isSet(col);
455 if (std.ascii.isPrint(byte)) {
456 try self.writeDiff(writer, "{c}", .{byte}, diff);
457 } else {
458 // TODO: remove this `if` when https://github.com/ziglang/zig/issues/7600 is fixed
459 if (self.ttyconf == .windows_api) {
460 try self.writeDiff(writer, ".", .{}, diff);
461 continue;
462 }
463
464 // Let's print some common control codes as graphical Unicode symbols.
465 // We don't want to do this for all control codes because most control codes apart from
466 // the ones that Zig has escape sequences for are likely not very useful to print as symbols.
467 switch (byte) {
468 '\n' => try self.writeDiff(writer, "␊", .{}, diff),
469 '\r' => try self.writeDiff(writer, "␍", .{}, diff),
470 '\t' => try self.writeDiff(writer, "␉", .{}, diff),
471 else => try self.writeDiff(writer, ".", .{}, diff),
472 }
473 }
455474 }
456475 try writer.writeByte('\n');
476 row += 1;
457477 }
458478 }
459479
460 fn writeByteDiff(self: BytesDiffer, writer: anytype, comptime fmt: []const u8, byte: u8, diff: bool) !void {
480 fn writeDiff(self: BytesDiffer, writer: anytype, comptime fmt: []const u8, args: anytype, diff: bool) !void {
461481 if (diff) try self.ttyconf.setColor(writer, .red);
462 try writer.print(fmt, .{byte});
482 try writer.print(fmt, args);
463483 if (diff) try self.ttyconf.setColor(writer, .reset);
464484 }
465
466 const ChunkIterator = struct {
467 bytes: []const u8,
468 index: usize = 0,
469
470 pub fn next(self: *ChunkIterator) ?[]const u8 {
471 if (self.index == self.bytes.len) return null;
472
473 const start_index = self.index;
474 const end_index = @min(self.bytes.len, start_index + 16);
475 self.index = end_index;
476 return self.bytes[start_index..end_index];
477 }
478 };
479485};
480486
481487test {
......@@ -926,11 +932,8 @@ fn printIndicatorLine(source: []const u8, indicator_index: usize) void {
926932 source.len;
927933
928934 printLine(source[line_begin_index..line_end_index]);
929 {
930 var i: usize = line_begin_index;
931 while (i < indicator_index) : (i += 1)
932 print(" ", .{});
933 }
935 for (line_begin_index..indicator_index) |_|
936 print(" ", .{});
934937 if (indicator_index >= source.len)
935938 print("^ (end of string)\n", .{})
936939 else
......@@ -947,7 +950,7 @@ fn printWithVisibleNewlines(source: []const u8) void {
947950
948951fn printLine(line: []const u8) void {
949952 if (line.len != 0) switch (line[line.len - 1]) {
950 ' ', '\t' => return print("{s}⏎\n", .{line}), // Carriage return symbol,
953 ' ', '\t' => return print("{s}⏎\n", .{line}), // Return symbol
951954 else => {},
952955 };
953956 print("{s}\n", .{line});