Skip to content

Commit cc92690

Browse files
Merge pull request #237 from google:omitted_elements_are
PiperOrigin-RevId: 542872969
2 parents a5fa333 + 99a32fc commit cc92690

File tree

6 files changed

+96
-0
lines changed

6 files changed

+96
-0
lines changed

googletest/src/assertions.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,30 @@
5252
/// # verify_that!(should_fail(), err(displays_as(contains_substring("Expected: is equal to 123"))))
5353
/// # .unwrap();
5454
/// ```
55+
///
56+
/// This macro has special support for matching against container. Namely:
57+
/// * `verify_that!(actual, [m1, m2, ...])` is equivalent to
58+
/// `verify_that!(actual, elements_are![m1, m2, ...])`
59+
/// * `verify_that!(actual, {m1, m2, ...})` is equivalent to
60+
/// `verify_that!(actual, unordered_elements_are![m1, m2, ...])`
5561
#[macro_export]
5662
macro_rules! verify_that {
63+
($actual:expr, [$($expecteds:expr),+]) => {
64+
$crate::assertions::internal::check_matcher(
65+
&$actual,
66+
$crate::elements_are![$($expecteds),+],
67+
stringify!($actual),
68+
$crate::internal::source_location::SourceLocation::new(file!(), line!(), column!()),
69+
)
70+
};
71+
($actual:expr, {$($expecteds:expr),+}) => {
72+
$crate::assertions::internal::check_matcher(
73+
&$actual,
74+
$crate::unordered_elements_are![$($expecteds),+],
75+
stringify!($actual),
76+
$crate::internal::source_location::SourceLocation::new(file!(), line!(), column!()),
77+
)
78+
};
5779
($actual:expr, $expected:expr) => {
5880
$crate::assertions::internal::check_matcher(
5981
&$actual,

googletest/src/matchers/elements_are_matcher.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,32 @@
3939
/// # .unwrap();
4040
/// ```
4141
///
42+
/// This can also be omitted in [`verify_that!`] macros and replaced with square
43+
/// brackets.
44+
///
45+
/// ```
46+
/// # use googletest::prelude::*;
47+
/// verify_that!(vec![1, 2], [eq(1), eq(2)])
48+
/// # .unwrap();
49+
/// ```
50+
///
51+
/// Note: This behavior is only possible in [`verify_that!`] macros. In any
52+
/// other cases, it is still necessary to use the
53+
/// [`elements_are!`][crate::elements_are] macro.
54+
///
55+
/// ```compile_fail
56+
/// # use googletest::prelude::*;
57+
/// verify_that!(vec![vec![1,2], vec![3]], [[eq(1), eq(2)], [eq(3)]])
58+
/// # .unwrap();
59+
/// ```
60+
///
61+
/// Use this instead:
62+
/// ```
63+
/// # use googletest::prelude::*;
64+
/// verify_that!(vec![vec![1,2], vec![3]], [elements_are![eq(1), eq(2)], elements_are![eq(3)]])
65+
/// # .unwrap();
66+
/// ```
67+
///
4268
/// This matcher does not support matching directly against an [`Iterator`]. To
4369
/// match against an iterator, use [`Iterator::collect`] to build a [`Vec`].
4470
///

googletest/src/matchers/unordered_elements_are_matcher.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,33 @@
6161
/// # .unwrap();
6262
/// ```
6363
///
64+
/// This can also be omitted in [`verify_that!`] macros and replaced with curly
65+
/// brackets.
66+
///
67+
/// ```
68+
/// # use googletest::prelude::*;
69+
/// verify_that!(vec![1, 2], {eq(2), eq(1)})
70+
/// # .unwrap();
71+
/// ```
72+
///
73+
/// Note: This behavior is only possible in [`verify_that!`] macros. In any
74+
/// other cases, it is still necessary to use the
75+
/// [`unordered_elements_are!`][crate::unordered_elements_are] macro.
76+
///
77+
/// ```compile_fail
78+
/// # use googletest::prelude::*;
79+
/// verify_that!(vec![vec![1,2], vec![3]], {{eq(2), eq(1)}, {eq(3)}})
80+
/// # .unwrap();
81+
/// ```
82+
///
83+
/// Use this instead:
84+
/// ```
85+
/// # use googletest::prelude::*;
86+
/// verify_that!(vec![vec![1,2], vec![3]],
87+
/// {unordered_elements_are![eq(2), eq(1)], unordered_elements_are![eq(3)]})
88+
/// # .unwrap();
89+
/// ```
90+
///
6491
/// This matcher does not support matching directly against an [`Iterator`]. To
6592
/// match against an iterator, use [`Iterator::collect`] to build a [`Vec`].
6693
///

googletest/tests/elements_are_matcher_test.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,8 @@ fn create_matcher() -> impl Matcher<ActualT = Vec<i32>> {
118118
fn elements_are_works_when_matcher_is_created_in_subroutine() -> Result<()> {
119119
verify_that!(vec![1], create_matcher())
120120
}
121+
122+
#[test]
123+
fn elements_are_implicitly_called() -> Result<()> {
124+
verify_that!(vec![1, 2, 3], [eq(1), eq(2), eq(3)])
125+
}

googletest/tests/unordered_elements_are_matcher_test.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ fn unordered_elements_are_matches_vector() -> Result<()> {
3535
verify_that!(value, unordered_elements_are![eq(1), eq(2), eq(3)])
3636
}
3737

38+
#[test]
39+
fn unordered_elements_are_omitted() -> Result<()> {
40+
let value = vec![1, 2, 3];
41+
verify_that!(value, {eq(3), eq(2), eq(1)})
42+
}
43+
3844
#[test]
3945
fn unordered_elements_are_matches_slice() -> Result<()> {
4046
let value = vec![1, 2, 3];

integration_tests/src/integration_tests.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ mod tests {
2626
verify_that!(value, eq(2))
2727
}
2828

29+
#[test]
30+
fn should_pass_with_omitted_elements_are() -> Result<()> {
31+
verify_that!(vec![1, 2], [eq(1), eq(2)])
32+
}
33+
34+
#[test]
35+
fn should_pass_with_omitted_unordered_elements_are() -> Result<()> {
36+
verify_that!(vec![1, 2], {eq(2), eq(1)})
37+
}
38+
2939
#[googletest::google_test]
3040
fn should_pass_with_google_test() -> Result<()> {
3141
let value = 2;

0 commit comments

Comments
 (0)