Skip to content

Commit 9e610d5

Browse files
Merge pull request #260 from google:impl-matcher-for-tuples
PiperOrigin-RevId: 548131226
2 parents f5b70bd + 6837e48 commit 9e610d5

File tree

6 files changed

+116
-400
lines changed

6 files changed

+116
-400
lines changed

googletest/crate_docs.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ The following matchers are provided in GoogleTest Rust:
118118
| [`starts_with`] | A string starting with the given prefix. |
119119
| [`subset_of`] | A container all of whose elements are contained in the argument. |
120120
| [`superset_of`] | A container containing all elements of the argument. |
121-
| [`tuple!`] | A tuple whose elements the arguments match. |
122121
| [`unordered_elements_are!`] | A container whose elements the arguments match, in any order. |
123122

124123
[`anything`]: matchers::anything

googletest/src/assertions.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,62 @@
5858
/// `verify_that!(actual, elements_are![m1, m2, ...])`
5959
/// * `verify_that!(actual, {m1, m2, ...})` is equivalent to
6060
/// `verify_that!(actual, unordered_elements_are![m1, m2, ...])`
61+
///
62+
/// ## Matching against tuples
63+
///
64+
/// One can match against a tuple by constructing a tuple of matchers as
65+
/// follows:
66+
///
67+
/// ```
68+
/// # use googletest::prelude::*;
69+
/// # fn should_pass() -> Result<()> {
70+
/// verify_that!((123, 456), (eq(123), eq(456)))?; // Passes
71+
/// # Ok(())
72+
/// # }
73+
/// # fn should_fail() -> Result<()> {
74+
/// verify_that!((123, 456), (eq(123), eq(0)))?; // Fails: second matcher does not match
75+
/// # Ok(())
76+
/// # }
77+
/// # should_pass().unwrap();
78+
/// # should_fail().unwrap_err();
79+
/// ```
80+
///
81+
/// This also works with composed matchers:
82+
///
83+
/// ```
84+
/// # use googletest::prelude::*;
85+
/// # fn should_pass() -> Result<()> {
86+
/// verify_that!((123, 456), not((eq(456), eq(123))))?; // Passes
87+
/// # Ok(())
88+
/// # }
89+
/// # should_pass().unwrap();
90+
/// ```
91+
///
92+
/// Matchers must correspond to the actual tuple in count and type. Otherwise
93+
/// the test will fail to compile.
94+
///
95+
/// ```compile_fail
96+
/// # use googletest::prelude::*;
97+
/// # fn should_not_compile() -> Result<()> {
98+
/// verify_that!((123, 456), (eq(123),))?; // Does not compile: wrong tuple size
99+
/// verify_that!((123, "A string"), (eq(123), eq(456)))?; // Does not compile: wrong type
100+
/// # Ok(())
101+
/// # }
102+
/// ```
103+
///
104+
/// All fields must be covered by matchers. Use
105+
/// [`anything`][crate::matchers::anything] for fields which are not relevant
106+
/// for the test.
107+
///
108+
/// ```
109+
/// # use googletest::prelude::*;
110+
/// verify_that!((123, 456), (eq(123), anything()))
111+
/// # .unwrap();
112+
/// ```
113+
///
114+
/// This supports tuples of up to 12 elements. Tuples longer than that do not
115+
/// automatically inherit the `Debug` trait from their members, so are generally
116+
/// not supported; see [Rust by Example](https://doc.rust-lang.org/rust-by-example/primitives/tuples.html#tuples).
61117
#[macro_export]
62118
macro_rules! verify_that {
63119
($actual:expr, [$($expecteds:expr),+]) => {

googletest/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub mod prelude {
5151
// Matcher macros
5252
pub use super::{
5353
all, contains_each, elements_are, field, is_contained_in, matches_pattern, pat, pointwise,
54-
property, tuple, unordered_elements_are,
54+
property, unordered_elements_are,
5555
};
5656
}
5757

0 commit comments

Comments
 (0)