Skip to content

Commit 9c50b57

Browse files
hovinenbcopybara-github
authored andcommitted
Display which tuple elements did not match when explaining a mismatch in the tuple! macro.
Previously, the matcher explanation would only show what the elements were expected to be. No information was given on which tuple elements did not match and why. This implements Matcher::explain_match to enhance the output to show which tuple elements did not match and give explanations for each one. PiperOrigin-RevId: 503913594
1 parent e3b92d8 commit 9c50b57

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

googletest/src/matchers/tuple_matcher.rs

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ macro_rules! tuple {
259259
pub mod internal {
260260
#[cfg(not(google3))]
261261
use crate as googletest;
262-
use googletest::matcher::{Describe, Matcher, MatcherResult};
263-
use std::fmt::Debug;
262+
use googletest::matcher::{Describe, MatchExplanation, Matcher, MatcherResult};
263+
use std::fmt::{Debug, Write};
264264

265265
/// Replaces the first expression with the second at compile time.
266266
///
@@ -297,6 +297,22 @@ pub mod internal {
297297
})*
298298
MatcherResult::Matches
299299
}
300+
301+
fn explain_match(&self, actual: &($($field_type,)*)) -> MatchExplanation {
302+
let mut explanation = format!("which {}", self.describe(self.matches(actual)));
303+
$(match self.$field_number.matches(&actual.$field_number) {
304+
MatcherResult::Matches => {},
305+
MatcherResult::DoesNotMatch => {
306+
writeln!(
307+
&mut explanation,
308+
concat!("Element #", $field_number, " is {:?}, {}"),
309+
actual.$field_number,
310+
self.$field_number.explain_match(&actual.$field_number)
311+
).unwrap();
312+
}
313+
})*
314+
MatchExplanation::create(explanation)
315+
}
300316
}
301317

302318
impl<$($matcher_type: Describe),*> Describe for $name<$($matcher_type),*> {
@@ -443,10 +459,10 @@ mod tests {
443459
use googletest::matchers;
444460
use googletest::{
445461
google_test,
446-
matcher::{Describe, MatcherResult},
462+
matcher::{Describe, Matcher, MatcherResult},
447463
verify_that, Result,
448464
};
449-
use matchers::{eq, not};
465+
use matchers::{displays_as, eq, not};
450466

451467
#[google_test]
452468
fn empty_matcher_matches_empty_tuple() -> Result<()> {
@@ -687,4 +703,31 @@ is a tuple whose values do not respectively match:
687703
")
688704
)
689705
}
706+
707+
#[google_test]
708+
fn describe_match_shows_which_tuple_element_did_not_match() -> Result<()> {
709+
verify_that!(
710+
tuple!(eq(1), eq(2)).explain_match(&(1, 3)),
711+
displays_as(eq("\
712+
which is a tuple whose values do not respectively match:
713+
is equal to 1,
714+
is equal to 2,
715+
Element #1 is 3, which isn't equal to 2
716+
"))
717+
)
718+
}
719+
720+
#[google_test]
721+
fn describe_match_shows_which_two_tuple_elements_did_not_match() -> Result<()> {
722+
verify_that!(
723+
tuple!(eq(1), eq(2)).explain_match(&(2, 3)),
724+
displays_as(eq("\
725+
which is a tuple whose values do not respectively match:
726+
is equal to 1,
727+
is equal to 2,
728+
Element #0 is 2, which isn't equal to 1
729+
Element #1 is 3, which isn't equal to 2
730+
"))
731+
)
732+
}
690733
}

0 commit comments

Comments
 (0)