Testing module

run_cases

procedure · 1 overloadsince 0.30.0Source

Runs body once per entry of cases, each as the subtest test/<values>; returns whether every case passed. An entry is the argument of body, or, when body takes several, a struct whose members are its arguments in order. body may take a leading *Test. This is what a @test procedure with a @cases(LIST) note runs; call it by hand to build the list at run time or to run a helper per case:

Example
Case :: struct { input: string; length: s64; }

checks_each_case :: (t: *Test) {
    lengths :: (input: string, length: s64) {
        expect_equal(input.count, length);
    }

    passed := run_cases(t, Case.[.{ "", 0 }, .{ "abc", 3 }], lengths);
    expect(passed);
}
run_cases :: (t: *Test, cases: [] $T, $body: $B) -> bool

Runs body once per entry of cases, each as the subtest test/<values>; returns whether every case passed. An entry is the argument of body, or, when body takes several, a struct whose members are its arguments in order. body may take a leading *Test. This is what a @test procedure with a @cases(LIST) note runs; call it by hand to build the list at run time or to run a helper per case:

Parameters

t: *Test
the running test.
cases: [] $T
the entries to run, one subtest each.
$body: $B
the procedure each entry is passed to.

Returns

bool
Example
Case :: struct { input: string; length: s64; }

checks_each_case :: (t: *Test) {
    lengths :: (input: string, length: s64) {
        expect_equal(input.count, length);
    }

    passed := run_cases(t, Case.[.{ "", 0 }, .{ "abc", 3 }], lengths);
    expect(passed);
}

See also