Skip to content

Commit 6837e48

Browse files
committed
Replace the tuple! macro with an implementation of Matcher for tuples.
This means that one can just put a bunch of matchers into a tuple and the result will itself be a matcher against corresponding tuples: ``` let value = (1, 2); verify_that!(value, (eq(1), eq(2))) ``` There is no need for a separate macro for this, since one can implement `Matcher` for a tuple of matchers directly. This is a breaking change, since it eliminates the `tuple!` macro. To port existing code, one just removes the call to `tuple!` and adds a trailing comma if necessary.
1 parent f5b70bd commit 6837e48

File tree

6 files changed

+115
-400
lines changed

6 files changed

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