Skip to content

Commit a7d7cc9

Browse files
hovinenbcopybara-github
authored andcommitted
Implement explain_match on remaining matchers which reference inner matchers.
This ensures that a test assertion failure message will contain all additional information pertaining to the actual value to explain the mismatch. Previously, that information would be lost when explain_match was left to its default implementation based on Matcher::describe. This excludes the has_size matcher. While that references an inner matcher, the only matchers which could be used match directly against numbers, and the only numeric matchers the library has do not implement explain_match themselves. So implementing explain_match on HasSizeMatcher would not bring any advantage and would be quite awkard to test. PiperOrigin-RevId: 505624541
1 parent d2be133 commit a7d7cc9

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

googletest/src/matchers/not_matcher.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
#[cfg(not(google3))]
1616
use crate as googletest;
17-
use googletest::matcher::{Matcher, MatcherResult};
17+
use googletest::matcher::{MatchExplanation, Matcher, MatcherResult};
1818
use std::fmt::Debug;
1919

2020
/// Matches the actual value exactly when the inner matcher does _not_ match.
@@ -39,6 +39,10 @@ impl<T: Debug, InnerMatcherT: Matcher<T>> Matcher<T> for NotMatcher<InnerMatcher
3939
}
4040
}
4141

42+
fn explain_match(&self, actual: &T) -> MatchExplanation {
43+
self.inner.explain_match(actual)
44+
}
45+
4246
fn describe(&self, matcher_result: MatcherResult) -> String {
4347
self.inner
4448
.describe(matcher_result.pick(MatcherResult::DoesNotMatch, MatcherResult::Matches))
@@ -53,7 +57,7 @@ mod tests {
5357
#[cfg(not(google3))]
5458
use googletest::matchers;
5559
use googletest::{google_test, verify_that, Result};
56-
use matchers::eq;
60+
use matchers::{container_eq, contains_substring, displays_as, eq, err};
5761

5862
#[google_test]
5963
fn matches_when_inner_matcher_does_not_match() -> Result<()> {
@@ -72,4 +76,20 @@ mod tests {
7276

7377
verify_that!(result, eq(MatcherResult::DoesNotMatch))
7478
}
79+
80+
#[google_test]
81+
fn match_explanation_references_actual_value() -> Result<()> {
82+
let result = verify_that!(*&[1], not(container_eq([1])));
83+
84+
verify_that!(
85+
result,
86+
err(displays_as(contains_substring(
87+
"\
88+
Actual: [
89+
1,
90+
], which contains all the elements
91+
"
92+
)))
93+
)
94+
}
7595
}

googletest/src/matchers/points_to_matcher.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
#[cfg(not(google3))]
1616
use crate as googletest;
17-
use googletest::matcher::{Matcher, MatcherResult};
17+
use googletest::matcher::{MatchExplanation, Matcher, MatcherResult};
1818
use std::fmt::Debug;
1919
use std::ops::Deref;
2020

@@ -50,6 +50,10 @@ where
5050
self.expected.matches(actual.deref())
5151
}
5252

53+
fn explain_match(&self, actual: &ActualT) -> MatchExplanation {
54+
self.expected.explain_match(actual.deref())
55+
}
56+
5357
fn describe(&self, matcher_result: MatcherResult) -> String {
5458
self.expected.describe(matcher_result)
5559
}
@@ -63,7 +67,7 @@ mod tests {
6367
#[cfg(not(google3))]
6468
use googletest::matchers;
6569
use googletest::{google_test, verify_that, Result};
66-
use matchers::eq;
70+
use matchers::{container_eq, contains_substring, displays_as, eq, err};
6771
use std::rc::Rc;
6872

6973
#[google_test]
@@ -80,4 +84,20 @@ mod tests {
8084
fn points_to_matches_box_of_owned_string_with_string_reference() -> Result<()> {
8185
verify_that!(Rc::new("A string".to_string()), points_to(eq("A string")))
8286
}
87+
88+
#[google_test]
89+
fn match_explanation_references_actual_value() -> Result<()> {
90+
let result = verify_that!(&vec![1], points_to(container_eq([])));
91+
92+
verify_that!(
93+
result,
94+
err(displays_as(contains_substring(
95+
"\
96+
Actual: [
97+
1,
98+
], which contains the unexpected element 1
99+
"
100+
)))
101+
)
102+
}
83103
}

0 commit comments

Comments
 (0)