--- layout: site.njk title: zul.testing ---
Helpers for writing tests.
zul.testing directly re-exports some functionality from std.testing.. This is to minimize the number of cases where both std.testing and zul.testing are needed. The following variables and functions are available under zul.testing:
arena: std.heap.ArenaAllocator
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:
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.
Asserts that expected is within delta of actual. Unlike the std.testing.expectApproxEq* functions, expectDelta works with both integers and floats.
Similar to std.testing.expectEqual, but will coerce expected to actual and can be used to compare strings.
Resets the zul.testing.arena. Typically called in a defer atop the test when the zul.testing.arena is used.
Helpers to generate random data.
Populates a []u8 with random bytes. The created []u8 will be between min and max bytes in length (inclusive). It is created using the zul.testing.arena so reset should be called.
Fill buf with random bytes. Because this only fills buf, overwriting any previous data, and doesn't allocate, in a tight loop, it can be much faster than bytes.
Fill buf with random bytes. Returns a slice that is between min and buf.len bytes in length (inclusive). Because this only fills buf, overwriting any previous data, and doesn't allocate, in a tight loop, it can be much faster than bytes.
Returns an integer of type T that is between min and max inclusive. This is a wrapper around std.RandomintRangeAtMost.
Returns a randomly seeded std.Random instance.