authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-04-30 12:04:23+03:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-04-30 12:04:23+03:00
loge72f45475d05fb446e48d3e34e6c8e367916bd50
treec8b12903027a506b533f34f4c78de3d739f2f5c2
parent611bd8e9f49bd3532a23c49f6b675da03403b635
parent87c9696121048fe9c14b898e26037f939d7959f0
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #4683 from LakeByTheWoods/parser_test

Add visible newlines to parser_test output when there's a failure.

3 files changed, 83 insertions(+), 8 deletions(-)

lib/std/mem.zig+19
......@@ -514,6 +514,25 @@ pub fn eql(comptime T: type, a: []const T, b: []const T) bool {
514514 return true;
515515}
516516
517/// Compares two slices and returns the index of the first inequality.
518/// Returns null if the slices are equal.
519pub fn indexOfDiff(comptime T: type, a: []const T, b: []const T) ?usize {
520 const shortest = math.min(a.len, b.len);
521 if (a.ptr == b.ptr)
522 return if (a.len == b.len) null else shortest;
523 var index: usize = 0;
524 while (index < shortest) : (index += 1) if (a[index] != b[index]) return index;
525 return if (a.len == b.len) null else shortest;
526}
527
528test "indexOfDiff" {
529 testing.expectEqual(indexOfDiff(u8, "one", "one"), null);
530 testing.expectEqual(indexOfDiff(u8, "one two", "one"), 3);
531 testing.expectEqual(indexOfDiff(u8, "one", "one two"), 3);
532 testing.expectEqual(indexOfDiff(u8, "one twx", "one two"), 6);
533 testing.expectEqual(indexOfDiff(u8, "xne", "one"), 0);
534}
535
517536pub const toSliceConst = @compileError("deprecated; use std.mem.spanZ");
518537pub const toSlice = @compileError("deprecated; use std.mem.spanZ");
519538
lib/std/testing.zig+63
......@@ -1,4 +1,5 @@
11const std = @import("std.zig");
2const warn = std.debug.warn;
23
34pub const LeakCountAllocator = @import("testing/leak_count_allocator.zig").LeakCountAllocator;
45pub const FailingAllocator = @import("testing/failing_allocator.zig").FailingAllocator;
......@@ -212,3 +213,65 @@ test "expectEqual vector" {
212213
213214 expectEqual(a, b);
214215}
216
217pub fn expectEqualStrings(expected: []const u8, actual: []const u8) void {
218 if (std.mem.indexOfDiff(u8, actual, expected)) |diff_index| {
219 warn("\n====== expected this output: =========\n", .{});
220 printWithVisibleNewlines(expected);
221 warn("\n======== instead found this: =========\n", .{});
222 printWithVisibleNewlines(actual);
223 warn("\n======================================\n", .{});
224
225 var diff_line_number: usize = 1;
226 for (expected[0..diff_index]) |value| {
227 if (value == '\n') diff_line_number += 1;
228 }
229 warn("First difference occurs on line {}:\n", .{diff_line_number});
230
231 warn("expected:\n", .{});
232 printIndicatorLine(expected, diff_index);
233
234 warn("found:\n", .{});
235 printIndicatorLine(actual, diff_index);
236
237 @panic("test failure");
238 }
239}
240
241fn printIndicatorLine(source: []const u8, indicator_index: usize) void {
242 const line_begin_index = if (std.mem.lastIndexOfScalar(u8, source[0..indicator_index], '\n')) |line_begin|
243 line_begin + 1
244 else
245 0;
246 const line_end_index = if (std.mem.indexOfScalar(u8, source[indicator_index..], '\n')) |line_end|
247 (indicator_index + line_end)
248 else
249 source.len;
250
251 printLine(source[line_begin_index..line_end_index]);
252 {
253 var i: usize = line_begin_index;
254 while (i < indicator_index) : (i += 1)
255 warn(" ", .{});
256 }
257 warn("^\n", .{});
258}
259
260fn printWithVisibleNewlines(source: []const u8) void {
261 var i: usize = 0;
262 while (std.mem.indexOf(u8, source[i..], "\n")) |nl| : (i += nl + 1) {
263 printLine(source[i .. i + nl]);
264 }
265 warn("{}␃\n", .{source[i..]}); // End of Text symbol (ETX)
266}
267
268fn printLine(line: []const u8) void {
269 switch (line[line.len - 1]) {
270 ' ', '\t' => warn("{}⏎\n", .{line}), // Carriage return symbol,
271 else => warn("{}\n", .{line}),
272 }
273}
274
275test "" {
276 expectEqualStrings("foo", "foo");
277}
lib/std/zig/parser_test.zig+1-8
......@@ -2971,14 +2971,7 @@ fn testTransform(source: []const u8, expected_source: []const u8) !void {
29712971 var failing_allocator = std.testing.FailingAllocator.init(&fixed_allocator.allocator, maxInt(usize));
29722972 var anything_changed: bool = undefined;
29732973 const result_source = try testParse(source, &failing_allocator.allocator, &anything_changed);
2974 if (!mem.eql(u8, result_source, expected_source)) {
2975 warn("\n====== expected this output: =========\n", .{});
2976 warn("{}", .{expected_source});
2977 warn("\n======== instead found this: =========\n", .{});
2978 warn("{}", .{result_source});
2979 warn("\n======================================\n", .{});
2980 return error.TestFailed;
2981 }
2974 std.testing.expectEqualStrings(expected_source, result_source);
29822975 const changes_expected = source.ptr != expected_source.ptr;
29832976 if (anything_changed != changes_expected) {
29842977 warn("std.zig.render returned {} instead of {}\n", .{ anything_changed, changes_expected });