Testing Your Larzscript Code
Larz covers Larzscript's test package: assert_eq and assert for checks, and report() for a pass/fail summary that also sets the process exit code.
writing an assertion
assert_eq checks a value against what you expected. report() prints a summary of everything you checked:
import "test" as t
fn add(a, b) {
return a + b
}
t.assert_eq(add(2, 3), 5, "adds")
t.assert(add(2, 3) > 0, "result is positive")
t.report()Output
ok - 2 passed
when a test fails
A failing assertion doesn't stop the run - it's recorded, and report() lists exactly what went wrong, and sets the exit code to 1:
import "test" as t
fn add(a, b) {
return a + b
}
t.assert_eq(add(2, 3), 5, "adds")
t.assert_eq(add(2, 3), 6, "adds wrong on purpose")
t.report()Output
FAIL: adds wrong on purpose: expected 6 but got 5 1 failed, 1 passed
Every snippet on this page is run through the real larzscript interpreter, not hand-typed โ what you see in the Output blocks is the actual output, for everything shown: a passing test run and a genuinely failing one.
Related
Lesson 14: Compiling to C for Speed--emit-c and gcc - what's next after testing your code.→Lesson 12: Building a Real CLI Toolargs and cli - what's next after gas-metered functions, and the same import style used here.→Lesson 8: Error Handling โ try/catchtry/catch - the exact mechanism the test package's raises() checks internally.→Learn Larzscript Cookbook29 short "how to X in Larzscript" recipes, good material to practice writing assertions against.→The test package sourceassert, assert_eq, the fluent expect()/to_equal() API, and raises() - the full implementation.→