test_better_core/result.rs
1//! [`TestResult`]: the return type that makes `?` the single control-flow
2//! operator of a test.
3//!
4//! A test (or any test helper) returns `TestResult`, so every fallible step
5//! short-circuits with `?` and the first failure is the one reported.
6
7use crate::error::TestError;
8
9/// The result type returned by `test-better` tests and helpers.
10///
11/// It defaults its `Ok` type to `()`, the common case for a `#[test]` function,
12/// while helpers that produce a value name it explicitly (`TestResult<User>`).
13///
14/// # Examples
15///
16/// ```
17/// use test_better_core::{TestError, TestResult};
18///
19/// fn checked_div(numerator: i32, denominator: i32) -> TestResult<i32> {
20/// if denominator == 0 {
21/// return Err(TestError::assertion("denominator must be non-zero"));
22/// }
23/// Ok(numerator / denominator)
24/// }
25///
26/// fn test_division() -> TestResult {
27/// let quotient = checked_div(10, 2)?;
28/// if quotient != 5 {
29/// return Err(TestError::from_expected_actual(5, quotient));
30/// }
31/// Ok(())
32/// }
33///
34/// fn main() -> TestResult {
35/// test_division()?;
36/// Ok(())
37/// }
38/// ```
39pub type TestResult<T = ()> = Result<T, TestError>;