authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-12-08 21:54:46-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-12-08 21:59:24-07:00
log4592fd26b937d8c7ff91295408d8377c1c13cf53
treef310569348c6115b0995eec2aeaa21ff3b111c5c
parent72f6c6e6345030392a3f8cac79862d58359f1e76

add std.testing.expectStringEndsWith


2 files changed, 23 insertions(+), 2 deletions(-)

lib/std/testing.zig+21
......@@ -247,6 +247,7 @@ test "expectWithinEpsilon" {
247247/// This function is intended to be used only in tests. When the two slices are not
248248/// equal, prints diagnostics to stderr to show exactly how they are not equal,
249249/// then aborts.
250/// If your inputs are UTF-8 encoded strings, consider calling `expectEqualStrings` instead.
250251pub fn expectEqualSlices(comptime T: type, expected: []const T, actual: []const T) void {
251252 // TODO better printing of the difference
252253 // If the arrays are small enough we could print the whole thing
......@@ -368,6 +369,26 @@ pub fn expectEqualStrings(expected: []const u8, actual: []const u8) void {
368369 }
369370}
370371
372pub fn expectStringEndsWith(actual: []const u8, expected_ends_with: []const u8) void {
373 if (std.mem.endsWith(u8, actual, expected_ends_with))
374 return;
375
376 const shortened_actual = if (actual.len >= expected_ends_with.len)
377 actual[0..expected_ends_with.len]
378 else
379 actual;
380
381 print("\n====== expected to end with: =========\n", .{});
382 printWithVisibleNewlines(expected_ends_with);
383 print("\n====== instead ended with: ===========\n", .{});
384 printWithVisibleNewlines(shortened_actual);
385 print("\n========= full output: ==============\n", .{});
386 printWithVisibleNewlines(actual);
387 print("\n======================================\n", .{});
388
389 @panic("test failure");
390}
391
371392fn printIndicatorLine(source: []const u8, indicator_index: usize) void {
372393 const line_begin_index = if (std.mem.lastIndexOfScalar(u8, source[0..indicator_index], '\n')) |line_begin|
373394 line_begin + 1
test/cli.zig+2-2
......@@ -92,13 +92,13 @@ fn exec(cwd: []const u8, expect_0: bool, argv: []const []const u8) !ChildProcess
9292fn testZigInitLib(zig_exe: []const u8, dir_path: []const u8) !void {
9393 _ = try exec(dir_path, true, &[_][]const u8{ zig_exe, "init-lib" });
9494 const test_result = try exec(dir_path, true, &[_][]const u8{ zig_exe, "build", "test" });
95 testing.expect(std.mem.endsWith(u8, test_result.stderr, "All 1 tests passed.\n"));
95 testing.expectStringEndsWith(test_result.stderr, "All 1 tests passed.\n");
9696}
9797
9898fn testZigInitExe(zig_exe: []const u8, dir_path: []const u8) !void {
9999 _ = try exec(dir_path, true, &[_][]const u8{ zig_exe, "init-exe" });
100100 const run_result = try exec(dir_path, true, &[_][]const u8{ zig_exe, "build", "run" });
101 testing.expect(std.mem.eql(u8, run_result.stderr, "info: All your codebase are belong to us.\n"));
101 testing.expectEqualStrings("info: All your codebase are belong to us.\n", run_result.stderr);
102102}
103103
104104fn testGodboltApi(zig_exe: []const u8, dir_path: []const u8) anyerror!void {