googletest::matcher

Trait Matcher

Source
pub trait Matcher<ActualT: Debug + Copy>: MatcherBase {
    // Required methods
    fn matches(&self, actual: ActualT) -> MatcherResult;
    fn describe(&self, matcher_result: MatcherResult) -> Description;

    // Provided method
    fn explain_match(&self, actual: ActualT) -> Description { ... }
}
Expand description

An interface for checking an arbitrary condition on a datum.

This trait is automatically implemented for a reference of any type implementing Matcher. This simplifies reusing a matcher in different assertions.

It is also implemented for tuple of Matcher. If MatcherT: Matcher<T> and MatcherU: Matcher<U>, then (MatcherT, MatcherU): Matcher<(T, U)>, and so on, up to 12 elements. Tuples longer than that do not automatically inherit the Debug trait from their members, so are generally not well-supported; see Rust by Example.

Required Methods§

Source

fn matches(&self, actual: ActualT) -> MatcherResult

Returns whether the condition matches the datum actual.

The trait implementation defines what it means to “match”. Often the matching condition is based on data stored in the matcher. For example, eq matches when its stored expected value is equal (in the sense of the == operator) to the value actual.

Source

fn describe(&self, matcher_result: MatcherResult) -> Description

Returns a description of self or a negative description if matcher_result is DoesNotMatch.

The function should print a verb phrase that describes the property a value matching, respectively not matching, this matcher should have. The subject of the verb phrase is the value being matched.

The output appears next to Expected in an assertion failure message. For example:

Value of: ...
Expected: is equal to 7
          ^^^^^^^^^^^^^
Actual: ...

When the matcher contains one or more inner matchers, the implementation should invoke Self::describe on the inner matchers to complete the description. It should place the inner description at a point where a verb phrase would fit. For example, the matcher some implements describe as follows:

