authorgravatar for twostepted@gmail.comTravis Staloch <twostepted@gmail.com> 2024-02-26 19:08:27-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-27 14:40:28-08:00
log30bf8d7147a337444df51926199b70dc152a8283
tree31dbeebfeef9faff6994cac4b03ae6464a0abefb
parentbd0dbb0a13d075a1ea7e681a242288e307648559

testing.expectFmt() - reuse expectEqualStrings()


1 files changed, 5 insertions(+), 11 deletions(-)

lib/std/testing.zig+5-11
......@@ -210,18 +210,12 @@ test "expectEqual.union(enum)" {
210210
211211/// This function is intended to be used only in tests. When the formatted result of the template
212212/// and its arguments does not equal the expected text, it prints diagnostics to stderr to show how
213/// they are not equal, then returns an error.
213/// they are not equal, then returns an error. It depends on `expectEqualStrings()` for printing
214/// diagnostics.
214215pub fn expectFmt(expected: []const u8, comptime template: []const u8, args: anytype) !void {
215 const result = try std.fmt.allocPrint(allocator, template, args);
216 defer allocator.free(result);
217 if (std.mem.eql(u8, result, expected)) return;
218
219 print("\n====== expected this output: =========\n", .{});
220 print("{s}", .{expected});
221 print("\n======== instead found this: =========\n", .{});
222 print("{s}", .{result});
223 print("\n======================================\n", .{});
224 return error.TestExpectedFmt;
216 const actual = try std.fmt.allocPrint(allocator, template, args);
217 defer allocator.free(actual);
218 return expectEqualStrings(expected, actual);
225219}
226220
227221/// This function is intended to be used only in tests. When the actual value is