Skip to content

Commit 703d41d

Browse files
hovinenbcopybara-github
authored andcommitted
Use pretty-print to display the actual value in test assertion failure messages.
This makes the output much easier to understand when the actual value consists of a complex data structure with a lot of nesting. Previously, everything was packed into a single line. Pretty-print enforces newlines after every field as well as indentation reflecting the data structure. This change also fixes several clippy warnings which surfaced in the same files affected by the above modifications. PiperOrigin-RevId: 505619168
1 parent 19c8a85 commit 703d41d

15 files changed

+280
-129
lines changed

googletest/integration_tests/integration_tests.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ mod tests {
2424
};
2525
#[cfg(google3)]
2626
use matchers::all;
27-
use matchers::{contains_regex, contains_substring, eq, not};
27+
use matchers::{anything, contains_regex, contains_substring, displays_as, eq, err, not};
2828
use std::process::Command;
2929

3030
#[google_test]
@@ -466,6 +466,29 @@ Actual: 1, which isn't equal to 2
466466
verify_that!(1, eq(2))
467467
}
468468

469+
#[google_test]
470+
fn failure_message_uses_pretty_print_for_actual_value() -> Result<()> {
471+
#[derive(Debug)]
472+
#[allow(unused)]
473+
struct NontrivialStruct {
474+
a: i32,
475+
b: i32,
476+
}
477+
let value = NontrivialStruct { a: 1, b: 2 };
478+
let failed_assertion_result = verify_that!(value, not(anything()));
479+
480+
verify_that!(
481+
failed_assertion_result,
482+
err(displays_as(contains_substring(
483+
"\
484+
Actual: NontrivialStruct {
485+
a: 1,
486+
b: 2,
487+
}"
488+
)))
489+
)
490+
}
491+
469492
fn run_external_process_in_tests_directory(name: &'static str) -> Result<String> {
470493
let mut command = run_external_process(name);
471494
let std::process::Output { stdout, .. } = command.output()?;

googletest/src/matcher.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub(crate) fn create_assertion_failure<T: Debug + ?Sized>(
6161
TestAssertionFailure::create(format!(
6262
"Value of: {}\n\
6363
Expected: {}\n\
64-
Actual: {:?}, {}\n\
64+
Actual: {:#?}, {}\n\
6565
{}",
6666
actual_expr,
6767
matcher.describe(MatcherResult::Matches),

googletest/src/matchers/container_eq_matcher.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl<const N: usize> Matcher<Vec<String>> for ContainerEqMatcher<[&str; N]> {
140140
return MatcherResult::DoesNotMatch;
141141
}
142142
}
143-
return MatcherResult::Matches;
143+
MatcherResult::Matches
144144
}
145145

146146
fn explain_match(&self, actual: &Vec<String>) -> MatchExplanation {
@@ -170,20 +170,14 @@ where
170170
where
171171
for<'a> &'a ActualT: IntoIterator<Item = &'a T>,
172172
{
173-
self.expected
174-
.into_iter()
175-
.filter(|i| actual.into_iter().find(|j| j == i).is_none())
176-
.collect()
173+
self.expected.into_iter().filter(|&i| !actual.into_iter().any(|j| j == i)).collect()
177174
}
178175

179176
fn get_unexpected_items<'a, ActualT: ?Sized>(&self, actual: &'a ActualT) -> Vec<&'a T>
180177
where
181178
for<'b> &'b ActualT: IntoIterator<Item = &'b T>,
182179
{
183-
actual
184-
.into_iter()
185-
.filter(|i| self.expected.into_iter().find(|j| j == i).is_none())
186-
.collect()
180+
actual.into_iter().filter(|&i| !self.expected.into_iter().any(|j| j == i)).collect()
187181
}
188182
}
189183

@@ -205,8 +199,7 @@ fn build_explanation<T: Debug, U: Debug>(missing: Vec<T>, unexpected: Vec<U>) ->
205199
unexpected[0]
206200
)),
207201
(0, _) => MatchExplanation::create(format!(
208-
"which contains the unexpected elements {:?}",
209-
unexpected
202+
"which contains the unexpected elements {unexpected:?}",
210203
)),
211204
(1, 0) => {
212205
MatchExplanation::create(format!("which is missing the element {:?}", missing[0]))
@@ -216,17 +209,16 @@ fn build_explanation<T: Debug, U: Debug>(missing: Vec<T>, unexpected: Vec<U>) ->
216209
missing[0], unexpected[0]
217210
)),
218211
(1, _) => MatchExplanation::create(format!(
219-
"which is missing the element {:?} and contains the unexpected elements {:?}",
220-
missing[0], unexpected
212+
"which is missing the element {:?} and contains the unexpected elements {unexpected:?}",
213+
missing[0]
221214
)),
222-
(_, 0) => MatchExplanation::create(format!("which is missing the elements {:?}", missing)),
215+
(_, 0) => MatchExplanation::create(format!("which is missing the elements {missing:?}")),
223216
(_, 1) => MatchExplanation::create(format!(
224-
"which is missing the elements {:?} and contains the unexpected element {:?}",
225-
missing, unexpected[0]
217+
"which is missing the elements {missing:?} and contains the unexpected element {:?}",
218+
unexpected[0]
226219
)),
227220
(_, _) => MatchExplanation::create(format!(
228-
"which is missing the elements {:?} and contains the unexpected elements {:?}",
229-
missing, unexpected
221+
"which is missing the elements {missing:?} and contains the unexpected elements {unexpected:?}",
230222
)),
231223
}
232224
}
@@ -281,9 +273,15 @@ mod tests {
281273
verify_that!(
282274
result,
283275
err(displays_as(contains_substring(
284-
"Value of: vec![1, 3, 2]\n\
285-
Expected: is equal to [1, 2, 3]\n\
286-
Actual: [1, 3, 2], which contains all the elements"
276+
"\
277+
Value of: vec![1, 3, 2]
278+
Expected: is equal to [1, 2, 3]
279+
Actual: [
280+
1,
281+
3,
282+
2,
283+
], which contains all the elements
284+
"
287285
)))
288286
)
289287
}

