Standard Library: Test

Write tests and verify program behavior

  1. assert
  2. equals
  3. returns
  4. runs
  5. throws

Test.assert(predicate)

Panic if the boolean predicate is false.

assert(1 + 1 == 2)
none
assert(1 + 1 == 11)
Error: assertion error

Test.equals(actual, expected)

Check that actual equals expected.

Test.equals(2 + 2, 4)
Test.equals(1 + 1, 11)
{ status: "pass" }
{ status: "fail", expected: 11, got: 2 }

Test.returns(func, expected)

Check that func returns expected.

Test.returns(fn() 2 + 2 end, 4)
Test.returns(fn() 1 + 1 end, 11)
Test.returns(fn() 0 / 0 end, 0)
{ status: "pass" }
{ status: "fail", expected: 11, got: 2 }
{ status: "fail", panic: "Error: invalid arguments" }

Test.runs(func)

Check that func runs without panicking or throwing an error.

fn inverse(n)
  assert(n != 0)
  1 / n
end

Test.runs(fn() inverse(10) end)
Test.runs(fn() inverse(0) end)
{ status: "pass" }
{ status: "fail", panic: "Error: assertion error" }

Test.throws(func, payload)

Check that func throws an error containing payload.

fn inverse(n)
  if n == 0 then
    Err.throw("n must not be zero")
  end

  1 / n
end

Test.throws(fn() inverse(0) end, "n must not be zero")
Test.throws(fn() inverse(0) end, "wrong!")
Test.throws(fn() inverse(10) end, "n must not be zero")
Test.throws(fn() inverse("0") end, "n must not be zero")
{ status: "pass" }
{ status: "fail", expected: "wrong!", actual: "n must not be zero" }
{ status: "fail", returned: 0.1 }
{ status: "fail", panic: "Error: type error" }