Skip to content

Testing

StarLang has a built-in assert keyword for writing tests.

assert

Checks a condition at runtime. If the condition is false, the program stops and reports the file, line number, and optional message.

assert(1 + 1 == 2)
assert(x > 0, "x must be positive")

Syntax

assert(condition)
assert(condition, message)
  • condition — a boolean expression
  • message — an optional string shown on failure

Failure Output

tests/test_math.star:5: assertion failed: x must be positive

Writing Tests

Test files follow the test_*.star naming convention. Each test file is a regular StarLang program that uses assert to verify behavior.

package main

fn add(a:number, b:number):number {
    return a + b
}

fn main():void {
    assert(add(2, 3) == 5, "addition")
    assert(add(0, 0) == 0, "zero case")
    assert(add(-1, 1) == 0, "negative")
}

Running Tests

star-lang test discovers and runs test_*.star files. See the test command in the CLI reference for usage and output.

Notes

  • Tests run in debug mode (console.log enabled)
  • Test files must have a main() function
  • A test passes if it completes without assertion failures
  • A test fails on the first failed assertion