aboutsummaryrefslogtreecommitdiff
path: root/lib/std/testing.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2020-12-08 21:54:46 -0700
committerAndrew Kelley <andrew@ziglang.org>2020-12-08 21:59:24 -0700
commit4592fd26b937d8c7ff91295408d8377c1c13cf53 (patch)
treef310569348c6115b0995eec2aeaa21ff3b111c5c /lib/std/testing.zig
parent72f6c6e6345030392a3f8cac79862d58359f1e76 (diff)
downloadzig-4592fd26b937d8c7ff91295408d8377c1c13cf53.tar.gz
zig-4592fd26b937d8c7ff91295408d8377c1c13cf53.zip
add std.testing.expectStringEndsWith
Diffstat (limited to 'lib/std/testing.zig')
-rw-r--r--lib/std/testing.zig21
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