#Custom format not shown in test

1 messages · Page 1 of 1 (latest)

copper willow
#
const std = @import("std");
const print = std.debug.print;

const Person = struct {
    name: []const u8,
    birth_year: i32,
    death_year: ?i32,

    pub fn format(
        self: Person,
        comptime fmt: []const u8,
        options: std.fmt.FormatOptions,
        writer: anytype,
    ) !void {
        _ = fmt;
        _ = options;
        try writer.print("{s} ({d}-", .{ self.name, self.birth_year });
        try writer.writeAll(")");
    }
};

test "custom printing" {
    const j = Person{
        .name = "John",
        .birth_year = 1987,
        .death_year = undefined,
    };
    print("j: {s}", .{j});
    print("TEST", .{});
}

Even the last print is not shown, so I suspect it has to do with the way I invoke zig test or something relevant, but the goal is to see the custom format of the Person struct on the screen.

brisk rapids
#

print a newline before anything else and it'll appear

#

it's getting overwritten by the progress bar & status

#

though I would recommend you not rely on printing in tests for anything other than debugging

#

unit tests are generally meant to be declarative

#

if your aim is to test the behaviour of the formatting function, you can do:

try std.testing.expectFmt("j: John (1987)-)", "j: {}", .{j});