Skip to content

ibokuri/protest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

76 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Protest

Version Build status Zig License

Protest is a set of modules for testing and validating Zig code. A testify for Zig, if you will, cause testify's awesome!

require Module

The require module some provides helpful functions to help you write tests.

  • Descriptive and easy to read failure descriptions.
  • Simplified testing code.
  • Requirements can be annotated with a custom message.
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.?);
    ^

Installation

  1. 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",
    +       },
        },
    }
  2. 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);
    
        ...
    }
  3. 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>",
    
  4. 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>",
            },
        },
    }