gmock-matchers.h revision 8d3dc0cdd870ecc53d8a8ba30566b3cd70910863
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>
46ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan#include <utility>
47e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <vector>
48e35fdd936d133bf8a48de140a3c666897588a05shiqian
4953e08c44dd34857ba57581d7c5774d6c96a8d0e1zhanyong.wan#include "gmock/internal/gmock-internal-utils.h"
5053e08c44dd34857ba57581d7c5774d6c96a8d0e1zhanyong.wan#include "gmock/internal/gmock-port.h"
5153e08c44dd34857ba57581d7c5774d6c96a8d0e1zhanyong.wan#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
67821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// MatchResultListener is an abstract class.  Its << operator can be
68821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// used by a matcher to explain why a value matches or doesn't match.
69821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan//
70821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// TODO(wan@google.com): add method
71821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan//   bool InterestedInWhy(bool result) const;
72821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// to indicate whether the listener is interested in why the match
73821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// result is 'result'.
74821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wanclass MatchResultListener {
75821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan public:
76821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // Creates a listener object with the given underlying ostream.  The
77821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // listener does not own the ostream.
78821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  explicit MatchResultListener(::std::ostream* os) : stream_(os) {}
79821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  virtual ~MatchResultListener() = 0;  // Makes this class abstract.
80821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
81821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // Streams x to the underlying ostream; does nothing if the ostream
82821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // is NULL.
83821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  template <typename T>
84821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  MatchResultListener& operator<<(const T& x) {
85821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (stream_ != NULL)
86821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      *stream_ << x;
87821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    return *this;
88821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  }
89821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
90821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // Returns the underlying ostream.
91821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  ::std::ostream* stream() { return stream_; }
92821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
93a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan  // Returns true iff the listener is interested in an explanation of
94a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan  // the match result.  A matcher's MatchAndExplain() method can use
95a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan  // this information to avoid generating the explanation when no one
96a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan  // intends to hear it.
97a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan  bool IsInterested() const { return stream_ != NULL; }
98a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan
99821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan private:
100821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  ::std::ostream* const stream_;
101821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
102821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  GTEST_DISALLOW_COPY_AND_ASSIGN_(MatchResultListener);
103821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan};
104821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
105821133180ccdb9eb062548d964929ba3b354eb84zhanyong.waninline MatchResultListener::~MatchResultListener() {
106821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan}
107821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
108e35fdd936d133bf8a48de140a3c666897588a05shiqian// The implementation of a matcher.
109e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
110e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatcherInterface {
111e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
112e35fdd936d133bf8a48de140a3c666897588a05shiqian  virtual ~MatcherInterface() {}
113e35fdd936d133bf8a48de140a3c666897588a05shiqian
114821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // Returns true iff the matcher matches x; also explains the match
115a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan  // result to 'listener', in the form of a non-restrictive relative
116a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan  // clause ("which ...", "whose ...", etc) that describes x.  For
117a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan  // example, the MatchAndExplain() method of the Pointee(...) matcher
118a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan  // should generate an explanation like "which points to ...".
119821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  //
120db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  // You should override this method when defining a new matcher.
121821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  //
122821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // It's the responsibility of the caller (Google Mock) to guarantee
123821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // that 'listener' is not NULL.  This helps to simplify a matcher's
124821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // implementation when it doesn't care about the performance, as it
125821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // can talk to 'listener' without checking its validity first.
126821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // However, in order to implement dummy listeners efficiently,
127821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // listener->stream() may be NULL.
128db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  virtual bool MatchAndExplain(T x, MatchResultListener* listener) const = 0;
129e35fdd936d133bf8a48de140a3c666897588a05shiqian
130a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan  // Describes this matcher to an ostream.  The function should print
131a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan  // a verb phrase that describes the property a value matching this
132a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan  // matcher should have.  The subject of the verb phrase is the value
133a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan  // being matched.  For example, the DescribeTo() method of the Gt(7)
134a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan  // matcher prints "is greater than 7".
135e35fdd936d133bf8a48de140a3c666897588a05shiqian  virtual void DescribeTo(::std::ostream* os) const = 0;
136e35fdd936d133bf8a48de140a3c666897588a05shiqian
137e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Describes the negation of this matcher to an ostream.  For
138e35fdd936d133bf8a48de140a3c666897588a05shiqian  // example, if the description of this matcher is "is greater than
139e35fdd936d133bf8a48de140a3c666897588a05shiqian  // 7", the negated description could be "is not greater than 7".
140e35fdd936d133bf8a48de140a3c666897588a05shiqian  // You are not required to override this when implementing
141e35fdd936d133bf8a48de140a3c666897588a05shiqian  // MatcherInterface, but it is highly advised so that your matcher
142e35fdd936d133bf8a48de140a3c666897588a05shiqian  // can produce good error messages.
143e35fdd936d133bf8a48de140a3c666897588a05shiqian  virtual void DescribeNegationTo(::std::ostream* os) const {
144e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "not (";
145e35fdd936d133bf8a48de140a3c666897588a05shiqian    DescribeTo(os);
146e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << ")";
147e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
148e35fdd936d133bf8a48de140a3c666897588a05shiqian};
149e35fdd936d133bf8a48de140a3c666897588a05shiqian
150e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace internal {
151e35fdd936d133bf8a48de140a3c666897588a05shiqian
152821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// A match result listener that ignores the explanation.
153821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wanclass DummyMatchResultListener : public MatchResultListener {
154821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan public:
155821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  DummyMatchResultListener() : MatchResultListener(NULL) {}
156821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
157821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan private:
158821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  GTEST_DISALLOW_COPY_AND_ASSIGN_(DummyMatchResultListener);
159821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan};
160821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
161821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// A match result listener that forwards the explanation to a given
162821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// ostream.  The difference between this and MatchResultListener is
163821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// that the former is concrete.
164821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wanclass StreamMatchResultListener : public MatchResultListener {
165821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan public:
166821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  explicit StreamMatchResultListener(::std::ostream* os)
167821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      : MatchResultListener(os) {}
168821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
169821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan private:
170821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  GTEST_DISALLOW_COPY_AND_ASSIGN_(StreamMatchResultListener);
171821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan};
172821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
173821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// A match result listener that stores the explanation in a string.
174821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wanclass StringMatchResultListener : public MatchResultListener {
175821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan public:
176821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  StringMatchResultListener() : MatchResultListener(&ss_) {}
177821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
178821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // Returns the explanation heard so far.
179821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  internal::string str() const { return ss_.str(); }
180821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
181821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan private:
182821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  ::std::stringstream ss_;
183821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
184821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  GTEST_DISALLOW_COPY_AND_ASSIGN_(StringMatchResultListener);
185821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan};
186821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
187e35fdd936d133bf8a48de140a3c666897588a05shiqian// An internal class for implementing Matcher<T>, which will derive
188e35fdd936d133bf8a48de140a3c666897588a05shiqian// from it.  We put functionalities common to all Matcher<T>
189e35fdd936d133bf8a48de140a3c666897588a05shiqian// specializations here to avoid code duplication.
190e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
191e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatcherBase {
192e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
193821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // Returns true iff the matcher matches x; also explains the match
194821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // result to 'listener'.
195821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  bool MatchAndExplain(T x, MatchResultListener* listener) const {
196821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    return impl_->MatchAndExplain(x, listener);
197821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  }
198821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
199e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Returns true iff this matcher matches x.
200821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  bool Matches(T x) const {
201821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    DummyMatchResultListener dummy;
202821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    return MatchAndExplain(x, &dummy);
203821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  }
204e35fdd936d133bf8a48de140a3c666897588a05shiqian
205e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Describes this matcher to an ostream.
206e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const { impl_->DescribeTo(os); }
207e35fdd936d133bf8a48de140a3c666897588a05shiqian
208e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Describes the negation of this matcher to an ostream.
209e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
210e35fdd936d133bf8a48de140a3c666897588a05shiqian    impl_->DescribeNegationTo(os);
211e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
212e35fdd936d133bf8a48de140a3c666897588a05shiqian
213e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Explains why x matches, or doesn't match, the matcher.
214e35fdd936d133bf8a48de140a3c666897588a05shiqian  void ExplainMatchResultTo(T x, ::std::ostream* os) const {
215821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    StreamMatchResultListener listener(os);
216821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    MatchAndExplain(x, &listener);
217e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
21832de5f53763125925e078498250f7e73a88de9edzhanyong.wan
219e35fdd936d133bf8a48de140a3c666897588a05shiqian protected:
220e35fdd936d133bf8a48de140a3c666897588a05shiqian  MatcherBase() {}
221e35fdd936d133bf8a48de140a3c666897588a05shiqian
222e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Constructs a matcher from its implementation.
223e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit MatcherBase(const MatcherInterface<T>* impl)
224e35fdd936d133bf8a48de140a3c666897588a05shiqian      : impl_(impl) {}
225e35fdd936d133bf8a48de140a3c666897588a05shiqian
226e35fdd936d133bf8a48de140a3c666897588a05shiqian  virtual ~MatcherBase() {}
22732de5f53763125925e078498250f7e73a88de9edzhanyong.wan
228e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
229e35fdd936d133bf8a48de140a3c666897588a05shiqian  // shared_ptr (util/gtl/shared_ptr.h) and linked_ptr have similar
230e35fdd936d133bf8a48de140a3c666897588a05shiqian  // interfaces.  The former dynamically allocates a chunk of memory
231e35fdd936d133bf8a48de140a3c666897588a05shiqian  // to hold the reference count, while the latter tracks all
232e35fdd936d133bf8a48de140a3c666897588a05shiqian  // references using a circular linked list without allocating
233e35fdd936d133bf8a48de140a3c666897588a05shiqian  // memory.  It has been observed that linked_ptr performs better in
234e35fdd936d133bf8a48de140a3c666897588a05shiqian  // typical scenarios.  However, shared_ptr can out-perform
235e35fdd936d133bf8a48de140a3c666897588a05shiqian  // linked_ptr when there are many more uses of the copy constructor
236e35fdd936d133bf8a48de140a3c666897588a05shiqian  // than the default constructor.
237e35fdd936d133bf8a48de140a3c666897588a05shiqian  //
238e35fdd936d133bf8a48de140a3c666897588a05shiqian  // If performance becomes a problem, we should see if using
239e35fdd936d133bf8a48de140a3c666897588a05shiqian  // shared_ptr helps.
240e35fdd936d133bf8a48de140a3c666897588a05shiqian  ::testing::internal::linked_ptr<const MatcherInterface<T> > impl_;
241e35fdd936d133bf8a48de140a3c666897588a05shiqian};
242e35fdd936d133bf8a48de140a3c666897588a05shiqian
243e35fdd936d133bf8a48de140a3c666897588a05shiqian}  // namespace internal
244e35fdd936d133bf8a48de140a3c666897588a05shiqian
245e35fdd936d133bf8a48de140a3c666897588a05shiqian// A Matcher<T> is a copyable and IMMUTABLE (except by assignment)
246e35fdd936d133bf8a48de140a3c666897588a05shiqian// object that can check whether a value of type T matches.  The
247e35fdd936d133bf8a48de140a3c666897588a05shiqian// implementation of Matcher<T> is just a linked_ptr to const
248e35fdd936d133bf8a48de140a3c666897588a05shiqian// MatcherInterface<T>, so copying is fairly cheap.  Don't inherit
249e35fdd936d133bf8a48de140a3c666897588a05shiqian// from Matcher!
250e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
251e35fdd936d133bf8a48de140a3c666897588a05shiqianclass Matcher : public internal::MatcherBase<T> {
252e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
25388032d8e0f6d4bc63b97aeeab84fdc44aeab1c89vladlosev  // Constructs a null matcher.  Needed for storing Matcher objects in STL
25488032d8e0f6d4bc63b97aeeab84fdc44aeab1c89vladlosev  // containers.  A default-constructed matcher is not yet initialized.  You
25588032d8e0f6d4bc63b97aeeab84fdc44aeab1c89vladlosev  // cannot use it until a valid value has been assigned to it.
256e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher() {}
257e35fdd936d133bf8a48de140a3c666897588a05shiqian
258e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Constructs a matcher from its implementation.
259e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit Matcher(const MatcherInterface<T>* impl)
260e35fdd936d133bf8a48de140a3c666897588a05shiqian      : internal::MatcherBase<T>(impl) {}
261e35fdd936d133bf8a48de140a3c666897588a05shiqian
26218490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  // Implicit constructor here allows people to write
263e35fdd936d133bf8a48de140a3c666897588a05shiqian  // EXPECT_CALL(foo, Bar(5)) instead of EXPECT_CALL(foo, Bar(Eq(5))) sometimes
264e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher(T value);  // NOLINT
265e35fdd936d133bf8a48de140a3c666897588a05shiqian};
266e35fdd936d133bf8a48de140a3c666897588a05shiqian
267e35fdd936d133bf8a48de140a3c666897588a05shiqian// The following two specializations allow the user to write str
268e35fdd936d133bf8a48de140a3c666897588a05shiqian// instead of Eq(str) and "foo" instead of Eq("foo") when a string
269e35fdd936d133bf8a48de140a3c666897588a05shiqian// matcher is expected.
270e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <>
271e35fdd936d133bf8a48de140a3c666897588a05shiqianclass Matcher<const internal::string&>
272e35fdd936d133bf8a48de140a3c666897588a05shiqian    : public internal::MatcherBase<const internal::string&> {
273e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
274e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher() {}
275e35fdd936d133bf8a48de140a3c666897588a05shiqian
276e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit Matcher(const MatcherInterface<const internal::string&>* impl)
277e35fdd936d133bf8a48de140a3c666897588a05shiqian      : internal::MatcherBase<const internal::string&>(impl) {}
278e35fdd936d133bf8a48de140a3c666897588a05shiqian
279e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Allows the user to write str instead of Eq(str) sometimes, where
280e35fdd936d133bf8a48de140a3c666897588a05shiqian  // str is a string object.
281e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher(const internal::string& s);  // NOLINT
282e35fdd936d133bf8a48de140a3c666897588a05shiqian
283e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Allows the user to write "foo" instead of Eq("foo") sometimes.
284e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher(const char* s);  // NOLINT
285e35fdd936d133bf8a48de140a3c666897588a05shiqian};
286e35fdd936d133bf8a48de140a3c666897588a05shiqian
287e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <>
288e35fdd936d133bf8a48de140a3c666897588a05shiqianclass Matcher<internal::string>
289e35fdd936d133bf8a48de140a3c666897588a05shiqian    : public internal::MatcherBase<internal::string> {
290e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
291e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher() {}
292e35fdd936d133bf8a48de140a3c666897588a05shiqian
293e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit Matcher(const MatcherInterface<internal::string>* impl)
294e35fdd936d133bf8a48de140a3c666897588a05shiqian      : internal::MatcherBase<internal::string>(impl) {}
295e35fdd936d133bf8a48de140a3c666897588a05shiqian
296e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Allows the user to write str instead of Eq(str) sometimes, where
297e35fdd936d133bf8a48de140a3c666897588a05shiqian  // str is a string object.
298e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher(const internal::string& s);  // NOLINT
299e35fdd936d133bf8a48de140a3c666897588a05shiqian
300e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Allows the user to write "foo" instead of Eq("foo") sometimes.
301e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher(const char* s);  // NOLINT
302e35fdd936d133bf8a48de140a3c666897588a05shiqian};
303e35fdd936d133bf8a48de140a3c666897588a05shiqian
304e35fdd936d133bf8a48de140a3c666897588a05shiqian// The PolymorphicMatcher class template makes it easy to implement a
305e35fdd936d133bf8a48de140a3c666897588a05shiqian// polymorphic matcher (i.e. a matcher that can match values of more
306e35fdd936d133bf8a48de140a3c666897588a05shiqian// than one type, e.g. Eq(n) and NotNull()).
307e35fdd936d133bf8a48de140a3c666897588a05shiqian//
308db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan// To define a polymorphic matcher, a user should provide an Impl
309db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan// class that has a DescribeTo() method and a DescribeNegationTo()
310db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan// method, and define a member function (or member function template)
311821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan//
312db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan//   bool MatchAndExplain(const Value& value,
313db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan//                        MatchResultListener* listener) const;
314821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan//
315821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// See the definition of NotNull() for a complete example.
316e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <class Impl>
317e35fdd936d133bf8a48de140a3c666897588a05shiqianclass PolymorphicMatcher {
318e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
31932de5f53763125925e078498250f7e73a88de9edzhanyong.wan  explicit PolymorphicMatcher(const Impl& an_impl) : impl_(an_impl) {}
320e35fdd936d133bf8a48de140a3c666897588a05shiqian
3212b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan  // Returns a mutable reference to the underlying matcher
3222b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan  // implementation object.
3232b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan  Impl& mutable_impl() { return impl_; }
3242b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan
3252b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan  // Returns an immutable reference to the underlying matcher
3262b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan  // implementation object.
3272b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan  const Impl& impl() const { return impl_; }
3282b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan
329e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
330e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<T>() const {
331e35fdd936d133bf8a48de140a3c666897588a05shiqian    return Matcher<T>(new MonomorphicImpl<T>(impl_));
332e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
33332de5f53763125925e078498250f7e73a88de9edzhanyong.wan
334e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
335e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
336e35fdd936d133bf8a48de140a3c666897588a05shiqian  class MonomorphicImpl : public MatcherInterface<T> {
337e35fdd936d133bf8a48de140a3c666897588a05shiqian   public:
338e35fdd936d133bf8a48de140a3c666897588a05shiqian    explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {}
339e35fdd936d133bf8a48de140a3c666897588a05shiqian
340e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeTo(::std::ostream* os) const {
341e35fdd936d133bf8a48de140a3c666897588a05shiqian      impl_.DescribeTo(os);
342e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
343e35fdd936d133bf8a48de140a3c666897588a05shiqian
344e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeNegationTo(::std::ostream* os) const {
345e35fdd936d133bf8a48de140a3c666897588a05shiqian      impl_.DescribeNegationTo(os);
346e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
347e35fdd936d133bf8a48de140a3c666897588a05shiqian
348821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
349db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan      return impl_.MatchAndExplain(x, listener);
350e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
3512b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan
352e35fdd936d133bf8a48de140a3c666897588a05shiqian   private:
353e35fdd936d133bf8a48de140a3c666897588a05shiqian    const Impl impl_;
35432de5f53763125925e078498250f7e73a88de9edzhanyong.wan
35532de5f53763125925e078498250f7e73a88de9edzhanyong.wan    GTEST_DISALLOW_ASSIGN_(MonomorphicImpl);
356e35fdd936d133bf8a48de140a3c666897588a05shiqian  };
357e35fdd936d133bf8a48de140a3c666897588a05shiqian
3582b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan  Impl impl_;
35932de5f53763125925e078498250f7e73a88de9edzhanyong.wan
36032de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(PolymorphicMatcher);
361e35fdd936d133bf8a48de140a3c666897588a05shiqian};
362e35fdd936d133bf8a48de140a3c666897588a05shiqian
363e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher from its implementation.  This is easier to use
364e35fdd936d133bf8a48de140a3c666897588a05shiqian// than the Matcher<T> constructor as it doesn't require you to
365e35fdd936d133bf8a48de140a3c666897588a05shiqian// explicitly write the template argument, e.g.
366e35fdd936d133bf8a48de140a3c666897588a05shiqian//
367e35fdd936d133bf8a48de140a3c666897588a05shiqian//   MakeMatcher(foo);
368e35fdd936d133bf8a48de140a3c666897588a05shiqian// vs
369e35fdd936d133bf8a48de140a3c666897588a05shiqian//   Matcher<const string&>(foo);
370e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
371e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<T> MakeMatcher(const MatcherInterface<T>* impl) {
372e35fdd936d133bf8a48de140a3c666897588a05shiqian  return Matcher<T>(impl);
373e35fdd936d133bf8a48de140a3c666897588a05shiqian};
374e35fdd936d133bf8a48de140a3c666897588a05shiqian
375e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher from its implementation.  This is
376e35fdd936d133bf8a48de140a3c666897588a05shiqian// easier to use than the PolymorphicMatcher<Impl> constructor as it
377e35fdd936d133bf8a48de140a3c666897588a05shiqian// doesn't require you to explicitly write the template argument, e.g.
378e35fdd936d133bf8a48de140a3c666897588a05shiqian//
379e35fdd936d133bf8a48de140a3c666897588a05shiqian//   MakePolymorphicMatcher(foo);
380e35fdd936d133bf8a48de140a3c666897588a05shiqian// vs
381e35fdd936d133bf8a48de140a3c666897588a05shiqian//   PolymorphicMatcher<TypeOfFoo>(foo);
382e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <class Impl>
383e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<Impl> MakePolymorphicMatcher(const Impl& impl) {
384e35fdd936d133bf8a48de140a3c666897588a05shiqian  return PolymorphicMatcher<Impl>(impl);
385e35fdd936d133bf8a48de140a3c666897588a05shiqian}
386e35fdd936d133bf8a48de140a3c666897588a05shiqian
387e35fdd936d133bf8a48de140a3c666897588a05shiqian// In order to be safe and clear, casting between different matcher
388e35fdd936d133bf8a48de140a3c666897588a05shiqian// types is done explicitly via MatcherCast<T>(m), which takes a
389e35fdd936d133bf8a48de140a3c666897588a05shiqian// matcher m and returns a Matcher<T>.  It compiles only when T can be
390e35fdd936d133bf8a48de140a3c666897588a05shiqian// statically converted to the argument type of m.
391e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T, typename M>
392e35fdd936d133bf8a48de140a3c666897588a05shiqianMatcher<T> MatcherCast(M m);
393e35fdd936d133bf8a48de140a3c666897588a05shiqian
39418490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// Implements SafeMatcherCast().
39518490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan//
39695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// We use an intermediate class to do the actual safe casting as Nokia's
39795b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// Symbian compiler cannot decide between
39895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// template <T, M> ... (M) and
39995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// template <T, U> ... (const Matcher<U>&)
40095b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// for function templates but can for member function templates.
40195b12332c345cee508a8e2b68e007280392506e0zhanyong.wantemplate <typename T>
40295b12332c345cee508a8e2b68e007280392506e0zhanyong.wanclass SafeMatcherCastImpl {
40395b12332c345cee508a8e2b68e007280392506e0zhanyong.wan public:
40495b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  // This overload handles polymorphic matchers only since monomorphic
40595b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  // matchers are handled by the next one.
40695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  template <typename M>
40795b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  static inline Matcher<T> Cast(M polymorphic_matcher) {
40895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan    return Matcher<T>(polymorphic_matcher);
40995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  }
41018490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan
41195b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  // This overload handles monomorphic matchers.
41295b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  //
41395b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  // In general, if type T can be implicitly converted to type U, we can
41495b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  // safely convert a Matcher<U> to a Matcher<T> (i.e. Matcher is
41595b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  // contravariant): just keep a copy of the original Matcher<U>, convert the
41695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  // argument from type T to U, and then pass it to the underlying Matcher<U>.
41795b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  // The only exception is when U is a reference and T is not, as the
41895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  // underlying Matcher<U> may be interested in the argument's address, which
41995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  // is not preserved in the conversion from T to U.
42095b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  template <typename U>
42195b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  static inline Matcher<T> Cast(const Matcher<U>& matcher) {
42295b12332c345cee508a8e2b68e007280392506e0zhanyong.wan    // Enforce that T can be implicitly converted to U.
42302f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan    GTEST_COMPILE_ASSERT_((internal::ImplicitlyConvertible<T, U>::value),
42495b12332c345cee508a8e2b68e007280392506e0zhanyong.wan                          T_must_be_implicitly_convertible_to_U);
42595b12332c345cee508a8e2b68e007280392506e0zhanyong.wan    // Enforce that we are not converting a non-reference type T to a reference
42695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan    // type U.
42702f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan    GTEST_COMPILE_ASSERT_(
42895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan        internal::is_reference<T>::value || !internal::is_reference<U>::value,
42995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan        cannot_convert_non_referentce_arg_to_reference);
43095b12332c345cee508a8e2b68e007280392506e0zhanyong.wan    // In case both T and U are arithmetic types, enforce that the
43195b12332c345cee508a8e2b68e007280392506e0zhanyong.wan    // conversion is not lossy.
432ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    typedef GTEST_REMOVE_REFERENCE_AND_CONST_(T) RawT;
433ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    typedef GTEST_REMOVE_REFERENCE_AND_CONST_(U) RawU;
43495b12332c345cee508a8e2b68e007280392506e0zhanyong.wan    const bool kTIsOther = GMOCK_KIND_OF_(RawT) == internal::kOther;
43595b12332c345cee508a8e2b68e007280392506e0zhanyong.wan    const bool kUIsOther = GMOCK_KIND_OF_(RawU) == internal::kOther;
43602f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan    GTEST_COMPILE_ASSERT_(
43795b12332c345cee508a8e2b68e007280392506e0zhanyong.wan        kTIsOther || kUIsOther ||
43895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan        (internal::LosslessArithmeticConvertible<RawT, RawU>::value),
43995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan        conversion_of_arithmetic_types_must_be_lossless);
44095b12332c345cee508a8e2b68e007280392506e0zhanyong.wan    return MatcherCast<T>(matcher);
44195b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  }
44295b12332c345cee508a8e2b68e007280392506e0zhanyong.wan};
44395b12332c345cee508a8e2b68e007280392506e0zhanyong.wan
44495b12332c345cee508a8e2b68e007280392506e0zhanyong.wantemplate <typename T, typename M>
44595b12332c345cee508a8e2b68e007280392506e0zhanyong.waninline Matcher<T> SafeMatcherCast(const M& polymorphic_matcher) {
44695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  return SafeMatcherCastImpl<T>::Cast(polymorphic_matcher);
44718490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan}
44818490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan
449e35fdd936d133bf8a48de140a3c666897588a05shiqian// A<T>() returns a matcher that matches any value of type T.
450e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
451e35fdd936d133bf8a48de140a3c666897588a05shiqianMatcher<T> A();
452e35fdd936d133bf8a48de140a3c666897588a05shiqian
453e35fdd936d133bf8a48de140a3c666897588a05shiqian// Anything inside the 'internal' namespace IS INTERNAL IMPLEMENTATION
454e35fdd936d133bf8a48de140a3c666897588a05shiqian// and MUST NOT BE USED IN USER CODE!!!
455e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace internal {
456e35fdd936d133bf8a48de140a3c666897588a05shiqian
457b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan// If the explanation is not empty, prints it to the ostream.
458b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.waninline void PrintIfNotEmpty(const internal::string& explanation,
459b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan                            std::ostream* os) {
460b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan  if (explanation != "" && os != NULL) {
461b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    *os << ", " << explanation;
462676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan  }
463676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan}
464676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan
465736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan// Returns true if the given type name is easy to read by a human.
466736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan// This is used to decide whether printing the type of a value might
467736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan// be helpful.
468736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.waninline bool IsReadableTypeName(const string& type_name) {
469736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan  // We consider a type name readable if it's short or doesn't contain
470736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan  // a template or function type.
471736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan  return (type_name.length() <= 20 ||
472736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan          type_name.find_first_of("<(") == string::npos);
473736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan}
474736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan
475676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan// Matches the value against the given matcher, prints the value and explains
476676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan// the match result to the listener. Returns the match result.
477676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan// 'listener' must not be NULL.
478676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan// Value cannot be passed by const reference, because some matchers take a
479676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan// non-const argument.
480676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wantemplate <typename Value, typename T>
481676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wanbool MatchPrintAndExplain(Value& value, const Matcher<T>& matcher,
482676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan                          MatchResultListener* listener) {
483676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan  if (!listener->IsInterested()) {
484676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    // If the listener is not interested, we do not need to construct the
485676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    // inner explanation.
486676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    return matcher.Matches(value);
487676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan  }
488676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan
489676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan  StringMatchResultListener inner_listener;
490676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan  const bool match = matcher.MatchAndExplain(value, &inner_listener);
491676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan
492676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan  UniversalPrint(value, listener->stream());
493736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan#if GTEST_HAS_RTTI
494736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan  const string& type_name = GetTypeName<Value>();
495736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan  if (IsReadableTypeName(type_name))
496736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan    *listener->stream() << " (of type " << type_name << ")";
497736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan#endif
498b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan  PrintIfNotEmpty(inner_listener.str(), listener->stream());
499676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan
500676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan  return match;
501676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan}
502676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan
503e35fdd936d133bf8a48de140a3c666897588a05shiqian// An internal helper class for doing compile-time loop on a tuple's
504e35fdd936d133bf8a48de140a3c666897588a05shiqian// fields.
505e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <size_t N>
506e35fdd936d133bf8a48de140a3c666897588a05shiqianclass TuplePrefix {
507e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
508e35fdd936d133bf8a48de140a3c666897588a05shiqian  // TuplePrefix<N>::Matches(matcher_tuple, value_tuple) returns true
509e35fdd936d133bf8a48de140a3c666897588a05shiqian  // iff the first N fields of matcher_tuple matches the first N
510e35fdd936d133bf8a48de140a3c666897588a05shiqian  // fields of value_tuple, respectively.
511e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename MatcherTuple, typename ValueTuple>
512e35fdd936d133bf8a48de140a3c666897588a05shiqian  static bool Matches(const MatcherTuple& matcher_tuple,
513e35fdd936d133bf8a48de140a3c666897588a05shiqian                      const ValueTuple& value_tuple) {
514e35fdd936d133bf8a48de140a3c666897588a05shiqian    using ::std::tr1::get;
515e35fdd936d133bf8a48de140a3c666897588a05shiqian    return TuplePrefix<N - 1>::Matches(matcher_tuple, value_tuple)
516e35fdd936d133bf8a48de140a3c666897588a05shiqian        && get<N - 1>(matcher_tuple).Matches(get<N - 1>(value_tuple));
517e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
518e35fdd936d133bf8a48de140a3c666897588a05shiqian
519b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan  // TuplePrefix<N>::ExplainMatchFailuresTo(matchers, values, os)
520e35fdd936d133bf8a48de140a3c666897588a05shiqian  // describes failures in matching the first N fields of matchers
521e35fdd936d133bf8a48de140a3c666897588a05shiqian  // against the first N fields of values.  If there is no failure,
522e35fdd936d133bf8a48de140a3c666897588a05shiqian  // nothing will be streamed to os.
523e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename MatcherTuple, typename ValueTuple>
524b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan  static void ExplainMatchFailuresTo(const MatcherTuple& matchers,
525b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan                                     const ValueTuple& values,
526b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan                                     ::std::ostream* os) {
527e35fdd936d133bf8a48de140a3c666897588a05shiqian    using ::std::tr1::tuple_element;
528e35fdd936d133bf8a48de140a3c666897588a05shiqian    using ::std::tr1::get;
529e35fdd936d133bf8a48de140a3c666897588a05shiqian
530e35fdd936d133bf8a48de140a3c666897588a05shiqian    // First, describes failures in the first N - 1 fields.
531b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    TuplePrefix<N - 1>::ExplainMatchFailuresTo(matchers, values, os);
532e35fdd936d133bf8a48de140a3c666897588a05shiqian
533e35fdd936d133bf8a48de140a3c666897588a05shiqian    // Then describes the failure (if any) in the (N - 1)-th (0-based)
534e35fdd936d133bf8a48de140a3c666897588a05shiqian    // field.
535e35fdd936d133bf8a48de140a3c666897588a05shiqian    typename tuple_element<N - 1, MatcherTuple>::type matcher =
536e35fdd936d133bf8a48de140a3c666897588a05shiqian        get<N - 1>(matchers);
537e35fdd936d133bf8a48de140a3c666897588a05shiqian    typedef typename tuple_element<N - 1, ValueTuple>::type Value;
538e35fdd936d133bf8a48de140a3c666897588a05shiqian    Value value = get<N - 1>(values);
539821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    StringMatchResultListener listener;
540821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (!matcher.MatchAndExplain(value, &listener)) {
541e35fdd936d133bf8a48de140a3c666897588a05shiqian      // TODO(wan): include in the message the name of the parameter
542e35fdd936d133bf8a48de140a3c666897588a05shiqian      // as used in MOCK_METHOD*() when possible.
543e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "  Expected arg #" << N - 1 << ": ";
544e35fdd936d133bf8a48de140a3c666897588a05shiqian      get<N - 1>(matchers).DescribeTo(os);
545e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "\n           Actual: ";
546e35fdd936d133bf8a48de140a3c666897588a05shiqian      // We remove the reference in type Value to prevent the
547e35fdd936d133bf8a48de140a3c666897588a05shiqian      // universal printer from printing the address of value, which
548e35fdd936d133bf8a48de140a3c666897588a05shiqian      // isn't interesting to the user most of the time.  The
549db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan      // matcher's MatchAndExplain() method handles the case when
550e35fdd936d133bf8a48de140a3c666897588a05shiqian      // the address is interesting.
551b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan      internal::UniversalPrint(value, os);
552b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan      PrintIfNotEmpty(listener.str(), os);
553e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "\n";
554e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
555e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
556e35fdd936d133bf8a48de140a3c666897588a05shiqian};
557e35fdd936d133bf8a48de140a3c666897588a05shiqian
558e35fdd936d133bf8a48de140a3c666897588a05shiqian// The base case.
559e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <>
560e35fdd936d133bf8a48de140a3c666897588a05shiqianclass TuplePrefix<0> {
561e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
562e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename MatcherTuple, typename ValueTuple>
5633fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.wan  static bool Matches(const MatcherTuple& /* matcher_tuple */,
5643fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.wan                      const ValueTuple& /* value_tuple */) {
565e35fdd936d133bf8a48de140a3c666897588a05shiqian    return true;
566e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
567e35fdd936d133bf8a48de140a3c666897588a05shiqian
568e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename MatcherTuple, typename ValueTuple>
569b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan  static void ExplainMatchFailuresTo(const MatcherTuple& /* matchers */,
570b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan                                     const ValueTuple& /* values */,
571b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan                                     ::std::ostream* /* os */) {}
572e35fdd936d133bf8a48de140a3c666897588a05shiqian};
573e35fdd936d133bf8a48de140a3c666897588a05shiqian
574e35fdd936d133bf8a48de140a3c666897588a05shiqian// TupleMatches(matcher_tuple, value_tuple) returns true iff all
575e35fdd936d133bf8a48de140a3c666897588a05shiqian// matchers in matcher_tuple match the corresponding fields in
576e35fdd936d133bf8a48de140a3c666897588a05shiqian// value_tuple.  It is a compiler error if matcher_tuple and
577e35fdd936d133bf8a48de140a3c666897588a05shiqian// value_tuple have different number of fields or incompatible field
578e35fdd936d133bf8a48de140a3c666897588a05shiqian// types.
579e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename MatcherTuple, typename ValueTuple>
580e35fdd936d133bf8a48de140a3c666897588a05shiqianbool TupleMatches(const MatcherTuple& matcher_tuple,
581e35fdd936d133bf8a48de140a3c666897588a05shiqian                  const ValueTuple& value_tuple) {
582e35fdd936d133bf8a48de140a3c666897588a05shiqian  using ::std::tr1::tuple_size;
583e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Makes sure that matcher_tuple and value_tuple have the same
584e35fdd936d133bf8a48de140a3c666897588a05shiqian  // number of fields.
58502f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan  GTEST_COMPILE_ASSERT_(tuple_size<MatcherTuple>::value ==
586e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan                        tuple_size<ValueTuple>::value,
587e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan                        matcher_and_value_have_different_numbers_of_fields);
588e35fdd936d133bf8a48de140a3c666897588a05shiqian  return TuplePrefix<tuple_size<ValueTuple>::value>::
589e35fdd936d133bf8a48de140a3c666897588a05shiqian      Matches(matcher_tuple, value_tuple);
590e35fdd936d133bf8a48de140a3c666897588a05shiqian}
591e35fdd936d133bf8a48de140a3c666897588a05shiqian
592e35fdd936d133bf8a48de140a3c666897588a05shiqian// Describes failures in matching matchers against values.  If there
593e35fdd936d133bf8a48de140a3c666897588a05shiqian// is no failure, nothing will be streamed to os.
594e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename MatcherTuple, typename ValueTuple>
595b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wanvoid ExplainMatchFailureTupleTo(const MatcherTuple& matchers,
596b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan                                const ValueTuple& values,
597b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan                                ::std::ostream* os) {
598e35fdd936d133bf8a48de140a3c666897588a05shiqian  using ::std::tr1::tuple_size;
599b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan  TuplePrefix<tuple_size<MatcherTuple>::value>::ExplainMatchFailuresTo(
600e35fdd936d133bf8a48de140a3c666897588a05shiqian      matchers, values, os);
601e35fdd936d133bf8a48de140a3c666897588a05shiqian}
602e35fdd936d133bf8a48de140a3c666897588a05shiqian
603e35fdd936d133bf8a48de140a3c666897588a05shiqian// The MatcherCastImpl class template is a helper for implementing
604e35fdd936d133bf8a48de140a3c666897588a05shiqian// MatcherCast().  We need this helper in order to partially
605e35fdd936d133bf8a48de140a3c666897588a05shiqian// specialize the implementation of MatcherCast() (C++ allows
606e35fdd936d133bf8a48de140a3c666897588a05shiqian// class/struct templates to be partially specialized, but not
607e35fdd936d133bf8a48de140a3c666897588a05shiqian// function templates.).
608e35fdd936d133bf8a48de140a3c666897588a05shiqian
609e35fdd936d133bf8a48de140a3c666897588a05shiqian// This general version is used when MatcherCast()'s argument is a
610e35fdd936d133bf8a48de140a3c666897588a05shiqian// polymorphic matcher (i.e. something that can be converted to a
611e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matcher but is not one yet; for example, Eq(value)).
612e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T, typename M>
613e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatcherCastImpl {
614e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
615e35fdd936d133bf8a48de140a3c666897588a05shiqian  static Matcher<T> Cast(M polymorphic_matcher) {
616e35fdd936d133bf8a48de140a3c666897588a05shiqian    return Matcher<T>(polymorphic_matcher);
617e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
618e35fdd936d133bf8a48de140a3c666897588a05shiqian};
619e35fdd936d133bf8a48de140a3c666897588a05shiqian
620e35fdd936d133bf8a48de140a3c666897588a05shiqian// This more specialized version is used when MatcherCast()'s argument
621e35fdd936d133bf8a48de140a3c666897588a05shiqian// is already a Matcher.  This only compiles when type T can be
622e35fdd936d133bf8a48de140a3c666897588a05shiqian// statically converted to type U.
623e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T, typename U>
624e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatcherCastImpl<T, Matcher<U> > {
625e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
626e35fdd936d133bf8a48de140a3c666897588a05shiqian  static Matcher<T> Cast(const Matcher<U>& source_matcher) {
627e35fdd936d133bf8a48de140a3c666897588a05shiqian    return Matcher<T>(new Impl(source_matcher));
628e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
62932de5f53763125925e078498250f7e73a88de9edzhanyong.wan
630e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
631e35fdd936d133bf8a48de140a3c666897588a05shiqian  class Impl : public MatcherInterface<T> {
632e35fdd936d133bf8a48de140a3c666897588a05shiqian   public:
633e35fdd936d133bf8a48de140a3c666897588a05shiqian    explicit Impl(const Matcher<U>& source_matcher)
634e35fdd936d133bf8a48de140a3c666897588a05shiqian        : source_matcher_(source_matcher) {}
635e35fdd936d133bf8a48de140a3c666897588a05shiqian
636e35fdd936d133bf8a48de140a3c666897588a05shiqian    // We delegate the matching logic to the source matcher.
637821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
638821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      return source_matcher_.MatchAndExplain(static_cast<U>(x), listener);
639e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
640e35fdd936d133bf8a48de140a3c666897588a05shiqian
641e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeTo(::std::ostream* os) const {
642e35fdd936d133bf8a48de140a3c666897588a05shiqian      source_matcher_.DescribeTo(os);
643e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
644e35fdd936d133bf8a48de140a3c666897588a05shiqian
645e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeNegationTo(::std::ostream* os) const {
646e35fdd936d133bf8a48de140a3c666897588a05shiqian      source_matcher_.DescribeNegationTo(os);
647e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
648e35fdd936d133bf8a48de140a3c666897588a05shiqian
649e35fdd936d133bf8a48de140a3c666897588a05shiqian   private:
650e35fdd936d133bf8a48de140a3c666897588a05shiqian    const Matcher<U> source_matcher_;
65132de5f53763125925e078498250f7e73a88de9edzhanyong.wan
65232de5f53763125925e078498250f7e73a88de9edzhanyong.wan    GTEST_DISALLOW_ASSIGN_(Impl);
653e35fdd936d133bf8a48de140a3c666897588a05shiqian  };
654e35fdd936d133bf8a48de140a3c666897588a05shiqian};
655e35fdd936d133bf8a48de140a3c666897588a05shiqian
656e35fdd936d133bf8a48de140a3c666897588a05shiqian// This even more specialized version is used for efficiently casting
657e35fdd936d133bf8a48de140a3c666897588a05shiqian// a matcher to its own type.
658e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
659e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatcherCastImpl<T, Matcher<T> > {
660e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
661e35fdd936d133bf8a48de140a3c666897588a05shiqian  static Matcher<T> Cast(const Matcher<T>& matcher) { return matcher; }
662e35fdd936d133bf8a48de140a3c666897588a05shiqian};
663e35fdd936d133bf8a48de140a3c666897588a05shiqian
664e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements A<T>().
665e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
666e35fdd936d133bf8a48de140a3c666897588a05shiqianclass AnyMatcherImpl : public MatcherInterface<T> {
667e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
668821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  virtual bool MatchAndExplain(
669821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      T /* x */, MatchResultListener* /* listener */) const { return true; }
670e35fdd936d133bf8a48de140a3c666897588a05shiqian  virtual void DescribeTo(::std::ostream* os) const { *os << "is anything"; }
671e35fdd936d133bf8a48de140a3c666897588a05shiqian  virtual void DescribeNegationTo(::std::ostream* os) const {
672e35fdd936d133bf8a48de140a3c666897588a05shiqian    // This is mostly for completeness' safe, as it's not very useful
673e35fdd936d133bf8a48de140a3c666897588a05shiqian    // to write Not(A<bool>()).  However we cannot completely rule out
674e35fdd936d133bf8a48de140a3c666897588a05shiqian    // such a possibility, and it doesn't hurt to be prepared.
675e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "never matches";
676e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
677e35fdd936d133bf8a48de140a3c666897588a05shiqian};
678e35fdd936d133bf8a48de140a3c666897588a05shiqian
679e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements _, a matcher that matches any value of any
680e35fdd936d133bf8a48de140a3c666897588a05shiqian// type.  This is a polymorphic matcher, so we need a template type
681e35fdd936d133bf8a48de140a3c666897588a05shiqian// conversion operator to make it appearing as a Matcher<T> for any
682e35fdd936d133bf8a48de140a3c666897588a05shiqian// type T.
683e35fdd936d133bf8a48de140a3c666897588a05shiqianclass AnythingMatcher {
684e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
685e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
686e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<T>() const { return A<T>(); }
687e35fdd936d133bf8a48de140a3c666897588a05shiqian};
688e35fdd936d133bf8a48de140a3c666897588a05shiqian
689e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements a matcher that compares a given value with a
690e35fdd936d133bf8a48de140a3c666897588a05shiqian// pre-supplied value using one of the ==, <=, <, etc, operators.  The
691e35fdd936d133bf8a48de140a3c666897588a05shiqian// two values being compared don't have to have the same type.
692e35fdd936d133bf8a48de140a3c666897588a05shiqian//
693e35fdd936d133bf8a48de140a3c666897588a05shiqian// The matcher defined here is polymorphic (for example, Eq(5) can be
694e35fdd936d133bf8a48de140a3c666897588a05shiqian// used to match an int, a short, a double, etc).  Therefore we use
695e35fdd936d133bf8a48de140a3c666897588a05shiqian// a template type conversion operator in the implementation.
696e35fdd936d133bf8a48de140a3c666897588a05shiqian//
697e35fdd936d133bf8a48de140a3c666897588a05shiqian// We define this as a macro in order to eliminate duplicated source
698e35fdd936d133bf8a48de140a3c666897588a05shiqian// code.
699e35fdd936d133bf8a48de140a3c666897588a05shiqian//
700e35fdd936d133bf8a48de140a3c666897588a05shiqian// The following template definition assumes that the Rhs parameter is
701e35fdd936d133bf8a48de140a3c666897588a05shiqian// a "bare" type (i.e. neither 'const T' nor 'T&').
702b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan#define GMOCK_IMPLEMENT_COMPARISON_MATCHER_( \
703b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    name, op, relation, negated_relation) \
704e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename Rhs> class name##Matcher { \
705e35fdd936d133bf8a48de140a3c666897588a05shiqian   public: \
706e35fdd936d133bf8a48de140a3c666897588a05shiqian    explicit name##Matcher(const Rhs& rhs) : rhs_(rhs) {} \
707e35fdd936d133bf8a48de140a3c666897588a05shiqian    template <typename Lhs> \
708e35fdd936d133bf8a48de140a3c666897588a05shiqian    operator Matcher<Lhs>() const { \
709e35fdd936d133bf8a48de140a3c666897588a05shiqian      return MakeMatcher(new Impl<Lhs>(rhs_)); \
710e35fdd936d133bf8a48de140a3c666897588a05shiqian    } \
711e35fdd936d133bf8a48de140a3c666897588a05shiqian   private: \
712e35fdd936d133bf8a48de140a3c666897588a05shiqian    template <typename Lhs> \
713e35fdd936d133bf8a48de140a3c666897588a05shiqian    class Impl : public MatcherInterface<Lhs> { \
714e35fdd936d133bf8a48de140a3c666897588a05shiqian     public: \
715e35fdd936d133bf8a48de140a3c666897588a05shiqian      explicit Impl(const Rhs& rhs) : rhs_(rhs) {} \
716821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      virtual bool MatchAndExplain(\
717821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan          Lhs lhs, MatchResultListener* /* listener */) const { \
718821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan        return lhs op rhs_; \
719821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      } \
720e35fdd936d133bf8a48de140a3c666897588a05shiqian      virtual void DescribeTo(::std::ostream* os) const { \
721b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan        *os << relation  " "; \
722e2e8ba401d198d1a8304c652e997505609b62696vladlosev        UniversalPrint(rhs_, os); \
723e35fdd936d133bf8a48de140a3c666897588a05shiqian      } \
724e35fdd936d133bf8a48de140a3c666897588a05shiqian      virtual void DescribeNegationTo(::std::ostream* os) const { \
725b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan        *os << negated_relation  " "; \
726e2e8ba401d198d1a8304c652e997505609b62696vladlosev        UniversalPrint(rhs_, os); \
727e35fdd936d133bf8a48de140a3c666897588a05shiqian      } \
728e35fdd936d133bf8a48de140a3c666897588a05shiqian     private: \
729e35fdd936d133bf8a48de140a3c666897588a05shiqian      Rhs rhs_; \
73032de5f53763125925e078498250f7e73a88de9edzhanyong.wan      GTEST_DISALLOW_ASSIGN_(Impl); \
731e35fdd936d133bf8a48de140a3c666897588a05shiqian    }; \
732e35fdd936d133bf8a48de140a3c666897588a05shiqian    Rhs rhs_; \
73332de5f53763125925e078498250f7e73a88de9edzhanyong.wan    GTEST_DISALLOW_ASSIGN_(name##Matcher); \
734e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
735e35fdd936d133bf8a48de140a3c666897588a05shiqian
736e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements Eq(v), Ge(v), Gt(v), Le(v), Lt(v), and Ne(v)
737e35fdd936d133bf8a48de140a3c666897588a05shiqian// respectively.
738b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Eq, ==, "is equal to", "isn't equal to");
739b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Ge, >=, "is >=", "isn't >=");
740b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Gt, >, "is >", "isn't >");
741b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Le, <=, "is <=", "isn't <=");
742b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Lt, <, "is <", "isn't <");
743b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Ne, !=, "isn't equal to", "is equal to");
744e35fdd936d133bf8a48de140a3c666897588a05shiqian
745e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan#undef GMOCK_IMPLEMENT_COMPARISON_MATCHER_
746e35fdd936d133bf8a48de140a3c666897588a05shiqian
74779b83505bcf73bf2903ebf2e2f82cb1e1f181816vladlosev// Implements the polymorphic IsNull() matcher, which matches any raw or smart
7482d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan// pointer that is NULL.
7492d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wanclass IsNullMatcher {
7502d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan public:
75179b83505bcf73bf2903ebf2e2f82cb1e1f181816vladlosev  template <typename Pointer>
752db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplain(const Pointer& p,
753db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                       MatchResultListener* /* listener */) const {
754db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return GetRawPointer(p) == NULL;
755db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  }
7562d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan
7572d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan  void DescribeTo(::std::ostream* os) const { *os << "is NULL"; }
7582d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan  void DescribeNegationTo(::std::ostream* os) const {
759b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    *os << "isn't NULL";
7602d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan  }
7612d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan};
7622d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan
76379b83505bcf73bf2903ebf2e2f82cb1e1f181816vladlosev// Implements the polymorphic NotNull() matcher, which matches any raw or smart
764e35fdd936d133bf8a48de140a3c666897588a05shiqian// pointer that is not NULL.
765e35fdd936d133bf8a48de140a3c666897588a05shiqianclass NotNullMatcher {
766e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
76779b83505bcf73bf2903ebf2e2f82cb1e1f181816vladlosev  template <typename Pointer>
768db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplain(const Pointer& p,
769db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                       MatchResultListener* /* listener */) const {
770db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return GetRawPointer(p) != NULL;
771db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  }
772e35fdd936d133bf8a48de140a3c666897588a05shiqian
773b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan  void DescribeTo(::std::ostream* os) const { *os << "isn't NULL"; }
774e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
775e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "is NULL";
776e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
777e35fdd936d133bf8a48de140a3c666897588a05shiqian};
778e35fdd936d133bf8a48de140a3c666897588a05shiqian
779e35fdd936d133bf8a48de140a3c666897588a05shiqian// Ref(variable) matches any argument that is a reference to
780e35fdd936d133bf8a48de140a3c666897588a05shiqian// 'variable'.  This matcher is polymorphic as it can match any
781e35fdd936d133bf8a48de140a3c666897588a05shiqian// super type of the type of 'variable'.
782e35fdd936d133bf8a48de140a3c666897588a05shiqian//
783e35fdd936d133bf8a48de140a3c666897588a05shiqian// The RefMatcher template class implements Ref(variable).  It can
784e35fdd936d133bf8a48de140a3c666897588a05shiqian// only be instantiated with a reference type.  This prevents a user
785e35fdd936d133bf8a48de140a3c666897588a05shiqian// from mistakenly using Ref(x) to match a non-reference function
786e35fdd936d133bf8a48de140a3c666897588a05shiqian// argument.  For example, the following will righteously cause a
787e35fdd936d133bf8a48de140a3c666897588a05shiqian// compiler error:
788e35fdd936d133bf8a48de140a3c666897588a05shiqian//
789e35fdd936d133bf8a48de140a3c666897588a05shiqian//   int n;
790e35fdd936d133bf8a48de140a3c666897588a05shiqian//   Matcher<int> m1 = Ref(n);   // This won't compile.
791e35fdd936d133bf8a48de140a3c666897588a05shiqian//   Matcher<int&> m2 = Ref(n);  // This will compile.
792e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
793e35fdd936d133bf8a48de140a3c666897588a05shiqianclass RefMatcher;
794e35fdd936d133bf8a48de140a3c666897588a05shiqian
795e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
796e35fdd936d133bf8a48de140a3c666897588a05shiqianclass RefMatcher<T&> {
797e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Google Mock is a generic framework and thus needs to support
798e35fdd936d133bf8a48de140a3c666897588a05shiqian  // mocking any function types, including those that take non-const
799e35fdd936d133bf8a48de140a3c666897588a05shiqian  // reference arguments.  Therefore the template parameter T (and
800e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Super below) can be instantiated to either a const type or a
801e35fdd936d133bf8a48de140a3c666897588a05shiqian  // non-const type.
802e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
803e35fdd936d133bf8a48de140a3c666897588a05shiqian  // RefMatcher() takes a T& instead of const T&, as we want the
804e35fdd936d133bf8a48de140a3c666897588a05shiqian  // compiler to catch using Ref(const_value) as a matcher for a
805e35fdd936d133bf8a48de140a3c666897588a05shiqian  // non-const reference.
806e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit RefMatcher(T& x) : object_(x) {}  // NOLINT
807e35fdd936d133bf8a48de140a3c666897588a05shiqian
808e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename Super>
809e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<Super&>() const {
810e35fdd936d133bf8a48de140a3c666897588a05shiqian    // By passing object_ (type T&) to Impl(), which expects a Super&,
811e35fdd936d133bf8a48de140a3c666897588a05shiqian    // we make sure that Super is a super type of T.  In particular,
812e35fdd936d133bf8a48de140a3c666897588a05shiqian    // this catches using Ref(const_value) as a matcher for a
813e35fdd936d133bf8a48de140a3c666897588a05shiqian    // non-const reference, as you cannot implicitly convert a const
814e35fdd936d133bf8a48de140a3c666897588a05shiqian    // reference to a non-const reference.
815e35fdd936d133bf8a48de140a3c666897588a05shiqian    return MakeMatcher(new Impl<Super>(object_));
816e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
81732de5f53763125925e078498250f7e73a88de9edzhanyong.wan
818e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
819e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename Super>
820e35fdd936d133bf8a48de140a3c666897588a05shiqian  class Impl : public MatcherInterface<Super&> {
821e35fdd936d133bf8a48de140a3c666897588a05shiqian   public:
822e35fdd936d133bf8a48de140a3c666897588a05shiqian    explicit Impl(Super& x) : object_(x) {}  // NOLINT
823e35fdd936d133bf8a48de140a3c666897588a05shiqian
824db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    // MatchAndExplain() takes a Super& (as opposed to const Super&)
825db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    // in order to match the interface MatcherInterface<Super&>.
826821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    virtual bool MatchAndExplain(
827821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan        Super& x, MatchResultListener* listener) const {
828b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan      *listener << "which is located @" << static_cast<const void*>(&x);
829821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      return &x == &object_;
830821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    }
831e35fdd936d133bf8a48de140a3c666897588a05shiqian
832e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeTo(::std::ostream* os) const {
833e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "references the variable ";
834e35fdd936d133bf8a48de140a3c666897588a05shiqian      UniversalPrinter<Super&>::Print(object_, os);
835e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
836e35fdd936d133bf8a48de140a3c666897588a05shiqian
837e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeNegationTo(::std::ostream* os) const {
838e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "does not reference the variable ";
839e35fdd936d133bf8a48de140a3c666897588a05shiqian      UniversalPrinter<Super&>::Print(object_, os);
840e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
841e35fdd936d133bf8a48de140a3c666897588a05shiqian
842e35fdd936d133bf8a48de140a3c666897588a05shiqian   private:
843e35fdd936d133bf8a48de140a3c666897588a05shiqian    const Super& object_;
84432de5f53763125925e078498250f7e73a88de9edzhanyong.wan
84532de5f53763125925e078498250f7e73a88de9edzhanyong.wan    GTEST_DISALLOW_ASSIGN_(Impl);
846e35fdd936d133bf8a48de140a3c666897588a05shiqian  };
847e35fdd936d133bf8a48de140a3c666897588a05shiqian
848e35fdd936d133bf8a48de140a3c666897588a05shiqian  T& object_;
84932de5f53763125925e078498250f7e73a88de9edzhanyong.wan
85032de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(RefMatcher);
851e35fdd936d133bf8a48de140a3c666897588a05shiqian};
852e35fdd936d133bf8a48de140a3c666897588a05shiqian
853e35fdd936d133bf8a48de140a3c666897588a05shiqian// Polymorphic helper functions for narrow and wide string matchers.
854e35fdd936d133bf8a48de140a3c666897588a05shiqianinline bool CaseInsensitiveCStringEquals(const char* lhs, const char* rhs) {
855e35fdd936d133bf8a48de140a3c666897588a05shiqian  return String::CaseInsensitiveCStringEquals(lhs, rhs);
856e35fdd936d133bf8a48de140a3c666897588a05shiqian}
857e35fdd936d133bf8a48de140a3c666897588a05shiqian
858e35fdd936d133bf8a48de140a3c666897588a05shiqianinline bool CaseInsensitiveCStringEquals(const wchar_t* lhs,
859e35fdd936d133bf8a48de140a3c666897588a05shiqian                                         const wchar_t* rhs) {
860e35fdd936d133bf8a48de140a3c666897588a05shiqian  return String::CaseInsensitiveWideCStringEquals(lhs, rhs);
861e35fdd936d133bf8a48de140a3c666897588a05shiqian}
862e35fdd936d133bf8a48de140a3c666897588a05shiqian
863e35fdd936d133bf8a48de140a3c666897588a05shiqian// String comparison for narrow or wide strings that can have embedded NUL
864e35fdd936d133bf8a48de140a3c666897588a05shiqian// characters.
865e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType>
866e35fdd936d133bf8a48de140a3c666897588a05shiqianbool CaseInsensitiveStringEquals(const StringType& s1,
867e35fdd936d133bf8a48de140a3c666897588a05shiqian                                 const StringType& s2) {
868e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Are the heads equal?
869e35fdd936d133bf8a48de140a3c666897588a05shiqian  if (!CaseInsensitiveCStringEquals(s1.c_str(), s2.c_str())) {
870e35fdd936d133bf8a48de140a3c666897588a05shiqian    return false;
871e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
872e35fdd936d133bf8a48de140a3c666897588a05shiqian
873e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Skip the equal heads.
874e35fdd936d133bf8a48de140a3c666897588a05shiqian  const typename StringType::value_type nul = 0;
875e35fdd936d133bf8a48de140a3c666897588a05shiqian  const size_t i1 = s1.find(nul), i2 = s2.find(nul);
876e35fdd936d133bf8a48de140a3c666897588a05shiqian
877e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Are we at the end of either s1 or s2?
878e35fdd936d133bf8a48de140a3c666897588a05shiqian  if (i1 == StringType::npos || i2 == StringType::npos) {
879e35fdd936d133bf8a48de140a3c666897588a05shiqian    return i1 == i2;
880e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
881e35fdd936d133bf8a48de140a3c666897588a05shiqian
882e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Are the tails equal?
883e35fdd936d133bf8a48de140a3c666897588a05shiqian  return CaseInsensitiveStringEquals(s1.substr(i1 + 1), s2.substr(i2 + 1));
884e35fdd936d133bf8a48de140a3c666897588a05shiqian}
885e35fdd936d133bf8a48de140a3c666897588a05shiqian
886e35fdd936d133bf8a48de140a3c666897588a05shiqian// String matchers.
887e35fdd936d133bf8a48de140a3c666897588a05shiqian
888e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements equality-based string matchers like StrEq, StrCaseNe, and etc.
889e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType>
890e35fdd936d133bf8a48de140a3c666897588a05shiqianclass StrEqualityMatcher {
891e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
892e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef typename StringType::const_pointer ConstCharPointer;
893e35fdd936d133bf8a48de140a3c666897588a05shiqian
894e35fdd936d133bf8a48de140a3c666897588a05shiqian  StrEqualityMatcher(const StringType& str, bool expect_eq,
895e35fdd936d133bf8a48de140a3c666897588a05shiqian                     bool case_sensitive)
896e35fdd936d133bf8a48de140a3c666897588a05shiqian      : string_(str), expect_eq_(expect_eq), case_sensitive_(case_sensitive) {}
897e35fdd936d133bf8a48de140a3c666897588a05shiqian
898e35fdd936d133bf8a48de140a3c666897588a05shiqian  // When expect_eq_ is true, returns true iff s is equal to string_;
899e35fdd936d133bf8a48de140a3c666897588a05shiqian  // otherwise returns true iff s is not equal to string_.
900db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplain(ConstCharPointer s,
901db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                       MatchResultListener* listener) const {
902e35fdd936d133bf8a48de140a3c666897588a05shiqian    if (s == NULL) {
903e35fdd936d133bf8a48de140a3c666897588a05shiqian      return !expect_eq_;
904e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
905db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return MatchAndExplain(StringType(s), listener);
906e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
907e35fdd936d133bf8a48de140a3c666897588a05shiqian
908db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplain(const StringType& s,
909db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                       MatchResultListener* /* listener */) const {
910e35fdd936d133bf8a48de140a3c666897588a05shiqian    const bool eq = case_sensitive_ ? s == string_ :
911e35fdd936d133bf8a48de140a3c666897588a05shiqian        CaseInsensitiveStringEquals(s, string_);
912e35fdd936d133bf8a48de140a3c666897588a05shiqian    return expect_eq_ == eq;
913e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
914e35fdd936d133bf8a48de140a3c666897588a05shiqian
915e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
916e35fdd936d133bf8a48de140a3c666897588a05shiqian    DescribeToHelper(expect_eq_, os);
917e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
918e35fdd936d133bf8a48de140a3c666897588a05shiqian
919e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
920e35fdd936d133bf8a48de140a3c666897588a05shiqian    DescribeToHelper(!expect_eq_, os);
921e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
92232de5f53763125925e078498250f7e73a88de9edzhanyong.wan
923e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
924e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeToHelper(bool expect_eq, ::std::ostream* os) const {
925b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    *os << (expect_eq ? "is " : "isn't ");
926e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "equal to ";
927e35fdd936d133bf8a48de140a3c666897588a05shiqian    if (!case_sensitive_) {
928e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "(ignoring case) ";
929e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
930e2e8ba401d198d1a8304c652e997505609b62696vladlosev    UniversalPrint(string_, os);
931e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
932e35fdd936d133bf8a48de140a3c666897588a05shiqian
933e35fdd936d133bf8a48de140a3c666897588a05shiqian  const StringType string_;
934e35fdd936d133bf8a48de140a3c666897588a05shiqian  const bool expect_eq_;
935e35fdd936d133bf8a48de140a3c666897588a05shiqian  const bool case_sensitive_;
93632de5f53763125925e078498250f7e73a88de9edzhanyong.wan
93732de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(StrEqualityMatcher);
938e35fdd936d133bf8a48de140a3c666897588a05shiqian};
939e35fdd936d133bf8a48de140a3c666897588a05shiqian
940e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the polymorphic HasSubstr(substring) matcher, which
941e35fdd936d133bf8a48de140a3c666897588a05shiqian// can be used as a Matcher<T> as long as T can be converted to a
942e35fdd936d133bf8a48de140a3c666897588a05shiqian// string.
943e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType>
944e35fdd936d133bf8a48de140a3c666897588a05shiqianclass HasSubstrMatcher {
945e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
946e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef typename StringType::const_pointer ConstCharPointer;
947e35fdd936d133bf8a48de140a3c666897588a05shiqian
948e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit HasSubstrMatcher(const StringType& substring)
949e35fdd936d133bf8a48de140a3c666897588a05shiqian      : substring_(substring) {}
950e35fdd936d133bf8a48de140a3c666897588a05shiqian
951e35fdd936d133bf8a48de140a3c666897588a05shiqian  // These overloaded methods allow HasSubstr(substring) to be used as a
952e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Matcher<T> as long as T can be converted to string.  Returns true
953e35fdd936d133bf8a48de140a3c666897588a05shiqian  // iff s contains substring_ as a substring.
954db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplain(ConstCharPointer s,
955db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                       MatchResultListener* listener) const {
956db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return s != NULL && MatchAndExplain(StringType(s), listener);
957e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
958e35fdd936d133bf8a48de140a3c666897588a05shiqian
959db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplain(const StringType& s,
960db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                       MatchResultListener* /* listener */) const {
961e35fdd936d133bf8a48de140a3c666897588a05shiqian    return s.find(substring_) != StringType::npos;
962e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
963e35fdd936d133bf8a48de140a3c666897588a05shiqian
964e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Describes what this matcher matches.
965e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
966e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "has substring ";
967e2e8ba401d198d1a8304c652e997505609b62696vladlosev    UniversalPrint(substring_, os);
968e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
969e35fdd936d133bf8a48de140a3c666897588a05shiqian
970e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
971e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "has no substring ";
972e2e8ba401d198d1a8304c652e997505609b62696vladlosev    UniversalPrint(substring_, os);
973e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
97432de5f53763125925e078498250f7e73a88de9edzhanyong.wan
975e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
976e35fdd936d133bf8a48de140a3c666897588a05shiqian  const StringType substring_;
97732de5f53763125925e078498250f7e73a88de9edzhanyong.wan
97832de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(HasSubstrMatcher);
979e35fdd936d133bf8a48de140a3c666897588a05shiqian};
980e35fdd936d133bf8a48de140a3c666897588a05shiqian
981e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the polymorphic StartsWith(substring) matcher, which
982e35fdd936d133bf8a48de140a3c666897588a05shiqian// can be used as a Matcher<T> as long as T can be converted to a
983e35fdd936d133bf8a48de140a3c666897588a05shiqian// string.
984e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType>
985e35fdd936d133bf8a48de140a3c666897588a05shiqianclass StartsWithMatcher {
986e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
987e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef typename StringType::const_pointer ConstCharPointer;
988e35fdd936d133bf8a48de140a3c666897588a05shiqian
989e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit StartsWithMatcher(const StringType& prefix) : prefix_(prefix) {
990e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
991e35fdd936d133bf8a48de140a3c666897588a05shiqian
992e35fdd936d133bf8a48de140a3c666897588a05shiqian  // These overloaded methods allow StartsWith(prefix) to be used as a
993e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Matcher<T> as long as T can be converted to string.  Returns true
994e35fdd936d133bf8a48de140a3c666897588a05shiqian  // iff s starts with prefix_.
995db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplain(ConstCharPointer s,
996db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                       MatchResultListener* listener) const {
997db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return s != NULL && MatchAndExplain(StringType(s), listener);
998e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
999e35fdd936d133bf8a48de140a3c666897588a05shiqian
1000db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplain(const StringType& s,
1001db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                       MatchResultListener* /* listener */) const {
1002e35fdd936d133bf8a48de140a3c666897588a05shiqian    return s.length() >= prefix_.length() &&
1003e35fdd936d133bf8a48de140a3c666897588a05shiqian        s.substr(0, prefix_.length()) == prefix_;
1004e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1005e35fdd936d133bf8a48de140a3c666897588a05shiqian
1006e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
1007e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "starts with ";
1008e2e8ba401d198d1a8304c652e997505609b62696vladlosev    UniversalPrint(prefix_, os);
1009e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1010e35fdd936d133bf8a48de140a3c666897588a05shiqian
1011e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
1012e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "doesn't start with ";
1013e2e8ba401d198d1a8304c652e997505609b62696vladlosev    UniversalPrint(prefix_, os);
1014e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
101532de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1016e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1017e35fdd936d133bf8a48de140a3c666897588a05shiqian  const StringType prefix_;
101832de5f53763125925e078498250f7e73a88de9edzhanyong.wan
101932de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(StartsWithMatcher);
1020e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1021e35fdd936d133bf8a48de140a3c666897588a05shiqian
1022e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the polymorphic EndsWith(substring) matcher, which
1023e35fdd936d133bf8a48de140a3c666897588a05shiqian// can be used as a Matcher<T> as long as T can be converted to a
1024e35fdd936d133bf8a48de140a3c666897588a05shiqian// string.
1025e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType>
1026e35fdd936d133bf8a48de140a3c666897588a05shiqianclass EndsWithMatcher {
1027e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1028e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef typename StringType::const_pointer ConstCharPointer;
1029e35fdd936d133bf8a48de140a3c666897588a05shiqian
1030e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit EndsWithMatcher(const StringType& suffix) : suffix_(suffix) {}
1031e35fdd936d133bf8a48de140a3c666897588a05shiqian
1032e35fdd936d133bf8a48de140a3c666897588a05shiqian  // These overloaded methods allow EndsWith(suffix) to be used as a
1033e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Matcher<T> as long as T can be converted to string.  Returns true
1034e35fdd936d133bf8a48de140a3c666897588a05shiqian  // iff s ends with suffix_.
1035db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplain(ConstCharPointer s,
1036db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                       MatchResultListener* listener) const {
1037db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return s != NULL && MatchAndExplain(StringType(s), listener);
1038e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1039e35fdd936d133bf8a48de140a3c666897588a05shiqian
1040db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplain(const StringType& s,
1041db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                       MatchResultListener* /* listener */) const {
1042e35fdd936d133bf8a48de140a3c666897588a05shiqian    return s.length() >= suffix_.length() &&
1043e35fdd936d133bf8a48de140a3c666897588a05shiqian        s.substr(s.length() - suffix_.length()) == suffix_;
1044e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1045e35fdd936d133bf8a48de140a3c666897588a05shiqian
1046e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
1047e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "ends with ";
1048e2e8ba401d198d1a8304c652e997505609b62696vladlosev    UniversalPrint(suffix_, os);
1049e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1050e35fdd936d133bf8a48de140a3c666897588a05shiqian
1051e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
1052e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "doesn't end with ";
1053e2e8ba401d198d1a8304c652e997505609b62696vladlosev    UniversalPrint(suffix_, os);
1054e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
105532de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1056e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1057e35fdd936d133bf8a48de140a3c666897588a05shiqian  const StringType suffix_;
105832de5f53763125925e078498250f7e73a88de9edzhanyong.wan
105932de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(EndsWithMatcher);
1060e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1061e35fdd936d133bf8a48de140a3c666897588a05shiqian
1062e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements polymorphic matchers MatchesRegex(regex) and
1063e35fdd936d133bf8a48de140a3c666897588a05shiqian// ContainsRegex(regex), which can be used as a Matcher<T> as long as
1064e35fdd936d133bf8a48de140a3c666897588a05shiqian// T can be converted to a string.
1065e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatchesRegexMatcher {
1066e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1067e35fdd936d133bf8a48de140a3c666897588a05shiqian  MatchesRegexMatcher(const RE* regex, bool full_match)
1068e35fdd936d133bf8a48de140a3c666897588a05shiqian      : regex_(regex), full_match_(full_match) {}
1069e35fdd936d133bf8a48de140a3c666897588a05shiqian
1070e35fdd936d133bf8a48de140a3c666897588a05shiqian  // These overloaded methods allow MatchesRegex(regex) to be used as
1071e35fdd936d133bf8a48de140a3c666897588a05shiqian  // a Matcher<T> as long as T can be converted to string.  Returns
1072e35fdd936d133bf8a48de140a3c666897588a05shiqian  // true iff s matches regular expression regex.  When full_match_ is
1073e35fdd936d133bf8a48de140a3c666897588a05shiqian  // true, a full match is done; otherwise a partial match is done.
1074db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplain(const char* s,
1075db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                       MatchResultListener* listener) const {
1076db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return s != NULL && MatchAndExplain(internal::string(s), listener);
1077e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1078e35fdd936d133bf8a48de140a3c666897588a05shiqian
1079db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplain(const internal::string& s,
1080db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                       MatchResultListener* /* listener */) const {
1081e35fdd936d133bf8a48de140a3c666897588a05shiqian    return full_match_ ? RE::FullMatch(s, *regex_) :
1082e35fdd936d133bf8a48de140a3c666897588a05shiqian        RE::PartialMatch(s, *regex_);
1083e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1084e35fdd936d133bf8a48de140a3c666897588a05shiqian
1085e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
1086e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << (full_match_ ? "matches" : "contains")
1087e35fdd936d133bf8a48de140a3c666897588a05shiqian        << " regular expression ";
1088e35fdd936d133bf8a48de140a3c666897588a05shiqian    UniversalPrinter<internal::string>::Print(regex_->pattern(), os);
1089e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1090e35fdd936d133bf8a48de140a3c666897588a05shiqian
1091e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
1092e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "doesn't " << (full_match_ ? "match" : "contain")
1093e35fdd936d133bf8a48de140a3c666897588a05shiqian        << " regular expression ";
1094e35fdd936d133bf8a48de140a3c666897588a05shiqian    UniversalPrinter<internal::string>::Print(regex_->pattern(), os);
1095e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
109632de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1097e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1098e35fdd936d133bf8a48de140a3c666897588a05shiqian  const internal::linked_ptr<const RE> regex_;
1099e35fdd936d133bf8a48de140a3c666897588a05shiqian  const bool full_match_;
110032de5f53763125925e078498250f7e73a88de9edzhanyong.wan
110132de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(MatchesRegexMatcher);
1102e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1103e35fdd936d133bf8a48de140a3c666897588a05shiqian
1104e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements a matcher that compares the two fields of a 2-tuple
1105e35fdd936d133bf8a48de140a3c666897588a05shiqian// using one of the ==, <=, <, etc, operators.  The two fields being
1106e35fdd936d133bf8a48de140a3c666897588a05shiqian// compared don't have to have the same type.
1107e35fdd936d133bf8a48de140a3c666897588a05shiqian//
1108e35fdd936d133bf8a48de140a3c666897588a05shiqian// The matcher defined here is polymorphic (for example, Eq() can be
1109e35fdd936d133bf8a48de140a3c666897588a05shiqian// used to match a tuple<int, short>, a tuple<const long&, double>,
1110e35fdd936d133bf8a48de140a3c666897588a05shiqian// etc).  Therefore we use a template type conversion operator in the
1111e35fdd936d133bf8a48de140a3c666897588a05shiqian// implementation.
1112e35fdd936d133bf8a48de140a3c666897588a05shiqian//
1113e35fdd936d133bf8a48de140a3c666897588a05shiqian// We define this as a macro in order to eliminate duplicated source
1114e35fdd936d133bf8a48de140a3c666897588a05shiqian// code.
1115ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan#define GMOCK_IMPLEMENT_COMPARISON2_MATCHER_(name, op, relation) \
1116e35fdd936d133bf8a48de140a3c666897588a05shiqian  class name##2Matcher { \
1117e35fdd936d133bf8a48de140a3c666897588a05shiqian   public: \
1118e35fdd936d133bf8a48de140a3c666897588a05shiqian    template <typename T1, typename T2> \
1119ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    operator Matcher< ::std::tr1::tuple<T1, T2> >() const { \
1120ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      return MakeMatcher(new Impl< ::std::tr1::tuple<T1, T2> >); \
1121ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    } \
1122ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    template <typename T1, typename T2> \
1123e35fdd936d133bf8a48de140a3c666897588a05shiqian    operator Matcher<const ::std::tr1::tuple<T1, T2>&>() const { \
1124ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      return MakeMatcher(new Impl<const ::std::tr1::tuple<T1, T2>&>); \
1125e35fdd936d133bf8a48de140a3c666897588a05shiqian    } \
1126e35fdd936d133bf8a48de140a3c666897588a05shiqian   private: \
1127ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    template <typename Tuple> \
1128ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    class Impl : public MatcherInterface<Tuple> { \
1129e35fdd936d133bf8a48de140a3c666897588a05shiqian     public: \
1130821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      virtual bool MatchAndExplain( \
1131ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan          Tuple args, \
1132821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan          MatchResultListener* /* listener */) const { \
1133e35fdd936d133bf8a48de140a3c666897588a05shiqian        return ::std::tr1::get<0>(args) op ::std::tr1::get<1>(args); \
1134e35fdd936d133bf8a48de140a3c666897588a05shiqian      } \
1135e35fdd936d133bf8a48de140a3c666897588a05shiqian      virtual void DescribeTo(::std::ostream* os) const { \
1136ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        *os << "are " relation;                                 \
1137e35fdd936d133bf8a48de140a3c666897588a05shiqian      } \
1138e35fdd936d133bf8a48de140a3c666897588a05shiqian      virtual void DescribeNegationTo(::std::ostream* os) const { \
1139ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        *os << "aren't " relation; \
1140e35fdd936d133bf8a48de140a3c666897588a05shiqian      } \
1141e35fdd936d133bf8a48de140a3c666897588a05shiqian    }; \
1142e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1143e35fdd936d133bf8a48de140a3c666897588a05shiqian
1144e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements Eq(), Ge(), Gt(), Le(), Lt(), and Ne() respectively.
1145ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Eq, ==, "an equal pair");
1146ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(
1147ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    Ge, >=, "a pair where the first >= the second");
1148ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(
1149ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    Gt, >, "a pair where the first > the second");
1150ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(
1151ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    Le, <=, "a pair where the first <= the second");
1152ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(
1153ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    Lt, <, "a pair where the first < the second");
1154ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Ne, !=, "an unequal pair");
1155e35fdd936d133bf8a48de140a3c666897588a05shiqian
1156e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan#undef GMOCK_IMPLEMENT_COMPARISON2_MATCHER_
1157e35fdd936d133bf8a48de140a3c666897588a05shiqian
1158c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// Implements the Not(...) matcher for a particular argument type T.
1159c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// We do not nest it inside the NotMatcher class template, as that
1160c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// will prevent different instantiations of NotMatcher from sharing
1161c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// the same NotMatcherImpl<T> class.
1162c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wantemplate <typename T>
1163c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wanclass NotMatcherImpl : public MatcherInterface<T> {
1164c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan public:
1165c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  explicit NotMatcherImpl(const Matcher<T>& matcher)
1166c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      : matcher_(matcher) {}
1167c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1168821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
1169821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    return !matcher_.MatchAndExplain(x, listener);
1170c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1171c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1172c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
1173c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    matcher_.DescribeNegationTo(os);
1174c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1175c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1176c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
1177c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    matcher_.DescribeTo(os);
1178c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1179c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1180c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan private:
1181c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  const Matcher<T> matcher_;
118232de5f53763125925e078498250f7e73a88de9edzhanyong.wan
118332de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(NotMatcherImpl);
1184c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan};
1185c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1186e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the Not(m) matcher, which matches a value that doesn't
1187e35fdd936d133bf8a48de140a3c666897588a05shiqian// match matcher m.
1188e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename InnerMatcher>
1189e35fdd936d133bf8a48de140a3c666897588a05shiqianclass NotMatcher {
1190e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1191e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit NotMatcher(InnerMatcher matcher) : matcher_(matcher) {}
1192e35fdd936d133bf8a48de140a3c666897588a05shiqian
1193e35fdd936d133bf8a48de140a3c666897588a05shiqian  // This template type conversion operator allows Not(m) to be used
1194e35fdd936d133bf8a48de140a3c666897588a05shiqian  // to match any type m can match.
1195e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1196e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<T>() const {
1197c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    return Matcher<T>(new NotMatcherImpl<T>(SafeMatcherCast<T>(matcher_)));
1198e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
119932de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1200e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1201c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  InnerMatcher matcher_;
120232de5f53763125925e078498250f7e73a88de9edzhanyong.wan
120332de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(NotMatcher);
1204c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan};
1205e35fdd936d133bf8a48de140a3c666897588a05shiqian
1206c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// Implements the AllOf(m1, m2) matcher for a particular argument type
1207c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// T. We do not nest it inside the BothOfMatcher class template, as
1208c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// that will prevent different instantiations of BothOfMatcher from
1209c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// sharing the same BothOfMatcherImpl<T> class.
1210c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wantemplate <typename T>
1211c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wanclass BothOfMatcherImpl : public MatcherInterface<T> {
1212c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan public:
1213c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  BothOfMatcherImpl(const Matcher<T>& matcher1, const Matcher<T>& matcher2)
1214c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      : matcher1_(matcher1), matcher2_(matcher2) {}
1215e35fdd936d133bf8a48de140a3c666897588a05shiqian
1216c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
1217c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    *os << "(";
1218c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    matcher1_.DescribeTo(os);
1219c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    *os << ") and (";
1220c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    matcher2_.DescribeTo(os);
1221c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    *os << ")";
1222c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1223e35fdd936d133bf8a48de140a3c666897588a05shiqian
1224c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
1225b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    *os << "(";
1226b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    matcher1_.DescribeNegationTo(os);
1227b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    *os << ") or (";
1228b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    matcher2_.DescribeNegationTo(os);
1229b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    *os << ")";
1230c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1231e35fdd936d133bf8a48de140a3c666897588a05shiqian
1232821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
1233821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // If either matcher1_ or matcher2_ doesn't match x, we only need
1234821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // to explain why one of them fails.
1235821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    StringMatchResultListener listener1;
1236821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (!matcher1_.MatchAndExplain(x, &listener1)) {
1237821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      *listener << listener1.str();
1238821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      return false;
1239821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    }
1240c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1241821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    StringMatchResultListener listener2;
1242821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (!matcher2_.MatchAndExplain(x, &listener2)) {
1243821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      *listener << listener2.str();
1244821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      return false;
1245821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    }
1246c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1247821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // Otherwise we need to explain why *both* of them match.
1248821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    const internal::string s1 = listener1.str();
1249821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    const internal::string s2 = listener2.str();
1250821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
1251821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (s1 == "") {
1252821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      *listener << s2;
1253c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    } else {
1254821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      *listener << s1;
1255821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      if (s2 != "") {
1256b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan        *listener << ", and " << s2;
1257c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      }
1258c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    }
1259821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    return true;
1260c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
126132de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1262c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan private:
1263c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  const Matcher<T> matcher1_;
1264c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  const Matcher<T> matcher2_;
126532de5f53763125925e078498250f7e73a88de9edzhanyong.wan
126632de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(BothOfMatcherImpl);
1267e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1268e35fdd936d133bf8a48de140a3c666897588a05shiqian
1269e35fdd936d133bf8a48de140a3c666897588a05shiqian// Used for implementing the AllOf(m_1, ..., m_n) matcher, which
1270e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a value that matches all of the matchers m_1, ..., and m_n.
1271e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2>
1272e35fdd936d133bf8a48de140a3c666897588a05shiqianclass BothOfMatcher {
1273e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1274e35fdd936d133bf8a48de140a3c666897588a05shiqian  BothOfMatcher(Matcher1 matcher1, Matcher2 matcher2)
1275e35fdd936d133bf8a48de140a3c666897588a05shiqian      : matcher1_(matcher1), matcher2_(matcher2) {}
1276e35fdd936d133bf8a48de140a3c666897588a05shiqian
1277e35fdd936d133bf8a48de140a3c666897588a05shiqian  // This template type conversion operator allows a
1278e35fdd936d133bf8a48de140a3c666897588a05shiqian  // BothOfMatcher<Matcher1, Matcher2> object to match any type that
1279e35fdd936d133bf8a48de140a3c666897588a05shiqian  // both Matcher1 and Matcher2 can match.
1280e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1281e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<T>() const {
1282c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    return Matcher<T>(new BothOfMatcherImpl<T>(SafeMatcherCast<T>(matcher1_),
1283c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan                                               SafeMatcherCast<T>(matcher2_)));
1284e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
128532de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1286e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1287c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  Matcher1 matcher1_;
1288c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  Matcher2 matcher2_;
128932de5f53763125925e078498250f7e73a88de9edzhanyong.wan
129032de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(BothOfMatcher);
1291c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan};
1292e35fdd936d133bf8a48de140a3c666897588a05shiqian
1293c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// Implements the AnyOf(m1, m2) matcher for a particular argument type
1294c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// T.  We do not nest it inside the AnyOfMatcher class template, as
1295c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// that will prevent different instantiations of AnyOfMatcher from
1296c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// sharing the same EitherOfMatcherImpl<T> class.
1297c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wantemplate <typename T>
1298c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wanclass EitherOfMatcherImpl : public MatcherInterface<T> {
1299c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan public:
1300c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  EitherOfMatcherImpl(const Matcher<T>& matcher1, const Matcher<T>& matcher2)
1301c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      : matcher1_(matcher1), matcher2_(matcher2) {}
1302e35fdd936d133bf8a48de140a3c666897588a05shiqian
1303c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
1304c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    *os << "(";
1305c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    matcher1_.DescribeTo(os);
1306c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    *os << ") or (";
1307c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    matcher2_.DescribeTo(os);
1308c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    *os << ")";
1309c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1310e35fdd936d133bf8a48de140a3c666897588a05shiqian
1311c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
1312b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    *os << "(";
1313b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    matcher1_.DescribeNegationTo(os);
1314b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    *os << ") and (";
1315b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    matcher2_.DescribeNegationTo(os);
1316b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    *os << ")";
1317c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1318c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1319821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
1320821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // If either matcher1_ or matcher2_ matches x, we just need to
1321821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // explain why *one* of them matches.
1322821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    StringMatchResultListener listener1;
1323821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (matcher1_.MatchAndExplain(x, &listener1)) {
1324821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      *listener << listener1.str();
1325821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      return true;
1326821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    }
1327c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1328821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    StringMatchResultListener listener2;
1329821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (matcher2_.MatchAndExplain(x, &listener2)) {
1330821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      *listener << listener2.str();
1331821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      return true;
1332821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    }
1333c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1334821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // Otherwise we need to explain why *both* of them fail.
1335821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    const internal::string s1 = listener1.str();
1336821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    const internal::string s2 = listener2.str();
1337821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
1338821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (s1 == "") {
1339821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      *listener << s2;
1340821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    } else {
1341821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      *listener << s1;
1342821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      if (s2 != "") {
1343b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan        *listener << ", and " << s2;
1344e35fdd936d133bf8a48de140a3c666897588a05shiqian      }
1345e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1346821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    return false;
1347c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
134832de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1349c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan private:
1350c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  const Matcher<T> matcher1_;
1351c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  const Matcher<T> matcher2_;
135232de5f53763125925e078498250f7e73a88de9edzhanyong.wan
135332de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(EitherOfMatcherImpl);
1354e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1355e35fdd936d133bf8a48de140a3c666897588a05shiqian
1356e35fdd936d133bf8a48de140a3c666897588a05shiqian// Used for implementing the AnyOf(m_1, ..., m_n) matcher, which
1357e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a value that matches at least one of the matchers m_1, ...,
1358e35fdd936d133bf8a48de140a3c666897588a05shiqian// and m_n.
1359e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2>
1360e35fdd936d133bf8a48de140a3c666897588a05shiqianclass EitherOfMatcher {
1361e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1362e35fdd936d133bf8a48de140a3c666897588a05shiqian  EitherOfMatcher(Matcher1 matcher1, Matcher2 matcher2)
1363e35fdd936d133bf8a48de140a3c666897588a05shiqian      : matcher1_(matcher1), matcher2_(matcher2) {}
1364e35fdd936d133bf8a48de140a3c666897588a05shiqian
1365e35fdd936d133bf8a48de140a3c666897588a05shiqian  // This template type conversion operator allows a
1366e35fdd936d133bf8a48de140a3c666897588a05shiqian  // EitherOfMatcher<Matcher1, Matcher2> object to match any type that
1367e35fdd936d133bf8a48de140a3c666897588a05shiqian  // both Matcher1 and Matcher2 can match.
1368e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1369e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<T>() const {
137016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan    return Matcher<T>(new EitherOfMatcherImpl<T>(
137116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan        SafeMatcherCast<T>(matcher1_), SafeMatcherCast<T>(matcher2_)));
1372e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
137332de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1374e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1375e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher1 matcher1_;
1376e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher2 matcher2_;
137732de5f53763125925e078498250f7e73a88de9edzhanyong.wan
137832de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(EitherOfMatcher);
1379e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1380e35fdd936d133bf8a48de140a3c666897588a05shiqian
1381e35fdd936d133bf8a48de140a3c666897588a05shiqian// Used for implementing Truly(pred), which turns a predicate into a
1382e35fdd936d133bf8a48de140a3c666897588a05shiqian// matcher.
1383e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Predicate>
1384e35fdd936d133bf8a48de140a3c666897588a05shiqianclass TrulyMatcher {
1385e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1386e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit TrulyMatcher(Predicate pred) : predicate_(pred) {}
1387e35fdd936d133bf8a48de140a3c666897588a05shiqian
1388e35fdd936d133bf8a48de140a3c666897588a05shiqian  // This method template allows Truly(pred) to be used as a matcher
1389e35fdd936d133bf8a48de140a3c666897588a05shiqian  // for type T where T is the argument type of predicate 'pred'.  The
1390e35fdd936d133bf8a48de140a3c666897588a05shiqian  // argument is passed by reference as the predicate may be
1391e35fdd936d133bf8a48de140a3c666897588a05shiqian  // interested in the address of the argument.
1392e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1393db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplain(T& x,  // NOLINT
1394db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                       MatchResultListener* /* listener */) const {
13958d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan    // Without the if-statement, MSVC sometimes warns about converting
13968d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan    // a value to bool (warning 4800).
13978d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan    //
13988d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan    // We cannot write 'return !!predicate_(x);' as that doesn't work
13998d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan    // when predicate_(x) returns a class convertible to bool but
14008d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan    // having no operator!().
14018d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan    if (predicate_(x))
14028d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan      return true;
14038d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan    return false;
1404e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1405e35fdd936d133bf8a48de140a3c666897588a05shiqian
1406e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
1407e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "satisfies the given predicate";
1408e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1409e35fdd936d133bf8a48de140a3c666897588a05shiqian
1410e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
1411e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "doesn't satisfy the given predicate";
1412e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
141332de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1414e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1415e35fdd936d133bf8a48de140a3c666897588a05shiqian  Predicate predicate_;
141632de5f53763125925e078498250f7e73a88de9edzhanyong.wan
141732de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(TrulyMatcher);
1418e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1419e35fdd936d133bf8a48de140a3c666897588a05shiqian
1420e35fdd936d133bf8a48de140a3c666897588a05shiqian// Used for implementing Matches(matcher), which turns a matcher into
1421e35fdd936d133bf8a48de140a3c666897588a05shiqian// a predicate.
1422e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename M>
1423e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatcherAsPredicate {
1424e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1425e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit MatcherAsPredicate(M matcher) : matcher_(matcher) {}
1426e35fdd936d133bf8a48de140a3c666897588a05shiqian
1427e35fdd936d133bf8a48de140a3c666897588a05shiqian  // This template operator() allows Matches(m) to be used as a
1428e35fdd936d133bf8a48de140a3c666897588a05shiqian  // predicate on type T where m is a matcher on type T.
1429e35fdd936d133bf8a48de140a3c666897588a05shiqian  //
1430e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The argument x is passed by reference instead of by value, as
1431e35fdd936d133bf8a48de140a3c666897588a05shiqian  // some matcher may be interested in its address (e.g. as in
1432e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Matches(Ref(n))(x)).
1433e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1434e35fdd936d133bf8a48de140a3c666897588a05shiqian  bool operator()(const T& x) const {
1435e35fdd936d133bf8a48de140a3c666897588a05shiqian    // We let matcher_ commit to a particular type here instead of
1436e35fdd936d133bf8a48de140a3c666897588a05shiqian    // when the MatcherAsPredicate object was constructed.  This
1437e35fdd936d133bf8a48de140a3c666897588a05shiqian    // allows us to write Matches(m) where m is a polymorphic matcher
1438e35fdd936d133bf8a48de140a3c666897588a05shiqian    // (e.g. Eq(5)).
1439e35fdd936d133bf8a48de140a3c666897588a05shiqian    //
1440e35fdd936d133bf8a48de140a3c666897588a05shiqian    // If we write Matcher<T>(matcher_).Matches(x) here, it won't
1441e35fdd936d133bf8a48de140a3c666897588a05shiqian    // compile when matcher_ has type Matcher<const T&>; if we write
1442e35fdd936d133bf8a48de140a3c666897588a05shiqian    // Matcher<const T&>(matcher_).Matches(x) here, it won't compile
1443e35fdd936d133bf8a48de140a3c666897588a05shiqian    // when matcher_ has type Matcher<T>; if we just write
1444e35fdd936d133bf8a48de140a3c666897588a05shiqian    // matcher_.Matches(x), it won't compile when matcher_ is
1445e35fdd936d133bf8a48de140a3c666897588a05shiqian    // polymorphic, e.g. Eq(5).
1446e35fdd936d133bf8a48de140a3c666897588a05shiqian    //
1447e35fdd936d133bf8a48de140a3c666897588a05shiqian    // MatcherCast<const T&>() is necessary for making the code work
1448e35fdd936d133bf8a48de140a3c666897588a05shiqian    // in all of the above situations.
1449e35fdd936d133bf8a48de140a3c666897588a05shiqian    return MatcherCast<const T&>(matcher_).Matches(x);
1450e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
145132de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1452e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1453e35fdd936d133bf8a48de140a3c666897588a05shiqian  M matcher_;
145432de5f53763125925e078498250f7e73a88de9edzhanyong.wan
145532de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(MatcherAsPredicate);
1456e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1457e35fdd936d133bf8a48de140a3c666897588a05shiqian
1458e35fdd936d133bf8a48de140a3c666897588a05shiqian// For implementing ASSERT_THAT() and EXPECT_THAT().  The template
1459e35fdd936d133bf8a48de140a3c666897588a05shiqian// argument M must be a type that can be converted to a matcher.
1460e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename M>
1461e35fdd936d133bf8a48de140a3c666897588a05shiqianclass PredicateFormatterFromMatcher {
1462e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1463e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit PredicateFormatterFromMatcher(const M& m) : matcher_(m) {}
1464e35fdd936d133bf8a48de140a3c666897588a05shiqian
1465e35fdd936d133bf8a48de140a3c666897588a05shiqian  // This template () operator allows a PredicateFormatterFromMatcher
1466e35fdd936d133bf8a48de140a3c666897588a05shiqian  // object to act as a predicate-formatter suitable for using with
1467e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Google Test's EXPECT_PRED_FORMAT1() macro.
1468e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1469e35fdd936d133bf8a48de140a3c666897588a05shiqian  AssertionResult operator()(const char* value_text, const T& x) const {
1470e35fdd936d133bf8a48de140a3c666897588a05shiqian    // We convert matcher_ to a Matcher<const T&> *now* instead of
1471e35fdd936d133bf8a48de140a3c666897588a05shiqian    // when the PredicateFormatterFromMatcher object was constructed,
1472e35fdd936d133bf8a48de140a3c666897588a05shiqian    // as matcher_ may be polymorphic (e.g. NotNull()) and we won't
1473e35fdd936d133bf8a48de140a3c666897588a05shiqian    // know which type to instantiate it to until we actually see the
1474e35fdd936d133bf8a48de140a3c666897588a05shiqian    // type of x here.
1475e35fdd936d133bf8a48de140a3c666897588a05shiqian    //
1476e35fdd936d133bf8a48de140a3c666897588a05shiqian    // We write MatcherCast<const T&>(matcher_) instead of
1477e35fdd936d133bf8a48de140a3c666897588a05shiqian    // Matcher<const T&>(matcher_), as the latter won't compile when
1478e35fdd936d133bf8a48de140a3c666897588a05shiqian    // matcher_ has type Matcher<T> (e.g. An<int>()).
1479e35fdd936d133bf8a48de140a3c666897588a05shiqian    const Matcher<const T&> matcher = MatcherCast<const T&>(matcher_);
1480821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    StringMatchResultListener listener;
1481b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    if (MatchPrintAndExplain(x, matcher, &listener))
1482e35fdd936d133bf8a48de140a3c666897588a05shiqian      return AssertionSuccess();
1483b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan
1484b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    ::std::stringstream ss;
1485b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    ss << "Value of: " << value_text << "\n"
1486b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan       << "Expected: ";
1487b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    matcher.DescribeTo(&ss);
1488b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    ss << "\n  Actual: " << listener.str();
1489b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    return AssertionFailure() << ss.str();
1490e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
149132de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1492e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1493e35fdd936d133bf8a48de140a3c666897588a05shiqian  const M matcher_;
149432de5f53763125925e078498250f7e73a88de9edzhanyong.wan
149532de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(PredicateFormatterFromMatcher);
1496e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1497e35fdd936d133bf8a48de140a3c666897588a05shiqian
1498e35fdd936d133bf8a48de140a3c666897588a05shiqian// A helper function for converting a matcher to a predicate-formatter
1499e35fdd936d133bf8a48de140a3c666897588a05shiqian// without the user needing to explicitly write the type.  This is
1500e35fdd936d133bf8a48de140a3c666897588a05shiqian// used for implementing ASSERT_THAT() and EXPECT_THAT().
1501e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename M>
1502e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PredicateFormatterFromMatcher<M>
1503e35fdd936d133bf8a48de140a3c666897588a05shiqianMakePredicateFormatterFromMatcher(const M& matcher) {
1504e35fdd936d133bf8a48de140a3c666897588a05shiqian  return PredicateFormatterFromMatcher<M>(matcher);
1505e35fdd936d133bf8a48de140a3c666897588a05shiqian}
1506e35fdd936d133bf8a48de140a3c666897588a05shiqian
1507e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the polymorphic floating point equality matcher, which
1508e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches two float values using ULP-based approximation.  The
1509e35fdd936d133bf8a48de140a3c666897588a05shiqian// template is meant to be instantiated with FloatType being either
1510e35fdd936d133bf8a48de140a3c666897588a05shiqian// float or double.
1511e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename FloatType>
1512e35fdd936d133bf8a48de140a3c666897588a05shiqianclass FloatingEqMatcher {
1513e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1514e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Constructor for FloatingEqMatcher.
1515e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The matcher's input will be compared with rhs.  The matcher treats two
1516e35fdd936d133bf8a48de140a3c666897588a05shiqian  // NANs as equal if nan_eq_nan is true.  Otherwise, under IEEE standards,
1517e35fdd936d133bf8a48de140a3c666897588a05shiqian  // equality comparisons between NANs will always return false.
1518e35fdd936d133bf8a48de140a3c666897588a05shiqian  FloatingEqMatcher(FloatType rhs, bool nan_eq_nan) :
1519e35fdd936d133bf8a48de140a3c666897588a05shiqian    rhs_(rhs), nan_eq_nan_(nan_eq_nan) {}
1520e35fdd936d133bf8a48de140a3c666897588a05shiqian
1521e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Implements floating point equality matcher as a Matcher<T>.
1522e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1523e35fdd936d133bf8a48de140a3c666897588a05shiqian  class Impl : public MatcherInterface<T> {
1524e35fdd936d133bf8a48de140a3c666897588a05shiqian   public:
1525e35fdd936d133bf8a48de140a3c666897588a05shiqian    Impl(FloatType rhs, bool nan_eq_nan) :
1526e35fdd936d133bf8a48de140a3c666897588a05shiqian      rhs_(rhs), nan_eq_nan_(nan_eq_nan) {}
1527e35fdd936d133bf8a48de140a3c666897588a05shiqian
1528821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    virtual bool MatchAndExplain(T value,
1529821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan                                 MatchResultListener* /* listener */) const {
1530e35fdd936d133bf8a48de140a3c666897588a05shiqian      const FloatingPoint<FloatType> lhs(value), rhs(rhs_);
1531e35fdd936d133bf8a48de140a3c666897588a05shiqian
1532e35fdd936d133bf8a48de140a3c666897588a05shiqian      // Compares NaNs first, if nan_eq_nan_ is true.
1533e35fdd936d133bf8a48de140a3c666897588a05shiqian      if (nan_eq_nan_ && lhs.is_nan()) {
1534e35fdd936d133bf8a48de140a3c666897588a05shiqian        return rhs.is_nan();
1535e35fdd936d133bf8a48de140a3c666897588a05shiqian      }
1536e35fdd936d133bf8a48de140a3c666897588a05shiqian
1537e35fdd936d133bf8a48de140a3c666897588a05shiqian      return lhs.AlmostEquals(rhs);
1538e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1539e35fdd936d133bf8a48de140a3c666897588a05shiqian
1540e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeTo(::std::ostream* os) const {
1541e35fdd936d133bf8a48de140a3c666897588a05shiqian      // os->precision() returns the previously set precision, which we
1542e35fdd936d133bf8a48de140a3c666897588a05shiqian      // store to restore the ostream to its original configuration
1543e35fdd936d133bf8a48de140a3c666897588a05shiqian      // after outputting.
1544e35fdd936d133bf8a48de140a3c666897588a05shiqian      const ::std::streamsize old_precision = os->precision(
1545e35fdd936d133bf8a48de140a3c666897588a05shiqian          ::std::numeric_limits<FloatType>::digits10 + 2);
1546e35fdd936d133bf8a48de140a3c666897588a05shiqian      if (FloatingPoint<FloatType>(rhs_).is_nan()) {
1547e35fdd936d133bf8a48de140a3c666897588a05shiqian        if (nan_eq_nan_) {
1548e35fdd936d133bf8a48de140a3c666897588a05shiqian          *os << "is NaN";
1549e35fdd936d133bf8a48de140a3c666897588a05shiqian        } else {
1550e35fdd936d133bf8a48de140a3c666897588a05shiqian          *os << "never matches";
1551e35fdd936d133bf8a48de140a3c666897588a05shiqian        }
1552e35fdd936d133bf8a48de140a3c666897588a05shiqian      } else {
1553e35fdd936d133bf8a48de140a3c666897588a05shiqian        *os << "is approximately " << rhs_;
1554e35fdd936d133bf8a48de140a3c666897588a05shiqian      }
1555e35fdd936d133bf8a48de140a3c666897588a05shiqian      os->precision(old_precision);
1556e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1557e35fdd936d133bf8a48de140a3c666897588a05shiqian
1558e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeNegationTo(::std::ostream* os) const {
1559e35fdd936d133bf8a48de140a3c666897588a05shiqian      // As before, get original precision.
1560e35fdd936d133bf8a48de140a3c666897588a05shiqian      const ::std::streamsize old_precision = os->precision(
1561e35fdd936d133bf8a48de140a3c666897588a05shiqian          ::std::numeric_limits<FloatType>::digits10 + 2);
1562e35fdd936d133bf8a48de140a3c666897588a05shiqian      if (FloatingPoint<FloatType>(rhs_).is_nan()) {
1563e35fdd936d133bf8a48de140a3c666897588a05shiqian        if (nan_eq_nan_) {
1564b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan          *os << "isn't NaN";
1565e35fdd936d133bf8a48de140a3c666897588a05shiqian        } else {
1566e35fdd936d133bf8a48de140a3c666897588a05shiqian          *os << "is anything";
1567e35fdd936d133bf8a48de140a3c666897588a05shiqian        }
1568e35fdd936d133bf8a48de140a3c666897588a05shiqian      } else {
1569b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan        *os << "isn't approximately " << rhs_;
1570e35fdd936d133bf8a48de140a3c666897588a05shiqian      }
1571e35fdd936d133bf8a48de140a3c666897588a05shiqian      // Restore original precision.
1572e35fdd936d133bf8a48de140a3c666897588a05shiqian      os->precision(old_precision);
1573e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1574e35fdd936d133bf8a48de140a3c666897588a05shiqian
1575e35fdd936d133bf8a48de140a3c666897588a05shiqian   private:
1576e35fdd936d133bf8a48de140a3c666897588a05shiqian    const FloatType rhs_;
1577e35fdd936d133bf8a48de140a3c666897588a05shiqian    const bool nan_eq_nan_;
157832de5f53763125925e078498250f7e73a88de9edzhanyong.wan
157932de5f53763125925e078498250f7e73a88de9edzhanyong.wan    GTEST_DISALLOW_ASSIGN_(Impl);
1580e35fdd936d133bf8a48de140a3c666897588a05shiqian  };
1581e35fdd936d133bf8a48de140a3c666897588a05shiqian
1582e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The following 3 type conversion operators allow FloatEq(rhs) and
1583e35fdd936d133bf8a48de140a3c666897588a05shiqian  // NanSensitiveFloatEq(rhs) to be used as a Matcher<float>, a
1584e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Matcher<const float&>, or a Matcher<float&>, but nothing else.
1585e35fdd936d133bf8a48de140a3c666897588a05shiqian  // (While Google's C++ coding style doesn't allow arguments passed
1586e35fdd936d133bf8a48de140a3c666897588a05shiqian  // by non-const reference, we may see them in code not conforming to
1587e35fdd936d133bf8a48de140a3c666897588a05shiqian  // the style.  Therefore Google Mock needs to support them.)
1588e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<FloatType>() const {
1589e35fdd936d133bf8a48de140a3c666897588a05shiqian    return MakeMatcher(new Impl<FloatType>(rhs_, nan_eq_nan_));
1590e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1591e35fdd936d133bf8a48de140a3c666897588a05shiqian
1592e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<const FloatType&>() const {
1593e35fdd936d133bf8a48de140a3c666897588a05shiqian    return MakeMatcher(new Impl<const FloatType&>(rhs_, nan_eq_nan_));
1594e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1595e35fdd936d133bf8a48de140a3c666897588a05shiqian
1596e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<FloatType&>() const {
1597e35fdd936d133bf8a48de140a3c666897588a05shiqian    return MakeMatcher(new Impl<FloatType&>(rhs_, nan_eq_nan_));
1598e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1599e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1600e35fdd936d133bf8a48de140a3c666897588a05shiqian  const FloatType rhs_;
1601e35fdd936d133bf8a48de140a3c666897588a05shiqian  const bool nan_eq_nan_;
160232de5f53763125925e078498250f7e73a88de9edzhanyong.wan
160332de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(FloatingEqMatcher);
1604e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1605e35fdd936d133bf8a48de140a3c666897588a05shiqian
1606e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the Pointee(m) matcher for matching a pointer whose
1607e35fdd936d133bf8a48de140a3c666897588a05shiqian// pointee matches matcher m.  The pointer can be either raw or smart.
1608e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename InnerMatcher>
1609e35fdd936d133bf8a48de140a3c666897588a05shiqianclass PointeeMatcher {
1610e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1611e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit PointeeMatcher(const InnerMatcher& matcher) : matcher_(matcher) {}
1612e35fdd936d133bf8a48de140a3c666897588a05shiqian
1613e35fdd936d133bf8a48de140a3c666897588a05shiqian  // This type conversion operator template allows Pointee(m) to be
1614e35fdd936d133bf8a48de140a3c666897588a05shiqian  // used as a matcher for any pointer type whose pointee type is
1615e35fdd936d133bf8a48de140a3c666897588a05shiqian  // compatible with the inner matcher, where type Pointer can be
1616e35fdd936d133bf8a48de140a3c666897588a05shiqian  // either a raw pointer or a smart pointer.
1617e35fdd936d133bf8a48de140a3c666897588a05shiqian  //
1618e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The reason we do this instead of relying on
1619e35fdd936d133bf8a48de140a3c666897588a05shiqian  // MakePolymorphicMatcher() is that the latter is not flexible
1620e35fdd936d133bf8a48de140a3c666897588a05shiqian  // enough for implementing the DescribeTo() method of Pointee().
1621e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename Pointer>
1622e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<Pointer>() const {
1623e35fdd936d133bf8a48de140a3c666897588a05shiqian    return MakeMatcher(new Impl<Pointer>(matcher_));
1624e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
162532de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1626e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1627e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The monomorphic implementation that works for a particular pointer type.
1628e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename Pointer>
1629e35fdd936d133bf8a48de140a3c666897588a05shiqian  class Impl : public MatcherInterface<Pointer> {
1630e35fdd936d133bf8a48de140a3c666897588a05shiqian   public:
163102f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan    typedef typename PointeeOf<GTEST_REMOVE_CONST_(  // NOLINT
163202f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan        GTEST_REMOVE_REFERENCE_(Pointer))>::type Pointee;
1633e35fdd936d133bf8a48de140a3c666897588a05shiqian
1634e35fdd936d133bf8a48de140a3c666897588a05shiqian    explicit Impl(const InnerMatcher& matcher)
1635e35fdd936d133bf8a48de140a3c666897588a05shiqian        : matcher_(MatcherCast<const Pointee&>(matcher)) {}
1636e35fdd936d133bf8a48de140a3c666897588a05shiqian
1637e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeTo(::std::ostream* os) const {
1638e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "points to a value that ";
1639e35fdd936d133bf8a48de140a3c666897588a05shiqian      matcher_.DescribeTo(os);
1640e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1641e35fdd936d133bf8a48de140a3c666897588a05shiqian
1642e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeNegationTo(::std::ostream* os) const {
1643e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "does not point to a value that ";
1644e35fdd936d133bf8a48de140a3c666897588a05shiqian      matcher_.DescribeTo(os);
1645e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1646e35fdd936d133bf8a48de140a3c666897588a05shiqian
1647821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    virtual bool MatchAndExplain(Pointer pointer,
1648821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan                                 MatchResultListener* listener) const {
1649e35fdd936d133bf8a48de140a3c666897588a05shiqian      if (GetRawPointer(pointer) == NULL)
1650821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan        return false;
1651e35fdd936d133bf8a48de140a3c666897588a05shiqian
1652676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      *listener << "which points to ";
1653676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      return MatchPrintAndExplain(*pointer, matcher_, listener);
1654e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
165532de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1656e35fdd936d133bf8a48de140a3c666897588a05shiqian   private:
1657e35fdd936d133bf8a48de140a3c666897588a05shiqian    const Matcher<const Pointee&> matcher_;
165832de5f53763125925e078498250f7e73a88de9edzhanyong.wan
165932de5f53763125925e078498250f7e73a88de9edzhanyong.wan    GTEST_DISALLOW_ASSIGN_(Impl);
1660e35fdd936d133bf8a48de140a3c666897588a05shiqian  };
1661e35fdd936d133bf8a48de140a3c666897588a05shiqian
1662e35fdd936d133bf8a48de140a3c666897588a05shiqian  const InnerMatcher matcher_;
166332de5f53763125925e078498250f7e73a88de9edzhanyong.wan
166432de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(PointeeMatcher);
1665e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1666e35fdd936d133bf8a48de140a3c666897588a05shiqian
1667e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the Field() matcher for matching a field (i.e. member
1668e35fdd936d133bf8a48de140a3c666897588a05shiqian// variable) of an object.
1669e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Class, typename FieldType>
1670e35fdd936d133bf8a48de140a3c666897588a05shiqianclass FieldMatcher {
1671e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1672e35fdd936d133bf8a48de140a3c666897588a05shiqian  FieldMatcher(FieldType Class::*field,
1673e35fdd936d133bf8a48de140a3c666897588a05shiqian               const Matcher<const FieldType&>& matcher)
1674e35fdd936d133bf8a48de140a3c666897588a05shiqian      : field_(field), matcher_(matcher) {}
1675e35fdd936d133bf8a48de140a3c666897588a05shiqian
1676e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
1677676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    *os << "is an object whose given field ";
1678e35fdd936d133bf8a48de140a3c666897588a05shiqian    matcher_.DescribeTo(os);
1679e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1680e35fdd936d133bf8a48de140a3c666897588a05shiqian
1681e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
1682676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    *os << "is an object whose given field ";
1683e35fdd936d133bf8a48de140a3c666897588a05shiqian    matcher_.DescribeNegationTo(os);
1684e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1685e35fdd936d133bf8a48de140a3c666897588a05shiqian
1686db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  template <typename T>
1687db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplain(const T& value, MatchResultListener* listener) const {
1688db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return MatchAndExplainImpl(
1689db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan        typename ::testing::internal::
169002f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan            is_pointer<GTEST_REMOVE_CONST_(T)>::type(),
1691db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan        value, listener);
1692db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  }
1693db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan
1694db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan private:
1695db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  // The first argument of MatchAndExplainImpl() is needed to help
169618490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  // Symbian's C++ compiler choose which overload to use.  Its type is
169718490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  // true_type iff the Field() matcher is used to match a pointer.
1698db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplainImpl(false_type /* is_not_pointer */, const Class& obj,
1699db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                           MatchResultListener* listener) const {
1700676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    *listener << "whose given field is ";
1701676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    return MatchPrintAndExplain(obj.*field_, matcher_, listener);
1702e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1703e35fdd936d133bf8a48de140a3c666897588a05shiqian
1704db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplainImpl(true_type /* is_pointer */, const Class* p,
1705db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                           MatchResultListener* listener) const {
1706821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (p == NULL)
1707821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      return false;
1708821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
1709676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    *listener << "which points to an object ";
1710821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // Since *p has a field, it must be a class/struct/union type and
1711821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // thus cannot be a pointer.  Therefore we pass false_type() as
1712821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // the first argument.
1713db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return MatchAndExplainImpl(false_type(), *p, listener);
1714e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
171532de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1716e35fdd936d133bf8a48de140a3c666897588a05shiqian  const FieldType Class::*field_;
1717e35fdd936d133bf8a48de140a3c666897588a05shiqian  const Matcher<const FieldType&> matcher_;
171832de5f53763125925e078498250f7e73a88de9edzhanyong.wan
171932de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(FieldMatcher);
1720e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1721e35fdd936d133bf8a48de140a3c666897588a05shiqian
1722e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the Property() matcher for matching a property
1723e35fdd936d133bf8a48de140a3c666897588a05shiqian// (i.e. return value of a getter method) of an object.
1724e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Class, typename PropertyType>
1725e35fdd936d133bf8a48de140a3c666897588a05shiqianclass PropertyMatcher {
1726e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1727e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The property may have a reference type, so 'const PropertyType&'
1728e35fdd936d133bf8a48de140a3c666897588a05shiqian  // may cause double references and fail to compile.  That's why we
172902f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan  // need GTEST_REFERENCE_TO_CONST, which works regardless of
1730e35fdd936d133bf8a48de140a3c666897588a05shiqian  // PropertyType being a reference or not.
173102f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan  typedef GTEST_REFERENCE_TO_CONST_(PropertyType) RefToConstProperty;
1732e35fdd936d133bf8a48de140a3c666897588a05shiqian
1733e35fdd936d133bf8a48de140a3c666897588a05shiqian  PropertyMatcher(PropertyType (Class::*property)() const,
1734e35fdd936d133bf8a48de140a3c666897588a05shiqian                  const Matcher<RefToConstProperty>& matcher)
1735e35fdd936d133bf8a48de140a3c666897588a05shiqian      : property_(property), matcher_(matcher) {}
1736e35fdd936d133bf8a48de140a3c666897588a05shiqian
1737e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
1738676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    *os << "is an object whose given property ";
1739e35fdd936d133bf8a48de140a3c666897588a05shiqian    matcher_.DescribeTo(os);
1740e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1741e35fdd936d133bf8a48de140a3c666897588a05shiqian
1742e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
1743676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    *os << "is an object whose given property ";
1744e35fdd936d133bf8a48de140a3c666897588a05shiqian    matcher_.DescribeNegationTo(os);
1745e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1746e35fdd936d133bf8a48de140a3c666897588a05shiqian
1747db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  template <typename T>
1748db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplain(const T&value, MatchResultListener* listener) const {
1749db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return MatchAndExplainImpl(
1750db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan        typename ::testing::internal::
175102f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan            is_pointer<GTEST_REMOVE_CONST_(T)>::type(),
1752db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan        value, listener);
1753db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  }
1754db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan
1755db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan private:
1756db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  // The first argument of MatchAndExplainImpl() is needed to help
175718490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  // Symbian's C++ compiler choose which overload to use.  Its type is
175818490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  // true_type iff the Property() matcher is used to match a pointer.
1759db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplainImpl(false_type /* is_not_pointer */, const Class& obj,
1760db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                           MatchResultListener* listener) const {
1761676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    *listener << "whose given property is ";
1762676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    // Cannot pass the return value (for example, int) to MatchPrintAndExplain,
1763676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    // which takes a non-const reference as argument.
1764676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    RefToConstProperty result = (obj.*property_)();
1765676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    return MatchPrintAndExplain(result, matcher_, listener);
1766e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1767e35fdd936d133bf8a48de140a3c666897588a05shiqian
1768db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplainImpl(true_type /* is_pointer */, const Class* p,
1769db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                           MatchResultListener* listener) const {
1770821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (p == NULL)
1771821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      return false;
1772821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
1773676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    *listener << "which points to an object ";
1774821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // Since *p has a property method, it must be a class/struct/union
1775821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // type and thus cannot be a pointer.  Therefore we pass
1776821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // false_type() as the first argument.
1777db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return MatchAndExplainImpl(false_type(), *p, listener);
1778e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
177932de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1780e35fdd936d133bf8a48de140a3c666897588a05shiqian  PropertyType (Class::*property_)() const;
1781e35fdd936d133bf8a48de140a3c666897588a05shiqian  const Matcher<RefToConstProperty> matcher_;
178232de5f53763125925e078498250f7e73a88de9edzhanyong.wan
178332de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(PropertyMatcher);
1784e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1785e35fdd936d133bf8a48de140a3c666897588a05shiqian
1786e35fdd936d133bf8a48de140a3c666897588a05shiqian// Type traits specifying various features of different functors for ResultOf.
1787e35fdd936d133bf8a48de140a3c666897588a05shiqian// The default template specifies features for functor objects.
1788e35fdd936d133bf8a48de140a3c666897588a05shiqian// Functor classes have to typedef argument_type and result_type
1789e35fdd936d133bf8a48de140a3c666897588a05shiqian// to be compatible with ResultOf.
1790e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Functor>
1791e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct CallableTraits {
1792e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef typename Functor::result_type ResultType;
1793e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef Functor StorageType;
1794e35fdd936d133bf8a48de140a3c666897588a05shiqian
179532de5f53763125925e078498250f7e73a88de9edzhanyong.wan  static void CheckIsValid(Functor /* functor */) {}
1796e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1797e35fdd936d133bf8a48de140a3c666897588a05shiqian  static ResultType Invoke(Functor f, T arg) { return f(arg); }
1798e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1799e35fdd936d133bf8a48de140a3c666897588a05shiqian
1800e35fdd936d133bf8a48de140a3c666897588a05shiqian// Specialization for function pointers.
1801e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename ArgType, typename ResType>
1802e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct CallableTraits<ResType(*)(ArgType)> {
1803e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef ResType ResultType;
1804e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef ResType(*StorageType)(ArgType);
1805e35fdd936d133bf8a48de140a3c666897588a05shiqian
1806e35fdd936d133bf8a48de140a3c666897588a05shiqian  static void CheckIsValid(ResType(*f)(ArgType)) {
1807f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    GTEST_CHECK_(f != NULL)
1808e35fdd936d133bf8a48de140a3c666897588a05shiqian        << "NULL function pointer is passed into ResultOf().";
1809e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1810e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1811e35fdd936d133bf8a48de140a3c666897588a05shiqian  static ResType Invoke(ResType(*f)(ArgType), T arg) {
1812e35fdd936d133bf8a48de140a3c666897588a05shiqian    return (*f)(arg);
1813e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1814e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1815e35fdd936d133bf8a48de140a3c666897588a05shiqian
1816e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the ResultOf() matcher for matching a return value of a
1817e35fdd936d133bf8a48de140a3c666897588a05shiqian// unary function of an object.
1818e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Callable>
1819e35fdd936d133bf8a48de140a3c666897588a05shiqianclass ResultOfMatcher {
1820e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1821e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef typename CallableTraits<Callable>::ResultType ResultType;
1822e35fdd936d133bf8a48de140a3c666897588a05shiqian
1823e35fdd936d133bf8a48de140a3c666897588a05shiqian  ResultOfMatcher(Callable callable, const Matcher<ResultType>& matcher)
1824e35fdd936d133bf8a48de140a3c666897588a05shiqian      : callable_(callable), matcher_(matcher) {
1825e35fdd936d133bf8a48de140a3c666897588a05shiqian    CallableTraits<Callable>::CheckIsValid(callable_);
1826e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1827e35fdd936d133bf8a48de140a3c666897588a05shiqian
1828e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1829e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<T>() const {
1830e35fdd936d133bf8a48de140a3c666897588a05shiqian    return Matcher<T>(new Impl<T>(callable_, matcher_));
1831e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1832e35fdd936d133bf8a48de140a3c666897588a05shiqian
1833e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1834e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef typename CallableTraits<Callable>::StorageType CallableStorageType;
1835e35fdd936d133bf8a48de140a3c666897588a05shiqian
1836e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1837e35fdd936d133bf8a48de140a3c666897588a05shiqian  class Impl : public MatcherInterface<T> {
1838e35fdd936d133bf8a48de140a3c666897588a05shiqian   public:
1839e35fdd936d133bf8a48de140a3c666897588a05shiqian    Impl(CallableStorageType callable, const Matcher<ResultType>& matcher)
1840e35fdd936d133bf8a48de140a3c666897588a05shiqian        : callable_(callable), matcher_(matcher) {}
1841e35fdd936d133bf8a48de140a3c666897588a05shiqian
1842e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeTo(::std::ostream* os) const {
1843676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      *os << "is mapped by the given callable to a value that ";
1844e35fdd936d133bf8a48de140a3c666897588a05shiqian      matcher_.DescribeTo(os);
1845e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1846e35fdd936d133bf8a48de140a3c666897588a05shiqian
1847e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeNegationTo(::std::ostream* os) const {
1848676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      *os << "is mapped by the given callable to a value that ";
1849e35fdd936d133bf8a48de140a3c666897588a05shiqian      matcher_.DescribeNegationTo(os);
1850e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1851e35fdd936d133bf8a48de140a3c666897588a05shiqian
1852821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    virtual bool MatchAndExplain(T obj, MatchResultListener* listener) const {
1853676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      *listener << "which is mapped by the given callable to ";
1854676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      // Cannot pass the return value (for example, int) to
1855676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      // MatchPrintAndExplain, which takes a non-const reference as argument.
1856676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      ResultType result =
1857676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan          CallableTraits<Callable>::template Invoke<T>(callable_, obj);
1858676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      return MatchPrintAndExplain(result, matcher_, listener);
1859e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
186032de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1861e35fdd936d133bf8a48de140a3c666897588a05shiqian   private:
1862e35fdd936d133bf8a48de140a3c666897588a05shiqian    // Functors often define operator() as non-const method even though
1863e35fdd936d133bf8a48de140a3c666897588a05shiqian    // they are actualy stateless. But we need to use them even when
1864e35fdd936d133bf8a48de140a3c666897588a05shiqian    // 'this' is a const pointer. It's the user's responsibility not to
1865e35fdd936d133bf8a48de140a3c666897588a05shiqian    // use stateful callables with ResultOf(), which does't guarantee
1866e35fdd936d133bf8a48de140a3c666897588a05shiqian    // how many times the callable will be invoked.
1867e35fdd936d133bf8a48de140a3c666897588a05shiqian    mutable CallableStorageType callable_;
1868e35fdd936d133bf8a48de140a3c666897588a05shiqian    const Matcher<ResultType> matcher_;
186932de5f53763125925e078498250f7e73a88de9edzhanyong.wan
187032de5f53763125925e078498250f7e73a88de9edzhanyong.wan    GTEST_DISALLOW_ASSIGN_(Impl);
1871e35fdd936d133bf8a48de140a3c666897588a05shiqian  };  // class Impl
1872e35fdd936d133bf8a48de140a3c666897588a05shiqian
1873e35fdd936d133bf8a48de140a3c666897588a05shiqian  const CallableStorageType callable_;
1874e35fdd936d133bf8a48de140a3c666897588a05shiqian  const Matcher<ResultType> matcher_;
187532de5f53763125925e078498250f7e73a88de9edzhanyong.wan
187632de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(ResultOfMatcher);
1877e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1878e35fdd936d133bf8a48de140a3c666897588a05shiqian
18796a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// Implements an equality matcher for any STL-style container whose elements
18806a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// support ==. This matcher is like Eq(), but its failure explanations provide
18816a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// more detailed information that is useful when the container is used as a set.
18826a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// The failure message reports elements that are in one of the operands but not
18836a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// the other. The failure messages do not report duplicate or out-of-order
18846a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// elements in the containers (which don't properly matter to sets, but can
18856a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// occur if the containers are vectors or lists, for example).
18866a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan//
18876a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// Uses the container's const_iterator, value_type, operator ==,
18886a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// begin(), and end().
18896a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wantemplate <typename Container>
18906a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wanclass ContainerEqMatcher {
18916a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan public:
1892b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef internal::StlContainerView<Container> View;
1893b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef typename View::type StlContainer;
1894b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef typename View::const_reference StlContainerReference;
1895b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
1896b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  // We make a copy of rhs in case the elements in it are modified
1897b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  // after this matcher is created.
1898b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  explicit ContainerEqMatcher(const Container& rhs) : rhs_(View::Copy(rhs)) {
1899b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    // Makes sure the user doesn't instantiate this class template
1900b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    // with a const or reference type.
1901ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    (void)testing::StaticAssertTypeEq<Container,
1902ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        GTEST_REMOVE_REFERENCE_AND_CONST_(Container)>();
1903b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  }
1904b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
19056a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan  void DescribeTo(::std::ostream* os) const {
19066a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan    *os << "equals ";
1907e2e8ba401d198d1a8304c652e997505609b62696vladlosev    UniversalPrint(rhs_, os);
19086a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan  }
19096a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan  void DescribeNegationTo(::std::ostream* os) const {
19106a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan    *os << "does not equal ";
1911e2e8ba401d198d1a8304c652e997505609b62696vladlosev    UniversalPrint(rhs_, os);
19126a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan  }
19136a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan
1914b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  template <typename LhsContainer>
1915e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan  bool MatchAndExplain(const LhsContainer& lhs,
1916e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan                       MatchResultListener* listener) const {
191702f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan    // GTEST_REMOVE_CONST_() is needed to work around an MSVC 8.0 bug
1918b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    // that causes LhsContainer to be a const type sometimes.
191902f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan    typedef internal::StlContainerView<GTEST_REMOVE_CONST_(LhsContainer)>
1920b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan        LhsView;
1921b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    typedef typename LhsView::type LhsStlContainer;
1922b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    StlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
1923e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan    if (lhs_stl_container == rhs_)
1924e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan      return true;
1925b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
1926e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan    ::std::ostream* const os = listener->stream();
1927e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan    if (os != NULL) {
1928b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan      // Something is different. Check for extra values first.
1929e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan      bool printed_header = false;
1930e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan      for (typename LhsStlContainer::const_iterator it =
1931e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan               lhs_stl_container.begin();
1932e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan           it != lhs_stl_container.end(); ++it) {
1933e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan        if (internal::ArrayAwareFind(rhs_.begin(), rhs_.end(), *it) ==
1934e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan            rhs_.end()) {
1935e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan          if (printed_header) {
1936e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan            *os << ", ";
1937e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan          } else {
1938b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan            *os << "which has these unexpected elements: ";
1939e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan            printed_header = true;
1940e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan          }
1941e2e8ba401d198d1a8304c652e997505609b62696vladlosev          UniversalPrint(*it, os);
19426a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan        }
19436a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan      }
19446a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan
1945b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan      // Now check for missing values.
1946e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan      bool printed_header2 = false;
1947e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan      for (typename StlContainer::const_iterator it = rhs_.begin();
1948e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan           it != rhs_.end(); ++it) {
1949e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan        if (internal::ArrayAwareFind(
1950e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan                lhs_stl_container.begin(), lhs_stl_container.end(), *it) ==
1951e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan            lhs_stl_container.end()) {
1952e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan          if (printed_header2) {
1953e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan            *os << ", ";
1954e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan          } else {
1955b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan            *os << (printed_header ? ",\nand" : "which")
1956b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan                << " doesn't have these expected elements: ";
1957e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan            printed_header2 = true;
1958e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan          }
1959e2e8ba401d198d1a8304c652e997505609b62696vladlosev          UniversalPrint(*it, os);
19606a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan        }
19616a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan      }
19626a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan    }
1963e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan
1964e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan    return false;
19656a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan  }
196632de5f53763125925e078498250f7e73a88de9edzhanyong.wan
19676a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan private:
1968b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  const StlContainer rhs_;
196932de5f53763125925e078498250f7e73a88de9edzhanyong.wan
197032de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(ContainerEqMatcher);
19716a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan};
19726a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan
1973ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// Implements Pointwise(tuple_matcher, rhs_container).  tuple_matcher
1974ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// must be able to be safely cast to Matcher<tuple<const T1&, const
1975ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// T2&> >, where T1 and T2 are the types of elements in the LHS
1976ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// container and the RHS container respectively.
1977ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wantemplate <typename TupleMatcher, typename RhsContainer>
1978ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wanclass PointwiseMatcher {
1979ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan public:
1980ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  typedef internal::StlContainerView<RhsContainer> RhsView;
1981ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  typedef typename RhsView::type RhsStlContainer;
1982ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  typedef typename RhsStlContainer::value_type RhsValue;
1983ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
1984ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  // Like ContainerEq, we make a copy of rhs in case the elements in
1985ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  // it are modified after this matcher is created.
1986ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  PointwiseMatcher(const TupleMatcher& tuple_matcher, const RhsContainer& rhs)
1987ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      : tuple_matcher_(tuple_matcher), rhs_(RhsView::Copy(rhs)) {
1988ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    // Makes sure the user doesn't instantiate this class template
1989ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    // with a const or reference type.
1990ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    (void)testing::StaticAssertTypeEq<RhsContainer,
1991ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        GTEST_REMOVE_REFERENCE_AND_CONST_(RhsContainer)>();
1992ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  }
1993ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
1994ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  template <typename LhsContainer>
1995ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  operator Matcher<LhsContainer>() const {
1996ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    return MakeMatcher(new Impl<LhsContainer>(tuple_matcher_, rhs_));
1997ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  }
1998ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
1999ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  template <typename LhsContainer>
2000ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  class Impl : public MatcherInterface<LhsContainer> {
2001ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan   public:
2002ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    typedef internal::StlContainerView<
2003ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan         GTEST_REMOVE_REFERENCE_AND_CONST_(LhsContainer)> LhsView;
2004ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    typedef typename LhsView::type LhsStlContainer;
2005ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    typedef typename LhsView::const_reference LhsStlContainerReference;
2006ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    typedef typename LhsStlContainer::value_type LhsValue;
2007ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    // We pass the LHS value and the RHS value to the inner matcher by
2008ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    // reference, as they may be expensive to copy.  We must use tuple
2009ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    // instead of pair here, as a pair cannot hold references (C++ 98,
2010ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    // 20.2.2 [lib.pairs]).
2011ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    typedef std::tr1::tuple<const LhsValue&, const RhsValue&> InnerMatcherArg;
2012ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2013ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    Impl(const TupleMatcher& tuple_matcher, const RhsStlContainer& rhs)
2014ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        // mono_tuple_matcher_ holds a monomorphic version of the tuple matcher.
2015ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        : mono_tuple_matcher_(SafeMatcherCast<InnerMatcherArg>(tuple_matcher)),
2016ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan          rhs_(rhs) {}
2017ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2018ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    virtual void DescribeTo(::std::ostream* os) const {
2019ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      *os << "contains " << rhs_.size()
2020ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan          << " values, where each value and its corresponding value in ";
2021ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      UniversalPrinter<RhsStlContainer>::Print(rhs_, os);
2022ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      *os << " ";
2023ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      mono_tuple_matcher_.DescribeTo(os);
2024ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    }
2025ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    virtual void DescribeNegationTo(::std::ostream* os) const {
2026ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      *os << "doesn't contain exactly " << rhs_.size()
2027ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan          << " values, or contains a value x at some index i"
2028ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan          << " where x and the i-th value of ";
2029ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      UniversalPrint(rhs_, os);
2030ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      *os << " ";
2031ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      mono_tuple_matcher_.DescribeNegationTo(os);
2032ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    }
2033ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2034ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    virtual bool MatchAndExplain(LhsContainer lhs,
2035ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan                                 MatchResultListener* listener) const {
2036ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2037ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      const size_t actual_size = lhs_stl_container.size();
2038ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      if (actual_size != rhs_.size()) {
2039ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        *listener << "which contains " << actual_size << " values";
2040ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        return false;
2041ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      }
2042ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2043ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      typename LhsStlContainer::const_iterator left = lhs_stl_container.begin();
2044ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      typename RhsStlContainer::const_iterator right = rhs_.begin();
2045ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      for (size_t i = 0; i != actual_size; ++i, ++left, ++right) {
2046ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        const InnerMatcherArg value_pair(*left, *right);
2047ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2048ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        if (listener->IsInterested()) {
2049ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan          StringMatchResultListener inner_listener;
2050ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan          if (!mono_tuple_matcher_.MatchAndExplain(
2051ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan                  value_pair, &inner_listener)) {
2052ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan            *listener << "where the value pair (";
2053ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan            UniversalPrint(*left, listener->stream());
2054ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan            *listener << ", ";
2055ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan            UniversalPrint(*right, listener->stream());
2056ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan            *listener << ") at index #" << i << " don't match";
2057ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan            PrintIfNotEmpty(inner_listener.str(), listener->stream());
2058ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan            return false;
2059ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan          }
2060ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        } else {
2061ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan          if (!mono_tuple_matcher_.Matches(value_pair))
2062ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan            return false;
2063ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        }
2064ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      }
2065ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2066ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      return true;
2067ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    }
2068ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2069ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan   private:
2070ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    const Matcher<InnerMatcherArg> mono_tuple_matcher_;
2071ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    const RhsStlContainer rhs_;
2072ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2073ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    GTEST_DISALLOW_ASSIGN_(Impl);
2074ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  };
2075ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2076ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan private:
2077ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  const TupleMatcher tuple_matcher_;
2078ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  const RhsStlContainer rhs_;
2079ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2080ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  GTEST_DISALLOW_ASSIGN_(PointwiseMatcher);
2081ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan};
2082ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
208333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Holds the logic common to ContainsMatcherImpl and EachMatcherImpl.
2084b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename Container>
208533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wanclass QuantifierMatcherImpl : public MatcherInterface<Container> {
2086b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan public:
2087ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
2088b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef StlContainerView<RawContainer> View;
2089b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef typename View::type StlContainer;
2090b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef typename View::const_reference StlContainerReference;
2091b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef typename StlContainer::value_type Element;
2092b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
2093b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  template <typename InnerMatcher>
209433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  explicit QuantifierMatcherImpl(InnerMatcher inner_matcher)
2095b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan      : inner_matcher_(
209633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan           testing::SafeMatcherCast<const Element&>(inner_matcher)) {}
209733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
209833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  // Checks whether:
209933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  // * All elements in the container match, if all_elements_should_match.
210033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  // * Any element in the container matches, if !all_elements_should_match.
210133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  bool MatchAndExplainImpl(bool all_elements_should_match,
210233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan                           Container container,
210333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan                           MatchResultListener* listener) const {
210433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    StlContainerReference stl_container = View::ConstReference(container);
210533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    size_t i = 0;
210633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    for (typename StlContainer::const_iterator it = stl_container.begin();
210733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan         it != stl_container.end(); ++it, ++i) {
210833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan      StringMatchResultListener inner_listener;
210933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan      const bool matches = inner_matcher_.MatchAndExplain(*it, &inner_listener);
211033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
211133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan      if (matches != all_elements_should_match) {
211233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan        *listener << "whose element #" << i
211333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan                  << (matches ? " matches" : " doesn't match");
211433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan        PrintIfNotEmpty(inner_listener.str(), listener->stream());
211533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan        return !all_elements_should_match;
211633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan      }
211733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    }
211833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    return all_elements_should_match;
211933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  }
212033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
212133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan protected:
212233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  const Matcher<const Element&> inner_matcher_;
212333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
212433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  GTEST_DISALLOW_ASSIGN_(QuantifierMatcherImpl);
212533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan};
212633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
212733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Implements Contains(element_matcher) for the given argument type Container.
212833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Symmetric to EachMatcherImpl.
212933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wantemplate <typename Container>
213033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wanclass ContainsMatcherImpl : public QuantifierMatcherImpl<Container> {
213133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan public:
213233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  template <typename InnerMatcher>
213333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  explicit ContainsMatcherImpl(InnerMatcher inner_matcher)
213433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan      : QuantifierMatcherImpl<Container>(inner_matcher) {}
2135b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
2136b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  // Describes what this matcher does.
2137b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
2138b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    *os << "contains at least one element that ";
213933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    this->inner_matcher_.DescribeTo(os);
2140b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  }
2141b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
2142b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
2143b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    *os << "doesn't contain any element that ";
214433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    this->inner_matcher_.DescribeTo(os);
2145b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  }
2146b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
2147821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  virtual bool MatchAndExplain(Container container,
2148821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan                               MatchResultListener* listener) const {
214933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    return this->MatchAndExplainImpl(false, container, listener);
2150b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  }
2151b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
2152b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan private:
215332de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(ContainsMatcherImpl);
2154b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan};
2155b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
215633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Implements Each(element_matcher) for the given argument type Container.
215733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Symmetric to ContainsMatcherImpl.
215833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wantemplate <typename Container>
215933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wanclass EachMatcherImpl : public QuantifierMatcherImpl<Container> {
216033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan public:
216133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  template <typename InnerMatcher>
216233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  explicit EachMatcherImpl(InnerMatcher inner_matcher)
216333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan      : QuantifierMatcherImpl<Container>(inner_matcher) {}
216433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
216533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  // Describes what this matcher does.
216633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
216733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    *os << "only contains elements that ";
216833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    this->inner_matcher_.DescribeTo(os);
216933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  }
217033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
217133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
217233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    *os << "contains some element that ";
217333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    this->inner_matcher_.DescribeNegationTo(os);
217433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  }
217533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
217633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  virtual bool MatchAndExplain(Container container,
217733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan                               MatchResultListener* listener) const {
217833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    return this->MatchAndExplainImpl(true, container, listener);
217933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  }
218033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
218133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan private:
218233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  GTEST_DISALLOW_ASSIGN_(EachMatcherImpl);
218333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan};
218433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
2185b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Implements polymorphic Contains(element_matcher).
2186b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename M>
2187b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanclass ContainsMatcher {
2188b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan public:
2189b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  explicit ContainsMatcher(M m) : inner_matcher_(m) {}
2190b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
2191b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  template <typename Container>
2192b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  operator Matcher<Container>() const {
2193b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    return MakeMatcher(new ContainsMatcherImpl<Container>(inner_matcher_));
2194b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  }
2195b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
2196b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan private:
2197b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  const M inner_matcher_;
219832de5f53763125925e078498250f7e73a88de9edzhanyong.wan
219932de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(ContainsMatcher);
2200b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan};
2201b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
220233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Implements polymorphic Each(element_matcher).
220333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wantemplate <typename M>
220433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wanclass EachMatcher {
220533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan public:
220633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  explicit EachMatcher(M m) : inner_matcher_(m) {}
220733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
220833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  template <typename Container>
220933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  operator Matcher<Container>() const {
221033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    return MakeMatcher(new EachMatcherImpl<Container>(inner_matcher_));
221133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  }
221233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
221333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan private:
221433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  const M inner_matcher_;
221533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
221633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  GTEST_DISALLOW_ASSIGN_(EachMatcher);
221733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan};
221833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
2219b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// Implements Key(inner_matcher) for the given argument pair type.
2220b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// Key(inner_matcher) matches an std::pair whose 'first' field matches
2221b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// inner_matcher.  For example, Contains(Key(Ge(5))) can be used to match an
2222b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// std::map that contains at least one element whose key is >= 5.
2223b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wantemplate <typename PairType>
2224b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wanclass KeyMatcherImpl : public MatcherInterface<PairType> {
2225b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan public:
2226ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  typedef GTEST_REMOVE_REFERENCE_AND_CONST_(PairType) RawPairType;
2227b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  typedef typename RawPairType::first_type KeyType;
2228b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
2229b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  template <typename InnerMatcher>
2230b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  explicit KeyMatcherImpl(InnerMatcher inner_matcher)
2231b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan      : inner_matcher_(
2232b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan          testing::SafeMatcherCast<const KeyType&>(inner_matcher)) {
2233b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  }
2234b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
2235b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  // Returns true iff 'key_value.first' (the key) matches the inner matcher.
2236821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  virtual bool MatchAndExplain(PairType key_value,
2237821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan                               MatchResultListener* listener) const {
2238b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    StringMatchResultListener inner_listener;
2239b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    const bool match = inner_matcher_.MatchAndExplain(key_value.first,
2240b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan                                                      &inner_listener);
2241b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    const internal::string explanation = inner_listener.str();
2242b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    if (explanation != "") {
2243b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan      *listener << "whose first field is a value " << explanation;
2244b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    }
2245b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    return match;
2246b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  }
2247b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
2248b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  // Describes what this matcher does.
2249b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
2250b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan    *os << "has a key that ";
2251b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan    inner_matcher_.DescribeTo(os);
2252b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  }
2253b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
2254b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  // Describes what the negation of this matcher does.
2255b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
2256b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan    *os << "doesn't have a key that ";
2257b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan    inner_matcher_.DescribeTo(os);
2258b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  }
2259b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
2260b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan private:
2261b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  const Matcher<const KeyType&> inner_matcher_;
226232de5f53763125925e078498250f7e73a88de9edzhanyong.wan
226332de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(KeyMatcherImpl);
2264b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan};
2265b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
2266b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// Implements polymorphic Key(matcher_for_key).
2267b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wantemplate <typename M>
2268b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wanclass KeyMatcher {
2269b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan public:
2270b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  explicit KeyMatcher(M m) : matcher_for_key_(m) {}
2271b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
2272b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  template <typename PairType>
2273b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  operator Matcher<PairType>() const {
2274b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan    return MakeMatcher(new KeyMatcherImpl<PairType>(matcher_for_key_));
2275b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  }
2276b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
2277b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan private:
2278b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  const M matcher_for_key_;
227932de5f53763125925e078498250f7e73a88de9edzhanyong.wan
228032de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(KeyMatcher);
2281b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan};
2282b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
2283f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// Implements Pair(first_matcher, second_matcher) for the given argument pair
2284f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// type with its two matchers. See Pair() function below.
2285f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wantemplate <typename PairType>
2286f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wanclass PairMatcherImpl : public MatcherInterface<PairType> {
2287f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan public:
2288ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  typedef GTEST_REMOVE_REFERENCE_AND_CONST_(PairType) RawPairType;
2289f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  typedef typename RawPairType::first_type FirstType;
2290f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  typedef typename RawPairType::second_type SecondType;
2291f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
2292f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  template <typename FirstMatcher, typename SecondMatcher>
2293f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  PairMatcherImpl(FirstMatcher first_matcher, SecondMatcher second_matcher)
2294f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan      : first_matcher_(
2295f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan            testing::SafeMatcherCast<const FirstType&>(first_matcher)),
2296f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan        second_matcher_(
2297f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan            testing::SafeMatcherCast<const SecondType&>(second_matcher)) {
2298f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  }
2299f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
2300f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  // Describes what this matcher does.
2301f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
2302f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    *os << "has a first field that ";
2303f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    first_matcher_.DescribeTo(os);
2304f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    *os << ", and has a second field that ";
2305f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    second_matcher_.DescribeTo(os);
2306f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  }
2307f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
2308f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  // Describes what the negation of this matcher does.
2309f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
2310f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    *os << "has a first field that ";
2311f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    first_matcher_.DescribeNegationTo(os);
2312f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    *os << ", or has a second field that ";
2313f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    second_matcher_.DescribeNegationTo(os);
2314f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  }
2315f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
2316821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // Returns true iff 'a_pair.first' matches first_matcher and 'a_pair.second'
2317821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // matches second_matcher.
2318821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  virtual bool MatchAndExplain(PairType a_pair,
2319821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan                               MatchResultListener* listener) const {
2320676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    if (!listener->IsInterested()) {
2321676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      // If the listener is not interested, we don't need to construct the
2322676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      // explanation.
2323676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      return first_matcher_.Matches(a_pair.first) &&
2324676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan             second_matcher_.Matches(a_pair.second);
2325821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    }
2326676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    StringMatchResultListener first_inner_listener;
2327676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    if (!first_matcher_.MatchAndExplain(a_pair.first,
2328676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan                                        &first_inner_listener)) {
2329676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      *listener << "whose first field does not match";
2330b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan      PrintIfNotEmpty(first_inner_listener.str(), listener->stream());
2331821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      return false;
2332f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    }
2333676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    StringMatchResultListener second_inner_listener;
2334676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    if (!second_matcher_.MatchAndExplain(a_pair.second,
2335676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan                                         &second_inner_listener)) {
2336676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      *listener << "whose second field does not match";
2337b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan      PrintIfNotEmpty(second_inner_listener.str(), listener->stream());
2338821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      return false;
2339f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    }
2340676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    ExplainSuccess(first_inner_listener.str(), second_inner_listener.str(),
2341676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan                   listener);
2342821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    return true;
2343f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  }
2344f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
2345f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan private:
2346676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan  void ExplainSuccess(const internal::string& first_explanation,
2347676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan                      const internal::string& second_explanation,
2348676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan                      MatchResultListener* listener) const {
2349676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    *listener << "whose both fields match";
2350676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    if (first_explanation != "") {
2351676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      *listener << ", where the first field is a value " << first_explanation;
2352676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    }
2353676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    if (second_explanation != "") {
2354676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      *listener << ", ";
2355676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      if (first_explanation != "") {
2356676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan        *listener << "and ";
2357676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      } else {
2358676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan        *listener << "where ";
2359676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      }
2360676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      *listener << "the second field is a value " << second_explanation;
2361676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    }
2362676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan  }
2363676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan
2364f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  const Matcher<const FirstType&> first_matcher_;
2365f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  const Matcher<const SecondType&> second_matcher_;
236632de5f53763125925e078498250f7e73a88de9edzhanyong.wan
236732de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(PairMatcherImpl);
2368f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan};
2369f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
2370f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// Implements polymorphic Pair(first_matcher, second_matcher).
2371f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wantemplate <typename FirstMatcher, typename SecondMatcher>
2372f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wanclass PairMatcher {
2373f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan public:
2374f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  PairMatcher(FirstMatcher first_matcher, SecondMatcher second_matcher)
2375f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan      : first_matcher_(first_matcher), second_matcher_(second_matcher) {}
2376f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
2377f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  template <typename PairType>
2378f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  operator Matcher<PairType> () const {
2379f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    return MakeMatcher(
2380f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan        new PairMatcherImpl<PairType>(
2381f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan            first_matcher_, second_matcher_));
2382f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  }
2383f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
2384f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan private:
2385f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  const FirstMatcher first_matcher_;
2386f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  const SecondMatcher second_matcher_;
238732de5f53763125925e078498250f7e73a88de9edzhanyong.wan
238832de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(PairMatcher);
2389f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan};
2390f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
23911afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// Implements ElementsAre() and ElementsAreArray().
23921afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wantemplate <typename Container>
23931afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wanclass ElementsAreMatcherImpl : public MatcherInterface<Container> {
23941afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan public:
2395ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
23961afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  typedef internal::StlContainerView<RawContainer> View;
23971afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  typedef typename View::type StlContainer;
23981afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  typedef typename View::const_reference StlContainerReference;
23991afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  typedef typename StlContainer::value_type Element;
24001afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
24011afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  // Constructs the matcher from a sequence of element values or
24021afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  // element matchers.
24031afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  template <typename InputIter>
240432de5f53763125925e078498250f7e73a88de9edzhanyong.wan  ElementsAreMatcherImpl(InputIter first, size_t a_count) {
240532de5f53763125925e078498250f7e73a88de9edzhanyong.wan    matchers_.reserve(a_count);
24061afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    InputIter it = first;
240732de5f53763125925e078498250f7e73a88de9edzhanyong.wan    for (size_t i = 0; i != a_count; ++i, ++it) {
24081afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      matchers_.push_back(MatcherCast<const Element&>(*it));
24091afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    }
24101afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  }
24111afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
24121afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  // Describes what this matcher does.
24131afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
24141afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    if (count() == 0) {
24151afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      *os << "is empty";
24161afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    } else if (count() == 1) {
24171afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      *os << "has 1 element that ";
24181afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      matchers_[0].DescribeTo(os);
24191afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    } else {
24201afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      *os << "has " << Elements(count()) << " where\n";
24211afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      for (size_t i = 0; i != count(); ++i) {
2422b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan        *os << "element #" << i << " ";
24231afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        matchers_[i].DescribeTo(os);
24241afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        if (i + 1 < count()) {
24251afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan          *os << ",\n";
24261afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        }
24271afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      }
24281afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    }
24291afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  }
24301afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
24311afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  // Describes what the negation of this matcher does.
24321afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
24331afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    if (count() == 0) {
2434b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan      *os << "isn't empty";
24351afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      return;
24361afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    }
24371afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
2438b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    *os << "doesn't have " << Elements(count()) << ", or\n";
24391afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    for (size_t i = 0; i != count(); ++i) {
2440b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan      *os << "element #" << i << " ";
24411afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      matchers_[i].DescribeNegationTo(os);
24421afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      if (i + 1 < count()) {
24431afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        *os << ", or\n";
24441afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      }
24451afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    }
24461afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  }
24471afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
2448821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  virtual bool MatchAndExplain(Container container,
2449821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan                               MatchResultListener* listener) const {
24501afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    StlContainerReference stl_container = View::ConstReference(container);
2451821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    const size_t actual_count = stl_container.size();
2452821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (actual_count != count()) {
2453821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      // The element count doesn't match.  If the container is empty,
2454821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      // there's no need to explain anything as Google Mock already
2455821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      // prints the empty container.  Otherwise we just need to show
2456821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      // how many elements there actually are.
2457821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      if (actual_count != 0) {
2458b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan        *listener << "which has " << Elements(actual_count);
24591afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      }
2460821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      return false;
2461821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    }
24621afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
2463821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    typename StlContainer::const_iterator it = stl_container.begin();
2464821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // explanations[i] is the explanation of the element at index i.
2465821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    std::vector<internal::string> explanations(count());
2466821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    for (size_t i = 0; i != count();  ++it, ++i) {
2467821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      StringMatchResultListener s;
2468821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      if (matchers_[i].MatchAndExplain(*it, &s)) {
2469821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan        explanations[i] = s.str();
2470821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      } else {
2471821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan        // The container has the right size but the i-th element
2472821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan        // doesn't match its expectation.
2473b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan        *listener << "whose element #" << i << " doesn't match";
2474b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan        PrintIfNotEmpty(s.str(), listener->stream());
2475821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan        return false;
2476821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      }
2477821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    }
24781afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
2479821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // Every element matches its expectation.  We need to explain why
2480821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // (the obvious ones can be skipped).
2481821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    bool reason_printed = false;
2482821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    for (size_t i = 0; i != count(); ++i) {
2483821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      const internal::string& s = explanations[i];
2484821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      if (!s.empty()) {
2485821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan        if (reason_printed) {
2486b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan          *listener << ",\nand ";
24871afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        }
2488b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan        *listener << "whose element #" << i << " matches, " << s;
2489821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan        reason_printed = true;
24901afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      }
24911afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    }
2492821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
2493821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    return true;
24941afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  }
24951afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
24961afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan private:
24971afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  static Message Elements(size_t count) {
24981afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    return Message() << count << (count == 1 ? " element" : " elements");
24991afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  }
25001afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
25011afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  size_t count() const { return matchers_.size(); }
25021afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  std::vector<Matcher<const Element&> > matchers_;
250332de5f53763125925e078498250f7e73a88de9edzhanyong.wan
250432de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(ElementsAreMatcherImpl);
25051afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan};
25061afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
25071afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// Implements ElementsAre() of 0 arguments.
25081afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wanclass ElementsAreMatcher0 {
25091afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan public:
25101afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  ElementsAreMatcher0() {}
25111afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
25121afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  template <typename Container>
25131afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  operator Matcher<Container>() const {
2514ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
25151afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    typedef typename internal::StlContainerView<RawContainer>::type::value_type
25161afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        Element;
25171afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
25181afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    const Matcher<const Element&>* const matchers = NULL;
25191afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    return MakeMatcher(new ElementsAreMatcherImpl<Container>(matchers, 0));
25201afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  }
25211afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan};
25221afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
25231afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// Implements ElementsAreArray().
25241afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wantemplate <typename T>
25251afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wanclass ElementsAreArrayMatcher {
25261afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan public:
25271afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  ElementsAreArrayMatcher(const T* first, size_t count) :
25281afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      first_(first), count_(count) {}
25291afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
25301afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  template <typename Container>
25311afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  operator Matcher<Container>() const {
2532ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
25331afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    typedef typename internal::StlContainerView<RawContainer>::type::value_type
25341afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        Element;
25351afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
25361afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    return MakeMatcher(new ElementsAreMatcherImpl<Container>(first_, count_));
25371afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  }
25381afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
25391afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan private:
25401afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  const T* const first_;
25411afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  const size_t count_;
254232de5f53763125925e078498250f7e73a88de9edzhanyong.wan
254332de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(ElementsAreArrayMatcher);
25441afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan};
25451afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
2546b4140808f98ff09c43ca1ddaa8ff13bc47cd0089zhanyong.wan// Returns the description for a matcher defined using the MATCHER*()
2547b4140808f98ff09c43ca1ddaa8ff13bc47cd0089zhanyong.wan// macro where the user-supplied description string is "", if
2548b4140808f98ff09c43ca1ddaa8ff13bc47cd0089zhanyong.wan// 'negation' is false; otherwise returns the description of the
2549b4140808f98ff09c43ca1ddaa8ff13bc47cd0089zhanyong.wan// negation of the matcher.  'param_values' contains a list of strings
2550b4140808f98ff09c43ca1ddaa8ff13bc47cd0089zhanyong.wan// that are the print-out of the matcher's parameters.
2551b4140808f98ff09c43ca1ddaa8ff13bc47cd0089zhanyong.wanstring FormatMatcherDescription(bool negation, const char* matcher_name,
2552b4140808f98ff09c43ca1ddaa8ff13bc47cd0089zhanyong.wan                                const Strings& param_values);
25531afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
2554e35fdd936d133bf8a48de140a3c666897588a05shiqian}  // namespace internal
2555e35fdd936d133bf8a48de140a3c666897588a05shiqian
2556e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements MatcherCast().
2557e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T, typename M>
2558e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<T> MatcherCast(M matcher) {
2559e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::MatcherCastImpl<T, M>::Cast(matcher);
2560e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2561e35fdd936d133bf8a48de140a3c666897588a05shiqian
2562e35fdd936d133bf8a48de140a3c666897588a05shiqian// _ is a matcher that matches anything of any type.
2563e35fdd936d133bf8a48de140a3c666897588a05shiqian//
2564e35fdd936d133bf8a48de140a3c666897588a05shiqian// This definition is fine as:
2565e35fdd936d133bf8a48de140a3c666897588a05shiqian//
2566e35fdd936d133bf8a48de140a3c666897588a05shiqian//   1. The C++ standard permits using the name _ in a namespace that
2567e35fdd936d133bf8a48de140a3c666897588a05shiqian//      is not the global namespace or ::std.
2568e35fdd936d133bf8a48de140a3c666897588a05shiqian//   2. The AnythingMatcher class has no data member or constructor,
2569e35fdd936d133bf8a48de140a3c666897588a05shiqian//      so it's OK to create global variables of this type.
2570e35fdd936d133bf8a48de140a3c666897588a05shiqian//   3. c-style has approved of using _ in this case.
2571e35fdd936d133bf8a48de140a3c666897588a05shiqianconst internal::AnythingMatcher _ = {};
2572e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any value of the given type T.
2573e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
2574e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<T> A() { return MakeMatcher(new internal::AnyMatcherImpl<T>()); }
2575e35fdd936d133bf8a48de140a3c666897588a05shiqian
2576e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any value of the given type T.
2577e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
2578e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<T> An() { return A<T>(); }
2579e35fdd936d133bf8a48de140a3c666897588a05shiqian
2580e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything equal to x.
2581e35fdd936d133bf8a48de140a3c666897588a05shiqian// Note: if the parameter of Eq() were declared as const T&, Eq("foo")
2582e35fdd936d133bf8a48de140a3c666897588a05shiqian// wouldn't compile.
2583e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
2584e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::EqMatcher<T> Eq(T x) { return internal::EqMatcher<T>(x); }
2585e35fdd936d133bf8a48de140a3c666897588a05shiqian
2586e35fdd936d133bf8a48de140a3c666897588a05shiqian// Constructs a Matcher<T> from a 'value' of type T.  The constructed
2587e35fdd936d133bf8a48de140a3c666897588a05shiqian// matcher matches any value that's equal to 'value'.
2588e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
2589e35fdd936d133bf8a48de140a3c666897588a05shiqianMatcher<T>::Matcher(T value) { *this = Eq(value); }
2590e35fdd936d133bf8a48de140a3c666897588a05shiqian
2591e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a monomorphic matcher that matches anything with type Lhs
2592e35fdd936d133bf8a48de140a3c666897588a05shiqian// and equal to rhs.  A user may need to use this instead of Eq(...)
2593e35fdd936d133bf8a48de140a3c666897588a05shiqian// in order to resolve an overloading ambiguity.
2594e35fdd936d133bf8a48de140a3c666897588a05shiqian//
2595e35fdd936d133bf8a48de140a3c666897588a05shiqian// TypedEq<T>(x) is just a convenient short-hand for Matcher<T>(Eq(x))
2596e35fdd936d133bf8a48de140a3c666897588a05shiqian// or Matcher<T>(x), but more readable than the latter.
2597e35fdd936d133bf8a48de140a3c666897588a05shiqian//
2598e35fdd936d133bf8a48de140a3c666897588a05shiqian// We could define similar monomorphic matchers for other comparison
2599e35fdd936d133bf8a48de140a3c666897588a05shiqian// operations (e.g. TypedLt, TypedGe, and etc), but decided not to do
2600e35fdd936d133bf8a48de140a3c666897588a05shiqian// it yet as those are used much less than Eq() in practice.  A user
2601e35fdd936d133bf8a48de140a3c666897588a05shiqian// can always write Matcher<T>(Lt(5)) to be explicit about the type,
2602e35fdd936d133bf8a48de140a3c666897588a05shiqian// for example.
2603e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Lhs, typename Rhs>
2604e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<Lhs> TypedEq(const Rhs& rhs) { return Eq(rhs); }
2605e35fdd936d133bf8a48de140a3c666897588a05shiqian
2606e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything >= x.
2607e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs>
2608e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::GeMatcher<Rhs> Ge(Rhs x) {
2609e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::GeMatcher<Rhs>(x);
2610e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2611e35fdd936d133bf8a48de140a3c666897588a05shiqian
2612e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything > x.
2613e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs>
2614e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::GtMatcher<Rhs> Gt(Rhs x) {
2615e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::GtMatcher<Rhs>(x);
2616e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2617e35fdd936d133bf8a48de140a3c666897588a05shiqian
2618e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything <= x.
2619e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs>
2620e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::LeMatcher<Rhs> Le(Rhs x) {
2621e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::LeMatcher<Rhs>(x);
2622e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2623e35fdd936d133bf8a48de140a3c666897588a05shiqian
2624e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything < x.
2625e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs>
2626e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::LtMatcher<Rhs> Lt(Rhs x) {
2627e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::LtMatcher<Rhs>(x);
2628e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2629e35fdd936d133bf8a48de140a3c666897588a05shiqian
2630e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything != x.
2631e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs>
2632e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::NeMatcher<Rhs> Ne(Rhs x) {
2633e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::NeMatcher<Rhs>(x);
2634e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2635e35fdd936d133bf8a48de140a3c666897588a05shiqian
26362d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan// Creates a polymorphic matcher that matches any NULL pointer.
26372d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.waninline PolymorphicMatcher<internal::IsNullMatcher > IsNull() {
26382d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan  return MakePolymorphicMatcher(internal::IsNullMatcher());
26392d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan}
26402d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan
2641e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches any non-NULL pointer.
2642e35fdd936d133bf8a48de140a3c666897588a05shiqian// This is convenient as Not(NULL) doesn't compile (the compiler
2643e35fdd936d133bf8a48de140a3c666897588a05shiqian// thinks that that expression is comparing a pointer with an integer).
2644e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::NotNullMatcher > NotNull() {
2645e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::NotNullMatcher());
2646e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2647e35fdd936d133bf8a48de140a3c666897588a05shiqian
2648e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches any argument that
2649e35fdd936d133bf8a48de140a3c666897588a05shiqian// references variable x.
2650e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
2651e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::RefMatcher<T&> Ref(T& x) {  // NOLINT
2652e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::RefMatcher<T&>(x);
2653e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2654e35fdd936d133bf8a48de140a3c666897588a05shiqian
2655e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any double argument approximately
2656e35fdd936d133bf8a48de140a3c666897588a05shiqian// equal to rhs, where two NANs are considered unequal.
2657e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::FloatingEqMatcher<double> DoubleEq(double rhs) {
2658e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::FloatingEqMatcher<double>(rhs, false);
2659e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2660e35fdd936d133bf8a48de140a3c666897588a05shiqian
2661e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any double argument approximately
2662e35fdd936d133bf8a48de140a3c666897588a05shiqian// equal to rhs, including NaN values when rhs is NaN.
2663e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::FloatingEqMatcher<double> NanSensitiveDoubleEq(double rhs) {
2664e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::FloatingEqMatcher<double>(rhs, true);
2665e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2666e35fdd936d133bf8a48de140a3c666897588a05shiqian
2667e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any float argument approximately
2668e35fdd936d133bf8a48de140a3c666897588a05shiqian// equal to rhs, where two NANs are considered unequal.
2669e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::FloatingEqMatcher<float> FloatEq(float rhs) {
2670e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::FloatingEqMatcher<float>(rhs, false);
2671e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2672e35fdd936d133bf8a48de140a3c666897588a05shiqian
2673e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any double argument approximately
2674e35fdd936d133bf8a48de140a3c666897588a05shiqian// equal to rhs, including NaN values when rhs is NaN.
2675e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::FloatingEqMatcher<float> NanSensitiveFloatEq(float rhs) {
2676e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::FloatingEqMatcher<float>(rhs, true);
2677e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2678e35fdd936d133bf8a48de140a3c666897588a05shiqian
2679e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches a pointer (raw or smart) that points
2680e35fdd936d133bf8a48de140a3c666897588a05shiqian// to a value that matches inner_matcher.
2681e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename InnerMatcher>
2682e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::PointeeMatcher<InnerMatcher> Pointee(
2683e35fdd936d133bf8a48de140a3c666897588a05shiqian    const InnerMatcher& inner_matcher) {
2684e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::PointeeMatcher<InnerMatcher>(inner_matcher);
2685e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2686e35fdd936d133bf8a48de140a3c666897588a05shiqian
2687e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches an object whose given field matches
2688e35fdd936d133bf8a48de140a3c666897588a05shiqian// 'matcher'.  For example,
2689e35fdd936d133bf8a48de140a3c666897588a05shiqian//   Field(&Foo::number, Ge(5))
2690e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a Foo object x iff x.number >= 5.
2691e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Class, typename FieldType, typename FieldMatcher>
2692e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<
2693e35fdd936d133bf8a48de140a3c666897588a05shiqian  internal::FieldMatcher<Class, FieldType> > Field(
2694e35fdd936d133bf8a48de140a3c666897588a05shiqian    FieldType Class::*field, const FieldMatcher& matcher) {
2695e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(
2696e35fdd936d133bf8a48de140a3c666897588a05shiqian      internal::FieldMatcher<Class, FieldType>(
2697e35fdd936d133bf8a48de140a3c666897588a05shiqian          field, MatcherCast<const FieldType&>(matcher)));
2698e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The call to MatcherCast() is required for supporting inner
2699e35fdd936d133bf8a48de140a3c666897588a05shiqian  // matchers of compatible types.  For example, it allows
2700e35fdd936d133bf8a48de140a3c666897588a05shiqian  //   Field(&Foo::bar, m)
2701e35fdd936d133bf8a48de140a3c666897588a05shiqian  // to compile where bar is an int32 and m is a matcher for int64.
2702e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2703e35fdd936d133bf8a48de140a3c666897588a05shiqian
2704e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches an object whose given property
2705e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches 'matcher'.  For example,
2706e35fdd936d133bf8a48de140a3c666897588a05shiqian//   Property(&Foo::str, StartsWith("hi"))
2707e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a Foo object x iff x.str() starts with "hi".
2708e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Class, typename PropertyType, typename PropertyMatcher>
2709e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<
2710e35fdd936d133bf8a48de140a3c666897588a05shiqian  internal::PropertyMatcher<Class, PropertyType> > Property(
2711e35fdd936d133bf8a48de140a3c666897588a05shiqian    PropertyType (Class::*property)() const, const PropertyMatcher& matcher) {
2712e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(
2713e35fdd936d133bf8a48de140a3c666897588a05shiqian      internal::PropertyMatcher<Class, PropertyType>(
2714e35fdd936d133bf8a48de140a3c666897588a05shiqian          property,
271502f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan          MatcherCast<GTEST_REFERENCE_TO_CONST_(PropertyType)>(matcher)));
2716e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The call to MatcherCast() is required for supporting inner
2717e35fdd936d133bf8a48de140a3c666897588a05shiqian  // matchers of compatible types.  For example, it allows
2718e35fdd936d133bf8a48de140a3c666897588a05shiqian  //   Property(&Foo::bar, m)
2719e35fdd936d133bf8a48de140a3c666897588a05shiqian  // to compile where bar() returns an int32 and m is a matcher for int64.
2720e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2721e35fdd936d133bf8a48de140a3c666897588a05shiqian
2722e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches an object iff the result of applying
2723e35fdd936d133bf8a48de140a3c666897588a05shiqian// a callable to x matches 'matcher'.
2724e35fdd936d133bf8a48de140a3c666897588a05shiqian// For example,
2725e35fdd936d133bf8a48de140a3c666897588a05shiqian//   ResultOf(f, StartsWith("hi"))
2726e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a Foo object x iff f(x) starts with "hi".
2727e35fdd936d133bf8a48de140a3c666897588a05shiqian// callable parameter can be a function, function pointer, or a functor.
2728e35fdd936d133bf8a48de140a3c666897588a05shiqian// Callable has to satisfy the following conditions:
2729e35fdd936d133bf8a48de140a3c666897588a05shiqian//   * It is required to keep no state affecting the results of
2730e35fdd936d133bf8a48de140a3c666897588a05shiqian//     the calls on it and make no assumptions about how many calls
2731e35fdd936d133bf8a48de140a3c666897588a05shiqian//     will be made. Any state it keeps must be protected from the
2732e35fdd936d133bf8a48de140a3c666897588a05shiqian//     concurrent access.
2733e35fdd936d133bf8a48de140a3c666897588a05shiqian//   * If it is a function object, it has to define type result_type.
2734e35fdd936d133bf8a48de140a3c666897588a05shiqian//     We recommend deriving your functor classes from std::unary_function.
2735e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Callable, typename ResultOfMatcher>
2736e35fdd936d133bf8a48de140a3c666897588a05shiqianinternal::ResultOfMatcher<Callable> ResultOf(
2737e35fdd936d133bf8a48de140a3c666897588a05shiqian    Callable callable, const ResultOfMatcher& matcher) {
2738e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::ResultOfMatcher<Callable>(
2739e35fdd936d133bf8a48de140a3c666897588a05shiqian          callable,
2740e35fdd936d133bf8a48de140a3c666897588a05shiqian          MatcherCast<typename internal::CallableTraits<Callable>::ResultType>(
2741e35fdd936d133bf8a48de140a3c666897588a05shiqian              matcher));
2742e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The call to MatcherCast() is required for supporting inner
2743e35fdd936d133bf8a48de140a3c666897588a05shiqian  // matchers of compatible types.  For example, it allows
2744e35fdd936d133bf8a48de140a3c666897588a05shiqian  //   ResultOf(Function, m)
2745e35fdd936d133bf8a48de140a3c666897588a05shiqian  // to compile where Function() returns an int32 and m is a matcher for int64.
2746e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2747e35fdd936d133bf8a48de140a3c666897588a05shiqian
2748e35fdd936d133bf8a48de140a3c666897588a05shiqian// String matchers.
2749e35fdd936d133bf8a48de140a3c666897588a05shiqian
2750e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string equal to str.
2751e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> >
2752e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrEq(const internal::string& str) {
2753e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>(
2754e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, true, true));
2755e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2756e35fdd936d133bf8a48de140a3c666897588a05shiqian
2757e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string not equal to str.
2758e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> >
2759e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrNe(const internal::string& str) {
2760e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>(
2761e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, false, true));
2762e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2763e35fdd936d133bf8a48de140a3c666897588a05shiqian
2764e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string equal to str, ignoring case.
2765e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> >
2766e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrCaseEq(const internal::string& str) {
2767e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>(
2768e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, true, false));
2769e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2770e35fdd936d133bf8a48de140a3c666897588a05shiqian
2771e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string not equal to str, ignoring case.
2772e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> >
2773e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrCaseNe(const internal::string& str) {
2774e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>(
2775e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, false, false));
2776e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2777e35fdd936d133bf8a48de140a3c666897588a05shiqian
2778e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any string, std::string, or C string
2779e35fdd936d133bf8a48de140a3c666897588a05shiqian// that contains the given substring.
2780e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::HasSubstrMatcher<internal::string> >
2781e35fdd936d133bf8a48de140a3c666897588a05shiqian    HasSubstr(const internal::string& substring) {
2782e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::HasSubstrMatcher<internal::string>(
2783e35fdd936d133bf8a48de140a3c666897588a05shiqian      substring));
2784e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2785e35fdd936d133bf8a48de140a3c666897588a05shiqian
2786e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that starts with 'prefix' (case-sensitive).
2787e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StartsWithMatcher<internal::string> >
2788e35fdd936d133bf8a48de140a3c666897588a05shiqian    StartsWith(const internal::string& prefix) {
2789e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StartsWithMatcher<internal::string>(
2790e35fdd936d133bf8a48de140a3c666897588a05shiqian      prefix));
2791e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2792e35fdd936d133bf8a48de140a3c666897588a05shiqian
2793e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that ends with 'suffix' (case-sensitive).
2794e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::EndsWithMatcher<internal::string> >
2795e35fdd936d133bf8a48de140a3c666897588a05shiqian    EndsWith(const internal::string& suffix) {
2796e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::EndsWithMatcher<internal::string>(
2797e35fdd936d133bf8a48de140a3c666897588a05shiqian      suffix));
2798e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2799e35fdd936d133bf8a48de140a3c666897588a05shiqian
2800e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that fully matches regular expression 'regex'.
2801e35fdd936d133bf8a48de140a3c666897588a05shiqian// The matcher takes ownership of 'regex'.
2802e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex(
2803e35fdd936d133bf8a48de140a3c666897588a05shiqian    const internal::RE* regex) {
2804e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, true));
2805e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2806e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex(
2807e35fdd936d133bf8a48de140a3c666897588a05shiqian    const internal::string& regex) {
2808e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MatchesRegex(new internal::RE(regex));
2809e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2810e35fdd936d133bf8a48de140a3c666897588a05shiqian
2811e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that contains regular expression 'regex'.
2812e35fdd936d133bf8a48de140a3c666897588a05shiqian// The matcher takes ownership of 'regex'.
2813e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex(
2814e35fdd936d133bf8a48de140a3c666897588a05shiqian    const internal::RE* regex) {
2815e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, false));
2816e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2817e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex(
2818e35fdd936d133bf8a48de140a3c666897588a05shiqian    const internal::string& regex) {
2819e35fdd936d133bf8a48de140a3c666897588a05shiqian  return ContainsRegex(new internal::RE(regex));
2820e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2821e35fdd936d133bf8a48de140a3c666897588a05shiqian
2822e35fdd936d133bf8a48de140a3c666897588a05shiqian#if GTEST_HAS_GLOBAL_WSTRING || GTEST_HAS_STD_WSTRING
2823e35fdd936d133bf8a48de140a3c666897588a05shiqian// Wide string matchers.
2824e35fdd936d133bf8a48de140a3c666897588a05shiqian
2825e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string equal to str.
2826e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> >
2827e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrEq(const internal::wstring& str) {
2828e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>(
2829e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, true, true));
2830e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2831e35fdd936d133bf8a48de140a3c666897588a05shiqian
2832e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string not equal to str.
2833e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> >
2834e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrNe(const internal::wstring& str) {
2835e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>(
2836e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, false, true));
2837e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2838e35fdd936d133bf8a48de140a3c666897588a05shiqian
2839e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string equal to str, ignoring case.
2840e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> >
2841e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrCaseEq(const internal::wstring& str) {
2842e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>(
2843e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, true, false));
2844e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2845e35fdd936d133bf8a48de140a3c666897588a05shiqian
2846e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string not equal to str, ignoring case.
2847e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> >
2848e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrCaseNe(const internal::wstring& str) {
2849e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>(
2850e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, false, false));
2851e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2852e35fdd936d133bf8a48de140a3c666897588a05shiqian
2853e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any wstring, std::wstring, or C wide string
2854e35fdd936d133bf8a48de140a3c666897588a05shiqian// that contains the given substring.
2855e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::HasSubstrMatcher<internal::wstring> >
2856e35fdd936d133bf8a48de140a3c666897588a05shiqian    HasSubstr(const internal::wstring& substring) {
2857e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::HasSubstrMatcher<internal::wstring>(
2858e35fdd936d133bf8a48de140a3c666897588a05shiqian      substring));
2859e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2860e35fdd936d133bf8a48de140a3c666897588a05shiqian
2861e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that starts with 'prefix' (case-sensitive).
2862e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StartsWithMatcher<internal::wstring> >
2863e35fdd936d133bf8a48de140a3c666897588a05shiqian    StartsWith(const internal::wstring& prefix) {
2864e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StartsWithMatcher<internal::wstring>(
2865e35fdd936d133bf8a48de140a3c666897588a05shiqian      prefix));
2866e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2867e35fdd936d133bf8a48de140a3c666897588a05shiqian
2868e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that ends with 'suffix' (case-sensitive).
2869e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::EndsWithMatcher<internal::wstring> >
2870e35fdd936d133bf8a48de140a3c666897588a05shiqian    EndsWith(const internal::wstring& suffix) {
2871e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::EndsWithMatcher<internal::wstring>(
2872e35fdd936d133bf8a48de140a3c666897588a05shiqian      suffix));
2873e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2874e35fdd936d133bf8a48de140a3c666897588a05shiqian
2875e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif  // GTEST_HAS_GLOBAL_WSTRING || GTEST_HAS_STD_WSTRING
2876e35fdd936d133bf8a48de140a3c666897588a05shiqian
2877e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the
2878e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field == the second field.
2879e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Eq2Matcher Eq() { return internal::Eq2Matcher(); }
2880e35fdd936d133bf8a48de140a3c666897588a05shiqian
2881e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the
2882e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field >= the second field.
2883e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Ge2Matcher Ge() { return internal::Ge2Matcher(); }
2884e35fdd936d133bf8a48de140a3c666897588a05shiqian
2885e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the
2886e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field > the second field.
2887e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Gt2Matcher Gt() { return internal::Gt2Matcher(); }
2888e35fdd936d133bf8a48de140a3c666897588a05shiqian
2889e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the
2890e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field <= the second field.
2891e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Le2Matcher Le() { return internal::Le2Matcher(); }
2892e35fdd936d133bf8a48de140a3c666897588a05shiqian
2893e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the
2894e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field < the second field.
2895e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Lt2Matcher Lt() { return internal::Lt2Matcher(); }
2896e35fdd936d133bf8a48de140a3c666897588a05shiqian
2897e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the
2898e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field != the second field.
2899e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Ne2Matcher Ne() { return internal::Ne2Matcher(); }
2900e35fdd936d133bf8a48de140a3c666897588a05shiqian
2901e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any value of type T that m doesn't
2902e35fdd936d133bf8a48de140a3c666897588a05shiqian// match.
2903e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename InnerMatcher>
2904e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::NotMatcher<InnerMatcher> Not(InnerMatcher m) {
2905e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::NotMatcher<InnerMatcher>(m);
2906e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2907e35fdd936d133bf8a48de140a3c666897588a05shiqian
2908e35fdd936d133bf8a48de140a3c666897588a05shiqian// Returns a matcher that matches anything that satisfies the given
2909e35fdd936d133bf8a48de140a3c666897588a05shiqian// predicate.  The predicate can be any unary function or functor
2910e35fdd936d133bf8a48de140a3c666897588a05shiqian// whose return type can be implicitly converted to bool.
2911e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Predicate>
2912e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::TrulyMatcher<Predicate> >
2913e35fdd936d133bf8a48de140a3c666897588a05shiqianTruly(Predicate pred) {
2914e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::TrulyMatcher<Predicate>(pred));
2915e35fdd936d133bf8a48de140a3c666897588a05shiqian}
2916e35fdd936d133bf8a48de140a3c666897588a05shiqian
29176a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// Returns a matcher that matches an equal container.
29186a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// This matcher behaves like Eq(), but in the event of mismatch lists the
29196a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// values that are included in one container but not the other. (Duplicate
29206a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// values and order differences are not explained.)
29216a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wantemplate <typename Container>
2922821133180ccdb9eb062548d964929ba3b354eb84zhanyong.waninline PolymorphicMatcher<internal::ContainerEqMatcher<  // NOLINT
292302f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan                            GTEST_REMOVE_CONST_(Container)> >
29246a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan    ContainerEq(const Container& rhs) {
2925b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  // This following line is for working around a bug in MSVC 8.0,
2926b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  // which causes Container to be a const type sometimes.
292702f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan  typedef GTEST_REMOVE_CONST_(Container) RawContainer;
2928821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  return MakePolymorphicMatcher(
2929821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      internal::ContainerEqMatcher<RawContainer>(rhs));
2930b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}
2931b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
2932ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// Matches an STL-style container or a native array that contains the
2933ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// same number of elements as in rhs, where its i-th element and rhs's
2934ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// i-th element (as a pair) satisfy the given pair matcher, for all i.
2935ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// TupleMatcher must be able to be safely cast to Matcher<tuple<const
2936ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// T1&, const T2&> >, where T1 and T2 are the types of elements in the
2937ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// LHS container and the RHS container respectively.
2938ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wantemplate <typename TupleMatcher, typename Container>
2939ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.waninline internal::PointwiseMatcher<TupleMatcher,
2940ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan                                  GTEST_REMOVE_CONST_(Container)>
2941ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wanPointwise(const TupleMatcher& tuple_matcher, const Container& rhs) {
2942ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  // This following line is for working around a bug in MSVC 8.0,
2943ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  // which causes Container to be a const type sometimes.
2944ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  typedef GTEST_REMOVE_CONST_(Container) RawContainer;
2945ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  return internal::PointwiseMatcher<TupleMatcher, RawContainer>(
2946ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      tuple_matcher, rhs);
2947ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan}
2948ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2949b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Matches an STL-style container or a native array that contains at
2950b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// least one element matching the given value or matcher.
2951b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//
2952b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Examples:
2953b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   ::std::set<int> page_ids;
2954b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   page_ids.insert(3);
2955b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   page_ids.insert(1);
2956b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   EXPECT_THAT(page_ids, Contains(1));
2957b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   EXPECT_THAT(page_ids, Contains(Gt(2)));
2958b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   EXPECT_THAT(page_ids, Not(Contains(4)));
2959b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//
2960b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   ::std::map<int, size_t> page_lengths;
2961b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   page_lengths[1] = 100;
29624019819dbb5369b1d638503523a04aaf7eb2a5adzhanyong.wan//   EXPECT_THAT(page_lengths,
29634019819dbb5369b1d638503523a04aaf7eb2a5adzhanyong.wan//               Contains(::std::pair<const int, size_t>(1, 100)));
2964b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//
2965b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   const char* user_ids[] = { "joe", "mike", "tom" };
2966b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   EXPECT_THAT(user_ids, Contains(Eq(::std::string("tom"))));
2967b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename M>
2968b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.waninline internal::ContainsMatcher<M> Contains(M matcher) {
2969b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  return internal::ContainsMatcher<M>(matcher);
29706a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan}
29716a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan
297233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Matches an STL-style container or a native array that contains only
297333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// elements matching the given value or matcher.
297433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//
297533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Each(m) is semantically equivalent to Not(Contains(Not(m))). Only
297633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// the messages are different.
297733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//
297833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Examples:
297933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   ::std::set<int> page_ids;
298033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   // Each(m) matches an empty container, regardless of what m is.
298133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   EXPECT_THAT(page_ids, Each(Eq(1)));
298233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   EXPECT_THAT(page_ids, Each(Eq(77)));
298333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//
298433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   page_ids.insert(3);
298533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   EXPECT_THAT(page_ids, Each(Gt(0)));
298633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   EXPECT_THAT(page_ids, Not(Each(Gt(4))));
298733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   page_ids.insert(1);
298833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   EXPECT_THAT(page_ids, Not(Each(Lt(2))));
298933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//
299033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   ::std::map<int, size_t> page_lengths;
299133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   page_lengths[1] = 100;
299233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   page_lengths[2] = 200;
299333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   page_lengths[3] = 300;
299433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   EXPECT_THAT(page_lengths, Not(Each(Pair(1, 100))));
299533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   EXPECT_THAT(page_lengths, Each(Key(Le(3))));
299633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//
299733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   const char* user_ids[] = { "joe", "mike", "tom" };
299833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   EXPECT_THAT(user_ids, Not(Each(Eq(::std::string("tom")))));
299933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wantemplate <typename M>
300033605ba45417979cff7254c1196dfee65a7275b3zhanyong.waninline internal::EachMatcher<M> Each(M matcher) {
300133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  return internal::EachMatcher<M>(matcher);
300233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan}
300333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
3004b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// Key(inner_matcher) matches an std::pair whose 'first' field matches
3005b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// inner_matcher.  For example, Contains(Key(Ge(5))) can be used to match an
3006b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// std::map that contains at least one element whose key is >= 5.
3007b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wantemplate <typename M>
3008b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.waninline internal::KeyMatcher<M> Key(M inner_matcher) {
3009b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  return internal::KeyMatcher<M>(inner_matcher);
3010b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan}
3011b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
3012f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// Pair(first_matcher, second_matcher) matches a std::pair whose 'first' field
3013f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// matches first_matcher and whose 'second' field matches second_matcher.  For
3014f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// example, EXPECT_THAT(map_type, ElementsAre(Pair(Ge(5), "foo"))) can be used
3015f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// to match a std::map<int, string> that contains exactly one element whose key
3016f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// is >= 5 and whose value equals "foo".
3017f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wantemplate <typename FirstMatcher, typename SecondMatcher>
3018f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.waninline internal::PairMatcher<FirstMatcher, SecondMatcher>
3019f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wanPair(FirstMatcher first_matcher, SecondMatcher second_matcher) {
3020f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  return internal::PairMatcher<FirstMatcher, SecondMatcher>(
3021f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan      first_matcher, second_matcher);
3022f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan}
3023f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
3024e35fdd936d133bf8a48de140a3c666897588a05shiqian// Returns a predicate that is satisfied by anything that matches the
3025e35fdd936d133bf8a48de140a3c666897588a05shiqian// given matcher.
3026e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename M>
3027e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::MatcherAsPredicate<M> Matches(M matcher) {
3028e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::MatcherAsPredicate<M>(matcher);
3029e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3030e35fdd936d133bf8a48de140a3c666897588a05shiqian
3031b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Returns true iff the value matches the matcher.
3032b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename T, typename M>
3033b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.waninline bool Value(const T& value, M matcher) {
3034b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  return testing::Matches(matcher)(value);
3035b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}
3036b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
303734b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wan// Matches the value against the given matcher and explains the match
303834b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wan// result to listener.
303934b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wantemplate <typename T, typename M>
3040a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.waninline bool ExplainMatchResult(
304134b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wan    M matcher, const T& value, MatchResultListener* listener) {
304234b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wan  return SafeMatcherCast<const T&>(matcher).MatchAndExplain(value, listener);
304334b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wan}
304434b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wan
3045bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan// AllArgs(m) is a synonym of m.  This is useful in
3046bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan//
3047bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan//   EXPECT_CALL(foo, Bar(_, _)).With(AllArgs(Eq()));
3048bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan//
3049bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan// which is easier to read than
3050bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan//
3051bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan//   EXPECT_CALL(foo, Bar(_, _)).With(Eq());
3052bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wantemplate <typename InnerMatcher>
3053bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.waninline InnerMatcher AllArgs(const InnerMatcher& matcher) { return matcher; }
3054bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan
3055e35fdd936d133bf8a48de140a3c666897588a05shiqian// These macros allow using matchers to check values in Google Test
3056e35fdd936d133bf8a48de140a3c666897588a05shiqian// tests.  ASSERT_THAT(value, matcher) and EXPECT_THAT(value, matcher)
3057e35fdd936d133bf8a48de140a3c666897588a05shiqian// succeed iff the value matches the matcher.  If the assertion fails,
3058e35fdd936d133bf8a48de140a3c666897588a05shiqian// the value and the description of the matcher will be printed.
3059e35fdd936d133bf8a48de140a3c666897588a05shiqian#define ASSERT_THAT(value, matcher) ASSERT_PRED_FORMAT1(\
3060e35fdd936d133bf8a48de140a3c666897588a05shiqian    ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
3061e35fdd936d133bf8a48de140a3c666897588a05shiqian#define EXPECT_THAT(value, matcher) EXPECT_PRED_FORMAT1(\
3062e35fdd936d133bf8a48de140a3c666897588a05shiqian    ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
3063e35fdd936d133bf8a48de140a3c666897588a05shiqian
3064e35fdd936d133bf8a48de140a3c666897588a05shiqian}  // namespace testing
3065e35fdd936d133bf8a48de140a3c666897588a05shiqian
3066e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif  // GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_
3067