| ... | ... | @@ -360,9 +360,6 @@ test expectApproxEqRel { |
| 360 | 360 | /// The colorized output is optional and controlled by the return of `std.io.tty.detectConfig()`. |
| 361 | 361 | /// If your inputs are UTF-8 encoded strings, consider calling `expectEqualStrings` instead. |
| 362 | 362 | pub fn expectEqualSlices(comptime T: type, expected: []const T, actual: []const T) !void { |
| 363 | | if (expected.ptr == actual.ptr and expected.len == actual.len) { |
| 364 | | return; |
| 365 | | } |
| 366 | 363 | const diff_index: usize = diff_index: { |
| 367 | 364 | const shortest = @min(expected.len, actual.len); |
| 368 | 365 | var index: usize = 0; |
| ... | ... | @@ -371,12 +368,21 @@ pub fn expectEqualSlices(comptime T: type, expected: []const T, actual: []const |
| 371 | 368 | } |
| 372 | 369 | break :diff_index if (expected.len == actual.len) return else shortest; |
| 373 | 370 | }; |
| 371 | if (!backend_can_print) return error.TestExpectedEqual; |
| 372 | const stderr_w = std.debug.lockStderrWriter(&.{}); |
| 373 | defer std.debug.unlockStderrWriter(); |
| 374 | failEqualSlices(T, expected, actual, diff_index, stderr_w) catch {}; |
| 375 | return error.TestExpectedEqual; |
| 376 | } |
| 374 | 377 | |
| 375 | | if (!backend_can_print) { |
| 376 | | return error.TestExpectedEqual; |
| 377 | | } |
| 378 | | |
| 379 | | print("slices differ. first difference occurs at index {d} (0x{X})\n", .{ diff_index, diff_index }); |
| 378 | fn failEqualSlices( |
| 379 | comptime T: type, |
| 380 | expected: []const T, |
| 381 | actual: []const T, |
| 382 | diff_index: usize, |
| 383 | w: *std.io.Writer, |
| 384 | ) !void { |
| 385 | try w.print("slices differ. first difference occurs at index {d} (0x{X})\n", .{ diff_index, diff_index }); |
| 380 | 386 | |
| 381 | 387 | // TODO: Should this be configurable by the caller? |
| 382 | 388 | const max_lines: usize = 16; |
| ... | ... | @@ -394,8 +400,7 @@ pub fn expectEqualSlices(comptime T: type, expected: []const T, actual: []const |
| 394 | 400 | const actual_window = actual[window_start..@min(actual.len, window_start + max_window_size)]; |
| 395 | 401 | const actual_truncated = window_start + actual_window.len < actual.len; |
| 396 | 402 | |
| 397 | | const stderr: std.fs.File = .stderr(); |
| 398 | | const ttyconf = std.io.tty.detectConfig(stderr); |
| 403 | const ttyconf = std.io.tty.detectConfig(.stderr()); |
| 399 | 404 | var differ = if (T == u8) BytesDiffer{ |
| 400 | 405 | .expected = expected_window, |
| 401 | 406 | .actual = actual_window, |
| ... | ... | @@ -411,47 +416,47 @@ pub fn expectEqualSlices(comptime T: type, expected: []const T, actual: []const |
| 411 | 416 | // that is usually useful. |
| 412 | 417 | const index_fmt = if (T == u8) "0x{X}" else "{}"; |
| 413 | 418 | |
| 414 | | print("\n============ expected this output: ============= len: {} (0x{X})\n\n", .{ expected.len, expected.len }); |
| 419 | try w.print("\n============ expected this output: ============= len: {} (0x{X})\n\n", .{ expected.len, expected.len }); |
| 415 | 420 | if (window_start > 0) { |
| 416 | 421 | if (T == u8) { |
| 417 | | print("... truncated, start index: " ++ index_fmt ++ " ...\n", .{window_start}); |
| 422 | try w.print("... truncated, start index: " ++ index_fmt ++ " ...\n", .{window_start}); |
| 418 | 423 | } else { |
| 419 | | print("... truncated ...\n", .{}); |
| 424 | try w.print("... truncated ...\n", .{}); |
| 420 | 425 | } |
| 421 | 426 | } |
| 422 | | differ.write(stderr.deprecatedWriter()) catch {}; |
| 427 | differ.write(w) catch {}; |
| 423 | 428 | if (expected_truncated) { |
| 424 | 429 | const end_offset = window_start + expected_window.len; |
| 425 | 430 | const num_missing_items = expected.len - (window_start + expected_window.len); |
| 426 | 431 | if (T == u8) { |
| 427 | | print("... truncated, indexes [" ++ index_fmt ++ "..] not shown, remaining bytes: " ++ index_fmt ++ " ...\n", .{ end_offset, num_missing_items }); |
| 432 | try w.print("... truncated, indexes [" ++ index_fmt ++ "..] not shown, remaining bytes: " ++ index_fmt ++ " ...\n", .{ end_offset, num_missing_items }); |
| 428 | 433 | } else { |
| 429 | | print("... truncated, remaining items: " ++ index_fmt ++ " ...\n", .{num_missing_items}); |
| 434 | try w.print("... truncated, remaining items: " ++ index_fmt ++ " ...\n", .{num_missing_items}); |
| 430 | 435 | } |
| 431 | 436 | } |
| 432 | 437 | |
| 433 | 438 | // now reverse expected/actual and print again |
| 434 | 439 | differ.expected = actual_window; |
| 435 | 440 | differ.actual = expected_window; |
| 436 | | print("\n============= instead found this: ============== len: {} (0x{X})\n\n", .{ actual.len, actual.len }); |
| 441 | try w.print("\n============= instead found this: ============== len: {} (0x{X})\n\n", .{ actual.len, actual.len }); |
| 437 | 442 | if (window_start > 0) { |
| 438 | 443 | if (T == u8) { |
| 439 | | print("... truncated, start index: " ++ index_fmt ++ " ...\n", .{window_start}); |
| 444 | try w.print("... truncated, start index: " ++ index_fmt ++ " ...\n", .{window_start}); |
| 440 | 445 | } else { |
| 441 | | print("... truncated ...\n", .{}); |
| 446 | try w.print("... truncated ...\n", .{}); |
| 442 | 447 | } |
| 443 | 448 | } |
| 444 | | differ.write(stderr.deprecatedWriter()) catch {}; |
| 449 | differ.write(w) catch {}; |
| 445 | 450 | if (actual_truncated) { |
| 446 | 451 | const end_offset = window_start + actual_window.len; |
| 447 | 452 | const num_missing_items = actual.len - (window_start + actual_window.len); |
| 448 | 453 | if (T == u8) { |
| 449 | | print("... truncated, indexes [" ++ index_fmt ++ "..] not shown, remaining bytes: " ++ index_fmt ++ " ...\n", .{ end_offset, num_missing_items }); |
| 454 | try w.print("... truncated, indexes [" ++ index_fmt ++ "..] not shown, remaining bytes: " ++ index_fmt ++ " ...\n", .{ end_offset, num_missing_items }); |
| 450 | 455 | } else { |
| 451 | | print("... truncated, remaining items: " ++ index_fmt ++ " ...\n", .{num_missing_items}); |
| 456 | try w.print("... truncated, remaining items: " ++ index_fmt ++ " ...\n", .{num_missing_items}); |
| 452 | 457 | } |
| 453 | 458 | } |
| 454 | | print("\n================================================\n\n", .{}); |
| 459 | try w.print("\n================================================\n\n", .{}); |
| 455 | 460 | |
| 456 | 461 | return error.TestExpectedEqual; |
| 457 | 462 | } |
| ... | ... | @@ -465,7 +470,7 @@ fn SliceDiffer(comptime T: type) type { |
| 465 | 470 | |
| 466 | 471 | const Self = @This(); |
| 467 | 472 | |
| 468 | | pub fn write(self: Self, writer: anytype) !void { |
| 473 | pub fn write(self: Self, writer: *std.io.Writer) !void { |
| 469 | 474 | for (self.expected, 0..) |value, i| { |
| 470 | 475 | const full_index = self.start_index + i; |
| 471 | 476 | const diff = if (i < self.actual.len) !std.meta.eql(self.actual[i], value) else true; |
| ... | ... | @@ -486,7 +491,7 @@ const BytesDiffer = struct { |
| 486 | 491 | actual: []const u8, |
| 487 | 492 | ttyconf: std.io.tty.Config, |
| 488 | 493 | |
| 489 | | pub fn write(self: BytesDiffer, writer: anytype) !void { |
| 494 | pub fn write(self: BytesDiffer, writer: *std.io.Writer) !void { |
| 490 | 495 | var expected_iterator = std.mem.window(u8, self.expected, 16, 16); |
| 491 | 496 | var row: usize = 0; |
| 492 | 497 | while (expected_iterator.next()) |chunk| { |
| ... | ... | @@ -503,7 +508,7 @@ const BytesDiffer = struct { |
| 503 | 508 | if (chunk.len < 16) { |
| 504 | 509 | var missing_columns = (16 - chunk.len) * 3; |
| 505 | 510 | if (chunk.len < 8) missing_columns += 1; |
| 506 | | try writer.writeByteNTimes(' ', missing_columns); |
| 511 | try writer.splatByteAll(' ', missing_columns); |
| 507 | 512 | } |
| 508 | 513 | for (chunk, 0..) |byte, col| { |
| 509 | 514 | const diff = diffs.isSet(col); |
| ... | ... | @@ -532,7 +537,7 @@ const BytesDiffer = struct { |
| 532 | 537 | } |
| 533 | 538 | } |
| 534 | 539 | |
| 535 | | fn writeDiff(self: BytesDiffer, writer: anytype, comptime fmt: []const u8, args: anytype, diff: bool) !void { |
| 540 | fn writeDiff(self: BytesDiffer, writer: *std.io.Writer, comptime fmt: []const u8, args: anytype, diff: bool) !void { |
| 536 | 541 | if (diff) try self.ttyconf.setColor(writer, .red); |
| 537 | 542 | try writer.print(fmt, args); |
| 538 | 543 | if (diff) try self.ttyconf.setColor(writer, .reset); |