diff options
| author | Jay Petacat <jay@jayschwa.net> | 2021-01-11 20:30:43 -0500 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-01-12 18:13:29 -0800 |
| commit | a021c7b1b2428ecda85e79e281d43fa1c92f8c94 (patch) | |
| tree | 069163111972ebb708431098f97b07fd91581a11 /lib/std/testing.zig | |
| parent | 2c79d669a7ad802c795583cf66bca1fffc8ce23e (diff) | |
| download | zig-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.zig | 16 |
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. |
