Contents

Contracts state properties that must hold for every value in a domain. Tests check concrete examples.

Test Blocks#

A test is a top-level block with a string name.

test "addition is commutative" {
    assert add(2, 3) == add(3, 2)
}

Tests may contain ordinary statements, declarations, loops, matches, and calls. assert is allowed only inside a test block. It takes a boolean expression and aborts if it is false.

test "checked construction rejects zero" {
    assert to_positive(0) is Error(_)
    assert to_positive(1) is Success(_)
}

Use is to assert a variant without unwrapping the payload. Bind the payload when its value matters.

test "the error names the failing invariant" {
    const result = to_positive(0)
    match result {
        Success(_) => {
            assert false
        }
        Error(reason) => {
            assert reason == "invariant failed for 'Positive'"
        }
    }
}

A failing assertion aborts with the test name and source location. The runner reports the file as failed and continues with the next file.

Running Tests#

hz test path accepts a file or a directory. A directory is searched recursively for .hz files. Each file is loaded as an entry module, its tests are compiled into a generated main, and the program is run. A file with no tests is skipped.

hz test tests/account_test.hz
hz test tests

On success the runner prints a count such as ok: 3 test(s) in 1 file(s). On failure it prints the failing file and exits non-zero.

Tests in imported modules are not run when the importer is tested. Pass the module's file or a directory that includes it.

Assertions and Release Mode#

assert is a test-only statement and is not removed by -release. A release build of a normal program has no assert statements; the flag only affects contract and invariant checks.

Do not use assert as a substitute for a contract in production code. It is not available outside a test.

What to Test#

Area Example cases
Precondition boundary The smallest accepted value, the largest accepted value, and the first rejected value
Postcondition A result that satisfies the guarantee and a case where the relationship is meaningful
Refinement construction A valid value, an invalid value, and a value that becomes valid after a cast
Record invariant A valid combination and a combination that violates a cross-field rule
Checked construction Success for a valid input and Error for an invalid one
Result propagation The success path, the error path, and an error from a nested call
Loop invariant An empty range, a single iteration, and the terminal boundary
Defer Cleanup on normal exit, early return, and error propagation
test "range length includes the terminal check" {
    assert range(0, 0).len == 0
    assert range(0, 1).len == 1
    assert range(1, 4).len == 3
}

Refinements and Checked Construction#

The compiler rejects constant violations, so a test that exercises the runtime path must use a value the compiler cannot fold. A checked construction that needs a runtime check must be in a helper that returns Result.

fn build(n i32) Result[Positive, str] => Success(Positive { n }?)

test "checked construction boundary" {
    assert build(0) is Error(_)
    assert build(1) is Success(_)
}

Avoiding Duplication#

Do not rewrite every precondition as a test assertion. The compiler checks constant calls and emits runtime checks for the rest. A test that calls a function with an invalid constant will not compile; a test that calls it with an invalid runtime value will abort at the contract.

Use tests for behaviour the contract does not state: an error message, a match branch, deferred cleanup order, or a property too complex for a contract expression. Use contracts for properties that must hold across every call.

Organisation#

A test may live with the code it tests. For a larger project, a tests/ directory with one file per module keeps runner output readable and lets each file compile independently. A test file is a module and follows the same import and visibility rules as production code.

use account {Account, deposit, withdraw}

test "deposit increases the balance" {
    const account = Account { id = 1, balance = 100 }
    const next = deposit(account, 25)
    assert next.balance == 125
}

test "withdraw rejects an overdraft" {
    const account = Account { id = 1, balance = 10 }
    assert withdraw(account, 50) is Error(_)
}

Summary#

Tool Scope Failure Use for
Precondition Every call Compile error or abort Caller obligations
Postcondition Every return Compile error or abort Callee guarantees
Type invariant Every construction Compile error or Error from ? Valid values
Loop invariant Every header Compile error or abort Loop correctness
assert One test execution Abort Concrete examples
Result Every call Returned value Expected failure