googletest/src/matchers/each_matcher.rs

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,24 +81,22 @@ 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 #{} is {:?}, {}",
85-
idx, element, explanation
84+
"whose element #{idx} is {element:?}, {explanation}"
8685
));
8786
}
8887

8988
let failed_indexes = non_matching_elements
9089
.iter()
91-
.map(|&(idx, _, _)| format!("#{}", idx))
90+
.map(|&(idx, _, _)| format!("#{idx}"))
9291
.collect::<Vec<_>>()
9392
.join(", ");
9493
let element_explanations = non_matching_elements
9594
.iter()
96-
.map(|&(_, element, ref explanation)| format!("{:?}, {}", element, explanation))
95+
.map(|&(_, element, ref explanation)| format!("{element:?}, {explanation}"))
9796
.collect::<Vec<_>>()
9897
.join("\n");
9998
MatchExplanation::create(format!(
100-
"whose elements {} don't match\n{}",
101-
failed_indexes, element_explanations
99+
"whose elements {failed_indexes} don't match\n{element_explanations}"
102100
))
103101
}
104102

@@ -177,9 +175,15 @@ mod tests {
177175
verify_that!(
178176
result,
179177
err(displays_as(contains_substring(
180-
"Value of: vec![0, 2, 3]\n\
181-
Expected: only contains elements that is greater than 0\n\
182-
Actual: [0, 2, 3], whose element #0 is 0, which is less than or equal to 0"
178+
"\
179+
Value of: vec![0, 2, 3]
180+
Expected: only contains elements that is greater than 0\n\
181+
Actual: [
182+
0,
183+
2,
184+
3,
185+
], whose element #0 is 0, which is less than or equal to 0
186+
"
183187
)))
184188
)
185189
}
@@ -191,9 +195,15 @@ mod tests {
191195
verify_that!(
192196
result,
193197
err(displays_as(contains_substring(
194-
"Value of: vec![1, 0, 3]\n\
195-
Expected: only contains elements that is greater than 0\n\
196-
Actual: [1, 0, 3], whose element #1 is 0, which is less than or equal to 0"
198+
"\
199+
Value of: vec![1, 0, 3]
200+
Expected: only contains elements that is greater than 0
201+
Actual: [
202+
1,
203+
0,
204+
3,
205+
], whose element #1 is 0, which is less than or equal to 0
206+
"
197207
)))
198208
)
199209
}
@@ -205,11 +215,17 @@ mod tests {
205215
verify_that!(
206216
result,
207217
err(displays_as(contains_substring(
208-
"Value of: vec![0, 1, 3]\n\
209-
Expected: only contains elements that is greater than 1\n\
210-
Actual: [0, 1, 3], whose elements #0, #1 don't match\n\
211-
0, which is less than or equal to 1\n\
212-
1, which is less than or equal to 1"
218+
"\
219+
Value of: vec![0, 1, 3]
220+
Expected: only contains elements that is greater than 1
221+
Actual: [
222+
0,
223+
1,
224+
3,
225+
], whose elements #0, #1 don't match
226+
0, which is less than or equal to 1
227+
1, which is less than or equal to 1
228+
"
213229
)))
214230
)
215231
}
@@ -220,9 +236,19 @@ mod tests {
220236
verify_that!(
221237
result,
222238
err(displays_as(contains_substring(
223-
"Value of: vec![vec! [1, 2], vec! [1]]\n\
224-
Expected: only contains elements that only contains elements that is equal to 1\n\
225-
Actual: [[1, 2], [1]], whose element #0 is [1, 2], whose element #1 is 2, which isn't equal to 1"
239+
"\
240+
Value of: vec![vec! [1, 2], vec! [1]]
241+
Expected: only contains elements that only contains elements that is equal to 1
242+
Actual: [
243+
[
244+
1,
245+
2,
246+
],
247+
[
248+
1,
249+
],
250+
], whose element #0 is [1, 2], whose element #1 is 2, which isn't equal to 1
251+
"
226252
)))
227253
)
228254
}

