Skip to content

Commit 221ccca

Browse files
hovinenbcopybara-github
authored andcommitted
Use pretty-print to display the actual values of individual elements in match explanations.
Previously, the standard debug output (:?) would be used to format individual elements of a collection when building an explanation why a matcher matched or did not match an actual value. Since the standard debug formatter packs the whole output in one line, and individual elements can be complex, this can lead to assertion failure messages which are hard to read. This changes the output formatter in most cases to use the pretty-print formatter (:#?), which adds newlines and indentation reflecting the data structure. For now, this change does not touch the display of keys in the has_entry matcher. In most cases, keys should be simple enough that the one-line debug output suffices. Splitting such simple structures into multiple lines in that context may have the opposite of the intended effect. PiperOrigin-RevId: 505620778
1 parent 703d41d commit 221ccca

10 files changed

+50
-23
lines changed

googletest/src/matchers/each_matcher.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ where
8181
if non_matching_elements.len() == 1 {
8282
let (idx, element, explanation) = non_matching_elements.remove(0);
8383
return MatchExplanation::create(format!(
84-
"whose element #{idx} is {element:?}, {explanation}"
84+
"whose element #{idx} is {element:#?}, {explanation}"
8585
));
8686
}
8787

@@ -92,7 +92,7 @@ where
9292
.join(", ");
9393
let element_explanations = non_matching_elements
9494
.iter()
95-
.map(|&(_, element, ref explanation)| format!("{element:?}, {explanation}"))
95+
.map(|&(_, element, ref explanation)| format!("{element:#?}, {explanation}"))
9696
.collect::<Vec<_>>()
9797
.join("\n");
9898
MatchExplanation::create(format!(
@@ -247,7 +247,10 @@ Actual: [
247247
[
248248
1,
249249
],
250-
], whose element #0 is [1, 2], whose element #1 is 2, which isn't equal to 1
250+
], whose element #0 is [
251+
1,
252+
2,
253+
], whose element #1 is 2, which isn't equal to 1
251254
"
252255
)))
253256
)

