Function xpct::each

source ·
pub fn each<'a, T>(
    block: impl FnOnce(&mut CombinatorContext<T>) + 'a
) -> Matcher<'a, T, T>where
    T: 'a,
Expand description

Succeeds when each of the passed matchers succeeds.

This is a matcher than can be used to compose other matchers. It’s similar to all, except it does not short-circuit and it does not chain the output of each matcher into the next. You can use matcher this when:

  1. You want to test all the matchers instead of just failing early and printing the first failure.
  2. You want to perform multiple assertions on the same value without transforming it (like be_ok and be_some do).

This matcher owns its value and passes it to each matcher, either by reference, or by value if the value is Clone or Copy. The closure you pass to this matcher accepts a CombinatorContext, which has methods like borrow, cloned and copied to determine how the value is passed to matchers. From there, you can call to and to_not to use matchers.

Examples

Passing the value to matchers by reference:

use xpct::{each, expect, have_len, match_regex};

expect!("11b72db5-ff70-40a5-8728-937faf86ce48").to(each(|ctx| {
    ctx.borrow::<str>()
        .to(have_len(36))
        .to(match_regex("[0-9a-f-]+"));
}));

Passing the value to matchers by value via Clone:

use xpct::{each, expect, have_len, match_regex};

let uuid = String::from("11b72db5-ff70-40a5-8728-937faf86ce48");

expect!(uuid).to(each(|ctx| {
    ctx.cloned()
        .to(have_len(36))
        .to(match_regex("[0-9a-f-]+"));
}));