Lines Matching defs:matcher

42 // defines a matcher with the given name that executes the statements,
47 // The description string documents what the matcher does, and is used
52 // case we'll use the sequence of words in the matcher name as the
76 // matcher name IsEven.
82 // determined by the context in which you use the matcher and is
84 // declaring it (nor can you). This allows the matcher to be
94 // Sometimes you'll want to parameterize the matcher. For that you
113 // Note that both the matcher description and its parameter are
116 // In the matcher definition body, you can write 'foo_type' to
128 // expression can reference all of the matcher's parameters and a
130 // false, the expression should evaluate to the matcher's description;
132 // the matcher. For example,
152 // contain the sequence of words in the matcher name followed by the
184 // 'arg_type' as that's determined by the context in which the matcher
189 // While you can instantiate a matcher template with reference types,
193 // matcher you will see the value of the referenced object but not its
199 // Sometimes the matcher description alone isn't enough to explain why
226 // When defining a new matcher, you should also consider implementing
230 // the matcher parameters, which may leads to better compiler error
231 // messages when the matcher is used wrong. They also allow
291 // To implement a matcher Foo for type T, define:
333 // polymorphic matcher (i.e. something that can be converted to a
340 // M can be a polymorphic matcher, in which case we want to use
345 // polymorphic matcher because it'll be ambiguous if T has an implicit
364 // M is a polymorphic matcher or Matcher<T> has an implicit constructor
366 // matcher.
375 // matcher. It's a value of a type implicitly convertible to T. Use direct
376 // initialization to create a matcher.
384 // polymorphic matcher Eq(value) in this case.
387 // only fall back to the polymorphic Eq() matcher afterwards because the
414 // We delegate the matching logic to the source matcher.
454 // a matcher to its own type.
458 static Matcher<T> Cast(const Matcher<T>& matcher) { return matcher; }
507 // In order to be safe and clear, casting between different matcher
509 // matcher m and returns a Matcher<T>. It compiles only when T can be
512 inline Matcher<T> MatcherCast(const M& matcher) {
513 return internal::MatcherCastImpl<T, M>::Cast(matcher);
533 inline Matcher<T> SafeMatcherCast(const Matcher<U>& matcher) {
551 return MatcherCast<T>(matcher);
554 // A<T>() returns a matcher that matches any value of type T.
580 // Matches the value against the given matcher, prints the value and explains
586 bool MatchPrintAndExplain(Value& value, const Matcher<T>& matcher,
591 return matcher.Matches(value);
595 const bool match = matcher.MatchAndExplain(value, &inner_listener);
636 typename std::tuple_element<N - 1, MatcherTuple>::type matcher =
641 if (!matcher.MatchAndExplain(value, &listener)) {
648 // matcher's MatchAndExplain() method handles the case when
685 "matcher and value have different numbers of fields");
739 // Implements _, a matcher that matches any value of any
740 // type. This is a polymorphic matcher, so we need a template type
760 // Implements the polymorphic IsNull() matcher, which matches any raw or smart
774 // Implements the polymorphic NotNull() matcher, which matches any raw or smart
789 // 'variable'. This matcher is polymorphic as it can match any
813 // compiler to catch using Ref(const_value) as a matcher for a
821 // this catches using Ref(const_value) as a matcher for a
960 // Implements the polymorphic HasSubstr(substring) matcher, which
999 // Describes what this matcher matches.
1014 // Implements the polymorphic StartsWith(substring) matcher, which
1068 // Implements the polymorphic EndsWith(substring) matcher, which
1122 // Implements the polymorphic WhenBase64Unescaped(matcher) matcher, which can be
1161 // Implements a matcher that compares the two fields of a 2-tuple
1165 // The matcher defined here is polymorphic (for example, Eq() can be
1227 // Implements the Not(...) matcher for a particular argument type T.
1234 explicit NotMatcherImpl(const Matcher<T>& matcher) : matcher_(matcher) {}
1253 // Implements the Not(m) matcher, which matches a value that doesn't
1254 // match matcher m.
1258 explicit NotMatcher(InnerMatcher matcher) : matcher_(matcher) {}
1271 // Implements the AllOf(m1, m2) matcher for a particular argument type
1341 static_assert(sizeof...(Args) > 0, "Must have at least one matcher.");
1376 // Implements the AnyOf(m1, m2) matcher for a particular argument type
1467 // Constructs the matcher from a sequence of element values or
1476 for (const auto& matcher : matchers_) {
1477 matchers.push_back(MatcherCast<RawU>(matcher));
1493 // matcher.
1499 // This method template allows Truly(pred) to be used as a matcher
1529 // Used for implementing Matches(matcher), which turns a matcher into
1534 explicit MatcherAsPredicate(M matcher) : matcher_(matcher) {}
1537 // predicate on type T where m is a matcher on type T.
1540 // some matcher may be interested in its address (e.g. as in
1546 // allows us to write Matches(m) where m is a polymorphic matcher
1566 // argument M must be a type that can be converted to a matcher.
1587 // potentially unsafe downcasting of the matcher argument.
1588 const Matcher<const T&> matcher = SafeMatcherCast<const T&>(matcher_);
1590 // The expected path here is that the matcher should match (i.e. that most
1592 if (matcher.Matches(x)) {
1599 matcher.DescribeTo(&ss);
1601 // Rerun the matcher to "PrintAndExplain" the failure.
1603 if (MatchPrintAndExplain(x, matcher, &listener)) {
1604 ss << "\n The matcher failed on the initial attempt; but passed when "
1615 // A helper function for converting a matcher to a predicate-formatter
1618 // Implementation detail: 'matcher' is received by-value to force decaying.
1621 M matcher) {
1622 return PredicateFormatterFromMatcher<M>(std::move(matcher));
1625 // Implements the polymorphic IsNan() matcher, which matches any floating type
1639 // Implements the polymorphic floating point equality matcher, which matches
1647 // The matcher's input will be compared with expected. The matcher treats two
1667 // Implements floating point equality matcher as a Matcher<T>.
1861 // Implements the Pointee(m) matcher for matching a pointer whose
1862 // pointee matches matcher m. The pointer can be either raw or smart.
1866 explicit PointeeMatcher(const InnerMatcher& matcher) : matcher_(matcher) {}
1869 // used as a matcher for any pointer type whose pointee type is
1870 // compatible with the inner matcher, where type Pointer can be
1890 explicit Impl(const InnerMatcher& matcher)
1891 : matcher_(MatcherCast<const Pointee&>(matcher)) {}
1918 // Implements the Pointer(m) matcher
1919 // Implements the Pointer(m) matcher for matching a pointer that matches matcher
1925 explicit PointerMatcher(const InnerMatcher& matcher) : matcher_(matcher) {}
1928 // used as a matcher for any pointer type whose pointer type is
1929 // compatible with the inner matcher, where type PointerType can be
1949 explicit Impl(const InnerMatcher& matcher)
1950 : matcher_(MatcherCast<Pointer>(matcher)) {}
1977 // Implements the WhenDynamicCastTo<T>(m) matcher that matches a pointer or
1979 // The result of dynamic_cast<To> is forwarded to the inner matcher.
1980 // If To is a pointer and the cast fails, the inner matcher will receive NULL.
1981 // If To is a reference and the cast fails, this matcher returns false
1986 explicit WhenDynamicCastToMatcherBase(const Matcher<To>& matcher)
1987 : matcher_(matcher) {}
2015 explicit WhenDynamicCastToMatcher(const Matcher<To>& matcher)
2016 : WhenDynamicCastToMatcherBase<To>(matcher) {}
2030 explicit WhenDynamicCastToMatcher(const Matcher<To&>& matcher)
2031 : WhenDynamicCastToMatcherBase<To&>(matcher) {}
2046 // Implements the Field() matcher for matching a field (i.e. member
2052 const Matcher<const FieldType&>& matcher)
2053 : field_(field), matcher_(matcher), whose_field_("whose given field ") {}
2056 const Matcher<const FieldType&>& matcher)
2058 matcher_(matcher),
2107 // Implements the Property() matcher for matching a property
2117 PropertyMatcher(Property property, const Matcher<RefToConstProperty>& matcher)
2119 matcher_(matcher),
2123 const Matcher<RefToConstProperty>& matcher)
2125 matcher_(matcher),
2205 // Implements the ResultOf() matcher for matching a return value of a
2210 ResultOfMatcher(Callable callable, InnerMatcher matcher)
2212 std::move(matcher)) {}
2215 InnerMatcher matcher)
2218 matcher_(std::move(matcher)) {
2239 const CallableStorageType& callable, const M& matcher)
2242 matcher_(MatcherCast<ResultType>(matcher)) {}
2293 // Implements a matcher that checks the size of an STL-style container.
2340 // Implements a matcher that checks the begin()..end() distance of an STL-style
2396 // Implements an equality matcher for any STL-style container whose elements
2397 // support ==. This matcher is like Eq(), but its failure explanations provide
2419 // after this matcher is created.
2497 const ContainerMatcher& matcher)
2498 : comparator_(comparator), matcher_(matcher) {}
2519 Impl(const Comparator& comparator, const ContainerMatcher& matcher)
2520 : comparator_(comparator), matcher_(matcher) {}
2591 // it are modified after this matcher is created.
2614 // We pass the LHS value and the RHS value to the inner matcher by
2621 // mono_tuple_matcher_ holds a monomorphic version of the tuple matcher.
2789 // Describes what this matcher does.
2815 // Describes what this matcher does.
2960 // matcher.
2973 // Describes what this matcher does.
2979 // Describes what the negation of this matcher does.
3022 explicit Impl(const InnerMatcher& matcher)
3023 : matcher_(MatcherCast<Address>(matcher)) {}
3064 // Describes what this matcher does.
3072 // Describes what the negation of this matcher does.
3372 // Constructs the matcher from a sequence of element values or
3381 // Describes what this matcher does.
3400 // Describes what the negation of this matcher does.
3435 bool match; // Does the current element match the current matcher?
3554 // The matching is represented as a vector of {element, matcher} pairs.
3574 // A vector of matcher describers, one for each element matcher.
3579 // Describes this UnorderedElementsAre matcher.
3582 // Describes the negation of this UnorderedElementsAre matcher.
3630 // Describes what this matcher does.
3635 // Describes what the negation of this matcher does.
3792 // Given a 2-tuple matcher tm of type Tuple2Matcher and a value second
3794 // second) is a polymorphic matcher that matches a value x if and only if
3857 // Given a 2-tuple matcher tm and a value second,
3858 // MatcherBindSecond(tm, second) returns a matcher that matches a
3867 // Returns the description for a matcher defined using the MATCHER*()
3870 // negation of the matcher. 'param_values' contains a list of strings
3871 // that are the print-out of the matcher's parameters.
3876 // Implements a matcher that checks the value of a optional<> type variable.
3936 // Implements a matcher that checks the value of a variant<> type variable.
3940 explicit VariantMatcher(::testing::Matcher<const T&> matcher)
3941 : matcher_(std::move(matcher)) {}
3997 // Implements a matcher that any_casts the value.
4001 explicit AnyCastMatcher(const ::testing::Matcher<const T&>& matcher)
4002 : matcher_(matcher) {}
4052 // Implements the Args() matcher.
4145 // All forms of ElementsAreArray() make a copy of the input matcher sequence.
4226 // _ is a matcher that matches anything of any type.
4236 // Creates a matcher that matches any value of the given type T.
4242 // Creates a matcher that matches any value of the given type T.
4255 // Creates a polymorphic matcher that matches any NULL pointer.
4260 // Creates a polymorphic matcher that matches any non-NULL pointer.
4267 // Creates a polymorphic matcher that matches any argument that
4274 // Creates a polymorphic matcher that matches any NaN floating point.
4279 // Creates a matcher that matches any double argument approximately
4285 // Creates a matcher that matches any double argument approximately
4291 // Creates a matcher that matches any double argument approximately equal to
4299 // Creates a matcher that matches any double argument approximately equal to
4307 // Creates a matcher that matches any float argument approximately
4313 // Creates a matcher that matches any float argument approximately
4319 // Creates a matcher that matches any float argument approximately equal to
4327 // Creates a matcher that matches any float argument approximately equal to
4335 // Creates a matcher that matches a pointer (raw or smart) that points
4344 // Creates a matcher that matches a pointer or reference that matches
4346 // The result of dynamic_cast<To> is forwarded to the inner matcher.
4347 // If To is a pointer and the cast fails, the inner matcher will receive NULL.
4348 // If To is a reference and the cast fails, this matcher returns false
4358 // Creates a matcher that matches an object whose given field matches
4359 // 'matcher'. For example,
4364 FieldType Class::*field, const FieldMatcher& matcher) {
4366 field, MatcherCast<const FieldType&>(matcher)));
4370 // to compile where bar is an int32 and m is a matcher for int64.
4378 const FieldMatcher& matcher) {
4380 field_name, field, MatcherCast<const FieldType&>(matcher)));
4383 // Creates a matcher that matches an object whose given property
4384 // matches 'matcher'. For example,
4391 const PropertyMatcher& matcher) {
4395 property, MatcherCast<const PropertyType&>(matcher)));
4399 // to compile where bar() returns an int32 and m is a matcher for int64.
4409 const PropertyMatcher& matcher) {
4413 property_name, property, MatcherCast<const PropertyType&>(matcher)));
4421 const PropertyMatcher& matcher) {
4425 property, MatcherCast<const PropertyType&>(matcher)));
4434 const PropertyMatcher& matcher) {
4438 property_name, property, MatcherCast<const PropertyType&>(matcher)));
4441 // Creates a matcher that matches an object if and only if the result of
4442 // applying a callable to x matches 'matcher'. For example,
4451 Callable callable, InnerMatcher matcher) {
4453 std::move(matcher));
4461 InnerMatcher matcher) {
4463 result_description, std::move(callable), std::move(matcher));
4500 // Creates a matcher that matches any string, std::string, or C string
4556 // Creates a matcher that matches any ::wstring, std::wstring, or C wide string
4580 // Creates a polymorphic matcher that matches a 2-tuple where the
4584 // Creates a polymorphic matcher that matches a 2-tuple where the
4588 // Creates a polymorphic matcher that matches a 2-tuple where the
4592 // Creates a polymorphic matcher that matches a 2-tuple where the
4596 // Creates a polymorphic matcher that matches a 2-tuple where the
4600 // Creates a polymorphic matcher that matches a 2-tuple where the
4604 // Creates a polymorphic matcher that matches a 2-tuple where
4610 // Creates a polymorphic matcher that matches a 2-tuple where
4616 // Creates a polymorphic matcher that matches a 2-tuple where
4622 // Creates a polymorphic matcher that matches a 2-tuple where
4628 // Creates a polymorphic matcher that matches a 2-tuple where
4634 // Creates a polymorphic matcher that matches a 2-tuple where
4640 // Creates a polymorphic matcher that matches a 2-tuple where
4648 // Creates a polymorphic matcher that matches a 2-tuple where
4656 // Creates a matcher that matches any value of type T that m doesn't
4663 // Returns a matcher that matches anything that satisfies the given
4672 // Returns a matcher that matches the container size. The container must
4675 // matcher. For instance:
4684 // Returns a matcher that matches the distance between the container's begin()
4685 // iterator and its end() iterator, i.e. the size of the container. This matcher
4695 // Returns a matcher that matches an equal container.
4696 // This matcher behaves like Eq(), but in the event of mismatch lists the
4706 // Returns a matcher that matches a container that, when sorted using
4715 // Returns a matcher that matches a container that, when sorted using
4727 // i-th element (as a pair) satisfy the given pair matcher, for all i.
4750 // pair matcher, for all i. Tuple2Matcher must be able to be safely
4773 // Create a matcher for each element in rhs_container.
4794 // least one element matching the given value or matcher.
4812 // The matcher supports a modifier `Times` that allows to check for arbitrary
4825 inline internal::ContainsMatcher<M> Contains(M matcher) {
4826 return internal::ContainsMatcher<M>(matcher);
4948 // elements matching the given value or matcher.
4975 inline internal::EachMatcher<M> Each(M matcher) {
4976 return internal::EachMatcher<M>(matcher);
5000 // Conditional() creates a matcher that conditionally uses either the first or
5001 // second matcher provided. For example, we could create an `equal if, and only
5002 // if' matcher using the Conditional wrapper as follows:
5023 // Creates a matcher that matches a pointer (raw or smart) that matches
5031 // Creates a matcher that matches an object that has an address that matches
5040 // internal matcher.
5049 // given matcher.
5051 inline internal::MatcherAsPredicate<M> Matches(M matcher) {
5052 return internal::MatcherAsPredicate<M>(matcher);
5055 // Returns true if and only if the value matches the matcher.
5057 inline bool Value(const T& value, M matcher) {
5058 return testing::Matches(matcher)(value);
5061 // Matches the value against the given matcher and explains the match
5064 inline bool ExplainMatchResult(M matcher, const T& value,
5066 return SafeMatcherCast<const T&>(matcher).MatchAndExplain(value, listener);
5069 // Returns a string representation of the given matcher. Useful for description
5073 // MATCHER_P(XAndYThat, matcher,
5074 // "X that " + DescribeMatcher<int>(matcher, negation) +
5076 // DescribeMatcher<double>(matcher, negation)) {
5077 // return ExplainMatchResult(matcher, arg.x(), result_listener) &&
5078 // ExplainMatchResult(matcher, arg.y(), result_listener);
5081 std::string DescribeMatcher(const M& matcher, bool negation = false) {
5083 Matcher<T> monomorphic_matcher = SafeMatcherCast<T>(matcher);
5110 // Define variadic matcher versions.
5212 InnerMatcher&& matcher) {
5214 std::forward<InnerMatcher>(matcher));
5225 inline InnerMatcher AllArgs(const InnerMatcher& matcher) {
5226 return matcher;
5229 // Returns a matcher that matches the value of an optional<> type variable.
5230 // The matcher implementation only uses '!arg' and requires that the optional<>
5243 // Returns a matcher that matches the value of a absl::any type variable.
5246 const Matcher<const T&>& matcher) {
5248 internal::any_cast_matcher::AnyCastMatcher<T>(matcher));
5251 // Returns a matcher that matches the value of a variant<> type variable.
5252 // The matcher implementation uses ADL to find the holds_alternative and get
5257 const Matcher<const T&>& matcher) {
5259 internal::variant_matcher::VariantMatcher<T>(matcher));
5270 WithWhatMatcherImpl(Matcher<std::string> matcher)
5271 : matcher_(std::move(matcher)) {}
5336 ExceptionMatcherImpl(Matcher<const Err&> matcher)
5337 : matcher_(std::move(matcher)) {}
5387 // This matcher accepts a callable and verifies that when invoked, it throws
5414 // Using matcher cast allows users to pass a matcher of a more broad type.
5433 // tests. ASSERT_THAT(value, matcher) and EXPECT_THAT(value, matcher)
5434 // succeed if and only if the value matches the matcher. If the assertion
5435 // fails, the value and the description of the matcher will be printed.
5436 #define ASSERT_THAT(value, matcher) \
5438 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
5439 #define EXPECT_THAT(value, matcher) \
5441 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)