|
| 1 | +// Copyright 2023 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// There are no visible documentation elements in this module; the declarative |
| 16 | +// macro is documented at the top level. |
| 17 | +#![doc(hidden)] |
| 18 | + |
| 19 | +/// Matches a value which at least one of the given matchers match. |
| 20 | +/// |
| 21 | +/// Each argument is a [`Matcher`][crate::matcher::Matcher] which matches |
| 22 | +/// against the actual value. |
| 23 | +/// |
| 24 | +/// For example: |
| 25 | +/// |
| 26 | +/// ``` |
| 27 | +/// # use googletest::prelude::*; |
| 28 | +/// # fn should_pass() -> Result<()> { |
| 29 | +/// verify_that!("A string", any!(starts_with("A"), ends_with("string")))?; // Passes |
| 30 | +/// verify_that!("A string", any!(starts_with("A"), starts_with("string")))?; // Passes |
| 31 | +/// verify_that!("A string", any!(ends_with("A"), ends_with("string")))?; // Passes |
| 32 | +/// # Ok(()) |
| 33 | +/// # } |
| 34 | +/// # fn should_fail() -> Result<()> { |
| 35 | +/// verify_that!("A string", any!(starts_with("An"), ends_with("not a string")))?; // Fails |
| 36 | +/// # Ok(()) |
| 37 | +/// # } |
| 38 | +/// # should_pass().unwrap(); |
| 39 | +/// # should_fail().unwrap_err(); |
| 40 | +/// ``` |
| 41 | +/// |
| 42 | +/// Using this macro is equivalent to using the |
| 43 | +/// [`or`][crate::matcher::Matcher::or] method: |
| 44 | +/// |
| 45 | +/// ``` |
| 46 | +/// # use googletest::prelude::*; |
| 47 | +/// # fn should_pass() -> Result<()> { |
| 48 | +/// verify_that!(10, gt(9).or(lt(8)))?; // Also passes |
| 49 | +/// # Ok(()) |
| 50 | +/// # } |
| 51 | +/// # should_pass().unwrap(); |
| 52 | +/// ``` |
| 53 | +/// |
| 54 | +/// Assertion failure messages are not guaranteed to be identical, however. |
| 55 | +#[macro_export] |
| 56 | +macro_rules! any { |
| 57 | + ($($matcher:expr),* $(,)?) => {{ |
| 58 | + use $crate::matchers::any_matcher::internal::AnyMatcher; |
| 59 | + AnyMatcher::new([$(Box::new($matcher)),*]) |
| 60 | + }} |
| 61 | +} |
| 62 | + |
| 63 | +/// Functionality needed by the [`any`] macro. |
| 64 | +/// |
| 65 | +/// For internal use only. API stablility is not guaranteed! |
| 66 | +#[doc(hidden)] |
| 67 | +pub mod internal { |
| 68 | + use crate::matcher::{Matcher, MatcherResult}; |
| 69 | + use crate::matcher_support::description::Description; |
| 70 | + use crate::matchers::anything; |
| 71 | + use std::fmt::Debug; |
| 72 | + |
| 73 | + /// A matcher which matches an input value matched by all matchers in the |
| 74 | + /// array `components`. |
| 75 | + /// |
| 76 | + /// For internal use only. API stablility is not guaranteed! |
| 77 | + #[doc(hidden)] |
| 78 | + pub struct AnyMatcher<'a, T: Debug + ?Sized, const N: usize> { |
| 79 | + components: [Box<dyn Matcher<ActualT = T> + 'a>; N], |
| 80 | + } |
| 81 | + |
| 82 | + impl<'a, T: Debug + ?Sized, const N: usize> AnyMatcher<'a, T, N> { |
| 83 | + /// Constructs an [`AnyMatcher`] with the given component matchers. |
| 84 | + /// |
| 85 | + /// Intended for use only by the [`all`] macro. |
| 86 | + pub fn new(components: [Box<dyn Matcher<ActualT = T> + 'a>; N]) -> Self { |
| 87 | + Self { components } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + impl<'a, T: Debug + ?Sized, const N: usize> Matcher for AnyMatcher<'a, T, N> { |
| 92 | + type ActualT = T; |
| 93 | + |
| 94 | + fn matches(&self, actual: &Self::ActualT) -> MatcherResult { |
| 95 | + for component in &self.components { |
| 96 | + match component.matches(actual) { |
| 97 | + MatcherResult::NoMatch => {} |
| 98 | + MatcherResult::Match => { |
| 99 | + return MatcherResult::Match; |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + MatcherResult::NoMatch |
| 104 | + } |
| 105 | + |
| 106 | + fn explain_match(&self, actual: &Self::ActualT) -> String { |
| 107 | + match N { |
| 108 | + 0 => format!("which {}", anything::<T>().describe(MatcherResult::NoMatch)), |
| 109 | + 1 => self.components[0].explain_match(actual), |
| 110 | + _ => { |
| 111 | + let failures = self |
| 112 | + .components |
| 113 | + .iter() |
| 114 | + .filter(|component| component.matches(actual).is_no_match()) |
| 115 | + .map(|component| component.explain_match(actual)) |
| 116 | + .collect::<Description>(); |
| 117 | + if failures.len() == 1 { |
| 118 | + format!("{}", failures) |
| 119 | + } else { |
| 120 | + format!("{}", failures.bullet_list().indent_except_first_line()) |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + fn describe(&self, matcher_result: MatcherResult) -> String { |
| 127 | + match N { |
| 128 | + 0 => anything::<T>().describe(matcher_result), |
| 129 | + 1 => self.components[0].describe(matcher_result), |
| 130 | + _ => { |
| 131 | + let properties = self |
| 132 | + .components |
| 133 | + .iter() |
| 134 | + .map(|m| m.describe(matcher_result)) |
| 135 | + .collect::<Description>() |
| 136 | + .bullet_list() |
| 137 | + .indent(); |
| 138 | + format!( |
| 139 | + "{}:\n{properties}", |
| 140 | + if matcher_result.into() { |
| 141 | + "has at least one of the following properties" |
| 142 | + } else { |
| 143 | + "has none of the following properties" |
| 144 | + } |
| 145 | + ) |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +#[cfg(test)] |
| 153 | +mod tests { |
| 154 | + use super::internal; |
| 155 | + use crate::matcher::{Matcher, MatcherResult}; |
| 156 | + use crate::prelude::*; |
| 157 | + use indoc::indoc; |
| 158 | + |
| 159 | + #[test] |
| 160 | + fn description_shows_more_than_one_matcher() -> Result<()> { |
| 161 | + let first_matcher = starts_with("A"); |
| 162 | + let second_matcher = ends_with("string"); |
| 163 | + let matcher: internal::AnyMatcher<String, 2> = any!(first_matcher, second_matcher); |
| 164 | + |
| 165 | + verify_that!( |
| 166 | + matcher.describe(MatcherResult::Match), |
| 167 | + eq(indoc!( |
| 168 | + " |
| 169 | + has at least one of the following properties: |
| 170 | + * starts with prefix \"A\" |
| 171 | + * ends with suffix \"string\"" |
| 172 | + )) |
| 173 | + ) |
| 174 | + } |
| 175 | + |
| 176 | + #[test] |
| 177 | + fn description_shows_one_matcher_directly() -> Result<()> { |
| 178 | + let first_matcher = starts_with("A"); |
| 179 | + let matcher: internal::AnyMatcher<String, 1> = any!(first_matcher); |
| 180 | + |
| 181 | + verify_that!(matcher.describe(MatcherResult::Match), eq("starts with prefix \"A\"")) |
| 182 | + } |
| 183 | + |
| 184 | + #[test] |
| 185 | + fn mismatch_description_shows_which_matcher_failed_if_more_than_one_constituent() -> Result<()> |
| 186 | + { |
| 187 | + let first_matcher = starts_with("Another"); |
| 188 | + let second_matcher = ends_with("string"); |
| 189 | + let matcher: internal::AnyMatcher<str, 2> = any!(first_matcher, second_matcher); |
| 190 | + |
| 191 | + verify_that!( |
| 192 | + matcher.explain_match("A string"), |
| 193 | + displays_as(eq("which does not start with \"Another\"")) |
| 194 | + ) |
| 195 | + } |
| 196 | + |
| 197 | + #[test] |
| 198 | + fn mismatch_description_is_simple_when_only_one_consistuent() -> Result<()> { |
| 199 | + let first_matcher = starts_with("Another"); |
| 200 | + let matcher: internal::AnyMatcher<str, 1> = any!(first_matcher); |
| 201 | + |
| 202 | + verify_that!( |
| 203 | + matcher.explain_match("A string"), |
| 204 | + displays_as(eq("which does not start with \"Another\"")) |
| 205 | + ) |
| 206 | + } |
| 207 | +} |
0 commit comments