std.assert

std.assert provides test-style assertions that throw on failure. Useful for
scripts, examples, and inline checks.

Reference

assert(cond[, message])        # throws if cond is falsy
assert_eq(a, b[, message])     # throws unless a == b
assert_ne(a, b[, message])     # throws if a == b
assert_throws(fn)              # true if calling fn raises; throws otherwise

All assert* functions throw a Scrii error on failure; assert_throws expects
the given zero-arg function to throw — it returns true if it does, and throws
itself if the function does not throw.

Examples

std.assert.assert(1 < 2)
std.assert.assert_eq(std.math.abs(-3), 3)
std.assert.assert_ne("a", "b")

# with a message
std.assert.assert(has({a: 1}, "a"), "missing key a")

# expect an error
std.assert.assert_throws(fn() {
  var x = 1 / 0
})

# failing assertions include the message in the error
# std.assert.assert(false, "oops") # throws: oops

Assertions integrate with the engine’s error propagation — a failed assertion
surfaces as a normal Scrii error with a message, and in async tasks it is
stored on the future and re-thrown by await (see Concurrency — Errors).

See also

  • Error Handling — status codes for assertion failures
  • std.json — handy for comparing structured values in asserts