ibokuri/protest
A set of modules for easy testing in Zig
Protest is a set of modules for testing and validating Zig code. A testify for Zig, if you will, cause testify's awesome!
require
ModuleThe require
module some provides helpful functions to help you write tests.
const require = @import("protest").require;
test {
// Require equality.
try require.equalf(123, 123, "They should be {s}", .{"equal"});
// Require inequality.
try require.notEqualf(123, 456, "They should not be {s}", .{"equal"});
// Require that `value` is not null.
try require.notNull(value);
// Since `value` cannot be null, safely unwrap it and check its payload.
try require.equal("Foobar", value.?);
}
run test: error: 'test_0' failed:
Error: Not equal:
expected: "Foobar"
actual: "Barfoo"
Error Trace:
/tmp/example/src/main.zig:14:5: 0x1048a5027 in test_0 (test)
try require.equal("Foobar", value.?);
^
Declare Protest as a dependency in build.zig.zon
:
.{
.name = "my-project",
.version = "1.0.0",
.paths = .{""},
.dependencies = .{
+ .protest = .{
+ .url = "https://github.com/ibokuri/protest/archive/<COMMIT>.tar.gz",
+ },
},
}
Add Protest as a module in build.zig
:
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
+ const opts = .{ .target = target, .optimize = optimize };
+ const protest_mod = b.dependency("protest", opts).module("protest");
const tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
+ tests.addModule("protest", protest_mod);
...
}
Obtain Protest's package hash:
$ zig build --fetch
my-project/build.zig.zon:7:20: error: url field is missing corresponding hash field
.url = "https://github.com/ibokuri/protest/archive/<COMMIT>.tar.gz",
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: expected .hash = "<HASH>",
Update build.zig.zon
with Protest's package hash:
.{
.name = "my-project",
.version = "1.0.0",
.paths = .{""},
.dependencies = .{
.protest = .{
.url = "https://github.com/ibokuri/protest/archive/<COMMIT>.tar.gz",
+ .hash = "<HASH>",
},
},
}