Home | History | Annotate | Download | only in gmock

Lines Matching defs:Matcher

55 // To implement a matcher Foo for type T, define:
58 // 2. a factory function that creates a Matcher<T> object from a
62 // to write "v" instead of "Eq(v)" where a Matcher is expected, which
64 // ownership management as Matcher objects can now be copied like
68 // used by a matcher to explain why a value matches or doesn't match.
94 // the match result. A matcher's MatchAndExplain() method can use
108 // The implementation of a matcher.
114 // Returns true iff the matcher matches x; also explains the match
117 // example, the MatchAndExplain() method of the Pointee(...) matcher
120 // You should override this method when defining a new matcher.
123 // that 'listener' is not NULL. This helps to simplify a matcher's
130 // Describes this matcher to an ostream. The function should print
132 // matcher should have. The subject of the verb phrase is the value
134 // matcher prints "is greater than 7".
137 // Describes the negation of this matcher to an ostream. For
138 // example, if the description of this matcher is "is greater than
141 // MatcherInterface, but it is highly advised so that your matcher
187 // An internal class for implementing Matcher<T>, which will derive
188 // from it. We put functionalities common to all Matcher<T>
193 // Returns true iff the matcher matches x; also explains the match
199 // Returns true iff this matcher matches x.
205 // Describes this matcher to an ostream.
208 // Describes the negation of this matcher to an ostream.
213 // Explains why x matches, or doesn't match, the matcher.
222 // Constructs a matcher from its implementation.
245 // A Matcher<T> is a copyable and IMMUTABLE (except by assignment)
247 // implementation of Matcher<T> is just a linked_ptr to const
249 // from Matcher!
251 class Matcher : public internal::MatcherBase<T> {
253 // Constructs a null matcher. Needed for storing Matcher objects in STL
254 // containers. A default-constructed matcher is not yet initialized. You
256 Matcher() {}
258 // Constructs a matcher from its implementation.
259 explicit Matcher(const MatcherInterface<T>* impl)
264 Matcher(T value); // NOLINT
269 // matcher is expected.
271 class GTEST_API_ Matcher<const internal::string&>
274 Matcher() {}
276 explicit Matcher(const MatcherInterface<const internal::string&>* impl)
281 Matcher(const internal::string& s); // NOLINT
284 Matcher(const char* s); // NOLINT
288 class GTEST_API_ Matcher<internal::string>
291 Matcher() {}
293 explicit Matcher(const MatcherInterface<internal::string>* impl)
298 Matcher(const internal::string& s); // NOLINT
301 Matcher(const char* s); // NOLINT
305 // polymorphic matcher (i.e. a matcher that can match values of more
308 // To define a polymorphic matcher, a user should provide an Impl
321 // Returns a mutable reference to the underlying matcher
325 // Returns an immutable reference to the underlying matcher
330 operator Matcher<T>() const {
331 return Matcher<T>(new MonomorphicImpl<T>(impl_));
363 // Creates a matcher from its implementation. This is easier to use
364 // than the Matcher<T> constructor as it doesn't require you to
369 // Matcher<const string&>(foo);
371 inline Matcher<T> MakeMatcher(const MatcherInterface<T>* impl) {
372 return Matcher<T>(impl);
375 // Creates a polymorphic matcher from its implementation. This is
398 // polymorphic matcher (i.e. something that can be converted to a
399 // Matcher but is not one yet; for example, Eq(value)) or a value (for
404 static Matcher<T> Cast(M polymorphic_matcher_or_value) {
405 // M can be a polymorhic matcher, in which case we want to use
406 // its conversion operator to create Matcher<T>. Or it can be a value
407 // that should be passed to the Matcher<T>'s constructor.
409 // We can't call Matcher<T>(polymorphic_matcher_or_value) when M is a
410 // polymorphic matcher because it'll be ambiguous if T has an implicit
415 // polymorphic_matcher_or_value to Matcher<T> because it won't trigger
421 internal::ImplicitlyConvertible<M, Matcher<T> >::value>());
425 static Matcher<T> CastImpl(M value, BooleanConstant<false>) {
426 // M can't be implicitly converted to Matcher<T>, so M isn't a polymorphic
427 // matcher. It must be a value then. Use direct initialization to create
428 // a matcher.
429 return Matcher<T>(ImplicitCast_<T>(value));
432 static Matcher<T> CastImpl(M polymorphic_matcher_or_value,
434 // M is implicitly convertible to Matcher<T>, which means that either
435 // M is a polymorhpic matcher or Matcher<T> has an implicit constructor
437 // matcher.
440 // creating Matcher<T> would require a chain of two user-defined conversions
441 // (first to create T from M and then to create Matcher<T> from T).
447 // is already a Matcher. This only compiles when type T can be
450 class MatcherCastImpl<T, Matcher<U> > {
452 static Matcher<T> Cast(const Matcher<U>& source_matcher) {
453 return Matcher<T>(new Impl(source_matcher));
459 explicit Impl(const Matcher<U>& source_matcher)
462 // We delegate the matching logic to the source matcher.
476 const Matcher<U> source_matcher_;
483 // a matcher to its own type.
485 class MatcherCastImpl<T, Matcher<T> > {
487 static Matcher<T> Cast(const Matcher<T>& matcher) { return matcher; }
492 // In order to be safe and clear, casting between different matcher
494 // matcher m and returns a Matcher<T>. It compiles only when T can be
497 inline Matcher<T> MatcherCast(M matcher) {
498 return internal::MatcherCastImpl<T, M>::Cast(matcher);
506 // template <T, U> ... (const Matcher<U>&)
514 static inline Matcher<T> Cast(M polymorphic_matcher_or_value) {
521 // safely convert a Matcher<U> to a Matcher<T> (i.e. Matcher is
522 // contravariant): just keep a copy of the original Matcher<U>, convert the
523 // argument from type T to U, and then pass it to the underlying Matcher<U>.
525 // underlying Matcher<U> may be interested in the argument's address, which
528 static inline Matcher<T> Cast(const Matcher<U>& matcher) {
547 return MatcherCast<T>(matcher);
552 inline Matcher<T> SafeMatcherCast(const M& polymorphic_matcher) {
556 // A<T>() returns a matcher that matches any value of type T.
558 Matcher<T> A();
582 // Matches the value against the given matcher, prints the value and explains
588 bool MatchPrintAndExplain(Value& value, const Matcher<T>& matcher,
593 return matcher.Matches(value);
597 const bool match = matcher.MatchAndExplain(value, &inner_listener);
642 typename tuple_element<N - 1, MatcherTuple>::type matcher =
647 if (!matcher.MatchAndExplain(value, &listener)) {
656 // matcher's MatchAndExplain() method handles the case when
725 // Implements _, a matcher that matches any value of any
726 // type. This is a polymorphic matcher, so we need a template type
727 // conversion operator to make it appearing as a Matcher<T> for any
732 operator Matcher<T>() const { return A<T>(); }
735 // Implements a matcher that compares a given value with a
739 // The matcher defined here is polymorphic (for example, Eq(5) can be
750 template <typename Rhs> class name##Matcher { \
752 explicit name##Matcher(const Rhs& rhs) : rhs_(rhs) {} \
754 operator Matcher<Lhs>() const { \
779 GTEST_DISALLOW_ASSIGN_(name##Matcher); \
793 // Implements the polymorphic IsNull() matcher, which matches any raw or smart
809 // Implements the polymorphic NotNull() matcher, which matches any raw or smart
826 // 'variable'. This matcher is polymorphic as it can match any
836 // Matcher<int> m1 = Ref(n); // This won't compile.
837 // Matcher<int&> m2 = Ref(n); // This will compile.
850 // compiler to catch using Ref(const_value) as a matcher for a
855 operator Matcher<Super&>() const {
858 // this catches using Ref(const_value) as a matcher for a
986 // Implements the polymorphic HasSubstr(substring) matcher, which
987 // can be used as a Matcher<T> as long as T can be converted to a
998 // Matcher<T> as long as T can be converted to string. Returns true
1010 // Describes what this matcher matches.
1027 // Implements the polymorphic StartsWith(substring) matcher, which
1028 // can be used as a Matcher<T> as long as T can be converted to a
1039 // Matcher<T> as long as T can be converted to string. Returns true
1068 // Implements the polymorphic EndsWith(substring) matcher, which
1069 // can be used as a Matcher<T> as long as T can be converted to a
1079 // Matcher<T> as long as T can be converted to string. Returns true
1109 // ContainsRegex(regex), which can be used as a Matcher<T> as long as
1117 // a Matcher<T> as long as T can be converted to string. Returns
1150 // Implements a matcher that compares the two fields of a 2-tuple
1154 // The matcher defined here is polymorphic (for example, Eq() can be
1162 class name##2Matcher { \
1165 operator Matcher< ::std::tr1::tuple<T1, T2> >() const { \
1169 operator Matcher<const ::std::tr1::tuple<T1, T2>&>() const { \
1204 // Implements the Not(...) matcher for a particular argument type T.
1211 explicit NotMatcherImpl(const Matcher<T>& matcher)
1212 : matcher_(matcher) {}
1227 const Matcher<T> matcher_;
1232 // Implements the Not(m) matcher, which matches a value that doesn't
1233 // match matcher m.
1237 explicit NotMatcher(InnerMatcher matcher) : matcher_(matcher) {}
1242 operator Matcher<T>() const {
1243 return Matcher<T>(new NotMatcherImpl<T>(SafeMatcherCast<T>(matcher_)));
1252 // Implements the AllOf(m1, m2) matcher for a particular argument type
1259 BothOfMatcherImpl(const Matcher<T>& matcher1, const Matcher<T>& matcher2)
1309 const Matcher<T> matcher1_;
1310 const Matcher<T> matcher2_;
1315 // Used for implementing the AllOf(m_1, ..., m_n) matcher, which
1327 operator Matcher<T>() const {
1328 return Matcher<T>(new BothOfMatcherImpl<T>(SafeMatcherCast<T>(matcher1_),
1339 // Implements the AnyOf(m1, m2) matcher for a particular argument type
1346 EitherOfMatcherImpl(const Matcher<T>& matcher1, const Matcher<T>& matcher2)
1396 const Matcher<T> matcher1_;
1397 const Matcher<T> matcher2_;
1402 matcher, which
1415 operator Matcher<T>() const {
1416 return Matcher<T>(new EitherOfMatcherImpl<T>(
1428 // matcher.
1434 // This method template allows Truly(pred) to be used as a matcher
1466 // Used for implementing Matches(matcher), which turns a matcher into
1471 explicit MatcherAsPredicate(M matcher) : matcher_(matcher) {}
1474 // predicate on type T where m is a matcher on type T.
1477 // some matcher may be interested in its address (e.g. as in
1483 // allows us to write Matches(m) where m is a polymorphic matcher
1486 // If we write Matcher<T>(matcher_).Matches(x) here, it won't
1487 // compile when matcher_ has type Matcher<const T&>; if we write
1488 // Matcher<const T&>(matcher_).Matches(x) here, it won't compile
1489 // when matcher_ has type Matcher<T>; if we just write
1505 // argument M must be a type that can be converted to a matcher.
1516 // We convert matcher_ to a Matcher<const T&> *now* instead of
1523 // Matcher<const T&>(matcher_), as the latter won't compile when
1524 // matcher_ has type Matcher<T> (e.g. An<int>()).
1525 const Matcher<const T&> matcher = MatcherCast<const T&>(matcher_);
1527 if (MatchPrintAndExplain(x, matcher, &listener))
1533 matcher.DescribeTo(&ss);
1544 // A helper function for converting a matcher to a predicate-formatter
1549 MakePredicateFormatterFromMatcher(const M& matcher) {
1550 return PredicateFormatterFromMatcher<M>(matcher);
1553 // Implements the polymorphic floating point equality matcher, which
1561 // The matcher's input will be compared with rhs. The matcher treats two
1567 // Implements floating point equality matcher as a Matcher<T>.
1629 // NanSensitiveFloatEq(rhs) to be used as a Matcher<float>, a
1630 // Matcher<const float&>, or a Matcher<float&>, but nothing else.
1634 operator Matcher<FloatType>() const {
1638 operator Matcher<const FloatType&>() const {
1642 operator Matcher<FloatType&>() const {
1653 // Implements the Pointee(m) matcher for matching a pointer whose
1654 // pointee matches matcher m. The pointer can be either raw or smart.
1658 explicit PointeeMatcher(const InnerMatcher& matcher) : matcher_(matcher) {}
1661 // used as a matcher for any pointer type whose pointee type is
1662 // compatible with the inner matcher, where type Pointer can be
1669 operator Matcher<Pointer>() const {
1681 explicit Impl(const InnerMatcher& matcher)
1682 : matcher_(MatcherCast<const Pointee&>(matcher)) {}
1704 const Matcher<const Pointee&> matcher_;
1714 // Implements the Field() matcher for matching a field (i.e. member
1720 const Matcher<const FieldType&>& matcher)
1721 : field_(field), matcher_(matcher) {}
1744 // true_type iff the Field() matcher is used to match a pointer.
1764 const Matcher<const FieldType&> matcher_;
1769 // Implements the Property() matcher for matching a property
1781 const Matcher<RefToConstProperty>& matcher)
1782 : property_(property), matcher_(matcher) {}
1805 // true_type iff the Property() matcher is used to match a pointer.
1828 const Matcher<RefToConstProperty> matcher_;
1863 // Implements the ResultOf() matcher for matching a return value of a
1870 ResultOfMatcher(Callable callable, const Matcher<ResultType>& matcher)
1871 : callable_(callable), matcher_(matcher) {
1876 operator Matcher<T>() const {
1877 return Matcher<T>(new Impl<T>(callable_, matcher_));
1886 Impl(CallableStorageType callable, const Matcher<ResultType>& matcher)
1887 : callable_(callable), matcher_(matcher) {}
1915 const Matcher<ResultType> matcher_;
1921 const Matcher<ResultType> matcher_;
1926 // Implements an equality matcher for any STL-style container whose elements
1927 // support ==. This matcher is like Eq(), but its failure explanations provide
1944 // after this matcher is created.
2031 const ContainerMatcher& matcher)
2032 : comparator_(comparator), matcher_(matcher) {}
2035 operator Matcher<LhsContainer>() const {
2048 Impl(const Comparator& comparator, const ContainerMatcher& matcher)
2049 : comparator_(comparator), matcher_(matcher) {}
2087 const Matcher<const std::vector<LhsValue>&> matcher_;
2100 // must be able to be safely cast to Matcher<tuple<const T1&, const
2111 // it are modified after this matcher is created.
2121 operator Matcher<LhsContainer>() const {
2133 // We pass the LHS value and the RHS value to the inner matcher by
2140 // mono_tuple_matcher_ holds a monomorphic version of the tuple matcher.
2196 const Matcher<InnerMatcherArg> mono_tuple_matcher_;
2248 const Matcher<const Element&> inner_matcher_;
2262 // Describes what this matcher does.
2291 // Describes what this matcher does.
2318 operator Matcher<Container>() const {
2335 operator Matcher<Container>() const {
2361 // Returns true iff 'key_value.first' (the key) matches the inner matcher.
2374 // Describes what this matcher does.
2380 // Describes what the negation of this matcher does.
2387 const Matcher<const KeyType&> inner_matcher_;
2399 operator Matcher<PairType>() const {
2426 // Describes what this matcher does.
2434 // Describes what the negation of this matcher does.
2490 const Matcher<const FirstType&> first_matcher_;
2491 const Matcher<const SecondType&> second_matcher_;
2504 operator Matcher<PairType> () const {
2527 // Constructs the matcher from a sequence of element values or
2538 // Describes what this matcher does.
2557 // Describes what the negation of this matcher does.
2628 std::vector<Matcher<const Element&> > matchers_;
2639 operator Matcher<Container>() const {
2644 const Matcher<const Element&>* const matchers = NULL;
2657 operator Matcher<Container>() const {
2672 // Returns the description for a matcher defined using the MATCHER*()
2675 // negation of the matcher. 'param_values' contains a list of strings
2676 // that are the print-out of the matcher's parameters.
2683 // _ is a matcher that matches anything of any type.
2693 // Creates a matcher that matches any value of the given type T.
2695 inline Matcher<T> A() { return MakeMatcher(new internal::AnyMatcherImpl<T>()); }
2697 // Creates a matcher that matches any value of the given type T.
2699 inline Matcher<T> An() { return A<T>(); }
2701 // Creates a polymorphic matcher that matches anything equal to x.
2707 // Constructs a Matcher<T> from a 'value' of type T. The constructed
2708 // matcher matches any value that's equal to 'value'.
2710 Matcher<T>::Matcher(T value) { *this = Eq(value); }
2712 // Creates a monomorphic matcher that matches anything with type Lhs
2716 // TypedEq<T>(x) is just a convenient short-hand for Matcher<T>(Eq(x))
2717 // or Matcher<T>(x), but more readable than the latter.
2722 // can always write Matcher<T>(Lt(5)) to be explicit about the type,
2725 inline Matcher<Lhs> TypedEq(const Rhs& rhs) { return Eq(rhs); }
2727 // Creates a polymorphic matcher that matches anything >= x.
2733 // Creates a polymorphic matcher that matches anything > x.
2739 // Creates a polymorphic matcher that matches anything <= x.
2745 // Creates a polymorphic matcher that matches anything < x.
2751 // Creates a polymorphic matcher that matches anything != x.
2757 // Creates a polymorphic matcher that matches any NULL pointer.
2762 // Creates a polymorphic matcher that matches any non-NULL pointer.
2769 // Creates a polymorphic matcher that matches any argument that
2776 // Creates a matcher that matches any double argument approximately
2782 // Creates a matcher that matches any double argument approximately
2788 // Creates a matcher that matches any float argument approximately
2794 // Creates a matcher
2800 // Creates a matcher that matches a pointer (raw or smart) that points
2808 // Creates a matcher that matches an object whose given field matches
2809 // 'matcher'. For example,
2815 FieldType Class::*field, const FieldMatcher& matcher) {
2818 field, MatcherCast<const FieldType&>(matcher)));
2822 // to compile where bar is an int32 and m is a matcher for int64.
2825 // Creates a matcher that matches an object whose given property
2826 // matches 'matcher'. For example,
2832 PropertyType (Class::*property)() const, const PropertyMatcher& matcher) {
2836 MatcherCast<GTEST_REFERENCE_TO_CONST_(PropertyType)>(matcher)));
2840 // to compile where bar() returns an int32 and m is a matcher for int64.
2843 // Creates a matcher that matches an object iff the result of applying
2844 // a callable to x matches 'matcher'.
2858 Callable callable, const ResultOfMatcher& matcher) {
2862 matcher));
2866 // to compile where Function() returns an int32 and m is a matcher for int64.
2899 // Creates a matcher that matches any string, std::string, or C string
2922 // The matcher takes ownership of 'regex'.
2933 // The matcher takes ownership of 'regex'.
2974 // Creates a matcher that matches any wstring, std::wstring, or C wide string
2998 // Creates a polymorphic matcher that matches a 2-tuple where the
3002 // Creates a polymorphic matcher that matches a 2-tuple where the
3006 // Creates a polymorphic matcher that matches a 2-tuple where the
3010 // Creates a polymorphic matcher that matches a 2-tuple where the
3014 // Creates a polymorphic matcher that matches a 2-tuple where the
3018 // Creates a polymorphic matcher that matches a 2-tuple where the
3022 // Creates a matcher that matches any value of type T that m doesn't
3029 // Returns a matcher that matches anything that satisfies the given
3038 // Returns a matcher that matches an equal container.
3039 // This matcher behaves like Eq(), but in the event of mismatch lists the
3053 // Returns a matcher that matches a container that, when sorted using
3063 // Returns a matcher that matches a container that, when sorted using
3075 // i-th element (as a pair) satisfy the given pair matcher, for all i.
3076 // TupleMatcher must be able to be safely cast to Matcher<tuple<const
3091 // least one element matching the given value or matcher.
3109 inline internal::ContainsMatcher<M> Contains(M matcher) {
3110 return internal::ContainsMatcher<M>(matcher);
3114 // elements matching the given value or matcher.
3141 inline internal::EachMatcher<M> Each(M matcher) {
3142 return internal::EachMatcher<M>(matcher);
3166 // given matcher.
3168 inline internal::MatcherAsPredicate<M> Matches(M matcher) {
3169 return internal::MatcherAsPredicate<M>(matcher);
3172 // Returns true iff the value matches the matcher.
3174 inline bool Value(const T& value, M matcher) {
3175 return testing::Matches(matcher)(value);
3178 // Matches the value against the given matcher and explains the match
3182 M matcher, const T& value, MatchResultListener* listener) {
3183 return SafeMatcherCast<const T&>(matcher).MatchAndExplain(value, listener);
3194 inline InnerMatcher AllArgs(const InnerMatcher& matcher) { return matcher; }
3197 // tests. ASSERT_THAT(value, matcher) and EXPECT_THAT(value, matcher)
3198 // succeed iff the value matches the matcher. If the assertion fails,
3199 // the value and the description of the matcher will be printed.
3200 #define ASSERT_THAT(value, matcher) ASSERT_PRED_FORMAT1(\
3201 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
3202 #define EXPECT_THAT(value, matcher) EXPECT_PRED_FORMAT1(\
3203 ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)