Skip to content

Commit 58506d2

Browse files
hovinenbcopybara-github
authored andcommitted
Move some tests of UnorderedElementsOfMapAreMatcher into the unit tests of the module matchers::unordered_elements_are_matcher.
These tests exercise some of the more internal aspects of the matcher rather than using `verify_that!` with the matcher itself. Because of the type inference issues such use creates, it is more convenient to locate these tests in the unit tests rather than integration tests. This will become particularly apparent in a follow-up change which modifies the `Matcher` trait to use an associated type rather than a type parameter. PiperOrigin-RevId: 527874814
1 parent 5be37e0 commit 58506d2

File tree

2 files changed

+66
-55
lines changed

2 files changed

+66
-55
lines changed

googletest/src/matchers/unordered_elements_are_matcher.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,3 +1056,67 @@ pub mod internal {
10561056
}
10571057
}
10581058
}
1059+
1060+
#[cfg(test)]
1061+
mod tests {
1062+
use crate::matcher::Matcher;
1063+
#[cfg(not(google3))]
1064+
use crate::matchers;
1065+
#[cfg(not(google3))]
1066+
use crate::unordered_elements_are;
1067+
use crate::{matcher::MatcherResult, verify_that, Result};
1068+
use indoc::indoc;
1069+
use matchers::{anything, contains_regex, contains_substring, displays_as, eq, AndMatcherExt};
1070+
use std::collections::HashMap;
1071+
1072+
#[test]
1073+
fn has_correct_description_for_map() -> Result<()> {
1074+
// UnorderedElementsAreMatcher maintains references to the matchers, so the
1075+
// constituent matchers must live longer. Inside a verify_that! macro, the
1076+
// compiler takes care of that, but when the matcher is created separately,
1077+
// we must create the constitute matchers separately so that they
1078+
// aren't dropped too early.
1079+
let matchers = ((eq(2), eq("Two")), (eq(1), eq("One")), (eq(3), eq("Three")));
1080+
let matcher = unordered_elements_are![
1081+
(matchers.0.0, matchers.0.1),
1082+
(matchers.1.0, matchers.1.1),
1083+
(matchers.2.0, matchers.2.1)
1084+
];
1085+
verify_that!(
1086+
Matcher::<HashMap<i32, &str>>::describe(&matcher, MatcherResult::Matches),
1087+
eq(indoc!(
1088+
"
1089+
contains elements matching in any order:
1090+
is equal to 2 => is equal to \"Two\"
1091+
is equal to 1 => is equal to \"One\"
1092+
is equal to 3 => is equal to \"Three\""
1093+
))
1094+
)
1095+
}
1096+
1097+
#[test]
1098+
fn unordered_elements_are_description_no_full_match_with_map() -> Result<()> {
1099+
// UnorderedElementsAreMatcher maintains references to the matchers, so the
1100+
// constituent matchers must live longer. Inside a verify_that! macro, the
1101+
// compiler takes care of that, but when the matcher is created separately,
1102+
// we must create the constitute matchers separately so that they
1103+
// aren't dropped too early.
1104+
let matchers = ((anything(), eq(1)), (anything(), eq(2)), (anything(), eq(2)));
1105+
let matcher = unordered_elements_are![
1106+
(matchers.0.0, matchers.0.1),
1107+
(matchers.1.0, matchers.1.1),
1108+
(matchers.2.0, matchers.2.1),
1109+
];
1110+
let value: HashMap<u32, u32> = HashMap::from_iter([(0, 1), (1, 1), (2, 2)]);
1111+
verify_that!(
1112+
matcher.explain_match(&value),
1113+
displays_as(contains_regex(
1114+
"Actual element 2 => 2 at index [0-2] matched expected element `is anything` => `is equal to 2` at index [0-2]."
1115+
)).and(displays_as(contains_regex(
1116+
"Actual element [0-1] => [0-1] at index [0-2] did not match any remaining expected element."
1117+
))).and(displays_as(contains_substring(
1118+
"Expected element `is anything` => `is equal to 2` at index 2 did not match any remaining actual element."
1119+
)))
1120+
)
1121+
}
1122+
}