googletest/src/matchers/elements_are_matcher.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,18 @@ mod tests {
148148
verify_that!(
149149
result,
150150
err(displays_as(contains_substring(
151-
"Value of: vec![1, 4, 3]\n\
152-
Expected: has elements:\n\
153-
is equal to 1\n\
154-
is equal to 2\n\
155-
is equal to 3\n\
156-
Actual: [1, 4, 3], whose element #1 is 4, which isn't equal to 2"
151+
"\
152+
Value of: vec![1, 4, 3]
153+
Expected: has elements:
154+
is equal to 1
155+
is equal to 2
156+
is equal to 3
157+
Actual: [
158+
1,
159+
4,
160+
3,
161+
], whose element #1 is 4, which isn't equal to 2
162+
"
157163
)))
158164
)
159165
}

googletest/src/matchers/err_matcher.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,13 @@ mod tests {
114114
verify_that!(
115115
result,
116116
err(displays_as(contains_substring(
117-
"Value of: Err::<i32, i32>(1)\n\
118-
Expected: is an error containing a value, which is equal to 2\n\
119-
Actual: Err(1), which is an error containing 1, which isn't equal to 2"
117+
"\
118+
Value of: Err::<i32, i32>(1)
119+
Expected: is an error containing a value, which is equal to 2
120+
Actual: Err(
121+
1,
122+
), which is an error containing 1, which isn't equal to 2
123+
"
120124
)))
121125
)
122126
}

