aboutsummaryrefslogtreecommitdiff
path: root/lib/std/testing.zig
diff options
context:
space:
mode:
authorJay Petacat <jay@jayschwa.net>2021-01-11 20:30:43 -0500
committerAndrew Kelley <andrew@ziglang.org>2021-01-12 18:13:29 -0800
commita021c7b1b2428ecda85e79e281d43fa1c92f8c94 (patch)
tree069163111972ebb708431098f97b07fd91581a11 /lib/std/testing.zig
parent2c79d669a7ad802c795583cf66bca1fffc8ce23e (diff)
downloadzig-a021c7b1b2428ecda85e79e281d43fa1c92f8c94.tar.gz
zig-a021c7b1b2428ecda85e79e281d43fa1c92f8c94.zip
Move fmt.testFmt to testing.expectFmt
Diffstat (limited to 'lib/std/testing.zig')
-rw-r--r--lib/std/testing.zig16
1 files changed, 16 insertions, 0 deletions
diff --git a/lib/std/testing.zig b/lib/std/testing.zig
index 4f4a46dbf7..288eb5b662 100644
--- a/lib/std/testing.zig
+++ b/lib/std/testing.zig
@@ -184,6 +184,22 @@ test "expectEqual.union(enum)" {
expectEqual(a10, a10);
}
+/// This function is intended to be used only in tests. When the formatted result of the template
+/// and its arguments does not equal the expected text, it prints diagnostics to stderr to show how
+/// they are not equal, then returns an error.
+pub fn expectFmt(expected: []const u8, comptime template: []const u8, args: anytype) !void {
+ const result = try std.fmt.allocPrint(allocator, template, args);
+ defer allocator.free(result);
+ if (std.mem.eql(u8, result, expected)) return;
+
+ print("\n====== expected this output: =========\n", .{});
+ print("{s}", .{expected});
+ print("\n======== instead found this: =========\n", .{});
+ print("{s}", .{result});
+ print("\n======================================\n", .{});
+ return error.TestFailed;
+}
+
/// This function is intended to be used only in tests. When the actual value is not
/// within the margin of the expected value,
/// prints diagnostics to stderr to show exactly how they are not equal, then aborts.