Skip to main content

superset_of

Function superset_of 

Source
pub fn superset_of<ExpectedT>(subset: ExpectedT) -> SupersetOfMatcher<ExpectedT>
Expand description

Matches a container containing all of the items in the given container subset.

The element type ElementT must implement PartialEq to allow element comparison.

ActualT and ExpectedT can each be any container which implements IntoIterator. For instance, ActualT and ExpectedT can be a common container like &Vec, arrays or slices. They need not be the same container type.

let value = vec![1, 2, 3];
verify_that!(value, superset_of([&1, &2]))?;  // Passes
let array_value = [1, 2, 3];
verify_that!(array_value, superset_of([1, 2]))?;  // Passes
verify_that!(value, superset_of([&1, &2, &4]))?;  // Fails: 4 is not in the subset

let value: HashSet<i32> = [1, 2, 3].into();
verify_that!(value, superset_of([&1, &2, &3]))?;  // Passes

Item multiplicity in both the actual and expected containers is ignored:

let value: Vec<i32> = vec![0, 0, 1];
verify_that!(value, superset_of([&0, &1]))?;  // Passes
verify_that!(value, superset_of([&0, &1, &1]))?;  // Passes

A note on performance: This matcher uses a naive algorithm with a worst-case runtime proportional to the product of the sizes of the actual and expected containers as well as the time to check equality of each pair of items. It should not be used on especially large containers.