Skip to content
CryoCryo home
Stdlibtest

table

import std::test::table; · source

Free Functions

Table-driven tests

v1.0 uses the inline-loop pattern: the test owns a loop over a const array of cases and calls the ordinary assertions on each row. The helpers here turn a per-row failure into a message naming the offending case, so a failure in row 7 reads case [7]: ... instead of an anonymous assertion failure.

type struct Case { input: i32; want: i32; }

![test]
function doubling_table() -> Result<(), TestError> {
    const cases: [Case] = [
        Case { input: 1, want: 2 },
        Case { input: 2, want: 4 },
        Case { input: 3, want: 6 },
    ];
    mut i: i64 = 0;
    while (i < cases.length) {
        const c: &Case = &cases[i];
        match (expect_eq(c.input * 2, c.want)) {
            Result::Ok(_)  => { }
            Result::Err(e) => { return Result::Err(prefix_case(i as u64, e)); }
        }
        i = i + 1;
    }
    return Result::Ok(());
}

prefix_named_case labels the row by name rather than index.

function prefix_case(index: u64, e: TestError) -> TestError;

Wrap e's message with a case [<index>]: prefix, preserving the error's variant (Failed vs Other). Consumes e and returns the new error; call it on the failing-row path: return Result::Err(prefix_case(i as u64, e));

function prefix_named_case(name: Str, e: TestError) -> TestError;

Like prefix_case but uses a caller-supplied case label instead of a numeric index: case [empty input]: .... Useful when the table rows carry their own names.