Testing
Tests are part of the language: a test declaration sits beside the code it tests, and
maxon test finds, compiles and runs them.
Test Declarations
Section titled “Test Declarations”A test is a top-level declaration named with a quoted phrase instead of an identifier:
test 'adds two numbers' Expect.equal(2 + 2, expected: 4)end 'adds two numbers'- The name may contain any character except
', and theendlabel repeats it exactly (E2008 on a mismatch). An empty name is E2059. - A test takes no parameters and has no
returnsclause. testis a contextual keyword: it opens a declaration only at the top level and only when a quoted name follows, sotestremains usable as a variable or parameter name.
Test Files
Section titled “Test Files”Tests live in files whose names end in .maxtest. A test in any other file is E2058, and its
message names the file to rename it to (pricing.maxon becomes pricing.maxtest). A regular build
compiles .maxon files only, and maxon test compiles the *.maxtest files together with the rest of
the project and a generated entry point — the project needs no main.
temperature/├── temperature.maxon # the code└── temperature.maxtest # its testsAssertions
Section titled “Assertions”Every test implicitly declares throws TestFailure (you cannot write the clause yourself). The Expect
assertions throw TestFailure.assertion when they fail, and a test body calls them without try: the test
handles every error its body raises (Uncaught Errors in Tests), and a
TestFailure fails it. Outside a test — in a helper function a test calls — an assertion needs try like
any throwing call, and a forgotten one is a compile error (E3057), never an assertion whose failure goes
unnoticed.
temperature.maxon:
export typealias Celsius = int(-273 to 10000)export typealias Fahrenheit = int(-460 to 18032)
/// Converts a Celsius reading to Fahrenheit, rounding toward zero.export function toFahrenheit(c Celsius) returns Fahrenheit return c * 9 / 5 + 32end 'toFahrenheit'
export function describe(c Celsius) returns String if c <= 0 'freezing' return "freezing" end 'freezing'
return "above freezing"end 'describe'temperature.maxtest:
test 'boiling point converts' Expect.equal(toFahrenheit(100), expected: 212)end 'boiling point converts'
test 'zero is freezing' Expect.equal(describe(0), expected: "freezing") Expect.startsWith(describe(5), needle: "above")end 'zero is freezing'
test 'body temperature' Expect.equal(toFahrenheit(37), expected: 98, message: "rounds toward zero")end 'body temperature'$ maxon test temperature --no-timingtemperature/temperature.maxtest: ✓ boiling point converts ✓ zero is freezing ✓ body temperature
3 pass 0 fail
3 tests across 1 file.The comparing matchers are generic functions: the type is inferred from the two
arguments, so a value of any alias is passed as it is. close takes AssertedReal floats. The matchers:
| Matcher | Checks |
|---|---|
Expect.equal(actual, expected:), Expect.notEqual(actual, expected:) |
any Equatable type |
Expect.greaterThan(actual, than:), lessThan, atLeast, atMost |
any Comparable type |
Expect.close(actual, expected:, within:) |
floats within a tolerance |
Expect.isTrue(actual), Expect.isFalse(actual) |
booleans |
Expect.contains(haystack, needle:), startsWith, endsWith, isEmpty |
strings |
Expect.fail(message) |
always fails |
Every matcher also takes an optional message: that is printed when it fails, and reports the line of the
assertion itself. A failing assertion prints what it expected and what it received — had the last test
expected 99, the run would report:
FAIL temperature/temperature.maxtest > body temperature FAIL temperature.maxtest:11: Expect.equal expected: 99 received: 98 message: rounds toward zeroThe full assertion reference is on the Testing page of the standard library.
Uncaught Errors in Tests
Section titled “Uncaught Errors in Tests”Inside a test body, a call to a throwing function, a call to a throwing interface method and an await of
a throwing promise need no try: the test handles every error its body raises, of any error type — not
only TestFailure. Any other error fails that test and reports the error and the line of the operation that
threw it:
test 'a missing user throws' let name = lookup(0) // lookup throws LookupError Expect.equal(name, expected: "user")end 'a missing user throws'FAIL users/lookup.maxtest > a missing user throws threw LookupError.notFound at lookup.maxtest:2The error is named as the test’s file would write it, so a library error whose name the project also
declares is reported as stdlib.ParseError.invalidFormat.
- A bare
trymeans the same thing, and anotherwiseclause you write always takes precedence. An explicittrycovers only the operation that produces its value; a throwing argument or operand inside it is handled by the test. Atryblock inside the test handles its own body’s errors first. - The rule applies to the test body only. In an ordinary function a throwing call without
tryis still E3057 and a baretryon another error type is still E3059, and a closure written inside a test is an ordinary function. - A
paniccannot be caught; the test is reported as crashed.
Test Diagnostics
Section titled “Test Diagnostics”| Code | Cause |
|---|---|
| E2008 | the end label does not repeat the test’s name |
| E2058 | a test declaration outside a *.maxtest file |
| E2059 | an empty test name |
| E3057 | an assertion (or other throwing call) without try outside a test body — in a helper function, or in a closure written inside a test |
| E3107 | two tests in one file whose names compile to the same symbol — each character outside A–Z, a–z, 0–9 and _ becomes _, so 'adds two' and 'adds-two' collide |
Running Tests
Section titled “Running Tests”maxon test [directory] discovers every test in the project’s sources — the directory, or the sources
its .maxproj target lists, as maxon build compiles them — runs them and exits 0 when all pass,
1 when any fails (or there are none), and 2 when the tests do not compile. --list prints the tests
without running them. A test binary’s heap is checked for leaks like any program’s, and a leaking test is
reported as such. maxon test <file>.maxtest runs that one file’s tests, compiled with the library tiers
alone. See the CLI reference for its flags.