use crate::description::Description;
use crate::matcher::{MatchResult, Matcher, Mismatch};
struct AlwaysMatches;
impl<T: ?Sized> Matcher<T> for AlwaysMatches {
fn check(&self, _actual: &T) -> MatchResult {
MatchResult::pass()
}
fn description(&self) -> Description {
Description::text("anything")
}
}
struct NeverMatches;
impl<T: ?Sized> Matcher<T> for NeverMatches {
fn check(&self, _actual: &T) -> MatchResult {
MatchResult::fail(Mismatch::new(Matcher::<T>::description(self), "<value>"))
}
fn description(&self) -> Description {
Description::text("nothing")
}
}
#[must_use]
pub fn always_matches<T: ?Sized>() -> impl Matcher<T> {
AlwaysMatches
}
#[must_use]
pub fn never_matches<T: ?Sized>() -> impl Matcher<T> {
NeverMatches
}
#[cfg(test)]
mod tests {
use test_better_core::{OrFail, TestResult};
use super::*;
use crate::{check, eq, is_false, is_true};
#[test]
fn always_matches_passes_for_any_type() -> TestResult {
check!(always_matches().check(&42).matched).satisfies(is_true())?;
check!(always_matches().check("a str").matched).satisfies(is_true())?;
check!(always_matches().check(&[1, 2, 3][..]).matched).satisfies(is_true())?;
Ok(())
}
#[test]
fn never_matches_fails_with_a_described_mismatch() -> TestResult {
let result = never_matches().check(&42);
check!(result.matched).satisfies(is_false())?;
let failure = result.failure.or_fail_with("never_matches always fails")?;
check!(failure.expected.to_string()).satisfies(eq("nothing".to_string()))?;
check!(failure.actual).satisfies(eq("<value>".to_string()))?;
Ok(())
}
}