use crate::{
description::Description,
matcher::{Describable, Matcher, MatcherResult},
matchers::containers::{OwnedItems, RefItems},
};
use std::{fmt::Debug, marker::PhantomData};
pub fn superset_of<ExpectedT: Debug, Mode>(
subset: ExpectedT,
) -> __internal::SupersetOfMatcher<ExpectedT, Mode> {
__internal::SupersetOfMatcher { subset, phantom: Default::default() }
}
pub mod __internal {
use super::*;
#[doc(hidden)]
pub struct SupersetOfMatcher<ExpectedT, Mode> {
pub(super) subset: ExpectedT,
pub(super) phantom: PhantomData<Mode>,
}
impl<ElementT: Debug + PartialEq, ActualT: Debug + ?Sized, ExpectedT: Debug> Matcher<ActualT>
for SupersetOfMatcher<ExpectedT, RefItems>
where
for<'a> &'a ActualT: IntoIterator<Item = &'a ElementT>,
for<'a> &'a ExpectedT: IntoIterator<Item = &'a ElementT>,
{
fn matches(&self, actual: &ActualT) -> MatcherResult {
for expected_item in &self.subset {
if Self::actual_is_missing(actual, expected_item) {
return MatcherResult::NoMatch;
}
}
MatcherResult::Match
}
fn explain_match(&self, actual: &ActualT) -> Description {
let missing_items: Vec<_> = self
.subset
.into_iter()
.filter(|expected_item| Self::actual_is_missing(actual, expected_item))
.map(|expected_item| format!("{expected_item:#?}"))
.collect();
match missing_items.len() {
0 => "whose no element is missing".into(),
1 => format!("whose element {} is missing", &missing_items[0]).into(),
_ => format!("whose elements {} are missing", missing_items.join(", ")).into(),
}
}
}
impl<ExpectedT: Debug> SupersetOfMatcher<ExpectedT, RefItems> {
fn actual_is_missing<ElementT: PartialEq, ActualT: ?Sized>(
actual: &ActualT,
needle: &ElementT,
) -> bool
where
for<'a> &'a ActualT: IntoIterator<Item = &'a ElementT>,
{
!actual.into_iter().any(|item| *item == *needle)
}
}
impl<ElementT: Debug + PartialEq, ActualT: Debug + ?Sized, ExpectedT: Debug> Matcher<ActualT>
for SupersetOfMatcher<ExpectedT, OwnedItems>
where
for<'a> &'a ActualT: IntoIterator<Item = ElementT>,
for<'a> &'a ExpectedT: IntoIterator<Item = &'a ElementT>,
{
fn matches(&self, actual: &ActualT) -> MatcherResult {
for expected_item in &self.subset {
if Self::actual_is_missing(actual, expected_item) {
return MatcherResult::NoMatch;
}
}
MatcherResult::Match
}
fn explain_match(&self, actual: &ActualT) -> Description {
let missing_items: Vec<_> = self
.subset
.into_iter()
.filter(|expected_item| Self::actual_is_missing(actual, expected_item))
.map(|expected_item| format!("{expected_item:#?}"))
.collect();
match missing_items.len() {
0 => "whose no element is missing".into(),
1 => format!("whose element {} is missing", &missing_items[0]).into(),
_ => format!("whose elements {} are missing", missing_items.join(", ")).into(),
}
}
}
impl<ExpectedT: Debug> SupersetOfMatcher<ExpectedT, OwnedItems> {
fn actual_is_missing<ElementT: PartialEq, ActualT: ?Sized>(
actual: &ActualT,
needle: &ElementT,
) -> bool
where
for<'a> &'a ActualT: IntoIterator<Item = ElementT>,
{
!actual.into_iter().any(|item| item == *needle)
}
}
impl<ExpectedT: Debug, Mode> Describable for SupersetOfMatcher<ExpectedT, Mode> {
fn describe(&self, matcher_result: MatcherResult) -> Description {
match matcher_result {
MatcherResult::Match => format!("is a superset of {:#?}", self.subset).into(),
MatcherResult::NoMatch => format!("isn't a superset of {:#?}", self.subset).into(),
}
}
}
}
#[cfg(test)]
mod tests {
use super::superset_of;
use crate::prelude::*;
use indoc::indoc;
use std::collections::HashSet;
#[test]
fn superset_of_matches_empty_vec() -> TestResult<()> {
let value: Vec<i32> = vec![];
verify_that!(value, superset_of([]))
}
#[test]
fn superset_of_matches_vec_with_one_element_with_array() -> TestResult<()> {
verify_that!(vec![1], superset_of([1]))
}
#[test]
fn superset_of_matches_vec_with_one_element_with_vec() -> TestResult<()> {
verify_that!(vec![1], superset_of(vec![1]))
}
#[test]
fn superset_of_matches_array_of_one_element_with_array() -> TestResult<()> {
verify_that!([1], superset_of([1]))
}
#[test]
fn superset_of_matches_vec_with_two_elements() -> TestResult<()> {
verify_that!(vec![1, 2], superset_of([1, 2]))
}
#[test]
fn superset_of_matches_vec_when_actual_has_excess_element() -> TestResult<()> {
verify_that!(vec![1, 2, 3], superset_of([1, 2]))
}
#[test]
fn superset_of_matches_vec_when_actual_has_excess_element_first() -> TestResult<()> {
verify_that!(vec![3, 1, 2], superset_of([1, 2]))
}
#[test]
fn superset_of_matches_array_ref_with_one_element_using_points_to() -> TestResult<()> {
let value = &[1];
verify_that!(value, points_to(superset_of([1])))
}
#[test]
fn superset_of_matches_array_ref_with_one_element_using_deref_notation() -> TestResult<()> {
let value = &[1];
verify_that!(*value, superset_of([1]))
}
#[test]
fn superset_of_matches_slice_with_one_element_using_points_to() -> TestResult<()> {
let value = vec![1];
let slice = value.as_slice();
verify_that!(slice, points_to(superset_of([1])))
}
#[test]
fn superset_of_matches_slice_with_one_element_using_deref_notation() -> TestResult<()> {
let value = vec![1];
let slice = value.as_slice();
verify_that!(*slice, superset_of([1]))
}
#[derive(Debug, PartialEq)]
struct OwnedItemContainer(Vec<i32>);
impl<'a> IntoIterator for &'a OwnedItemContainer {
type Item = i32;
type IntoIter = std::iter::Copied<std::slice::Iter<'a, i32>>;
fn into_iter(self) -> Self::IntoIter {
self.0.iter().copied()
}
}
#[test]
fn superset_of_matches_on_container_when_ref_to_container_has_into_iterator_producing_owned_values()
-> TestResult<()> {
verify_that!(OwnedItemContainer(vec![1]), superset_of([1]))
}
#[test]
fn superset_of_matches_vec_of_string_slices() -> TestResult<()> {
verify_that!(
vec!["String 1", "String 2", "String 3"],
superset_of(["String 1", "String 2", "String 3"])
)
}
#[test]
fn superset_of_matches_vec_of_string_slices_with_non_static_lifetime() -> TestResult<()> {
let string_1 = String::from("String 1");
let string_2 = String::from("String 2");
let string_3 = String::from("String 3");
verify_that!(
vec![string_1.as_str(), string_2.as_str(), string_3.as_str()],
superset_of(["String 1", "String 2", "String 3"])
)
}
#[test]
fn superset_of_matches_slice_of_string_slices() -> TestResult<()> {
let value = vec!["String 1", "String 2", "String 3"];
verify_that!(value.as_slice(), points_to(superset_of(["String 1", "String 2", "String 3"])))
}
#[test]
fn superset_of_matches_slice_of_string_slices_with_non_static_lifetime() -> TestResult<()> {
let string_1 = String::from("String 1");
let string_2 = String::from("String 2");
let string_3 = String::from("String 3");
let value = vec![string_1.as_str(), string_2.as_str(), string_3.as_str()];
verify_that!(value.as_slice(), points_to(superset_of(["String 1", "String 2", "String 3"])))
}
#[test]
fn superset_of_matches_hash_set_with_one_element() -> TestResult<()> {
verify_that!(HashSet::from([1]), superset_of([1]))
}
#[test]
fn superset_of_does_not_match_when_first_element_does_not_match() -> TestResult<()> {
verify_that!(vec![0], not(superset_of([1])))
}
#[test]
fn superset_of_does_not_match_when_second_element_does_not_match() -> TestResult<()> {
verify_that!(vec![2], not(superset_of([2, 0])))
}
#[test]
fn superset_of_shows_correct_message_when_first_item_does_not_match() -> TestResult<()> {
let result = verify_that!(vec![0, 2, 3], superset_of([1, 2, 3]));
verify_that!(
result,
err(displays_as(contains_substring(indoc!(
"
Value of: vec![0, 2, 3]
Expected: is a superset of [
1,
2,
3,
]
Actual: [0, 2, 3],
whose element 1 is missing
"
))))
)
}
#[test]
fn superset_of_shows_correct_message_when_second_item_does_not_match() -> TestResult<()> {
let result = verify_that!(vec![1, 0, 3], superset_of([1, 2, 3]));
verify_that!(
result,
err(displays_as(contains_substring(indoc!(
"
Value of: vec![1, 0, 3]
Expected: is a superset of [
1,
2,
3,
]
Actual: [1, 0, 3],
whose element 2 is missing
"
))))
)
}
#[test]
fn superset_of_shows_correct_message_when_first_two_items_do_not_match() -> TestResult<()> {
let result = verify_that!(vec![0, 0, 3], superset_of([1, 2, 3]));
verify_that!(
result,
err(displays_as(contains_substring(indoc!(
"
Value of: vec![0, 0, 3]
Expected: is a superset of [
1,
2,
3,
]
Actual: [0, 0, 3],
whose elements 1, 2 are missing
"
))))
)
}
}