Skip to content

Commit 008b366

Browse files
bjacotgcopybara-github
authored andcommitted
Use Description in other matchers.
PiperOrigin-RevId: 515576928
1 parent b031979 commit 008b366

File tree

5 files changed

+270
-216
lines changed

5 files changed

+270
-216
lines changed

googletest/src/matchers/all_matcher.rs

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,13 @@ pub mod internal {
5757
use crate as googletest;
5858
#[cfg(google3)]
5959
use anything_matcher::anything;
60+
#[cfg(google3)]
61+
use description::Description;
6062
use googletest::matcher::{MatchExplanation, Matcher, MatcherResult};
6163
#[cfg(not(google3))]
6264
use googletest::matchers::anything;
65+
#[cfg(not(google3))]
66+
use googletest::matchers::description::Description;
6367
use std::fmt::Debug;
6468

6569
/// A matcher which matches an input value matched by all matchers in the
@@ -105,8 +109,12 @@ pub mod internal {
105109
matches!(component.matches(actual), MatcherResult::DoesNotMatch)
106110
})
107111
.map(|component| format!("{}", component.explain_match(actual)))
108-
.collect::<Vec<_>>();
109-
MatchExplanation::create(failures.join(" AND\n"))
112+
.collect::<Description>();
113+
if failures.len() == 1 {
114+
MatchExplanation::create(format!("{}", failures))
115+
} else {
116+
MatchExplanation::create(format!("\n{}", failures.bullet_list().indent()))
117+
}
110118
}
111119
}
112120
}
@@ -115,12 +123,22 @@ pub mod internal {
115123
match N {
116124
0 => anything::<T>().describe(matcher_result),
117125
1 => self.components[0].describe(matcher_result),
118-
_ => self
119-
.components
120-
.iter()
121-
.map(|m| m.describe(matcher_result))
122-
.collect::<Vec<_>>()
123-
.join(matcher_result.pick(" and\n", " or\n")),
126+
_ => {
127+
let properties = self
128+
.components
129+
.iter()
130+
.map(|m| m.describe(matcher_result))
131+
.collect::<Description>()
132+
.bullet_list()
133+
.indent();
134+
format!(
135+
"{}:\n{properties}",
136+
matcher_result.pick(
137+
"has all the following properties",
138+
"has at least one of the following properties"
139+
)
140+
)
141+
}
124142
}
125143
}
126144
}
@@ -138,7 +156,8 @@ mod tests {
138156
matcher::{Matcher, MatcherResult},
139157
verify_that, Result,
140158
};
141-
use matchers::{contains_substring, displays_as, ends_with, eq, not, starts_with};
159+
use indoc::indoc;
160+
use matchers::{contains_substring, displays_as, ends_with, eq, err, not, starts_with};
142161

