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.
| Function | Passes when |
|---|---|
expect(condition, message) | The condition holds. |
expect_true / expect_false | The 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_none | The option has / lacks a value. |
expect_ok / expect_err | The 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>;
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>;
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>;
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;
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;
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>;
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>;
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>;
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>;
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>;
function expect_err<T, E>(r: &Result<T, E>, message: Str) -> Result<(), TestError>;Ok(()) if r is Err(_).
function bail(message: Str) -> Result<(), TestError>;
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>;
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".
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
primitive ()The unit type: a value that carries no information, and the empty tuple. Distinct from void, which is the absence of a value - Result<(), E> succeeds with nothing to return. 0 bytes.
type enum TestError {
Failed(String);
Other(String);
}Why a test failed. The runner prints the variant's message verbatim to stderr from the child process before exiting non-zero, so the parent can show the failing message in the test summary.
std::test::errorfunction 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 + DisplayOk(()) 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 + DisplayOk(()) 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.
type trait Eq {
equals(&this, other: &This) -> boolean;
}Types that support value equality. equals(a, b) must be reflexive, symmetric, and transitive.
type trait Display {
fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWrite;
}Display / Debug / Formatter.
std::fmt::displayfunction 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(_).
Result::Ok(T)Result::Err(E)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".
function name<T>(param: Type) -> Ret { ... }Declares a function. Parameters are written name: Type and are never inferred; the return type follows -> and defaults to void. Declaration order does not matter: every signature is collected before any body is checked, so functions may call each other freely and recurse.
function expect(condition: boolean, message: Str) -> Result<(), TestError>Return Ok(()) if condition is true, Err(TestError::Failed(msg)) otherwise. The cheap, no-format assertion.
(parameter) condition: boolean(parameter) message: Strtype struct Str {
bytes: u8*;
length: u64;
}Str: a borrowed UTF-8 string slice.
std::collections::strfunction 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 + DisplayOk(()) 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.
type parameter Twhere T: Eq + Display
(parameter) a: T(parameter) b: Twhere T: Ord + Clone, U: HashConstrains generic parameters to types that implement the listed traits, which is what makes those traits' methods callable on T. Bounds on one parameter join with +; parameters separate with ,. A where on a single method narrows the type's parameter for that method only.
function expect_ne<T>(a: T, b: T) -> Result<(), TestError>
where T: Eq + DisplayOk(()) 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".
(parameter) a: f64(parameter) b: f64(parameter) tolerance: f64function 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.
type parameter T(parameter) opt: &Option<T>Option::Some(T)function expect_none<T>(opt: &Option<T>, message: Str) -> Result<(), TestError>Ok(()) if opt is None.
Option::Nonefunction 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.
type parameter E(parameter) r: &Result<T, E>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".
TestError::Other(String)Any other reason a test could not produce a definitive result: I/O error during setup, environment problem, etc.
std::test::error