Skip to content

Commit 18c0b7c

Browse files
Merge pull request #356 from google:auto_ref_impl
PiperOrigin-RevId: 604373184
2 parents c4d9f9f + 27dd6b8 commit 18c0b7c

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

googletest/src/matcher.rs

+40
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ use crate::matchers::__internal_unstable_do_not_depend_on_these::DisjunctionMatc
2222
use std::fmt::Debug;
2323

2424
/// An interface for checking an arbitrary condition on a datum.
25+
///
26+
/// This trait is automatically implemented for a reference of any type
27+
/// implementing `Matcher`. This simplifies reusing a matcher in different
28+
/// assertions.
2529
pub trait Matcher {
2630
/// The type against which this matcher matches.
2731
type ActualT: Debug + ?Sized;
@@ -268,3 +272,39 @@ impl MatcherResult {
268272
matches!(self, MatcherResult::NoMatch)
269273
}
270274
}
275+
276+
impl<M: Matcher> Matcher for &M {
277+
type ActualT = M::ActualT;
278+
279+
fn matches(&self, actual: &Self::ActualT) -> MatcherResult {
280+
(*self).matches(actual)
281+
}
282+
283+
fn describe(&self, matcher_result: MatcherResult) -> Description {
284+
(*self).describe(matcher_result)
285+
}
286+
287+
fn explain_match(&self, actual: &Self::ActualT) -> Description {
288+
(*self).explain_match(actual)
289+
}
290+
}
291+
292+
#[cfg(test)]
293+
mod tests {
294+
use crate::prelude::*;
295+
296+
#[test]
297+
fn ref_matchers_can_be_reused() -> Result<()> {
298+
let matcher = eq(1);
299+
300+
verify_that!(1, &matcher)?;
301+
verify_that!(1, &matcher)
302+
}
303+
304+
#[test]
305+
fn ref_matchers_as_inner_matcher() -> Result<()> {
306+
let matcher = gt(1);
307+
308+
verify_that!([2, 3, 4, 5], [&matcher, &matcher, &matcher, &matcher])
309+
}
310+
}

0 commit comments

Comments
 (0)