pub fn predicate<T, F>(name: &'static str, pred: F) -> impl Matcher<T>Expand description
Matches a value for which pred returns true.
The escape hatch for when no standard matcher fits. name is what the
failure reports as the expectation, so give it a readable one: a closure has
no name of its own, and a failure that says expected: <closure> helps no
one.
The matcher pairs naturally with Subject::satisfies,
reading as “x satisfies the predicate <name>”.
use test_better_core::{OrFail, TestResult};
use test_better_matchers::{Matcher, check, eq, predicate};
fn main() -> TestResult {
check!(4).satisfies(predicate("an even number", |n: &i32| n % 2 == 0))?;
// The `name` is what a failure reports, not `<closure>`.
let failure = predicate("an even number", |n: &i32| n % 2 == 0)
.check(&3)
.failure
.or_fail_with("3 is not even")?;
check!(failure.expected.to_string()).satisfies(eq("an even number".to_string()))?;
Ok(())
}