Testing: assert and luma test
Tests are small functions that call your code and check what it gives. They go in a file beside the one they test, named after it with _test: the tests of hello.luma are in hello_test.luma.
File hello.luma:
fn hello(name: str): str {
return "Hello, ${name}!"
}
print(hello("world"))File hello_test.luma:
fn test_hello() {
assert hello("Ann") == "Hello, Ann!"
}
fn test_empty_name() {
assert hello("") == "Hello!"
}luma testhello_test.luma
PASS test_hello
FAIL test_empty_name
hello_test.luma:6:5: assert failed: hello("") == "Hello!"
got: "Hello, !"
want: "Hello!"
2 tests, 1 failedluma test exits with code 1 when a test failed, so it can stop a build or a deploy.
assert
assert checks a condition. If it is false, it raises error 17, and the message quotes the condition as you wrote it. For a comparison (==, !=, <, <=, >, >=), it also shows both sides; with ==, the left one is what you got and the right one what you want:
total: int = 42
names: [str] = ["Ann", "Bo"]
assert total == 42
assert len(names) > 0 && names[0] == "Ann"A message after a comma says what went wrong in your words:
n: int = -3
assert n > 0, "the count can't be negative"assert failed: n > 0: the count can't be negative
left: -3
right: 0Each side is worked out once, before it is compared, so a call in an assert runs once whether the check passes or fails.
assert works everywhere, not only in tests. In a program, it’s a check that stops the program with a clear message when something is not as the code expects. Like any error, try can catch it.
Test functions
A test is a function at the top level of a test file whose name starts with test_. It takes nothing and gives nothing. It passes if it ends without an error, and fails at the first assert that fails, or at any other error that it doesn’t catch. The other tests run anyway.
Other functions in the test file are helpers that tests can call.
To check that something fails, catch the error with try and check it:
fn parse(s: str): int {
n: int = convert(s)
return n
}
fn test_bad_number() {
e: error = nil
try e {
parse("seven")
}
assert e != nil && e.code == 11
}What a test file sees
A test file sees everything in the file it tests, as if it were written there: its functions, objs, traits and locked values, the ones without pub too. Nothing needs to be imported.
The tested file’s own top-level code doesn’t run: in tests, hello.luma doesn’t print. Its functions don’t need it, because a function can’t see the variables of its file’s top level anyway (see Functions). The files it imports are set up as usual. A test file can import other files too, with import.
A function in the test file can’t have the name of one in the tested file.
Values several tests use
A function can’t see the variables of its file’s top level, so tests get the values they share from a helper function. Each call makes them anew, so a test can change them without changing them for the others:
File cart.luma:
obj Cart {
items: [float] = []
fn total(): float {
sum: float = 0.0
self.items.walk(x) {
sum = sum + x
}
return sum
}
}File cart_test.luma:
fn cart(): Cart {
return Cart{items: [10.0, 20.0]}
}
fn test_total() {
assert cart().total() == 30.0
}
fn test_more() {
c: Cart = cart()
c.items[] = 5.0
assert c.total() == 35.0
}Each test runs on its own, as a program of its own: the test file’s top-level code first (to prepare what the tests need outside the program, such as a file to read, or env.set), then the test.
luma test
luma test # every *_test.luma in this folder
luma test shop # ... in the folder shop
luma test hello.luma # the tests of hello.luma (in hello_test.luma)
luma test -build # compiled instead of interpreted
luma test -run cart # only the tests with "cart" in their names
luma test -run total,bad # ... with "total" or "bad" in them-run keeps the tests whose names have one of the words in them (the test_ needn’t be written), in every test file luma test looks at. The summary says how many weren’t run: 2 tests passed (4 not run). If no test matches, luma test says so and exits with code 1, as a misspelled name usually is the reason.
Tests run in the folder of their file, so a test can read files beside it with a relative path. Their input is empty. What a test prints is shown when it fails.
With -build, each test is compiled to a native program and run, which checks the compiler too. Both ways give the same results. -memcheck builds them with memory checks, and a test whose values aren’t all freed fails.