| author | |
| committer | |
| log | 87c9696121048fe9c14b898e26037f939d7959f0 |
| tree | 772c5f384d017d2c1313bdde042fb8fd2bbc7a4c |
| parent | 2d06e731ec4feee03a2a9bc79eb44b15e2db2488 |
| signature | Commit is signed but in an unrecognized format. |
2 files changed, 64 insertions(+), 51 deletions(-)
lib/std/testing.zig+63| ... | ... | @@ -1,4 +1,5 @@ |
| 1 | 1 | const std = @import("std.zig"); |
| 2 | const warn = std.debug.warn; | |
| 2 | 3 | |
| 3 | 4 | pub const LeakCountAllocator = @import("testing/leak_count_allocator.zig").LeakCountAllocator; |
| 4 | 5 | pub const FailingAllocator = @import("testing/failing_allocator.zig").FailingAllocator; |
| ... | ... | @@ -212,3 +213,65 @@ test "expectEqual vector" { |
| 212 | 213 | |
| 213 | 214 | expectEqual(a, b); |
| 214 | 215 | } |
| 216 | ||
| 217 | pub 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 | ||
| 241 | fn 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 | ||
| 260 | fn 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 | ||
| 268 | fn 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 | ||
| 275 | test "" { | |
| 276 | expectEqualStrings("foo", "foo"); | |
| 277 | } |
lib/std/zig/parser_test.zig+1-51| ... | ... | @@ -2960,36 +2960,6 @@ fn testParse(source: []const u8, allocator: *mem.Allocator, anything_changed: *b |
| 2960 | 2960 | return buffer.toOwnedSlice(); |
| 2961 | 2961 | } |
| 2962 | 2962 | |
| 2963 | fn printLine(line: []const u8) void { | |
| 2964 | warn("{}⏎\n", .{line}); // Carriage return symbol | |
| 2965 | } | |
| 2966 | ||
| 2967 | fn printIndicatorLine(source: []const u8, indicator_index: usize) void { | |
| 2968 | const line_begin_index = if (mem.lastIndexOfScalar(u8, source[0..indicator_index], '\n')) |line_begin| | |
| 2969 | line_begin + 1 | |
| 2970 | else | |
| 2971 | 0; | |
| 2972 | const line_end_index = if (mem.indexOfScalar(u8, source[indicator_index..], '\n')) |line_end| | |
| 2973 | (indicator_index + line_end) | |
| 2974 | else | |
| 2975 | (source.len - 1); | |
| 2976 | printLine(source[line_begin_index..line_end_index]); | |
| 2977 | { | |
| 2978 | var i: usize = line_begin_index; | |
| 2979 | while (i < indicator_index) : (i += 1) | |
| 2980 | warn(" ", .{}); | |
| 2981 | } | |
| 2982 | warn("^~~~~\n", .{}); | |
| 2983 | } | |
| 2984 | ||
| 2985 | fn printWithVisibleNewlines(source: []const u8) void { | |
| 2986 | var i: usize = 0; | |
| 2987 | while (mem.indexOf(u8, source[i..], "\n")) |nl| : (i += nl + 1) { | |
| 2988 | printLine(source[i .. i + nl]); | |
| 2989 | } | |
| 2990 | warn("{}{}\n", .{ source[i..], "\u{2403}" }); // End of Text symbol (ETX) | |
| 2991 | } | |
| 2992 | ||
| 2993 | 2963 | fn testTransform(source: []const u8, expected_source: []const u8) !void { |
| 2994 | 2964 | const needed_alloc_count = x: { |
| 2995 | 2965 | // Try it once with unlimited memory, make sure it works |
| ... | ... | @@ -2997,27 +2967,7 @@ fn testTransform(source: []const u8, expected_source: []const u8) !void { |
| 2997 | 2967 | var failing_allocator = std.testing.FailingAllocator.init(&fixed_allocator.allocator, maxInt(usize)); |
| 2998 | 2968 | var anything_changed: bool = undefined; |
| 2999 | 2969 | const result_source = try testParse(source, &failing_allocator.allocator, &anything_changed); |
| 3000 | if (mem.indexOfDiff(u8, result_source, expected_source)) |diff_index| { | |
| 3001 | warn("\n====== expected this output: =========\n", .{}); | |
| 3002 | printWithVisibleNewlines(expected_source); | |
| 3003 | warn("\n======== instead found this: =========\n", .{}); | |
| 3004 | printWithVisibleNewlines(result_source); | |
| 3005 | warn("\n======================================\n", .{}); | |
| 3006 | ||
| 3007 | var diff_line_number: usize = 1; | |
| 3008 | for (expected_source[0..diff_index]) |value| { | |
| 3009 | if (value == '\n') diff_line_number += 1; | |
| 3010 | } | |
| 3011 | warn("First difference occurs on line {}:\n", .{diff_line_number}); | |
| 3012 | ||
| 3013 | warn("expected:\n", .{}); | |
| 3014 | printIndicatorLine(expected_source, diff_index); | |
| 3015 | ||
| 3016 | warn("found:\n", .{}); | |
| 3017 | printIndicatorLine(result_source, diff_index); | |
| 3018 | ||
| 3019 | return error.TestFailed; | |
| 3020 | } | |
| 2970 | std.testing.expectEqualStrings(expected_source, result_source); | |
| 3021 | 2971 | const changes_expected = source.ptr != expected_source.ptr; |
| 3022 | 2972 | if (anything_changed != changes_expected) { |
| 3023 | 2973 | warn("std.zig.render returned {} instead of {}\n", .{ anything_changed, changes_expected }); |