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>
43fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan#include <iterator>
4416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#include <limits>
45e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <ostream>  // NOLINT
46e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <sstream>
47e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <string>
48ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan#include <utility>
49e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <vector>
50e35fdd936d133bf8a48de140a3c666897588a05shiqian
5153e08c44dd34857ba57581d7c5774d6c96a8d0e1zhanyong.wan#include "gmock/internal/gmock-internal-utils.h"
5253e08c44dd34857ba57581d7c5774d6c96a8d0e1zhanyong.wan#include "gmock/internal/gmock-port.h"
5353e08c44dd34857ba57581d7c5774d6c96a8d0e1zhanyong.wan#include "gtest/gtest.h"
54e35fdd936d133bf8a48de140a3c666897588a05shiqian
5518489fa4f81a65ab5f42a6705c28ca2f80eab3bdkosak#if GTEST_HAS_STD_INITIALIZER_LIST_
5618489fa4f81a65ab5f42a6705c28ca2f80eab3bdkosak# include <initializer_list>  // NOLINT -- must be after gtest.h
571cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan#endif
581cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan
59e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace testing {
60e35fdd936d133bf8a48de140a3c666897588a05shiqian
61e35fdd936d133bf8a48de140a3c666897588a05shiqian// To implement a matcher Foo for type T, define:
62e35fdd936d133bf8a48de140a3c666897588a05shiqian//   1. a class FooMatcherImpl that implements the
63e35fdd936d133bf8a48de140a3c666897588a05shiqian//      MatcherInterface<T> interface, and
64e35fdd936d133bf8a48de140a3c666897588a05shiqian//   2. a factory function that creates a Matcher<T> object from a
65e35fdd936d133bf8a48de140a3c666897588a05shiqian//      FooMatcherImpl*.
66e35fdd936d133bf8a48de140a3c666897588a05shiqian//
67e35fdd936d133bf8a48de140a3c666897588a05shiqian// The two-level delegation design makes it possible to allow a user
68e35fdd936d133bf8a48de140a3c666897588a05shiqian// to write "v" instead of "Eq(v)" where a Matcher is expected, which
69e35fdd936d133bf8a48de140a3c666897588a05shiqian// is impossible if we pass matchers by pointers.  It also eases
70e35fdd936d133bf8a48de140a3c666897588a05shiqian// ownership management as Matcher objects can now be copied like
71e35fdd936d133bf8a48de140a3c666897588a05shiqian// plain values.
72e35fdd936d133bf8a48de140a3c666897588a05shiqian
73821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// MatchResultListener is an abstract class.  Its << operator can be
74821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// used by a matcher to explain why a value matches or doesn't match.
75821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan//
76821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// TODO(wan@google.com): add method
77821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan//   bool InterestedInWhy(bool result) const;
78821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// to indicate whether the listener is interested in why the match
79821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// result is 'result'.
80821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wanclass MatchResultListener {
81821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan public:
82821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // Creates a listener object with the given underlying ostream.  The
831cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan  // listener does not own the ostream, and does not dereference it
841cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan  // in the constructor or destructor.
85821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  explicit MatchResultListener(::std::ostream* os) : stream_(os) {}
86821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  virtual ~MatchResultListener() = 0;  // Makes this class abstract.
87821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
88821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // Streams x to the underlying ostream; does nothing if the ostream
89821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // is NULL.
90821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  template <typename T>
91821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  MatchResultListener& operator<<(const T& x) {
92821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (stream_ != NULL)
93821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      *stream_ << x;
94821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    return *this;
95821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  }
96821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
97821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // Returns the underlying ostream.
98821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  ::std::ostream* stream() { return stream_; }
99821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
100a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan  // Returns true iff the listener is interested in an explanation of
101a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan  // the match result.  A matcher's MatchAndExplain() method can use
102a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan  // this information to avoid generating the explanation when no one
103a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan  // intends to hear it.
104a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan  bool IsInterested() const { return stream_ != NULL; }
105a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan
106821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan private:
107821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  ::std::ostream* const stream_;
108821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
109821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  GTEST_DISALLOW_COPY_AND_ASSIGN_(MatchResultListener);
110821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan};
111821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
112821133180ccdb9eb062548d964929ba3b354eb84zhanyong.waninline MatchResultListener::~MatchResultListener() {
113821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan}
114821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
115fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// An instance of a subclass of this knows how to describe itself as a
116fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// matcher.
117fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanclass MatcherDescriberInterface {
118fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan public:
119fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  virtual ~MatcherDescriberInterface() {}
120fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
121fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // Describes this matcher to an ostream.  The function should print
122fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // a verb phrase that describes the property a value matching this
123fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // matcher should have.  The subject of the verb phrase is the value
124fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // being matched.  For example, the DescribeTo() method of the Gt(7)
125fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // matcher prints "is greater than 7".
126fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  virtual void DescribeTo(::std::ostream* os) const = 0;
127fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
128fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // Describes the negation of this matcher to an ostream.  For
129fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // example, if the description of this matcher is "is greater than
130fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // 7", the negated description could be "is not greater than 7".
131fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // You are not required to override this when implementing
132fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // MatcherInterface, but it is highly advised so that your matcher
133fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // can produce good error messages.
134fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
135fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    *os << "not (";
136fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    DescribeTo(os);
137fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    *os << ")";
138fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  }
139fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan};
140fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
141e35fdd936d133bf8a48de140a3c666897588a05shiqian// The implementation of a matcher.
142e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
143fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanclass MatcherInterface : public MatcherDescriberInterface {
144e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
145821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // Returns true iff the matcher matches x; also explains the match
14683f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // result to 'listener' if necessary (see the next paragraph), in
14783f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // the form of a non-restrictive relative clause ("which ...",
14883f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // "whose ...", etc) that describes x.  For example, the
14983f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // MatchAndExplain() method of the Pointee(...) matcher should
15083f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // generate an explanation like "which points to ...".
15183f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  //
15283f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // Implementations of MatchAndExplain() should add an explanation of
15383f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // the match result *if and only if* they can provide additional
15483f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // information that's not already present (or not obvious) in the
15583f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // print-out of x and the matcher's description.  Whether the match
15683f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // succeeds is not a factor in deciding whether an explanation is
15783f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // needed, as sometimes the caller needs to print a failure message
15883f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // when the match succeeds (e.g. when the matcher is used inside
15983f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // Not()).
16083f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  //
16183f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // For example, a "has at least 10 elements" matcher should explain
16283f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // what the actual element count is, regardless of the match result,
16383f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // as it is useful information to the reader; on the other hand, an
16483f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // "is empty" matcher probably only needs to explain what the actual
16583f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // size is when the match fails, as it's redundant to say that the
16683f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan  // size is 0 when the value is already known to be empty.
167821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  //
168db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  // You should override this method when defining a new matcher.
169821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  //
170821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // It's the responsibility of the caller (Google Mock) to guarantee
171821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // that 'listener' is not NULL.  This helps to simplify a matcher's
172821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // implementation when it doesn't care about the performance, as it
173821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // can talk to 'listener' without checking its validity first.
174821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // However, in order to implement dummy listeners efficiently,
175821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // listener->stream() may be NULL.
176db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  virtual bool MatchAndExplain(T x, MatchResultListener* listener) const = 0;
177e35fdd936d133bf8a48de140a3c666897588a05shiqian
178fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // Inherits these methods from MatcherDescriberInterface:
179fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  //   virtual void DescribeTo(::std::ostream* os) const = 0;
180fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  //   virtual void DescribeNegationTo(::std::ostream* os) const;
181e35fdd936d133bf8a48de140a3c666897588a05shiqian};
182e35fdd936d133bf8a48de140a3c666897588a05shiqian
1831cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan// A match result listener that stores the explanation in a string.
1841cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wanclass StringMatchResultListener : public MatchResultListener {
1851cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan public:
1861cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan  StringMatchResultListener() : MatchResultListener(&ss_) {}
1871cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan
1881cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan  // Returns the explanation accumulated so far.
1891cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan  internal::string str() const { return ss_.str(); }
1901cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan
1911cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan  // Clears the explanation accumulated so far.
1921cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan  void Clear() { ss_.str(""); }
1931cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan
1941cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan private:
1951cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan  ::std::stringstream ss_;
1961cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan
1971cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan  GTEST_DISALLOW_COPY_AND_ASSIGN_(StringMatchResultListener);
1981cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan};
1991cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan
200e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace internal {
201e35fdd936d133bf8a48de140a3c666897588a05shiqian
202506340a66b7814b741b4b4a032ca4b059086f1bbkosakstruct AnyEq {
203506340a66b7814b741b4b4a032ca4b059086f1bbkosak  template <typename A, typename B>
204506340a66b7814b741b4b4a032ca4b059086f1bbkosak  bool operator()(const A& a, const B& b) const { return a == b; }
205506340a66b7814b741b4b4a032ca4b059086f1bbkosak};
206506340a66b7814b741b4b4a032ca4b059086f1bbkosakstruct AnyNe {
207506340a66b7814b741b4b4a032ca4b059086f1bbkosak  template <typename A, typename B>
208506340a66b7814b741b4b4a032ca4b059086f1bbkosak  bool operator()(const A& a, const B& b) const { return a != b; }
209506340a66b7814b741b4b4a032ca4b059086f1bbkosak};
210506340a66b7814b741b4b4a032ca4b059086f1bbkosakstruct AnyLt {
211506340a66b7814b741b4b4a032ca4b059086f1bbkosak  template <typename A, typename B>
212506340a66b7814b741b4b4a032ca4b059086f1bbkosak  bool operator()(const A& a, const B& b) const { return a < b; }
213506340a66b7814b741b4b4a032ca4b059086f1bbkosak};
214506340a66b7814b741b4b4a032ca4b059086f1bbkosakstruct AnyGt {
215506340a66b7814b741b4b4a032ca4b059086f1bbkosak  template <typename A, typename B>
216506340a66b7814b741b4b4a032ca4b059086f1bbkosak  bool operator()(const A& a, const B& b) const { return a > b; }
217506340a66b7814b741b4b4a032ca4b059086f1bbkosak};
218506340a66b7814b741b4b4a032ca4b059086f1bbkosakstruct AnyLe {
219506340a66b7814b741b4b4a032ca4b059086f1bbkosak  template <typename A, typename B>
220506340a66b7814b741b4b4a032ca4b059086f1bbkosak  bool operator()(const A& a, const B& b) const { return a <= b; }
221506340a66b7814b741b4b4a032ca4b059086f1bbkosak};
222506340a66b7814b741b4b4a032ca4b059086f1bbkosakstruct AnyGe {
223506340a66b7814b741b4b4a032ca4b059086f1bbkosak  template <typename A, typename B>
224506340a66b7814b741b4b4a032ca4b059086f1bbkosak  bool operator()(const A& a, const B& b) const { return a >= b; }
225506340a66b7814b741b4b4a032ca4b059086f1bbkosak};
226506340a66b7814b741b4b4a032ca4b059086f1bbkosak
227821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// A match result listener that ignores the explanation.
228821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wanclass DummyMatchResultListener : public MatchResultListener {
229821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan public:
230821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  DummyMatchResultListener() : MatchResultListener(NULL) {}
231821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
232821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan private:
233821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  GTEST_DISALLOW_COPY_AND_ASSIGN_(DummyMatchResultListener);
234821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan};
235821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
236821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// A match result listener that forwards the explanation to a given
237821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// ostream.  The difference between this and MatchResultListener is
238821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// that the former is concrete.
239821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wanclass StreamMatchResultListener : public MatchResultListener {
240821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan public:
241821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  explicit StreamMatchResultListener(::std::ostream* os)
242821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      : MatchResultListener(os) {}
243821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
244821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan private:
245821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  GTEST_DISALLOW_COPY_AND_ASSIGN_(StreamMatchResultListener);
246821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan};
247821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
248e35fdd936d133bf8a48de140a3c666897588a05shiqian// An internal class for implementing Matcher<T>, which will derive
249e35fdd936d133bf8a48de140a3c666897588a05shiqian// from it.  We put functionalities common to all Matcher<T>
250e35fdd936d133bf8a48de140a3c666897588a05shiqian// specializations here to avoid code duplication.
251e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
252e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatcherBase {
253e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
254821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // Returns true iff the matcher matches x; also explains the match
255821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // result to 'listener'.
256821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  bool MatchAndExplain(T x, MatchResultListener* listener) const {
257821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    return impl_->MatchAndExplain(x, listener);
258821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  }
259821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
260e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Returns true iff this matcher matches x.
261821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  bool Matches(T x) const {
262821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    DummyMatchResultListener dummy;
263821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    return MatchAndExplain(x, &dummy);
264821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  }
265e35fdd936d133bf8a48de140a3c666897588a05shiqian
266e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Describes this matcher to an ostream.
267e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const { impl_->DescribeTo(os); }
268e35fdd936d133bf8a48de140a3c666897588a05shiqian
269e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Describes the negation of this matcher to an ostream.
270e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
271e35fdd936d133bf8a48de140a3c666897588a05shiqian    impl_->DescribeNegationTo(os);
272e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
273e35fdd936d133bf8a48de140a3c666897588a05shiqian
274e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Explains why x matches, or doesn't match, the matcher.
275e35fdd936d133bf8a48de140a3c666897588a05shiqian  void ExplainMatchResultTo(T x, ::std::ostream* os) const {
276821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    StreamMatchResultListener listener(os);
277821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    MatchAndExplain(x, &listener);
278e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
27932de5f53763125925e078498250f7e73a88de9edzhanyong.wan
280fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // Returns the describer for this matcher object; retains ownership
281fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // of the describer, which is only guaranteed to be alive when
282fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // this matcher object is alive.
283fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  const MatcherDescriberInterface* GetDescriber() const {
284fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    return impl_.get();
285fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  }
286fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
287e35fdd936d133bf8a48de140a3c666897588a05shiqian protected:
288e35fdd936d133bf8a48de140a3c666897588a05shiqian  MatcherBase() {}
289e35fdd936d133bf8a48de140a3c666897588a05shiqian
290e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Constructs a matcher from its implementation.
291e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit MatcherBase(const MatcherInterface<T>* impl)
292e35fdd936d133bf8a48de140a3c666897588a05shiqian      : impl_(impl) {}
293e35fdd936d133bf8a48de140a3c666897588a05shiqian
294e35fdd936d133bf8a48de140a3c666897588a05shiqian  virtual ~MatcherBase() {}
29532de5f53763125925e078498250f7e73a88de9edzhanyong.wan
296e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
297e35fdd936d133bf8a48de140a3c666897588a05shiqian  // shared_ptr (util/gtl/shared_ptr.h) and linked_ptr have similar
298e35fdd936d133bf8a48de140a3c666897588a05shiqian  // interfaces.  The former dynamically allocates a chunk of memory
299e35fdd936d133bf8a48de140a3c666897588a05shiqian  // to hold the reference count, while the latter tracks all
300e35fdd936d133bf8a48de140a3c666897588a05shiqian  // references using a circular linked list without allocating
301e35fdd936d133bf8a48de140a3c666897588a05shiqian  // memory.  It has been observed that linked_ptr performs better in
302e35fdd936d133bf8a48de140a3c666897588a05shiqian  // typical scenarios.  However, shared_ptr can out-perform
303e35fdd936d133bf8a48de140a3c666897588a05shiqian  // linked_ptr when there are many more uses of the copy constructor
304e35fdd936d133bf8a48de140a3c666897588a05shiqian  // than the default constructor.
305e35fdd936d133bf8a48de140a3c666897588a05shiqian  //
306e35fdd936d133bf8a48de140a3c666897588a05shiqian  // If performance becomes a problem, we should see if using
307e35fdd936d133bf8a48de140a3c666897588a05shiqian  // shared_ptr helps.
308e35fdd936d133bf8a48de140a3c666897588a05shiqian  ::testing::internal::linked_ptr<const MatcherInterface<T> > impl_;
309e35fdd936d133bf8a48de140a3c666897588a05shiqian};
310e35fdd936d133bf8a48de140a3c666897588a05shiqian
311e35fdd936d133bf8a48de140a3c666897588a05shiqian}  // namespace internal
312e35fdd936d133bf8a48de140a3c666897588a05shiqian
313e35fdd936d133bf8a48de140a3c666897588a05shiqian// A Matcher<T> is a copyable and IMMUTABLE (except by assignment)
314e35fdd936d133bf8a48de140a3c666897588a05shiqian// object that can check whether a value of type T matches.  The
315e35fdd936d133bf8a48de140a3c666897588a05shiqian// implementation of Matcher<T> is just a linked_ptr to const
316e35fdd936d133bf8a48de140a3c666897588a05shiqian// MatcherInterface<T>, so copying is fairly cheap.  Don't inherit
317e35fdd936d133bf8a48de140a3c666897588a05shiqian// from Matcher!
318e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
319e35fdd936d133bf8a48de140a3c666897588a05shiqianclass Matcher : public internal::MatcherBase<T> {
320e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
32188032d8e0f6d4bc63b97aeeab84fdc44aeab1c89vladlosev  // Constructs a null matcher.  Needed for storing Matcher objects in STL
32288032d8e0f6d4bc63b97aeeab84fdc44aeab1c89vladlosev  // containers.  A default-constructed matcher is not yet initialized.  You
32388032d8e0f6d4bc63b97aeeab84fdc44aeab1c89vladlosev  // cannot use it until a valid value has been assigned to it.
324d86a723ed95229f7412e72daafd2c0a89c705830kosak  explicit Matcher() {}  // NOLINT
325e35fdd936d133bf8a48de140a3c666897588a05shiqian
326e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Constructs a matcher from its implementation.
327e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit Matcher(const MatcherInterface<T>* impl)
328e35fdd936d133bf8a48de140a3c666897588a05shiqian      : internal::MatcherBase<T>(impl) {}
329e35fdd936d133bf8a48de140a3c666897588a05shiqian
33018490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  // Implicit constructor here allows people to write
331e35fdd936d133bf8a48de140a3c666897588a05shiqian  // EXPECT_CALL(foo, Bar(5)) instead of EXPECT_CALL(foo, Bar(Eq(5))) sometimes
332e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher(T value);  // NOLINT
333e35fdd936d133bf8a48de140a3c666897588a05shiqian};
334e35fdd936d133bf8a48de140a3c666897588a05shiqian
335e35fdd936d133bf8a48de140a3c666897588a05shiqian// The following two specializations allow the user to write str
336e35fdd936d133bf8a48de140a3c666897588a05shiqian// instead of Eq(str) and "foo" instead of Eq("foo") when a string
337e35fdd936d133bf8a48de140a3c666897588a05shiqian// matcher is expected.
338e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <>
339587c1b37c2f0b6d430fb13ce09326db0135b557cvladlosevclass GTEST_API_ Matcher<const internal::string&>
340e35fdd936d133bf8a48de140a3c666897588a05shiqian    : public internal::MatcherBase<const internal::string&> {
341e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
342e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher() {}
343e35fdd936d133bf8a48de140a3c666897588a05shiqian
344e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit Matcher(const MatcherInterface<const internal::string&>* impl)
345e35fdd936d133bf8a48de140a3c666897588a05shiqian      : internal::MatcherBase<const internal::string&>(impl) {}
346e35fdd936d133bf8a48de140a3c666897588a05shiqian
347e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Allows the user to write str instead of Eq(str) sometimes, where
348e35fdd936d133bf8a48de140a3c666897588a05shiqian  // str is a string object.
349e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher(const internal::string& s);  // NOLINT
350e35fdd936d133bf8a48de140a3c666897588a05shiqian
351e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Allows the user to write "foo" instead of Eq("foo") sometimes.
352e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher(const char* s);  // NOLINT
353e35fdd936d133bf8a48de140a3c666897588a05shiqian};
354e35fdd936d133bf8a48de140a3c666897588a05shiqian
355e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <>
356587c1b37c2f0b6d430fb13ce09326db0135b557cvladlosevclass GTEST_API_ Matcher<internal::string>
357e35fdd936d133bf8a48de140a3c666897588a05shiqian    : public internal::MatcherBase<internal::string> {
358e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
359e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher() {}
360e35fdd936d133bf8a48de140a3c666897588a05shiqian
361e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit Matcher(const MatcherInterface<internal::string>* impl)
362e35fdd936d133bf8a48de140a3c666897588a05shiqian      : internal::MatcherBase<internal::string>(impl) {}
363e35fdd936d133bf8a48de140a3c666897588a05shiqian
364e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Allows the user to write str instead of Eq(str) sometimes, where
365e35fdd936d133bf8a48de140a3c666897588a05shiqian  // str is a string object.
366e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher(const internal::string& s);  // NOLINT
367e35fdd936d133bf8a48de140a3c666897588a05shiqian
368e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Allows the user to write "foo" instead of Eq("foo") sometimes.
369e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher(const char* s);  // NOLINT
370e35fdd936d133bf8a48de140a3c666897588a05shiqian};
371e35fdd936d133bf8a48de140a3c666897588a05shiqian
3721f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan#if GTEST_HAS_STRING_PIECE_
3731f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan// The following two specializations allow the user to write str
3741f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan// instead of Eq(str) and "foo" instead of Eq("foo") when a StringPiece
3751f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan// matcher is expected.
3761f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wantemplate <>
3771f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wanclass GTEST_API_ Matcher<const StringPiece&>
3781f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan    : public internal::MatcherBase<const StringPiece&> {
3791f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan public:
3801f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  Matcher() {}
3811f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan
3821f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  explicit Matcher(const MatcherInterface<const StringPiece&>* impl)
3831f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan      : internal::MatcherBase<const StringPiece&>(impl) {}
3841f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan
3851f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  // Allows the user to write str instead of Eq(str) sometimes, where
3861f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  // str is a string object.
3871f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  Matcher(const internal::string& s);  // NOLINT
3881f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan
3891f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  // Allows the user to write "foo" instead of Eq("foo") sometimes.
3901f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  Matcher(const char* s);  // NOLINT
3911f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan
3921f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  // Allows the user to pass StringPieces directly.
3931f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  Matcher(StringPiece s);  // NOLINT
3941f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan};
3951f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan
3961f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wantemplate <>
3971f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wanclass GTEST_API_ Matcher<StringPiece>
3981f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan    : public internal::MatcherBase<StringPiece> {
3991f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan public:
4001f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  Matcher() {}
4011f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan
4021f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  explicit Matcher(const MatcherInterface<StringPiece>* impl)
4031f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan      : internal::MatcherBase<StringPiece>(impl) {}
4041f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan
4051f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  // Allows the user to write str instead of Eq(str) sometimes, where
4061f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  // str is a string object.
4071f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  Matcher(const internal::string& s);  // NOLINT
4081f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan
4091f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  // Allows the user to write "foo" instead of Eq("foo") sometimes.
4101f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  Matcher(const char* s);  // NOLINT
4111f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan
4121f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  // Allows the user to pass StringPieces directly.
4131f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan  Matcher(StringPiece s);  // NOLINT
4141f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan};
4151f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan#endif  // GTEST_HAS_STRING_PIECE_
4161f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan
417e35fdd936d133bf8a48de140a3c666897588a05shiqian// The PolymorphicMatcher class template makes it easy to implement a
418e35fdd936d133bf8a48de140a3c666897588a05shiqian// polymorphic matcher (i.e. a matcher that can match values of more
419e35fdd936d133bf8a48de140a3c666897588a05shiqian// than one type, e.g. Eq(n) and NotNull()).
420e35fdd936d133bf8a48de140a3c666897588a05shiqian//
421db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan// To define a polymorphic matcher, a user should provide an Impl
422db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan// class that has a DescribeTo() method and a DescribeNegationTo()
423db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan// method, and define a member function (or member function template)
424821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan//
425db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan//   bool MatchAndExplain(const Value& value,
426db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan//                        MatchResultListener* listener) const;
427821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan//
428821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// See the definition of NotNull() for a complete example.
429e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <class Impl>
430e35fdd936d133bf8a48de140a3c666897588a05shiqianclass PolymorphicMatcher {
431e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
43232de5f53763125925e078498250f7e73a88de9edzhanyong.wan  explicit PolymorphicMatcher(const Impl& an_impl) : impl_(an_impl) {}
433e35fdd936d133bf8a48de140a3c666897588a05shiqian
4342b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan  // Returns a mutable reference to the underlying matcher
4352b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan  // implementation object.
4362b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan  Impl& mutable_impl() { return impl_; }
4372b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan
4382b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan  // Returns an immutable reference to the underlying matcher
4392b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan  // implementation object.
4402b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan  const Impl& impl() const { return impl_; }
4412b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan
442e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
443e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<T>() const {
444e35fdd936d133bf8a48de140a3c666897588a05shiqian    return Matcher<T>(new MonomorphicImpl<T>(impl_));
445e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
44632de5f53763125925e078498250f7e73a88de9edzhanyong.wan
447e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
448e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
449e35fdd936d133bf8a48de140a3c666897588a05shiqian  class MonomorphicImpl : public MatcherInterface<T> {
450e35fdd936d133bf8a48de140a3c666897588a05shiqian   public:
451e35fdd936d133bf8a48de140a3c666897588a05shiqian    explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {}
452e35fdd936d133bf8a48de140a3c666897588a05shiqian
453e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeTo(::std::ostream* os) const {
454e35fdd936d133bf8a48de140a3c666897588a05shiqian      impl_.DescribeTo(os);
455e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
456e35fdd936d133bf8a48de140a3c666897588a05shiqian
457e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeNegationTo(::std::ostream* os) const {
458e35fdd936d133bf8a48de140a3c666897588a05shiqian      impl_.DescribeNegationTo(os);
459e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
460e35fdd936d133bf8a48de140a3c666897588a05shiqian
461821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
462db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan      return impl_.MatchAndExplain(x, listener);
463e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
4642b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan
465e35fdd936d133bf8a48de140a3c666897588a05shiqian   private:
466e35fdd936d133bf8a48de140a3c666897588a05shiqian    const Impl impl_;
46732de5f53763125925e078498250f7e73a88de9edzhanyong.wan
46832de5f53763125925e078498250f7e73a88de9edzhanyong.wan    GTEST_DISALLOW_ASSIGN_(MonomorphicImpl);
469e35fdd936d133bf8a48de140a3c666897588a05shiqian  };
470e35fdd936d133bf8a48de140a3c666897588a05shiqian
4712b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan  Impl impl_;
47232de5f53763125925e078498250f7e73a88de9edzhanyong.wan
47332de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(PolymorphicMatcher);
474e35fdd936d133bf8a48de140a3c666897588a05shiqian};
475e35fdd936d133bf8a48de140a3c666897588a05shiqian
476e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher from its implementation.  This is easier to use
477e35fdd936d133bf8a48de140a3c666897588a05shiqian// than the Matcher<T> constructor as it doesn't require you to
478e35fdd936d133bf8a48de140a3c666897588a05shiqian// explicitly write the template argument, e.g.
479e35fdd936d133bf8a48de140a3c666897588a05shiqian//
480e35fdd936d133bf8a48de140a3c666897588a05shiqian//   MakeMatcher(foo);
481e35fdd936d133bf8a48de140a3c666897588a05shiqian// vs
482e35fdd936d133bf8a48de140a3c666897588a05shiqian//   Matcher<const string&>(foo);
483e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
484e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<T> MakeMatcher(const MatcherInterface<T>* impl) {
485e35fdd936d133bf8a48de140a3c666897588a05shiqian  return Matcher<T>(impl);
4862eab17b76d350dac1b1c85879ec8e1135da615cezhanyong.wan}
487e35fdd936d133bf8a48de140a3c666897588a05shiqian
488e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher from its implementation.  This is
489e35fdd936d133bf8a48de140a3c666897588a05shiqian// easier to use than the PolymorphicMatcher<Impl> constructor as it
490e35fdd936d133bf8a48de140a3c666897588a05shiqian// doesn't require you to explicitly write the template argument, e.g.
491e35fdd936d133bf8a48de140a3c666897588a05shiqian//
492e35fdd936d133bf8a48de140a3c666897588a05shiqian//   MakePolymorphicMatcher(foo);
493e35fdd936d133bf8a48de140a3c666897588a05shiqian// vs
494e35fdd936d133bf8a48de140a3c666897588a05shiqian//   PolymorphicMatcher<TypeOfFoo>(foo);
495e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <class Impl>
496e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<Impl> MakePolymorphicMatcher(const Impl& impl) {
497e35fdd936d133bf8a48de140a3c666897588a05shiqian  return PolymorphicMatcher<Impl>(impl);
498e35fdd936d133bf8a48de140a3c666897588a05shiqian}
499e35fdd936d133bf8a48de140a3c666897588a05shiqian
50079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// Anything inside the 'internal' namespace IS INTERNAL IMPLEMENTATION
50179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// and MUST NOT BE USED IN USER CODE!!!
50279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgmnamespace internal {
50379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm
50479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// The MatcherCastImpl class template is a helper for implementing
50579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// MatcherCast().  We need this helper in order to partially
50679a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// specialize the implementation of MatcherCast() (C++ allows
50779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// class/struct templates to be partially specialized, but not
50879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// function templates.).
50979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm
51079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// This general version is used when MatcherCast()'s argument is a
51179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// polymorphic matcher (i.e. something that can be converted to a
51279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// Matcher but is not one yet; for example, Eq(value)) or a value (for
51379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// example, "hello").
51479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgmtemplate <typename T, typename M>
51579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgmclass MatcherCastImpl {
51679a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm public:
5175f2a6ca4fd5e1ab6330be758ea88bfe778af2b77kosak  static Matcher<T> Cast(const M& polymorphic_matcher_or_value) {
51879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // M can be a polymorhic matcher, in which case we want to use
51979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // its conversion operator to create Matcher<T>.  Or it can be a value
52079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // that should be passed to the Matcher<T>'s constructor.
52179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    //
52279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // We can't call Matcher<T>(polymorphic_matcher_or_value) when M is a
52379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // polymorphic matcher because it'll be ambiguous if T has an implicit
52479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // constructor from M (this usually happens when T has an implicit
52579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // constructor from any type).
52679a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    //
52779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // It won't work to unconditionally implict_cast
52879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // polymorphic_matcher_or_value to Matcher<T> because it won't trigger
52979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // a user-defined conversion from M to T if one exists (assuming M is
53079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // a value).
53179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    return CastImpl(
53279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm        polymorphic_matcher_or_value,
53379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm        BooleanConstant<
53479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm            internal::ImplicitlyConvertible<M, Matcher<T> >::value>());
53579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm  }
53679a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm
53779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm private:
5385f2a6ca4fd5e1ab6330be758ea88bfe778af2b77kosak  static Matcher<T> CastImpl(const M& value, BooleanConstant<false>) {
53979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // M can't be implicitly converted to Matcher<T>, so M isn't a polymorphic
54079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // matcher.  It must be a value then.  Use direct initialization to create
54179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // a matcher.
54279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    return Matcher<T>(ImplicitCast_<T>(value));
54379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm  }
54479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm
5455f2a6ca4fd5e1ab6330be758ea88bfe778af2b77kosak  static Matcher<T> CastImpl(const M& polymorphic_matcher_or_value,
54679a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm                             BooleanConstant<true>) {
54779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // M is implicitly convertible to Matcher<T>, which means that either
54879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // M is a polymorhpic matcher or Matcher<T> has an implicit constructor
54979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // from M.  In both cases using the implicit conversion will produce a
55079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // matcher.
55179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    //
55279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // Even if T has an implicit constructor from M, it won't be called because
55379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // creating Matcher<T> would require a chain of two user-defined conversions
55479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // (first to create T from M and then to create Matcher<T> from T).
55579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    return polymorphic_matcher_or_value;
55679a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm  }
55779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm};
55879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm
55979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// This more specialized version is used when MatcherCast()'s argument
56079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// is already a Matcher.  This only compiles when type T can be
56179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// statically converted to type U.
56279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgmtemplate <typename T, typename U>
56379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgmclass MatcherCastImpl<T, Matcher<U> > {
56479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm public:
56579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm  static Matcher<T> Cast(const Matcher<U>& source_matcher) {
56679a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    return Matcher<T>(new Impl(source_matcher));
56779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm  }
56879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm
56979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm private:
57079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm  class Impl : public MatcherInterface<T> {
57179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm   public:
57279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    explicit Impl(const Matcher<U>& source_matcher)
57379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm        : source_matcher_(source_matcher) {}
57479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm
57579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    // We delegate the matching logic to the source matcher.
57679a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
57779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm      return source_matcher_.MatchAndExplain(static_cast<U>(x), listener);
57879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    }
57979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm
58079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    virtual void DescribeTo(::std::ostream* os) const {
58179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm      source_matcher_.DescribeTo(os);
58279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    }
58379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm
58479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    virtual void DescribeNegationTo(::std::ostream* os) const {
58579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm      source_matcher_.DescribeNegationTo(os);
58679a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    }
58779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm
58879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm   private:
58979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    const Matcher<U> source_matcher_;
59079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm
59179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    GTEST_DISALLOW_ASSIGN_(Impl);
59279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm  };
59379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm};
59479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm
59579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// This even more specialized version is used for efficiently casting
59679a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// a matcher to its own type.
59779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgmtemplate <typename T>
59879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgmclass MatcherCastImpl<T, Matcher<T> > {
59979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm public:
60079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm  static Matcher<T> Cast(const Matcher<T>& matcher) { return matcher; }
60179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm};
60279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm
60379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm}  // namespace internal
60479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm
605e35fdd936d133bf8a48de140a3c666897588a05shiqian// In order to be safe and clear, casting between different matcher
606e35fdd936d133bf8a48de140a3c666897588a05shiqian// types is done explicitly via MatcherCast<T>(m), which takes a
607e35fdd936d133bf8a48de140a3c666897588a05shiqian// matcher m and returns a Matcher<T>.  It compiles only when T can be
608e35fdd936d133bf8a48de140a3c666897588a05shiqian// statically converted to the argument type of m.
609e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T, typename M>
6105f2a6ca4fd5e1ab6330be758ea88bfe778af2b77kosakinline Matcher<T> MatcherCast(const M& matcher) {
61179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm  return internal::MatcherCastImpl<T, M>::Cast(matcher);
61279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm}
613e35fdd936d133bf8a48de140a3c666897588a05shiqian
61418490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// Implements SafeMatcherCast().
61518490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan//
61695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// We use an intermediate class to do the actual safe casting as Nokia's
61795b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// Symbian compiler cannot decide between
61895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// template <T, M> ... (M) and
61995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// template <T, U> ... (const Matcher<U>&)
62095b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// for function templates but can for member function templates.
62195b12332c345cee508a8e2b68e007280392506e0zhanyong.wantemplate <typename T>
62295b12332c345cee508a8e2b68e007280392506e0zhanyong.wanclass SafeMatcherCastImpl {
62395b12332c345cee508a8e2b68e007280392506e0zhanyong.wan public:
62479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm  // This overload handles polymorphic matchers and values only since
62579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm  // monomorphic matchers are handled by the next one.
62695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  template <typename M>
6275f2a6ca4fd5e1ab6330be758ea88bfe778af2b77kosak  static inline Matcher<T> Cast(const M& polymorphic_matcher_or_value) {
62879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm    return internal::MatcherCastImpl<T, M>::Cast(polymorphic_matcher_or_value);
62995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  }
63018490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan
63195b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  // This overload handles monomorphic matchers.
63295b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  //
63395b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  // In general, if type T can be implicitly converted to type U, we can
63495b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  // safely convert a Matcher<U> to a Matcher<T> (i.e. Matcher is
63595b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  // contravariant): just keep a copy of the original Matcher<U>, convert the
63695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  // argument from type T to U, and then pass it to the underlying Matcher<U>.
63795b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  // The only exception is when U is a reference and T is not, as the
63895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  // underlying Matcher<U> may be interested in the argument's address, which
63995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  // is not preserved in the conversion from T to U.
64095b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  template <typename U>
64195b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  static inline Matcher<T> Cast(const Matcher<U>& matcher) {
64295b12332c345cee508a8e2b68e007280392506e0zhanyong.wan    // Enforce that T can be implicitly converted to U.
64302f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan    GTEST_COMPILE_ASSERT_((internal::ImplicitlyConvertible<T, U>::value),
64495b12332c345cee508a8e2b68e007280392506e0zhanyong.wan                          T_must_be_implicitly_convertible_to_U);
64595b12332c345cee508a8e2b68e007280392506e0zhanyong.wan    // Enforce that we are not converting a non-reference type T to a reference
64695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan    // type U.
64702f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan    GTEST_COMPILE_ASSERT_(
64895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan        internal::is_reference<T>::value || !internal::is_reference<U>::value,
64995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan        cannot_convert_non_referentce_arg_to_reference);
65095b12332c345cee508a8e2b68e007280392506e0zhanyong.wan    // In case both T and U are arithmetic types, enforce that the
65195b12332c345cee508a8e2b68e007280392506e0zhanyong.wan    // conversion is not lossy.
652ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    typedef GTEST_REMOVE_REFERENCE_AND_CONST_(T) RawT;
653ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    typedef GTEST_REMOVE_REFERENCE_AND_CONST_(U) RawU;
65495b12332c345cee508a8e2b68e007280392506e0zhanyong.wan    const bool kTIsOther = GMOCK_KIND_OF_(RawT) == internal::kOther;
65595b12332c345cee508a8e2b68e007280392506e0zhanyong.wan    const bool kUIsOther = GMOCK_KIND_OF_(RawU) == internal::kOther;
65602f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan    GTEST_COMPILE_ASSERT_(
65795b12332c345cee508a8e2b68e007280392506e0zhanyong.wan        kTIsOther || kUIsOther ||
65895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan        (internal::LosslessArithmeticConvertible<RawT, RawU>::value),
65995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan        conversion_of_arithmetic_types_must_be_lossless);
66095b12332c345cee508a8e2b68e007280392506e0zhanyong.wan    return MatcherCast<T>(matcher);
66195b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  }
66295b12332c345cee508a8e2b68e007280392506e0zhanyong.wan};
66395b12332c345cee508a8e2b68e007280392506e0zhanyong.wan
66495b12332c345cee508a8e2b68e007280392506e0zhanyong.wantemplate <typename T, typename M>
66595b12332c345cee508a8e2b68e007280392506e0zhanyong.waninline Matcher<T> SafeMatcherCast(const M& polymorphic_matcher) {
66695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan  return SafeMatcherCastImpl<T>::Cast(polymorphic_matcher);
66718490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan}
66818490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan
669e35fdd936d133bf8a48de140a3c666897588a05shiqian// A<T>() returns a matcher that matches any value of type T.
670e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
671e35fdd936d133bf8a48de140a3c666897588a05shiqianMatcher<T> A();
672e35fdd936d133bf8a48de140a3c666897588a05shiqian
673e35fdd936d133bf8a48de140a3c666897588a05shiqian// Anything inside the 'internal' namespace IS INTERNAL IMPLEMENTATION
674e35fdd936d133bf8a48de140a3c666897588a05shiqian// and MUST NOT BE USED IN USER CODE!!!
675e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace internal {
676e35fdd936d133bf8a48de140a3c666897588a05shiqian
677b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan// If the explanation is not empty, prints it to the ostream.
678b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.waninline void PrintIfNotEmpty(const internal::string& explanation,
679fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan                            ::std::ostream* os) {
680b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan  if (explanation != "" && os != NULL) {
681b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    *os << ", " << explanation;
682676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan  }
683676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan}
684676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan
685736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan// Returns true if the given type name is easy to read by a human.
686736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan// This is used to decide whether printing the type of a value might
687736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan// be helpful.
688736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.waninline bool IsReadableTypeName(const string& type_name) {
689736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan  // We consider a type name readable if it's short or doesn't contain
690736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan  // a template or function type.
691736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan  return (type_name.length() <= 20 ||
692736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan          type_name.find_first_of("<(") == string::npos);
693736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan}
694736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan
695676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan// Matches the value against the given matcher, prints the value and explains
696676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan// the match result to the listener. Returns the match result.
697676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan// 'listener' must not be NULL.
698676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan// Value cannot be passed by const reference, because some matchers take a
699676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan// non-const argument.
700676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wantemplate <typename Value, typename T>
701676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wanbool MatchPrintAndExplain(Value& value, const Matcher<T>& matcher,
702676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan                          MatchResultListener* listener) {
703676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan  if (!listener->IsInterested()) {
704676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    // If the listener is not interested, we do not need to construct the
705676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    // inner explanation.
706676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    return matcher.Matches(value);
707676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan  }
708676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan
709676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan  StringMatchResultListener inner_listener;
710676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan  const bool match = matcher.MatchAndExplain(value, &inner_listener);
711676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan
712676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan  UniversalPrint(value, listener->stream());
713736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan#if GTEST_HAS_RTTI
714736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan  const string& type_name = GetTypeName<Value>();
715736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan  if (IsReadableTypeName(type_name))
716736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan    *listener->stream() << " (of type " << type_name << ")";
717736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan#endif
718b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan  PrintIfNotEmpty(inner_listener.str(), listener->stream());
719676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan
720676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan  return match;
721676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan}
722676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan
723e35fdd936d133bf8a48de140a3c666897588a05shiqian// An internal helper class for doing compile-time loop on a tuple's
724e35fdd936d133bf8a48de140a3c666897588a05shiqian// fields.
725e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <size_t N>
726e35fdd936d133bf8a48de140a3c666897588a05shiqianclass TuplePrefix {
727e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
728e35fdd936d133bf8a48de140a3c666897588a05shiqian  // TuplePrefix<N>::Matches(matcher_tuple, value_tuple) returns true
729e35fdd936d133bf8a48de140a3c666897588a05shiqian  // iff the first N fields of matcher_tuple matches the first N
730e35fdd936d133bf8a48de140a3c666897588a05shiqian  // fields of value_tuple, respectively.
731e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename MatcherTuple, typename ValueTuple>
732e35fdd936d133bf8a48de140a3c666897588a05shiqian  static bool Matches(const MatcherTuple& matcher_tuple,
733e35fdd936d133bf8a48de140a3c666897588a05shiqian                      const ValueTuple& value_tuple) {
734e35fdd936d133bf8a48de140a3c666897588a05shiqian    return TuplePrefix<N - 1>::Matches(matcher_tuple, value_tuple)
735e35fdd936d133bf8a48de140a3c666897588a05shiqian        && get<N - 1>(matcher_tuple).Matches(get<N - 1>(value_tuple));
736e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
737e35fdd936d133bf8a48de140a3c666897588a05shiqian
738b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan  // TuplePrefix<N>::ExplainMatchFailuresTo(matchers, values, os)
739e35fdd936d133bf8a48de140a3c666897588a05shiqian  // describes failures in matching the first N fields of matchers
740e35fdd936d133bf8a48de140a3c666897588a05shiqian  // against the first N fields of values.  If there is no failure,
741e35fdd936d133bf8a48de140a3c666897588a05shiqian  // nothing will be streamed to os.
742e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename MatcherTuple, typename ValueTuple>
743b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan  static void ExplainMatchFailuresTo(const MatcherTuple& matchers,
744b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan                                     const ValueTuple& values,
745b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan                                     ::std::ostream* os) {
746e35fdd936d133bf8a48de140a3c666897588a05shiqian    // First, describes failures in the first N - 1 fields.
747b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    TuplePrefix<N - 1>::ExplainMatchFailuresTo(matchers, values, os);
748e35fdd936d133bf8a48de140a3c666897588a05shiqian
749e35fdd936d133bf8a48de140a3c666897588a05shiqian    // Then describes the failure (if any) in the (N - 1)-th (0-based)
750e35fdd936d133bf8a48de140a3c666897588a05shiqian    // field.
751e35fdd936d133bf8a48de140a3c666897588a05shiqian    typename tuple_element<N - 1, MatcherTuple>::type matcher =
752e35fdd936d133bf8a48de140a3c666897588a05shiqian        get<N - 1>(matchers);
753e35fdd936d133bf8a48de140a3c666897588a05shiqian    typedef typename tuple_element<N - 1, ValueTuple>::type Value;
754e35fdd936d133bf8a48de140a3c666897588a05shiqian    Value value = get<N - 1>(values);
755821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    StringMatchResultListener listener;
756821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (!matcher.MatchAndExplain(value, &listener)) {
757e35fdd936d133bf8a48de140a3c666897588a05shiqian      // TODO(wan): include in the message the name of the parameter
758e35fdd936d133bf8a48de140a3c666897588a05shiqian      // as used in MOCK_METHOD*() when possible.
759e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "  Expected arg #" << N - 1 << ": ";
760e35fdd936d133bf8a48de140a3c666897588a05shiqian      get<N - 1>(matchers).DescribeTo(os);
761e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "\n           Actual: ";
762e35fdd936d133bf8a48de140a3c666897588a05shiqian      // We remove the reference in type Value to prevent the
763e35fdd936d133bf8a48de140a3c666897588a05shiqian      // universal printer from printing the address of value, which
764e35fdd936d133bf8a48de140a3c666897588a05shiqian      // isn't interesting to the user most of the time.  The
765db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan      // matcher's MatchAndExplain() method handles the case when
766e35fdd936d133bf8a48de140a3c666897588a05shiqian      // the address is interesting.
767b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan      internal::UniversalPrint(value, os);
768b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan      PrintIfNotEmpty(listener.str(), os);
769e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "\n";
770e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
771e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
772e35fdd936d133bf8a48de140a3c666897588a05shiqian};
773e35fdd936d133bf8a48de140a3c666897588a05shiqian
774e35fdd936d133bf8a48de140a3c666897588a05shiqian// The base case.
775e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <>
776e35fdd936d133bf8a48de140a3c666897588a05shiqianclass TuplePrefix<0> {
777e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
778e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename MatcherTuple, typename ValueTuple>
7793fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.wan  static bool Matches(const MatcherTuple& /* matcher_tuple */,
7803fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.wan                      const ValueTuple& /* value_tuple */) {
781e35fdd936d133bf8a48de140a3c666897588a05shiqian    return true;
782e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
783e35fdd936d133bf8a48de140a3c666897588a05shiqian
784e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename MatcherTuple, typename ValueTuple>
785b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan  static void ExplainMatchFailuresTo(const MatcherTuple& /* matchers */,
786b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan                                     const ValueTuple& /* values */,
787b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan                                     ::std::ostream* /* os */) {}
788e35fdd936d133bf8a48de140a3c666897588a05shiqian};
789e35fdd936d133bf8a48de140a3c666897588a05shiqian
790e35fdd936d133bf8a48de140a3c666897588a05shiqian// TupleMatches(matcher_tuple, value_tuple) returns true iff all
791e35fdd936d133bf8a48de140a3c666897588a05shiqian// matchers in matcher_tuple match the corresponding fields in
792e35fdd936d133bf8a48de140a3c666897588a05shiqian// value_tuple.  It is a compiler error if matcher_tuple and
793e35fdd936d133bf8a48de140a3c666897588a05shiqian// value_tuple have different number of fields or incompatible field
794e35fdd936d133bf8a48de140a3c666897588a05shiqian// types.
795e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename MatcherTuple, typename ValueTuple>
796e35fdd936d133bf8a48de140a3c666897588a05shiqianbool TupleMatches(const MatcherTuple& matcher_tuple,
797e35fdd936d133bf8a48de140a3c666897588a05shiqian                  const ValueTuple& value_tuple) {
798e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Makes sure that matcher_tuple and value_tuple have the same
799e35fdd936d133bf8a48de140a3c666897588a05shiqian  // number of fields.
80002f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan  GTEST_COMPILE_ASSERT_(tuple_size<MatcherTuple>::value ==
801e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan                        tuple_size<ValueTuple>::value,
802e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan                        matcher_and_value_have_different_numbers_of_fields);
803e35fdd936d133bf8a48de140a3c666897588a05shiqian  return TuplePrefix<tuple_size<ValueTuple>::value>::
804e35fdd936d133bf8a48de140a3c666897588a05shiqian      Matches(matcher_tuple, value_tuple);
805e35fdd936d133bf8a48de140a3c666897588a05shiqian}
806e35fdd936d133bf8a48de140a3c666897588a05shiqian
807e35fdd936d133bf8a48de140a3c666897588a05shiqian// Describes failures in matching matchers against values.  If there
808e35fdd936d133bf8a48de140a3c666897588a05shiqian// is no failure, nothing will be streamed to os.
809e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename MatcherTuple, typename ValueTuple>
810b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wanvoid ExplainMatchFailureTupleTo(const MatcherTuple& matchers,
811b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan                                const ValueTuple& values,
812b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan                                ::std::ostream* os) {
813b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan  TuplePrefix<tuple_size<MatcherTuple>::value>::ExplainMatchFailuresTo(
814e35fdd936d133bf8a48de140a3c666897588a05shiqian      matchers, values, os);
815e35fdd936d133bf8a48de140a3c666897588a05shiqian}
816e35fdd936d133bf8a48de140a3c666897588a05shiqian
817fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// TransformTupleValues and its helper.
818fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan//
819fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// TransformTupleValuesHelper hides the internal machinery that
820fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// TransformTupleValues uses to implement a tuple traversal.
821fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantemplate <typename Tuple, typename Func, typename OutIter>
822fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanclass TransformTupleValuesHelper {
823fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan private:
824bd0188320de5aab1b09718e2c466387099d43cfbkosak  typedef ::testing::tuple_size<Tuple> TupleSize;
825fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
826fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan public:
827fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // For each member of tuple 't', taken in order, evaluates '*out++ = f(t)'.
828fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // Returns the final value of 'out' in case the caller needs it.
829fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  static OutIter Run(Func f, const Tuple& t, OutIter out) {
830fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    return IterateOverTuple<Tuple, TupleSize::value>()(f, t, out);
831fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  }
832fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
833fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan private:
834fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  template <typename Tup, size_t kRemainingSize>
835fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  struct IterateOverTuple {
836fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    OutIter operator() (Func f, const Tup& t, OutIter out) const {
837bd0188320de5aab1b09718e2c466387099d43cfbkosak      *out++ = f(::testing::get<TupleSize::value - kRemainingSize>(t));
838fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan      return IterateOverTuple<Tup, kRemainingSize - 1>()(f, t, out);
839fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    }
840fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  };
841fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  template <typename Tup>
842fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  struct IterateOverTuple<Tup, 0> {
843fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    OutIter operator() (Func /* f */, const Tup& /* t */, OutIter out) const {
844fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan      return out;
845fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    }
846fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  };
847fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan};
848fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
849fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// Successively invokes 'f(element)' on each element of the tuple 't',
850fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// appending each result to the 'out' iterator. Returns the final value
851fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// of 'out'.
852fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantemplate <typename Tuple, typename Func, typename OutIter>
853fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanOutIter TransformTupleValues(Func f, const Tuple& t, OutIter out) {
854fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  return TransformTupleValuesHelper<Tuple, Func, OutIter>::Run(f, t, out);
855fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan}
856fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
857e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements A<T>().
858e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
859e35fdd936d133bf8a48de140a3c666897588a05shiqianclass AnyMatcherImpl : public MatcherInterface<T> {
860e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
861821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  virtual bool MatchAndExplain(
862821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      T /* x */, MatchResultListener* /* listener */) const { return true; }
863e35fdd936d133bf8a48de140a3c666897588a05shiqian  virtual void DescribeTo(::std::ostream* os) const { *os << "is anything"; }
864e35fdd936d133bf8a48de140a3c666897588a05shiqian  virtual void DescribeNegationTo(::std::ostream* os) const {
865e35fdd936d133bf8a48de140a3c666897588a05shiqian    // This is mostly for completeness' safe, as it's not very useful
866e35fdd936d133bf8a48de140a3c666897588a05shiqian    // to write Not(A<bool>()).  However we cannot completely rule out
867e35fdd936d133bf8a48de140a3c666897588a05shiqian    // such a possibility, and it doesn't hurt to be prepared.
868e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "never matches";
869e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
870e35fdd936d133bf8a48de140a3c666897588a05shiqian};
871e35fdd936d133bf8a48de140a3c666897588a05shiqian
872e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements _, a matcher that matches any value of any
873e35fdd936d133bf8a48de140a3c666897588a05shiqian// type.  This is a polymorphic matcher, so we need a template type
874e35fdd936d133bf8a48de140a3c666897588a05shiqian// conversion operator to make it appearing as a Matcher<T> for any
875e35fdd936d133bf8a48de140a3c666897588a05shiqian// type T.
876e35fdd936d133bf8a48de140a3c666897588a05shiqianclass AnythingMatcher {
877e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
878e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
879e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<T>() const { return A<T>(); }
880e35fdd936d133bf8a48de140a3c666897588a05shiqian};
881e35fdd936d133bf8a48de140a3c666897588a05shiqian
882e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements a matcher that compares a given value with a
883e35fdd936d133bf8a48de140a3c666897588a05shiqian// pre-supplied value using one of the ==, <=, <, etc, operators.  The
884e35fdd936d133bf8a48de140a3c666897588a05shiqian// two values being compared don't have to have the same type.
885e35fdd936d133bf8a48de140a3c666897588a05shiqian//
886e35fdd936d133bf8a48de140a3c666897588a05shiqian// The matcher defined here is polymorphic (for example, Eq(5) can be
887e35fdd936d133bf8a48de140a3c666897588a05shiqian// used to match an int, a short, a double, etc).  Therefore we use
888e35fdd936d133bf8a48de140a3c666897588a05shiqian// a template type conversion operator in the implementation.
889e35fdd936d133bf8a48de140a3c666897588a05shiqian//
890e35fdd936d133bf8a48de140a3c666897588a05shiqian// The following template definition assumes that the Rhs parameter is
891e35fdd936d133bf8a48de140a3c666897588a05shiqian// a "bare" type (i.e. neither 'const T' nor 'T&').
892506340a66b7814b741b4b4a032ca4b059086f1bbkosaktemplate <typename D, typename Rhs, typename Op>
893506340a66b7814b741b4b4a032ca4b059086f1bbkosakclass ComparisonBase {
894506340a66b7814b741b4b4a032ca4b059086f1bbkosak public:
895506340a66b7814b741b4b4a032ca4b059086f1bbkosak  explicit ComparisonBase(const Rhs& rhs) : rhs_(rhs) {}
896506340a66b7814b741b4b4a032ca4b059086f1bbkosak  template <typename Lhs>
897506340a66b7814b741b4b4a032ca4b059086f1bbkosak  operator Matcher<Lhs>() const {
898506340a66b7814b741b4b4a032ca4b059086f1bbkosak    return MakeMatcher(new Impl<Lhs>(rhs_));
899e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
900e35fdd936d133bf8a48de140a3c666897588a05shiqian
901506340a66b7814b741b4b4a032ca4b059086f1bbkosak private:
902506340a66b7814b741b4b4a032ca4b059086f1bbkosak  template <typename Lhs>
903506340a66b7814b741b4b4a032ca4b059086f1bbkosak  class Impl : public MatcherInterface<Lhs> {
904506340a66b7814b741b4b4a032ca4b059086f1bbkosak   public:
905506340a66b7814b741b4b4a032ca4b059086f1bbkosak    explicit Impl(const Rhs& rhs) : rhs_(rhs) {}
906506340a66b7814b741b4b4a032ca4b059086f1bbkosak    virtual bool MatchAndExplain(
907506340a66b7814b741b4b4a032ca4b059086f1bbkosak        Lhs lhs, MatchResultListener* /* listener */) const {
908506340a66b7814b741b4b4a032ca4b059086f1bbkosak      return Op()(lhs, rhs_);
909506340a66b7814b741b4b4a032ca4b059086f1bbkosak    }
910506340a66b7814b741b4b4a032ca4b059086f1bbkosak    virtual void DescribeTo(::std::ostream* os) const {
911506340a66b7814b741b4b4a032ca4b059086f1bbkosak      *os << D::Desc() << " ";
912506340a66b7814b741b4b4a032ca4b059086f1bbkosak      UniversalPrint(rhs_, os);
913506340a66b7814b741b4b4a032ca4b059086f1bbkosak    }
914506340a66b7814b741b4b4a032ca4b059086f1bbkosak    virtual void DescribeNegationTo(::std::ostream* os) const {
915506340a66b7814b741b4b4a032ca4b059086f1bbkosak      *os << D::NegatedDesc() <<  " ";
916506340a66b7814b741b4b4a032ca4b059086f1bbkosak      UniversalPrint(rhs_, os);
917506340a66b7814b741b4b4a032ca4b059086f1bbkosak    }
918506340a66b7814b741b4b4a032ca4b059086f1bbkosak   private:
919506340a66b7814b741b4b4a032ca4b059086f1bbkosak    Rhs rhs_;
920506340a66b7814b741b4b4a032ca4b059086f1bbkosak    GTEST_DISALLOW_ASSIGN_(Impl);
921506340a66b7814b741b4b4a032ca4b059086f1bbkosak  };
922506340a66b7814b741b4b4a032ca4b059086f1bbkosak  Rhs rhs_;
923506340a66b7814b741b4b4a032ca4b059086f1bbkosak  GTEST_DISALLOW_ASSIGN_(ComparisonBase);
924506340a66b7814b741b4b4a032ca4b059086f1bbkosak};
925e35fdd936d133bf8a48de140a3c666897588a05shiqian
926506340a66b7814b741b4b4a032ca4b059086f1bbkosaktemplate <typename Rhs>
927506340a66b7814b741b4b4a032ca4b059086f1bbkosakclass EqMatcher : public ComparisonBase<EqMatcher<Rhs>, Rhs, AnyEq> {
928506340a66b7814b741b4b4a032ca4b059086f1bbkosak public:
929506340a66b7814b741b4b4a032ca4b059086f1bbkosak  explicit EqMatcher(const Rhs& rhs)
930506340a66b7814b741b4b4a032ca4b059086f1bbkosak      : ComparisonBase<EqMatcher<Rhs>, Rhs, AnyEq>(rhs) { }
931506340a66b7814b741b4b4a032ca4b059086f1bbkosak  static const char* Desc() { return "is equal to"; }
932506340a66b7814b741b4b4a032ca4b059086f1bbkosak  static const char* NegatedDesc() { return "isn't equal to"; }
933506340a66b7814b741b4b4a032ca4b059086f1bbkosak};
934506340a66b7814b741b4b4a032ca4b059086f1bbkosaktemplate <typename Rhs>
935506340a66b7814b741b4b4a032ca4b059086f1bbkosakclass NeMatcher : public ComparisonBase<NeMatcher<Rhs>, Rhs, AnyNe> {
936506340a66b7814b741b4b4a032ca4b059086f1bbkosak public:
937506340a66b7814b741b4b4a032ca4b059086f1bbkosak  explicit NeMatcher(const Rhs& rhs)
938506340a66b7814b741b4b4a032ca4b059086f1bbkosak      : ComparisonBase<NeMatcher<Rhs>, Rhs, AnyNe>(rhs) { }
939506340a66b7814b741b4b4a032ca4b059086f1bbkosak  static const char* Desc() { return "isn't equal to"; }
940506340a66b7814b741b4b4a032ca4b059086f1bbkosak  static const char* NegatedDesc() { return "is equal to"; }
941506340a66b7814b741b4b4a032ca4b059086f1bbkosak};
942506340a66b7814b741b4b4a032ca4b059086f1bbkosaktemplate <typename Rhs>
943506340a66b7814b741b4b4a032ca4b059086f1bbkosakclass LtMatcher : public ComparisonBase<LtMatcher<Rhs>, Rhs, AnyLt> {
944506340a66b7814b741b4b4a032ca4b059086f1bbkosak public:
945506340a66b7814b741b4b4a032ca4b059086f1bbkosak  explicit LtMatcher(const Rhs& rhs)
946506340a66b7814b741b4b4a032ca4b059086f1bbkosak      : ComparisonBase<LtMatcher<Rhs>, Rhs, AnyLt>(rhs) { }
947506340a66b7814b741b4b4a032ca4b059086f1bbkosak  static const char* Desc() { return "is <"; }
948506340a66b7814b741b4b4a032ca4b059086f1bbkosak  static const char* NegatedDesc() { return "isn't <"; }
949506340a66b7814b741b4b4a032ca4b059086f1bbkosak};
950506340a66b7814b741b4b4a032ca4b059086f1bbkosaktemplate <typename Rhs>
951506340a66b7814b741b4b4a032ca4b059086f1bbkosakclass GtMatcher : public ComparisonBase<GtMatcher<Rhs>, Rhs, AnyGt> {
952506340a66b7814b741b4b4a032ca4b059086f1bbkosak public:
953506340a66b7814b741b4b4a032ca4b059086f1bbkosak  explicit GtMatcher(const Rhs& rhs)
954506340a66b7814b741b4b4a032ca4b059086f1bbkosak      : ComparisonBase<GtMatcher<Rhs>, Rhs, AnyGt>(rhs) { }
955506340a66b7814b741b4b4a032ca4b059086f1bbkosak  static const char* Desc() { return "is >"; }
956506340a66b7814b741b4b4a032ca4b059086f1bbkosak  static const char* NegatedDesc() { return "isn't >"; }
957506340a66b7814b741b4b4a032ca4b059086f1bbkosak};
958506340a66b7814b741b4b4a032ca4b059086f1bbkosaktemplate <typename Rhs>
959506340a66b7814b741b4b4a032ca4b059086f1bbkosakclass LeMatcher : public ComparisonBase<LeMatcher<Rhs>, Rhs, AnyLe> {
960506340a66b7814b741b4b4a032ca4b059086f1bbkosak public:
961506340a66b7814b741b4b4a032ca4b059086f1bbkosak  explicit LeMatcher(const Rhs& rhs)
962506340a66b7814b741b4b4a032ca4b059086f1bbkosak      : ComparisonBase<LeMatcher<Rhs>, Rhs, AnyLe>(rhs) { }
963506340a66b7814b741b4b4a032ca4b059086f1bbkosak  static const char* Desc() { return "is <="; }
964506340a66b7814b741b4b4a032ca4b059086f1bbkosak  static const char* NegatedDesc() { return "isn't <="; }
965506340a66b7814b741b4b4a032ca4b059086f1bbkosak};
966506340a66b7814b741b4b4a032ca4b059086f1bbkosaktemplate <typename Rhs>
967506340a66b7814b741b4b4a032ca4b059086f1bbkosakclass GeMatcher : public ComparisonBase<GeMatcher<Rhs>, Rhs, AnyGe> {
968506340a66b7814b741b4b4a032ca4b059086f1bbkosak public:
969506340a66b7814b741b4b4a032ca4b059086f1bbkosak  explicit GeMatcher(const Rhs& rhs)
970506340a66b7814b741b4b4a032ca4b059086f1bbkosak      : ComparisonBase<GeMatcher<Rhs>, Rhs, AnyGe>(rhs) { }
971506340a66b7814b741b4b4a032ca4b059086f1bbkosak  static const char* Desc() { return "is >="; }
972506340a66b7814b741b4b4a032ca4b059086f1bbkosak  static const char* NegatedDesc() { return "isn't >="; }
973506340a66b7814b741b4b4a032ca4b059086f1bbkosak};
974e35fdd936d133bf8a48de140a3c666897588a05shiqian
97579b83505bcf73bf2903ebf2e2f82cb1e1f181816vladlosev// Implements the polymorphic IsNull() matcher, which matches any raw or smart
9762d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan// pointer that is NULL.
9772d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wanclass IsNullMatcher {
9782d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan public:
97979b83505bcf73bf2903ebf2e2f82cb1e1f181816vladlosev  template <typename Pointer>
980db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplain(const Pointer& p,
981db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                       MatchResultListener* /* listener */) const {
9826305ff5a922ddf3b428f08ad3b53d07d2c08d89akosak#if GTEST_LANG_CXX11
9836305ff5a922ddf3b428f08ad3b53d07d2c08d89akosak    return p == nullptr;
9846305ff5a922ddf3b428f08ad3b53d07d2c08d89akosak#else  // GTEST_LANG_CXX11
985db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return GetRawPointer(p) == NULL;
9866305ff5a922ddf3b428f08ad3b53d07d2c08d89akosak#endif  // GTEST_LANG_CXX11
987db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  }
9882d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan
9892d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan  void DescribeTo(::std::ostream* os) const { *os << "is NULL"; }
9902d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan  void DescribeNegationTo(::std::ostream* os) const {
991b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    *os << "isn't NULL";
9922d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan  }
9932d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan};
9942d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan
99579b83505bcf73bf2903ebf2e2f82cb1e1f181816vladlosev// Implements the polymorphic NotNull() matcher, which matches any raw or smart
996e35fdd936d133bf8a48de140a3c666897588a05shiqian// pointer that is not NULL.
997e35fdd936d133bf8a48de140a3c666897588a05shiqianclass NotNullMatcher {
998e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
99979b83505bcf73bf2903ebf2e2f82cb1e1f181816vladlosev  template <typename Pointer>
1000db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplain(const Pointer& p,
1001db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                       MatchResultListener* /* listener */) const {
10026305ff5a922ddf3b428f08ad3b53d07d2c08d89akosak#if GTEST_LANG_CXX11
10036305ff5a922ddf3b428f08ad3b53d07d2c08d89akosak    return p != nullptr;
10046305ff5a922ddf3b428f08ad3b53d07d2c08d89akosak#else  // GTEST_LANG_CXX11
1005db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return GetRawPointer(p) != NULL;
10066305ff5a922ddf3b428f08ad3b53d07d2c08d89akosak#endif  // GTEST_LANG_CXX11
1007db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  }
1008e35fdd936d133bf8a48de140a3c666897588a05shiqian
1009b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan  void DescribeTo(::std::ostream* os) const { *os << "isn't NULL"; }
1010e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
1011e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "is NULL";
1012e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1013e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1014e35fdd936d133bf8a48de140a3c666897588a05shiqian
1015e35fdd936d133bf8a48de140a3c666897588a05shiqian// Ref(variable) matches any argument that is a reference to
1016e35fdd936d133bf8a48de140a3c666897588a05shiqian// 'variable'.  This matcher is polymorphic as it can match any
1017e35fdd936d133bf8a48de140a3c666897588a05shiqian// super type of the type of 'variable'.
1018e35fdd936d133bf8a48de140a3c666897588a05shiqian//
1019e35fdd936d133bf8a48de140a3c666897588a05shiqian// The RefMatcher template class implements Ref(variable).  It can
1020e35fdd936d133bf8a48de140a3c666897588a05shiqian// only be instantiated with a reference type.  This prevents a user
1021e35fdd936d133bf8a48de140a3c666897588a05shiqian// from mistakenly using Ref(x) to match a non-reference function
1022e35fdd936d133bf8a48de140a3c666897588a05shiqian// argument.  For example, the following will righteously cause a
1023e35fdd936d133bf8a48de140a3c666897588a05shiqian// compiler error:
1024e35fdd936d133bf8a48de140a3c666897588a05shiqian//
1025e35fdd936d133bf8a48de140a3c666897588a05shiqian//   int n;
1026e35fdd936d133bf8a48de140a3c666897588a05shiqian//   Matcher<int> m1 = Ref(n);   // This won't compile.
1027e35fdd936d133bf8a48de140a3c666897588a05shiqian//   Matcher<int&> m2 = Ref(n);  // This will compile.
1028e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
1029e35fdd936d133bf8a48de140a3c666897588a05shiqianclass RefMatcher;
1030e35fdd936d133bf8a48de140a3c666897588a05shiqian
1031e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
1032e35fdd936d133bf8a48de140a3c666897588a05shiqianclass RefMatcher<T&> {
1033e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Google Mock is a generic framework and thus needs to support
1034e35fdd936d133bf8a48de140a3c666897588a05shiqian  // mocking any function types, including those that take non-const
1035e35fdd936d133bf8a48de140a3c666897588a05shiqian  // reference arguments.  Therefore the template parameter T (and
1036e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Super below) can be instantiated to either a const type or a
1037e35fdd936d133bf8a48de140a3c666897588a05shiqian  // non-const type.
1038e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1039e35fdd936d133bf8a48de140a3c666897588a05shiqian  // RefMatcher() takes a T& instead of const T&, as we want the
1040e35fdd936d133bf8a48de140a3c666897588a05shiqian  // compiler to catch using Ref(const_value) as a matcher for a
1041e35fdd936d133bf8a48de140a3c666897588a05shiqian  // non-const reference.
1042e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit RefMatcher(T& x) : object_(x) {}  // NOLINT
1043e35fdd936d133bf8a48de140a3c666897588a05shiqian
1044e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename Super>
1045e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<Super&>() const {
1046e35fdd936d133bf8a48de140a3c666897588a05shiqian    // By passing object_ (type T&) to Impl(), which expects a Super&,
1047e35fdd936d133bf8a48de140a3c666897588a05shiqian    // we make sure that Super is a super type of T.  In particular,
1048e35fdd936d133bf8a48de140a3c666897588a05shiqian    // this catches using Ref(const_value) as a matcher for a
1049e35fdd936d133bf8a48de140a3c666897588a05shiqian    // non-const reference, as you cannot implicitly convert a const
1050e35fdd936d133bf8a48de140a3c666897588a05shiqian    // reference to a non-const reference.
1051e35fdd936d133bf8a48de140a3c666897588a05shiqian    return MakeMatcher(new Impl<Super>(object_));
1052e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
105332de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1054e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1055e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename Super>
1056e35fdd936d133bf8a48de140a3c666897588a05shiqian  class Impl : public MatcherInterface<Super&> {
1057e35fdd936d133bf8a48de140a3c666897588a05shiqian   public:
1058e35fdd936d133bf8a48de140a3c666897588a05shiqian    explicit Impl(Super& x) : object_(x) {}  // NOLINT
1059e35fdd936d133bf8a48de140a3c666897588a05shiqian
1060db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    // MatchAndExplain() takes a Super& (as opposed to const Super&)
1061db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    // in order to match the interface MatcherInterface<Super&>.
1062821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    virtual bool MatchAndExplain(
1063821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan        Super& x, MatchResultListener* listener) const {
1064b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan      *listener << "which is located @" << static_cast<const void*>(&x);
1065821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      return &x == &object_;
1066821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    }
1067e35fdd936d133bf8a48de140a3c666897588a05shiqian
1068e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeTo(::std::ostream* os) const {
1069e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "references the variable ";
1070e35fdd936d133bf8a48de140a3c666897588a05shiqian      UniversalPrinter<Super&>::Print(object_, os);
1071e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1072e35fdd936d133bf8a48de140a3c666897588a05shiqian
1073e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeNegationTo(::std::ostream* os) const {
1074e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "does not reference the variable ";
1075e35fdd936d133bf8a48de140a3c666897588a05shiqian      UniversalPrinter<Super&>::Print(object_, os);
1076e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1077e35fdd936d133bf8a48de140a3c666897588a05shiqian
1078e35fdd936d133bf8a48de140a3c666897588a05shiqian   private:
1079e35fdd936d133bf8a48de140a3c666897588a05shiqian    const Super& object_;
108032de5f53763125925e078498250f7e73a88de9edzhanyong.wan
108132de5f53763125925e078498250f7e73a88de9edzhanyong.wan    GTEST_DISALLOW_ASSIGN_(Impl);
1082e35fdd936d133bf8a48de140a3c666897588a05shiqian  };
1083e35fdd936d133bf8a48de140a3c666897588a05shiqian
1084e35fdd936d133bf8a48de140a3c666897588a05shiqian  T& object_;
108532de5f53763125925e078498250f7e73a88de9edzhanyong.wan
108632de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(RefMatcher);
1087e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1088e35fdd936d133bf8a48de140a3c666897588a05shiqian
1089e35fdd936d133bf8a48de140a3c666897588a05shiqian// Polymorphic helper functions for narrow and wide string matchers.
1090e35fdd936d133bf8a48de140a3c666897588a05shiqianinline bool CaseInsensitiveCStringEquals(const char* lhs, const char* rhs) {
1091e35fdd936d133bf8a48de140a3c666897588a05shiqian  return String::CaseInsensitiveCStringEquals(lhs, rhs);
1092e35fdd936d133bf8a48de140a3c666897588a05shiqian}
1093e35fdd936d133bf8a48de140a3c666897588a05shiqian
1094e35fdd936d133bf8a48de140a3c666897588a05shiqianinline bool CaseInsensitiveCStringEquals(const wchar_t* lhs,
1095e35fdd936d133bf8a48de140a3c666897588a05shiqian                                         const wchar_t* rhs) {
1096e35fdd936d133bf8a48de140a3c666897588a05shiqian  return String::CaseInsensitiveWideCStringEquals(lhs, rhs);
1097e35fdd936d133bf8a48de140a3c666897588a05shiqian}
1098e35fdd936d133bf8a48de140a3c666897588a05shiqian
1099e35fdd936d133bf8a48de140a3c666897588a05shiqian// String comparison for narrow or wide strings that can have embedded NUL
1100e35fdd936d133bf8a48de140a3c666897588a05shiqian// characters.
1101e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType>
1102e35fdd936d133bf8a48de140a3c666897588a05shiqianbool CaseInsensitiveStringEquals(const StringType& s1,
1103e35fdd936d133bf8a48de140a3c666897588a05shiqian                                 const StringType& s2) {
1104e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Are the heads equal?
1105e35fdd936d133bf8a48de140a3c666897588a05shiqian  if (!CaseInsensitiveCStringEquals(s1.c_str(), s2.c_str())) {
1106e35fdd936d133bf8a48de140a3c666897588a05shiqian    return false;
1107e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1108e35fdd936d133bf8a48de140a3c666897588a05shiqian
1109e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Skip the equal heads.
1110e35fdd936d133bf8a48de140a3c666897588a05shiqian  const typename StringType::value_type nul = 0;
1111e35fdd936d133bf8a48de140a3c666897588a05shiqian  const size_t i1 = s1.find(nul), i2 = s2.find(nul);
1112e35fdd936d133bf8a48de140a3c666897588a05shiqian
1113e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Are we at the end of either s1 or s2?
1114e35fdd936d133bf8a48de140a3c666897588a05shiqian  if (i1 == StringType::npos || i2 == StringType::npos) {
1115e35fdd936d133bf8a48de140a3c666897588a05shiqian    return i1 == i2;
1116e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1117e35fdd936d133bf8a48de140a3c666897588a05shiqian
1118e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Are the tails equal?
1119e35fdd936d133bf8a48de140a3c666897588a05shiqian  return CaseInsensitiveStringEquals(s1.substr(i1 + 1), s2.substr(i2 + 1));
1120e35fdd936d133bf8a48de140a3c666897588a05shiqian}
1121e35fdd936d133bf8a48de140a3c666897588a05shiqian
1122e35fdd936d133bf8a48de140a3c666897588a05shiqian// String matchers.
1123e35fdd936d133bf8a48de140a3c666897588a05shiqian
1124e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements equality-based string matchers like StrEq, StrCaseNe, and etc.
1125e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType>
1126e35fdd936d133bf8a48de140a3c666897588a05shiqianclass StrEqualityMatcher {
1127e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1128e35fdd936d133bf8a48de140a3c666897588a05shiqian  StrEqualityMatcher(const StringType& str, bool expect_eq,
1129e35fdd936d133bf8a48de140a3c666897588a05shiqian                     bool case_sensitive)
1130e35fdd936d133bf8a48de140a3c666897588a05shiqian      : string_(str), expect_eq_(expect_eq), case_sensitive_(case_sensitive) {}
1131e35fdd936d133bf8a48de140a3c666897588a05shiqian
113238513a8bb154f0b6d0a4088814fe92552696d465jgm  // Accepts pointer types, particularly:
113338513a8bb154f0b6d0a4088814fe92552696d465jgm  //   const char*
113438513a8bb154f0b6d0a4088814fe92552696d465jgm  //   char*
113538513a8bb154f0b6d0a4088814fe92552696d465jgm  //   const wchar_t*
113638513a8bb154f0b6d0a4088814fe92552696d465jgm  //   wchar_t*
113738513a8bb154f0b6d0a4088814fe92552696d465jgm  template <typename CharType>
113838513a8bb154f0b6d0a4088814fe92552696d465jgm  bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {
1139e35fdd936d133bf8a48de140a3c666897588a05shiqian    if (s == NULL) {
1140e35fdd936d133bf8a48de140a3c666897588a05shiqian      return !expect_eq_;
1141e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1142db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return MatchAndExplain(StringType(s), listener);
1143e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1144e35fdd936d133bf8a48de140a3c666897588a05shiqian
114538513a8bb154f0b6d0a4088814fe92552696d465jgm  // Matches anything that can convert to StringType.
114638513a8bb154f0b6d0a4088814fe92552696d465jgm  //
114738513a8bb154f0b6d0a4088814fe92552696d465jgm  // This is a template, not just a plain function with const StringType&,
114838513a8bb154f0b6d0a4088814fe92552696d465jgm  // because StringPiece has some interfering non-explicit constructors.
114938513a8bb154f0b6d0a4088814fe92552696d465jgm  template <typename MatcheeStringType>
115038513a8bb154f0b6d0a4088814fe92552696d465jgm  bool MatchAndExplain(const MatcheeStringType& s,
1151db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                       MatchResultListener* /* listener */) const {
115238513a8bb154f0b6d0a4088814fe92552696d465jgm    const StringType& s2(s);
115338513a8bb154f0b6d0a4088814fe92552696d465jgm    const bool eq = case_sensitive_ ? s2 == string_ :
115438513a8bb154f0b6d0a4088814fe92552696d465jgm        CaseInsensitiveStringEquals(s2, string_);
1155e35fdd936d133bf8a48de140a3c666897588a05shiqian    return expect_eq_ == eq;
1156e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1157e35fdd936d133bf8a48de140a3c666897588a05shiqian
1158e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
1159e35fdd936d133bf8a48de140a3c666897588a05shiqian    DescribeToHelper(expect_eq_, os);
1160e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1161e35fdd936d133bf8a48de140a3c666897588a05shiqian
1162e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
1163e35fdd936d133bf8a48de140a3c666897588a05shiqian    DescribeToHelper(!expect_eq_, os);
1164e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
116532de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1166e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1167e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeToHelper(bool expect_eq, ::std::ostream* os) const {
1168b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    *os << (expect_eq ? "is " : "isn't ");
1169e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "equal to ";
1170e35fdd936d133bf8a48de140a3c666897588a05shiqian    if (!case_sensitive_) {
1171e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "(ignoring case) ";
1172e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1173e2e8ba401d198d1a8304c652e997505609b62696vladlosev    UniversalPrint(string_, os);
1174e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1175e35fdd936d133bf8a48de140a3c666897588a05shiqian
1176e35fdd936d133bf8a48de140a3c666897588a05shiqian  const StringType string_;
1177e35fdd936d133bf8a48de140a3c666897588a05shiqian  const bool expect_eq_;
1178e35fdd936d133bf8a48de140a3c666897588a05shiqian  const bool case_sensitive_;
117932de5f53763125925e078498250f7e73a88de9edzhanyong.wan
118032de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(StrEqualityMatcher);
1181e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1182e35fdd936d133bf8a48de140a3c666897588a05shiqian
1183e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the polymorphic HasSubstr(substring) matcher, which
1184e35fdd936d133bf8a48de140a3c666897588a05shiqian// can be used as a Matcher<T> as long as T can be converted to a
1185e35fdd936d133bf8a48de140a3c666897588a05shiqian// string.
1186e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType>
1187e35fdd936d133bf8a48de140a3c666897588a05shiqianclass HasSubstrMatcher {
1188e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1189e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit HasSubstrMatcher(const StringType& substring)
1190e35fdd936d133bf8a48de140a3c666897588a05shiqian      : substring_(substring) {}
1191e35fdd936d133bf8a48de140a3c666897588a05shiqian
119238513a8bb154f0b6d0a4088814fe92552696d465jgm  // Accepts pointer types, particularly:
119338513a8bb154f0b6d0a4088814fe92552696d465jgm  //   const char*
119438513a8bb154f0b6d0a4088814fe92552696d465jgm  //   char*
119538513a8bb154f0b6d0a4088814fe92552696d465jgm  //   const wchar_t*
119638513a8bb154f0b6d0a4088814fe92552696d465jgm  //   wchar_t*
119738513a8bb154f0b6d0a4088814fe92552696d465jgm  template <typename CharType>
119838513a8bb154f0b6d0a4088814fe92552696d465jgm  bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {
1199db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return s != NULL && MatchAndExplain(StringType(s), listener);
1200e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1201e35fdd936d133bf8a48de140a3c666897588a05shiqian
120238513a8bb154f0b6d0a4088814fe92552696d465jgm  // Matches anything that can convert to StringType.
120338513a8bb154f0b6d0a4088814fe92552696d465jgm  //
120438513a8bb154f0b6d0a4088814fe92552696d465jgm  // This is a template, not just a plain function with const StringType&,
120538513a8bb154f0b6d0a4088814fe92552696d465jgm  // because StringPiece has some interfering non-explicit constructors.
120638513a8bb154f0b6d0a4088814fe92552696d465jgm  template <typename MatcheeStringType>
120738513a8bb154f0b6d0a4088814fe92552696d465jgm  bool MatchAndExplain(const MatcheeStringType& s,
1208db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                       MatchResultListener* /* listener */) const {
120938513a8bb154f0b6d0a4088814fe92552696d465jgm    const StringType& s2(s);
121038513a8bb154f0b6d0a4088814fe92552696d465jgm    return s2.find(substring_) != StringType::npos;
1211e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1212e35fdd936d133bf8a48de140a3c666897588a05shiqian
1213e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Describes what this matcher matches.
1214e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
1215e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "has substring ";
1216e2e8ba401d198d1a8304c652e997505609b62696vladlosev    UniversalPrint(substring_, os);
1217e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1218e35fdd936d133bf8a48de140a3c666897588a05shiqian
1219e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
1220e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "has no substring ";
1221e2e8ba401d198d1a8304c652e997505609b62696vladlosev    UniversalPrint(substring_, os);
1222e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
122332de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1224e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1225e35fdd936d133bf8a48de140a3c666897588a05shiqian  const StringType substring_;
122632de5f53763125925e078498250f7e73a88de9edzhanyong.wan
122732de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(HasSubstrMatcher);
1228e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1229e35fdd936d133bf8a48de140a3c666897588a05shiqian
1230e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the polymorphic StartsWith(substring) matcher, which
1231e35fdd936d133bf8a48de140a3c666897588a05shiqian// can be used as a Matcher<T> as long as T can be converted to a
1232e35fdd936d133bf8a48de140a3c666897588a05shiqian// string.
1233e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType>
1234e35fdd936d133bf8a48de140a3c666897588a05shiqianclass StartsWithMatcher {
1235e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1236e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit StartsWithMatcher(const StringType& prefix) : prefix_(prefix) {
1237e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1238e35fdd936d133bf8a48de140a3c666897588a05shiqian
123938513a8bb154f0b6d0a4088814fe92552696d465jgm  // Accepts pointer types, particularly:
124038513a8bb154f0b6d0a4088814fe92552696d465jgm  //   const char*
124138513a8bb154f0b6d0a4088814fe92552696d465jgm  //   char*
124238513a8bb154f0b6d0a4088814fe92552696d465jgm  //   const wchar_t*
124338513a8bb154f0b6d0a4088814fe92552696d465jgm  //   wchar_t*
124438513a8bb154f0b6d0a4088814fe92552696d465jgm  template <typename CharType>
124538513a8bb154f0b6d0a4088814fe92552696d465jgm  bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {
1246db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return s != NULL && MatchAndExplain(StringType(s), listener);
1247e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1248e35fdd936d133bf8a48de140a3c666897588a05shiqian
124938513a8bb154f0b6d0a4088814fe92552696d465jgm  // Matches anything that can convert to StringType.
125038513a8bb154f0b6d0a4088814fe92552696d465jgm  //
125138513a8bb154f0b6d0a4088814fe92552696d465jgm  // This is a template, not just a plain function with const StringType&,
125238513a8bb154f0b6d0a4088814fe92552696d465jgm  // because StringPiece has some interfering non-explicit constructors.
125338513a8bb154f0b6d0a4088814fe92552696d465jgm  template <typename MatcheeStringType>
125438513a8bb154f0b6d0a4088814fe92552696d465jgm  bool MatchAndExplain(const MatcheeStringType& s,
1255db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                       MatchResultListener* /* listener */) const {
125638513a8bb154f0b6d0a4088814fe92552696d465jgm    const StringType& s2(s);
125738513a8bb154f0b6d0a4088814fe92552696d465jgm    return s2.length() >= prefix_.length() &&
125838513a8bb154f0b6d0a4088814fe92552696d465jgm        s2.substr(0, prefix_.length()) == prefix_;
1259e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1260e35fdd936d133bf8a48de140a3c666897588a05shiqian
1261e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
1262e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "starts with ";
1263e2e8ba401d198d1a8304c652e997505609b62696vladlosev    UniversalPrint(prefix_, os);
1264e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1265e35fdd936d133bf8a48de140a3c666897588a05shiqian
1266e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
1267e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "doesn't start with ";
1268e2e8ba401d198d1a8304c652e997505609b62696vladlosev    UniversalPrint(prefix_, os);
1269e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
127032de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1271e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1272e35fdd936d133bf8a48de140a3c666897588a05shiqian  const StringType prefix_;
127332de5f53763125925e078498250f7e73a88de9edzhanyong.wan
127432de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(StartsWithMatcher);
1275e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1276e35fdd936d133bf8a48de140a3c666897588a05shiqian
1277e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the polymorphic EndsWith(substring) matcher, which
1278e35fdd936d133bf8a48de140a3c666897588a05shiqian// can be used as a Matcher<T> as long as T can be converted to a
1279e35fdd936d133bf8a48de140a3c666897588a05shiqian// string.
1280e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType>
1281e35fdd936d133bf8a48de140a3c666897588a05shiqianclass EndsWithMatcher {
1282e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1283e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit EndsWithMatcher(const StringType& suffix) : suffix_(suffix) {}
1284e35fdd936d133bf8a48de140a3c666897588a05shiqian
128538513a8bb154f0b6d0a4088814fe92552696d465jgm  // Accepts pointer types, particularly:
128638513a8bb154f0b6d0a4088814fe92552696d465jgm  //   const char*
128738513a8bb154f0b6d0a4088814fe92552696d465jgm  //   char*
128838513a8bb154f0b6d0a4088814fe92552696d465jgm  //   const wchar_t*
128938513a8bb154f0b6d0a4088814fe92552696d465jgm  //   wchar_t*
129038513a8bb154f0b6d0a4088814fe92552696d465jgm  template <typename CharType>
129138513a8bb154f0b6d0a4088814fe92552696d465jgm  bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {
1292db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return s != NULL && MatchAndExplain(StringType(s), listener);
1293e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1294e35fdd936d133bf8a48de140a3c666897588a05shiqian
129538513a8bb154f0b6d0a4088814fe92552696d465jgm  // Matches anything that can convert to StringType.
129638513a8bb154f0b6d0a4088814fe92552696d465jgm  //
129738513a8bb154f0b6d0a4088814fe92552696d465jgm  // This is a template, not just a plain function with const StringType&,
129838513a8bb154f0b6d0a4088814fe92552696d465jgm  // because StringPiece has some interfering non-explicit constructors.
129938513a8bb154f0b6d0a4088814fe92552696d465jgm  template <typename MatcheeStringType>
130038513a8bb154f0b6d0a4088814fe92552696d465jgm  bool MatchAndExplain(const MatcheeStringType& s,
1301db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                       MatchResultListener* /* listener */) const {
130238513a8bb154f0b6d0a4088814fe92552696d465jgm    const StringType& s2(s);
130338513a8bb154f0b6d0a4088814fe92552696d465jgm    return s2.length() >= suffix_.length() &&
130438513a8bb154f0b6d0a4088814fe92552696d465jgm        s2.substr(s2.length() - suffix_.length()) == suffix_;
1305e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1306e35fdd936d133bf8a48de140a3c666897588a05shiqian
1307e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
1308e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "ends with ";
1309e2e8ba401d198d1a8304c652e997505609b62696vladlosev    UniversalPrint(suffix_, os);
1310e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1311e35fdd936d133bf8a48de140a3c666897588a05shiqian
1312e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
1313e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "doesn't end with ";
1314e2e8ba401d198d1a8304c652e997505609b62696vladlosev    UniversalPrint(suffix_, os);
1315e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
131632de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1317e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1318e35fdd936d133bf8a48de140a3c666897588a05shiqian  const StringType suffix_;
131932de5f53763125925e078498250f7e73a88de9edzhanyong.wan
132032de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(EndsWithMatcher);
1321e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1322e35fdd936d133bf8a48de140a3c666897588a05shiqian
1323e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements polymorphic matchers MatchesRegex(regex) and
1324e35fdd936d133bf8a48de140a3c666897588a05shiqian// ContainsRegex(regex), which can be used as a Matcher<T> as long as
1325e35fdd936d133bf8a48de140a3c666897588a05shiqian// T can be converted to a string.
1326e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatchesRegexMatcher {
1327e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1328e35fdd936d133bf8a48de140a3c666897588a05shiqian  MatchesRegexMatcher(const RE* regex, bool full_match)
1329e35fdd936d133bf8a48de140a3c666897588a05shiqian      : regex_(regex), full_match_(full_match) {}
1330e35fdd936d133bf8a48de140a3c666897588a05shiqian
133138513a8bb154f0b6d0a4088814fe92552696d465jgm  // Accepts pointer types, particularly:
133238513a8bb154f0b6d0a4088814fe92552696d465jgm  //   const char*
133338513a8bb154f0b6d0a4088814fe92552696d465jgm  //   char*
133438513a8bb154f0b6d0a4088814fe92552696d465jgm  //   const wchar_t*
133538513a8bb154f0b6d0a4088814fe92552696d465jgm  //   wchar_t*
133638513a8bb154f0b6d0a4088814fe92552696d465jgm  template <typename CharType>
133738513a8bb154f0b6d0a4088814fe92552696d465jgm  bool MatchAndExplain(CharType* s, MatchResultListener* listener) const {
1338db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return s != NULL && MatchAndExplain(internal::string(s), listener);
1339e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1340e35fdd936d133bf8a48de140a3c666897588a05shiqian
134138513a8bb154f0b6d0a4088814fe92552696d465jgm  // Matches anything that can convert to internal::string.
134238513a8bb154f0b6d0a4088814fe92552696d465jgm  //
134338513a8bb154f0b6d0a4088814fe92552696d465jgm  // This is a template, not just a plain function with const internal::string&,
134438513a8bb154f0b6d0a4088814fe92552696d465jgm  // because StringPiece has some interfering non-explicit constructors.
134538513a8bb154f0b6d0a4088814fe92552696d465jgm  template <class MatcheeStringType>
134638513a8bb154f0b6d0a4088814fe92552696d465jgm  bool MatchAndExplain(const MatcheeStringType& s,
1347db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                       MatchResultListener* /* listener */) const {
134838513a8bb154f0b6d0a4088814fe92552696d465jgm    const internal::string& s2(s);
134938513a8bb154f0b6d0a4088814fe92552696d465jgm    return full_match_ ? RE::FullMatch(s2, *regex_) :
135038513a8bb154f0b6d0a4088814fe92552696d465jgm        RE::PartialMatch(s2, *regex_);
1351e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1352e35fdd936d133bf8a48de140a3c666897588a05shiqian
1353e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
1354e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << (full_match_ ? "matches" : "contains")
1355e35fdd936d133bf8a48de140a3c666897588a05shiqian        << " regular expression ";
1356e35fdd936d133bf8a48de140a3c666897588a05shiqian    UniversalPrinter<internal::string>::Print(regex_->pattern(), os);
1357e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1358e35fdd936d133bf8a48de140a3c666897588a05shiqian
1359e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
1360e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "doesn't " << (full_match_ ? "match" : "contain")
1361e35fdd936d133bf8a48de140a3c666897588a05shiqian        << " regular expression ";
1362e35fdd936d133bf8a48de140a3c666897588a05shiqian    UniversalPrinter<internal::string>::Print(regex_->pattern(), os);
1363e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
136432de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1365e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1366e35fdd936d133bf8a48de140a3c666897588a05shiqian  const internal::linked_ptr<const RE> regex_;
1367e35fdd936d133bf8a48de140a3c666897588a05shiqian  const bool full_match_;
136832de5f53763125925e078498250f7e73a88de9edzhanyong.wan
136932de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(MatchesRegexMatcher);
1370e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1371e35fdd936d133bf8a48de140a3c666897588a05shiqian
1372e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements a matcher that compares the two fields of a 2-tuple
1373e35fdd936d133bf8a48de140a3c666897588a05shiqian// using one of the ==, <=, <, etc, operators.  The two fields being
1374e35fdd936d133bf8a48de140a3c666897588a05shiqian// compared don't have to have the same type.
1375e35fdd936d133bf8a48de140a3c666897588a05shiqian//
1376e35fdd936d133bf8a48de140a3c666897588a05shiqian// The matcher defined here is polymorphic (for example, Eq() can be
1377e35fdd936d133bf8a48de140a3c666897588a05shiqian// used to match a tuple<int, short>, a tuple<const long&, double>,
1378e35fdd936d133bf8a48de140a3c666897588a05shiqian// etc).  Therefore we use a template type conversion operator in the
1379e35fdd936d133bf8a48de140a3c666897588a05shiqian// implementation.
1380506340a66b7814b741b4b4a032ca4b059086f1bbkosaktemplate <typename D, typename Op>
1381506340a66b7814b741b4b4a032ca4b059086f1bbkosakclass PairMatchBase {
1382506340a66b7814b741b4b4a032ca4b059086f1bbkosak public:
1383506340a66b7814b741b4b4a032ca4b059086f1bbkosak  template <typename T1, typename T2>
1384506340a66b7814b741b4b4a032ca4b059086f1bbkosak  operator Matcher< ::testing::tuple<T1, T2> >() const {
1385506340a66b7814b741b4b4a032ca4b059086f1bbkosak    return MakeMatcher(new Impl< ::testing::tuple<T1, T2> >);
1386506340a66b7814b741b4b4a032ca4b059086f1bbkosak  }
1387506340a66b7814b741b4b4a032ca4b059086f1bbkosak  template <typename T1, typename T2>
1388506340a66b7814b741b4b4a032ca4b059086f1bbkosak  operator Matcher<const ::testing::tuple<T1, T2>&>() const {
1389506340a66b7814b741b4b4a032ca4b059086f1bbkosak    return MakeMatcher(new Impl<const ::testing::tuple<T1, T2>&>);
1390506340a66b7814b741b4b4a032ca4b059086f1bbkosak  }
1391506340a66b7814b741b4b4a032ca4b059086f1bbkosak
1392506340a66b7814b741b4b4a032ca4b059086f1bbkosak private:
1393506340a66b7814b741b4b4a032ca4b059086f1bbkosak  static ::std::ostream& GetDesc(::std::ostream& os) {  // NOLINT
1394506340a66b7814b741b4b4a032ca4b059086f1bbkosak    return os << D::Desc();
1395e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1396e35fdd936d133bf8a48de140a3c666897588a05shiqian
1397506340a66b7814b741b4b4a032ca4b059086f1bbkosak  template <typename Tuple>
1398506340a66b7814b741b4b4a032ca4b059086f1bbkosak  class Impl : public MatcherInterface<Tuple> {
1399506340a66b7814b741b4b4a032ca4b059086f1bbkosak   public:
1400506340a66b7814b741b4b4a032ca4b059086f1bbkosak    virtual bool MatchAndExplain(
1401506340a66b7814b741b4b4a032ca4b059086f1bbkosak        Tuple args,
1402506340a66b7814b741b4b4a032ca4b059086f1bbkosak        MatchResultListener* /* listener */) const {
1403506340a66b7814b741b4b4a032ca4b059086f1bbkosak      return Op()(::testing::get<0>(args), ::testing::get<1>(args));
1404506340a66b7814b741b4b4a032ca4b059086f1bbkosak    }
1405506340a66b7814b741b4b4a032ca4b059086f1bbkosak    virtual void DescribeTo(::std::ostream* os) const {
1406506340a66b7814b741b4b4a032ca4b059086f1bbkosak      *os << "are " << GetDesc;
1407506340a66b7814b741b4b4a032ca4b059086f1bbkosak    }
1408506340a66b7814b741b4b4a032ca4b059086f1bbkosak    virtual void DescribeNegationTo(::std::ostream* os) const {
1409506340a66b7814b741b4b4a032ca4b059086f1bbkosak      *os << "aren't " << GetDesc;
1410506340a66b7814b741b4b4a032ca4b059086f1bbkosak    }
1411506340a66b7814b741b4b4a032ca4b059086f1bbkosak  };
1412506340a66b7814b741b4b4a032ca4b059086f1bbkosak};
1413506340a66b7814b741b4b4a032ca4b059086f1bbkosak
1414506340a66b7814b741b4b4a032ca4b059086f1bbkosakclass Eq2Matcher : public PairMatchBase<Eq2Matcher, AnyEq> {
1415506340a66b7814b741b4b4a032ca4b059086f1bbkosak public:
1416506340a66b7814b741b4b4a032ca4b059086f1bbkosak  static const char* Desc() { return "an equal pair"; }
1417506340a66b7814b741b4b4a032ca4b059086f1bbkosak};
1418506340a66b7814b741b4b4a032ca4b059086f1bbkosakclass Ne2Matcher : public PairMatchBase<Ne2Matcher, AnyNe> {
1419506340a66b7814b741b4b4a032ca4b059086f1bbkosak public:
1420506340a66b7814b741b4b4a032ca4b059086f1bbkosak  static const char* Desc() { return "an unequal pair"; }
1421506340a66b7814b741b4b4a032ca4b059086f1bbkosak};
1422506340a66b7814b741b4b4a032ca4b059086f1bbkosakclass Lt2Matcher : public PairMatchBase<Lt2Matcher, AnyLt> {
1423506340a66b7814b741b4b4a032ca4b059086f1bbkosak public:
1424506340a66b7814b741b4b4a032ca4b059086f1bbkosak  static const char* Desc() { return "a pair where the first < the second"; }
1425506340a66b7814b741b4b4a032ca4b059086f1bbkosak};
1426506340a66b7814b741b4b4a032ca4b059086f1bbkosakclass Gt2Matcher : public PairMatchBase<Gt2Matcher, AnyGt> {
1427506340a66b7814b741b4b4a032ca4b059086f1bbkosak public:
1428506340a66b7814b741b4b4a032ca4b059086f1bbkosak  static const char* Desc() { return "a pair where the first > the second"; }
1429506340a66b7814b741b4b4a032ca4b059086f1bbkosak};
1430506340a66b7814b741b4b4a032ca4b059086f1bbkosakclass Le2Matcher : public PairMatchBase<Le2Matcher, AnyLe> {
1431506340a66b7814b741b4b4a032ca4b059086f1bbkosak public:
1432506340a66b7814b741b4b4a032ca4b059086f1bbkosak  static const char* Desc() { return "a pair where the first <= the second"; }
1433506340a66b7814b741b4b4a032ca4b059086f1bbkosak};
1434506340a66b7814b741b4b4a032ca4b059086f1bbkosakclass Ge2Matcher : public PairMatchBase<Ge2Matcher, AnyGe> {
1435506340a66b7814b741b4b4a032ca4b059086f1bbkosak public:
1436506340a66b7814b741b4b4a032ca4b059086f1bbkosak  static const char* Desc() { return "a pair where the first >= the second"; }
1437506340a66b7814b741b4b4a032ca4b059086f1bbkosak};
1438e35fdd936d133bf8a48de140a3c666897588a05shiqian
1439c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// Implements the Not(...) matcher for a particular argument type T.
1440c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// We do not nest it inside the NotMatcher class template, as that
1441c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// will prevent different instantiations of NotMatcher from sharing
1442c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// the same NotMatcherImpl<T> class.
1443c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wantemplate <typename T>
1444c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wanclass NotMatcherImpl : public MatcherInterface<T> {
1445c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan public:
1446c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  explicit NotMatcherImpl(const Matcher<T>& matcher)
1447c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      : matcher_(matcher) {}
1448c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1449821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
1450821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    return !matcher_.MatchAndExplain(x, listener);
1451c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1452c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1453c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
1454c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    matcher_.DescribeNegationTo(os);
1455c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1456c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1457c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
1458c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    matcher_.DescribeTo(os);
1459c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1460c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1461c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan private:
1462c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  const Matcher<T> matcher_;
146332de5f53763125925e078498250f7e73a88de9edzhanyong.wan
146432de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(NotMatcherImpl);
1465c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan};
1466c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1467e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the Not(m) matcher, which matches a value that doesn't
1468e35fdd936d133bf8a48de140a3c666897588a05shiqian// match matcher m.
1469e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename InnerMatcher>
1470e35fdd936d133bf8a48de140a3c666897588a05shiqianclass NotMatcher {
1471e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1472e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit NotMatcher(InnerMatcher matcher) : matcher_(matcher) {}
1473e35fdd936d133bf8a48de140a3c666897588a05shiqian
1474e35fdd936d133bf8a48de140a3c666897588a05shiqian  // This template type conversion operator allows Not(m) to be used
1475e35fdd936d133bf8a48de140a3c666897588a05shiqian  // to match any type m can match.
1476e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1477e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<T>() const {
1478c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    return Matcher<T>(new NotMatcherImpl<T>(SafeMatcherCast<T>(matcher_)));
1479e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
148032de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1481e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1482c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  InnerMatcher matcher_;
148332de5f53763125925e078498250f7e73a88de9edzhanyong.wan
148432de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(NotMatcher);
1485c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan};
1486e35fdd936d133bf8a48de140a3c666897588a05shiqian
1487c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// Implements the AllOf(m1, m2) matcher for a particular argument type
1488c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// T. We do not nest it inside the BothOfMatcher class template, as
1489c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// that will prevent different instantiations of BothOfMatcher from
1490c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// sharing the same BothOfMatcherImpl<T> class.
1491c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wantemplate <typename T>
1492c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wanclass BothOfMatcherImpl : public MatcherInterface<T> {
1493c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan public:
1494c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  BothOfMatcherImpl(const Matcher<T>& matcher1, const Matcher<T>& matcher2)
1495c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      : matcher1_(matcher1), matcher2_(matcher2) {}
1496e35fdd936d133bf8a48de140a3c666897588a05shiqian
1497c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
1498c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    *os << "(";
1499c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    matcher1_.DescribeTo(os);
1500c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    *os << ") and (";
1501c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    matcher2_.DescribeTo(os);
1502c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    *os << ")";
1503c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1504e35fdd936d133bf8a48de140a3c666897588a05shiqian
1505c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
1506b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    *os << "(";
1507b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    matcher1_.DescribeNegationTo(os);
1508b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    *os << ") or (";
1509b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    matcher2_.DescribeNegationTo(os);
1510b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    *os << ")";
1511c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1512e35fdd936d133bf8a48de140a3c666897588a05shiqian
1513821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
1514821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // If either matcher1_ or matcher2_ doesn't match x, we only need
1515821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // to explain why one of them fails.
1516821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    StringMatchResultListener listener1;
1517821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (!matcher1_.MatchAndExplain(x, &listener1)) {
1518821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      *listener << listener1.str();
1519821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      return false;
1520821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    }
1521c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1522821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    StringMatchResultListener listener2;
1523821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (!matcher2_.MatchAndExplain(x, &listener2)) {
1524821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      *listener << listener2.str();
1525821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      return false;
1526821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    }
1527c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1528821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // Otherwise we need to explain why *both* of them match.
1529821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    const internal::string s1 = listener1.str();
1530821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    const internal::string s2 = listener2.str();
1531821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
1532821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (s1 == "") {
1533821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      *listener << s2;
1534c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    } else {
1535821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      *listener << s1;
1536821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      if (s2 != "") {
1537b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan        *listener << ", and " << s2;
1538c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      }
1539c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    }
1540821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    return true;
1541c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
154232de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1543c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan private:
1544c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  const Matcher<T> matcher1_;
1545c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  const Matcher<T> matcher2_;
154632de5f53763125925e078498250f7e73a88de9edzhanyong.wan
154732de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(BothOfMatcherImpl);
1548e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1549e35fdd936d133bf8a48de140a3c666897588a05shiqian
1550616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan#if GTEST_LANG_CXX11
1551616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// MatcherList provides mechanisms for storing a variable number of matchers in
1552616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// a list structure (ListType) and creating a combining matcher from such a
1553616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// list.
1554616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// The template is defined recursively using the following template paramters:
1555616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan//   * kSize is the length of the MatcherList.
1556616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan//   * Head is the type of the first matcher of the list.
1557616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan//   * Tail denotes the types of the remaining matchers of the list.
1558616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wantemplate <int kSize, typename Head, typename... Tail>
1559616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wanstruct MatcherList {
1560616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  typedef MatcherList<kSize - 1, Tail...> MatcherListTail;
15612989703ed85154ae7ef90e0d754590116ecb565dzhanyong.wan  typedef ::std::pair<Head, typename MatcherListTail::ListType> ListType;
1562616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1563616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // BuildList stores variadic type values in a nested pair structure.
1564616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // Example:
1565616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // MatcherList<3, int, string, float>::BuildList(5, "foo", 2.0) will return
1566616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // the corresponding result of type pair<int, pair<string, float>>.
1567616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  static ListType BuildList(const Head& matcher, const Tail&... tail) {
1568616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    return ListType(matcher, MatcherListTail::BuildList(tail...));
1569616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  }
1570616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1571616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // CreateMatcher<T> creates a Matcher<T> from a given list of matchers (built
1572616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // by BuildList()). CombiningMatcher<T> is used to combine the matchers of the
1573616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // list. CombiningMatcher<T> must implement MatcherInterface<T> and have a
1574616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // constructor taking two Matcher<T>s as input.
1575616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  template <typename T, template <typename /* T */> class CombiningMatcher>
1576616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  static Matcher<T> CreateMatcher(const ListType& matchers) {
1577616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    return Matcher<T>(new CombiningMatcher<T>(
1578616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        SafeMatcherCast<T>(matchers.first),
1579616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        MatcherListTail::template CreateMatcher<T, CombiningMatcher>(
1580616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan            matchers.second)));
1581616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  }
1582616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan};
1583616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1584616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// The following defines the base case for the recursive definition of
1585616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// MatcherList.
1586616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wantemplate <typename Matcher1, typename Matcher2>
1587616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wanstruct MatcherList<2, Matcher1, Matcher2> {
15882989703ed85154ae7ef90e0d754590116ecb565dzhanyong.wan  typedef ::std::pair<Matcher1, Matcher2> ListType;
1589616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1590616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  static ListType BuildList(const Matcher1& matcher1,
1591616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan                            const Matcher2& matcher2) {
15922989703ed85154ae7ef90e0d754590116ecb565dzhanyong.wan    return ::std::pair<Matcher1, Matcher2>(matcher1, matcher2);
1593616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  }
1594616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1595616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  template <typename T, template <typename /* T */> class CombiningMatcher>
1596616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  static Matcher<T> CreateMatcher(const ListType& matchers) {
1597616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    return Matcher<T>(new CombiningMatcher<T>(
1598616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        SafeMatcherCast<T>(matchers.first),
1599616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        SafeMatcherCast<T>(matchers.second)));
1600616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  }
1601616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan};
1602616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1603616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// VariadicMatcher is used for the variadic implementation of
1604616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// AllOf(m_1, m_2, ...) and AnyOf(m_1, m_2, ...).
1605616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// CombiningMatcher<T> is used to recursively combine the provided matchers
1606616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// (of type Args...).
1607616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wantemplate <template <typename T> class CombiningMatcher, typename... Args>
1608616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wanclass VariadicMatcher {
1609616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan public:
1610616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  VariadicMatcher(const Args&... matchers)  // NOLINT
1611616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan      : matchers_(MatcherListType::BuildList(matchers...)) {}
1612616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1613616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // This template type conversion operator allows an
1614616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // VariadicMatcher<Matcher1, Matcher2...> object to match any type that
1615616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // all of the provided matchers (Matcher1, Matcher2, ...) can match.
1616616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  template <typename T>
1617616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  operator Matcher<T>() const {
1618616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    return MatcherListType::template CreateMatcher<T, CombiningMatcher>(
1619616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        matchers_);
1620616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  }
1621616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1622616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan private:
1623616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  typedef MatcherList<sizeof...(Args), Args...> MatcherListType;
1624616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1625616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  const typename MatcherListType::ListType matchers_;
1626616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1627616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  GTEST_DISALLOW_ASSIGN_(VariadicMatcher);
1628616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan};
1629616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1630616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wantemplate <typename... Args>
1631616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wanusing AllOfMatcher = VariadicMatcher<BothOfMatcherImpl, Args...>;
1632616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1633616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan#endif  // GTEST_LANG_CXX11
1634616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1635e35fdd936d133bf8a48de140a3c666897588a05shiqian// Used for implementing the AllOf(m_1, ..., m_n) matcher, which
1636e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a value that matches all of the matchers m_1, ..., and m_n.
1637e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2>
1638e35fdd936d133bf8a48de140a3c666897588a05shiqianclass BothOfMatcher {
1639e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1640e35fdd936d133bf8a48de140a3c666897588a05shiqian  BothOfMatcher(Matcher1 matcher1, Matcher2 matcher2)
1641e35fdd936d133bf8a48de140a3c666897588a05shiqian      : matcher1_(matcher1), matcher2_(matcher2) {}
1642e35fdd936d133bf8a48de140a3c666897588a05shiqian
1643e35fdd936d133bf8a48de140a3c666897588a05shiqian  // This template type conversion operator allows a
1644e35fdd936d133bf8a48de140a3c666897588a05shiqian  // BothOfMatcher<Matcher1, Matcher2> object to match any type that
1645e35fdd936d133bf8a48de140a3c666897588a05shiqian  // both Matcher1 and Matcher2 can match.
1646e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1647e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<T>() const {
1648c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    return Matcher<T>(new BothOfMatcherImpl<T>(SafeMatcherCast<T>(matcher1_),
1649c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan                                               SafeMatcherCast<T>(matcher2_)));
1650e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
165132de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1652e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1653c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  Matcher1 matcher1_;
1654c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  Matcher2 matcher2_;
165532de5f53763125925e078498250f7e73a88de9edzhanyong.wan
165632de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(BothOfMatcher);
1657c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan};
1658e35fdd936d133bf8a48de140a3c666897588a05shiqian
1659c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// Implements the AnyOf(m1, m2) matcher for a particular argument type
1660c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// T.  We do not nest it inside the AnyOfMatcher class template, as
1661c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// that will prevent different instantiations of AnyOfMatcher from
1662c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// sharing the same EitherOfMatcherImpl<T> class.
1663c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wantemplate <typename T>
1664c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wanclass EitherOfMatcherImpl : public MatcherInterface<T> {
1665c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan public:
1666c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  EitherOfMatcherImpl(const Matcher<T>& matcher1, const Matcher<T>& matcher2)
1667c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan      : matcher1_(matcher1), matcher2_(matcher2) {}
1668e35fdd936d133bf8a48de140a3c666897588a05shiqian
1669c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
1670c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    *os << "(";
1671c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    matcher1_.DescribeTo(os);
1672c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    *os << ") or (";
1673c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    matcher2_.DescribeTo(os);
1674c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan    *os << ")";
1675c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1676e35fdd936d133bf8a48de140a3c666897588a05shiqian
1677c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
1678b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    *os << "(";
1679b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    matcher1_.DescribeNegationTo(os);
1680b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    *os << ") and (";
1681b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    matcher2_.DescribeNegationTo(os);
1682b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    *os << ")";
1683c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
1684c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1685821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
1686821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // If either matcher1_ or matcher2_ matches x, we just need to
1687821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // explain why *one* of them matches.
1688821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    StringMatchResultListener listener1;
1689821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (matcher1_.MatchAndExplain(x, &listener1)) {
1690821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      *listener << listener1.str();
1691821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      return true;
1692821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    }
1693c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1694821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    StringMatchResultListener listener2;
1695821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (matcher2_.MatchAndExplain(x, &listener2)) {
1696821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      *listener << listener2.str();
1697821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      return true;
1698821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    }
1699c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan
1700821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // Otherwise we need to explain why *both* of them fail.
1701821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    const internal::string s1 = listener1.str();
1702821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    const internal::string s2 = listener2.str();
1703821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
1704821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (s1 == "") {
1705821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      *listener << s2;
1706821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    } else {
1707821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      *listener << s1;
1708821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      if (s2 != "") {
1709b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan        *listener << ", and " << s2;
1710e35fdd936d133bf8a48de140a3c666897588a05shiqian      }
1711e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1712821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    return false;
1713c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  }
171432de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1715c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan private:
1716c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  const Matcher<T> matcher1_;
1717c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan  const Matcher<T> matcher2_;
171832de5f53763125925e078498250f7e73a88de9edzhanyong.wan
171932de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(EitherOfMatcherImpl);
1720e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1721e35fdd936d133bf8a48de140a3c666897588a05shiqian
1722616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan#if GTEST_LANG_CXX11
1723616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// AnyOfMatcher is used for the variadic implementation of AnyOf(m_1, m_2, ...).
1724616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wantemplate <typename... Args>
1725616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wanusing AnyOfMatcher = VariadicMatcher<EitherOfMatcherImpl, Args...>;
1726616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1727616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan#endif  // GTEST_LANG_CXX11
1728616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1729e35fdd936d133bf8a48de140a3c666897588a05shiqian// Used for implementing the AnyOf(m_1, ..., m_n) matcher, which
1730e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a value that matches at least one of the matchers m_1, ...,
1731e35fdd936d133bf8a48de140a3c666897588a05shiqian// and m_n.
1732e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2>
1733e35fdd936d133bf8a48de140a3c666897588a05shiqianclass EitherOfMatcher {
1734e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1735e35fdd936d133bf8a48de140a3c666897588a05shiqian  EitherOfMatcher(Matcher1 matcher1, Matcher2 matcher2)
1736e35fdd936d133bf8a48de140a3c666897588a05shiqian      : matcher1_(matcher1), matcher2_(matcher2) {}
1737e35fdd936d133bf8a48de140a3c666897588a05shiqian
1738e35fdd936d133bf8a48de140a3c666897588a05shiqian  // This template type conversion operator allows a
1739e35fdd936d133bf8a48de140a3c666897588a05shiqian  // EitherOfMatcher<Matcher1, Matcher2> object to match any type that
1740e35fdd936d133bf8a48de140a3c666897588a05shiqian  // both Matcher1 and Matcher2 can match.
1741e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1742e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<T>() const {
174316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan    return Matcher<T>(new EitherOfMatcherImpl<T>(
174416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan        SafeMatcherCast<T>(matcher1_), SafeMatcherCast<T>(matcher2_)));
1745e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
174632de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1747e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1748e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher1 matcher1_;
1749e35fdd936d133bf8a48de140a3c666897588a05shiqian  Matcher2 matcher2_;
175032de5f53763125925e078498250f7e73a88de9edzhanyong.wan
175132de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(EitherOfMatcher);
1752e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1753e35fdd936d133bf8a48de140a3c666897588a05shiqian
1754e35fdd936d133bf8a48de140a3c666897588a05shiqian// Used for implementing Truly(pred), which turns a predicate into a
1755e35fdd936d133bf8a48de140a3c666897588a05shiqian// matcher.
1756e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Predicate>
1757e35fdd936d133bf8a48de140a3c666897588a05shiqianclass TrulyMatcher {
1758e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1759e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit TrulyMatcher(Predicate pred) : predicate_(pred) {}
1760e35fdd936d133bf8a48de140a3c666897588a05shiqian
1761e35fdd936d133bf8a48de140a3c666897588a05shiqian  // This method template allows Truly(pred) to be used as a matcher
1762e35fdd936d133bf8a48de140a3c666897588a05shiqian  // for type T where T is the argument type of predicate 'pred'.  The
1763e35fdd936d133bf8a48de140a3c666897588a05shiqian  // argument is passed by reference as the predicate may be
1764e35fdd936d133bf8a48de140a3c666897588a05shiqian  // interested in the address of the argument.
1765e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1766db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplain(T& x,  // NOLINT
1767db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                       MatchResultListener* /* listener */) const {
17688d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan    // Without the if-statement, MSVC sometimes warns about converting
17698d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan    // a value to bool (warning 4800).
17708d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan    //
17718d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan    // We cannot write 'return !!predicate_(x);' as that doesn't work
17728d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan    // when predicate_(x) returns a class convertible to bool but
17738d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan    // having no operator!().
17748d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan    if (predicate_(x))
17758d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan      return true;
17768d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan    return false;
1777e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1778e35fdd936d133bf8a48de140a3c666897588a05shiqian
1779e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
1780e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "satisfies the given predicate";
1781e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
1782e35fdd936d133bf8a48de140a3c666897588a05shiqian
1783e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
1784e35fdd936d133bf8a48de140a3c666897588a05shiqian    *os << "doesn't satisfy the given predicate";
1785e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
178632de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1787e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1788e35fdd936d133bf8a48de140a3c666897588a05shiqian  Predicate predicate_;
178932de5f53763125925e078498250f7e73a88de9edzhanyong.wan
179032de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(TrulyMatcher);
1791e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1792e35fdd936d133bf8a48de140a3c666897588a05shiqian
1793e35fdd936d133bf8a48de140a3c666897588a05shiqian// Used for implementing Matches(matcher), which turns a matcher into
1794e35fdd936d133bf8a48de140a3c666897588a05shiqian// a predicate.
1795e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename M>
1796e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatcherAsPredicate {
1797e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1798e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit MatcherAsPredicate(M matcher) : matcher_(matcher) {}
1799e35fdd936d133bf8a48de140a3c666897588a05shiqian
1800e35fdd936d133bf8a48de140a3c666897588a05shiqian  // This template operator() allows Matches(m) to be used as a
1801e35fdd936d133bf8a48de140a3c666897588a05shiqian  // predicate on type T where m is a matcher on type T.
1802e35fdd936d133bf8a48de140a3c666897588a05shiqian  //
1803e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The argument x is passed by reference instead of by value, as
1804e35fdd936d133bf8a48de140a3c666897588a05shiqian  // some matcher may be interested in its address (e.g. as in
1805e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Matches(Ref(n))(x)).
1806e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1807e35fdd936d133bf8a48de140a3c666897588a05shiqian  bool operator()(const T& x) const {
1808e35fdd936d133bf8a48de140a3c666897588a05shiqian    // We let matcher_ commit to a particular type here instead of
1809e35fdd936d133bf8a48de140a3c666897588a05shiqian    // when the MatcherAsPredicate object was constructed.  This
1810e35fdd936d133bf8a48de140a3c666897588a05shiqian    // allows us to write Matches(m) where m is a polymorphic matcher
1811e35fdd936d133bf8a48de140a3c666897588a05shiqian    // (e.g. Eq(5)).
1812e35fdd936d133bf8a48de140a3c666897588a05shiqian    //
1813e35fdd936d133bf8a48de140a3c666897588a05shiqian    // If we write Matcher<T>(matcher_).Matches(x) here, it won't
1814e35fdd936d133bf8a48de140a3c666897588a05shiqian    // compile when matcher_ has type Matcher<const T&>; if we write
1815e35fdd936d133bf8a48de140a3c666897588a05shiqian    // Matcher<const T&>(matcher_).Matches(x) here, it won't compile
1816e35fdd936d133bf8a48de140a3c666897588a05shiqian    // when matcher_ has type Matcher<T>; if we just write
1817e35fdd936d133bf8a48de140a3c666897588a05shiqian    // matcher_.Matches(x), it won't compile when matcher_ is
1818e35fdd936d133bf8a48de140a3c666897588a05shiqian    // polymorphic, e.g. Eq(5).
1819e35fdd936d133bf8a48de140a3c666897588a05shiqian    //
1820e35fdd936d133bf8a48de140a3c666897588a05shiqian    // MatcherCast<const T&>() is necessary for making the code work
1821e35fdd936d133bf8a48de140a3c666897588a05shiqian    // in all of the above situations.
1822e35fdd936d133bf8a48de140a3c666897588a05shiqian    return MatcherCast<const T&>(matcher_).Matches(x);
1823e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
182432de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1825e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1826e35fdd936d133bf8a48de140a3c666897588a05shiqian  M matcher_;
182732de5f53763125925e078498250f7e73a88de9edzhanyong.wan
182832de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(MatcherAsPredicate);
1829e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1830e35fdd936d133bf8a48de140a3c666897588a05shiqian
1831e35fdd936d133bf8a48de140a3c666897588a05shiqian// For implementing ASSERT_THAT() and EXPECT_THAT().  The template
1832e35fdd936d133bf8a48de140a3c666897588a05shiqian// argument M must be a type that can be converted to a matcher.
1833e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename M>
1834e35fdd936d133bf8a48de140a3c666897588a05shiqianclass PredicateFormatterFromMatcher {
1835e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
18369b1a944ec48b7f84ad2f4466ed3babd232a20db5kosak  explicit PredicateFormatterFromMatcher(M m) : matcher_(internal::move(m)) {}
1837e35fdd936d133bf8a48de140a3c666897588a05shiqian
1838e35fdd936d133bf8a48de140a3c666897588a05shiqian  // This template () operator allows a PredicateFormatterFromMatcher
1839e35fdd936d133bf8a48de140a3c666897588a05shiqian  // object to act as a predicate-formatter suitable for using with
1840e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Google Test's EXPECT_PRED_FORMAT1() macro.
1841e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1842e35fdd936d133bf8a48de140a3c666897588a05shiqian  AssertionResult operator()(const char* value_text, const T& x) const {
1843e35fdd936d133bf8a48de140a3c666897588a05shiqian    // We convert matcher_ to a Matcher<const T&> *now* instead of
1844e35fdd936d133bf8a48de140a3c666897588a05shiqian    // when the PredicateFormatterFromMatcher object was constructed,
1845e35fdd936d133bf8a48de140a3c666897588a05shiqian    // as matcher_ may be polymorphic (e.g. NotNull()) and we won't
1846e35fdd936d133bf8a48de140a3c666897588a05shiqian    // know which type to instantiate it to until we actually see the
1847e35fdd936d133bf8a48de140a3c666897588a05shiqian    // type of x here.
1848e35fdd936d133bf8a48de140a3c666897588a05shiqian    //
1849f4274520da256a3e8dd4fe2aa2e714f44ad89d18zhanyong.wan    // We write SafeMatcherCast<const T&>(matcher_) instead of
1850e35fdd936d133bf8a48de140a3c666897588a05shiqian    // Matcher<const T&>(matcher_), as the latter won't compile when
1851e35fdd936d133bf8a48de140a3c666897588a05shiqian    // matcher_ has type Matcher<T> (e.g. An<int>()).
1852f4274520da256a3e8dd4fe2aa2e714f44ad89d18zhanyong.wan    // We don't write MatcherCast<const T&> either, as that allows
1853f4274520da256a3e8dd4fe2aa2e714f44ad89d18zhanyong.wan    // potentially unsafe downcasting of the matcher argument.
1854f4274520da256a3e8dd4fe2aa2e714f44ad89d18zhanyong.wan    const Matcher<const T&> matcher = SafeMatcherCast<const T&>(matcher_);
1855821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    StringMatchResultListener listener;
1856b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    if (MatchPrintAndExplain(x, matcher, &listener))
1857e35fdd936d133bf8a48de140a3c666897588a05shiqian      return AssertionSuccess();
1858b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan
1859b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    ::std::stringstream ss;
1860b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    ss << "Value of: " << value_text << "\n"
1861b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan       << "Expected: ";
1862b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    matcher.DescribeTo(&ss);
1863b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    ss << "\n  Actual: " << listener.str();
1864b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    return AssertionFailure() << ss.str();
1865e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
186632de5f53763125925e078498250f7e73a88de9edzhanyong.wan
1867e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
1868e35fdd936d133bf8a48de140a3c666897588a05shiqian  const M matcher_;
186932de5f53763125925e078498250f7e73a88de9edzhanyong.wan
187032de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(PredicateFormatterFromMatcher);
1871e35fdd936d133bf8a48de140a3c666897588a05shiqian};
1872e35fdd936d133bf8a48de140a3c666897588a05shiqian
1873e35fdd936d133bf8a48de140a3c666897588a05shiqian// A helper function for converting a matcher to a predicate-formatter
1874e35fdd936d133bf8a48de140a3c666897588a05shiqian// without the user needing to explicitly write the type.  This is
1875e35fdd936d133bf8a48de140a3c666897588a05shiqian// used for implementing ASSERT_THAT() and EXPECT_THAT().
18769b1a944ec48b7f84ad2f4466ed3babd232a20db5kosak// Implementation detail: 'matcher' is received by-value to force decaying.
1877e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename M>
1878e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PredicateFormatterFromMatcher<M>
18799b1a944ec48b7f84ad2f4466ed3babd232a20db5kosakMakePredicateFormatterFromMatcher(M matcher) {
18809b1a944ec48b7f84ad2f4466ed3babd232a20db5kosak  return PredicateFormatterFromMatcher<M>(internal::move(matcher));
1881e35fdd936d133bf8a48de140a3c666897588a05shiqian}
1882e35fdd936d133bf8a48de140a3c666897588a05shiqian
1883616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// Implements the polymorphic floating point equality matcher, which matches
1884616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// two float values using ULP-based approximation or, optionally, a
1885616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// user-specified epsilon.  The template is meant to be instantiated with
1886616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// FloatType being either float or double.
1887e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename FloatType>
1888e35fdd936d133bf8a48de140a3c666897588a05shiqianclass FloatingEqMatcher {
1889e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
1890e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Constructor for FloatingEqMatcher.
18916b817803104b30d5951c652a1427492db116a490kosak  // The matcher's input will be compared with expected.  The matcher treats two
1892e35fdd936d133bf8a48de140a3c666897588a05shiqian  // NANs as equal if nan_eq_nan is true.  Otherwise, under IEEE standards,
1893616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // equality comparisons between NANs will always return false.  We specify a
1894616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // negative max_abs_error_ term to indicate that ULP-based approximation will
1895616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // be used for comparison.
18966b817803104b30d5951c652a1427492db116a490kosak  FloatingEqMatcher(FloatType expected, bool nan_eq_nan) :
18976b817803104b30d5951c652a1427492db116a490kosak    expected_(expected), nan_eq_nan_(nan_eq_nan), max_abs_error_(-1) {
1898616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  }
1899616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
1900616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // Constructor that supports a user-specified max_abs_error that will be used
1901616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // for comparison instead of ULP-based approximation.  The max absolute
1902616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // should be non-negative.
19036b817803104b30d5951c652a1427492db116a490kosak  FloatingEqMatcher(FloatType expected, bool nan_eq_nan,
19046b817803104b30d5951c652a1427492db116a490kosak                    FloatType max_abs_error)
19056b817803104b30d5951c652a1427492db116a490kosak      : expected_(expected),
19066b817803104b30d5951c652a1427492db116a490kosak        nan_eq_nan_(nan_eq_nan),
19076b817803104b30d5951c652a1427492db116a490kosak        max_abs_error_(max_abs_error) {
1908616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    GTEST_CHECK_(max_abs_error >= 0)
1909616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        << ", where max_abs_error is" << max_abs_error;
1910616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  }
1911e35fdd936d133bf8a48de140a3c666897588a05shiqian
1912e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Implements floating point equality matcher as a Matcher<T>.
1913e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
1914e35fdd936d133bf8a48de140a3c666897588a05shiqian  class Impl : public MatcherInterface<T> {
1915e35fdd936d133bf8a48de140a3c666897588a05shiqian   public:
19166b817803104b30d5951c652a1427492db116a490kosak    Impl(FloatType expected, bool nan_eq_nan, FloatType max_abs_error)
19176b817803104b30d5951c652a1427492db116a490kosak        : expected_(expected),
19186b817803104b30d5951c652a1427492db116a490kosak          nan_eq_nan_(nan_eq_nan),
19196b817803104b30d5951c652a1427492db116a490kosak          max_abs_error_(max_abs_error) {}
1920e35fdd936d133bf8a48de140a3c666897588a05shiqian
1921821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    virtual bool MatchAndExplain(T value,
19226b817803104b30d5951c652a1427492db116a490kosak                                 MatchResultListener* listener) const {
19236b817803104b30d5951c652a1427492db116a490kosak      const FloatingPoint<FloatType> actual(value), expected(expected_);
1924e35fdd936d133bf8a48de140a3c666897588a05shiqian
1925e35fdd936d133bf8a48de140a3c666897588a05shiqian      // Compares NaNs first, if nan_eq_nan_ is true.
19266b817803104b30d5951c652a1427492db116a490kosak      if (actual.is_nan() || expected.is_nan()) {
19276b817803104b30d5951c652a1427492db116a490kosak        if (actual.is_nan() && expected.is_nan()) {
1928616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan          return nan_eq_nan_;
1929616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        }
1930616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        // One is nan; the other is not nan.
1931616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        return false;
1932616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan      }
1933616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan      if (HasMaxAbsError()) {
1934616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        // We perform an equality check so that inf will match inf, regardless
19356b817803104b30d5951c652a1427492db116a490kosak        // of error bounds.  If the result of value - expected_ would result in
1936616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        // overflow or if either value is inf, the default result is infinity,
1937616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        // which should only match if max_abs_error_ is also infinity.
19386b817803104b30d5951c652a1427492db116a490kosak        if (value == expected_) {
19396b817803104b30d5951c652a1427492db116a490kosak          return true;
19406b817803104b30d5951c652a1427492db116a490kosak        }
19416b817803104b30d5951c652a1427492db116a490kosak
19426b817803104b30d5951c652a1427492db116a490kosak        const FloatType diff = value - expected_;
19436b817803104b30d5951c652a1427492db116a490kosak        if (fabs(diff) <= max_abs_error_) {
19446b817803104b30d5951c652a1427492db116a490kosak          return true;
19456b817803104b30d5951c652a1427492db116a490kosak        }
19466b817803104b30d5951c652a1427492db116a490kosak
19476b817803104b30d5951c652a1427492db116a490kosak        if (listener->IsInterested()) {
19486b817803104b30d5951c652a1427492db116a490kosak          *listener << "which is " << diff << " from " << expected_;
19496b817803104b30d5951c652a1427492db116a490kosak        }
19506b817803104b30d5951c652a1427492db116a490kosak        return false;
1951616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan      } else {
19526b817803104b30d5951c652a1427492db116a490kosak        return actual.AlmostEquals(expected);
1953e35fdd936d133bf8a48de140a3c666897588a05shiqian      }
1954e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1955e35fdd936d133bf8a48de140a3c666897588a05shiqian
1956e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeTo(::std::ostream* os) const {
1957e35fdd936d133bf8a48de140a3c666897588a05shiqian      // os->precision() returns the previously set precision, which we
1958e35fdd936d133bf8a48de140a3c666897588a05shiqian      // store to restore the ostream to its original configuration
1959e35fdd936d133bf8a48de140a3c666897588a05shiqian      // after outputting.
1960e35fdd936d133bf8a48de140a3c666897588a05shiqian      const ::std::streamsize old_precision = os->precision(
1961e35fdd936d133bf8a48de140a3c666897588a05shiqian          ::std::numeric_limits<FloatType>::digits10 + 2);
19626b817803104b30d5951c652a1427492db116a490kosak      if (FloatingPoint<FloatType>(expected_).is_nan()) {
1963e35fdd936d133bf8a48de140a3c666897588a05shiqian        if (nan_eq_nan_) {
1964e35fdd936d133bf8a48de140a3c666897588a05shiqian          *os << "is NaN";
1965e35fdd936d133bf8a48de140a3c666897588a05shiqian        } else {
1966e35fdd936d133bf8a48de140a3c666897588a05shiqian          *os << "never matches";
1967e35fdd936d133bf8a48de140a3c666897588a05shiqian        }
1968e35fdd936d133bf8a48de140a3c666897588a05shiqian      } else {
19696b817803104b30d5951c652a1427492db116a490kosak        *os << "is approximately " << expected_;
1970616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        if (HasMaxAbsError()) {
1971616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan          *os << " (absolute error <= " << max_abs_error_ << ")";
1972616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        }
1973e35fdd936d133bf8a48de140a3c666897588a05shiqian      }
1974e35fdd936d133bf8a48de140a3c666897588a05shiqian      os->precision(old_precision);
1975e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1976e35fdd936d133bf8a48de140a3c666897588a05shiqian
1977e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeNegationTo(::std::ostream* os) const {
1978e35fdd936d133bf8a48de140a3c666897588a05shiqian      // As before, get original precision.
1979e35fdd936d133bf8a48de140a3c666897588a05shiqian      const ::std::streamsize old_precision = os->precision(
1980e35fdd936d133bf8a48de140a3c666897588a05shiqian          ::std::numeric_limits<FloatType>::digits10 + 2);
19816b817803104b30d5951c652a1427492db116a490kosak      if (FloatingPoint<FloatType>(expected_).is_nan()) {
1982e35fdd936d133bf8a48de140a3c666897588a05shiqian        if (nan_eq_nan_) {
1983b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan          *os << "isn't NaN";
1984e35fdd936d133bf8a48de140a3c666897588a05shiqian        } else {
1985e35fdd936d133bf8a48de140a3c666897588a05shiqian          *os << "is anything";
1986e35fdd936d133bf8a48de140a3c666897588a05shiqian        }
1987e35fdd936d133bf8a48de140a3c666897588a05shiqian      } else {
19886b817803104b30d5951c652a1427492db116a490kosak        *os << "isn't approximately " << expected_;
1989616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        if (HasMaxAbsError()) {
1990616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan          *os << " (absolute error > " << max_abs_error_ << ")";
1991616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan        }
1992e35fdd936d133bf8a48de140a3c666897588a05shiqian      }
1993e35fdd936d133bf8a48de140a3c666897588a05shiqian      // Restore original precision.
1994e35fdd936d133bf8a48de140a3c666897588a05shiqian      os->precision(old_precision);
1995e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
1996e35fdd936d133bf8a48de140a3c666897588a05shiqian
1997e35fdd936d133bf8a48de140a3c666897588a05shiqian   private:
1998616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    bool HasMaxAbsError() const {
1999616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan      return max_abs_error_ >= 0;
2000616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    }
2001616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
20026b817803104b30d5951c652a1427492db116a490kosak    const FloatType expected_;
2003e35fdd936d133bf8a48de140a3c666897588a05shiqian    const bool nan_eq_nan_;
2004616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    // max_abs_error will be used for value comparison when >= 0.
2005616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    const FloatType max_abs_error_;
200632de5f53763125925e078498250f7e73a88de9edzhanyong.wan
200732de5f53763125925e078498250f7e73a88de9edzhanyong.wan    GTEST_DISALLOW_ASSIGN_(Impl);
2008e35fdd936d133bf8a48de140a3c666897588a05shiqian  };
2009e35fdd936d133bf8a48de140a3c666897588a05shiqian
20106b817803104b30d5951c652a1427492db116a490kosak  // The following 3 type conversion operators allow FloatEq(expected) and
20116b817803104b30d5951c652a1427492db116a490kosak  // NanSensitiveFloatEq(expected) to be used as a Matcher<float>, a
2012e35fdd936d133bf8a48de140a3c666897588a05shiqian  // Matcher<const float&>, or a Matcher<float&>, but nothing else.
2013e35fdd936d133bf8a48de140a3c666897588a05shiqian  // (While Google's C++ coding style doesn't allow arguments passed
2014e35fdd936d133bf8a48de140a3c666897588a05shiqian  // by non-const reference, we may see them in code not conforming to
2015e35fdd936d133bf8a48de140a3c666897588a05shiqian  // the style.  Therefore Google Mock needs to support them.)
2016e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<FloatType>() const {
20176b817803104b30d5951c652a1427492db116a490kosak    return MakeMatcher(
20186b817803104b30d5951c652a1427492db116a490kosak        new Impl<FloatType>(expected_, nan_eq_nan_, max_abs_error_));
2019e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
2020e35fdd936d133bf8a48de140a3c666897588a05shiqian
2021e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<const FloatType&>() const {
2022616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    return MakeMatcher(
20236b817803104b30d5951c652a1427492db116a490kosak        new Impl<const FloatType&>(expected_, nan_eq_nan_, max_abs_error_));
2024e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
2025e35fdd936d133bf8a48de140a3c666897588a05shiqian
2026e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<FloatType&>() const {
20276b817803104b30d5951c652a1427492db116a490kosak    return MakeMatcher(
20286b817803104b30d5951c652a1427492db116a490kosak        new Impl<FloatType&>(expected_, nan_eq_nan_, max_abs_error_));
2029e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
203079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm
2031e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
20326b817803104b30d5951c652a1427492db116a490kosak  const FloatType expected_;
2033e35fdd936d133bf8a48de140a3c666897588a05shiqian  const bool nan_eq_nan_;
2034616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  // max_abs_error will be used for value comparison when >= 0.
2035616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  const FloatType max_abs_error_;
203632de5f53763125925e078498250f7e73a88de9edzhanyong.wan
203732de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(FloatingEqMatcher);
2038e35fdd936d133bf8a48de140a3c666897588a05shiqian};
2039e35fdd936d133bf8a48de140a3c666897588a05shiqian
2040e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the Pointee(m) matcher for matching a pointer whose
2041e35fdd936d133bf8a48de140a3c666897588a05shiqian// pointee matches matcher m.  The pointer can be either raw or smart.
2042e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename InnerMatcher>
2043e35fdd936d133bf8a48de140a3c666897588a05shiqianclass PointeeMatcher {
2044e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
2045e35fdd936d133bf8a48de140a3c666897588a05shiqian  explicit PointeeMatcher(const InnerMatcher& matcher) : matcher_(matcher) {}
2046e35fdd936d133bf8a48de140a3c666897588a05shiqian
2047e35fdd936d133bf8a48de140a3c666897588a05shiqian  // This type conversion operator template allows Pointee(m) to be
2048e35fdd936d133bf8a48de140a3c666897588a05shiqian  // used as a matcher for any pointer type whose pointee type is
2049e35fdd936d133bf8a48de140a3c666897588a05shiqian  // compatible with the inner matcher, where type Pointer can be
2050e35fdd936d133bf8a48de140a3c666897588a05shiqian  // either a raw pointer or a smart pointer.
2051e35fdd936d133bf8a48de140a3c666897588a05shiqian  //
2052e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The reason we do this instead of relying on
2053e35fdd936d133bf8a48de140a3c666897588a05shiqian  // MakePolymorphicMatcher() is that the latter is not flexible
2054e35fdd936d133bf8a48de140a3c666897588a05shiqian  // enough for implementing the DescribeTo() method of Pointee().
2055e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename Pointer>
2056e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<Pointer>() const {
2057e35fdd936d133bf8a48de140a3c666897588a05shiqian    return MakeMatcher(new Impl<Pointer>(matcher_));
2058e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
205932de5f53763125925e078498250f7e73a88de9edzhanyong.wan
2060e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
2061e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The monomorphic implementation that works for a particular pointer type.
2062e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename Pointer>
2063e35fdd936d133bf8a48de140a3c666897588a05shiqian  class Impl : public MatcherInterface<Pointer> {
2064e35fdd936d133bf8a48de140a3c666897588a05shiqian   public:
206502f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan    typedef typename PointeeOf<GTEST_REMOVE_CONST_(  // NOLINT
206602f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan        GTEST_REMOVE_REFERENCE_(Pointer))>::type Pointee;
2067e35fdd936d133bf8a48de140a3c666897588a05shiqian
2068e35fdd936d133bf8a48de140a3c666897588a05shiqian    explicit Impl(const InnerMatcher& matcher)
2069e35fdd936d133bf8a48de140a3c666897588a05shiqian        : matcher_(MatcherCast<const Pointee&>(matcher)) {}
2070e35fdd936d133bf8a48de140a3c666897588a05shiqian
2071e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeTo(::std::ostream* os) const {
2072e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "points to a value that ";
2073e35fdd936d133bf8a48de140a3c666897588a05shiqian      matcher_.DescribeTo(os);
2074e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
2075e35fdd936d133bf8a48de140a3c666897588a05shiqian
2076e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeNegationTo(::std::ostream* os) const {
2077e35fdd936d133bf8a48de140a3c666897588a05shiqian      *os << "does not point to a value that ";
2078e35fdd936d133bf8a48de140a3c666897588a05shiqian      matcher_.DescribeTo(os);
2079e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
2080e35fdd936d133bf8a48de140a3c666897588a05shiqian
2081821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    virtual bool MatchAndExplain(Pointer pointer,
2082821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan                                 MatchResultListener* listener) const {
2083e35fdd936d133bf8a48de140a3c666897588a05shiqian      if (GetRawPointer(pointer) == NULL)
2084821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan        return false;
2085e35fdd936d133bf8a48de140a3c666897588a05shiqian
2086676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      *listener << "which points to ";
2087676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      return MatchPrintAndExplain(*pointer, matcher_, listener);
2088e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
208932de5f53763125925e078498250f7e73a88de9edzhanyong.wan
2090e35fdd936d133bf8a48de140a3c666897588a05shiqian   private:
2091e35fdd936d133bf8a48de140a3c666897588a05shiqian    const Matcher<const Pointee&> matcher_;
209232de5f53763125925e078498250f7e73a88de9edzhanyong.wan
209332de5f53763125925e078498250f7e73a88de9edzhanyong.wan    GTEST_DISALLOW_ASSIGN_(Impl);
2094e35fdd936d133bf8a48de140a3c666897588a05shiqian  };
2095e35fdd936d133bf8a48de140a3c666897588a05shiqian
2096e35fdd936d133bf8a48de140a3c666897588a05shiqian  const InnerMatcher matcher_;
209732de5f53763125925e078498250f7e73a88de9edzhanyong.wan
209832de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(PointeeMatcher);
2099e35fdd936d133bf8a48de140a3c666897588a05shiqian};
2100e35fdd936d133bf8a48de140a3c666897588a05shiqian
21011f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue// Implements the WhenDynamicCastTo<T>(m) matcher that matches a pointer or
21021f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue// reference that matches inner_matcher when dynamic_cast<T> is applied.
21031f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue// The result of dynamic_cast<To> is forwarded to the inner matcher.
21041f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue// If To is a pointer and the cast fails, the inner matcher will receive NULL.
21051f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue// If To is a reference and the cast fails, this matcher returns false
21061f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue// immediately.
21071f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahuetemplate <typename To>
21081f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahueclass WhenDynamicCastToMatcherBase {
21091f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue public:
21101f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue  explicit WhenDynamicCastToMatcherBase(const Matcher<To>& matcher)
21111f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue      : matcher_(matcher) {}
21121f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue
21131f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue  void DescribeTo(::std::ostream* os) const {
21141f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue    GetCastTypeDescription(os);
21151f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue    matcher_.DescribeTo(os);
21161f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue  }
21171f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue
21181f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue  void DescribeNegationTo(::std::ostream* os) const {
21191f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue    GetCastTypeDescription(os);
21201f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue    matcher_.DescribeNegationTo(os);
21211f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue  }
21221f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue
21231f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue protected:
21241f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue  const Matcher<To> matcher_;
21251f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue
21261f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue  static string GetToName() {
21271f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue#if GTEST_HAS_RTTI
21281f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue    return GetTypeName<To>();
21291f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue#else  // GTEST_HAS_RTTI
21301f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue    return "the target type";
21311f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue#endif  // GTEST_HAS_RTTI
21321f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue  }
21331f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue
21341f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue private:
21351f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue  static void GetCastTypeDescription(::std::ostream* os) {
21361f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue    *os << "when dynamic_cast to " << GetToName() << ", ";
21371f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue  }
21381f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue
21391f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue  GTEST_DISALLOW_ASSIGN_(WhenDynamicCastToMatcherBase);
21401f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue};
21411f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue
21421f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue// Primary template.
21431f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue// To is a pointer. Cast and forward the result.
21441f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahuetemplate <typename To>
21451f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahueclass WhenDynamicCastToMatcher : public WhenDynamicCastToMatcherBase<To> {
21461f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue public:
21471f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue  explicit WhenDynamicCastToMatcher(const Matcher<To>& matcher)
21481f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue      : WhenDynamicCastToMatcherBase<To>(matcher) {}
21491f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue
21501f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue  template <typename From>
21511f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue  bool MatchAndExplain(From from, MatchResultListener* listener) const {
21521f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue    // TODO(sbenza): Add more detail on failures. ie did the dyn_cast fail?
21531f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue    To to = dynamic_cast<To>(from);
21541f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue    return MatchPrintAndExplain(to, this->matcher_, listener);
21551f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue  }
21561f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue};
21571f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue
21581f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue// Specialize for references.
21591f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue// In this case we return false if the dynamic_cast fails.
21601f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahuetemplate <typename To>
21611f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahueclass WhenDynamicCastToMatcher<To&> : public WhenDynamicCastToMatcherBase<To&> {
21621f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue public:
21631f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue  explicit WhenDynamicCastToMatcher(const Matcher<To&>& matcher)
21641f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue      : WhenDynamicCastToMatcherBase<To&>(matcher) {}
21651f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue
21661f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue  template <typename From>
21671f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue  bool MatchAndExplain(From& from, MatchResultListener* listener) const {
21681f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue    // We don't want an std::bad_cast here, so do the cast with pointers.
21691f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue    To* to = dynamic_cast<To*>(&from);
21701f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue    if (to == NULL) {
21711f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue      *listener << "which cannot be dynamic_cast to " << this->GetToName();
21721f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue      return false;
21731f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue    }
21741f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue    return MatchPrintAndExplain(*to, this->matcher_, listener);
21751f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue  }
21761f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue};
21771f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue
2178e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the Field() matcher for matching a field (i.e. member
2179e35fdd936d133bf8a48de140a3c666897588a05shiqian// variable) of an object.
2180e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Class, typename FieldType>
2181e35fdd936d133bf8a48de140a3c666897588a05shiqianclass FieldMatcher {
2182e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
2183e35fdd936d133bf8a48de140a3c666897588a05shiqian  FieldMatcher(FieldType Class::*field,
2184e35fdd936d133bf8a48de140a3c666897588a05shiqian               const Matcher<const FieldType&>& matcher)
2185e35fdd936d133bf8a48de140a3c666897588a05shiqian      : field_(field), matcher_(matcher) {}
2186e35fdd936d133bf8a48de140a3c666897588a05shiqian
2187e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
2188676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    *os << "is an object whose given field ";
2189e35fdd936d133bf8a48de140a3c666897588a05shiqian    matcher_.DescribeTo(os);
2190e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
2191e35fdd936d133bf8a48de140a3c666897588a05shiqian
2192e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
2193676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    *os << "is an object whose given field ";
2194e35fdd936d133bf8a48de140a3c666897588a05shiqian    matcher_.DescribeNegationTo(os);
2195e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
2196e35fdd936d133bf8a48de140a3c666897588a05shiqian
2197db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  template <typename T>
2198db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplain(const T& value, MatchResultListener* listener) const {
2199db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return MatchAndExplainImpl(
2200db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan        typename ::testing::internal::
220102f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan            is_pointer<GTEST_REMOVE_CONST_(T)>::type(),
2202db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan        value, listener);
2203db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  }
2204db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan
2205db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan private:
2206db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  // The first argument of MatchAndExplainImpl() is needed to help
220718490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  // Symbian's C++ compiler choose which overload to use.  Its type is
220818490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  // true_type iff the Field() matcher is used to match a pointer.
2209db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplainImpl(false_type /* is_not_pointer */, const Class& obj,
2210db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                           MatchResultListener* listener) const {
2211676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    *listener << "whose given field is ";
2212676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    return MatchPrintAndExplain(obj.*field_, matcher_, listener);
2213e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
2214e35fdd936d133bf8a48de140a3c666897588a05shiqian
2215db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplainImpl(true_type /* is_pointer */, const Class* p,
2216db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                           MatchResultListener* listener) const {
2217821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (p == NULL)
2218821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      return false;
2219821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
2220676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    *listener << "which points to an object ";
2221821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // Since *p has a field, it must be a class/struct/union type and
2222821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // thus cannot be a pointer.  Therefore we pass false_type() as
2223821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // the first argument.
2224db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return MatchAndExplainImpl(false_type(), *p, listener);
2225e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
222632de5f53763125925e078498250f7e73a88de9edzhanyong.wan
2227e35fdd936d133bf8a48de140a3c666897588a05shiqian  const FieldType Class::*field_;
2228e35fdd936d133bf8a48de140a3c666897588a05shiqian  const Matcher<const FieldType&> matcher_;
222932de5f53763125925e078498250f7e73a88de9edzhanyong.wan
223032de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(FieldMatcher);
2231e35fdd936d133bf8a48de140a3c666897588a05shiqian};
2232e35fdd936d133bf8a48de140a3c666897588a05shiqian
2233e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the Property() matcher for matching a property
2234e35fdd936d133bf8a48de140a3c666897588a05shiqian// (i.e. return value of a getter method) of an object.
2235e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Class, typename PropertyType>
2236e35fdd936d133bf8a48de140a3c666897588a05shiqianclass PropertyMatcher {
2237e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
2238e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The property may have a reference type, so 'const PropertyType&'
2239e35fdd936d133bf8a48de140a3c666897588a05shiqian  // may cause double references and fail to compile.  That's why we
224002f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan  // need GTEST_REFERENCE_TO_CONST, which works regardless of
2241e35fdd936d133bf8a48de140a3c666897588a05shiqian  // PropertyType being a reference or not.
224202f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan  typedef GTEST_REFERENCE_TO_CONST_(PropertyType) RefToConstProperty;
2243e35fdd936d133bf8a48de140a3c666897588a05shiqian
2244e35fdd936d133bf8a48de140a3c666897588a05shiqian  PropertyMatcher(PropertyType (Class::*property)() const,
2245e35fdd936d133bf8a48de140a3c666897588a05shiqian                  const Matcher<RefToConstProperty>& matcher)
2246e35fdd936d133bf8a48de140a3c666897588a05shiqian      : property_(property), matcher_(matcher) {}
2247e35fdd936d133bf8a48de140a3c666897588a05shiqian
2248e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeTo(::std::ostream* os) const {
2249676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    *os << "is an object whose given property ";
2250e35fdd936d133bf8a48de140a3c666897588a05shiqian    matcher_.DescribeTo(os);
2251e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
2252e35fdd936d133bf8a48de140a3c666897588a05shiqian
2253e35fdd936d133bf8a48de140a3c666897588a05shiqian  void DescribeNegationTo(::std::ostream* os) const {
2254676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    *os << "is an object whose given property ";
2255e35fdd936d133bf8a48de140a3c666897588a05shiqian    matcher_.DescribeNegationTo(os);
2256e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
2257e35fdd936d133bf8a48de140a3c666897588a05shiqian
2258db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  template <typename T>
2259db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplain(const T&value, MatchResultListener* listener) const {
2260db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return MatchAndExplainImpl(
2261db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan        typename ::testing::internal::
226202f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan            is_pointer<GTEST_REMOVE_CONST_(T)>::type(),
2263db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan        value, listener);
2264db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  }
2265db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan
2266db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan private:
2267db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  // The first argument of MatchAndExplainImpl() is needed to help
226818490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  // Symbian's C++ compiler choose which overload to use.  Its type is
226918490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan  // true_type iff the Property() matcher is used to match a pointer.
2270db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplainImpl(false_type /* is_not_pointer */, const Class& obj,
2271db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                           MatchResultListener* listener) const {
2272676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    *listener << "whose given property is ";
2273676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    // Cannot pass the return value (for example, int) to MatchPrintAndExplain,
2274676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    // which takes a non-const reference as argument.
227502d647925955836d04d90aafb5ae10a092af29bckosak#if defined(_PREFAST_ ) && _MSC_VER == 1800
227602d647925955836d04d90aafb5ae10a092af29bckosak    // Workaround bug in VC++ 2013's /analyze parser.
227702d647925955836d04d90aafb5ae10a092af29bckosak    // https://connect.microsoft.com/VisualStudio/feedback/details/1106363/internal-compiler-error-with-analyze-due-to-failure-to-infer-move
227802d647925955836d04d90aafb5ae10a092af29bckosak    posix::Abort();  // To make sure it is never run.
227902d647925955836d04d90aafb5ae10a092af29bckosak    return false;
228002d647925955836d04d90aafb5ae10a092af29bckosak#else
2281676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    RefToConstProperty result = (obj.*property_)();
2282676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    return MatchPrintAndExplain(result, matcher_, listener);
228302d647925955836d04d90aafb5ae10a092af29bckosak#endif
2284e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
2285e35fdd936d133bf8a48de140a3c666897588a05shiqian
2286db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan  bool MatchAndExplainImpl(true_type /* is_pointer */, const Class* p,
2287db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan                           MatchResultListener* listener) const {
2288821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (p == NULL)
2289821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      return false;
2290821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan
2291676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    *listener << "which points to an object ";
2292821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // Since *p has a property method, it must be a class/struct/union
2293821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // type and thus cannot be a pointer.  Therefore we pass
2294821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // false_type() as the first argument.
2295db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan    return MatchAndExplainImpl(false_type(), *p, listener);
2296e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
229732de5f53763125925e078498250f7e73a88de9edzhanyong.wan
2298e35fdd936d133bf8a48de140a3c666897588a05shiqian  PropertyType (Class::*property_)() const;
2299e35fdd936d133bf8a48de140a3c666897588a05shiqian  const Matcher<RefToConstProperty> matcher_;
230032de5f53763125925e078498250f7e73a88de9edzhanyong.wan
230132de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(PropertyMatcher);
2302e35fdd936d133bf8a48de140a3c666897588a05shiqian};
2303e35fdd936d133bf8a48de140a3c666897588a05shiqian
2304e35fdd936d133bf8a48de140a3c666897588a05shiqian// Type traits specifying various features of different functors for ResultOf.
2305e35fdd936d133bf8a48de140a3c666897588a05shiqian// The default template specifies features for functor objects.
2306e35fdd936d133bf8a48de140a3c666897588a05shiqian// Functor classes have to typedef argument_type and result_type
2307e35fdd936d133bf8a48de140a3c666897588a05shiqian// to be compatible with ResultOf.
2308e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Functor>
2309e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct CallableTraits {
2310e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef typename Functor::result_type ResultType;
2311e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef Functor StorageType;
2312e35fdd936d133bf8a48de140a3c666897588a05shiqian
231332de5f53763125925e078498250f7e73a88de9edzhanyong.wan  static void CheckIsValid(Functor /* functor */) {}
2314e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
2315e35fdd936d133bf8a48de140a3c666897588a05shiqian  static ResultType Invoke(Functor f, T arg) { return f(arg); }
2316e35fdd936d133bf8a48de140a3c666897588a05shiqian};
2317e35fdd936d133bf8a48de140a3c666897588a05shiqian
2318e35fdd936d133bf8a48de140a3c666897588a05shiqian// Specialization for function pointers.
2319e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename ArgType, typename ResType>
2320e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct CallableTraits<ResType(*)(ArgType)> {
2321e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef ResType ResultType;
2322e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef ResType(*StorageType)(ArgType);
2323e35fdd936d133bf8a48de140a3c666897588a05shiqian
2324e35fdd936d133bf8a48de140a3c666897588a05shiqian  static void CheckIsValid(ResType(*f)(ArgType)) {
2325f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    GTEST_CHECK_(f != NULL)
2326e35fdd936d133bf8a48de140a3c666897588a05shiqian        << "NULL function pointer is passed into ResultOf().";
2327e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
2328e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
2329e35fdd936d133bf8a48de140a3c666897588a05shiqian  static ResType Invoke(ResType(*f)(ArgType), T arg) {
2330e35fdd936d133bf8a48de140a3c666897588a05shiqian    return (*f)(arg);
2331e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
2332e35fdd936d133bf8a48de140a3c666897588a05shiqian};
2333e35fdd936d133bf8a48de140a3c666897588a05shiqian
2334e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the ResultOf() matcher for matching a return value of a
2335e35fdd936d133bf8a48de140a3c666897588a05shiqian// unary function of an object.
2336e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Callable>
2337e35fdd936d133bf8a48de140a3c666897588a05shiqianclass ResultOfMatcher {
2338e35fdd936d133bf8a48de140a3c666897588a05shiqian public:
2339e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef typename CallableTraits<Callable>::ResultType ResultType;
2340e35fdd936d133bf8a48de140a3c666897588a05shiqian
2341e35fdd936d133bf8a48de140a3c666897588a05shiqian  ResultOfMatcher(Callable callable, const Matcher<ResultType>& matcher)
2342e35fdd936d133bf8a48de140a3c666897588a05shiqian      : callable_(callable), matcher_(matcher) {
2343e35fdd936d133bf8a48de140a3c666897588a05shiqian    CallableTraits<Callable>::CheckIsValid(callable_);
2344e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
2345e35fdd936d133bf8a48de140a3c666897588a05shiqian
2346e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
2347e35fdd936d133bf8a48de140a3c666897588a05shiqian  operator Matcher<T>() const {
2348e35fdd936d133bf8a48de140a3c666897588a05shiqian    return Matcher<T>(new Impl<T>(callable_, matcher_));
2349e35fdd936d133bf8a48de140a3c666897588a05shiqian  }
2350e35fdd936d133bf8a48de140a3c666897588a05shiqian
2351e35fdd936d133bf8a48de140a3c666897588a05shiqian private:
2352e35fdd936d133bf8a48de140a3c666897588a05shiqian  typedef typename CallableTraits<Callable>::StorageType CallableStorageType;
2353e35fdd936d133bf8a48de140a3c666897588a05shiqian
2354e35fdd936d133bf8a48de140a3c666897588a05shiqian  template <typename T>
2355e35fdd936d133bf8a48de140a3c666897588a05shiqian  class Impl : public MatcherInterface<T> {
2356e35fdd936d133bf8a48de140a3c666897588a05shiqian   public:
2357e35fdd936d133bf8a48de140a3c666897588a05shiqian    Impl(CallableStorageType callable, const Matcher<ResultType>& matcher)
2358e35fdd936d133bf8a48de140a3c666897588a05shiqian        : callable_(callable), matcher_(matcher) {}
2359e35fdd936d133bf8a48de140a3c666897588a05shiqian
2360e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeTo(::std::ostream* os) const {
2361676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      *os << "is mapped by the given callable to a value that ";
2362e35fdd936d133bf8a48de140a3c666897588a05shiqian      matcher_.DescribeTo(os);
2363e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
2364e35fdd936d133bf8a48de140a3c666897588a05shiqian
2365e35fdd936d133bf8a48de140a3c666897588a05shiqian    virtual void DescribeNegationTo(::std::ostream* os) const {
2366676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      *os << "is mapped by the given callable to a value that ";
2367e35fdd936d133bf8a48de140a3c666897588a05shiqian      matcher_.DescribeNegationTo(os);
2368e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
2369e35fdd936d133bf8a48de140a3c666897588a05shiqian
2370821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    virtual bool MatchAndExplain(T obj, MatchResultListener* listener) const {
2371676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      *listener << "which is mapped by the given callable to ";
2372676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      // Cannot pass the return value (for example, int) to
2373676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      // MatchPrintAndExplain, which takes a non-const reference as argument.
2374676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      ResultType result =
2375676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan          CallableTraits<Callable>::template Invoke<T>(callable_, obj);
2376676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      return MatchPrintAndExplain(result, matcher_, listener);
2377e35fdd936d133bf8a48de140a3c666897588a05shiqian    }
237832de5f53763125925e078498250f7e73a88de9edzhanyong.wan
2379e35fdd936d133bf8a48de140a3c666897588a05shiqian   private:
2380e35fdd936d133bf8a48de140a3c666897588a05shiqian    // Functors often define operator() as non-const method even though
2381e35fdd936d133bf8a48de140a3c666897588a05shiqian    // they are actualy stateless. But we need to use them even when
2382e35fdd936d133bf8a48de140a3c666897588a05shiqian    // 'this' is a const pointer. It's the user's responsibility not to
2383e35fdd936d133bf8a48de140a3c666897588a05shiqian    // use stateful callables with ResultOf(), which does't guarantee
2384e35fdd936d133bf8a48de140a3c666897588a05shiqian    // how many times the callable will be invoked.
2385e35fdd936d133bf8a48de140a3c666897588a05shiqian    mutable CallableStorageType callable_;
2386e35fdd936d133bf8a48de140a3c666897588a05shiqian    const Matcher<ResultType> matcher_;
238732de5f53763125925e078498250f7e73a88de9edzhanyong.wan
238832de5f53763125925e078498250f7e73a88de9edzhanyong.wan    GTEST_DISALLOW_ASSIGN_(Impl);
2389e35fdd936d133bf8a48de140a3c666897588a05shiqian  };  // class Impl
2390e35fdd936d133bf8a48de140a3c666897588a05shiqian
2391e35fdd936d133bf8a48de140a3c666897588a05shiqian  const CallableStorageType callable_;
2392e35fdd936d133bf8a48de140a3c666897588a05shiqian  const Matcher<ResultType> matcher_;
239332de5f53763125925e078498250f7e73a88de9edzhanyong.wan
239432de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(ResultOfMatcher);
2395e35fdd936d133bf8a48de140a3c666897588a05shiqian};
2396e35fdd936d133bf8a48de140a3c666897588a05shiqian
2397a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan// Implements a matcher that checks the size of an STL-style container.
2398a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wantemplate <typename SizeMatcher>
2399a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wanclass SizeIsMatcher {
2400a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan public:
2401a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan  explicit SizeIsMatcher(const SizeMatcher& size_matcher)
2402a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan       : size_matcher_(size_matcher) {
2403a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan  }
2404a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan
2405a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan  template <typename Container>
2406a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan  operator Matcher<Container>() const {
2407a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan    return MakeMatcher(new Impl<Container>(size_matcher_));
2408a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan  }
2409a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan
2410a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan  template <typename Container>
2411a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan  class Impl : public MatcherInterface<Container> {
2412a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan   public:
2413a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan    typedef internal::StlContainerView<
2414a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan         GTEST_REMOVE_REFERENCE_AND_CONST_(Container)> ContainerView;
2415a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan    typedef typename ContainerView::type::size_type SizeType;
2416a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan    explicit Impl(const SizeMatcher& size_matcher)
2417a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan        : size_matcher_(MatcherCast<SizeType>(size_matcher)) {}
2418a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan
2419a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan    virtual void DescribeTo(::std::ostream* os) const {
2420a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan      *os << "size ";
2421a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan      size_matcher_.DescribeTo(os);
2422a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan    }
2423a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan    virtual void DescribeNegationTo(::std::ostream* os) const {
2424a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan      *os << "size ";
2425a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan      size_matcher_.DescribeNegationTo(os);
2426a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan    }
2427a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan
2428a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan    virtual bool MatchAndExplain(Container container,
2429a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan                                 MatchResultListener* listener) const {
2430a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan      SizeType size = container.size();
2431a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan      StringMatchResultListener size_listener;
2432a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan      const bool result = size_matcher_.MatchAndExplain(size, &size_listener);
2433a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan      *listener
2434a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan          << "whose size " << size << (result ? " matches" : " doesn't match");
2435a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan      PrintIfNotEmpty(size_listener.str(), listener->stream());
2436a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan      return result;
2437a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan    }
2438a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan
2439a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan   private:
2440a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan    const Matcher<SizeType> size_matcher_;
2441a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan    GTEST_DISALLOW_ASSIGN_(Impl);
2442a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan  };
2443a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan
2444a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan private:
2445a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan  const SizeMatcher size_matcher_;
2446a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan  GTEST_DISALLOW_ASSIGN_(SizeIsMatcher);
2447a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan};
2448a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan
2449b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak// Implements a matcher that checks the begin()..end() distance of an STL-style
2450b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak// container.
2451b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosaktemplate <typename DistanceMatcher>
2452b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosakclass BeginEndDistanceIsMatcher {
2453b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak public:
2454b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak  explicit BeginEndDistanceIsMatcher(const DistanceMatcher& distance_matcher)
2455b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak      : distance_matcher_(distance_matcher) {}
2456b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak
2457b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak  template <typename Container>
2458b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak  operator Matcher<Container>() const {
2459b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak    return MakeMatcher(new Impl<Container>(distance_matcher_));
2460b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak  }
2461b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak
2462b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak  template <typename Container>
2463b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak  class Impl : public MatcherInterface<Container> {
2464b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak   public:
2465b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak    typedef internal::StlContainerView<
2466b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak        GTEST_REMOVE_REFERENCE_AND_CONST_(Container)> ContainerView;
2467b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak    typedef typename std::iterator_traits<
2468b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak        typename ContainerView::type::const_iterator>::difference_type
2469b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak        DistanceType;
2470b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak    explicit Impl(const DistanceMatcher& distance_matcher)
2471b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak        : distance_matcher_(MatcherCast<DistanceType>(distance_matcher)) {}
2472b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak
2473b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak    virtual void DescribeTo(::std::ostream* os) const {
2474b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak      *os << "distance between begin() and end() ";
2475b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak      distance_matcher_.DescribeTo(os);
2476b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak    }
2477b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak    virtual void DescribeNegationTo(::std::ostream* os) const {
2478b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak      *os << "distance between begin() and end() ";
2479b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak      distance_matcher_.DescribeNegationTo(os);
2480b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak    }
2481b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak
2482b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak    virtual bool MatchAndExplain(Container container,
2483b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak                                 MatchResultListener* listener) const {
24845b9cbbb16d774bfcb51572d52eda3f7319088e23kosak#if GTEST_HAS_STD_BEGIN_AND_END_
2485b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak      using std::begin;
2486b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak      using std::end;
2487b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak      DistanceType distance = std::distance(begin(container), end(container));
2488b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak#else
2489b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak      DistanceType distance = std::distance(container.begin(), container.end());
2490b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak#endif
2491b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak      StringMatchResultListener distance_listener;
2492b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak      const bool result =
2493b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak          distance_matcher_.MatchAndExplain(distance, &distance_listener);
2494b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak      *listener << "whose distance between begin() and end() " << distance
2495b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak                << (result ? " matches" : " doesn't match");
2496b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak      PrintIfNotEmpty(distance_listener.str(), listener->stream());
2497b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak      return result;
2498b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak    }
2499b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak
2500b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak   private:
2501b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak    const Matcher<DistanceType> distance_matcher_;
2502b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak    GTEST_DISALLOW_ASSIGN_(Impl);
2503b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak  };
2504b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak
2505b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak private:
2506b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak  const DistanceMatcher distance_matcher_;
2507b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak  GTEST_DISALLOW_ASSIGN_(BeginEndDistanceIsMatcher);
2508b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak};
2509b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak
25106a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// Implements an equality matcher for any STL-style container whose elements
25116a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// support ==. This matcher is like Eq(), but its failure explanations provide
25126a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// more detailed information that is useful when the container is used as a set.
25136a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// The failure message reports elements that are in one of the operands but not
25146a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// the other. The failure messages do not report duplicate or out-of-order
25156a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// elements in the containers (which don't properly matter to sets, but can
25166a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// occur if the containers are vectors or lists, for example).
25176a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan//
25186a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// Uses the container's const_iterator, value_type, operator ==,
25196a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// begin(), and end().
25206a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wantemplate <typename Container>
25216a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wanclass ContainerEqMatcher {
25226a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan public:
2523b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef internal::StlContainerView<Container> View;
2524b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef typename View::type StlContainer;
2525b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef typename View::const_reference StlContainerReference;
2526b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
25276b817803104b30d5951c652a1427492db116a490kosak  // We make a copy of expected in case the elements in it are modified
2528b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  // after this matcher is created.
25296b817803104b30d5951c652a1427492db116a490kosak  explicit ContainerEqMatcher(const Container& expected)
25306b817803104b30d5951c652a1427492db116a490kosak      : expected_(View::Copy(expected)) {
2531b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    // Makes sure the user doesn't instantiate this class template
2532b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    // with a const or reference type.
2533ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    (void)testing::StaticAssertTypeEq<Container,
2534ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        GTEST_REMOVE_REFERENCE_AND_CONST_(Container)>();
2535b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  }
2536b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
25376a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan  void DescribeTo(::std::ostream* os) const {
25386a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan    *os << "equals ";
25396b817803104b30d5951c652a1427492db116a490kosak    UniversalPrint(expected_, os);
25406a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan  }
25416a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan  void DescribeNegationTo(::std::ostream* os) const {
25426a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan    *os << "does not equal ";
25436b817803104b30d5951c652a1427492db116a490kosak    UniversalPrint(expected_, os);
25446a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan  }
25456a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan
2546b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  template <typename LhsContainer>
2547e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan  bool MatchAndExplain(const LhsContainer& lhs,
2548e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan                       MatchResultListener* listener) const {
254902f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan    // GTEST_REMOVE_CONST_() is needed to work around an MSVC 8.0 bug
2550b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    // that causes LhsContainer to be a const type sometimes.
255102f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan    typedef internal::StlContainerView<GTEST_REMOVE_CONST_(LhsContainer)>
2552b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan        LhsView;
2553b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    typedef typename LhsView::type LhsStlContainer;
2554b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    StlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
25556b817803104b30d5951c652a1427492db116a490kosak    if (lhs_stl_container == expected_)
2556e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan      return true;
2557b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
2558e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan    ::std::ostream* const os = listener->stream();
2559e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan    if (os != NULL) {
2560b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan      // Something is different. Check for extra values first.
2561e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan      bool printed_header = false;
2562e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan      for (typename LhsStlContainer::const_iterator it =
2563e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan               lhs_stl_container.begin();
2564e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan           it != lhs_stl_container.end(); ++it) {
25656b817803104b30d5951c652a1427492db116a490kosak        if (internal::ArrayAwareFind(expected_.begin(), expected_.end(), *it) ==
25666b817803104b30d5951c652a1427492db116a490kosak            expected_.end()) {
2567e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan          if (printed_header) {
2568e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan            *os << ", ";
2569e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan          } else {
2570b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan            *os << "which has these unexpected elements: ";
2571e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan            printed_header = true;
2572e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan          }
2573e2e8ba401d198d1a8304c652e997505609b62696vladlosev          UniversalPrint(*it, os);
25746a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan        }
25756a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan      }
25766a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan
2577b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan      // Now check for missing values.
2578e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan      bool printed_header2 = false;
25796b817803104b30d5951c652a1427492db116a490kosak      for (typename StlContainer::const_iterator it = expected_.begin();
25806b817803104b30d5951c652a1427492db116a490kosak           it != expected_.end(); ++it) {
2581e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan        if (internal::ArrayAwareFind(
2582e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan                lhs_stl_container.begin(), lhs_stl_container.end(), *it) ==
2583e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan            lhs_stl_container.end()) {
2584e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan          if (printed_header2) {
2585e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan            *os << ", ";
2586e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan          } else {
2587b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan            *os << (printed_header ? ",\nand" : "which")
2588b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan                << " doesn't have these expected elements: ";
2589e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan            printed_header2 = true;
2590e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan          }
2591e2e8ba401d198d1a8304c652e997505609b62696vladlosev          UniversalPrint(*it, os);
25926a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan        }
25936a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan      }
25946a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan    }
2595e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan
2596e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan    return false;
25976a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan  }
259832de5f53763125925e078498250f7e73a88de9edzhanyong.wan
25996a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan private:
26006b817803104b30d5951c652a1427492db116a490kosak  const StlContainer expected_;
260132de5f53763125925e078498250f7e73a88de9edzhanyong.wan
260232de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(ContainerEqMatcher);
26036a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan};
26046a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan
2605898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan// A comparator functor that uses the < operator to compare two values.
2606898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wanstruct LessComparator {
2607898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan  template <typename T, typename U>
2608898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan  bool operator()(const T& lhs, const U& rhs) const { return lhs < rhs; }
2609898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan};
2610898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
2611898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan// Implements WhenSortedBy(comparator, container_matcher).
2612898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wantemplate <typename Comparator, typename ContainerMatcher>
2613898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wanclass WhenSortedByMatcher {
2614898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan public:
2615898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan  WhenSortedByMatcher(const Comparator& comparator,
2616898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan                      const ContainerMatcher& matcher)
2617898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      : comparator_(comparator), matcher_(matcher) {}
2618898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
2619898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan  template <typename LhsContainer>
2620898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan  operator Matcher<LhsContainer>() const {
2621898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan    return MakeMatcher(new Impl<LhsContainer>(comparator_, matcher_));
2622898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan  }
2623898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
2624898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan  template <typename LhsContainer>
2625898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan  class Impl : public MatcherInterface<LhsContainer> {
2626898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan   public:
2627898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan    typedef internal::StlContainerView<
2628898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan         GTEST_REMOVE_REFERENCE_AND_CONST_(LhsContainer)> LhsView;
2629898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan    typedef typename LhsView::type LhsStlContainer;
2630898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan    typedef typename LhsView::const_reference LhsStlContainerReference;
2631a9a59e06dd7cdfe52c988bf065bc156a7ed96a5czhanyong.wan    // Transforms std::pair<const Key, Value> into std::pair<Key, Value>
2632a9a59e06dd7cdfe52c988bf065bc156a7ed96a5czhanyong.wan    // so that we can match associative containers.
2633a9a59e06dd7cdfe52c988bf065bc156a7ed96a5czhanyong.wan    typedef typename RemoveConstFromKey<
2634a9a59e06dd7cdfe52c988bf065bc156a7ed96a5czhanyong.wan        typename LhsStlContainer::value_type>::type LhsValue;
2635898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
2636898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan    Impl(const Comparator& comparator, const ContainerMatcher& matcher)
2637898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan        : comparator_(comparator), matcher_(matcher) {}
2638898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
2639898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan    virtual void DescribeTo(::std::ostream* os) const {
2640898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      *os << "(when sorted) ";
2641898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      matcher_.DescribeTo(os);
2642898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan    }
2643898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
2644898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan    virtual void DescribeNegationTo(::std::ostream* os) const {
2645898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      *os << "(when sorted) ";
2646898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      matcher_.DescribeNegationTo(os);
2647898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan    }
2648898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
2649898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan    virtual bool MatchAndExplain(LhsContainer lhs,
2650898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan                                 MatchResultListener* listener) const {
2651898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2652fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan      ::std::vector<LhsValue> sorted_container(lhs_stl_container.begin(),
2653fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan                                               lhs_stl_container.end());
2654fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan      ::std::sort(
2655fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan           sorted_container.begin(), sorted_container.end(), comparator_);
2656898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
2657898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      if (!listener->IsInterested()) {
2658898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan        // If the listener is not interested, we do not need to
2659898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan        // construct the inner explanation.
2660898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan        return matcher_.Matches(sorted_container);
2661898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      }
2662898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
2663898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      *listener << "which is ";
2664898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      UniversalPrint(sorted_container, listener->stream());
2665898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      *listener << " when sorted";
2666898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
2667898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      StringMatchResultListener inner_listener;
2668898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      const bool match = matcher_.MatchAndExplain(sorted_container,
2669898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan                                                  &inner_listener);
2670898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      PrintIfNotEmpty(inner_listener.str(), listener->stream());
2671898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      return match;
2672898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan    }
2673898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
2674898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan   private:
2675898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan    const Comparator comparator_;
2676fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    const Matcher<const ::std::vector<LhsValue>&> matcher_;
2677898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
2678898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan    GTEST_DISALLOW_COPY_AND_ASSIGN_(Impl);
2679898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan  };
2680898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
2681898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan private:
2682898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan  const Comparator comparator_;
2683898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan  const ContainerMatcher matcher_;
2684898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
2685898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan  GTEST_DISALLOW_ASSIGN_(WhenSortedByMatcher);
2686898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan};
2687898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
2688ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// Implements Pointwise(tuple_matcher, rhs_container).  tuple_matcher
2689ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// must be able to be safely cast to Matcher<tuple<const T1&, const
2690ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// T2&> >, where T1 and T2 are the types of elements in the LHS
2691ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// container and the RHS container respectively.
2692ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wantemplate <typename TupleMatcher, typename RhsContainer>
2693ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wanclass PointwiseMatcher {
2694ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan public:
2695ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  typedef internal::StlContainerView<RhsContainer> RhsView;
2696ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  typedef typename RhsView::type RhsStlContainer;
2697ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  typedef typename RhsStlContainer::value_type RhsValue;
2698ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2699ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  // Like ContainerEq, we make a copy of rhs in case the elements in
2700ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  // it are modified after this matcher is created.
2701ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  PointwiseMatcher(const TupleMatcher& tuple_matcher, const RhsContainer& rhs)
2702ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      : tuple_matcher_(tuple_matcher), rhs_(RhsView::Copy(rhs)) {
2703ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    // Makes sure the user doesn't instantiate this class template
2704ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    // with a const or reference type.
2705ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    (void)testing::StaticAssertTypeEq<RhsContainer,
2706ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        GTEST_REMOVE_REFERENCE_AND_CONST_(RhsContainer)>();
2707ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  }
2708ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2709ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  template <typename LhsContainer>
2710ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  operator Matcher<LhsContainer>() const {
2711ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    return MakeMatcher(new Impl<LhsContainer>(tuple_matcher_, rhs_));
2712ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  }
2713ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2714ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  template <typename LhsContainer>
2715ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  class Impl : public MatcherInterface<LhsContainer> {
2716ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan   public:
2717ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    typedef internal::StlContainerView<
2718ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan         GTEST_REMOVE_REFERENCE_AND_CONST_(LhsContainer)> LhsView;
2719ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    typedef typename LhsView::type LhsStlContainer;
2720ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    typedef typename LhsView::const_reference LhsStlContainerReference;
2721ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    typedef typename LhsStlContainer::value_type LhsValue;
2722ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    // We pass the LHS value and the RHS value to the inner matcher by
2723ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    // reference, as they may be expensive to copy.  We must use tuple
2724ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    // instead of pair here, as a pair cannot hold references (C++ 98,
2725ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    // 20.2.2 [lib.pairs]).
2726bd0188320de5aab1b09718e2c466387099d43cfbkosak    typedef ::testing::tuple<const LhsValue&, const RhsValue&> InnerMatcherArg;
2727ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2728ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    Impl(const TupleMatcher& tuple_matcher, const RhsStlContainer& rhs)
2729ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        // mono_tuple_matcher_ holds a monomorphic version of the tuple matcher.
2730ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        : mono_tuple_matcher_(SafeMatcherCast<InnerMatcherArg>(tuple_matcher)),
2731ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan          rhs_(rhs) {}
2732ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2733ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    virtual void DescribeTo(::std::ostream* os) const {
2734ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      *os << "contains " << rhs_.size()
2735ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan          << " values, where each value and its corresponding value in ";
2736ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      UniversalPrinter<RhsStlContainer>::Print(rhs_, os);
2737ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      *os << " ";
2738ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      mono_tuple_matcher_.DescribeTo(os);
2739ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    }
2740ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    virtual void DescribeNegationTo(::std::ostream* os) const {
2741ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      *os << "doesn't contain exactly " << rhs_.size()
2742ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan          << " values, or contains a value x at some index i"
2743ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan          << " where x and the i-th value of ";
2744ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      UniversalPrint(rhs_, os);
2745ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      *os << " ";
2746ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      mono_tuple_matcher_.DescribeNegationTo(os);
2747ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    }
2748ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2749ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    virtual bool MatchAndExplain(LhsContainer lhs,
2750ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan                                 MatchResultListener* listener) const {
2751ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs);
2752ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      const size_t actual_size = lhs_stl_container.size();
2753ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      if (actual_size != rhs_.size()) {
2754ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        *listener << "which contains " << actual_size << " values";
2755ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        return false;
2756ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      }
2757ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2758ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      typename LhsStlContainer::const_iterator left = lhs_stl_container.begin();
2759ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      typename RhsStlContainer::const_iterator right = rhs_.begin();
2760ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      for (size_t i = 0; i != actual_size; ++i, ++left, ++right) {
2761ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        const InnerMatcherArg value_pair(*left, *right);
2762ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2763ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        if (listener->IsInterested()) {
2764ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan          StringMatchResultListener inner_listener;
2765ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan          if (!mono_tuple_matcher_.MatchAndExplain(
2766ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan                  value_pair, &inner_listener)) {
2767ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan            *listener << "where the value pair (";
2768ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan            UniversalPrint(*left, listener->stream());
2769ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan            *listener << ", ";
2770ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan            UniversalPrint(*right, listener->stream());
2771ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan            *listener << ") at index #" << i << " don't match";
2772ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan            PrintIfNotEmpty(inner_listener.str(), listener->stream());
2773ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan            return false;
2774ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan          }
2775ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        } else {
2776ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan          if (!mono_tuple_matcher_.Matches(value_pair))
2777ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan            return false;
2778ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan        }
2779ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      }
2780ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2781ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      return true;
2782ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    }
2783ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2784ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan   private:
2785ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    const Matcher<InnerMatcherArg> mono_tuple_matcher_;
2786ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    const RhsStlContainer rhs_;
2787ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2788ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    GTEST_DISALLOW_ASSIGN_(Impl);
2789ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  };
2790ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2791ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan private:
2792ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  const TupleMatcher tuple_matcher_;
2793ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  const RhsStlContainer rhs_;
2794ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
2795ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  GTEST_DISALLOW_ASSIGN_(PointwiseMatcher);
2796ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan};
2797ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
279833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Holds the logic common to ContainsMatcherImpl and EachMatcherImpl.
2799b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename Container>
280033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wanclass QuantifierMatcherImpl : public MatcherInterface<Container> {
2801b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan public:
2802ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
2803b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef StlContainerView<RawContainer> View;
2804b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef typename View::type StlContainer;
2805b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef typename View::const_reference StlContainerReference;
2806b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  typedef typename StlContainer::value_type Element;
2807b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
2808b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  template <typename InnerMatcher>
280933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  explicit QuantifierMatcherImpl(InnerMatcher inner_matcher)
2810b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan      : inner_matcher_(
281133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan           testing::SafeMatcherCast<const Element&>(inner_matcher)) {}
281233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
281333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  // Checks whether:
281433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  // * All elements in the container match, if all_elements_should_match.
281533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  // * Any element in the container matches, if !all_elements_should_match.
281633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  bool MatchAndExplainImpl(bool all_elements_should_match,
281733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan                           Container container,
281833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan                           MatchResultListener* listener) const {
281933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    StlContainerReference stl_container = View::ConstReference(container);
282033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    size_t i = 0;
282133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    for (typename StlContainer::const_iterator it = stl_container.begin();
282233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan         it != stl_container.end(); ++it, ++i) {
282333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan      StringMatchResultListener inner_listener;
282433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan      const bool matches = inner_matcher_.MatchAndExplain(*it, &inner_listener);
282533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
282633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan      if (matches != all_elements_should_match) {
282733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan        *listener << "whose element #" << i
282833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan                  << (matches ? " matches" : " doesn't match");
282933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan        PrintIfNotEmpty(inner_listener.str(), listener->stream());
283033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan        return !all_elements_should_match;
283133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan      }
283233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    }
283333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    return all_elements_should_match;
283433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  }
283533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
283633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan protected:
283733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  const Matcher<const Element&> inner_matcher_;
283833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
283933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  GTEST_DISALLOW_ASSIGN_(QuantifierMatcherImpl);
284033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan};
284133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
284233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Implements Contains(element_matcher) for the given argument type Container.
284333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Symmetric to EachMatcherImpl.
284433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wantemplate <typename Container>
284533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wanclass ContainsMatcherImpl : public QuantifierMatcherImpl<Container> {
284633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan public:
284733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  template <typename InnerMatcher>
284833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  explicit ContainsMatcherImpl(InnerMatcher inner_matcher)
284933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan      : QuantifierMatcherImpl<Container>(inner_matcher) {}
2850b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
2851b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  // Describes what this matcher does.
2852b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
2853b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    *os << "contains at least one element that ";
285433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    this->inner_matcher_.DescribeTo(os);
2855b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  }
2856b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
2857b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
2858b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    *os << "doesn't contain any element that ";
285933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    this->inner_matcher_.DescribeTo(os);
2860b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  }
2861b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
2862821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  virtual bool MatchAndExplain(Container container,
2863821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan                               MatchResultListener* listener) const {
286433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    return this->MatchAndExplainImpl(false, container, listener);
2865b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  }
2866b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
2867b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan private:
286832de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(ContainsMatcherImpl);
2869b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan};
2870b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
287133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Implements Each(element_matcher) for the given argument type Container.
287233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Symmetric to ContainsMatcherImpl.
287333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wantemplate <typename Container>
287433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wanclass EachMatcherImpl : public QuantifierMatcherImpl<Container> {
287533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan public:
287633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  template <typename InnerMatcher>
287733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  explicit EachMatcherImpl(InnerMatcher inner_matcher)
287833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan      : QuantifierMatcherImpl<Container>(inner_matcher) {}
287933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
288033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  // Describes what this matcher does.
288133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
288233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    *os << "only contains elements that ";
288333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    this->inner_matcher_.DescribeTo(os);
288433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  }
288533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
288633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
288733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    *os << "contains some element that ";
288833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    this->inner_matcher_.DescribeNegationTo(os);
288933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  }
289033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
289133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  virtual bool MatchAndExplain(Container container,
289233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan                               MatchResultListener* listener) const {
289333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    return this->MatchAndExplainImpl(true, container, listener);
289433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  }
289533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
289633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan private:
289733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  GTEST_DISALLOW_ASSIGN_(EachMatcherImpl);
289833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan};
289933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
2900b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Implements polymorphic Contains(element_matcher).
2901b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename M>
2902b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanclass ContainsMatcher {
2903b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan public:
2904b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  explicit ContainsMatcher(M m) : inner_matcher_(m) {}
2905b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
2906b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  template <typename Container>
2907b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  operator Matcher<Container>() const {
2908b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan    return MakeMatcher(new ContainsMatcherImpl<Container>(inner_matcher_));
2909b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  }
2910b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
2911b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan private:
2912b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  const M inner_matcher_;
291332de5f53763125925e078498250f7e73a88de9edzhanyong.wan
291432de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(ContainsMatcher);
2915b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan};
2916b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
291733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Implements polymorphic Each(element_matcher).
291833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wantemplate <typename M>
291933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wanclass EachMatcher {
292033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan public:
292133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  explicit EachMatcher(M m) : inner_matcher_(m) {}
292233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
292333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  template <typename Container>
292433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  operator Matcher<Container>() const {
292533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan    return MakeMatcher(new EachMatcherImpl<Container>(inner_matcher_));
292633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  }
292733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
292833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan private:
292933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  const M inner_matcher_;
293033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
293133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  GTEST_DISALLOW_ASSIGN_(EachMatcher);
293233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan};
293333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
2934b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// Implements Key(inner_matcher) for the given argument pair type.
2935b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// Key(inner_matcher) matches an std::pair whose 'first' field matches
2936b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// inner_matcher.  For example, Contains(Key(Ge(5))) can be used to match an
2937b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// std::map that contains at least one element whose key is >= 5.
2938b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wantemplate <typename PairType>
2939b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wanclass KeyMatcherImpl : public MatcherInterface<PairType> {
2940b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan public:
2941ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  typedef GTEST_REMOVE_REFERENCE_AND_CONST_(PairType) RawPairType;
2942b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  typedef typename RawPairType::first_type KeyType;
2943b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
2944b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  template <typename InnerMatcher>
2945b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  explicit KeyMatcherImpl(InnerMatcher inner_matcher)
2946b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan      : inner_matcher_(
2947b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan          testing::SafeMatcherCast<const KeyType&>(inner_matcher)) {
2948b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  }
2949b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
2950b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  // Returns true iff 'key_value.first' (the key) matches the inner matcher.
2951821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  virtual bool MatchAndExplain(PairType key_value,
2952821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan                               MatchResultListener* listener) const {
2953b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    StringMatchResultListener inner_listener;
2954b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    const bool match = inner_matcher_.MatchAndExplain(key_value.first,
2955b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan                                                      &inner_listener);
2956b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    const internal::string explanation = inner_listener.str();
2957b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    if (explanation != "") {
2958b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan      *listener << "whose first field is a value " << explanation;
2959b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    }
2960b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    return match;
2961b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  }
2962b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
2963b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  // Describes what this matcher does.
2964b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
2965b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan    *os << "has a key that ";
2966b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan    inner_matcher_.DescribeTo(os);
2967b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  }
2968b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
2969b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  // Describes what the negation of this matcher does.
2970b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
2971b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan    *os << "doesn't have a key that ";
2972b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan    inner_matcher_.DescribeTo(os);
2973b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  }
2974b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
2975b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan private:
2976b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  const Matcher<const KeyType&> inner_matcher_;
297732de5f53763125925e078498250f7e73a88de9edzhanyong.wan
297832de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(KeyMatcherImpl);
2979b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan};
2980b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
2981b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// Implements polymorphic Key(matcher_for_key).
2982b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wantemplate <typename M>
2983b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wanclass KeyMatcher {
2984b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan public:
2985b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  explicit KeyMatcher(M m) : matcher_for_key_(m) {}
2986b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
2987b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  template <typename PairType>
2988b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  operator Matcher<PairType>() const {
2989b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan    return MakeMatcher(new KeyMatcherImpl<PairType>(matcher_for_key_));
2990b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  }
2991b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
2992b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan private:
2993b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  const M matcher_for_key_;
299432de5f53763125925e078498250f7e73a88de9edzhanyong.wan
299532de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(KeyMatcher);
2996b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan};
2997b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
2998f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// Implements Pair(first_matcher, second_matcher) for the given argument pair
2999f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// type with its two matchers. See Pair() function below.
3000f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wantemplate <typename PairType>
3001f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wanclass PairMatcherImpl : public MatcherInterface<PairType> {
3002f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan public:
3003ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  typedef GTEST_REMOVE_REFERENCE_AND_CONST_(PairType) RawPairType;
3004f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  typedef typename RawPairType::first_type FirstType;
3005f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  typedef typename RawPairType::second_type SecondType;
3006f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
3007f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  template <typename FirstMatcher, typename SecondMatcher>
3008f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  PairMatcherImpl(FirstMatcher first_matcher, SecondMatcher second_matcher)
3009f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan      : first_matcher_(
3010f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan            testing::SafeMatcherCast<const FirstType&>(first_matcher)),
3011f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan        second_matcher_(
3012f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan            testing::SafeMatcherCast<const SecondType&>(second_matcher)) {
3013f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  }
3014f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
3015f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  // Describes what this matcher does.
3016f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
3017f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    *os << "has a first field that ";
3018f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    first_matcher_.DescribeTo(os);
3019f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    *os << ", and has a second field that ";
3020f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    second_matcher_.DescribeTo(os);
3021f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  }
3022f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
3023f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  // Describes what the negation of this matcher does.
3024f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
3025f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    *os << "has a first field that ";
3026f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    first_matcher_.DescribeNegationTo(os);
3027f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    *os << ", or has a second field that ";
3028f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    second_matcher_.DescribeNegationTo(os);
3029f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  }
3030f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
3031821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // Returns true iff 'a_pair.first' matches first_matcher and 'a_pair.second'
3032821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  // matches second_matcher.
3033821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  virtual bool MatchAndExplain(PairType a_pair,
3034821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan                               MatchResultListener* listener) const {
3035676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    if (!listener->IsInterested()) {
3036676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      // If the listener is not interested, we don't need to construct the
3037676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      // explanation.
3038676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      return first_matcher_.Matches(a_pair.first) &&
3039676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan             second_matcher_.Matches(a_pair.second);
3040821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    }
3041676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    StringMatchResultListener first_inner_listener;
3042676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    if (!first_matcher_.MatchAndExplain(a_pair.first,
3043676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan                                        &first_inner_listener)) {
3044676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      *listener << "whose first field does not match";
3045b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan      PrintIfNotEmpty(first_inner_listener.str(), listener->stream());
3046821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      return false;
3047f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    }
3048676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    StringMatchResultListener second_inner_listener;
3049676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    if (!second_matcher_.MatchAndExplain(a_pair.second,
3050676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan                                         &second_inner_listener)) {
3051676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      *listener << "whose second field does not match";
3052b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan      PrintIfNotEmpty(second_inner_listener.str(), listener->stream());
3053821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      return false;
3054f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    }
3055676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    ExplainSuccess(first_inner_listener.str(), second_inner_listener.str(),
3056676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan                   listener);
3057821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    return true;
3058f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  }
3059f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
3060f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan private:
3061676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan  void ExplainSuccess(const internal::string& first_explanation,
3062676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan                      const internal::string& second_explanation,
3063676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan                      MatchResultListener* listener) const {
3064676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    *listener << "whose both fields match";
3065676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    if (first_explanation != "") {
3066676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      *listener << ", where the first field is a value " << first_explanation;
3067676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    }
3068676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    if (second_explanation != "") {
3069676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      *listener << ", ";
3070676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      if (first_explanation != "") {
3071676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan        *listener << "and ";
3072676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      } else {
3073676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan        *listener << "where ";
3074676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      }
3075676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan      *listener << "the second field is a value " << second_explanation;
3076676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan    }
3077676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan  }
3078676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan
3079f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  const Matcher<const FirstType&> first_matcher_;
3080f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  const Matcher<const SecondType&> second_matcher_;
308132de5f53763125925e078498250f7e73a88de9edzhanyong.wan
308232de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(PairMatcherImpl);
3083f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan};
3084f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
3085f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// Implements polymorphic Pair(first_matcher, second_matcher).
3086f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wantemplate <typename FirstMatcher, typename SecondMatcher>
3087f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wanclass PairMatcher {
3088f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan public:
3089f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  PairMatcher(FirstMatcher first_matcher, SecondMatcher second_matcher)
3090f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan      : first_matcher_(first_matcher), second_matcher_(second_matcher) {}
3091f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
3092f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  template <typename PairType>
3093f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  operator Matcher<PairType> () const {
3094f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan    return MakeMatcher(
3095f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan        new PairMatcherImpl<PairType>(
3096f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan            first_matcher_, second_matcher_));
3097f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  }
3098f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
3099f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan private:
3100f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  const FirstMatcher first_matcher_;
3101f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  const SecondMatcher second_matcher_;
310232de5f53763125925e078498250f7e73a88de9edzhanyong.wan
310332de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(PairMatcher);
3104f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan};
3105f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
31061afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// Implements ElementsAre() and ElementsAreArray().
31071afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wantemplate <typename Container>
31081afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wanclass ElementsAreMatcherImpl : public MatcherInterface<Container> {
31091afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan public:
3110ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
31111afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  typedef internal::StlContainerView<RawContainer> View;
31121afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  typedef typename View::type StlContainer;
31131afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  typedef typename View::const_reference StlContainerReference;
31141afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  typedef typename StlContainer::value_type Element;
31151afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
31161afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  // Constructs the matcher from a sequence of element values or
31171afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  // element matchers.
31181afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  template <typename InputIter>
311938513a8bb154f0b6d0a4088814fe92552696d465jgm  ElementsAreMatcherImpl(InputIter first, InputIter last) {
312038513a8bb154f0b6d0a4088814fe92552696d465jgm    while (first != last) {
312138513a8bb154f0b6d0a4088814fe92552696d465jgm      matchers_.push_back(MatcherCast<const Element&>(*first++));
31221afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    }
31231afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  }
31241afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
31251afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  // Describes what this matcher does.
31261afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
31271afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    if (count() == 0) {
31281afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      *os << "is empty";
31291afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    } else if (count() == 1) {
31301afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      *os << "has 1 element that ";
31311afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      matchers_[0].DescribeTo(os);
31321afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    } else {
31331afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      *os << "has " << Elements(count()) << " where\n";
31341afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      for (size_t i = 0; i != count(); ++i) {
3135b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan        *os << "element #" << i << " ";
31361afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        matchers_[i].DescribeTo(os);
31371afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        if (i + 1 < count()) {
31381afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan          *os << ",\n";
31391afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        }
31401afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      }
31411afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    }
31421afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  }
31431afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
31441afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  // Describes what the negation of this matcher does.
31451afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
31461afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    if (count() == 0) {
3147b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan      *os << "isn't empty";
31481afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      return;
31491afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    }
31501afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
3151b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan    *os << "doesn't have " << Elements(count()) << ", or\n";
31521afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    for (size_t i = 0; i != count(); ++i) {
3153b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan      *os << "element #" << i << " ";
31541afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      matchers_[i].DescribeNegationTo(os);
31551afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      if (i + 1 < count()) {
31561afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        *os << ", or\n";
31571afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      }
31581afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    }
31591afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  }
31601afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
3161821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  virtual bool MatchAndExplain(Container container,
3162821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan                               MatchResultListener* listener) const {
31631cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan    // To work with stream-like "containers", we must only walk
31641cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan    // through the elements in one pass.
31651cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan
31661cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan    const bool listener_interested = listener->IsInterested();
31671cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan
31681cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan    // explanations[i] is the explanation of the element at index i.
31691cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan    ::std::vector<internal::string> explanations(count());
31701afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    StlContainerReference stl_container = View::ConstReference(container);
31711cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan    typename StlContainer::const_iterator it = stl_container.begin();
31721cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan    size_t exam_pos = 0;
31731cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan    bool mismatch_found = false;  // Have we found a mismatched element yet?
31741cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan
31751cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan    // Go through the elements and matchers in pairs, until we reach
31761cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan    // the end of either the elements or the matchers, or until we find a
31771cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan    // mismatch.
31781cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan    for (; it != stl_container.end() && exam_pos != count(); ++it, ++exam_pos) {
31791cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan      bool match;  // Does the current element match the current matcher?
31801cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan      if (listener_interested) {
31811cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan        StringMatchResultListener s;
31821cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan        match = matchers_[exam_pos].MatchAndExplain(*it, &s);
31831cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan        explanations[exam_pos] = s.str();
31841cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan      } else {
31851cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan        match = matchers_[exam_pos].Matches(*it);
31861cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan      }
31871cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan
31881cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan      if (!match) {
31891cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan        mismatch_found = true;
31901cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan        break;
31911cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan      }
31921cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan    }
31931cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan    // If mismatch_found is true, 'exam_pos' is the index of the mismatch.
31941cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan
31951cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan    // Find how many elements the actual container has.  We avoid
31961cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan    // calling size() s.t. this code works for stream-like "containers"
31971cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan    // that don't define size().
31981cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan    size_t actual_count = exam_pos;
31991cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan    for (; it != stl_container.end(); ++it) {
32001cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan      ++actual_count;
32011cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan    }
32021cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan
3203821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    if (actual_count != count()) {
3204821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      // The element count doesn't match.  If the container is empty,
3205821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      // there's no need to explain anything as Google Mock already
3206821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      // prints the empty container.  Otherwise we just need to show
3207821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      // how many elements there actually are.
32081cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan      if (listener_interested && (actual_count != 0)) {
3209b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan        *listener << "which has " << Elements(actual_count);
32101afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      }
3211821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      return false;
3212821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    }
32131afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
32141cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan    if (mismatch_found) {
32151cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan      // The element count matches, but the exam_pos-th element doesn't match.
32161cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan      if (listener_interested) {
32171cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan        *listener << "whose element #" << exam_pos << " doesn't match";
32181cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan        PrintIfNotEmpty(explanations[exam_pos], listener->stream());
3219821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      }
32201cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan      return false;
3221821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    }
32221afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
3223821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // Every element matches its expectation.  We need to explain why
3224821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    // (the obvious ones can be skipped).
32251cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan    if (listener_interested) {
32261cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan      bool reason_printed = false;
32271cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan      for (size_t i = 0; i != count(); ++i) {
32281cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan        const internal::string& s = explanations[i];
32291cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan        if (!s.empty()) {
32301cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan          if (reason_printed) {
32311cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan            *listener << ",\nand ";
32321cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan          }
32331cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan          *listener << "whose element #" << i << " matches, " << s;
32341cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan          reason_printed = true;
32351afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan        }
32361afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan      }
32371afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    }
3238821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan    return true;
32391afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  }
32401afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
32411afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan private:
32421afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  static Message Elements(size_t count) {
32431afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan    return Message() << count << (count == 1 ? " element" : " elements");
32441afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  }
32451afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
32461afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  size_t count() const { return matchers_.size(); }
3247fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3248fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  ::std::vector<Matcher<const Element&> > matchers_;
324932de5f53763125925e078498250f7e73a88de9edzhanyong.wan
325032de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(ElementsAreMatcherImpl);
32511afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan};
32521afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
3253fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// Connectivity matrix of (elements X matchers), in element-major order.
3254fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// Initially, there are no edges.
3255fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// Use NextGraph() to iterate over all possible edge configurations.
3256fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// Use Randomize() to generate a random edge configuration.
3257fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanclass GTEST_API_ MatchMatrix {
32581afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan public:
3259fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  MatchMatrix(size_t num_elements, size_t num_matchers)
3260fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan      : num_elements_(num_elements),
3261fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan        num_matchers_(num_matchers),
3262fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan        matched_(num_elements_* num_matchers_, 0) {
3263fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  }
3264fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3265fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  size_t LhsSize() const { return num_elements_; }
3266fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  size_t RhsSize() const { return num_matchers_; }
3267fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  bool HasEdge(size_t ilhs, size_t irhs) const {
3268fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    return matched_[SpaceIndex(ilhs, irhs)] == 1;
3269fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  }
3270fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  void SetEdge(size_t ilhs, size_t irhs, bool b) {
3271fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    matched_[SpaceIndex(ilhs, irhs)] = b ? 1 : 0;
3272fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  }
3273fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3274fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // Treating the connectivity matrix as a (LhsSize()*RhsSize())-bit number,
3275fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // adds 1 to that number; returns false if incrementing the graph left it
3276fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // empty.
3277fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  bool NextGraph();
3278fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3279fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  void Randomize();
3280fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3281fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  string DebugString() const;
3282fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3283fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan private:
3284fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  size_t SpaceIndex(size_t ilhs, size_t irhs) const {
3285fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    return ilhs * num_matchers_ + irhs;
3286fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  }
3287fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3288fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  size_t num_elements_;
3289fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  size_t num_matchers_;
3290fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3291fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // Each element is a char interpreted as bool. They are stored as a
3292fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // flattened array in lhs-major order, use 'SpaceIndex()' to translate
3293fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // a (ilhs, irhs) matrix coordinate into an offset.
3294fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  ::std::vector<char> matched_;
3295fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan};
3296fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3297fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantypedef ::std::pair<size_t, size_t> ElementMatcherPair;
3298fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantypedef ::std::vector<ElementMatcherPair> ElementMatcherPairs;
3299fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3300fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// Returns a maximum bipartite matching for the specified graph 'g'.
3301fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// The matching is represented as a vector of {element, matcher} pairs.
3302fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanGTEST_API_ ElementMatcherPairs
3303fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanFindMaxBipartiteMatching(const MatchMatrix& g);
3304fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3305fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanGTEST_API_ bool FindPairing(const MatchMatrix& matrix,
3306fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan                            MatchResultListener* listener);
3307fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3308fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// Untyped base class for implementing UnorderedElementsAre.  By
3309fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// putting logic that's not specific to the element type here, we
3310fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// reduce binary bloat and increase compilation speed.
3311fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanclass GTEST_API_ UnorderedElementsAreMatcherImplBase {
3312fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan protected:
3313fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // A vector of matcher describers, one for each element matcher.
3314fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // Does not own the describers (and thus can be used only when the
3315fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // element matchers are alive).
3316fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  typedef ::std::vector<const MatcherDescriberInterface*> MatcherDescriberVec;
3317fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3318fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // Describes this UnorderedElementsAre matcher.
3319fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  void DescribeToImpl(::std::ostream* os) const;
3320fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3321fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // Describes the negation of this UnorderedElementsAre matcher.
3322fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  void DescribeNegationToImpl(::std::ostream* os) const;
3323fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3324fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  bool VerifyAllElementsAndMatchersAreMatched(
3325fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan      const ::std::vector<string>& element_printouts,
3326fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan      const MatchMatrix& matrix,
3327fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan      MatchResultListener* listener) const;
3328fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3329fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  MatcherDescriberVec& matcher_describers() {
3330fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    return matcher_describers_;
3331fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  }
3332fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3333fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  static Message Elements(size_t n) {
3334fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    return Message() << n << " element" << (n == 1 ? "" : "s");
3335fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  }
3336fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3337fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan private:
3338fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  MatcherDescriberVec matcher_describers_;
3339fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3340fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  GTEST_DISALLOW_ASSIGN_(UnorderedElementsAreMatcherImplBase);
3341fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan};
3342fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3343fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// Implements unordered ElementsAre and unordered ElementsAreArray.
3344fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantemplate <typename Container>
3345fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanclass UnorderedElementsAreMatcherImpl
3346fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    : public MatcherInterface<Container>,
3347fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan      public UnorderedElementsAreMatcherImplBase {
3348fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan public:
3349fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
3350fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  typedef internal::StlContainerView<RawContainer> View;
3351fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  typedef typename View::type StlContainer;
3352fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  typedef typename View::const_reference StlContainerReference;
3353fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  typedef typename StlContainer::const_iterator StlContainerConstIterator;
3354fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  typedef typename StlContainer::value_type Element;
3355fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3356fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // Constructs the matcher from a sequence of element values or
3357fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // element matchers.
3358fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  template <typename InputIter>
3359fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  UnorderedElementsAreMatcherImpl(InputIter first, InputIter last) {
3360fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    for (; first != last; ++first) {
3361fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan      matchers_.push_back(MatcherCast<const Element&>(*first));
3362fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan      matcher_describers().push_back(matchers_.back().GetDescriber());
3363fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    }
3364fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  }
3365fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3366fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // Describes what this matcher does.
3367fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  virtual void DescribeTo(::std::ostream* os) const {
3368fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    return UnorderedElementsAreMatcherImplBase::DescribeToImpl(os);
3369fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  }
3370fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3371fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  // Describes what the negation of this matcher does.
3372fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  virtual void DescribeNegationTo(::std::ostream* os) const {
3373fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    return UnorderedElementsAreMatcherImplBase::DescribeNegationToImpl(os);
3374fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  }
3375fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3376fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  virtual bool MatchAndExplain(Container container,
3377fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan                               MatchResultListener* listener) const {
3378fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    StlContainerReference stl_container = View::ConstReference(container);
33795579c1a8b1591d4932495b8cb3cc61f3edca2555zhanyong.wan    ::std::vector<string> element_printouts;
33805579c1a8b1591d4932495b8cb3cc61f3edca2555zhanyong.wan    MatchMatrix matrix = AnalyzeElements(stl_container.begin(),
33815579c1a8b1591d4932495b8cb3cc61f3edca2555zhanyong.wan                                         stl_container.end(),
33825579c1a8b1591d4932495b8cb3cc61f3edca2555zhanyong.wan                                         &element_printouts,
33835579c1a8b1591d4932495b8cb3cc61f3edca2555zhanyong.wan                                         listener);
3384fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
33855579c1a8b1591d4932495b8cb3cc61f3edca2555zhanyong.wan    const size_t actual_count = matrix.LhsSize();
3386fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    if (actual_count == 0 && matchers_.empty()) {
3387fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan      return true;
3388fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    }
3389fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    if (actual_count != matchers_.size()) {
3390fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan      // The element count doesn't match.  If the container is empty,
3391fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan      // there's no need to explain anything as Google Mock already
3392fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan      // prints the empty container. Otherwise we just need to show
3393fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan      // how many elements there actually are.
3394fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan      if (actual_count != 0 && listener->IsInterested()) {
3395fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan        *listener << "which has " << Elements(actual_count);
3396fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan      }
3397fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan      return false;
3398fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    }
3399fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3400fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    return VerifyAllElementsAndMatchersAreMatched(element_printouts,
3401fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan                                                  matrix, listener) &&
3402fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan           FindPairing(matrix, listener);
3403fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  }
3404fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3405fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan private:
3406fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  typedef ::std::vector<Matcher<const Element&> > MatcherVec;
3407fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3408fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  template <typename ElementIter>
3409fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  MatchMatrix AnalyzeElements(ElementIter elem_first, ElementIter elem_last,
3410fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan                              ::std::vector<string>* element_printouts,
3411fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan                              MatchResultListener* listener) const {
34125579c1a8b1591d4932495b8cb3cc61f3edca2555zhanyong.wan    element_printouts->clear();
3413fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    ::std::vector<char> did_match;
3414fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    size_t num_elements = 0;
3415fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    for (; elem_first != elem_last; ++num_elements, ++elem_first) {
3416fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan      if (listener->IsInterested()) {
3417fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan        element_printouts->push_back(PrintToString(*elem_first));
3418fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan      }
3419fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan      for (size_t irhs = 0; irhs != matchers_.size(); ++irhs) {
3420fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan        did_match.push_back(Matches(matchers_[irhs])(*elem_first));
3421fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan      }
3422fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    }
3423fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3424fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    MatchMatrix matrix(num_elements, matchers_.size());
3425fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    ::std::vector<char>::const_iterator did_match_iter = did_match.begin();
3426fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    for (size_t ilhs = 0; ilhs != num_elements; ++ilhs) {
3427fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan      for (size_t irhs = 0; irhs != matchers_.size(); ++irhs) {
3428fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan        matrix.SetEdge(ilhs, irhs, *did_match_iter++ != 0);
3429fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan      }
3430fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    }
3431fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    return matrix;
3432fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  }
3433fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3434fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  MatcherVec matchers_;
3435fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3436fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  GTEST_DISALLOW_ASSIGN_(UnorderedElementsAreMatcherImpl);
3437fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan};
3438fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3439fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// Functor for use in TransformTuple.
3440fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// Performs MatcherCast<Target> on an input argument of any type.
3441fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantemplate <typename Target>
3442fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanstruct CastAndAppendTransform {
3443fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  template <typename Arg>
3444fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  Matcher<Target> operator()(const Arg& a) const {
3445fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    return MatcherCast<Target>(a);
3446fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  }
3447fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan};
3448fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3449fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// Implements UnorderedElementsAre.
3450fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantemplate <typename MatcherTuple>
3451fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanclass UnorderedElementsAreMatcher {
3452fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan public:
3453fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  explicit UnorderedElementsAreMatcher(const MatcherTuple& args)
3454fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan      : matchers_(args) {}
3455fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3456fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  template <typename Container>
3457fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  operator Matcher<Container>() const {
3458fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
3459fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    typedef typename internal::StlContainerView<RawContainer>::type View;
3460fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    typedef typename View::value_type Element;
3461fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    typedef ::std::vector<Matcher<const Element&> > MatcherVec;
3462fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    MatcherVec matchers;
3463bd0188320de5aab1b09718e2c466387099d43cfbkosak    matchers.reserve(::testing::tuple_size<MatcherTuple>::value);
3464fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    TransformTupleValues(CastAndAppendTransform<const Element&>(), matchers_,
3465fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan                         ::std::back_inserter(matchers));
3466fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    return MakeMatcher(new UnorderedElementsAreMatcherImpl<Container>(
3467fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan                           matchers.begin(), matchers.end()));
3468fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  }
3469fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3470fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan private:
3471fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  const MatcherTuple matchers_;
3472fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  GTEST_DISALLOW_ASSIGN_(UnorderedElementsAreMatcher);
3473fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan};
3474fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3475fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// Implements ElementsAre.
3476fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantemplate <typename MatcherTuple>
3477fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanclass ElementsAreMatcher {
3478fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan public:
3479fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  explicit ElementsAreMatcher(const MatcherTuple& args) : matchers_(args) {}
34801afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
34811afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  template <typename Container>
34821afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  operator Matcher<Container>() const {
3483ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan    typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
3484fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    typedef typename internal::StlContainerView<RawContainer>::type View;
3485fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    typedef typename View::value_type Element;
3486fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    typedef ::std::vector<Matcher<const Element&> > MatcherVec;
3487fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    MatcherVec matchers;
3488bd0188320de5aab1b09718e2c466387099d43cfbkosak    matchers.reserve(::testing::tuple_size<MatcherTuple>::value);
3489fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    TransformTupleValues(CastAndAppendTransform<const Element&>(), matchers_,
3490fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan                         ::std::back_inserter(matchers));
3491fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    return MakeMatcher(new ElementsAreMatcherImpl<Container>(
3492fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan                           matchers.begin(), matchers.end()));
3493fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  }
3494fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3495fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan private:
3496fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  const MatcherTuple matchers_;
3497fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  GTEST_DISALLOW_ASSIGN_(ElementsAreMatcher);
3498fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan};
34991afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
3500fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// Implements UnorderedElementsAreArray().
3501fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantemplate <typename T>
3502fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanclass UnorderedElementsAreArrayMatcher {
3503fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan public:
3504fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  UnorderedElementsAreArrayMatcher() {}
3505fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3506fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  template <typename Iter>
3507fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  UnorderedElementsAreArrayMatcher(Iter first, Iter last)
3508fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan      : matchers_(first, last) {}
3509fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3510fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  template <typename Container>
3511fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  operator Matcher<Container>() const {
3512fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    return MakeMatcher(
3513fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan        new UnorderedElementsAreMatcherImpl<Container>(matchers_.begin(),
3514fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan                                                       matchers_.end()));
35151afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  }
3516fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3517fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan private:
3518fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  ::std::vector<T> matchers_;
3519fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3520fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  GTEST_DISALLOW_ASSIGN_(UnorderedElementsAreArrayMatcher);
35211afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan};
35221afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
35231afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// Implements ElementsAreArray().
35241afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wantemplate <typename T>
35251afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wanclass ElementsAreArrayMatcher {
35261afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan public:
352738513a8bb154f0b6d0a4088814fe92552696d465jgm  template <typename Iter>
352838513a8bb154f0b6d0a4088814fe92552696d465jgm  ElementsAreArrayMatcher(Iter first, Iter last) : matchers_(first, last) {}
35291afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
35301afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  template <typename Container>
35311afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  operator Matcher<Container>() const {
353238513a8bb154f0b6d0a4088814fe92552696d465jgm    return MakeMatcher(new ElementsAreMatcherImpl<Container>(
353338513a8bb154f0b6d0a4088814fe92552696d465jgm        matchers_.begin(), matchers_.end()));
35341afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan  }
35351afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
35361afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan private:
3537fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  const ::std::vector<T> matchers_;
353832de5f53763125925e078498250f7e73a88de9edzhanyong.wan
353932de5f53763125925e078498250f7e73a88de9edzhanyong.wan  GTEST_DISALLOW_ASSIGN_(ElementsAreArrayMatcher);
35401afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan};
35411afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
35422336e9c171b5894bac98f828289f537e29691896kosak// Given a 2-tuple matcher tm of type Tuple2Matcher and a value second
35432336e9c171b5894bac98f828289f537e29691896kosak// of type Second, BoundSecondMatcher<Tuple2Matcher, Second>(tm,
35442336e9c171b5894bac98f828289f537e29691896kosak// second) is a polymorphic matcher that matches a value x iff tm
35452336e9c171b5894bac98f828289f537e29691896kosak// matches tuple (x, second).  Useful for implementing
35462336e9c171b5894bac98f828289f537e29691896kosak// UnorderedPointwise() in terms of UnorderedElementsAreArray().
35472336e9c171b5894bac98f828289f537e29691896kosak//
35482336e9c171b5894bac98f828289f537e29691896kosak// BoundSecondMatcher is copyable and assignable, as we need to put
35492336e9c171b5894bac98f828289f537e29691896kosak// instances of this class in a vector when implementing
35502336e9c171b5894bac98f828289f537e29691896kosak// UnorderedPointwise().
35512336e9c171b5894bac98f828289f537e29691896kosaktemplate <typename Tuple2Matcher, typename Second>
35522336e9c171b5894bac98f828289f537e29691896kosakclass BoundSecondMatcher {
35532336e9c171b5894bac98f828289f537e29691896kosak public:
35542336e9c171b5894bac98f828289f537e29691896kosak  BoundSecondMatcher(const Tuple2Matcher& tm, const Second& second)
35552336e9c171b5894bac98f828289f537e29691896kosak      : tuple2_matcher_(tm), second_value_(second) {}
35562336e9c171b5894bac98f828289f537e29691896kosak
35572336e9c171b5894bac98f828289f537e29691896kosak  template <typename T>
35582336e9c171b5894bac98f828289f537e29691896kosak  operator Matcher<T>() const {
35592336e9c171b5894bac98f828289f537e29691896kosak    return MakeMatcher(new Impl<T>(tuple2_matcher_, second_value_));
35602336e9c171b5894bac98f828289f537e29691896kosak  }
35612336e9c171b5894bac98f828289f537e29691896kosak
35622336e9c171b5894bac98f828289f537e29691896kosak  // We have to define this for UnorderedPointwise() to compile in
35632336e9c171b5894bac98f828289f537e29691896kosak  // C++98 mode, as it puts BoundSecondMatcher instances in a vector,
35642336e9c171b5894bac98f828289f537e29691896kosak  // which requires the elements to be assignable in C++98.  The
35652336e9c171b5894bac98f828289f537e29691896kosak  // compiler cannot generate the operator= for us, as Tuple2Matcher
35662336e9c171b5894bac98f828289f537e29691896kosak  // and Second may not be assignable.
35672336e9c171b5894bac98f828289f537e29691896kosak  //
35682336e9c171b5894bac98f828289f537e29691896kosak  // However, this should never be called, so the implementation just
35692336e9c171b5894bac98f828289f537e29691896kosak  // need to assert.
35702336e9c171b5894bac98f828289f537e29691896kosak  void operator=(const BoundSecondMatcher& /*rhs*/) {
35712336e9c171b5894bac98f828289f537e29691896kosak    GTEST_LOG_(FATAL) << "BoundSecondMatcher should never be assigned.";
35722336e9c171b5894bac98f828289f537e29691896kosak  }
35732336e9c171b5894bac98f828289f537e29691896kosak
35742336e9c171b5894bac98f828289f537e29691896kosak private:
35752336e9c171b5894bac98f828289f537e29691896kosak  template <typename T>
35762336e9c171b5894bac98f828289f537e29691896kosak  class Impl : public MatcherInterface<T> {
35772336e9c171b5894bac98f828289f537e29691896kosak   public:
35782336e9c171b5894bac98f828289f537e29691896kosak    typedef ::testing::tuple<T, Second> ArgTuple;
35792336e9c171b5894bac98f828289f537e29691896kosak
35802336e9c171b5894bac98f828289f537e29691896kosak    Impl(const Tuple2Matcher& tm, const Second& second)
35812336e9c171b5894bac98f828289f537e29691896kosak        : mono_tuple2_matcher_(SafeMatcherCast<const ArgTuple&>(tm)),
35822336e9c171b5894bac98f828289f537e29691896kosak          second_value_(second) {}
35832336e9c171b5894bac98f828289f537e29691896kosak
35842336e9c171b5894bac98f828289f537e29691896kosak    virtual void DescribeTo(::std::ostream* os) const {
35852336e9c171b5894bac98f828289f537e29691896kosak      *os << "and ";
35862336e9c171b5894bac98f828289f537e29691896kosak      UniversalPrint(second_value_, os);
35872336e9c171b5894bac98f828289f537e29691896kosak      *os << " ";
35882336e9c171b5894bac98f828289f537e29691896kosak      mono_tuple2_matcher_.DescribeTo(os);
35892336e9c171b5894bac98f828289f537e29691896kosak    }
35902336e9c171b5894bac98f828289f537e29691896kosak
35912336e9c171b5894bac98f828289f537e29691896kosak    virtual bool MatchAndExplain(T x, MatchResultListener* listener) const {
35922336e9c171b5894bac98f828289f537e29691896kosak      return mono_tuple2_matcher_.MatchAndExplain(ArgTuple(x, second_value_),
35932336e9c171b5894bac98f828289f537e29691896kosak                                                  listener);
35942336e9c171b5894bac98f828289f537e29691896kosak    }
35952336e9c171b5894bac98f828289f537e29691896kosak
35962336e9c171b5894bac98f828289f537e29691896kosak   private:
35972336e9c171b5894bac98f828289f537e29691896kosak    const Matcher<const ArgTuple&> mono_tuple2_matcher_;
35982336e9c171b5894bac98f828289f537e29691896kosak    const Second second_value_;
35992336e9c171b5894bac98f828289f537e29691896kosak
36002336e9c171b5894bac98f828289f537e29691896kosak    GTEST_DISALLOW_ASSIGN_(Impl);
36012336e9c171b5894bac98f828289f537e29691896kosak  };
36022336e9c171b5894bac98f828289f537e29691896kosak
36032336e9c171b5894bac98f828289f537e29691896kosak  const Tuple2Matcher tuple2_matcher_;
36042336e9c171b5894bac98f828289f537e29691896kosak  const Second second_value_;
36052336e9c171b5894bac98f828289f537e29691896kosak};
36062336e9c171b5894bac98f828289f537e29691896kosak
36072336e9c171b5894bac98f828289f537e29691896kosak// Given a 2-tuple matcher tm and a value second,
36082336e9c171b5894bac98f828289f537e29691896kosak// MatcherBindSecond(tm, second) returns a matcher that matches a
36092336e9c171b5894bac98f828289f537e29691896kosak// value x iff tm matches tuple (x, second).  Useful for implementing
36102336e9c171b5894bac98f828289f537e29691896kosak// UnorderedPointwise() in terms of UnorderedElementsAreArray().
36112336e9c171b5894bac98f828289f537e29691896kosaktemplate <typename Tuple2Matcher, typename Second>
36122336e9c171b5894bac98f828289f537e29691896kosakBoundSecondMatcher<Tuple2Matcher, Second> MatcherBindSecond(
36132336e9c171b5894bac98f828289f537e29691896kosak    const Tuple2Matcher& tm, const Second& second) {
36142336e9c171b5894bac98f828289f537e29691896kosak  return BoundSecondMatcher<Tuple2Matcher, Second>(tm, second);
36152336e9c171b5894bac98f828289f537e29691896kosak}
36162336e9c171b5894bac98f828289f537e29691896kosak
3617b4140808f98ff09c43ca1ddaa8ff13bc47cd0089zhanyong.wan// Returns the description for a matcher defined using the MATCHER*()
3618b4140808f98ff09c43ca1ddaa8ff13bc47cd0089zhanyong.wan// macro where the user-supplied description string is "", if
3619b4140808f98ff09c43ca1ddaa8ff13bc47cd0089zhanyong.wan// 'negation' is false; otherwise returns the description of the
3620b4140808f98ff09c43ca1ddaa8ff13bc47cd0089zhanyong.wan// negation of the matcher.  'param_values' contains a list of strings
3621b4140808f98ff09c43ca1ddaa8ff13bc47cd0089zhanyong.wan// that are the print-out of the matcher's parameters.
3622587c1b37c2f0b6d430fb13ce09326db0135b557cvladlosevGTEST_API_ string FormatMatcherDescription(bool negation,
3623587c1b37c2f0b6d430fb13ce09326db0135b557cvladlosev                                           const char* matcher_name,
3624587c1b37c2f0b6d430fb13ce09326db0135b557cvladlosev                                           const Strings& param_values);
36251afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan
3626e35fdd936d133bf8a48de140a3c666897588a05shiqian}  // namespace internal
3627e35fdd936d133bf8a48de140a3c666897588a05shiqian
3628fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// ElementsAreArray(first, last)
3629fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// ElementsAreArray(pointer, count)
3630fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// ElementsAreArray(array)
363106678924fa3a48708f2c90e7226dfda54badc808kosak// ElementsAreArray(container)
36321cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan// ElementsAreArray({ e1, e2, ..., en })
3633fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan//
36341cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan// The ElementsAreArray() functions are like ElementsAre(...), except
36351cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan// that they are given a homogeneous sequence rather than taking each
36361cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan// element as a function argument. The sequence can be specified as an
36371cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan// array, a pointer and count, a vector, an initializer list, or an
36381cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan// STL iterator range. In each of these cases, the underlying sequence
36391cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan// can be either a sequence of values or a sequence of matchers.
3640fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan//
3641fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// All forms of ElementsAreArray() make a copy of the input matcher sequence.
3642fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3643fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantemplate <typename Iter>
3644fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.waninline internal::ElementsAreArrayMatcher<
3645fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    typename ::std::iterator_traits<Iter>::value_type>
3646fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanElementsAreArray(Iter first, Iter last) {
3647fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  typedef typename ::std::iterator_traits<Iter>::value_type T;
3648fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  return internal::ElementsAreArrayMatcher<T>(first, last);
3649fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan}
3650fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3651fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantemplate <typename T>
3652fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.waninline internal::ElementsAreArrayMatcher<T> ElementsAreArray(
3653fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    const T* pointer, size_t count) {
3654fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  return ElementsAreArray(pointer, pointer + count);
3655fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan}
3656fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3657fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantemplate <typename T, size_t N>
3658fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.waninline internal::ElementsAreArrayMatcher<T> ElementsAreArray(
3659fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    const T (&array)[N]) {
3660fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  return ElementsAreArray(array, N);
3661fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan}
3662fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
366306678924fa3a48708f2c90e7226dfda54badc808kosaktemplate <typename Container>
366406678924fa3a48708f2c90e7226dfda54badc808kosakinline internal::ElementsAreArrayMatcher<typename Container::value_type>
366506678924fa3a48708f2c90e7226dfda54badc808kosakElementsAreArray(const Container& container) {
366606678924fa3a48708f2c90e7226dfda54badc808kosak  return ElementsAreArray(container.begin(), container.end());
3667fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan}
3668fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
366918489fa4f81a65ab5f42a6705c28ca2f80eab3bdkosak#if GTEST_HAS_STD_INITIALIZER_LIST_
36701cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wantemplate <typename T>
36711cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.waninline internal::ElementsAreArrayMatcher<T>
36721cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wanElementsAreArray(::std::initializer_list<T> xs) {
36731cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan  return ElementsAreArray(xs.begin(), xs.end());
36741cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan}
36751cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan#endif
36761cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan
3677fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// UnorderedElementsAreArray(first, last)
3678fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// UnorderedElementsAreArray(pointer, count)
3679fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// UnorderedElementsAreArray(array)
368006678924fa3a48708f2c90e7226dfda54badc808kosak// UnorderedElementsAreArray(container)
36811cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan// UnorderedElementsAreArray({ e1, e2, ..., en })
3682fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan//
3683fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// The UnorderedElementsAreArray() functions are like
3684fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// ElementsAreArray(...), but allow matching the elements in any order.
3685fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantemplate <typename Iter>
3686fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.waninline internal::UnorderedElementsAreArrayMatcher<
3687fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan    typename ::std::iterator_traits<Iter>::value_type>
3688fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanUnorderedElementsAreArray(Iter first, Iter last) {
3689fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  typedef typename ::std::iterator_traits<Iter>::value_type T;
3690fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  return internal::UnorderedElementsAreArrayMatcher<T>(first, last);
3691fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan}
3692fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3693fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantemplate <typename T>
3694fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.waninline internal::UnorderedElementsAreArrayMatcher<T>
3695fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanUnorderedElementsAreArray(const T* pointer, size_t count) {
3696fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  return UnorderedElementsAreArray(pointer, pointer + count);
3697fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan}
3698fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3699fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantemplate <typename T, size_t N>
3700fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.waninline internal::UnorderedElementsAreArrayMatcher<T>
3701fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanUnorderedElementsAreArray(const T (&array)[N]) {
3702fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan  return UnorderedElementsAreArray(array, N);
3703fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan}
3704fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
370506678924fa3a48708f2c90e7226dfda54badc808kosaktemplate <typename Container>
370606678924fa3a48708f2c90e7226dfda54badc808kosakinline internal::UnorderedElementsAreArrayMatcher<
370706678924fa3a48708f2c90e7226dfda54badc808kosak    typename Container::value_type>
370806678924fa3a48708f2c90e7226dfda54badc808kosakUnorderedElementsAreArray(const Container& container) {
370906678924fa3a48708f2c90e7226dfda54badc808kosak  return UnorderedElementsAreArray(container.begin(), container.end());
3710fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan}
3711fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
371218489fa4f81a65ab5f42a6705c28ca2f80eab3bdkosak#if GTEST_HAS_STD_INITIALIZER_LIST_
37131cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wantemplate <typename T>
37141cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.waninline internal::UnorderedElementsAreArrayMatcher<T>
37151cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wanUnorderedElementsAreArray(::std::initializer_list<T> xs) {
37161cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan  return UnorderedElementsAreArray(xs.begin(), xs.end());
37171cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan}
37181cc1d4bcecdac906250f863bef2636b278eb679dzhanyong.wan#endif
3719fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan
3720e35fdd936d133bf8a48de140a3c666897588a05shiqian// _ is a matcher that matches anything of any type.
3721e35fdd936d133bf8a48de140a3c666897588a05shiqian//
3722e35fdd936d133bf8a48de140a3c666897588a05shiqian// This definition is fine as:
3723e35fdd936d133bf8a48de140a3c666897588a05shiqian//
3724e35fdd936d133bf8a48de140a3c666897588a05shiqian//   1. The C++ standard permits using the name _ in a namespace that
3725e35fdd936d133bf8a48de140a3c666897588a05shiqian//      is not the global namespace or ::std.
3726e35fdd936d133bf8a48de140a3c666897588a05shiqian//   2. The AnythingMatcher class has no data member or constructor,
3727e35fdd936d133bf8a48de140a3c666897588a05shiqian//      so it's OK to create global variables of this type.
3728e35fdd936d133bf8a48de140a3c666897588a05shiqian//   3. c-style has approved of using _ in this case.
3729e35fdd936d133bf8a48de140a3c666897588a05shiqianconst internal::AnythingMatcher _ = {};
3730e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any value of the given type T.
3731e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
3732e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<T> A() { return MakeMatcher(new internal::AnyMatcherImpl<T>()); }
3733e35fdd936d133bf8a48de140a3c666897588a05shiqian
3734e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any value of the given type T.
3735e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
3736e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<T> An() { return A<T>(); }
3737e35fdd936d133bf8a48de140a3c666897588a05shiqian
3738e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything equal to x.
3739e35fdd936d133bf8a48de140a3c666897588a05shiqian// Note: if the parameter of Eq() were declared as const T&, Eq("foo")
3740e35fdd936d133bf8a48de140a3c666897588a05shiqian// wouldn't compile.
3741e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
3742e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::EqMatcher<T> Eq(T x) { return internal::EqMatcher<T>(x); }
3743e35fdd936d133bf8a48de140a3c666897588a05shiqian
3744e35fdd936d133bf8a48de140a3c666897588a05shiqian// Constructs a Matcher<T> from a 'value' of type T.  The constructed
3745e35fdd936d133bf8a48de140a3c666897588a05shiqian// matcher matches any value that's equal to 'value'.
3746e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
3747e35fdd936d133bf8a48de140a3c666897588a05shiqianMatcher<T>::Matcher(T value) { *this = Eq(value); }
3748e35fdd936d133bf8a48de140a3c666897588a05shiqian
3749e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a monomorphic matcher that matches anything with type Lhs
3750e35fdd936d133bf8a48de140a3c666897588a05shiqian// and equal to rhs.  A user may need to use this instead of Eq(...)
3751e35fdd936d133bf8a48de140a3c666897588a05shiqian// in order to resolve an overloading ambiguity.
3752e35fdd936d133bf8a48de140a3c666897588a05shiqian//
3753e35fdd936d133bf8a48de140a3c666897588a05shiqian// TypedEq<T>(x) is just a convenient short-hand for Matcher<T>(Eq(x))
3754e35fdd936d133bf8a48de140a3c666897588a05shiqian// or Matcher<T>(x), but more readable than the latter.
3755e35fdd936d133bf8a48de140a3c666897588a05shiqian//
3756e35fdd936d133bf8a48de140a3c666897588a05shiqian// We could define similar monomorphic matchers for other comparison
3757e35fdd936d133bf8a48de140a3c666897588a05shiqian// operations (e.g. TypedLt, TypedGe, and etc), but decided not to do
3758e35fdd936d133bf8a48de140a3c666897588a05shiqian// it yet as those are used much less than Eq() in practice.  A user
3759e35fdd936d133bf8a48de140a3c666897588a05shiqian// can always write Matcher<T>(Lt(5)) to be explicit about the type,
3760e35fdd936d133bf8a48de140a3c666897588a05shiqian// for example.
3761e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Lhs, typename Rhs>
3762e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<Lhs> TypedEq(const Rhs& rhs) { return Eq(rhs); }
3763e35fdd936d133bf8a48de140a3c666897588a05shiqian
3764e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything >= x.
3765e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs>
3766e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::GeMatcher<Rhs> Ge(Rhs x) {
3767e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::GeMatcher<Rhs>(x);
3768e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3769e35fdd936d133bf8a48de140a3c666897588a05shiqian
3770e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything > x.
3771e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs>
3772e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::GtMatcher<Rhs> Gt(Rhs x) {
3773e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::GtMatcher<Rhs>(x);
3774e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3775e35fdd936d133bf8a48de140a3c666897588a05shiqian
3776e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything <= x.
3777e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs>
3778e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::LeMatcher<Rhs> Le(Rhs x) {
3779e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::LeMatcher<Rhs>(x);
3780e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3781e35fdd936d133bf8a48de140a3c666897588a05shiqian
3782e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything < x.
3783e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs>
3784e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::LtMatcher<Rhs> Lt(Rhs x) {
3785e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::LtMatcher<Rhs>(x);
3786e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3787e35fdd936d133bf8a48de140a3c666897588a05shiqian
3788e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything != x.
3789e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs>
3790e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::NeMatcher<Rhs> Ne(Rhs x) {
3791e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::NeMatcher<Rhs>(x);
3792e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3793e35fdd936d133bf8a48de140a3c666897588a05shiqian
37942d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan// Creates a polymorphic matcher that matches any NULL pointer.
37952d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.waninline PolymorphicMatcher<internal::IsNullMatcher > IsNull() {
37962d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan  return MakePolymorphicMatcher(internal::IsNullMatcher());
37972d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan}
37982d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan
3799e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches any non-NULL pointer.
3800e35fdd936d133bf8a48de140a3c666897588a05shiqian// This is convenient as Not(NULL) doesn't compile (the compiler
3801e35fdd936d133bf8a48de140a3c666897588a05shiqian// thinks that that expression is comparing a pointer with an integer).
3802e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::NotNullMatcher > NotNull() {
3803e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::NotNullMatcher());
3804e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3805e35fdd936d133bf8a48de140a3c666897588a05shiqian
3806e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches any argument that
3807e35fdd936d133bf8a48de140a3c666897588a05shiqian// references variable x.
3808e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T>
3809e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::RefMatcher<T&> Ref(T& x) {  // NOLINT
3810e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::RefMatcher<T&>(x);
3811e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3812e35fdd936d133bf8a48de140a3c666897588a05shiqian
3813e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any double argument approximately
3814e35fdd936d133bf8a48de140a3c666897588a05shiqian// equal to rhs, where two NANs are considered unequal.
3815e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::FloatingEqMatcher<double> DoubleEq(double rhs) {
3816e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::FloatingEqMatcher<double>(rhs, false);
3817e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3818e35fdd936d133bf8a48de140a3c666897588a05shiqian
3819e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any double argument approximately
3820e35fdd936d133bf8a48de140a3c666897588a05shiqian// equal to rhs, including NaN values when rhs is NaN.
3821e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::FloatingEqMatcher<double> NanSensitiveDoubleEq(double rhs) {
3822e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::FloatingEqMatcher<double>(rhs, true);
3823e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3824e35fdd936d133bf8a48de140a3c666897588a05shiqian
3825616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// Creates a matcher that matches any double argument approximately equal to
3826616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// rhs, up to the specified max absolute error bound, where two NANs are
3827616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// considered unequal.  The max absolute error bound must be non-negative.
3828616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.waninline internal::FloatingEqMatcher<double> DoubleNear(
3829616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    double rhs, double max_abs_error) {
3830616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  return internal::FloatingEqMatcher<double>(rhs, false, max_abs_error);
3831616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan}
3832616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
3833616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// Creates a matcher that matches any double argument approximately equal to
3834616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// rhs, up to the specified max absolute error bound, including NaN values when
3835616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// rhs is NaN.  The max absolute error bound must be non-negative.
3836616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.waninline internal::FloatingEqMatcher<double> NanSensitiveDoubleNear(
3837616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    double rhs, double max_abs_error) {
3838616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  return internal::FloatingEqMatcher<double>(rhs, true, max_abs_error);
3839616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan}
3840616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
3841e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any float argument approximately
3842e35fdd936d133bf8a48de140a3c666897588a05shiqian// equal to rhs, where two NANs are considered unequal.
3843e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::FloatingEqMatcher<float> FloatEq(float rhs) {
3844e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::FloatingEqMatcher<float>(rhs, false);
3845e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3846e35fdd936d133bf8a48de140a3c666897588a05shiqian
3847616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// Creates a matcher that matches any float argument approximately
3848e35fdd936d133bf8a48de140a3c666897588a05shiqian// equal to rhs, including NaN values when rhs is NaN.
3849e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::FloatingEqMatcher<float> NanSensitiveFloatEq(float rhs) {
3850e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::FloatingEqMatcher<float>(rhs, true);
3851e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3852e35fdd936d133bf8a48de140a3c666897588a05shiqian
3853616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// Creates a matcher that matches any float argument approximately equal to
3854616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// rhs, up to the specified max absolute error bound, where two NANs are
3855616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// considered unequal.  The max absolute error bound must be non-negative.
3856616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.waninline internal::FloatingEqMatcher<float> FloatNear(
3857616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    float rhs, float max_abs_error) {
3858616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  return internal::FloatingEqMatcher<float>(rhs, false, max_abs_error);
3859616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan}
3860616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
3861616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// Creates a matcher that matches any float argument approximately equal to
3862616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// rhs, up to the specified max absolute error bound, including NaN values when
3863616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// rhs is NaN.  The max absolute error bound must be non-negative.
3864616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.waninline internal::FloatingEqMatcher<float> NanSensitiveFloatNear(
3865616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan    float rhs, float max_abs_error) {
3866616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  return internal::FloatingEqMatcher<float>(rhs, true, max_abs_error);
3867616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan}
3868616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
3869e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches a pointer (raw or smart) that points
3870e35fdd936d133bf8a48de140a3c666897588a05shiqian// to a value that matches inner_matcher.
3871e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename InnerMatcher>
3872e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::PointeeMatcher<InnerMatcher> Pointee(
3873e35fdd936d133bf8a48de140a3c666897588a05shiqian    const InnerMatcher& inner_matcher) {
3874e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::PointeeMatcher<InnerMatcher>(inner_matcher);
3875e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3876e35fdd936d133bf8a48de140a3c666897588a05shiqian
38771f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue// Creates a matcher that matches a pointer or reference that matches
38781f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue// inner_matcher when dynamic_cast<To> is applied.
38791f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue// The result of dynamic_cast<To> is forwarded to the inner matcher.
38801f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue// If To is a pointer and the cast fails, the inner matcher will receive NULL.
38811f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue// If To is a reference and the cast fails, this matcher returns false
38821f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue// immediately.
38831f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahuetemplate <typename To>
38841f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahueinline PolymorphicMatcher<internal::WhenDynamicCastToMatcher<To> >
38851f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahueWhenDynamicCastTo(const Matcher<To>& inner_matcher) {
38861f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue  return MakePolymorphicMatcher(
38871f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue      internal::WhenDynamicCastToMatcher<To>(inner_matcher));
38881f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue}
38891f5fdea417f1a55cffcfc0711d345d1d8ca2bc01billydonahue
3890e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches an object whose given field matches
3891e35fdd936d133bf8a48de140a3c666897588a05shiqian// 'matcher'.  For example,
3892e35fdd936d133bf8a48de140a3c666897588a05shiqian//   Field(&Foo::number, Ge(5))
3893e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a Foo object x iff x.number >= 5.
3894e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Class, typename FieldType, typename FieldMatcher>
3895e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<
3896e35fdd936d133bf8a48de140a3c666897588a05shiqian  internal::FieldMatcher<Class, FieldType> > Field(
3897e35fdd936d133bf8a48de140a3c666897588a05shiqian    FieldType Class::*field, const FieldMatcher& matcher) {
3898e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(
3899e35fdd936d133bf8a48de140a3c666897588a05shiqian      internal::FieldMatcher<Class, FieldType>(
3900e35fdd936d133bf8a48de140a3c666897588a05shiqian          field, MatcherCast<const FieldType&>(matcher)));
3901e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The call to MatcherCast() is required for supporting inner
3902e35fdd936d133bf8a48de140a3c666897588a05shiqian  // matchers of compatible types.  For example, it allows
3903e35fdd936d133bf8a48de140a3c666897588a05shiqian  //   Field(&Foo::bar, m)
3904e35fdd936d133bf8a48de140a3c666897588a05shiqian  // to compile where bar is an int32 and m is a matcher for int64.
3905e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3906e35fdd936d133bf8a48de140a3c666897588a05shiqian
3907e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches an object whose given property
3908e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches 'matcher'.  For example,
3909e35fdd936d133bf8a48de140a3c666897588a05shiqian//   Property(&Foo::str, StartsWith("hi"))
3910e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a Foo object x iff x.str() starts with "hi".
3911e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Class, typename PropertyType, typename PropertyMatcher>
3912e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<
3913e35fdd936d133bf8a48de140a3c666897588a05shiqian  internal::PropertyMatcher<Class, PropertyType> > Property(
3914e35fdd936d133bf8a48de140a3c666897588a05shiqian    PropertyType (Class::*property)() const, const PropertyMatcher& matcher) {
3915e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(
3916e35fdd936d133bf8a48de140a3c666897588a05shiqian      internal::PropertyMatcher<Class, PropertyType>(
3917e35fdd936d133bf8a48de140a3c666897588a05shiqian          property,
391802f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan          MatcherCast<GTEST_REFERENCE_TO_CONST_(PropertyType)>(matcher)));
3919e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The call to MatcherCast() is required for supporting inner
3920e35fdd936d133bf8a48de140a3c666897588a05shiqian  // matchers of compatible types.  For example, it allows
3921e35fdd936d133bf8a48de140a3c666897588a05shiqian  //   Property(&Foo::bar, m)
3922e35fdd936d133bf8a48de140a3c666897588a05shiqian  // to compile where bar() returns an int32 and m is a matcher for int64.
3923e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3924e35fdd936d133bf8a48de140a3c666897588a05shiqian
3925e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches an object iff the result of applying
3926e35fdd936d133bf8a48de140a3c666897588a05shiqian// a callable to x matches 'matcher'.
3927e35fdd936d133bf8a48de140a3c666897588a05shiqian// For example,
3928e35fdd936d133bf8a48de140a3c666897588a05shiqian//   ResultOf(f, StartsWith("hi"))
3929e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a Foo object x iff f(x) starts with "hi".
3930e35fdd936d133bf8a48de140a3c666897588a05shiqian// callable parameter can be a function, function pointer, or a functor.
3931e35fdd936d133bf8a48de140a3c666897588a05shiqian// Callable has to satisfy the following conditions:
3932e35fdd936d133bf8a48de140a3c666897588a05shiqian//   * It is required to keep no state affecting the results of
3933e35fdd936d133bf8a48de140a3c666897588a05shiqian//     the calls on it and make no assumptions about how many calls
3934e35fdd936d133bf8a48de140a3c666897588a05shiqian//     will be made. Any state it keeps must be protected from the
3935e35fdd936d133bf8a48de140a3c666897588a05shiqian//     concurrent access.
3936e35fdd936d133bf8a48de140a3c666897588a05shiqian//   * If it is a function object, it has to define type result_type.
3937e35fdd936d133bf8a48de140a3c666897588a05shiqian//     We recommend deriving your functor classes from std::unary_function.
3938e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Callable, typename ResultOfMatcher>
3939e35fdd936d133bf8a48de140a3c666897588a05shiqianinternal::ResultOfMatcher<Callable> ResultOf(
3940e35fdd936d133bf8a48de140a3c666897588a05shiqian    Callable callable, const ResultOfMatcher& matcher) {
3941e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::ResultOfMatcher<Callable>(
3942e35fdd936d133bf8a48de140a3c666897588a05shiqian          callable,
3943e35fdd936d133bf8a48de140a3c666897588a05shiqian          MatcherCast<typename internal::CallableTraits<Callable>::ResultType>(
3944e35fdd936d133bf8a48de140a3c666897588a05shiqian              matcher));
3945e35fdd936d133bf8a48de140a3c666897588a05shiqian  // The call to MatcherCast() is required for supporting inner
3946e35fdd936d133bf8a48de140a3c666897588a05shiqian  // matchers of compatible types.  For example, it allows
3947e35fdd936d133bf8a48de140a3c666897588a05shiqian  //   ResultOf(Function, m)
3948e35fdd936d133bf8a48de140a3c666897588a05shiqian  // to compile where Function() returns an int32 and m is a matcher for int64.
3949e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3950e35fdd936d133bf8a48de140a3c666897588a05shiqian
3951e35fdd936d133bf8a48de140a3c666897588a05shiqian// String matchers.
3952e35fdd936d133bf8a48de140a3c666897588a05shiqian
3953e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string equal to str.
3954e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> >
3955e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrEq(const internal::string& str) {
3956e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>(
3957e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, true, true));
3958e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3959e35fdd936d133bf8a48de140a3c666897588a05shiqian
3960e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string not equal to str.
3961e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> >
3962e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrNe(const internal::string& str) {
3963e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>(
3964e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, false, true));
3965e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3966e35fdd936d133bf8a48de140a3c666897588a05shiqian
3967e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string equal to str, ignoring case.
3968e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> >
3969e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrCaseEq(const internal::string& str) {
3970e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>(
3971e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, true, false));
3972e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3973e35fdd936d133bf8a48de140a3c666897588a05shiqian
3974e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string not equal to str, ignoring case.
3975e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> >
3976e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrCaseNe(const internal::string& str) {
3977e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>(
3978e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, false, false));
3979e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3980e35fdd936d133bf8a48de140a3c666897588a05shiqian
3981e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any string, std::string, or C string
3982e35fdd936d133bf8a48de140a3c666897588a05shiqian// that contains the given substring.
3983e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::HasSubstrMatcher<internal::string> >
3984e35fdd936d133bf8a48de140a3c666897588a05shiqian    HasSubstr(const internal::string& substring) {
3985e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::HasSubstrMatcher<internal::string>(
3986e35fdd936d133bf8a48de140a3c666897588a05shiqian      substring));
3987e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3988e35fdd936d133bf8a48de140a3c666897588a05shiqian
3989e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that starts with 'prefix' (case-sensitive).
3990e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StartsWithMatcher<internal::string> >
3991e35fdd936d133bf8a48de140a3c666897588a05shiqian    StartsWith(const internal::string& prefix) {
3992e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StartsWithMatcher<internal::string>(
3993e35fdd936d133bf8a48de140a3c666897588a05shiqian      prefix));
3994e35fdd936d133bf8a48de140a3c666897588a05shiqian}
3995e35fdd936d133bf8a48de140a3c666897588a05shiqian
3996e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that ends with 'suffix' (case-sensitive).
3997e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::EndsWithMatcher<internal::string> >
3998e35fdd936d133bf8a48de140a3c666897588a05shiqian    EndsWith(const internal::string& suffix) {
3999e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::EndsWithMatcher<internal::string>(
4000e35fdd936d133bf8a48de140a3c666897588a05shiqian      suffix));
4001e35fdd936d133bf8a48de140a3c666897588a05shiqian}
4002e35fdd936d133bf8a48de140a3c666897588a05shiqian
4003e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that fully matches regular expression 'regex'.
4004e35fdd936d133bf8a48de140a3c666897588a05shiqian// The matcher takes ownership of 'regex'.
4005e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex(
4006e35fdd936d133bf8a48de140a3c666897588a05shiqian    const internal::RE* regex) {
4007e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, true));
4008e35fdd936d133bf8a48de140a3c666897588a05shiqian}
4009e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex(
4010e35fdd936d133bf8a48de140a3c666897588a05shiqian    const internal::string& regex) {
4011e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MatchesRegex(new internal::RE(regex));
4012e35fdd936d133bf8a48de140a3c666897588a05shiqian}
4013e35fdd936d133bf8a48de140a3c666897588a05shiqian
4014e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that contains regular expression 'regex'.
4015e35fdd936d133bf8a48de140a3c666897588a05shiqian// The matcher takes ownership of 'regex'.
4016e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex(
4017e35fdd936d133bf8a48de140a3c666897588a05shiqian    const internal::RE* regex) {
4018e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, false));
4019e35fdd936d133bf8a48de140a3c666897588a05shiqian}
4020e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex(
4021e35fdd936d133bf8a48de140a3c666897588a05shiqian    const internal::string& regex) {
4022e35fdd936d133bf8a48de140a3c666897588a05shiqian  return ContainsRegex(new internal::RE(regex));
4023e35fdd936d133bf8a48de140a3c666897588a05shiqian}
4024e35fdd936d133bf8a48de140a3c666897588a05shiqian
4025e35fdd936d133bf8a48de140a3c666897588a05shiqian#if GTEST_HAS_GLOBAL_WSTRING || GTEST_HAS_STD_WSTRING
4026e35fdd936d133bf8a48de140a3c666897588a05shiqian// Wide string matchers.
4027e35fdd936d133bf8a48de140a3c666897588a05shiqian
4028e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string equal to str.
4029e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> >
4030e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrEq(const internal::wstring& str) {
4031e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>(
4032e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, true, true));
4033e35fdd936d133bf8a48de140a3c666897588a05shiqian}
4034e35fdd936d133bf8a48de140a3c666897588a05shiqian
4035e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string not equal to str.
4036e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> >
4037e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrNe(const internal::wstring& str) {
4038e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>(
4039e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, false, true));
4040e35fdd936d133bf8a48de140a3c666897588a05shiqian}
4041e35fdd936d133bf8a48de140a3c666897588a05shiqian
4042e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string equal to str, ignoring case.
4043e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> >
4044e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrCaseEq(const internal::wstring& str) {
4045e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>(
4046e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, true, false));
4047e35fdd936d133bf8a48de140a3c666897588a05shiqian}
4048e35fdd936d133bf8a48de140a3c666897588a05shiqian
4049e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string not equal to str, ignoring case.
4050e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> >
4051e35fdd936d133bf8a48de140a3c666897588a05shiqian    StrCaseNe(const internal::wstring& str) {
4052e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>(
4053e35fdd936d133bf8a48de140a3c666897588a05shiqian      str, false, false));
4054e35fdd936d133bf8a48de140a3c666897588a05shiqian}
4055e35fdd936d133bf8a48de140a3c666897588a05shiqian
4056e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any wstring, std::wstring, or C wide string
4057e35fdd936d133bf8a48de140a3c666897588a05shiqian// that contains the given substring.
4058e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::HasSubstrMatcher<internal::wstring> >
4059e35fdd936d133bf8a48de140a3c666897588a05shiqian    HasSubstr(const internal::wstring& substring) {
4060e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::HasSubstrMatcher<internal::wstring>(
4061e35fdd936d133bf8a48de140a3c666897588a05shiqian      substring));
4062e35fdd936d133bf8a48de140a3c666897588a05shiqian}
4063e35fdd936d133bf8a48de140a3c666897588a05shiqian
4064e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that starts with 'prefix' (case-sensitive).
4065e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StartsWithMatcher<internal::wstring> >
4066e35fdd936d133bf8a48de140a3c666897588a05shiqian    StartsWith(const internal::wstring& prefix) {
4067e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::StartsWithMatcher<internal::wstring>(
4068e35fdd936d133bf8a48de140a3c666897588a05shiqian      prefix));
4069e35fdd936d133bf8a48de140a3c666897588a05shiqian}
4070e35fdd936d133bf8a48de140a3c666897588a05shiqian
4071e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that ends with 'suffix' (case-sensitive).
4072e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::EndsWithMatcher<internal::wstring> >
4073e35fdd936d133bf8a48de140a3c666897588a05shiqian    EndsWith(const internal::wstring& suffix) {
4074e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::EndsWithMatcher<internal::wstring>(
4075e35fdd936d133bf8a48de140a3c666897588a05shiqian      suffix));
4076e35fdd936d133bf8a48de140a3c666897588a05shiqian}
4077e35fdd936d133bf8a48de140a3c666897588a05shiqian
4078e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif  // GTEST_HAS_GLOBAL_WSTRING || GTEST_HAS_STD_WSTRING
4079e35fdd936d133bf8a48de140a3c666897588a05shiqian
4080e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the
4081e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field == the second field.
4082e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Eq2Matcher Eq() { return internal::Eq2Matcher(); }
4083e35fdd936d133bf8a48de140a3c666897588a05shiqian
4084e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the
4085e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field >= the second field.
4086e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Ge2Matcher Ge() { return internal::Ge2Matcher(); }
4087e35fdd936d133bf8a48de140a3c666897588a05shiqian
4088e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the
4089e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field > the second field.
4090e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Gt2Matcher Gt() { return internal::Gt2Matcher(); }
4091e35fdd936d133bf8a48de140a3c666897588a05shiqian
4092e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the
4093e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field <= the second field.
4094e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Le2Matcher Le() { return internal::Le2Matcher(); }
4095e35fdd936d133bf8a48de140a3c666897588a05shiqian
4096e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the
4097e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field < the second field.
4098e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Lt2Matcher Lt() { return internal::Lt2Matcher(); }
4099e35fdd936d133bf8a48de140a3c666897588a05shiqian
4100e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the
4101e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field != the second field.
4102e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Ne2Matcher Ne() { return internal::Ne2Matcher(); }
4103e35fdd936d133bf8a48de140a3c666897588a05shiqian
4104e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any value of type T that m doesn't
4105e35fdd936d133bf8a48de140a3c666897588a05shiqian// match.
4106e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename InnerMatcher>
4107e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::NotMatcher<InnerMatcher> Not(InnerMatcher m) {
4108e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::NotMatcher<InnerMatcher>(m);
4109e35fdd936d133bf8a48de140a3c666897588a05shiqian}
4110e35fdd936d133bf8a48de140a3c666897588a05shiqian
4111e35fdd936d133bf8a48de140a3c666897588a05shiqian// Returns a matcher that matches anything that satisfies the given
4112e35fdd936d133bf8a48de140a3c666897588a05shiqian// predicate.  The predicate can be any unary function or functor
4113e35fdd936d133bf8a48de140a3c666897588a05shiqian// whose return type can be implicitly converted to bool.
4114e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Predicate>
4115e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::TrulyMatcher<Predicate> >
4116e35fdd936d133bf8a48de140a3c666897588a05shiqianTruly(Predicate pred) {
4117e35fdd936d133bf8a48de140a3c666897588a05shiqian  return MakePolymorphicMatcher(internal::TrulyMatcher<Predicate>(pred));
4118e35fdd936d133bf8a48de140a3c666897588a05shiqian}
4119e35fdd936d133bf8a48de140a3c666897588a05shiqian
4120a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan// Returns a matcher that matches the container size. The container must
4121a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan// support both size() and size_type which all STL-like containers provide.
4122a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan// Note that the parameter 'size' can be a value of type size_type as well as
4123a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan// matcher. For instance:
4124a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan//   EXPECT_THAT(container, SizeIs(2));     // Checks container has 2 elements.
4125a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan//   EXPECT_THAT(container, SizeIs(Le(2));  // Checks container has at most 2.
4126a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wantemplate <typename SizeMatcher>
4127a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.waninline internal::SizeIsMatcher<SizeMatcher>
4128a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wanSizeIs(const SizeMatcher& size_matcher) {
4129a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan  return internal::SizeIsMatcher<SizeMatcher>(size_matcher);
4130a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan}
4131a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan
4132b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak// Returns a matcher that matches the distance between the container's begin()
4133b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak// iterator and its end() iterator, i.e. the size of the container. This matcher
4134b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak// can be used instead of SizeIs with containers such as std::forward_list which
4135b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak// do not implement size(). The container must provide const_iterator (with
4136b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak// valid iterator_traits), begin() and end().
4137b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosaktemplate <typename DistanceMatcher>
4138b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosakinline internal::BeginEndDistanceIsMatcher<DistanceMatcher>
4139b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosakBeginEndDistanceIs(const DistanceMatcher& distance_matcher) {
4140b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak  return internal::BeginEndDistanceIsMatcher<DistanceMatcher>(distance_matcher);
4141b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak}
4142b6a348862b3ef745ae4c43117b67c7c6f877cd7ekosak
41436a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// Returns a matcher that matches an equal container.
41446a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// This matcher behaves like Eq(), but in the event of mismatch lists the
41456a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// values that are included in one container but not the other. (Duplicate
41466a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// values and order differences are not explained.)
41476a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wantemplate <typename Container>
4148821133180ccdb9eb062548d964929ba3b354eb84zhanyong.waninline PolymorphicMatcher<internal::ContainerEqMatcher<  // NOLINT
414902f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan                            GTEST_REMOVE_CONST_(Container)> >
41506a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan    ContainerEq(const Container& rhs) {
4151b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  // This following line is for working around a bug in MSVC 8.0,
4152b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  // which causes Container to be a const type sometimes.
415302f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan  typedef GTEST_REMOVE_CONST_(Container) RawContainer;
4154821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan  return MakePolymorphicMatcher(
4155821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan      internal::ContainerEqMatcher<RawContainer>(rhs));
4156b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}
4157b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
4158898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan// Returns a matcher that matches a container that, when sorted using
4159898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan// the given comparator, matches container_matcher.
4160898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wantemplate <typename Comparator, typename ContainerMatcher>
4161898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.waninline internal::WhenSortedByMatcher<Comparator, ContainerMatcher>
4162898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wanWhenSortedBy(const Comparator& comparator,
4163898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan             const ContainerMatcher& container_matcher) {
4164898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan  return internal::WhenSortedByMatcher<Comparator, ContainerMatcher>(
4165898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      comparator, container_matcher);
4166898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan}
4167898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
4168898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan// Returns a matcher that matches a container that, when sorted using
4169898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan// the < operator, matches container_matcher.
4170898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wantemplate <typename ContainerMatcher>
4171898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.waninline internal::WhenSortedByMatcher<internal::LessComparator, ContainerMatcher>
4172898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wanWhenSorted(const ContainerMatcher& container_matcher) {
4173898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan  return
4174898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan      internal::WhenSortedByMatcher<internal::LessComparator, ContainerMatcher>(
4175898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan          internal::LessComparator(), container_matcher);
4176898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan}
4177898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan
4178ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// Matches an STL-style container or a native array that contains the
4179ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// same number of elements as in rhs, where its i-th element and rhs's
4180ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// i-th element (as a pair) satisfy the given pair matcher, for all i.
4181ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// TupleMatcher must be able to be safely cast to Matcher<tuple<const
4182ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// T1&, const T2&> >, where T1 and T2 are the types of elements in the
4183ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// LHS container and the RHS container respectively.
4184ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wantemplate <typename TupleMatcher, typename Container>
4185ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.waninline internal::PointwiseMatcher<TupleMatcher,
4186ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan                                  GTEST_REMOVE_CONST_(Container)>
4187ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wanPointwise(const TupleMatcher& tuple_matcher, const Container& rhs) {
4188ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  // This following line is for working around a bug in MSVC 8.0,
41892336e9c171b5894bac98f828289f537e29691896kosak  // which causes Container to be a const type sometimes (e.g. when
41902336e9c171b5894bac98f828289f537e29691896kosak  // rhs is a const int[])..
4191ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  typedef GTEST_REMOVE_CONST_(Container) RawContainer;
4192ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan  return internal::PointwiseMatcher<TupleMatcher, RawContainer>(
4193ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan      tuple_matcher, rhs);
4194ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan}
4195ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan
41962336e9c171b5894bac98f828289f537e29691896kosak#if GTEST_HAS_STD_INITIALIZER_LIST_
41972336e9c171b5894bac98f828289f537e29691896kosak
41982336e9c171b5894bac98f828289f537e29691896kosak// Supports the Pointwise(m, {a, b, c}) syntax.
41992336e9c171b5894bac98f828289f537e29691896kosaktemplate <typename TupleMatcher, typename T>
42002336e9c171b5894bac98f828289f537e29691896kosakinline internal::PointwiseMatcher<TupleMatcher, std::vector<T> > Pointwise(
42012336e9c171b5894bac98f828289f537e29691896kosak    const TupleMatcher& tuple_matcher, std::initializer_list<T> rhs) {
42022336e9c171b5894bac98f828289f537e29691896kosak  return Pointwise(tuple_matcher, std::vector<T>(rhs));
42032336e9c171b5894bac98f828289f537e29691896kosak}
42042336e9c171b5894bac98f828289f537e29691896kosak
42052336e9c171b5894bac98f828289f537e29691896kosak#endif  // GTEST_HAS_STD_INITIALIZER_LIST_
42062336e9c171b5894bac98f828289f537e29691896kosak
42072336e9c171b5894bac98f828289f537e29691896kosak// UnorderedPointwise(pair_matcher, rhs) matches an STL-style
42082336e9c171b5894bac98f828289f537e29691896kosak// container or a native array that contains the same number of
42092336e9c171b5894bac98f828289f537e29691896kosak// elements as in rhs, where in some permutation of the container, its
42102336e9c171b5894bac98f828289f537e29691896kosak// i-th element and rhs's i-th element (as a pair) satisfy the given
42112336e9c171b5894bac98f828289f537e29691896kosak// pair matcher, for all i.  Tuple2Matcher must be able to be safely
42122336e9c171b5894bac98f828289f537e29691896kosak// cast to Matcher<tuple<const T1&, const T2&> >, where T1 and T2 are
42132336e9c171b5894bac98f828289f537e29691896kosak// the types of elements in the LHS container and the RHS container
42142336e9c171b5894bac98f828289f537e29691896kosak// respectively.
42152336e9c171b5894bac98f828289f537e29691896kosak//
42162336e9c171b5894bac98f828289f537e29691896kosak// This is like Pointwise(pair_matcher, rhs), except that the element
42172336e9c171b5894bac98f828289f537e29691896kosak// order doesn't matter.
42182336e9c171b5894bac98f828289f537e29691896kosaktemplate <typename Tuple2Matcher, typename RhsContainer>
42192336e9c171b5894bac98f828289f537e29691896kosakinline internal::UnorderedElementsAreArrayMatcher<
42202336e9c171b5894bac98f828289f537e29691896kosak    typename internal::BoundSecondMatcher<
42212336e9c171b5894bac98f828289f537e29691896kosak        Tuple2Matcher, typename internal::StlContainerView<GTEST_REMOVE_CONST_(
42222336e9c171b5894bac98f828289f537e29691896kosak                           RhsContainer)>::type::value_type> >
42232336e9c171b5894bac98f828289f537e29691896kosakUnorderedPointwise(const Tuple2Matcher& tuple2_matcher,
42242336e9c171b5894bac98f828289f537e29691896kosak                   const RhsContainer& rhs_container) {
42252336e9c171b5894bac98f828289f537e29691896kosak  // This following line is for working around a bug in MSVC 8.0,
42262336e9c171b5894bac98f828289f537e29691896kosak  // which causes RhsContainer to be a const type sometimes (e.g. when
42272336e9c171b5894bac98f828289f537e29691896kosak  // rhs_container is a const int[]).
42282336e9c171b5894bac98f828289f537e29691896kosak  typedef GTEST_REMOVE_CONST_(RhsContainer) RawRhsContainer;
42292336e9c171b5894bac98f828289f537e29691896kosak
42302336e9c171b5894bac98f828289f537e29691896kosak  // RhsView allows the same code to handle RhsContainer being a
42312336e9c171b5894bac98f828289f537e29691896kosak  // STL-style container and it being a native C-style array.
42322336e9c171b5894bac98f828289f537e29691896kosak  typedef typename internal::StlContainerView<RawRhsContainer> RhsView;
42332336e9c171b5894bac98f828289f537e29691896kosak  typedef typename RhsView::type RhsStlContainer;
42342336e9c171b5894bac98f828289f537e29691896kosak  typedef typename RhsStlContainer::value_type Second;
42352336e9c171b5894bac98f828289f537e29691896kosak  const RhsStlContainer& rhs_stl_container =
42362336e9c171b5894bac98f828289f537e29691896kosak      RhsView::ConstReference(rhs_container);
42372336e9c171b5894bac98f828289f537e29691896kosak
42382336e9c171b5894bac98f828289f537e29691896kosak  // Create a matcher for each element in rhs_container.
42392336e9c171b5894bac98f828289f537e29691896kosak  ::std::vector<internal::BoundSecondMatcher<Tuple2Matcher, Second> > matchers;
42402336e9c171b5894bac98f828289f537e29691896kosak  for (typename RhsStlContainer::const_iterator it = rhs_stl_container.begin();
42412336e9c171b5894bac98f828289f537e29691896kosak       it != rhs_stl_container.end(); ++it) {
42422336e9c171b5894bac98f828289f537e29691896kosak    matchers.push_back(
42432336e9c171b5894bac98f828289f537e29691896kosak        internal::MatcherBindSecond(tuple2_matcher, *it));
42442336e9c171b5894bac98f828289f537e29691896kosak  }
42452336e9c171b5894bac98f828289f537e29691896kosak
42462336e9c171b5894bac98f828289f537e29691896kosak  // Delegate the work to UnorderedElementsAreArray().
42472336e9c171b5894bac98f828289f537e29691896kosak  return UnorderedElementsAreArray(matchers);
42482336e9c171b5894bac98f828289f537e29691896kosak}
42492336e9c171b5894bac98f828289f537e29691896kosak
42502336e9c171b5894bac98f828289f537e29691896kosak#if GTEST_HAS_STD_INITIALIZER_LIST_
42512336e9c171b5894bac98f828289f537e29691896kosak
42522336e9c171b5894bac98f828289f537e29691896kosak// Supports the UnorderedPointwise(m, {a, b, c}) syntax.
42532336e9c171b5894bac98f828289f537e29691896kosaktemplate <typename Tuple2Matcher, typename T>
42542336e9c171b5894bac98f828289f537e29691896kosakinline internal::UnorderedElementsAreArrayMatcher<
42552336e9c171b5894bac98f828289f537e29691896kosak    typename internal::BoundSecondMatcher<Tuple2Matcher, T> >
42562336e9c171b5894bac98f828289f537e29691896kosakUnorderedPointwise(const Tuple2Matcher& tuple2_matcher,
42572336e9c171b5894bac98f828289f537e29691896kosak                   std::initializer_list<T> rhs) {
42582336e9c171b5894bac98f828289f537e29691896kosak  return UnorderedPointwise(tuple2_matcher, std::vector<T>(rhs));
42592336e9c171b5894bac98f828289f537e29691896kosak}
42602336e9c171b5894bac98f828289f537e29691896kosak
42612336e9c171b5894bac98f828289f537e29691896kosak#endif  // GTEST_HAS_STD_INITIALIZER_LIST_
42622336e9c171b5894bac98f828289f537e29691896kosak
4263b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Matches an STL-style container or a native array that contains at
4264b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// least one element matching the given value or matcher.
4265b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//
4266b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Examples:
4267b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   ::std::set<int> page_ids;
4268b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   page_ids.insert(3);
4269b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   page_ids.insert(1);
4270b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   EXPECT_THAT(page_ids, Contains(1));
4271b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   EXPECT_THAT(page_ids, Contains(Gt(2)));
4272b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   EXPECT_THAT(page_ids, Not(Contains(4)));
4273b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//
4274b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   ::std::map<int, size_t> page_lengths;
4275b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   page_lengths[1] = 100;
42764019819dbb5369b1d638503523a04aaf7eb2a5adzhanyong.wan//   EXPECT_THAT(page_lengths,
42774019819dbb5369b1d638503523a04aaf7eb2a5adzhanyong.wan//               Contains(::std::pair<const int, size_t>(1, 100)));
4278b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//
4279b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   const char* user_ids[] = { "joe", "mike", "tom" };
4280b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan//   EXPECT_THAT(user_ids, Contains(Eq(::std::string("tom"))));
4281b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename M>
4282b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.waninline internal::ContainsMatcher<M> Contains(M matcher) {
4283b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  return internal::ContainsMatcher<M>(matcher);
42846a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan}
42856a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan
428633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Matches an STL-style container or a native array that contains only
428733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// elements matching the given value or matcher.
428833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//
428933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Each(m) is semantically equivalent to Not(Contains(Not(m))). Only
429033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// the messages are different.
429133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//
429233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Examples:
429333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   ::std::set<int> page_ids;
429433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   // Each(m) matches an empty container, regardless of what m is.
429533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   EXPECT_THAT(page_ids, Each(Eq(1)));
429633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   EXPECT_THAT(page_ids, Each(Eq(77)));
429733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//
429833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   page_ids.insert(3);
429933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   EXPECT_THAT(page_ids, Each(Gt(0)));
430033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   EXPECT_THAT(page_ids, Not(Each(Gt(4))));
430133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   page_ids.insert(1);
430233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   EXPECT_THAT(page_ids, Not(Each(Lt(2))));
430333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//
430433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   ::std::map<int, size_t> page_lengths;
430533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   page_lengths[1] = 100;
430633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   page_lengths[2] = 200;
430733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   page_lengths[3] = 300;
430833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   EXPECT_THAT(page_lengths, Not(Each(Pair(1, 100))));
430933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   EXPECT_THAT(page_lengths, Each(Key(Le(3))));
431033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//
431133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   const char* user_ids[] = { "joe", "mike", "tom" };
431233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan//   EXPECT_THAT(user_ids, Not(Each(Eq(::std::string("tom")))));
431333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wantemplate <typename M>
431433605ba45417979cff7254c1196dfee65a7275b3zhanyong.waninline internal::EachMatcher<M> Each(M matcher) {
431533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan  return internal::EachMatcher<M>(matcher);
431633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan}
431733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan
4318b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// Key(inner_matcher) matches an std::pair whose 'first' field matches
4319b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// inner_matcher.  For example, Contains(Key(Ge(5))) can be used to match an
4320b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// std::map that contains at least one element whose key is >= 5.
4321b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wantemplate <typename M>
4322b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.waninline internal::KeyMatcher<M> Key(M inner_matcher) {
4323b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan  return internal::KeyMatcher<M>(inner_matcher);
4324b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan}
4325b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan
4326f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// Pair(first_matcher, second_matcher) matches a std::pair whose 'first' field
4327f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// matches first_matcher and whose 'second' field matches second_matcher.  For
4328f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// example, EXPECT_THAT(map_type, ElementsAre(Pair(Ge(5), "foo"))) can be used
4329f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// to match a std::map<int, string> that contains exactly one element whose key
4330f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// is >= 5 and whose value equals "foo".
4331f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wantemplate <typename FirstMatcher, typename SecondMatcher>
4332f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.waninline internal::PairMatcher<FirstMatcher, SecondMatcher>
4333f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wanPair(FirstMatcher first_matcher, SecondMatcher second_matcher) {
4334f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan  return internal::PairMatcher<FirstMatcher, SecondMatcher>(
4335f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan      first_matcher, second_matcher);
4336f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan}
4337f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan
4338e35fdd936d133bf8a48de140a3c666897588a05shiqian// Returns a predicate that is satisfied by anything that matches the
4339e35fdd936d133bf8a48de140a3c666897588a05shiqian// given matcher.
4340e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename M>
4341e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::MatcherAsPredicate<M> Matches(M matcher) {
4342e35fdd936d133bf8a48de140a3c666897588a05shiqian  return internal::MatcherAsPredicate<M>(matcher);
4343e35fdd936d133bf8a48de140a3c666897588a05shiqian}
4344e35fdd936d133bf8a48de140a3c666897588a05shiqian
4345b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Returns true iff the value matches the matcher.
4346b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename T, typename M>
4347b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.waninline bool Value(const T& value, M matcher) {
4348b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan  return testing::Matches(matcher)(value);
4349b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}
4350b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan
435134b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wan// Matches the value against the given matcher and explains the match
435234b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wan// result to listener.
435334b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wantemplate <typename T, typename M>
4354a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.waninline bool ExplainMatchResult(
435534b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wan    M matcher, const T& value, MatchResultListener* listener) {
435634b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wan  return SafeMatcherCast<const T&>(matcher).MatchAndExplain(value, listener);
435734b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wan}
435834b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wan
4359616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan#if GTEST_LANG_CXX11
4360616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// Define variadic matcher versions. They are overloaded in
4361616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// gmock-generated-matchers.h for the cases supported by pre C++11 compilers.
4362616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wantemplate <typename... Args>
4363616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.waninline internal::AllOfMatcher<Args...> AllOf(const Args&... matchers) {
4364616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  return internal::AllOfMatcher<Args...>(matchers...);
4365616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan}
4366616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
4367616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wantemplate <typename... Args>
4368616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.waninline internal::AnyOfMatcher<Args...> AnyOf(const Args&... matchers) {
4369616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan  return internal::AnyOfMatcher<Args...>(matchers...);
4370616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan}
4371616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
4372616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan#endif  // GTEST_LANG_CXX11
4373616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan
4374bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan// AllArgs(m) is a synonym of m.  This is useful in
4375bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan//
4376bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan//   EXPECT_CALL(foo, Bar(_, _)).With(AllArgs(Eq()));
4377bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan//
4378bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan// which is easier to read than
4379bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan//
4380bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan//   EXPECT_CALL(foo, Bar(_, _)).With(Eq());
4381bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wantemplate <typename InnerMatcher>
4382bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.waninline InnerMatcher AllArgs(const InnerMatcher& matcher) { return matcher; }
4383bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan
4384e35fdd936d133bf8a48de140a3c666897588a05shiqian// These macros allow using matchers to check values in Google Test
4385e35fdd936d133bf8a48de140a3c666897588a05shiqian// tests.  ASSERT_THAT(value, matcher) and EXPECT_THAT(value, matcher)
4386e35fdd936d133bf8a48de140a3c666897588a05shiqian// succeed iff the value matches the matcher.  If the assertion fails,
4387e35fdd936d133bf8a48de140a3c666897588a05shiqian// the value and the description of the matcher will be printed.
4388e35fdd936d133bf8a48de140a3c666897588a05shiqian#define ASSERT_THAT(value, matcher) ASSERT_PRED_FORMAT1(\
4389e35fdd936d133bf8a48de140a3c666897588a05shiqian    ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
4390e35fdd936d133bf8a48de140a3c666897588a05shiqian#define EXPECT_THAT(value, matcher) EXPECT_PRED_FORMAT1(\
4391e35fdd936d133bf8a48de140a3c666897588a05shiqian    ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value)
4392e35fdd936d133bf8a48de140a3c666897588a05shiqian
4393e35fdd936d133bf8a48de140a3c666897588a05shiqian}  // namespace testing
4394e35fdd936d133bf8a48de140a3c666897588a05shiqian
43956702b97d5e012f9eb490daa6cfe4cef98f312b6bkosak// Include any custom callback matchers added by the local installation.
43966702b97d5e012f9eb490daa6cfe4cef98f312b6bkosak// We must include this header at the end to make sure it can use the
43976702b97d5e012f9eb490daa6cfe4cef98f312b6bkosak// declarations from this file.
43986702b97d5e012f9eb490daa6cfe4cef98f312b6bkosak#include "gmock/internal/custom/gmock-matchers.h"
4399e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif  // GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_
4400