googletest/tests/unordered_elements_are_matcher_test.rs

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@ use googletest::matcher::Matcher;
1717
use googletest::matchers;
1818
#[cfg(not(google3))]
1919
use googletest::{contains_each, is_contained_in, unordered_elements_are};
20-
use googletest::{google_test, matcher::MatcherResult, verify_that, Result};
20+
use googletest::{google_test, verify_that, Result};
2121
use indoc::indoc;
22-
use matchers::{
23-
anything, contains_regex, contains_substring, displays_as, eq, err, ge, not, AndMatcherExt,
24-
};
2522
#[cfg(google3)]
2623
use matchers::{contains_each, is_contained_in, unordered_elements_are};
24+
use matchers::{contains_substring, displays_as, eq, err, ge, not};
2725
use std::collections::HashMap;
2826

2927
#[google_test]
@@ -150,31 +148,6 @@ fn unordered_elements_are_description_mismatch() -> Result<()> {
150148
)
151149
}
152150

153-
#[google_test]
154-
fn has_correct_description_for_map() -> Result<()> {
155-
// UnorderedElementsAreMatcher maintains references to the matchers, so the
156-
// constituent matchers must live longer. Inside a verify_that! macro, the
157-
// compiler takes care of that, but when the matcher is created separately,
158-
// we must create the constitute matchers separately so that they
159-
// aren't dropped too early.
160-
let matchers = ((eq(2), eq("Two")), (eq(1), eq("One")), (eq(3), eq("Three")));
161-
let matcher = unordered_elements_are![
162-
(matchers.0.0, matchers.0.1),
163-
(matchers.1.0, matchers.1.1),
164-
(matchers.2.0, matchers.2.1)
165-
];
166-
verify_that!(
167-
Matcher::<HashMap<u32, &'static str>>::describe(&matcher, MatcherResult::Matches),
168-
eq(indoc!(
169-
"
170-
contains elements matching in any order:
171-
is equal to 2 => is equal to \"Two\"
172-
is equal to 1 => is equal to \"One\"
173-
is equal to 3 => is equal to \"Three\""
174-
))
175-
)
176-
}
177-
178151
#[google_test]
179152
fn unordered_elements_are_matches_unordered() -> Result<()> {
180153
let value = vec![1, 2];
@@ -210,32 +183,6 @@ fn unordered_elements_are_description_no_full_match() -> Result<()> {
210183
)
211184
}
212185

213-
#[google_test]
214-
fn unordered_elements_are_description_no_full_match_with_map() -> Result<()> {
215-
// UnorderedElementsAreMatcher maintains references to the matchers, so the
216-
// constituent matchers must live longer. Inside a verify_that! macro, the
217-
// compiler takes care of that, but when the matcher is created separately,
218-
// we must create the constitute matchers separately so that they
219-
// aren't dropped too early.
220-
let matchers = ((anything(), eq(1)), (anything(), eq(2)), (anything(), eq(2)));
221-
let matcher = unordered_elements_are![
222-
(matchers.0.0, matchers.0.1),
223-
(matchers.1.0, matchers.1.1),
224-
(matchers.2.0, matchers.2.1),
225-
];
226-
let value: HashMap<u32, u32> = HashMap::from_iter([(0, 1), (1, 1), (2, 2)]);
227-
verify_that!(
228-
matcher.explain_match(&value),
229-
displays_as(contains_regex(
230-
"Actual element 2 => 2 at index [0-2] matched expected element `is anything` => `is equal to 2` at index [0-2]."
231-
)).and(displays_as(contains_regex(
232-
"Actual element [0-1] => [0-1] at index [0-2] did not match any remaining expected element."
233-
))).and(displays_as(contains_substring(
234-
"Expected element `is anything` => `is equal to 2` at index 2 did not match any remaining actual element."
235-
)))
236-
)
237-
}
238-
239186
#[google_test]
240187
fn unordered_elements_are_unmatchable_expected_description_mismatch() -> Result<()> {
241188
verify_that!(

0 commit comments

Comments
 (0)