← All examples

Tests

A test fn takes nothing and returns nothing; zomp test runs each one.

testing.zg
# Tests (owner, 2026-09-29): a `test fn` takes nothing and returns nothing; `zomp test` runs each one.
# `must` states what must hold (a comparison shows both sides when it does not), `must.fail` that
# something fails (`.not_a_number`: with that reason), `must.stop` that a check site fires. Two of
# these fail on purpose. A normal run ignores the tests and runs MAIN.
fn add ['a': int32, 'b': int32] -> int32 { return 'a' plus 'b'; }
fn wrong ['a': int32, 'b': int32] -> int32 { return 'a' plus 'b' plus 1; }
test fn adds_up [] {
    must add[2, 3] equal_to 5;
    must add[2, 3] > 4;
}
test fn catches_a_bug [] {
    must wrong[2, 3] equal_to 5;
}
test fn parsing [] {
    let 'n' := to.int32["42"] otherwise 0;
    must 'n' equal_to 42;
    must.fail to.int32["abc"];
    must.fail.not_a_number to.int32["abc"];
}
test fn wrong_reason [] {
    must.fail.out_of_range to.int32["abc"];
}
test fn guards [] {
    let 'xs': list[int32] = [1, 2, 3];
    must.stop 'xs'[pos 99];
}
MAIN { std::print.useful[add[1, 1], newln]; }
Output, then zomp test
2
test adds_up ... ok
test catches_a_bug ... FAILED: line 12 of testing.zg: `must` does not hold: the left side is 6, and it must be `equal_to` 5
test parsing ... ok
test wrong_reason ... FAILED: line 21 of testing.zg: `must.fail.out_of_range`: it failed with `not_a_number`
test guards ... ok
5 tests: 3 passed, 2 failed