143162
#[google_test]
144163
fn matches_any_value_when_list_is_empty() -> Result<()> {
@@ -181,7 +200,12 @@ mod tests {
181200

182201
verify_that!(
183202
matcher.describe(MatcherResult::Matches),
184-
eq("starts with prefix \"A\" and\nends with suffix \"string\"")
203+
eq(indoc!(
204+
"
205+
has all the following properties:
206+
* starts with prefix \"A\"
207+
* ends with suffix \"string\""
208+
))
185209
)
186210
}
187211

@@ -222,7 +246,7 @@ mod tests {
222246
verify_that!(
223247
all!(starts_with("One"), starts_with("Two")).explain_match("Three"),
224248
displays_as(eq(
225-
"which does not start with \"One\" AND\nwhich does not start with \"Two\""
249+
"\n * which does not start with \"One\"\n * which does not start with \"Two\""
226250
))
227251
)
228252
}
@@ -231,4 +255,24 @@ mod tests {
231255
fn mismatch_description_empty_matcher() -> Result<()> {
232256
verify_that!(all!().explain_match("Three"), displays_as(eq("which is anything")))
233257
}
258+
259+
#[google_test]
260+
fn all_multiple_failed_assertions() -> Result<()> {
261+
let result = verify_that!(4, all![eq(1), eq(2), eq(3)]);
262+
verify_that!(
263+
result,
264+
err(displays_as(contains_substring(indoc!(
265+
"
266+
Value of: 4
267+
Expected: has all the following properties:
268+
* is equal to 1
269+
* is equal to 2
270+
* is equal to 3
271+
Actual: 4,
272+
* which isn't equal to 1
273+
* which isn't equal to 2
274+
* which isn't equal to 3"
275+
))))
276+
)
277+
}
234278
}

googletest/src/matchers/each_matcher.rs

Lines changed: 55 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414

1515
#[cfg(not(google3))]
1616
use crate as googletest;
17+
#[cfg(google3)]
18+
use description::Description;
1719
use googletest::matcher::{MatchExplanation, Matcher, MatcherResult};
20+
#[cfg(not(google3))]
21+
use googletest::matchers::description::Description;
1822
use std::fmt::Debug;
1923

2024
/// Matches a container all of whose elements are matched by the matcher
@@ -81,7 +85,7 @@ where
8185
if non_matching_elements.len() == 1 {
8286
let (idx, element, explanation) = non_matching_elements.remove(0);
8387
return MatchExplanation::create(format!(
84-
"whose element #{idx} is {element:#?}, {explanation}"
88+
"whose element #{idx} is {element:?}, {explanation}"
8589
));
8690
}
8791

@@ -92,9 +96,9 @@ where
9296
.join(", ");
9397
let element_explanations = non_matching_elements
9498
.iter()
95-
.map(|&(_, element, ref explanation)| format!("{element:#?}, {explanation}"))
96-
.collect::<Vec<_>>()
97-
.join("\n");
99+
.map(|&(_, element, ref explanation)| format!("{element:?}, {explanation}"))
100+
.collect::<Description>()
101+
.indent();
98102
MatchExplanation::create(format!(
99103
"whose elements {failed_indexes} don't match\n{element_explanations}"
100104
))
@@ -123,6 +127,7 @@ mod tests {
123127
#[cfg(not(google3))]
124128
use googletest::matchers;
125129
use googletest::{google_test, verify_that, Result};
130+
use indoc::indoc;
126131
use matchers::{contains_substring, displays_as, eq, err, gt, not};
127132
use std::collections::HashSet;
128133

@@ -174,17 +179,16 @@ mod tests {
174179

175180
verify_that!(
176181
result,
177-
err(displays_as(contains_substring(
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-
"
187-
)))
182+
err(displays_as(contains_substring(indoc!(
183+
"
184+
Value of: vec![0, 2, 3]
185+
Expected: only contains elements that is greater than 0
186+
Actual: [
187+
0,
188+
2,
189+
3,
190+
], whose element #0 is 0, which is less than or equal to 0"
191+
))))
188192
)
189193
}
190194

@@ -194,17 +198,16 @@ Actual: [
194198

195199
verify_that!(
196200
result,
197-
err(displays_as(contains_substring(
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-
"
207-
)))
201+
err(displays_as(contains_substring(indoc!(
202+
"
203+
Value of: vec![1, 0, 3]
204+
Expected: only contains elements that is greater than 0
205+
Actual: [
206+
1,
207+
0,
208+
3,
209+
], whose element #1 is 0, which is less than or equal to 0"
210+
))))
208211
)
209212
}
210213

@@ -214,19 +217,18 @@ Actual: [
214217

215218
verify_that!(
216219
result,
217-
err(displays_as(contains_substring(
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-
"
229-
)))
220+
err(displays_as(contains_substring(indoc!(
221+
"
222+
Value of: vec![0, 1, 3]
223+
Expected: only contains elements that is greater than 1
224+
Actual: [
225+
0,
226+
1,
227+
3,
228+
], whose elements #0, #1 don't match
229+
0, which is less than or equal to 1
230+
1, which is less than or equal to 1"
231+
))))
230232
)
231233
}
232234
#[google_test]
@@ -235,24 +237,20 @@ Actual: [
235237

236238
verify_that!(
237239
result,
238-
err(displays_as(contains_substring(
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 [
251-
1,
252-
2,
253-
], whose element #1 is 2, which isn't equal to 1
254-
"
255-
)))
240+
err(displays_as(contains_substring(indoc!(
241+
"
242+
Value of: vec![vec! [1, 2], vec! [1]]
243+
Expected: only contains elements that only contains elements that is equal to 1
244+
Actual: [
245+
[
246+
1,
247+
2,
248+
],
249+
[
250+
1,
251+
],
252+
], whose element #0 is [1, 2], whose element #1 is 2, which isn't equal to 1"
253+
))))
256254
)
257255
}
258256
}

googletest/src/matchers/matches_pattern.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ mod tests {
379379
#[cfg(not(google3))]
380380
use googletest::matchers;
381381
use googletest::{google_test, verify_that, Result};
382+
use indoc::indoc;
382383
use matchers::{contains_substring, displays_as, eq, err, not};
383384

384385
#[google_test]
@@ -509,21 +510,21 @@ Actual: AStruct {
509510
actual,
510511
matches_pattern!(AStruct { a_field: eq(234), another_field: eq(123) })
511512
);
512-
// TODO(bjacotg) Improve this error message. The format is not obvious.
513513
verify_that!(
514514
result,
515-
err(displays_as(contains_substring(
516-
"\
517-
Value of: actual
518-
Expected: has field `a_field`, which is equal to 234 and
519-
has field `another_field`, which is equal to 123
520-
Actual: AStruct {
521-
a_field: 123,
522-
another_field: 234,
523-
}, which has field `a_field`, which isn't equal to 234 AND
524-
which has field `another_field`, which isn't equal to 123
525-
"
526-
)))
515+
err(displays_as(contains_substring(indoc!(
516+
"
517+
Value of: actual
518+
Expected: has all the following properties:
519+
* has field `a_field`, which is equal to 234
520+
* has field `another_field`, which is equal to 123
521+
Actual: AStruct {
522+
a_field: 123,
523+
another_field: 234,
524+
},
525+
* which has field `a_field`, which isn't equal to 234
526+
* which has field `another_field`, which isn't equal to 123"
527+
))))
527528
)
528529
}
529530

0 commit comments

Comments
 (0)