gmock-matchers.h revision 616180e6847dd3cc6517a59ff2d24e9fd5c8052c
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
41616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan#include <math.h>
426a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan#include <algorithm>
4316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#include <limits>
44e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <ostream>  // NOLINT
45e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <sstream>
46e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <string>
47ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan#include <utility>
48e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <vector>
49e35fdd936d133bf8a48de140a3c666897588a05shiqian
5053e08c44dd34857ba57581d7c5774d6c96a8d0e1zhanyong.wan#include "gmock/internal/gmock-internal-utils.h"
5153e08c44dd34857ba57581d7c5774d6c96a8d0e1zhanyong.wan#include "gmock/internal/gmock-port.h"
5253e08c44dd34857ba57581d7c5774d6c96a8d0e1zhanyong.wan#include "gtest/gtest.h"
53e35fdd936d133bf8a48de140a3c666897588a05shiqian
54e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace testing {
55e35fdd936d133bf8a48de140a3c666897588a05shiqian
56e35fdd936d133bf8a48de140a3c666897588a05shiqian// To implement a matcher Foo for type T, define:
57e35fdd936d133bf8a48de140a3c666897588a05shiqian//   1. a class FooMatcherImpl that implements the
58e35fdd936d133bf8a48de140a3c666897588a05shiqian//      MatcherInterface<T> interface, and
59e35fdd936d133bf8a48de140a3c666897588a05shiqian//   2. a factory function that creates a Matcher<T> object from a
60e35fdd936d133bf8a48de140a3c666897588a05shiqian//      FooMatcherImpl*.
61e35fdd936d133bf8a48de140a3c666897588a05shiqian//
62e35fdd936d133bf8a48de140a3c666897588a05shiqian// The two-level delegation design makes it possible to allow a user
63e35fdd936d133bf8a48de140a3c666897588a05shiqian// to write "v" instead of "Eq(v)" where a Matcher is expected, which
64e35fdd936d133bf8a48de140a3c666897588a05shiqian// is impossible if we pass matchers by pointers.  It also eases
65e35fdd936d133bf8a48de140a3c666897588a05shiqian// ownership management as Matcher objects can now be copied like
66e35fdd936d133bf8a48de140a3c666897588a05shiqian// plain values.
67e35fdd936d133bf8a48de140a3c666897588a05shiqian
68821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// MatchResultListener is an abstract class.  Its << operator can be
69821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// used by a matcher to explain why a value matches or doesn't match.
70821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan//
71821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// TODO(wan@google.com): add method
72821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan//   bool InterestedInWhy(bool result) const;
73821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// to indicate whether the listener is interested in why the match
74821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// result is 'result'.
75821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wanclass MatchResultListener {
76821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan public:
77821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // Creates a listener object with the given underlying ostream.  The
78821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // listener does not own the ostream.
79821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  explicit MatchResultListener(::std::ostream* os) : stream_(os) {}
80821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  virtual ~MatchResultListener() = 0;  // Makes this class abstract.
81821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
82821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // Streams x to the underlying ostream; does nothing if the ostream
83821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // is NULL.
84821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  template <typename T>
85821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  MatchResultListener& operator<<(const T& x) {
86821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (stream_ != NULL)
87821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      *stream_ << x;
88821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    return *this;
89821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  }
90821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
91821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // Returns the underlying ostream.
92821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  ::std::ostream* stream() { return stream_; }
93821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
94a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan  // Returns true iff the listener is interested in an explanation of
95a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan  // the match result.  A matcher's MatchAndExplain() method can use
96a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan  // this information to avoid generating the explanation when no one
97a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan  // intends to hear it.
98a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan  bool IsInterested() const { return stream_ != NULL; }
99a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan
100821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan private:
101821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  ::std::ostream* const stream_;
102821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
103821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  GTEST_DISALLOW_COPY_AND_ASSIGN_(MatchResultListener);
104821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan};
105821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
106821133180ccdb9eb062548d964929ba3b354eb84zhanyong.waninline MatchResultListener::~MatchResultListener() {
107821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan}
108821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
109e35fdd936d133bf8a48de140a3c666897588a05shiqian// The implementation of a matcher.
110e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
111e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatcherInterface {
112e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
113e35fdd936d133bf8a48de140a3c666897588a05shiqian  virtual ~MatcherInterface() {}
114e35fdd936d133bf8a48de140a3c666897588a05shiqian
115821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // Returns true iff the matcher matches x; also explains the match
11683f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // result to 'listener' if necessary (see the next paragraph), in
11783f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // the form of a non-restrictive relative clause ("which ...",
11883f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // "whose ...", etc) that describes x.  For example, the
11983f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // MatchAndExplain() method of the Pointee(...) matcher should
12083f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // generate an explanation like "which points to ...".
12183f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  //
12283f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // Implementations of MatchAndExplain() should add an explanation of
12383f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // the match result *if and only if* they can provide additional
12483f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // information that's not already present (or not obvious) in the
12583f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // print-out of x and the matcher's description.  Whether the match
12683f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // succeeds is not a factor in deciding whether an explanation is
12783f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // needed, as sometimes the caller needs to print a failure message
12883f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // when the match succeeds (e.g. when the matcher is used inside
12983f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // Not()).
13083f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  //
13183f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // For example, a "has at least 10 elements" matcher should explain
13283f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // what the actual element count is, regardless of the match result,
13383f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // as it is useful information to the reader; on the other hand, an
13483f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // "is empty" matcher probably only needs to explain what the actual
13583f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // size is when the match fails, as it's redundant to say that the
13683f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // size is 0 when the value is already known to be empty.
137821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  //
138db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  // You should override this method when defining a new matcher.
139821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  //
140821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // It's the responsibility of the caller (Google Mock) to guarantee
141821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // that 'listener' is not NULL.  This helps to simplify a matcher's
142821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // implementation when it doesn't care about the performance, as it
143821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // can talk to 'listener' without checking its validity first.
144821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // However, in order to implement dummy listeners efficiently,
145821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // listener->stream() may be NULL.
146db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  virtual bool MatchAndExplain(T x, MatchResultListener* listener) const = 0;
147e35fdd936d133bf8a48de140a3c666897588a05shiqian
148a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan  // Describes this matcher to an ostream.  The function should print
149a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan  // a verb phrase that describes the property a value matching this
150a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan  // matcher should have.  The subject of the verb phrase is the value
151a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan  // being matched.  For example, the DescribeTo() method of the Gt(7)
152a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan  // matcher prints "is greater than 7".
153e35fdd936d133bf8a48de140a3c666897588a05shiqian  virtual void DescribeTo(::std::ostream* os) const = 0;
154e35fdd936d133bf8a48de140a3c666897588a05shiqian
155e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Describes the negation of this matcher to an ostream.  For
156e35fdd936d133bf8a48de140a3c666897588a05shiqian  // example, if the description of this matcher is "is greater than
157e35fdd936d133bf8a48de140a3c666897588a05shiqian  // 7", the negated description could be "is not greater than 7".
158e35fdd936d133bf8a48de140a3c666897588a05shiqian  // You are not required to override this when implementing
159e35fdd936d133bf8a48de140a3c666897588a05shiqian  // MatcherInterface, but it is highly advised so that your matcher
160e35fdd936d133bf8a48de140a3c666897588a05shiqian  // can produce good error messages.
161e35fdd936d133bf8a48de140a3c666897588a05shiqian  virtual void DescribeNegationTo(::std::ostream* os) const {
162e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "not (";
163e35fdd936d133bf8a48de140a3c666897588a05shiqian    DescribeTo(os);
164e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << ")";
165e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
166e35fdd936d133bf8a48de140a3c666897588a05shiqian};
167e35fdd936d133bf8a48de140a3c666897588a05shiqian
168e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace internal {
169e35fdd936d133bf8a48de140a3c666897588a05shiqian
170821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// A match result listener that ignores the explanation.
171821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wanclass DummyMatchResultListener : public MatchResultListener {
172821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan public:
173821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  DummyMatchResultListener() : MatchResultListener(NULL) {}
174821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
175821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan private:
176821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  GTEST_DISALLOW_COPY_AND_ASSIGN_(DummyMatchResultListener);
177821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan};
178821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
179821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// A match result listener that forwards the explanation to a given
180821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// ostream.  The difference between this and MatchResultListener is
181821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// that the former is concrete.
182821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wanclass StreamMatchResultListener : public MatchResultListener {
183821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan public:
184821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  explicit StreamMatchResultListener(::std::ostream* os)
185821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      : MatchResultListener(os) {}
186821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
187821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan private:
188821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  GTEST_DISALLOW_COPY_AND_ASSIGN_(StreamMatchResultListener);
189821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan};
190821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
191821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// A match result listener that stores the explanation in a string.
192821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wanclass StringMatchResultListener : public MatchResultListener {
193821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan public:
194821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  StringMatchResultListener() : MatchResultListener(&ss_) {}
195821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
196821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // Returns the explanation heard so far.
197821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  internal::string str() const { return ss_.str(); }
198821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
199821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan private:
200821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  ::std::stringstream ss_;
201821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
202821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  GTEST_DISALLOW_COPY_AND_ASSIGN_(StringMatchResultListener);
203821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan};
204821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
205e35fdd936d133bf8a48de140a3c666897588a05shiqian// An internal class for implementing Matcher<T>, which will derive
206e35fdd936d133bf8a48de140a3c666897588a05shiqian// from it.  We put functionalities common to all Matcher<T>
207e35fdd936d133bf8a48de140a3c666897588a05shiqian// specializations here to avoid code duplication.
208e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
209e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatcherBase {
210e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
211821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // Returns true iff the matcher matches x; also explains the match
212821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // result to 'listener'.
213821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  bool MatchAndExplain(T x, MatchResultListener* listener) const {
214821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    return impl_->MatchAndExplain(x, listener);
215821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  }
216821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
217e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Returns true iff this matcher matches x.
218821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  bool Matches(T x) const {
219821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    DummyMatchResultListener dummy;
220821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    return MatchAndExplain(x, &dummy);
221821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  }
222e35fdd936d133bf8a48de140a3c666897588a05shiqian
223e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Describes this matcher to an ostream.
224e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const { impl_->DescribeTo(os); }
225e35fdd936d133bf8a48de140a3c666897588a05shiqian
226e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Describes the negation of this matcher to an ostream.
227e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
228e35fdd936d133bf8a48de140a3c666897588a05shiqian    impl_->DescribeNegationTo(os);
229e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
230e35fdd936d133bf8a48de140a3c666897588a05shiqian
231e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Explains why x matches, or doesn't match, the matcher.
232e35fdd936d133bf8a48de140a3c666897588a05shiqian  void ExplainMatchResultTo(T x, ::std::ostream* os) const {
233821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    StreamMatchResultListener listener(os);
234821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    MatchAndExplain(x, &listener);
235e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
23632de5f53763125925e078498250f7e73a88de9edzhanyong.wan
237e35fdd936d133bf8a48de140a3c666897588a05shiqian protected:
238e35fdd936d133bf8a48de140a3c666897588a05shiqian  MatcherBase() {}
239e35fdd936d133bf8a48de140a3c666897588a05shiqian
240e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Constructs a matcher from its implementation.
241e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit MatcherBase(const MatcherInterface<T>* impl)
242e35fdd936d133bf8a48de140a3c666897588a05shiqian      : impl_(impl) {}
243e35fdd936d133bf8a48de140a3c666897588a05shiqian
244e35fdd936d133bf8a48de140a3c666897588a05shiqian  virtual ~MatcherBase() {}
24532de5f53763125925e078498250f7e73a88de9edzhanyong.wan
246e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
247e35fdd936d133bf8a48de140a3c666897588a05shiqian  // shared_ptr (util/gtl/shared_ptr.h) and linked_ptr have similar
248e35fdd936d133bf8a48de140a3c666897588a05shiqian  // interfaces.  The former dynamically allocates a chunk of memory
249e35fdd936d133bf8a48de140a3c666897588a05shiqian  // to hold the reference count, while the latter tracks all
250e35fdd936d133bf8a48de140a3c666897588a05shiqian  // references using a circular linked list without allocating
251e35fdd936d133bf8a48de140a3c666897588a05shiqian  // memory.  It has been observed that linked_ptr performs better in
252e35fdd936d133bf8a48de140a3c666897588a05shiqian  // typical scenarios.  However, shared_ptr can out-perform
253e35fdd936d133bf8a48de140a3c666897588a05shiqian  // linked_ptr when there are many more uses of the copy constructor
254e35fdd936d133bf8a48de140a3c666897588a05shiqian  // than the default constructor.
255e35fdd936d133bf8a48de140a3c666897588a05shiqian  //
256e35fdd936d133bf8a48de140a3c666897588a05shiqian  // If performance becomes a problem, we should see if using
257e35fdd936d133bf8a48de140a3c666897588a05shiqian  // shared_ptr helps.
258e35fdd936d133bf8a48de140a3c666897588a05shiqian  ::testing::internal::linked_ptr<const MatcherInterface<T> > impl_;
259e35fdd936d133bf8a48de140a3c666897588a05shiqian};
260e35fdd936d133bf8a48de140a3c666897588a05shiqian
261e35fdd936d133bf8a48de140a3c666897588a05shiqian}  // namespace internal
262e35fdd936d133bf8a48de140a3c666897588a05shiqian
263e35fdd936d133bf8a48de140a3c666897588a05shiqian// A Matcher<T> is a copyable and IMMUTABLE (except by assignment)
264e35fdd936d133bf8a48de140a3c666897588a05shiqian// object that can check whether a value of type T matches.  The
265e35fdd936d133bf8a48de140a3c666897588a05shiqian// implementation of Matcher<T> is just a linked_ptr to const
266e35fdd936d133bf8a48de140a3c666897588a05shiqian// MatcherInterface<T>, so copying is fairly cheap.  Don't inherit
267e35fdd936d133bf8a48de140a3c666897588a05shiqian// from Matcher!
268e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
269e35fdd936d133bf8a48de140a3c666897588a05shiqianclass Matcher : public internal::MatcherBase<T> {
270e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
27188032d8e0f6d4bc63b97aeeab84fdc44aeab1c89vladlosev  // Constructs a null matcher.  Needed for storing Matcher objects in STL
27288032d8e0f6d4bc63b97aeeab84fdc44aeab1c89vladlosev  // containers.  A default-constructed matcher is not yet initialized.  You
27388032d8e0f6d4bc63b97aeeab84fdc44aeab1c89vladlosev  // cannot use it until a valid value has been assigned to it.
274e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher() {}
275e35fdd936d133bf8a48de140a3c666897588a05shiqian
276e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Constructs a matcher from its implementation.
277e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit Matcher(const MatcherInterface<T>* impl)
278e35fdd936d133bf8a48de140a3c666897588a05shiqian      : internal::MatcherBase<T>(impl) {}
279e35fdd936d133bf8a48de140a3c666897588a05shiqian
28018490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  // Implicit constructor here allows people to write
281e35fdd936d133bf8a48de140a3c666897588a05shiqian  // EXPECT_CALL(foo, Bar(5)) instead of EXPECT_CALL(foo, Bar(Eq(5))) sometimes
282e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher(T value);  // NOLINT
283e35fdd936d133bf8a48de140a3c666897588a05shiqian};
284e35fdd936d133bf8a48de140a3c666897588a05shiqian
285e35fdd936d133bf8a48de140a3c666897588a05shiqian// The following two specializations allow the user to write str
286e35fdd936d133bf8a48de140a3c666897588a05shiqian// instead of Eq(str) and "foo" instead of Eq("foo") when a string
287e35fdd936d133bf8a48de140a3c666897588a05shiqian// matcher is expected.
288e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <>
289587c1b37c2f0b6d430fb13ce09326db0135b557cvladlosevclass GTEST_API_ Matcher<const internal::string&>
290e35fdd936d133bf8a48de140a3c666897588a05shiqian    : public internal::MatcherBase<const internal::string&> {
291e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
292e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher() {}
293e35fdd936d133bf8a48de140a3c666897588a05shiqian
294e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit Matcher(const MatcherInterface<const internal::string&>* impl)
295e35fdd936d133bf8a48de140a3c666897588a05shiqian      : internal::MatcherBase<const internal::string&>(impl) {}
296e35fdd936d133bf8a48de140a3c666897588a05shiqian
297e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Allows the user to write str instead of Eq(str) sometimes, where
298e35fdd936d133bf8a48de140a3c666897588a05shiqian  // str is a string object.
299e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher(const internal::string& s);  // NOLINT
300e35fdd936d133bf8a48de140a3c666897588a05shiqian
301e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Allows the user to write "foo" instead of Eq("foo") sometimes.
302e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher(const char* s);  // NOLINT
303e35fdd936d133bf8a48de140a3c666897588a05shiqian};
304e35fdd936d133bf8a48de140a3c666897588a05shiqian
305e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <>
306587c1b37c2f0b6d430fb13ce09326db0135b557cvladlosevclass GTEST_API_ Matcher<internal::string>
307e35fdd936d133bf8a48de140a3c666897588a05shiqian    : public internal::MatcherBase<internal::string> {
308e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
309e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher() {}
310e35fdd936d133bf8a48de140a3c666897588a05shiqian
311e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit Matcher(const MatcherInterface<internal::string>* impl)
312e35fdd936d133bf8a48de140a3c666897588a05shiqian      : internal::MatcherBase<internal::string>(impl) {}
313e35fdd936d133bf8a48de140a3c666897588a05shiqian
314e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Allows the user to write str instead of Eq(str) sometimes, where
315e35fdd936d133bf8a48de140a3c666897588a05shiqian  // str is a string object.
316e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher(const internal::string& s);  // NOLINT
317e35fdd936d133bf8a48de140a3c666897588a05shiqian
318e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Allows the user to write "foo" instead of Eq("foo") sometimes.
319e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher(const char* s);  // NOLINT
320e35fdd936d133bf8a48de140a3c666897588a05shiqian};
321e35fdd936d133bf8a48de140a3c666897588a05shiqian
3221f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan#if GTEST_HAS_STRING_PIECE_
3231f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan// The following two specializations allow the user to write str
3241f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan// instead of Eq(str) and "foo" instead of Eq("foo") when a StringPiece
3251f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan// matcher is expected.
3261f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wantemplate <>
3271f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wanclass GTEST_API_ Matcher<const StringPiece&>
3281f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan    : public internal::MatcherBase<const StringPiece&> {
3291f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan public:
3301f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  Matcher() {}
3311f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan
3321f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  explicit Matcher(const MatcherInterface<const StringPiece&>* impl)
3331f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan      : internal::MatcherBase<const StringPiece&>(impl) {}
3341f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan
3351f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  // Allows the user to write str instead of Eq(str) sometimes, where
3361f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  // str is a string object.
3371f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  Matcher(const internal::string& s);  // NOLINT
3381f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan
3391f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  // Allows the user to write "foo" instead of Eq("foo") sometimes.
3401f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  Matcher(const char* s);  // NOLINT
3411f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan
3421f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  // Allows the user to pass StringPieces directly.
3431f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  Matcher(StringPiece s);  // NOLINT
3441f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan};
3451f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan
3461f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wantemplate <>
3471f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wanclass GTEST_API_ Matcher<StringPiece>
3481f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan    : public internal::MatcherBase<StringPiece> {
3491f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan public:
3501f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  Matcher() {}
3511f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan
3521f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  explicit Matcher(const MatcherInterface<StringPiece>* impl)
3531f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan      : internal::MatcherBase<StringPiece>(impl) {}
3541f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan
3551f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  // Allows the user to write str instead of Eq(str) sometimes, where
3561f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  // str is a string object.
3571f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  Matcher(const internal::string& s);  // NOLINT
3581f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan
3591f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  // Allows the user to write "foo" instead of Eq("foo") sometimes.
3601f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  Matcher(const char* s);  // NOLINT
3611f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan
3621f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  // Allows the user to pass StringPieces directly.
3631f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  Matcher(StringPiece s);  // NOLINT
3641f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan};
3651f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan#endif  // GTEST_HAS_STRING_PIECE_
3661f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan
367e35fdd936d133bf8a48de140a3c666897588a05shiqian// The PolymorphicMatcher class template makes it easy to implement a
368e35fdd936d133bf8a48de140a3c666897588a05shiqian// polymorphic matcher (i.e. a matcher that can match values of more
369e35fdd936d133bf8a48de140a3c666897588a05shiqian// than one type, e.g. Eq(n) and NotNull()).
370e35fdd936d133bf8a48de140a3c666897588a05shiqian//
371db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan// To define a polymorphic matcher, a user should provide an Impl
372db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan// class that has a DescribeTo() method and a DescribeNegationTo()
373db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan// method, and define a member function (or member function template)
374821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan//
375db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan//   bool MatchAndExplain(const Value& value,
376db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan//                        MatchResultListener* listener) const;
377821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan//
378821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// See the definition of NotNull() for a complete example.
379e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <class Impl>
380e35fdd936d133bf8a48de140a3c666897588a05shiqianclass PolymorphicMatcher {
381e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
38232de5f53763125925e078498250f7e73a88de9edzhanyong.wan  explicit PolymorphicMatcher(const Impl& an_impl) : impl_(an_impl) {}
383e35fdd936d133bf8a48de140a3c666897588a05shiqian
3842b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan  // Returns a mutable reference to the underlying matcher
3852b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan  // implementation object.
3862b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan  Impl& mutable_impl() { return impl_; }
3872b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan
3882b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan  // Returns an immutable reference to the underlying matcher
3892b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan  // implementation object.
3902b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan  const Impl& impl() const { return impl_; }
3912b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan
392e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
393e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<T>() const {
394e35fdd936d133bf8a48de140a3c666897588a05shiqian    return Matcher<T>(new MonomorphicImpl<T>(impl_));
395e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
39632de5f53763125925e078498250f7e73a88de9edzhanyong.wan
397e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
398e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
399e35fdd936d133bf8a48de140a3c666897588a05shiqian  class MonomorphicImpl : public MatcherInterface<T> {
400e35fdd936d133bf8a48de140a3c666897588a05shiqian   public:
401e35fdd936d133bf8a48de140a3c666897588a05shiqian    explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {}
402e35fdd936d133bf8a48de140a3c666897588a05shiqian
403e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeTo(::std::ostream* os) const {
404e35fdd936d133bf8a48de140a3c666897588a05shiqian      impl_.DescribeTo(os);
405e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
406e35fdd936d133bf8a48de140a3c666897588a05shiqian
407e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeNegationTo(::std::ostream* os) const {
408e35fdd936d133bf8a48de140a3c666897588a05shiqian      impl_.DescribeNegationTo(os);
409e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
410e35fdd936d133bf8a48de140a3c666897588a05shiqian
411821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
412db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan      return impl_.MatchAndExplain(x, listener);
413e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
4142b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan
415e35fdd936d133bf8a48de140a3c666897588a05shiqian   private:
416e35fdd936d133bf8a48de140a3c666897588a05shiqian    const Impl impl_;
41732de5f53763125925e078498250f7e73a88de9edzhanyong.wan
41832de5f53763125925e078498250f7e73a88de9edzhanyong.wan    GTEST_DISALLOW_ASSIGN_(MonomorphicImpl);
419e35fdd936d133bf8a48de140a3c666897588a05shiqian  };
420e35fdd936d133bf8a48de140a3c666897588a05shiqian
4212b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan  Impl impl_;
42232de5f53763125925e078498250f7e73a88de9edzhanyong.wan
42332de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(PolymorphicMatcher);
424e35fdd936d133bf8a48de140a3c666897588a05shiqian};
425e35fdd936d133bf8a48de140a3c666897588a05shiqian
426e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher from its implementation.  This is easier to use
427e35fdd936d133bf8a48de140a3c666897588a05shiqian// than the Matcher<T> constructor as it doesn't require you to
428e35fdd936d133bf8a48de140a3c666897588a05shiqian// explicitly write the template argument, e.g.
429e35fdd936d133bf8a48de140a3c666897588a05shiqian//
430e35fdd936d133bf8a48de140a3c666897588a05shiqian//   MakeMatcher(foo);
431e35fdd936d133bf8a48de140a3c666897588a05shiqian// vs
432e35fdd936d133bf8a48de140a3c666897588a05shiqian//   Matcher<const string&>(foo);
433e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
434e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<T> MakeMatcher(const MatcherInterface<T>* impl) {
435e35fdd936d133bf8a48de140a3c666897588a05shiqian  return Matcher<T>(impl);
4362eab17b76d350dac1b1c85879ec8e1135da615cezhanyong.wan}
437e35fdd936d133bf8a48de140a3c666897588a05shiqian
438e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher from its implementation.  This is
439e35fdd936d133bf8a48de140a3c666897588a05shiqian// easier to use than the PolymorphicMatcher<Impl> constructor as it
440e35fdd936d133bf8a48de140a3c666897588a05shiqian// doesn't require you to explicitly write the template argument, e.g.
441e35fdd936d133bf8a48de140a3c666897588a05shiqian//
442e35fdd936d133bf8a48de140a3c666897588a05shiqian//   MakePolymorphicMatcher(foo);
443e35fdd936d133bf8a48de140a3c666897588a05shiqian// vs
444e35fdd936d133bf8a48de140a3c666897588a05shiqian//   PolymorphicMatcher<TypeOfFoo>(foo);
445e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <class Impl>
446e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<Impl> MakePolymorphicMatcher(const Impl& impl) {
447e35fdd936d133bf8a48de140a3c666897588a05shiqian  return PolymorphicMatcher<Impl>(impl);
448e35fdd936d133bf8a48de140a3c666897588a05shiqian}
449e35fdd936d133bf8a48de140a3c666897588a05shiqian
45079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// Anything inside the 'internal' namespace IS INTERNAL IMPLEMENTATION
45179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// and MUST NOT BE USED IN USER CODE!!!
45279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgmnamespace internal {
45379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm
45479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// The MatcherCastImpl class template is a helper for implementing
45579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// MatcherCast().  We need this helper in order to partially
45679a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// specialize the implementation of MatcherCast() (C++ allows
45779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// class/struct templates to be partially specialized, but not
45879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// function templates.).
45979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm
46079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// This general version is used when MatcherCast()'s argument is a
46179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// polymorphic matcher (i.e. something that can be converted to a
46279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// Matcher but is not one yet; for example, Eq(value)) or a value (for
46379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// example, "hello").
46479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgmtemplate <typename T, typename M>
46579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgmclass MatcherCastImpl {
46679a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm public:
46779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm  static Matcher<T> Cast(M polymorphic_matcher_or_value) {
46879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // M can be a polymorhic matcher, in which case we want to use
46979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // its conversion operator to create Matcher<T>.  Or it can be a value
47079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // that should be passed to the Matcher<T>'s constructor.
47179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    //
47279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // We can't call Matcher<T>(polymorphic_matcher_or_value) when M is a
47379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // polymorphic matcher because it'll be ambiguous if T has an implicit
47479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // constructor from M (this usually happens when T has an implicit
47579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // constructor from any type).
47679a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    //
47779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // It won't work to unconditionally implict_cast
47879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // polymorphic_matcher_or_value to Matcher<T> because it won't trigger
47979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // a user-defined conversion from M to T if one exists (assuming M is
48079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // a value).
48179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    return CastImpl(
48279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm        polymorphic_matcher_or_value,
48379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm        BooleanConstant<
48479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm            internal::ImplicitlyConvertible<M, Matcher<T> >::value>());
48579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm  }
48679a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm
48779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm private:
48879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm  static Matcher<T> CastImpl(M value, BooleanConstant<false>) {
48979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // M can't be implicitly converted to Matcher<T>, so M isn't a polymorphic
49079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // matcher.  It must be a value then.  Use direct initialization to create
49179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // a matcher.
49279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    return Matcher<T>(ImplicitCast_<T>(value));
49379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm  }
49479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm
49579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm  static Matcher<T> CastImpl(M polymorphic_matcher_or_value,
49679a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm                             BooleanConstant<true>) {
49779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // M is implicitly convertible to Matcher<T>, which means that either
49879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // M is a polymorhpic matcher or Matcher<T> has an implicit constructor
49979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // from M.  In both cases using the implicit conversion will produce a
50079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // matcher.
50179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    //
50279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // Even if T has an implicit constructor from M, it won't be called because
50379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // creating Matcher<T> would require a chain of two user-defined conversions
50479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // (first to create T from M and then to create Matcher<T> from T).
50579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    return polymorphic_matcher_or_value;
50679a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm  }
50779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm};
50879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm
50979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// This more specialized version is used when MatcherCast()'s argument
51079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// is already a Matcher.  This only compiles when type T can be
51179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// statically converted to type U.
51279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgmtemplate <typename T, typename U>
51379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgmclass MatcherCastImpl<T, Matcher<U> > {
51479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm public:
51579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm  static Matcher<T> Cast(const Matcher<U>& source_matcher) {
51679a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    return Matcher<T>(new Impl(source_matcher));
51779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm  }
51879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm
51979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm private:
52079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm  class Impl : public MatcherInterface<T> {
52179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm   public:
52279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    explicit Impl(const Matcher<U>& source_matcher)
52379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm        : source_matcher_(source_matcher) {}
52479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm
52579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // We delegate the matching logic to the source matcher.
52679a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
52779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm      return source_matcher_.MatchAndExplain(static_cast<U>(x), listener);
52879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    }
52979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm
53079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    virtual void DescribeTo(::std::ostream* os) const {
53179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm      source_matcher_.DescribeTo(os);
53279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    }
53379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm
53479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    virtual void DescribeNegationTo(::std::ostream* os) const {
53579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm      source_matcher_.DescribeNegationTo(os);
53679a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    }
53779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm
53879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm   private:
53979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    const Matcher<U> source_matcher_;
54079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm
54179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    GTEST_DISALLOW_ASSIGN_(Impl);
54279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm  };
54379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm};
54479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm
54579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// This even more specialized version is used for efficiently casting
54679a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// a matcher to its own type.
54779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgmtemplate <typename T>
54879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgmclass MatcherCastImpl<T, Matcher<T> > {
54979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm public:
55079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm  static Matcher<T> Cast(const Matcher<T>& matcher) { return matcher; }
55179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm};
55279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm
55379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm}  // namespace internal
55479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm
555e35fdd936d133bf8a48de140a3c666897588a05shiqian// In order to be safe and clear, casting between different matcher
556e35fdd936d133bf8a48de140a3c666897588a05shiqian// types is done explicitly via MatcherCast<T>(m), which takes a
557e35fdd936d133bf8a48de140a3c666897588a05shiqian// matcher m and returns a Matcher<T>.  It compiles only when T can be
558e35fdd936d133bf8a48de140a3c666897588a05shiqian// statically converted to the argument type of m.
559e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T, typename M>
56079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgminline Matcher<T> MatcherCast(M matcher) {
56179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm  return internal::MatcherCastImpl<T, M>::Cast(matcher);
56279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm}
563e35fdd936d133bf8a48de140a3c666897588a05shiqian
56418490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// Implements SafeMatcherCast().
56518490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan//
56695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// We use an intermediate class to do the actual safe casting as Nokia's
56795b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// Symbian compiler cannot decide between
56895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// template <T, M> ... (M) and
56995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// template <T, U> ... (const Matcher<U>&)
57095b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// for function templates but can for member function templates.
57195b12332c345cee508a8e2b68e007280392506e0zhanyong.wantemplate <typename T>
57295b12332c345cee508a8e2b68e007280392506e0zhanyong.wanclass SafeMatcherCastImpl {
57395b12332c345cee508a8e2b68e007280392506e0zhanyong.wan public:
57479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm  // This overload handles polymorphic matchers and values only since
57579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm  // monomorphic matchers are handled by the next one.
57695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  template <typename M>
57779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm  static inline Matcher<T> Cast(M polymorphic_matcher_or_value) {
57879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    return internal::MatcherCastImpl<T, M>::Cast(polymorphic_matcher_or_value);
57995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  }
58018490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan
58195b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  // This overload handles monomorphic matchers.
58295b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  //
58395b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  // In general, if type T can be implicitly converted to type U, we can
58495b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  // safely convert a Matcher<U> to a Matcher<T> (i.e. Matcher is
58595b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  // contravariant): just keep a copy of the original Matcher<U>, convert the
58695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  // argument from type T to U, and then pass it to the underlying Matcher<U>.
58795b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  // The only exception is when U is a reference and T is not, as the
58895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  // underlying Matcher<U> may be interested in the argument's address, which
58995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  // is not preserved in the conversion from T to U.
59095b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  template <typename U>
59195b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  static inline Matcher<T> Cast(const Matcher<U>& matcher) {
59295b12332c345cee508a8e2b68e007280392506e0zhanyong.wan    // Enforce that T can be implicitly converted to U.
59302f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan    GTEST_COMPILE_ASSERT_((internal::ImplicitlyConvertible<T, U>::value),
59495b12332c345cee508a8e2b68e007280392506e0zhanyong.wan                          T_must_be_implicitly_convertible_to_U);
59595b12332c345cee508a8e2b68e007280392506e0zhanyong.wan    // Enforce that we are not converting a non-reference type T to a reference
59695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan    // type U.
59702f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan    GTEST_COMPILE_ASSERT_(
59895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan        internal::is_reference<T>::value || !internal::is_reference<U>::value,
59995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan        cannot_convert_non_referentce_arg_to_reference);
60095b12332c345cee508a8e2b68e007280392506e0zhanyong.wan    // In case both T and U are arithmetic types, enforce that the
60195b12332c345cee508a8e2b68e007280392506e0zhanyong.wan    // conversion is not lossy.
602ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    typedef GTEST_REMOVE_REFERENCE_AND_CONST_(T) RawT;
603ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    typedef GTEST_REMOVE_REFERENCE_AND_CONST_(U) RawU;
60495b12332c345cee508a8e2b68e007280392506e0zhanyong.wan    const bool kTIsOther = GMOCK_KIND_OF_(RawT) == internal::kOther;
60595b12332c345cee508a8e2b68e007280392506e0zhanyong.wan    const bool kUIsOther = GMOCK_KIND_OF_(RawU) == internal::kOther;
60602f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan    GTEST_COMPILE_ASSERT_(
60795b12332c345cee508a8e2b68e007280392506e0zhanyong.wan        kTIsOther || kUIsOther ||
60895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan        (internal::LosslessArithmeticConvertible<RawT, RawU>::value),
60995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan        conversion_of_arithmetic_types_must_be_lossless);
61095b12332c345cee508a8e2b68e007280392506e0zhanyong.wan    return MatcherCast<T>(matcher);
61195b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  }
61295b12332c345cee508a8e2b68e007280392506e0zhanyong.wan};
61395b12332c345cee508a8e2b68e007280392506e0zhanyong.wan
61495b12332c345cee508a8e2b68e007280392506e0zhanyong.wantemplate <typename T, typename M>
61595b12332c345cee508a8e2b68e007280392506e0zhanyong.waninline Matcher<T> SafeMatcherCast(const M& polymorphic_matcher) {
61695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  return SafeMatcherCastImpl<T>::Cast(polymorphic_matcher);
61718490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan}
61818490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan
619e35fdd936d133bf8a48de140a3c666897588a05shiqian// A<T>() returns a matcher that matches any value of type T.
620e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
621e35fdd936d133bf8a48de140a3c666897588a05shiqianMatcher<T> A();
622e35fdd936d133bf8a48de140a3c666897588a05shiqian
623e35fdd936d133bf8a48de140a3c666897588a05shiqian// Anything inside the 'internal' namespace IS INTERNAL IMPLEMENTATION
624e35fdd936d133bf8a48de140a3c666897588a05shiqian// and MUST NOT BE USED IN USER CODE!!!
625e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace internal {
626e35fdd936d133bf8a48de140a3c666897588a05shiqian
627b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan// If the explanation is not empty, prints it to the ostream.
628b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.waninline void PrintIfNotEmpty(const internal::string& explanation,
629b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan                            std::ostream* os) {
630b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan  if (explanation != "" && os != NULL) {
631b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    *os << ", " << explanation;
632676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan  }
633676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan}
634676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan
635736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan// Returns true if the given type name is easy to read by a human.
636736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan// This is used to decide whether printing the type of a value might
637736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan// be helpful.
638736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.waninline bool IsReadableTypeName(const string& type_name) {
639736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan  // We consider a type name readable if it's short or doesn't contain
640736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan  // a template or function type.
641736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan  return (type_name.length() <= 20 ||
642736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan          type_name.find_first_of("<(") == string::npos);
643736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan}
644736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan
645676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan// Matches the value against the given matcher, prints the value and explains
646676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan// the match result to the listener. Returns the match result.
647676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan// 'listener' must not be NULL.
648676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan// Value cannot be passed by const reference, because some matchers take a
649676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan// non-const argument.
650676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wantemplate <typename Value, typename T>
651676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wanbool MatchPrintAndExplain(Value& value, const Matcher<T>& matcher,
652676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan                          MatchResultListener* listener) {
653676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan  if (!listener->IsInterested()) {
654676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    // If the listener is not interested, we do not need to construct the
655676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    // inner explanation.
656676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    return matcher.Matches(value);
657676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan  }
658676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan
659676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan  StringMatchResultListener inner_listener;
660676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan  const bool match = matcher.MatchAndExplain(value, &inner_listener);
661676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan
662676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan  UniversalPrint(value, listener->stream());
663736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan#if GTEST_HAS_RTTI
664736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan  const string& type_name = GetTypeName<Value>();
665736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan  if (IsReadableTypeName(type_name))
666736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan    *listener->stream() << " (of type " << type_name << ")";
667736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan#endif
668b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan  PrintIfNotEmpty(inner_listener.str(), listener->stream());
669676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan
670676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan  return match;
671676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan}
672676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan
673e35fdd936d133bf8a48de140a3c666897588a05shiqian// An internal helper class for doing compile-time loop on a tuple's
674e35fdd936d133bf8a48de140a3c666897588a05shiqian// fields.
675e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <size_t N>
676e35fdd936d133bf8a48de140a3c666897588a05shiqianclass TuplePrefix {
677e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
678e35fdd936d133bf8a48de140a3c666897588a05shiqian  // TuplePrefix<N>::Matches(matcher_tuple, value_tuple) returns true
679e35fdd936d133bf8a48de140a3c666897588a05shiqian  // iff the first N fields of matcher_tuple matches the first N
680e35fdd936d133bf8a48de140a3c666897588a05shiqian  // fields of value_tuple, respectively.
681e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename MatcherTuple, typename ValueTuple>
682e35fdd936d133bf8a48de140a3c666897588a05shiqian  static bool Matches(const MatcherTuple& matcher_tuple,
683e35fdd936d133bf8a48de140a3c666897588a05shiqian                      const ValueTuple& value_tuple) {
684e35fdd936d133bf8a48de140a3c666897588a05shiqian    using ::std::tr1::get;
685e35fdd936d133bf8a48de140a3c666897588a05shiqian    return TuplePrefix<N - 1>::Matches(matcher_tuple, value_tuple)
686e35fdd936d133bf8a48de140a3c666897588a05shiqian        && get<N - 1>(matcher_tuple).Matches(get<N - 1>(value_tuple));
687e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
688e35fdd936d133bf8a48de140a3c666897588a05shiqian
689b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan  // TuplePrefix<N>::ExplainMatchFailuresTo(matchers, values, os)
690e35fdd936d133bf8a48de140a3c666897588a05shiqian  // describes failures in matching the first N fields of matchers
691e35fdd936d133bf8a48de140a3c666897588a05shiqian  // against the first N fields of values.  If there is no failure,
692e35fdd936d133bf8a48de140a3c666897588a05shiqian  // nothing will be streamed to os.
693e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename MatcherTuple, typename ValueTuple>
694b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan  static void ExplainMatchFailuresTo(const MatcherTuple& matchers,
695b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan                                     const ValueTuple& values,
696b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan                                     ::std::ostream* os) {
697e35fdd936d133bf8a48de140a3c666897588a05shiqian    using ::std::tr1::tuple_element;
698e35fdd936d133bf8a48de140a3c666897588a05shiqian    using ::std::tr1::get;
699e35fdd936d133bf8a48de140a3c666897588a05shiqian
700e35fdd936d133bf8a48de140a3c666897588a05shiqian    // First, describes failures in the first N - 1 fields.
701b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    TuplePrefix<N - 1>::ExplainMatchFailuresTo(matchers, values, os);
702e35fdd936d133bf8a48de140a3c666897588a05shiqian
703e35fdd936d133bf8a48de140a3c666897588a05shiqian    // Then describes the failure (if any) in the (N - 1)-th (0-based)
704e35fdd936d133bf8a48de140a3c666897588a05shiqian    // field.
705e35fdd936d133bf8a48de140a3c666897588a05shiqian    typename tuple_element<N - 1, MatcherTuple>::type matcher =
706e35fdd936d133bf8a48de140a3c666897588a05shiqian        get<N - 1>(matchers);
707e35fdd936d133bf8a48de140a3c666897588a05shiqian    typedef typename tuple_element<N - 1, ValueTuple>::type Value;
708e35fdd936d133bf8a48de140a3c666897588a05shiqian    Value value = get<N - 1>(values);
709821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    StringMatchResultListener listener;
710821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (!matcher.MatchAndExplain(value, &listener)) {
711e35fdd936d133bf8a48de140a3c666897588a05shiqian      // TODO(wan): include in the message the name of the parameter
712e35fdd936d133bf8a48de140a3c666897588a05shiqian      // as used in MOCK_METHOD*() when possible.
713e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "  Expected arg #" << N - 1 << ": ";
714e35fdd936d133bf8a48de140a3c666897588a05shiqian      get<N - 1>(matchers).DescribeTo(os);
715e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "\n           Actual: ";
716e35fdd936d133bf8a48de140a3c666897588a05shiqian      // We remove the reference in type Value to prevent the
717e35fdd936d133bf8a48de140a3c666897588a05shiqian      // universal printer from printing the address of value, which
718e35fdd936d133bf8a48de140a3c666897588a05shiqian      // isn't interesting to the user most of the time.  The
719db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan      // matcher's MatchAndExplain() method handles the case when
720e35fdd936d133bf8a48de140a3c666897588a05shiqian      // the address is interesting.
721b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan      internal::UniversalPrint(value, os);
722b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan      PrintIfNotEmpty(listener.str(), os);
723e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "\n";
724e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
725e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
726e35fdd936d133bf8a48de140a3c666897588a05shiqian};
727e35fdd936d133bf8a48de140a3c666897588a05shiqian
728e35fdd936d133bf8a48de140a3c666897588a05shiqian// The base case.
729e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <>
730e35fdd936d133bf8a48de140a3c666897588a05shiqianclass TuplePrefix<0> {
731e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
732e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename MatcherTuple, typename ValueTuple>
7333fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.wan  static bool Matches(const MatcherTuple& /* matcher_tuple */,
7343fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.wan                      const ValueTuple& /* value_tuple */) {
735e35fdd936d133bf8a48de140a3c666897588a05shiqian    return true;
736e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
737e35fdd936d133bf8a48de140a3c666897588a05shiqian
738e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename MatcherTuple, typename ValueTuple>
739b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan  static void ExplainMatchFailuresTo(const MatcherTuple& /* matchers */,
740b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan                                     const ValueTuple& /* values */,
741b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan                                     ::std::ostream* /* os */) {}
742e35fdd936d133bf8a48de140a3c666897588a05shiqian};
743e35fdd936d133bf8a48de140a3c666897588a05shiqian
744e35fdd936d133bf8a48de140a3c666897588a05shiqian// TupleMatches(matcher_tuple, value_tuple) returns true iff all
745e35fdd936d133bf8a48de140a3c666897588a05shiqian// matchers in matcher_tuple match the corresponding fields in
746e35fdd936d133bf8a48de140a3c666897588a05shiqian// value_tuple.  It is a compiler error if matcher_tuple and
747e35fdd936d133bf8a48de140a3c666897588a05shiqian// value_tuple have different number of fields or incompatible field
748e35fdd936d133bf8a48de140a3c666897588a05shiqian// types.
749e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename MatcherTuple, typename ValueTuple>
750e35fdd936d133bf8a48de140a3c666897588a05shiqianbool TupleMatches(const MatcherTuple& matcher_tuple,
751e35fdd936d133bf8a48de140a3c666897588a05shiqian                  const ValueTuple& value_tuple) {
752e35fdd936d133bf8a48de140a3c666897588a05shiqian  using ::std::tr1::tuple_size;
753e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Makes sure that matcher_tuple and value_tuple have the same
754e35fdd936d133bf8a48de140a3c666897588a05shiqian  // number of fields.
75502f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan  GTEST_COMPILE_ASSERT_(tuple_size<MatcherTuple>::value ==
756e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan                        tuple_size<ValueTuple>::value,
757e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan                        matcher_and_value_have_different_numbers_of_fields);
758e35fdd936d133bf8a48de140a3c666897588a05shiqian  return TuplePrefix<tuple_size<ValueTuple>::value>::
759e35fdd936d133bf8a48de140a3c666897588a05shiqian      Matches(matcher_tuple, value_tuple);
760e35fdd936d133bf8a48de140a3c666897588a05shiqian}
761e35fdd936d133bf8a48de140a3c666897588a05shiqian
762e35fdd936d133bf8a48de140a3c666897588a05shiqian// Describes failures in matching matchers against values.  If there
763e35fdd936d133bf8a48de140a3c666897588a05shiqian// is no failure, nothing will be streamed to os.
764e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename MatcherTuple, typename ValueTuple>
765b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wanvoid ExplainMatchFailureTupleTo(const MatcherTuple& matchers,
766b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan                                const ValueTuple& values,
767b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan                                ::std::ostream* os) {
768e35fdd936d133bf8a48de140a3c666897588a05shiqian  using ::std::tr1::tuple_size;
769b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan  TuplePrefix<tuple_size<MatcherTuple>::value>::ExplainMatchFailuresTo(
770e35fdd936d133bf8a48de140a3c666897588a05shiqian      matchers, values, os);
771e35fdd936d133bf8a48de140a3c666897588a05shiqian}
772e35fdd936d133bf8a48de140a3c666897588a05shiqian
773e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements A<T>().
774e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
775e35fdd936d133bf8a48de140a3c666897588a05shiqianclass AnyMatcherImpl : public MatcherInterface<T> {
776e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
777821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  virtual bool MatchAndExplain(
778821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      T /* x */, MatchResultListener* /* listener */) const { return true; }
779e35fdd936d133bf8a48de140a3c666897588a05shiqian  virtual void DescribeTo(::std::ostream* os) const { *os << "is anything"; }
780e35fdd936d133bf8a48de140a3c666897588a05shiqian  virtual void DescribeNegationTo(::std::ostream* os) const {
781e35fdd936d133bf8a48de140a3c666897588a05shiqian    // This is mostly for completeness' safe, as it's not very useful
782e35fdd936d133bf8a48de140a3c666897588a05shiqian    // to write Not(A<bool>()).  However we cannot completely rule out
783e35fdd936d133bf8a48de140a3c666897588a05shiqian    // such a possibility, and it doesn't hurt to be prepared.
784e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "never matches";
785e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
786e35fdd936d133bf8a48de140a3c666897588a05shiqian};
787e35fdd936d133bf8a48de140a3c666897588a05shiqian
788e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements _, a matcher that matches any value of any
789e35fdd936d133bf8a48de140a3c666897588a05shiqian// type.  This is a polymorphic matcher, so we need a template type
790e35fdd936d133bf8a48de140a3c666897588a05shiqian// conversion operator to make it appearing as a Matcher<T> for any
791e35fdd936d133bf8a48de140a3c666897588a05shiqian// type T.
792e35fdd936d133bf8a48de140a3c666897588a05shiqianclass AnythingMatcher {
793e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
794e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
795e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<T>() const { return A<T>(); }
796e35fdd936d133bf8a48de140a3c666897588a05shiqian};
797e35fdd936d133bf8a48de140a3c666897588a05shiqian
798e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements a matcher that compares a given value with a
799e35fdd936d133bf8a48de140a3c666897588a05shiqian// pre-supplied value using one of the ==, <=, <, etc, operators.  The
800e35fdd936d133bf8a48de140a3c666897588a05shiqian// two values being compared don't have to have the same type.
801e35fdd936d133bf8a48de140a3c666897588a05shiqian//
802e35fdd936d133bf8a48de140a3c666897588a05shiqian// The matcher defined here is polymorphic (for example, Eq(5) can be
803e35fdd936d133bf8a48de140a3c666897588a05shiqian// used to match an int, a short, a double, etc).  Therefore we use
804e35fdd936d133bf8a48de140a3c666897588a05shiqian// a template type conversion operator in the implementation.
805e35fdd936d133bf8a48de140a3c666897588a05shiqian//
806e35fdd936d133bf8a48de140a3c666897588a05shiqian// We define this as a macro in order to eliminate duplicated source
807e35fdd936d133bf8a48de140a3c666897588a05shiqian// code.
808e35fdd936d133bf8a48de140a3c666897588a05shiqian//
809e35fdd936d133bf8a48de140a3c666897588a05shiqian// The following template definition assumes that the Rhs parameter is
810e35fdd936d133bf8a48de140a3c666897588a05shiqian// a "bare" type (i.e. neither 'const T' nor 'T&').
811b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan#define GMOCK_IMPLEMENT_COMPARISON_MATCHER_( \
812b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    name, op, relation, negated_relation) \
813e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename Rhs> class name##Matcher { \
814e35fdd936d133bf8a48de140a3c666897588a05shiqian   public: \
815e35fdd936d133bf8a48de140a3c666897588a05shiqian    explicit name##Matcher(const Rhs& rhs) : rhs_(rhs) {} \
816e35fdd936d133bf8a48de140a3c666897588a05shiqian    template <typename Lhs> \
817e35fdd936d133bf8a48de140a3c666897588a05shiqian    operator Matcher<Lhs>() const { \
818e35fdd936d133bf8a48de140a3c666897588a05shiqian      return MakeMatcher(new Impl<Lhs>(rhs_)); \
819e35fdd936d133bf8a48de140a3c666897588a05shiqian    } \
820e35fdd936d133bf8a48de140a3c666897588a05shiqian   private: \
821e35fdd936d133bf8a48de140a3c666897588a05shiqian    template <typename Lhs> \
822e35fdd936d133bf8a48de140a3c666897588a05shiqian    class Impl : public MatcherInterface<Lhs> { \
823e35fdd936d133bf8a48de140a3c666897588a05shiqian     public: \
824e35fdd936d133bf8a48de140a3c666897588a05shiqian      explicit Impl(const Rhs& rhs) : rhs_(rhs) {} \
825821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      virtual bool MatchAndExplain(\
826821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan          Lhs lhs, MatchResultListener* /* listener */) const { \
827821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan        return lhs op rhs_; \
828821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      } \
829e35fdd936d133bf8a48de140a3c666897588a05shiqian      virtual void DescribeTo(::std::ostream* os) const { \
830b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan        *os << relation  " "; \
831e2e8ba401d198d1a8304c652e997505609b62696vladlosev        UniversalPrint(rhs_, os); \
832e35fdd936d133bf8a48de140a3c666897588a05shiqian      } \
833e35fdd936d133bf8a48de140a3c666897588a05shiqian      virtual void DescribeNegationTo(::std::ostream* os) const { \
834b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan        *os << negated_relation  " "; \
835e2e8ba401d198d1a8304c652e997505609b62696vladlosev        UniversalPrint(rhs_, os); \
836e35fdd936d133bf8a48de140a3c666897588a05shiqian      } \
837e35fdd936d133bf8a48de140a3c666897588a05shiqian     private: \
838e35fdd936d133bf8a48de140a3c666897588a05shiqian      Rhs rhs_; \
83932de5f53763125925e078498250f7e73a88de9edzhanyong.wan      GTEST_DISALLOW_ASSIGN_(Impl); \
840e35fdd936d133bf8a48de140a3c666897588a05shiqian    }; \
841e35fdd936d133bf8a48de140a3c666897588a05shiqian    Rhs rhs_; \
84232de5f53763125925e078498250f7e73a88de9edzhanyong.wan    GTEST_DISALLOW_ASSIGN_(name##Matcher); \
843e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
844e35fdd936d133bf8a48de140a3c666897588a05shiqian
845e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements Eq(v), Ge(v), Gt(v), Le(v), Lt(v), and Ne(v)
846e35fdd936d133bf8a48de140a3c666897588a05shiqian// respectively.
847b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Eq, ==, "is equal to", "isn't equal to");
848b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Ge, >=, "is >=", "isn't >=");
849b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Gt, >, "is >", "isn't >");
850b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Le, <=, "is <=", "isn't <=");
851b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Lt, <, "is <", "isn't <");
852b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Ne, !=, "isn't equal to", "is equal to");
853e35fdd936d133bf8a48de140a3c666897588a05shiqian
854e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan#undef GMOCK_IMPLEMENT_COMPARISON_MATCHER_
855e35fdd936d133bf8a48de140a3c666897588a05shiqian
85679b83505bcf73bf2903ebf2e2f82cb1e1f181816vladlosev// Implements the polymorphic IsNull() matcher, which matches any raw or smart
8572d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan// pointer that is NULL.
8582d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wanclass IsNullMatcher {
8592d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan public:
86079b83505bcf73bf2903ebf2e2f82cb1e1f181816vladlosev  template <typename Pointer>
861db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplain(const Pointer& p,
862db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                       MatchResultListener* /* listener */) const {
863db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return GetRawPointer(p) == NULL;
864db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  }
8652d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan
8662d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan  void DescribeTo(::std::ostream* os) const { *os << "is NULL"; }
8672d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan  void DescribeNegationTo(::std::ostream* os) const {
868b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    *os << "isn't NULL";
8692d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan  }
8702d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan};
8712d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan
87279b83505bcf73bf2903ebf2e2f82cb1e1f181816vladlosev// Implements the polymorphic NotNull() matcher, which matches any raw or smart
873e35fdd936d133bf8a48de140a3c666897588a05shiqian// pointer that is not NULL.
874e35fdd936d133bf8a48de140a3c666897588a05shiqianclass NotNullMatcher {
875e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
87679b83505bcf73bf2903ebf2e2f82cb1e1f181816vladlosev  template <typename Pointer>
877db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplain(const Pointer& p,
878db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                       MatchResultListener* /* listener */) const {
879db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return GetRawPointer(p) != NULL;
880db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  }
881e35fdd936d133bf8a48de140a3c666897588a05shiqian
882b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan  void DescribeTo(::std::ostream* os) const { *os << "isn't NULL"; }
883e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
884e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "is NULL";
885e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
886e35fdd936d133bf8a48de140a3c666897588a05shiqian};
887e35fdd936d133bf8a48de140a3c666897588a05shiqian
888e35fdd936d133bf8a48de140a3c666897588a05shiqian// Ref(variable) matches any argument that is a reference to
889e35fdd936d133bf8a48de140a3c666897588a05shiqian// 'variable'.  This matcher is polymorphic as it can match any
890e35fdd936d133bf8a48de140a3c666897588a05shiqian// super type of the type of 'variable'.
891e35fdd936d133bf8a48de140a3c666897588a05shiqian//
892e35fdd936d133bf8a48de140a3c666897588a05shiqian// The RefMatcher template class implements Ref(variable).  It can
893e35fdd936d133bf8a48de140a3c666897588a05shiqian// only be instantiated with a reference type.  This prevents a user
894e35fdd936d133bf8a48de140a3c666897588a05shiqian// from mistakenly using Ref(x) to match a non-reference function
895e35fdd936d133bf8a48de140a3c666897588a05shiqian// argument.  For example, the following will righteously cause a
896e35fdd936d133bf8a48de140a3c666897588a05shiqian// compiler error:
897e35fdd936d133bf8a48de140a3c666897588a05shiqian//
898e35fdd936d133bf8a48de140a3c666897588a05shiqian//   int n;
899e35fdd936d133bf8a48de140a3c666897588a05shiqian//   Matcher<int> m1 = Ref(n);   // This won't compile.
900e35fdd936d133bf8a48de140a3c666897588a05shiqian//   Matcher<int&> m2 = Ref(n);  // This will compile.
901e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
902e35fdd936d133bf8a48de140a3c666897588a05shiqianclass RefMatcher;
903e35fdd936d133bf8a48de140a3c666897588a05shiqian
904e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
905e35fdd936d133bf8a48de140a3c666897588a05shiqianclass RefMatcher<T&> {
906e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Google Mock is a generic framework and thus needs to support
907e35fdd936d133bf8a48de140a3c666897588a05shiqian  // mocking any function types, including those that take non-const
908e35fdd936d133bf8a48de140a3c666897588a05shiqian  // reference arguments.  Therefore the template parameter T (and
909e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Super below) can be instantiated to either a const type or a
910e35fdd936d133bf8a48de140a3c666897588a05shiqian  // non-const type.
911e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
912e35fdd936d133bf8a48de140a3c666897588a05shiqian  // RefMatcher() takes a T& instead of const T&, as we want the
913e35fdd936d133bf8a48de140a3c666897588a05shiqian  // compiler to catch using Ref(const_value) as a matcher for a
914e35fdd936d133bf8a48de140a3c666897588a05shiqian  // non-const reference.
915e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit RefMatcher(T& x) : object_(x) {}  // NOLINT
916e35fdd936d133bf8a48de140a3c666897588a05shiqian
917e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename Super>
918e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<Super&>() const {
919e35fdd936d133bf8a48de140a3c666897588a05shiqian    // By passing object_ (type T&) to Impl(), which expects a Super&,
920e35fdd936d133bf8a48de140a3c666897588a05shiqian    // we make sure that Super is a super type of T.  In particular,
921e35fdd936d133bf8a48de140a3c666897588a05shiqian    // this catches using Ref(const_value) as a matcher for a
922e35fdd936d133bf8a48de140a3c666897588a05shiqian    // non-const reference, as you cannot implicitly convert a const
923e35fdd936d133bf8a48de140a3c666897588a05shiqian    // reference to a non-const reference.
924e35fdd936d133bf8a48de140a3c666897588a05shiqian    return MakeMatcher(new Impl<Super>(object_));
925e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
92632de5f53763125925e078498250f7e73a88de9edzhanyong.wan
927e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
928e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename Super>
929e35fdd936d133bf8a48de140a3c666897588a05shiqian  class Impl : public MatcherInterface<Super&> {
930e35fdd936d133bf8a48de140a3c666897588a05shiqian   public:
931e35fdd936d133bf8a48de140a3c666897588a05shiqian    explicit Impl(Super& x) : object_(x) {}  // NOLINT
932e35fdd936d133bf8a48de140a3c666897588a05shiqian
933db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    // MatchAndExplain() takes a Super& (as opposed to const Super&)
934db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    // in order to match the interface MatcherInterface<Super&>.
935821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    virtual bool MatchAndExplain(
936821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan        Super& x, MatchResultListener* listener) const {
937b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan      *listener << "which is located @" << static_cast<const void*>(&x);
938821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      return &x == &object_;
939821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    }
940e35fdd936d133bf8a48de140a3c666897588a05shiqian
941e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeTo(::std::ostream* os) const {
942e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "references the variable ";
943e35fdd936d133bf8a48de140a3c666897588a05shiqian      UniversalPrinter<Super&>::Print(object_, os);
944e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
945e35fdd936d133bf8a48de140a3c666897588a05shiqian
946e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeNegationTo(::std::ostream* os) const {
947e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "does not reference the variable ";
948e35fdd936d133bf8a48de140a3c666897588a05shiqian      UniversalPrinter<Super&>::Print(object_, os);
949e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
950e35fdd936d133bf8a48de140a3c666897588a05shiqian
951e35fdd936d133bf8a48de140a3c666897588a05shiqian   private:
952e35fdd936d133bf8a48de140a3c666897588a05shiqian    const Super& object_;
95332de5f53763125925e078498250f7e73a88de9edzhanyong.wan
95432de5f53763125925e078498250f7e73a88de9edzhanyong.wan    GTEST_DISALLOW_ASSIGN_(Impl);
955e35fdd936d133bf8a48de140a3c666897588a05shiqian  };
956e35fdd936d133bf8a48de140a3c666897588a05shiqian
957e35fdd936d133bf8a48de140a3c666897588a05shiqian  T& object_;
95832de5f53763125925e078498250f7e73a88de9edzhanyong.wan
95932de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(RefMatcher);
960e35fdd936d133bf8a48de140a3c666897588a05shiqian};
961e35fdd936d133bf8a48de140a3c666897588a05shiqian
962e35fdd936d133bf8a48de140a3c666897588a05shiqian// Polymorphic helper functions for narrow and wide string matchers.
963e35fdd936d133bf8a48de140a3c666897588a05shiqianinline bool CaseInsensitiveCStringEquals(const char* lhs, const char* rhs) {
964e35fdd936d133bf8a48de140a3c666897588a05shiqian  return String::CaseInsensitiveCStringEquals(lhs, rhs);
965e35fdd936d133bf8a48de140a3c666897588a05shiqian}
966e35fdd936d133bf8a48de140a3c666897588a05shiqian
967e35fdd936d133bf8a48de140a3c666897588a05shiqianinline bool CaseInsensitiveCStringEquals(const wchar_t* lhs,
968e35fdd936d133bf8a48de140a3c666897588a05shiqian                                         const wchar_t* rhs) {
969e35fdd936d133bf8a48de140a3c666897588a05shiqian  return String::CaseInsensitiveWideCStringEquals(lhs, rhs);
970e35fdd936d133bf8a48de140a3c666897588a05shiqian}
971e35fdd936d133bf8a48de140a3c666897588a05shiqian
972e35fdd936d133bf8a48de140a3c666897588a05shiqian// String comparison for narrow or wide strings that can have embedded NUL
973e35fdd936d133bf8a48de140a3c666897588a05shiqian// characters.
974e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType>
975e35fdd936d133bf8a48de140a3c666897588a05shiqianbool CaseInsensitiveStringEquals(const StringType& s1,
976e35fdd936d133bf8a48de140a3c666897588a05shiqian                                 const StringType& s2) {
977e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Are the heads equal?
978e35fdd936d133bf8a48de140a3c666897588a05shiqian  if (!CaseInsensitiveCStringEquals(s1.c_str(), s2.c_str())) {
979e35fdd936d133bf8a48de140a3c666897588a05shiqian    return false;
980e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
981e35fdd936d133bf8a48de140a3c666897588a05shiqian
982e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Skip the equal heads.
983e35fdd936d133bf8a48de140a3c666897588a05shiqian  const typename StringType::value_type nul = 0;
984e35fdd936d133bf8a48de140a3c666897588a05shiqian  const size_t i1 = s1.find(nul), i2 = s2.find(nul);
985e35fdd936d133bf8a48de140a3c666897588a05shiqian
986e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Are we at the end of either s1 or s2?
987e35fdd936d133bf8a48de140a3c666897588a05shiqian  if (i1 == StringType::npos || i2 == StringType::npos) {
988e35fdd936d133bf8a48de140a3c666897588a05shiqian    return i1 == i2;
989e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
990e35fdd936d133bf8a48de140a3c666897588a05shiqian
991e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Are the tails equal?
992e35fdd936d133bf8a48de140a3c666897588a05shiqian  return CaseInsensitiveStringEquals(s1.substr(i1 + 1), s2.substr(i2 + 1));
993e35fdd936d133bf8a48de140a3c666897588a05shiqian}
994e35fdd936d133bf8a48de140a3c666897588a05shiqian
995e35fdd936d133bf8a48de140a3c666897588a05shiqian// String matchers.
996e35fdd936d133bf8a48de140a3c666897588a05shiqian
997e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements equality-based string matchers like StrEq, StrCaseNe, and etc.
998e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType>
999e35fdd936d133bf8a48de140a3c666897588a05shiqianclass StrEqualityMatcher {
1000e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1001e35fdd936d133bf8a48de140a3c666897588a05shiqian  StrEqualityMatcher(const StringType& str, bool expect_eq,
1002e35fdd936d133bf8a48de140a3c666897588a05shiqian                     bool case_sensitive)
1003e35fdd936d133bf8a48de140a3c666897588a05shiqian      : string_(str), expect_eq_(expect_eq), case_sensitive_(case_sensitive) {}
1004e35fdd936d133bf8a48de140a3c666897588a05shiqian
100538513a8bb154f0b6d0a4088814fe92552696d465jgm  // Accepts pointer types, particularly:
100638513a8bb154f0b6d0a4088814fe92552696d465jgm  //   const char*
100738513a8bb154f0b6d0a4088814fe92552696d465jgm  //   char*
100838513a8bb154f0b6d0a4088814fe92552696d465jgm  //   const wchar_t*
100938513a8bb154f0b6d0a4088814fe92552696d465jgm  //   wchar_t*
101038513a8bb154f0b6d0a4088814fe92552696d465jgm  template <typename CharType>
101138513a8bb154f0b6d0a4088814fe92552696d465jgm  bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {
1012e35fdd936d133bf8a48de140a3c666897588a05shiqian    if (s == NULL) {
1013e35fdd936d133bf8a48de140a3c666897588a05shiqian      return !expect_eq_;
1014e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1015db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return MatchAndExplain(StringType(s), listener);
1016e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1017e35fdd936d133bf8a48de140a3c666897588a05shiqian
101838513a8bb154f0b6d0a4088814fe92552696d465jgm  // Matches anything that can convert to StringType.
101938513a8bb154f0b6d0a4088814fe92552696d465jgm  //
102038513a8bb154f0b6d0a4088814fe92552696d465jgm  // This is a template, not just a plain function with const StringType&,
102138513a8bb154f0b6d0a4088814fe92552696d465jgm  // because StringPiece has some interfering non-explicit constructors.
102238513a8bb154f0b6d0a4088814fe92552696d465jgm  template <typename MatcheeStringType>
102338513a8bb154f0b6d0a4088814fe92552696d465jgm  bool MatchAndExplain(const MatcheeStringType& s,
1024db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                       MatchResultListener* /* listener */) const {
102538513a8bb154f0b6d0a4088814fe92552696d465jgm    const StringType& s2(s);
102638513a8bb154f0b6d0a4088814fe92552696d465jgm    const bool eq = case_sensitive_ ? s2 == string_ :
102738513a8bb154f0b6d0a4088814fe92552696d465jgm        CaseInsensitiveStringEquals(s2, string_);
1028e35fdd936d133bf8a48de140a3c666897588a05shiqian    return expect_eq_ == eq;
1029e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1030e35fdd936d133bf8a48de140a3c666897588a05shiqian
1031e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
1032e35fdd936d133bf8a48de140a3c666897588a05shiqian    DescribeToHelper(expect_eq_, os);
1033e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1034e35fdd936d133bf8a48de140a3c666897588a05shiqian
1035e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
1036e35fdd936d133bf8a48de140a3c666897588a05shiqian    DescribeToHelper(!expect_eq_, os);
1037e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
103832de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1039e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1040e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeToHelper(bool expect_eq, ::std::ostream* os) const {
1041b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    *os << (expect_eq ? "is " : "isn't ");
1042e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "equal to ";
1043e35fdd936d133bf8a48de140a3c666897588a05shiqian    if (!case_sensitive_) {
1044e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "(ignoring case) ";
1045e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1046e2e8ba401d198d1a8304c652e997505609b62696vladlosev    UniversalPrint(string_, os);
1047e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1048e35fdd936d133bf8a48de140a3c666897588a05shiqian
1049e35fdd936d133bf8a48de140a3c666897588a05shiqian  const StringType string_;
1050e35fdd936d133bf8a48de140a3c666897588a05shiqian  const bool expect_eq_;
1051e35fdd936d133bf8a48de140a3c666897588a05shiqian  const bool case_sensitive_;
105232de5f53763125925e078498250f7e73a88de9edzhanyong.wan
105332de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(StrEqualityMatcher);
1054e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1055e35fdd936d133bf8a48de140a3c666897588a05shiqian
1056e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the polymorphic HasSubstr(substring) matcher, which
1057e35fdd936d133bf8a48de140a3c666897588a05shiqian// can be used as a Matcher<T> as long as T can be converted to a
1058e35fdd936d133bf8a48de140a3c666897588a05shiqian// string.
1059e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType>
1060e35fdd936d133bf8a48de140a3c666897588a05shiqianclass HasSubstrMatcher {
1061e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1062e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit HasSubstrMatcher(const StringType& substring)
1063e35fdd936d133bf8a48de140a3c666897588a05shiqian      : substring_(substring) {}
1064e35fdd936d133bf8a48de140a3c666897588a05shiqian
106538513a8bb154f0b6d0a4088814fe92552696d465jgm  // Accepts pointer types, particularly:
106638513a8bb154f0b6d0a4088814fe92552696d465jgm  //   const char*
106738513a8bb154f0b6d0a4088814fe92552696d465jgm  //   char*
106838513a8bb154f0b6d0a4088814fe92552696d465jgm  //   const wchar_t*
106938513a8bb154f0b6d0a4088814fe92552696d465jgm  //   wchar_t*
107038513a8bb154f0b6d0a4088814fe92552696d465jgm  template <typename CharType>
107138513a8bb154f0b6d0a4088814fe92552696d465jgm  bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {
1072db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return s != NULL && MatchAndExplain(StringType(s), listener);
1073e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1074e35fdd936d133bf8a48de140a3c666897588a05shiqian
107538513a8bb154f0b6d0a4088814fe92552696d465jgm  // Matches anything that can convert to StringType.
107638513a8bb154f0b6d0a4088814fe92552696d465jgm  //
107738513a8bb154f0b6d0a4088814fe92552696d465jgm  // This is a template, not just a plain function with const StringType&,
107838513a8bb154f0b6d0a4088814fe92552696d465jgm  // because StringPiece has some interfering non-explicit constructors.
107938513a8bb154f0b6d0a4088814fe92552696d465jgm  template <typename MatcheeStringType>
108038513a8bb154f0b6d0a4088814fe92552696d465jgm  bool MatchAndExplain(const MatcheeStringType& s,
1081db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                       MatchResultListener* /* listener */) const {
108238513a8bb154f0b6d0a4088814fe92552696d465jgm    const StringType& s2(s);
108338513a8bb154f0b6d0a4088814fe92552696d465jgm    return s2.find(substring_) != StringType::npos;
1084e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1085e35fdd936d133bf8a48de140a3c666897588a05shiqian
1086e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Describes what this matcher matches.
1087e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
1088e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "has substring ";
1089e2e8ba401d198d1a8304c652e997505609b62696vladlosev    UniversalPrint(substring_, os);
1090e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1091e35fdd936d133bf8a48de140a3c666897588a05shiqian
1092e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
1093e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "has no substring ";
1094e2e8ba401d198d1a8304c652e997505609b62696vladlosev    UniversalPrint(substring_, os);
1095e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
109632de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1097e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1098e35fdd936d133bf8a48de140a3c666897588a05shiqian  const StringType substring_;
109932de5f53763125925e078498250f7e73a88de9edzhanyong.wan
110032de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(HasSubstrMatcher);
1101e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1102e35fdd936d133bf8a48de140a3c666897588a05shiqian
1103e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the polymorphic StartsWith(substring) matcher, which
1104e35fdd936d133bf8a48de140a3c666897588a05shiqian// can be used as a Matcher<T> as long as T can be converted to a
1105e35fdd936d133bf8a48de140a3c666897588a05shiqian// string.
1106e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType>
1107e35fdd936d133bf8a48de140a3c666897588a05shiqianclass StartsWithMatcher {
1108e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1109e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit StartsWithMatcher(const StringType& prefix) : prefix_(prefix) {
1110e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1111e35fdd936d133bf8a48de140a3c666897588a05shiqian
111238513a8bb154f0b6d0a4088814fe92552696d465jgm  // Accepts pointer types, particularly:
111338513a8bb154f0b6d0a4088814fe92552696d465jgm  //   const char*
111438513a8bb154f0b6d0a4088814fe92552696d465jgm  //   char*
111538513a8bb154f0b6d0a4088814fe92552696d465jgm  //   const wchar_t*
111638513a8bb154f0b6d0a4088814fe92552696d465jgm  //   wchar_t*
111738513a8bb154f0b6d0a4088814fe92552696d465jgm  template <typename CharType>
111838513a8bb154f0b6d0a4088814fe92552696d465jgm  bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {
1119db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return s != NULL && MatchAndExplain(StringType(s), listener);
1120e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1121e35fdd936d133bf8a48de140a3c666897588a05shiqian
112238513a8bb154f0b6d0a4088814fe92552696d465jgm  // Matches anything that can convert to StringType.
112338513a8bb154f0b6d0a4088814fe92552696d465jgm  //
112438513a8bb154f0b6d0a4088814fe92552696d465jgm  // This is a template, not just a plain function with const StringType&,
112538513a8bb154f0b6d0a4088814fe92552696d465jgm  // because StringPiece has some interfering non-explicit constructors.
112638513a8bb154f0b6d0a4088814fe92552696d465jgm  template <typename MatcheeStringType>
112738513a8bb154f0b6d0a4088814fe92552696d465jgm  bool MatchAndExplain(const MatcheeStringType& s,
1128db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                       MatchResultListener* /* listener */) const {
112938513a8bb154f0b6d0a4088814fe92552696d465jgm    const StringType& s2(s);
113038513a8bb154f0b6d0a4088814fe92552696d465jgm    return s2.length() >= prefix_.length() &&
113138513a8bb154f0b6d0a4088814fe92552696d465jgm        s2.substr(0, prefix_.length()) == prefix_;
1132e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1133e35fdd936d133bf8a48de140a3c666897588a05shiqian
1134e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
1135e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "starts with ";
1136e2e8ba401d198d1a8304c652e997505609b62696vladlosev    UniversalPrint(prefix_, os);
1137e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1138e35fdd936d133bf8a48de140a3c666897588a05shiqian
1139e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
1140e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "doesn't start with ";
1141e2e8ba401d198d1a8304c652e997505609b62696vladlosev    UniversalPrint(prefix_, os);
1142e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
114332de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1144e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1145e35fdd936d133bf8a48de140a3c666897588a05shiqian  const StringType prefix_;
114632de5f53763125925e078498250f7e73a88de9edzhanyong.wan
114732de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(StartsWithMatcher);
1148e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1149e35fdd936d133bf8a48de140a3c666897588a05shiqian
1150e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the polymorphic EndsWith(substring) matcher, which
1151e35fdd936d133bf8a48de140a3c666897588a05shiqian// can be used as a Matcher<T> as long as T can be converted to a
1152e35fdd936d133bf8a48de140a3c666897588a05shiqian// string.
1153e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType>
1154e35fdd936d133bf8a48de140a3c666897588a05shiqianclass EndsWithMatcher {
1155e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1156e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit EndsWithMatcher(const StringType& suffix) : suffix_(suffix) {}
1157e35fdd936d133bf8a48de140a3c666897588a05shiqian
115838513a8bb154f0b6d0a4088814fe92552696d465jgm  // Accepts pointer types, particularly:
115938513a8bb154f0b6d0a4088814fe92552696d465jgm  //   const char*
116038513a8bb154f0b6d0a4088814fe92552696d465jgm  //   char*
116138513a8bb154f0b6d0a4088814fe92552696d465jgm  //   const wchar_t*
116238513a8bb154f0b6d0a4088814fe92552696d465jgm  //   wchar_t*
116338513a8bb154f0b6d0a4088814fe92552696d465jgm  template <typename CharType>
116438513a8bb154f0b6d0a4088814fe92552696d465jgm  bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {
1165db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return s != NULL && MatchAndExplain(StringType(s), listener);
1166e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1167e35fdd936d133bf8a48de140a3c666897588a05shiqian
116838513a8bb154f0b6d0a4088814fe92552696d465jgm  // Matches anything that can convert to StringType.
116938513a8bb154f0b6d0a4088814fe92552696d465jgm  //
117038513a8bb154f0b6d0a4088814fe92552696d465jgm  // This is a template, not just a plain function with const StringType&,
117138513a8bb154f0b6d0a4088814fe92552696d465jgm  // because StringPiece has some interfering non-explicit constructors.
117238513a8bb154f0b6d0a4088814fe92552696d465jgm  template <typename MatcheeStringType>
117338513a8bb154f0b6d0a4088814fe92552696d465jgm  bool MatchAndExplain(const MatcheeStringType& s,
1174db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                       MatchResultListener* /* listener */) const {
117538513a8bb154f0b6d0a4088814fe92552696d465jgm    const StringType& s2(s);
117638513a8bb154f0b6d0a4088814fe92552696d465jgm    return s2.length() >= suffix_.length() &&
117738513a8bb154f0b6d0a4088814fe92552696d465jgm        s2.substr(s2.length() - suffix_.length()) == suffix_;
1178e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1179e35fdd936d133bf8a48de140a3c666897588a05shiqian
1180e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
1181e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "ends with ";
1182e2e8ba401d198d1a8304c652e997505609b62696vladlosev    UniversalPrint(suffix_, os);
1183e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1184e35fdd936d133bf8a48de140a3c666897588a05shiqian
1185e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
1186e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "doesn't end with ";
1187e2e8ba401d198d1a8304c652e997505609b62696vladlosev    UniversalPrint(suffix_, os);
1188e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
118932de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1190e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1191e35fdd936d133bf8a48de140a3c666897588a05shiqian  const StringType suffix_;
119232de5f53763125925e078498250f7e73a88de9edzhanyong.wan
119332de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(EndsWithMatcher);
1194e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1195e35fdd936d133bf8a48de140a3c666897588a05shiqian
1196e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements polymorphic matchers MatchesRegex(regex) and
1197e35fdd936d133bf8a48de140a3c666897588a05shiqian// ContainsRegex(regex), which can be used as a Matcher<T> as long as
1198e35fdd936d133bf8a48de140a3c666897588a05shiqian// T can be converted to a string.
1199e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatchesRegexMatcher {
1200e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1201e35fdd936d133bf8a48de140a3c666897588a05shiqian  MatchesRegexMatcher(const RE* regex, bool full_match)
1202e35fdd936d133bf8a48de140a3c666897588a05shiqian      : regex_(regex), full_match_(full_match) {}
1203e35fdd936d133bf8a48de140a3c666897588a05shiqian
120438513a8bb154f0b6d0a4088814fe92552696d465jgm  // Accepts pointer types, particularly:
120538513a8bb154f0b6d0a4088814fe92552696d465jgm  //   const char*
120638513a8bb154f0b6d0a4088814fe92552696d465jgm  //   char*
120738513a8bb154f0b6d0a4088814fe92552696d465jgm  //   const wchar_t*
120838513a8bb154f0b6d0a4088814fe92552696d465jgm  //   wchar_t*
120938513a8bb154f0b6d0a4088814fe92552696d465jgm  template <typename CharType>
121038513a8bb154f0b6d0a4088814fe92552696d465jgm  bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {
1211db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return s != NULL && MatchAndExplain(internal::string(s), listener);
1212e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1213e35fdd936d133bf8a48de140a3c666897588a05shiqian
121438513a8bb154f0b6d0a4088814fe92552696d465jgm  // Matches anything that can convert to internal::string.
121538513a8bb154f0b6d0a4088814fe92552696d465jgm  //
121638513a8bb154f0b6d0a4088814fe92552696d465jgm  // This is a template, not just a plain function with const internal::string&,
121738513a8bb154f0b6d0a4088814fe92552696d465jgm  // because StringPiece has some interfering non-explicit constructors.
121838513a8bb154f0b6d0a4088814fe92552696d465jgm  template <class MatcheeStringType>
121938513a8bb154f0b6d0a4088814fe92552696d465jgm  bool MatchAndExplain(const MatcheeStringType& s,
1220db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                       MatchResultListener* /* listener */) const {
122138513a8bb154f0b6d0a4088814fe92552696d465jgm    const internal::string& s2(s);
122238513a8bb154f0b6d0a4088814fe92552696d465jgm    return full_match_ ? RE::FullMatch(s2, *regex_) :
122338513a8bb154f0b6d0a4088814fe92552696d465jgm        RE::PartialMatch(s2, *regex_);
1224e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1225e35fdd936d133bf8a48de140a3c666897588a05shiqian
1226e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
1227e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << (full_match_ ? "matches" : "contains")
1228e35fdd936d133bf8a48de140a3c666897588a05shiqian        << " regular expression ";
1229e35fdd936d133bf8a48de140a3c666897588a05shiqian    UniversalPrinter<internal::string>::Print(regex_->pattern(), os);
1230e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1231e35fdd936d133bf8a48de140a3c666897588a05shiqian
1232e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
1233e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "doesn't " << (full_match_ ? "match" : "contain")
1234e35fdd936d133bf8a48de140a3c666897588a05shiqian        << " regular expression ";
1235e35fdd936d133bf8a48de140a3c666897588a05shiqian    UniversalPrinter<internal::string>::Print(regex_->pattern(), os);
1236e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
123732de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1238e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1239e35fdd936d133bf8a48de140a3c666897588a05shiqian  const internal::linked_ptr<const RE> regex_;
1240e35fdd936d133bf8a48de140a3c666897588a05shiqian  const bool full_match_;
124132de5f53763125925e078498250f7e73a88de9edzhanyong.wan
124232de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(MatchesRegexMatcher);
1243e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1244e35fdd936d133bf8a48de140a3c666897588a05shiqian
1245e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements a matcher that compares the two fields of a 2-tuple
1246e35fdd936d133bf8a48de140a3c666897588a05shiqian// using one of the ==, <=, <, etc, operators.  The two fields being
1247e35fdd936d133bf8a48de140a3c666897588a05shiqian// compared don't have to have the same type.
1248e35fdd936d133bf8a48de140a3c666897588a05shiqian//
1249e35fdd936d133bf8a48de140a3c666897588a05shiqian// The matcher defined here is polymorphic (for example, Eq() can be
1250e35fdd936d133bf8a48de140a3c666897588a05shiqian// used to match a tuple<int, short>, a tuple<const long&, double>,
1251e35fdd936d133bf8a48de140a3c666897588a05shiqian// etc).  Therefore we use a template type conversion operator in the
1252e35fdd936d133bf8a48de140a3c666897588a05shiqian// implementation.
1253e35fdd936d133bf8a48de140a3c666897588a05shiqian//
1254e35fdd936d133bf8a48de140a3c666897588a05shiqian// We define this as a macro in order to eliminate duplicated source
1255e35fdd936d133bf8a48de140a3c666897588a05shiqian// code.
1256ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan#define GMOCK_IMPLEMENT_COMPARISON2_MATCHER_(name, op, relation) \
1257e35fdd936d133bf8a48de140a3c666897588a05shiqian  class name##2Matcher { \
1258e35fdd936d133bf8a48de140a3c666897588a05shiqian   public: \
1259e35fdd936d133bf8a48de140a3c666897588a05shiqian    template <typename T1, typename T2> \
1260ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    operator Matcher< ::std::tr1::tuple<T1, T2> >() const { \
1261ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      return MakeMatcher(new Impl< ::std::tr1::tuple<T1, T2> >); \
1262ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    } \
1263ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    template <typename T1, typename T2> \
1264e35fdd936d133bf8a48de140a3c666897588a05shiqian    operator Matcher<const ::std::tr1::tuple<T1, T2>&>() const { \
1265ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      return MakeMatcher(new Impl<const ::std::tr1::tuple<T1, T2>&>); \
1266e35fdd936d133bf8a48de140a3c666897588a05shiqian    } \
1267e35fdd936d133bf8a48de140a3c666897588a05shiqian   private: \
1268ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    template <typename Tuple> \
1269ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    class Impl : public MatcherInterface<Tuple> { \
1270e35fdd936d133bf8a48de140a3c666897588a05shiqian     public: \
1271821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      virtual bool MatchAndExplain( \
1272ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan          Tuple args, \
1273821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan          MatchResultListener* /* listener */) const { \
1274e35fdd936d133bf8a48de140a3c666897588a05shiqian        return ::std::tr1::get<0>(args) op ::std::tr1::get<1>(args); \
1275e35fdd936d133bf8a48de140a3c666897588a05shiqian      } \
1276e35fdd936d133bf8a48de140a3c666897588a05shiqian      virtual void DescribeTo(::std::ostream* os) const { \
1277ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        *os << "are " relation;                                 \
1278e35fdd936d133bf8a48de140a3c666897588a05shiqian      } \
1279e35fdd936d133bf8a48de140a3c666897588a05shiqian      virtual void DescribeNegationTo(::std::ostream* os) const { \
1280ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        *os << "aren't " relation; \
1281e35fdd936d133bf8a48de140a3c666897588a05shiqian      } \
1282e35fdd936d133bf8a48de140a3c666897588a05shiqian    }; \
1283e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1284e35fdd936d133bf8a48de140a3c666897588a05shiqian
1285e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements Eq(), Ge(), Gt(), Le(), Lt(), and Ne() respectively.
1286ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Eq, ==, "an equal pair");
1287ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(
1288ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    Ge, >=, "a pair where the first >= the second");
1289ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(
1290ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    Gt, >, "a pair where the first > the second");
1291ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(
1292ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    Le, <=, "a pair where the first <= the second");
1293ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(
1294ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    Lt, <, "a pair where the first < the second");
1295ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Ne, !=, "an unequal pair");
1296e35fdd936d133bf8a48de140a3c666897588a05shiqian
1297e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan#undef GMOCK_IMPLEMENT_COMPARISON2_MATCHER_
1298e35fdd936d133bf8a48de140a3c666897588a05shiqian
1299c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// Implements the Not(...) matcher for a particular argument type T.
1300c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// We do not nest it inside the NotMatcher class template, as that
1301c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// will prevent different instantiations of NotMatcher from sharing
1302c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// the same NotMatcherImpl<T> class.
1303c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wantemplate <typename T>
1304c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wanclass NotMatcherImpl : public MatcherInterface<T> {
1305c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan public:
1306c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  explicit NotMatcherImpl(const Matcher<T>& matcher)
1307c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      : matcher_(matcher) {}
1308c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1309821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
1310821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    return !matcher_.MatchAndExplain(x, listener);
1311c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1312c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1313c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
1314c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    matcher_.DescribeNegationTo(os);
1315c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1316c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1317c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
1318c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    matcher_.DescribeTo(os);
1319c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1320c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1321c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan private:
1322c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  const Matcher<T> matcher_;
132332de5f53763125925e078498250f7e73a88de9edzhanyong.wan
132432de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(NotMatcherImpl);
1325c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan};
1326c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1327e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the Not(m) matcher, which matches a value that doesn't
1328e35fdd936d133bf8a48de140a3c666897588a05shiqian// match matcher m.
1329e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename InnerMatcher>
1330e35fdd936d133bf8a48de140a3c666897588a05shiqianclass NotMatcher {
1331e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1332e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit NotMatcher(InnerMatcher matcher) : matcher_(matcher) {}
1333e35fdd936d133bf8a48de140a3c666897588a05shiqian
1334e35fdd936d133bf8a48de140a3c666897588a05shiqian  // This template type conversion operator allows Not(m) to be used
1335e35fdd936d133bf8a48de140a3c666897588a05shiqian  // to match any type m can match.
1336e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1337e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<T>() const {
1338c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    return Matcher<T>(new NotMatcherImpl<T>(SafeMatcherCast<T>(matcher_)));
1339e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
134032de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1341e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1342c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  InnerMatcher matcher_;
134332de5f53763125925e078498250f7e73a88de9edzhanyong.wan
134432de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(NotMatcher);
1345c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan};
1346e35fdd936d133bf8a48de140a3c666897588a05shiqian
1347c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// Implements the AllOf(m1, m2) matcher for a particular argument type
1348c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// T. We do not nest it inside the BothOfMatcher class template, as
1349c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// that will prevent different instantiations of BothOfMatcher from
1350c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// sharing the same BothOfMatcherImpl<T> class.
1351c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wantemplate <typename T>
1352c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wanclass BothOfMatcherImpl : public MatcherInterface<T> {
1353c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan public:
1354c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  BothOfMatcherImpl(const Matcher<T>& matcher1, const Matcher<T>& matcher2)
1355c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      : matcher1_(matcher1), matcher2_(matcher2) {}
1356e35fdd936d133bf8a48de140a3c666897588a05shiqian
1357c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
1358c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    *os << "(";
1359c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    matcher1_.DescribeTo(os);
1360c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    *os << ") and (";
1361c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    matcher2_.DescribeTo(os);
1362c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    *os << ")";
1363c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1364e35fdd936d133bf8a48de140a3c666897588a05shiqian
1365c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
1366b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    *os << "(";
1367b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    matcher1_.DescribeNegationTo(os);
1368b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    *os << ") or (";
1369b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    matcher2_.DescribeNegationTo(os);
1370b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    *os << ")";
1371c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1372e35fdd936d133bf8a48de140a3c666897588a05shiqian
1373821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
1374821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // If either matcher1_ or matcher2_ doesn't match x, we only need
1375821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // to explain why one of them fails.
1376821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    StringMatchResultListener listener1;
1377821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (!matcher1_.MatchAndExplain(x, &listener1)) {
1378821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      *listener << listener1.str();
1379821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      return false;
1380821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    }
1381c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1382821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    StringMatchResultListener listener2;
1383821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (!matcher2_.MatchAndExplain(x, &listener2)) {
1384821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      *listener << listener2.str();
1385821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      return false;
1386821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    }
1387c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1388821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // Otherwise we need to explain why *both* of them match.
1389821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    const internal::string s1 = listener1.str();
1390821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    const internal::string s2 = listener2.str();
1391821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
1392821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (s1 == "") {
1393821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      *listener << s2;
1394c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    } else {
1395821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      *listener << s1;
1396821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      if (s2 != "") {
1397b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan        *listener << ", and " << s2;
1398c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      }
1399c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    }
1400821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    return true;
1401c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
140232de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1403c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan private:
1404c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  const Matcher<T> matcher1_;
1405c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  const Matcher<T> matcher2_;
140632de5f53763125925e078498250f7e73a88de9edzhanyong.wan
140732de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(BothOfMatcherImpl);
1408e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1409e35fdd936d133bf8a48de140a3c666897588a05shiqian
1410616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan#if GTEST_LANG_CXX11
1411616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// MatcherList provides mechanisms for storing a variable number of matchers in
1412616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// a list structure (ListType) and creating a combining matcher from such a
1413616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// list.
1414616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// The template is defined recursively using the following template paramters:
1415616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan//   * kSize is the length of the MatcherList.
1416616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan//   * Head is the type of the first matcher of the list.
1417616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan//   * Tail denotes the types of the remaining matchers of the list.
1418616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wantemplate <int kSize, typename Head, typename... Tail>
1419616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wanstruct MatcherList {
1420616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  typedef MatcherList<kSize - 1, Tail...> MatcherListTail;
1421616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  typedef pair<Head, typename MatcherListTail::ListType> ListType;
1422616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1423616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // BuildList stores variadic type values in a nested pair structure.
1424616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // Example:
1425616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // MatcherList<3, int, string, float>::BuildList(5, "foo", 2.0) will return
1426616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // the corresponding result of type pair<int, pair<string, float>>.
1427616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  static ListType BuildList(const Head& matcher, const Tail&... tail) {
1428616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    return ListType(matcher, MatcherListTail::BuildList(tail...));
1429616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  }
1430616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1431616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // CreateMatcher<T> creates a Matcher<T> from a given list of matchers (built
1432616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // by BuildList()). CombiningMatcher<T> is used to combine the matchers of the
1433616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // list. CombiningMatcher<T> must implement MatcherInterface<T> and have a
1434616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // constructor taking two Matcher<T>s as input.
1435616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  template <typename T, template <typename /* T */> class CombiningMatcher>
1436616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  static Matcher<T> CreateMatcher(const ListType& matchers) {
1437616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    return Matcher<T>(new CombiningMatcher<T>(
1438616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        SafeMatcherCast<T>(matchers.first),
1439616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        MatcherListTail::template CreateMatcher<T, CombiningMatcher>(
1440616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan            matchers.second)));
1441616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  }
1442616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan};
1443616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1444616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// The following defines the base case for the recursive definition of
1445616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// MatcherList.
1446616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wantemplate <typename Matcher1, typename Matcher2>
1447616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wanstruct MatcherList<2, Matcher1, Matcher2> {
1448616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  typedef pair<Matcher1, Matcher2> ListType;
1449616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1450616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  static ListType BuildList(const Matcher1& matcher1,
1451616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan                            const Matcher2& matcher2) {
1452616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    return pair<Matcher1, Matcher2>(matcher1, matcher2);
1453616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  }
1454616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1455616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  template <typename T, template <typename /* T */> class CombiningMatcher>
1456616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  static Matcher<T> CreateMatcher(const ListType& matchers) {
1457616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    return Matcher<T>(new CombiningMatcher<T>(
1458616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        SafeMatcherCast<T>(matchers.first),
1459616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        SafeMatcherCast<T>(matchers.second)));
1460616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  }
1461616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan};
1462616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1463616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// VariadicMatcher is used for the variadic implementation of
1464616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// AllOf(m_1, m_2, ...) and AnyOf(m_1, m_2, ...).
1465616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// CombiningMatcher<T> is used to recursively combine the provided matchers
1466616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// (of type Args...).
1467616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wantemplate <template <typename T> class CombiningMatcher, typename... Args>
1468616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wanclass VariadicMatcher {
1469616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan public:
1470616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  VariadicMatcher(const Args&... matchers)  // NOLINT
1471616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan      : matchers_(MatcherListType::BuildList(matchers...)) {}
1472616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1473616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // This template type conversion operator allows an
1474616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // VariadicMatcher<Matcher1, Matcher2...> object to match any type that
1475616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // all of the provided matchers (Matcher1, Matcher2, ...) can match.
1476616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  template <typename T>
1477616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  operator Matcher<T>() const {
1478616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    return MatcherListType::template CreateMatcher<T, CombiningMatcher>(
1479616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        matchers_);
1480616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  }
1481616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1482616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan private:
1483616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  typedef MatcherList<sizeof...(Args), Args...> MatcherListType;
1484616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1485616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  const typename MatcherListType::ListType matchers_;
1486616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1487616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  GTEST_DISALLOW_ASSIGN_(VariadicMatcher);
1488616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan};
1489616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1490616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wantemplate <typename... Args>
1491616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wanusing AllOfMatcher = VariadicMatcher<BothOfMatcherImpl, Args...>;
1492616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1493616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan#endif  // GTEST_LANG_CXX11
1494616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1495e35fdd936d133bf8a48de140a3c666897588a05shiqian// Used for implementing the AllOf(m_1, ..., m_n) matcher, which
1496e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a value that matches all of the matchers m_1, ..., and m_n.
1497e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2>
1498e35fdd936d133bf8a48de140a3c666897588a05shiqianclass BothOfMatcher {
1499e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1500e35fdd936d133bf8a48de140a3c666897588a05shiqian  BothOfMatcher(Matcher1 matcher1, Matcher2 matcher2)
1501e35fdd936d133bf8a48de140a3c666897588a05shiqian      : matcher1_(matcher1), matcher2_(matcher2) {}
1502e35fdd936d133bf8a48de140a3c666897588a05shiqian
1503e35fdd936d133bf8a48de140a3c666897588a05shiqian  // This template type conversion operator allows a
1504e35fdd936d133bf8a48de140a3c666897588a05shiqian  // BothOfMatcher<Matcher1, Matcher2> object to match any type that
1505e35fdd936d133bf8a48de140a3c666897588a05shiqian  // both Matcher1 and Matcher2 can match.
1506e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1507e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<T>() const {
1508c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    return Matcher<T>(new BothOfMatcherImpl<T>(SafeMatcherCast<T>(matcher1_),
1509c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan                                               SafeMatcherCast<T>(matcher2_)));
1510e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
151132de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1512e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1513c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  Matcher1 matcher1_;
1514c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  Matcher2 matcher2_;
151532de5f53763125925e078498250f7e73a88de9edzhanyong.wan
151632de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(BothOfMatcher);
1517c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan};
1518e35fdd936d133bf8a48de140a3c666897588a05shiqian
1519c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// Implements the AnyOf(m1, m2) matcher for a particular argument type
1520c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// T.  We do not nest it inside the AnyOfMatcher class template, as
1521c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// that will prevent different instantiations of AnyOfMatcher from
1522c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// sharing the same EitherOfMatcherImpl<T> class.
1523c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wantemplate <typename T>
1524c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wanclass EitherOfMatcherImpl : public MatcherInterface<T> {
1525c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan public:
1526c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  EitherOfMatcherImpl(const Matcher<T>& matcher1, const Matcher<T>& matcher2)
1527c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      : matcher1_(matcher1), matcher2_(matcher2) {}
1528e35fdd936d133bf8a48de140a3c666897588a05shiqian
1529c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
1530c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    *os << "(";
1531c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    matcher1_.DescribeTo(os);
1532c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    *os << ") or (";
1533c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    matcher2_.DescribeTo(os);
1534c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    *os << ")";
1535c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1536e35fdd936d133bf8a48de140a3c666897588a05shiqian
1537c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
1538b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    *os << "(";
1539b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    matcher1_.DescribeNegationTo(os);
1540b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    *os << ") and (";
1541b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    matcher2_.DescribeNegationTo(os);
1542b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    *os << ")";
1543c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1544c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1545821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
1546821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // If either matcher1_ or matcher2_ matches x, we just need to
1547821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // explain why *one* of them matches.
1548821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    StringMatchResultListener listener1;
1549821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (matcher1_.MatchAndExplain(x, &listener1)) {
1550821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      *listener << listener1.str();
1551821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      return true;
1552821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    }
1553c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1554821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    StringMatchResultListener listener2;
1555821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (matcher2_.MatchAndExplain(x, &listener2)) {
1556821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      *listener << listener2.str();
1557821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      return true;
1558821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    }
1559c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1560821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // Otherwise we need to explain why *both* of them fail.
1561821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    const internal::string s1 = listener1.str();
1562821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    const internal::string s2 = listener2.str();
1563821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
1564821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (s1 == "") {
1565821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      *listener << s2;
1566821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    } else {
1567821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      *listener << s1;
1568821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      if (s2 != "") {
1569b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan        *listener << ", and " << s2;
1570e35fdd936d133bf8a48de140a3c666897588a05shiqian      }
1571e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1572821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    return false;
1573c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
157432de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1575c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan private:
1576c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  const Matcher<T> matcher1_;
1577c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  const Matcher<T> matcher2_;
157832de5f53763125925e078498250f7e73a88de9edzhanyong.wan
157932de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(EitherOfMatcherImpl);
1580e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1581e35fdd936d133bf8a48de140a3c666897588a05shiqian
1582616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan#if GTEST_LANG_CXX11
1583616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// AnyOfMatcher is used for the variadic implementation of AnyOf(m_1, m_2, ...).
1584616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wantemplate <typename... Args>
1585616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wanusing AnyOfMatcher = VariadicMatcher<EitherOfMatcherImpl, Args...>;
1586616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1587616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan#endif  // GTEST_LANG_CXX11
1588616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1589e35fdd936d133bf8a48de140a3c666897588a05shiqian// Used for implementing the AnyOf(m_1, ..., m_n) matcher, which
1590e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a value that matches at least one of the matchers m_1, ...,
1591e35fdd936d133bf8a48de140a3c666897588a05shiqian// and m_n.
1592e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2>
1593e35fdd936d133bf8a48de140a3c666897588a05shiqianclass EitherOfMatcher {
1594e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1595e35fdd936d133bf8a48de140a3c666897588a05shiqian  EitherOfMatcher(Matcher1 matcher1, Matcher2 matcher2)
1596e35fdd936d133bf8a48de140a3c666897588a05shiqian      : matcher1_(matcher1), matcher2_(matcher2) {}
1597e35fdd936d133bf8a48de140a3c666897588a05shiqian
1598e35fdd936d133bf8a48de140a3c666897588a05shiqian  // This template type conversion operator allows a
1599e35fdd936d133bf8a48de140a3c666897588a05shiqian  // EitherOfMatcher<Matcher1, Matcher2> object to match any type that
1600e35fdd936d133bf8a48de140a3c666897588a05shiqian  // both Matcher1 and Matcher2 can match.
1601e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1602e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<T>() const {
160316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan    return Matcher<T>(new EitherOfMatcherImpl<T>(
160416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan        SafeMatcherCast<T>(matcher1_), SafeMatcherCast<T>(matcher2_)));
1605e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
160632de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1607e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1608e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher1 matcher1_;
1609e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher2 matcher2_;
161032de5f53763125925e078498250f7e73a88de9edzhanyong.wan
161132de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(EitherOfMatcher);
1612e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1613e35fdd936d133bf8a48de140a3c666897588a05shiqian
1614e35fdd936d133bf8a48de140a3c666897588a05shiqian// Used for implementing Truly(pred), which turns a predicate into a
1615e35fdd936d133bf8a48de140a3c666897588a05shiqian// matcher.
1616e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Predicate>
1617e35fdd936d133bf8a48de140a3c666897588a05shiqianclass TrulyMatcher {
1618e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1619e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit TrulyMatcher(Predicate pred) : predicate_(pred) {}
1620e35fdd936d133bf8a48de140a3c666897588a05shiqian
1621e35fdd936d133bf8a48de140a3c666897588a05shiqian  // This method template allows Truly(pred) to be used as a matcher
1622e35fdd936d133bf8a48de140a3c666897588a05shiqian  // for type T where T is the argument type of predicate 'pred'.  The
1623e35fdd936d133bf8a48de140a3c666897588a05shiqian  // argument is passed by reference as the predicate may be
1624e35fdd936d133bf8a48de140a3c666897588a05shiqian  // interested in the address of the argument.
1625e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1626db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplain(T& x,  // NOLINT
1627db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                       MatchResultListener* /* listener */) const {
16288d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan    // Without the if-statement, MSVC sometimes warns about converting
16298d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan    // a value to bool (warning 4800).
16308d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan    //
16318d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan    // We cannot write 'return !!predicate_(x);' as that doesn't work
16328d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan    // when predicate_(x) returns a class convertible to bool but
16338d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan    // having no operator!().
16348d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan    if (predicate_(x))
16358d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan      return true;
16368d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan    return false;
1637e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1638e35fdd936d133bf8a48de140a3c666897588a05shiqian
1639e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
1640e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "satisfies the given predicate";
1641e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1642e35fdd936d133bf8a48de140a3c666897588a05shiqian
1643e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
1644e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "doesn't satisfy the given predicate";
1645e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
164632de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1647e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1648e35fdd936d133bf8a48de140a3c666897588a05shiqian  Predicate predicate_;
164932de5f53763125925e078498250f7e73a88de9edzhanyong.wan
165032de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(TrulyMatcher);
1651e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1652e35fdd936d133bf8a48de140a3c666897588a05shiqian
1653e35fdd936d133bf8a48de140a3c666897588a05shiqian// Used for implementing Matches(matcher), which turns a matcher into
1654e35fdd936d133bf8a48de140a3c666897588a05shiqian// a predicate.
1655e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename M>
1656e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatcherAsPredicate {
1657e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1658e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit MatcherAsPredicate(M matcher) : matcher_(matcher) {}
1659e35fdd936d133bf8a48de140a3c666897588a05shiqian
1660e35fdd936d133bf8a48de140a3c666897588a05shiqian  // This template operator() allows Matches(m) to be used as a
1661e35fdd936d133bf8a48de140a3c666897588a05shiqian  // predicate on type T where m is a matcher on type T.
1662e35fdd936d133bf8a48de140a3c666897588a05shiqian  //
1663e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The argument x is passed by reference instead of by value, as
1664e35fdd936d133bf8a48de140a3c666897588a05shiqian  // some matcher may be interested in its address (e.g. as in
1665e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Matches(Ref(n))(x)).
1666e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1667e35fdd936d133bf8a48de140a3c666897588a05shiqian  bool operator()(const T& x) const {
1668e35fdd936d133bf8a48de140a3c666897588a05shiqian    // We let matcher_ commit to a particular type here instead of
1669e35fdd936d133bf8a48de140a3c666897588a05shiqian    // when the MatcherAsPredicate object was constructed.  This
1670e35fdd936d133bf8a48de140a3c666897588a05shiqian    // allows us to write Matches(m) where m is a polymorphic matcher
1671e35fdd936d133bf8a48de140a3c666897588a05shiqian    // (e.g. Eq(5)).
1672e35fdd936d133bf8a48de140a3c666897588a05shiqian    //
1673e35fdd936d133bf8a48de140a3c666897588a05shiqian    // If we write Matcher<T>(matcher_).Matches(x) here, it won't
1674e35fdd936d133bf8a48de140a3c666897588a05shiqian    // compile when matcher_ has type Matcher<const T&>; if we write
1675e35fdd936d133bf8a48de140a3c666897588a05shiqian    // Matcher<const T&>(matcher_).Matches(x) here, it won't compile
1676e35fdd936d133bf8a48de140a3c666897588a05shiqian    // when matcher_ has type Matcher<T>; if we just write
1677e35fdd936d133bf8a48de140a3c666897588a05shiqian    // matcher_.Matches(x), it won't compile when matcher_ is
1678e35fdd936d133bf8a48de140a3c666897588a05shiqian    // polymorphic, e.g. Eq(5).
1679e35fdd936d133bf8a48de140a3c666897588a05shiqian    //
1680e35fdd936d133bf8a48de140a3c666897588a05shiqian    // MatcherCast<const T&>() is necessary for making the code work
1681e35fdd936d133bf8a48de140a3c666897588a05shiqian    // in all of the above situations.
1682e35fdd936d133bf8a48de140a3c666897588a05shiqian    return MatcherCast<const T&>(matcher_).Matches(x);
1683e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
168432de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1685e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1686e35fdd936d133bf8a48de140a3c666897588a05shiqian  M matcher_;
168732de5f53763125925e078498250f7e73a88de9edzhanyong.wan
168832de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(MatcherAsPredicate);
1689e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1690e35fdd936d133bf8a48de140a3c666897588a05shiqian
1691e35fdd936d133bf8a48de140a3c666897588a05shiqian// For implementing ASSERT_THAT() and EXPECT_THAT().  The template
1692e35fdd936d133bf8a48de140a3c666897588a05shiqian// argument M must be a type that can be converted to a matcher.
1693e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename M>
1694e35fdd936d133bf8a48de140a3c666897588a05shiqianclass PredicateFormatterFromMatcher {
1695e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1696e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit PredicateFormatterFromMatcher(const M& m) : matcher_(m) {}
1697e35fdd936d133bf8a48de140a3c666897588a05shiqian
1698e35fdd936d133bf8a48de140a3c666897588a05shiqian  // This template () operator allows a PredicateFormatterFromMatcher
1699e35fdd936d133bf8a48de140a3c666897588a05shiqian  // object to act as a predicate-formatter suitable for using with
1700e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Google Test's EXPECT_PRED_FORMAT1() macro.
1701e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1702e35fdd936d133bf8a48de140a3c666897588a05shiqian  AssertionResult operator()(const char* value_text, const T& x) const {
1703e35fdd936d133bf8a48de140a3c666897588a05shiqian    // We convert matcher_ to a Matcher<const T&> *now* instead of
1704e35fdd936d133bf8a48de140a3c666897588a05shiqian    // when the PredicateFormatterFromMatcher object was constructed,
1705e35fdd936d133bf8a48de140a3c666897588a05shiqian    // as matcher_ may be polymorphic (e.g. NotNull()) and we won't
1706e35fdd936d133bf8a48de140a3c666897588a05shiqian    // know which type to instantiate it to until we actually see the
1707e35fdd936d133bf8a48de140a3c666897588a05shiqian    // type of x here.
1708e35fdd936d133bf8a48de140a3c666897588a05shiqian    //
1709f4274520da256a3e8dd4fe2aa2e714f44ad89d18zhanyong.wan    // We write SafeMatcherCast<const T&>(matcher_) instead of
1710e35fdd936d133bf8a48de140a3c666897588a05shiqian    // Matcher<const T&>(matcher_), as the latter won't compile when
1711e35fdd936d133bf8a48de140a3c666897588a05shiqian    // matcher_ has type Matcher<T> (e.g. An<int>()).
1712f4274520da256a3e8dd4fe2aa2e714f44ad89d18zhanyong.wan    // We don't write MatcherCast<const T&> either, as that allows
1713f4274520da256a3e8dd4fe2aa2e714f44ad89d18zhanyong.wan    // potentially unsafe downcasting of the matcher argument.
1714f4274520da256a3e8dd4fe2aa2e714f44ad89d18zhanyong.wan    const Matcher<const T&> matcher = SafeMatcherCast<const T&>(matcher_);
1715821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    StringMatchResultListener listener;
1716b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    if (MatchPrintAndExplain(x, matcher, &listener))
1717e35fdd936d133bf8a48de140a3c666897588a05shiqian      return AssertionSuccess();
1718b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan
1719b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    ::std::stringstream ss;
1720b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    ss << "Value of: " << value_text << "\n"
1721b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan       << "Expected: ";
1722b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    matcher.DescribeTo(&ss);
1723b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    ss << "\n  Actual: " << listener.str();
1724b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    return AssertionFailure() << ss.str();
1725e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
172632de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1727e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1728e35fdd936d133bf8a48de140a3c666897588a05shiqian  const M matcher_;
172932de5f53763125925e078498250f7e73a88de9edzhanyong.wan
173032de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(PredicateFormatterFromMatcher);
1731e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1732e35fdd936d133bf8a48de140a3c666897588a05shiqian
1733e35fdd936d133bf8a48de140a3c666897588a05shiqian// A helper function for converting a matcher to a predicate-formatter
1734e35fdd936d133bf8a48de140a3c666897588a05shiqian// without the user needing to explicitly write the type.  This is
1735e35fdd936d133bf8a48de140a3c666897588a05shiqian// used for implementing ASSERT_THAT() and EXPECT_THAT().
1736e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename M>
1737e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PredicateFormatterFromMatcher<M>
1738e35fdd936d133bf8a48de140a3c666897588a05shiqianMakePredicateFormatterFromMatcher(const M& matcher) {
1739e35fdd936d133bf8a48de140a3c666897588a05shiqian  return PredicateFormatterFromMatcher<M>(matcher);
1740e35fdd936d133bf8a48de140a3c666897588a05shiqian}
1741e35fdd936d133bf8a48de140a3c666897588a05shiqian
1742616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// Implements the polymorphic floating point equality matcher, which matches
1743616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// two float values using ULP-based approximation or, optionally, a
1744616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// user-specified epsilon.  The template is meant to be instantiated with
1745616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// FloatType being either float or double.
1746e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename FloatType>
1747e35fdd936d133bf8a48de140a3c666897588a05shiqianclass FloatingEqMatcher {
1748e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1749e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Constructor for FloatingEqMatcher.
1750e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The matcher's input will be compared with rhs.  The matcher treats two
1751e35fdd936d133bf8a48de140a3c666897588a05shiqian  // NANs as equal if nan_eq_nan is true.  Otherwise, under IEEE standards,
1752616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // equality comparisons between NANs will always return false.  We specify a
1753616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // negative max_abs_error_ term to indicate that ULP-based approximation will
1754616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // be used for comparison.
1755e35fdd936d133bf8a48de140a3c666897588a05shiqian  FloatingEqMatcher(FloatType rhs, bool nan_eq_nan) :
1756616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    rhs_(rhs), nan_eq_nan_(nan_eq_nan), max_abs_error_(-1) {
1757616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  }
1758616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1759616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // Constructor that supports a user-specified max_abs_error that will be used
1760616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // for comparison instead of ULP-based approximation.  The max absolute
1761616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // should be non-negative.
1762616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  FloatingEqMatcher(FloatType rhs, bool nan_eq_nan, FloatType max_abs_error) :
1763616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    rhs_(rhs), nan_eq_nan_(nan_eq_nan), max_abs_error_(max_abs_error) {
1764616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    GTEST_CHECK_(max_abs_error >= 0)
1765616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        << ", where max_abs_error is" << max_abs_error;
1766616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  }
1767e35fdd936d133bf8a48de140a3c666897588a05shiqian
1768e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Implements floating point equality matcher as a Matcher<T>.
1769e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1770e35fdd936d133bf8a48de140a3c666897588a05shiqian  class Impl : public MatcherInterface<T> {
1771e35fdd936d133bf8a48de140a3c666897588a05shiqian   public:
1772616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    Impl(FloatType rhs, bool nan_eq_nan, FloatType max_abs_error) :
1773616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan      rhs_(rhs), nan_eq_nan_(nan_eq_nan), max_abs_error_(max_abs_error) {}
1774e35fdd936d133bf8a48de140a3c666897588a05shiqian
1775821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    virtual bool MatchAndExplain(T value,
1776821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan                                 MatchResultListener* /* listener */) const {
1777e35fdd936d133bf8a48de140a3c666897588a05shiqian      const FloatingPoint<FloatType> lhs(value), rhs(rhs_);
1778e35fdd936d133bf8a48de140a3c666897588a05shiqian
1779e35fdd936d133bf8a48de140a3c666897588a05shiqian      // Compares NaNs first, if nan_eq_nan_ is true.
1780616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan      if (lhs.is_nan() || rhs.is_nan()) {
1781616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        if (lhs.is_nan() && rhs.is_nan()) {
1782616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan          return nan_eq_nan_;
1783616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        }
1784616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        // One is nan; the other is not nan.
1785616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        return false;
1786616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan      }
1787616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan      if (HasMaxAbsError()) {
1788616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        // We perform an equality check so that inf will match inf, regardless
1789616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        // of error bounds.  If the result of value - rhs_ would result in
1790616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        // overflow or if either value is inf, the default result is infinity,
1791616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        // which should only match if max_abs_error_ is also infinity.
1792616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        return value == rhs_ || fabs(value - rhs_) <= max_abs_error_;
1793616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan      } else {
1794616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        return lhs.AlmostEquals(rhs);
1795e35fdd936d133bf8a48de140a3c666897588a05shiqian      }
1796e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1797e35fdd936d133bf8a48de140a3c666897588a05shiqian
1798e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeTo(::std::ostream* os) const {
1799e35fdd936d133bf8a48de140a3c666897588a05shiqian      // os->precision() returns the previously set precision, which we
1800e35fdd936d133bf8a48de140a3c666897588a05shiqian      // store to restore the ostream to its original configuration
1801e35fdd936d133bf8a48de140a3c666897588a05shiqian      // after outputting.
1802e35fdd936d133bf8a48de140a3c666897588a05shiqian      const ::std::streamsize old_precision = os->precision(
1803e35fdd936d133bf8a48de140a3c666897588a05shiqian          ::std::numeric_limits<FloatType>::digits10 + 2);
1804e35fdd936d133bf8a48de140a3c666897588a05shiqian      if (FloatingPoint<FloatType>(rhs_).is_nan()) {
1805e35fdd936d133bf8a48de140a3c666897588a05shiqian        if (nan_eq_nan_) {
1806e35fdd936d133bf8a48de140a3c666897588a05shiqian          *os << "is NaN";
1807e35fdd936d133bf8a48de140a3c666897588a05shiqian        } else {
1808e35fdd936d133bf8a48de140a3c666897588a05shiqian          *os << "never matches";
1809e35fdd936d133bf8a48de140a3c666897588a05shiqian        }
1810e35fdd936d133bf8a48de140a3c666897588a05shiqian      } else {
1811e35fdd936d133bf8a48de140a3c666897588a05shiqian        *os << "is approximately " << rhs_;
1812616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        if (HasMaxAbsError()) {
1813616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan          *os << " (absolute error <= " << max_abs_error_ << ")";
1814616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        }
1815e35fdd936d133bf8a48de140a3c666897588a05shiqian      }
1816e35fdd936d133bf8a48de140a3c666897588a05shiqian      os->precision(old_precision);
1817e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1818e35fdd936d133bf8a48de140a3c666897588a05shiqian
1819e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeNegationTo(::std::ostream* os) const {
1820e35fdd936d133bf8a48de140a3c666897588a05shiqian      // As before, get original precision.
1821e35fdd936d133bf8a48de140a3c666897588a05shiqian      const ::std::streamsize old_precision = os->precision(
1822e35fdd936d133bf8a48de140a3c666897588a05shiqian          ::std::numeric_limits<FloatType>::digits10 + 2);
1823e35fdd936d133bf8a48de140a3c666897588a05shiqian      if (FloatingPoint<FloatType>(rhs_).is_nan()) {
1824e35fdd936d133bf8a48de140a3c666897588a05shiqian        if (nan_eq_nan_) {
1825b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan          *os << "isn't NaN";
1826e35fdd936d133bf8a48de140a3c666897588a05shiqian        } else {
1827e35fdd936d133bf8a48de140a3c666897588a05shiqian          *os << "is anything";
1828e35fdd936d133bf8a48de140a3c666897588a05shiqian        }
1829e35fdd936d133bf8a48de140a3c666897588a05shiqian      } else {
1830b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan        *os << "isn't approximately " << rhs_;
1831616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        if (HasMaxAbsError()) {
1832616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan          *os << " (absolute error > " << max_abs_error_ << ")";
1833616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        }
1834e35fdd936d133bf8a48de140a3c666897588a05shiqian      }
1835e35fdd936d133bf8a48de140a3c666897588a05shiqian      // Restore original precision.
1836e35fdd936d133bf8a48de140a3c666897588a05shiqian      os->precision(old_precision);
1837e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1838e35fdd936d133bf8a48de140a3c666897588a05shiqian
1839e35fdd936d133bf8a48de140a3c666897588a05shiqian   private:
1840616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    bool HasMaxAbsError() const {
1841616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan      return max_abs_error_ >= 0;
1842616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    }
1843616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1844e35fdd936d133bf8a48de140a3c666897588a05shiqian    const FloatType rhs_;
1845e35fdd936d133bf8a48de140a3c666897588a05shiqian    const bool nan_eq_nan_;
1846616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    // max_abs_error will be used for value comparison when >= 0.
1847616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    const FloatType max_abs_error_;
184832de5f53763125925e078498250f7e73a88de9edzhanyong.wan
184932de5f53763125925e078498250f7e73a88de9edzhanyong.wan    GTEST_DISALLOW_ASSIGN_(Impl);
1850e35fdd936d133bf8a48de140a3c666897588a05shiqian  };
1851e35fdd936d133bf8a48de140a3c666897588a05shiqian
1852e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The following 3 type conversion operators allow FloatEq(rhs) and
1853e35fdd936d133bf8a48de140a3c666897588a05shiqian  // NanSensitiveFloatEq(rhs) to be used as a Matcher<float>, a
1854e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Matcher<const float&>, or a Matcher<float&>, but nothing else.
1855e35fdd936d133bf8a48de140a3c666897588a05shiqian  // (While Google's C++ coding style doesn't allow arguments passed
1856e35fdd936d133bf8a48de140a3c666897588a05shiqian  // by non-const reference, we may see them in code not conforming to
1857e35fdd936d133bf8a48de140a3c666897588a05shiqian  // the style.  Therefore Google Mock needs to support them.)
1858e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<FloatType>() const {
1859616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    return MakeMatcher(new Impl<FloatType>(rhs_, nan_eq_nan_, max_abs_error_));
1860e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1861e35fdd936d133bf8a48de140a3c666897588a05shiqian
1862e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<const FloatType&>() const {
1863616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    return MakeMatcher(
1864616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        new Impl<const FloatType&>(rhs_, nan_eq_nan_, max_abs_error_));
1865e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1866e35fdd936d133bf8a48de140a3c666897588a05shiqian
1867e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<FloatType&>() const {
1868616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    return MakeMatcher(new Impl<FloatType&>(rhs_, nan_eq_nan_, max_abs_error_));
1869e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
187079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm
1871e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1872e35fdd936d133bf8a48de140a3c666897588a05shiqian  const FloatType rhs_;
1873e35fdd936d133bf8a48de140a3c666897588a05shiqian  const bool nan_eq_nan_;
1874616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // max_abs_error will be used for value comparison when >= 0.
1875616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  const FloatType max_abs_error_;
187632de5f53763125925e078498250f7e73a88de9edzhanyong.wan
187732de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(FloatingEqMatcher);
1878e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1879e35fdd936d133bf8a48de140a3c666897588a05shiqian
1880e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the Pointee(m) matcher for matching a pointer whose
1881e35fdd936d133bf8a48de140a3c666897588a05shiqian// pointee matches matcher m.  The pointer can be either raw or smart.
1882e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename InnerMatcher>
1883e35fdd936d133bf8a48de140a3c666897588a05shiqianclass PointeeMatcher {
1884e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1885e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit PointeeMatcher(const InnerMatcher& matcher) : matcher_(matcher) {}
1886e35fdd936d133bf8a48de140a3c666897588a05shiqian
1887e35fdd936d133bf8a48de140a3c666897588a05shiqian  // This type conversion operator template allows Pointee(m) to be
1888e35fdd936d133bf8a48de140a3c666897588a05shiqian  // used as a matcher for any pointer type whose pointee type is
1889e35fdd936d133bf8a48de140a3c666897588a05shiqian  // compatible with the inner matcher, where type Pointer can be
1890e35fdd936d133bf8a48de140a3c666897588a05shiqian  // either a raw pointer or a smart pointer.
1891e35fdd936d133bf8a48de140a3c666897588a05shiqian  //
1892e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The reason we do this instead of relying on
1893e35fdd936d133bf8a48de140a3c666897588a05shiqian  // MakePolymorphicMatcher() is that the latter is not flexible
1894e35fdd936d133bf8a48de140a3c666897588a05shiqian  // enough for implementing the DescribeTo() method of Pointee().
1895e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename Pointer>
1896e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<Pointer>() const {
1897e35fdd936d133bf8a48de140a3c666897588a05shiqian    return MakeMatcher(new Impl<Pointer>(matcher_));
1898e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
189932de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1900e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1901e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The monomorphic implementation that works for a particular pointer type.
1902e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename Pointer>
1903e35fdd936d133bf8a48de140a3c666897588a05shiqian  class Impl : public MatcherInterface<Pointer> {
1904e35fdd936d133bf8a48de140a3c666897588a05shiqian   public:
190502f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan    typedef typename PointeeOf<GTEST_REMOVE_CONST_(  // NOLINT
190602f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan        GTEST_REMOVE_REFERENCE_(Pointer))>::type Pointee;
1907e35fdd936d133bf8a48de140a3c666897588a05shiqian
1908e35fdd936d133bf8a48de140a3c666897588a05shiqian    explicit Impl(const InnerMatcher& matcher)
1909e35fdd936d133bf8a48de140a3c666897588a05shiqian        : matcher_(MatcherCast<const Pointee&>(matcher)) {}
1910e35fdd936d133bf8a48de140a3c666897588a05shiqian
1911e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeTo(::std::ostream* os) const {
1912e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "points to a value that ";
1913e35fdd936d133bf8a48de140a3c666897588a05shiqian      matcher_.DescribeTo(os);
1914e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1915e35fdd936d133bf8a48de140a3c666897588a05shiqian
1916e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeNegationTo(::std::ostream* os) const {
1917e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "does not point to a value that ";
1918e35fdd936d133bf8a48de140a3c666897588a05shiqian      matcher_.DescribeTo(os);
1919e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1920e35fdd936d133bf8a48de140a3c666897588a05shiqian
1921821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    virtual bool MatchAndExplain(Pointer pointer,
1922821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan                                 MatchResultListener* listener) const {
1923e35fdd936d133bf8a48de140a3c666897588a05shiqian      if (GetRawPointer(pointer) == NULL)
1924821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan        return false;
1925e35fdd936d133bf8a48de140a3c666897588a05shiqian
1926676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      *listener << "which points to ";
1927676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      return MatchPrintAndExplain(*pointer, matcher_, listener);
1928e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
192932de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1930e35fdd936d133bf8a48de140a3c666897588a05shiqian   private:
1931e35fdd936d133bf8a48de140a3c666897588a05shiqian    const Matcher<const Pointee&> matcher_;
193232de5f53763125925e078498250f7e73a88de9edzhanyong.wan
193332de5f53763125925e078498250f7e73a88de9edzhanyong.wan    GTEST_DISALLOW_ASSIGN_(Impl);
1934e35fdd936d133bf8a48de140a3c666897588a05shiqian  };
1935e35fdd936d133bf8a48de140a3c666897588a05shiqian
1936e35fdd936d133bf8a48de140a3c666897588a05shiqian  const InnerMatcher matcher_;
193732de5f53763125925e078498250f7e73a88de9edzhanyong.wan
193832de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(PointeeMatcher);
1939e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1940e35fdd936d133bf8a48de140a3c666897588a05shiqian
1941e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the Field() matcher for matching a field (i.e. member
1942e35fdd936d133bf8a48de140a3c666897588a05shiqian// variable) of an object.
1943e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Class, typename FieldType>
1944e35fdd936d133bf8a48de140a3c666897588a05shiqianclass FieldMatcher {
1945e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1946e35fdd936d133bf8a48de140a3c666897588a05shiqian  FieldMatcher(FieldType Class::*field,
1947e35fdd936d133bf8a48de140a3c666897588a05shiqian               const Matcher<const FieldType&>& matcher)
1948e35fdd936d133bf8a48de140a3c666897588a05shiqian      : field_(field), matcher_(matcher) {}
1949e35fdd936d133bf8a48de140a3c666897588a05shiqian
1950e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
1951676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    *os << "is an object whose given field ";
1952e35fdd936d133bf8a48de140a3c666897588a05shiqian    matcher_.DescribeTo(os);
1953e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1954e35fdd936d133bf8a48de140a3c666897588a05shiqian
1955e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
1956676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    *os << "is an object whose given field ";
1957e35fdd936d133bf8a48de140a3c666897588a05shiqian    matcher_.DescribeNegationTo(os);
1958e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1959e35fdd936d133bf8a48de140a3c666897588a05shiqian
1960db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  template <typename T>
1961db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplain(const T& value, MatchResultListener* listener) const {
1962db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return MatchAndExplainImpl(
1963db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan        typename ::testing::internal::
196402f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan            is_pointer<GTEST_REMOVE_CONST_(T)>::type(),
1965db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan        value, listener);
1966db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  }
1967db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan
1968db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan private:
1969db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  // The first argument of MatchAndExplainImpl() is needed to help
197018490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  // Symbian's C++ compiler choose which overload to use.  Its type is
197118490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  // true_type iff the Field() matcher is used to match a pointer.
1972db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplainImpl(false_type /* is_not_pointer */, const Class& obj,
1973db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                           MatchResultListener* listener) const {
1974676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    *listener << "whose given field is ";
1975676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    return MatchPrintAndExplain(obj.*field_, matcher_, listener);
1976e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1977e35fdd936d133bf8a48de140a3c666897588a05shiqian
1978db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplainImpl(true_type /* is_pointer */, const Class* p,
1979db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                           MatchResultListener* listener) const {
1980821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (p == NULL)
1981821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      return false;
1982821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
1983676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    *listener << "which points to an object ";
1984821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // Since *p has a field, it must be a class/struct/union type and
1985821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // thus cannot be a pointer.  Therefore we pass false_type() as
1986821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // the first argument.
1987db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return MatchAndExplainImpl(false_type(), *p, listener);
1988e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
198932de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1990e35fdd936d133bf8a48de140a3c666897588a05shiqian  const FieldType Class::*field_;
1991e35fdd936d133bf8a48de140a3c666897588a05shiqian  const Matcher<const FieldType&> matcher_;
199232de5f53763125925e078498250f7e73a88de9edzhanyong.wan
199332de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(FieldMatcher);
1994e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1995e35fdd936d133bf8a48de140a3c666897588a05shiqian
1996e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the Property() matcher for matching a property
1997e35fdd936d133bf8a48de140a3c666897588a05shiqian// (i.e. return value of a getter method) of an object.
1998e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Class, typename PropertyType>
1999e35fdd936d133bf8a48de140a3c666897588a05shiqianclass PropertyMatcher {
2000e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
2001e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The property may have a reference type, so 'const PropertyType&'
2002e35fdd936d133bf8a48de140a3c666897588a05shiqian  // may cause double references and fail to compile.  That's why we
200302f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan  // need GTEST_REFERENCE_TO_CONST, which works regardless of
2004e35fdd936d133bf8a48de140a3c666897588a05shiqian  // PropertyType being a reference or not.
200502f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan  typedef GTEST_REFERENCE_TO_CONST_(PropertyType) RefToConstProperty;
2006e35fdd936d133bf8a48de140a3c666897588a05shiqian
2007e35fdd936d133bf8a48de140a3c666897588a05shiqian  PropertyMatcher(PropertyType (Class::*property)() const,
2008e35fdd936d133bf8a48de140a3c666897588a05shiqian                  const Matcher<RefToConstProperty>& matcher)
2009e35fdd936d133bf8a48de140a3c666897588a05shiqian      : property_(property), matcher_(matcher) {}
2010e35fdd936d133bf8a48de140a3c666897588a05shiqian
2011e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
2012676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    *os << "is an object whose given property ";
2013e35fdd936d133bf8a48de140a3c666897588a05shiqian    matcher_.DescribeTo(os);
2014e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
2015e35fdd936d133bf8a48de140a3c666897588a05shiqian
2016e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
2017676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    *os << "is an object whose given property ";
2018e35fdd936d133bf8a48de140a3c666897588a05shiqian    matcher_.DescribeNegationTo(os);
2019e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
2020e35fdd936d133bf8a48de140a3c666897588a05shiqian
2021db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  template <typename T>
2022db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplain(const T&value, MatchResultListener* listener) const {
2023db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return MatchAndExplainImpl(
2024db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan        typename ::testing::internal::
202502f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan            is_pointer<GTEST_REMOVE_CONST_(T)>::type(),
2026db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan        value, listener);
2027db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  }
2028db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan
2029db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan private:
2030db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  // The first argument of MatchAndExplainImpl() is needed to help
203118490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  // Symbian's C++ compiler choose which overload to use.  Its type is
203218490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  // true_type iff the Property() matcher is used to match a pointer.
2033db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplainImpl(false_type /* is_not_pointer */, const Class& obj,
2034db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                           MatchResultListener* listener) const {
2035676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    *listener << "whose given property is ";
2036676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    // Cannot pass the return value (for example, int) to MatchPrintAndExplain,
2037676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    // which takes a non-const reference as argument.
2038676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    RefToConstProperty result = (obj.*property_)();
2039676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    return MatchPrintAndExplain(result, matcher_, listener);
2040e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
2041e35fdd936d133bf8a48de140a3c666897588a05shiqian
2042db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplainImpl(true_type /* is_pointer */, const Class* p,
2043db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                           MatchResultListener* listener) const {
2044821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (p == NULL)
2045821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      return false;
2046821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
2047676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    *listener << "which points to an object ";
2048821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // Since *p has a property method, it must be a class/struct/union
2049821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // type and thus cannot be a pointer.  Therefore we pass
2050821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // false_type() as the first argument.
2051db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return MatchAndExplainImpl(false_type(), *p, listener);
2052e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
205332de5f53763125925e078498250f7e73a88de9edzhanyong.wan
2054e35fdd936d133bf8a48de140a3c666897588a05shiqian  PropertyType (Class::*property_)() const;
2055e35fdd936d133bf8a48de140a3c666897588a05shiqian  const Matcher<RefToConstProperty> matcher_;
205632de5f53763125925e078498250f7e73a88de9edzhanyong.wan
205732de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(PropertyMatcher);
2058e35fdd936d133bf8a48de140a3c666897588a05shiqian};
2059e35fdd936d133bf8a48de140a3c666897588a05shiqian
2060e35fdd936d133bf8a48de140a3c666897588a05shiqian// Type traits specifying various features of different functors for ResultOf.
2061e35fdd936d133bf8a48de140a3c666897588a05shiqian// The default template specifies features for functor objects.
2062e35fdd936d133bf8a48de140a3c666897588a05shiqian// Functor classes have to typedef argument_type and result_type
2063e35fdd936d133bf8a48de140a3c666897588a05shiqian// to be compatible with ResultOf.
2064e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Functor>
2065e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct CallableTraits {
2066e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef typename Functor::result_type ResultType;
2067e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef Functor StorageType;
2068e35fdd936d133bf8a48de140a3c666897588a05shiqian
206932de5f53763125925e078498250f7e73a88de9edzhanyong.wan  static void CheckIsValid(Functor /* functor */) {}
2070e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
2071e35fdd936d133bf8a48de140a3c666897588a05shiqian  static ResultType Invoke(Functor f, T arg) { return f(arg); }
2072e35fdd936d133bf8a48de140a3c666897588a05shiqian};
2073e35fdd936d133bf8a48de140a3c666897588a05shiqian
2074e35fdd936d133bf8a48de140a3c666897588a05shiqian// Specialization for function pointers.
2075e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename ArgType, typename ResType>
2076e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct CallableTraits<ResType(*)(ArgType)> {
2077e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef ResType ResultType;
2078e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef ResType(*StorageType)(ArgType);
2079e35fdd936d133bf8a48de140a3c666897588a05shiqian
2080e35fdd936d133bf8a48de140a3c666897588a05shiqian  static void CheckIsValid(ResType(*f)(ArgType)) {
2081f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    GTEST_CHECK_(f != NULL)
2082e35fdd936d133bf8a48de140a3c666897588a05shiqian        << "NULL function pointer is passed into ResultOf().";
2083e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
2084e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
2085e35fdd936d133bf8a48de140a3c666897588a05shiqian  static ResType Invoke(ResType(*f)(ArgType), T arg) {
2086e35fdd936d133bf8a48de140a3c666897588a05shiqian    return (*f)(arg);
2087e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
2088e35fdd936d133bf8a48de140a3c666897588a05shiqian};
2089e35fdd936d133bf8a48de140a3c666897588a05shiqian
2090e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the ResultOf() matcher for matching a return value of a
2091e35fdd936d133bf8a48de140a3c666897588a05shiqian// unary function of an object.
2092e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Callable>
2093e35fdd936d133bf8a48de140a3c666897588a05shiqianclass ResultOfMatcher {
2094e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
2095e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef typename CallableTraits<Callable>::ResultType ResultType;
2096e35fdd936d133bf8a48de140a3c666897588a05shiqian
2097e35fdd936d133bf8a48de140a3c666897588a05shiqian  ResultOfMatcher(Callable callable, const Matcher<ResultType>& matcher)
2098e35fdd936d133bf8a48de140a3c666897588a05shiqian      : callable_(callable), matcher_(matcher) {
2099e35fdd936d133bf8a48de140a3c666897588a05shiqian    CallableTraits<Callable>::CheckIsValid(callable_);
2100e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
2101e35fdd936d133bf8a48de140a3c666897588a05shiqian
2102e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
2103e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<T>() const {
2104e35fdd936d133bf8a48de140a3c666897588a05shiqian    return Matcher<T>(new Impl<T>(callable_, matcher_));
2105e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
2106e35fdd936d133bf8a48de140a3c666897588a05shiqian
2107e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
2108e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef typename CallableTraits<Callable>::StorageType CallableStorageType;
2109e35fdd936d133bf8a48de140a3c666897588a05shiqian
2110e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
2111e35fdd936d133bf8a48de140a3c666897588a05shiqian  class Impl : public MatcherInterface<T> {
2112e35fdd936d133bf8a48de140a3c666897588a05shiqian   public:
2113e35fdd936d133bf8a48de140a3c666897588a05shiqian    Impl(CallableStorageType callable, const Matcher<ResultType>& matcher)
2114e35fdd936d133bf8a48de140a3c666897588a05shiqian        : callable_(callable), matcher_(matcher) {}
2115e35fdd936d133bf8a48de140a3c666897588a05shiqian
2116e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeTo(::std::ostream* os) const {
2117676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      *os << "is mapped by the given callable to a value that ";
2118e35fdd936d133bf8a48de140a3c666897588a05shiqian      matcher_.DescribeTo(os);
2119e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
2120e35fdd936d133bf8a48de140a3c666897588a05shiqian
2121e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeNegationTo(::std::ostream* os) const {
2122676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      *os << "is mapped by the given callable to a value that ";
2123e35fdd936d133bf8a48de140a3c666897588a05shiqian      matcher_.DescribeNegationTo(os);
2124e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
2125e35fdd936d133bf8a48de140a3c666897588a05shiqian
2126821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    virtual bool MatchAndExplain(T obj, MatchResultListener* listener) const {
2127676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      *listener << "which is mapped by the given callable to ";
2128676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      // Cannot pass the return value (for example, int) to
2129676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      // MatchPrintAndExplain, which takes a non-const reference as argument.
2130676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      ResultType result =
2131676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan          CallableTraits<Callable>::template Invoke<T>(callable_, obj);
2132676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      return MatchPrintAndExplain(result, matcher_, listener);
2133e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
213432de5f53763125925e078498250f7e73a88de9edzhanyong.wan
2135e35fdd936d133bf8a48de140a3c666897588a05shiqian   private:
2136e35fdd936d133bf8a48de140a3c666897588a05shiqian    // Functors often define operator() as non-const method even though
2137e35fdd936d133bf8a48de140a3c666897588a05shiqian    // they are actualy stateless. But we need to use them even when
2138e35fdd936d133bf8a48de140a3c666897588a05shiqian    // 'this' is a const pointer. It's the user's responsibility not to
2139e35fdd936d133bf8a48de140a3c666897588a05shiqian    // use stateful callables with ResultOf(), which does't guarantee
2140e35fdd936d133bf8a48de140a3c666897588a05shiqian    // how many times the callable will be invoked.
2141e35fdd936d133bf8a48de140a3c666897588a05shiqian    mutable CallableStorageType callable_;
2142e35fdd936d133bf8a48de140a3c666897588a05shiqian    const Matcher<ResultType> matcher_;
214332de5f53763125925e078498250f7e73a88de9edzhanyong.wan
214432de5f53763125925e078498250f7e73a88de9edzhanyong.wan    GTEST_DISALLOW_ASSIGN_(Impl);
2145e35fdd936d133bf8a48de140a3c666897588a05shiqian  };  // class Impl
2146e35fdd936d133bf8a48de140a3c666897588a05shiqian
2147e35fdd936d133bf8a48de140a3c666897588a05shiqian  const CallableStorageType callable_;
2148e35fdd936d133bf8a48de140a3c666897588a05shiqian  const Matcher<ResultType> matcher_;
214932de5f53763125925e078498250f7e73a88de9edzhanyong.wan
215032de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(ResultOfMatcher);
2151e35fdd936d133bf8a48de140a3c666897588a05shiqian};
2152e35fdd936d133bf8a48de140a3c666897588a05shiqian
2153a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan// Implements a matcher that checks the size of an STL-style container.
2154a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wantemplate <typename SizeMatcher>
2155a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wanclass SizeIsMatcher {
2156a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan public:
2157a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan  explicit SizeIsMatcher(const SizeMatcher& size_matcher)
2158a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan       : size_matcher_(size_matcher) {
2159a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan  }
2160a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan
2161a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan  template <typename Container>
2162a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan  operator Matcher<Container>() const {
2163a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan    return MakeMatcher(new Impl<Container>(size_matcher_));
2164a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan  }
2165a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan
2166a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan  template <typename Container>
2167a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan  class Impl : public MatcherInterface<Container> {
2168a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan   public:
2169a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan    typedef internal::StlContainerView<
2170a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan         GTEST_REMOVE_REFERENCE_AND_CONST_(Container)> ContainerView;
2171a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan    typedef typename ContainerView::type::size_type SizeType;
2172a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan    explicit Impl(const SizeMatcher& size_matcher)
2173a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan        : size_matcher_(MatcherCast<SizeType>(size_matcher)) {}
2174a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan
2175a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan    virtual void DescribeTo(::std::ostream* os) const {
2176a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan      *os << "size ";
2177a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan      size_matcher_.DescribeTo(os);
2178a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan    }
2179a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan    virtual void DescribeNegationTo(::std::ostream* os) const {
2180a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan      *os << "size ";
2181a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan      size_matcher_.DescribeNegationTo(os);
2182a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan    }
2183a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan
2184a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan    virtual bool MatchAndExplain(Container container,
2185a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan                                 MatchResultListener* listener) const {
2186a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan      SizeType size = container.size();
2187a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan      StringMatchResultListener size_listener;
2188a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan      const bool result = size_matcher_.MatchAndExplain(size, &size_listener);
2189a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan      *listener
2190a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan          << "whose size " << size << (result ? " matches" : " doesn't match");
2191a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan      PrintIfNotEmpty(size_listener.str(), listener->stream());
2192a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan      return result;
2193a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan    }
2194a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan
2195a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan   private:
2196a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan    const Matcher<SizeType> size_matcher_;
2197a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan    GTEST_DISALLOW_ASSIGN_(Impl);
2198a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan  };
2199a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan
2200a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan private:
2201a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan  const SizeMatcher size_matcher_;
2202a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan  GTEST_DISALLOW_ASSIGN_(SizeIsMatcher);
2203a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan};
2204a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan
22056a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// Implements an equality matcher for any STL-style container whose elements
22066a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// support ==. This matcher is like Eq(), but its failure explanations provide
22076a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// more detailed information that is useful when the container is used as a set.
22086a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// The failure message reports elements that are in one of the operands but not
22096a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// the other. The failure messages do not report duplicate or out-of-order
22106a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// elements in the containers (which don't properly matter to sets, but can
22116a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// occur if the containers are vectors or lists, for example).
22126a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan//
22136a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// Uses the container's const_iterator, value_type, operator ==,
22146a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// begin(), and end().
22156a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wantemplate <typename Container>
22166a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wanclass ContainerEqMatcher {
22176a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan public:
2218b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef internal::StlContainerView<Container> View;
2219b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef typename View::type StlContainer;
2220b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef typename View::const_reference StlContainerReference;
2221b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
2222b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  // We make a copy of rhs in case the elements in it are modified
2223b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  // after this matcher is created.
2224b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  explicit ContainerEqMatcher(const Container& rhs) : rhs_(View::Copy(rhs)) {
2225b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    // Makes sure the user doesn't instantiate this class template
2226b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    // with a const or reference type.
2227ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    (void)testing::StaticAssertTypeEq<Container,
2228ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        GTEST_REMOVE_REFERENCE_AND_CONST_(Container)>();
2229b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  }
2230b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
22316a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan  void DescribeTo(::std::ostream* os) const {
22326a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan    *os << "equals ";
2233e2e8ba401d198d1a8304c652e997505609b62696vladlosev    UniversalPrint(rhs_, os);
22346a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan  }
22356a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan  void DescribeNegationTo(::std::ostream* os) const {
22366a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan    *os << "does not equal ";
2237e2e8ba401d198d1a8304c652e997505609b62696vladlosev    UniversalPrint(rhs_, os);
22386a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan  }
22396a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan
2240b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  template <typename LhsContainer>
2241e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan  bool MatchAndExplain(const LhsContainer& lhs,
2242e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan                       MatchResultListener* listener) const {
224302f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan    // GTEST_REMOVE_CONST_() is needed to work around an MSVC 8.0 bug
2244b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    // that causes LhsContainer to be a const type sometimes.
224502f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan    typedef internal::StlContainerView<GTEST_REMOVE_CONST_(LhsContainer)>
2246b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan        LhsView;
2247b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    typedef typename LhsView::type LhsStlContainer;
2248b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    StlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2249e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan    if (lhs_stl_container == rhs_)
2250e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan      return true;
2251b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
2252e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan    ::std::ostream* const os = listener->stream();
2253e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan    if (os != NULL) {
2254b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan      // Something is different. Check for extra values first.
2255e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan      bool printed_header = false;
2256e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan      for (typename LhsStlContainer::const_iterator it =
2257e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan               lhs_stl_container.begin();
2258e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan           it != lhs_stl_container.end(); ++it) {
2259e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan        if (internal::ArrayAwareFind(rhs_.begin(), rhs_.end(), *it) ==
2260e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan            rhs_.end()) {
2261e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan          if (printed_header) {
2262e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan            *os << ", ";
2263e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan          } else {
2264b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan            *os << "which has these unexpected elements: ";
2265e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan            printed_header = true;
2266e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan          }
2267e2e8ba401d198d1a8304c652e997505609b62696vladlosev          UniversalPrint(*it, os);
22686a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan        }
22696a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan      }
22706a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan
2271b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan      // Now check for missing values.
2272e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan      bool printed_header2 = false;
2273e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan      for (typename StlContainer::const_iterator it = rhs_.begin();
2274e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan           it != rhs_.end(); ++it) {
2275e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan        if (internal::ArrayAwareFind(
2276e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan                lhs_stl_container.begin(), lhs_stl_container.end(), *it) ==
2277e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan            lhs_stl_container.end()) {
2278e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan          if (printed_header2) {
2279e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan            *os << ", ";
2280e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan          } else {
2281b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan            *os << (printed_header ? ",\nand" : "which")
2282b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan                << " doesn't have these expected elements: ";
2283e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan            printed_header2 = true;
2284e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan          }
2285e2e8ba401d198d1a8304c652e997505609b62696vladlosev          UniversalPrint(*it, os);
22866a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan        }
22876a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan      }
22886a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan    }
2289e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan
2290e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan    return false;
22916a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan  }
229232de5f53763125925e078498250f7e73a88de9edzhanyong.wan
22936a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan private:
2294b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  const StlContainer rhs_;
229532de5f53763125925e078498250f7e73a88de9edzhanyong.wan
229632de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(ContainerEqMatcher);
22976a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan};
22986a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan
2299898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan// A comparator functor that uses the < operator to compare two values.
2300898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wanstruct LessComparator {
2301898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan  template <typename T, typename U>
2302898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan  bool operator()(const T& lhs, const U& rhs) const { return lhs < rhs; }
2303898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan};
2304898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
2305898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan// Implements WhenSortedBy(comparator, container_matcher).
2306898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wantemplate <typename Comparator, typename ContainerMatcher>
2307898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wanclass WhenSortedByMatcher {
2308898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan public:
2309898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan  WhenSortedByMatcher(const Comparator& comparator,
2310898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan                      const ContainerMatcher& matcher)
2311898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      : comparator_(comparator), matcher_(matcher) {}
2312898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
2313898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan  template <typename LhsContainer>
2314898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan  operator Matcher<LhsContainer>() const {
2315898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan    return MakeMatcher(new Impl<LhsContainer>(comparator_, matcher_));
2316898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan  }
2317898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
2318898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan  template <typename LhsContainer>
2319898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan  class Impl : public MatcherInterface<LhsContainer> {
2320898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan   public:
2321898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan    typedef internal::StlContainerView<
2322898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan         GTEST_REMOVE_REFERENCE_AND_CONST_(LhsContainer)> LhsView;
2323898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan    typedef typename LhsView::type LhsStlContainer;
2324898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan    typedef typename LhsView::const_reference LhsStlContainerReference;
2325a9a59e06dd7cdfe52c988bf065bc156a7ed96a5czhanyong.wan    // Transforms std::pair<const Key, Value> into std::pair<Key, Value>
2326a9a59e06dd7cdfe52c988bf065bc156a7ed96a5czhanyong.wan    // so that we can match associative containers.
2327a9a59e06dd7cdfe52c988bf065bc156a7ed96a5czhanyong.wan    typedef typename RemoveConstFromKey<
2328a9a59e06dd7cdfe52c988bf065bc156a7ed96a5czhanyong.wan        typename LhsStlContainer::value_type>::type LhsValue;
2329898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
2330898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan    Impl(const Comparator& comparator, const ContainerMatcher& matcher)
2331898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan        : comparator_(comparator), matcher_(matcher) {}
2332898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
2333898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan    virtual void DescribeTo(::std::ostream* os) const {
2334898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      *os << "(when sorted) ";
2335898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      matcher_.DescribeTo(os);
2336898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan    }
2337898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
2338898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan    virtual void DescribeNegationTo(::std::ostream* os) const {
2339898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      *os << "(when sorted) ";
2340898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      matcher_.DescribeNegationTo(os);
2341898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan    }
2342898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
2343898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan    virtual bool MatchAndExplain(LhsContainer lhs,
2344898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan                                 MatchResultListener* listener) const {
2345898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2346898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      std::vector<LhsValue> sorted_container(lhs_stl_container.begin(),
2347898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan                                             lhs_stl_container.end());
2348898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      std::sort(sorted_container.begin(), sorted_container.end(), comparator_);
2349898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
2350898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      if (!listener->IsInterested()) {
2351898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan        // If the listener is not interested, we do not need to
2352898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan        // construct the inner explanation.
2353898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan        return matcher_.Matches(sorted_container);
2354898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      }
2355898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
2356898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      *listener << "which is ";
2357898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      UniversalPrint(sorted_container, listener->stream());
2358898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      *listener << " when sorted";
2359898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
2360898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      StringMatchResultListener inner_listener;
2361898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      const bool match = matcher_.MatchAndExplain(sorted_container,
2362898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan                                                  &inner_listener);
2363898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      PrintIfNotEmpty(inner_listener.str(), listener->stream());
2364898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      return match;
2365898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan    }
2366898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
2367898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan   private:
2368898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan    const Comparator comparator_;
2369898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan    const Matcher<const std::vector<LhsValue>&> matcher_;
2370898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
2371898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan    GTEST_DISALLOW_COPY_AND_ASSIGN_(Impl);
2372898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan  };
2373898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
2374898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan private:
2375898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan  const Comparator comparator_;
2376898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan  const ContainerMatcher matcher_;
2377898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
2378898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan  GTEST_DISALLOW_ASSIGN_(WhenSortedByMatcher);
2379898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan};
2380898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
2381ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// Implements Pointwise(tuple_matcher, rhs_container).  tuple_matcher
2382ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// must be able to be safely cast to Matcher<tuple<const T1&, const
2383ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// T2&> >, where T1 and T2 are the types of elements in the LHS
2384ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// container and the RHS container respectively.
2385ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wantemplate <typename TupleMatcher, typename RhsContainer>
2386ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wanclass PointwiseMatcher {
2387ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan public:
2388ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  typedef internal::StlContainerView<RhsContainer> RhsView;
2389ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  typedef typename RhsView::type RhsStlContainer;
2390ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  typedef typename RhsStlContainer::value_type RhsValue;
2391ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2392ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  // Like ContainerEq, we make a copy of rhs in case the elements in
2393ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  // it are modified after this matcher is created.
2394ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  PointwiseMatcher(const TupleMatcher& tuple_matcher, const RhsContainer& rhs)
2395ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      : tuple_matcher_(tuple_matcher), rhs_(RhsView::Copy(rhs)) {
2396ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    // Makes sure the user doesn't instantiate this class template
2397ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    // with a const or reference type.
2398ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    (void)testing::StaticAssertTypeEq<RhsContainer,
2399ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        GTEST_REMOVE_REFERENCE_AND_CONST_(RhsContainer)>();
2400ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  }
2401ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2402ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  template <typename LhsContainer>
2403ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  operator Matcher<LhsContainer>() const {
2404ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    return MakeMatcher(new Impl<LhsContainer>(tuple_matcher_, rhs_));
2405ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  }
2406ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2407ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  template <typename LhsContainer>
2408ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  class Impl : public MatcherInterface<LhsContainer> {
2409ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan   public:
2410ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    typedef internal::StlContainerView<
2411ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan         GTEST_REMOVE_REFERENCE_AND_CONST_(LhsContainer)> LhsView;
2412ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    typedef typename LhsView::type LhsStlContainer;
2413ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    typedef typename LhsView::const_reference LhsStlContainerReference;
2414ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    typedef typename LhsStlContainer::value_type LhsValue;
2415ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    // We pass the LHS value and the RHS value to the inner matcher by
2416ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    // reference, as they may be expensive to copy.  We must use tuple
2417ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    // instead of pair here, as a pair cannot hold references (C++ 98,
2418ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    // 20.2.2 [lib.pairs]).
2419ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    typedef std::tr1::tuple<const LhsValue&, const RhsValue&> InnerMatcherArg;
2420ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2421ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    Impl(const TupleMatcher& tuple_matcher, const RhsStlContainer& rhs)
2422ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        // mono_tuple_matcher_ holds a monomorphic version of the tuple matcher.
2423ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        : mono_tuple_matcher_(SafeMatcherCast<InnerMatcherArg>(tuple_matcher)),
2424ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan          rhs_(rhs) {}
2425ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2426ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    virtual void DescribeTo(::std::ostream* os) const {
2427ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      *os << "contains " << rhs_.size()
2428ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan          << " values, where each value and its corresponding value in ";
2429ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      UniversalPrinter<RhsStlContainer>::Print(rhs_, os);
2430ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      *os << " ";
2431ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      mono_tuple_matcher_.DescribeTo(os);
2432ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    }
2433ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    virtual void DescribeNegationTo(::std::ostream* os) const {
2434ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      *os << "doesn't contain exactly " << rhs_.size()
2435ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan          << " values, or contains a value x at some index i"
2436ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan          << " where x and the i-th value of ";
2437ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      UniversalPrint(rhs_, os);
2438ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      *os << " ";
2439ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      mono_tuple_matcher_.DescribeNegationTo(os);
2440ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    }
2441ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2442ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    virtual bool MatchAndExplain(LhsContainer lhs,
2443ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan                                 MatchResultListener* listener) const {
2444ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2445ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      const size_t actual_size = lhs_stl_container.size();
2446ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      if (actual_size != rhs_.size()) {
2447ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        *listener << "which contains " << actual_size << " values";
2448ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        return false;
2449ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      }
2450ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2451ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      typename LhsStlContainer::const_iterator left = lhs_stl_container.begin();
2452ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      typename RhsStlContainer::const_iterator right = rhs_.begin();
2453ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      for (size_t i = 0; i != actual_size; ++i, ++left, ++right) {
2454ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        const InnerMatcherArg value_pair(*left, *right);
2455ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2456ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        if (listener->IsInterested()) {
2457ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan          StringMatchResultListener inner_listener;
2458ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan          if (!mono_tuple_matcher_.MatchAndExplain(
2459ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan                  value_pair, &inner_listener)) {
2460ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan            *listener << "where the value pair (";
2461ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan            UniversalPrint(*left, listener->stream());
2462ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan            *listener << ", ";
2463ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan            UniversalPrint(*right, listener->stream());
2464ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan            *listener << ") at index #" << i << " don't match";
2465ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan            PrintIfNotEmpty(inner_listener.str(), listener->stream());
2466ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan            return false;
2467ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan          }
2468ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        } else {
2469ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan          if (!mono_tuple_matcher_.Matches(value_pair))
2470ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan            return false;
2471ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        }
2472ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      }
2473ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2474ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      return true;
2475ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    }
2476ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2477ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan   private:
2478ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    const Matcher<InnerMatcherArg> mono_tuple_matcher_;
2479ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    const RhsStlContainer rhs_;
2480ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2481ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    GTEST_DISALLOW_ASSIGN_(Impl);
2482ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  };
2483ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2484ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan private:
2485ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  const TupleMatcher tuple_matcher_;
2486ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  const RhsStlContainer rhs_;
2487ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2488ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  GTEST_DISALLOW_ASSIGN_(PointwiseMatcher);
2489ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan};
2490ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
249133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Holds the logic common to ContainsMatcherImpl and EachMatcherImpl.
2492b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename Container>
249333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wanclass QuantifierMatcherImpl : public MatcherInterface<Container> {
2494b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan public:
2495ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
2496b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef StlContainerView<RawContainer> View;
2497b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef typename View::type StlContainer;
2498b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef typename View::const_reference StlContainerReference;
2499b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef typename StlContainer::value_type Element;
2500b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
2501b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  template <typename InnerMatcher>
250233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  explicit QuantifierMatcherImpl(InnerMatcher inner_matcher)
2503b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan      : inner_matcher_(
250433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan           testing::SafeMatcherCast<const Element&>(inner_matcher)) {}
250533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
250633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  // Checks whether:
250733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  // * All elements in the container match, if all_elements_should_match.
250833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  // * Any element in the container matches, if !all_elements_should_match.
250933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  bool MatchAndExplainImpl(bool all_elements_should_match,
251033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan                           Container container,
251133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan                           MatchResultListener* listener) const {
251233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    StlContainerReference stl_container = View::ConstReference(container);
251333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    size_t i = 0;
251433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    for (typename StlContainer::const_iterator it = stl_container.begin();
251533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan         it != stl_container.end(); ++it, ++i) {
251633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan      StringMatchResultListener inner_listener;
251733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan      const bool matches = inner_matcher_.MatchAndExplain(*it, &inner_listener);
251833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
251933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan      if (matches != all_elements_should_match) {
252033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan        *listener << "whose element #" << i
252133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan                  << (matches ? " matches" : " doesn't match");
252233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan        PrintIfNotEmpty(inner_listener.str(), listener->stream());
252333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan        return !all_elements_should_match;
252433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan      }
252533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    }
252633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    return all_elements_should_match;
252733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  }
252833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
252933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan protected:
253033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  const Matcher<const Element&> inner_matcher_;
253133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
253233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  GTEST_DISALLOW_ASSIGN_(QuantifierMatcherImpl);
253333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan};
253433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
253533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Implements Contains(element_matcher) for the given argument type Container.
253633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Symmetric to EachMatcherImpl.
253733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wantemplate <typename Container>
253833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wanclass ContainsMatcherImpl : public QuantifierMatcherImpl<Container> {
253933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan public:
254033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  template <typename InnerMatcher>
254133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  explicit ContainsMatcherImpl(InnerMatcher inner_matcher)
254233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan      : QuantifierMatcherImpl<Container>(inner_matcher) {}
2543b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
2544b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  // Describes what this matcher does.
2545b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
2546b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    *os << "contains at least one element that ";
254733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    this->inner_matcher_.DescribeTo(os);
2548b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  }
2549b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
2550b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
2551b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    *os << "doesn't contain any element that ";
255233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    this->inner_matcher_.DescribeTo(os);
2553b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  }
2554b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
2555821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  virtual bool MatchAndExplain(Container container,
2556821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan                               MatchResultListener* listener) const {
255733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    return this->MatchAndExplainImpl(false, container, listener);
2558b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  }
2559b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
2560b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan private:
256132de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(ContainsMatcherImpl);
2562b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan};
2563b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
256433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Implements Each(element_matcher) for the given argument type Container.
256533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Symmetric to ContainsMatcherImpl.
256633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wantemplate <typename Container>
256733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wanclass EachMatcherImpl : public QuantifierMatcherImpl<Container> {
256833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan public:
256933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  template <typename InnerMatcher>
257033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  explicit EachMatcherImpl(InnerMatcher inner_matcher)
257133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan      : QuantifierMatcherImpl<Container>(inner_matcher) {}
257233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
257333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  // Describes what this matcher does.
257433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
257533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    *os << "only contains elements that ";
257633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    this->inner_matcher_.DescribeTo(os);
257733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  }
257833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
257933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
258033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    *os << "contains some element that ";
258133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    this->inner_matcher_.DescribeNegationTo(os);
258233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  }
258333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
258433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  virtual bool MatchAndExplain(Container container,
258533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan                               MatchResultListener* listener) const {
258633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    return this->MatchAndExplainImpl(true, container, listener);
258733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  }
258833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
258933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan private:
259033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  GTEST_DISALLOW_ASSIGN_(EachMatcherImpl);
259133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan};
259233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
2593b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Implements polymorphic Contains(element_matcher).
2594b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename M>
2595b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanclass ContainsMatcher {
2596b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan public:
2597b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  explicit ContainsMatcher(M m) : inner_matcher_(m) {}
2598b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
2599b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  template <typename Container>
2600b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  operator Matcher<Container>() const {
2601b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    return MakeMatcher(new ContainsMatcherImpl<Container>(inner_matcher_));
2602b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  }
2603b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
2604b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan private:
2605b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  const M inner_matcher_;
260632de5f53763125925e078498250f7e73a88de9edzhanyong.wan
260732de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(ContainsMatcher);
2608b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan};
2609b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
261033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Implements polymorphic Each(element_matcher).
261133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wantemplate <typename M>
261233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wanclass EachMatcher {
261333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan public:
261433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  explicit EachMatcher(M m) : inner_matcher_(m) {}
261533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
261633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  template <typename Container>
261733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  operator Matcher<Container>() const {
261833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    return MakeMatcher(new EachMatcherImpl<Container>(inner_matcher_));
261933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  }
262033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
262133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan private:
262233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  const M inner_matcher_;
262333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
262433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  GTEST_DISALLOW_ASSIGN_(EachMatcher);
262533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan};
262633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
2627b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// Implements Key(inner_matcher) for the given argument pair type.
2628b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// Key(inner_matcher) matches an std::pair whose 'first' field matches
2629b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// inner_matcher.  For example, Contains(Key(Ge(5))) can be used to match an
2630b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// std::map that contains at least one element whose key is >= 5.
2631b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wantemplate <typename PairType>
2632b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wanclass KeyMatcherImpl : public MatcherInterface<PairType> {
2633b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan public:
2634ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  typedef GTEST_REMOVE_REFERENCE_AND_CONST_(PairType) RawPairType;
2635b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  typedef typename RawPairType::first_type KeyType;
2636b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
2637b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  template <typename InnerMatcher>
2638b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  explicit KeyMatcherImpl(InnerMatcher inner_matcher)
2639b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan      : inner_matcher_(
2640b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan          testing::SafeMatcherCast<const KeyType&>(inner_matcher)) {
2641b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  }
2642b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
2643b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  // Returns true iff 'key_value.first' (the key) matches the inner matcher.
2644821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  virtual bool MatchAndExplain(PairType key_value,
2645821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan                               MatchResultListener* listener) const {
2646b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    StringMatchResultListener inner_listener;
2647b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    const bool match = inner_matcher_.MatchAndExplain(key_value.first,
2648b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan                                                      &inner_listener);
2649b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    const internal::string explanation = inner_listener.str();
2650b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    if (explanation != "") {
2651b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan      *listener << "whose first field is a value " << explanation;
2652b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    }
2653b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    return match;
2654b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  }
2655b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
2656b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  // Describes what this matcher does.
2657b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
2658b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan    *os << "has a key that ";
2659b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan    inner_matcher_.DescribeTo(os);
2660b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  }
2661b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
2662b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  // Describes what the negation of this matcher does.
2663b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
2664b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan    *os << "doesn't have a key that ";
2665b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan    inner_matcher_.DescribeTo(os);
2666b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  }
2667b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
2668b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan private:
2669b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  const Matcher<const KeyType&> inner_matcher_;
267032de5f53763125925e078498250f7e73a88de9edzhanyong.wan
267132de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(KeyMatcherImpl);
2672b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan};
2673b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
2674b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// Implements polymorphic Key(matcher_for_key).
2675b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wantemplate <typename M>
2676b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wanclass KeyMatcher {
2677b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan public:
2678b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  explicit KeyMatcher(M m) : matcher_for_key_(m) {}
2679b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
2680b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  template <typename PairType>
2681b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  operator Matcher<PairType>() const {
2682b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan    return MakeMatcher(new KeyMatcherImpl<PairType>(matcher_for_key_));
2683b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  }
2684b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
2685b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan private:
2686b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  const M matcher_for_key_;
268732de5f53763125925e078498250f7e73a88de9edzhanyong.wan
268832de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(KeyMatcher);
2689b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan};
2690b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
2691f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// Implements Pair(first_matcher, second_matcher) for the given argument pair
2692f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// type with its two matchers. See Pair() function below.
2693f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wantemplate <typename PairType>
2694f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wanclass PairMatcherImpl : public MatcherInterface<PairType> {
2695f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan public:
2696ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  typedef GTEST_REMOVE_REFERENCE_AND_CONST_(PairType) RawPairType;
2697f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  typedef typename RawPairType::first_type FirstType;
2698f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  typedef typename RawPairType::second_type SecondType;
2699f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
2700f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  template <typename FirstMatcher, typename SecondMatcher>
2701f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  PairMatcherImpl(FirstMatcher first_matcher, SecondMatcher second_matcher)
2702f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan      : first_matcher_(
2703f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan            testing::SafeMatcherCast<const FirstType&>(first_matcher)),
2704f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan        second_matcher_(
2705f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan            testing::SafeMatcherCast<const SecondType&>(second_matcher)) {
2706f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  }
2707f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
2708f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  // Describes what this matcher does.
2709f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
2710f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    *os << "has a first field that ";
2711f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    first_matcher_.DescribeTo(os);
2712f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    *os << ", and has a second field that ";
2713f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    second_matcher_.DescribeTo(os);
2714f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  }
2715f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
2716f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  // Describes what the negation of this matcher does.
2717f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
2718f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    *os << "has a first field that ";
2719f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    first_matcher_.DescribeNegationTo(os);
2720f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    *os << ", or has a second field that ";
2721f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    second_matcher_.DescribeNegationTo(os);
2722f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  }
2723f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
2724821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // Returns true iff 'a_pair.first' matches first_matcher and 'a_pair.second'
2725821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // matches second_matcher.
2726821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  virtual bool MatchAndExplain(PairType a_pair,
2727821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan                               MatchResultListener* listener) const {
2728676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    if (!listener->IsInterested()) {
2729676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      // If the listener is not interested, we don't need to construct the
2730676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      // explanation.
2731676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      return first_matcher_.Matches(a_pair.first) &&
2732676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan             second_matcher_.Matches(a_pair.second);
2733821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    }
2734676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    StringMatchResultListener first_inner_listener;
2735676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    if (!first_matcher_.MatchAndExplain(a_pair.first,
2736676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan                                        &first_inner_listener)) {
2737676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      *listener << "whose first field does not match";
2738b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan      PrintIfNotEmpty(first_inner_listener.str(), listener->stream());
2739821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      return false;
2740f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    }
2741676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    StringMatchResultListener second_inner_listener;
2742676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    if (!second_matcher_.MatchAndExplain(a_pair.second,
2743676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan                                         &second_inner_listener)) {
2744676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      *listener << "whose second field does not match";
2745b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan      PrintIfNotEmpty(second_inner_listener.str(), listener->stream());
2746821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      return false;
2747f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    }
2748676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    ExplainSuccess(first_inner_listener.str(), second_inner_listener.str(),
2749676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan                   listener);
2750821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    return true;
2751f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  }
2752f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
2753f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan private:
2754676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan  void ExplainSuccess(const internal::string& first_explanation,
2755676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan                      const internal::string& second_explanation,
2756676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan                      MatchResultListener* listener) const {
2757676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    *listener << "whose both fields match";
2758676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    if (first_explanation != "") {
2759676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      *listener << ", where the first field is a value " << first_explanation;
2760676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    }
2761676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    if (second_explanation != "") {
2762676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      *listener << ", ";
2763676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      if (first_explanation != "") {
2764676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan        *listener << "and ";
2765676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      } else {
2766676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan        *listener << "where ";
2767676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      }
2768676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      *listener << "the second field is a value " << second_explanation;
2769676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    }
2770676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan  }
2771676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan
2772f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  const Matcher<const FirstType&> first_matcher_;
2773f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  const Matcher<const SecondType&> second_matcher_;
277432de5f53763125925e078498250f7e73a88de9edzhanyong.wan
277532de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(PairMatcherImpl);
2776f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan};
2777f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
2778f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// Implements polymorphic Pair(first_matcher, second_matcher).
2779f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wantemplate <typename FirstMatcher, typename SecondMatcher>
2780f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wanclass PairMatcher {
2781f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan public:
2782f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  PairMatcher(FirstMatcher first_matcher, SecondMatcher second_matcher)
2783f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan      : first_matcher_(first_matcher), second_matcher_(second_matcher) {}
2784f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
2785f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  template <typename PairType>
2786f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  operator Matcher<PairType> () const {
2787f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    return MakeMatcher(
2788f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan        new PairMatcherImpl<PairType>(
2789f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan            first_matcher_, second_matcher_));
2790f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  }
2791f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
2792f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan private:
2793f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  const FirstMatcher first_matcher_;
2794f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  const SecondMatcher second_matcher_;
279532de5f53763125925e078498250f7e73a88de9edzhanyong.wan
279632de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(PairMatcher);
2797f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan};
2798f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
27991afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// Implements ElementsAre() and ElementsAreArray().
28001afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wantemplate <typename Container>
28011afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wanclass ElementsAreMatcherImpl : public MatcherInterface<Container> {
28021afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan public:
2803ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
28041afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  typedef internal::StlContainerView<RawContainer> View;
28051afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  typedef typename View::type StlContainer;
28061afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  typedef typename View::const_reference StlContainerReference;
28071afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  typedef typename StlContainer::value_type Element;
28081afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
28091afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  // Constructs the matcher from a sequence of element values or
28101afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  // element matchers.
28111afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  template <typename InputIter>
281238513a8bb154f0b6d0a4088814fe92552696d465jgm  ElementsAreMatcherImpl(InputIter first, InputIter last) {
281338513a8bb154f0b6d0a4088814fe92552696d465jgm    while (first != last) {
281438513a8bb154f0b6d0a4088814fe92552696d465jgm      matchers_.push_back(MatcherCast<const Element&>(*first++));
28151afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    }
28161afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  }
28171afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
28181afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  // Describes what this matcher does.
28191afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
28201afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    if (count() == 0) {
28211afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      *os << "is empty";
28221afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    } else if (count() == 1) {
28231afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      *os << "has 1 element that ";
28241afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      matchers_[0].DescribeTo(os);
28251afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    } else {
28261afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      *os << "has " << Elements(count()) << " where\n";
28271afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      for (size_t i = 0; i != count(); ++i) {
2828b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan        *os << "element #" << i << " ";
28291afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        matchers_[i].DescribeTo(os);
28301afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        if (i + 1 < count()) {
28311afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan          *os << ",\n";
28321afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        }
28331afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      }
28341afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    }
28351afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  }
28361afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
28371afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  // Describes what the negation of this matcher does.
28381afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
28391afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    if (count() == 0) {
2840b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan      *os << "isn't empty";
28411afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      return;
28421afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    }
28431afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
2844b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    *os << "doesn't have " << Elements(count()) << ", or\n";
28451afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    for (size_t i = 0; i != count(); ++i) {
2846b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan      *os << "element #" << i << " ";
28471afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      matchers_[i].DescribeNegationTo(os);
28481afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      if (i + 1 < count()) {
28491afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        *os << ", or\n";
28501afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      }
28511afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    }
28521afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  }
28531afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
2854821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  virtual bool MatchAndExplain(Container container,
2855821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan                               MatchResultListener* listener) const {
28561afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    StlContainerReference stl_container = View::ConstReference(container);
2857821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    const size_t actual_count = stl_container.size();
2858821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (actual_count != count()) {
2859821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      // The element count doesn't match.  If the container is empty,
2860821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      // there's no need to explain anything as Google Mock already
2861821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      // prints the empty container.  Otherwise we just need to show
2862821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      // how many elements there actually are.
2863821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      if (actual_count != 0) {
2864b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan        *listener << "which has " << Elements(actual_count);
28651afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      }
2866821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      return false;
2867821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    }
28681afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
2869821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    typename StlContainer::const_iterator it = stl_container.begin();
2870821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // explanations[i] is the explanation of the element at index i.
2871821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    std::vector<internal::string> explanations(count());
2872821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    for (size_t i = 0; i != count();  ++it, ++i) {
2873821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      StringMatchResultListener s;
2874821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      if (matchers_[i].MatchAndExplain(*it, &s)) {
2875821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan        explanations[i] = s.str();
2876821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      } else {
2877821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan        // The container has the right size but the i-th element
2878821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan        // doesn't match its expectation.
2879b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan        *listener << "whose element #" << i << " doesn't match";
2880b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan        PrintIfNotEmpty(s.str(), listener->stream());
2881821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan        return false;
2882821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      }
2883821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    }
28841afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
2885821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // Every element matches its expectation.  We need to explain why
2886821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // (the obvious ones can be skipped).
2887821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    bool reason_printed = false;
2888821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    for (size_t i = 0; i != count(); ++i) {
2889821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      const internal::string& s = explanations[i];
2890821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      if (!s.empty()) {
2891821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan        if (reason_printed) {
2892b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan          *listener << ",\nand ";
28931afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        }
2894b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan        *listener << "whose element #" << i << " matches, " << s;
2895821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan        reason_printed = true;
28961afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      }
28971afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    }
2898821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
2899821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    return true;
29001afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  }
29011afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
29021afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan private:
29031afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  static Message Elements(size_t count) {
29041afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    return Message() << count << (count == 1 ? " element" : " elements");
29051afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  }
29061afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
29071afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  size_t count() const { return matchers_.size(); }
29081afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  std::vector<Matcher<const Element&> > matchers_;
290932de5f53763125925e078498250f7e73a88de9edzhanyong.wan
291032de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(ElementsAreMatcherImpl);
29111afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan};
29121afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
29131afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// Implements ElementsAre() of 0 arguments.
29141afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wanclass ElementsAreMatcher0 {
29151afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan public:
29161afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  ElementsAreMatcher0() {}
29171afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
29181afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  template <typename Container>
29191afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  operator Matcher<Container>() const {
2920ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
29211afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    typedef typename internal::StlContainerView<RawContainer>::type::value_type
29221afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        Element;
29231afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
29241afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    const Matcher<const Element&>* const matchers = NULL;
292538513a8bb154f0b6d0a4088814fe92552696d465jgm    return MakeMatcher(new ElementsAreMatcherImpl<Container>(matchers,
292638513a8bb154f0b6d0a4088814fe92552696d465jgm                                                             matchers));
29271afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  }
29281afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan};
29291afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
29301afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// Implements ElementsAreArray().
29311afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wantemplate <typename T>
29321afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wanclass ElementsAreArrayMatcher {
29331afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan public:
293438513a8bb154f0b6d0a4088814fe92552696d465jgm  template <typename Iter>
293538513a8bb154f0b6d0a4088814fe92552696d465jgm  ElementsAreArrayMatcher(Iter first, Iter last) : matchers_(first, last) {}
29361afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
29371afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  template <typename Container>
29381afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  operator Matcher<Container>() const {
293938513a8bb154f0b6d0a4088814fe92552696d465jgm    return MakeMatcher(new ElementsAreMatcherImpl<Container>(
294038513a8bb154f0b6d0a4088814fe92552696d465jgm        matchers_.begin(), matchers_.end()));
29411afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  }
29421afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
29431afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan private:
294438513a8bb154f0b6d0a4088814fe92552696d465jgm  const std::vector<T> matchers_;
294532de5f53763125925e078498250f7e73a88de9edzhanyong.wan
294632de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(ElementsAreArrayMatcher);
29471afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan};
29481afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
2949b4140808f98ff09c43ca1ddaa8ff13bc47cd0089zhanyong.wan// Returns the description for a matcher defined using the MATCHER*()
2950b4140808f98ff09c43ca1ddaa8ff13bc47cd0089zhanyong.wan// macro where the user-supplied description string is "", if
2951b4140808f98ff09c43ca1ddaa8ff13bc47cd0089zhanyong.wan// 'negation' is false; otherwise returns the description of the
2952b4140808f98ff09c43ca1ddaa8ff13bc47cd0089zhanyong.wan// negation of the matcher.  'param_values' contains a list of strings
2953b4140808f98ff09c43ca1ddaa8ff13bc47cd0089zhanyong.wan// that are the print-out of the matcher's parameters.
2954587c1b37c2f0b6d430fb13ce09326db0135b557cvladlosevGTEST_API_ string FormatMatcherDescription(bool negation,
2955587c1b37c2f0b6d430fb13ce09326db0135b557cvladlosev                                           const char* matcher_name,
2956587c1b37c2f0b6d430fb13ce09326db0135b557cvladlosev                                           const Strings& param_values);
29571afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
2958e35fdd936d133bf8a48de140a3c666897588a05shiqian}  // namespace internal
2959e35fdd936d133bf8a48de140a3c666897588a05shiqian
2960e35fdd936d133bf8a48de140a3c666897588a05shiqian// _ is a matcher that matches anything of any type.
2961e35fdd936d133bf8a48de140a3c666897588a05shiqian//
2962e35fdd936d133bf8a48de140a3c666897588a05shiqian// This definition is fine as:
2963e35fdd936d133bf8a48de140a3c666897588a05shiqian//
2964e35fdd936d133bf8a48de140a3c666897588a05shiqian//   1. The C++ standard permits using the name _ in a namespace that
2965e35fdd936d133bf8a48de140a3c666897588a05shiqian//      is not the global namespace or ::std.
2966e35fdd936d133bf8a48de140a3c666897588a05shiqian//   2. The AnythingMatcher class has no data member or constructor,
2967e35fdd936d133bf8a48de140a3c666897588a05shiqian//      so it's OK to create global variables of this type.
2968e35fdd936d133bf8a48de140a3c666897588a05shiqian//   3. c-style has approved of using _ in this case.
2969e35fdd936d133bf8a48de140a3c666897588a05shiqianconst internal::AnythingMatcher _ = {};
2970e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any value of the given type T.
2971e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
2972e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<T> A() { return MakeMatcher(new internal::AnyMatcherImpl<T>()); }
2973e35fdd936d133bf8a48de140a3c666897588a05shiqian
2974e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any value of the given type T.
2975e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
2976e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<T> An() { return A<T>(); }
2977e35fdd936d133bf8a48de140a3c666897588a05shiqian
2978e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything equal to x.
2979e35fdd936d133bf8a48de140a3c666897588a05shiqian// Note: if the parameter of Eq() were declared as const T&, Eq("foo")
2980e35fdd936d133bf8a48de140a3c666897588a05shiqian// wouldn't compile.
2981e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
2982e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::EqMatcher<T> Eq(T x) { return internal::EqMatcher<T>(x); }
2983e35fdd936d133bf8a48de140a3c666897588a05shiqian
2984e35fdd936d133bf8a48de140a3c666897588a05shiqian// Constructs a Matcher<T> from a 'value' of type T.  The constructed
2985e35fdd936d133bf8a48de140a3c666897588a05shiqian// matcher matches any value that's equal to 'value'.
2986e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
2987e35fdd936d133bf8a48de140a3c666897588a05shiqianMatcher<T>::Matcher(T value) { *this = Eq(value); }
2988e35fdd936d133bf8a48de140a3c666897588a05shiqian
2989e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a monomorphic matcher that matches anything with type Lhs
2990e35fdd936d133bf8a48de140a3c666897588a05shiqian// and equal to rhs.  A user may need to use this instead of Eq(...)
2991e35fdd936d133bf8a48de140a3c666897588a05shiqian// in order to resolve an overloading ambiguity.
2992e35fdd936d133bf8a48de140a3c666897588a05shiqian//
2993e35fdd936d133bf8a48de140a3c666897588a05shiqian// TypedEq<T>(x) is just a convenient short-hand for Matcher<T>(Eq(x))
2994e35fdd936d133bf8a48de140a3c666897588a05shiqian// or Matcher<T>(x), but more readable than the latter.
2995e35fdd936d133bf8a48de140a3c666897588a05shiqian//
2996e35fdd936d133bf8a48de140a3c666897588a05shiqian// We could define similar monomorphic matchers for other comparison
2997e35fdd936d133bf8a48de140a3c666897588a05shiqian// operations (e.g. TypedLt, TypedGe, and etc), but decided not to do
2998e35fdd936d133bf8a48de140a3c666897588a05shiqian// it yet as those are used much less than Eq() in practice.  A user
2999e35fdd936d133bf8a48de140a3c666897588a05shiqian// can always write Matcher<T>(Lt(5)) to be explicit about the type,
3000e35fdd936d133bf8a48de140a3c666897588a05shiqian// for example.
3001e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Lhs, typename Rhs>
3002e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<Lhs> TypedEq(const Rhs& rhs) { return Eq(rhs); }
3003e35fdd936d133bf8a48de140a3c666897588a05shiqian
3004e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything >= x.
3005e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs>
3006e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::GeMatcher<Rhs> Ge(Rhs x) {
3007e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::GeMatcher<Rhs>(x);
3008e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3009e35fdd936d133bf8a48de140a3c666897588a05shiqian
3010e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything > x.
3011e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs>
3012e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::GtMatcher<Rhs> Gt(Rhs x) {
3013e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::GtMatcher<Rhs>(x);
3014e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3015e35fdd936d133bf8a48de140a3c666897588a05shiqian
3016e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything <= x.
3017e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs>
3018e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::LeMatcher<Rhs> Le(Rhs x) {
3019e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::LeMatcher<Rhs>(x);
3020e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3021e35fdd936d133bf8a48de140a3c666897588a05shiqian
3022e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything < x.
3023e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs>
3024e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::LtMatcher<Rhs> Lt(Rhs x) {
3025e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::LtMatcher<Rhs>(x);
3026e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3027e35fdd936d133bf8a48de140a3c666897588a05shiqian
3028e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything != x.
3029e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs>
3030e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::NeMatcher<Rhs> Ne(Rhs x) {
3031e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::NeMatcher<Rhs>(x);
3032e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3033e35fdd936d133bf8a48de140a3c666897588a05shiqian
30342d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan// Creates a polymorphic matcher that matches any NULL pointer.
30352d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.waninline PolymorphicMatcher<internal::IsNullMatcher > IsNull() {
30362d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan  return MakePolymorphicMatcher(internal::IsNullMatcher());
30372d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan}
30382d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan
3039e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches any non-NULL pointer.
3040e35fdd936d133bf8a48de140a3c666897588a05shiqian// This is convenient as Not(NULL) doesn't compile (the compiler
3041e35fdd936d133bf8a48de140a3c666897588a05shiqian// thinks that that expression is comparing a pointer with an integer).
3042e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::NotNullMatcher > NotNull() {
3043e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::NotNullMatcher());
3044e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3045e35fdd936d133bf8a48de140a3c666897588a05shiqian
3046e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches any argument that
3047e35fdd936d133bf8a48de140a3c666897588a05shiqian// references variable x.
3048e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
3049e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::RefMatcher<T&> Ref(T& x) {  // NOLINT
3050e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::RefMatcher<T&>(x);
3051e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3052e35fdd936d133bf8a48de140a3c666897588a05shiqian
3053e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any double argument approximately
3054e35fdd936d133bf8a48de140a3c666897588a05shiqian// equal to rhs, where two NANs are considered unequal.
3055e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::FloatingEqMatcher<double> DoubleEq(double rhs) {
3056e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::FloatingEqMatcher<double>(rhs, false);
3057e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3058e35fdd936d133bf8a48de140a3c666897588a05shiqian
3059e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any double argument approximately
3060e35fdd936d133bf8a48de140a3c666897588a05shiqian// equal to rhs, including NaN values when rhs is NaN.
3061e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::FloatingEqMatcher<double> NanSensitiveDoubleEq(double rhs) {
3062e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::FloatingEqMatcher<double>(rhs, true);
3063e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3064e35fdd936d133bf8a48de140a3c666897588a05shiqian
3065616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// Creates a matcher that matches any double argument approximately equal to
3066616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// rhs, up to the specified max absolute error bound, where two NANs are
3067616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// considered unequal.  The max absolute error bound must be non-negative.
3068616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.waninline internal::FloatingEqMatcher<double> DoubleNear(
3069616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    double rhs, double max_abs_error) {
3070616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  return internal::FloatingEqMatcher<double>(rhs, false, max_abs_error);
3071616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan}
3072616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
3073616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// Creates a matcher that matches any double argument approximately equal to
3074616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// rhs, up to the specified max absolute error bound, including NaN values when
3075616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// rhs is NaN.  The max absolute error bound must be non-negative.
3076616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.waninline internal::FloatingEqMatcher<double> NanSensitiveDoubleNear(
3077616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    double rhs, double max_abs_error) {
3078616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  return internal::FloatingEqMatcher<double>(rhs, true, max_abs_error);
3079616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan}
3080616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
3081e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any float argument approximately
3082e35fdd936d133bf8a48de140a3c666897588a05shiqian// equal to rhs, where two NANs are considered unequal.
3083e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::FloatingEqMatcher<float> FloatEq(float rhs) {
3084e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::FloatingEqMatcher<float>(rhs, false);
3085e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3086e35fdd936d133bf8a48de140a3c666897588a05shiqian
3087616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// Creates a matcher that matches any float argument approximately
3088e35fdd936d133bf8a48de140a3c666897588a05shiqian// equal to rhs, including NaN values when rhs is NaN.
3089e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::FloatingEqMatcher<float> NanSensitiveFloatEq(float rhs) {
3090e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::FloatingEqMatcher<float>(rhs, true);
3091e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3092e35fdd936d133bf8a48de140a3c666897588a05shiqian
3093616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// Creates a matcher that matches any float argument approximately equal to
3094616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// rhs, up to the specified max absolute error bound, where two NANs are
3095616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// considered unequal.  The max absolute error bound must be non-negative.
3096616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.waninline internal::FloatingEqMatcher<float> FloatNear(
3097616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    float rhs, float max_abs_error) {
3098616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  return internal::FloatingEqMatcher<float>(rhs, false, max_abs_error);
3099616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan}
3100616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
3101616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// Creates a matcher that matches any float argument approximately equal to
3102616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// rhs, up to the specified max absolute error bound, including NaN values when
3103616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// rhs is NaN.  The max absolute error bound must be non-negative.
3104616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.waninline internal::FloatingEqMatcher<float> NanSensitiveFloatNear(
3105616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    float rhs, float max_abs_error) {
3106616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  return internal::FloatingEqMatcher<float>(rhs, true, max_abs_error);
3107616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan}
3108616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
3109e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches a pointer (raw or smart) that points
3110e35fdd936d133bf8a48de140a3c666897588a05shiqian// to a value that matches inner_matcher.
3111e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename InnerMatcher>
3112e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::PointeeMatcher<InnerMatcher> Pointee(
3113e35fdd936d133bf8a48de140a3c666897588a05shiqian    const InnerMatcher& inner_matcher) {
3114e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::PointeeMatcher<InnerMatcher>(inner_matcher);
3115e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3116e35fdd936d133bf8a48de140a3c666897588a05shiqian
3117e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches an object whose given field matches
3118e35fdd936d133bf8a48de140a3c666897588a05shiqian// 'matcher'.  For example,
3119e35fdd936d133bf8a48de140a3c666897588a05shiqian//   Field(&Foo::number, Ge(5))
3120e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a Foo object x iff x.number >= 5.
3121e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Class, typename FieldType, typename FieldMatcher>
3122e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<
3123e35fdd936d133bf8a48de140a3c666897588a05shiqian  internal::FieldMatcher<Class, FieldType> > Field(
3124e35fdd936d133bf8a48de140a3c666897588a05shiqian    FieldType Class::*field, const FieldMatcher& matcher) {
3125e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(
3126e35fdd936d133bf8a48de140a3c666897588a05shiqian      internal::FieldMatcher<Class, FieldType>(
3127e35fdd936d133bf8a48de140a3c666897588a05shiqian          field, MatcherCast<const FieldType&>(matcher)));
3128e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The call to MatcherCast() is required for supporting inner
3129e35fdd936d133bf8a48de140a3c666897588a05shiqian  // matchers of compatible types.  For example, it allows
3130e35fdd936d133bf8a48de140a3c666897588a05shiqian  //   Field(&Foo::bar, m)
3131e35fdd936d133bf8a48de140a3c666897588a05shiqian  // to compile where bar is an int32 and m is a matcher for int64.
3132e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3133e35fdd936d133bf8a48de140a3c666897588a05shiqian
3134e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches an object whose given property
3135e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches 'matcher'.  For example,
3136e35fdd936d133bf8a48de140a3c666897588a05shiqian//   Property(&Foo::str, StartsWith("hi"))
3137e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a Foo object x iff x.str() starts with "hi".
3138e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Class, typename PropertyType, typename PropertyMatcher>
3139e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<
3140e35fdd936d133bf8a48de140a3c666897588a05shiqian  internal::PropertyMatcher<Class, PropertyType> > Property(
3141e35fdd936d133bf8a48de140a3c666897588a05shiqian    PropertyType (Class::*property)() const, const PropertyMatcher& matcher) {
3142e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(
3143e35fdd936d133bf8a48de140a3c666897588a05shiqian      internal::PropertyMatcher<Class, PropertyType>(
3144e35fdd936d133bf8a48de140a3c666897588a05shiqian          property,
314502f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan          MatcherCast<GTEST_REFERENCE_TO_CONST_(PropertyType)>(matcher)));
3146e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The call to MatcherCast() is required for supporting inner
3147e35fdd936d133bf8a48de140a3c666897588a05shiqian  // matchers of compatible types.  For example, it allows
3148e35fdd936d133bf8a48de140a3c666897588a05shiqian  //   Property(&Foo::bar, m)
3149e35fdd936d133bf8a48de140a3c666897588a05shiqian  // to compile where bar() returns an int32 and m is a matcher for int64.
3150e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3151e35fdd936d133bf8a48de140a3c666897588a05shiqian
3152e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches an object iff the result of applying
3153e35fdd936d133bf8a48de140a3c666897588a05shiqian// a callable to x matches 'matcher'.
3154e35fdd936d133bf8a48de140a3c666897588a05shiqian// For example,
3155e35fdd936d133bf8a48de140a3c666897588a05shiqian//   ResultOf(f, StartsWith("hi"))
3156e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a Foo object x iff f(x) starts with "hi".
3157e35fdd936d133bf8a48de140a3c666897588a05shiqian// callable parameter can be a function, function pointer, or a functor.
3158e35fdd936d133bf8a48de140a3c666897588a05shiqian// Callable has to satisfy the following conditions:
3159e35fdd936d133bf8a48de140a3c666897588a05shiqian//   * It is required to keep no state affecting the results of
3160e35fdd936d133bf8a48de140a3c666897588a05shiqian//     the calls on it and make no assumptions about how many calls
3161e35fdd936d133bf8a48de140a3c666897588a05shiqian//     will be made. Any state it keeps must be protected from the
3162e35fdd936d133bf8a48de140a3c666897588a05shiqian//     concurrent access.
3163e35fdd936d133bf8a48de140a3c666897588a05shiqian//   * If it is a function object, it has to define type result_type.
3164e35fdd936d133bf8a48de140a3c666897588a05shiqian//     We recommend deriving your functor classes from std::unary_function.
3165e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Callable, typename ResultOfMatcher>
3166e35fdd936d133bf8a48de140a3c666897588a05shiqianinternal::ResultOfMatcher<Callable> ResultOf(
3167e35fdd936d133bf8a48de140a3c666897588a05shiqian    Callable callable, const ResultOfMatcher& matcher) {
3168e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::ResultOfMatcher<Callable>(
3169e35fdd936d133bf8a48de140a3c666897588a05shiqian          callable,
3170e35fdd936d133bf8a48de140a3c666897588a05shiqian          MatcherCast<typename internal::CallableTraits<Callable>::ResultType>(
3171e35fdd936d133bf8a48de140a3c666897588a05shiqian              matcher));
3172e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The call to MatcherCast() is required for supporting inner
3173e35fdd936d133bf8a48de140a3c666897588a05shiqian  // matchers of compatible types.  For example, it allows
3174e35fdd936d133bf8a48de140a3c666897588a05shiqian  //   ResultOf(Function, m)
3175e35fdd936d133bf8a48de140a3c666897588a05shiqian  // to compile where Function() returns an int32 and m is a matcher for int64.
3176e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3177e35fdd936d133bf8a48de140a3c666897588a05shiqian
3178e35fdd936d133bf8a48de140a3c666897588a05shiqian// String matchers.
3179e35fdd936d133bf8a48de140a3c666897588a05shiqian
3180e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string equal to str.
3181e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> >
3182e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrEq(const internal::string& str) {
3183e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>(
3184e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, true, true));
3185e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3186e35fdd936d133bf8a48de140a3c666897588a05shiqian
3187e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string not equal to str.
3188e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> >
3189e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrNe(const internal::string& str) {
3190e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>(
3191e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, false, true));
3192e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3193e35fdd936d133bf8a48de140a3c666897588a05shiqian
3194e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string equal to str, ignoring case.
3195e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> >
3196e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrCaseEq(const internal::string& str) {
3197e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>(
3198e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, true, false));
3199e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3200e35fdd936d133bf8a48de140a3c666897588a05shiqian
3201e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string not equal to str, ignoring case.
3202e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> >
3203e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrCaseNe(const internal::string& str) {
3204e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>(
3205e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, false, false));
3206e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3207e35fdd936d133bf8a48de140a3c666897588a05shiqian
3208e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any string, std::string, or C string
3209e35fdd936d133bf8a48de140a3c666897588a05shiqian// that contains the given substring.
3210e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::HasSubstrMatcher<internal::string> >
3211e35fdd936d133bf8a48de140a3c666897588a05shiqian    HasSubstr(const internal::string& substring) {
3212e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::HasSubstrMatcher<internal::string>(
3213e35fdd936d133bf8a48de140a3c666897588a05shiqian      substring));
3214e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3215e35fdd936d133bf8a48de140a3c666897588a05shiqian
3216e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that starts with 'prefix' (case-sensitive).
3217e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StartsWithMatcher<internal::string> >
3218e35fdd936d133bf8a48de140a3c666897588a05shiqian    StartsWith(const internal::string& prefix) {
3219e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StartsWithMatcher<internal::string>(
3220e35fdd936d133bf8a48de140a3c666897588a05shiqian      prefix));
3221e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3222e35fdd936d133bf8a48de140a3c666897588a05shiqian
3223e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that ends with 'suffix' (case-sensitive).
3224e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::EndsWithMatcher<internal::string> >
3225e35fdd936d133bf8a48de140a3c666897588a05shiqian    EndsWith(const internal::string& suffix) {
3226e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::EndsWithMatcher<internal::string>(
3227e35fdd936d133bf8a48de140a3c666897588a05shiqian      suffix));
3228e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3229e35fdd936d133bf8a48de140a3c666897588a05shiqian
3230e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that fully matches regular expression 'regex'.
3231e35fdd936d133bf8a48de140a3c666897588a05shiqian// The matcher takes ownership of 'regex'.
3232e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex(
3233e35fdd936d133bf8a48de140a3c666897588a05shiqian    const internal::RE* regex) {
3234e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, true));
3235e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3236e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex(
3237e35fdd936d133bf8a48de140a3c666897588a05shiqian    const internal::string& regex) {
3238e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MatchesRegex(new internal::RE(regex));
3239e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3240e35fdd936d133bf8a48de140a3c666897588a05shiqian
3241e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that contains regular expression 'regex'.
3242e35fdd936d133bf8a48de140a3c666897588a05shiqian// The matcher takes ownership of 'regex'.
3243e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex(
3244e35fdd936d133bf8a48de140a3c666897588a05shiqian    const internal::RE* regex) {
3245e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, false));
3246e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3247e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex(
3248e35fdd936d133bf8a48de140a3c666897588a05shiqian    const internal::string& regex) {
3249e35fdd936d133bf8a48de140a3c666897588a05shiqian  return ContainsRegex(new internal::RE(regex));
3250e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3251e35fdd936d133bf8a48de140a3c666897588a05shiqian
3252e35fdd936d133bf8a48de140a3c666897588a05shiqian#if GTEST_HAS_GLOBAL_WSTRING || GTEST_HAS_STD_WSTRING
3253e35fdd936d133bf8a48de140a3c666897588a05shiqian// Wide string matchers.
3254e35fdd936d133bf8a48de140a3c666897588a05shiqian
3255e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string equal to str.
3256e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> >
3257e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrEq(const internal::wstring& str) {
3258e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>(
3259e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, true, true));
3260e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3261e35fdd936d133bf8a48de140a3c666897588a05shiqian
3262e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string not equal to str.
3263e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> >
3264e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrNe(const internal::wstring& str) {
3265e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>(
3266e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, false, true));
3267e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3268e35fdd936d133bf8a48de140a3c666897588a05shiqian
3269e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string equal to str, ignoring case.
3270e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> >
3271e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrCaseEq(const internal::wstring& str) {
3272e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>(
3273e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, true, false));
3274e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3275e35fdd936d133bf8a48de140a3c666897588a05shiqian
3276e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string not equal to str, ignoring case.
3277e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> >
3278e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrCaseNe(const internal::wstring& str) {
3279e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>(
3280e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, false, false));
3281e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3282e35fdd936d133bf8a48de140a3c666897588a05shiqian
3283e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any wstring, std::wstring, or C wide string
3284e35fdd936d133bf8a48de140a3c666897588a05shiqian// that contains the given substring.
3285e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::HasSubstrMatcher<internal::wstring> >
3286e35fdd936d133bf8a48de140a3c666897588a05shiqian    HasSubstr(const internal::wstring& substring) {
3287e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::HasSubstrMatcher<internal::wstring>(
3288e35fdd936d133bf8a48de140a3c666897588a05shiqian      substring));
3289e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3290e35fdd936d133bf8a48de140a3c666897588a05shiqian
3291e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that starts with 'prefix' (case-sensitive).
3292e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StartsWithMatcher<internal::wstring> >
3293e35fdd936d133bf8a48de140a3c666897588a05shiqian    StartsWith(const internal::wstring& prefix) {
3294e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StartsWithMatcher<internal::wstring>(
3295e35fdd936d133bf8a48de140a3c666897588a05shiqian      prefix));
3296e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3297e35fdd936d133bf8a48de140a3c666897588a05shiqian
3298e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that ends with 'suffix' (case-sensitive).
3299e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::EndsWithMatcher<internal::wstring> >
3300e35fdd936d133bf8a48de140a3c666897588a05shiqian    EndsWith(const internal::wstring& suffix) {
3301e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::EndsWithMatcher<internal::wstring>(
3302e35fdd936d133bf8a48de140a3c666897588a05shiqian      suffix));
3303e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3304e35fdd936d133bf8a48de140a3c666897588a05shiqian
3305e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif  // GTEST_HAS_GLOBAL_WSTRING || GTEST_HAS_STD_WSTRING
3306e35fdd936d133bf8a48de140a3c666897588a05shiqian
3307e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the
3308e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field == the second field.
3309e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Eq2Matcher Eq() { return internal::Eq2Matcher(); }
3310e35fdd936d133bf8a48de140a3c666897588a05shiqian
3311e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the
3312e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field >= the second field.
3313e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Ge2Matcher Ge() { return internal::Ge2Matcher(); }
3314e35fdd936d133bf8a48de140a3c666897588a05shiqian
3315e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the
3316e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field > the second field.
3317e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Gt2Matcher Gt() { return internal::Gt2Matcher(); }
3318e35fdd936d133bf8a48de140a3c666897588a05shiqian
3319e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the
3320e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field <= the second field.
3321e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Le2Matcher Le() { return internal::Le2Matcher(); }
3322e35fdd936d133bf8a48de140a3c666897588a05shiqian
3323e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the
3324e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field < the second field.
3325e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Lt2Matcher Lt() { return internal::Lt2Matcher(); }
3326e35fdd936d133bf8a48de140a3c666897588a05shiqian
3327e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the
3328e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field != the second field.
3329e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Ne2Matcher Ne() { return internal::Ne2Matcher(); }
3330e35fdd936d133bf8a48de140a3c666897588a05shiqian
3331e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any value of type T that m doesn't
3332e35fdd936d133bf8a48de140a3c666897588a05shiqian// match.
3333e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename InnerMatcher>
3334e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::NotMatcher<InnerMatcher> Not(InnerMatcher m) {
3335e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::NotMatcher<InnerMatcher>(m);
3336e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3337e35fdd936d133bf8a48de140a3c666897588a05shiqian
3338e35fdd936d133bf8a48de140a3c666897588a05shiqian// Returns a matcher that matches anything that satisfies the given
3339e35fdd936d133bf8a48de140a3c666897588a05shiqian// predicate.  The predicate can be any unary function or functor
3340e35fdd936d133bf8a48de140a3c666897588a05shiqian// whose return type can be implicitly converted to bool.
3341e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Predicate>
3342e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::TrulyMatcher<Predicate> >
3343e35fdd936d133bf8a48de140a3c666897588a05shiqianTruly(Predicate pred) {
3344e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::TrulyMatcher<Predicate>(pred));
3345e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3346e35fdd936d133bf8a48de140a3c666897588a05shiqian
3347a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan// Returns a matcher that matches the container size. The container must
3348a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan// support both size() and size_type which all STL-like containers provide.
3349a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan// Note that the parameter 'size' can be a value of type size_type as well as
3350a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan// matcher. For instance:
3351a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan//   EXPECT_THAT(container, SizeIs(2));     // Checks container has 2 elements.
3352a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan//   EXPECT_THAT(container, SizeIs(Le(2));  // Checks container has at most 2.
3353a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wantemplate <typename SizeMatcher>
3354a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.waninline internal::SizeIsMatcher<SizeMatcher>
3355a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wanSizeIs(const SizeMatcher& size_matcher) {
3356a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan  return internal::SizeIsMatcher<SizeMatcher>(size_matcher);
3357a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan}
3358a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan
33596a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// Returns a matcher that matches an equal container.
33606a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// This matcher behaves like Eq(), but in the event of mismatch lists the
33616a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// values that are included in one container but not the other. (Duplicate
33626a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// values and order differences are not explained.)
33636a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wantemplate <typename Container>
3364821133180ccdb9eb062548d964929ba3b354eb84zhanyong.waninline PolymorphicMatcher<internal::ContainerEqMatcher<  // NOLINT
336502f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan                            GTEST_REMOVE_CONST_(Container)> >
33666a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan    ContainerEq(const Container& rhs) {
3367b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  // This following line is for working around a bug in MSVC 8.0,
3368b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  // which causes Container to be a const type sometimes.
336902f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan  typedef GTEST_REMOVE_CONST_(Container) RawContainer;
3370821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  return MakePolymorphicMatcher(
3371821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      internal::ContainerEqMatcher<RawContainer>(rhs));
3372b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}
3373b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
3374898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan// Returns a matcher that matches a container that, when sorted using
3375898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan// the given comparator, matches container_matcher.
3376898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wantemplate <typename Comparator, typename ContainerMatcher>
3377898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.waninline internal::WhenSortedByMatcher<Comparator, ContainerMatcher>
3378898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wanWhenSortedBy(const Comparator& comparator,
3379898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan             const ContainerMatcher& container_matcher) {
3380898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan  return internal::WhenSortedByMatcher<Comparator, ContainerMatcher>(
3381898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      comparator, container_matcher);
3382898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan}
3383898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
3384898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan// Returns a matcher that matches a container that, when sorted using
3385898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan// the < operator, matches container_matcher.
3386898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wantemplate <typename ContainerMatcher>
3387898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.waninline internal::WhenSortedByMatcher<internal::LessComparator, ContainerMatcher>
3388898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wanWhenSorted(const ContainerMatcher& container_matcher) {
3389898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan  return
3390898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      internal::WhenSortedByMatcher<internal::LessComparator, ContainerMatcher>(
3391898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan          internal::LessComparator(), container_matcher);
3392898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan}
3393898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
3394ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// Matches an STL-style container or a native array that contains the
3395ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// same number of elements as in rhs, where its i-th element and rhs's
3396ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// i-th element (as a pair) satisfy the given pair matcher, for all i.
3397ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// TupleMatcher must be able to be safely cast to Matcher<tuple<const
3398ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// T1&, const T2&> >, where T1 and T2 are the types of elements in the
3399ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// LHS container and the RHS container respectively.
3400ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wantemplate <typename TupleMatcher, typename Container>
3401ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.waninline internal::PointwiseMatcher<TupleMatcher,
3402ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan                                  GTEST_REMOVE_CONST_(Container)>
3403ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wanPointwise(const TupleMatcher& tuple_matcher, const Container& rhs) {
3404ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  // This following line is for working around a bug in MSVC 8.0,
3405ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  // which causes Container to be a const type sometimes.
3406ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  typedef GTEST_REMOVE_CONST_(Container) RawContainer;
3407ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  return internal::PointwiseMatcher<TupleMatcher, RawContainer>(
3408ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      tuple_matcher, rhs);
3409ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan}
3410ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
3411b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Matches an STL-style container or a native array that contains at
3412b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// least one element matching the given value or matcher.
3413b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//
3414b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Examples:
3415b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   ::std::set<int> page_ids;
3416b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   page_ids.insert(3);
3417b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   page_ids.insert(1);
3418b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   EXPECT_THAT(page_ids, Contains(1));
3419b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   EXPECT_THAT(page_ids, Contains(Gt(2)));
3420b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   EXPECT_THAT(page_ids, Not(Contains(4)));
3421b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//
3422b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   ::std::map<int, size_t> page_lengths;
3423b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   page_lengths[1] = 100;
34244019819dbb5369b1d638503523a04aaf7eb2a5adzhanyong.wan//   EXPECT_THAT(page_lengths,
34254019819dbb5369b1d638503523a04aaf7eb2a5adzhanyong.wan//               Contains(::std::pair<const int, size_t>(1, 100)));
3426b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//
3427b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   const char* user_ids[] = { "joe", "mike", "tom" };
3428b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   EXPECT_THAT(user_ids, Contains(Eq(::std::string("tom"))));
3429b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename M>
3430b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.waninline internal::ContainsMatcher<M> Contains(M matcher) {
3431b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  return internal::ContainsMatcher<M>(matcher);
34326a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan}
34336a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan
343433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Matches an STL-style container or a native array that contains only
343533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// elements matching the given value or matcher.
343633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//
343733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Each(m) is semantically equivalent to Not(Contains(Not(m))). Only
343833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// the messages are different.
343933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//
344033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Examples:
344133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   ::std::set<int> page_ids;
344233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   // Each(m) matches an empty container, regardless of what m is.
344333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   EXPECT_THAT(page_ids, Each(Eq(1)));
344433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   EXPECT_THAT(page_ids, Each(Eq(77)));
344533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//
344633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   page_ids.insert(3);
344733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   EXPECT_THAT(page_ids, Each(Gt(0)));
344833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   EXPECT_THAT(page_ids, Not(Each(Gt(4))));
344933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   page_ids.insert(1);
345033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   EXPECT_THAT(page_ids, Not(Each(Lt(2))));
345133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//
345233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   ::std::map<int, size_t> page_lengths;
345333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   page_lengths[1] = 100;
345433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   page_lengths[2] = 200;
345533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   page_lengths[3] = 300;
345633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   EXPECT_THAT(page_lengths, Not(Each(Pair(1, 100))));
345733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   EXPECT_THAT(page_lengths, Each(Key(Le(3))));
345833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//
345933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   const char* user_ids[] = { "joe", "mike", "tom" };
346033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   EXPECT_THAT(user_ids, Not(Each(Eq(::std::string("tom")))));
346133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wantemplate <typename M>
346233605ba45417979cff7254c1196dfee65a7275b3zhanyong.waninline internal::EachMatcher<M> Each(M matcher) {
346333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  return internal::EachMatcher<M>(matcher);
346433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan}
346533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
3466b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// Key(inner_matcher) matches an std::pair whose 'first' field matches
3467b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// inner_matcher.  For example, Contains(Key(Ge(5))) can be used to match an
3468b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// std::map that contains at least one element whose key is >= 5.
3469b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wantemplate <typename M>
3470b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.waninline internal::KeyMatcher<M> Key(M inner_matcher) {
3471b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  return internal::KeyMatcher<M>(inner_matcher);
3472b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan}
3473b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
3474f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// Pair(first_matcher, second_matcher) matches a std::pair whose 'first' field
3475f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// matches first_matcher and whose 'second' field matches second_matcher.  For
3476f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// example, EXPECT_THAT(map_type, ElementsAre(Pair(Ge(5), "foo"))) can be used
3477f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// to match a std::map<int, string> that contains exactly one element whose key
3478f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// is >= 5 and whose value equals "foo".
3479f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wantemplate <typename FirstMatcher, typename SecondMatcher>
3480f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.waninline internal::PairMatcher<FirstMatcher, SecondMatcher>
3481f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wanPair(FirstMatcher first_matcher, SecondMatcher second_matcher) {
3482f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  return internal::PairMatcher<FirstMatcher, SecondMatcher>(
3483f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan      first_matcher, second_matcher);
3484f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan}
3485f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
3486e35fdd936d133bf8a48de140a3c666897588a05shiqian// Returns a predicate that is satisfied by anything that matches the
3487e35fdd936d133bf8a48de140a3c666897588a05shiqian// given matcher.
3488e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename M>
3489e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::MatcherAsPredicate<M> Matches(M matcher) {
3490e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::MatcherAsPredicate<M>(matcher);
3491e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3492e35fdd936d133bf8a48de140a3c666897588a05shiqian
3493b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Returns true iff the value matches the matcher.
3494b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename T, typename M>
3495b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.waninline bool Value(const T& value, M matcher) {
3496b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  return testing::Matches(matcher)(value);
3497b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}
3498b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
349934b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wan// Matches the value against the given matcher and explains the match
350034b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wan// result to listener.
350134b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wantemplate <typename T, typename M>
3502a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.waninline bool ExplainMatchResult(
350334b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wan    M matcher, const T& value, MatchResultListener* listener) {
350434b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wan  return SafeMatcherCast<const T&>(matcher).MatchAndExplain(value, listener);
350534b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wan}
350634b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wan
3507616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan#if GTEST_LANG_CXX11
3508616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// Define variadic matcher versions. They are overloaded in
3509616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// gmock-generated-matchers.h for the cases supported by pre C++11 compilers.
3510616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wantemplate <typename... Args>
3511616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.waninline internal::AllOfMatcher<Args...> AllOf(const Args&... matchers) {
3512616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  return internal::AllOfMatcher<Args...>(matchers...);
3513616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan}
3514616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
3515616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wantemplate <typename... Args>
3516616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.waninline internal::AnyOfMatcher<Args...> AnyOf(const Args&... matchers) {
3517616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  return internal::AnyOfMatcher<Args...>(matchers...);
3518616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan}
3519616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
3520616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan#endif  // GTEST_LANG_CXX11
3521616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
3522bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan// AllArgs(m) is a synonym of m.  This is useful in
3523bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan//
3524bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan//   EXPECT_CALL(foo, Bar(_, _)).With(AllArgs(Eq()));
3525bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan//
3526bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan// which is easier to read than
3527bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan//
3528bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan//   EXPECT_CALL(foo, Bar(_, _)).With(Eq());
3529bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wantemplate <typename InnerMatcher>
3530bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.waninline InnerMatcher AllArgs(const InnerMatcher& matcher) { return matcher; }
3531bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan
3532e35fdd936d133bf8a48de140a3c666897588a05shiqian// These macros allow using matchers to check values in Google Test
3533e35fdd936d133bf8a48de140a3c666897588a05shiqian// tests.  ASSERT_THAT(value, matcher) and EXPECT_THAT(value, matcher)
3534e35fdd936d133bf8a48de140a3c666897588a05shiqian// succeed iff the value matches the matcher.  If the assertion fails,
3535e35fdd936d133bf8a48de140a3c666897588a05shiqian// the value and the description of the matcher will be printed.
3536e35fdd936d133bf8a48de140a3c666897588a05shiqian#define ASSERT_THAT(value, matcher) ASSERT_PRED_FORMAT1(\
3537e35fdd936d133bf8a48de140a3c666897588a05shiqian    ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
3538e35fdd936d133bf8a48de140a3c666897588a05shiqian#define EXPECT_THAT(value, matcher) EXPECT_PRED_FORMAT1(\
3539e35fdd936d133bf8a48de140a3c666897588a05shiqian    ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
3540e35fdd936d133bf8a48de140a3c666897588a05shiqian
3541e35fdd936d133bf8a48de140a3c666897588a05shiqian}  // namespace testing
3542e35fdd936d133bf8a48de140a3c666897588a05shiqian
3543e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif  // GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_
3544