Contents

Haze tests are top-level blocks that the compiler can compile into a runnable program. A normal compile ignores them; hz test collects them 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. It is only allowed inside a test block. A false assertion panics 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 and does not protect a production call. 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 resulting program is run.

hz test tests/account_test.hz
hz test tests

A file with no test blocks is skipped. A file whose tests panic 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. The tests run in source order. Each test is a separate generated function, so a panic in one test stops that file's run; the runner does not isolate each test in a separate process.

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 still a module and follows the same import and visibility rules. It may import the module under test, and it may define local helpers. A test helper is an ordinary function, not a special kind of declaration.

Assertions and Release Mode#

-release removes runtime contract and invariant checks from the generated program. It does not remove test assertions or index bounds checks. A test run with -release therefore still checks its assertions, but the contracts on the functions it calls are no longer enforced at runtime. Use release mode for tests only when the difference is deliberate.

Contracts, Tests, and Helpers#

A test should exercise the behaviour that the contract does not. A contract says that every output is sorted; a test supplies a particular list and checks the exact order. A contract says that a 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 panic Universal properties
assert One test execution Panic 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, and a call with a runtime value that violates it panics at the contract rather than reaching the assertion. Test the behaviour the contract leaves open.

Testing Error Paths#

A Result is tested with is or match. The is form is concise when only the variant matters; the match form 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]. The generated test function returns void, so the test calls the helper and asserts on its 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(_)
}

Setup and Teardown#

Haze does not have a test setup or teardown hook. 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. A second test would need its own state; tests do not share locals.

Test 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 The 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; the test runner generates one.

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 A panic 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 panic; expected failures use Result

The guide ends with the Review Checklist, which turns the contract, type, error, loop, and FFI advice into a compact set of review questions.