Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ibokuri/protest

A set of modules for easy testing in Zig
https://github.com/ibokuri/protest

assert assertions require testing zig ziglang

Last synced: about 2 months ago
JSON representation

A set of modules for easy testing in Zig

Awesome Lists containing this project

README

        

# Protest

Version
Build status
Zig
License

Protest is a set of modules for testing and validating Zig code. A
[testify](https://github.com/stretchr/testify) for Zig, if you will, cause testify's awesome!

## [`require`](https://ibokuri.github.io/protest/#A;protest: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.

```zig
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`:

```diff
.{
.name = "my-project",
.version = "1.0.0",
.paths = .{""},
.dependencies = .{
+ .protest = .{
+ .url = "https://github.com/ibokuri/protest/archive/.tar.gz",
+ },
},
}
```

2. Add Protest as a module in `build.zig`:

```diff
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/.tar.gz",
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: expected .hash = "",
```

4. Update `build.zig.zon` with Protest's package hash:

```diff
.{
.name = "my-project",
.version = "1.0.0",
.paths = .{""},
.dependencies = .{
.protest = .{
.url = "https://github.com/ibokuri/protest/archive/.tar.gz",
+ .hash = "",
},
},
}
```