Contents

Test blocks are top-level blocks that the compiler can compile into a runnable program. A normal compile ignores them; hz test collects and runs them.

Test Blocks#

A test has a string literal name and a block body.

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

The name must be a plain string, not an interpolated string. Test blocks are top-level only. They may contain ordinary statements, local declarations, loops, matches, calls, and defer.

Assertions#

assert takes a boolean expression and is only allowed inside a test block. A false assertion aborts with a message that includes the test name, file, and line.

test "range covers the half-open interval" {
    assert range(0, 0).len == 0
    assert range(0, 3).len == 3
}

An assertion is a concrete check, not a contract. It runs once per test execution. Use Result for expected failures and a contract for a property that must hold for every input.

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.

hz test tests/account_test.hz
hz test tests

A file with no test blocks is skipped. A file whose tests abort is reported as failed, and the runner continues with the next file. On success the output includes a count such as ok: 3 test(s) in 1 file(s).

The generated program renames a user main to an internal function and does not call it. Tests run in source order. Each test is a separate generated function, so an abort in one test stops that file's run; tests are not isolated in separate processes.

Tests and Modules#

Only the entry module's tests are retained by the normal module loader. Tests in an imported module are not run when the importer is tested. To run a module's own tests, pass that module's file to hz test or pass a directory containing it.

use account {Account, deposit}

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

A test file is a module and follows the same import and visibility rules. A test helper is an ordinary function.

Release Mode#

-release removes runtime contract and invariant checks. It does not remove test assertions or index bounds checks. A test run with -release still checks its assertions, but contracts on the functions it calls are not enforced at runtime.

Contracts, Tests, and Helpers#

A test should exercise behaviour the contract does not. A contract says every output is sorted; a test supplies a list and checks the exact order. A contract says checked construction returns an error for an invalid invariant; a test supplies an invalid runtime value and asserts the error.

Tool Scope Failure Best use
Contract Every call or construction Compile error or abort Universal properties
assert One test execution Abort Concrete examples
Result Every call Returned value Expected failure
Option Every call None Expected absence

Do not duplicate a contract as an assertion. A call with a constant that violates a precondition does not compile; a call with a runtime value that violates it aborts at the contract.

Testing Error Paths#

A Result is tested with is or match. is is concise when only the variant matters; match is used when the payload matters.

test "parse rejects an empty string" {
    assert parse_quantity("") is Error(Empty)
}

test "parse reports the failing digit" {
    const result = parse_quantity("12x")
    if result is Error(reason) {
        match reason {
            InvalidDigit(position, character) => {
                assert position == 2
                assert character == 120
            }
            _ => {
                assert false
            }
        }
    } else {
        assert false
    }
}

A checked construction that needs a runtime check must be placed in a helper that returns Result[_, str], because the generated test function returns void.

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

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

Setup and Teardown#

There are no setup or teardown hooks. A test declares its own state and uses defer for cleanup.

test "defer runs on every exit path" {
    var cleaned = false
    defer {
        cleaned = true
    }
    assert !cleaned
}

The assertion runs before the test function returns, so it observes the pre-cleanup state.

Organisation#

Layout Advantage
Tests in the same file as the code The test is close to the contract and the implementation
A tests/ directory with one file per module Runner output is easy to read and each file compiles independently
A dedicated test module imported by a test file Shared fixtures are explicit and reusable
Tests in an imported module Not run by the importer; pass the module file to hz test

The runner treats every file as an entry, so a test file must be a valid module. It does not need a main unless it also compiles as a normal program.

Summary#

Rule Detail
Test syntax test "name" { ... }, top level only
Assertion assert expr, test blocks only
Runner hz test file-or-directory
Order Source order within a file
Isolation An abort stops the file's run; the runner continues with the next file
Imported tests Not run implicitly
Release mode Removes contract and invariant checks, not assertions or bounds checks
Setup and teardown No hooks; use locals and defer
Failure model Assertions abort; expected failures use Result