Skip to content

Modules and tests

sub init makes a small project:

main.sub
lib/text.sub
main.sub
needs [ clock ]
use "./lib/text.sub"
let started = now()
print(greet("world"))
print(shout("subtype"))
print("took", now() - started >= 0)
test "greet says hello" {
assert_eq(greet("sub"), "hello, sub")
}
lib/text.sub
fn greet(name: String) -> String {
"hello, {name}"
}
fn shout(text: String) -> String {
upper(text) + "!"
}
test "shout makes it loud" {
assert_eq(shout("hi"), "HI!")
}

A library function that touches the outside says so with its own needs. The script that calls it must allow that. A call that needs more than the script holds is an error, with the file and the line:

fn load(p: Path) needs [read [p]] -> String { read(p) or "" }
Terminal window
sub test main.sub --yes
ok greet says hello
ok shout makes it loud
2 passed, 0 failed

Test blocks live next to the code they test, in the script or a library. sub test runs on the interpreter, not on Wasmtime.