diff --git a/lib/std/mem.zig b/lib/std/mem.zig index 5ba984ecdc..a40334e587 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -514,6 +514,25 @@ pub fn eql(comptime T: type, a: []const T, b: []const T) bool { return true; } +/// Compares two slices and returns the index of the first inequality. +/// Returns null if the slices are equal. +pub fn indexOfDiff(comptime T: type, a: []const T, b: []const T) ?usize { + const shortest = math.min(a.len, b.len); + if (a.ptr == b.ptr) + return if (a.len == b.len) null else shortest; + var index: usize = 0; + while (index < shortest) : (index += 1) if (a[index] != b[index]) return index; + return if (a.len == b.len) null else shortest; +} + +test "indexOfDiff" { + testing.expectEqual(indexOfDiff(u8, "one", "one"), null); + testing.expectEqual(indexOfDiff(u8, "one two", "one"), 3); + testing.expectEqual(indexOfDiff(u8, "one", "one two"), 3); + testing.expectEqual(indexOfDiff(u8, "one twx", "one two"), 6); + testing.expectEqual(indexOfDiff(u8, "xne", "one"), 0); +} + pub const toSliceConst = @compileError("deprecated; use std.mem.spanZ"); pub const toSlice = @compileError("deprecated; use std.mem.spanZ"); diff --git a/lib/std/testing.zig b/lib/std/testing.zig index 4b629156f6..fed2b15bf5 100644 --- a/lib/std/testing.zig +++ b/lib/std/testing.zig @@ -1,4 +1,5 @@ const std = @import("std.zig"); +const warn = std.debug.warn; pub const LeakCountAllocator = @import("testing/leak_count_allocator.zig").LeakCountAllocator; pub const FailingAllocator = @import("testing/failing_allocator.zig").FailingAllocator; @@ -212,3 +213,65 @@ test "expectEqual vector" { expectEqual(a, b); } + +pub fn expectEqualStrings(expected: []const u8, actual: []const u8) void { + if (std.mem.indexOfDiff(u8, actual, expected)) |diff_index| { + warn("\n====== expected this output: =========\n", .{}); + printWithVisibleNewlines(expected); + warn("\n======== instead found this: =========\n", .{}); + printWithVisibleNewlines(actual); + warn("\n======================================\n", .{}); + + var diff_line_number: usize = 1; + for (expected[0..diff_index]) |value| { + if (value == '\n') diff_line_number += 1; + } + warn("First difference occurs on line {}:\n", .{diff_line_number}); + + warn("expected:\n", .{}); + printIndicatorLine(expected, diff_index); + + warn("found:\n", .{}); + printIndicatorLine(actual, diff_index); + + @panic("test failure"); + } +} + +fn printIndicatorLine(source: []const u8, indicator_index: usize) void { + const line_begin_index = if (std.mem.lastIndexOfScalar(u8, source[0..indicator_index], '\n')) |line_begin| + line_begin + 1 + else + 0; + const line_end_index = if (std.mem.indexOfScalar(u8, source[indicator_index..], '\n')) |line_end| + (indicator_index + line_end) + else + source.len; + + printLine(source[line_begin_index..line_end_index]); + { + var i: usize = line_begin_index; + while (i < indicator_index) : (i += 1) + warn(" ", .{}); + } + warn("^\n", .{}); +} + +fn printWithVisibleNewlines(source: []const u8) void { + var i: usize = 0; + while (std.mem.indexOf(u8, source[i..], "\n")) |nl| : (i += nl + 1) { + printLine(source[i .. i + nl]); + } + warn("{}␃\n", .{source[i..]}); // End of Text symbol (ETX) +} + +fn printLine(line: []const u8) void { + switch (line[line.len - 1]) { + ' ', '\t' => warn("{}⏎\n", .{line}), // Carriage return symbol, + else => warn("{}\n", .{line}), + } +} + +test "" { + expectEqualStrings("foo", "foo"); +} diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig index a6975929d9..28d3fa0be2 100644 --- a/lib/std/zig/parser_test.zig +++ b/lib/std/zig/parser_test.zig @@ -2971,14 +2971,7 @@ fn testTransform(source: []const u8, expected_source: []const u8) !void { var failing_allocator = std.testing.FailingAllocator.init(&fixed_allocator.allocator, maxInt(usize)); var anything_changed: bool = undefined; const result_source = try testParse(source, &failing_allocator.allocator, &anything_changed); - if (!mem.eql(u8, result_source, expected_source)) { - warn("\n====== expected this output: =========\n", .{}); - warn("{}", .{expected_source}); - warn("\n======== instead found this: =========\n", .{}); - warn("{}", .{result_source}); - warn("\n======================================\n", .{}); - return error.TestFailed; - } + std.testing.expectEqualStrings(expected_source, result_source); const changes_expected = source.ptr != expected_source.ptr; if (anything_changed != changes_expected) { warn("std.zig.render returned {} instead of {}\n", .{ anything_changed, changes_expected });