Std.Test (Cure Standard Library v0.37.2)

View Source

Test assertion primitives plus a shrinking-aware property runner.

Assertions raise an Erlang error with a descriptive atom on failure (:assertion_failed, :not_equal, etc.). The property-based helpers pair with Std.Gen generators.

Examples

mod MyTest
  use Std.Test

  fn test_sanity() -> Unit =
    assert_eq(1 + 1, 2)
# Property: reversing a list twice is the identity.
use Std.Test
use Std.Gen
use Std.List

let prop = fn(xs: List(Int)) -> Bool =
  reverse(reverse(xs)) == xs

forall_default(fn(_) -> list_int(10, 0, 100), prop)
# => :ok

Summary

Functions

Assert condition is true; raises :assertion_failed on false.

Assert structural equality; raises :not_equal on mismatch. Assertions are intentionally available for every runtime value, including generic containers whose element dictionary is not otherwise in lexical scope.

Assert a > b; raises :not_greater otherwise.

Assert a < b; raises :not_less otherwise.

Assert a != b; raises :unexpectedly_equal when they match.

Run property against runs random samples drawn from gen. Returns :ok when every sample satisfies the property, or raises :property_failed on the first counterexample.

Convenience wrapper that uses 100 runs by default.

Run property against runs random samples. On the first failure, shrink the counterexample until no smaller value also fails.

Private: raise reason as an Erlang error.

Functions

assert(condition: Bool) -> Unit

 fn assert(condition: Bool) -> Unit

Assert condition is true; raises :assertion_failed on false.

assert_eq(a: t, b: t) -> Unit

 fn assert_eq(a: t, b: t) -> Unit

Assert structural equality; raises :not_equal on mismatch. Assertions are intentionally available for every runtime value, including generic containers whose element dictionary is not otherwise in lexical scope.

assert_gt(a: t, b: t) -> Unit

 fn assert_gt(a: t, b: t) -> Unit

Assert a > b; raises :not_greater otherwise.

assert_lt(a: t, b: t) -> Unit

 fn assert_lt(a: t, b: t) -> Unit

Assert a < b; raises :not_less otherwise.

assert_ne(a: t, b: t) -> Unit

 fn assert_ne(a: t, b: t) -> Unit

Assert a != b; raises :unexpectedly_equal when they match.

forall(gen: Function(Unit, t), property: Function(t, Bool), runs: Int) -> Unit

 fn forall(gen: Function(Unit, t), property: Function(t, Bool), runs: Int) -> Unit

Run property against runs random samples drawn from gen. Returns :ok when every sample satisfies the property, or raises :property_failed on the first counterexample.

gen is a zero-argument function (e.g. fn(_) -> Std.Gen.int_in(0, 100)) that returns a fresh value on every call.

forall_default(gen: Function(Unit, t), property: Function(t, Bool)) -> Unit

 fn forall_default(gen: Function(Unit, t), property: Function(t, Bool)) -> Unit

Convenience wrapper that uses 100 runs by default.

forall_shrunk(gen: Function(Unit, t), property: Function(t, Bool), runs: Int) -> Result(Unit, t)

 fn forall_shrunk(gen: Function(Unit, t), property: Function(t, Bool), runs: Int) -> Result(Unit, t)

Run property against runs random samples. On the first failure, shrink the counterexample until no smaller value also fails.

Returns Ok(:ok) when every sample satisfied the property, and Error(minimal) carrying the minimised counterexample otherwise. Unlike forall/3, a failing property is a value, not a raise — including a property that itself throws (treated as a failure). Given a total generator and property this never raises; a generator that throws has no witness value to return and is a caller error.

forall_shrunk_default(gen: Function(Unit, t), property: Function(t, Bool)) -> Result(Unit, t)

 fn forall_shrunk_default(gen: Function(Unit, t), property: Function(t, Bool)) -> Result(Unit, t)

Same as forall_shrunk/3 but with a 100-run default.

raise_error(reason: Atom) -> Unit

 fn raise_error(reason: Atom) -> Unit

Private: raise reason as an Erlang error.