Testing
Grammar: test-decl
A test is a top-level declaration, test "what it shows" { … }, named in prose because the name is documentation rather than something anything calls. It takes no parameters and answers with nothing — a bare return leaves one early — and a file holding only tests needs no main. The title is how the report names it, so two tests may not share one, nor two titles that differ only in a space against an _.
hive test entrypoint.hive runs every test the entrypoint reaches, with no framework and no vocabulary beyond assert, and exits non-zero when any fails, which is what a commit hook or a CI step wants. Inside a test:
- a failed
assertis recorded with its condition as written and, for==or!=, both sides — and the test carries on, so every failure in it is listed; - a
panicfails that test alone, and the rest of the suite still runs; - what a test
echos is shown only when it fails.
Every run reports coverage — the statements your own declarations have, and which of them no test reached, an imported one named as its module spells it. A failure points at the generated main_test.go rather than at the .hive line, which is a known gap. A test runs with the working directory set to the generated project, not the entrypoint's folder, so a test that needs files writes them itself.
type Item {
name: Str
price: Int
}
func total(items: Item[]): Int {
mut sum := 0
for each item in items {
sum = sum + item.price
}
return sum
}
test "an empty cart costs nothing" {
Item[0] empty = []
assert total(empty) == 0
}
test "two items add up" {
assert total([Item("wax", 5), Item("comb", 3)]) == 8
}func total(prices: Int[]): Int {
mut sum := 0
for each p in prices {
sum += p
}
return sum
}
func unused(n: Int): Int {
return n
}
test "an empty basket costs nothing" {
Int[0] empty = []
assert total(empty) == 0
}
test "this one is wrong on purpose" {
echo "shown, because this test fails"
assert total([3, 4]) == 8
assert total([1]) == 2
}$ hive test basket.hive
PASS an empty basket costs nothing
FAIL this one is wrong on purpose
shown, because this test fails
main_test.go:24: assert total([3, 4]) == 8
left: 7
right: 8
main_test.go:27: assert total([1]) == 2
left: 1
right: 2
2 tests: 1 passed, 1 failed
coverage: 77.7% of statements (7/9)
never exercised: unused