Complex tests often require their own allocation. This test-only data is particularly well suited for an ArenaAllocator.
Take care when using the arena. While it can streamline tests, it's easy to abuse. Code under test should use zul.testing.allocator
(which is std.testing.allocator
) so that leaks can be properly detected. Consider this slightly modified example taken from a real readLines
test:
const t = zul.testing;
test "readLines" {
defer t.reset();
const aa = t.arena.allocator();
const path = try std.fs.cwd().realpathAlloc(aa, "tests/sample");
var out: [30]u8 = undefined;
var it = try readLines(path, &out, .{});
defer it.deinit();
try t.expectEqual("Consider Phlebas", it.next().?);
try t.expectEqual("Old Man's War", it.next().?);
try t.expectEqual(null, it.next());
}
path
is clearly data that belongs to the test and its lifecycle isn't something that our function under test, readLines
, should be concerned with. If however, readLInes
took some type of ownership over path
, then zul.testing.allocator
should have been used.