Tiny assertion helpers for writing tests in Bern.
import assert
Each assertion returns a string ending in PASS or FAIL, named so you can read a column of results at a glance. They never throw - you collect and print the outcomes yourself.
Compare two values with ==.
assert_equals("Addition test", 2 + 2, 4)
-- Output: "Addition test PASS"
assert_equals("Wrong test", 2 + 2, 5)
-- Output: "Wrong test FAIL"
Whether a and b are within tolerance of each other - the helper behind assert_approx.
approx_eq(3.14159, 3.14, 0.01) -- Output: true
Like assert_equals, but passes when the two numbers are close enough - the right tool for floating-point results.
assert_approx("Pi test", 3.14159, 3.14, 0.01)
-- Output: "Pi test PASS"
A test file usually gathers a list of results and prints them. Here assert teams up with math to check a couple of computations at once:
import assert
import core
import math
results = [
assert_equals("sum", sum([1, 2, 3, 4]), 10),
assert_equals("reverse", reverse([1, 2, 3]), [3, 2, 1]),
assert_approx("sqrt(2)", sqrt(2), 1.41421, 0.001),
assert_approx("sin(0)", sin(0), 0.0, 0.0001)
]
loop r : results do
r
end
-- Output:
-- "sum PASS"
-- "reverse PASS"
-- "sqrt(2) PASS"
-- "sin(0) PASS"