Skip to main content

elements_are

Macro elements_are 

Source
macro_rules! elements_are {
    ($($matcher:expr),* $(,)?) => { ... };
}
Expand description

Matches a container’s elements to each matcher in order.

This macro produces a matcher against a container. It takes as arguments a sequence of matchers each of which should respectively match the corresponding element of the actual value.

verify_that!(vec![1, 2, 3], elements_are![eq(&1), anything(), gt(&0).and(lt(&123))])

The actual value must be a container such as a &Vec, an array, or a slice. More precisely, the actual value must implement IntoIterator.

let vector = vec![1, 2, 3];
let slice = vector.as_slice();
verify_that!(slice, elements_are![eq(&1), anything(), gt(&0).and(lt(&123))])

This can also be omitted in verify_that! macros and replaced with square brackets.

 verify_that!(vec![1, 2], [eq(&1), eq(&2)])

Note: This behavior is only possible in verify_that! macros. In any other cases, it is still necessary to use the elements_are! macro.

verify_that!(vec![vec![1,2], vec![3]], [[eq(&1), eq(&2)], [eq(&3)]])

Use this instead:

verify_that!(vec![vec![1,2], vec![3]], [elements_are![eq(&1), eq(&2)], elements_are![eq(&3)]])

If an inner matcher is eq(...), it can be omitted:


verify_that!(vec![1,2,3], elements_are![&1, lt(&1000), gt(&1)])

Do not use this with unordered containers, since that will lead to flaky tests. Use unordered_elements_are! instead.