Skip to content

Commit edb1d11

Browse files
hovinenbcopybara-github
authored andcommitted
Modify DisplayMatcher::explain_match to invoke explain_match on its inner matcher.
Generally matchers which contain inner matchers should always invoke `explain_match` on those inner matchers in their own `explain_match` implementations. Otherwise, any information gleaned from that inner matcher's explanation is lost. In this case, the newly introduced string diff was lost whenever a string matcher was invoked inside `displays_as`. Now, it is displayed. PiperOrigin-RevId: 533481815
1 parent d7ddb47 commit edb1d11

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

googletest/src/matchers/display_matcher.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl<T: Debug + Display, InnerMatcher: Matcher<ActualT = String>> Matcher
4343
}
4444

4545
fn explain_match(&self, actual: &T) -> String {
46-
format!("which displays as \"{}\"", actual)
46+
format!("which displays as a string {}", self.inner.explain_match(&format!("{actual}")))
4747
}
4848

4949
fn describe(&self, matcher_result: MatcherResult) -> String {
@@ -67,8 +67,8 @@ impl<T: Debug + Display, InnerMatcher: Matcher<ActualT = String>> Matcher
6767
#[cfg(test)]
6868
mod tests {
6969
use super::displays_as;
70-
use crate::matcher::Matcher;
7170
use crate::prelude::*;
71+
use indoc::indoc;
7272
use std::fmt::{Debug, Display, Error, Formatter};
7373

7474
#[test]
@@ -101,11 +101,22 @@ mod tests {
101101
verify_that!(Struct { a: 123, b: 321 }, displays_as(eq("Struct { a: 123, b: 321 }")))?;
102102
Ok(())
103103
}
104+
104105
#[test]
105-
fn display_displays_error_message() -> Result<()> {
106+
fn display_displays_error_message_with_explanation_from_inner_matcher() -> Result<()> {
107+
let result = verify_that!("123\n234", displays_as(eq("123\n345")));
108+
106109
verify_that!(
107-
displays_as(eq("31")).explain_match(&43),
108-
displays_as(eq("which displays as \"43\""))
110+
result,
111+
err(displays_as(contains_substring(indoc!(
112+
"
113+
which displays as a string which isn't equal to \"123\\n345\"
114+
Difference:
115+
123
116+
+234
117+
-345
118+
"
119+
))))
109120
)
110121
}
111122
}

0 commit comments

Comments
 (0)