Skip to content
CryoCryo home
Stdlibtest

table

import std::test::table; · source

Table-driven tests

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

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.