Skip to content

Standard Library: Testing

The std::core::utils::testing module provides assertion functions for unit testing Shape programs. All assertions return Result<bool, string>Ok(true) on success, Err(message) on failure.

from std::core::utils::testing use { assert, assert_eq, assert_ne, assert_approx, assert_ok, assert_err }

Assert that a condition is true. An optional message is included in the error on failure.

assert(x > 0, "x must be positive")
assert(list.length > 0)

Assert that two values are equal. The error message includes both the expected and actual values.

assert_eq(add(2, 3), 5, "addition should work")
assert_eq(name, "Alice")

Assert that two values are not equal.

assert_ne(result, 0, "result must not be zero")
assert_ne(a, b)

Assert that two numbers are approximately equal within a tolerance. The default tolerance is 1e-10.

assert_approx(sqrt(2.0) * sqrt(2.0), 2.0)
assert_approx(pi(), 3.14, 0.01) // custom tolerance

Assert that a Result value is Ok.

assert_ok(parse_number("42"))

Assert that a Result value is Err.

assert_err(parse_number("not a number"))
FunctionSignatureDescription
assert(cond, msg?)(bool, string?) -> Result<bool, string>Assert condition is true
assert_eq(actual, expected, msg?)(T, T, string?) -> Result<bool, string>Assert values are equal
assert_ne(actual, expected, msg?)(T, T, string?) -> Result<bool, string>Assert values are not equal
assert_approx(actual, expected, tol?)(number, number, number?) -> Result<bool, string>Assert approximate equality
assert_ok(result)(Result<T, string>) -> Result<bool, string>Assert result is Ok
assert_err(result)(Result<T, string>) -> Result<bool, string>Assert result is Err
from std::core::utils::testing use { assert_eq, assert_approx, assert_ok }
fn test_arithmetic() {
assert_eq(2 + 2, 4)?
assert_eq(10 / 3, 3)?
assert_approx(10.0 / 3.0, 3.333, 0.001)?
}
fn test_string_ops() {
let greeting = "hello" + " " + "world"
assert_eq(greeting, "hello world")?
assert_eq(greeting.length, 11)?
}
test_arithmetic()?
test_string_ops()?
print("All tests passed")