diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2020-12-08 21:54:46 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2020-12-08 21:59:24 -0700 |
| commit | 4592fd26b937d8c7ff91295408d8377c1c13cf53 (patch) | |
| tree | f310569348c6115b0995eec2aeaa21ff3b111c5c /lib/std/testing.zig | |
| parent | 72f6c6e6345030392a3f8cac79862d58359f1e76 (diff) | |
| download | zig-4592fd26b937d8c7ff91295408d8377c1c13cf53.tar.gz zig-4592fd26b937d8c7ff91295408d8377c1c13cf53.zip | |
add std.testing.expectStringEndsWith
Diffstat (limited to 'lib/std/testing.zig')
| -rw-r--r-- | lib/std/testing.zig | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/lib/std/testing.zig b/lib/std/testing.zig index 8ab4e802ab..69df01190d 100644 --- a/lib/std/testing.zig +++ b/lib/std/testing.zig @@ -247,6 +247,7 @@ test "expectWithinEpsilon" { /// This function is intended to be used only in tests. When the two slices are not /// equal, prints diagnostics to stderr to show exactly how they are not equal, /// then aborts. +/// If your inputs are UTF-8 encoded strings, consider calling `expectEqualStrings` instead. pub fn expectEqualSlices(comptime T: type, expected: []const T, actual: []const T) void { // TODO better printing of the difference // 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 { } } +pub fn expectStringEndsWith(actual: []const u8, expected_ends_with: []const u8) void { + if (std.mem.endsWith(u8, actual, expected_ends_with)) + return; + + const shortened_actual = if (actual.len >= expected_ends_with.len) + actual[0..expected_ends_with.len] + else + actual; + + print("\n====== expected to end with: =========\n", .{}); + printWithVisibleNewlines(expected_ends_with); + print("\n====== instead ended with: ===========\n", .{}); + printWithVisibleNewlines(shortened_actual); + print("\n========= full output: ==============\n", .{}); + printWithVisibleNewlines(actual); + print("\n======================================\n", .{}); + + @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 |
