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.
Syntax
condition— a boolean expressionmessage— an optional string shown on failure
Failure Output
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.logenabled) - Test files must have a
main()function - A test passes if it completes without assertion failures
- A test fails on the first failed assertion