From a021c7b1b2428ecda85e79e281d43fa1c92f8c94 Mon Sep 17 00:00:00 2001 From: Jay Petacat Date: Mon, 11 Jan 2021 20:30:43 -0500 Subject: Move fmt.testFmt to testing.expectFmt --- lib/std/testing.zig | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'lib/std/testing.zig') 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. -- cgit v1.2.3