#![doc(hidden)]
#[macro_export]
#[doc(hidden)]
macro_rules! __pointwise {
($matcher:expr, $container:expr) => {{
$crate::matchers::__internal_unstable_do_not_depend_on_these::PointwiseMatcher::new(
$container.into_iter().map($matcher).collect(),
)
}};
($matcher:expr, $left_container:expr, $right_container:expr) => {{
$crate::matchers::__internal_unstable_do_not_depend_on_these::PointwiseMatcher::new(
$left_container
.into_iter()
.zip($right_container.into_iter())
.map(|(l, r)| $matcher(l, r))
.collect(),
)
}};
($matcher:expr, $left_container:expr, $middle_container:expr, $right_container:expr) => {{
$crate::matchers::__internal_unstable_do_not_depend_on_these::PointwiseMatcher::new(
$left_container
.into_iter()
.zip($right_container.into_iter().zip($middle_container.into_iter()))
.map(|(l, (m, r))| $matcher(l, m, r))
.collect(),
)
}};
}
#[doc(hidden)]
pub mod internal {
use crate::description::Description;
use crate::matcher::{Matcher, MatcherBase, MatcherResult};
use crate::matcher_support::zipped_iterator::zip;
use std::fmt::Debug;
#[doc(hidden)]
#[derive(MatcherBase)]
pub struct PointwiseMatcher<MatcherT> {
matchers: Vec<MatcherT>,
}
impl<MatcherT> PointwiseMatcher<MatcherT> {
pub fn new(matchers: Vec<MatcherT>) -> Self {
Self { matchers }
}
}
impl<T: Debug + Copy, MatcherT: Matcher<T>, ContainerT: Copy + Debug> Matcher<ContainerT>
for PointwiseMatcher<MatcherT>
where
ContainerT: IntoIterator<Item = T>,
{
fn matches(&self, actual: ContainerT) -> MatcherResult {
let mut zipped_iterator = zip(actual.into_iter(), self.matchers.iter());
for (element, matcher) in zipped_iterator.by_ref() {
if matcher.matches(element).is_no_match() {
return MatcherResult::NoMatch;
}
}
if zipped_iterator.has_size_mismatch() {
MatcherResult::NoMatch
} else {
MatcherResult::Match
}
}
fn explain_match(&self, actual: ContainerT) -> Description {
let actual_iterator = actual.into_iter();
let mut zipped_iterator = zip(actual_iterator, self.matchers.iter());
let mut mismatches = Vec::new();
for (idx, (a, e)) in zipped_iterator.by_ref().enumerate() {
if e.matches(a).is_no_match() {
mismatches.push(format!("element #{idx} is {a:?}, {}", e.explain_match(a)));
}
}
if mismatches.is_empty() {
if !zipped_iterator.has_size_mismatch() {
"which matches all elements".into()
} else {
format!(
"which has size {} (expected {})",
zipped_iterator.left_size(),
self.matchers.len()
)
.into()
}
} else if mismatches.len() == 1 {
format!("where {}", mismatches[0]).into()
} else {
let mismatches = mismatches.into_iter().collect::<Description>();
format!("where:\n{}", mismatches.bullet_list().indent()).into()
}
}
fn describe(&self, matcher_result: MatcherResult) -> Description {
format!(
"{} elements satisfying respectively:\n{}",
if matcher_result.into() { "has" } else { "doesn't have" },
self.matchers
.iter()
.map(|m| m.describe(MatcherResult::Match))
.collect::<Description>()
.enumerate()
.indent()
)
.into()
}
}
}