fn describe(&self, matcher_result: MatcherResult) -> Description {
    match matcher_result {
        MatcherResult::Matches => {
            Description::new()
                .text("has a value which")
                .nested(self.inner.describe(MatcherResult::Matches))
      // Inner matcher: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        }
        MatcherResult::DoesNotMatch => {...} // Similar to the above
    }
}

The output expectation differs from that of explain_match in that it is a verb phrase (beginning with a verb like “is”) rather than a relative clause (beginning with “which” or “whose”). This difference is because the output of explain_match is always used adjectivally to describe the actual value, while describe is used in contexts where a relative clause would not make sense.

Provided Methods§

Source

fn explain_match(&self, actual: ActualT) -> Description

Prepares a String describing how the expected value encoded in this instance matches or does not match the given value actual.

This should be in the form of a relative clause, i.e. something starting with a relative pronoun such as “which” or “whose”. It will appear next to the actual value in an assertion failure. For example:

Value of: ...
Expected: ...
Actual: ["Something"], which does not contain "Something else"
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The default implementation relies on describe. Thus it does not make any use of the actual value itself, but rather only whether the value is matched.

Override the default implementation to provide additional context on why a particular value matched or did not match. For example, the container_eq matcher displays information on which elements of the actual value were not present in the expected value and vice versa.

This implementation should be overridden in any matcher which contains one or more inner matchers. The implementation should invoke explain_match on the inner matchers, so that the generated match explanation also reflects their implementation. Without this, the match explanation of the inner matchers will not be able to make use of the actual value at all.

For example, the explain_match implementation of the matcher points_to defers immediately to the inner matcher and appears as follows:

fn explain_match(&self, actual: &ActualT) -> Description {
    self.expected.explain_match(*actual)
}

The matcher can also provide some additional context before deferring to an inner matcher. In that case it should invoke explain_match on the inner matcher at a point where a relative clause would fit. For example:

fn explain_match(&self, actual: ActualT) -> Description {
    Description::new()
        .text("which points to a value")
        .nested(self.expected.explain_match(*actual))
}

Implementations on Foreign Types§

Source§

impl Matcher<&()> for ()

Source§

fn matches(&self, _: &()) -> MatcherResult

Source§

fn describe(&self, matcher_result: MatcherResult) -> Description

Source§

impl Matcher<()> for ()

Source§

fn matches(&self, _: ()) -> MatcherResult

Source§

fn describe(&self, matcher_result: MatcherResult) -> Description

Source§

impl<'a, T0: Debug, I0: Matcher<&'a T0>> Matcher<&'a (T0,)> for (I0,)

Source§

fn matches(&self, actual: &'a (T0,)) -> MatcherResult

Source§

fn explain_match(&self, actual: &'a (T0,)) -> Description

Source§

fn describe(&self, matcher_result: MatcherResult) -> Description

Source§

impl<'a, T0: Debug, I0: Matcher<&'a T0>, T1: Debug, I1: Matcher<&'a T1>> Matcher<&'a (T0, T1)> for (I0, I1)

Source§

fn matches(&self, actual: &'a (T0, T1)) -> MatcherResult

Source§

fn explain_match(&self, actual: &'a (T0, T1)) -> Description

Source§

fn describe(&self, matcher_result: MatcherResult) -> Description

Source§

impl<'a, T0: Debug, I0: Matcher<&'a T0>, T1: Debug, I1: Matcher<&'a T1>, T2: Debug, I2: Matcher<&'a T2>> Matcher<&'a (T0, T1, T2)> for (I0, I1, I2)

Source§

fn matches(&self, actual: &'a (T0, T1, T2)) -> MatcherResult

Source§

fn explain_match(&self, actual: &'a (T0, T1, T2)) -> Description

Source§

fn describe(&self, matcher_result: MatcherResult) -> Description

Source§

impl<'a, T0: Debug, I0: Matcher<&'a T0>, T1: Debug, I1: Matcher<&'a T1>, T2: Debug, I2: Matcher<&'a T2>, T3: Debug, I3: Matcher<&'a T3>> Matcher<&'a (T0, T1, T2, T3)> for (I0, I1, I2, I3)

Source§

fn matches(&self, actual: &'a (T0, T1, T2, T3)) -> MatcherResult

Source§

fn explain_match(&self, actual: &'a (T0, T1, T2, T3)) -> Description

Source§

fn describe(&self, matcher_result: MatcherResult) -> Description

Source§

impl<'a, T0: Debug, I0: Matcher<&'a T0>, T1: Debug, I1: Matcher<&'a T1>, T2: Debug, I2: Matcher<&'a T2>, T3: Debug, I3: Matcher<&'a T3>, T4: Debug, I4: Matcher<&'a T4>> Matcher<&'a (T0, T1, T2, T3, T4)> for (I0, I1, I2, I3, I4)

Source§

impl<'a, T0: Debug, I0: Matcher<&'a T0>, T1: Debug, I1: Matcher<&'a T1>, T2: Debug, I2: Matcher<&'a T2>, T3: Debug, I3: Matcher<&'a T3>, T4: Debug, I4: Matcher<&'a T4>, T5: Debug, I5: Matcher<&'a T5>> Matcher<&'a (T0, T1, T2, T3, T4, T5)> for (I0, I1, I2, I3, I4, I5)

Source§

impl<'a, T0: Debug, I0: Matcher<&'a T0>, T1: Debug, I1: Matcher<&'a T1>, T2: Debug, I2: Matcher<&'a T2>, T3: Debug, I3: Matcher<&'a T3>, T4: Debug, I4: Matcher<&'a T4>, T5: Debug, I5: Matcher<&'a T5>, T6: Debug, I6: Matcher<&'a T6>> Matcher<&'a (T0, T1, T2, T3, T4, T5, T6)> for (I0, I1, I2, I3, I4, I5, I6)

Source§

impl<'a, T0: Debug, I0: Matcher<&'a T0>, T1: Debug, I1: Matcher<&'a T1>, T2: Debug, I2: Matcher<&'a T2>, T3: Debug, I3: Matcher<&'a T3>, T4: Debug, I4: Matcher<&'a T4>, T5: Debug, I5: Matcher<&'a T5>, T6: Debug, I6: Matcher<&'a T6>, T7: Debug, I7: Matcher<&'a T7>> Matcher<&'a (T0, T1, T2, T3, T4, T5, T6, T7)> for (I0, I1, I2, I3, I4, I5, I6, I7)

Source§

impl<'a, T0: Debug, I0: Matcher<&'a T0>, T1: Debug, I1: Matcher<&'a T1>, T2: Debug, I2: Matcher<&'a T2>, T3: Debug, I3: Matcher<&'a T3>, T4: Debug, I4: Matcher<&'a T4>, T5: Debug, I5: Matcher<&'a T5>, T6: Debug, I6: Matcher<&'a T6>, T7: Debug, I7: Matcher<&'a T7>, T8: Debug, I8: Matcher<&'a T8>> Matcher<&'a (T0, T1, T2, T3, T4, T5, T6, T7, T8)> for (I0, I1, I2, I3, I4, I5, I6, I7, I8)

Source§

impl<'a, T0: Debug, I0: Matcher<&'a T0>, T1: Debug, I1: Matcher<&'a T1>, T2: Debug, I2: Matcher<&'a T2>, T3: Debug, I3: Matcher<&'a T3>, T4: Debug, I4: Matcher<&'a T4>, T5: Debug, I5: Matcher<&'a T5>, T6: Debug, I6: Matcher<&'a T6>, T7: Debug, I7: Matcher<&'a T7>, T8: Debug, I8: Matcher<&'a T8>, T9: Debug, I9: Matcher<&'a T9>> Matcher<&'a (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)> for (I0, I1, I2, I3, I4, I5, I6, I7, I8, I9)

Source§

impl<'a, T0: Debug, I0: Matcher<&'a T0>, T1: Debug, I1: Matcher<&'a T1>, T2: Debug, I2: Matcher<&'a T2>, T3: Debug, I3: Matcher<&'a T3>, T4: Debug, I4: Matcher<&'a T4>, T5: Debug, I5: Matcher<&'a T5>, T6: Debug, I6: Matcher<&'a T6>, T7: Debug, I7: Matcher<&'a T7>, T8: Debug, I8: Matcher<&'a T8>, T9: Debug, I9: Matcher<&'a T9>, T10: Debug, I10: Matcher<&'a T10>> Matcher<&'a (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)> for (I0, I1, I2, I3, I4, I5, I6, I7, I8, I9, I10)

Source§

impl<'a, T0: Debug, I0: Matcher<&'a T0>, T1: Debug, I1: Matcher<&'a T1>, T2: Debug, I2: Matcher<&'a T2>, T3: Debug, I3: Matcher<&'a T3>, T4: Debug, I4: Matcher<&'a T4>, T5: Debug, I5: Matcher<&'a T5>, T6: Debug, I6: Matcher<&'a T6>, T7: Debug, I7: Matcher<&'a T7>, T8: Debug, I8: Matcher<&'a T8>, T9: Debug, I9: Matcher<&'a T9>, T10: Debug, I10: Matcher<&'a T10>, T11: Debug, I11: Matcher<&'a T11>> Matcher<&'a (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)> for (I0, I1, I2, I3, I4, I5, I6, I7, I8, I9, I10, I11)

Source§

impl<T0: Debug + Copy, I0: Matcher<T0>> Matcher<(T0,)> for (I0,)

Source§

fn matches(&self, actual: (T0,)) -> MatcherResult

Source§

fn explain_match(&self, actual: (T0,)) -> Description

Source§

fn describe(&self, matcher_result: MatcherResult) -> Description

Source§

impl<T0: Debug + Copy, I0: Matcher<T0>, T1: Debug + Copy, I1: Matcher<T1>> Matcher<(T0, T1)> for (I0, I1)

Source§

fn matches(&self, actual: (T0, T1)) -> MatcherResult

Source§

fn explain_match(&self, actual: (T0, T1)) -> Description

Source§

fn describe(&self, matcher_result: MatcherResult) -> Description

Source§

impl<T0: Debug + Copy, I0: Matcher<T0>, T1: Debug + Copy, I1: Matcher<T1>, T2: Debug + Copy, I2: Matcher<T2>> Matcher<(T0, T1, T2)> for (I0, I1, I2)

Source§

impl<T0: Debug + Copy, I0: Matcher<T0>, T1: Debug + Copy, I1: Matcher<T1>, T2: Debug + Copy, I2: Matcher<T2>, T3: Debug + Copy, I3: Matcher<T3>> Matcher<(T0, T1, T2, T3)> for (I0, I1, I2, I3)

Source§

impl<T0: Debug + Copy, I0: Matcher<T0>, T1: Debug + Copy, I1: Matcher<T1>, T2: Debug + Copy, I2: Matcher<T2>, T3: Debug + Copy, I3: Matcher<T3>, T4: Debug + Copy, I4: Matcher<T4>> Matcher<(T0, T1, T2, T3, T4)> for (I0, I1, I2, I3, I4)

Source§

impl<T0: Debug + Copy, I0: Matcher<T0>, T1: Debug + Copy, I1: Matcher<T1>, T2: Debug + Copy, I2: Matcher<T2>, T3: Debug + Copy, I3: Matcher<T3>, T4: Debug + Copy, I4: Matcher<T4>, T5: Debug + Copy, I5: Matcher<T5>> Matcher<(T0, T1, T2, T3, T4, T5)> for (I0, I1, I2, I3, I4, I5)

Source§

impl<T0: Debug + Copy, I0: Matcher<T0>, T1: Debug + Copy, I1: Matcher<T1>, T2: Debug + Copy, I2: Matcher<T2>, T3: Debug + Copy, I3: Matcher<T3>, T4: Debug + Copy, I4: Matcher<T4>, T5: Debug + Copy, I5: Matcher<T5>, T6: Debug + Copy, I6: Matcher<T6>> Matcher<(T0, T1, T2, T3, T4, T5, T6)> for (I0, I1, I2, I3, I4, I5, I6)

Source§

impl<T0: Debug + Copy, I0: Matcher<T0>, T1: Debug + Copy, I1: Matcher<T1>, T2: Debug + Copy, I2: Matcher<T2>, T3: Debug + Copy, I3: Matcher<T3>, T4: Debug + Copy, I4: Matcher<T4>, T5: Debug + Copy, I5: Matcher<T5>, T6: Debug + Copy, I6: Matcher<T6>, T7: Debug + Copy, I7: Matcher<T7>> Matcher<(T0, T1, T2, T3, T4, T5, T6, T7)> for (I0, I1, I2, I3, I4, I5, I6, I7)

Source§

impl<T0: Debug + Copy, I0: Matcher<T0>, T1: Debug + Copy, I1: Matcher<T1>, T2: Debug + Copy, I2: Matcher<T2>, T3: Debug + Copy, I3: Matcher<T3>, T4: Debug + Copy, I4: Matcher<T4>, T5: Debug + Copy, I5: Matcher<T5>, T6: Debug + Copy, I6: Matcher<T6>, T7: Debug + Copy, I7: Matcher<T7>, T8: Debug + Copy, I8: Matcher<T8>> Matcher<(T0, T1, T2, T3, T4, T5, T6, T7, T8)> for (I0, I1, I2, I3, I4, I5, I6, I7, I8)

Source§

impl<T0: Debug + Copy, I0: Matcher<T0>, T1: Debug + Copy, I1: Matcher<T1>, T2: Debug + Copy, I2: Matcher<T2>, T3: Debug + Copy, I3: Matcher<T3>, T4: Debug + Copy, I4: Matcher<T4>, T5: Debug + Copy, I5: Matcher<T5>, T6: Debug + Copy, I6: Matcher<T6>, T7: Debug + Copy, I7: Matcher<T7>, T8: Debug + Copy, I8: Matcher<T8>, T9: Debug + Copy, I9: Matcher<T9>> Matcher<(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)> for (I0, I1, I2, I3, I4, I5, I6, I7, I8, I9)

Source§

impl<T0: Debug + Copy, I0: Matcher<T0>, T1: Debug + Copy, I1: Matcher<T1>, T2: Debug + Copy, I2: Matcher<T2>, T3: Debug + Copy, I3: Matcher<T3>, T4: Debug + Copy, I4: Matcher<T4>, T5: Debug + Copy, I5: Matcher<T5>, T6: Debug + Copy, I6: Matcher<T6>, T7: Debug + Copy, I7: Matcher<T7>, T8: Debug + Copy, I8: Matcher<T8>, T9: Debug + Copy, I9: Matcher<T9>, T10: Debug + Copy, I10: Matcher<T10>> Matcher<(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)> for (I0, I1, I2, I3, I4, I5, I6, I7, I8, I9, I10)

Source§

impl<T0: Debug + Copy, I0: Matcher<T0>, T1: Debug + Copy, I1: Matcher<T1>, T2: Debug + Copy, I2: Matcher<T2>, T3: Debug + Copy, I3: Matcher<T3>, T4: Debug + Copy, I4: Matcher<T4>, T5: Debug + Copy, I5: Matcher<T5>, T6: Debug + Copy, I6: Matcher<T6>, T7: Debug + Copy, I7: Matcher<T7>, T8: Debug + Copy, I8: Matcher<T8>, T9: Debug + Copy, I9: Matcher<T9>, T10: Debug + Copy, I10: Matcher<T10>, T11: Debug + Copy, I11: Matcher<T11>> Matcher<(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)> for (I0, I1, I2, I3, I4, I5, I6, I7, I8, I9, I10, I11)

Source§

impl<T: Debug + Copy, M: Matcher<T>> Matcher<T> for &M

Source§

fn matches(&self, actual: T) -> MatcherResult

Source§

fn describe(&self, matcher_result: MatcherResult) -> Description

Source§

fn explain_match(&self, actual: T) -> Description

Implementors§

Source§

impl<ExpectedT, ActualT> Matcher<ActualT> for StrMatcher<ExpectedT>
where ExpectedT: Deref<Target = str> + Debug, ActualT: AsRef<str> + Debug + Copy,

Source§

impl<T: Debug + Copy, InnerMatcherT: Matcher<T>, ContainerT> Matcher<ContainerT> for ContainsMatcher<InnerMatcherT>
where ContainerT: IntoIterator<Item = T> + Debug + Copy,

Source§

impl<T: Debug + Copy, P, D1: PredicateDescription, D2: PredicateDescription> Matcher<T> for PredicateMatcher<P, D1, D2>
where P: Fn(T) -> bool,

Source§

impl<T: Debug + Float + Copy> Matcher<T> for NearMatcher<T>

Source§

impl<T: Debug, A: Debug + Copy + PartialEq<T>> Matcher<A> for EqMatcher<T>