Skip to main content

test_that/
matcher.rs

1// Copyright 2022 Google LLC
2// Copyright 2026 Bradford Hovinen <bradford@hovinen.me>
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//      http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! The components required to implement matchers.
17
18use crate::{
19    description::Description,
20    internal::{source_location::SourceLocation, test_outcome::TestAssertionFailure},
21    matchers::__internal::{ConjunctionMatcher, DisjunctionMatcher},
22};
23use core::fmt::Debug;
24
25/// Represents an arbitrary condition on data of the given type which can be
26/// checked to perform an assertion.
27///
28/// Matchers are the core of the assertion language of Test That!. They can be
29/// combined and composed to assert on complex data structures. The variety
30/// of available matchers allows precise specification of the intent of the
31/// assertion.
32///
33/// Matchers can be logically combined with the [`and`] and [`or`] methods as
34/// well as the [`not`] matcher.
35///
36/// This trait is implemented for tuples of up to twelve arbitrary
37/// implementations of `Matcher`. So one can match tuples of up to twelve
38/// items using corresponding tuple of matchers.
39///
40/// ```rust
41/// # use test_that::prelude::*;
42/// let value = (1, "Hello, world");
43/// assert_that!(value, (eq(1), ends_with("world")));
44/// ```
45///
46/// Tuples of more than twelve items do not automatically inherit the `Debug`
47/// trait from their members, so are generally not supported; see
48/// [Rust by Example](https://doc.rust-lang.org/rust-by-example/primitives/tuples.html#tuples).
49///
50/// [`and`]: crate::matcher::MatcherExt::and
51/// [`or`]: crate::matcher::MatcherExt::or
52/// [`not`]: crate::matchers::not
53pub trait Matcher<ActualT: Debug + ?Sized>: Describable {
54    /// Returns whether the condition matches the datum `actual`.
55    ///
56    /// The trait implementation defines what it means to "match". Often the
57    /// matching condition is based on data stored in the matcher. For example,
58    /// `eq` matches when its stored expected value is equal (in the sense of
59    /// the `==` operator) to the value `actual`.
60    fn matches(&self, actual: &ActualT) -> MatcherResult;
61
62    /// Prepares a [`String`] describing how the expected value
63    /// encoded in this instance matches or does not match the given value
64    /// `actual`.
65    ///
66    /// This should be in the form of a relative clause, i.e. something starting
67    /// with a relative pronoun such as "which" or "whose". It will appear next
68    /// to the actual value in an assertion failure. For example:
69    ///
70    /// ```text
71    /// Value of: ...
72    /// Expected: ...
73    /// Actual: ["Something"], which does not contain "Something else"
74    ///                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
75    /// ```
76    ///
77    /// The default implementation relies on
78    /// [`describe`][Describable::describe]. Thus it does not make any use
79    /// of the actual value itself, but rather only whether the value is
80    /// matched.
81    ///
82    /// Override the default implementation to provide additional context on why
83    /// a particular value matched or did not match. For example, the
84    /// [`container_eq`][crate::matchers::containers::container_eq] matcher
85    /// displays information on which elements of the actual value were not
86    /// present in the expected value and vice versa.
87    ///
88    /// This implementation should be overridden in any matcher which contains
89    /// one or more inner matchers. The implementation should invoke
90    /// `explain_match` on the inner matchers, so that the generated match
91    /// explanation also reflects their implementation. Without this, the match
92    /// explanation of the inner matchers will not be able to make use of the
93    /// actual value at all.
94    ///
95    /// For example, the `explain_match` implementation of the matcher
96    /// [`points_to`][crate::matchers::points_to] defers immediately to the
97    /// inner matcher and appears as follows:
98    ///
99    /// ```ignore
100    /// fn explain_match(&self, actual: &Self::ActualT) -> Description {
101    ///     self.expected.explain_match(actual.deref())
102    /// }
103    /// ```
104    ///
105    /// The matcher can also provide some additional context before deferring to
106    /// an inner matcher. In that case it should invoke `explain_match` on the
107    /// inner matcher at a point where a relative clause would fit. For example:
108    ///
109    /// ```ignore
110    /// fn explain_match(&self, actual: &Self::ActualT) -> Description {
111    ///     Description::new()
112    ///         .text("which points to a value")
113    ///         .nested(self.expected.explain_match(actual.deref()))
114    /// }
115    /// ```
116    ///
117    /// [`String`]: alloc::string::String
118    fn explain_match(&self, actual: &ActualT) -> Description {
119        format!("which {}", self.describe(self.matches(actual))).into()
120    }
121}
122
123/// Extension methods for composing matchers.
124///
125/// This trait is implemented for all [`Sized`] types, but the resulting
126/// combinators are only useful when the underlying types implement [`Matcher`].
127//
128// This is kept separate from [`Matcher`] so that the type parameter `ActualT`
129// does not need to be known at the `.and()` / `.or()` call site. Type
130// inference determines `ActualT` later, when the combined matcher is applied
131// to an actual value.
132pub trait MatcherExt: Sized {
133    /// Constructs a matcher that matches both `self` and `right`.
134    ///
135    /// ```
136    /// # use test_that::prelude::*;
137    /// # fn should_pass() -> TestResult<()> {
138    /// verify_that!("A string", starts_with("A").and(ends_with("string")))?; // Passes
139    /// #     Ok(())
140    /// # }
141    /// # fn should_fail_1() -> TestResult<()> {
142    /// verify_that!("A string", starts_with("Another").and(ends_with("string")))?; // Fails
143    /// #     Ok(())
144    /// # }
145    /// # fn should_fail_2() -> TestResult<()> {
146    /// verify_that!("A string", starts_with("A").and(ends_with("non-string")))?; // Fails
147    /// #     Ok(())
148    /// # }
149    /// # should_pass().unwrap();
150    /// # should_fail_1().unwrap_err();
151    /// # should_fail_2().unwrap_err();
152    /// ```
153    // TODO(b/264518763): Replace the return type with impl Matcher and reduce
154    // visibility of ConjunctionMatcher once impl in return position in trait
155    // methods is stable.
156    fn and<Right>(self, right: Right) -> ConjunctionMatcher<Self, Right> {
157        ConjunctionMatcher::new(self, right)
158    }
159
160    /// Constructs a matcher that matches when at least one of `self` or `right`
161    /// matches the input.
162    ///
163    /// ```
164    /// # use test_that::prelude::*;
165    /// # fn should_pass() -> TestResult<()> {
166    /// verify_that!(10, eq(2).or(ge(5)))?;  // Passes
167    /// verify_that!(10, eq(2).or(eq(5)).or(ge(9)))?;  // Passes
168    /// #     Ok(())
169    /// # }
170    /// # fn should_fail() -> TestResult<()> {
171    /// verify_that!(10, eq(2).or(ge(15)))?; // Fails
172    /// #     Ok(())
173    /// # }
174    /// # should_pass().unwrap();
175    /// # should_fail().unwrap_err();
176    /// ```
177    // TODO(b/264518763): Replace the return type with impl Matcher and reduce
178    // visibility of DisjunctionMatcher once impl in return position in trait
179    // methods is stable.
180    fn or<Right>(self, right: Right) -> DisjunctionMatcher<Self, Right> {
181        DisjunctionMatcher::new(self, right)
182    }
183}
184
185impl<M: Sized> MatcherExt for M {}
186
187/// An item, normally a [Matcher] with positive and negative valences which can
188/// be turned into a [Description] for human consumption.
189pub trait Describable {
190    /// Returns a description of `self` or a negative description if
191    /// `matcher_result` is `DoesNotMatch`.
192    ///
193    /// The function should print a verb phrase that describes the property a
194    /// value matching, respectively not matching, this matcher should have.
195    /// The subject of the verb phrase is the value being matched.
196    ///
197    /// The output appears next to `Expected` in an assertion failure message.
198    /// For example:
199    ///
200    /// ```text
201    /// Value of: ...
202    /// Expected: is equal to 7
203    ///           ^^^^^^^^^^^^^
204    /// Actual: ...
205    /// ```
206    ///
207    /// When the matcher contains one or more inner matchers, the implementation
208    /// should invoke [`Self::describe`] on the inner matchers to complete the
209    /// description. It should place the inner description at a point where a
210    /// verb phrase would fit. For example, the matcher
211    /// [`some`][crate::matchers::some] implements `describe` as follows:
212    ///
213    /// ```ignore
214    /// fn describe(&self, matcher_result: MatcherResult) -> Description {
215    ///     match matcher_result {
216    ///         MatcherResult::Matches => {
217    ///             Description::new()
218    ///                 .text("has a value which")
219    ///                 .nested(self.inner.describe(MatcherResult::Matches))
220    ///       // Inner matcher: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
221    ///         }
222    ///         MatcherResult::DoesNotMatch => {...} // Similar to the above
223    ///     }
224    /// }
225    /// ```
226    ///
227    /// The output expectation differs from that of
228    /// [`explain_match`][Matcher::explain_match] in that it is a verb phrase
229    /// (beginning with a verb like "is") rather than a relative clause
230    /// (beginning with "which" or "whose"). This difference is because the
231    /// output of `explain_match` is always used adjectivally to describe the
232    /// actual value, while `describe` is used in contexts where a relative
233    /// clause would not make sense.
234    fn describe(&self, matcher_result: MatcherResult) -> Description;
235}
236
237/// Any actual value whose debug length is greater than this value will be
238/// pretty-printed. Otherwise, it will have normal debug output formatting.
239const PRETTY_PRINT_LENGTH_THRESHOLD: usize = 60;
240
241/// Constructs a [`TestAssertionFailure`] reporting that the given `matcher`
242/// does not match the value `actual`.
243///
244/// The parameter `actual_expr` contains the expression which was evaluated to
245/// obtain `actual`.
246pub(crate) fn create_assertion_failure<T: Debug + ?Sized>(
247    matcher: &impl Matcher<T>,
248    actual: &T,
249    actual_expr: &'static str,
250    source_location: SourceLocation,
251) -> TestAssertionFailure {
252    let actual_formatted = format!("{actual:?}");
253    let actual_formatted = if actual_formatted.len() > PRETTY_PRINT_LENGTH_THRESHOLD {
254        format!("{actual:#?}")
255    } else {
256        actual_formatted
257    };
258    TestAssertionFailure::create(format!(
259        "\
260Value of: {actual_expr}
261Expected: {}
262Actual: {actual_formatted},
263{}
264{source_location}",
265        matcher.describe(MatcherResult::Match),
266        matcher.explain_match(actual).indent(),
267    ))
268}
269
270/// The result of applying a [`Matcher`] on an actual value.
271#[derive(Debug, PartialEq, Clone, Copy)]
272pub enum MatcherResult {
273    /// The actual value matches according to the [`Matcher`] definition.
274    Match,
275    /// The actual value does not match according to the [`Matcher`] definition.
276    NoMatch,
277}
278
279impl From<bool> for MatcherResult {
280    fn from(b: bool) -> Self {
281        if b { MatcherResult::Match } else { MatcherResult::NoMatch }
282    }
283}
284
285impl From<MatcherResult> for bool {
286    fn from(matcher_result: MatcherResult) -> Self {
287        matcher_result.is_match()
288    }
289}
290
291impl MatcherResult {
292    /// Returns `true` if `self` is [`MatcherResult::Match`], otherwise
293    /// `false`.
294    pub fn is_match(self) -> bool {
295        matches!(self, MatcherResult::Match)
296    }
297
298    /// Returns `true` if `self` is [`MatcherResult::NoMatch`], otherwise
299    /// `false`.
300    pub fn is_no_match(self) -> bool {
301        matches!(self, MatcherResult::NoMatch)
302    }
303}