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" {...@@ -247,6 +247,7 @@ test "expectWithinEpsilon" {
247/// This function is intended to be used only in tests. When the two slices are not247/// This function is intended to be used only in tests. When the two slices are not
248/// equal, prints diagnostics to stderr to show exactly how they are not equal,248/// equal, prints diagnostics to stderr to show exactly how they are not equal,
249/// then aborts.249/// then aborts.
250/// If your inputs are UTF-8 encoded strings, consider calling `expectEqualStrings` instead.
250pub fn expectEqualSlices(comptime T: type, expected: []const T, actual: []const T) void {251pub fn expectEqualSlices(comptime T: type, expected: []const T, actual: []const T) void {
251 // TODO better printing of the difference252 // TODO better printing of the difference
252 // If the arrays are small enough we could print the whole thing253 // 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 {...@@ -368,6 +369,26 @@ pub fn expectEqualStrings(expected: []const u8, actual: []const u8) void {
368 }369 }
369}370}
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
371fn printIndicatorLine(source: []const u8, indicator_index: usize) void {392fn printIndicatorLine(source: []const u8, indicator_index: usize) void {
372 const line_begin_index = if (std.mem.lastIndexOfScalar(u8, source[0..indicator_index], '\n')) |line_begin|393 const line_begin_index = if (std.mem.lastIndexOfScalar(u8, source[0..indicator_index], '\n')) |line_begin|
373 line_begin + 1394 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...@@ -92,13 +92,13 @@ fn exec(cwd: []const u8, expect_0: bool, argv: []const []const u8) !ChildProcess
92fn testZigInitLib(zig_exe: []const u8, dir_path: []const u8) !void {92fn testZigInitLib(zig_exe: []const u8, dir_path: []const u8) !void {
93 _ = try exec(dir_path, true, &[_][]const u8{ zig_exe, "init-lib" });93 _ = try exec(dir_path, true, &[_][]const u8{ zig_exe, "init-lib" });
94 const test_result = try exec(dir_path, true, &[_][]const u8{ zig_exe, "build", "test" });94 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");
96}96}
9797
98fn testZigInitExe(zig_exe: []const u8, dir_path: []const u8) !void {98fn testZigInitExe(zig_exe: []const u8, dir_path: []const u8) !void {
99 _ = try exec(dir_path, true, &[_][]const u8{ zig_exe, "init-exe" });99 _ = try exec(dir_path, true, &[_][]const u8{ zig_exe, "init-exe" });
100 const run_result = try exec(dir_path, true, &[_][]const u8{ zig_exe, "build", "run" });100 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);
102}102}
103103
104fn testGodboltApi(zig_exe: []const u8, dir_path: []const u8) anyerror!void {104fn testGodboltApi(zig_exe: []const u8, dir_path: []const u8) anyerror!void {