Skip to content

Commit

Permalink
Replace the tuple! macro with an implementation of Matcher for tu…
Browse files Browse the repository at this point in the history
…ples.

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!`.
  • Loading branch information
hovinen committed Jul 14, 2023
1 parent f5b70bd commit 50b8b80
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 400 deletions.
1 change: 0 additions & 1 deletion googletest/crate_docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ The following matchers are provided in GoogleTest Rust:
| [`starts_with`] | A string starting with the given prefix. |
| [`subset_of`] | A container all of whose elements are contained in the argument. |
| [`superset_of`] | A container containing all elements of the argument. |
| [`tuple!`] | A tuple whose elements the arguments match. |
| [`unordered_elements_are!`] | A container whose elements the arguments match, in any order. |

[`anything`]: matchers::anything
Expand Down
55 changes: 55 additions & 0 deletions googletest/src/assertions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,61 @@
/// `verify_that!(actual, elements_are![m1, m2, ...])`
/// * `verify_that!(actual, {m1, m2, ...})` is equivalent to
/// `verify_that!(actual, unordered_elements_are![m1, m2, ...])`
///
/// ## Matching against tuples
///
/// One can match against a tuple by constructing a tuple of matchers as follows:
///
/// ```
/// # use googletest::prelude::*;
/// # fn should_pass() -> Result<()> {
/// verify_that!((123, 456), (eq(123), eq(456)))?; // Passes
/// # Ok(())
/// # }
/// # fn should_fail() -> Result<()> {
/// verify_that!((123, 456), (eq(123), eq(0)))?; // Fails: second matcher does not match
/// # Ok(())
/// # }
/// # should_pass().unwrap();
/// # should_fail().unwrap_err();
/// ```
///
/// This also works with composed matchers:
///
/// ```
/// # use googletest::prelude::*;
/// # fn should_pass() -> Result<()> {
/// verify_that!((123, 456), not((eq(456), eq(123))))?; // Passes
/// # Ok(())
/// # }
/// # should_pass().unwrap();
/// ```
///
/// Matchers must correspond to the actual tuple in count and type. Otherwise
/// the test will fail to compile.
///
/// ```compile_fail
/// # use googletest::prelude::*;
/// # fn should_not_compile() -> Result<()> {
/// verify_that!((123, 456), (eq(123),))?; // Does not compile: wrong tuple size
/// verify_that!((123, "A string"), (eq(123), eq(456)))?; // Does not compile: wrong type
/// # Ok(())
/// # }
/// ```
///
/// All fields must be covered by matchers. Use
/// [`anything`][crate::matchers::anything] for fields which are not relevant
/// for the test.
///
/// ```
/// # use googletest::prelude::*;
/// verify_that!((123, 456), (eq(123), anything()))
/// # .unwrap();
/// ```
///
/// This supports tuples of up to 12 elements. Tuples longer than that do not
/// automatically inherit the `Debug` trait from their members, so are generally
/// not supported; see [Rust by Example](https://doc.rust-lang.org/rust-by-example/primitives/tuples.html#tuples).
#[macro_export]
macro_rules! verify_that {
($actual:expr, [$($expecteds:expr),+]) => {
Expand Down
2 changes: 1 addition & 1 deletion googletest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub mod prelude {
// Matcher macros
pub use super::{
all, contains_each, elements_are, field, is_contained_in, matches_pattern, pat, pointwise,
property, tuple, unordered_elements_are,
property, unordered_elements_are,
};
}

Expand Down
Loading

0 comments on commit 50b8b80

Please sign in to comment.