Skip to content

Commit 3f106d6

Browse files
committed
Allow to omit [unordered_]elements_are!
1 parent 44c1e2d commit 3f106d6

8 files changed

+80
-0
lines changed

googletest/src/assertions.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@
5454
/// ```
5555
#[macro_export]
5656
macro_rules! verify_that {
57+
($actual:expr, [$($expecteds:expr),+]) => {
58+
$crate::assertions::internal::check_matcher(
59+
&$actual,
60+
$crate::elements_are![$($expecteds),+],
61+
stringify!($actual),
62+
$crate::internal::source_location::SourceLocation::new(file!(), line!(), column!()),
63+
)
64+
};
65+
($actual:expr, {$($expecteds:expr),+}) => {
66+
$crate::assertions::internal::check_matcher(
67+
&$actual,
68+
$crate::unordered_elements_are![$($expecteds),+],
69+
stringify!($actual),
70+
$crate::internal::source_location::SourceLocation::new(file!(), line!(), column!()),
71+
)
72+
};
5773
($actual:expr, $expected:expr) => {
5874
$crate::assertions::internal::check_matcher(
5975
&$actual,

googletest/src/matchers/elements_are_matcher.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@
3939
/// # .unwrap();
4040
/// ```
4141
///
42+
/// This can also be omitted in [`verify_that!`] macros and replaced with square brackets.
43+
///
44+
/// ```
45+
/// # use googletest::prelude::*;
46+
/// verify_that!(vec![1, 2], [eq(1), eq(2)])
47+
/// # .unwrap();
48+
/// ```
49+
///
4250
/// This matcher does not support matching directly against an [`Iterator`]. To
4351
/// match against an iterator, use [`Iterator::collect`] to build a [`Vec`].
4452
///

googletest/src/matchers/unordered_elements_are_matcher.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@
6161
/// # .unwrap();
6262
/// ```
6363
///
64+
/// This can also be omitted in [`verify_that!`] macros and replaced with curly brackets.
65+
///
66+
/// ```
67+
/// # use googletest::prelude::*;
68+
/// verify_that!(vec![1, 2], {eq(2), eq(1)})
69+
/// # .unwrap();
70+
/// ```
71+
///
6472
/// This matcher does not support matching directly against an [`Iterator`]. To
6573
/// match against an iterator, use [`Iterator::collect`] to build a [`Vec`].
6674
///

googletest/tests/elements_are_matcher_test.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,9 @@ 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+
123+
#[test]
124+
fn elements_are_implicitly_called() -> Result<()> {
125+
verify_that!(vec![1,2,3], [eq(1), eq(2), eq(3)])
126+
}

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(1), eq(2), eq(3)})
42+
}
43+
3844
#[test]
3945
fn unordered_elements_are_matches_slice() -> Result<()> {
4046
let value = vec![1, 2, 3];

integration_tests/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,8 @@ test = false
148148
name = "verify_predicate_with_failure_as_method_in_submodule"
149149
path = "src/verify_predicate_with_failure_as_method_in_submodule.rs"
150150
test = false
151+
152+
[[bin]]
153+
name = "verify_that_omitted_container_matcher"
154+
path = "src/verify_that_omitted_container_matcher.rs"
155+
test = false
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
fn main() {}
16+
17+
#[cfg(test)]
18+
mod tests {
19+
use googletest::prelude::*;
20+
21+
#[googletest::test]
22+
fn verify_that_ordered_elements_omitted() -> Result<()> {
23+
verify_that!(vec![1, 2], [eq(1), eq(2)])
24+
}
25+
26+
#[googletest::test]
27+
fn verify_that_unordered_elements_omitted() -> Result<()> {
28+
verify_that!(vec![1, 2], {eq(2), eq(1)})
29+
}
30+
}

run_integration_tests.sh

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ INTEGRATION_TEST_BINARIES=(
4646
"two_non_fatal_failures"
4747
"verify_predicate_with_failure"
4848
"verify_predicate_with_failure_as_method_in_submodule"
49+
"verify_that_omitted_container_matcher"
4950
)
5051

5152
cargo build

0 commit comments

Comments
 (0)