gmock-matchers.h
Go to the documentation of this file.
1 // Copyright 2007, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: wan@google.com (Zhanyong Wan)
31 
32 // Google Mock - a framework for writing C++ mock classes.
33 //
34 // This file implements some commonly used argument matchers. More
35 // matchers can be defined by the user implementing the
36 // MatcherInterface<T> interface if necessary.
37 
38 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_
39 #define GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_
40 
41 #include <math.h>
42 #include <algorithm>
43 #include <iterator>
44 #include <limits>
45 #include <ostream> // NOLINT
46 #include <sstream>
47 #include <string>
48 #include <utility>
49 #include <vector>
50 
53 #include "gtest/gtest.h"
54 
55 #if GTEST_LANG_CXX11
56 #include <initializer_list> // NOLINT -- must be after gtest.h
57 #endif
58 
59 namespace testing
60 {
61 
62 // To implement a matcher Foo for type T, define:
63 // 1. a class FooMatcherImpl that implements the
64 // MatcherInterface<T> interface, and
65 // 2. a factory function that creates a Matcher<T> object from a
66 // FooMatcherImpl*.
67 //
68 // The two-level delegation design makes it possible to allow a user
69 // to write "v" instead of "Eq(v)" where a Matcher is expected, which
70 // is impossible if we pass matchers by pointers. It also eases
71 // ownership management as Matcher objects can now be copied like
72 // plain values.
73 
74 // MatchResultListener is an abstract class. Its << operator can be
75 // used by a matcher to explain why a value matches or doesn't match.
76 //
77 // TODO(wan@google.com): add method
78 // bool InterestedInWhy(bool result) const;
79 // to indicate whether the listener is interested in why the match
80 // result is 'result'.
81 class MatchResultListener
82 {
83 public:
84  // Creates a listener object with the given underlying ostream. The
85  // listener does not own the ostream, and does not dereference it
86  // in the constructor or destructor.
87  explicit MatchResultListener(::std::ostream * os) : stream_(os) {}
88  virtual ~MatchResultListener() = 0; // Makes this class abstract.
89 
90  // Streams x to the underlying ostream; does nothing if the ostream
91  // is NULL.
92  template <typename T>
94  {
95  if (stream_ != NULL)
96  { *stream_ << x; }
97 
98  return *this;
99  }
100 
101  // Returns the underlying ostream.
102  ::std::ostream * stream() { return stream_; }
103 
104  // Returns true iff the listener is interested in an explanation of
105  // the match result. A matcher's MatchAndExplain() method can use
106  // this information to avoid generating the explanation when no one
107  // intends to hear it.
108  bool IsInterested() const { return stream_ != NULL; }
109 
110 private:
111  ::std::ostream * const stream_;
112 
114 };
115 
117 {
118 }
119 
120 // An instance of a subclass of this knows how to describe itself as a
121 // matcher.
123 {
124 public:
126 
127  // Describes this matcher to an ostream. The function should print
128  // a verb phrase that describes the property a value matching this
129  // matcher should have. The subject of the verb phrase is the value
130  // being matched. For example, the DescribeTo() method of the Gt(7)
131  // matcher prints "is greater than 7".
132  virtual void DescribeTo(::std::ostream * os) const = 0;
133 
134  // Describes the negation of this matcher to an ostream. For
135  // example, if the description of this matcher is "is greater than
136  // 7", the negated description could be "is not greater than 7".
137  // You are not required to override this when implementing
138  // MatcherInterface, but it is highly advised so that your matcher
139  // can produce good error messages.
140  virtual void DescribeNegationTo(::std::ostream * os) const
141  {
142  *os << "not (";
143  DescribeTo(os);
144  *os << ")";
145  }
146 };
147 
148 // The implementation of a matcher.
149 template <typename T>
151 {
152 public:
153  // Returns true iff the matcher matches x; also explains the match
154  // result to 'listener' if necessary (see the next paragraph), in
155  // the form of a non-restrictive relative clause ("which ...",
156  // "whose ...", etc) that describes x. For example, the
157  // MatchAndExplain() method of the Pointee(...) matcher should
158  // generate an explanation like "which points to ...".
159  //
160  // Implementations of MatchAndExplain() should add an explanation of
161  // the match result *if and only if* they can provide additional
162  // information that's not already present (or not obvious) in the
163  // print-out of x and the matcher's description. Whether the match
164  // succeeds is not a factor in deciding whether an explanation is
165  // needed, as sometimes the caller needs to print a failure message
166  // when the match succeeds (e.g. when the matcher is used inside
167  // Not()).
168  //
169  // For example, a "has at least 10 elements" matcher should explain
170  // what the actual element count is, regardless of the match result,
171  // as it is useful information to the reader; on the other hand, an
172  // "is empty" matcher probably only needs to explain what the actual
173  // size is when the match fails, as it's redundant to say that the
174  // size is 0 when the value is already known to be empty.
175  //
176  // You should override this method when defining a new matcher.
177  //
178  // It's the responsibility of the caller (Google Mock) to guarantee
179  // that 'listener' is not NULL. This helps to simplify a matcher's
180  // implementation when it doesn't care about the performance, as it
181  // can talk to 'listener' without checking its validity first.
182  // However, in order to implement dummy listeners efficiently,
183  // listener->stream() may be NULL.
184  virtual bool MatchAndExplain(T x, MatchResultListener * listener) const = 0;
185 
186  // Inherits these methods from MatcherDescriberInterface:
187  // virtual void DescribeTo(::std::ostream* os) const = 0;
188  // virtual void DescribeNegationTo(::std::ostream* os) const;
189 };
190 
191 // A match result listener that stores the explanation in a string.
193 {
194 public:
196 
197  // Returns the explanation accumulated so far.
198  internal::string str() const { return ss_.str(); }
199 
200  // Clears the explanation accumulated so far.
201  void Clear() { ss_.str(""); }
202 
203 private:
204  ::std::stringstream ss_;
205 
207 };
208 
209 namespace internal
210 {
211 
212 // A match result listener that ignores the explanation.
213 class DummyMatchResultListener : public MatchResultListener
214 {
215 public:
217 
218 private:
220 };
221 
222 // A match result listener that forwards the explanation to a given
223 // ostream. The difference between this and MatchResultListener is
224 // that the former is concrete.
226 {
227 public:
228  explicit StreamMatchResultListener(::std::ostream * os)
229  : MatchResultListener(os) {}
230 
231 private:
233 };
234 
235 // An internal class for implementing Matcher<T>, which will derive
236 // from it. We put functionalities common to all Matcher<T>
237 // specializations here to avoid code duplication.
238 template <typename T>
239 class MatcherBase
240 {
241 public:
242  // Returns true iff the matcher matches x; also explains the match
243  // result to 'listener'.
244  bool MatchAndExplain(T x, MatchResultListener * listener) const
245  {
246  return impl_->MatchAndExplain(x, listener);
247  }
248 
249  // Returns true iff this matcher matches x.
250  bool Matches(T x) const
251  {
253  return MatchAndExplain(x, &dummy);
254  }
255 
256  // Describes this matcher to an ostream.
257  void DescribeTo(::std::ostream * os) const { impl_->DescribeTo(os); }
258 
259  // Describes the negation of this matcher to an ostream.
260  void DescribeNegationTo(::std::ostream * os) const
261  {
262  impl_->DescribeNegationTo(os);
263  }
264 
265  // Explains why x matches, or doesn't match, the matcher.
266  void ExplainMatchResultTo(T x, ::std::ostream * os) const
267  {
268  StreamMatchResultListener listener(os);
269  MatchAndExplain(x, &listener);
270  }
271 
272  // Returns the describer for this matcher object; retains ownership
273  // of the describer, which is only guaranteed to be alive when
274  // this matcher object is alive.
276  {
277  return impl_.get();
278  }
279 
280 protected:
282 
283  // Constructs a matcher from its implementation.
284  explicit MatcherBase(const MatcherInterface<T> * impl)
285  : impl_(impl) {}
286 
287  virtual ~MatcherBase() {}
288 
289 private:
290  // shared_ptr (util/gtl/shared_ptr.h) and linked_ptr have similar
291  // interfaces. The former dynamically allocates a chunk of memory
292  // to hold the reference count, while the latter tracks all
293  // references using a circular linked list without allocating
294  // memory. It has been observed that linked_ptr performs better in
295  // typical scenarios. However, shared_ptr can out-perform
296  // linked_ptr when there are many more uses of the copy constructor
297  // than the default constructor.
298  //
299  // If performance becomes a problem, we should see if using
300  // shared_ptr helps.
302 };
303 
304 } // namespace internal
305 
306 // A Matcher<T> is a copyable and IMMUTABLE (except by assignment)
307 // object that can check whether a value of type T matches. The
308 // implementation of Matcher<T> is just a linked_ptr to const
309 // MatcherInterface<T>, so copying is fairly cheap. Don't inherit
310 // from Matcher!
311 template <typename T>
312 class Matcher : public internal::MatcherBase<T>
313 {
314 public:
315  // Constructs a null matcher. Needed for storing Matcher objects in STL
316  // containers. A default-constructed matcher is not yet initialized. You
317  // cannot use it until a valid value has been assigned to it.
318  Matcher() {}
319 
320  // Constructs a matcher from its implementation.
321  explicit Matcher(const MatcherInterface<T> * impl)
322  : internal::MatcherBase<T>(impl) {}
323 
324  // Implicit constructor here allows people to write
325  // EXPECT_CALL(foo, Bar(5)) instead of EXPECT_CALL(foo, Bar(Eq(5))) sometimes
326  Matcher(T value); // NOLINT
327 };
328 
329 // The following two specializations allow the user to write str
330 // instead of Eq(str) and "foo" instead of Eq("foo") when a string
331 // matcher is expected.
332 template <>
333 class GTEST_API_ Matcher<const internal::string &>
334  : public internal::MatcherBase<const internal::string &>
335 {
336 public:
337  Matcher() {}
338 
340  : internal::MatcherBase<const internal::string & >(impl) {}
341 
342  // Allows the user to write str instead of Eq(str) sometimes, where
343  // str is a string object.
344  Matcher(const internal::string & s); // NOLINT
345 
346  // Allows the user to write "foo" instead of Eq("foo") sometimes.
347  Matcher(const char * s); // NOLINT
348 };
349 
350 template <>
351 class GTEST_API_ Matcher<internal::string>
352  : public internal::MatcherBase<internal::string>
353 {
354 public:
355  Matcher() {}
356 
358  : internal::MatcherBase<internal::string>(impl) {}
359 
360  // Allows the user to write str instead of Eq(str) sometimes, where
361  // str is a string object.
362  Matcher(const internal::string & s); // NOLINT
363 
364  // Allows the user to write "foo" instead of Eq("foo") sometimes.
365  Matcher(const char * s); // NOLINT
366 };
367 
368 #if GTEST_HAS_STRING_PIECE_
369 // The following two specializations allow the user to write str
370 // instead of Eq(str) and "foo" instead of Eq("foo") when a StringPiece
371 // matcher is expected.
372 template <>
373 class GTEST_API_ Matcher<const StringPiece &>
375 {
376 public:
377  Matcher() {}
378 
379  explicit Matcher(const MatcherInterface<const StringPiece &> * impl)
381 
382  // Allows the user to write str instead of Eq(str) sometimes, where
383  // str is a string object.
384  Matcher(const internal::string & s); // NOLINT
385 
386  // Allows the user to write "foo" instead of Eq("foo") sometimes.
387  Matcher(const char * s); // NOLINT
388 
389  // Allows the user to pass StringPieces directly.
390  Matcher(StringPiece s); // NOLINT
391 };
392 
393 template <>
394 class GTEST_API_ Matcher<StringPiece>
395  : public internal::MatcherBase<StringPiece>
396 {
397 public:
398  Matcher() {}
399 
400  explicit Matcher(const MatcherInterface<StringPiece> * impl)
402 
403  // Allows the user to write str instead of Eq(str) sometimes, where
404  // str is a string object.
405  Matcher(const internal::string & s); // NOLINT
406 
407  // Allows the user to write "foo" instead of Eq("foo") sometimes.
408  Matcher(const char * s); // NOLINT
409 
410  // Allows the user to pass StringPieces directly.
411  Matcher(StringPiece s); // NOLINT
412 };
413 #endif // GTEST_HAS_STRING_PIECE_
414 
415 // The PolymorphicMatcher class template makes it easy to implement a
416 // polymorphic matcher (i.e. a matcher that can match values of more
417 // than one type, e.g. Eq(n) and NotNull()).
418 //
419 // To define a polymorphic matcher, a user should provide an Impl
420 // class that has a DescribeTo() method and a DescribeNegationTo()
421 // method, and define a member function (or member function template)
422 //
423 // bool MatchAndExplain(const Value& value,
424 // MatchResultListener* listener) const;
425 //
426 // See the definition of NotNull() for a complete example.
427 template <class Impl>
428 class PolymorphicMatcher
429 {
430 public:
431  explicit PolymorphicMatcher(const Impl & an_impl) : impl_(an_impl) {}
432 
433  // Returns a mutable reference to the underlying matcher
434  // implementation object.
435  Impl & mutable_impl() { return impl_; }
436 
437  // Returns an immutable reference to the underlying matcher
438  // implementation object.
439  const Impl & impl() const { return impl_; }
440 
441  template <typename T>
442  operator Matcher<T>() const
443  {
444  return Matcher<T>(new MonomorphicImpl<T>(impl_));
445  }
446 
447 private:
448  template <typename T>
449  class MonomorphicImpl : public MatcherInterface<T>
450  {
451  public:
452  explicit MonomorphicImpl(const Impl & impl) : impl_(impl) {}
453 
454  virtual void DescribeTo(::std::ostream * os) const
455  {
456  impl_.DescribeTo(os);
457  }
458 
459  virtual void DescribeNegationTo(::std::ostream * os) const
460  {
461  impl_.DescribeNegationTo(os);
462  }
463 
464  virtual bool MatchAndExplain(T x, MatchResultListener * listener) const
465  {
466  return impl_.MatchAndExplain(x, listener);
467  }
468 
469  private:
470  const Impl impl_;
471 
473  };
474 
475  Impl impl_;
476 
478 };
479 
480 // Creates a matcher from its implementation. This is easier to use
481 // than the Matcher<T> constructor as it doesn't require you to
482 // explicitly write the template argument, e.g.
483 //
484 // MakeMatcher(foo);
485 // vs
486 // Matcher<const string&>(foo);
487 template <typename T>
488 inline Matcher<T> MakeMatcher(const MatcherInterface<T> * impl)
489 {
490  return Matcher<T>(impl);
491 }
492 
493 // Creates a polymorphic matcher from its implementation. This is
494 // easier to use than the PolymorphicMatcher<Impl> constructor as it
495 // doesn't require you to explicitly write the template argument, e.g.
496 //
497 // MakePolymorphicMatcher(foo);
498 // vs
499 // PolymorphicMatcher<TypeOfFoo>(foo);
500 template <class Impl>
501 inline PolymorphicMatcher<Impl> MakePolymorphicMatcher(const Impl & impl)
502 {
503  return PolymorphicMatcher<Impl>(impl);
504 }
505 
506 // Anything inside the 'internal' namespace IS INTERNAL IMPLEMENTATION
507 // and MUST NOT BE USED IN USER CODE!!!
508 namespace internal
509 {
510 
511 // The MatcherCastImpl class template is a helper for implementing
512 // MatcherCast(). We need this helper in order to partially
513 // specialize the implementation of MatcherCast() (C++ allows
514 // class/struct templates to be partially specialized, but not
515 // function templates.).
516 
517 // This general version is used when MatcherCast()'s argument is a
518 // polymorphic matcher (i.e. something that can be converted to a
519 // Matcher but is not one yet; for example, Eq(value)) or a value (for
520 // example, "hello").
521 template <typename T, typename M>
522 class MatcherCastImpl
523 {
524 public:
525  static Matcher<T> Cast(M polymorphic_matcher_or_value)
526  {
527  // M can be a polymorhic matcher, in which case we want to use
528  // its conversion operator to create Matcher<T>. Or it can be a value
529  // that should be passed to the Matcher<T>'s constructor.
530  //
531  // We can't call Matcher<T>(polymorphic_matcher_or_value) when M is a
532  // polymorphic matcher because it'll be ambiguous if T has an implicit
533  // constructor from M (this usually happens when T has an implicit
534  // constructor from any type).
535  //
536  // It won't work to unconditionally implict_cast
537  // polymorphic_matcher_or_value to Matcher<T> because it won't trigger
538  // a user-defined conversion from M to T if one exists (assuming M is
539  // a value).
540  return CastImpl(
541  polymorphic_matcher_or_value,
544  }
545 
546 private:
548  {
549  // M can't be implicitly converted to Matcher<T>, so M isn't a polymorphic
550  // matcher. It must be a value then. Use direct initialization to create
551  // a matcher.
552  return Matcher<T>(ImplicitCast_<T>(value));
553  }
554 
555  static Matcher<T> CastImpl(M polymorphic_matcher_or_value,
557  {
558  // M is implicitly convertible to Matcher<T>, which means that either
559  // M is a polymorhpic matcher or Matcher<T> has an implicit constructor
560  // from M. In both cases using the implicit conversion will produce a
561  // matcher.
562  //
563  // Even if T has an implicit constructor from M, it won't be called because
564  // creating Matcher<T> would require a chain of two user-defined conversions
565  // (first to create T from M and then to create Matcher<T> from T).
566  return polymorphic_matcher_or_value;
567  }
568 };
569 
570 // This more specialized version is used when MatcherCast()'s argument
571 // is already a Matcher. This only compiles when type T can be
572 // statically converted to type U.
573 template <typename T, typename U>
574 class MatcherCastImpl<T, Matcher<U> >
575 {
576 public:
577  static Matcher<T> Cast(const Matcher<U> & source_matcher)
578  {
579  return Matcher<T>(new Impl(source_matcher));
580  }
581 
582 private:
583  class Impl : public MatcherInterface<T>
584  {
585  public:
586  explicit Impl(const Matcher<U> & source_matcher)
587  : source_matcher_(source_matcher) {}
588 
589  // We delegate the matching logic to the source matcher.
590  virtual bool MatchAndExplain(T x, MatchResultListener * listener) const
591  {
592  return source_matcher_.MatchAndExplain(static_cast<U>(x), listener);
593  }
594 
595  virtual void DescribeTo(::std::ostream * os) const
596  {
597  source_matcher_.DescribeTo(os);
598  }
599 
600  virtual void DescribeNegationTo(::std::ostream * os) const
601  {
602  source_matcher_.DescribeNegationTo(os);
603  }
604 
605  private:
606  const Matcher<U> source_matcher_;
607 
609  };
610 };
611 
612 // This even more specialized version is used for efficiently casting
613 // a matcher to its own type.
614 template <typename T>
615 class MatcherCastImpl<T, Matcher<T> >
616 {
617 public:
618  static Matcher<T> Cast(const Matcher<T> & matcher) { return matcher; }
619 };
620 
621 } // namespace internal
622 
623 // In order to be safe and clear, casting between different matcher
624 // types is done explicitly via MatcherCast<T>(m), which takes a
625 // matcher m and returns a Matcher<T>. It compiles only when T can be
626 // statically converted to the argument type of m.
627 template <typename T, typename M>
628 inline Matcher<T> MatcherCast(M matcher)
629 {
631 }
632 
633 // Implements SafeMatcherCast().
634 //
635 // We use an intermediate class to do the actual safe casting as Nokia's
636 // Symbian compiler cannot decide between
637 // template <T, M> ... (M) and
638 // template <T, U> ... (const Matcher<U>&)
639 // for function templates but can for member function templates.
640 template <typename T>
642 {
643 public:
644  // This overload handles polymorphic matchers and values only since
645  // monomorphic matchers are handled by the next one.
646  template <typename M>
647  static inline Matcher<T> Cast(M polymorphic_matcher_or_value)
648  {
649  return internal::MatcherCastImpl<T, M>::Cast(polymorphic_matcher_or_value);
650  }
651 
652  // This overload handles monomorphic matchers.
653  //
654  // In general, if type T can be implicitly converted to type U, we can
655  // safely convert a Matcher<U> to a Matcher<T> (i.e. Matcher is
656  // contravariant): just keep a copy of the original Matcher<U>, convert the
657  // argument from type T to U, and then pass it to the underlying Matcher<U>.
658  // The only exception is when U is a reference and T is not, as the
659  // underlying Matcher<U> may be interested in the argument's address, which
660  // is not preserved in the conversion from T to U.
661  template <typename U>
662  static inline Matcher<T> Cast(const Matcher<U> & matcher)
663  {
664  // Enforce that T can be implicitly converted to U.
666  T_must_be_implicitly_convertible_to_U);
667  // Enforce that we are not converting a non-reference type T to a reference
668  // type U.
671  cannot_convert_non_referentce_arg_to_reference);
672  // In case both T and U are arithmetic types, enforce that the
673  // conversion is not lossy.
674  typedef GTEST_REMOVE_REFERENCE_AND_CONST_(T) RawT;
675  typedef GTEST_REMOVE_REFERENCE_AND_CONST_(U) RawU;
676  const bool kTIsOther = GMOCK_KIND_OF_(RawT) == internal::kOther;
677  const bool kUIsOther = GMOCK_KIND_OF_(RawU) == internal::kOther;
679  kTIsOther || kUIsOther ||
680  (internal::LosslessArithmeticConvertible<RawT, RawU>::value),
681  conversion_of_arithmetic_types_must_be_lossless);
682  return MatcherCast<T>(matcher);
683  }
684 };
685 
686 template <typename T, typename M>
687 inline Matcher<T> SafeMatcherCast(const M & polymorphic_matcher)
688 {
689  return SafeMatcherCastImpl<T>::Cast(polymorphic_matcher);
690 }
691 
692 // A<T>() returns a matcher that matches any value of type T.
693 template <typename T>
694 Matcher<T> A();
695 
696 // Anything inside the 'internal' namespace IS INTERNAL IMPLEMENTATION
697 // and MUST NOT BE USED IN USER CODE!!!
698 namespace internal
699 {
700 
701 // If the explanation is not empty, prints it to the ostream.
702 inline void PrintIfNotEmpty(const internal::string & explanation,
703  ::std::ostream * os)
704 {
705  if (explanation != "" && os != NULL)
706  {
707  *os << ", " << explanation;
708  }
709 }
710 
711 // Returns true if the given type name is easy to read by a human.
712 // This is used to decide whether printing the type of a value might
713 // be helpful.
714 inline bool IsReadableTypeName(const string & type_name)
715 {
716  // We consider a type name readable if it's short or doesn't contain
717  // a template or function type.
718  return (type_name.length() <= 20 ||
719  type_name.find_first_of("<(") == string::npos);
720 }
721 
722 // Matches the value against the given matcher, prints the value and explains
723 // the match result to the listener. Returns the match result.
724 // 'listener' must not be NULL.
725 // Value cannot be passed by const reference, because some matchers take a
726 // non-const argument.
727 template <typename Value, typename T>
728 bool MatchPrintAndExplain(Value & value, const Matcher<T> & matcher,
729  MatchResultListener * listener)
730 {
731  if (!listener->IsInterested())
732  {
733  // If the listener is not interested, we do not need to construct the
734  // inner explanation.
735  return matcher.Matches(value);
736  }
737 
738  StringMatchResultListener inner_listener;
739  const bool match = matcher.MatchAndExplain(value, &inner_listener);
740 
741  UniversalPrint(value, listener->stream());
742 #if GTEST_HAS_RTTI
743  const string & type_name = GetTypeName<Value>();
744 
745  if (IsReadableTypeName(type_name))
746  { *listener->stream() << " (of type " << type_name << ")"; }
747 
748 #endif
749  PrintIfNotEmpty(inner_listener.str(), listener->stream());
750 
751  return match;
752 }
753 
754 // An internal helper class for doing compile-time loop on a tuple's
755 // fields.
756 template <size_t N>
757 class TuplePrefix
758 {
759 public:
760  // TuplePrefix<N>::Matches(matcher_tuple, value_tuple) returns true
761  // iff the first N fields of matcher_tuple matches the first N
762  // fields of value_tuple, respectively.
763  template <typename MatcherTuple, typename ValueTuple>
764  static bool Matches(const MatcherTuple & matcher_tuple,
765  const ValueTuple & value_tuple)
766  {
768  return TuplePrefix < N - 1 >::Matches(matcher_tuple, value_tuple)
769  && get < N - 1 > (matcher_tuple).Matches(get < N - 1 > (value_tuple));
770  }
771 
772  // TuplePrefix<N>::ExplainMatchFailuresTo(matchers, values, os)
773  // describes failures in matching the first N fields of matchers
774  // against the first N fields of values. If there is no failure,
775  // nothing will be streamed to os.
776  template <typename MatcherTuple, typename ValueTuple>
777  static void ExplainMatchFailuresTo(const MatcherTuple & matchers,
778  const ValueTuple & values,
779  ::std::ostream * os)
780  {
781  using ::std::tr1::tuple_element;
783 
784  // First, describes failures in the first N - 1 fields.
785  TuplePrefix < N - 1 >::ExplainMatchFailuresTo(matchers, values, os);
786 
787  // Then describes the failure (if any) in the (N - 1)-th (0-based)
788  // field.
789  typename tuple_element < N - 1, MatcherTuple >::type matcher =
790  get < N - 1 > (matchers);
791  typedef typename tuple_element < N - 1, ValueTuple >::type Value;
792  Value value = get < N - 1 > (values);
793  StringMatchResultListener listener;
794 
795  if (!matcher.MatchAndExplain(value, &listener))
796  {
797  // TODO(wan): include in the message the name of the parameter
798  // as used in MOCK_METHOD*() when possible.
799  *os << " Expected arg #" << N - 1 << ": ";
800  get < N - 1 > (matchers).DescribeTo(os);
801  *os << "\n Actual: ";
802  // We remove the reference in type Value to prevent the
803  // universal printer from printing the address of value, which
804  // isn't interesting to the user most of the time. The
805  // matcher's MatchAndExplain() method handles the case when
806  // the address is interesting.
807  internal::UniversalPrint(value, os);
808  PrintIfNotEmpty(listener.str(), os);
809  *os << "\n";
810  }
811  }
812 };
813 
814 // The base case.
815 template <>
816 class TuplePrefix<0>
817 {
818 public:
819  template <typename MatcherTuple, typename ValueTuple>
820  static bool Matches(const MatcherTuple & /* matcher_tuple */,
821  const ValueTuple & /* value_tuple */)
822  {
823  return true;
824  }
825 
826  template <typename MatcherTuple, typename ValueTuple>
827  static void ExplainMatchFailuresTo(const MatcherTuple & /* matchers */,
828  const ValueTuple & /* values */,
829  ::std::ostream * /* os */) {}
830 };
831 
832 // TupleMatches(matcher_tuple, value_tuple) returns true iff all
833 // matchers in matcher_tuple match the corresponding fields in
834 // value_tuple. It is a compiler error if matcher_tuple and
835 // value_tuple have different number of fields or incompatible field
836 // types.
837 template <typename MatcherTuple, typename ValueTuple>
838 bool TupleMatches(const MatcherTuple & matcher_tuple,
839  const ValueTuple & value_tuple)
840 {
841  using ::std::tr1::tuple_size;
842  // Makes sure that matcher_tuple and value_tuple have the same
843  // number of fields.
844  GTEST_COMPILE_ASSERT_(tuple_size<MatcherTuple>::value ==
845  tuple_size<ValueTuple>::value,
846  matcher_and_value_have_different_numbers_of_fields);
848  Matches(matcher_tuple, value_tuple);
849 }
850 
851 // Describes failures in matching matchers against values. If there
852 // is no failure, nothing will be streamed to os.
853 template <typename MatcherTuple, typename ValueTuple>
854 void ExplainMatchFailureTupleTo(const MatcherTuple & matchers,
855  const ValueTuple & values,
856  ::std::ostream * os)
857 {
858  using ::std::tr1::tuple_size;
859  TuplePrefix<tuple_size<MatcherTuple>::value>::ExplainMatchFailuresTo(
860  matchers, values, os);
861 }
862 
863 // TransformTupleValues and its helper.
864 //
865 // TransformTupleValuesHelper hides the internal machinery that
866 // TransformTupleValues uses to implement a tuple traversal.
867 template <typename Tuple, typename Func, typename OutIter>
869 {
870 private:
871  typedef typename ::std::tr1::tuple_size<Tuple> TupleSize;
872 
873 public:
874  // For each member of tuple 't', taken in order, evaluates '*out++ = f(t)'.
875  // Returns the final value of 'out' in case the caller needs it.
876  static OutIter Run(Func f, const Tuple & t, OutIter out)
877  {
878  return IterateOverTuple<Tuple, TupleSize::value>()(f, t, out);
879  }
880 
881 private:
882  template <typename Tup, size_t kRemainingSize>
883  struct IterateOverTuple
884  {
885  OutIter operator()(Func f, const Tup & t, OutIter out) const
886  {
887  *out++ = f(::std::tr1::get < TupleSize::value - kRemainingSize > (t));
888  return IterateOverTuple < Tup, kRemainingSize - 1 > ()(f, t, out);
889  }
890  };
891  template <typename Tup>
892  struct IterateOverTuple<Tup, 0>
893  {
894  OutIter operator()(Func /* f */, const Tup & /* t */, OutIter out) const
895  {
896  return out;
897  }
898  };
899 };
900 
901 // Successively invokes 'f(element)' on each element of the tuple 't',
902 // appending each result to the 'out' iterator. Returns the final value
903 // of 'out'.
904 template <typename Tuple, typename Func, typename OutIter>
905 OutIter TransformTupleValues(Func f, const Tuple & t, OutIter out)
906 {
908 }
909 
910 // Implements A<T>().
911 template <typename T>
912 class AnyMatcherImpl : public MatcherInterface<T>
913 {
914 public:
915  virtual bool MatchAndExplain(
916  T /* x */, MatchResultListener * /* listener */) const { return true; }
917  virtual void DescribeTo(::std::ostream * os) const { *os << "is anything"; }
918  virtual void DescribeNegationTo(::std::ostream * os) const
919  {
920  // This is mostly for completeness' safe, as it's not very useful
921  // to write Not(A<bool>()). However we cannot completely rule out
922  // such a possibility, and it doesn't hurt to be prepared.
923  *os << "never matches";
924  }
925 };
926 
927 // Implements _, a matcher that matches any value of any
928 // type. This is a polymorphic matcher, so we need a template type
929 // conversion operator to make it appearing as a Matcher<T> for any
930 // type T.
931 class AnythingMatcher
932 {
933 public:
934  template <typename T>
935  operator Matcher<T>() const { return A<T>(); }
936 };
937 
938 // Implements a matcher that compares a given value with a
939 // pre-supplied value using one of the ==, <=, <, etc, operators. The
940 // two values being compared don't have to have the same type.
941 //
942 // The matcher defined here is polymorphic (for example, Eq(5) can be
943 // used to match an int, a short, a double, etc). Therefore we use
944 // a template type conversion operator in the implementation.
945 //
946 // We define this as a macro in order to eliminate duplicated source
947 // code.
948 //
949 // The following template definition assumes that the Rhs parameter is
950 // a "bare" type (i.e. neither 'const T' nor 'T&').
951 #define GMOCK_IMPLEMENT_COMPARISON_MATCHER_( \
952  name, op, relation, negated_relation) \
953  template <typename Rhs> class name##Matcher { \
954  public: \
955  explicit name##Matcher(const Rhs& rhs) : rhs_(rhs) {} \
956  template <typename Lhs> \
957  operator Matcher<Lhs>() const { \
958  return MakeMatcher(new Impl<Lhs>(rhs_)); \
959  } \
960  private: \
961  template <typename Lhs> \
962  class Impl : public MatcherInterface<Lhs> { \
963  public: \
964  explicit Impl(const Rhs& rhs) : rhs_(rhs) {} \
965  virtual bool MatchAndExplain(\
966  Lhs lhs, MatchResultListener* /* listener */) const { \
967  return lhs op rhs_; \
968  } \
969  virtual void DescribeTo(::std::ostream* os) const { \
970  *os << relation " "; \
971  UniversalPrint(rhs_, os); \
972  } \
973  virtual void DescribeNegationTo(::std::ostream* os) const { \
974  *os << negated_relation " "; \
975  UniversalPrint(rhs_, os); \
976  } \
977  private: \
978  Rhs rhs_; \
979  GTEST_DISALLOW_ASSIGN_(Impl); \
980  }; \
981  Rhs rhs_; \
982  GTEST_DISALLOW_ASSIGN_(name##Matcher); \
983  }
984 
985 // Implements Eq(v), Ge(v), Gt(v), Le(v), Lt(v), and Ne(v)
986 // respectively.
987 GMOCK_IMPLEMENT_COMPARISON_MATCHER_(Eq, ==, "is equal to", "isn't equal to");
988 GMOCK_IMPLEMENT_COMPARISON_MATCHER_(Ge, >=, "is >=", "isn't >=");
989 GMOCK_IMPLEMENT_COMPARISON_MATCHER_(Gt, >, "is >", "isn't >");
990 GMOCK_IMPLEMENT_COMPARISON_MATCHER_(Le, <=, "is <=", "isn't <=");
991 GMOCK_IMPLEMENT_COMPARISON_MATCHER_(Lt, <, "is <", "isn't <");
992 GMOCK_IMPLEMENT_COMPARISON_MATCHER_(Ne, !=, "isn't equal to", "is equal to");
993 
994 #undef GMOCK_IMPLEMENT_COMPARISON_MATCHER_
995 
996 // Implements the polymorphic IsNull() matcher, which matches any raw or smart
997 // pointer that is NULL.
998 class IsNullMatcher
999 {
1000 public:
1001  template <typename Pointer>
1002  bool MatchAndExplain(const Pointer & p,
1003  MatchResultListener * /* listener */) const
1004  {
1005  return GetRawPointer(p) == NULL;
1006  }
1007 
1008  void DescribeTo(::std::ostream * os) const { *os << "is NULL"; }
1009  void DescribeNegationTo(::std::ostream * os) const
1010  {
1011  *os << "isn't NULL";
1012  }
1013 };
1014 
1015 // Implements the polymorphic NotNull() matcher, which matches any raw or smart
1016 // pointer that is not NULL.
1017 class NotNullMatcher
1018 {
1019 public:
1020  template <typename Pointer>
1021  bool MatchAndExplain(const Pointer & p,
1022  MatchResultListener * /* listener */) const
1023  {
1024  return GetRawPointer(p) != NULL;
1025  }
1026 
1027  void DescribeTo(::std::ostream * os) const { *os << "isn't NULL"; }
1028  void DescribeNegationTo(::std::ostream * os) const
1029  {
1030  *os << "is NULL";
1031  }
1032 };
1033 
1034 // Ref(variable) matches any argument that is a reference to
1035 // 'variable'. This matcher is polymorphic as it can match any
1036 // super type of the type of 'variable'.
1037 //
1038 // The RefMatcher template class implements Ref(variable). It can
1039 // only be instantiated with a reference type. This prevents a user
1040 // from mistakenly using Ref(x) to match a non-reference function
1041 // argument. For example, the following will righteously cause a
1042 // compiler error:
1043 //
1044 // int n;
1045 // Matcher<int> m1 = Ref(n); // This won't compile.
1046 // Matcher<int&> m2 = Ref(n); // This will compile.
1047 template <typename T>
1048 class RefMatcher;
1049 
1050 template <typename T>
1051 class RefMatcher<T &>
1052 {
1053  // Google Mock is a generic framework and thus needs to support
1054  // mocking any function types, including those that take non-const
1055  // reference arguments. Therefore the template parameter T (and
1056  // Super below) can be instantiated to either a const type or a
1057  // non-const type.
1058 public:
1059  // RefMatcher() takes a T& instead of const T&, as we want the
1060  // compiler to catch using Ref(const_value) as a matcher for a
1061  // non-const reference.
1062  explicit RefMatcher(T & x) : object_(x) {} // NOLINT
1063 
1064  template <typename Super>
1065  operator Matcher<Super &>() const
1066  {
1067  // By passing object_ (type T&) to Impl(), which expects a Super&,
1068  // we make sure that Super is a super type of T. In particular,
1069  // this catches using Ref(const_value) as a matcher for a
1070  // non-const reference, as you cannot implicitly convert a const
1071  // reference to a non-const reference.
1072  return MakeMatcher(new Impl<Super>(object_));
1073  }
1074 
1075 private:
1076  template <typename Super>
1077  class Impl : public MatcherInterface<Super &>
1078  {
1079  public:
1080  explicit Impl(Super & x) : object_(x) {} // NOLINT
1081 
1082  // MatchAndExplain() takes a Super& (as opposed to const Super&)
1083  // in order to match the interface MatcherInterface<Super&>.
1084  virtual bool MatchAndExplain(
1085  Super & x, MatchResultListener * listener) const
1086  {
1087  *listener << "which is located @" << static_cast<const void *>(&x);
1088  return &x == &object_;
1089  }
1090 
1091  virtual void DescribeTo(::std::ostream * os) const
1092  {
1093  *os << "references the variable ";
1094  UniversalPrinter<Super &>::Print(object_, os);
1095  }
1096 
1097  virtual void DescribeNegationTo(::std::ostream * os) const
1098  {
1099  *os << "does not reference the variable ";
1100  UniversalPrinter<Super &>::Print(object_, os);
1101  }
1102 
1103  private:
1104  const Super & object_;
1105 
1106  GTEST_DISALLOW_ASSIGN_(Impl);
1107  };
1108 
1109  T & object_;
1110 
1112 };
1113 
1114 // Polymorphic helper functions for narrow and wide string matchers.
1115 inline bool CaseInsensitiveCStringEquals(const char * lhs, const char * rhs)
1116 {
1117  return String::CaseInsensitiveCStringEquals(lhs, rhs);
1118 }
1119 
1120 inline bool CaseInsensitiveCStringEquals(const wchar_t * lhs,
1121  const wchar_t * rhs)
1122 {
1123  return String::CaseInsensitiveWideCStringEquals(lhs, rhs);
1124 }
1125 
1126 // String comparison for narrow or wide strings that can have embedded NUL
1127 // characters.
1128 template <typename StringType>
1129 bool CaseInsensitiveStringEquals(const StringType & s1,
1130  const StringType & s2)
1131 {
1132  // Are the heads equal?
1133  if (!CaseInsensitiveCStringEquals(s1.c_str(), s2.c_str()))
1134  {
1135  return false;
1136  }
1137 
1138  // Skip the equal heads.
1139  const typename StringType::value_type nul = 0;
1140  const size_t i1 = s1.find(nul), i2 = s2.find(nul);
1141 
1142  // Are we at the end of either s1 or s2?
1143  if (i1 == StringType::npos || i2 == StringType::npos)
1144  {
1145  return i1 == i2;
1146  }
1147 
1148  // Are the tails equal?
1149  return CaseInsensitiveStringEquals(s1.substr(i1 + 1), s2.substr(i2 + 1));
1150 }
1151 
1152 // String matchers.
1153 
1154 // Implements equality-based string matchers like StrEq, StrCaseNe, and etc.
1155 template <typename StringType>
1156 class StrEqualityMatcher
1157 {
1158 public:
1159  StrEqualityMatcher(const StringType & str, bool expect_eq,
1160  bool case_sensitive)
1161  : string_(str), expect_eq_(expect_eq), case_sensitive_(case_sensitive) {}
1162 
1163  // Accepts pointer types, particularly:
1164  // const char*
1165  // char*
1166  // const wchar_t*
1167  // wchar_t*
1168  template <typename CharType>
1169  bool MatchAndExplain(CharType * s, MatchResultListener * listener) const
1170  {
1171  if (s == NULL)
1172  {
1173  return !expect_eq_;
1174  }
1175 
1176  return MatchAndExplain(StringType(s), listener);
1177  }
1178 
1179  // Matches anything that can convert to StringType.
1180  //
1181  // This is a template, not just a plain function with const StringType&,
1182  // because StringPiece has some interfering non-explicit constructors.
1183  template <typename MatcheeStringType>
1184  bool MatchAndExplain(const MatcheeStringType & s,
1185  MatchResultListener * /* listener */) const
1186  {
1187  const StringType & s2(s);
1188  const bool eq = case_sensitive_ ? s2 == string_ :
1189  CaseInsensitiveStringEquals(s2, string_);
1190  return expect_eq_ == eq;
1191  }
1192 
1193  void DescribeTo(::std::ostream * os) const
1194  {
1195  DescribeToHelper(expect_eq_, os);
1196  }
1197 
1198  void DescribeNegationTo(::std::ostream * os) const
1199  {
1200  DescribeToHelper(!expect_eq_, os);
1201  }
1202 
1203 private:
1204  void DescribeToHelper(bool expect_eq, ::std::ostream * os) const
1205  {
1206  *os << (expect_eq ? "is " : "isn't ");
1207  *os << "equal to ";
1208 
1209  if (!case_sensitive_)
1210  {
1211  *os << "(ignoring case) ";
1212  }
1213 
1214  UniversalPrint(string_, os);
1215  }
1216 
1217  const StringType string_;
1218  const bool expect_eq_;
1219  const bool case_sensitive_;
1220 
1222 };
1223 
1224 // Implements the polymorphic HasSubstr(substring) matcher, which
1225 // can be used as a Matcher<T> as long as T can be converted to a
1226 // string.
1227 template <typename StringType>
1228 class HasSubstrMatcher
1229 {
1230 public:
1231  explicit HasSubstrMatcher(const StringType & substring)
1232  : substring_(substring) {}
1233 
1234  // Accepts pointer types, particularly:
1235  // const char*
1236  // char*
1237  // const wchar_t*
1238  // wchar_t*
1239  template <typename CharType>
1240  bool MatchAndExplain(CharType * s, MatchResultListener * listener) const
1241  {
1242  return s != NULL && MatchAndExplain(StringType(s), listener);
1243  }
1244 
1245  // Matches anything that can convert to StringType.
1246  //
1247  // This is a template, not just a plain function with const StringType&,
1248  // because StringPiece has some interfering non-explicit constructors.
1249  template <typename MatcheeStringType>
1250  bool MatchAndExplain(const MatcheeStringType & s,
1251  MatchResultListener * /* listener */) const
1252  {
1253  const StringType & s2(s);
1254  return s2.find(substring_) != StringType::npos;
1255  }
1256 
1257  // Describes what this matcher matches.
1258  void DescribeTo(::std::ostream * os) const
1259  {
1260  *os << "has substring ";
1261  UniversalPrint(substring_, os);
1262  }
1263 
1264  void DescribeNegationTo(::std::ostream * os) const
1265  {
1266  *os << "has no substring ";
1267  UniversalPrint(substring_, os);
1268  }
1269 
1270 private:
1271  const StringType substring_;
1272 
1274 };
1275 
1276 // Implements the polymorphic StartsWith(substring) matcher, which
1277 // can be used as a Matcher<T> as long as T can be converted to a
1278 // string.
1279 template <typename StringType>
1280 class StartsWithMatcher
1281 {
1282 public:
1283  explicit StartsWithMatcher(const StringType & prefix) : prefix_(prefix)
1284  {
1285  }
1286 
1287  // Accepts pointer types, particularly:
1288  // const char*
1289  // char*
1290  // const wchar_t*
1291  // wchar_t*
1292  template <typename CharType>
1293  bool MatchAndExplain(CharType * s, MatchResultListener * listener) const
1294  {
1295  return s != NULL && MatchAndExplain(StringType(s), listener);
1296  }
1297 
1298  // Matches anything that can convert to StringType.
1299  //
1300  // This is a template, not just a plain function with const StringType&,
1301  // because StringPiece has some interfering non-explicit constructors.
1302  template <typename MatcheeStringType>
1303  bool MatchAndExplain(const MatcheeStringType & s,
1304  MatchResultListener * /* listener */) const
1305  {
1306  const StringType & s2(s);
1307  return s2.length() >= prefix_.length() &&
1308  s2.substr(0, prefix_.length()) == prefix_;
1309  }
1310 
1311  void DescribeTo(::std::ostream * os) const
1312  {
1313  *os << "starts with ";
1314  UniversalPrint(prefix_, os);
1315  }
1316 
1317  void DescribeNegationTo(::std::ostream * os) const
1318  {
1319  *os << "doesn't start with ";
1320  UniversalPrint(prefix_, os);
1321  }
1322 
1323 private:
1324  const StringType prefix_;
1325 
1327 };
1328 
1329 // Implements the polymorphic EndsWith(substring) matcher, which
1330 // can be used as a Matcher<T> as long as T can be converted to a
1331 // string.
1332 template <typename StringType>
1333 class EndsWithMatcher
1334 {
1335 public:
1336  explicit EndsWithMatcher(const StringType & suffix) : suffix_(suffix) {}
1337 
1338  // Accepts pointer types, particularly:
1339  // const char*
1340  // char*
1341  // const wchar_t*
1342  // wchar_t*
1343  template <typename CharType>
1344  bool MatchAndExplain(CharType * s, MatchResultListener * listener) const
1345  {
1346  return s != NULL && MatchAndExplain(StringType(s), listener);
1347  }
1348 
1349  // Matches anything that can convert to StringType.
1350  //
1351  // This is a template, not just a plain function with const StringType&,
1352  // because StringPiece has some interfering non-explicit constructors.
1353  template <typename MatcheeStringType>
1354  bool MatchAndExplain(const MatcheeStringType & s,
1355  MatchResultListener * /* listener */) const
1356  {
1357  const StringType & s2(s);
1358  return s2.length() >= suffix_.length() &&
1359  s2.substr(s2.length() - suffix_.length()) == suffix_;
1360  }
1361 
1362  void DescribeTo(::std::ostream * os) const
1363  {
1364  *os << "ends with ";
1365  UniversalPrint(suffix_, os);
1366  }
1367 
1368  void DescribeNegationTo(::std::ostream * os) const
1369  {
1370  *os << "doesn't end with ";
1371  UniversalPrint(suffix_, os);
1372  }
1373 
1374 private:
1375  const StringType suffix_;
1376 
1378 };
1379 
1380 // Implements polymorphic matchers MatchesRegex(regex) and
1381 // ContainsRegex(regex), which can be used as a Matcher<T> as long as
1382 // T can be converted to a string.
1383 class MatchesRegexMatcher
1384 {
1385 public:
1386  MatchesRegexMatcher(const RE * regex, bool full_match)
1387  : regex_(regex), full_match_(full_match) {}
1388 
1389  // Accepts pointer types, particularly:
1390  // const char*
1391  // char*
1392  // const wchar_t*
1393  // wchar_t*
1394  template <typename CharType>
1395  bool MatchAndExplain(CharType * s, MatchResultListener * listener) const
1396  {
1397  return s != NULL && MatchAndExplain(internal::string(s), listener);
1398  }
1399 
1400  // Matches anything that can convert to internal::string.
1401  //
1402  // This is a template, not just a plain function with const internal::string&,
1403  // because StringPiece has some interfering non-explicit constructors.
1404  template <class MatcheeStringType>
1405  bool MatchAndExplain(const MatcheeStringType & s,
1406  MatchResultListener * /* listener */) const
1407  {
1408  const internal::string & s2(s);
1409  return full_match_ ? RE::FullMatch(s2, *regex_) :
1410  RE::PartialMatch(s2, *regex_);
1411  }
1412 
1413  void DescribeTo(::std::ostream * os) const
1414  {
1415  *os << (full_match_ ? "matches" : "contains")
1416  << " regular expression ";
1417  UniversalPrinter<internal::string>::Print(regex_->pattern(), os);
1418  }
1419 
1420  void DescribeNegationTo(::std::ostream * os) const
1421  {
1422  *os << "doesn't " << (full_match_ ? "match" : "contain")
1423  << " regular expression ";
1424  UniversalPrinter<internal::string>::Print(regex_->pattern(), os);
1425  }
1426 
1427 private:
1428  const internal::linked_ptr<const RE> regex_;
1429  const bool full_match_;
1430 
1432 };
1433 
1434 // Implements a matcher that compares the two fields of a 2-tuple
1435 // using one of the ==, <=, <, etc, operators. The two fields being
1436 // compared don't have to have the same type.
1437 //
1438 // The matcher defined here is polymorphic (for example, Eq() can be
1439 // used to match a tuple<int, short>, a tuple<const long&, double>,
1440 // etc). Therefore we use a template type conversion operator in the
1441 // implementation.
1442 //
1443 // We define this as a macro in order to eliminate duplicated source
1444 // code.
1445 #define GMOCK_IMPLEMENT_COMPARISON2_MATCHER_(name, op, relation) \
1446  class name##2Matcher { \
1447  public: \
1448  template <typename T1, typename T2> \
1449  operator Matcher< ::std::tr1::tuple<T1, T2> >() const { \
1450  return MakeMatcher(new Impl< ::std::tr1::tuple<T1, T2> >); \
1451  } \
1452  template <typename T1, typename T2> \
1453  operator Matcher<const ::std::tr1::tuple<T1, T2>&>() const { \
1454  return MakeMatcher(new Impl<const ::std::tr1::tuple<T1, T2>&>); \
1455  } \
1456  private: \
1457  template <typename Tuple> \
1458  class Impl : public MatcherInterface<Tuple> { \
1459  public: \
1460  virtual bool MatchAndExplain( \
1461  Tuple args, \
1462  MatchResultListener* /* listener */) const { \
1463  return ::std::tr1::get<0>(args) op ::std::tr1::get<1>(args); \
1464  } \
1465  virtual void DescribeTo(::std::ostream* os) const { \
1466  *os << "are " relation; \
1467  } \
1468  virtual void DescribeNegationTo(::std::ostream* os) const { \
1469  *os << "aren't " relation; \
1470  } \
1471  }; \
1472  }
1473 
1474 // Implements Eq(), Ge(), Gt(), Le(), Lt(), and Ne() respectively.
1475 GMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Eq, ==, "an equal pair");
1477  Ge, >=, "a pair where the first >= the second");
1479  Gt, >, "a pair where the first > the second");
1481  Le, <=, "a pair where the first <= the second");
1483  Lt, <, "a pair where the first < the second");
1484 GMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Ne, !=, "an unequal pair");
1485 
1486 #undef GMOCK_IMPLEMENT_COMPARISON2_MATCHER_
1487 
1488 // Implements the Not(...) matcher for a particular argument type T.
1489 // We do not nest it inside the NotMatcher class template, as that
1490 // will prevent different instantiations of NotMatcher from sharing
1491 // the same NotMatcherImpl<T> class.
1492 template <typename T>
1493 class NotMatcherImpl : public MatcherInterface<T>
1494 {
1495 public:
1496  explicit NotMatcherImpl(const Matcher<T> & matcher)
1497  : matcher_(matcher) {}
1498 
1499  virtual bool MatchAndExplain(T x, MatchResultListener * listener) const
1500  {
1501  return !matcher_.MatchAndExplain(x, listener);
1502  }
1503 
1504  virtual void DescribeTo(::std::ostream * os) const
1505  {
1506  matcher_.DescribeNegationTo(os);
1507  }
1508 
1509  virtual void DescribeNegationTo(::std::ostream * os) const
1510  {
1511  matcher_.DescribeTo(os);
1512  }
1513 
1514 private:
1515  const Matcher<T> matcher_;
1516 
1518 };
1519 
1520 // Implements the Not(m) matcher, which matches a value that doesn't
1521 // match matcher m.
1522 template <typename InnerMatcher>
1523 class NotMatcher
1524 {
1525 public:
1526  explicit NotMatcher(InnerMatcher matcher) : matcher_(matcher) {}
1527 
1528  // This template type conversion operator allows Not(m) to be used
1529  // to match any type m can match.
1530  template <typename T>
1531  operator Matcher<T>() const
1532  {
1533  return Matcher<T>(new NotMatcherImpl<T>(SafeMatcherCast<T>(matcher_)));
1534  }
1535 
1536 private:
1537  InnerMatcher matcher_;
1538 
1540 };
1541 
1542 // Implements the AllOf(m1, m2) matcher for a particular argument type
1543 // T. We do not nest it inside the BothOfMatcher class template, as
1544 // that will prevent different instantiations of BothOfMatcher from
1545 // sharing the same BothOfMatcherImpl<T> class.
1546 template <typename T>
1547 class BothOfMatcherImpl : public MatcherInterface<T>
1548 {
1549 public:
1550  BothOfMatcherImpl(const Matcher<T> & matcher1, const Matcher<T> & matcher2)
1551  : matcher1_(matcher1), matcher2_(matcher2) {}
1552 
1553  virtual void DescribeTo(::std::ostream * os) const
1554  {
1555  *os << "(";
1556  matcher1_.DescribeTo(os);
1557  *os << ") and (";
1558  matcher2_.DescribeTo(os);
1559  *os << ")";
1560  }
1561 
1562  virtual void DescribeNegationTo(::std::ostream * os) const
1563  {
1564  *os << "(";
1565  matcher1_.DescribeNegationTo(os);
1566  *os << ") or (";
1567  matcher2_.DescribeNegationTo(os);
1568  *os << ")";
1569  }
1570 
1571  virtual bool MatchAndExplain(T x, MatchResultListener * listener) const
1572  {
1573  // If either matcher1_ or matcher2_ doesn't match x, we only need
1574  // to explain why one of them fails.
1575  StringMatchResultListener listener1;
1576 
1577  if (!matcher1_.MatchAndExplain(x, &listener1))
1578  {
1579  *listener << listener1.str();
1580  return false;
1581  }
1582 
1583  StringMatchResultListener listener2;
1584 
1585  if (!matcher2_.MatchAndExplain(x, &listener2))
1586  {
1587  *listener << listener2.str();
1588  return false;
1589  }
1590 
1591  // Otherwise we need to explain why *both* of them match.
1592  const internal::string s1 = listener1.str();
1593  const internal::string s2 = listener2.str();
1594 
1595  if (s1 == "")
1596  {
1597  *listener << s2;
1598  }
1599 
1600  else
1601  {
1602  *listener << s1;
1603 
1604  if (s2 != "")
1605  {
1606  *listener << ", and " << s2;
1607  }
1608  }
1609 
1610  return true;
1611  }
1612 
1613 private:
1614  const Matcher<T> matcher1_;
1615  const Matcher<T> matcher2_;
1616 
1618 };
1619 
1620 #if GTEST_LANG_CXX11
1621 // MatcherList provides mechanisms for storing a variable number of matchers in
1622 // a list structure (ListType) and creating a combining matcher from such a
1623 // list.
1624 // The template is defined recursively using the following template paramters:
1625 // * kSize is the length of the MatcherList.
1626 // * Head is the type of the first matcher of the list.
1627 // * Tail denotes the types of the remaining matchers of the list.
1628 template <int kSize, typename Head, typename... Tail>
1629 struct MatcherList
1630 {
1631  typedef MatcherList < kSize - 1, Tail... > MatcherListTail;
1632  typedef ::std::pair<Head, typename MatcherListTail::ListType> ListType;
1633 
1634  // BuildList stores variadic type values in a nested pair structure.
1635  // Example:
1636  // MatcherList<3, int, string, float>::BuildList(5, "foo", 2.0) will return
1637  // the corresponding result of type pair<int, pair<string, float>>.
1638  static ListType BuildList(const Head & matcher, const Tail & ... tail)
1639  {
1640  return ListType(matcher, MatcherListTail::BuildList(tail...));
1641  }
1642 
1643  // CreateMatcher<T> creates a Matcher<T> from a given list of matchers (built
1644  // by BuildList()). CombiningMatcher<T> is used to combine the matchers of the
1645  // list. CombiningMatcher<T> must implement MatcherInterface<T> and have a
1646  // constructor taking two Matcher<T>s as input.
1647  template <typename T, template <typename /* T */> class CombiningMatcher>
1648  static Matcher<T> CreateMatcher(const ListType & matchers)
1649  {
1650  return Matcher<T>(new CombiningMatcher<T>(
1651  SafeMatcherCast<T>(matchers.first),
1652  MatcherListTail::template CreateMatcher<T, CombiningMatcher>(
1653  matchers.second)));
1654  }
1655 };
1656 
1657 // The following defines the base case for the recursive definition of
1658 // MatcherList.
1659 template <typename Matcher1, typename Matcher2>
1660 struct MatcherList<2, Matcher1, Matcher2>
1661 {
1662  typedef ::std::pair<Matcher1, Matcher2> ListType;
1663 
1664  static ListType BuildList(const Matcher1 & matcher1,
1665  const Matcher2 & matcher2)
1666  {
1667  return ::std::pair<Matcher1, Matcher2>(matcher1, matcher2);
1668  }
1669 
1670  template <typename T, template <typename /* T */> class CombiningMatcher>
1671  static Matcher<T> CreateMatcher(const ListType & matchers)
1672  {
1673  return Matcher<T>(new CombiningMatcher<T>(
1674  SafeMatcherCast<T>(matchers.first),
1675  SafeMatcherCast<T>(matchers.second)));
1676  }
1677 };
1678 
1679 // VariadicMatcher is used for the variadic implementation of
1680 // AllOf(m_1, m_2, ...) and AnyOf(m_1, m_2, ...).
1681 // CombiningMatcher<T> is used to recursively combine the provided matchers
1682 // (of type Args...).
1683 template <template <typename T> class CombiningMatcher, typename... Args>
1684 class VariadicMatcher
1685 {
1686 public:
1687  VariadicMatcher(const Args & ... matchers) // NOLINT
1688  : matchers_(MatcherListType::BuildList(matchers...)) {}
1689 
1690  // This template type conversion operator allows an
1691  // VariadicMatcher<Matcher1, Matcher2...> object to match any type that
1692  // all of the provided matchers (Matcher1, Matcher2, ...) can match.
1693  template <typename T>
1694  operator Matcher<T>() const
1695  {
1696  return MatcherListType::template CreateMatcher<T, CombiningMatcher>(
1697  matchers_);
1698  }
1699 
1700 private:
1701  typedef MatcherList<sizeof...(Args), Args...> MatcherListType;
1702 
1703  const typename MatcherListType::ListType matchers_;
1704 
1705  GTEST_DISALLOW_ASSIGN_(VariadicMatcher);
1706 };
1707 
1708 template <typename... Args>
1709 using AllOfMatcher = VariadicMatcher<BothOfMatcherImpl, Args...>;
1710 
1711 #endif // GTEST_LANG_CXX11
1712 
1713 // Used for implementing the AllOf(m_1, ..., m_n) matcher, which
1714 // matches a value that matches all of the matchers m_1, ..., and m_n.
1715 template <typename Matcher1, typename Matcher2>
1716 class BothOfMatcher
1717 {
1718 public:
1719  BothOfMatcher(Matcher1 matcher1, Matcher2 matcher2)
1720  : matcher1_(matcher1), matcher2_(matcher2) {}
1721 
1722  // This template type conversion operator allows a
1723  // BothOfMatcher<Matcher1, Matcher2> object to match any type that
1724  // both Matcher1 and Matcher2 can match.
1725  template <typename T>
1726  operator Matcher<T>() const
1727  {
1728  return Matcher<T>(new BothOfMatcherImpl<T>(SafeMatcherCast<T>(matcher1_),
1729  SafeMatcherCast<T>(matcher2_)));
1730  }
1731 
1732 private:
1733  Matcher1 matcher1_;
1734  Matcher2 matcher2_;
1735 
1737 };
1738 
1739 // Implements the AnyOf(m1, m2) matcher for a particular argument type
1740 // T. We do not nest it inside the AnyOfMatcher class template, as
1741 // that will prevent different instantiations of AnyOfMatcher from
1742 // sharing the same EitherOfMatcherImpl<T> class.
1743 template <typename T>
1744 class EitherOfMatcherImpl : public MatcherInterface<T>
1745 {
1746 public:
1747  EitherOfMatcherImpl(const Matcher<T> & matcher1, const Matcher<T> & matcher2)
1748  : matcher1_(matcher1), matcher2_(matcher2) {}
1749 
1750  virtual void DescribeTo(::std::ostream * os) const
1751  {
1752  *os << "(";
1753  matcher1_.DescribeTo(os);
1754  *os << ") or (";
1755  matcher2_.DescribeTo(os);
1756  *os << ")";
1757  }
1758 
1759  virtual void DescribeNegationTo(::std::ostream * os) const
1760  {
1761  *os << "(";
1762  matcher1_.DescribeNegationTo(os);
1763  *os << ") and (";
1764  matcher2_.DescribeNegationTo(os);
1765  *os << ")";
1766  }
1767 
1768  virtual bool MatchAndExplain(T x, MatchResultListener * listener) const
1769  {
1770  // If either matcher1_ or matcher2_ matches x, we just need to
1771  // explain why *one* of them matches.
1772  StringMatchResultListener listener1;
1773 
1774  if (matcher1_.MatchAndExplain(x, &listener1))
1775  {
1776  *listener << listener1.str();
1777  return true;
1778  }
1779 
1780  StringMatchResultListener listener2;
1781 
1782  if (matcher2_.MatchAndExplain(x, &listener2))
1783  {
1784  *listener << listener2.str();
1785  return true;
1786  }
1787 
1788  // Otherwise we need to explain why *both* of them fail.
1789  const internal::string s1 = listener1.str();
1790  const internal::string s2 = listener2.str();
1791 
1792  if (s1 == "")
1793  {
1794  *listener << s2;
1795  }
1796 
1797  else
1798  {
1799  *listener << s1;
1800 
1801  if (s2 != "")
1802  {
1803  *listener << ", and " << s2;
1804  }
1805  }
1806 
1807  return false;
1808  }
1809 
1810 private:
1811  const Matcher<T> matcher1_;
1812  const Matcher<T> matcher2_;
1813 
1815 };
1816 
1817 #if GTEST_LANG_CXX11
1818 // AnyOfMatcher is used for the variadic implementation of AnyOf(m_1, m_2, ...).
1819 template <typename... Args>
1820 using AnyOfMatcher = VariadicMatcher<EitherOfMatcherImpl, Args...>;
1821 
1822 #endif // GTEST_LANG_CXX11
1823 
1824 // Used for implementing the AnyOf(m_1, ..., m_n) matcher, which
1825 // matches a value that matches at least one of the matchers m_1, ...,
1826 // and m_n.
1827 template <typename Matcher1, typename Matcher2>
1828 class EitherOfMatcher
1829 {
1830 public:
1831  EitherOfMatcher(Matcher1 matcher1, Matcher2 matcher2)
1832  : matcher1_(matcher1), matcher2_(matcher2) {}
1833 
1834  // This template type conversion operator allows a
1835  // EitherOfMatcher<Matcher1, Matcher2> object to match any type that
1836  // both Matcher1 and Matcher2 can match.
1837  template <typename T>
1838  operator Matcher<T>() const
1839  {
1841  SafeMatcherCast<T>(matcher1_), SafeMatcherCast<T>(matcher2_)));
1842  }
1843 
1844 private:
1845  Matcher1 matcher1_;
1846  Matcher2 matcher2_;
1847 
1849 };
1850 
1851 // Used for implementing Truly(pred), which turns a predicate into a
1852 // matcher.
1853 template <typename Predicate>
1854 class TrulyMatcher
1855 {
1856 public:
1857  explicit TrulyMatcher(Predicate pred) : predicate_(pred) {}
1858 
1859  // This method template allows Truly(pred) to be used as a matcher
1860  // for type T where T is the argument type of predicate 'pred'. The
1861  // argument is passed by reference as the predicate may be
1862  // interested in the address of the argument.
1863  template <typename T>
1864  bool MatchAndExplain(T & x, // NOLINT
1865  MatchResultListener * /* listener */) const
1866  {
1867  // Without the if-statement, MSVC sometimes warns about converting
1868  // a value to bool (warning 4800).
1869  //
1870  // We cannot write 'return !!predicate_(x);' as that doesn't work
1871  // when predicate_(x) returns a class convertible to bool but
1872  // having no operator!().
1873  if (predicate_(x))
1874  { return true; }
1875 
1876  return false;
1877  }
1878 
1879  void DescribeTo(::std::ostream * os) const
1880  {
1881  *os << "satisfies the given predicate";
1882  }
1883 
1884  void DescribeNegationTo(::std::ostream * os) const
1885  {
1886  *os << "doesn't satisfy the given predicate";
1887  }
1888 
1889 private:
1890  Predicate predicate_;
1891 
1893 };
1894 
1895 // Used for implementing Matches(matcher), which turns a matcher into
1896 // a predicate.
1897 template <typename M>
1898 class MatcherAsPredicate
1899 {
1900 public:
1901  explicit MatcherAsPredicate(M matcher) : matcher_(matcher) {}
1902 
1903  // This template operator() allows Matches(m) to be used as a
1904  // predicate on type T where m is a matcher on type T.
1905  //
1906  // The argument x is passed by reference instead of by value, as
1907  // some matcher may be interested in its address (e.g. as in
1908  // Matches(Ref(n))(x)).
1909  template <typename T>
1910  bool operator()(const T & x) const
1911  {
1912  // We let matcher_ commit to a particular type here instead of
1913  // when the MatcherAsPredicate object was constructed. This
1914  // allows us to write Matches(m) where m is a polymorphic matcher
1915  // (e.g. Eq(5)).
1916  //
1917  // If we write Matcher<T>(matcher_).Matches(x) here, it won't
1918  // compile when matcher_ has type Matcher<const T&>; if we write
1919  // Matcher<const T&>(matcher_).Matches(x) here, it won't compile
1920  // when matcher_ has type Matcher<T>; if we just write
1921  // matcher_.Matches(x), it won't compile when matcher_ is
1922  // polymorphic, e.g. Eq(5).
1923  //
1924  // MatcherCast<const T&>() is necessary for making the code work
1925  // in all of the above situations.
1926  return MatcherCast<const T &>(matcher_).Matches(x);
1927  }
1928 
1929 private:
1930  M matcher_;
1931 
1933 };
1934 
1935 // For implementing ASSERT_THAT() and EXPECT_THAT(). The template
1936 // argument M must be a type that can be converted to a matcher.
1937 template <typename M>
1939 {
1940 public:
1941  explicit PredicateFormatterFromMatcher(const M & m) : matcher_(m) {}
1942 
1943  // This template () operator allows a PredicateFormatterFromMatcher
1944  // object to act as a predicate-formatter suitable for using with
1945  // Google Test's EXPECT_PRED_FORMAT1() macro.
1946  template <typename T>
1947  AssertionResult operator()(const char * value_text, const T & x) const
1948  {
1949  // We convert matcher_ to a Matcher<const T&> *now* instead of
1950  // when the PredicateFormatterFromMatcher object was constructed,
1951  // as matcher_ may be polymorphic (e.g. NotNull()) and we won't
1952  // know which type to instantiate it to until we actually see the
1953  // type of x here.
1954  //
1955  // We write SafeMatcherCast<const T&>(matcher_) instead of
1956  // Matcher<const T&>(matcher_), as the latter won't compile when
1957  // matcher_ has type Matcher<T> (e.g. An<int>()).
1958  // We don't write MatcherCast<const T&> either, as that allows
1959  // potentially unsafe downcasting of the matcher argument.
1960  const Matcher<const T &> matcher = SafeMatcherCast<const T &>(matcher_);
1961  StringMatchResultListener listener;
1962 
1963  if (MatchPrintAndExplain(x, matcher, &listener))
1964  { return AssertionSuccess(); }
1965 
1966  ::std::stringstream ss;
1967  ss << "Value of: " << value_text << "\n"
1968  << "Expected: ";
1969  matcher.DescribeTo(&ss);
1970  ss << "\n Actual: " << listener.str();
1971  return AssertionFailure() << ss.str();
1972  }
1973 
1974 private:
1975  const M matcher_;
1976 
1978 };
1979 
1980 // A helper function for converting a matcher to a predicate-formatter
1981 // without the user needing to explicitly write the type. This is
1982 // used for implementing ASSERT_THAT() and EXPECT_THAT().
1983 template <typename M>
1985 MakePredicateFormatterFromMatcher(const M & matcher)
1986 {
1987  return PredicateFormatterFromMatcher<M>(matcher);
1988 }
1989 
1990 // Implements the polymorphic floating point equality matcher, which matches
1991 // two float values using ULP-based approximation or, optionally, a
1992 // user-specified epsilon. The template is meant to be instantiated with
1993 // FloatType being either float or double.
1994 template <typename FloatType>
1995 class FloatingEqMatcher
1996 {
1997 public:
1998  // Constructor for FloatingEqMatcher.
1999  // The matcher's input will be compared with rhs. The matcher treats two
2000  // NANs as equal if nan_eq_nan is true. Otherwise, under IEEE standards,
2001  // equality comparisons between NANs will always return false. We specify a
2002  // negative max_abs_error_ term to indicate that ULP-based approximation will
2003  // be used for comparison.
2004  FloatingEqMatcher(FloatType rhs, bool nan_eq_nan) :
2005  rhs_(rhs), nan_eq_nan_(nan_eq_nan), max_abs_error_(-1)
2006  {
2007  }
2008 
2009  // Constructor that supports a user-specified max_abs_error that will be used
2010  // for comparison instead of ULP-based approximation. The max absolute
2011  // should be non-negative.
2012  FloatingEqMatcher(FloatType rhs, bool nan_eq_nan, FloatType max_abs_error) :
2013  rhs_(rhs), nan_eq_nan_(nan_eq_nan), max_abs_error_(max_abs_error)
2014  {
2015  GTEST_CHECK_(max_abs_error >= 0)
2016  << ", where max_abs_error is" << max_abs_error;
2017  }
2018 
2019  // Implements floating point equality matcher as a Matcher<T>.
2020  template <typename T>
2021  class Impl : public MatcherInterface<T>
2022  {
2023  public:
2024  Impl(FloatType rhs, bool nan_eq_nan, FloatType max_abs_error) :
2025  rhs_(rhs), nan_eq_nan_(nan_eq_nan), max_abs_error_(max_abs_error) {}
2026 
2027  virtual bool MatchAndExplain(T value,
2028  MatchResultListener * /* listener */) const
2029  {
2030  const FloatingPoint<FloatType> lhs(value), rhs(rhs_);
2031 
2032  // Compares NaNs first, if nan_eq_nan_ is true.
2033  if (lhs.is_nan() || rhs.is_nan())
2034  {
2035  if (lhs.is_nan() && rhs.is_nan())
2036  {
2037  return nan_eq_nan_;
2038  }
2039 
2040  // One is nan; the other is not nan.
2041  return false;
2042  }
2043 
2044  if (HasMaxAbsError())
2045  {
2046  // We perform an equality check so that inf will match inf, regardless
2047  // of error bounds. If the result of value - rhs_ would result in
2048  // overflow or if either value is inf, the default result is infinity,
2049  // which should only match if max_abs_error_ is also infinity.
2050  return value == rhs_ || fabs(value - rhs_) <= max_abs_error_;
2051  }
2052 
2053  else
2054  {
2055  return lhs.AlmostEquals(rhs);
2056  }
2057  }
2058 
2059  virtual void DescribeTo(::std::ostream * os) const
2060  {
2061  // os->precision() returns the previously set precision, which we
2062  // store to restore the ostream to its original configuration
2063  // after outputting.
2064  const ::std::streamsize old_precision = os->precision(
2065  ::std::numeric_limits<FloatType>::digits10 + 2);
2066 
2067  if (FloatingPoint<FloatType>(rhs_).is_nan())
2068  {
2069  if (nan_eq_nan_)
2070  {
2071  *os << "is NaN";
2072  }
2073 
2074  else
2075  {
2076  *os << "never matches";
2077  }
2078  }
2079 
2080  else
2081  {
2082  *os << "is approximately " << rhs_;
2083 
2084  if (HasMaxAbsError())
2085  {
2086  *os << " (absolute error <= " << max_abs_error_ << ")";
2087  }
2088  }
2089 
2090  os->precision(old_precision);
2091  }
2092 
2093  virtual void DescribeNegationTo(::std::ostream * os) const
2094  {
2095  // As before, get original precision.
2096  const ::std::streamsize old_precision = os->precision(
2097  ::std::numeric_limits<FloatType>::digits10 + 2);
2098 
2099  if (FloatingPoint<FloatType>(rhs_).is_nan())
2100  {
2101  if (nan_eq_nan_)
2102  {
2103  *os << "isn't NaN";
2104  }
2105 
2106  else
2107  {
2108  *os << "is anything";
2109  }
2110  }
2111 
2112  else
2113  {
2114  *os << "isn't approximately " << rhs_;
2115 
2116  if (HasMaxAbsError())
2117  {
2118  *os << " (absolute error > " << max_abs_error_ << ")";
2119  }
2120  }
2121 
2122  // Restore original precision.
2123  os->precision(old_precision);
2124  }
2125 
2126  private:
2127  bool HasMaxAbsError() const
2128  {
2129  return max_abs_error_ >= 0;
2130  }
2131 
2132  const FloatType rhs_;
2133  const bool nan_eq_nan_;
2134  // max_abs_error will be used for value comparison when >= 0.
2135  const FloatType max_abs_error_;
2136 
2138  };
2139 
2140  // The following 3 type conversion operators allow FloatEq(rhs) and
2141  // NanSensitiveFloatEq(rhs) to be used as a Matcher<float>, a
2142  // Matcher<const float&>, or a Matcher<float&>, but nothing else.
2143  // (While Google's C++ coding style doesn't allow arguments passed
2144  // by non-const reference, we may see them in code not conforming to
2145  // the style. Therefore Google Mock needs to support them.)
2146  operator Matcher<FloatType>() const
2147  {
2148  return MakeMatcher(new Impl<FloatType>(rhs_, nan_eq_nan_, max_abs_error_));
2149  }
2150 
2152  {
2153  return MakeMatcher(
2154  new Impl<const FloatType &>(rhs_, nan_eq_nan_, max_abs_error_));
2155  }
2156 
2157  operator Matcher<FloatType &>() const
2158  {
2159  return MakeMatcher(new Impl<FloatType &>(rhs_, nan_eq_nan_, max_abs_error_));
2160  }
2161 
2162 private:
2163  const FloatType rhs_;
2164  const bool nan_eq_nan_;
2165  // max_abs_error will be used for value comparison when >= 0.
2166  const FloatType max_abs_error_;
2167 
2169 };
2170 
2171 // Implements the Pointee(m) matcher for matching a pointer whose
2172 // pointee matches matcher m. The pointer can be either raw or smart.
2173 template <typename InnerMatcher>
2174 class PointeeMatcher
2175 {
2176 public:
2177  explicit PointeeMatcher(const InnerMatcher & matcher) : matcher_(matcher) {}
2178 
2179  // This type conversion operator template allows Pointee(m) to be
2180  // used as a matcher for any pointer type whose pointee type is
2181  // compatible with the inner matcher, where type Pointer can be
2182  // either a raw pointer or a smart pointer.
2183  //
2184  // The reason we do this instead of relying on
2185  // MakePolymorphicMatcher() is that the latter is not flexible
2186  // enough for implementing the DescribeTo() method of Pointee().
2187  template <typename Pointer>
2188  operator Matcher<Pointer>() const
2189  {
2190  return MakeMatcher(new Impl<Pointer>(matcher_));
2191  }
2192 
2193 private:
2194  // The monomorphic implementation that works for a particular pointer type.
2195  template <typename Pointer>
2196  class Impl : public MatcherInterface<Pointer>
2197  {
2198  public:
2199  typedef typename PointeeOf<GTEST_REMOVE_CONST_( // NOLINT
2201 
2202  explicit Impl(const InnerMatcher & matcher)
2203  : matcher_(MatcherCast<const Pointee & >(matcher)) {}
2204 
2205  virtual void DescribeTo(::std::ostream * os) const
2206  {
2207  *os << "points to a value that ";
2208  matcher_.DescribeTo(os);
2209  }
2210 
2211  virtual void DescribeNegationTo(::std::ostream * os) const
2212  {
2213  *os << "does not point to a value that ";
2214  matcher_.DescribeTo(os);
2215  }
2216 
2217  virtual bool MatchAndExplain(Pointer pointer,
2218  MatchResultListener * listener) const
2219  {
2220  if (GetRawPointer(pointer) == NULL)
2221  { return false; }
2222 
2223  *listener << "which points to ";
2224  return MatchPrintAndExplain(*pointer, matcher_, listener);
2225  }
2226 
2227  private:
2228  const Matcher<const Pointee &> matcher_;
2229 
2231  };
2232 
2233  const InnerMatcher matcher_;
2234 
2236 };
2237 
2238 // Implements the Field() matcher for matching a field (i.e. member
2239 // variable) of an object.
2240 template <typename Class, typename FieldType>
2241 class FieldMatcher
2242 {
2243 public:
2244  FieldMatcher(FieldType Class::*field,
2245  const Matcher<const FieldType &> & matcher)
2246  : field_(field), matcher_(matcher) {}
2247 
2248  void DescribeTo(::std::ostream * os) const
2249  {
2250  *os << "is an object whose given field ";
2251  matcher_.DescribeTo(os);
2252  }
2253 
2254  void DescribeNegationTo(::std::ostream * os) const
2255  {
2256  *os << "is an object whose given field ";
2257  matcher_.DescribeNegationTo(os);
2258  }
2259 
2260  template <typename T>
2261  bool MatchAndExplain(const T & value, MatchResultListener * listener) const
2262  {
2263  return MatchAndExplainImpl(
2264  typename ::testing::internal::
2265  is_pointer<GTEST_REMOVE_CONST_(T)>::type(),
2266  value, listener);
2267  }
2268 
2269 private:
2270  // The first argument of MatchAndExplainImpl() is needed to help
2271  // Symbian's C++ compiler choose which overload to use. Its type is
2272  // true_type iff the Field() matcher is used to match a pointer.
2273  bool MatchAndExplainImpl(false_type /* is_not_pointer */, const Class & obj,
2274  MatchResultListener * listener) const
2275  {
2276  *listener << "whose given field is ";
2277  return MatchPrintAndExplain(obj.*field_, matcher_, listener);
2278  }
2279 
2280  bool MatchAndExplainImpl(true_type /* is_pointer */, const Class * p,
2281  MatchResultListener * listener) const
2282  {
2283  if (p == NULL)
2284  { return false; }
2285 
2286  *listener << "which points to an object ";
2287  // Since *p has a field, it must be a class/struct/union type and
2288  // thus cannot be a pointer. Therefore we pass false_type() as
2289  // the first argument.
2290  return MatchAndExplainImpl(false_type(), *p, listener);
2291  }
2292 
2293  const FieldType Class::* field_;
2294  const Matcher<const FieldType &> matcher_;
2295 
2297 };
2298 
2299 // Implements the Property() matcher for matching a property
2300 // (i.e. return value of a getter method) of an object.
2301 template <typename Class, typename PropertyType>
2302 class PropertyMatcher
2303 {
2304 public:
2305  // The property may have a reference type, so 'const PropertyType&'
2306  // may cause double references and fail to compile. That's why we
2307  // need GTEST_REFERENCE_TO_CONST, which works regardless of
2308  // PropertyType being a reference or not.
2309  typedef GTEST_REFERENCE_TO_CONST_(PropertyType) RefToConstProperty;
2310 
2311  PropertyMatcher(PropertyType(Class::*property)() const,
2312  const Matcher<RefToConstProperty> & matcher)
2313  : property_(property), matcher_(matcher) {}
2314 
2315  void DescribeTo(::std::ostream * os) const
2316  {
2317  *os << "is an object whose given property ";
2318  matcher_.DescribeTo(os);
2319  }
2320 
2321  void DescribeNegationTo(::std::ostream * os) const
2322  {
2323  *os << "is an object whose given property ";
2324  matcher_.DescribeNegationTo(os);
2325  }
2326 
2327  template <typename T>
2328  bool MatchAndExplain(const T & value, MatchResultListener * listener) const
2329  {
2330  return MatchAndExplainImpl(
2331  typename ::testing::internal::
2332  is_pointer<GTEST_REMOVE_CONST_(T)>::type(),
2333  value, listener);
2334  }
2335 
2336 private:
2337  // The first argument of MatchAndExplainImpl() is needed to help
2338  // Symbian's C++ compiler choose which overload to use. Its type is
2339  // true_type iff the Property() matcher is used to match a pointer.
2340  bool MatchAndExplainImpl(false_type /* is_not_pointer */, const Class & obj,
2341  MatchResultListener * listener) const
2342  {
2343  *listener << "whose given property is ";
2344  // Cannot pass the return value (for example, int) to MatchPrintAndExplain,
2345  // which takes a non-const reference as argument.
2346  RefToConstProperty result = (obj.*property_)();
2347  return MatchPrintAndExplain(result, matcher_, listener);
2348  }
2349 
2350  bool MatchAndExplainImpl(true_type /* is_pointer */, const Class * p,
2351  MatchResultListener * listener) const
2352  {
2353  if (p == NULL)
2354  { return false; }
2355 
2356  *listener << "which points to an object ";
2357  // Since *p has a property method, it must be a class/struct/union
2358  // type and thus cannot be a pointer. Therefore we pass
2359  // false_type() as the first argument.
2360  return MatchAndExplainImpl(false_type(), *p, listener);
2361  }
2362 
2363  PropertyType(Class::*property_)() const;
2364  const Matcher<RefToConstProperty> matcher_;
2365 
2367 };
2368 
2369 // Type traits specifying various features of different functors for ResultOf.
2370 // The default template specifies features for functor objects.
2371 // Functor classes have to typedef argument_type and result_type
2372 // to be compatible with ResultOf.
2373 template <typename Functor>
2374 struct CallableTraits
2375 {
2376  typedef typename Functor::result_type ResultType;
2377  typedef Functor StorageType;
2378 
2379  static void CheckIsValid(Functor /* functor */) {}
2380  template <typename T>
2381  static ResultType Invoke(Functor f, T arg) { return f(arg); }
2382 };
2383 
2384 // Specialization for function pointers.
2385 template <typename ArgType, typename ResType>
2386 struct CallableTraits<ResType(*)(ArgType)>
2387 {
2388  typedef ResType ResultType;
2389  typedef ResType(*StorageType)(ArgType);
2390 
2391  static void CheckIsValid(ResType(*f)(ArgType))
2392 {
2393  GTEST_CHECK_(f != NULL)
2394  << "NULL function pointer is passed into ResultOf().";
2395 }
2396 template <typename T>
2397 static ResType Invoke(ResType(*f)(ArgType), T arg)
2398 {
2399  return (*f)(arg);
2400 }
2401  };
2402 
2403 // Implements the ResultOf() matcher for matching a return value of a
2404 // unary function of an object.
2405 template <typename Callable>
2406 class ResultOfMatcher
2407 {
2408 public:
2410 
2411  ResultOfMatcher(Callable callable, const Matcher<ResultType> & matcher)
2412  : callable_(callable), matcher_(matcher)
2413  {
2415  }
2416 
2417  template <typename T>
2418  operator Matcher<T>() const
2419  {
2420  return Matcher<T>(new Impl<T>(callable_, matcher_));
2421  }
2422 
2423 private:
2425 
2426  template <typename T>
2427  class Impl : public MatcherInterface<T>
2428  {
2429  public:
2430  Impl(CallableStorageType callable, const Matcher<ResultType> & matcher)
2431  : callable_(callable), matcher_(matcher) {}
2432 
2433  virtual void DescribeTo(::std::ostream * os) const
2434  {
2435  *os << "is mapped by the given callable to a value that ";
2436  matcher_.DescribeTo(os);
2437  }
2438 
2439  virtual void DescribeNegationTo(::std::ostream * os) const
2440  {
2441  *os << "is mapped by the given callable to a value that ";
2442  matcher_.DescribeNegationTo(os);
2443  }
2444 
2445  virtual bool MatchAndExplain(T obj, MatchResultListener * listener) const
2446  {
2447  *listener << "which is mapped by the given callable to ";
2448  // Cannot pass the return value (for example, int) to
2449  // MatchPrintAndExplain, which takes a non-const reference as argument.
2450  ResultType result =
2451  CallableTraits<Callable>::template Invoke<T>(callable_, obj);
2452  return MatchPrintAndExplain(result, matcher_, listener);
2453  }
2454 
2455  private:
2456  // Functors often define operator() as non-const method even though
2457  // they are actualy stateless. But we need to use them even when
2458  // 'this' is a const pointer. It's the user's responsibility not to
2459  // use stateful callables with ResultOf(), which does't guarantee
2460  // how many times the callable will be invoked.
2461  mutable CallableStorageType callable_;
2462  const Matcher<ResultType> matcher_;
2463 
2465  }; // class Impl
2466 
2467  const CallableStorageType callable_;
2468  const Matcher<ResultType> matcher_;
2469 
2471 };
2472 
2473 // Implements a matcher that checks the size of an STL-style container.
2474 template <typename SizeMatcher>
2475 class SizeIsMatcher
2476 {
2477 public:
2478  explicit SizeIsMatcher(const SizeMatcher & size_matcher)
2479  : size_matcher_(size_matcher)
2480  {
2481  }
2482 
2483  template <typename Container>
2484  operator Matcher<Container>() const
2485  {
2486  return MakeMatcher(new Impl<Container>(size_matcher_));
2487  }
2488 
2489  template <typename Container>
2490  class Impl : public MatcherInterface<Container>
2491  {
2492  public:
2493  typedef internal::StlContainerView <
2495  typedef typename ContainerView::type::size_type SizeType;
2496  explicit Impl(const SizeMatcher & size_matcher)
2497  : size_matcher_(MatcherCast<SizeType>(size_matcher)) {}
2498 
2499  virtual void DescribeTo(::std::ostream * os) const
2500  {
2501  *os << "size ";
2502  size_matcher_.DescribeTo(os);
2503  }
2504  virtual void DescribeNegationTo(::std::ostream * os) const
2505  {
2506  *os << "size ";
2507  size_matcher_.DescribeNegationTo(os);
2508  }
2509 
2510  virtual bool MatchAndExplain(Container container,
2511  MatchResultListener * listener) const
2512  {
2513  SizeType size = container.size();
2514  StringMatchResultListener size_listener;
2515  const bool result = size_matcher_.MatchAndExplain(size, &size_listener);
2516  *listener
2517  << "whose size " << size << (result ? " matches" : " doesn't match");
2518  PrintIfNotEmpty(size_listener.str(), listener->stream());
2519  return result;
2520  }
2521 
2522  private:
2523  const Matcher<SizeType> size_matcher_;
2525  };
2526 
2527 private:
2528  const SizeMatcher size_matcher_;
2530 };
2531 
2532 // Implements an equality matcher for any STL-style container whose elements
2533 // support ==. This matcher is like Eq(), but its failure explanations provide
2534 // more detailed information that is useful when the container is used as a set.
2535 // The failure message reports elements that are in one of the operands but not
2536 // the other. The failure messages do not report duplicate or out-of-order
2537 // elements in the containers (which don't properly matter to sets, but can
2538 // occur if the containers are vectors or lists, for example).
2539 //
2540 // Uses the container's const_iterator, value_type, operator ==,
2541 // begin(), and end().
2542 template <typename Container>
2543 class ContainerEqMatcher
2544 {
2545 public:
2547  typedef typename View::type StlContainer;
2549 
2550  // We make a copy of rhs in case the elements in it are modified
2551  // after this matcher is created.
2552  explicit ContainerEqMatcher(const Container & rhs) : rhs_(View::Copy(rhs))
2553  {
2554  // Makes sure the user doesn't instantiate this class template
2555  // with a const or reference type.
2556  (void)testing::StaticAssertTypeEq<Container,
2557  GTEST_REMOVE_REFERENCE_AND_CONST_(Container)>();
2558  }
2559 
2560  void DescribeTo(::std::ostream * os) const
2561  {
2562  *os << "equals ";
2563  UniversalPrint(rhs_, os);
2564  }
2565  void DescribeNegationTo(::std::ostream * os) const
2566  {
2567  *os << "does not equal ";
2568  UniversalPrint(rhs_, os);
2569  }
2570 
2571  template <typename LhsContainer>
2572  bool MatchAndExplain(const LhsContainer & lhs,
2573  MatchResultListener * listener) const
2574  {
2575  // GTEST_REMOVE_CONST_() is needed to work around an MSVC 8.0 bug
2576  // that causes LhsContainer to be a const type sometimes.
2578  LhsView;
2579  typedef typename LhsView::type LhsStlContainer;
2580  StlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2581 
2582  if (lhs_stl_container == rhs_)
2583  { return true; }
2584 
2585  ::std::ostream * const os = listener->stream();
2586 
2587  if (os != NULL)
2588  {
2589  // Something is different. Check for extra values first.
2590  bool printed_header = false;
2591 
2592  for (typename LhsStlContainer::const_iterator it =
2593  lhs_stl_container.begin();
2594  it != lhs_stl_container.end(); ++it)
2595  {
2596  if (internal::ArrayAwareFind(rhs_.begin(), rhs_.end(), *it) ==
2597  rhs_.end())
2598  {
2599  if (printed_header)
2600  {
2601  *os << ", ";
2602  }
2603 
2604  else
2605  {
2606  *os << "which has these unexpected elements: ";
2607  printed_header = true;
2608  }
2609 
2610  UniversalPrint(*it, os);
2611  }
2612  }
2613 
2614  // Now check for missing values.
2615  bool printed_header2 = false;
2616 
2617  for (typename StlContainer::const_iterator it = rhs_.begin();
2618  it != rhs_.end(); ++it)
2619  {
2621  lhs_stl_container.begin(), lhs_stl_container.end(), *it) ==
2622  lhs_stl_container.end())
2623  {
2624  if (printed_header2)
2625  {
2626  *os << ", ";
2627  }
2628 
2629  else
2630  {
2631  *os << (printed_header ? ",\nand" : "which")
2632  << " doesn't have these expected elements: ";
2633  printed_header2 = true;
2634  }
2635 
2636  UniversalPrint(*it, os);
2637  }
2638  }
2639  }
2640 
2641  return false;
2642  }
2643 
2644 private:
2645  const StlContainer rhs_;
2646 
2648 };
2649 
2650 // A comparator functor that uses the < operator to compare two values.
2651 struct LessComparator
2652 {
2653  template <typename T, typename U>
2654  bool operator()(const T & lhs, const U & rhs) const { return lhs < rhs; }
2655 };
2656 
2657 // Implements WhenSortedBy(comparator, container_matcher).
2658 template <typename Comparator, typename ContainerMatcher>
2659 class WhenSortedByMatcher
2660 {
2661 public:
2662  WhenSortedByMatcher(const Comparator & comparator,
2663  const ContainerMatcher & matcher)
2664  : comparator_(comparator), matcher_(matcher) {}
2665 
2666  template <typename LhsContainer>
2667  operator Matcher<LhsContainer>() const
2668  {
2669  return MakeMatcher(new Impl<LhsContainer>(comparator_, matcher_));
2670  }
2671 
2672  template <typename LhsContainer>
2673  class Impl : public MatcherInterface<LhsContainer>
2674  {
2675  public:
2676  typedef internal::StlContainerView <
2678  typedef typename LhsView::type LhsStlContainer;
2680  // Transforms std::pair<const Key, Value> into std::pair<Key, Value>
2681  // so that we can match associative containers.
2682  typedef typename RemoveConstFromKey <
2683  typename LhsStlContainer::value_type >::type LhsValue;
2684 
2685  Impl(const Comparator & comparator, const ContainerMatcher & matcher)
2686  : comparator_(comparator), matcher_(matcher) {}
2687 
2688  virtual void DescribeTo(::std::ostream * os) const
2689  {
2690  *os << "(when sorted) ";
2691  matcher_.DescribeTo(os);
2692  }
2693 
2694  virtual void DescribeNegationTo(::std::ostream * os) const
2695  {
2696  *os << "(when sorted) ";
2697  matcher_.DescribeNegationTo(os);
2698  }
2699 
2700  virtual bool MatchAndExplain(LhsContainer lhs,
2701  MatchResultListener * listener) const
2702  {
2703  LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2704  ::std::vector<LhsValue> sorted_container(lhs_stl_container.begin(),
2705  lhs_stl_container.end());
2706  ::std::sort(
2707  sorted_container.begin(), sorted_container.end(), comparator_);
2708 
2709  if (!listener->IsInterested())
2710  {
2711  // If the listener is not interested, we do not need to
2712  // construct the inner explanation.
2713  return matcher_.Matches(sorted_container);
2714  }
2715 
2716  *listener << "which is ";
2717  UniversalPrint(sorted_container, listener->stream());
2718  *listener << " when sorted";
2719 
2720  StringMatchResultListener inner_listener;
2721  const bool match = matcher_.MatchAndExplain(sorted_container,
2722  &inner_listener);
2723  PrintIfNotEmpty(inner_listener.str(), listener->stream());
2724  return match;
2725  }
2726 
2727  private:
2728  const Comparator comparator_;
2729  const Matcher<const ::std::vector<LhsValue>&> matcher_;
2730 
2732  };
2733 
2734 private:
2735  const Comparator comparator_;
2736  const ContainerMatcher matcher_;
2737 
2739 };
2740 
2741 // Implements Pointwise(tuple_matcher, rhs_container). tuple_matcher
2742 // must be able to be safely cast to Matcher<tuple<const T1&, const
2743 // T2&> >, where T1 and T2 are the types of elements in the LHS
2744 // container and the RHS container respectively.
2745 template <typename TupleMatcher, typename RhsContainer>
2746 class PointwiseMatcher
2747 {
2748 public:
2750  typedef typename RhsView::type RhsStlContainer;
2751  typedef typename RhsStlContainer::value_type RhsValue;
2752 
2753  // Like ContainerEq, we make a copy of rhs in case the elements in
2754  // it are modified after this matcher is created.
2755  PointwiseMatcher(const TupleMatcher & tuple_matcher, const RhsContainer & rhs)
2756  : tuple_matcher_(tuple_matcher), rhs_(RhsView::Copy(rhs))
2757  {
2758  // Makes sure the user doesn't instantiate this class template
2759  // with a const or reference type.
2760  (void)testing::StaticAssertTypeEq<RhsContainer,
2761  GTEST_REMOVE_REFERENCE_AND_CONST_(RhsContainer)>();
2762  }
2763 
2764  template <typename LhsContainer>
2765  operator Matcher<LhsContainer>() const
2766  {
2767  return MakeMatcher(new Impl<LhsContainer>(tuple_matcher_, rhs_));
2768  }
2769 
2770  template <typename LhsContainer>
2771  class Impl : public MatcherInterface<LhsContainer>
2772  {
2773  public:
2774  typedef internal::StlContainerView <
2776  typedef typename LhsView::type LhsStlContainer;
2778  typedef typename LhsStlContainer::value_type LhsValue;
2779  // We pass the LHS value and the RHS value to the inner matcher by
2780  // reference, as they may be expensive to copy. We must use tuple
2781  // instead of pair here, as a pair cannot hold references (C++ 98,
2782  // 20.2.2 [lib.pairs]).
2783  typedef ::std::tr1::tuple<const LhsValue &, const RhsValue &> InnerMatcherArg;
2784 
2785  Impl(const TupleMatcher & tuple_matcher, const RhsStlContainer & rhs)
2786  // mono_tuple_matcher_ holds a monomorphic version of the tuple matcher.
2787  : mono_tuple_matcher_(SafeMatcherCast<InnerMatcherArg>(tuple_matcher)),
2788  rhs_(rhs) {}
2789 
2790  virtual void DescribeTo(::std::ostream * os) const
2791  {
2792  *os << "contains " << rhs_.size()
2793  << " values, where each value and its corresponding value in ";
2795  *os << " ";
2796  mono_tuple_matcher_.DescribeTo(os);
2797  }
2798  virtual void DescribeNegationTo(::std::ostream * os) const
2799  {
2800  *os << "doesn't contain exactly " << rhs_.size()
2801  << " values, or contains a value x at some index i"
2802  << " where x and the i-th value of ";
2803  UniversalPrint(rhs_, os);
2804  *os << " ";
2805  mono_tuple_matcher_.DescribeNegationTo(os);
2806  }
2807 
2808  virtual bool MatchAndExplain(LhsContainer lhs,
2809  MatchResultListener * listener) const
2810  {
2811  LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2812  const size_t actual_size = lhs_stl_container.size();
2813 
2814  if (actual_size != rhs_.size())
2815  {
2816  *listener << "which contains " << actual_size << " values";
2817  return false;
2818  }
2819 
2820  typename LhsStlContainer::const_iterator left = lhs_stl_container.begin();
2821  typename RhsStlContainer::const_iterator right = rhs_.begin();
2822 
2823  for (size_t i = 0; i != actual_size; ++i, ++left, ++right)
2824  {
2825  const InnerMatcherArg value_pair(*left, *right);
2826 
2827  if (listener->IsInterested())
2828  {
2829  StringMatchResultListener inner_listener;
2830 
2831  if (!mono_tuple_matcher_.MatchAndExplain(
2832  value_pair, &inner_listener))
2833  {
2834  *listener << "where the value pair (";
2835  UniversalPrint(*left, listener->stream());
2836  *listener << ", ";
2837  UniversalPrint(*right, listener->stream());
2838  *listener << ") at index #" << i << " don't match";
2839  PrintIfNotEmpty(inner_listener.str(), listener->stream());
2840  return false;
2841  }
2842  }
2843 
2844  else
2845  {
2846  if (!mono_tuple_matcher_.Matches(value_pair))
2847  { return false; }
2848  }
2849  }
2850 
2851  return true;
2852  }
2853 
2854  private:
2855  const Matcher<InnerMatcherArg> mono_tuple_matcher_;
2856  const RhsStlContainer rhs_;
2857 
2859  };
2860 
2861 private:
2862  const TupleMatcher tuple_matcher_;
2863  const RhsStlContainer rhs_;
2864 
2866 };
2867 
2868 // Holds the logic common to ContainsMatcherImpl and EachMatcherImpl.
2869 template <typename Container>
2870 class QuantifierMatcherImpl : public MatcherInterface<Container>
2871 {
2872 public:
2873  typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
2875  typedef typename View::type StlContainer;
2877  typedef typename StlContainer::value_type Element;
2878 
2879  template <typename InnerMatcher>
2880  explicit QuantifierMatcherImpl(InnerMatcher inner_matcher)
2881  : inner_matcher_(
2882  testing::SafeMatcherCast<const Element & >(inner_matcher)) {}
2883 
2884  // Checks whether:
2885  // * All elements in the container match, if all_elements_should_match.
2886  // * Any element in the container matches, if !all_elements_should_match.
2887  bool MatchAndExplainImpl(bool all_elements_should_match,
2888  Container container,
2889  MatchResultListener * listener) const
2890  {
2891  StlContainerReference stl_container = View::ConstReference(container);
2892  size_t i = 0;
2893 
2894  for (typename StlContainer::const_iterator it = stl_container.begin();
2895  it != stl_container.end(); ++it, ++i)
2896  {
2897  StringMatchResultListener inner_listener;
2898  const bool matches = inner_matcher_.MatchAndExplain(*it, &inner_listener);
2899 
2900  if (matches != all_elements_should_match)
2901  {
2902  *listener << "whose element #" << i
2903  << (matches ? " matches" : " doesn't match");
2904  PrintIfNotEmpty(inner_listener.str(), listener->stream());
2905  return !all_elements_should_match;
2906  }
2907  }
2908 
2909  return all_elements_should_match;
2910  }
2911 
2912 protected:
2913  const Matcher<const Element &> inner_matcher_;
2914 
2916 };
2917 
2918 // Implements Contains(element_matcher) for the given argument type Container.
2919 // Symmetric to EachMatcherImpl.
2920 template <typename Container>
2921 class ContainsMatcherImpl : public QuantifierMatcherImpl<Container>
2922 {
2923 public:
2924  template <typename InnerMatcher>
2925  explicit ContainsMatcherImpl(InnerMatcher inner_matcher)
2926  : QuantifierMatcherImpl<Container>(inner_matcher) {}
2927 
2928  // Describes what this matcher does.
2929  virtual void DescribeTo(::std::ostream * os) const
2930  {
2931  *os << "contains at least one element that ";
2932  this->inner_matcher_.DescribeTo(os);
2933  }
2934 
2935  virtual void DescribeNegationTo(::std::ostream * os) const
2936  {
2937  *os << "doesn't contain any element that ";
2938  this->inner_matcher_.DescribeTo(os);
2939  }
2940 
2941  virtual bool MatchAndExplain(Container container,
2942  MatchResultListener * listener) const
2943  {
2944  return this->MatchAndExplainImpl(false, container, listener);
2945  }
2946 
2947 private:
2949 };
2950 
2951 // Implements Each(element_matcher) for the given argument type Container.
2952 // Symmetric to ContainsMatcherImpl.
2953 template <typename Container>
2954 class EachMatcherImpl : public QuantifierMatcherImpl<Container>
2955 {
2956 public:
2957  template <typename InnerMatcher>
2958  explicit EachMatcherImpl(InnerMatcher inner_matcher)
2959  : QuantifierMatcherImpl<Container>(inner_matcher) {}
2960 
2961  // Describes what this matcher does.
2962  virtual void DescribeTo(::std::ostream * os) const
2963  {
2964  *os << "only contains elements that ";
2965  this->inner_matcher_.DescribeTo(os);
2966  }
2967 
2968  virtual void DescribeNegationTo(::std::ostream * os) const
2969  {
2970  *os << "contains some element that ";
2971  this->inner_matcher_.DescribeNegationTo(os);
2972  }
2973 
2974  virtual bool MatchAndExplain(Container container,
2975  MatchResultListener * listener) const
2976  {
2977  return this->MatchAndExplainImpl(true, container, listener);
2978  }
2979 
2980 private:
2982 };
2983 
2984 // Implements polymorphic Contains(element_matcher).
2985 template <typename M>
2986 class ContainsMatcher
2987 {
2988 public:
2989  explicit ContainsMatcher(M m) : inner_matcher_(m) {}
2990 
2991  template <typename Container>
2992  operator Matcher<Container>() const
2993  {
2994  return MakeMatcher(new ContainsMatcherImpl<Container>(inner_matcher_));
2995  }
2996 
2997 private:
2998  const M inner_matcher_;
2999 
3001 };
3002 
3003 // Implements polymorphic Each(element_matcher).
3004 template <typename M>
3005 class EachMatcher
3006 {
3007 public:
3008  explicit EachMatcher(M m) : inner_matcher_(m) {}
3009 
3010  template <typename Container>
3011  operator Matcher<Container>() const
3012  {
3013  return MakeMatcher(new EachMatcherImpl<Container>(inner_matcher_));
3014  }
3015 
3016 private:
3017  const M inner_matcher_;
3018 
3020 };
3021 
3022 // Implements Key(inner_matcher) for the given argument pair type.
3023 // Key(inner_matcher) matches an std::pair whose 'first' field matches
3024 // inner_matcher. For example, Contains(Key(Ge(5))) can be used to match an
3025 // std::map that contains at least one element whose key is >= 5.
3026 template <typename PairType>
3027 class KeyMatcherImpl : public MatcherInterface<PairType>
3028 {
3029 public:
3030  typedef GTEST_REMOVE_REFERENCE_AND_CONST_(PairType) RawPairType;
3031  typedef typename RawPairType::first_type KeyType;
3032 
3033  template <typename InnerMatcher>
3034  explicit KeyMatcherImpl(InnerMatcher inner_matcher)
3035  : inner_matcher_(
3036  testing::SafeMatcherCast<const KeyType & >(inner_matcher))
3037  {
3038  }
3039 
3040  // Returns true iff 'key_value.first' (the key) matches the inner matcher.
3041  virtual bool MatchAndExplain(PairType key_value,
3042  MatchResultListener * listener) const
3043  {
3044  StringMatchResultListener inner_listener;
3045  const bool match = inner_matcher_.MatchAndExplain(key_value.first,
3046  &inner_listener);
3047  const internal::string explanation = inner_listener.str();
3048 
3049  if (explanation != "")
3050  {
3051  *listener << "whose first field is a value " << explanation;
3052  }
3053 
3054  return match;
3055  }
3056 
3057  // Describes what this matcher does.
3058  virtual void DescribeTo(::std::ostream * os) const
3059  {
3060  *os << "has a key that ";
3061  inner_matcher_.DescribeTo(os);
3062  }
3063 
3064  // Describes what the negation of this matcher does.
3065  virtual void DescribeNegationTo(::std::ostream * os) const
3066  {
3067  *os << "doesn't have a key that ";
3068  inner_matcher_.DescribeTo(os);
3069  }
3070 
3071 private:
3072  const Matcher<const KeyType &> inner_matcher_;
3073 
3075 };
3076 
3077 // Implements polymorphic Key(matcher_for_key).
3078 template <typename M>
3079 class KeyMatcher
3080 {
3081 public:
3082  explicit KeyMatcher(M m) : matcher_for_key_(m) {}
3083 
3084  template <typename PairType>
3085  operator Matcher<PairType>() const
3086  {
3087  return MakeMatcher(new KeyMatcherImpl<PairType>(matcher_for_key_));
3088  }
3089 
3090 private:
3091  const M matcher_for_key_;
3092 
3094 };
3095 
3096 // Implements Pair(first_matcher, second_matcher) for the given argument pair
3097 // type with its two matchers. See Pair() function below.
3098 template <typename PairType>
3099 class PairMatcherImpl : public MatcherInterface<PairType>
3100 {
3101 public:
3102  typedef GTEST_REMOVE_REFERENCE_AND_CONST_(PairType) RawPairType;
3103  typedef typename RawPairType::first_type FirstType;
3104  typedef typename RawPairType::second_type SecondType;
3105 
3106  template <typename FirstMatcher, typename SecondMatcher>
3107  PairMatcherImpl(FirstMatcher first_matcher, SecondMatcher second_matcher)
3108  : first_matcher_(
3109  testing::SafeMatcherCast<const FirstType & >(first_matcher)),
3110  second_matcher_(
3111  testing::SafeMatcherCast<const SecondType & >(second_matcher))
3112  {
3113  }
3114 
3115  // Describes what this matcher does.
3116  virtual void DescribeTo(::std::ostream * os) const
3117  {
3118  *os << "has a first field that ";
3119  first_matcher_.DescribeTo(os);
3120  *os << ", and has a second field that ";
3121  second_matcher_.DescribeTo(os);
3122  }
3123 
3124  // Describes what the negation of this matcher does.
3125  virtual void DescribeNegationTo(::std::ostream * os) const
3126  {
3127  *os << "has a first field that ";
3128  first_matcher_.DescribeNegationTo(os);
3129  *os << ", or has a second field that ";
3130  second_matcher_.DescribeNegationTo(os);
3131  }
3132 
3133  // Returns true iff 'a_pair.first' matches first_matcher and 'a_pair.second'
3134  // matches second_matcher.
3135  virtual bool MatchAndExplain(PairType a_pair,
3136  MatchResultListener * listener) const
3137  {
3138  if (!listener->IsInterested())
3139  {
3140  // If the listener is not interested, we don't need to construct the
3141  // explanation.
3142  return first_matcher_.Matches(a_pair.first) &&
3143  second_matcher_.Matches(a_pair.second);
3144  }
3145 
3146  StringMatchResultListener first_inner_listener;
3147 
3148  if (!first_matcher_.MatchAndExplain(a_pair.first,
3149  &first_inner_listener))
3150  {
3151  *listener << "whose first field does not match";
3152  PrintIfNotEmpty(first_inner_listener.str(), listener->stream());
3153  return false;
3154  }
3155 
3156  StringMatchResultListener second_inner_listener;
3157 
3158  if (!second_matcher_.MatchAndExplain(a_pair.second,
3159  &second_inner_listener))
3160  {
3161  *listener << "whose second field does not match";
3162  PrintIfNotEmpty(second_inner_listener.str(), listener->stream());
3163  return false;
3164  }
3165 
3166  ExplainSuccess(first_inner_listener.str(), second_inner_listener.str(),
3167  listener);
3168  return true;
3169  }
3170 
3171 private:
3172  void ExplainSuccess(const internal::string & first_explanation,
3173  const internal::string & second_explanation,
3174  MatchResultListener * listener) const
3175  {
3176  *listener << "whose both fields match";
3177 
3178  if (first_explanation != "")
3179  {
3180  *listener << ", where the first field is a value " << first_explanation;
3181  }
3182 
3183  if (second_explanation != "")
3184  {
3185  *listener << ", ";
3186 
3187  if (first_explanation != "")
3188  {
3189  *listener << "and ";
3190  }
3191 
3192  else
3193  {
3194  *listener << "where ";
3195  }
3196 
3197  *listener << "the second field is a value " << second_explanation;
3198  }
3199  }
3200 
3201  const Matcher<const FirstType &> first_matcher_;
3202  const Matcher<const SecondType &> second_matcher_;
3203 
3205 };
3206 
3207 // Implements polymorphic Pair(first_matcher, second_matcher).
3208 template <typename FirstMatcher, typename SecondMatcher>
3209 class PairMatcher
3210 {
3211 public:
3212  PairMatcher(FirstMatcher first_matcher, SecondMatcher second_matcher)
3213  : first_matcher_(first_matcher), second_matcher_(second_matcher) {}
3214 
3215  template <typename PairType>
3216  operator Matcher<PairType> () const
3217  {
3218  return MakeMatcher(
3220  first_matcher_, second_matcher_));
3221  }
3222 
3223 private:
3224  const FirstMatcher first_matcher_;
3225  const SecondMatcher second_matcher_;
3226 
3228 };
3229 
3230 // Implements ElementsAre() and ElementsAreArray().
3231 template <typename Container>
3232 class ElementsAreMatcherImpl : public MatcherInterface<Container>
3233 {
3234 public:
3235  typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
3237  typedef typename View::type StlContainer;
3239  typedef typename StlContainer::value_type Element;
3240 
3241  // Constructs the matcher from a sequence of element values or
3242  // element matchers.
3243  template <typename InputIter>
3244  ElementsAreMatcherImpl(InputIter first, InputIter last)
3245  {
3246  while (first != last)
3247  {
3248  matchers_.push_back(MatcherCast<const Element &>(*first++));
3249  }
3250  }
3251 
3252  // Describes what this matcher does.
3253  virtual void DescribeTo(::std::ostream * os) const
3254  {
3255  if (count() == 0)
3256  {
3257  *os << "is empty";
3258  }
3259 
3260  else if (count() == 1)
3261  {
3262  *os << "has 1 element that ";
3263  matchers_[0].DescribeTo(os);
3264  }
3265 
3266  else
3267  {
3268  *os << "has " << Elements(count()) << " where\n";
3269 
3270  for (size_t i = 0; i != count(); ++i)
3271  {
3272  *os << "element #" << i << " ";
3273  matchers_[i].DescribeTo(os);
3274 
3275  if (i + 1 < count())
3276  {
3277  *os << ",\n";
3278  }
3279  }
3280  }
3281  }
3282 
3283  // Describes what the negation of this matcher does.
3284  virtual void DescribeNegationTo(::std::ostream * os) const
3285  {
3286  if (count() == 0)
3287  {
3288  *os << "isn't empty";
3289  return;
3290  }
3291 
3292  *os << "doesn't have " << Elements(count()) << ", or\n";
3293 
3294  for (size_t i = 0; i != count(); ++i)
3295  {
3296  *os << "element #" << i << " ";
3297  matchers_[i].DescribeNegationTo(os);
3298 
3299  if (i + 1 < count())
3300  {
3301  *os << ", or\n";
3302  }
3303  }
3304  }
3305 
3306  virtual bool MatchAndExplain(Container container,
3307  MatchResultListener * listener) const
3308  {
3309  // To work with stream-like "containers", we must only walk
3310  // through the elements in one pass.
3311 
3312  const bool listener_interested = listener->IsInterested();
3313 
3314  // explanations[i] is the explanation of the element at index i.
3315  ::std::vector<internal::string> explanations(count());
3316  StlContainerReference stl_container = View::ConstReference(container);
3317  typename StlContainer::const_iterator it = stl_container.begin();
3318  size_t exam_pos = 0;
3319  bool mismatch_found = false; // Have we found a mismatched element yet?
3320 
3321  // Go through the elements and matchers in pairs, until we reach
3322  // the end of either the elements or the matchers, or until we find a
3323  // mismatch.
3324  for (; it != stl_container.end() && exam_pos != count(); ++it, ++exam_pos)
3325  {
3326  bool match; // Does the current element match the current matcher?
3327 
3328  if (listener_interested)
3329  {
3331  match = matchers_[exam_pos].MatchAndExplain(*it, &s);
3332  explanations[exam_pos] = s.str();
3333  }
3334 
3335  else
3336  {
3337  match = matchers_[exam_pos].Matches(*it);
3338  }
3339 
3340  if (!match)
3341  {
3342  mismatch_found = true;
3343  break;
3344  }
3345  }
3346 
3347  // If mismatch_found is true, 'exam_pos' is the index of the mismatch.
3348 
3349  // Find how many elements the actual container has. We avoid
3350  // calling size() s.t. this code works for stream-like "containers"
3351  // that don't define size().
3352  size_t actual_count = exam_pos;
3353 
3354  for (; it != stl_container.end(); ++it)
3355  {
3356  ++actual_count;
3357  }
3358 
3359  if (actual_count != count())
3360  {
3361  // The element count doesn't match. If the container is empty,
3362  // there's no need to explain anything as Google Mock already
3363  // prints the empty container. Otherwise we just need to show
3364  // how many elements there actually are.
3365  if (listener_interested && (actual_count != 0))
3366  {
3367  *listener << "which has " << Elements(actual_count);
3368  }
3369 
3370  return false;
3371  }
3372 
3373  if (mismatch_found)
3374  {
3375  // The element count matches, but the exam_pos-th element doesn't match.
3376  if (listener_interested)
3377  {
3378  *listener << "whose element #" << exam_pos << " doesn't match";
3379  PrintIfNotEmpty(explanations[exam_pos], listener->stream());
3380  }
3381 
3382  return false;
3383  }
3384 
3385  // Every element matches its expectation. We need to explain why
3386  // (the obvious ones can be skipped).
3387  if (listener_interested)
3388  {
3389  bool reason_printed = false;
3390 
3391  for (size_t i = 0; i != count(); ++i)
3392  {
3393  const internal::string & s = explanations[i];
3394 
3395  if (!s.empty())
3396  {
3397  if (reason_printed)
3398  {
3399  *listener << ",\nand ";
3400  }
3401 
3402  *listener << "whose element #" << i << " matches, " << s;
3403  reason_printed = true;
3404  }
3405  }
3406  }
3407 
3408  return true;
3409  }
3410 
3411 private:
3412  static Message Elements(size_t count)
3413  {
3414  return Message() << count << (count == 1 ? " element" : " elements");
3415  }
3416 
3417  size_t count() const { return matchers_.size(); }
3418 
3419  ::std::vector<Matcher<const Element &> > matchers_;
3420 
3422 };
3423 
3424 // Connectivity matrix of (elements X matchers), in element-major order.
3425 // Initially, there are no edges.
3426 // Use NextGraph() to iterate over all possible edge configurations.
3427 // Use Randomize() to generate a random edge configuration.
3428 class GTEST_API_ MatchMatrix
3429 {
3430 public:
3431  MatchMatrix(size_t num_elements, size_t num_matchers)
3432  : num_elements_(num_elements),
3433  num_matchers_(num_matchers),
3434  matched_(num_elements_ * num_matchers_, 0)
3435  {
3436  }
3437 
3438  size_t LhsSize() const { return num_elements_; }
3439  size_t RhsSize() const { return num_matchers_; }
3440  bool HasEdge(size_t ilhs, size_t irhs) const
3441  {
3442  return matched_[SpaceIndex(ilhs, irhs)] == 1;
3443  }
3444  void SetEdge(size_t ilhs, size_t irhs, bool b)
3445  {
3446  matched_[SpaceIndex(ilhs, irhs)] = b ? 1 : 0;
3447  }
3448 
3449  // Treating the connectivity matrix as a (LhsSize()*RhsSize())-bit number,
3450  // adds 1 to that number; returns false if incrementing the graph left it
3451  // empty.
3452  bool NextGraph();
3453 
3454  void Randomize();
3455 
3456  string DebugString() const;
3457 
3458 private:
3459  size_t SpaceIndex(size_t ilhs, size_t irhs) const
3460  {
3461  return ilhs * num_matchers_ + irhs;
3462  }
3463 
3464  size_t num_elements_;
3465  size_t num_matchers_;
3466 
3467  // Each element is a char interpreted as bool. They are stored as a
3468  // flattened array in lhs-major order, use 'SpaceIndex()' to translate
3469  // a (ilhs, irhs) matrix coordinate into an offset.
3470  ::std::vector<char> matched_;
3471 };
3472 
3473 typedef ::std::pair<size_t, size_t> ElementMatcherPair;
3474 typedef ::std::vector<ElementMatcherPair> ElementMatcherPairs;
3475 
3476 // Returns a maximum bipartite matching for the specified graph 'g'.
3477 // The matching is represented as a vector of {element, matcher} pairs.
3478 GTEST_API_ ElementMatcherPairs
3479 FindMaxBipartiteMatching(const MatchMatrix & g);
3480 
3481 GTEST_API_ bool FindPairing(const MatchMatrix & matrix,
3482  MatchResultListener * listener);
3483 
3484 // Untyped base class for implementing UnorderedElementsAre. By
3485 // putting logic that's not specific to the element type here, we
3486 // reduce binary bloat and increase compilation speed.
3487 class GTEST_API_ UnorderedElementsAreMatcherImplBase
3488 {
3489 protected:
3490  // A vector of matcher describers, one for each element matcher.
3491  // Does not own the describers (and thus can be used only when the
3492  // element matchers are alive).
3493  typedef ::std::vector<const MatcherDescriberInterface *> MatcherDescriberVec;
3494 
3495  // Describes this UnorderedElementsAre matcher.
3496  void DescribeToImpl(::std::ostream * os) const;
3497 
3498  // Describes the negation of this UnorderedElementsAre matcher.
3499  void DescribeNegationToImpl(::std::ostream * os) const;
3500 
3501  bool VerifyAllElementsAndMatchersAreMatched(
3502  const ::std::vector<string> & element_printouts,
3503  const MatchMatrix & matrix,
3504  MatchResultListener * listener) const;
3505 
3506  MatcherDescriberVec & matcher_describers()
3507  {
3508  return matcher_describers_;
3509  }
3510 
3511  static Message Elements(size_t n)
3512  {
3513  return Message() << n << " element" << (n == 1 ? "" : "s");
3514  }
3515 
3516 private:
3517  MatcherDescriberVec matcher_describers_;
3518 
3519  GTEST_DISALLOW_ASSIGN_(UnorderedElementsAreMatcherImplBase);
3520 };
3521 
3522 // Implements unordered ElementsAre and unordered ElementsAreArray.
3523 template <typename Container>
3525  : public MatcherInterface<Container>,
3526  public UnorderedElementsAreMatcherImplBase
3527 {
3528 public:
3529  typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
3531  typedef typename View::type StlContainer;
3533  typedef typename StlContainer::const_iterator StlContainerConstIterator;
3534  typedef typename StlContainer::value_type Element;
3535 
3536  // Constructs the matcher from a sequence of element values or
3537  // element matchers.
3538  template <typename InputIter>
3539  UnorderedElementsAreMatcherImpl(InputIter first, InputIter last)
3540  {
3541  for (; first != last; ++first)
3542  {
3543  matchers_.push_back(MatcherCast<const Element &>(*first));
3544  matcher_describers().push_back(matchers_.back().GetDescriber());
3545  }
3546  }
3547 
3548  // Describes what this matcher does.
3549  virtual void DescribeTo(::std::ostream * os) const
3550  {
3551  return UnorderedElementsAreMatcherImplBase::DescribeToImpl(os);
3552  }
3553 
3554  // Describes what the negation of this matcher does.
3555  virtual void DescribeNegationTo(::std::ostream * os) const
3556  {
3557  return UnorderedElementsAreMatcherImplBase::DescribeNegationToImpl(os);
3558  }
3559 
3560  virtual bool MatchAndExplain(Container container,
3561  MatchResultListener * listener) const
3562  {
3563  StlContainerReference stl_container = View::ConstReference(container);
3564  ::std::vector<string> element_printouts;
3565  MatchMatrix matrix = AnalyzeElements(stl_container.begin(),
3566  stl_container.end(),
3567  &element_printouts,
3568  listener);
3569 
3570  const size_t actual_count = matrix.LhsSize();
3571 
3572  if (actual_count == 0 && matchers_.empty())
3573  {
3574  return true;
3575  }
3576 
3577  if (actual_count != matchers_.size())
3578  {
3579  // The element count doesn't match. If the container is empty,
3580  // there's no need to explain anything as Google Mock already
3581  // prints the empty container. Otherwise we just need to show
3582  // how many elements there actually are.
3583  if (actual_count != 0 && listener->IsInterested())
3584  {
3585  *listener << "which has " << Elements(actual_count);
3586  }
3587 
3588  return false;
3589  }
3590 
3591  return VerifyAllElementsAndMatchersAreMatched(element_printouts,
3592  matrix, listener) &&
3593  FindPairing(matrix, listener);
3594  }
3595 
3596 private:
3597  typedef ::std::vector<Matcher<const Element &> > MatcherVec;
3598 
3599  template <typename ElementIter>
3600  MatchMatrix AnalyzeElements(ElementIter elem_first, ElementIter elem_last,
3601  ::std::vector<string> * element_printouts,
3602  MatchResultListener * listener) const
3603  {
3604  element_printouts->clear();
3605  ::std::vector<char> did_match;
3606  size_t num_elements = 0;
3607 
3608  for (; elem_first != elem_last; ++num_elements, ++elem_first)
3609  {
3610  if (listener->IsInterested())
3611  {
3612  element_printouts->push_back(PrintToString(*elem_first));
3613  }
3614 
3615  for (size_t irhs = 0; irhs != matchers_.size(); ++irhs)
3616  {
3617  did_match.push_back(Matches(matchers_[irhs])(*elem_first));
3618  }
3619  }
3620 
3621  MatchMatrix matrix(num_elements, matchers_.size());
3622  ::std::vector<char>::const_iterator did_match_iter = did_match.begin();
3623 
3624  for (size_t ilhs = 0; ilhs != num_elements; ++ilhs)
3625  {
3626  for (size_t irhs = 0; irhs != matchers_.size(); ++irhs)
3627  {
3628  matrix.SetEdge(ilhs, irhs, *did_match_iter++ != 0);
3629  }
3630  }
3631 
3632  return matrix;
3633  }
3634 
3635  MatcherVec matchers_;
3636 
3638 };
3639 
3640 // Functor for use in TransformTuple.
3641 // Performs MatcherCast<Target> on an input argument of any type.
3642 template <typename Target>
3644 {
3645  template <typename Arg>
3646  Matcher<Target> operator()(const Arg & a) const
3647  {
3648  return MatcherCast<Target>(a);
3649  }
3650 };
3651 
3652 // Implements UnorderedElementsAre.
3653 template <typename MatcherTuple>
3655 {
3656 public:
3658  : matchers_(args) {}
3659 
3660  template <typename Container>
3661  operator Matcher<Container>() const
3662  {
3663  typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
3665  typedef typename View::value_type Element;
3666  typedef ::std::vector<Matcher<const Element &> > MatcherVec;
3667  MatcherVec matchers;
3668  matchers.reserve(::std::tr1::tuple_size<MatcherTuple>::value);
3670  ::std::back_inserter(matchers));
3672  matchers.begin(), matchers.end()));
3673  }
3674 
3675 private:
3676  const MatcherTuple matchers_;
3678 };
3679 
3680 // Implements ElementsAre.
3681 template <typename MatcherTuple>
3682 class ElementsAreMatcher
3683 {
3684 public:
3685  explicit ElementsAreMatcher(const MatcherTuple & args) : matchers_(args) {}
3686 
3687  template <typename Container>
3688  operator Matcher<Container>() const
3689  {
3690  typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
3692  typedef typename View::value_type Element;
3693  typedef ::std::vector<Matcher<const Element &> > MatcherVec;
3694  MatcherVec matchers;
3695  matchers.reserve(::std::tr1::tuple_size<MatcherTuple>::value);
3697  ::std::back_inserter(matchers));
3699  matchers.begin(), matchers.end()));
3700  }
3701 
3702 private:
3703  const MatcherTuple matchers_;
3705 };
3706 
3707 // Implements UnorderedElementsAreArray().
3708 template <typename T>
3710 {
3711 public:
3713 
3714  template <typename Iter>
3716  : matchers_(first, last) {}
3717 
3718  template <typename Container>
3719  operator Matcher<Container>() const
3720  {
3721  return MakeMatcher(
3722  new UnorderedElementsAreMatcherImpl<Container>(matchers_.begin(),
3723  matchers_.end()));
3724  }
3725 
3726 private:
3727  ::std::vector<T> matchers_;
3728 
3730 };
3731 
3732 // Implements ElementsAreArray().
3733 template <typename T>
3735 {
3736 public:
3737  template <typename Iter>
3738  ElementsAreArrayMatcher(Iter first, Iter last) : matchers_(first, last) {}
3739 
3740  template <typename Container>
3741  operator Matcher<Container>() const
3742  {
3744  matchers_.begin(), matchers_.end()));
3745  }
3746 
3747 private:
3748  const ::std::vector<T> matchers_;
3749 
3751 };
3752 
3753 // Returns the description for a matcher defined using the MATCHER*()
3754 // macro where the user-supplied description string is "", if
3755 // 'negation' is false; otherwise returns the description of the
3756 // negation of the matcher. 'param_values' contains a list of strings
3757 // that are the print-out of the matcher's parameters.
3758 GTEST_API_ string FormatMatcherDescription(bool negation,
3759  const char * matcher_name,
3760  const Strings & param_values);
3761 
3762 } // namespace internal
3763 
3764 // ElementsAreArray(first, last)
3765 // ElementsAreArray(pointer, count)
3766 // ElementsAreArray(array)
3767 // ElementsAreArray(vector)
3768 // ElementsAreArray({ e1, e2, ..., en })
3769 //
3770 // The ElementsAreArray() functions are like ElementsAre(...), except
3771 // that they are given a homogeneous sequence rather than taking each
3772 // element as a function argument. The sequence can be specified as an
3773 // array, a pointer and count, a vector, an initializer list, or an
3774 // STL iterator range. In each of these cases, the underlying sequence
3775 // can be either a sequence of values or a sequence of matchers.
3776 //
3777 // All forms of ElementsAreArray() make a copy of the input matcher sequence.
3778 
3779 template <typename Iter>
3781 typename ::std::iterator_traits<Iter>::value_type >
3782 ElementsAreArray(Iter first, Iter last)
3783 {
3784  typedef typename ::std::iterator_traits<Iter>::value_type T;
3785  return internal::ElementsAreArrayMatcher<T>(first, last);
3786 }
3787 
3788 template <typename T>
3790  const T * pointer, size_t count)
3791 {
3792  return ElementsAreArray(pointer, pointer + count);
3793 }
3794 
3795 template <typename T, size_t N>
3797  const T(&array)[N])
3798 {
3799  return ElementsAreArray(array, N);
3800 }
3801 
3802 template <typename T, typename A>
3804  const ::std::vector<T, A> & vec)
3805 {
3806  return ElementsAreArray(vec.begin(), vec.end());
3807 }
3808 
3809 #if GTEST_LANG_CXX11
3810 template <typename T>
3812 ElementsAreArray(::std::initializer_list<T> xs)
3813 {
3814  return ElementsAreArray(xs.begin(), xs.end());
3815 }
3816 #endif
3817 
3818 // UnorderedElementsAreArray(first, last)
3819 // UnorderedElementsAreArray(pointer, count)
3820 // UnorderedElementsAreArray(array)
3821 // UnorderedElementsAreArray(vector)
3822 // UnorderedElementsAreArray({ e1, e2, ..., en })
3823 //
3824 // The UnorderedElementsAreArray() functions are like
3825 // ElementsAreArray(...), but allow matching the elements in any order.
3826 template <typename Iter>
3828 typename ::std::iterator_traits<Iter>::value_type >
3829 UnorderedElementsAreArray(Iter first, Iter last)
3830 {
3831  typedef typename ::std::iterator_traits<Iter>::value_type T;
3833 }
3834 
3835 template <typename T>
3837 UnorderedElementsAreArray(const T * pointer, size_t count)
3838 {
3839  return UnorderedElementsAreArray(pointer, pointer + count);
3840 }
3841 
3842 template <typename T, size_t N>
3844 UnorderedElementsAreArray(const T(&array)[N])
3845 {
3846  return UnorderedElementsAreArray(array, N);
3847 }
3848 
3849 template <typename T, typename A>
3851 UnorderedElementsAreArray(const ::std::vector<T, A> & vec)
3852 {
3853  return UnorderedElementsAreArray(vec.begin(), vec.end());
3854 }
3855 
3856 #if GTEST_LANG_CXX11
3857 template <typename T>
3859 UnorderedElementsAreArray(::std::initializer_list<T> xs)
3860 {
3861  return UnorderedElementsAreArray(xs.begin(), xs.end());
3862 }
3863 #endif
3864 
3865 // _ is a matcher that matches anything of any type.
3866 //
3867 // This definition is fine as:
3868 //
3869 // 1. The C++ standard permits using the name _ in a namespace that
3870 // is not the global namespace or ::std.
3871 // 2. The AnythingMatcher class has no data member or constructor,
3872 // so it's OK to create global variables of this type.
3873 // 3. c-style has approved of using _ in this case.
3874 const internal::AnythingMatcher _ = {};
3875 // Creates a matcher that matches any value of the given type T.
3876 template <typename T>
3877 inline Matcher<T> A() { return MakeMatcher(new internal::AnyMatcherImpl<T>()); }
3878 
3879 // Creates a matcher that matches any value of the given type T.
3880 template <typename T>
3881 inline Matcher<T> An() { return A<T>(); }
3882 
3883 // Creates a polymorphic matcher that matches anything equal to x.
3884 // Note: if the parameter of Eq() were declared as const T&, Eq("foo")
3885 // wouldn't compile.
3886 template <typename T>
3887 inline internal::EqMatcher<T> Eq(T x) { return internal::EqMatcher<T>(x); }
3888 
3889 // Constructs a Matcher<T> from a 'value' of type T. The constructed
3890 // matcher matches any value that's equal to 'value'.
3891 template <typename T>
3892 Matcher<T>::Matcher(T value) { *this = Eq(value); }
3893 
3894 // Creates a monomorphic matcher that matches anything with type Lhs
3895 // and equal to rhs. A user may need to use this instead of Eq(...)
3896 // in order to resolve an overloading ambiguity.
3897 //
3898 // TypedEq<T>(x) is just a convenient short-hand for Matcher<T>(Eq(x))
3899 // or Matcher<T>(x), but more readable than the latter.
3900 //
3901 // We could define similar monomorphic matchers for other comparison
3902 // operations (e.g. TypedLt, TypedGe, and etc), but decided not to do
3903 // it yet as those are used much less than Eq() in practice. A user
3904 // can always write Matcher<T>(Lt(5)) to be explicit about the type,
3905 // for example.
3906 template <typename Lhs, typename Rhs>
3907 inline Matcher<Lhs> TypedEq(const Rhs & rhs) { return Eq(rhs); }
3908 
3909 // Creates a polymorphic matcher that matches anything >= x.
3910 template <typename Rhs>
3911 inline internal::GeMatcher<Rhs> Ge(Rhs x)
3912 {
3913  return internal::GeMatcher<Rhs>(x);
3914 }
3915 
3916 // Creates a polymorphic matcher that matches anything > x.
3917 template <typename Rhs>
3918 inline internal::GtMatcher<Rhs> Gt(Rhs x)
3919 {
3920  return internal::GtMatcher<Rhs>(x);
3921 }
3922 
3923 // Creates a polymorphic matcher that matches anything <= x.
3924 template <typename Rhs>
3925 inline internal::LeMatcher<Rhs> Le(Rhs x)
3926 {
3927  return internal::LeMatcher<Rhs>(x);
3928 }
3929 
3930 // Creates a polymorphic matcher that matches anything < x.
3931 template <typename Rhs>
3932 inline internal::LtMatcher<Rhs> Lt(Rhs x)
3933 {
3934  return internal::LtMatcher<Rhs>(x);
3935 }
3936 
3937 // Creates a polymorphic matcher that matches anything != x.
3938 template <typename Rhs>
3939 inline internal::NeMatcher<Rhs> Ne(Rhs x)
3940 {
3941  return internal::NeMatcher<Rhs>(x);
3942 }
3943 
3944 // Creates a polymorphic matcher that matches any NULL pointer.
3946 {
3948 }
3949 
3950 // Creates a polymorphic matcher that matches any non-NULL pointer.
3951 // This is convenient as Not(NULL) doesn't compile (the compiler
3952 // thinks that that expression is comparing a pointer with an integer).
3954 {
3956 }
3957 
3958 // Creates a polymorphic matcher that matches any argument that
3959 // references variable x.
3960 template <typename T>
3961 inline internal::RefMatcher<T &> Ref(T & x) // NOLINT
3962 {
3963  return internal::RefMatcher<T &>(x);
3964 }
3965 
3966 // Creates a matcher that matches any double argument approximately
3967 // equal to rhs, where two NANs are considered unequal.
3969 {
3970  return internal::FloatingEqMatcher<double>(rhs, false);
3971 }
3972 
3973 // Creates a matcher that matches any double argument approximately
3974 // equal to rhs, including NaN values when rhs is NaN.
3976 {
3977  return internal::FloatingEqMatcher<double>(rhs, true);
3978 }
3979 
3980 // Creates a matcher that matches any double argument approximately equal to
3981 // rhs, up to the specified max absolute error bound, where two NANs are
3982 // considered unequal. The max absolute error bound must be non-negative.
3984  double rhs, double max_abs_error)
3985 {
3986  return internal::FloatingEqMatcher<double>(rhs, false, max_abs_error);
3987 }
3988 
3989 // Creates a matcher that matches any double argument approximately equal to
3990 // rhs, up to the specified max absolute error bound, including NaN values when
3991 // rhs is NaN. The max absolute error bound must be non-negative.
3993  double rhs, double max_abs_error)
3994 {
3995  return internal::FloatingEqMatcher<double>(rhs, true, max_abs_error);
3996 }
3997 
3998 // Creates a matcher that matches any float argument approximately
3999 // equal to rhs, where two NANs are considered unequal.
4001 {
4002  return internal::FloatingEqMatcher<float>(rhs, false);
4003 }
4004 
4005 // Creates a matcher that matches any float argument approximately
4006 // equal to rhs, including NaN values when rhs is NaN.
4008 {
4009  return internal::FloatingEqMatcher<float>(rhs, true);
4010 }
4011 
4012 // Creates a matcher that matches any float argument approximately equal to
4013 // rhs, up to the specified max absolute error bound, where two NANs are
4014 // considered unequal. The max absolute error bound must be non-negative.
4016  float rhs, float max_abs_error)
4017 {
4018  return internal::FloatingEqMatcher<float>(rhs, false, max_abs_error);
4019 }
4020 
4021 // Creates a matcher that matches any float argument approximately equal to
4022 // rhs, up to the specified max absolute error bound, including NaN values when
4023 // rhs is NaN. The max absolute error bound must be non-negative.
4025  float rhs, float max_abs_error)
4026 {
4027  return internal::FloatingEqMatcher<float>(rhs, true, max_abs_error);
4028 }
4029 
4030 // Creates a matcher that matches a pointer (raw or smart) that points
4031 // to a value that matches inner_matcher.
4032 template <typename InnerMatcher>
4034  const InnerMatcher & inner_matcher)
4035 {
4036  return internal::PointeeMatcher<InnerMatcher>(inner_matcher);
4037 }
4038 
4039 // Creates a matcher that matches an object whose given field matches
4040 // 'matcher'. For example,
4041 // Field(&Foo::number, Ge(5))
4042 // matches a Foo object x iff x.number >= 5.
4043 template <typename Class, typename FieldType, typename FieldMatcher>
4044 inline PolymorphicMatcher <
4046  FieldType Class::*field, const FieldMatcher & matcher)
4047 {
4048  return MakePolymorphicMatcher(
4050  field, MatcherCast<const FieldType &>(matcher)));
4051  // The call to MatcherCast() is required for supporting inner
4052  // matchers of compatible types. For example, it allows
4053  // Field(&Foo::bar, m)
4054  // to compile where bar is an int32 and m is a matcher for int64.
4055 }
4056 
4057 // Creates a matcher that matches an object whose given property
4058 // matches 'matcher'. For example,
4059 // Property(&Foo::str, StartsWith("hi"))
4060 // matches a Foo object x iff x.str() starts with "hi".
4061 template <typename Class, typename PropertyType, typename PropertyMatcher>
4062 inline PolymorphicMatcher <
4064  PropertyType(Class::*property)() const, const PropertyMatcher & matcher)
4065 {
4066  return MakePolymorphicMatcher(
4068  property,
4069  MatcherCast<GTEST_REFERENCE_TO_CONST_(PropertyType)>(matcher)));
4070  // The call to MatcherCast() is required for supporting inner
4071  // matchers of compatible types. For example, it allows
4072  // Property(&Foo::bar, m)
4073  // to compile where bar() returns an int32 and m is a matcher for int64.
4074 }
4075 
4076 // Creates a matcher that matches an object iff the result of applying
4077 // a callable to x matches 'matcher'.
4078 // For example,
4079 // ResultOf(f, StartsWith("hi"))
4080 // matches a Foo object x iff f(x) starts with "hi".
4081 // callable parameter can be a function, function pointer, or a functor.
4082 // Callable has to satisfy the following conditions:
4083 // * It is required to keep no state affecting the results of
4084 // the calls on it and make no assumptions about how many calls
4085 // will be made. Any state it keeps must be protected from the
4086 // concurrent access.
4087 // * If it is a function object, it has to define type result_type.
4088 // We recommend deriving your functor classes from std::unary_function.
4089 template <typename Callable, typename ResultOfMatcher>
4091  Callable callable, const ResultOfMatcher & matcher)
4092 {
4094  callable,
4095  MatcherCast<typename internal::CallableTraits<Callable>::ResultType>(
4096  matcher));
4097  // The call to MatcherCast() is required for supporting inner
4098  // matchers of compatible types. For example, it allows
4099  // ResultOf(Function, m)
4100  // to compile where Function() returns an int32 and m is a matcher for int64.
4101 }
4102 
4103 // String matchers.
4104 
4105 // Matches a string equal to str.
4107 StrEq(const internal::string & str)
4108 {
4110  str, true, true));
4111 }
4112 
4113 // Matches a string not equal to str.
4115 StrNe(const internal::string & str)
4116 {
4118  str, false, true));
4119 }
4120 
4121 // Matches a string equal to str, ignoring case.
4123 StrCaseEq(const internal::string & str)
4124 {
4126  str, true, false));
4127 }
4128 
4129 // Matches a string not equal to str, ignoring case.
4131 StrCaseNe(const internal::string & str)
4132 {
4134  str, false, false));
4135 }
4136 
4137 // Creates a matcher that matches any string, std::string, or C string
4138 // that contains the given substring.
4140 HasSubstr(const internal::string & substring)
4141 {
4143  substring));
4144 }
4145 
4146 // Matches a string that starts with 'prefix' (case-sensitive).
4148 StartsWith(const internal::string & prefix)
4149 {
4151  prefix));
4152 }
4153 
4154 // Matches a string that ends with 'suffix' (case-sensitive).
4156 EndsWith(const internal::string & suffix)
4157 {
4159  suffix));
4160 }
4161 
4162 // Matches a string that fully matches regular expression 'regex'.
4163 // The matcher takes ownership of 'regex'.
4165  const internal::RE * regex)
4166 {
4168 }
4170  const internal::string & regex)
4171 {
4172  return MatchesRegex(new internal::RE(regex));
4173 }
4174 
4175 // Matches a string that contains regular expression 'regex'.
4176 // The matcher takes ownership of 'regex'.
4178  const internal::RE * regex)
4179 {
4181 }
4183  const internal::string & regex)
4184 {
4185  return ContainsRegex(new internal::RE(regex));
4186 }
4187 
4188 #if GTEST_HAS_GLOBAL_WSTRING || GTEST_HAS_STD_WSTRING
4189 // Wide string matchers.
4190 
4191 // Matches a string equal to str.
4193 StrEq(const internal::wstring & str)
4194 {
4196  str, true, true));
4197 }
4198 
4199 // Matches a string not equal to str.
4201 StrNe(const internal::wstring & str)
4202 {
4204  str, false, true));
4205 }
4206 
4207 // Matches a string equal to str, ignoring case.
4209 StrCaseEq(const internal::wstring & str)
4210 {
4212  str, true, false));
4213 }
4214 
4215 // Matches a string not equal to str, ignoring case.
4217 StrCaseNe(const internal::wstring & str)
4218 {
4220  str, false, false));
4221 }
4222 
4223 // Creates a matcher that matches any wstring, std::wstring, or C wide string
4224 // that contains the given substring.
4226 HasSubstr(const internal::wstring & substring)
4227 {
4229  substring));
4230 }
4231 
4232 // Matches a string that starts with 'prefix' (case-sensitive).
4234 StartsWith(const internal::wstring & prefix)
4235 {
4237  prefix));
4238 }
4239 
4240 // Matches a string that ends with 'suffix' (case-sensitive).
4242 EndsWith(const internal::wstring & suffix)
4243 {
4245  suffix));
4246 }
4247 
4248 #endif // GTEST_HAS_GLOBAL_WSTRING || GTEST_HAS_STD_WSTRING
4249 
4250 // Creates a polymorphic matcher that matches a 2-tuple where the
4251 // first field == the second field.
4252 inline internal::Eq2Matcher Eq() { return internal::Eq2Matcher(); }
4253 
4254 // Creates a polymorphic matcher that matches a 2-tuple where the
4255 // first field >= the second field.
4256 inline internal::Ge2Matcher Ge() { return internal::Ge2Matcher(); }
4257 
4258 // Creates a polymorphic matcher that matches a 2-tuple where the
4259 // first field > the second field.
4260 inline internal::Gt2Matcher Gt() { return internal::Gt2Matcher(); }
4261 
4262 // Creates a polymorphic matcher that matches a 2-tuple where the
4263 // first field <= the second field.
4264 inline internal::Le2Matcher Le() { return internal::Le2Matcher(); }
4265 
4266 // Creates a polymorphic matcher that matches a 2-tuple where the
4267 // first field < the second field.
4268 inline internal::Lt2Matcher Lt() { return internal::Lt2Matcher(); }
4269 
4270 // Creates a polymorphic matcher that matches a 2-tuple where the
4271 // first field != the second field.
4272 inline internal::Ne2Matcher Ne() { return internal::Ne2Matcher(); }
4273 
4274 // Creates a matcher that matches any value of type T that m doesn't
4275 // match.
4276 template <typename InnerMatcher>
4277 inline internal::NotMatcher<InnerMatcher> Not(InnerMatcher m)
4278 {
4280 }
4281 
4282 // Returns a matcher that matches anything that satisfies the given
4283 // predicate. The predicate can be any unary function or functor
4284 // whose return type can be implicitly converted to bool.
4285 template <typename Predicate>
4287 Truly(Predicate pred)
4288 {
4290 }
4291 
4292 // Returns a matcher that matches the container size. The container must
4293 // support both size() and size_type which all STL-like containers provide.
4294 // Note that the parameter 'size' can be a value of type size_type as well as
4295 // matcher. For instance:
4296 // EXPECT_THAT(container, SizeIs(2)); // Checks container has 2 elements.
4297 // EXPECT_THAT(container, SizeIs(Le(2)); // Checks container has at most 2.
4298 template <typename SizeMatcher>
4300 SizeIs(const SizeMatcher & size_matcher)
4301 {
4302  return internal::SizeIsMatcher<SizeMatcher>(size_matcher);
4303 }
4304 
4305 // Returns a matcher that matches an equal container.
4306 // This matcher behaves like Eq(), but in the event of mismatch lists the
4307 // values that are included in one container but not the other. (Duplicate
4308 // values and order differences are not explained.)
4309 template <typename Container>
4311 GTEST_REMOVE_CONST_(Container)> >
4312 ContainerEq(const Container & rhs)
4313 {
4314  // This following line is for working around a bug in MSVC 8.0,
4315  // which causes Container to be a const type sometimes.
4316  typedef GTEST_REMOVE_CONST_(Container) RawContainer;
4317  return MakePolymorphicMatcher(
4319 }
4320 
4321 // Returns a matcher that matches a container that, when sorted using
4322 // the given comparator, matches container_matcher.
4323 template <typename Comparator, typename ContainerMatcher>
4325 WhenSortedBy(const Comparator & comparator,
4326  const ContainerMatcher & container_matcher)
4327 {
4329  comparator, container_matcher);
4330 }
4331 
4332 // Returns a matcher that matches a container that, when sorted using
4333 // the < operator, matches container_matcher.
4334 template <typename ContainerMatcher>
4336 WhenSorted(const ContainerMatcher & container_matcher)
4337 {
4338  return
4340  internal::LessComparator(), container_matcher);
4341 }
4342 
4343 // Matches an STL-style container or a native array that contains the
4344 // same number of elements as in rhs, where its i-th element and rhs's
4345 // i-th element (as a pair) satisfy the given pair matcher, for all i.
4346 // TupleMatcher must be able to be safely cast to Matcher<tuple<const
4347 // T1&, const T2&> >, where T1 and T2 are the types of elements in the
4348 // LHS container and the RHS container respectively.
4349 template <typename TupleMatcher, typename Container>
4350 inline internal::PointwiseMatcher<TupleMatcher,
4351  GTEST_REMOVE_CONST_(Container)>
4352  Pointwise(const TupleMatcher & tuple_matcher, const Container & rhs)
4353 {
4354  // This following line is for working around a bug in MSVC 8.0,
4355  // which causes Container to be a const type sometimes.
4356  typedef GTEST_REMOVE_CONST_(Container) RawContainer;
4358  tuple_matcher, rhs);
4359 }
4360 
4361 // Matches an STL-style container or a native array that contains at
4362 // least one element matching the given value or matcher.
4363 //
4364 // Examples:
4365 // ::std::set<int> page_ids;
4366 // page_ids.insert(3);
4367 // page_ids.insert(1);
4368 // EXPECT_THAT(page_ids, Contains(1));
4369 // EXPECT_THAT(page_ids, Contains(Gt(2)));
4370 // EXPECT_THAT(page_ids, Not(Contains(4)));
4371 //
4372 // ::std::map<int, size_t> page_lengths;
4373 // page_lengths[1] = 100;
4374 // EXPECT_THAT(page_lengths,
4375 // Contains(::std::pair<const int, size_t>(1, 100)));
4376 //
4377 // const char* user_ids[] = { "joe", "mike", "tom" };
4378 // EXPECT_THAT(user_ids, Contains(Eq(::std::string("tom"))));
4379 template <typename M>
4380 inline internal::ContainsMatcher<M> Contains(M matcher)
4381 {
4382  return internal::ContainsMatcher<M>(matcher);
4383 }
4384 
4385 // Matches an STL-style container or a native array that contains only
4386 // elements matching the given value or matcher.
4387 //
4388 // Each(m) is semantically equivalent to Not(Contains(Not(m))). Only
4389 // the messages are different.
4390 //
4391 // Examples:
4392 // ::std::set<int> page_ids;
4393 // // Each(m) matches an empty container, regardless of what m is.
4394 // EXPECT_THAT(page_ids, Each(Eq(1)));
4395 // EXPECT_THAT(page_ids, Each(Eq(77)));
4396 //
4397 // page_ids.insert(3);
4398 // EXPECT_THAT(page_ids, Each(Gt(0)));
4399 // EXPECT_THAT(page_ids, Not(Each(Gt(4))));
4400 // page_ids.insert(1);
4401 // EXPECT_THAT(page_ids, Not(Each(Lt(2))));
4402 //
4403 // ::std::map<int, size_t> page_lengths;
4404 // page_lengths[1] = 100;
4405 // page_lengths[2] = 200;
4406 // page_lengths[3] = 300;
4407 // EXPECT_THAT(page_lengths, Not(Each(Pair(1, 100))));
4408 // EXPECT_THAT(page_lengths, Each(Key(Le(3))));
4409 //
4410 // const char* user_ids[] = { "joe", "mike", "tom" };
4411 // EXPECT_THAT(user_ids, Not(Each(Eq(::std::string("tom")))));
4412 template <typename M>
4413 inline internal::EachMatcher<M> Each(M matcher)
4414 {
4415  return internal::EachMatcher<M>(matcher);
4416 }
4417 
4418 // Key(inner_matcher) matches an std::pair whose 'first' field matches
4419 // inner_matcher. For example, Contains(Key(Ge(5))) can be used to match an
4420 // std::map that contains at least one element whose key is >= 5.
4421 template <typename M>
4422 inline internal::KeyMatcher<M> Key(M inner_matcher)
4423 {
4424  return internal::KeyMatcher<M>(inner_matcher);
4425 }
4426 
4427 // Pair(first_matcher, second_matcher) matches a std::pair whose 'first' field
4428 // matches first_matcher and whose 'second' field matches second_matcher. For
4429 // example, EXPECT_THAT(map_type, ElementsAre(Pair(Ge(5), "foo"))) can be used
4430 // to match a std::map<int, string> that contains exactly one element whose key
4431 // is >= 5 and whose value equals "foo".
4432 template <typename FirstMatcher, typename SecondMatcher>
4434 Pair(FirstMatcher first_matcher, SecondMatcher second_matcher)
4435 {
4437  first_matcher, second_matcher);
4438 }
4439 
4440 // Returns a predicate that is satisfied by anything that matches the
4441 // given matcher.
4442 template <typename M>
4443 inline internal::MatcherAsPredicate<M> Matches(M matcher)
4444 {
4445  return internal::MatcherAsPredicate<M>(matcher);
4446 }
4447 
4448 // Returns true iff the value matches the matcher.
4449 template <typename T, typename M>
4450 inline bool Value(const T & value, M matcher)
4451 {
4452  return testing::Matches(matcher)(value);
4453 }
4454 
4455 // Matches the value against the given matcher and explains the match
4456 // result to listener.
4457 template <typename T, typename M>
4458 inline bool ExplainMatchResult(
4459  M matcher, const T & value, MatchResultListener * listener)
4460 {
4461  return SafeMatcherCast<const T &>(matcher).MatchAndExplain(value, listener);
4462 }
4463 
4464 #if GTEST_LANG_CXX11
4465 // Define variadic matcher versions. They are overloaded in
4466 // gmock-generated-matchers.h for the cases supported by pre C++11 compilers.
4467 template <typename... Args>
4468 inline internal::AllOfMatcher<Args...> AllOf(const Args & ... matchers)
4469 {
4470  return internal::AllOfMatcher<Args...>(matchers...);
4471 }
4472 
4473 template <typename... Args>
4474 inline internal::AnyOfMatcher<Args...> AnyOf(const Args & ... matchers)
4475 {
4476  return internal::AnyOfMatcher<Args...>(matchers...);
4477 }
4478 
4479 #endif // GTEST_LANG_CXX11
4480 
4481 // AllArgs(m) is a synonym of m. This is useful in
4482 //
4483 // EXPECT_CALL(foo, Bar(_, _)).With(AllArgs(Eq()));
4484 //
4485 // which is easier to read than
4486 //
4487 // EXPECT_CALL(foo, Bar(_, _)).With(Eq());
4488 template <typename InnerMatcher>
4489 inline InnerMatcher AllArgs(const InnerMatcher & matcher) { return matcher; }
4490 
4491 // These macros allow using matchers to check values in Google Test
4492 // tests. ASSERT_THAT(value, matcher) and EXPECT_THAT(value, matcher)
4493 // succeed iff the value matches the matcher. If the assertion fails,
4494 // the value and the description of the matcher will be printed.
4495 #define ASSERT_THAT(value, matcher) ASSERT_PRED_FORMAT1(\
4496  ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
4497 #define EXPECT_THAT(value, matcher) EXPECT_PRED_FORMAT1(\
4498  ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
4499 
4500 } // namespace testing
4501 
4502 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_
LhsView::const_reference LhsStlContainerReference
virtual void DescribeTo(::std::ostream *os) const
virtual void DescribeTo(::std::ostream *os) const
PolymorphicMatcher< internal::IsNullMatcher > IsNull()
virtual bool MatchAndExplain(Container container, MatchResultListener *listener) const
virtual bool MatchAndExplain(Container container, MatchResultListener *listener) const
virtual void DescribeTo(::std::ostream *os) const
internal::GtMatcher< Rhs > Gt(Rhs x)
bool MatchAndExplainImpl(true_type, const Class *p, MatchResultListener *listener) const
virtual bool MatchAndExplain(T x, MatchResultListener *listener) const
static void ExplainMatchFailuresTo(const MatcherTuple &matchers, const ValueTuple &values,::std::ostream *os)
void DescribeToHelper(bool expect_eq,::std::ostream *os) const
internal::StlContainerView< GTEST_REMOVE_REFERENCE_AND_CONST_(Container) > ContainerView
virtual void DescribeNegationTo(::std::ostream *os) const
StartsWithMatcher(const StringType &prefix)
StrEqualityMatcher(const StringType &str, bool expect_eq, bool case_sensitive)
internal::StlContainerView< GTEST_REMOVE_REFERENCE_AND_CONST_(LhsContainer) > LhsView
virtual void DescribeTo(::std::ostream *os) const
PolymorphicMatcher< internal::MatchesRegexMatcher > MatchesRegex(const internal::RE *regex)
CallableTraits< Callable >::StorageType CallableStorageType
static Matcher< T > Cast(M polymorphic_matcher_or_value)
internal::NeMatcher< Rhs > Ne(Rhs x)
Impl(const InnerMatcher &matcher)
bool MatchAndExplain(const T &value, MatchResultListener *listener) const
virtual void DescribeTo(::std::ostream *os) const
Matcher< T > An()
PolymorphicMatcher< internal::NotNullMatcher > NotNull()
AssertionResult AssertionFailure()
virtual void DescribeNegationTo(::std::ostream *os) const
int * count
internal::StlContainerView< RhsContainer > RhsView
EitherOfMatcher(Matcher1 matcher1, Matcher2 matcher2)
PolymorphicMatcher< internal::StartsWithMatcher< internal::string > > StartsWith(const internal::string &prefix)
virtual void DescribeNegationTo(::std::ostream *os) const
AssertionResult operator()(const char *value_text, const T &x) const
virtual bool MatchAndExplain(T value, MatchResultListener *) const
MatchResultListener(::std::ostream *os)
#define GTEST_API_
BothOfMatcher(Matcher1 matcher1, Matcher2 matcher2)
virtual void DescribeTo(::std::ostream *os) const
f
virtual void DescribeNegationTo(::std::ostream *os) const
virtual void DescribeTo(::std::ostream *os) const
internal::EqMatcher< T > Eq(T x)
void ExplainMatchResultTo(T x,::std::ostream *os) const
::std::string PrintToString(const T &value)
static Matcher< T > Cast(const Matcher< U > &source_matcher)
internal::StlContainerView< RawContainer > View
const char Message[]
Definition: strings.h:102
internal::SizeIsMatcher< SizeMatcher > SizeIs(const SizeMatcher &size_matcher)
RawPairType::second_type SecondType
Matcher(const MatcherInterface< const internal::string & > *impl)
GTEST_API_ bool FindPairing(const MatchMatrix &matrix, MatchResultListener *listener)
XmlRpcServer s
virtual bool MatchAndExplain(Pointer pointer, MatchResultListener *listener) const
virtual void DescribeTo(::std::ostream *os) const
void DescribeTo(::std::ostream *os) const
PolymorphicMatcher< internal::MatchesRegexMatcher > ContainsRegex(const internal::RE *regex)
size_t SpaceIndex(size_t ilhs, size_t irhs) const
static Matcher< T > Cast(const Matcher< U > &matcher)
internal::KeyMatcher< M > Key(M inner_matcher)
bool HasEdge(size_t ilhs, size_t irhs) const
EndsWithMatcher(const StringType &suffix)
virtual void DescribeNegationTo(::std::ostream *os) const
internal::string str() const
virtual bool MatchAndExplain(Container container, MatchResultListener *listener) const
InnerMatcher AllArgs(const InnerMatcher &matcher)
void DescribeTo(::std::ostream *os) const
void DescribeTo(::std::ostream *os) const
NotMatcherImpl(const Matcher< T > &matcher)
virtual void DescribeNegationTo(::std::ostream *os) const
bool MatchPrintAndExplain(Value &value, const Matcher< T > &matcher, MatchResultListener *listener)
virtual void DescribeNegationTo(::std::ostream *os) const
virtual void DescribeTo(::std::ostream *os) const
void DescribeNegationTo(::std::ostream *os) const
EachMatcherImpl(InnerMatcher inner_matcher)
internal::PointwiseMatcher< TupleMatcher, GTEST_REMOVE_CONST_(Container)> Pointwise(const TupleMatcher &tuple_matcher, const Container &rhs)
internal::FloatingEqMatcher< double > NanSensitiveDoubleEq(double rhs)
QuantifierMatcherImpl(InnerMatcher inner_matcher)
internal::ArgsMatcher< InnerMatcher > Args(const InnerMatcher &matcher)
bool TupleMatches(const MatcherTuple &matcher_tuple, const ValueTuple &value_tuple)
PolymorphicMatcher< internal::EndsWithMatcher< internal::string > > EndsWith(const internal::string &suffix)
#define GMOCK_IMPLEMENT_COMPARISON_MATCHER_(name, op, relation, negated_relation)
virtual bool MatchAndExplain(T obj, MatchResultListener *listener) const
internal::UnorderedElementsAreArrayMatcher< typename::std::iterator_traits< Iter >::value_type > UnorderedElementsAreArray(Iter first, Iter last)
#define GTEST_COMPILE_ASSERT_(expr, msg)
const Impl & impl() const
PolymorphicMatcher< internal::ContainerEqMatcher< GTEST_REMOVE_CONST_(Container)> > ContainerEq(const Container &rhs)
FieldMatcher(FieldType Class::*field, const Matcher< const FieldType & > &matcher)
static Matcher< T > Cast(M polymorphic_matcher_or_value)
bool MatchAndExplainImpl(false_type, const Class &obj, MatchResultListener *listener) const
bool MatchAndExplain(T &x, MatchResultListener *) const
View::const_reference StlContainerReference
virtual bool MatchAndExplain(PairType key_value, MatchResultListener *listener) const
virtual bool MatchAndExplain(Container container, MatchResultListener *listener) const
virtual void DescribeNegationTo(::std::ostream *os) const
internal::AllOfResult2< M1, M2 >::type AllOf(M1 m1, M2 m2)
internal::RefMatcher< T & > Ref(T &x)
PolymorphicMatcher< internal::StrEqualityMatcher< internal::string > > StrEq(const internal::string &str)
virtual void DescribeTo(::std::ostream *os) const
::std::vector< ElementMatcherPair > ElementMatcherPairs
void DescribeTo(::std::ostream *os) const
Impl(const SizeMatcher &size_matcher)
void DescribeNegationTo(::std::ostream *os) const
PairMatcher(FirstMatcher first_matcher, SecondMatcher second_matcher)
bool MatchAndExplain(CharType *s, MatchResultListener *listener) const
internal::LtMatcher< Rhs > Lt(Rhs x)
ElementsAreMatcher(const MatcherTuple &args)
void DescribeNegationTo(::std::ostream *os) const
static Matcher< T > CastImpl(M polymorphic_matcher_or_value, BooleanConstant< true >)
MatcherBase(const MatcherInterface< T > *impl)
internal::FloatingEqMatcher< double > DoubleNear(double rhs, double max_abs_error)
FloatingEqMatcher(FloatType rhs, bool nan_eq_nan)
bool MatchAndExplain(T x, MatchResultListener *listener) const
internal::FloatingEqMatcher< double > DoubleEq(double rhs)
bool MatchAndExplain(CharType *s, MatchResultListener *listener) const
void DescribeTo(::std::ostream *os) const
NotMatcher(InnerMatcher matcher)
Matcher(const MatcherInterface< T > *impl)
virtual bool MatchAndExplain(T x, MatchResultListener *listener) const
virtual void DescribeNegationTo(::std::ostream *os) const
void DescribeNegationTo(::std::ostream *os) const
virtual void DescribeNegationTo(::std::ostream *os) const
MatchMatrix(size_t num_elements, size_t num_matchers)
internal::LeMatcher< Rhs > Le(Rhs x)
MatchesRegexMatcher(const RE *regex, bool full_match)
RawPairType::first_type FirstType
AssertionResult AssertionSuccess()
PolymorphicMatcher< Impl > MakePolymorphicMatcher(const Impl &impl)
void DescribeTo(::std::ostream *os) const
PolymorphicMatcher< internal::StrEqualityMatcher< internal::string > > StrCaseNe(const internal::string &str)
StlContainer::const_iterator StlContainerConstIterator
BothOfMatcherImpl(const Matcher< T > &matcher1, const Matcher< T > &matcher2)
static void ExplainMatchFailuresTo(const MatcherTuple &, const ValueTuple &,::std::ostream *)
static Matcher< T > CastImpl(M value, BooleanConstant< false >)
void DescribeNegationTo(::std::ostream *os) const
void DescribeTo(::std::ostream *os) const
internal::ElementsAreArrayMatcher< typename::std::iterator_traits< Iter >::value_type > ElementsAreArray(Iter first, Iter last)
static OutIter Run(Func f, const Tuple &t, OutIter out)
PolymorphicMatcher(const Impl &an_impl)
MatchMatrix AnalyzeElements(ElementIter elem_first, ElementIter elem_last,::std::vector< string > *element_printouts, MatchResultListener *listener) const
MatchResultListener & operator<<(const T &x)
bool MatchAndExplainImpl(true_type, const Class *p, MatchResultListener *listener) const
bool MatchAndExplain(const T &value, MatchResultListener *listener) const
::std::vector< Matcher< const Element & > > MatcherVec
PolymorphicMatcher< internal::HasSubstrMatcher< internal::string > > HasSubstr(const internal::string &substring)
void DescribeNegationTo(::std::ostream *os) const
bool Value(const T &value, M matcher)
virtual bool MatchAndExplain(T x, MatchResultListener *listener) const
void DescribeTo(::std::ostream *os) const
internal::PairMatcher< FirstMatcher, SecondMatcher > Pair(FirstMatcher first_matcher, SecondMatcher second_matcher)
void PrintIfNotEmpty(const internal::string &explanation,::std::ostream *os)
virtual bool MatchAndExplain(Super &x, MatchResultListener *listener) const
internal::NotMatcher< InnerMatcher > Not(InnerMatcher m)
bool MatchAndExplain(const Pointer &p, MatchResultListener *) const
virtual void DescribeNegationTo(::std::ostream *os) const
virtual void DescribeNegationTo(::std::ostream *os) const
virtual void DescribeTo(::std::ostream *os) const
PropertyMatcher(PropertyType(Class::*property)() const, const Matcher< RefToConstProperty > &matcher)
internal::AnyOfResult2< M1, M2 >::type AnyOf(M1 m1, M2 m2)
bool CaseInsensitiveCStringEquals(const char *lhs, const char *rhs)
internal::MatcherAsPredicate< M > Matches(M matcher)
virtual bool MatchAndExplain(T x, MatchResultListener *listener) const
Matcher< T > MakeMatcher(const MatcherInterface< T > *impl)
Matcher< T > SafeMatcherCast(const M &polymorphic_matcher)
virtual bool MatchAndExplain(LhsContainer lhs, MatchResultListener *listener) const
UnorderedElementsAreMatcherImpl(InputIter first, InputIter last)
::std::pair< size_t, size_t > ElementMatcherPair
void UniversalPrint(const T &value,::std::ostream *os)
internal::FloatingEqMatcher< float > NanSensitiveFloatEq(float rhs)
virtual void DescribeTo(::std::ostream *os) const
#define GTEST_REMOVE_REFERENCE_AND_CONST_(T)
void DescribeNegationTo(::std::ostream *os) const
#define GTEST_DISALLOW_ASSIGN_(type)
void DescribeTo(::std::ostream *os) const
virtual void DescribeNegationTo(::std::ostream *os) const
bool MatchAndExplain(CharType *s, MatchResultListener *listener) const
#define GTEST_REMOVE_REFERENCE_(T)
GTEST_API_ string FormatMatcherDescription(bool negation, const char *matcher_name, const Strings &param_values)
bool MatchAndExplain(CharType *s, MatchResultListener *listener) const
internal::PointeeMatcher< InnerMatcher > Pointee(const InnerMatcher &inner_matcher)
virtual bool MatchAndExplain(PairType a_pair, MatchResultListener *listener) const
void DescribeNegationTo(::std::ostream *os) const
GTEST_DISALLOW_COPY_AND_ASSIGN_(MatchResultListener)
#define GMOCK_IMPLEMENT_COMPARISON2_MATCHER_(name, op, relation)
PairMatcherImpl(FirstMatcher first_matcher, SecondMatcher second_matcher)
#define GTEST_REFERENCE_TO_CONST_(T)
PolymorphicMatcher< internal::FieldMatcher< Class, FieldType > > Field(FieldType Class::*field, const FieldMatcher &matcher)
virtual bool MatchAndExplain(LhsContainer lhs, MatchResultListener *listener) const
bool MatchAndExplain(const Pointer &p, MatchResultListener *) const
bool MatchAndExplain(CharType *s, MatchResultListener *listener) const
virtual void DescribeNegationTo(::std::ostream *os) const
void DescribeTo(::std::ostream *os) const
ElementsAreMatcherImpl(InputIter first, InputIter last)
virtual void DescribeTo(::std::ostream *os) const
ContainsMatcherImpl(InnerMatcher inner_matcher)
static Matcher< T > Cast(const Matcher< T > &matcher)
internal::FloatingEqMatcher< float > NanSensitiveFloatNear(float rhs, float max_abs_error)
virtual void DescribeTo(::std::ostream *os) const
static bool Matches(const MatcherTuple &matcher_tuple, const ValueTuple &value_tuple)
static ResType Invoke(ResType(*f)(ArgType), T arg)
void DescribeTo(::std::ostream *os) const
PolymorphicMatcher< internal::StrEqualityMatcher< internal::string > > StrNe(const internal::string &str)
PolymorphicMatcher< internal::PropertyMatcher< Class, PropertyType > > Property(PropertyType(Class::*property)() const, const PropertyMatcher &matcher)
bool operator()(const T &lhs, const U &rhs) const
PredicateFormatterFromMatcher< M > MakePredicateFormatterFromMatcher(const M &matcher)
void ExplainMatchFailureTupleTo(const MatcherTuple &matchers, const ValueTuple &values,::std::ostream *os)
bool MatchAndExplain(const MatcheeStringType &s, MatchResultListener *) const
ResultOfMatcher(Callable callable, const Matcher< ResultType > &matcher)
virtual void DescribeTo(::std::ostream *os) const
HasSubstrMatcher(const StringType &substring)
void SetEdge(size_t ilhs, size_t irhs, bool b)
bool MatchAndExplain(const LhsContainer &lhs, MatchResultListener *listener) const
Matcher< Lhs > TypedEq(const Rhs &rhs)
#define GMOCK_KIND_OF_(type)
internal::WhenSortedByMatcher< internal::LessComparator, ContainerMatcher > WhenSorted(const ContainerMatcher &container_matcher)
internal::FloatingEqMatcher< double > NanSensitiveDoubleNear(double rhs, double max_abs_error)
internal::NamedArg< char > arg(StringRef name, const T &arg)
Definition: format.h:3918
bool ExplainMatchResult(M matcher, const T &value, MatchResultListener *listener)
bool MatchAndExplain(const MatcheeStringType &s, MatchResultListener *) const
FloatingEqMatcher(FloatType rhs, bool nan_eq_nan, FloatType max_abs_error)
RawPairType::first_type KeyType
Matcher< T > MatcherCast(M matcher)
virtual void DescribeTo(::std::ostream *os) const
static bool Matches(const MatcherTuple &, const ValueTuple &)
ContainerView::type::size_type SizeType
WhenSortedByMatcher(const Comparator &comparator, const ContainerMatcher &matcher)
Impl(const TupleMatcher &tuple_matcher, const RhsStlContainer &rhs)
const MatcherDescriberInterface * GetDescriber() const
LhsView::const_reference LhsStlContainerReference
void DescribeTo(::std::ostream *os) const
Matcher< T > A()
internal::StlContainerView< GTEST_REMOVE_REFERENCE_AND_CONST_(LhsContainer) > LhsView
bool MatchAndExplain(const MatcheeStringType &s, MatchResultListener *) const
virtual void DescribeNegationTo(::std::ostream *os) const
virtual void DescribeTo(::std::ostream *os) const
GTEST_API_ ElementMatcherPairs FindMaxBipartiteMatching(const MatchMatrix &g)
internal::EachMatcher< M > Each(M matcher)
bool IsReadableTypeName(const string &type_name)
PointeeOf< GTEST_REMOVE_CONST_(GTEST_REMOVE_REFERENCE_(Pointer))>::type Pointee
OutIter TransformTupleValues(Func f, const Tuple &t, OutIter out)
LhsStlContainer::value_type LhsValue
::std::vector< string > Strings
Matcher< Target > operator()(const Arg &a) const
#define GTEST_REMOVE_CONST_(T)
internal::FloatingEqMatcher< float > FloatEq(float rhs)
bool MatchAndExplain(const MatcheeStringType &s, MatchResultListener *) const
virtual void DescribeTo(::std::ostream *os) const
const Pointer::element_type * GetRawPointer(const Pointer &p)
bool_constant< false > false_type
bool CaseInsensitiveStringEquals(const StringType &s1, const StringType &s2)
void DescribeNegationTo(::std::ostream *os) const
virtual bool MatchAndExplain(T, MatchResultListener *) const
const internal::AnythingMatcher _
virtual void DescribeNegationTo(::std::ostream *os) const
PointeeMatcher(const InnerMatcher &matcher)
virtual bool MatchAndExplain(Container container, MatchResultListener *listener) const
void DescribeNegationTo(::std::ostream *os) const
bool MatchAndExplainImpl(false_type, const Class &obj, MatchResultListener *listener) const
::std::tr1::tuple_size< Tuple > TupleSize
KeyMatcherImpl(InnerMatcher inner_matcher)
CallableTraits< Callable >::ResultType ResultType
StlContainerView< RawContainer > View
RemoveConstFromKey< typename LhsStlContainer::value_type >::type LhsValue
UnorderedElementsAreMatcher(const MatcherTuple &args)
virtual bool MatchAndExplain(T x, MatchResultListener *listener) const
virtual void DescribeNegationTo(::std::ostream *os) const
ContainerEqMatcher(const Container &rhs)
virtual void DescribeTo(::std::ostream *os) const
OutIter operator()(Func f, const Tup &t, OutIter out) const
internal::GeMatcher< Rhs > Ge(Rhs x)
Iter ArrayAwareFind(Iter begin, Iter end, const Element &elem)
PolymorphicMatcher< internal::StrEqualityMatcher< internal::string > > StrCaseEq(const internal::string &str)
void DescribeNegationTo(::std::ostream *os) const
void ExplainSuccess(const internal::string &first_explanation, const internal::string &second_explanation, MatchResultListener *listener) const
internal::StlContainerView< RawContainer > View
void DescribeNegationTo(::std::ostream *os) const
EitherOfMatcherImpl(const Matcher< T > &matcher1, const Matcher< T > &matcher2)
internal::ContainsMatcher< M > Contains(M matcher)
#define GTEST_CHECK_(condition)
Impl(FloatType rhs, bool nan_eq_nan, FloatType max_abs_error)
internal::StlContainerView< Container > View
internal::ResultOfMatcher< Callable > ResultOf(Callable callable, const ResultOfMatcher &matcher)
PolymorphicMatcher< internal::TrulyMatcher< Predicate > > Truly(Predicate pred)
PointwiseMatcher(const TupleMatcher &tuple_matcher, const RhsContainer &rhs)
Impl(CallableStorageType callable, const Matcher< ResultType > &matcher)
Matcher(const MatcherInterface< internal::string > *impl)
static Message Elements(size_t count)
bool MatchAndExplain(const MatcheeStringType &s, MatchResultListener *) const
::std::tr1::tuple< const LhsValue &, const RhsValue & > InnerMatcherArg
virtual void DescribeNegationTo(::std::ostream *os) const
SizeIsMatcher(const SizeMatcher &size_matcher)
virtual void DescribeNegationTo(::std::ostream *os) const
::std::vector< const MatcherDescriberInterface * > MatcherDescriberVec
internal::WhenSortedByMatcher< Comparator, ContainerMatcher > WhenSortedBy(const Comparator &comparator, const ContainerMatcher &container_matcher)
bool MatchAndExplainImpl(bool all_elements_should_match, Container container, MatchResultListener *listener) const
static ResultType Invoke(Functor f, T arg)
RhsStlContainer::value_type RhsValue
Impl(const Comparator &comparator, const ContainerMatcher &matcher)
virtual void DescribeNegationTo(::std::ostream *os) const
internal::FloatingEqMatcher< float > FloatNear(float rhs, float max_abs_error)


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:06:06