googletest/src/matchers/gt_matcher.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,10 @@ mod tests {
122122
verify_that!(
123123
result,
124124
err(displays_as(contains_substring(
125-
"Value of: 481\n\
126-
Expected: is greater than 632\n\
127-
Actual: 481"
125+
"\
126+
Value of: 481
127+
Expected: is greater than 632
128+
Actual: 481"
128129
)))
129130
)
130131
}
@@ -136,9 +137,15 @@ mod tests {
136137
verify_that!(
137138
result,
138139
err(displays_as(contains_substring(
139-
"Value of: vec![19, 23, 11]\n\
140-
Expected: only contains elements that is greater than 15\n\
141-
Actual: [19, 23, 11], whose element #2 is 11, which is less than or equal to 15"
140+
"\
141+
Value of: vec![19, 23, 11]\n\
142+
Expected: only contains elements that is greater than 15\n\
143+
Actual: [
144+
19,
145+
23,
146+
11,
147+
], whose element #2 is 11, which is less than or equal to 15
148+
"
142149
)))
143150
)
144151
}

googletest/src/matchers/has_entry_matcher.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,13 @@ mod tests {
135135
verify_that!(
136136
result,
137137
err(displays_as(contains_substring(
138-
"Value of: HashMap::from([(0, 0)])\n\
139-
Expected: contains key 1, which value is equal to 0\n\
140-
Actual: {0: 0}, which doesn't contain key 1"
138+
"\
139+
Value of: HashMap::from([(0, 0)])
140+
Expected: contains key 1, which value is equal to 0
141+
Actual: {
142+
0: 0,
143+
}, which doesn't contain key 1
144+
"
141145
)))
142146
)
143147
}
@@ -149,9 +153,13 @@ mod tests {
149153
verify_that!(
150154
result,
151155
err(displays_as(contains_substring(
152-
"Value of: HashMap::from([(0, 0)])\n\
153-
Expected: contains key 0, which value is equal to 1\n\
154-
Actual: {0: 0}, which contains key 0, but is mapped to value 0, which isn't equal to 1"
156+
"\
157+
Value of: HashMap::from([(0, 0)])
158+
Expected: contains key 0, which value is equal to 1
159+
Actual: {
160+
0: 0,
161+
}, which contains key 0, but is mapped to value 0, which isn't equal to 1
162+
"
155163
)))
156164
)
157165
}

googletest/src/matchers/matches_pattern.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -460,9 +460,13 @@ mod tests {
460460
verify_that!(
461461
result,
462462
err(displays_as(contains_substring(
463-
"Value of: actual\n\
464-
Expected: has field `a_field`, which is equal to 234\n\
465-
Actual: AStruct { a_field: 123 }, which has field `a_field`, which isn't equal to 234"
463+
"\
464+
Value of: actual
465+
Expected: has field `a_field`, which is equal to 234
466+
Actual: AStruct {
467+
a_field: 123,
468+
}, which has field `a_field`, which isn't equal to 234
469+
"
466470
)))
467471
)
468472
}
@@ -483,11 +487,16 @@ mod tests {
483487
verify_that!(
484488
result,
485489
err(displays_as(contains_substring(
486-
"Value of: actual\n\
487-
Expected: has field `a_field`, which is equal to 234 and\n\
488-
has field `another_field`, which is equal to 123\n\
489-
Actual: AStruct { a_field: 123, another_field: 234 }, which has field `a_field`, which isn't equal to 234 AND\n\
490-
which has field `another_field`, which isn't equal to 123"
490+
"\
491+
Value of: actual
492+
Expected: has field `a_field`, which is equal to 234 and
493+
has field `another_field`, which is equal to 123
494+
Actual: AStruct {
495+
a_field: 123,
496+
another_field: 234,
497+
}, which has field `a_field`, which isn't equal to 234 AND
498+
which has field `another_field`, which isn't equal to 123
499+
"
491500
)))
492501
)
493502
}

0 commit comments

Comments
 (0)