googletest/src/matchers/elements_are_matcher.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub mod internal {
9191
.zip(self.elements)
9292
.enumerate()
9393
.filter(|&(_, (a, e))| matches!(e.matches(a), MatcherResult::DoesNotMatch))
94-
.map(|(idx, (a, e))| format!("element #{} is {:?}, {}", idx, a, e.explain_match(a)))
94+
.map(|(idx, (a, e))| format!("element #{idx} is {a:#?}, {}", e.explain_match(a)))
9595
.collect::<Vec<_>>();
9696
if mismatches.is_empty() {
9797
MatchExplanation::create("whose elements all match".to_string())

googletest/src/matchers/err_matcher.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl<T: Debug, E: Debug, InnerMatcherT: Matcher<E>> Matcher<Result<T, E>>
4242
fn explain_match(&self, actual: &Result<T, E>) -> MatchExplanation {
4343
match actual {
4444
Err(e) => MatchExplanation::create(format!(
45-
"which is an error containing {e:?}, {}",
45+
"which is an error containing {e:#?}, {}",
4646
self.inner.explain_match(e)
4747
)),
4848
Ok(_) => MatchExplanation::create("which is a success".to_string()),

googletest/src/matchers/has_entry_matcher.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl<KeyT: Debug + Eq + Hash, ValueT: Debug, MatcherT: Matcher<ValueT>>
6666
fn explain_match(&self, actual: &HashMap<KeyT, ValueT>) -> MatchExplanation {
6767
if let Some(value) = actual.get(&self.key) {
6868
MatchExplanation::create(format!(
69-
"which contains key {:?}, but is mapped to value {:?}, {}",
69+
"which contains key {:?}, but is mapped to value {:#?}, {}",
7070
self.key,
7171
value,
7272
self.inner.explain_match(value)

googletest/src/matchers/ok_matcher.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl<T: Debug, E: Debug, InnerMatcherT: Matcher<T>> Matcher<Result<T, E>>
4242
fn explain_match(&self, actual: &Result<T, E>) -> MatchExplanation {
4343
match actual {
4444
Ok(o) => MatchExplanation::create(format!(
45-
"which is a success containing {o:?}, {}",
45+
"which is a success containing {o:#?}, {}",
4646
self.inner.explain_match(o)
4747
)),
4848
Err(_) => MatchExplanation::create("which is an error".to_string()),

googletest/src/matchers/pointwise_matcher.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ pub mod internal {
117117
.zip(self.matchers.iter())
118118
.enumerate()
119119
.filter(|&(_, (a, e))| matches!(e.matches(a), MatcherResult::DoesNotMatch))
120-
.map(|(idx, (a, e))| format!("element #{} is {:?}, {}", idx, a, e.explain_match(a)))
120+
.map(|(idx, (a, e))| format!("element #{idx} is {a:#?}, {}", e.explain_match(a)))
121121
.collect::<Vec<_>>();
122122
if mismatches.is_empty() {
123123
MatchExplanation::create("which matches all elements".to_string())

googletest/src/matchers/some_matcher.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl<T: Debug, InnerMatcherT: Matcher<T>> Matcher<Option<T>> for SomeMatcher<Inn
4040
fn explain_match(&self, actual: &Option<T>) -> MatchExplanation {
4141
match (self.matches(actual), actual) {
4242
(_, Some(t)) => MatchExplanation::create(format!(
43-
"which contains {t:?}, {}",
43+
"which contains {t:#?}, {}",
4444
self.inner.explain_match(t)
4545
)),
4646
(_, None) => MatchExplanation::create("which is none".to_string()),

googletest/src/matchers/subset_of_matcher.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ where
8888
.into_iter()
8989
.enumerate()
9090
.filter(|&(_, actual_item)| self.expected_is_missing(actual_item))
91-
.map(|(idx, actual_item)| format!("{actual_item:?} at #{idx}"))
91+
.map(|(idx, actual_item)| format!("{actual_item:#?} at #{idx}"))
9292
.collect::<Vec<_>>();
9393

9494
match unexpected_elements.len() {
@@ -106,8 +106,8 @@ where
106106

107107
fn describe(&self, matcher_result: MatcherResult) -> String {
108108
match matcher_result {
109-
MatcherResult::Matches => format!("is a subset of {:?}", self.superset),
110-
MatcherResult::DoesNotMatch => format!("isn't a subset of {:?}", self.superset),
109+
MatcherResult::Matches => format!("is a subset of {:#?}", self.superset),
110+
MatcherResult::DoesNotMatch => format!("isn't a subset of {:#?}", self.superset),
111111
}
112112
}
113113
}
@@ -195,7 +195,11 @@ mod tests {
195195
err(displays_as(contains_substring(
196196
"\
197197
Value of: vec![0, 2, 3]
198-
Expected: is a subset of [1, 2, 3]
198+
Expected: is a subset of [
199+
1,
200+
2,
201+
3,
202+
]
199203
Actual: [
200204
0,
201205
2,
@@ -215,7 +219,11 @@ Actual: [
215219
err(displays_as(contains_substring(
216220
"\
217221
Value of: vec![1, 0, 3]
218-
Expected: is a subset of [1, 2, 3]
222+
Expected: is a subset of [
223+
1,
224+
2,
225+
3,
226+
]
219227
Actual: [
220228
1,
221229
0,
@@ -235,7 +243,11 @@ Actual: [
235243
err(displays_as(contains_substring(
236244
"\
237245
Value of: vec![0, 0, 3]
238-
Expected: is a subset of [1, 2, 3]
246+
Expected: is a subset of [
247+
1,
248+
2,
249+
3,
250+
]
239251
Actual: [
240252
0,
241253
0,

googletest/src/matchers/superset_of_matcher.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ where
8888
.subset
8989
.into_iter()
9090
.filter(|expected_item| actual_is_missing(actual, expected_item))
91-
.map(|expected_item| format!("{expected_item:?}"))
91+
.map(|expected_item| format!("{expected_item:#?}"))
9292
.collect();
9393
match missing_items.len() {
9494
0 => MatchExplanation::create("whose no element is missing".to_string()),
@@ -104,8 +104,8 @@ where
104104

105105
fn describe(&self, matcher_result: MatcherResult) -> String {
106106
match matcher_result {
107-
MatcherResult::Matches => format!("is a superset of {:?}", self.subset),
108-
MatcherResult::DoesNotMatch => format!("isn't a superset of {:?}", self.subset),
107+
MatcherResult::Matches => format!("is a superset of {:#?}", self.subset),
108+
MatcherResult::DoesNotMatch => format!("isn't a superset of {:#?}", self.subset),
109109
}
110110
}
111111
}
@@ -194,7 +194,11 @@ mod tests {
194194
err(displays_as(contains_substring(
195195
"\
196196
Value of: vec![0, 2, 3]
197-
Expected: is a superset of [1, 2, 3]
197+
Expected: is a superset of [
198+
1,
199+
2,
200+
3,
201+
]
198202
Actual: [
199203
0,
200204
2,
@@ -214,7 +218,11 @@ Actual: [
214218
err(displays_as(contains_substring(
215219
"\
216220
Value of: vec![1, 0, 3]
217-
Expected: is a superset of [1, 2, 3]
221+
Expected: is a superset of [
222+
1,
223+
2,
224+
3,
225+
]
218226
Actual: [
219227
1,
220228
0,
@@ -234,7 +242,11 @@ Actual: [
234242
err(displays_as(contains_substring(
235243
"\
236244
Value of: vec![0, 0, 3]
237-
Expected: is a superset of [1, 2, 3]
245+
Expected: is a superset of [
246+
1,
247+
2,
248+
3,
249+
]
238250
Actual: [
239251
0,
240252
0,

googletest/src/matchers/unordered_elements_are_matcher.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ pub mod internal {
692692
for (actual_idx, expected_idx) in self.get_matches() {
693693
error_message.push_str(
694694
format!(
695-
"\n Actual element {:?} at index {actual_idx} matched expected element `{}` at index {expected_idx}.",
695+
"\n Actual element {:#?} at index {actual_idx} matched expected element `{}` at index {expected_idx}.",
696696
actual[actual_idx],
697697
expected[expected_idx].describe(MatcherResult::Matches),
698698
)
@@ -702,7 +702,7 @@ pub mod internal {
702702

703703
for actual_idx in self.get_unmatched_actual() {
704704
error_message.push_str(format!(
705-
"\n Actual element {:?} at index {actual_idx} did not match any remaining expected element.",
705+
"\n Actual element {:#?} at index {actual_idx} did not match any remaining expected element.",
706706
actual[actual_idx]
707707
).as_str());
708708
}

0 commit comments

Comments
 (0)