#![doc(hidden)]
use crate::{
description::Description,
matcher::{Matcher, MatcherBase, MatcherResult},
};
use std::fmt::Debug;
#[doc(hidden)]
#[derive(MatcherBase)]
pub struct DisjunctionMatcher<M1, M2> {
m1: M1,
m2: M2,
}
impl<M1, M2> DisjunctionMatcher<M1, M2> {
pub fn new(m1: M1, m2: M2) -> Self {
Self { m1, m2 }
}
}
impl<T: Debug + Copy, M1: Matcher<T>, M2: Matcher<T>> Matcher<T> for DisjunctionMatcher<M1, M2> {
fn matches(&self, actual: T) -> MatcherResult {
match (self.m1.matches(actual), self.m2.matches(actual)) {
(MatcherResult::NoMatch, MatcherResult::NoMatch) => MatcherResult::NoMatch,
_ => MatcherResult::Match,
}
}
fn explain_match(&self, actual: T) -> Description {
match (self.m1.matches(actual), self.m2.matches(actual)) {
(MatcherResult::NoMatch, MatcherResult::Match) => self.m1.explain_match(actual),
(MatcherResult::Match, MatcherResult::NoMatch) => self.m2.explain_match(actual),
(_, _) => {
let m1_description = self.m1.explain_match(actual);
if m1_description.is_disjunction_description() {
m1_description.nested(self.m2.explain_match(actual))
} else {
Description::new()
.bullet_list()
.collect([m1_description, self.m2.explain_match(actual)])
.disjunction_description()
}
}
}
}
fn describe(&self, matcher_result: MatcherResult) -> Description {
let m1_description = self.m1.describe(matcher_result);
if m1_description.is_disjunction_description() {
m1_description.push_in_last_nested(self.m2.describe(matcher_result))
} else {
let header = if matcher_result.into() {
"has at least one of the following properties:"
} else {
"has all of the following properties:"
};
Description::new()
.text(header)
.nested(
Description::new()
.bullet_list()
.collect([m1_description, self.m2.describe(matcher_result)]),
)
.disjunction_description()
}
}
}
#[cfg(test)]
mod tests {
use crate::prelude::*;
use crate::Result;
use indoc::indoc;
#[test]
fn or_true_true_matches() -> Result<()> {
verify_that!(1, anything().or(anything()))
}
#[test]
fn or_true_false_matches() -> Result<()> {
verify_that!(1, anything().or(not(anything())))
}
#[test]
fn or_false_true_matches() -> Result<()> {
verify_that!(1, not(anything()).or(anything()))
}
#[test]
fn or_false_false_does_not_match() -> Result<()> {
let result = verify_that!(1, not(anything()).or(not(anything())));
verify_that!(
result,
err(displays_as(contains_substring(indoc!(
"
Value of: 1
Expected: has at least one of the following properties:
* never matches
* never matches
Actual: 1,
* which is anything
* which is anything
"
))))
)
}
#[test]
fn chained_or_matches() -> Result<()> {
verify_that!(10, eq(1).or(eq(5)).or(ge(9)))
}
#[test]
fn works_with_str_slices() -> Result<()> {
verify_that!("A string", ends_with("A").or(ends_with("string")))
}
#[test]
fn works_with_owned_strings() -> Result<()> {
verify_that!("A string".to_string(), ends_with("A").or(ends_with("string")))
}
}