authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-10-08 17:02:59+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-10-15 14:00:08+02:00
logdc44baf763f38ba3735d45fc4f633cac13949b0e
tree94017201182ca6009fd1d43a89ba711767f3eee6
parent839a93a101804bb41cb519a965dc62e3eb7deed0
signature Signed by SSH key SHA256:CQ99aPxq+RueiL9u7z0FEki5Fm7V6T8q4PrEGmINrA4

std.testing: allow print() at comptime

This allows functions like expectEqual to be performed at comptime. If an error is detected, the result is logged via a compile error.

1 files changed, 7 insertions(+), 3 deletions(-)

lib/std/testing.zig+7-3
...@@ -22,10 +22,14 @@ pub var base_allocator_instance = std.heap.FixedBufferAllocator.init("");...@@ -22,10 +22,14 @@ pub var base_allocator_instance = std.heap.FixedBufferAllocator.init("");
22pub var log_level = std.log.Level.warn;22pub var log_level = std.log.Level.warn;
2323
24fn print(comptime fmt: []const u8, args: anytype) void {24fn print(comptime fmt: []const u8, args: anytype) void {
25 // Disable printing in tests for simple backends.25 if (@inComptime()) {
26 if (builtin.zig_backend == .stage2_spirv64) return;26 @compileError(std.fmt.comptimePrint(fmt, args));
27 } else {
28 // Disable printing in tests for simple backends.
29 if (builtin.zig_backend == .stage2_spirv64) return;
2730
28 std.debug.print(fmt, args);31 std.debug.print(fmt, args);
32 }
29}33}
3034
31/// This function is intended to be used only in tests. It prints diagnostics to stderr35/// This function is intended to be used only in tests. It prints diagnostics to stderr