macro_rules! unordered_elements_are {
    ($(,)?) => { ... };
    ($(($key_matcher:expr, $value_matcher:expr)),* $(,)?) => { ... };
    ($($matcher:expr),* $(,)?) => { ... };
}
Expand description

Matches a container whose elements in any order have a 1:1 correspondence with the provided element matchers.

verify_that!(vec![3, 2, 1], unordered_elements_are![eq(1), ge(2), anything()])?;   // Passes
verify_that!(vec![1], unordered_elements_are![eq(1), ge(2)])?;              // Fails: container has wrong size
verify_that!(vec![3, 2, 1], unordered_elements_are![eq(1), ge(4), eq(2)])?; // Fails: second matcher not matched
verify_that!(vec![3, 2, 1], unordered_elements_are![ge(3), ge(3), ge(3)])?; // Fails: no 1:1 correspondence

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

This can also match against HashMap and similar collections. The arguments are a sequence of pairs of matchers corresponding to the keys and their respective values.

let value: HashMap<u32, &'static str> =
    HashMap::from_iter([(1, "One"), (2, "Two"), (3, "Three")]);
verify_that!(
    value,
    unordered_elements_are![(eq(2), eq("Two")), (eq(1), eq("One")), (eq(3), eq("Three"))]
)

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

 verify_that!(vec![1, 2], {eq(2), eq(1)})

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

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

Use this instead:

verify_that!(vec![vec![1,2], vec![3]],
  {unordered_elements_are![eq(2), eq(1)], unordered_elements_are![eq(3)]})

This matcher does not support matching directly against an Iterator. To match against an iterator, use Iterator::collect to build a Vec.

The matcher proceeds in three stages:

  1. It first checks whether the actual value is of the right size to possibly be matched by each of the given matchers. If not, then it immediately fails explaining that the size is incorrect.

  2. It then checks whether each matcher matches at least one corresponding element in the actual container and each element in the actual container is matched by at least one matcher. If not, it fails with a message indicating which matcher respectively container elements had no counterparts.

  3. Finally, it checks whether the mapping of matchers to corresponding actual elements is a 1-1 correspondence and fails if that is not the case. The failure message then shows the best matching it could find, including which matchers did not have corresponding unique elements in the container and which container elements had no corresponding matchers.