Skip to content
CryoCryo home
Stdlibtest

assert

import std::test::assert; · source

Free Functions

The assertion family

Every assertion returns Result<(), TestError>, so ? propagates a failure out of the test function.

FunctionPasses when
expect(condition, message)The condition holds.
expect_true / expect_falseThe obvious.
expect_eq(a, b) / expect_ne(a, b)The values compare equal / unequal. T: Eq + Display, so the failure message renders both.
expect_close_f64(a, b, tolerance)The values are within tolerance.
expect_some / expect_noneThe option has / lacks a value.
expect_ok / expect_errThe result is Ok / Err.

bail(message) fails the test outright, and bail_other(message) reports a non-assertion failure — a setup problem rather than a wrong answer.

function expect(condition: boolean, message: Str) -> Result<(), TestError>;

Return Ok(()) if condition is true, Err(TestError::Failed(msg)) otherwise. The cheap, no-format assertion.

function expect_true(condition: boolean, message: Str) -> Result<(), TestError>;

Same as expect. Exists so the spelling at the call site matches the intent ("is this true?") rather than mentioning the word expect twice in a row.

function expect_false(condition: boolean, message: Str) -> Result<(), TestError>;

Mirror of expect_true. expect_false(condition, msg) is clearer than expect(!condition, msg) at a call site dominated by negative assertions.

function expect_eq<T>(a: T, b: T) -> Result<(), TestError> where T: Eq + Display;

Ok(()) if a.equals(&b), otherwise Err carrying a multi-line "assertion left == right failed" message that Display-formats both values. Allocates the message on the heap; on allocation failure during message building, returns a generic fallback message.

function expect_ne<T>(a: T, b: T) -> Result<(), TestError> where T: Eq + Display;

Ok(()) if !a.equals(&b), otherwise Err carrying a multi-line "assertion left != right failed" message that Display-formats both values. Allocates as expect_eq does.

function expect_close_f64(a: f64, b: f64, tolerance: f64) -> Result<(), TestError>;

Ok(()) if |a - b| <= tolerance. Use this in place of expect_eq whenever floats are involved; expect_eq would route through f64::equals, which is exact-bit comparison and disagrees with every useful definition of "approximately the same".

function expect_some<T>(opt: &Option<T>, message: Str) -> Result<(), TestError>;

Ok(()) if opt is Some(_). Takes a borrow so the test still owns the option afterwards; important for Option<T> where T: Drop, because the test author is responsible for dropping it.

function expect_none<T>(opt: &Option<T>, message: Str) -> Result<(), TestError>;

Ok(()) if opt is None.

function expect_ok<T, E>(r: &Result<T, E>, message: Str) -> Result<(), TestError>;

Ok(()) if r is Ok(_). Takes a borrow so the caller still owns the result.

function expect_err<T, E>(r: &Result<T, E>, message: Str) -> Result<(), TestError>;

Ok(()) if r is Err(_).

function bail(message: Str) -> Result<(), TestError>;

Always return Err(TestError::Failed(message)). Use this when the failure condition is implicit in reaching this code path.

function bail_other(message: Str) -> Result<(), TestError>;

Same as bail but produces a TestError::Other, for non-assertion failures (I/O setup, environment problems, etc.) that the runner should display under "test error" rather than "assertion failed".