Skip to content

Commit

Permalink
Add automatic reference implementation of Matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
bjacotg committed Feb 2, 2024
1 parent 1862fe3 commit 27dd6b8
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions googletest/src/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ use crate::matchers::__internal_unstable_do_not_depend_on_these::DisjunctionMatc
use std::fmt::Debug;

/// An interface for checking an arbitrary condition on a datum.
///
/// This trait is automatically implemented for a reference of any type
/// implementing `Matcher`. This simplifies reusing a matcher in different
/// assertions.
pub trait Matcher {
/// The type against which this matcher matches.
type ActualT: Debug + ?Sized;
Expand Down Expand Up @@ -268,3 +272,39 @@ impl MatcherResult {
matches!(self, MatcherResult::NoMatch)
}
}

impl<M: Matcher> Matcher for &M {
type ActualT = M::ActualT;

fn matches(&self, actual: &Self::ActualT) -> MatcherResult {
(*self).matches(actual)
}

fn describe(&self, matcher_result: MatcherResult) -> Description {
(*self).describe(matcher_result)
}

fn explain_match(&self, actual: &Self::ActualT) -> Description {
(*self).explain_match(actual)
}
}

#[cfg(test)]
mod tests {
use crate::prelude::*;

#[test]
fn ref_matchers_can_be_reused() -> Result<()> {
let matcher = eq(1);

verify_that!(1, &matcher)?;
verify_that!(1, &matcher)
}

#[test]
fn ref_matchers_as_inner_matcher() -> Result<()> {
let matcher = gt(1);

verify_that!([2, 3, 4, 5], [&matcher, &matcher, &matcher, &matcher])
}
}

0 comments on commit 27dd6b8

Please sign in to comment.