gmock-matchers.h revision f5e1ce5b9237edbc2e524ae9ebcb2452dc842937
1e35fdd936d133bf8a48de140a3c666897588a05shiqian// Copyright 2007, Google Inc.
2e35fdd936d133bf8a48de140a3c666897588a05shiqian// All rights reserved.
3e35fdd936d133bf8a48de140a3c666897588a05shiqian//
4e35fdd936d133bf8a48de140a3c666897588a05shiqian// Redistribution and use in source and binary forms, with or without
5e35fdd936d133bf8a48de140a3c666897588a05shiqian// modification, are permitted provided that the following conditions are
6e35fdd936d133bf8a48de140a3c666897588a05shiqian// met:
7e35fdd936d133bf8a48de140a3c666897588a05shiqian//
8e35fdd936d133bf8a48de140a3c666897588a05shiqian//     * Redistributions of source code must retain the above copyright
9e35fdd936d133bf8a48de140a3c666897588a05shiqian// notice, this list of conditions and the following disclaimer.
10e35fdd936d133bf8a48de140a3c666897588a05shiqian//     * Redistributions in binary form must reproduce the above
11e35fdd936d133bf8a48de140a3c666897588a05shiqian// copyright notice, this list of conditions and the following disclaimer
12e35fdd936d133bf8a48de140a3c666897588a05shiqian// in the documentation and/or other materials provided with the
13e35fdd936d133bf8a48de140a3c666897588a05shiqian// distribution.
14e35fdd936d133bf8a48de140a3c666897588a05shiqian//     * Neither the name of Google Inc. nor the names of its
15e35fdd936d133bf8a48de140a3c666897588a05shiqian// contributors may be used to endorse or promote products derived from
16e35fdd936d133bf8a48de140a3c666897588a05shiqian// this software without specific prior written permission.
17e35fdd936d133bf8a48de140a3c666897588a05shiqian//
18e35fdd936d133bf8a48de140a3c666897588a05shiqian// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19e35fdd936d133bf8a48de140a3c666897588a05shiqian// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20e35fdd936d133bf8a48de140a3c666897588a05shiqian// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21e35fdd936d133bf8a48de140a3c666897588a05shiqian// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22e35fdd936d133bf8a48de140a3c666897588a05shiqian// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23e35fdd936d133bf8a48de140a3c666897588a05shiqian// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24e35fdd936d133bf8a48de140a3c666897588a05shiqian// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25e35fdd936d133bf8a48de140a3c666897588a05shiqian// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26e35fdd936d133bf8a48de140a3c666897588a05shiqian// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27e35fdd936d133bf8a48de140a3c666897588a05shiqian// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28e35fdd936d133bf8a48de140a3c666897588a05shiqian// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29e35fdd936d133bf8a48de140a3c666897588a05shiqian//
30e35fdd936d133bf8a48de140a3c666897588a05shiqian// Author: wan@google.com (Zhanyong Wan)
31e35fdd936d133bf8a48de140a3c666897588a05shiqian
32e35fdd936d133bf8a48de140a3c666897588a05shiqian// Google Mock - a framework for writing C++ mock classes.
33e35fdd936d133bf8a48de140a3c666897588a05shiqian//
34e35fdd936d133bf8a48de140a3c666897588a05shiqian// This file implements some commonly used argument matchers.  More
35e35fdd936d133bf8a48de140a3c666897588a05shiqian// matchers can be defined by the user implementing the
36e35fdd936d133bf8a48de140a3c666897588a05shiqian// MatcherInterface<T> interface if necessary.
37e35fdd936d133bf8a48de140a3c666897588a05shiqian
38e35fdd936d133bf8a48de140a3c666897588a05shiqian#ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_
39e35fdd936d133bf8a48de140a3c666897588a05shiqian#define GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_
40e35fdd936d133bf8a48de140a3c666897588a05shiqian
416a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan#include <algorithm>
4216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#include <limits>
43e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <ostream>  // NOLINT
44e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <sstream>
45e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <string>
46e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <vector>
47e35fdd936d133bf8a48de140a3c666897588a05shiqian
48e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <gmock/gmock-printers.h>
49e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <gmock/internal/gmock-internal-utils.h>
50e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <gmock/internal/gmock-port.h>
51e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <gtest/gtest.h>
52e35fdd936d133bf8a48de140a3c666897588a05shiqian
53e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace testing {
54e35fdd936d133bf8a48de140a3c666897588a05shiqian
55e35fdd936d133bf8a48de140a3c666897588a05shiqian// To implement a matcher Foo for type T, define:
56e35fdd936d133bf8a48de140a3c666897588a05shiqian//   1. a class FooMatcherImpl that implements the
57e35fdd936d133bf8a48de140a3c666897588a05shiqian//      MatcherInterface<T> interface, and
58e35fdd936d133bf8a48de140a3c666897588a05shiqian//   2. a factory function that creates a Matcher<T> object from a
59e35fdd936d133bf8a48de140a3c666897588a05shiqian//      FooMatcherImpl*.
60e35fdd936d133bf8a48de140a3c666897588a05shiqian//
61e35fdd936d133bf8a48de140a3c666897588a05shiqian// The two-level delegation design makes it possible to allow a user
62e35fdd936d133bf8a48de140a3c666897588a05shiqian// to write "v" instead of "Eq(v)" where a Matcher is expected, which
63e35fdd936d133bf8a48de140a3c666897588a05shiqian// is impossible if we pass matchers by pointers.  It also eases
64e35fdd936d133bf8a48de140a3c666897588a05shiqian// ownership management as Matcher objects can now be copied like
65e35fdd936d133bf8a48de140a3c666897588a05shiqian// plain values.
66e35fdd936d133bf8a48de140a3c666897588a05shiqian
67e35fdd936d133bf8a48de140a3c666897588a05shiqian// The implementation of a matcher.
68e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
69e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatcherInterface {
70e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
71e35fdd936d133bf8a48de140a3c666897588a05shiqian  virtual ~MatcherInterface() {}
72e35fdd936d133bf8a48de140a3c666897588a05shiqian
73e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Returns true iff the matcher matches x.
74e35fdd936d133bf8a48de140a3c666897588a05shiqian  virtual bool Matches(T x) const = 0;
75e35fdd936d133bf8a48de140a3c666897588a05shiqian
76e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Describes this matcher to an ostream.
77e35fdd936d133bf8a48de140a3c666897588a05shiqian  virtual void DescribeTo(::std::ostream* os) const = 0;
78e35fdd936d133bf8a48de140a3c666897588a05shiqian
79e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Describes the negation of this matcher to an ostream.  For
80e35fdd936d133bf8a48de140a3c666897588a05shiqian  // example, if the description of this matcher is "is greater than
81e35fdd936d133bf8a48de140a3c666897588a05shiqian  // 7", the negated description could be "is not greater than 7".
82e35fdd936d133bf8a48de140a3c666897588a05shiqian  // You are not required to override this when implementing
83e35fdd936d133bf8a48de140a3c666897588a05shiqian  // MatcherInterface, but it is highly advised so that your matcher
84e35fdd936d133bf8a48de140a3c666897588a05shiqian  // can produce good error messages.
85e35fdd936d133bf8a48de140a3c666897588a05shiqian  virtual void DescribeNegationTo(::std::ostream* os) const {
86e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "not (";
87e35fdd936d133bf8a48de140a3c666897588a05shiqian    DescribeTo(os);
88e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << ")";
89e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
90e35fdd936d133bf8a48de140a3c666897588a05shiqian
91e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Explains why x matches, or doesn't match, the matcher.  Override
92e35fdd936d133bf8a48de140a3c666897588a05shiqian  // this to provide any additional information that helps a user
93e35fdd936d133bf8a48de140a3c666897588a05shiqian  // understand the match result.
943fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.wan  virtual void ExplainMatchResultTo(T /* x */, ::std::ostream* /* os */) const {
95e35fdd936d133bf8a48de140a3c666897588a05shiqian    // By default, nothing more needs to be explained, as Google Mock
96e35fdd936d133bf8a48de140a3c666897588a05shiqian    // has already printed the value of x when this function is
97e35fdd936d133bf8a48de140a3c666897588a05shiqian    // called.
98e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
99e35fdd936d133bf8a48de140a3c666897588a05shiqian};
100e35fdd936d133bf8a48de140a3c666897588a05shiqian
101e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace internal {
102e35fdd936d133bf8a48de140a3c666897588a05shiqian
103e35fdd936d133bf8a48de140a3c666897588a05shiqian// An internal class for implementing Matcher<T>, which will derive
104e35fdd936d133bf8a48de140a3c666897588a05shiqian// from it.  We put functionalities common to all Matcher<T>
105e35fdd936d133bf8a48de140a3c666897588a05shiqian// specializations here to avoid code duplication.
106e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
107e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatcherBase {
108e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
109e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Returns true iff this matcher matches x.
110e35fdd936d133bf8a48de140a3c666897588a05shiqian  bool Matches(T x) const { return impl_->Matches(x); }
111e35fdd936d133bf8a48de140a3c666897588a05shiqian
112e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Describes this matcher to an ostream.
113e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const { impl_->DescribeTo(os); }
114e35fdd936d133bf8a48de140a3c666897588a05shiqian
115e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Describes the negation of this matcher to an ostream.
116e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
117e35fdd936d133bf8a48de140a3c666897588a05shiqian    impl_->DescribeNegationTo(os);
118e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
119e35fdd936d133bf8a48de140a3c666897588a05shiqian
120e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Explains why x matches, or doesn't match, the matcher.
121e35fdd936d133bf8a48de140a3c666897588a05shiqian  void ExplainMatchResultTo(T x, ::std::ostream* os) const {
122e35fdd936d133bf8a48de140a3c666897588a05shiqian    impl_->ExplainMatchResultTo(x, os);
123e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
124e35fdd936d133bf8a48de140a3c666897588a05shiqian protected:
125e35fdd936d133bf8a48de140a3c666897588a05shiqian  MatcherBase() {}
126e35fdd936d133bf8a48de140a3c666897588a05shiqian
127e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Constructs a matcher from its implementation.
128e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit MatcherBase(const MatcherInterface<T>* impl)
129e35fdd936d133bf8a48de140a3c666897588a05shiqian      : impl_(impl) {}
130e35fdd936d133bf8a48de140a3c666897588a05shiqian
131e35fdd936d133bf8a48de140a3c666897588a05shiqian  virtual ~MatcherBase() {}
132e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
133e35fdd936d133bf8a48de140a3c666897588a05shiqian  // shared_ptr (util/gtl/shared_ptr.h) and linked_ptr have similar
134e35fdd936d133bf8a48de140a3c666897588a05shiqian  // interfaces.  The former dynamically allocates a chunk of memory
135e35fdd936d133bf8a48de140a3c666897588a05shiqian  // to hold the reference count, while the latter tracks all
136e35fdd936d133bf8a48de140a3c666897588a05shiqian  // references using a circular linked list without allocating
137e35fdd936d133bf8a48de140a3c666897588a05shiqian  // memory.  It has been observed that linked_ptr performs better in
138e35fdd936d133bf8a48de140a3c666897588a05shiqian  // typical scenarios.  However, shared_ptr can out-perform
139e35fdd936d133bf8a48de140a3c666897588a05shiqian  // linked_ptr when there are many more uses of the copy constructor
140e35fdd936d133bf8a48de140a3c666897588a05shiqian  // than the default constructor.
141e35fdd936d133bf8a48de140a3c666897588a05shiqian  //
142e35fdd936d133bf8a48de140a3c666897588a05shiqian  // If performance becomes a problem, we should see if using
143e35fdd936d133bf8a48de140a3c666897588a05shiqian  // shared_ptr helps.
144e35fdd936d133bf8a48de140a3c666897588a05shiqian  ::testing::internal::linked_ptr<const MatcherInterface<T> > impl_;
145e35fdd936d133bf8a48de140a3c666897588a05shiqian};
146e35fdd936d133bf8a48de140a3c666897588a05shiqian
147e35fdd936d133bf8a48de140a3c666897588a05shiqian// The default implementation of ExplainMatchResultTo() for
148e35fdd936d133bf8a48de140a3c666897588a05shiqian// polymorphic matchers.
149e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename PolymorphicMatcherImpl, typename T>
1503fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.waninline void ExplainMatchResultTo(const PolymorphicMatcherImpl& /* impl */,
1513fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.wan                                 const T& /* x */,
1523fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.wan                                 ::std::ostream* /* os */) {
153e35fdd936d133bf8a48de140a3c666897588a05shiqian  // By default, nothing more needs to be said, as Google Mock already
154e35fdd936d133bf8a48de140a3c666897588a05shiqian  // prints the value of x elsewhere.
155e35fdd936d133bf8a48de140a3c666897588a05shiqian}
156e35fdd936d133bf8a48de140a3c666897588a05shiqian
157e35fdd936d133bf8a48de140a3c666897588a05shiqian}  // namespace internal
158e35fdd936d133bf8a48de140a3c666897588a05shiqian
159e35fdd936d133bf8a48de140a3c666897588a05shiqian// A Matcher<T> is a copyable and IMMUTABLE (except by assignment)
160e35fdd936d133bf8a48de140a3c666897588a05shiqian// object that can check whether a value of type T matches.  The
161e35fdd936d133bf8a48de140a3c666897588a05shiqian// implementation of Matcher<T> is just a linked_ptr to const
162e35fdd936d133bf8a48de140a3c666897588a05shiqian// MatcherInterface<T>, so copying is fairly cheap.  Don't inherit
163e35fdd936d133bf8a48de140a3c666897588a05shiqian// from Matcher!
164e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
165e35fdd936d133bf8a48de140a3c666897588a05shiqianclass Matcher : public internal::MatcherBase<T> {
166e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
167e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Constructs a null matcher.  Needed for storing Matcher objects in
168e35fdd936d133bf8a48de140a3c666897588a05shiqian  // STL containers.
169e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher() {}
170e35fdd936d133bf8a48de140a3c666897588a05shiqian
171e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Constructs a matcher from its implementation.
172e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit Matcher(const MatcherInterface<T>* impl)
173e35fdd936d133bf8a48de140a3c666897588a05shiqian      : internal::MatcherBase<T>(impl) {}
174e35fdd936d133bf8a48de140a3c666897588a05shiqian
17518490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  // Implicit constructor here allows people to write
176e35fdd936d133bf8a48de140a3c666897588a05shiqian  // EXPECT_CALL(foo, Bar(5)) instead of EXPECT_CALL(foo, Bar(Eq(5))) sometimes
177e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher(T value);  // NOLINT
178e35fdd936d133bf8a48de140a3c666897588a05shiqian};
179e35fdd936d133bf8a48de140a3c666897588a05shiqian
180e35fdd936d133bf8a48de140a3c666897588a05shiqian// The following two specializations allow the user to write str
181e35fdd936d133bf8a48de140a3c666897588a05shiqian// instead of Eq(str) and "foo" instead of Eq("foo") when a string
182e35fdd936d133bf8a48de140a3c666897588a05shiqian// matcher is expected.
183e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <>
184e35fdd936d133bf8a48de140a3c666897588a05shiqianclass Matcher<const internal::string&>
185e35fdd936d133bf8a48de140a3c666897588a05shiqian    : public internal::MatcherBase<const internal::string&> {
186e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
187e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher() {}
188e35fdd936d133bf8a48de140a3c666897588a05shiqian
189e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit Matcher(const MatcherInterface<const internal::string&>* impl)
190e35fdd936d133bf8a48de140a3c666897588a05shiqian      : internal::MatcherBase<const internal::string&>(impl) {}
191e35fdd936d133bf8a48de140a3c666897588a05shiqian
192e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Allows the user to write str instead of Eq(str) sometimes, where
193e35fdd936d133bf8a48de140a3c666897588a05shiqian  // str is a string object.
194e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher(const internal::string& s);  // NOLINT
195e35fdd936d133bf8a48de140a3c666897588a05shiqian
196e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Allows the user to write "foo" instead of Eq("foo") sometimes.
197e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher(const char* s);  // NOLINT
198e35fdd936d133bf8a48de140a3c666897588a05shiqian};
199e35fdd936d133bf8a48de140a3c666897588a05shiqian
200e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <>
201e35fdd936d133bf8a48de140a3c666897588a05shiqianclass Matcher<internal::string>
202e35fdd936d133bf8a48de140a3c666897588a05shiqian    : public internal::MatcherBase<internal::string> {
203e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
204e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher() {}
205e35fdd936d133bf8a48de140a3c666897588a05shiqian
206e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit Matcher(const MatcherInterface<internal::string>* impl)
207e35fdd936d133bf8a48de140a3c666897588a05shiqian      : internal::MatcherBase<internal::string>(impl) {}
208e35fdd936d133bf8a48de140a3c666897588a05shiqian
209e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Allows the user to write str instead of Eq(str) sometimes, where
210e35fdd936d133bf8a48de140a3c666897588a05shiqian  // str is a string object.
211e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher(const internal::string& s);  // NOLINT
212e35fdd936d133bf8a48de140a3c666897588a05shiqian
213e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Allows the user to write "foo" instead of Eq("foo") sometimes.
214e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher(const char* s);  // NOLINT
215e35fdd936d133bf8a48de140a3c666897588a05shiqian};
216e35fdd936d133bf8a48de140a3c666897588a05shiqian
217e35fdd936d133bf8a48de140a3c666897588a05shiqian// The PolymorphicMatcher class template makes it easy to implement a
218e35fdd936d133bf8a48de140a3c666897588a05shiqian// polymorphic matcher (i.e. a matcher that can match values of more
219e35fdd936d133bf8a48de140a3c666897588a05shiqian// than one type, e.g. Eq(n) and NotNull()).
220e35fdd936d133bf8a48de140a3c666897588a05shiqian//
221e35fdd936d133bf8a48de140a3c666897588a05shiqian// To define a polymorphic matcher, a user first provides a Impl class
222e35fdd936d133bf8a48de140a3c666897588a05shiqian// that has a Matches() method, a DescribeTo() method, and a
223e35fdd936d133bf8a48de140a3c666897588a05shiqian// DescribeNegationTo() method.  The Matches() method is usually a
224e35fdd936d133bf8a48de140a3c666897588a05shiqian// method template (such that it works with multiple types).  Then the
225e35fdd936d133bf8a48de140a3c666897588a05shiqian// user creates the polymorphic matcher using
226e35fdd936d133bf8a48de140a3c666897588a05shiqian// MakePolymorphicMatcher().  To provide additional explanation to the
227e35fdd936d133bf8a48de140a3c666897588a05shiqian// match result, define a FREE function (or function template)
228e35fdd936d133bf8a48de140a3c666897588a05shiqian//
229e35fdd936d133bf8a48de140a3c666897588a05shiqian//   void ExplainMatchResultTo(const Impl& matcher, const Value& value,
230e35fdd936d133bf8a48de140a3c666897588a05shiqian//                             ::std::ostream* os);
231e35fdd936d133bf8a48de140a3c666897588a05shiqian//
232e35fdd936d133bf8a48de140a3c666897588a05shiqian// in the SAME NAME SPACE where Impl is defined.  See the definition
233e35fdd936d133bf8a48de140a3c666897588a05shiqian// of NotNull() for a complete example.
234e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <class Impl>
235e35fdd936d133bf8a48de140a3c666897588a05shiqianclass PolymorphicMatcher {
236e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
237e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit PolymorphicMatcher(const Impl& impl) : impl_(impl) {}
238e35fdd936d133bf8a48de140a3c666897588a05shiqian
2392b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan  // Returns a mutable reference to the underlying matcher
2402b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan  // implementation object.
2412b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan  Impl& mutable_impl() { return impl_; }
2422b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan
2432b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan  // Returns an immutable reference to the underlying matcher
2442b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan  // implementation object.
2452b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan  const Impl& impl() const { return impl_; }
2462b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan
247e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
248e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<T>() const {
249e35fdd936d133bf8a48de140a3c666897588a05shiqian    return Matcher<T>(new MonomorphicImpl<T>(impl_));
250e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
251e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
252e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
253e35fdd936d133bf8a48de140a3c666897588a05shiqian  class MonomorphicImpl : public MatcherInterface<T> {
254e35fdd936d133bf8a48de140a3c666897588a05shiqian   public:
255e35fdd936d133bf8a48de140a3c666897588a05shiqian    explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {}
256e35fdd936d133bf8a48de140a3c666897588a05shiqian
257e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual bool Matches(T x) const { return impl_.Matches(x); }
258e35fdd936d133bf8a48de140a3c666897588a05shiqian
259e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeTo(::std::ostream* os) const {
260e35fdd936d133bf8a48de140a3c666897588a05shiqian      impl_.DescribeTo(os);
261e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
262e35fdd936d133bf8a48de140a3c666897588a05shiqian
263e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeNegationTo(::std::ostream* os) const {
264e35fdd936d133bf8a48de140a3c666897588a05shiqian      impl_.DescribeNegationTo(os);
265e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
266e35fdd936d133bf8a48de140a3c666897588a05shiqian
267e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void ExplainMatchResultTo(T x, ::std::ostream* os) const {
268e35fdd936d133bf8a48de140a3c666897588a05shiqian      using ::testing::internal::ExplainMatchResultTo;
269e35fdd936d133bf8a48de140a3c666897588a05shiqian
270e35fdd936d133bf8a48de140a3c666897588a05shiqian      // C++ uses Argument-Dependent Look-up (aka Koenig Look-up) to
271e35fdd936d133bf8a48de140a3c666897588a05shiqian      // resolve the call to ExplainMatchResultTo() here.  This
272e35fdd936d133bf8a48de140a3c666897588a05shiqian      // means that if there's a ExplainMatchResultTo() function
273e35fdd936d133bf8a48de140a3c666897588a05shiqian      // defined in the name space where class Impl is defined, it
274e35fdd936d133bf8a48de140a3c666897588a05shiqian      // will be picked by the compiler as the better match.
275e35fdd936d133bf8a48de140a3c666897588a05shiqian      // Otherwise the default implementation of it in
276e35fdd936d133bf8a48de140a3c666897588a05shiqian      // ::testing::internal will be picked.
277e35fdd936d133bf8a48de140a3c666897588a05shiqian      //
278e35fdd936d133bf8a48de140a3c666897588a05shiqian      // This look-up rule lets a writer of a polymorphic matcher
279e35fdd936d133bf8a48de140a3c666897588a05shiqian      // customize the behavior of ExplainMatchResultTo() when he
280e35fdd936d133bf8a48de140a3c666897588a05shiqian      // cares to.  Nothing needs to be done by the writer if he
281e35fdd936d133bf8a48de140a3c666897588a05shiqian      // doesn't need to customize it.
282e35fdd936d133bf8a48de140a3c666897588a05shiqian      ExplainMatchResultTo(impl_, x, os);
283e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
2842b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan
285e35fdd936d133bf8a48de140a3c666897588a05shiqian   private:
286e35fdd936d133bf8a48de140a3c666897588a05shiqian    const Impl impl_;
287e35fdd936d133bf8a48de140a3c666897588a05shiqian  };
288e35fdd936d133bf8a48de140a3c666897588a05shiqian
2892b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan  Impl impl_;
290e35fdd936d133bf8a48de140a3c666897588a05shiqian};
291e35fdd936d133bf8a48de140a3c666897588a05shiqian
292e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher from its implementation.  This is easier to use
293e35fdd936d133bf8a48de140a3c666897588a05shiqian// than the Matcher<T> constructor as it doesn't require you to
294e35fdd936d133bf8a48de140a3c666897588a05shiqian// explicitly write the template argument, e.g.
295e35fdd936d133bf8a48de140a3c666897588a05shiqian//
296e35fdd936d133bf8a48de140a3c666897588a05shiqian//   MakeMatcher(foo);
297e35fdd936d133bf8a48de140a3c666897588a05shiqian// vs
298e35fdd936d133bf8a48de140a3c666897588a05shiqian//   Matcher<const string&>(foo);
299e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
300e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<T> MakeMatcher(const MatcherInterface<T>* impl) {
301e35fdd936d133bf8a48de140a3c666897588a05shiqian  return Matcher<T>(impl);
302e35fdd936d133bf8a48de140a3c666897588a05shiqian};
303e35fdd936d133bf8a48de140a3c666897588a05shiqian
304e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher from its implementation.  This is
305e35fdd936d133bf8a48de140a3c666897588a05shiqian// easier to use than the PolymorphicMatcher<Impl> constructor as it
306e35fdd936d133bf8a48de140a3c666897588a05shiqian// doesn't require you to explicitly write the template argument, e.g.
307e35fdd936d133bf8a48de140a3c666897588a05shiqian//
308e35fdd936d133bf8a48de140a3c666897588a05shiqian//   MakePolymorphicMatcher(foo);
309e35fdd936d133bf8a48de140a3c666897588a05shiqian// vs
310e35fdd936d133bf8a48de140a3c666897588a05shiqian//   PolymorphicMatcher<TypeOfFoo>(foo);
311e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <class Impl>
312e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<Impl> MakePolymorphicMatcher(const Impl& impl) {
313e35fdd936d133bf8a48de140a3c666897588a05shiqian  return PolymorphicMatcher<Impl>(impl);
314e35fdd936d133bf8a48de140a3c666897588a05shiqian}
315e35fdd936d133bf8a48de140a3c666897588a05shiqian
316e35fdd936d133bf8a48de140a3c666897588a05shiqian// In order to be safe and clear, casting between different matcher
317e35fdd936d133bf8a48de140a3c666897588a05shiqian// types is done explicitly via MatcherCast<T>(m), which takes a
318e35fdd936d133bf8a48de140a3c666897588a05shiqian// matcher m and returns a Matcher<T>.  It compiles only when T can be
319e35fdd936d133bf8a48de140a3c666897588a05shiqian// statically converted to the argument type of m.
320e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T, typename M>
321e35fdd936d133bf8a48de140a3c666897588a05shiqianMatcher<T> MatcherCast(M m);
322e35fdd936d133bf8a48de140a3c666897588a05shiqian
32318490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// TODO(vladl@google.com): Modify the implementation to reject casting
32418490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// Matcher<int> to Matcher<double>.
32518490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// Implements SafeMatcherCast().
32618490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan//
32718490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// This overload handles polymorphic matchers only since monomorphic
32818490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// matchers are handled by the next one.
32918490653e80d484b4650d8799184fd1e021efc7bzhanyong.wantemplate <typename T, typename M>
33018490653e80d484b4650d8799184fd1e021efc7bzhanyong.waninline Matcher<T> SafeMatcherCast(M polymorphic_matcher) {
33118490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  return Matcher<T>(polymorphic_matcher);
33218490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan}
33318490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan
33418490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// This overload handles monomorphic matchers.
33518490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan//
33618490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// In general, if type T can be implicitly converted to type U, we can
33718490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// safely convert a Matcher<U> to a Matcher<T> (i.e. Matcher is
33818490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// contravariant): just keep a copy of the original Matcher<U>, convert the
33918490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// argument from type T to U, and then pass it to the underlying Matcher<U>.
34018490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// The only exception is when U is a reference and T is not, as the
34118490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// underlying Matcher<U> may be interested in the argument's address, which
34218490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// is not preserved in the conversion from T to U.
34318490653e80d484b4650d8799184fd1e021efc7bzhanyong.wantemplate <typename T, typename U>
34418490653e80d484b4650d8799184fd1e021efc7bzhanyong.wanMatcher<T> SafeMatcherCast(const Matcher<U>& matcher) {
34518490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  // Enforce that T can be implicitly converted to U.
34618490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  GMOCK_COMPILE_ASSERT_((internal::ImplicitlyConvertible<T, U>::value),
34718490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan                        T_must_be_implicitly_convertible_to_U);
34818490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  // Enforce that we are not converting a non-reference type T to a reference
34918490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  // type U.
35018490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  GMOCK_COMPILE_ASSERT_(
35118490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan      internal::is_reference<T>::value || !internal::is_reference<U>::value,
35218490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan      cannot_convert_non_referentce_arg_to_reference);
35316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  // In case both T and U are arithmetic types, enforce that the
35416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  // conversion is not lossy.
35516cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(T)) RawT;
35616cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(U)) RawU;
35716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  const bool kTIsOther = GMOCK_KIND_OF_(RawT) == internal::kOther;
35816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  const bool kUIsOther = GMOCK_KIND_OF_(RawU) == internal::kOther;
35916cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  GMOCK_COMPILE_ASSERT_(
36016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan      kTIsOther || kUIsOther ||
36116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan      (internal::LosslessArithmeticConvertible<RawT, RawU>::value),
36216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan      conversion_of_arithmetic_types_must_be_lossless);
36318490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  return MatcherCast<T>(matcher);
36418490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan}
36518490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan
366e35fdd936d133bf8a48de140a3c666897588a05shiqian// A<T>() returns a matcher that matches any value of type T.
367e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
368e35fdd936d133bf8a48de140a3c666897588a05shiqianMatcher<T> A();
369e35fdd936d133bf8a48de140a3c666897588a05shiqian
370e35fdd936d133bf8a48de140a3c666897588a05shiqian// Anything inside the 'internal' namespace IS INTERNAL IMPLEMENTATION
371e35fdd936d133bf8a48de140a3c666897588a05shiqian// and MUST NOT BE USED IN USER CODE!!!
372e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace internal {
373e35fdd936d133bf8a48de140a3c666897588a05shiqian
374e35fdd936d133bf8a48de140a3c666897588a05shiqian// Appends the explanation on the result of matcher.Matches(value) to
375e35fdd936d133bf8a48de140a3c666897588a05shiqian// os iff the explanation is not empty.
376e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
377e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid ExplainMatchResultAsNeededTo(const Matcher<T>& matcher, T value,
378e35fdd936d133bf8a48de140a3c666897588a05shiqian                                  ::std::ostream* os) {
379e35fdd936d133bf8a48de140a3c666897588a05shiqian  ::std::stringstream reason;
380e35fdd936d133bf8a48de140a3c666897588a05shiqian  matcher.ExplainMatchResultTo(value, &reason);
381e35fdd936d133bf8a48de140a3c666897588a05shiqian  const internal::string s = reason.str();
382e35fdd936d133bf8a48de140a3c666897588a05shiqian  if (s != "") {
383e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << " (" << s << ")";
384e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
385e35fdd936d133bf8a48de140a3c666897588a05shiqian}
386e35fdd936d133bf8a48de140a3c666897588a05shiqian
387e35fdd936d133bf8a48de140a3c666897588a05shiqian// An internal helper class for doing compile-time loop on a tuple's
388e35fdd936d133bf8a48de140a3c666897588a05shiqian// fields.
389e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <size_t N>
390e35fdd936d133bf8a48de140a3c666897588a05shiqianclass TuplePrefix {
391e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
392e35fdd936d133bf8a48de140a3c666897588a05shiqian  // TuplePrefix<N>::Matches(matcher_tuple, value_tuple) returns true
393e35fdd936d133bf8a48de140a3c666897588a05shiqian  // iff the first N fields of matcher_tuple matches the first N
394e35fdd936d133bf8a48de140a3c666897588a05shiqian  // fields of value_tuple, respectively.
395e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename MatcherTuple, typename ValueTuple>
396e35fdd936d133bf8a48de140a3c666897588a05shiqian  static bool Matches(const MatcherTuple& matcher_tuple,
397e35fdd936d133bf8a48de140a3c666897588a05shiqian                      const ValueTuple& value_tuple) {
398e35fdd936d133bf8a48de140a3c666897588a05shiqian    using ::std::tr1::get;
399e35fdd936d133bf8a48de140a3c666897588a05shiqian    return TuplePrefix<N - 1>::Matches(matcher_tuple, value_tuple)
400e35fdd936d133bf8a48de140a3c666897588a05shiqian        && get<N - 1>(matcher_tuple).Matches(get<N - 1>(value_tuple));
401e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
402e35fdd936d133bf8a48de140a3c666897588a05shiqian
403e35fdd936d133bf8a48de140a3c666897588a05shiqian  // TuplePrefix<N>::DescribeMatchFailuresTo(matchers, values, os)
404e35fdd936d133bf8a48de140a3c666897588a05shiqian  // describes failures in matching the first N fields of matchers
405e35fdd936d133bf8a48de140a3c666897588a05shiqian  // against the first N fields of values.  If there is no failure,
406e35fdd936d133bf8a48de140a3c666897588a05shiqian  // nothing will be streamed to os.
407e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename MatcherTuple, typename ValueTuple>
408e35fdd936d133bf8a48de140a3c666897588a05shiqian  static void DescribeMatchFailuresTo(const MatcherTuple& matchers,
409e35fdd936d133bf8a48de140a3c666897588a05shiqian                                      const ValueTuple& values,
410e35fdd936d133bf8a48de140a3c666897588a05shiqian                                      ::std::ostream* os) {
411e35fdd936d133bf8a48de140a3c666897588a05shiqian    using ::std::tr1::tuple_element;
412e35fdd936d133bf8a48de140a3c666897588a05shiqian    using ::std::tr1::get;
413e35fdd936d133bf8a48de140a3c666897588a05shiqian
414e35fdd936d133bf8a48de140a3c666897588a05shiqian    // First, describes failures in the first N - 1 fields.
415e35fdd936d133bf8a48de140a3c666897588a05shiqian    TuplePrefix<N - 1>::DescribeMatchFailuresTo(matchers, values, os);
416e35fdd936d133bf8a48de140a3c666897588a05shiqian
417e35fdd936d133bf8a48de140a3c666897588a05shiqian    // Then describes the failure (if any) in the (N - 1)-th (0-based)
418e35fdd936d133bf8a48de140a3c666897588a05shiqian    // field.
419e35fdd936d133bf8a48de140a3c666897588a05shiqian    typename tuple_element<N - 1, MatcherTuple>::type matcher =
420e35fdd936d133bf8a48de140a3c666897588a05shiqian        get<N - 1>(matchers);
421e35fdd936d133bf8a48de140a3c666897588a05shiqian    typedef typename tuple_element<N - 1, ValueTuple>::type Value;
422e35fdd936d133bf8a48de140a3c666897588a05shiqian    Value value = get<N - 1>(values);
423e35fdd936d133bf8a48de140a3c666897588a05shiqian    if (!matcher.Matches(value)) {
424e35fdd936d133bf8a48de140a3c666897588a05shiqian      // TODO(wan): include in the message the name of the parameter
425e35fdd936d133bf8a48de140a3c666897588a05shiqian      // as used in MOCK_METHOD*() when possible.
426e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "  Expected arg #" << N - 1 << ": ";
427e35fdd936d133bf8a48de140a3c666897588a05shiqian      get<N - 1>(matchers).DescribeTo(os);
428e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "\n           Actual: ";
429e35fdd936d133bf8a48de140a3c666897588a05shiqian      // We remove the reference in type Value to prevent the
430e35fdd936d133bf8a48de140a3c666897588a05shiqian      // universal printer from printing the address of value, which
431e35fdd936d133bf8a48de140a3c666897588a05shiqian      // isn't interesting to the user most of the time.  The
432e35fdd936d133bf8a48de140a3c666897588a05shiqian      // matcher's ExplainMatchResultTo() method handles the case when
433e35fdd936d133bf8a48de140a3c666897588a05shiqian      // the address is interesting.
434e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan      internal::UniversalPrinter<GMOCK_REMOVE_REFERENCE_(Value)>::
435e35fdd936d133bf8a48de140a3c666897588a05shiqian          Print(value, os);
436e35fdd936d133bf8a48de140a3c666897588a05shiqian      ExplainMatchResultAsNeededTo<Value>(matcher, value, os);
437e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "\n";
438e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
439e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
440e35fdd936d133bf8a48de140a3c666897588a05shiqian};
441e35fdd936d133bf8a48de140a3c666897588a05shiqian
442e35fdd936d133bf8a48de140a3c666897588a05shiqian// The base case.
443e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <>
444e35fdd936d133bf8a48de140a3c666897588a05shiqianclass TuplePrefix<0> {
445e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
446e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename MatcherTuple, typename ValueTuple>
4473fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.wan  static bool Matches(const MatcherTuple& /* matcher_tuple */,
4483fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.wan                      const ValueTuple& /* value_tuple */) {
449e35fdd936d133bf8a48de140a3c666897588a05shiqian    return true;
450e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
451e35fdd936d133bf8a48de140a3c666897588a05shiqian
452e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename MatcherTuple, typename ValueTuple>
4533fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.wan  static void DescribeMatchFailuresTo(const MatcherTuple& /* matchers */,
4543fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.wan                                      const ValueTuple& /* values */,
4553fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.wan                                      ::std::ostream* /* os */) {}
456e35fdd936d133bf8a48de140a3c666897588a05shiqian};
457e35fdd936d133bf8a48de140a3c666897588a05shiqian
458e35fdd936d133bf8a48de140a3c666897588a05shiqian// TupleMatches(matcher_tuple, value_tuple) returns true iff all
459e35fdd936d133bf8a48de140a3c666897588a05shiqian// matchers in matcher_tuple match the corresponding fields in
460e35fdd936d133bf8a48de140a3c666897588a05shiqian// value_tuple.  It is a compiler error if matcher_tuple and
461e35fdd936d133bf8a48de140a3c666897588a05shiqian// value_tuple have different number of fields or incompatible field
462e35fdd936d133bf8a48de140a3c666897588a05shiqian// types.
463e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename MatcherTuple, typename ValueTuple>
464e35fdd936d133bf8a48de140a3c666897588a05shiqianbool TupleMatches(const MatcherTuple& matcher_tuple,
465e35fdd936d133bf8a48de140a3c666897588a05shiqian                  const ValueTuple& value_tuple) {
466e35fdd936d133bf8a48de140a3c666897588a05shiqian  using ::std::tr1::tuple_size;
467e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Makes sure that matcher_tuple and value_tuple have the same
468e35fdd936d133bf8a48de140a3c666897588a05shiqian  // number of fields.
469e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan  GMOCK_COMPILE_ASSERT_(tuple_size<MatcherTuple>::value ==
470e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan                        tuple_size<ValueTuple>::value,
471e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan                        matcher_and_value_have_different_numbers_of_fields);
472e35fdd936d133bf8a48de140a3c666897588a05shiqian  return TuplePrefix<tuple_size<ValueTuple>::value>::
473e35fdd936d133bf8a48de140a3c666897588a05shiqian      Matches(matcher_tuple, value_tuple);
474e35fdd936d133bf8a48de140a3c666897588a05shiqian}
475e35fdd936d133bf8a48de140a3c666897588a05shiqian
476e35fdd936d133bf8a48de140a3c666897588a05shiqian// Describes failures in matching matchers against values.  If there
477e35fdd936d133bf8a48de140a3c666897588a05shiqian// is no failure, nothing will be streamed to os.
478e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename MatcherTuple, typename ValueTuple>
479e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid DescribeMatchFailureTupleTo(const MatcherTuple& matchers,
480e35fdd936d133bf8a48de140a3c666897588a05shiqian                                 const ValueTuple& values,
481e35fdd936d133bf8a48de140a3c666897588a05shiqian                                 ::std::ostream* os) {
482e35fdd936d133bf8a48de140a3c666897588a05shiqian  using ::std::tr1::tuple_size;
483e35fdd936d133bf8a48de140a3c666897588a05shiqian  TuplePrefix<tuple_size<MatcherTuple>::value>::DescribeMatchFailuresTo(
484e35fdd936d133bf8a48de140a3c666897588a05shiqian      matchers, values, os);
485e35fdd936d133bf8a48de140a3c666897588a05shiqian}
486e35fdd936d133bf8a48de140a3c666897588a05shiqian
487e35fdd936d133bf8a48de140a3c666897588a05shiqian// The MatcherCastImpl class template is a helper for implementing
488e35fdd936d133bf8a48de140a3c666897588a05shiqian// MatcherCast().  We need this helper in order to partially
489e35fdd936d133bf8a48de140a3c666897588a05shiqian// specialize the implementation of MatcherCast() (C++ allows
490e35fdd936d133bf8a48de140a3c666897588a05shiqian// class/struct templates to be partially specialized, but not
491e35fdd936d133bf8a48de140a3c666897588a05shiqian// function templates.).
492e35fdd936d133bf8a48de140a3c666897588a05shiqian
493e35fdd936d133bf8a48de140a3c666897588a05shiqian// This general version is used when MatcherCast()'s argument is a
494e35fdd936d133bf8a48de140a3c666897588a05shiqian// polymorphic matcher (i.e. something that can be converted to a
495e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matcher but is not one yet; for example, Eq(value)).
496e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T, typename M>
497e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatcherCastImpl {
498e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
499e35fdd936d133bf8a48de140a3c666897588a05shiqian  static Matcher<T> Cast(M polymorphic_matcher) {
500e35fdd936d133bf8a48de140a3c666897588a05shiqian    return Matcher<T>(polymorphic_matcher);
501e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
502e35fdd936d133bf8a48de140a3c666897588a05shiqian};
503e35fdd936d133bf8a48de140a3c666897588a05shiqian
504e35fdd936d133bf8a48de140a3c666897588a05shiqian// This more specialized version is used when MatcherCast()'s argument
505e35fdd936d133bf8a48de140a3c666897588a05shiqian// is already a Matcher.  This only compiles when type T can be
506e35fdd936d133bf8a48de140a3c666897588a05shiqian// statically converted to type U.
507e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T, typename U>
508e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatcherCastImpl<T, Matcher<U> > {
509e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
510e35fdd936d133bf8a48de140a3c666897588a05shiqian  static Matcher<T> Cast(const Matcher<U>& source_matcher) {
511e35fdd936d133bf8a48de140a3c666897588a05shiqian    return Matcher<T>(new Impl(source_matcher));
512e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
513e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
514e35fdd936d133bf8a48de140a3c666897588a05shiqian  class Impl : public MatcherInterface<T> {
515e35fdd936d133bf8a48de140a3c666897588a05shiqian   public:
516e35fdd936d133bf8a48de140a3c666897588a05shiqian    explicit Impl(const Matcher<U>& source_matcher)
517e35fdd936d133bf8a48de140a3c666897588a05shiqian        : source_matcher_(source_matcher) {}
518e35fdd936d133bf8a48de140a3c666897588a05shiqian
519e35fdd936d133bf8a48de140a3c666897588a05shiqian    // We delegate the matching logic to the source matcher.
520e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual bool Matches(T x) const {
521e35fdd936d133bf8a48de140a3c666897588a05shiqian      return source_matcher_.Matches(static_cast<U>(x));
522e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
523e35fdd936d133bf8a48de140a3c666897588a05shiqian
524e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeTo(::std::ostream* os) const {
525e35fdd936d133bf8a48de140a3c666897588a05shiqian      source_matcher_.DescribeTo(os);
526e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
527e35fdd936d133bf8a48de140a3c666897588a05shiqian
528e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeNegationTo(::std::ostream* os) const {
529e35fdd936d133bf8a48de140a3c666897588a05shiqian      source_matcher_.DescribeNegationTo(os);
530e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
531e35fdd936d133bf8a48de140a3c666897588a05shiqian
532e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void ExplainMatchResultTo(T x, ::std::ostream* os) const {
533e35fdd936d133bf8a48de140a3c666897588a05shiqian      source_matcher_.ExplainMatchResultTo(static_cast<U>(x), os);
534e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
535e35fdd936d133bf8a48de140a3c666897588a05shiqian   private:
536e35fdd936d133bf8a48de140a3c666897588a05shiqian    const Matcher<U> source_matcher_;
537e35fdd936d133bf8a48de140a3c666897588a05shiqian  };
538e35fdd936d133bf8a48de140a3c666897588a05shiqian};
539e35fdd936d133bf8a48de140a3c666897588a05shiqian
540e35fdd936d133bf8a48de140a3c666897588a05shiqian// This even more specialized version is used for efficiently casting
541e35fdd936d133bf8a48de140a3c666897588a05shiqian// a matcher to its own type.
542e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
543e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatcherCastImpl<T, Matcher<T> > {
544e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
545e35fdd936d133bf8a48de140a3c666897588a05shiqian  static Matcher<T> Cast(const Matcher<T>& matcher) { return matcher; }
546e35fdd936d133bf8a48de140a3c666897588a05shiqian};
547e35fdd936d133bf8a48de140a3c666897588a05shiqian
548e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements A<T>().
549e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
550e35fdd936d133bf8a48de140a3c666897588a05shiqianclass AnyMatcherImpl : public MatcherInterface<T> {
551e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
5523fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.wan  virtual bool Matches(T /* x */) const { return true; }
553e35fdd936d133bf8a48de140a3c666897588a05shiqian  virtual void DescribeTo(::std::ostream* os) const { *os << "is anything"; }
554e35fdd936d133bf8a48de140a3c666897588a05shiqian  virtual void DescribeNegationTo(::std::ostream* os) const {
555e35fdd936d133bf8a48de140a3c666897588a05shiqian    // This is mostly for completeness' safe, as it's not very useful
556e35fdd936d133bf8a48de140a3c666897588a05shiqian    // to write Not(A<bool>()).  However we cannot completely rule out
557e35fdd936d133bf8a48de140a3c666897588a05shiqian    // such a possibility, and it doesn't hurt to be prepared.
558e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "never matches";
559e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
560e35fdd936d133bf8a48de140a3c666897588a05shiqian};
561e35fdd936d133bf8a48de140a3c666897588a05shiqian
562e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements _, a matcher that matches any value of any
563e35fdd936d133bf8a48de140a3c666897588a05shiqian// type.  This is a polymorphic matcher, so we need a template type
564e35fdd936d133bf8a48de140a3c666897588a05shiqian// conversion operator to make it appearing as a Matcher<T> for any
565e35fdd936d133bf8a48de140a3c666897588a05shiqian// type T.
566e35fdd936d133bf8a48de140a3c666897588a05shiqianclass AnythingMatcher {
567e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
568e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
569e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<T>() const { return A<T>(); }
570e35fdd936d133bf8a48de140a3c666897588a05shiqian};
571e35fdd936d133bf8a48de140a3c666897588a05shiqian
572e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements a matcher that compares a given value with a
573e35fdd936d133bf8a48de140a3c666897588a05shiqian// pre-supplied value using one of the ==, <=, <, etc, operators.  The
574e35fdd936d133bf8a48de140a3c666897588a05shiqian// two values being compared don't have to have the same type.
575e35fdd936d133bf8a48de140a3c666897588a05shiqian//
576e35fdd936d133bf8a48de140a3c666897588a05shiqian// The matcher defined here is polymorphic (for example, Eq(5) can be
577e35fdd936d133bf8a48de140a3c666897588a05shiqian// used to match an int, a short, a double, etc).  Therefore we use
578e35fdd936d133bf8a48de140a3c666897588a05shiqian// a template type conversion operator in the implementation.
579e35fdd936d133bf8a48de140a3c666897588a05shiqian//
580e35fdd936d133bf8a48de140a3c666897588a05shiqian// We define this as a macro in order to eliminate duplicated source
581e35fdd936d133bf8a48de140a3c666897588a05shiqian// code.
582e35fdd936d133bf8a48de140a3c666897588a05shiqian//
583e35fdd936d133bf8a48de140a3c666897588a05shiqian// The following template definition assumes that the Rhs parameter is
584e35fdd936d133bf8a48de140a3c666897588a05shiqian// a "bare" type (i.e. neither 'const T' nor 'T&').
585e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan#define GMOCK_IMPLEMENT_COMPARISON_MATCHER_(name, op, relation) \
586e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename Rhs> class name##Matcher { \
587e35fdd936d133bf8a48de140a3c666897588a05shiqian   public: \
588e35fdd936d133bf8a48de140a3c666897588a05shiqian    explicit name##Matcher(const Rhs& rhs) : rhs_(rhs) {} \
589e35fdd936d133bf8a48de140a3c666897588a05shiqian    template <typename Lhs> \
590e35fdd936d133bf8a48de140a3c666897588a05shiqian    operator Matcher<Lhs>() const { \
591e35fdd936d133bf8a48de140a3c666897588a05shiqian      return MakeMatcher(new Impl<Lhs>(rhs_)); \
592e35fdd936d133bf8a48de140a3c666897588a05shiqian    } \
593e35fdd936d133bf8a48de140a3c666897588a05shiqian   private: \
594e35fdd936d133bf8a48de140a3c666897588a05shiqian    template <typename Lhs> \
595e35fdd936d133bf8a48de140a3c666897588a05shiqian    class Impl : public MatcherInterface<Lhs> { \
596e35fdd936d133bf8a48de140a3c666897588a05shiqian     public: \
597e35fdd936d133bf8a48de140a3c666897588a05shiqian      explicit Impl(const Rhs& rhs) : rhs_(rhs) {} \
598e35fdd936d133bf8a48de140a3c666897588a05shiqian      virtual bool Matches(Lhs lhs) const { return lhs op rhs_; } \
599e35fdd936d133bf8a48de140a3c666897588a05shiqian      virtual void DescribeTo(::std::ostream* os) const { \
600e35fdd936d133bf8a48de140a3c666897588a05shiqian        *os << "is " relation  " "; \
601e35fdd936d133bf8a48de140a3c666897588a05shiqian        UniversalPrinter<Rhs>::Print(rhs_, os); \
602e35fdd936d133bf8a48de140a3c666897588a05shiqian      } \
603e35fdd936d133bf8a48de140a3c666897588a05shiqian      virtual void DescribeNegationTo(::std::ostream* os) const { \
604e35fdd936d133bf8a48de140a3c666897588a05shiqian        *os << "is not " relation  " "; \
605e35fdd936d133bf8a48de140a3c666897588a05shiqian        UniversalPrinter<Rhs>::Print(rhs_, os); \
606e35fdd936d133bf8a48de140a3c666897588a05shiqian      } \
607e35fdd936d133bf8a48de140a3c666897588a05shiqian     private: \
608e35fdd936d133bf8a48de140a3c666897588a05shiqian      Rhs rhs_; \
609e35fdd936d133bf8a48de140a3c666897588a05shiqian    }; \
610e35fdd936d133bf8a48de140a3c666897588a05shiqian    Rhs rhs_; \
611e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
612e35fdd936d133bf8a48de140a3c666897588a05shiqian
613e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements Eq(v), Ge(v), Gt(v), Le(v), Lt(v), and Ne(v)
614e35fdd936d133bf8a48de140a3c666897588a05shiqian// respectively.
615e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Eq, ==, "equal to");
616e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Ge, >=, "greater than or equal to");
617e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Gt, >, "greater than");
618e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Le, <=, "less than or equal to");
619e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Lt, <, "less than");
620e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Ne, !=, "not equal to");
621e35fdd936d133bf8a48de140a3c666897588a05shiqian
622e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan#undef GMOCK_IMPLEMENT_COMPARISON_MATCHER_
623e35fdd936d133bf8a48de140a3c666897588a05shiqian
624e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the polymorphic NotNull() matcher, which matches any
625e35fdd936d133bf8a48de140a3c666897588a05shiqian// pointer that is not NULL.
626e35fdd936d133bf8a48de140a3c666897588a05shiqianclass NotNullMatcher {
627e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
628e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
629e35fdd936d133bf8a48de140a3c666897588a05shiqian  bool Matches(T* p) const { return p != NULL; }
630e35fdd936d133bf8a48de140a3c666897588a05shiqian
631e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const { *os << "is not NULL"; }
632e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
633e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "is NULL";
634e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
635e35fdd936d133bf8a48de140a3c666897588a05shiqian};
636e35fdd936d133bf8a48de140a3c666897588a05shiqian
637e35fdd936d133bf8a48de140a3c666897588a05shiqian// Ref(variable) matches any argument that is a reference to
638e35fdd936d133bf8a48de140a3c666897588a05shiqian// 'variable'.  This matcher is polymorphic as it can match any
639e35fdd936d133bf8a48de140a3c666897588a05shiqian// super type of the type of 'variable'.
640e35fdd936d133bf8a48de140a3c666897588a05shiqian//
641e35fdd936d133bf8a48de140a3c666897588a05shiqian// The RefMatcher template class implements Ref(variable).  It can
642e35fdd936d133bf8a48de140a3c666897588a05shiqian// only be instantiated with a reference type.  This prevents a user
643e35fdd936d133bf8a48de140a3c666897588a05shiqian// from mistakenly using Ref(x) to match a non-reference function
644e35fdd936d133bf8a48de140a3c666897588a05shiqian// argument.  For example, the following will righteously cause a
645e35fdd936d133bf8a48de140a3c666897588a05shiqian// compiler error:
646e35fdd936d133bf8a48de140a3c666897588a05shiqian//
647e35fdd936d133bf8a48de140a3c666897588a05shiqian//   int n;
648e35fdd936d133bf8a48de140a3c666897588a05shiqian//   Matcher<int> m1 = Ref(n);   // This won't compile.
649e35fdd936d133bf8a48de140a3c666897588a05shiqian//   Matcher<int&> m2 = Ref(n);  // This will compile.
650e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
651e35fdd936d133bf8a48de140a3c666897588a05shiqianclass RefMatcher;
652e35fdd936d133bf8a48de140a3c666897588a05shiqian
653e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
654e35fdd936d133bf8a48de140a3c666897588a05shiqianclass RefMatcher<T&> {
655e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Google Mock is a generic framework and thus needs to support
656e35fdd936d133bf8a48de140a3c666897588a05shiqian  // mocking any function types, including those that take non-const
657e35fdd936d133bf8a48de140a3c666897588a05shiqian  // reference arguments.  Therefore the template parameter T (and
658e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Super below) can be instantiated to either a const type or a
659e35fdd936d133bf8a48de140a3c666897588a05shiqian  // non-const type.
660e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
661e35fdd936d133bf8a48de140a3c666897588a05shiqian  // RefMatcher() takes a T& instead of const T&, as we want the
662e35fdd936d133bf8a48de140a3c666897588a05shiqian  // compiler to catch using Ref(const_value) as a matcher for a
663e35fdd936d133bf8a48de140a3c666897588a05shiqian  // non-const reference.
664e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit RefMatcher(T& x) : object_(x) {}  // NOLINT
665e35fdd936d133bf8a48de140a3c666897588a05shiqian
666e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename Super>
667e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<Super&>() const {
668e35fdd936d133bf8a48de140a3c666897588a05shiqian    // By passing object_ (type T&) to Impl(), which expects a Super&,
669e35fdd936d133bf8a48de140a3c666897588a05shiqian    // we make sure that Super is a super type of T.  In particular,
670e35fdd936d133bf8a48de140a3c666897588a05shiqian    // this catches using Ref(const_value) as a matcher for a
671e35fdd936d133bf8a48de140a3c666897588a05shiqian    // non-const reference, as you cannot implicitly convert a const
672e35fdd936d133bf8a48de140a3c666897588a05shiqian    // reference to a non-const reference.
673e35fdd936d133bf8a48de140a3c666897588a05shiqian    return MakeMatcher(new Impl<Super>(object_));
674e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
675e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
676e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename Super>
677e35fdd936d133bf8a48de140a3c666897588a05shiqian  class Impl : public MatcherInterface<Super&> {
678e35fdd936d133bf8a48de140a3c666897588a05shiqian   public:
679e35fdd936d133bf8a48de140a3c666897588a05shiqian    explicit Impl(Super& x) : object_(x) {}  // NOLINT
680e35fdd936d133bf8a48de140a3c666897588a05shiqian
681e35fdd936d133bf8a48de140a3c666897588a05shiqian    // Matches() takes a Super& (as opposed to const Super&) in
682e35fdd936d133bf8a48de140a3c666897588a05shiqian    // order to match the interface MatcherInterface<Super&>.
683e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual bool Matches(Super& x) const { return &x == &object_; }  // NOLINT
684e35fdd936d133bf8a48de140a3c666897588a05shiqian
685e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeTo(::std::ostream* os) const {
686e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "references the variable ";
687e35fdd936d133bf8a48de140a3c666897588a05shiqian      UniversalPrinter<Super&>::Print(object_, os);
688e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
689e35fdd936d133bf8a48de140a3c666897588a05shiqian
690e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeNegationTo(::std::ostream* os) const {
691e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "does not reference the variable ";
692e35fdd936d133bf8a48de140a3c666897588a05shiqian      UniversalPrinter<Super&>::Print(object_, os);
693e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
694e35fdd936d133bf8a48de140a3c666897588a05shiqian
695e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void ExplainMatchResultTo(Super& x,  // NOLINT
696e35fdd936d133bf8a48de140a3c666897588a05shiqian                                      ::std::ostream* os) const {
697e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "is located @" << static_cast<const void*>(&x);
698e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
699e35fdd936d133bf8a48de140a3c666897588a05shiqian   private:
700e35fdd936d133bf8a48de140a3c666897588a05shiqian    const Super& object_;
701e35fdd936d133bf8a48de140a3c666897588a05shiqian  };
702e35fdd936d133bf8a48de140a3c666897588a05shiqian
703e35fdd936d133bf8a48de140a3c666897588a05shiqian  T& object_;
704e35fdd936d133bf8a48de140a3c666897588a05shiqian};
705e35fdd936d133bf8a48de140a3c666897588a05shiqian
706e35fdd936d133bf8a48de140a3c666897588a05shiqian// Polymorphic helper functions for narrow and wide string matchers.
707e35fdd936d133bf8a48de140a3c666897588a05shiqianinline bool CaseInsensitiveCStringEquals(const char* lhs, const char* rhs) {
708e35fdd936d133bf8a48de140a3c666897588a05shiqian  return String::CaseInsensitiveCStringEquals(lhs, rhs);
709e35fdd936d133bf8a48de140a3c666897588a05shiqian}
710e35fdd936d133bf8a48de140a3c666897588a05shiqian
711e35fdd936d133bf8a48de140a3c666897588a05shiqianinline bool CaseInsensitiveCStringEquals(const wchar_t* lhs,
712e35fdd936d133bf8a48de140a3c666897588a05shiqian                                         const wchar_t* rhs) {
713e35fdd936d133bf8a48de140a3c666897588a05shiqian  return String::CaseInsensitiveWideCStringEquals(lhs, rhs);
714e35fdd936d133bf8a48de140a3c666897588a05shiqian}
715e35fdd936d133bf8a48de140a3c666897588a05shiqian
716e35fdd936d133bf8a48de140a3c666897588a05shiqian// String comparison for narrow or wide strings that can have embedded NUL
717e35fdd936d133bf8a48de140a3c666897588a05shiqian// characters.
718e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType>
719e35fdd936d133bf8a48de140a3c666897588a05shiqianbool CaseInsensitiveStringEquals(const StringType& s1,
720e35fdd936d133bf8a48de140a3c666897588a05shiqian                                 const StringType& s2) {
721e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Are the heads equal?
722e35fdd936d133bf8a48de140a3c666897588a05shiqian  if (!CaseInsensitiveCStringEquals(s1.c_str(), s2.c_str())) {
723e35fdd936d133bf8a48de140a3c666897588a05shiqian    return false;
724e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
725e35fdd936d133bf8a48de140a3c666897588a05shiqian
726e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Skip the equal heads.
727e35fdd936d133bf8a48de140a3c666897588a05shiqian  const typename StringType::value_type nul = 0;
728e35fdd936d133bf8a48de140a3c666897588a05shiqian  const size_t i1 = s1.find(nul), i2 = s2.find(nul);
729e35fdd936d133bf8a48de140a3c666897588a05shiqian
730e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Are we at the end of either s1 or s2?
731e35fdd936d133bf8a48de140a3c666897588a05shiqian  if (i1 == StringType::npos || i2 == StringType::npos) {
732e35fdd936d133bf8a48de140a3c666897588a05shiqian    return i1 == i2;
733e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
734e35fdd936d133bf8a48de140a3c666897588a05shiqian
735e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Are the tails equal?
736e35fdd936d133bf8a48de140a3c666897588a05shiqian  return CaseInsensitiveStringEquals(s1.substr(i1 + 1), s2.substr(i2 + 1));
737e35fdd936d133bf8a48de140a3c666897588a05shiqian}
738e35fdd936d133bf8a48de140a3c666897588a05shiqian
739e35fdd936d133bf8a48de140a3c666897588a05shiqian// String matchers.
740e35fdd936d133bf8a48de140a3c666897588a05shiqian
741e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements equality-based string matchers like StrEq, StrCaseNe, and etc.
742e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType>
743e35fdd936d133bf8a48de140a3c666897588a05shiqianclass StrEqualityMatcher {
744e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
745e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef typename StringType::const_pointer ConstCharPointer;
746e35fdd936d133bf8a48de140a3c666897588a05shiqian
747e35fdd936d133bf8a48de140a3c666897588a05shiqian  StrEqualityMatcher(const StringType& str, bool expect_eq,
748e35fdd936d133bf8a48de140a3c666897588a05shiqian                     bool case_sensitive)
749e35fdd936d133bf8a48de140a3c666897588a05shiqian      : string_(str), expect_eq_(expect_eq), case_sensitive_(case_sensitive) {}
750e35fdd936d133bf8a48de140a3c666897588a05shiqian
751e35fdd936d133bf8a48de140a3c666897588a05shiqian  // When expect_eq_ is true, returns true iff s is equal to string_;
752e35fdd936d133bf8a48de140a3c666897588a05shiqian  // otherwise returns true iff s is not equal to string_.
753e35fdd936d133bf8a48de140a3c666897588a05shiqian  bool Matches(ConstCharPointer s) const {
754e35fdd936d133bf8a48de140a3c666897588a05shiqian    if (s == NULL) {
755e35fdd936d133bf8a48de140a3c666897588a05shiqian      return !expect_eq_;
756e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
757e35fdd936d133bf8a48de140a3c666897588a05shiqian    return Matches(StringType(s));
758e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
759e35fdd936d133bf8a48de140a3c666897588a05shiqian
760e35fdd936d133bf8a48de140a3c666897588a05shiqian  bool Matches(const StringType& s) const {
761e35fdd936d133bf8a48de140a3c666897588a05shiqian    const bool eq = case_sensitive_ ? s == string_ :
762e35fdd936d133bf8a48de140a3c666897588a05shiqian        CaseInsensitiveStringEquals(s, string_);
763e35fdd936d133bf8a48de140a3c666897588a05shiqian    return expect_eq_ == eq;
764e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
765e35fdd936d133bf8a48de140a3c666897588a05shiqian
766e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
767e35fdd936d133bf8a48de140a3c666897588a05shiqian    DescribeToHelper(expect_eq_, os);
768e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
769e35fdd936d133bf8a48de140a3c666897588a05shiqian
770e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
771e35fdd936d133bf8a48de140a3c666897588a05shiqian    DescribeToHelper(!expect_eq_, os);
772e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
773e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
774e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeToHelper(bool expect_eq, ::std::ostream* os) const {
775e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "is ";
776e35fdd936d133bf8a48de140a3c666897588a05shiqian    if (!expect_eq) {
777e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "not ";
778e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
779e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "equal to ";
780e35fdd936d133bf8a48de140a3c666897588a05shiqian    if (!case_sensitive_) {
781e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "(ignoring case) ";
782e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
783e35fdd936d133bf8a48de140a3c666897588a05shiqian    UniversalPrinter<StringType>::Print(string_, os);
784e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
785e35fdd936d133bf8a48de140a3c666897588a05shiqian
786e35fdd936d133bf8a48de140a3c666897588a05shiqian  const StringType string_;
787e35fdd936d133bf8a48de140a3c666897588a05shiqian  const bool expect_eq_;
788e35fdd936d133bf8a48de140a3c666897588a05shiqian  const bool case_sensitive_;
789e35fdd936d133bf8a48de140a3c666897588a05shiqian};
790e35fdd936d133bf8a48de140a3c666897588a05shiqian
791e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the polymorphic HasSubstr(substring) matcher, which
792e35fdd936d133bf8a48de140a3c666897588a05shiqian// can be used as a Matcher<T> as long as T can be converted to a
793e35fdd936d133bf8a48de140a3c666897588a05shiqian// string.
794e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType>
795e35fdd936d133bf8a48de140a3c666897588a05shiqianclass HasSubstrMatcher {
796e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
797e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef typename StringType::const_pointer ConstCharPointer;
798e35fdd936d133bf8a48de140a3c666897588a05shiqian
799e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit HasSubstrMatcher(const StringType& substring)
800e35fdd936d133bf8a48de140a3c666897588a05shiqian      : substring_(substring) {}
801e35fdd936d133bf8a48de140a3c666897588a05shiqian
802e35fdd936d133bf8a48de140a3c666897588a05shiqian  // These overloaded methods allow HasSubstr(substring) to be used as a
803e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Matcher<T> as long as T can be converted to string.  Returns true
804e35fdd936d133bf8a48de140a3c666897588a05shiqian  // iff s contains substring_ as a substring.
805e35fdd936d133bf8a48de140a3c666897588a05shiqian  bool Matches(ConstCharPointer s) const {
806e35fdd936d133bf8a48de140a3c666897588a05shiqian    return s != NULL && Matches(StringType(s));
807e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
808e35fdd936d133bf8a48de140a3c666897588a05shiqian
809e35fdd936d133bf8a48de140a3c666897588a05shiqian  bool Matches(const StringType& s) const {
810e35fdd936d133bf8a48de140a3c666897588a05shiqian    return s.find(substring_) != StringType::npos;
811e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
812e35fdd936d133bf8a48de140a3c666897588a05shiqian
813e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Describes what this matcher matches.
814e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
815e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "has substring ";
816e35fdd936d133bf8a48de140a3c666897588a05shiqian    UniversalPrinter<StringType>::Print(substring_, os);
817e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
818e35fdd936d133bf8a48de140a3c666897588a05shiqian
819e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
820e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "has no substring ";
821e35fdd936d133bf8a48de140a3c666897588a05shiqian    UniversalPrinter<StringType>::Print(substring_, os);
822e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
823e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
824e35fdd936d133bf8a48de140a3c666897588a05shiqian  const StringType substring_;
825e35fdd936d133bf8a48de140a3c666897588a05shiqian};
826e35fdd936d133bf8a48de140a3c666897588a05shiqian
827e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the polymorphic StartsWith(substring) matcher, which
828e35fdd936d133bf8a48de140a3c666897588a05shiqian// can be used as a Matcher<T> as long as T can be converted to a
829e35fdd936d133bf8a48de140a3c666897588a05shiqian// string.
830e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType>
831e35fdd936d133bf8a48de140a3c666897588a05shiqianclass StartsWithMatcher {
832e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
833e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef typename StringType::const_pointer ConstCharPointer;
834e35fdd936d133bf8a48de140a3c666897588a05shiqian
835e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit StartsWithMatcher(const StringType& prefix) : prefix_(prefix) {
836e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
837e35fdd936d133bf8a48de140a3c666897588a05shiqian
838e35fdd936d133bf8a48de140a3c666897588a05shiqian  // These overloaded methods allow StartsWith(prefix) to be used as a
839e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Matcher<T> as long as T can be converted to string.  Returns true
840e35fdd936d133bf8a48de140a3c666897588a05shiqian  // iff s starts with prefix_.
841e35fdd936d133bf8a48de140a3c666897588a05shiqian  bool Matches(ConstCharPointer s) const {
842e35fdd936d133bf8a48de140a3c666897588a05shiqian    return s != NULL && Matches(StringType(s));
843e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
844e35fdd936d133bf8a48de140a3c666897588a05shiqian
845e35fdd936d133bf8a48de140a3c666897588a05shiqian  bool Matches(const StringType& s) const {
846e35fdd936d133bf8a48de140a3c666897588a05shiqian    return s.length() >= prefix_.length() &&
847e35fdd936d133bf8a48de140a3c666897588a05shiqian        s.substr(0, prefix_.length()) == prefix_;
848e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
849e35fdd936d133bf8a48de140a3c666897588a05shiqian
850e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
851e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "starts with ";
852e35fdd936d133bf8a48de140a3c666897588a05shiqian    UniversalPrinter<StringType>::Print(prefix_, os);
853e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
854e35fdd936d133bf8a48de140a3c666897588a05shiqian
855e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
856e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "doesn't start with ";
857e35fdd936d133bf8a48de140a3c666897588a05shiqian    UniversalPrinter<StringType>::Print(prefix_, os);
858e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
859e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
860e35fdd936d133bf8a48de140a3c666897588a05shiqian  const StringType prefix_;
861e35fdd936d133bf8a48de140a3c666897588a05shiqian};
862e35fdd936d133bf8a48de140a3c666897588a05shiqian
863e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the polymorphic EndsWith(substring) matcher, which
864e35fdd936d133bf8a48de140a3c666897588a05shiqian// can be used as a Matcher<T> as long as T can be converted to a
865e35fdd936d133bf8a48de140a3c666897588a05shiqian// string.
866e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType>
867e35fdd936d133bf8a48de140a3c666897588a05shiqianclass EndsWithMatcher {
868e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
869e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef typename StringType::const_pointer ConstCharPointer;
870e35fdd936d133bf8a48de140a3c666897588a05shiqian
871e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit EndsWithMatcher(const StringType& suffix) : suffix_(suffix) {}
872e35fdd936d133bf8a48de140a3c666897588a05shiqian
873e35fdd936d133bf8a48de140a3c666897588a05shiqian  // These overloaded methods allow EndsWith(suffix) to be used as a
874e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Matcher<T> as long as T can be converted to string.  Returns true
875e35fdd936d133bf8a48de140a3c666897588a05shiqian  // iff s ends with suffix_.
876e35fdd936d133bf8a48de140a3c666897588a05shiqian  bool Matches(ConstCharPointer s) const {
877e35fdd936d133bf8a48de140a3c666897588a05shiqian    return s != NULL && Matches(StringType(s));
878e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
879e35fdd936d133bf8a48de140a3c666897588a05shiqian
880e35fdd936d133bf8a48de140a3c666897588a05shiqian  bool Matches(const StringType& s) const {
881e35fdd936d133bf8a48de140a3c666897588a05shiqian    return s.length() >= suffix_.length() &&
882e35fdd936d133bf8a48de140a3c666897588a05shiqian        s.substr(s.length() - suffix_.length()) == suffix_;
883e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
884e35fdd936d133bf8a48de140a3c666897588a05shiqian
885e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
886e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "ends with ";
887e35fdd936d133bf8a48de140a3c666897588a05shiqian    UniversalPrinter<StringType>::Print(suffix_, os);
888e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
889e35fdd936d133bf8a48de140a3c666897588a05shiqian
890e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
891e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "doesn't end with ";
892e35fdd936d133bf8a48de140a3c666897588a05shiqian    UniversalPrinter<StringType>::Print(suffix_, os);
893e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
894e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
895e35fdd936d133bf8a48de140a3c666897588a05shiqian  const StringType suffix_;
896e35fdd936d133bf8a48de140a3c666897588a05shiqian};
897e35fdd936d133bf8a48de140a3c666897588a05shiqian
898e35fdd936d133bf8a48de140a3c666897588a05shiqian#if GMOCK_HAS_REGEX
899e35fdd936d133bf8a48de140a3c666897588a05shiqian
900e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements polymorphic matchers MatchesRegex(regex) and
901e35fdd936d133bf8a48de140a3c666897588a05shiqian// ContainsRegex(regex), which can be used as a Matcher<T> as long as
902e35fdd936d133bf8a48de140a3c666897588a05shiqian// T can be converted to a string.
903e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatchesRegexMatcher {
904e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
905e35fdd936d133bf8a48de140a3c666897588a05shiqian  MatchesRegexMatcher(const RE* regex, bool full_match)
906e35fdd936d133bf8a48de140a3c666897588a05shiqian      : regex_(regex), full_match_(full_match) {}
907e35fdd936d133bf8a48de140a3c666897588a05shiqian
908e35fdd936d133bf8a48de140a3c666897588a05shiqian  // These overloaded methods allow MatchesRegex(regex) to be used as
909e35fdd936d133bf8a48de140a3c666897588a05shiqian  // a Matcher<T> as long as T can be converted to string.  Returns
910e35fdd936d133bf8a48de140a3c666897588a05shiqian  // true iff s matches regular expression regex.  When full_match_ is
911e35fdd936d133bf8a48de140a3c666897588a05shiqian  // true, a full match is done; otherwise a partial match is done.
912e35fdd936d133bf8a48de140a3c666897588a05shiqian  bool Matches(const char* s) const {
913e35fdd936d133bf8a48de140a3c666897588a05shiqian    return s != NULL && Matches(internal::string(s));
914e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
915e35fdd936d133bf8a48de140a3c666897588a05shiqian
916e35fdd936d133bf8a48de140a3c666897588a05shiqian  bool Matches(const internal::string& s) const {
917e35fdd936d133bf8a48de140a3c666897588a05shiqian    return full_match_ ? RE::FullMatch(s, *regex_) :
918e35fdd936d133bf8a48de140a3c666897588a05shiqian        RE::PartialMatch(s, *regex_);
919e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
920e35fdd936d133bf8a48de140a3c666897588a05shiqian
921e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
922e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << (full_match_ ? "matches" : "contains")
923e35fdd936d133bf8a48de140a3c666897588a05shiqian        << " regular expression ";
924e35fdd936d133bf8a48de140a3c666897588a05shiqian    UniversalPrinter<internal::string>::Print(regex_->pattern(), os);
925e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
926e35fdd936d133bf8a48de140a3c666897588a05shiqian
927e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
928e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "doesn't " << (full_match_ ? "match" : "contain")
929e35fdd936d133bf8a48de140a3c666897588a05shiqian        << " regular expression ";
930e35fdd936d133bf8a48de140a3c666897588a05shiqian    UniversalPrinter<internal::string>::Print(regex_->pattern(), os);
931e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
932e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
933e35fdd936d133bf8a48de140a3c666897588a05shiqian  const internal::linked_ptr<const RE> regex_;
934e35fdd936d133bf8a48de140a3c666897588a05shiqian  const bool full_match_;
935e35fdd936d133bf8a48de140a3c666897588a05shiqian};
936e35fdd936d133bf8a48de140a3c666897588a05shiqian
937e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif  // GMOCK_HAS_REGEX
938e35fdd936d133bf8a48de140a3c666897588a05shiqian
939e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements a matcher that compares the two fields of a 2-tuple
940e35fdd936d133bf8a48de140a3c666897588a05shiqian// using one of the ==, <=, <, etc, operators.  The two fields being
941e35fdd936d133bf8a48de140a3c666897588a05shiqian// compared don't have to have the same type.
942e35fdd936d133bf8a48de140a3c666897588a05shiqian//
943e35fdd936d133bf8a48de140a3c666897588a05shiqian// The matcher defined here is polymorphic (for example, Eq() can be
944e35fdd936d133bf8a48de140a3c666897588a05shiqian// used to match a tuple<int, short>, a tuple<const long&, double>,
945e35fdd936d133bf8a48de140a3c666897588a05shiqian// etc).  Therefore we use a template type conversion operator in the
946e35fdd936d133bf8a48de140a3c666897588a05shiqian// implementation.
947e35fdd936d133bf8a48de140a3c666897588a05shiqian//
948e35fdd936d133bf8a48de140a3c666897588a05shiqian// We define this as a macro in order to eliminate duplicated source
949e35fdd936d133bf8a48de140a3c666897588a05shiqian// code.
9502661c6821a4d7964697e48747c4d739e1ac3a243zhanyong.wan#define GMOCK_IMPLEMENT_COMPARISON2_MATCHER_(name, op) \
951e35fdd936d133bf8a48de140a3c666897588a05shiqian  class name##2Matcher { \
952e35fdd936d133bf8a48de140a3c666897588a05shiqian   public: \
953e35fdd936d133bf8a48de140a3c666897588a05shiqian    template <typename T1, typename T2> \
954e35fdd936d133bf8a48de140a3c666897588a05shiqian    operator Matcher<const ::std::tr1::tuple<T1, T2>&>() const { \
955e35fdd936d133bf8a48de140a3c666897588a05shiqian      return MakeMatcher(new Impl<T1, T2>); \
956e35fdd936d133bf8a48de140a3c666897588a05shiqian    } \
957e35fdd936d133bf8a48de140a3c666897588a05shiqian   private: \
958e35fdd936d133bf8a48de140a3c666897588a05shiqian    template <typename T1, typename T2> \
959e35fdd936d133bf8a48de140a3c666897588a05shiqian    class Impl : public MatcherInterface<const ::std::tr1::tuple<T1, T2>&> { \
960e35fdd936d133bf8a48de140a3c666897588a05shiqian     public: \
961e35fdd936d133bf8a48de140a3c666897588a05shiqian      virtual bool Matches(const ::std::tr1::tuple<T1, T2>& args) const { \
962e35fdd936d133bf8a48de140a3c666897588a05shiqian        return ::std::tr1::get<0>(args) op ::std::tr1::get<1>(args); \
963e35fdd936d133bf8a48de140a3c666897588a05shiqian      } \
964e35fdd936d133bf8a48de140a3c666897588a05shiqian      virtual void DescribeTo(::std::ostream* os) const { \
9652661c6821a4d7964697e48747c4d739e1ac3a243zhanyong.wan        *os << "are a pair (x, y) where x " #op " y"; \
966e35fdd936d133bf8a48de140a3c666897588a05shiqian      } \
967e35fdd936d133bf8a48de140a3c666897588a05shiqian      virtual void DescribeNegationTo(::std::ostream* os) const { \
9682661c6821a4d7964697e48747c4d739e1ac3a243zhanyong.wan        *os << "are a pair (x, y) where x " #op " y is false"; \
969e35fdd936d133bf8a48de140a3c666897588a05shiqian      } \
970e35fdd936d133bf8a48de140a3c666897588a05shiqian    }; \
971e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
972e35fdd936d133bf8a48de140a3c666897588a05shiqian
973e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements Eq(), Ge(), Gt(), Le(), Lt(), and Ne() respectively.
9742661c6821a4d7964697e48747c4d739e1ac3a243zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Eq, ==);
9752661c6821a4d7964697e48747c4d739e1ac3a243zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Ge, >=);
9762661c6821a4d7964697e48747c4d739e1ac3a243zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Gt, >);
9772661c6821a4d7964697e48747c4d739e1ac3a243zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Le, <=);
9782661c6821a4d7964697e48747c4d739e1ac3a243zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Lt, <);
9792661c6821a4d7964697e48747c4d739e1ac3a243zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Ne, !=);
980e35fdd936d133bf8a48de140a3c666897588a05shiqian
981e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan#undef GMOCK_IMPLEMENT_COMPARISON2_MATCHER_
982e35fdd936d133bf8a48de140a3c666897588a05shiqian
983c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// Implements the Not(...) matcher for a particular argument type T.
984c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// We do not nest it inside the NotMatcher class template, as that
985c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// will prevent different instantiations of NotMatcher from sharing
986c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// the same NotMatcherImpl<T> class.
987c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wantemplate <typename T>
988c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wanclass NotMatcherImpl : public MatcherInterface<T> {
989c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan public:
990c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  explicit NotMatcherImpl(const Matcher<T>& matcher)
991c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      : matcher_(matcher) {}
992c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
993c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  virtual bool Matches(T x) const {
994c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    return !matcher_.Matches(x);
995c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
996c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
997c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
998c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    matcher_.DescribeNegationTo(os);
999c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1000c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1001c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
1002c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    matcher_.DescribeTo(os);
1003c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1004c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1005c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  virtual void ExplainMatchResultTo(T x, ::std::ostream* os) const {
1006c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    matcher_.ExplainMatchResultTo(x, os);
1007c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1008c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan private:
1009c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  const Matcher<T> matcher_;
1010c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan};
1011c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1012e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the Not(m) matcher, which matches a value that doesn't
1013e35fdd936d133bf8a48de140a3c666897588a05shiqian// match matcher m.
1014e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename InnerMatcher>
1015e35fdd936d133bf8a48de140a3c666897588a05shiqianclass NotMatcher {
1016e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1017e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit NotMatcher(InnerMatcher matcher) : matcher_(matcher) {}
1018e35fdd936d133bf8a48de140a3c666897588a05shiqian
1019e35fdd936d133bf8a48de140a3c666897588a05shiqian  // This template type conversion operator allows Not(m) to be used
1020e35fdd936d133bf8a48de140a3c666897588a05shiqian  // to match any type m can match.
1021e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1022e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<T>() const {
1023c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    return Matcher<T>(new NotMatcherImpl<T>(SafeMatcherCast<T>(matcher_)));
1024e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1025e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1026c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  InnerMatcher matcher_;
1027c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan};
1028e35fdd936d133bf8a48de140a3c666897588a05shiqian
1029c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// Implements the AllOf(m1, m2) matcher for a particular argument type
1030c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// T. We do not nest it inside the BothOfMatcher class template, as
1031c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// that will prevent different instantiations of BothOfMatcher from
1032c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// sharing the same BothOfMatcherImpl<T> class.
1033c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wantemplate <typename T>
1034c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wanclass BothOfMatcherImpl : public MatcherInterface<T> {
1035c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan public:
1036c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  BothOfMatcherImpl(const Matcher<T>& matcher1, const Matcher<T>& matcher2)
1037c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      : matcher1_(matcher1), matcher2_(matcher2) {}
1038e35fdd936d133bf8a48de140a3c666897588a05shiqian
1039c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  virtual bool Matches(T x) const {
1040c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    return matcher1_.Matches(x) && matcher2_.Matches(x);
1041c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1042e35fdd936d133bf8a48de140a3c666897588a05shiqian
1043c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
1044c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    *os << "(";
1045c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    matcher1_.DescribeTo(os);
1046c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    *os << ") and (";
1047c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    matcher2_.DescribeTo(os);
1048c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    *os << ")";
1049c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1050e35fdd936d133bf8a48de140a3c666897588a05shiqian
1051c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
1052c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    *os << "not ";
1053c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    DescribeTo(os);
1054c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1055e35fdd936d133bf8a48de140a3c666897588a05shiqian
1056c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  virtual void ExplainMatchResultTo(T x, ::std::ostream* os) const {
1057c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    if (Matches(x)) {
1058c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      // When both matcher1_ and matcher2_ match x, we need to
1059c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      // explain why *both* of them match.
1060c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      ::std::stringstream ss1;
1061c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      matcher1_.ExplainMatchResultTo(x, &ss1);
1062c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      const internal::string s1 = ss1.str();
1063c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1064c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      ::std::stringstream ss2;
1065c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      matcher2_.ExplainMatchResultTo(x, &ss2);
1066c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      const internal::string s2 = ss2.str();
1067c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1068c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      if (s1 == "") {
1069c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan        *os << s2;
1070c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      } else {
1071c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan        *os << s1;
1072c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan        if (s2 != "") {
1073c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan          *os << "; " << s2;
1074c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan        }
1075c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      }
1076c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    } else {
1077c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      // Otherwise we only need to explain why *one* of them fails
1078c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      // to match.
1079c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      if (!matcher1_.Matches(x)) {
1080c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan        matcher1_.ExplainMatchResultTo(x, os);
1081c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      } else {
1082c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan        matcher2_.ExplainMatchResultTo(x, os);
1083c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      }
1084c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    }
1085c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1086c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan private:
1087c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  const Matcher<T> matcher1_;
1088c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  const Matcher<T> matcher2_;
1089e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1090e35fdd936d133bf8a48de140a3c666897588a05shiqian
1091e35fdd936d133bf8a48de140a3c666897588a05shiqian// Used for implementing the AllOf(m_1, ..., m_n) matcher, which
1092e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a value that matches all of the matchers m_1, ..., and m_n.
1093e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2>
1094e35fdd936d133bf8a48de140a3c666897588a05shiqianclass BothOfMatcher {
1095e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1096e35fdd936d133bf8a48de140a3c666897588a05shiqian  BothOfMatcher(Matcher1 matcher1, Matcher2 matcher2)
1097e35fdd936d133bf8a48de140a3c666897588a05shiqian      : matcher1_(matcher1), matcher2_(matcher2) {}
1098e35fdd936d133bf8a48de140a3c666897588a05shiqian
1099e35fdd936d133bf8a48de140a3c666897588a05shiqian  // This template type conversion operator allows a
1100e35fdd936d133bf8a48de140a3c666897588a05shiqian  // BothOfMatcher<Matcher1, Matcher2> object to match any type that
1101e35fdd936d133bf8a48de140a3c666897588a05shiqian  // both Matcher1 and Matcher2 can match.
1102e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1103e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<T>() const {
1104c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    return Matcher<T>(new BothOfMatcherImpl<T>(SafeMatcherCast<T>(matcher1_),
1105c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan                                               SafeMatcherCast<T>(matcher2_)));
1106e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1107e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1108c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  Matcher1 matcher1_;
1109c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  Matcher2 matcher2_;
1110c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan};
1111e35fdd936d133bf8a48de140a3c666897588a05shiqian
1112c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// Implements the AnyOf(m1, m2) matcher for a particular argument type
1113c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// T.  We do not nest it inside the AnyOfMatcher class template, as
1114c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// that will prevent different instantiations of AnyOfMatcher from
1115c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// sharing the same EitherOfMatcherImpl<T> class.
1116c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wantemplate <typename T>
1117c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wanclass EitherOfMatcherImpl : public MatcherInterface<T> {
1118c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan public:
1119c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  EitherOfMatcherImpl(const Matcher<T>& matcher1, const Matcher<T>& matcher2)
1120c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      : matcher1_(matcher1), matcher2_(matcher2) {}
1121e35fdd936d133bf8a48de140a3c666897588a05shiqian
1122c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  virtual bool Matches(T x) const {
1123c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    return matcher1_.Matches(x) || matcher2_.Matches(x);
1124c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1125e35fdd936d133bf8a48de140a3c666897588a05shiqian
1126c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
1127c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    *os << "(";
1128c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    matcher1_.DescribeTo(os);
1129c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    *os << ") or (";
1130c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    matcher2_.DescribeTo(os);
1131c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    *os << ")";
1132c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1133e35fdd936d133bf8a48de140a3c666897588a05shiqian
1134c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
1135c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    *os << "not ";
1136c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    DescribeTo(os);
1137c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1138c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1139c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  virtual void ExplainMatchResultTo(T x, ::std::ostream* os) const {
1140c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    if (Matches(x)) {
1141c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      // If either matcher1_ or matcher2_ matches x, we just need
1142c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      // to explain why *one* of them matches.
1143c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      if (matcher1_.Matches(x)) {
1144c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan        matcher1_.ExplainMatchResultTo(x, os);
1145e35fdd936d133bf8a48de140a3c666897588a05shiqian      } else {
1146c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan        matcher2_.ExplainMatchResultTo(x, os);
1147c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      }
1148c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    } else {
1149c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      // Otherwise we need to explain why *neither* matches.
1150c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      ::std::stringstream ss1;
1151c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      matcher1_.ExplainMatchResultTo(x, &ss1);
1152c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      const internal::string s1 = ss1.str();
1153c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1154c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      ::std::stringstream ss2;
1155c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      matcher2_.ExplainMatchResultTo(x, &ss2);
1156c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      const internal::string s2 = ss2.str();
1157c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1158c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      if (s1 == "") {
1159c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan        *os << s2;
1160c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      } else {
1161c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan        *os << s1;
1162c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan        if (s2 != "") {
1163c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan          *os << "; " << s2;
1164e35fdd936d133bf8a48de140a3c666897588a05shiqian        }
1165e35fdd936d133bf8a48de140a3c666897588a05shiqian      }
1166e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1167c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1168c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan private:
1169c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  const Matcher<T> matcher1_;
1170c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  const Matcher<T> matcher2_;
1171e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1172e35fdd936d133bf8a48de140a3c666897588a05shiqian
1173e35fdd936d133bf8a48de140a3c666897588a05shiqian// Used for implementing the AnyOf(m_1, ..., m_n) matcher, which
1174e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a value that matches at least one of the matchers m_1, ...,
1175e35fdd936d133bf8a48de140a3c666897588a05shiqian// and m_n.
1176e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2>
1177e35fdd936d133bf8a48de140a3c666897588a05shiqianclass EitherOfMatcher {
1178e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1179e35fdd936d133bf8a48de140a3c666897588a05shiqian  EitherOfMatcher(Matcher1 matcher1, Matcher2 matcher2)
1180e35fdd936d133bf8a48de140a3c666897588a05shiqian      : matcher1_(matcher1), matcher2_(matcher2) {}
1181e35fdd936d133bf8a48de140a3c666897588a05shiqian
1182e35fdd936d133bf8a48de140a3c666897588a05shiqian  // This template type conversion operator allows a
1183e35fdd936d133bf8a48de140a3c666897588a05shiqian  // EitherOfMatcher<Matcher1, Matcher2> object to match any type that
1184e35fdd936d133bf8a48de140a3c666897588a05shiqian  // both Matcher1 and Matcher2 can match.
1185e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1186e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<T>() const {
118716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan    return Matcher<T>(new EitherOfMatcherImpl<T>(
118816cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan        SafeMatcherCast<T>(matcher1_), SafeMatcherCast<T>(matcher2_)));
1189e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1190e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1191e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher1 matcher1_;
1192e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher2 matcher2_;
1193e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1194e35fdd936d133bf8a48de140a3c666897588a05shiqian
1195e35fdd936d133bf8a48de140a3c666897588a05shiqian// Used for implementing Truly(pred), which turns a predicate into a
1196e35fdd936d133bf8a48de140a3c666897588a05shiqian// matcher.
1197e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Predicate>
1198e35fdd936d133bf8a48de140a3c666897588a05shiqianclass TrulyMatcher {
1199e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1200e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit TrulyMatcher(Predicate pred) : predicate_(pred) {}
1201e35fdd936d133bf8a48de140a3c666897588a05shiqian
1202e35fdd936d133bf8a48de140a3c666897588a05shiqian  // This method template allows Truly(pred) to be used as a matcher
1203e35fdd936d133bf8a48de140a3c666897588a05shiqian  // for type T where T is the argument type of predicate 'pred'.  The
1204e35fdd936d133bf8a48de140a3c666897588a05shiqian  // argument is passed by reference as the predicate may be
1205e35fdd936d133bf8a48de140a3c666897588a05shiqian  // interested in the address of the argument.
1206e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
120716cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan  bool Matches(T& x) const {  // NOLINT
1208652540a278e56528e576f9ea77515f386698a326zhanyong.wan#if GTEST_OS_WINDOWS
1209e35fdd936d133bf8a48de140a3c666897588a05shiqian    // MSVC warns about converting a value into bool (warning 4800).
1210e35fdd936d133bf8a48de140a3c666897588a05shiqian#pragma warning(push)          // Saves the current warning state.
1211e35fdd936d133bf8a48de140a3c666897588a05shiqian#pragma warning(disable:4800)  // Temporarily disables warning 4800.
1212e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif  // GTEST_OS_WINDOWS
1213e35fdd936d133bf8a48de140a3c666897588a05shiqian    return predicate_(x);
1214652540a278e56528e576f9ea77515f386698a326zhanyong.wan#if GTEST_OS_WINDOWS
1215e35fdd936d133bf8a48de140a3c666897588a05shiqian#pragma warning(pop)           // Restores the warning state.
1216e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif  // GTEST_OS_WINDOWS
1217e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1218e35fdd936d133bf8a48de140a3c666897588a05shiqian
1219e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
1220e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "satisfies the given predicate";
1221e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1222e35fdd936d133bf8a48de140a3c666897588a05shiqian
1223e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
1224e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "doesn't satisfy the given predicate";
1225e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1226e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1227e35fdd936d133bf8a48de140a3c666897588a05shiqian  Predicate predicate_;
1228e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1229e35fdd936d133bf8a48de140a3c666897588a05shiqian
1230e35fdd936d133bf8a48de140a3c666897588a05shiqian// Used for implementing Matches(matcher), which turns a matcher into
1231e35fdd936d133bf8a48de140a3c666897588a05shiqian// a predicate.
1232e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename M>
1233e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatcherAsPredicate {
1234e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1235e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit MatcherAsPredicate(M matcher) : matcher_(matcher) {}
1236e35fdd936d133bf8a48de140a3c666897588a05shiqian
1237e35fdd936d133bf8a48de140a3c666897588a05shiqian  // This template operator() allows Matches(m) to be used as a
1238e35fdd936d133bf8a48de140a3c666897588a05shiqian  // predicate on type T where m is a matcher on type T.
1239e35fdd936d133bf8a48de140a3c666897588a05shiqian  //
1240e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The argument x is passed by reference instead of by value, as
1241e35fdd936d133bf8a48de140a3c666897588a05shiqian  // some matcher may be interested in its address (e.g. as in
1242e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Matches(Ref(n))(x)).
1243e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1244e35fdd936d133bf8a48de140a3c666897588a05shiqian  bool operator()(const T& x) const {
1245e35fdd936d133bf8a48de140a3c666897588a05shiqian    // We let matcher_ commit to a particular type here instead of
1246e35fdd936d133bf8a48de140a3c666897588a05shiqian    // when the MatcherAsPredicate object was constructed.  This
1247e35fdd936d133bf8a48de140a3c666897588a05shiqian    // allows us to write Matches(m) where m is a polymorphic matcher
1248e35fdd936d133bf8a48de140a3c666897588a05shiqian    // (e.g. Eq(5)).
1249e35fdd936d133bf8a48de140a3c666897588a05shiqian    //
1250e35fdd936d133bf8a48de140a3c666897588a05shiqian    // If we write Matcher<T>(matcher_).Matches(x) here, it won't
1251e35fdd936d133bf8a48de140a3c666897588a05shiqian    // compile when matcher_ has type Matcher<const T&>; if we write
1252e35fdd936d133bf8a48de140a3c666897588a05shiqian    // Matcher<const T&>(matcher_).Matches(x) here, it won't compile
1253e35fdd936d133bf8a48de140a3c666897588a05shiqian    // when matcher_ has type Matcher<T>; if we just write
1254e35fdd936d133bf8a48de140a3c666897588a05shiqian    // matcher_.Matches(x), it won't compile when matcher_ is
1255e35fdd936d133bf8a48de140a3c666897588a05shiqian    // polymorphic, e.g. Eq(5).
1256e35fdd936d133bf8a48de140a3c666897588a05shiqian    //
1257e35fdd936d133bf8a48de140a3c666897588a05shiqian    // MatcherCast<const T&>() is necessary for making the code work
1258e35fdd936d133bf8a48de140a3c666897588a05shiqian    // in all of the above situations.
1259e35fdd936d133bf8a48de140a3c666897588a05shiqian    return MatcherCast<const T&>(matcher_).Matches(x);
1260e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1261e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1262e35fdd936d133bf8a48de140a3c666897588a05shiqian  M matcher_;
1263e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1264e35fdd936d133bf8a48de140a3c666897588a05shiqian
1265e35fdd936d133bf8a48de140a3c666897588a05shiqian// For implementing ASSERT_THAT() and EXPECT_THAT().  The template
1266e35fdd936d133bf8a48de140a3c666897588a05shiqian// argument M must be a type that can be converted to a matcher.
1267e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename M>
1268e35fdd936d133bf8a48de140a3c666897588a05shiqianclass PredicateFormatterFromMatcher {
1269e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1270e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit PredicateFormatterFromMatcher(const M& m) : matcher_(m) {}
1271e35fdd936d133bf8a48de140a3c666897588a05shiqian
1272e35fdd936d133bf8a48de140a3c666897588a05shiqian  // This template () operator allows a PredicateFormatterFromMatcher
1273e35fdd936d133bf8a48de140a3c666897588a05shiqian  // object to act as a predicate-formatter suitable for using with
1274e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Google Test's EXPECT_PRED_FORMAT1() macro.
1275e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1276e35fdd936d133bf8a48de140a3c666897588a05shiqian  AssertionResult operator()(const char* value_text, const T& x) const {
1277e35fdd936d133bf8a48de140a3c666897588a05shiqian    // We convert matcher_ to a Matcher<const T&> *now* instead of
1278e35fdd936d133bf8a48de140a3c666897588a05shiqian    // when the PredicateFormatterFromMatcher object was constructed,
1279e35fdd936d133bf8a48de140a3c666897588a05shiqian    // as matcher_ may be polymorphic (e.g. NotNull()) and we won't
1280e35fdd936d133bf8a48de140a3c666897588a05shiqian    // know which type to instantiate it to until we actually see the
1281e35fdd936d133bf8a48de140a3c666897588a05shiqian    // type of x here.
1282e35fdd936d133bf8a48de140a3c666897588a05shiqian    //
1283e35fdd936d133bf8a48de140a3c666897588a05shiqian    // We write MatcherCast<const T&>(matcher_) instead of
1284e35fdd936d133bf8a48de140a3c666897588a05shiqian    // Matcher<const T&>(matcher_), as the latter won't compile when
1285e35fdd936d133bf8a48de140a3c666897588a05shiqian    // matcher_ has type Matcher<T> (e.g. An<int>()).
1286e35fdd936d133bf8a48de140a3c666897588a05shiqian    const Matcher<const T&> matcher = MatcherCast<const T&>(matcher_);
1287e35fdd936d133bf8a48de140a3c666897588a05shiqian    if (matcher.Matches(x)) {
1288e35fdd936d133bf8a48de140a3c666897588a05shiqian      return AssertionSuccess();
1289e35fdd936d133bf8a48de140a3c666897588a05shiqian    } else {
1290e35fdd936d133bf8a48de140a3c666897588a05shiqian      ::std::stringstream ss;
1291e35fdd936d133bf8a48de140a3c666897588a05shiqian      ss << "Value of: " << value_text << "\n"
1292e35fdd936d133bf8a48de140a3c666897588a05shiqian         << "Expected: ";
1293e35fdd936d133bf8a48de140a3c666897588a05shiqian      matcher.DescribeTo(&ss);
1294e35fdd936d133bf8a48de140a3c666897588a05shiqian      ss << "\n  Actual: ";
1295e35fdd936d133bf8a48de140a3c666897588a05shiqian      UniversalPrinter<T>::Print(x, &ss);
1296e35fdd936d133bf8a48de140a3c666897588a05shiqian      ExplainMatchResultAsNeededTo<const T&>(matcher, x, &ss);
1297e35fdd936d133bf8a48de140a3c666897588a05shiqian      return AssertionFailure(Message() << ss.str());
1298e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1299e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1300e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1301e35fdd936d133bf8a48de140a3c666897588a05shiqian  const M matcher_;
1302e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1303e35fdd936d133bf8a48de140a3c666897588a05shiqian
1304e35fdd936d133bf8a48de140a3c666897588a05shiqian// A helper function for converting a matcher to a predicate-formatter
1305e35fdd936d133bf8a48de140a3c666897588a05shiqian// without the user needing to explicitly write the type.  This is
1306e35fdd936d133bf8a48de140a3c666897588a05shiqian// used for implementing ASSERT_THAT() and EXPECT_THAT().
1307e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename M>
1308e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PredicateFormatterFromMatcher<M>
1309e35fdd936d133bf8a48de140a3c666897588a05shiqianMakePredicateFormatterFromMatcher(const M& matcher) {
1310e35fdd936d133bf8a48de140a3c666897588a05shiqian  return PredicateFormatterFromMatcher<M>(matcher);
1311e35fdd936d133bf8a48de140a3c666897588a05shiqian}
1312e35fdd936d133bf8a48de140a3c666897588a05shiqian
1313e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the polymorphic floating point equality matcher, which
1314e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches two float values using ULP-based approximation.  The
1315e35fdd936d133bf8a48de140a3c666897588a05shiqian// template is meant to be instantiated with FloatType being either
1316e35fdd936d133bf8a48de140a3c666897588a05shiqian// float or double.
1317e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename FloatType>
1318e35fdd936d133bf8a48de140a3c666897588a05shiqianclass FloatingEqMatcher {
1319e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1320e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Constructor for FloatingEqMatcher.
1321e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The matcher's input will be compared with rhs.  The matcher treats two
1322e35fdd936d133bf8a48de140a3c666897588a05shiqian  // NANs as equal if nan_eq_nan is true.  Otherwise, under IEEE standards,
1323e35fdd936d133bf8a48de140a3c666897588a05shiqian  // equality comparisons between NANs will always return false.
1324e35fdd936d133bf8a48de140a3c666897588a05shiqian  FloatingEqMatcher(FloatType rhs, bool nan_eq_nan) :
1325e35fdd936d133bf8a48de140a3c666897588a05shiqian    rhs_(rhs), nan_eq_nan_(nan_eq_nan) {}
1326e35fdd936d133bf8a48de140a3c666897588a05shiqian
1327e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Implements floating point equality matcher as a Matcher<T>.
1328e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1329e35fdd936d133bf8a48de140a3c666897588a05shiqian  class Impl : public MatcherInterface<T> {
1330e35fdd936d133bf8a48de140a3c666897588a05shiqian   public:
1331e35fdd936d133bf8a48de140a3c666897588a05shiqian    Impl(FloatType rhs, bool nan_eq_nan) :
1332e35fdd936d133bf8a48de140a3c666897588a05shiqian      rhs_(rhs), nan_eq_nan_(nan_eq_nan) {}
1333e35fdd936d133bf8a48de140a3c666897588a05shiqian
1334e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual bool Matches(T value) const {
1335e35fdd936d133bf8a48de140a3c666897588a05shiqian      const FloatingPoint<FloatType> lhs(value), rhs(rhs_);
1336e35fdd936d133bf8a48de140a3c666897588a05shiqian
1337e35fdd936d133bf8a48de140a3c666897588a05shiqian      // Compares NaNs first, if nan_eq_nan_ is true.
1338e35fdd936d133bf8a48de140a3c666897588a05shiqian      if (nan_eq_nan_ && lhs.is_nan()) {
1339e35fdd936d133bf8a48de140a3c666897588a05shiqian        return rhs.is_nan();
1340e35fdd936d133bf8a48de140a3c666897588a05shiqian      }
1341e35fdd936d133bf8a48de140a3c666897588a05shiqian
1342e35fdd936d133bf8a48de140a3c666897588a05shiqian      return lhs.AlmostEquals(rhs);
1343e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1344e35fdd936d133bf8a48de140a3c666897588a05shiqian
1345e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeTo(::std::ostream* os) const {
1346e35fdd936d133bf8a48de140a3c666897588a05shiqian      // os->precision() returns the previously set precision, which we
1347e35fdd936d133bf8a48de140a3c666897588a05shiqian      // store to restore the ostream to its original configuration
1348e35fdd936d133bf8a48de140a3c666897588a05shiqian      // after outputting.
1349e35fdd936d133bf8a48de140a3c666897588a05shiqian      const ::std::streamsize old_precision = os->precision(
1350e35fdd936d133bf8a48de140a3c666897588a05shiqian          ::std::numeric_limits<FloatType>::digits10 + 2);
1351e35fdd936d133bf8a48de140a3c666897588a05shiqian      if (FloatingPoint<FloatType>(rhs_).is_nan()) {
1352e35fdd936d133bf8a48de140a3c666897588a05shiqian        if (nan_eq_nan_) {
1353e35fdd936d133bf8a48de140a3c666897588a05shiqian          *os << "is NaN";
1354e35fdd936d133bf8a48de140a3c666897588a05shiqian        } else {
1355e35fdd936d133bf8a48de140a3c666897588a05shiqian          *os << "never matches";
1356e35fdd936d133bf8a48de140a3c666897588a05shiqian        }
1357e35fdd936d133bf8a48de140a3c666897588a05shiqian      } else {
1358e35fdd936d133bf8a48de140a3c666897588a05shiqian        *os << "is approximately " << rhs_;
1359e35fdd936d133bf8a48de140a3c666897588a05shiqian      }
1360e35fdd936d133bf8a48de140a3c666897588a05shiqian      os->precision(old_precision);
1361e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1362e35fdd936d133bf8a48de140a3c666897588a05shiqian
1363e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeNegationTo(::std::ostream* os) const {
1364e35fdd936d133bf8a48de140a3c666897588a05shiqian      // As before, get original precision.
1365e35fdd936d133bf8a48de140a3c666897588a05shiqian      const ::std::streamsize old_precision = os->precision(
1366e35fdd936d133bf8a48de140a3c666897588a05shiqian          ::std::numeric_limits<FloatType>::digits10 + 2);
1367e35fdd936d133bf8a48de140a3c666897588a05shiqian      if (FloatingPoint<FloatType>(rhs_).is_nan()) {
1368e35fdd936d133bf8a48de140a3c666897588a05shiqian        if (nan_eq_nan_) {
1369e35fdd936d133bf8a48de140a3c666897588a05shiqian          *os << "is not NaN";
1370e35fdd936d133bf8a48de140a3c666897588a05shiqian        } else {
1371e35fdd936d133bf8a48de140a3c666897588a05shiqian          *os << "is anything";
1372e35fdd936d133bf8a48de140a3c666897588a05shiqian        }
1373e35fdd936d133bf8a48de140a3c666897588a05shiqian      } else {
1374e35fdd936d133bf8a48de140a3c666897588a05shiqian        *os << "is not approximately " << rhs_;
1375e35fdd936d133bf8a48de140a3c666897588a05shiqian      }
1376e35fdd936d133bf8a48de140a3c666897588a05shiqian      // Restore original precision.
1377e35fdd936d133bf8a48de140a3c666897588a05shiqian      os->precision(old_precision);
1378e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1379e35fdd936d133bf8a48de140a3c666897588a05shiqian
1380e35fdd936d133bf8a48de140a3c666897588a05shiqian   private:
1381e35fdd936d133bf8a48de140a3c666897588a05shiqian    const FloatType rhs_;
1382e35fdd936d133bf8a48de140a3c666897588a05shiqian    const bool nan_eq_nan_;
1383e35fdd936d133bf8a48de140a3c666897588a05shiqian  };
1384e35fdd936d133bf8a48de140a3c666897588a05shiqian
1385e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The following 3 type conversion operators allow FloatEq(rhs) and
1386e35fdd936d133bf8a48de140a3c666897588a05shiqian  // NanSensitiveFloatEq(rhs) to be used as a Matcher<float>, a
1387e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Matcher<const float&>, or a Matcher<float&>, but nothing else.
1388e35fdd936d133bf8a48de140a3c666897588a05shiqian  // (While Google's C++ coding style doesn't allow arguments passed
1389e35fdd936d133bf8a48de140a3c666897588a05shiqian  // by non-const reference, we may see them in code not conforming to
1390e35fdd936d133bf8a48de140a3c666897588a05shiqian  // the style.  Therefore Google Mock needs to support them.)
1391e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<FloatType>() const {
1392e35fdd936d133bf8a48de140a3c666897588a05shiqian    return MakeMatcher(new Impl<FloatType>(rhs_, nan_eq_nan_));
1393e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1394e35fdd936d133bf8a48de140a3c666897588a05shiqian
1395e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<const FloatType&>() const {
1396e35fdd936d133bf8a48de140a3c666897588a05shiqian    return MakeMatcher(new Impl<const FloatType&>(rhs_, nan_eq_nan_));
1397e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1398e35fdd936d133bf8a48de140a3c666897588a05shiqian
1399e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<FloatType&>() const {
1400e35fdd936d133bf8a48de140a3c666897588a05shiqian    return MakeMatcher(new Impl<FloatType&>(rhs_, nan_eq_nan_));
1401e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1402e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1403e35fdd936d133bf8a48de140a3c666897588a05shiqian  const FloatType rhs_;
1404e35fdd936d133bf8a48de140a3c666897588a05shiqian  const bool nan_eq_nan_;
1405e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1406e35fdd936d133bf8a48de140a3c666897588a05shiqian
1407e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the Pointee(m) matcher for matching a pointer whose
1408e35fdd936d133bf8a48de140a3c666897588a05shiqian// pointee matches matcher m.  The pointer can be either raw or smart.
1409e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename InnerMatcher>
1410e35fdd936d133bf8a48de140a3c666897588a05shiqianclass PointeeMatcher {
1411e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1412e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit PointeeMatcher(const InnerMatcher& matcher) : matcher_(matcher) {}
1413e35fdd936d133bf8a48de140a3c666897588a05shiqian
1414e35fdd936d133bf8a48de140a3c666897588a05shiqian  // This type conversion operator template allows Pointee(m) to be
1415e35fdd936d133bf8a48de140a3c666897588a05shiqian  // used as a matcher for any pointer type whose pointee type is
1416e35fdd936d133bf8a48de140a3c666897588a05shiqian  // compatible with the inner matcher, where type Pointer can be
1417e35fdd936d133bf8a48de140a3c666897588a05shiqian  // either a raw pointer or a smart pointer.
1418e35fdd936d133bf8a48de140a3c666897588a05shiqian  //
1419e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The reason we do this instead of relying on
1420e35fdd936d133bf8a48de140a3c666897588a05shiqian  // MakePolymorphicMatcher() is that the latter is not flexible
1421e35fdd936d133bf8a48de140a3c666897588a05shiqian  // enough for implementing the DescribeTo() method of Pointee().
1422e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename Pointer>
1423e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<Pointer>() const {
1424e35fdd936d133bf8a48de140a3c666897588a05shiqian    return MakeMatcher(new Impl<Pointer>(matcher_));
1425e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1426e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1427e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The monomorphic implementation that works for a particular pointer type.
1428e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename Pointer>
1429e35fdd936d133bf8a48de140a3c666897588a05shiqian  class Impl : public MatcherInterface<Pointer> {
1430e35fdd936d133bf8a48de140a3c666897588a05shiqian   public:
1431e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan    typedef typename PointeeOf<GMOCK_REMOVE_CONST_(  // NOLINT
1432e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan        GMOCK_REMOVE_REFERENCE_(Pointer))>::type Pointee;
1433e35fdd936d133bf8a48de140a3c666897588a05shiqian
1434e35fdd936d133bf8a48de140a3c666897588a05shiqian    explicit Impl(const InnerMatcher& matcher)
1435e35fdd936d133bf8a48de140a3c666897588a05shiqian        : matcher_(MatcherCast<const Pointee&>(matcher)) {}
1436e35fdd936d133bf8a48de140a3c666897588a05shiqian
1437e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual bool Matches(Pointer p) const {
1438e35fdd936d133bf8a48de140a3c666897588a05shiqian      return GetRawPointer(p) != NULL && matcher_.Matches(*p);
1439e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1440e35fdd936d133bf8a48de140a3c666897588a05shiqian
1441e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeTo(::std::ostream* os) const {
1442e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "points to a value that ";
1443e35fdd936d133bf8a48de140a3c666897588a05shiqian      matcher_.DescribeTo(os);
1444e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1445e35fdd936d133bf8a48de140a3c666897588a05shiqian
1446e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeNegationTo(::std::ostream* os) const {
1447e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "does not point to a value that ";
1448e35fdd936d133bf8a48de140a3c666897588a05shiqian      matcher_.DescribeTo(os);
1449e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1450e35fdd936d133bf8a48de140a3c666897588a05shiqian
1451e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void ExplainMatchResultTo(Pointer pointer,
1452e35fdd936d133bf8a48de140a3c666897588a05shiqian                                      ::std::ostream* os) const {
1453e35fdd936d133bf8a48de140a3c666897588a05shiqian      if (GetRawPointer(pointer) == NULL)
1454e35fdd936d133bf8a48de140a3c666897588a05shiqian        return;
1455e35fdd936d133bf8a48de140a3c666897588a05shiqian
1456e35fdd936d133bf8a48de140a3c666897588a05shiqian      ::std::stringstream ss;
1457e35fdd936d133bf8a48de140a3c666897588a05shiqian      matcher_.ExplainMatchResultTo(*pointer, &ss);
1458e35fdd936d133bf8a48de140a3c666897588a05shiqian      const internal::string s = ss.str();
1459e35fdd936d133bf8a48de140a3c666897588a05shiqian      if (s != "") {
1460e35fdd936d133bf8a48de140a3c666897588a05shiqian        *os << "points to a value that " << s;
1461e35fdd936d133bf8a48de140a3c666897588a05shiqian      }
1462e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1463e35fdd936d133bf8a48de140a3c666897588a05shiqian   private:
1464e35fdd936d133bf8a48de140a3c666897588a05shiqian    const Matcher<const Pointee&> matcher_;
1465e35fdd936d133bf8a48de140a3c666897588a05shiqian  };
1466e35fdd936d133bf8a48de140a3c666897588a05shiqian
1467e35fdd936d133bf8a48de140a3c666897588a05shiqian  const InnerMatcher matcher_;
1468e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1469e35fdd936d133bf8a48de140a3c666897588a05shiqian
1470e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the Field() matcher for matching a field (i.e. member
1471e35fdd936d133bf8a48de140a3c666897588a05shiqian// variable) of an object.
1472e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Class, typename FieldType>
1473e35fdd936d133bf8a48de140a3c666897588a05shiqianclass FieldMatcher {
1474e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1475e35fdd936d133bf8a48de140a3c666897588a05shiqian  FieldMatcher(FieldType Class::*field,
1476e35fdd936d133bf8a48de140a3c666897588a05shiqian               const Matcher<const FieldType&>& matcher)
1477e35fdd936d133bf8a48de140a3c666897588a05shiqian      : field_(field), matcher_(matcher) {}
1478e35fdd936d133bf8a48de140a3c666897588a05shiqian
1479e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Returns true iff the inner matcher matches obj.field.
1480e35fdd936d133bf8a48de140a3c666897588a05shiqian  bool Matches(const Class& obj) const {
1481e35fdd936d133bf8a48de140a3c666897588a05shiqian    return matcher_.Matches(obj.*field_);
1482e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1483e35fdd936d133bf8a48de140a3c666897588a05shiqian
1484e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Returns true iff the inner matcher matches obj->field.
1485e35fdd936d133bf8a48de140a3c666897588a05shiqian  bool Matches(const Class* p) const {
1486e35fdd936d133bf8a48de140a3c666897588a05shiqian    return (p != NULL) && matcher_.Matches(p->*field_);
1487e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1488e35fdd936d133bf8a48de140a3c666897588a05shiqian
1489e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
1490e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "the given field ";
1491e35fdd936d133bf8a48de140a3c666897588a05shiqian    matcher_.DescribeTo(os);
1492e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1493e35fdd936d133bf8a48de140a3c666897588a05shiqian
1494e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
1495e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "the given field ";
1496e35fdd936d133bf8a48de140a3c666897588a05shiqian    matcher_.DescribeNegationTo(os);
1497e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1498e35fdd936d133bf8a48de140a3c666897588a05shiqian
149918490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  // The first argument of ExplainMatchResultTo() is needed to help
150018490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  // Symbian's C++ compiler choose which overload to use.  Its type is
150118490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  // true_type iff the Field() matcher is used to match a pointer.
150218490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  void ExplainMatchResultTo(false_type /* is_not_pointer */, const Class& obj,
150318490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan                            ::std::ostream* os) const {
1504e35fdd936d133bf8a48de140a3c666897588a05shiqian    ::std::stringstream ss;
1505e35fdd936d133bf8a48de140a3c666897588a05shiqian    matcher_.ExplainMatchResultTo(obj.*field_, &ss);
1506e35fdd936d133bf8a48de140a3c666897588a05shiqian    const internal::string s = ss.str();
1507e35fdd936d133bf8a48de140a3c666897588a05shiqian    if (s != "") {
1508e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "the given field " << s;
1509e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1510e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1511e35fdd936d133bf8a48de140a3c666897588a05shiqian
151218490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  void ExplainMatchResultTo(true_type /* is_pointer */, const Class* p,
151318490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan                            ::std::ostream* os) const {
1514e35fdd936d133bf8a48de140a3c666897588a05shiqian    if (p != NULL) {
151518490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan      // Since *p has a field, it must be a class/struct/union type
151618490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan      // and thus cannot be a pointer.  Therefore we pass false_type()
151718490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan      // as the first argument.
151818490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan      ExplainMatchResultTo(false_type(), *p, os);
1519e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1520e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1521e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1522e35fdd936d133bf8a48de140a3c666897588a05shiqian  const FieldType Class::*field_;
1523e35fdd936d133bf8a48de140a3c666897588a05shiqian  const Matcher<const FieldType&> matcher_;
1524e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1525e35fdd936d133bf8a48de140a3c666897588a05shiqian
152618490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// Explains the result of matching an object or pointer against a field matcher.
152718490653e80d484b4650d8799184fd1e021efc7bzhanyong.wantemplate <typename Class, typename FieldType, typename T>
1528e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid ExplainMatchResultTo(const FieldMatcher<Class, FieldType>& matcher,
152918490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan                          const T& value, ::std::ostream* os) {
153018490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  matcher.ExplainMatchResultTo(
153118490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan      typename ::testing::internal::is_pointer<T>::type(), value, os);
1532e35fdd936d133bf8a48de140a3c666897588a05shiqian}
1533e35fdd936d133bf8a48de140a3c666897588a05shiqian
1534e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the Property() matcher for matching a property
1535e35fdd936d133bf8a48de140a3c666897588a05shiqian// (i.e. return value of a getter method) of an object.
1536e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Class, typename PropertyType>
1537e35fdd936d133bf8a48de140a3c666897588a05shiqianclass PropertyMatcher {
1538e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1539e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The property may have a reference type, so 'const PropertyType&'
1540e35fdd936d133bf8a48de140a3c666897588a05shiqian  // may cause double references and fail to compile.  That's why we
1541e35fdd936d133bf8a48de140a3c666897588a05shiqian  // need GMOCK_REFERENCE_TO_CONST, which works regardless of
1542e35fdd936d133bf8a48de140a3c666897588a05shiqian  // PropertyType being a reference or not.
1543e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan  typedef GMOCK_REFERENCE_TO_CONST_(PropertyType) RefToConstProperty;
1544e35fdd936d133bf8a48de140a3c666897588a05shiqian
1545e35fdd936d133bf8a48de140a3c666897588a05shiqian  PropertyMatcher(PropertyType (Class::*property)() const,
1546e35fdd936d133bf8a48de140a3c666897588a05shiqian                  const Matcher<RefToConstProperty>& matcher)
1547e35fdd936d133bf8a48de140a3c666897588a05shiqian      : property_(property), matcher_(matcher) {}
1548e35fdd936d133bf8a48de140a3c666897588a05shiqian
1549e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Returns true iff obj.property() matches the inner matcher.
1550e35fdd936d133bf8a48de140a3c666897588a05shiqian  bool Matches(const Class& obj) const {
1551e35fdd936d133bf8a48de140a3c666897588a05shiqian    return matcher_.Matches((obj.*property_)());
1552e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1553e35fdd936d133bf8a48de140a3c666897588a05shiqian
1554e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Returns true iff p->property() matches the inner matcher.
1555e35fdd936d133bf8a48de140a3c666897588a05shiqian  bool Matches(const Class* p) const {
1556e35fdd936d133bf8a48de140a3c666897588a05shiqian    return (p != NULL) && matcher_.Matches((p->*property_)());
1557e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1558e35fdd936d133bf8a48de140a3c666897588a05shiqian
1559e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
1560e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "the given property ";
1561e35fdd936d133bf8a48de140a3c666897588a05shiqian    matcher_.DescribeTo(os);
1562e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1563e35fdd936d133bf8a48de140a3c666897588a05shiqian
1564e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
1565e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "the given property ";
1566e35fdd936d133bf8a48de140a3c666897588a05shiqian    matcher_.DescribeNegationTo(os);
1567e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1568e35fdd936d133bf8a48de140a3c666897588a05shiqian
156918490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  // The first argument of ExplainMatchResultTo() is needed to help
157018490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  // Symbian's C++ compiler choose which overload to use.  Its type is
157118490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  // true_type iff the Property() matcher is used to match a pointer.
157218490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  void ExplainMatchResultTo(false_type /* is_not_pointer */, const Class& obj,
157318490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan                            ::std::ostream* os) const {
1574e35fdd936d133bf8a48de140a3c666897588a05shiqian    ::std::stringstream ss;
1575e35fdd936d133bf8a48de140a3c666897588a05shiqian    matcher_.ExplainMatchResultTo((obj.*property_)(), &ss);
1576e35fdd936d133bf8a48de140a3c666897588a05shiqian    const internal::string s = ss.str();
1577e35fdd936d133bf8a48de140a3c666897588a05shiqian    if (s != "") {
1578e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "the given property " << s;
1579e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1580e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1581e35fdd936d133bf8a48de140a3c666897588a05shiqian
158218490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  void ExplainMatchResultTo(true_type /* is_pointer */, const Class* p,
158318490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan                            ::std::ostream* os) const {
1584e35fdd936d133bf8a48de140a3c666897588a05shiqian    if (p != NULL) {
158518490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan      // Since *p has a property method, it must be a
158618490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan      // class/struct/union type and thus cannot be a pointer.
158718490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan      // Therefore we pass false_type() as the first argument.
158818490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan      ExplainMatchResultTo(false_type(), *p, os);
1589e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1590e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1591e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1592e35fdd936d133bf8a48de140a3c666897588a05shiqian  PropertyType (Class::*property_)() const;
1593e35fdd936d133bf8a48de140a3c666897588a05shiqian  const Matcher<RefToConstProperty> matcher_;
1594e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1595e35fdd936d133bf8a48de140a3c666897588a05shiqian
159618490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// Explains the result of matching an object or pointer against a
159718490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// property matcher.
159818490653e80d484b4650d8799184fd1e021efc7bzhanyong.wantemplate <typename Class, typename PropertyType, typename T>
1599e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid ExplainMatchResultTo(const PropertyMatcher<Class, PropertyType>& matcher,
160018490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan                          const T& value, ::std::ostream* os) {
160118490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  matcher.ExplainMatchResultTo(
160218490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan      typename ::testing::internal::is_pointer<T>::type(), value, os);
1603e35fdd936d133bf8a48de140a3c666897588a05shiqian}
1604e35fdd936d133bf8a48de140a3c666897588a05shiqian
1605e35fdd936d133bf8a48de140a3c666897588a05shiqian// Type traits specifying various features of different functors for ResultOf.
1606e35fdd936d133bf8a48de140a3c666897588a05shiqian// The default template specifies features for functor objects.
1607e35fdd936d133bf8a48de140a3c666897588a05shiqian// Functor classes have to typedef argument_type and result_type
1608e35fdd936d133bf8a48de140a3c666897588a05shiqian// to be compatible with ResultOf.
1609e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Functor>
1610e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct CallableTraits {
1611e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef typename Functor::result_type ResultType;
1612e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef Functor StorageType;
1613e35fdd936d133bf8a48de140a3c666897588a05shiqian
1614e35fdd936d133bf8a48de140a3c666897588a05shiqian  static void CheckIsValid(Functor functor) {}
1615e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1616e35fdd936d133bf8a48de140a3c666897588a05shiqian  static ResultType Invoke(Functor f, T arg) { return f(arg); }
1617e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1618e35fdd936d133bf8a48de140a3c666897588a05shiqian
1619e35fdd936d133bf8a48de140a3c666897588a05shiqian// Specialization for function pointers.
1620e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename ArgType, typename ResType>
1621e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct CallableTraits<ResType(*)(ArgType)> {
1622e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef ResType ResultType;
1623e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef ResType(*StorageType)(ArgType);
1624e35fdd936d133bf8a48de140a3c666897588a05shiqian
1625e35fdd936d133bf8a48de140a3c666897588a05shiqian  static void CheckIsValid(ResType(*f)(ArgType)) {
1626f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    GTEST_CHECK_(f != NULL)
1627e35fdd936d133bf8a48de140a3c666897588a05shiqian        << "NULL function pointer is passed into ResultOf().";
1628e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1629e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1630e35fdd936d133bf8a48de140a3c666897588a05shiqian  static ResType Invoke(ResType(*f)(ArgType), T arg) {
1631e35fdd936d133bf8a48de140a3c666897588a05shiqian    return (*f)(arg);
1632e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1633e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1634e35fdd936d133bf8a48de140a3c666897588a05shiqian
1635e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the ResultOf() matcher for matching a return value of a
1636e35fdd936d133bf8a48de140a3c666897588a05shiqian// unary function of an object.
1637e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Callable>
1638e35fdd936d133bf8a48de140a3c666897588a05shiqianclass ResultOfMatcher {
1639e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1640e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef typename CallableTraits<Callable>::ResultType ResultType;
1641e35fdd936d133bf8a48de140a3c666897588a05shiqian
1642e35fdd936d133bf8a48de140a3c666897588a05shiqian  ResultOfMatcher(Callable callable, const Matcher<ResultType>& matcher)
1643e35fdd936d133bf8a48de140a3c666897588a05shiqian      : callable_(callable), matcher_(matcher) {
1644e35fdd936d133bf8a48de140a3c666897588a05shiqian    CallableTraits<Callable>::CheckIsValid(callable_);
1645e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1646e35fdd936d133bf8a48de140a3c666897588a05shiqian
1647e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1648e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<T>() const {
1649e35fdd936d133bf8a48de140a3c666897588a05shiqian    return Matcher<T>(new Impl<T>(callable_, matcher_));
1650e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1651e35fdd936d133bf8a48de140a3c666897588a05shiqian
1652e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1653e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef typename CallableTraits<Callable>::StorageType CallableStorageType;
1654e35fdd936d133bf8a48de140a3c666897588a05shiqian
1655e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1656e35fdd936d133bf8a48de140a3c666897588a05shiqian  class Impl : public MatcherInterface<T> {
1657e35fdd936d133bf8a48de140a3c666897588a05shiqian   public:
1658e35fdd936d133bf8a48de140a3c666897588a05shiqian    Impl(CallableStorageType callable, const Matcher<ResultType>& matcher)
1659e35fdd936d133bf8a48de140a3c666897588a05shiqian        : callable_(callable), matcher_(matcher) {}
1660e35fdd936d133bf8a48de140a3c666897588a05shiqian    // Returns true iff callable_(obj) matches the inner matcher.
1661e35fdd936d133bf8a48de140a3c666897588a05shiqian    // The calling syntax is different for different types of callables
1662e35fdd936d133bf8a48de140a3c666897588a05shiqian    // so we abstract it in CallableTraits<Callable>::Invoke().
1663e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual bool Matches(T obj) const {
1664e35fdd936d133bf8a48de140a3c666897588a05shiqian      return matcher_.Matches(
1665e35fdd936d133bf8a48de140a3c666897588a05shiqian          CallableTraits<Callable>::template Invoke<T>(callable_, obj));
1666e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1667e35fdd936d133bf8a48de140a3c666897588a05shiqian
1668e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeTo(::std::ostream* os) const {
1669e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "result of the given callable ";
1670e35fdd936d133bf8a48de140a3c666897588a05shiqian      matcher_.DescribeTo(os);
1671e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1672e35fdd936d133bf8a48de140a3c666897588a05shiqian
1673e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeNegationTo(::std::ostream* os) const {
1674e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "result of the given callable ";
1675e35fdd936d133bf8a48de140a3c666897588a05shiqian      matcher_.DescribeNegationTo(os);
1676e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1677e35fdd936d133bf8a48de140a3c666897588a05shiqian
1678e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void ExplainMatchResultTo(T obj, ::std::ostream* os) const {
1679e35fdd936d133bf8a48de140a3c666897588a05shiqian      ::std::stringstream ss;
1680e35fdd936d133bf8a48de140a3c666897588a05shiqian      matcher_.ExplainMatchResultTo(
1681e35fdd936d133bf8a48de140a3c666897588a05shiqian          CallableTraits<Callable>::template Invoke<T>(callable_, obj),
1682e35fdd936d133bf8a48de140a3c666897588a05shiqian          &ss);
1683e35fdd936d133bf8a48de140a3c666897588a05shiqian      const internal::string s = ss.str();
1684e35fdd936d133bf8a48de140a3c666897588a05shiqian      if (s != "")
1685e35fdd936d133bf8a48de140a3c666897588a05shiqian        *os << "result of the given callable " << s;
1686e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1687e35fdd936d133bf8a48de140a3c666897588a05shiqian   private:
1688e35fdd936d133bf8a48de140a3c666897588a05shiqian    // Functors often define operator() as non-const method even though
1689e35fdd936d133bf8a48de140a3c666897588a05shiqian    // they are actualy stateless. But we need to use them even when
1690e35fdd936d133bf8a48de140a3c666897588a05shiqian    // 'this' is a const pointer. It's the user's responsibility not to
1691e35fdd936d133bf8a48de140a3c666897588a05shiqian    // use stateful callables with ResultOf(), which does't guarantee
1692e35fdd936d133bf8a48de140a3c666897588a05shiqian    // how many times the callable will be invoked.
1693e35fdd936d133bf8a48de140a3c666897588a05shiqian    mutable CallableStorageType callable_;
1694e35fdd936d133bf8a48de140a3c666897588a05shiqian    const Matcher<ResultType> matcher_;
1695e35fdd936d133bf8a48de140a3c666897588a05shiqian  };  // class Impl
1696e35fdd936d133bf8a48de140a3c666897588a05shiqian
1697e35fdd936d133bf8a48de140a3c666897588a05shiqian  const CallableStorageType callable_;
1698e35fdd936d133bf8a48de140a3c666897588a05shiqian  const Matcher<ResultType> matcher_;
1699e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1700e35fdd936d133bf8a48de140a3c666897588a05shiqian
1701e35fdd936d133bf8a48de140a3c666897588a05shiqian// Explains the result of matching a value against a functor matcher.
1702e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T, typename Callable>
1703e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid ExplainMatchResultTo(const ResultOfMatcher<Callable>& matcher,
1704e35fdd936d133bf8a48de140a3c666897588a05shiqian                          T obj, ::std::ostream* os) {
1705e35fdd936d133bf8a48de140a3c666897588a05shiqian  matcher.ExplainMatchResultTo(obj, os);
1706e35fdd936d133bf8a48de140a3c666897588a05shiqian}
1707e35fdd936d133bf8a48de140a3c666897588a05shiqian
17086a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// Implements an equality matcher for any STL-style container whose elements
17096a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// support ==. This matcher is like Eq(), but its failure explanations provide
17106a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// more detailed information that is useful when the container is used as a set.
17116a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// The failure message reports elements that are in one of the operands but not
17126a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// the other. The failure messages do not report duplicate or out-of-order
17136a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// elements in the containers (which don't properly matter to sets, but can
17146a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// occur if the containers are vectors or lists, for example).
17156a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan//
17166a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// Uses the container's const_iterator, value_type, operator ==,
17176a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// begin(), and end().
17186a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wantemplate <typename Container>
17196a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wanclass ContainerEqMatcher {
17206a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan public:
1721b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef internal::StlContainerView<Container> View;
1722b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef typename View::type StlContainer;
1723b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef typename View::const_reference StlContainerReference;
1724b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
1725b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  // We make a copy of rhs in case the elements in it are modified
1726b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  // after this matcher is created.
1727b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  explicit ContainerEqMatcher(const Container& rhs) : rhs_(View::Copy(rhs)) {
1728b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    // Makes sure the user doesn't instantiate this class template
1729b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    // with a const or reference type.
1730b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    testing::StaticAssertTypeEq<Container,
1731b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan        GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(Container))>();
1732b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  }
1733b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
1734b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  template <typename LhsContainer>
1735b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  bool Matches(const LhsContainer& lhs) const {
1736b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    // GMOCK_REMOVE_CONST_() is needed to work around an MSVC 8.0 bug
1737b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    // that causes LhsContainer to be a const type sometimes.
1738b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    typedef internal::StlContainerView<GMOCK_REMOVE_CONST_(LhsContainer)>
1739b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan        LhsView;
1740b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    StlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
1741b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    return lhs_stl_container == rhs_;
1742b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  }
17436a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan  void DescribeTo(::std::ostream* os) const {
17446a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan    *os << "equals ";
1745b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    UniversalPrinter<StlContainer>::Print(rhs_, os);
17466a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan  }
17476a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan  void DescribeNegationTo(::std::ostream* os) const {
17486a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan    *os << "does not equal ";
1749b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    UniversalPrinter<StlContainer>::Print(rhs_, os);
17506a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan  }
17516a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan
1752b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  template <typename LhsContainer>
1753b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  void ExplainMatchResultTo(const LhsContainer& lhs,
17546a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan                            ::std::ostream* os) const {
1755b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    // GMOCK_REMOVE_CONST_() is needed to work around an MSVC 8.0 bug
1756b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    // that causes LhsContainer to be a const type sometimes.
1757b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    typedef internal::StlContainerView<GMOCK_REMOVE_CONST_(LhsContainer)>
1758b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan        LhsView;
1759b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    typedef typename LhsView::type LhsStlContainer;
1760b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    StlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
1761b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
17626a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan    // Something is different. Check for missing values first.
17636a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan    bool printed_header = false;
1764b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    for (typename LhsStlContainer::const_iterator it =
1765b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan             lhs_stl_container.begin();
1766b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan         it != lhs_stl_container.end(); ++it) {
1767b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan      if (internal::ArrayAwareFind(rhs_.begin(), rhs_.end(), *it) ==
1768b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan          rhs_.end()) {
17696a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan        if (printed_header) {
17706a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan          *os << ", ";
17716a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan        } else {
17726a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan          *os << "Only in actual: ";
17736a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan          printed_header = true;
17746a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan        }
1775b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan        UniversalPrinter<typename LhsStlContainer::value_type>::Print(*it, os);
17766a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan      }
17776a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan    }
17786a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan
17796a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan    // Now check for extra values.
17806a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan    bool printed_header2 = false;
1781b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    for (typename StlContainer::const_iterator it = rhs_.begin();
17826a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan         it != rhs_.end(); ++it) {
1783b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan      if (internal::ArrayAwareFind(
1784b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan              lhs_stl_container.begin(), lhs_stl_container.end(), *it) ==
1785b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan          lhs_stl_container.end()) {
17866a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan        if (printed_header2) {
17876a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan          *os << ", ";
17886a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan        } else {
17896a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan          *os << (printed_header ? "; not" : "Not") << " in actual: ";
17906a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan          printed_header2 = true;
17916a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan        }
1792b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan        UniversalPrinter<typename StlContainer::value_type>::Print(*it, os);
17936a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan      }
17946a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan    }
17956a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan  }
17966a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan private:
1797b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  const StlContainer rhs_;
17986a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan};
17996a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan
1800b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename LhsContainer, typename Container>
18016a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wanvoid ExplainMatchResultTo(const ContainerEqMatcher<Container>& matcher,
1802b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan                          const LhsContainer& lhs,
18036a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan                          ::std::ostream* os) {
18046a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan  matcher.ExplainMatchResultTo(lhs, os);
18056a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan}
18066a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan
1807b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Implements Contains(element_matcher) for the given argument type Container.
1808b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename Container>
1809b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanclass ContainsMatcherImpl : public MatcherInterface<Container> {
1810b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan public:
1811b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(Container)) RawContainer;
1812b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef StlContainerView<RawContainer> View;
1813b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef typename View::type StlContainer;
1814b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef typename View::const_reference StlContainerReference;
1815b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef typename StlContainer::value_type Element;
1816b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
1817b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  template <typename InnerMatcher>
1818b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  explicit ContainsMatcherImpl(InnerMatcher inner_matcher)
1819b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan      : inner_matcher_(
1820b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan          testing::SafeMatcherCast<const Element&>(inner_matcher)) {}
1821b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
1822b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  // Returns true iff 'container' matches.
1823b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  virtual bool Matches(Container container) const {
1824b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    StlContainerReference stl_container = View::ConstReference(container);
1825b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    for (typename StlContainer::const_iterator it = stl_container.begin();
1826b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan         it != stl_container.end(); ++it) {
1827b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan      if (inner_matcher_.Matches(*it))
1828b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan        return true;
1829b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    }
1830b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    return false;
1831b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  }
1832b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
1833b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  // Describes what this matcher does.
1834b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
1835b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    *os << "contains at least one element that ";
1836b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    inner_matcher_.DescribeTo(os);
1837b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  }
1838b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
1839b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  // Describes what the negation of this matcher does.
1840b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
1841b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    *os << "doesn't contain any element that ";
1842b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    inner_matcher_.DescribeTo(os);
1843b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  }
1844b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
1845b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  // Explains why 'container' matches, or doesn't match, this matcher.
1846b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  virtual void ExplainMatchResultTo(Container container,
1847b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan                                    ::std::ostream* os) const {
1848b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    StlContainerReference stl_container = View::ConstReference(container);
1849b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
1850b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    // We need to explain which (if any) element matches inner_matcher_.
1851b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    typename StlContainer::const_iterator it = stl_container.begin();
1852b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    for (size_t i = 0; it != stl_container.end(); ++it, ++i) {
1853b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan      if (inner_matcher_.Matches(*it)) {
1854b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan        *os << "element " << i << " matches";
1855b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan        return;
1856b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan      }
1857b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    }
1858b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  }
1859b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
1860b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan private:
1861b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  const Matcher<const Element&> inner_matcher_;
1862b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan};
1863b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
1864b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Implements polymorphic Contains(element_matcher).
1865b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename M>
1866b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanclass ContainsMatcher {
1867b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan public:
1868b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  explicit ContainsMatcher(M m) : inner_matcher_(m) {}
1869b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
1870b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  template <typename Container>
1871b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  operator Matcher<Container>() const {
1872b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    return MakeMatcher(new ContainsMatcherImpl<Container>(inner_matcher_));
1873b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  }
1874b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
1875b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan private:
1876b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  const M inner_matcher_;
1877b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan};
1878b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
1879b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// Implements Key(inner_matcher) for the given argument pair type.
1880b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// Key(inner_matcher) matches an std::pair whose 'first' field matches
1881b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// inner_matcher.  For example, Contains(Key(Ge(5))) can be used to match an
1882b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// std::map that contains at least one element whose key is >= 5.
1883b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wantemplate <typename PairType>
1884b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wanclass KeyMatcherImpl : public MatcherInterface<PairType> {
1885b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan public:
1886b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(PairType)) RawPairType;
1887b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  typedef typename RawPairType::first_type KeyType;
1888b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
1889b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  template <typename InnerMatcher>
1890b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  explicit KeyMatcherImpl(InnerMatcher inner_matcher)
1891b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan      : inner_matcher_(
1892b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan          testing::SafeMatcherCast<const KeyType&>(inner_matcher)) {
1893b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  }
1894b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
1895b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  // Returns true iff 'key_value.first' (the key) matches the inner matcher.
1896b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  virtual bool Matches(PairType key_value) const {
1897b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan    return inner_matcher_.Matches(key_value.first);
1898b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  }
1899b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
1900b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  // Describes what this matcher does.
1901b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
1902b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan    *os << "has a key that ";
1903b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan    inner_matcher_.DescribeTo(os);
1904b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  }
1905b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
1906b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  // Describes what the negation of this matcher does.
1907b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
1908b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan    *os << "doesn't have a key that ";
1909b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan    inner_matcher_.DescribeTo(os);
1910b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  }
1911b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
1912b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  // Explains why 'key_value' matches, or doesn't match, this matcher.
1913b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  virtual void ExplainMatchResultTo(PairType key_value,
1914b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan                                    ::std::ostream* os) const {
1915b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan    inner_matcher_.ExplainMatchResultTo(key_value.first, os);
1916b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  }
1917b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
1918b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan private:
1919b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  const Matcher<const KeyType&> inner_matcher_;
1920b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan};
1921b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
1922b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// Implements polymorphic Key(matcher_for_key).
1923b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wantemplate <typename M>
1924b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wanclass KeyMatcher {
1925b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan public:
1926b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  explicit KeyMatcher(M m) : matcher_for_key_(m) {}
1927b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
1928b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  template <typename PairType>
1929b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  operator Matcher<PairType>() const {
1930b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan    return MakeMatcher(new KeyMatcherImpl<PairType>(matcher_for_key_));
1931b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  }
1932b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
1933b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan private:
1934b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  const M matcher_for_key_;
1935b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan};
1936b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
1937f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// Implements Pair(first_matcher, second_matcher) for the given argument pair
1938f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// type with its two matchers. See Pair() function below.
1939f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wantemplate <typename PairType>
1940f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wanclass PairMatcherImpl : public MatcherInterface<PairType> {
1941f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan public:
1942f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(PairType)) RawPairType;
1943f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  typedef typename RawPairType::first_type FirstType;
1944f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  typedef typename RawPairType::second_type SecondType;
1945f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
1946f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  template <typename FirstMatcher, typename SecondMatcher>
1947f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  PairMatcherImpl(FirstMatcher first_matcher, SecondMatcher second_matcher)
1948f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan      : first_matcher_(
1949f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan            testing::SafeMatcherCast<const FirstType&>(first_matcher)),
1950f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan        second_matcher_(
1951f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan            testing::SafeMatcherCast<const SecondType&>(second_matcher)) {
1952f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  }
1953f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
1954f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  // Returns true iff 'a_pair.first' matches first_matcher and 'a_pair.second'
1955f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  // matches second_matcher.
1956f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  virtual bool Matches(PairType a_pair) const {
1957f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    return first_matcher_.Matches(a_pair.first) &&
1958f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan           second_matcher_.Matches(a_pair.second);
1959f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  }
1960f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
1961f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  // Describes what this matcher does.
1962f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
1963f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    *os << "has a first field that ";
1964f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    first_matcher_.DescribeTo(os);
1965f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    *os << ", and has a second field that ";
1966f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    second_matcher_.DescribeTo(os);
1967f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  }
1968f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
1969f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  // Describes what the negation of this matcher does.
1970f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
1971f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    *os << "has a first field that ";
1972f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    first_matcher_.DescribeNegationTo(os);
1973f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    *os << ", or has a second field that ";
1974f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    second_matcher_.DescribeNegationTo(os);
1975f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  }
1976f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
1977f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  // Explains why 'a_pair' matches, or doesn't match, this matcher.
1978f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  virtual void ExplainMatchResultTo(PairType a_pair,
1979f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan                                    ::std::ostream* os) const {
1980f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    ::std::stringstream ss1;
1981f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    first_matcher_.ExplainMatchResultTo(a_pair.first, &ss1);
1982f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    internal::string s1 = ss1.str();
1983f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    if (s1 != "") {
1984f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan       s1 = "the first field " + s1;
1985f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    }
1986f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
1987f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    ::std::stringstream ss2;
1988f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    second_matcher_.ExplainMatchResultTo(a_pair.second, &ss2);
1989f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    internal::string s2 = ss2.str();
1990f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    if (s2 != "") {
1991f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan       s2 = "the second field " + s2;
1992f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    }
1993f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
1994f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    *os << s1;
1995f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    if (s1 != "" && s2 != "") {
1996f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan       *os << ", and ";
1997f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    }
1998f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    *os << s2;
1999f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  }
2000f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
2001f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan private:
2002f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  const Matcher<const FirstType&> first_matcher_;
2003f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  const Matcher<const SecondType&> second_matcher_;
2004f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan};
2005f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
2006f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// Implements polymorphic Pair(first_matcher, second_matcher).
2007f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wantemplate <typename FirstMatcher, typename SecondMatcher>
2008f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wanclass PairMatcher {
2009f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan public:
2010f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  PairMatcher(FirstMatcher first_matcher, SecondMatcher second_matcher)
2011f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan      : first_matcher_(first_matcher), second_matcher_(second_matcher) {}
2012f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
2013f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  template <typename PairType>
2014f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  operator Matcher<PairType> () const {
2015f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    return MakeMatcher(
2016f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan        new PairMatcherImpl<PairType>(
2017f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan            first_matcher_, second_matcher_));
2018f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  }
2019f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
2020f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan private:
2021f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  const FirstMatcher first_matcher_;
2022f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  const SecondMatcher second_matcher_;
2023f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan};
2024f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
20251afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// Implements ElementsAre() and ElementsAreArray().
20261afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wantemplate <typename Container>
20271afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wanclass ElementsAreMatcherImpl : public MatcherInterface<Container> {
20281afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan public:
20291afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(Container)) RawContainer;
20301afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  typedef internal::StlContainerView<RawContainer> View;
20311afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  typedef typename View::type StlContainer;
20321afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  typedef typename View::const_reference StlContainerReference;
20331afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  typedef typename StlContainer::value_type Element;
20341afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
20351afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  // Constructs the matcher from a sequence of element values or
20361afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  // element matchers.
20371afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  template <typename InputIter>
20381afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  ElementsAreMatcherImpl(InputIter first, size_t count) {
20391afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    matchers_.reserve(count);
20401afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    InputIter it = first;
20411afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    for (size_t i = 0; i != count; ++i, ++it) {
20421afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      matchers_.push_back(MatcherCast<const Element&>(*it));
20431afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    }
20441afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  }
20451afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
20461afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  // Returns true iff 'container' matches.
20471afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  virtual bool Matches(Container container) const {
20481afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    StlContainerReference stl_container = View::ConstReference(container);
20491afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    if (stl_container.size() != count())
20501afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      return false;
20511afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
20521afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    typename StlContainer::const_iterator it = stl_container.begin();
20531afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    for (size_t i = 0; i != count();  ++it, ++i) {
20541afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      if (!matchers_[i].Matches(*it))
20551afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        return false;
20561afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    }
20571afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
20581afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    return true;
20591afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  }
20601afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
20611afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  // Describes what this matcher does.
20621afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
20631afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    if (count() == 0) {
20641afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      *os << "is empty";
20651afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    } else if (count() == 1) {
20661afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      *os << "has 1 element that ";
20671afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      matchers_[0].DescribeTo(os);
20681afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    } else {
20691afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      *os << "has " << Elements(count()) << " where\n";
20701afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      for (size_t i = 0; i != count(); ++i) {
20711afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        *os << "element " << i << " ";
20721afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        matchers_[i].DescribeTo(os);
20731afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        if (i + 1 < count()) {
20741afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan          *os << ",\n";
20751afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        }
20761afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      }
20771afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    }
20781afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  }
20791afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
20801afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  // Describes what the negation of this matcher does.
20811afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
20821afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    if (count() == 0) {
20831afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      *os << "is not empty";
20841afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      return;
20851afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    }
20861afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
20871afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    *os << "does not have " << Elements(count()) << ", or\n";
20881afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    for (size_t i = 0; i != count(); ++i) {
20891afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      *os << "element " << i << " ";
20901afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      matchers_[i].DescribeNegationTo(os);
20911afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      if (i + 1 < count()) {
20921afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        *os << ", or\n";
20931afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      }
20941afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    }
20951afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  }
20961afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
20971afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  // Explains why 'container' matches, or doesn't match, this matcher.
20981afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  virtual void ExplainMatchResultTo(Container container,
20991afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan                                    ::std::ostream* os) const {
21001afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    StlContainerReference stl_container = View::ConstReference(container);
21011afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    if (Matches(container)) {
21021afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      // We need to explain why *each* element matches (the obvious
21031afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      // ones can be skipped).
21041afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
21051afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      bool reason_printed = false;
21061afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      typename StlContainer::const_iterator it = stl_container.begin();
21071afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      for (size_t i = 0; i != count(); ++it, ++i) {
21081afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        ::std::stringstream ss;
21091afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        matchers_[i].ExplainMatchResultTo(*it, &ss);
21101afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
21111afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        const string s = ss.str();
21121afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        if (!s.empty()) {
21131afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan          if (reason_printed) {
21141afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan            *os << ",\n";
21151afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan          }
21161afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan          *os << "element " << i << " " << s;
21171afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan          reason_printed = true;
21181afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        }
21191afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      }
21201afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    } else {
21211afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      // We need to explain why the container doesn't match.
21221afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      const size_t actual_count = stl_container.size();
21231afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      if (actual_count != count()) {
21241afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        // The element count doesn't match.  If the container is
21251afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        // empty, there's no need to explain anything as Google Mock
21261afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        // already prints the empty container.  Otherwise we just need
21271afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        // to show how many elements there actually are.
21281afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        if (actual_count != 0) {
21291afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan          *os << "has " << Elements(actual_count);
21301afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        }
21311afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        return;
21321afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      }
21331afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
21341afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      // The container has the right size but at least one element
21351afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      // doesn't match expectation.  We need to find this element and
21361afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      // explain why it doesn't match.
21371afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      typename StlContainer::const_iterator it = stl_container.begin();
21381afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      for (size_t i = 0; i != count(); ++it, ++i) {
21391afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        if (matchers_[i].Matches(*it)) {
21401afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan          continue;
21411afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        }
21421afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
21431afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        *os << "element " << i << " doesn't match";
21441afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
21451afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        ::std::stringstream ss;
21461afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        matchers_[i].ExplainMatchResultTo(*it, &ss);
21471afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        const string s = ss.str();
21481afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        if (!s.empty()) {
21491afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan          *os << " (" << s << ")";
21501afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        }
21511afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        return;
21521afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      }
21531afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    }
21541afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  }
21551afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
21561afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan private:
21571afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  static Message Elements(size_t count) {
21581afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    return Message() << count << (count == 1 ? " element" : " elements");
21591afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  }
21601afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
21611afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  size_t count() const { return matchers_.size(); }
21621afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  std::vector<Matcher<const Element&> > matchers_;
21631afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan};
21641afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
21651afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// Implements ElementsAre() of 0 arguments.
21661afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wanclass ElementsAreMatcher0 {
21671afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan public:
21681afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  ElementsAreMatcher0() {}
21691afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
21701afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  template <typename Container>
21711afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  operator Matcher<Container>() const {
21721afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(Container))
21731afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        RawContainer;
21741afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    typedef typename internal::StlContainerView<RawContainer>::type::value_type
21751afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        Element;
21761afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
21771afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    const Matcher<const Element&>* const matchers = NULL;
21781afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    return MakeMatcher(new ElementsAreMatcherImpl<Container>(matchers, 0));
21791afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  }
21801afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan};
21811afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
21821afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// Implements ElementsAreArray().
21831afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wantemplate <typename T>
21841afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wanclass ElementsAreArrayMatcher {
21851afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan public:
21861afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  ElementsAreArrayMatcher(const T* first, size_t count) :
21871afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      first_(first), count_(count) {}
21881afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
21891afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  template <typename Container>
21901afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  operator Matcher<Container>() const {
21911afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(Container))
21921afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        RawContainer;
21931afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    typedef typename internal::StlContainerView<RawContainer>::type::value_type
21941afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        Element;
21951afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
21961afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    return MakeMatcher(new ElementsAreMatcherImpl<Container>(first_, count_));
21971afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  }
21981afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
21991afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan private:
22001afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  const T* const first_;
22011afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  const size_t count_;
22021afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan};
22031afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
22041afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// Constants denoting interpolations in a matcher description string.
22051afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wanconst int kTupleInterpolation = -1;    // "%(*)s"
22061afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wanconst int kPercentInterpolation = -2;  // "%%"
22071afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wanconst int kInvalidInterpolation = -3;  // "%" followed by invalid text
22081afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
22091afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// Records the location and content of an interpolation.
22101afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wanstruct Interpolation {
22111afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  Interpolation(const char* start, const char* end, int param)
22121afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      : start_pos(start), end_pos(end), param_index(param) {}
22131afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
22141afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  // Points to the start of the interpolation (the '%' character).
22151afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  const char* start_pos;
22161afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  // Points to the first character after the interpolation.
22171afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  const char* end_pos;
22181afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  // 0-based index of the interpolated matcher parameter;
22191afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  // kTupleInterpolation for "%(*)s"; kPercentInterpolation for "%%".
22201afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  int param_index;
22211afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan};
22221afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
22231afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wantypedef ::std::vector<Interpolation> Interpolations;
22241afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
22251afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// Parses a matcher description string and returns a vector of
22261afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// interpolations that appear in the string; generates non-fatal
22271afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// failures iff 'description' is an invalid matcher description.
22281afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// 'param_names' is a NULL-terminated array of parameter names in the
22291afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// order they appear in the MATCHER_P*() parameter list.
22301afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wanInterpolations ValidateMatcherDescription(
22311afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    const char* param_names[], const char* description);
22321afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
22331afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// Returns the actual matcher description, given the matcher name,
22341afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// user-supplied description template string, interpolations in the
22351afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// string, and the printed values of the matcher parameters.
22361afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wanstring FormatMatcherDescription(
22371afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    const char* matcher_name, const char* description,
22381afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    const Interpolations& interp, const Strings& param_values);
22391afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
2240e35fdd936d133bf8a48de140a3c666897588a05shiqian}  // namespace internal
2241e35fdd936d133bf8a48de140a3c666897588a05shiqian
2242e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements MatcherCast().
2243e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T, typename M>
2244e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<T> MatcherCast(M matcher) {
2245e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::MatcherCastImpl<T, M>::Cast(matcher);
2246e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2247e35fdd936d133bf8a48de140a3c666897588a05shiqian
2248e35fdd936d133bf8a48de140a3c666897588a05shiqian// _ is a matcher that matches anything of any type.
2249e35fdd936d133bf8a48de140a3c666897588a05shiqian//
2250e35fdd936d133bf8a48de140a3c666897588a05shiqian// This definition is fine as:
2251e35fdd936d133bf8a48de140a3c666897588a05shiqian//
2252e35fdd936d133bf8a48de140a3c666897588a05shiqian//   1. The C++ standard permits using the name _ in a namespace that
2253e35fdd936d133bf8a48de140a3c666897588a05shiqian//      is not the global namespace or ::std.
2254e35fdd936d133bf8a48de140a3c666897588a05shiqian//   2. The AnythingMatcher class has no data member or constructor,
2255e35fdd936d133bf8a48de140a3c666897588a05shiqian//      so it's OK to create global variables of this type.
2256e35fdd936d133bf8a48de140a3c666897588a05shiqian//   3. c-style has approved of using _ in this case.
2257e35fdd936d133bf8a48de140a3c666897588a05shiqianconst internal::AnythingMatcher _ = {};
2258e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any value of the given type T.
2259e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
2260e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<T> A() { return MakeMatcher(new internal::AnyMatcherImpl<T>()); }
2261e35fdd936d133bf8a48de140a3c666897588a05shiqian
2262e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any value of the given type T.
2263e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
2264e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<T> An() { return A<T>(); }
2265e35fdd936d133bf8a48de140a3c666897588a05shiqian
2266e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything equal to x.
2267e35fdd936d133bf8a48de140a3c666897588a05shiqian// Note: if the parameter of Eq() were declared as const T&, Eq("foo")
2268e35fdd936d133bf8a48de140a3c666897588a05shiqian// wouldn't compile.
2269e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
2270e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::EqMatcher<T> Eq(T x) { return internal::EqMatcher<T>(x); }
2271e35fdd936d133bf8a48de140a3c666897588a05shiqian
2272e35fdd936d133bf8a48de140a3c666897588a05shiqian// Constructs a Matcher<T> from a 'value' of type T.  The constructed
2273e35fdd936d133bf8a48de140a3c666897588a05shiqian// matcher matches any value that's equal to 'value'.
2274e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
2275e35fdd936d133bf8a48de140a3c666897588a05shiqianMatcher<T>::Matcher(T value) { *this = Eq(value); }
2276e35fdd936d133bf8a48de140a3c666897588a05shiqian
2277e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a monomorphic matcher that matches anything with type Lhs
2278e35fdd936d133bf8a48de140a3c666897588a05shiqian// and equal to rhs.  A user may need to use this instead of Eq(...)
2279e35fdd936d133bf8a48de140a3c666897588a05shiqian// in order to resolve an overloading ambiguity.
2280e35fdd936d133bf8a48de140a3c666897588a05shiqian//
2281e35fdd936d133bf8a48de140a3c666897588a05shiqian// TypedEq<T>(x) is just a convenient short-hand for Matcher<T>(Eq(x))
2282e35fdd936d133bf8a48de140a3c666897588a05shiqian// or Matcher<T>(x), but more readable than the latter.
2283e35fdd936d133bf8a48de140a3c666897588a05shiqian//
2284e35fdd936d133bf8a48de140a3c666897588a05shiqian// We could define similar monomorphic matchers for other comparison
2285e35fdd936d133bf8a48de140a3c666897588a05shiqian// operations (e.g. TypedLt, TypedGe, and etc), but decided not to do
2286e35fdd936d133bf8a48de140a3c666897588a05shiqian// it yet as those are used much less than Eq() in practice.  A user
2287e35fdd936d133bf8a48de140a3c666897588a05shiqian// can always write Matcher<T>(Lt(5)) to be explicit about the type,
2288e35fdd936d133bf8a48de140a3c666897588a05shiqian// for example.
2289e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Lhs, typename Rhs>
2290e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<Lhs> TypedEq(const Rhs& rhs) { return Eq(rhs); }
2291e35fdd936d133bf8a48de140a3c666897588a05shiqian
2292e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything >= x.
2293e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs>
2294e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::GeMatcher<Rhs> Ge(Rhs x) {
2295e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::GeMatcher<Rhs>(x);
2296e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2297e35fdd936d133bf8a48de140a3c666897588a05shiqian
2298e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything > x.
2299e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs>
2300e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::GtMatcher<Rhs> Gt(Rhs x) {
2301e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::GtMatcher<Rhs>(x);
2302e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2303e35fdd936d133bf8a48de140a3c666897588a05shiqian
2304e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything <= x.
2305e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs>
2306e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::LeMatcher<Rhs> Le(Rhs x) {
2307e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::LeMatcher<Rhs>(x);
2308e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2309e35fdd936d133bf8a48de140a3c666897588a05shiqian
2310e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything < x.
2311e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs>
2312e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::LtMatcher<Rhs> Lt(Rhs x) {
2313e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::LtMatcher<Rhs>(x);
2314e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2315e35fdd936d133bf8a48de140a3c666897588a05shiqian
2316e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything != x.
2317e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs>
2318e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::NeMatcher<Rhs> Ne(Rhs x) {
2319e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::NeMatcher<Rhs>(x);
2320e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2321e35fdd936d133bf8a48de140a3c666897588a05shiqian
2322e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches any non-NULL pointer.
2323e35fdd936d133bf8a48de140a3c666897588a05shiqian// This is convenient as Not(NULL) doesn't compile (the compiler
2324e35fdd936d133bf8a48de140a3c666897588a05shiqian// thinks that that expression is comparing a pointer with an integer).
2325e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::NotNullMatcher > NotNull() {
2326e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::NotNullMatcher());
2327e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2328e35fdd936d133bf8a48de140a3c666897588a05shiqian
2329e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches any argument that
2330e35fdd936d133bf8a48de140a3c666897588a05shiqian// references variable x.
2331e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
2332e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::RefMatcher<T&> Ref(T& x) {  // NOLINT
2333e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::RefMatcher<T&>(x);
2334e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2335e35fdd936d133bf8a48de140a3c666897588a05shiqian
2336e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any double argument approximately
2337e35fdd936d133bf8a48de140a3c666897588a05shiqian// equal to rhs, where two NANs are considered unequal.
2338e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::FloatingEqMatcher<double> DoubleEq(double rhs) {
2339e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::FloatingEqMatcher<double>(rhs, false);
2340e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2341e35fdd936d133bf8a48de140a3c666897588a05shiqian
2342e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any double argument approximately
2343e35fdd936d133bf8a48de140a3c666897588a05shiqian// equal to rhs, including NaN values when rhs is NaN.
2344e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::FloatingEqMatcher<double> NanSensitiveDoubleEq(double rhs) {
2345e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::FloatingEqMatcher<double>(rhs, true);
2346e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2347e35fdd936d133bf8a48de140a3c666897588a05shiqian
2348e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any float argument approximately
2349e35fdd936d133bf8a48de140a3c666897588a05shiqian// equal to rhs, where two NANs are considered unequal.
2350e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::FloatingEqMatcher<float> FloatEq(float rhs) {
2351e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::FloatingEqMatcher<float>(rhs, false);
2352e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2353e35fdd936d133bf8a48de140a3c666897588a05shiqian
2354e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any double argument approximately
2355e35fdd936d133bf8a48de140a3c666897588a05shiqian// equal to rhs, including NaN values when rhs is NaN.
2356e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::FloatingEqMatcher<float> NanSensitiveFloatEq(float rhs) {
2357e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::FloatingEqMatcher<float>(rhs, true);
2358e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2359e35fdd936d133bf8a48de140a3c666897588a05shiqian
2360e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches a pointer (raw or smart) that points
2361e35fdd936d133bf8a48de140a3c666897588a05shiqian// to a value that matches inner_matcher.
2362e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename InnerMatcher>
2363e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::PointeeMatcher<InnerMatcher> Pointee(
2364e35fdd936d133bf8a48de140a3c666897588a05shiqian    const InnerMatcher& inner_matcher) {
2365e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::PointeeMatcher<InnerMatcher>(inner_matcher);
2366e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2367e35fdd936d133bf8a48de140a3c666897588a05shiqian
2368e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches an object whose given field matches
2369e35fdd936d133bf8a48de140a3c666897588a05shiqian// 'matcher'.  For example,
2370e35fdd936d133bf8a48de140a3c666897588a05shiqian//   Field(&Foo::number, Ge(5))
2371e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a Foo object x iff x.number >= 5.
2372e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Class, typename FieldType, typename FieldMatcher>
2373e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<
2374e35fdd936d133bf8a48de140a3c666897588a05shiqian  internal::FieldMatcher<Class, FieldType> > Field(
2375e35fdd936d133bf8a48de140a3c666897588a05shiqian    FieldType Class::*field, const FieldMatcher& matcher) {
2376e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(
2377e35fdd936d133bf8a48de140a3c666897588a05shiqian      internal::FieldMatcher<Class, FieldType>(
2378e35fdd936d133bf8a48de140a3c666897588a05shiqian          field, MatcherCast<const FieldType&>(matcher)));
2379e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The call to MatcherCast() is required for supporting inner
2380e35fdd936d133bf8a48de140a3c666897588a05shiqian  // matchers of compatible types.  For example, it allows
2381e35fdd936d133bf8a48de140a3c666897588a05shiqian  //   Field(&Foo::bar, m)
2382e35fdd936d133bf8a48de140a3c666897588a05shiqian  // to compile where bar is an int32 and m is a matcher for int64.
2383e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2384e35fdd936d133bf8a48de140a3c666897588a05shiqian
2385e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches an object whose given property
2386e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches 'matcher'.  For example,
2387e35fdd936d133bf8a48de140a3c666897588a05shiqian//   Property(&Foo::str, StartsWith("hi"))
2388e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a Foo object x iff x.str() starts with "hi".
2389e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Class, typename PropertyType, typename PropertyMatcher>
2390e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<
2391e35fdd936d133bf8a48de140a3c666897588a05shiqian  internal::PropertyMatcher<Class, PropertyType> > Property(
2392e35fdd936d133bf8a48de140a3c666897588a05shiqian    PropertyType (Class::*property)() const, const PropertyMatcher& matcher) {
2393e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(
2394e35fdd936d133bf8a48de140a3c666897588a05shiqian      internal::PropertyMatcher<Class, PropertyType>(
2395e35fdd936d133bf8a48de140a3c666897588a05shiqian          property,
2396e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan          MatcherCast<GMOCK_REFERENCE_TO_CONST_(PropertyType)>(matcher)));
2397e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The call to MatcherCast() is required for supporting inner
2398e35fdd936d133bf8a48de140a3c666897588a05shiqian  // matchers of compatible types.  For example, it allows
2399e35fdd936d133bf8a48de140a3c666897588a05shiqian  //   Property(&Foo::bar, m)
2400e35fdd936d133bf8a48de140a3c666897588a05shiqian  // to compile where bar() returns an int32 and m is a matcher for int64.
2401e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2402e35fdd936d133bf8a48de140a3c666897588a05shiqian
2403e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches an object iff the result of applying
2404e35fdd936d133bf8a48de140a3c666897588a05shiqian// a callable to x matches 'matcher'.
2405e35fdd936d133bf8a48de140a3c666897588a05shiqian// For example,
2406e35fdd936d133bf8a48de140a3c666897588a05shiqian//   ResultOf(f, StartsWith("hi"))
2407e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a Foo object x iff f(x) starts with "hi".
2408e35fdd936d133bf8a48de140a3c666897588a05shiqian// callable parameter can be a function, function pointer, or a functor.
2409e35fdd936d133bf8a48de140a3c666897588a05shiqian// Callable has to satisfy the following conditions:
2410e35fdd936d133bf8a48de140a3c666897588a05shiqian//   * It is required to keep no state affecting the results of
2411e35fdd936d133bf8a48de140a3c666897588a05shiqian//     the calls on it and make no assumptions about how many calls
2412e35fdd936d133bf8a48de140a3c666897588a05shiqian//     will be made. Any state it keeps must be protected from the
2413e35fdd936d133bf8a48de140a3c666897588a05shiqian//     concurrent access.
2414e35fdd936d133bf8a48de140a3c666897588a05shiqian//   * If it is a function object, it has to define type result_type.
2415e35fdd936d133bf8a48de140a3c666897588a05shiqian//     We recommend deriving your functor classes from std::unary_function.
2416e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Callable, typename ResultOfMatcher>
2417e35fdd936d133bf8a48de140a3c666897588a05shiqianinternal::ResultOfMatcher<Callable> ResultOf(
2418e35fdd936d133bf8a48de140a3c666897588a05shiqian    Callable callable, const ResultOfMatcher& matcher) {
2419e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::ResultOfMatcher<Callable>(
2420e35fdd936d133bf8a48de140a3c666897588a05shiqian          callable,
2421e35fdd936d133bf8a48de140a3c666897588a05shiqian          MatcherCast<typename internal::CallableTraits<Callable>::ResultType>(
2422e35fdd936d133bf8a48de140a3c666897588a05shiqian              matcher));
2423e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The call to MatcherCast() is required for supporting inner
2424e35fdd936d133bf8a48de140a3c666897588a05shiqian  // matchers of compatible types.  For example, it allows
2425e35fdd936d133bf8a48de140a3c666897588a05shiqian  //   ResultOf(Function, m)
2426e35fdd936d133bf8a48de140a3c666897588a05shiqian  // to compile where Function() returns an int32 and m is a matcher for int64.
2427e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2428e35fdd936d133bf8a48de140a3c666897588a05shiqian
2429e35fdd936d133bf8a48de140a3c666897588a05shiqian// String matchers.
2430e35fdd936d133bf8a48de140a3c666897588a05shiqian
2431e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string equal to str.
2432e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> >
2433e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrEq(const internal::string& str) {
2434e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>(
2435e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, true, true));
2436e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2437e35fdd936d133bf8a48de140a3c666897588a05shiqian
2438e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string not equal to str.
2439e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> >
2440e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrNe(const internal::string& str) {
2441e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>(
2442e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, false, true));
2443e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2444e35fdd936d133bf8a48de140a3c666897588a05shiqian
2445e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string equal to str, ignoring case.
2446e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> >
2447e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrCaseEq(const internal::string& str) {
2448e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>(
2449e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, true, false));
2450e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2451e35fdd936d133bf8a48de140a3c666897588a05shiqian
2452e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string not equal to str, ignoring case.
2453e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> >
2454e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrCaseNe(const internal::string& str) {
2455e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>(
2456e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, false, false));
2457e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2458e35fdd936d133bf8a48de140a3c666897588a05shiqian
2459e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any string, std::string, or C string
2460e35fdd936d133bf8a48de140a3c666897588a05shiqian// that contains the given substring.
2461e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::HasSubstrMatcher<internal::string> >
2462e35fdd936d133bf8a48de140a3c666897588a05shiqian    HasSubstr(const internal::string& substring) {
2463e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::HasSubstrMatcher<internal::string>(
2464e35fdd936d133bf8a48de140a3c666897588a05shiqian      substring));
2465e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2466e35fdd936d133bf8a48de140a3c666897588a05shiqian
2467e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that starts with 'prefix' (case-sensitive).
2468e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StartsWithMatcher<internal::string> >
2469e35fdd936d133bf8a48de140a3c666897588a05shiqian    StartsWith(const internal::string& prefix) {
2470e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StartsWithMatcher<internal::string>(
2471e35fdd936d133bf8a48de140a3c666897588a05shiqian      prefix));
2472e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2473e35fdd936d133bf8a48de140a3c666897588a05shiqian
2474e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that ends with 'suffix' (case-sensitive).
2475e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::EndsWithMatcher<internal::string> >
2476e35fdd936d133bf8a48de140a3c666897588a05shiqian    EndsWith(const internal::string& suffix) {
2477e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::EndsWithMatcher<internal::string>(
2478e35fdd936d133bf8a48de140a3c666897588a05shiqian      suffix));
2479e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2480e35fdd936d133bf8a48de140a3c666897588a05shiqian
2481e35fdd936d133bf8a48de140a3c666897588a05shiqian#ifdef GMOCK_HAS_REGEX
2482e35fdd936d133bf8a48de140a3c666897588a05shiqian
2483e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that fully matches regular expression 'regex'.
2484e35fdd936d133bf8a48de140a3c666897588a05shiqian// The matcher takes ownership of 'regex'.
2485e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex(
2486e35fdd936d133bf8a48de140a3c666897588a05shiqian    const internal::RE* regex) {
2487e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, true));
2488e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2489e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex(
2490e35fdd936d133bf8a48de140a3c666897588a05shiqian    const internal::string& regex) {
2491e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MatchesRegex(new internal::RE(regex));
2492e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2493e35fdd936d133bf8a48de140a3c666897588a05shiqian
2494e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that contains regular expression 'regex'.
2495e35fdd936d133bf8a48de140a3c666897588a05shiqian// The matcher takes ownership of 'regex'.
2496e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex(
2497e35fdd936d133bf8a48de140a3c666897588a05shiqian    const internal::RE* regex) {
2498e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, false));
2499e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2500e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex(
2501e35fdd936d133bf8a48de140a3c666897588a05shiqian    const internal::string& regex) {
2502e35fdd936d133bf8a48de140a3c666897588a05shiqian  return ContainsRegex(new internal::RE(regex));
2503e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2504e35fdd936d133bf8a48de140a3c666897588a05shiqian
2505e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif  // GMOCK_HAS_REGEX
2506e35fdd936d133bf8a48de140a3c666897588a05shiqian
2507e35fdd936d133bf8a48de140a3c666897588a05shiqian#if GTEST_HAS_GLOBAL_WSTRING || GTEST_HAS_STD_WSTRING
2508e35fdd936d133bf8a48de140a3c666897588a05shiqian// Wide string matchers.
2509e35fdd936d133bf8a48de140a3c666897588a05shiqian
2510e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string equal to str.
2511e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> >
2512e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrEq(const internal::wstring& str) {
2513e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>(
2514e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, true, true));
2515e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2516e35fdd936d133bf8a48de140a3c666897588a05shiqian
2517e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string not equal to str.
2518e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> >
2519e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrNe(const internal::wstring& str) {
2520e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>(
2521e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, false, true));
2522e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2523e35fdd936d133bf8a48de140a3c666897588a05shiqian
2524e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string equal to str, ignoring case.
2525e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> >
2526e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrCaseEq(const internal::wstring& str) {
2527e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>(
2528e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, true, false));
2529e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2530e35fdd936d133bf8a48de140a3c666897588a05shiqian
2531e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string not equal to str, ignoring case.
2532e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> >
2533e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrCaseNe(const internal::wstring& str) {
2534e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>(
2535e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, false, false));
2536e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2537e35fdd936d133bf8a48de140a3c666897588a05shiqian
2538e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any wstring, std::wstring, or C wide string
2539e35fdd936d133bf8a48de140a3c666897588a05shiqian// that contains the given substring.
2540e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::HasSubstrMatcher<internal::wstring> >
2541e35fdd936d133bf8a48de140a3c666897588a05shiqian    HasSubstr(const internal::wstring& substring) {
2542e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::HasSubstrMatcher<internal::wstring>(
2543e35fdd936d133bf8a48de140a3c666897588a05shiqian      substring));
2544e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2545e35fdd936d133bf8a48de140a3c666897588a05shiqian
2546e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that starts with 'prefix' (case-sensitive).
2547e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StartsWithMatcher<internal::wstring> >
2548e35fdd936d133bf8a48de140a3c666897588a05shiqian    StartsWith(const internal::wstring& prefix) {
2549e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StartsWithMatcher<internal::wstring>(
2550e35fdd936d133bf8a48de140a3c666897588a05shiqian      prefix));
2551e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2552e35fdd936d133bf8a48de140a3c666897588a05shiqian
2553e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that ends with 'suffix' (case-sensitive).
2554e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::EndsWithMatcher<internal::wstring> >
2555e35fdd936d133bf8a48de140a3c666897588a05shiqian    EndsWith(const internal::wstring& suffix) {
2556e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::EndsWithMatcher<internal::wstring>(
2557e35fdd936d133bf8a48de140a3c666897588a05shiqian      suffix));
2558e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2559e35fdd936d133bf8a48de140a3c666897588a05shiqian
2560e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif  // GTEST_HAS_GLOBAL_WSTRING || GTEST_HAS_STD_WSTRING
2561e35fdd936d133bf8a48de140a3c666897588a05shiqian
2562e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the
2563e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field == the second field.
2564e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Eq2Matcher Eq() { return internal::Eq2Matcher(); }
2565e35fdd936d133bf8a48de140a3c666897588a05shiqian
2566e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the
2567e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field >= the second field.
2568e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Ge2Matcher Ge() { return internal::Ge2Matcher(); }
2569e35fdd936d133bf8a48de140a3c666897588a05shiqian
2570e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the
2571e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field > the second field.
2572e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Gt2Matcher Gt() { return internal::Gt2Matcher(); }
2573e35fdd936d133bf8a48de140a3c666897588a05shiqian
2574e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the
2575e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field <= the second field.
2576e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Le2Matcher Le() { return internal::Le2Matcher(); }
2577e35fdd936d133bf8a48de140a3c666897588a05shiqian
2578e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the
2579e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field < the second field.
2580e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Lt2Matcher Lt() { return internal::Lt2Matcher(); }
2581e35fdd936d133bf8a48de140a3c666897588a05shiqian
2582e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the
2583e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field != the second field.
2584e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Ne2Matcher Ne() { return internal::Ne2Matcher(); }
2585e35fdd936d133bf8a48de140a3c666897588a05shiqian
2586e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any value of type T that m doesn't
2587e35fdd936d133bf8a48de140a3c666897588a05shiqian// match.
2588e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename InnerMatcher>
2589e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::NotMatcher<InnerMatcher> Not(InnerMatcher m) {
2590e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::NotMatcher<InnerMatcher>(m);
2591e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2592e35fdd936d133bf8a48de140a3c666897588a05shiqian
2593e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any value that matches all of the
2594e35fdd936d133bf8a48de140a3c666897588a05shiqian// given matchers.
2595e35fdd936d133bf8a48de140a3c666897588a05shiqian//
2596e35fdd936d133bf8a48de140a3c666897588a05shiqian// For now we only support up to 5 matchers.  Support for more
2597e35fdd936d133bf8a48de140a3c666897588a05shiqian// matchers can be added as needed, or the user can use nested
2598e35fdd936d133bf8a48de140a3c666897588a05shiqian// AllOf()s.
2599e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2>
2600e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::BothOfMatcher<Matcher1, Matcher2>
2601e35fdd936d133bf8a48de140a3c666897588a05shiqianAllOf(Matcher1 m1, Matcher2 m2) {
2602e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::BothOfMatcher<Matcher1, Matcher2>(m1, m2);
2603e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2604e35fdd936d133bf8a48de140a3c666897588a05shiqian
2605e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2, typename Matcher3>
2606e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::BothOfMatcher<Matcher1,
2607e35fdd936d133bf8a48de140a3c666897588a05shiqian           internal::BothOfMatcher<Matcher2, Matcher3> >
2608e35fdd936d133bf8a48de140a3c666897588a05shiqianAllOf(Matcher1 m1, Matcher2 m2, Matcher3 m3) {
2609e35fdd936d133bf8a48de140a3c666897588a05shiqian  return AllOf(m1, AllOf(m2, m3));
2610e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2611e35fdd936d133bf8a48de140a3c666897588a05shiqian
2612e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2, typename Matcher3,
2613e35fdd936d133bf8a48de140a3c666897588a05shiqian          typename Matcher4>
2614e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::BothOfMatcher<Matcher1,
2615e35fdd936d133bf8a48de140a3c666897588a05shiqian           internal::BothOfMatcher<Matcher2,
2616e35fdd936d133bf8a48de140a3c666897588a05shiqian               internal::BothOfMatcher<Matcher3, Matcher4> > >
2617e35fdd936d133bf8a48de140a3c666897588a05shiqianAllOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4) {
2618e35fdd936d133bf8a48de140a3c666897588a05shiqian  return AllOf(m1, AllOf(m2, m3, m4));
2619e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2620e35fdd936d133bf8a48de140a3c666897588a05shiqian
2621e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2, typename Matcher3,
2622e35fdd936d133bf8a48de140a3c666897588a05shiqian          typename Matcher4, typename Matcher5>
2623e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::BothOfMatcher<Matcher1,
2624e35fdd936d133bf8a48de140a3c666897588a05shiqian           internal::BothOfMatcher<Matcher2,
2625e35fdd936d133bf8a48de140a3c666897588a05shiqian               internal::BothOfMatcher<Matcher3,
2626e35fdd936d133bf8a48de140a3c666897588a05shiqian                   internal::BothOfMatcher<Matcher4, Matcher5> > > >
2627e35fdd936d133bf8a48de140a3c666897588a05shiqianAllOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4, Matcher5 m5) {
2628e35fdd936d133bf8a48de140a3c666897588a05shiqian  return AllOf(m1, AllOf(m2, m3, m4, m5));
2629e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2630e35fdd936d133bf8a48de140a3c666897588a05shiqian
2631e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any value that matches at least one
2632e35fdd936d133bf8a48de140a3c666897588a05shiqian// of the given matchers.
2633e35fdd936d133bf8a48de140a3c666897588a05shiqian//
2634e35fdd936d133bf8a48de140a3c666897588a05shiqian// For now we only support up to 5 matchers.  Support for more
2635e35fdd936d133bf8a48de140a3c666897588a05shiqian// matchers can be added as needed, or the user can use nested
2636e35fdd936d133bf8a48de140a3c666897588a05shiqian// AnyOf()s.
2637e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2>
2638e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::EitherOfMatcher<Matcher1, Matcher2>
2639e35fdd936d133bf8a48de140a3c666897588a05shiqianAnyOf(Matcher1 m1, Matcher2 m2) {
2640e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::EitherOfMatcher<Matcher1, Matcher2>(m1, m2);
2641e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2642e35fdd936d133bf8a48de140a3c666897588a05shiqian
2643e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2, typename Matcher3>
2644e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::EitherOfMatcher<Matcher1,
2645e35fdd936d133bf8a48de140a3c666897588a05shiqian           internal::EitherOfMatcher<Matcher2, Matcher3> >
2646e35fdd936d133bf8a48de140a3c666897588a05shiqianAnyOf(Matcher1 m1, Matcher2 m2, Matcher3 m3) {
2647e35fdd936d133bf8a48de140a3c666897588a05shiqian  return AnyOf(m1, AnyOf(m2, m3));
2648e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2649e35fdd936d133bf8a48de140a3c666897588a05shiqian
2650e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2, typename Matcher3,
2651e35fdd936d133bf8a48de140a3c666897588a05shiqian          typename Matcher4>
2652e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::EitherOfMatcher<Matcher1,
2653e35fdd936d133bf8a48de140a3c666897588a05shiqian           internal::EitherOfMatcher<Matcher2,
2654e35fdd936d133bf8a48de140a3c666897588a05shiqian               internal::EitherOfMatcher<Matcher3, Matcher4> > >
2655e35fdd936d133bf8a48de140a3c666897588a05shiqianAnyOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4) {
2656e35fdd936d133bf8a48de140a3c666897588a05shiqian  return AnyOf(m1, AnyOf(m2, m3, m4));
2657e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2658e35fdd936d133bf8a48de140a3c666897588a05shiqian
2659e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2, typename Matcher3,
2660e35fdd936d133bf8a48de140a3c666897588a05shiqian          typename Matcher4, typename Matcher5>
2661e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::EitherOfMatcher<Matcher1,
2662e35fdd936d133bf8a48de140a3c666897588a05shiqian           internal::EitherOfMatcher<Matcher2,
2663e35fdd936d133bf8a48de140a3c666897588a05shiqian               internal::EitherOfMatcher<Matcher3,
2664e35fdd936d133bf8a48de140a3c666897588a05shiqian                   internal::EitherOfMatcher<Matcher4, Matcher5> > > >
2665e35fdd936d133bf8a48de140a3c666897588a05shiqianAnyOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4, Matcher5 m5) {
2666e35fdd936d133bf8a48de140a3c666897588a05shiqian  return AnyOf(m1, AnyOf(m2, m3, m4, m5));
2667e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2668e35fdd936d133bf8a48de140a3c666897588a05shiqian
2669e35fdd936d133bf8a48de140a3c666897588a05shiqian// Returns a matcher that matches anything that satisfies the given
2670e35fdd936d133bf8a48de140a3c666897588a05shiqian// predicate.  The predicate can be any unary function or functor
2671e35fdd936d133bf8a48de140a3c666897588a05shiqian// whose return type can be implicitly converted to bool.
2672e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Predicate>
2673e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::TrulyMatcher<Predicate> >
2674e35fdd936d133bf8a48de140a3c666897588a05shiqianTruly(Predicate pred) {
2675e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::TrulyMatcher<Predicate>(pred));
2676e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2677e35fdd936d133bf8a48de140a3c666897588a05shiqian
26786a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// Returns a matcher that matches an equal container.
26796a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// This matcher behaves like Eq(), but in the event of mismatch lists the
26806a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// values that are included in one container but not the other. (Duplicate
26816a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// values and order differences are not explained.)
26826a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wantemplate <typename Container>
2683b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.waninline PolymorphicMatcher<internal::ContainerEqMatcher<
2684b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan                            GMOCK_REMOVE_CONST_(Container)> >
26856a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan    ContainerEq(const Container& rhs) {
2686b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  // This following line is for working around a bug in MSVC 8.0,
2687b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  // which causes Container to be a const type sometimes.
2688b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef GMOCK_REMOVE_CONST_(Container) RawContainer;
2689b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  return MakePolymorphicMatcher(internal::ContainerEqMatcher<RawContainer>(rhs));
2690b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}
2691b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
2692b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Matches an STL-style container or a native array that contains at
2693b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// least one element matching the given value or matcher.
2694b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//
2695b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Examples:
2696b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   ::std::set<int> page_ids;
2697b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   page_ids.insert(3);
2698b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   page_ids.insert(1);
2699b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   EXPECT_THAT(page_ids, Contains(1));
2700b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   EXPECT_THAT(page_ids, Contains(Gt(2)));
2701b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   EXPECT_THAT(page_ids, Not(Contains(4)));
2702b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//
2703b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   ::std::map<int, size_t> page_lengths;
2704b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   page_lengths[1] = 100;
27054019819dbb5369b1d638503523a04aaf7eb2a5adzhanyong.wan//   EXPECT_THAT(page_lengths,
27064019819dbb5369b1d638503523a04aaf7eb2a5adzhanyong.wan//               Contains(::std::pair<const int, size_t>(1, 100)));
2707b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//
2708b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   const char* user_ids[] = { "joe", "mike", "tom" };
2709b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   EXPECT_THAT(user_ids, Contains(Eq(::std::string("tom"))));
2710b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename M>
2711b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.waninline internal::ContainsMatcher<M> Contains(M matcher) {
2712b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  return internal::ContainsMatcher<M>(matcher);
27136a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan}
27146a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan
2715b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// Key(inner_matcher) matches an std::pair whose 'first' field matches
2716b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// inner_matcher.  For example, Contains(Key(Ge(5))) can be used to match an
2717b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// std::map that contains at least one element whose key is >= 5.
2718b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wantemplate <typename M>
2719b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.waninline internal::KeyMatcher<M> Key(M inner_matcher) {
2720b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  return internal::KeyMatcher<M>(inner_matcher);
2721b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan}
2722b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
2723f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// Pair(first_matcher, second_matcher) matches a std::pair whose 'first' field
2724f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// matches first_matcher and whose 'second' field matches second_matcher.  For
2725f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// example, EXPECT_THAT(map_type, ElementsAre(Pair(Ge(5), "foo"))) can be used
2726f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// to match a std::map<int, string> that contains exactly one element whose key
2727f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// is >= 5 and whose value equals "foo".
2728f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wantemplate <typename FirstMatcher, typename SecondMatcher>
2729f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.waninline internal::PairMatcher<FirstMatcher, SecondMatcher>
2730f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wanPair(FirstMatcher first_matcher, SecondMatcher second_matcher) {
2731f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  return internal::PairMatcher<FirstMatcher, SecondMatcher>(
2732f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan      first_matcher, second_matcher);
2733f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan}
2734f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
2735e35fdd936d133bf8a48de140a3c666897588a05shiqian// Returns a predicate that is satisfied by anything that matches the
2736e35fdd936d133bf8a48de140a3c666897588a05shiqian// given matcher.
2737e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename M>
2738e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::MatcherAsPredicate<M> Matches(M matcher) {
2739e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::MatcherAsPredicate<M>(matcher);
2740e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2741e35fdd936d133bf8a48de140a3c666897588a05shiqian
2742b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Returns true iff the value matches the matcher.
2743b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename T, typename M>
2744b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.waninline bool Value(const T& value, M matcher) {
2745b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  return testing::Matches(matcher)(value);
2746b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}
2747b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
2748bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan// AllArgs(m) is a synonym of m.  This is useful in
2749bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan//
2750bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan//   EXPECT_CALL(foo, Bar(_, _)).With(AllArgs(Eq()));
2751bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan//
2752bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan// which is easier to read than
2753bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan//
2754bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan//   EXPECT_CALL(foo, Bar(_, _)).With(Eq());
2755bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wantemplate <typename InnerMatcher>
2756bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.waninline InnerMatcher AllArgs(const InnerMatcher& matcher) { return matcher; }
2757bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan
2758e35fdd936d133bf8a48de140a3c666897588a05shiqian// These macros allow using matchers to check values in Google Test
2759e35fdd936d133bf8a48de140a3c666897588a05shiqian// tests.  ASSERT_THAT(value, matcher) and EXPECT_THAT(value, matcher)
2760e35fdd936d133bf8a48de140a3c666897588a05shiqian// succeed iff the value matches the matcher.  If the assertion fails,
2761e35fdd936d133bf8a48de140a3c666897588a05shiqian// the value and the description of the matcher will be printed.
2762e35fdd936d133bf8a48de140a3c666897588a05shiqian#define ASSERT_THAT(value, matcher) ASSERT_PRED_FORMAT1(\
2763e35fdd936d133bf8a48de140a3c666897588a05shiqian    ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
2764e35fdd936d133bf8a48de140a3c666897588a05shiqian#define EXPECT_THAT(value, matcher) EXPECT_PRED_FORMAT1(\
2765e35fdd936d133bf8a48de140a3c666897588a05shiqian    ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
2766e35fdd936d133bf8a48de140a3c666897588a05shiqian
2767e35fdd936d133bf8a48de140a3c666897588a05shiqian}  // namespace testing
2768e35fdd936d133bf8a48de140a3c666897588a05shiqian
2769e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif  // GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_
2770