gmock-matchers.h revision a862f1de30ed1ef259bcec61c5939200b29c765c
1e35fdd936d133bf8a48de140a3c666897588a05shiqian// Copyright 2007, Google Inc. 2e35fdd936d133bf8a48de140a3c666897588a05shiqian// All rights reserved. 3e35fdd936d133bf8a48de140a3c666897588a05shiqian// 4e35fdd936d133bf8a48de140a3c666897588a05shiqian// Redistribution and use in source and binary forms, with or without 5e35fdd936d133bf8a48de140a3c666897588a05shiqian// modification, are permitted provided that the following conditions are 6e35fdd936d133bf8a48de140a3c666897588a05shiqian// met: 7e35fdd936d133bf8a48de140a3c666897588a05shiqian// 8e35fdd936d133bf8a48de140a3c666897588a05shiqian// * Redistributions of source code must retain the above copyright 9e35fdd936d133bf8a48de140a3c666897588a05shiqian// notice, this list of conditions and the following disclaimer. 10e35fdd936d133bf8a48de140a3c666897588a05shiqian// * Redistributions in binary form must reproduce the above 11e35fdd936d133bf8a48de140a3c666897588a05shiqian// copyright notice, this list of conditions and the following disclaimer 12e35fdd936d133bf8a48de140a3c666897588a05shiqian// in the documentation and/or other materials provided with the 13e35fdd936d133bf8a48de140a3c666897588a05shiqian// distribution. 14e35fdd936d133bf8a48de140a3c666897588a05shiqian// * Neither the name of Google Inc. nor the names of its 15e35fdd936d133bf8a48de140a3c666897588a05shiqian// contributors may be used to endorse or promote products derived from 16e35fdd936d133bf8a48de140a3c666897588a05shiqian// this software without specific prior written permission. 17e35fdd936d133bf8a48de140a3c666897588a05shiqian// 18e35fdd936d133bf8a48de140a3c666897588a05shiqian// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19e35fdd936d133bf8a48de140a3c666897588a05shiqian// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20e35fdd936d133bf8a48de140a3c666897588a05shiqian// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21e35fdd936d133bf8a48de140a3c666897588a05shiqian// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22e35fdd936d133bf8a48de140a3c666897588a05shiqian// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23e35fdd936d133bf8a48de140a3c666897588a05shiqian// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24e35fdd936d133bf8a48de140a3c666897588a05shiqian// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25e35fdd936d133bf8a48de140a3c666897588a05shiqian// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26e35fdd936d133bf8a48de140a3c666897588a05shiqian// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27e35fdd936d133bf8a48de140a3c666897588a05shiqian// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28e35fdd936d133bf8a48de140a3c666897588a05shiqian// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29e35fdd936d133bf8a48de140a3c666897588a05shiqian// 30e35fdd936d133bf8a48de140a3c666897588a05shiqian// Author: wan@google.com (Zhanyong Wan) 31e35fdd936d133bf8a48de140a3c666897588a05shiqian 32e35fdd936d133bf8a48de140a3c666897588a05shiqian// Google Mock - a framework for writing C++ mock classes. 33e35fdd936d133bf8a48de140a3c666897588a05shiqian// 34e35fdd936d133bf8a48de140a3c666897588a05shiqian// This file implements some commonly used argument matchers. More 35e35fdd936d133bf8a48de140a3c666897588a05shiqian// matchers can be defined by the user implementing the 36e35fdd936d133bf8a48de140a3c666897588a05shiqian// MatcherInterface<T> interface if necessary. 37e35fdd936d133bf8a48de140a3c666897588a05shiqian 38e35fdd936d133bf8a48de140a3c666897588a05shiqian#ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_ 39e35fdd936d133bf8a48de140a3c666897588a05shiqian#define GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_ 40e35fdd936d133bf8a48de140a3c666897588a05shiqian 416a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan#include <algorithm> 4216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#include <limits> 43e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <ostream> // NOLINT 44e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <sstream> 45e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <string> 46e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <vector> 47e35fdd936d133bf8a48de140a3c666897588a05shiqian 48e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <gmock/gmock-printers.h> 49e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <gmock/internal/gmock-internal-utils.h> 50e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <gmock/internal/gmock-port.h> 51e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <gtest/gtest.h> 52e35fdd936d133bf8a48de140a3c666897588a05shiqian 53e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace testing { 54e35fdd936d133bf8a48de140a3c666897588a05shiqian 55e35fdd936d133bf8a48de140a3c666897588a05shiqian// To implement a matcher Foo for type T, define: 56e35fdd936d133bf8a48de140a3c666897588a05shiqian// 1. a class FooMatcherImpl that implements the 57e35fdd936d133bf8a48de140a3c666897588a05shiqian// MatcherInterface<T> interface, and 58e35fdd936d133bf8a48de140a3c666897588a05shiqian// 2. a factory function that creates a Matcher<T> object from a 59e35fdd936d133bf8a48de140a3c666897588a05shiqian// FooMatcherImpl*. 60e35fdd936d133bf8a48de140a3c666897588a05shiqian// 61e35fdd936d133bf8a48de140a3c666897588a05shiqian// The two-level delegation design makes it possible to allow a user 62e35fdd936d133bf8a48de140a3c666897588a05shiqian// to write "v" instead of "Eq(v)" where a Matcher is expected, which 63e35fdd936d133bf8a48de140a3c666897588a05shiqian// is impossible if we pass matchers by pointers. It also eases 64e35fdd936d133bf8a48de140a3c666897588a05shiqian// ownership management as Matcher objects can now be copied like 65e35fdd936d133bf8a48de140a3c666897588a05shiqian// plain values. 66e35fdd936d133bf8a48de140a3c666897588a05shiqian 67821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// MatchResultListener is an abstract class. Its << operator can be 68821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// used by a matcher to explain why a value matches or doesn't match. 69821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// 70821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// TODO(wan@google.com): add method 71821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// bool InterestedInWhy(bool result) const; 72821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// to indicate whether the listener is interested in why the match 73821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// result is 'result'. 74821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wanclass MatchResultListener { 75821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan public: 76821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // Creates a listener object with the given underlying ostream. The 77821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // listener does not own the ostream. 78821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan explicit MatchResultListener(::std::ostream* os) : stream_(os) {} 79821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual ~MatchResultListener() = 0; // Makes this class abstract. 80821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 81821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // Streams x to the underlying ostream; does nothing if the ostream 82821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // is NULL. 83821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan template <typename T> 84821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan MatchResultListener& operator<<(const T& x) { 85821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (stream_ != NULL) 86821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *stream_ << x; 87821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return *this; 88821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan } 89821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 90821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // Returns the underlying ostream. 91821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan ::std::ostream* stream() { return stream_; } 92821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 93a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan // Returns true iff the listener is interested in an explanation of 94a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan // the match result. A matcher's MatchAndExplain() method can use 95a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan // this information to avoid generating the explanation when no one 96a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan // intends to hear it. 97a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan bool IsInterested() const { return stream_ != NULL; } 98a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan 99821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan private: 100821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan ::std::ostream* const stream_; 101821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 102821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan GTEST_DISALLOW_COPY_AND_ASSIGN_(MatchResultListener); 103821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan}; 104821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 105821133180ccdb9eb062548d964929ba3b354eb84zhanyong.waninline MatchResultListener::~MatchResultListener() { 106821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan} 107821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 108e35fdd936d133bf8a48de140a3c666897588a05shiqian// The implementation of a matcher. 109e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 110e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatcherInterface { 111e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 112e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual ~MatcherInterface() {} 113e35fdd936d133bf8a48de140a3c666897588a05shiqian 114821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // Returns true iff the matcher matches x; also explains the match 115a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan // result to 'listener', in the form of a non-restrictive relative 116a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan // clause ("which ...", "whose ...", etc) that describes x. For 117a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan // example, the MatchAndExplain() method of the Pointee(...) matcher 118a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan // should generate an explanation like "which points to ...". 119821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // 120db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan // You should override this method when defining a new matcher. 121821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // 122821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // It's the responsibility of the caller (Google Mock) to guarantee 123821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // that 'listener' is not NULL. This helps to simplify a matcher's 124821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // implementation when it doesn't care about the performance, as it 125821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // can talk to 'listener' without checking its validity first. 126821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // However, in order to implement dummy listeners efficiently, 127821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // listener->stream() may be NULL. 128db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan virtual bool MatchAndExplain(T x, MatchResultListener* listener) const = 0; 129e35fdd936d133bf8a48de140a3c666897588a05shiqian 130a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan // Describes this matcher to an ostream. The function should print 131a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan // a verb phrase that describes the property a value matching this 132a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan // matcher should have. The subject of the verb phrase is the value 133a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan // being matched. For example, the DescribeTo() method of the Gt(7) 134a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan // matcher prints "is greater than 7". 135e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeTo(::std::ostream* os) const = 0; 136e35fdd936d133bf8a48de140a3c666897588a05shiqian 137e35fdd936d133bf8a48de140a3c666897588a05shiqian // Describes the negation of this matcher to an ostream. For 138e35fdd936d133bf8a48de140a3c666897588a05shiqian // example, if the description of this matcher is "is greater than 139e35fdd936d133bf8a48de140a3c666897588a05shiqian // 7", the negated description could be "is not greater than 7". 140e35fdd936d133bf8a48de140a3c666897588a05shiqian // You are not required to override this when implementing 141e35fdd936d133bf8a48de140a3c666897588a05shiqian // MatcherInterface, but it is highly advised so that your matcher 142e35fdd936d133bf8a48de140a3c666897588a05shiqian // can produce good error messages. 143e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeNegationTo(::std::ostream* os) const { 144e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "not ("; 145e35fdd936d133bf8a48de140a3c666897588a05shiqian DescribeTo(os); 146e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << ")"; 147e35fdd936d133bf8a48de140a3c666897588a05shiqian } 148e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 149e35fdd936d133bf8a48de140a3c666897588a05shiqian 150e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace internal { 151e35fdd936d133bf8a48de140a3c666897588a05shiqian 152821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// A match result listener that ignores the explanation. 153821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wanclass DummyMatchResultListener : public MatchResultListener { 154821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan public: 155821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan DummyMatchResultListener() : MatchResultListener(NULL) {} 156821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 157821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan private: 158821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan GTEST_DISALLOW_COPY_AND_ASSIGN_(DummyMatchResultListener); 159821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan}; 160821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 161821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// A match result listener that forwards the explanation to a given 162821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// ostream. The difference between this and MatchResultListener is 163821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// that the former is concrete. 164821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wanclass StreamMatchResultListener : public MatchResultListener { 165821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan public: 166821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan explicit StreamMatchResultListener(::std::ostream* os) 167821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan : MatchResultListener(os) {} 168821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 169821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan private: 170821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan GTEST_DISALLOW_COPY_AND_ASSIGN_(StreamMatchResultListener); 171821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan}; 172821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 173821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// A match result listener that stores the explanation in a string. 174821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wanclass StringMatchResultListener : public MatchResultListener { 175821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan public: 176821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan StringMatchResultListener() : MatchResultListener(&ss_) {} 177821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 178821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // Returns the explanation heard so far. 179821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan internal::string str() const { return ss_.str(); } 180821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 181821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan private: 182821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan ::std::stringstream ss_; 183821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 184821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan GTEST_DISALLOW_COPY_AND_ASSIGN_(StringMatchResultListener); 185821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan}; 186821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 187e35fdd936d133bf8a48de140a3c666897588a05shiqian// An internal class for implementing Matcher<T>, which will derive 188e35fdd936d133bf8a48de140a3c666897588a05shiqian// from it. We put functionalities common to all Matcher<T> 189e35fdd936d133bf8a48de140a3c666897588a05shiqian// specializations here to avoid code duplication. 190e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 191e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatcherBase { 192e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 193821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // Returns true iff the matcher matches x; also explains the match 194821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // result to 'listener'. 195821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan bool MatchAndExplain(T x, MatchResultListener* listener) const { 196821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return impl_->MatchAndExplain(x, listener); 197821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan } 198821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 199e35fdd936d133bf8a48de140a3c666897588a05shiqian // Returns true iff this matcher matches x. 200821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan bool Matches(T x) const { 201821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan DummyMatchResultListener dummy; 202821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return MatchAndExplain(x, &dummy); 203821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan } 204e35fdd936d133bf8a48de140a3c666897588a05shiqian 205e35fdd936d133bf8a48de140a3c666897588a05shiqian // Describes this matcher to an ostream. 206e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeTo(::std::ostream* os) const { impl_->DescribeTo(os); } 207e35fdd936d133bf8a48de140a3c666897588a05shiqian 208e35fdd936d133bf8a48de140a3c666897588a05shiqian // Describes the negation of this matcher to an ostream. 209e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeNegationTo(::std::ostream* os) const { 210e35fdd936d133bf8a48de140a3c666897588a05shiqian impl_->DescribeNegationTo(os); 211e35fdd936d133bf8a48de140a3c666897588a05shiqian } 212e35fdd936d133bf8a48de140a3c666897588a05shiqian 213e35fdd936d133bf8a48de140a3c666897588a05shiqian // Explains why x matches, or doesn't match, the matcher. 214e35fdd936d133bf8a48de140a3c666897588a05shiqian void ExplainMatchResultTo(T x, ::std::ostream* os) const { 215821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan StreamMatchResultListener listener(os); 216821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan MatchAndExplain(x, &listener); 217e35fdd936d133bf8a48de140a3c666897588a05shiqian } 21832de5f53763125925e078498250f7e73a88de9edzhanyong.wan 219e35fdd936d133bf8a48de140a3c666897588a05shiqian protected: 220e35fdd936d133bf8a48de140a3c666897588a05shiqian MatcherBase() {} 221e35fdd936d133bf8a48de140a3c666897588a05shiqian 222e35fdd936d133bf8a48de140a3c666897588a05shiqian // Constructs a matcher from its implementation. 223e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit MatcherBase(const MatcherInterface<T>* impl) 224e35fdd936d133bf8a48de140a3c666897588a05shiqian : impl_(impl) {} 225e35fdd936d133bf8a48de140a3c666897588a05shiqian 226e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual ~MatcherBase() {} 22732de5f53763125925e078498250f7e73a88de9edzhanyong.wan 228e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 229e35fdd936d133bf8a48de140a3c666897588a05shiqian // shared_ptr (util/gtl/shared_ptr.h) and linked_ptr have similar 230e35fdd936d133bf8a48de140a3c666897588a05shiqian // interfaces. The former dynamically allocates a chunk of memory 231e35fdd936d133bf8a48de140a3c666897588a05shiqian // to hold the reference count, while the latter tracks all 232e35fdd936d133bf8a48de140a3c666897588a05shiqian // references using a circular linked list without allocating 233e35fdd936d133bf8a48de140a3c666897588a05shiqian // memory. It has been observed that linked_ptr performs better in 234e35fdd936d133bf8a48de140a3c666897588a05shiqian // typical scenarios. However, shared_ptr can out-perform 235e35fdd936d133bf8a48de140a3c666897588a05shiqian // linked_ptr when there are many more uses of the copy constructor 236e35fdd936d133bf8a48de140a3c666897588a05shiqian // than the default constructor. 237e35fdd936d133bf8a48de140a3c666897588a05shiqian // 238e35fdd936d133bf8a48de140a3c666897588a05shiqian // If performance becomes a problem, we should see if using 239e35fdd936d133bf8a48de140a3c666897588a05shiqian // shared_ptr helps. 240e35fdd936d133bf8a48de140a3c666897588a05shiqian ::testing::internal::linked_ptr<const MatcherInterface<T> > impl_; 241e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 242e35fdd936d133bf8a48de140a3c666897588a05shiqian 243e35fdd936d133bf8a48de140a3c666897588a05shiqian} // namespace internal 244e35fdd936d133bf8a48de140a3c666897588a05shiqian 245e35fdd936d133bf8a48de140a3c666897588a05shiqian// A Matcher<T> is a copyable and IMMUTABLE (except by assignment) 246e35fdd936d133bf8a48de140a3c666897588a05shiqian// object that can check whether a value of type T matches. The 247e35fdd936d133bf8a48de140a3c666897588a05shiqian// implementation of Matcher<T> is just a linked_ptr to const 248e35fdd936d133bf8a48de140a3c666897588a05shiqian// MatcherInterface<T>, so copying is fairly cheap. Don't inherit 249e35fdd936d133bf8a48de140a3c666897588a05shiqian// from Matcher! 250e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 251e35fdd936d133bf8a48de140a3c666897588a05shiqianclass Matcher : public internal::MatcherBase<T> { 252e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 253e35fdd936d133bf8a48de140a3c666897588a05shiqian // Constructs a null matcher. Needed for storing Matcher objects in 254e35fdd936d133bf8a48de140a3c666897588a05shiqian // STL containers. 255e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher() {} 256e35fdd936d133bf8a48de140a3c666897588a05shiqian 257e35fdd936d133bf8a48de140a3c666897588a05shiqian // Constructs a matcher from its implementation. 258e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit Matcher(const MatcherInterface<T>* impl) 259e35fdd936d133bf8a48de140a3c666897588a05shiqian : internal::MatcherBase<T>(impl) {} 260e35fdd936d133bf8a48de140a3c666897588a05shiqian 26118490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan // Implicit constructor here allows people to write 262e35fdd936d133bf8a48de140a3c666897588a05shiqian // EXPECT_CALL(foo, Bar(5)) instead of EXPECT_CALL(foo, Bar(Eq(5))) sometimes 263e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher(T value); // NOLINT 264e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 265e35fdd936d133bf8a48de140a3c666897588a05shiqian 266e35fdd936d133bf8a48de140a3c666897588a05shiqian// The following two specializations allow the user to write str 267e35fdd936d133bf8a48de140a3c666897588a05shiqian// instead of Eq(str) and "foo" instead of Eq("foo") when a string 268e35fdd936d133bf8a48de140a3c666897588a05shiqian// matcher is expected. 269e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <> 270e35fdd936d133bf8a48de140a3c666897588a05shiqianclass Matcher<const internal::string&> 271e35fdd936d133bf8a48de140a3c666897588a05shiqian : public internal::MatcherBase<const internal::string&> { 272e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 273e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher() {} 274e35fdd936d133bf8a48de140a3c666897588a05shiqian 275e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit Matcher(const MatcherInterface<const internal::string&>* impl) 276e35fdd936d133bf8a48de140a3c666897588a05shiqian : internal::MatcherBase<const internal::string&>(impl) {} 277e35fdd936d133bf8a48de140a3c666897588a05shiqian 278e35fdd936d133bf8a48de140a3c666897588a05shiqian // Allows the user to write str instead of Eq(str) sometimes, where 279e35fdd936d133bf8a48de140a3c666897588a05shiqian // str is a string object. 280e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher(const internal::string& s); // NOLINT 281e35fdd936d133bf8a48de140a3c666897588a05shiqian 282e35fdd936d133bf8a48de140a3c666897588a05shiqian // Allows the user to write "foo" instead of Eq("foo") sometimes. 283e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher(const char* s); // NOLINT 284e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 285e35fdd936d133bf8a48de140a3c666897588a05shiqian 286e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <> 287e35fdd936d133bf8a48de140a3c666897588a05shiqianclass Matcher<internal::string> 288e35fdd936d133bf8a48de140a3c666897588a05shiqian : public internal::MatcherBase<internal::string> { 289e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 290e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher() {} 291e35fdd936d133bf8a48de140a3c666897588a05shiqian 292e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit Matcher(const MatcherInterface<internal::string>* impl) 293e35fdd936d133bf8a48de140a3c666897588a05shiqian : internal::MatcherBase<internal::string>(impl) {} 294e35fdd936d133bf8a48de140a3c666897588a05shiqian 295e35fdd936d133bf8a48de140a3c666897588a05shiqian // Allows the user to write str instead of Eq(str) sometimes, where 296e35fdd936d133bf8a48de140a3c666897588a05shiqian // str is a string object. 297e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher(const internal::string& s); // NOLINT 298e35fdd936d133bf8a48de140a3c666897588a05shiqian 299e35fdd936d133bf8a48de140a3c666897588a05shiqian // Allows the user to write "foo" instead of Eq("foo") sometimes. 300e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher(const char* s); // NOLINT 301e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 302e35fdd936d133bf8a48de140a3c666897588a05shiqian 303e35fdd936d133bf8a48de140a3c666897588a05shiqian// The PolymorphicMatcher class template makes it easy to implement a 304e35fdd936d133bf8a48de140a3c666897588a05shiqian// polymorphic matcher (i.e. a matcher that can match values of more 305e35fdd936d133bf8a48de140a3c666897588a05shiqian// than one type, e.g. Eq(n) and NotNull()). 306e35fdd936d133bf8a48de140a3c666897588a05shiqian// 307db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan// To define a polymorphic matcher, a user should provide an Impl 308db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan// class that has a DescribeTo() method and a DescribeNegationTo() 309db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan// method, and define a member function (or member function template) 310821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// 311db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan// bool MatchAndExplain(const Value& value, 312db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan// MatchResultListener* listener) const; 313821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// 314821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// See the definition of NotNull() for a complete example. 315e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <class Impl> 316e35fdd936d133bf8a48de140a3c666897588a05shiqianclass PolymorphicMatcher { 317e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 31832de5f53763125925e078498250f7e73a88de9edzhanyong.wan explicit PolymorphicMatcher(const Impl& an_impl) : impl_(an_impl) {} 319e35fdd936d133bf8a48de140a3c666897588a05shiqian 3202b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan // Returns a mutable reference to the underlying matcher 3212b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan // implementation object. 3222b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan Impl& mutable_impl() { return impl_; } 3232b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan 3242b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan // Returns an immutable reference to the underlying matcher 3252b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan // implementation object. 3262b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan const Impl& impl() const { return impl_; } 3272b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan 328e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 329e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<T>() const { 330e35fdd936d133bf8a48de140a3c666897588a05shiqian return Matcher<T>(new MonomorphicImpl<T>(impl_)); 331e35fdd936d133bf8a48de140a3c666897588a05shiqian } 33232de5f53763125925e078498250f7e73a88de9edzhanyong.wan 333e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 334e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 335e35fdd936d133bf8a48de140a3c666897588a05shiqian class MonomorphicImpl : public MatcherInterface<T> { 336e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 337e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {} 338e35fdd936d133bf8a48de140a3c666897588a05shiqian 339e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeTo(::std::ostream* os) const { 340e35fdd936d133bf8a48de140a3c666897588a05shiqian impl_.DescribeTo(os); 341e35fdd936d133bf8a48de140a3c666897588a05shiqian } 342e35fdd936d133bf8a48de140a3c666897588a05shiqian 343e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeNegationTo(::std::ostream* os) const { 344e35fdd936d133bf8a48de140a3c666897588a05shiqian impl_.DescribeNegationTo(os); 345e35fdd936d133bf8a48de140a3c666897588a05shiqian } 346e35fdd936d133bf8a48de140a3c666897588a05shiqian 347821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual bool MatchAndExplain(T x, MatchResultListener* listener) const { 348db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan return impl_.MatchAndExplain(x, listener); 349e35fdd936d133bf8a48de140a3c666897588a05shiqian } 3502b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan 351e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 352e35fdd936d133bf8a48de140a3c666897588a05shiqian const Impl impl_; 35332de5f53763125925e078498250f7e73a88de9edzhanyong.wan 35432de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(MonomorphicImpl); 355e35fdd936d133bf8a48de140a3c666897588a05shiqian }; 356e35fdd936d133bf8a48de140a3c666897588a05shiqian 3572b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan Impl impl_; 35832de5f53763125925e078498250f7e73a88de9edzhanyong.wan 35932de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(PolymorphicMatcher); 360e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 361e35fdd936d133bf8a48de140a3c666897588a05shiqian 362e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher from its implementation. This is easier to use 363e35fdd936d133bf8a48de140a3c666897588a05shiqian// than the Matcher<T> constructor as it doesn't require you to 364e35fdd936d133bf8a48de140a3c666897588a05shiqian// explicitly write the template argument, e.g. 365e35fdd936d133bf8a48de140a3c666897588a05shiqian// 366e35fdd936d133bf8a48de140a3c666897588a05shiqian// MakeMatcher(foo); 367e35fdd936d133bf8a48de140a3c666897588a05shiqian// vs 368e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matcher<const string&>(foo); 369e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 370e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<T> MakeMatcher(const MatcherInterface<T>* impl) { 371e35fdd936d133bf8a48de140a3c666897588a05shiqian return Matcher<T>(impl); 372e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 373e35fdd936d133bf8a48de140a3c666897588a05shiqian 374e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher from its implementation. This is 375e35fdd936d133bf8a48de140a3c666897588a05shiqian// easier to use than the PolymorphicMatcher<Impl> constructor as it 376e35fdd936d133bf8a48de140a3c666897588a05shiqian// doesn't require you to explicitly write the template argument, e.g. 377e35fdd936d133bf8a48de140a3c666897588a05shiqian// 378e35fdd936d133bf8a48de140a3c666897588a05shiqian// MakePolymorphicMatcher(foo); 379e35fdd936d133bf8a48de140a3c666897588a05shiqian// vs 380e35fdd936d133bf8a48de140a3c666897588a05shiqian// PolymorphicMatcher<TypeOfFoo>(foo); 381e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <class Impl> 382e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<Impl> MakePolymorphicMatcher(const Impl& impl) { 383e35fdd936d133bf8a48de140a3c666897588a05shiqian return PolymorphicMatcher<Impl>(impl); 384e35fdd936d133bf8a48de140a3c666897588a05shiqian} 385e35fdd936d133bf8a48de140a3c666897588a05shiqian 386e35fdd936d133bf8a48de140a3c666897588a05shiqian// In order to be safe and clear, casting between different matcher 387e35fdd936d133bf8a48de140a3c666897588a05shiqian// types is done explicitly via MatcherCast<T>(m), which takes a 388e35fdd936d133bf8a48de140a3c666897588a05shiqian// matcher m and returns a Matcher<T>. It compiles only when T can be 389e35fdd936d133bf8a48de140a3c666897588a05shiqian// statically converted to the argument type of m. 390e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T, typename M> 391e35fdd936d133bf8a48de140a3c666897588a05shiqianMatcher<T> MatcherCast(M m); 392e35fdd936d133bf8a48de140a3c666897588a05shiqian 39318490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// Implements SafeMatcherCast(). 39418490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// 39595b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// We use an intermediate class to do the actual safe casting as Nokia's 39695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// Symbian compiler cannot decide between 39795b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// template <T, M> ... (M) and 39895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// template <T, U> ... (const Matcher<U>&) 39995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// for function templates but can for member function templates. 40095b12332c345cee508a8e2b68e007280392506e0zhanyong.wantemplate <typename T> 40195b12332c345cee508a8e2b68e007280392506e0zhanyong.wanclass SafeMatcherCastImpl { 40295b12332c345cee508a8e2b68e007280392506e0zhanyong.wan public: 40395b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // This overload handles polymorphic matchers only since monomorphic 40495b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // matchers are handled by the next one. 40595b12332c345cee508a8e2b68e007280392506e0zhanyong.wan template <typename M> 40695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan static inline Matcher<T> Cast(M polymorphic_matcher) { 40795b12332c345cee508a8e2b68e007280392506e0zhanyong.wan return Matcher<T>(polymorphic_matcher); 40895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan } 40918490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan 41095b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // This overload handles monomorphic matchers. 41195b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // 41295b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // In general, if type T can be implicitly converted to type U, we can 41395b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // safely convert a Matcher<U> to a Matcher<T> (i.e. Matcher is 41495b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // contravariant): just keep a copy of the original Matcher<U>, convert the 41595b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // argument from type T to U, and then pass it to the underlying Matcher<U>. 41695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // The only exception is when U is a reference and T is not, as the 41795b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // underlying Matcher<U> may be interested in the argument's address, which 41895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // is not preserved in the conversion from T to U. 41995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan template <typename U> 42095b12332c345cee508a8e2b68e007280392506e0zhanyong.wan static inline Matcher<T> Cast(const Matcher<U>& matcher) { 42195b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // Enforce that T can be implicitly converted to U. 42295b12332c345cee508a8e2b68e007280392506e0zhanyong.wan GMOCK_COMPILE_ASSERT_((internal::ImplicitlyConvertible<T, U>::value), 42395b12332c345cee508a8e2b68e007280392506e0zhanyong.wan T_must_be_implicitly_convertible_to_U); 42495b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // Enforce that we are not converting a non-reference type T to a reference 42595b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // type U. 42695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan GMOCK_COMPILE_ASSERT_( 42795b12332c345cee508a8e2b68e007280392506e0zhanyong.wan internal::is_reference<T>::value || !internal::is_reference<U>::value, 42895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan cannot_convert_non_referentce_arg_to_reference); 42995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // In case both T and U are arithmetic types, enforce that the 43095b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // conversion is not lossy. 43195b12332c345cee508a8e2b68e007280392506e0zhanyong.wan typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(T)) RawT; 43295b12332c345cee508a8e2b68e007280392506e0zhanyong.wan typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(U)) RawU; 43395b12332c345cee508a8e2b68e007280392506e0zhanyong.wan const bool kTIsOther = GMOCK_KIND_OF_(RawT) == internal::kOther; 43495b12332c345cee508a8e2b68e007280392506e0zhanyong.wan const bool kUIsOther = GMOCK_KIND_OF_(RawU) == internal::kOther; 43595b12332c345cee508a8e2b68e007280392506e0zhanyong.wan GMOCK_COMPILE_ASSERT_( 43695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan kTIsOther || kUIsOther || 43795b12332c345cee508a8e2b68e007280392506e0zhanyong.wan (internal::LosslessArithmeticConvertible<RawT, RawU>::value), 43895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan conversion_of_arithmetic_types_must_be_lossless); 43995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan return MatcherCast<T>(matcher); 44095b12332c345cee508a8e2b68e007280392506e0zhanyong.wan } 44195b12332c345cee508a8e2b68e007280392506e0zhanyong.wan}; 44295b12332c345cee508a8e2b68e007280392506e0zhanyong.wan 44395b12332c345cee508a8e2b68e007280392506e0zhanyong.wantemplate <typename T, typename M> 44495b12332c345cee508a8e2b68e007280392506e0zhanyong.waninline Matcher<T> SafeMatcherCast(const M& polymorphic_matcher) { 44595b12332c345cee508a8e2b68e007280392506e0zhanyong.wan return SafeMatcherCastImpl<T>::Cast(polymorphic_matcher); 44618490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan} 44718490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan 448e35fdd936d133bf8a48de140a3c666897588a05shiqian// A<T>() returns a matcher that matches any value of type T. 449e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 450e35fdd936d133bf8a48de140a3c666897588a05shiqianMatcher<T> A(); 451e35fdd936d133bf8a48de140a3c666897588a05shiqian 452e35fdd936d133bf8a48de140a3c666897588a05shiqian// Anything inside the 'internal' namespace IS INTERNAL IMPLEMENTATION 453e35fdd936d133bf8a48de140a3c666897588a05shiqian// and MUST NOT BE USED IN USER CODE!!! 454e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace internal { 455e35fdd936d133bf8a48de140a3c666897588a05shiqian 456821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// If the given string is not empty and os is not NULL, wraps the 457821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// string inside a pair of parentheses and streams the result to os. 458821133180ccdb9eb062548d964929ba3b354eb84zhanyong.waninline void StreamInParensAsNeeded(const internal::string& str, 459821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan ::std::ostream* os) { 460821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (!str.empty() && os != NULL) { 461821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *os << " (" << str << ")"; 462e35fdd936d133bf8a48de140a3c666897588a05shiqian } 463e35fdd936d133bf8a48de140a3c666897588a05shiqian} 464e35fdd936d133bf8a48de140a3c666897588a05shiqian 465e35fdd936d133bf8a48de140a3c666897588a05shiqian// An internal helper class for doing compile-time loop on a tuple's 466e35fdd936d133bf8a48de140a3c666897588a05shiqian// fields. 467e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <size_t N> 468e35fdd936d133bf8a48de140a3c666897588a05shiqianclass TuplePrefix { 469e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 470e35fdd936d133bf8a48de140a3c666897588a05shiqian // TuplePrefix<N>::Matches(matcher_tuple, value_tuple) returns true 471e35fdd936d133bf8a48de140a3c666897588a05shiqian // iff the first N fields of matcher_tuple matches the first N 472e35fdd936d133bf8a48de140a3c666897588a05shiqian // fields of value_tuple, respectively. 473e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename MatcherTuple, typename ValueTuple> 474e35fdd936d133bf8a48de140a3c666897588a05shiqian static bool Matches(const MatcherTuple& matcher_tuple, 475e35fdd936d133bf8a48de140a3c666897588a05shiqian const ValueTuple& value_tuple) { 476e35fdd936d133bf8a48de140a3c666897588a05shiqian using ::std::tr1::get; 477e35fdd936d133bf8a48de140a3c666897588a05shiqian return TuplePrefix<N - 1>::Matches(matcher_tuple, value_tuple) 478e35fdd936d133bf8a48de140a3c666897588a05shiqian && get<N - 1>(matcher_tuple).Matches(get<N - 1>(value_tuple)); 479e35fdd936d133bf8a48de140a3c666897588a05shiqian } 480e35fdd936d133bf8a48de140a3c666897588a05shiqian 481e35fdd936d133bf8a48de140a3c666897588a05shiqian // TuplePrefix<N>::DescribeMatchFailuresTo(matchers, values, os) 482e35fdd936d133bf8a48de140a3c666897588a05shiqian // describes failures in matching the first N fields of matchers 483e35fdd936d133bf8a48de140a3c666897588a05shiqian // against the first N fields of values. If there is no failure, 484e35fdd936d133bf8a48de140a3c666897588a05shiqian // nothing will be streamed to os. 485e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename MatcherTuple, typename ValueTuple> 486e35fdd936d133bf8a48de140a3c666897588a05shiqian static void DescribeMatchFailuresTo(const MatcherTuple& matchers, 487e35fdd936d133bf8a48de140a3c666897588a05shiqian const ValueTuple& values, 488e35fdd936d133bf8a48de140a3c666897588a05shiqian ::std::ostream* os) { 489e35fdd936d133bf8a48de140a3c666897588a05shiqian using ::std::tr1::tuple_element; 490e35fdd936d133bf8a48de140a3c666897588a05shiqian using ::std::tr1::get; 491e35fdd936d133bf8a48de140a3c666897588a05shiqian 492e35fdd936d133bf8a48de140a3c666897588a05shiqian // First, describes failures in the first N - 1 fields. 493e35fdd936d133bf8a48de140a3c666897588a05shiqian TuplePrefix<N - 1>::DescribeMatchFailuresTo(matchers, values, os); 494e35fdd936d133bf8a48de140a3c666897588a05shiqian 495e35fdd936d133bf8a48de140a3c666897588a05shiqian // Then describes the failure (if any) in the (N - 1)-th (0-based) 496e35fdd936d133bf8a48de140a3c666897588a05shiqian // field. 497e35fdd936d133bf8a48de140a3c666897588a05shiqian typename tuple_element<N - 1, MatcherTuple>::type matcher = 498e35fdd936d133bf8a48de140a3c666897588a05shiqian get<N - 1>(matchers); 499e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef typename tuple_element<N - 1, ValueTuple>::type Value; 500e35fdd936d133bf8a48de140a3c666897588a05shiqian Value value = get<N - 1>(values); 501821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan StringMatchResultListener listener; 502821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (!matcher.MatchAndExplain(value, &listener)) { 503e35fdd936d133bf8a48de140a3c666897588a05shiqian // TODO(wan): include in the message the name of the parameter 504e35fdd936d133bf8a48de140a3c666897588a05shiqian // as used in MOCK_METHOD*() when possible. 505e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << " Expected arg #" << N - 1 << ": "; 506e35fdd936d133bf8a48de140a3c666897588a05shiqian get<N - 1>(matchers).DescribeTo(os); 507e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "\n Actual: "; 508e35fdd936d133bf8a48de140a3c666897588a05shiqian // We remove the reference in type Value to prevent the 509e35fdd936d133bf8a48de140a3c666897588a05shiqian // universal printer from printing the address of value, which 510e35fdd936d133bf8a48de140a3c666897588a05shiqian // isn't interesting to the user most of the time. The 511db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan // matcher's MatchAndExplain() method handles the case when 512e35fdd936d133bf8a48de140a3c666897588a05shiqian // the address is interesting. 513e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan internal::UniversalPrinter<GMOCK_REMOVE_REFERENCE_(Value)>:: 514e35fdd936d133bf8a48de140a3c666897588a05shiqian Print(value, os); 515821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 516821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan StreamInParensAsNeeded(listener.str(), os); 517e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "\n"; 518e35fdd936d133bf8a48de140a3c666897588a05shiqian } 519e35fdd936d133bf8a48de140a3c666897588a05shiqian } 520e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 521e35fdd936d133bf8a48de140a3c666897588a05shiqian 522e35fdd936d133bf8a48de140a3c666897588a05shiqian// The base case. 523e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <> 524e35fdd936d133bf8a48de140a3c666897588a05shiqianclass TuplePrefix<0> { 525e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 526e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename MatcherTuple, typename ValueTuple> 5273fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.wan static bool Matches(const MatcherTuple& /* matcher_tuple */, 5283fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.wan const ValueTuple& /* value_tuple */) { 529e35fdd936d133bf8a48de140a3c666897588a05shiqian return true; 530e35fdd936d133bf8a48de140a3c666897588a05shiqian } 531e35fdd936d133bf8a48de140a3c666897588a05shiqian 532e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename MatcherTuple, typename ValueTuple> 5333fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.wan static void DescribeMatchFailuresTo(const MatcherTuple& /* matchers */, 5343fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.wan const ValueTuple& /* values */, 5353fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.wan ::std::ostream* /* os */) {} 536e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 537e35fdd936d133bf8a48de140a3c666897588a05shiqian 538e35fdd936d133bf8a48de140a3c666897588a05shiqian// TupleMatches(matcher_tuple, value_tuple) returns true iff all 539e35fdd936d133bf8a48de140a3c666897588a05shiqian// matchers in matcher_tuple match the corresponding fields in 540e35fdd936d133bf8a48de140a3c666897588a05shiqian// value_tuple. It is a compiler error if matcher_tuple and 541e35fdd936d133bf8a48de140a3c666897588a05shiqian// value_tuple have different number of fields or incompatible field 542e35fdd936d133bf8a48de140a3c666897588a05shiqian// types. 543e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename MatcherTuple, typename ValueTuple> 544e35fdd936d133bf8a48de140a3c666897588a05shiqianbool TupleMatches(const MatcherTuple& matcher_tuple, 545e35fdd936d133bf8a48de140a3c666897588a05shiqian const ValueTuple& value_tuple) { 546e35fdd936d133bf8a48de140a3c666897588a05shiqian using ::std::tr1::tuple_size; 547e35fdd936d133bf8a48de140a3c666897588a05shiqian // Makes sure that matcher_tuple and value_tuple have the same 548e35fdd936d133bf8a48de140a3c666897588a05shiqian // number of fields. 549e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan GMOCK_COMPILE_ASSERT_(tuple_size<MatcherTuple>::value == 550e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan tuple_size<ValueTuple>::value, 551e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan matcher_and_value_have_different_numbers_of_fields); 552e35fdd936d133bf8a48de140a3c666897588a05shiqian return TuplePrefix<tuple_size<ValueTuple>::value>:: 553e35fdd936d133bf8a48de140a3c666897588a05shiqian Matches(matcher_tuple, value_tuple); 554e35fdd936d133bf8a48de140a3c666897588a05shiqian} 555e35fdd936d133bf8a48de140a3c666897588a05shiqian 556e35fdd936d133bf8a48de140a3c666897588a05shiqian// Describes failures in matching matchers against values. If there 557e35fdd936d133bf8a48de140a3c666897588a05shiqian// is no failure, nothing will be streamed to os. 558e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename MatcherTuple, typename ValueTuple> 559e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid DescribeMatchFailureTupleTo(const MatcherTuple& matchers, 560e35fdd936d133bf8a48de140a3c666897588a05shiqian const ValueTuple& values, 561e35fdd936d133bf8a48de140a3c666897588a05shiqian ::std::ostream* os) { 562e35fdd936d133bf8a48de140a3c666897588a05shiqian using ::std::tr1::tuple_size; 563e35fdd936d133bf8a48de140a3c666897588a05shiqian TuplePrefix<tuple_size<MatcherTuple>::value>::DescribeMatchFailuresTo( 564e35fdd936d133bf8a48de140a3c666897588a05shiqian matchers, values, os); 565e35fdd936d133bf8a48de140a3c666897588a05shiqian} 566e35fdd936d133bf8a48de140a3c666897588a05shiqian 567e35fdd936d133bf8a48de140a3c666897588a05shiqian// The MatcherCastImpl class template is a helper for implementing 568e35fdd936d133bf8a48de140a3c666897588a05shiqian// MatcherCast(). We need this helper in order to partially 569e35fdd936d133bf8a48de140a3c666897588a05shiqian// specialize the implementation of MatcherCast() (C++ allows 570e35fdd936d133bf8a48de140a3c666897588a05shiqian// class/struct templates to be partially specialized, but not 571e35fdd936d133bf8a48de140a3c666897588a05shiqian// function templates.). 572e35fdd936d133bf8a48de140a3c666897588a05shiqian 573e35fdd936d133bf8a48de140a3c666897588a05shiqian// This general version is used when MatcherCast()'s argument is a 574e35fdd936d133bf8a48de140a3c666897588a05shiqian// polymorphic matcher (i.e. something that can be converted to a 575e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matcher but is not one yet; for example, Eq(value)). 576e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T, typename M> 577e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatcherCastImpl { 578e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 579e35fdd936d133bf8a48de140a3c666897588a05shiqian static Matcher<T> Cast(M polymorphic_matcher) { 580e35fdd936d133bf8a48de140a3c666897588a05shiqian return Matcher<T>(polymorphic_matcher); 581e35fdd936d133bf8a48de140a3c666897588a05shiqian } 582e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 583e35fdd936d133bf8a48de140a3c666897588a05shiqian 584e35fdd936d133bf8a48de140a3c666897588a05shiqian// This more specialized version is used when MatcherCast()'s argument 585e35fdd936d133bf8a48de140a3c666897588a05shiqian// is already a Matcher. This only compiles when type T can be 586e35fdd936d133bf8a48de140a3c666897588a05shiqian// statically converted to type U. 587e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T, typename U> 588e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatcherCastImpl<T, Matcher<U> > { 589e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 590e35fdd936d133bf8a48de140a3c666897588a05shiqian static Matcher<T> Cast(const Matcher<U>& source_matcher) { 591e35fdd936d133bf8a48de140a3c666897588a05shiqian return Matcher<T>(new Impl(source_matcher)); 592e35fdd936d133bf8a48de140a3c666897588a05shiqian } 59332de5f53763125925e078498250f7e73a88de9edzhanyong.wan 594e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 595e35fdd936d133bf8a48de140a3c666897588a05shiqian class Impl : public MatcherInterface<T> { 596e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 597e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit Impl(const Matcher<U>& source_matcher) 598e35fdd936d133bf8a48de140a3c666897588a05shiqian : source_matcher_(source_matcher) {} 599e35fdd936d133bf8a48de140a3c666897588a05shiqian 600e35fdd936d133bf8a48de140a3c666897588a05shiqian // We delegate the matching logic to the source matcher. 601821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual bool MatchAndExplain(T x, MatchResultListener* listener) const { 602821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return source_matcher_.MatchAndExplain(static_cast<U>(x), listener); 603e35fdd936d133bf8a48de140a3c666897588a05shiqian } 604e35fdd936d133bf8a48de140a3c666897588a05shiqian 605e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeTo(::std::ostream* os) const { 606e35fdd936d133bf8a48de140a3c666897588a05shiqian source_matcher_.DescribeTo(os); 607e35fdd936d133bf8a48de140a3c666897588a05shiqian } 608e35fdd936d133bf8a48de140a3c666897588a05shiqian 609e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeNegationTo(::std::ostream* os) const { 610e35fdd936d133bf8a48de140a3c666897588a05shiqian source_matcher_.DescribeNegationTo(os); 611e35fdd936d133bf8a48de140a3c666897588a05shiqian } 612e35fdd936d133bf8a48de140a3c666897588a05shiqian 613e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 614e35fdd936d133bf8a48de140a3c666897588a05shiqian const Matcher<U> source_matcher_; 61532de5f53763125925e078498250f7e73a88de9edzhanyong.wan 61632de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(Impl); 617e35fdd936d133bf8a48de140a3c666897588a05shiqian }; 618e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 619e35fdd936d133bf8a48de140a3c666897588a05shiqian 620e35fdd936d133bf8a48de140a3c666897588a05shiqian// This even more specialized version is used for efficiently casting 621e35fdd936d133bf8a48de140a3c666897588a05shiqian// a matcher to its own type. 622e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 623e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatcherCastImpl<T, Matcher<T> > { 624e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 625e35fdd936d133bf8a48de140a3c666897588a05shiqian static Matcher<T> Cast(const Matcher<T>& matcher) { return matcher; } 626e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 627e35fdd936d133bf8a48de140a3c666897588a05shiqian 628e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements A<T>(). 629e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 630e35fdd936d133bf8a48de140a3c666897588a05shiqianclass AnyMatcherImpl : public MatcherInterface<T> { 631e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 632821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual bool MatchAndExplain( 633821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan T /* x */, MatchResultListener* /* listener */) const { return true; } 634e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeTo(::std::ostream* os) const { *os << "is anything"; } 635e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeNegationTo(::std::ostream* os) const { 636e35fdd936d133bf8a48de140a3c666897588a05shiqian // This is mostly for completeness' safe, as it's not very useful 637e35fdd936d133bf8a48de140a3c666897588a05shiqian // to write Not(A<bool>()). However we cannot completely rule out 638e35fdd936d133bf8a48de140a3c666897588a05shiqian // such a possibility, and it doesn't hurt to be prepared. 639e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "never matches"; 640e35fdd936d133bf8a48de140a3c666897588a05shiqian } 641e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 642e35fdd936d133bf8a48de140a3c666897588a05shiqian 643e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements _, a matcher that matches any value of any 644e35fdd936d133bf8a48de140a3c666897588a05shiqian// type. This is a polymorphic matcher, so we need a template type 645e35fdd936d133bf8a48de140a3c666897588a05shiqian// conversion operator to make it appearing as a Matcher<T> for any 646e35fdd936d133bf8a48de140a3c666897588a05shiqian// type T. 647e35fdd936d133bf8a48de140a3c666897588a05shiqianclass AnythingMatcher { 648e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 649e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 650e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<T>() const { return A<T>(); } 651e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 652e35fdd936d133bf8a48de140a3c666897588a05shiqian 653e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements a matcher that compares a given value with a 654e35fdd936d133bf8a48de140a3c666897588a05shiqian// pre-supplied value using one of the ==, <=, <, etc, operators. The 655e35fdd936d133bf8a48de140a3c666897588a05shiqian// two values being compared don't have to have the same type. 656e35fdd936d133bf8a48de140a3c666897588a05shiqian// 657e35fdd936d133bf8a48de140a3c666897588a05shiqian// The matcher defined here is polymorphic (for example, Eq(5) can be 658e35fdd936d133bf8a48de140a3c666897588a05shiqian// used to match an int, a short, a double, etc). Therefore we use 659e35fdd936d133bf8a48de140a3c666897588a05shiqian// a template type conversion operator in the implementation. 660e35fdd936d133bf8a48de140a3c666897588a05shiqian// 661e35fdd936d133bf8a48de140a3c666897588a05shiqian// We define this as a macro in order to eliminate duplicated source 662e35fdd936d133bf8a48de140a3c666897588a05shiqian// code. 663e35fdd936d133bf8a48de140a3c666897588a05shiqian// 664e35fdd936d133bf8a48de140a3c666897588a05shiqian// The following template definition assumes that the Rhs parameter is 665e35fdd936d133bf8a48de140a3c666897588a05shiqian// a "bare" type (i.e. neither 'const T' nor 'T&'). 666e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan#define GMOCK_IMPLEMENT_COMPARISON_MATCHER_(name, op, relation) \ 667e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename Rhs> class name##Matcher { \ 668e35fdd936d133bf8a48de140a3c666897588a05shiqian public: \ 669e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit name##Matcher(const Rhs& rhs) : rhs_(rhs) {} \ 670e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename Lhs> \ 671e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<Lhs>() const { \ 672e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakeMatcher(new Impl<Lhs>(rhs_)); \ 673e35fdd936d133bf8a48de140a3c666897588a05shiqian } \ 674e35fdd936d133bf8a48de140a3c666897588a05shiqian private: \ 675e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename Lhs> \ 676e35fdd936d133bf8a48de140a3c666897588a05shiqian class Impl : public MatcherInterface<Lhs> { \ 677e35fdd936d133bf8a48de140a3c666897588a05shiqian public: \ 678e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit Impl(const Rhs& rhs) : rhs_(rhs) {} \ 679821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual bool MatchAndExplain(\ 680821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan Lhs lhs, MatchResultListener* /* listener */) const { \ 681821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return lhs op rhs_; \ 682821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan } \ 683e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeTo(::std::ostream* os) const { \ 684e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "is " relation " "; \ 685e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<Rhs>::Print(rhs_, os); \ 686e35fdd936d133bf8a48de140a3c666897588a05shiqian } \ 687e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeNegationTo(::std::ostream* os) const { \ 688e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "is not " relation " "; \ 689e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<Rhs>::Print(rhs_, os); \ 690e35fdd936d133bf8a48de140a3c666897588a05shiqian } \ 691e35fdd936d133bf8a48de140a3c666897588a05shiqian private: \ 692e35fdd936d133bf8a48de140a3c666897588a05shiqian Rhs rhs_; \ 69332de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(Impl); \ 694e35fdd936d133bf8a48de140a3c666897588a05shiqian }; \ 695e35fdd936d133bf8a48de140a3c666897588a05shiqian Rhs rhs_; \ 69632de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(name##Matcher); \ 697e35fdd936d133bf8a48de140a3c666897588a05shiqian } 698e35fdd936d133bf8a48de140a3c666897588a05shiqian 699e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements Eq(v), Ge(v), Gt(v), Le(v), Lt(v), and Ne(v) 700e35fdd936d133bf8a48de140a3c666897588a05shiqian// respectively. 701e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Eq, ==, "equal to"); 702e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Ge, >=, "greater than or equal to"); 703e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Gt, >, "greater than"); 704e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Le, <=, "less than or equal to"); 705e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Lt, <, "less than"); 706e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Ne, !=, "not equal to"); 707e35fdd936d133bf8a48de140a3c666897588a05shiqian 708e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan#undef GMOCK_IMPLEMENT_COMPARISON_MATCHER_ 709e35fdd936d133bf8a48de140a3c666897588a05shiqian 71079b83505bcf73bf2903ebf2e2f82cb1e1f181816vladlosev// Implements the polymorphic IsNull() matcher, which matches any raw or smart 7112d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan// pointer that is NULL. 7122d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wanclass IsNullMatcher { 7132d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan public: 71479b83505bcf73bf2903ebf2e2f82cb1e1f181816vladlosev template <typename Pointer> 715db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan bool MatchAndExplain(const Pointer& p, 716db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan MatchResultListener* /* listener */) const { 717db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan return GetRawPointer(p) == NULL; 718db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan } 7192d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan 7202d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan void DescribeTo(::std::ostream* os) const { *os << "is NULL"; } 7212d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan void DescribeNegationTo(::std::ostream* os) const { 7222d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan *os << "is not NULL"; 7232d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan } 7242d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan}; 7252d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan 72679b83505bcf73bf2903ebf2e2f82cb1e1f181816vladlosev// Implements the polymorphic NotNull() matcher, which matches any raw or smart 727e35fdd936d133bf8a48de140a3c666897588a05shiqian// pointer that is not NULL. 728e35fdd936d133bf8a48de140a3c666897588a05shiqianclass NotNullMatcher { 729e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 73079b83505bcf73bf2903ebf2e2f82cb1e1f181816vladlosev template <typename Pointer> 731db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan bool MatchAndExplain(const Pointer& p, 732db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan MatchResultListener* /* listener */) const { 733db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan return GetRawPointer(p) != NULL; 734db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan } 735e35fdd936d133bf8a48de140a3c666897588a05shiqian 736e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeTo(::std::ostream* os) const { *os << "is not NULL"; } 737e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeNegationTo(::std::ostream* os) const { 738e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "is NULL"; 739e35fdd936d133bf8a48de140a3c666897588a05shiqian } 740e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 741e35fdd936d133bf8a48de140a3c666897588a05shiqian 742e35fdd936d133bf8a48de140a3c666897588a05shiqian// Ref(variable) matches any argument that is a reference to 743e35fdd936d133bf8a48de140a3c666897588a05shiqian// 'variable'. This matcher is polymorphic as it can match any 744e35fdd936d133bf8a48de140a3c666897588a05shiqian// super type of the type of 'variable'. 745e35fdd936d133bf8a48de140a3c666897588a05shiqian// 746e35fdd936d133bf8a48de140a3c666897588a05shiqian// The RefMatcher template class implements Ref(variable). It can 747e35fdd936d133bf8a48de140a3c666897588a05shiqian// only be instantiated with a reference type. This prevents a user 748e35fdd936d133bf8a48de140a3c666897588a05shiqian// from mistakenly using Ref(x) to match a non-reference function 749e35fdd936d133bf8a48de140a3c666897588a05shiqian// argument. For example, the following will righteously cause a 750e35fdd936d133bf8a48de140a3c666897588a05shiqian// compiler error: 751e35fdd936d133bf8a48de140a3c666897588a05shiqian// 752e35fdd936d133bf8a48de140a3c666897588a05shiqian// int n; 753e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matcher<int> m1 = Ref(n); // This won't compile. 754e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matcher<int&> m2 = Ref(n); // This will compile. 755e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 756e35fdd936d133bf8a48de140a3c666897588a05shiqianclass RefMatcher; 757e35fdd936d133bf8a48de140a3c666897588a05shiqian 758e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 759e35fdd936d133bf8a48de140a3c666897588a05shiqianclass RefMatcher<T&> { 760e35fdd936d133bf8a48de140a3c666897588a05shiqian // Google Mock is a generic framework and thus needs to support 761e35fdd936d133bf8a48de140a3c666897588a05shiqian // mocking any function types, including those that take non-const 762e35fdd936d133bf8a48de140a3c666897588a05shiqian // reference arguments. Therefore the template parameter T (and 763e35fdd936d133bf8a48de140a3c666897588a05shiqian // Super below) can be instantiated to either a const type or a 764e35fdd936d133bf8a48de140a3c666897588a05shiqian // non-const type. 765e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 766e35fdd936d133bf8a48de140a3c666897588a05shiqian // RefMatcher() takes a T& instead of const T&, as we want the 767e35fdd936d133bf8a48de140a3c666897588a05shiqian // compiler to catch using Ref(const_value) as a matcher for a 768e35fdd936d133bf8a48de140a3c666897588a05shiqian // non-const reference. 769e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit RefMatcher(T& x) : object_(x) {} // NOLINT 770e35fdd936d133bf8a48de140a3c666897588a05shiqian 771e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename Super> 772e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<Super&>() const { 773e35fdd936d133bf8a48de140a3c666897588a05shiqian // By passing object_ (type T&) to Impl(), which expects a Super&, 774e35fdd936d133bf8a48de140a3c666897588a05shiqian // we make sure that Super is a super type of T. In particular, 775e35fdd936d133bf8a48de140a3c666897588a05shiqian // this catches using Ref(const_value) as a matcher for a 776e35fdd936d133bf8a48de140a3c666897588a05shiqian // non-const reference, as you cannot implicitly convert a const 777e35fdd936d133bf8a48de140a3c666897588a05shiqian // reference to a non-const reference. 778e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakeMatcher(new Impl<Super>(object_)); 779e35fdd936d133bf8a48de140a3c666897588a05shiqian } 78032de5f53763125925e078498250f7e73a88de9edzhanyong.wan 781e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 782e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename Super> 783e35fdd936d133bf8a48de140a3c666897588a05shiqian class Impl : public MatcherInterface<Super&> { 784e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 785e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit Impl(Super& x) : object_(x) {} // NOLINT 786e35fdd936d133bf8a48de140a3c666897588a05shiqian 787db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan // MatchAndExplain() takes a Super& (as opposed to const Super&) 788db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan // in order to match the interface MatcherInterface<Super&>. 789821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual bool MatchAndExplain( 790821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan Super& x, MatchResultListener* listener) const { 791821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << "is located @" << static_cast<const void*>(&x); 792821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return &x == &object_; 793821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan } 794e35fdd936d133bf8a48de140a3c666897588a05shiqian 795e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeTo(::std::ostream* os) const { 796e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "references the variable "; 797e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<Super&>::Print(object_, os); 798e35fdd936d133bf8a48de140a3c666897588a05shiqian } 799e35fdd936d133bf8a48de140a3c666897588a05shiqian 800e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeNegationTo(::std::ostream* os) const { 801e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "does not reference the variable "; 802e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<Super&>::Print(object_, os); 803e35fdd936d133bf8a48de140a3c666897588a05shiqian } 804e35fdd936d133bf8a48de140a3c666897588a05shiqian 805e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 806e35fdd936d133bf8a48de140a3c666897588a05shiqian const Super& object_; 80732de5f53763125925e078498250f7e73a88de9edzhanyong.wan 80832de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(Impl); 809e35fdd936d133bf8a48de140a3c666897588a05shiqian }; 810e35fdd936d133bf8a48de140a3c666897588a05shiqian 811e35fdd936d133bf8a48de140a3c666897588a05shiqian T& object_; 81232de5f53763125925e078498250f7e73a88de9edzhanyong.wan 81332de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(RefMatcher); 814e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 815e35fdd936d133bf8a48de140a3c666897588a05shiqian 816e35fdd936d133bf8a48de140a3c666897588a05shiqian// Polymorphic helper functions for narrow and wide string matchers. 817e35fdd936d133bf8a48de140a3c666897588a05shiqianinline bool CaseInsensitiveCStringEquals(const char* lhs, const char* rhs) { 818e35fdd936d133bf8a48de140a3c666897588a05shiqian return String::CaseInsensitiveCStringEquals(lhs, rhs); 819e35fdd936d133bf8a48de140a3c666897588a05shiqian} 820e35fdd936d133bf8a48de140a3c666897588a05shiqian 821e35fdd936d133bf8a48de140a3c666897588a05shiqianinline bool CaseInsensitiveCStringEquals(const wchar_t* lhs, 822e35fdd936d133bf8a48de140a3c666897588a05shiqian const wchar_t* rhs) { 823e35fdd936d133bf8a48de140a3c666897588a05shiqian return String::CaseInsensitiveWideCStringEquals(lhs, rhs); 824e35fdd936d133bf8a48de140a3c666897588a05shiqian} 825e35fdd936d133bf8a48de140a3c666897588a05shiqian 826e35fdd936d133bf8a48de140a3c666897588a05shiqian// String comparison for narrow or wide strings that can have embedded NUL 827e35fdd936d133bf8a48de140a3c666897588a05shiqian// characters. 828e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType> 829e35fdd936d133bf8a48de140a3c666897588a05shiqianbool CaseInsensitiveStringEquals(const StringType& s1, 830e35fdd936d133bf8a48de140a3c666897588a05shiqian const StringType& s2) { 831e35fdd936d133bf8a48de140a3c666897588a05shiqian // Are the heads equal? 832e35fdd936d133bf8a48de140a3c666897588a05shiqian if (!CaseInsensitiveCStringEquals(s1.c_str(), s2.c_str())) { 833e35fdd936d133bf8a48de140a3c666897588a05shiqian return false; 834e35fdd936d133bf8a48de140a3c666897588a05shiqian } 835e35fdd936d133bf8a48de140a3c666897588a05shiqian 836e35fdd936d133bf8a48de140a3c666897588a05shiqian // Skip the equal heads. 837e35fdd936d133bf8a48de140a3c666897588a05shiqian const typename StringType::value_type nul = 0; 838e35fdd936d133bf8a48de140a3c666897588a05shiqian const size_t i1 = s1.find(nul), i2 = s2.find(nul); 839e35fdd936d133bf8a48de140a3c666897588a05shiqian 840e35fdd936d133bf8a48de140a3c666897588a05shiqian // Are we at the end of either s1 or s2? 841e35fdd936d133bf8a48de140a3c666897588a05shiqian if (i1 == StringType::npos || i2 == StringType::npos) { 842e35fdd936d133bf8a48de140a3c666897588a05shiqian return i1 == i2; 843e35fdd936d133bf8a48de140a3c666897588a05shiqian } 844e35fdd936d133bf8a48de140a3c666897588a05shiqian 845e35fdd936d133bf8a48de140a3c666897588a05shiqian // Are the tails equal? 846e35fdd936d133bf8a48de140a3c666897588a05shiqian return CaseInsensitiveStringEquals(s1.substr(i1 + 1), s2.substr(i2 + 1)); 847e35fdd936d133bf8a48de140a3c666897588a05shiqian} 848e35fdd936d133bf8a48de140a3c666897588a05shiqian 849e35fdd936d133bf8a48de140a3c666897588a05shiqian// String matchers. 850e35fdd936d133bf8a48de140a3c666897588a05shiqian 851e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements equality-based string matchers like StrEq, StrCaseNe, and etc. 852e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType> 853e35fdd936d133bf8a48de140a3c666897588a05shiqianclass StrEqualityMatcher { 854e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 855e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef typename StringType::const_pointer ConstCharPointer; 856e35fdd936d133bf8a48de140a3c666897588a05shiqian 857e35fdd936d133bf8a48de140a3c666897588a05shiqian StrEqualityMatcher(const StringType& str, bool expect_eq, 858e35fdd936d133bf8a48de140a3c666897588a05shiqian bool case_sensitive) 859e35fdd936d133bf8a48de140a3c666897588a05shiqian : string_(str), expect_eq_(expect_eq), case_sensitive_(case_sensitive) {} 860e35fdd936d133bf8a48de140a3c666897588a05shiqian 861e35fdd936d133bf8a48de140a3c666897588a05shiqian // When expect_eq_ is true, returns true iff s is equal to string_; 862e35fdd936d133bf8a48de140a3c666897588a05shiqian // otherwise returns true iff s is not equal to string_. 863db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan bool MatchAndExplain(ConstCharPointer s, 864db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan MatchResultListener* listener) const { 865e35fdd936d133bf8a48de140a3c666897588a05shiqian if (s == NULL) { 866e35fdd936d133bf8a48de140a3c666897588a05shiqian return !expect_eq_; 867e35fdd936d133bf8a48de140a3c666897588a05shiqian } 868db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan return MatchAndExplain(StringType(s), listener); 869e35fdd936d133bf8a48de140a3c666897588a05shiqian } 870e35fdd936d133bf8a48de140a3c666897588a05shiqian 871db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan bool MatchAndExplain(const StringType& s, 872db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan MatchResultListener* /* listener */) const { 873e35fdd936d133bf8a48de140a3c666897588a05shiqian const bool eq = case_sensitive_ ? s == string_ : 874e35fdd936d133bf8a48de140a3c666897588a05shiqian CaseInsensitiveStringEquals(s, string_); 875e35fdd936d133bf8a48de140a3c666897588a05shiqian return expect_eq_ == eq; 876e35fdd936d133bf8a48de140a3c666897588a05shiqian } 877e35fdd936d133bf8a48de140a3c666897588a05shiqian 878e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeTo(::std::ostream* os) const { 879e35fdd936d133bf8a48de140a3c666897588a05shiqian DescribeToHelper(expect_eq_, os); 880e35fdd936d133bf8a48de140a3c666897588a05shiqian } 881e35fdd936d133bf8a48de140a3c666897588a05shiqian 882e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeNegationTo(::std::ostream* os) const { 883e35fdd936d133bf8a48de140a3c666897588a05shiqian DescribeToHelper(!expect_eq_, os); 884e35fdd936d133bf8a48de140a3c666897588a05shiqian } 88532de5f53763125925e078498250f7e73a88de9edzhanyong.wan 886e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 887e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeToHelper(bool expect_eq, ::std::ostream* os) const { 888e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "is "; 889e35fdd936d133bf8a48de140a3c666897588a05shiqian if (!expect_eq) { 890e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "not "; 891e35fdd936d133bf8a48de140a3c666897588a05shiqian } 892e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "equal to "; 893e35fdd936d133bf8a48de140a3c666897588a05shiqian if (!case_sensitive_) { 894e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "(ignoring case) "; 895e35fdd936d133bf8a48de140a3c666897588a05shiqian } 896e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<StringType>::Print(string_, os); 897e35fdd936d133bf8a48de140a3c666897588a05shiqian } 898e35fdd936d133bf8a48de140a3c666897588a05shiqian 899e35fdd936d133bf8a48de140a3c666897588a05shiqian const StringType string_; 900e35fdd936d133bf8a48de140a3c666897588a05shiqian const bool expect_eq_; 901e35fdd936d133bf8a48de140a3c666897588a05shiqian const bool case_sensitive_; 90232de5f53763125925e078498250f7e73a88de9edzhanyong.wan 90332de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(StrEqualityMatcher); 904e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 905e35fdd936d133bf8a48de140a3c666897588a05shiqian 906e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the polymorphic HasSubstr(substring) matcher, which 907e35fdd936d133bf8a48de140a3c666897588a05shiqian// can be used as a Matcher<T> as long as T can be converted to a 908e35fdd936d133bf8a48de140a3c666897588a05shiqian// string. 909e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType> 910e35fdd936d133bf8a48de140a3c666897588a05shiqianclass HasSubstrMatcher { 911e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 912e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef typename StringType::const_pointer ConstCharPointer; 913e35fdd936d133bf8a48de140a3c666897588a05shiqian 914e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit HasSubstrMatcher(const StringType& substring) 915e35fdd936d133bf8a48de140a3c666897588a05shiqian : substring_(substring) {} 916e35fdd936d133bf8a48de140a3c666897588a05shiqian 917e35fdd936d133bf8a48de140a3c666897588a05shiqian // These overloaded methods allow HasSubstr(substring) to be used as a 918e35fdd936d133bf8a48de140a3c666897588a05shiqian // Matcher<T> as long as T can be converted to string. Returns true 919e35fdd936d133bf8a48de140a3c666897588a05shiqian // iff s contains substring_ as a substring. 920db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan bool MatchAndExplain(ConstCharPointer s, 921db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan MatchResultListener* listener) const { 922db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan return s != NULL && MatchAndExplain(StringType(s), listener); 923e35fdd936d133bf8a48de140a3c666897588a05shiqian } 924e35fdd936d133bf8a48de140a3c666897588a05shiqian 925db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan bool MatchAndExplain(const StringType& s, 926db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan MatchResultListener* /* listener */) const { 927e35fdd936d133bf8a48de140a3c666897588a05shiqian return s.find(substring_) != StringType::npos; 928e35fdd936d133bf8a48de140a3c666897588a05shiqian } 929e35fdd936d133bf8a48de140a3c666897588a05shiqian 930e35fdd936d133bf8a48de140a3c666897588a05shiqian // Describes what this matcher matches. 931e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeTo(::std::ostream* os) const { 932e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "has substring "; 933e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<StringType>::Print(substring_, os); 934e35fdd936d133bf8a48de140a3c666897588a05shiqian } 935e35fdd936d133bf8a48de140a3c666897588a05shiqian 936e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeNegationTo(::std::ostream* os) const { 937e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "has no substring "; 938e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<StringType>::Print(substring_, os); 939e35fdd936d133bf8a48de140a3c666897588a05shiqian } 94032de5f53763125925e078498250f7e73a88de9edzhanyong.wan 941e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 942e35fdd936d133bf8a48de140a3c666897588a05shiqian const StringType substring_; 94332de5f53763125925e078498250f7e73a88de9edzhanyong.wan 94432de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(HasSubstrMatcher); 945e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 946e35fdd936d133bf8a48de140a3c666897588a05shiqian 947e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the polymorphic StartsWith(substring) matcher, which 948e35fdd936d133bf8a48de140a3c666897588a05shiqian// can be used as a Matcher<T> as long as T can be converted to a 949e35fdd936d133bf8a48de140a3c666897588a05shiqian// string. 950e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType> 951e35fdd936d133bf8a48de140a3c666897588a05shiqianclass StartsWithMatcher { 952e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 953e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef typename StringType::const_pointer ConstCharPointer; 954e35fdd936d133bf8a48de140a3c666897588a05shiqian 955e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit StartsWithMatcher(const StringType& prefix) : prefix_(prefix) { 956e35fdd936d133bf8a48de140a3c666897588a05shiqian } 957e35fdd936d133bf8a48de140a3c666897588a05shiqian 958e35fdd936d133bf8a48de140a3c666897588a05shiqian // These overloaded methods allow StartsWith(prefix) to be used as a 959e35fdd936d133bf8a48de140a3c666897588a05shiqian // Matcher<T> as long as T can be converted to string. Returns true 960e35fdd936d133bf8a48de140a3c666897588a05shiqian // iff s starts with prefix_. 961db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan bool MatchAndExplain(ConstCharPointer s, 962db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan MatchResultListener* listener) const { 963db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan return s != NULL && MatchAndExplain(StringType(s), listener); 964e35fdd936d133bf8a48de140a3c666897588a05shiqian } 965e35fdd936d133bf8a48de140a3c666897588a05shiqian 966db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan bool MatchAndExplain(const StringType& s, 967db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan MatchResultListener* /* listener */) const { 968e35fdd936d133bf8a48de140a3c666897588a05shiqian return s.length() >= prefix_.length() && 969e35fdd936d133bf8a48de140a3c666897588a05shiqian s.substr(0, prefix_.length()) == prefix_; 970e35fdd936d133bf8a48de140a3c666897588a05shiqian } 971e35fdd936d133bf8a48de140a3c666897588a05shiqian 972e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeTo(::std::ostream* os) const { 973e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "starts with "; 974e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<StringType>::Print(prefix_, os); 975e35fdd936d133bf8a48de140a3c666897588a05shiqian } 976e35fdd936d133bf8a48de140a3c666897588a05shiqian 977e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeNegationTo(::std::ostream* os) const { 978e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "doesn't start with "; 979e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<StringType>::Print(prefix_, os); 980e35fdd936d133bf8a48de140a3c666897588a05shiqian } 98132de5f53763125925e078498250f7e73a88de9edzhanyong.wan 982e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 983e35fdd936d133bf8a48de140a3c666897588a05shiqian const StringType prefix_; 98432de5f53763125925e078498250f7e73a88de9edzhanyong.wan 98532de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(StartsWithMatcher); 986e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 987e35fdd936d133bf8a48de140a3c666897588a05shiqian 988e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the polymorphic EndsWith(substring) matcher, which 989e35fdd936d133bf8a48de140a3c666897588a05shiqian// can be used as a Matcher<T> as long as T can be converted to a 990e35fdd936d133bf8a48de140a3c666897588a05shiqian// string. 991e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType> 992e35fdd936d133bf8a48de140a3c666897588a05shiqianclass EndsWithMatcher { 993e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 994e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef typename StringType::const_pointer ConstCharPointer; 995e35fdd936d133bf8a48de140a3c666897588a05shiqian 996e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit EndsWithMatcher(const StringType& suffix) : suffix_(suffix) {} 997e35fdd936d133bf8a48de140a3c666897588a05shiqian 998e35fdd936d133bf8a48de140a3c666897588a05shiqian // These overloaded methods allow EndsWith(suffix) to be used as a 999e35fdd936d133bf8a48de140a3c666897588a05shiqian // Matcher<T> as long as T can be converted to string. Returns true 1000e35fdd936d133bf8a48de140a3c666897588a05shiqian // iff s ends with suffix_. 1001db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan bool MatchAndExplain(ConstCharPointer s, 1002db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan MatchResultListener* listener) const { 1003db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan return s != NULL && MatchAndExplain(StringType(s), listener); 1004e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1005e35fdd936d133bf8a48de140a3c666897588a05shiqian 1006db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan bool MatchAndExplain(const StringType& s, 1007db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan MatchResultListener* /* listener */) const { 1008e35fdd936d133bf8a48de140a3c666897588a05shiqian return s.length() >= suffix_.length() && 1009e35fdd936d133bf8a48de140a3c666897588a05shiqian s.substr(s.length() - suffix_.length()) == suffix_; 1010e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1011e35fdd936d133bf8a48de140a3c666897588a05shiqian 1012e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeTo(::std::ostream* os) const { 1013e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "ends with "; 1014e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<StringType>::Print(suffix_, os); 1015e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1016e35fdd936d133bf8a48de140a3c666897588a05shiqian 1017e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeNegationTo(::std::ostream* os) const { 1018e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "doesn't end with "; 1019e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<StringType>::Print(suffix_, os); 1020e35fdd936d133bf8a48de140a3c666897588a05shiqian } 102132de5f53763125925e078498250f7e73a88de9edzhanyong.wan 1022e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1023e35fdd936d133bf8a48de140a3c666897588a05shiqian const StringType suffix_; 102432de5f53763125925e078498250f7e73a88de9edzhanyong.wan 102532de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(EndsWithMatcher); 1026e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1027e35fdd936d133bf8a48de140a3c666897588a05shiqian 1028e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements polymorphic matchers MatchesRegex(regex) and 1029e35fdd936d133bf8a48de140a3c666897588a05shiqian// ContainsRegex(regex), which can be used as a Matcher<T> as long as 1030e35fdd936d133bf8a48de140a3c666897588a05shiqian// T can be converted to a string. 1031e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatchesRegexMatcher { 1032e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1033e35fdd936d133bf8a48de140a3c666897588a05shiqian MatchesRegexMatcher(const RE* regex, bool full_match) 1034e35fdd936d133bf8a48de140a3c666897588a05shiqian : regex_(regex), full_match_(full_match) {} 1035e35fdd936d133bf8a48de140a3c666897588a05shiqian 1036e35fdd936d133bf8a48de140a3c666897588a05shiqian // These overloaded methods allow MatchesRegex(regex) to be used as 1037e35fdd936d133bf8a48de140a3c666897588a05shiqian // a Matcher<T> as long as T can be converted to string. Returns 1038e35fdd936d133bf8a48de140a3c666897588a05shiqian // true iff s matches regular expression regex. When full_match_ is 1039e35fdd936d133bf8a48de140a3c666897588a05shiqian // true, a full match is done; otherwise a partial match is done. 1040db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan bool MatchAndExplain(const char* s, 1041db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan MatchResultListener* listener) const { 1042db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan return s != NULL && MatchAndExplain(internal::string(s), listener); 1043e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1044e35fdd936d133bf8a48de140a3c666897588a05shiqian 1045db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan bool MatchAndExplain(const internal::string& s, 1046db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan MatchResultListener* /* listener */) const { 1047e35fdd936d133bf8a48de140a3c666897588a05shiqian return full_match_ ? RE::FullMatch(s, *regex_) : 1048e35fdd936d133bf8a48de140a3c666897588a05shiqian RE::PartialMatch(s, *regex_); 1049e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1050e35fdd936d133bf8a48de140a3c666897588a05shiqian 1051e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeTo(::std::ostream* os) const { 1052e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << (full_match_ ? "matches" : "contains") 1053e35fdd936d133bf8a48de140a3c666897588a05shiqian << " regular expression "; 1054e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<internal::string>::Print(regex_->pattern(), os); 1055e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1056e35fdd936d133bf8a48de140a3c666897588a05shiqian 1057e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeNegationTo(::std::ostream* os) const { 1058e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "doesn't " << (full_match_ ? "match" : "contain") 1059e35fdd936d133bf8a48de140a3c666897588a05shiqian << " regular expression "; 1060e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<internal::string>::Print(regex_->pattern(), os); 1061e35fdd936d133bf8a48de140a3c666897588a05shiqian } 106232de5f53763125925e078498250f7e73a88de9edzhanyong.wan 1063e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1064e35fdd936d133bf8a48de140a3c666897588a05shiqian const internal::linked_ptr<const RE> regex_; 1065e35fdd936d133bf8a48de140a3c666897588a05shiqian const bool full_match_; 106632de5f53763125925e078498250f7e73a88de9edzhanyong.wan 106732de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(MatchesRegexMatcher); 1068e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1069e35fdd936d133bf8a48de140a3c666897588a05shiqian 1070e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements a matcher that compares the two fields of a 2-tuple 1071e35fdd936d133bf8a48de140a3c666897588a05shiqian// using one of the ==, <=, <, etc, operators. The two fields being 1072e35fdd936d133bf8a48de140a3c666897588a05shiqian// compared don't have to have the same type. 1073e35fdd936d133bf8a48de140a3c666897588a05shiqian// 1074e35fdd936d133bf8a48de140a3c666897588a05shiqian// The matcher defined here is polymorphic (for example, Eq() can be 1075e35fdd936d133bf8a48de140a3c666897588a05shiqian// used to match a tuple<int, short>, a tuple<const long&, double>, 1076e35fdd936d133bf8a48de140a3c666897588a05shiqian// etc). Therefore we use a template type conversion operator in the 1077e35fdd936d133bf8a48de140a3c666897588a05shiqian// implementation. 1078e35fdd936d133bf8a48de140a3c666897588a05shiqian// 1079e35fdd936d133bf8a48de140a3c666897588a05shiqian// We define this as a macro in order to eliminate duplicated source 1080e35fdd936d133bf8a48de140a3c666897588a05shiqian// code. 10812661c6821a4d7964697e48747c4d739e1ac3a243zhanyong.wan#define GMOCK_IMPLEMENT_COMPARISON2_MATCHER_(name, op) \ 1082e35fdd936d133bf8a48de140a3c666897588a05shiqian class name##2Matcher { \ 1083e35fdd936d133bf8a48de140a3c666897588a05shiqian public: \ 1084e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T1, typename T2> \ 1085e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<const ::std::tr1::tuple<T1, T2>&>() const { \ 1086e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakeMatcher(new Impl<T1, T2>); \ 1087e35fdd936d133bf8a48de140a3c666897588a05shiqian } \ 1088e35fdd936d133bf8a48de140a3c666897588a05shiqian private: \ 1089e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T1, typename T2> \ 1090e35fdd936d133bf8a48de140a3c666897588a05shiqian class Impl : public MatcherInterface<const ::std::tr1::tuple<T1, T2>&> { \ 1091e35fdd936d133bf8a48de140a3c666897588a05shiqian public: \ 1092821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual bool MatchAndExplain( \ 1093821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan const ::std::tr1::tuple<T1, T2>& args, \ 1094821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan MatchResultListener* /* listener */) const { \ 1095e35fdd936d133bf8a48de140a3c666897588a05shiqian return ::std::tr1::get<0>(args) op ::std::tr1::get<1>(args); \ 1096e35fdd936d133bf8a48de140a3c666897588a05shiqian } \ 1097e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeTo(::std::ostream* os) const { \ 10982661c6821a4d7964697e48747c4d739e1ac3a243zhanyong.wan *os << "are a pair (x, y) where x " #op " y"; \ 1099e35fdd936d133bf8a48de140a3c666897588a05shiqian } \ 1100e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeNegationTo(::std::ostream* os) const { \ 11012661c6821a4d7964697e48747c4d739e1ac3a243zhanyong.wan *os << "are a pair (x, y) where x " #op " y is false"; \ 1102e35fdd936d133bf8a48de140a3c666897588a05shiqian } \ 1103e35fdd936d133bf8a48de140a3c666897588a05shiqian }; \ 1104e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1105e35fdd936d133bf8a48de140a3c666897588a05shiqian 1106e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements Eq(), Ge(), Gt(), Le(), Lt(), and Ne() respectively. 11072661c6821a4d7964697e48747c4d739e1ac3a243zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Eq, ==); 11082661c6821a4d7964697e48747c4d739e1ac3a243zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Ge, >=); 11092661c6821a4d7964697e48747c4d739e1ac3a243zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Gt, >); 11102661c6821a4d7964697e48747c4d739e1ac3a243zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Le, <=); 11112661c6821a4d7964697e48747c4d739e1ac3a243zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Lt, <); 11122661c6821a4d7964697e48747c4d739e1ac3a243zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Ne, !=); 1113e35fdd936d133bf8a48de140a3c666897588a05shiqian 1114e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan#undef GMOCK_IMPLEMENT_COMPARISON2_MATCHER_ 1115e35fdd936d133bf8a48de140a3c666897588a05shiqian 1116c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// Implements the Not(...) matcher for a particular argument type T. 1117c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// We do not nest it inside the NotMatcher class template, as that 1118c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// will prevent different instantiations of NotMatcher from sharing 1119c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// the same NotMatcherImpl<T> class. 1120c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wantemplate <typename T> 1121c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wanclass NotMatcherImpl : public MatcherInterface<T> { 1122c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan public: 1123c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan explicit NotMatcherImpl(const Matcher<T>& matcher) 1124c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan : matcher_(matcher) {} 1125c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan 1126821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual bool MatchAndExplain(T x, MatchResultListener* listener) const { 1127821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return !matcher_.MatchAndExplain(x, listener); 1128c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 1129c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan 1130c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan virtual void DescribeTo(::std::ostream* os) const { 1131c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan matcher_.DescribeNegationTo(os); 1132c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 1133c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan 1134c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan virtual void DescribeNegationTo(::std::ostream* os) const { 1135c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan matcher_.DescribeTo(os); 1136c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 1137c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan 1138c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan private: 1139c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan const Matcher<T> matcher_; 114032de5f53763125925e078498250f7e73a88de9edzhanyong.wan 114132de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(NotMatcherImpl); 1142c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan}; 1143c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan 1144e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the Not(m) matcher, which matches a value that doesn't 1145e35fdd936d133bf8a48de140a3c666897588a05shiqian// match matcher m. 1146e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename InnerMatcher> 1147e35fdd936d133bf8a48de140a3c666897588a05shiqianclass NotMatcher { 1148e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1149e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit NotMatcher(InnerMatcher matcher) : matcher_(matcher) {} 1150e35fdd936d133bf8a48de140a3c666897588a05shiqian 1151e35fdd936d133bf8a48de140a3c666897588a05shiqian // This template type conversion operator allows Not(m) to be used 1152e35fdd936d133bf8a48de140a3c666897588a05shiqian // to match any type m can match. 1153e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 1154e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<T>() const { 1155c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan return Matcher<T>(new NotMatcherImpl<T>(SafeMatcherCast<T>(matcher_))); 1156e35fdd936d133bf8a48de140a3c666897588a05shiqian } 115732de5f53763125925e078498250f7e73a88de9edzhanyong.wan 1158e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1159c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan InnerMatcher matcher_; 116032de5f53763125925e078498250f7e73a88de9edzhanyong.wan 116132de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(NotMatcher); 1162c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan}; 1163e35fdd936d133bf8a48de140a3c666897588a05shiqian 1164c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// Implements the AllOf(m1, m2) matcher for a particular argument type 1165c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// T. We do not nest it inside the BothOfMatcher class template, as 1166c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// that will prevent different instantiations of BothOfMatcher from 1167c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// sharing the same BothOfMatcherImpl<T> class. 1168c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wantemplate <typename T> 1169c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wanclass BothOfMatcherImpl : public MatcherInterface<T> { 1170c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan public: 1171c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan BothOfMatcherImpl(const Matcher<T>& matcher1, const Matcher<T>& matcher2) 1172c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan : matcher1_(matcher1), matcher2_(matcher2) {} 1173e35fdd936d133bf8a48de140a3c666897588a05shiqian 1174c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan virtual void DescribeTo(::std::ostream* os) const { 1175c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan *os << "("; 1176c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan matcher1_.DescribeTo(os); 1177c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan *os << ") and ("; 1178c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan matcher2_.DescribeTo(os); 1179c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan *os << ")"; 1180c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 1181e35fdd936d133bf8a48de140a3c666897588a05shiqian 1182c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan virtual void DescribeNegationTo(::std::ostream* os) const { 1183c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan *os << "not "; 1184c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan DescribeTo(os); 1185c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 1186e35fdd936d133bf8a48de140a3c666897588a05shiqian 1187821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual bool MatchAndExplain(T x, MatchResultListener* listener) const { 1188821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // If either matcher1_ or matcher2_ doesn't match x, we only need 1189821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // to explain why one of them fails. 1190821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan StringMatchResultListener listener1; 1191821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (!matcher1_.MatchAndExplain(x, &listener1)) { 1192821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << listener1.str(); 1193821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return false; 1194821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan } 1195c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan 1196821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan StringMatchResultListener listener2; 1197821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (!matcher2_.MatchAndExplain(x, &listener2)) { 1198821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << listener2.str(); 1199821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return false; 1200821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan } 1201c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan 1202821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // Otherwise we need to explain why *both* of them match. 1203821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan const internal::string s1 = listener1.str(); 1204821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan const internal::string s2 = listener2.str(); 1205821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 1206821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (s1 == "") { 1207821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << s2; 1208c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } else { 1209821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << s1; 1210821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (s2 != "") { 1211821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << "; " << s2; 1212c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 1213c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 1214821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return true; 1215c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 121632de5f53763125925e078498250f7e73a88de9edzhanyong.wan 1217c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan private: 1218c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan const Matcher<T> matcher1_; 1219c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan const Matcher<T> matcher2_; 122032de5f53763125925e078498250f7e73a88de9edzhanyong.wan 122132de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(BothOfMatcherImpl); 1222e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1223e35fdd936d133bf8a48de140a3c666897588a05shiqian 1224e35fdd936d133bf8a48de140a3c666897588a05shiqian// Used for implementing the AllOf(m_1, ..., m_n) matcher, which 1225e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a value that matches all of the matchers m_1, ..., and m_n. 1226e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2> 1227e35fdd936d133bf8a48de140a3c666897588a05shiqianclass BothOfMatcher { 1228e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1229e35fdd936d133bf8a48de140a3c666897588a05shiqian BothOfMatcher(Matcher1 matcher1, Matcher2 matcher2) 1230e35fdd936d133bf8a48de140a3c666897588a05shiqian : matcher1_(matcher1), matcher2_(matcher2) {} 1231e35fdd936d133bf8a48de140a3c666897588a05shiqian 1232e35fdd936d133bf8a48de140a3c666897588a05shiqian // This template type conversion operator allows a 1233e35fdd936d133bf8a48de140a3c666897588a05shiqian // BothOfMatcher<Matcher1, Matcher2> object to match any type that 1234e35fdd936d133bf8a48de140a3c666897588a05shiqian // both Matcher1 and Matcher2 can match. 1235e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 1236e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<T>() const { 1237c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan return Matcher<T>(new BothOfMatcherImpl<T>(SafeMatcherCast<T>(matcher1_), 1238c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan SafeMatcherCast<T>(matcher2_))); 1239e35fdd936d133bf8a48de140a3c666897588a05shiqian } 124032de5f53763125925e078498250f7e73a88de9edzhanyong.wan 1241e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1242c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan Matcher1 matcher1_; 1243c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan Matcher2 matcher2_; 124432de5f53763125925e078498250f7e73a88de9edzhanyong.wan 124532de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(BothOfMatcher); 1246c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan}; 1247e35fdd936d133bf8a48de140a3c666897588a05shiqian 1248c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// Implements the AnyOf(m1, m2) matcher for a particular argument type 1249c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// T. We do not nest it inside the AnyOfMatcher class template, as 1250c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// that will prevent different instantiations of AnyOfMatcher from 1251c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// sharing the same EitherOfMatcherImpl<T> class. 1252c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wantemplate <typename T> 1253c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wanclass EitherOfMatcherImpl : public MatcherInterface<T> { 1254c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan public: 1255c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan EitherOfMatcherImpl(const Matcher<T>& matcher1, const Matcher<T>& matcher2) 1256c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan : matcher1_(matcher1), matcher2_(matcher2) {} 1257e35fdd936d133bf8a48de140a3c666897588a05shiqian 1258c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan virtual void DescribeTo(::std::ostream* os) const { 1259c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan *os << "("; 1260c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan matcher1_.DescribeTo(os); 1261c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan *os << ") or ("; 1262c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan matcher2_.DescribeTo(os); 1263c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan *os << ")"; 1264c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 1265e35fdd936d133bf8a48de140a3c666897588a05shiqian 1266c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan virtual void DescribeNegationTo(::std::ostream* os) const { 1267c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan *os << "not "; 1268c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan DescribeTo(os); 1269c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 1270c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan 1271821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual bool MatchAndExplain(T x, MatchResultListener* listener) const { 1272821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // If either matcher1_ or matcher2_ matches x, we just need to 1273821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // explain why *one* of them matches. 1274821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan StringMatchResultListener listener1; 1275821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (matcher1_.MatchAndExplain(x, &listener1)) { 1276821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << listener1.str(); 1277821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return true; 1278821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan } 1279c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan 1280821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan StringMatchResultListener listener2; 1281821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (matcher2_.MatchAndExplain(x, &listener2)) { 1282821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << listener2.str(); 1283821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return true; 1284821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan } 1285c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan 1286821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // Otherwise we need to explain why *both* of them fail. 1287821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan const internal::string s1 = listener1.str(); 1288821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan const internal::string s2 = listener2.str(); 1289821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 1290821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (s1 == "") { 1291821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << s2; 1292821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan } else { 1293821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << s1; 1294821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (s2 != "") { 1295821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << "; " << s2; 1296e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1297e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1298821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return false; 1299c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 130032de5f53763125925e078498250f7e73a88de9edzhanyong.wan 1301c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan private: 1302c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan const Matcher<T> matcher1_; 1303c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan const Matcher<T> matcher2_; 130432de5f53763125925e078498250f7e73a88de9edzhanyong.wan 130532de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(EitherOfMatcherImpl); 1306e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1307e35fdd936d133bf8a48de140a3c666897588a05shiqian 1308e35fdd936d133bf8a48de140a3c666897588a05shiqian// Used for implementing the AnyOf(m_1, ..., m_n) matcher, which 1309e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a value that matches at least one of the matchers m_1, ..., 1310e35fdd936d133bf8a48de140a3c666897588a05shiqian// and m_n. 1311e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2> 1312e35fdd936d133bf8a48de140a3c666897588a05shiqianclass EitherOfMatcher { 1313e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1314e35fdd936d133bf8a48de140a3c666897588a05shiqian EitherOfMatcher(Matcher1 matcher1, Matcher2 matcher2) 1315e35fdd936d133bf8a48de140a3c666897588a05shiqian : matcher1_(matcher1), matcher2_(matcher2) {} 1316e35fdd936d133bf8a48de140a3c666897588a05shiqian 1317e35fdd936d133bf8a48de140a3c666897588a05shiqian // This template type conversion operator allows a 1318e35fdd936d133bf8a48de140a3c666897588a05shiqian // EitherOfMatcher<Matcher1, Matcher2> object to match any type that 1319e35fdd936d133bf8a48de140a3c666897588a05shiqian // both Matcher1 and Matcher2 can match. 1320e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 1321e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<T>() const { 132216cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan return Matcher<T>(new EitherOfMatcherImpl<T>( 132316cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan SafeMatcherCast<T>(matcher1_), SafeMatcherCast<T>(matcher2_))); 1324e35fdd936d133bf8a48de140a3c666897588a05shiqian } 132532de5f53763125925e078498250f7e73a88de9edzhanyong.wan 1326e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1327e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher1 matcher1_; 1328e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher2 matcher2_; 132932de5f53763125925e078498250f7e73a88de9edzhanyong.wan 133032de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(EitherOfMatcher); 1331e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1332e35fdd936d133bf8a48de140a3c666897588a05shiqian 1333e35fdd936d133bf8a48de140a3c666897588a05shiqian// Used for implementing Truly(pred), which turns a predicate into a 1334e35fdd936d133bf8a48de140a3c666897588a05shiqian// matcher. 1335e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Predicate> 1336e35fdd936d133bf8a48de140a3c666897588a05shiqianclass TrulyMatcher { 1337e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1338e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit TrulyMatcher(Predicate pred) : predicate_(pred) {} 1339e35fdd936d133bf8a48de140a3c666897588a05shiqian 1340e35fdd936d133bf8a48de140a3c666897588a05shiqian // This method template allows Truly(pred) to be used as a matcher 1341e35fdd936d133bf8a48de140a3c666897588a05shiqian // for type T where T is the argument type of predicate 'pred'. The 1342e35fdd936d133bf8a48de140a3c666897588a05shiqian // argument is passed by reference as the predicate may be 1343e35fdd936d133bf8a48de140a3c666897588a05shiqian // interested in the address of the argument. 1344e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 1345db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan bool MatchAndExplain(T& x, // NOLINT 1346db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan MatchResultListener* /* listener */) const { 1347652540a278e56528e576f9ea77515f386698a326zhanyong.wan#if GTEST_OS_WINDOWS 1348e35fdd936d133bf8a48de140a3c666897588a05shiqian // MSVC warns about converting a value into bool (warning 4800). 1349e35fdd936d133bf8a48de140a3c666897588a05shiqian#pragma warning(push) // Saves the current warning state. 1350e35fdd936d133bf8a48de140a3c666897588a05shiqian#pragma warning(disable:4800) // Temporarily disables warning 4800. 1351e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif // GTEST_OS_WINDOWS 1352e35fdd936d133bf8a48de140a3c666897588a05shiqian return predicate_(x); 1353652540a278e56528e576f9ea77515f386698a326zhanyong.wan#if GTEST_OS_WINDOWS 1354e35fdd936d133bf8a48de140a3c666897588a05shiqian#pragma warning(pop) // Restores the warning state. 1355e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif // GTEST_OS_WINDOWS 1356e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1357e35fdd936d133bf8a48de140a3c666897588a05shiqian 1358e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeTo(::std::ostream* os) const { 1359e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "satisfies the given predicate"; 1360e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1361e35fdd936d133bf8a48de140a3c666897588a05shiqian 1362e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeNegationTo(::std::ostream* os) const { 1363e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "doesn't satisfy the given predicate"; 1364e35fdd936d133bf8a48de140a3c666897588a05shiqian } 136532de5f53763125925e078498250f7e73a88de9edzhanyong.wan 1366e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1367e35fdd936d133bf8a48de140a3c666897588a05shiqian Predicate predicate_; 136832de5f53763125925e078498250f7e73a88de9edzhanyong.wan 136932de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(TrulyMatcher); 1370e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1371e35fdd936d133bf8a48de140a3c666897588a05shiqian 1372e35fdd936d133bf8a48de140a3c666897588a05shiqian// Used for implementing Matches(matcher), which turns a matcher into 1373e35fdd936d133bf8a48de140a3c666897588a05shiqian// a predicate. 1374e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename M> 1375e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatcherAsPredicate { 1376e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1377e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit MatcherAsPredicate(M matcher) : matcher_(matcher) {} 1378e35fdd936d133bf8a48de140a3c666897588a05shiqian 1379e35fdd936d133bf8a48de140a3c666897588a05shiqian // This template operator() allows Matches(m) to be used as a 1380e35fdd936d133bf8a48de140a3c666897588a05shiqian // predicate on type T where m is a matcher on type T. 1381e35fdd936d133bf8a48de140a3c666897588a05shiqian // 1382e35fdd936d133bf8a48de140a3c666897588a05shiqian // The argument x is passed by reference instead of by value, as 1383e35fdd936d133bf8a48de140a3c666897588a05shiqian // some matcher may be interested in its address (e.g. as in 1384e35fdd936d133bf8a48de140a3c666897588a05shiqian // Matches(Ref(n))(x)). 1385e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 1386e35fdd936d133bf8a48de140a3c666897588a05shiqian bool operator()(const T& x) const { 1387e35fdd936d133bf8a48de140a3c666897588a05shiqian // We let matcher_ commit to a particular type here instead of 1388e35fdd936d133bf8a48de140a3c666897588a05shiqian // when the MatcherAsPredicate object was constructed. This 1389e35fdd936d133bf8a48de140a3c666897588a05shiqian // allows us to write Matches(m) where m is a polymorphic matcher 1390e35fdd936d133bf8a48de140a3c666897588a05shiqian // (e.g. Eq(5)). 1391e35fdd936d133bf8a48de140a3c666897588a05shiqian // 1392e35fdd936d133bf8a48de140a3c666897588a05shiqian // If we write Matcher<T>(matcher_).Matches(x) here, it won't 1393e35fdd936d133bf8a48de140a3c666897588a05shiqian // compile when matcher_ has type Matcher<const T&>; if we write 1394e35fdd936d133bf8a48de140a3c666897588a05shiqian // Matcher<const T&>(matcher_).Matches(x) here, it won't compile 1395e35fdd936d133bf8a48de140a3c666897588a05shiqian // when matcher_ has type Matcher<T>; if we just write 1396e35fdd936d133bf8a48de140a3c666897588a05shiqian // matcher_.Matches(x), it won't compile when matcher_ is 1397e35fdd936d133bf8a48de140a3c666897588a05shiqian // polymorphic, e.g. Eq(5). 1398e35fdd936d133bf8a48de140a3c666897588a05shiqian // 1399e35fdd936d133bf8a48de140a3c666897588a05shiqian // MatcherCast<const T&>() is necessary for making the code work 1400e35fdd936d133bf8a48de140a3c666897588a05shiqian // in all of the above situations. 1401e35fdd936d133bf8a48de140a3c666897588a05shiqian return MatcherCast<const T&>(matcher_).Matches(x); 1402e35fdd936d133bf8a48de140a3c666897588a05shiqian } 140332de5f53763125925e078498250f7e73a88de9edzhanyong.wan 1404e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1405e35fdd936d133bf8a48de140a3c666897588a05shiqian M matcher_; 140632de5f53763125925e078498250f7e73a88de9edzhanyong.wan 140732de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(MatcherAsPredicate); 1408e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1409e35fdd936d133bf8a48de140a3c666897588a05shiqian 1410e35fdd936d133bf8a48de140a3c666897588a05shiqian// For implementing ASSERT_THAT() and EXPECT_THAT(). The template 1411e35fdd936d133bf8a48de140a3c666897588a05shiqian// argument M must be a type that can be converted to a matcher. 1412e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename M> 1413e35fdd936d133bf8a48de140a3c666897588a05shiqianclass PredicateFormatterFromMatcher { 1414e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1415e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit PredicateFormatterFromMatcher(const M& m) : matcher_(m) {} 1416e35fdd936d133bf8a48de140a3c666897588a05shiqian 1417e35fdd936d133bf8a48de140a3c666897588a05shiqian // This template () operator allows a PredicateFormatterFromMatcher 1418e35fdd936d133bf8a48de140a3c666897588a05shiqian // object to act as a predicate-formatter suitable for using with 1419e35fdd936d133bf8a48de140a3c666897588a05shiqian // Google Test's EXPECT_PRED_FORMAT1() macro. 1420e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 1421e35fdd936d133bf8a48de140a3c666897588a05shiqian AssertionResult operator()(const char* value_text, const T& x) const { 1422e35fdd936d133bf8a48de140a3c666897588a05shiqian // We convert matcher_ to a Matcher<const T&> *now* instead of 1423e35fdd936d133bf8a48de140a3c666897588a05shiqian // when the PredicateFormatterFromMatcher object was constructed, 1424e35fdd936d133bf8a48de140a3c666897588a05shiqian // as matcher_ may be polymorphic (e.g. NotNull()) and we won't 1425e35fdd936d133bf8a48de140a3c666897588a05shiqian // know which type to instantiate it to until we actually see the 1426e35fdd936d133bf8a48de140a3c666897588a05shiqian // type of x here. 1427e35fdd936d133bf8a48de140a3c666897588a05shiqian // 1428e35fdd936d133bf8a48de140a3c666897588a05shiqian // We write MatcherCast<const T&>(matcher_) instead of 1429e35fdd936d133bf8a48de140a3c666897588a05shiqian // Matcher<const T&>(matcher_), as the latter won't compile when 1430e35fdd936d133bf8a48de140a3c666897588a05shiqian // matcher_ has type Matcher<T> (e.g. An<int>()). 1431e35fdd936d133bf8a48de140a3c666897588a05shiqian const Matcher<const T&> matcher = MatcherCast<const T&>(matcher_); 1432821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan StringMatchResultListener listener; 1433821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (matcher.MatchAndExplain(x, &listener)) { 1434e35fdd936d133bf8a48de140a3c666897588a05shiqian return AssertionSuccess(); 1435e35fdd936d133bf8a48de140a3c666897588a05shiqian } else { 1436e35fdd936d133bf8a48de140a3c666897588a05shiqian ::std::stringstream ss; 1437e35fdd936d133bf8a48de140a3c666897588a05shiqian ss << "Value of: " << value_text << "\n" 1438e35fdd936d133bf8a48de140a3c666897588a05shiqian << "Expected: "; 1439e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher.DescribeTo(&ss); 1440e35fdd936d133bf8a48de140a3c666897588a05shiqian ss << "\n Actual: "; 1441e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<T>::Print(x, &ss); 1442821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan StreamInParensAsNeeded(listener.str(), &ss); 1443e35fdd936d133bf8a48de140a3c666897588a05shiqian return AssertionFailure(Message() << ss.str()); 1444e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1445e35fdd936d133bf8a48de140a3c666897588a05shiqian } 144632de5f53763125925e078498250f7e73a88de9edzhanyong.wan 1447e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1448e35fdd936d133bf8a48de140a3c666897588a05shiqian const M matcher_; 144932de5f53763125925e078498250f7e73a88de9edzhanyong.wan 145032de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(PredicateFormatterFromMatcher); 1451e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1452e35fdd936d133bf8a48de140a3c666897588a05shiqian 1453e35fdd936d133bf8a48de140a3c666897588a05shiqian// A helper function for converting a matcher to a predicate-formatter 1454e35fdd936d133bf8a48de140a3c666897588a05shiqian// without the user needing to explicitly write the type. This is 1455e35fdd936d133bf8a48de140a3c666897588a05shiqian// used for implementing ASSERT_THAT() and EXPECT_THAT(). 1456e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename M> 1457e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PredicateFormatterFromMatcher<M> 1458e35fdd936d133bf8a48de140a3c666897588a05shiqianMakePredicateFormatterFromMatcher(const M& matcher) { 1459e35fdd936d133bf8a48de140a3c666897588a05shiqian return PredicateFormatterFromMatcher<M>(matcher); 1460e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1461e35fdd936d133bf8a48de140a3c666897588a05shiqian 1462e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the polymorphic floating point equality matcher, which 1463e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches two float values using ULP-based approximation. The 1464e35fdd936d133bf8a48de140a3c666897588a05shiqian// template is meant to be instantiated with FloatType being either 1465e35fdd936d133bf8a48de140a3c666897588a05shiqian// float or double. 1466e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename FloatType> 1467e35fdd936d133bf8a48de140a3c666897588a05shiqianclass FloatingEqMatcher { 1468e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1469e35fdd936d133bf8a48de140a3c666897588a05shiqian // Constructor for FloatingEqMatcher. 1470e35fdd936d133bf8a48de140a3c666897588a05shiqian // The matcher's input will be compared with rhs. The matcher treats two 1471e35fdd936d133bf8a48de140a3c666897588a05shiqian // NANs as equal if nan_eq_nan is true. Otherwise, under IEEE standards, 1472e35fdd936d133bf8a48de140a3c666897588a05shiqian // equality comparisons between NANs will always return false. 1473e35fdd936d133bf8a48de140a3c666897588a05shiqian FloatingEqMatcher(FloatType rhs, bool nan_eq_nan) : 1474e35fdd936d133bf8a48de140a3c666897588a05shiqian rhs_(rhs), nan_eq_nan_(nan_eq_nan) {} 1475e35fdd936d133bf8a48de140a3c666897588a05shiqian 1476e35fdd936d133bf8a48de140a3c666897588a05shiqian // Implements floating point equality matcher as a Matcher<T>. 1477e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 1478e35fdd936d133bf8a48de140a3c666897588a05shiqian class Impl : public MatcherInterface<T> { 1479e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1480e35fdd936d133bf8a48de140a3c666897588a05shiqian Impl(FloatType rhs, bool nan_eq_nan) : 1481e35fdd936d133bf8a48de140a3c666897588a05shiqian rhs_(rhs), nan_eq_nan_(nan_eq_nan) {} 1482e35fdd936d133bf8a48de140a3c666897588a05shiqian 1483821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual bool MatchAndExplain(T value, 1484821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan MatchResultListener* /* listener */) const { 1485e35fdd936d133bf8a48de140a3c666897588a05shiqian const FloatingPoint<FloatType> lhs(value), rhs(rhs_); 1486e35fdd936d133bf8a48de140a3c666897588a05shiqian 1487e35fdd936d133bf8a48de140a3c666897588a05shiqian // Compares NaNs first, if nan_eq_nan_ is true. 1488e35fdd936d133bf8a48de140a3c666897588a05shiqian if (nan_eq_nan_ && lhs.is_nan()) { 1489e35fdd936d133bf8a48de140a3c666897588a05shiqian return rhs.is_nan(); 1490e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1491e35fdd936d133bf8a48de140a3c666897588a05shiqian 1492e35fdd936d133bf8a48de140a3c666897588a05shiqian return lhs.AlmostEquals(rhs); 1493e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1494e35fdd936d133bf8a48de140a3c666897588a05shiqian 1495e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeTo(::std::ostream* os) const { 1496e35fdd936d133bf8a48de140a3c666897588a05shiqian // os->precision() returns the previously set precision, which we 1497e35fdd936d133bf8a48de140a3c666897588a05shiqian // store to restore the ostream to its original configuration 1498e35fdd936d133bf8a48de140a3c666897588a05shiqian // after outputting. 1499e35fdd936d133bf8a48de140a3c666897588a05shiqian const ::std::streamsize old_precision = os->precision( 1500e35fdd936d133bf8a48de140a3c666897588a05shiqian ::std::numeric_limits<FloatType>::digits10 + 2); 1501e35fdd936d133bf8a48de140a3c666897588a05shiqian if (FloatingPoint<FloatType>(rhs_).is_nan()) { 1502e35fdd936d133bf8a48de140a3c666897588a05shiqian if (nan_eq_nan_) { 1503e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "is NaN"; 1504e35fdd936d133bf8a48de140a3c666897588a05shiqian } else { 1505e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "never matches"; 1506e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1507e35fdd936d133bf8a48de140a3c666897588a05shiqian } else { 1508e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "is approximately " << rhs_; 1509e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1510e35fdd936d133bf8a48de140a3c666897588a05shiqian os->precision(old_precision); 1511e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1512e35fdd936d133bf8a48de140a3c666897588a05shiqian 1513e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeNegationTo(::std::ostream* os) const { 1514e35fdd936d133bf8a48de140a3c666897588a05shiqian // As before, get original precision. 1515e35fdd936d133bf8a48de140a3c666897588a05shiqian const ::std::streamsize old_precision = os->precision( 1516e35fdd936d133bf8a48de140a3c666897588a05shiqian ::std::numeric_limits<FloatType>::digits10 + 2); 1517e35fdd936d133bf8a48de140a3c666897588a05shiqian if (FloatingPoint<FloatType>(rhs_).is_nan()) { 1518e35fdd936d133bf8a48de140a3c666897588a05shiqian if (nan_eq_nan_) { 1519e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "is not NaN"; 1520e35fdd936d133bf8a48de140a3c666897588a05shiqian } else { 1521e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "is anything"; 1522e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1523e35fdd936d133bf8a48de140a3c666897588a05shiqian } else { 1524e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "is not approximately " << rhs_; 1525e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1526e35fdd936d133bf8a48de140a3c666897588a05shiqian // Restore original precision. 1527e35fdd936d133bf8a48de140a3c666897588a05shiqian os->precision(old_precision); 1528e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1529e35fdd936d133bf8a48de140a3c666897588a05shiqian 1530e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1531e35fdd936d133bf8a48de140a3c666897588a05shiqian const FloatType rhs_; 1532e35fdd936d133bf8a48de140a3c666897588a05shiqian const bool nan_eq_nan_; 153332de5f53763125925e078498250f7e73a88de9edzhanyong.wan 153432de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(Impl); 1535e35fdd936d133bf8a48de140a3c666897588a05shiqian }; 1536e35fdd936d133bf8a48de140a3c666897588a05shiqian 1537e35fdd936d133bf8a48de140a3c666897588a05shiqian // The following 3 type conversion operators allow FloatEq(rhs) and 1538e35fdd936d133bf8a48de140a3c666897588a05shiqian // NanSensitiveFloatEq(rhs) to be used as a Matcher<float>, a 1539e35fdd936d133bf8a48de140a3c666897588a05shiqian // Matcher<const float&>, or a Matcher<float&>, but nothing else. 1540e35fdd936d133bf8a48de140a3c666897588a05shiqian // (While Google's C++ coding style doesn't allow arguments passed 1541e35fdd936d133bf8a48de140a3c666897588a05shiqian // by non-const reference, we may see them in code not conforming to 1542e35fdd936d133bf8a48de140a3c666897588a05shiqian // the style. Therefore Google Mock needs to support them.) 1543e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<FloatType>() const { 1544e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakeMatcher(new Impl<FloatType>(rhs_, nan_eq_nan_)); 1545e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1546e35fdd936d133bf8a48de140a3c666897588a05shiqian 1547e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<const FloatType&>() const { 1548e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakeMatcher(new Impl<const FloatType&>(rhs_, nan_eq_nan_)); 1549e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1550e35fdd936d133bf8a48de140a3c666897588a05shiqian 1551e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<FloatType&>() const { 1552e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakeMatcher(new Impl<FloatType&>(rhs_, nan_eq_nan_)); 1553e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1554e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1555e35fdd936d133bf8a48de140a3c666897588a05shiqian const FloatType rhs_; 1556e35fdd936d133bf8a48de140a3c666897588a05shiqian const bool nan_eq_nan_; 155732de5f53763125925e078498250f7e73a88de9edzhanyong.wan 155832de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(FloatingEqMatcher); 1559e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1560e35fdd936d133bf8a48de140a3c666897588a05shiqian 1561e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the Pointee(m) matcher for matching a pointer whose 1562e35fdd936d133bf8a48de140a3c666897588a05shiqian// pointee matches matcher m. The pointer can be either raw or smart. 1563e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename InnerMatcher> 1564e35fdd936d133bf8a48de140a3c666897588a05shiqianclass PointeeMatcher { 1565e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1566e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit PointeeMatcher(const InnerMatcher& matcher) : matcher_(matcher) {} 1567e35fdd936d133bf8a48de140a3c666897588a05shiqian 1568e35fdd936d133bf8a48de140a3c666897588a05shiqian // This type conversion operator template allows Pointee(m) to be 1569e35fdd936d133bf8a48de140a3c666897588a05shiqian // used as a matcher for any pointer type whose pointee type is 1570e35fdd936d133bf8a48de140a3c666897588a05shiqian // compatible with the inner matcher, where type Pointer can be 1571e35fdd936d133bf8a48de140a3c666897588a05shiqian // either a raw pointer or a smart pointer. 1572e35fdd936d133bf8a48de140a3c666897588a05shiqian // 1573e35fdd936d133bf8a48de140a3c666897588a05shiqian // The reason we do this instead of relying on 1574e35fdd936d133bf8a48de140a3c666897588a05shiqian // MakePolymorphicMatcher() is that the latter is not flexible 1575e35fdd936d133bf8a48de140a3c666897588a05shiqian // enough for implementing the DescribeTo() method of Pointee(). 1576e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename Pointer> 1577e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<Pointer>() const { 1578e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakeMatcher(new Impl<Pointer>(matcher_)); 1579e35fdd936d133bf8a48de140a3c666897588a05shiqian } 158032de5f53763125925e078498250f7e73a88de9edzhanyong.wan 1581e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1582e35fdd936d133bf8a48de140a3c666897588a05shiqian // The monomorphic implementation that works for a particular pointer type. 1583e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename Pointer> 1584e35fdd936d133bf8a48de140a3c666897588a05shiqian class Impl : public MatcherInterface<Pointer> { 1585e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1586e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan typedef typename PointeeOf<GMOCK_REMOVE_CONST_( // NOLINT 1587e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan GMOCK_REMOVE_REFERENCE_(Pointer))>::type Pointee; 1588e35fdd936d133bf8a48de140a3c666897588a05shiqian 1589e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit Impl(const InnerMatcher& matcher) 1590e35fdd936d133bf8a48de140a3c666897588a05shiqian : matcher_(MatcherCast<const Pointee&>(matcher)) {} 1591e35fdd936d133bf8a48de140a3c666897588a05shiqian 1592e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeTo(::std::ostream* os) const { 1593e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "points to a value that "; 1594e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher_.DescribeTo(os); 1595e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1596e35fdd936d133bf8a48de140a3c666897588a05shiqian 1597e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeNegationTo(::std::ostream* os) const { 1598e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "does not point to a value that "; 1599e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher_.DescribeTo(os); 1600e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1601e35fdd936d133bf8a48de140a3c666897588a05shiqian 1602821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual bool MatchAndExplain(Pointer pointer, 1603821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan MatchResultListener* listener) const { 1604e35fdd936d133bf8a48de140a3c666897588a05shiqian if (GetRawPointer(pointer) == NULL) 1605821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return false; 1606e35fdd936d133bf8a48de140a3c666897588a05shiqian 1607821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan StringMatchResultListener inner_listener; 1608821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan const bool match = matcher_.MatchAndExplain(*pointer, &inner_listener); 1609821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan const internal::string s = inner_listener.str(); 1610e35fdd936d133bf8a48de140a3c666897588a05shiqian if (s != "") { 1611821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << "points to a value that " << s; 1612e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1613821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return match; 1614e35fdd936d133bf8a48de140a3c666897588a05shiqian } 161532de5f53763125925e078498250f7e73a88de9edzhanyong.wan 1616e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1617e35fdd936d133bf8a48de140a3c666897588a05shiqian const Matcher<const Pointee&> matcher_; 161832de5f53763125925e078498250f7e73a88de9edzhanyong.wan 161932de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(Impl); 1620e35fdd936d133bf8a48de140a3c666897588a05shiqian }; 1621e35fdd936d133bf8a48de140a3c666897588a05shiqian 1622e35fdd936d133bf8a48de140a3c666897588a05shiqian const InnerMatcher matcher_; 162332de5f53763125925e078498250f7e73a88de9edzhanyong.wan 162432de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(PointeeMatcher); 1625e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1626e35fdd936d133bf8a48de140a3c666897588a05shiqian 1627e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the Field() matcher for matching a field (i.e. member 1628e35fdd936d133bf8a48de140a3c666897588a05shiqian// variable) of an object. 1629e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Class, typename FieldType> 1630e35fdd936d133bf8a48de140a3c666897588a05shiqianclass FieldMatcher { 1631e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1632e35fdd936d133bf8a48de140a3c666897588a05shiqian FieldMatcher(FieldType Class::*field, 1633e35fdd936d133bf8a48de140a3c666897588a05shiqian const Matcher<const FieldType&>& matcher) 1634e35fdd936d133bf8a48de140a3c666897588a05shiqian : field_(field), matcher_(matcher) {} 1635e35fdd936d133bf8a48de140a3c666897588a05shiqian 1636e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeTo(::std::ostream* os) const { 1637e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "the given field "; 1638e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher_.DescribeTo(os); 1639e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1640e35fdd936d133bf8a48de140a3c666897588a05shiqian 1641e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeNegationTo(::std::ostream* os) const { 1642e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "the given field "; 1643e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher_.DescribeNegationTo(os); 1644e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1645e35fdd936d133bf8a48de140a3c666897588a05shiqian 1646db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan template <typename T> 1647db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan bool MatchAndExplain(const T& value, MatchResultListener* listener) const { 1648db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan return MatchAndExplainImpl( 1649db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan typename ::testing::internal:: 1650db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan is_pointer<GMOCK_REMOVE_CONST_(T)>::type(), 1651db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan value, listener); 1652db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan } 1653db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan 1654db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan private: 1655db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan // The first argument of MatchAndExplainImpl() is needed to help 165618490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan // Symbian's C++ compiler choose which overload to use. Its type is 165718490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan // true_type iff the Field() matcher is used to match a pointer. 1658db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan bool MatchAndExplainImpl(false_type /* is_not_pointer */, const Class& obj, 1659db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan MatchResultListener* listener) const { 1660821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan StringMatchResultListener inner_listener; 1661821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan const bool match = matcher_.MatchAndExplain(obj.*field_, &inner_listener); 1662821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan const internal::string s = inner_listener.str(); 1663e35fdd936d133bf8a48de140a3c666897588a05shiqian if (s != "") { 1664821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << "the given field " << s; 1665e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1666821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return match; 1667e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1668e35fdd936d133bf8a48de140a3c666897588a05shiqian 1669db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan bool MatchAndExplainImpl(true_type /* is_pointer */, const Class* p, 1670db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan MatchResultListener* listener) const { 1671821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (p == NULL) 1672821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return false; 1673821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 1674821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // Since *p has a field, it must be a class/struct/union type and 1675821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // thus cannot be a pointer. Therefore we pass false_type() as 1676821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // the first argument. 1677db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan return MatchAndExplainImpl(false_type(), *p, listener); 1678e35fdd936d133bf8a48de140a3c666897588a05shiqian } 167932de5f53763125925e078498250f7e73a88de9edzhanyong.wan 1680e35fdd936d133bf8a48de140a3c666897588a05shiqian const FieldType Class::*field_; 1681e35fdd936d133bf8a48de140a3c666897588a05shiqian const Matcher<const FieldType&> matcher_; 168232de5f53763125925e078498250f7e73a88de9edzhanyong.wan 168332de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(FieldMatcher); 1684e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1685e35fdd936d133bf8a48de140a3c666897588a05shiqian 1686e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the Property() matcher for matching a property 1687e35fdd936d133bf8a48de140a3c666897588a05shiqian// (i.e. return value of a getter method) of an object. 1688e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Class, typename PropertyType> 1689e35fdd936d133bf8a48de140a3c666897588a05shiqianclass PropertyMatcher { 1690e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1691e35fdd936d133bf8a48de140a3c666897588a05shiqian // The property may have a reference type, so 'const PropertyType&' 1692e35fdd936d133bf8a48de140a3c666897588a05shiqian // may cause double references and fail to compile. That's why we 1693e35fdd936d133bf8a48de140a3c666897588a05shiqian // need GMOCK_REFERENCE_TO_CONST, which works regardless of 1694e35fdd936d133bf8a48de140a3c666897588a05shiqian // PropertyType being a reference or not. 1695e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan typedef GMOCK_REFERENCE_TO_CONST_(PropertyType) RefToConstProperty; 1696e35fdd936d133bf8a48de140a3c666897588a05shiqian 1697e35fdd936d133bf8a48de140a3c666897588a05shiqian PropertyMatcher(PropertyType (Class::*property)() const, 1698e35fdd936d133bf8a48de140a3c666897588a05shiqian const Matcher<RefToConstProperty>& matcher) 1699e35fdd936d133bf8a48de140a3c666897588a05shiqian : property_(property), matcher_(matcher) {} 1700e35fdd936d133bf8a48de140a3c666897588a05shiqian 1701e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeTo(::std::ostream* os) const { 1702e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "the given property "; 1703e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher_.DescribeTo(os); 1704e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1705e35fdd936d133bf8a48de140a3c666897588a05shiqian 1706e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeNegationTo(::std::ostream* os) const { 1707e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "the given property "; 1708e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher_.DescribeNegationTo(os); 1709e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1710e35fdd936d133bf8a48de140a3c666897588a05shiqian 1711db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan template <typename T> 1712db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan bool MatchAndExplain(const T&value, MatchResultListener* listener) const { 1713db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan return MatchAndExplainImpl( 1714db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan typename ::testing::internal:: 1715db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan is_pointer<GMOCK_REMOVE_CONST_(T)>::type(), 1716db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan value, listener); 1717db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan } 1718db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan 1719db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan private: 1720db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan // The first argument of MatchAndExplainImpl() is needed to help 172118490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan // Symbian's C++ compiler choose which overload to use. Its type is 172218490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan // true_type iff the Property() matcher is used to match a pointer. 1723db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan bool MatchAndExplainImpl(false_type /* is_not_pointer */, const Class& obj, 1724db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan MatchResultListener* listener) const { 1725821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan StringMatchResultListener inner_listener; 1726821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan const bool match = matcher_.MatchAndExplain((obj.*property_)(), 1727821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan &inner_listener); 1728821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan const internal::string s = inner_listener.str(); 1729e35fdd936d133bf8a48de140a3c666897588a05shiqian if (s != "") { 1730821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << "the given property " << s; 1731e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1732821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return match; 1733e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1734e35fdd936d133bf8a48de140a3c666897588a05shiqian 1735db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan bool MatchAndExplainImpl(true_type /* is_pointer */, const Class* p, 1736db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan MatchResultListener* listener) const { 1737821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (p == NULL) 1738821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return false; 1739821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 1740821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // Since *p has a property method, it must be a class/struct/union 1741821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // type and thus cannot be a pointer. Therefore we pass 1742821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // false_type() as the first argument. 1743db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan return MatchAndExplainImpl(false_type(), *p, listener); 1744e35fdd936d133bf8a48de140a3c666897588a05shiqian } 174532de5f53763125925e078498250f7e73a88de9edzhanyong.wan 1746e35fdd936d133bf8a48de140a3c666897588a05shiqian PropertyType (Class::*property_)() const; 1747e35fdd936d133bf8a48de140a3c666897588a05shiqian const Matcher<RefToConstProperty> matcher_; 174832de5f53763125925e078498250f7e73a88de9edzhanyong.wan 174932de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(PropertyMatcher); 1750e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1751e35fdd936d133bf8a48de140a3c666897588a05shiqian 1752e35fdd936d133bf8a48de140a3c666897588a05shiqian// Type traits specifying various features of different functors for ResultOf. 1753e35fdd936d133bf8a48de140a3c666897588a05shiqian// The default template specifies features for functor objects. 1754e35fdd936d133bf8a48de140a3c666897588a05shiqian// Functor classes have to typedef argument_type and result_type 1755e35fdd936d133bf8a48de140a3c666897588a05shiqian// to be compatible with ResultOf. 1756e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Functor> 1757e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct CallableTraits { 1758e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef typename Functor::result_type ResultType; 1759e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef Functor StorageType; 1760e35fdd936d133bf8a48de140a3c666897588a05shiqian 176132de5f53763125925e078498250f7e73a88de9edzhanyong.wan static void CheckIsValid(Functor /* functor */) {} 1762e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 1763e35fdd936d133bf8a48de140a3c666897588a05shiqian static ResultType Invoke(Functor f, T arg) { return f(arg); } 1764e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1765e35fdd936d133bf8a48de140a3c666897588a05shiqian 1766e35fdd936d133bf8a48de140a3c666897588a05shiqian// Specialization for function pointers. 1767e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename ArgType, typename ResType> 1768e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct CallableTraits<ResType(*)(ArgType)> { 1769e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef ResType ResultType; 1770e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef ResType(*StorageType)(ArgType); 1771e35fdd936d133bf8a48de140a3c666897588a05shiqian 1772e35fdd936d133bf8a48de140a3c666897588a05shiqian static void CheckIsValid(ResType(*f)(ArgType)) { 1773f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan GTEST_CHECK_(f != NULL) 1774e35fdd936d133bf8a48de140a3c666897588a05shiqian << "NULL function pointer is passed into ResultOf()."; 1775e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1776e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 1777e35fdd936d133bf8a48de140a3c666897588a05shiqian static ResType Invoke(ResType(*f)(ArgType), T arg) { 1778e35fdd936d133bf8a48de140a3c666897588a05shiqian return (*f)(arg); 1779e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1780e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1781e35fdd936d133bf8a48de140a3c666897588a05shiqian 1782e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the ResultOf() matcher for matching a return value of a 1783e35fdd936d133bf8a48de140a3c666897588a05shiqian// unary function of an object. 1784e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Callable> 1785e35fdd936d133bf8a48de140a3c666897588a05shiqianclass ResultOfMatcher { 1786e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1787e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef typename CallableTraits<Callable>::ResultType ResultType; 1788e35fdd936d133bf8a48de140a3c666897588a05shiqian 1789e35fdd936d133bf8a48de140a3c666897588a05shiqian ResultOfMatcher(Callable callable, const Matcher<ResultType>& matcher) 1790e35fdd936d133bf8a48de140a3c666897588a05shiqian : callable_(callable), matcher_(matcher) { 1791e35fdd936d133bf8a48de140a3c666897588a05shiqian CallableTraits<Callable>::CheckIsValid(callable_); 1792e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1793e35fdd936d133bf8a48de140a3c666897588a05shiqian 1794e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 1795e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<T>() const { 1796e35fdd936d133bf8a48de140a3c666897588a05shiqian return Matcher<T>(new Impl<T>(callable_, matcher_)); 1797e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1798e35fdd936d133bf8a48de140a3c666897588a05shiqian 1799e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1800e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef typename CallableTraits<Callable>::StorageType CallableStorageType; 1801e35fdd936d133bf8a48de140a3c666897588a05shiqian 1802e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 1803e35fdd936d133bf8a48de140a3c666897588a05shiqian class Impl : public MatcherInterface<T> { 1804e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1805e35fdd936d133bf8a48de140a3c666897588a05shiqian Impl(CallableStorageType callable, const Matcher<ResultType>& matcher) 1806e35fdd936d133bf8a48de140a3c666897588a05shiqian : callable_(callable), matcher_(matcher) {} 1807e35fdd936d133bf8a48de140a3c666897588a05shiqian 1808e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeTo(::std::ostream* os) const { 1809e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "result of the given callable "; 1810e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher_.DescribeTo(os); 1811e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1812e35fdd936d133bf8a48de140a3c666897588a05shiqian 1813e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeNegationTo(::std::ostream* os) const { 1814e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "result of the given callable "; 1815e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher_.DescribeNegationTo(os); 1816e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1817e35fdd936d133bf8a48de140a3c666897588a05shiqian 1818821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual bool MatchAndExplain(T obj, MatchResultListener* listener) const { 1819821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan StringMatchResultListener inner_listener; 1820821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan const bool match = matcher_.MatchAndExplain( 1821e35fdd936d133bf8a48de140a3c666897588a05shiqian CallableTraits<Callable>::template Invoke<T>(callable_, obj), 1822821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan &inner_listener); 1823821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 1824821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan const internal::string s = inner_listener.str(); 1825e35fdd936d133bf8a48de140a3c666897588a05shiqian if (s != "") 1826821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << "result of the given callable " << s; 1827821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 1828821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return match; 1829e35fdd936d133bf8a48de140a3c666897588a05shiqian } 183032de5f53763125925e078498250f7e73a88de9edzhanyong.wan 1831e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1832e35fdd936d133bf8a48de140a3c666897588a05shiqian // Functors often define operator() as non-const method even though 1833e35fdd936d133bf8a48de140a3c666897588a05shiqian // they are actualy stateless. But we need to use them even when 1834e35fdd936d133bf8a48de140a3c666897588a05shiqian // 'this' is a const pointer. It's the user's responsibility not to 1835e35fdd936d133bf8a48de140a3c666897588a05shiqian // use stateful callables with ResultOf(), which does't guarantee 1836e35fdd936d133bf8a48de140a3c666897588a05shiqian // how many times the callable will be invoked. 1837e35fdd936d133bf8a48de140a3c666897588a05shiqian mutable CallableStorageType callable_; 1838e35fdd936d133bf8a48de140a3c666897588a05shiqian const Matcher<ResultType> matcher_; 183932de5f53763125925e078498250f7e73a88de9edzhanyong.wan 184032de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(Impl); 1841e35fdd936d133bf8a48de140a3c666897588a05shiqian }; // class Impl 1842e35fdd936d133bf8a48de140a3c666897588a05shiqian 1843e35fdd936d133bf8a48de140a3c666897588a05shiqian const CallableStorageType callable_; 1844e35fdd936d133bf8a48de140a3c666897588a05shiqian const Matcher<ResultType> matcher_; 184532de5f53763125925e078498250f7e73a88de9edzhanyong.wan 184632de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(ResultOfMatcher); 1847e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1848e35fdd936d133bf8a48de140a3c666897588a05shiqian 18496a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// Implements an equality matcher for any STL-style container whose elements 18506a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// support ==. This matcher is like Eq(), but its failure explanations provide 18516a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// more detailed information that is useful when the container is used as a set. 18526a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// The failure message reports elements that are in one of the operands but not 18536a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// the other. The failure messages do not report duplicate or out-of-order 18546a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// elements in the containers (which don't properly matter to sets, but can 18556a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// occur if the containers are vectors or lists, for example). 18566a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// 18576a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// Uses the container's const_iterator, value_type, operator ==, 18586a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// begin(), and end(). 18596a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wantemplate <typename Container> 18606a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wanclass ContainerEqMatcher { 18616a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan public: 1862b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef internal::StlContainerView<Container> View; 1863b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef typename View::type StlContainer; 1864b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef typename View::const_reference StlContainerReference; 1865b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 1866b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // We make a copy of rhs in case the elements in it are modified 1867b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // after this matcher is created. 1868b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan explicit ContainerEqMatcher(const Container& rhs) : rhs_(View::Copy(rhs)) { 1869b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // Makes sure the user doesn't instantiate this class template 1870b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // with a const or reference type. 1871b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan testing::StaticAssertTypeEq<Container, 1872b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(Container))>(); 1873b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } 1874b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 18756a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan void DescribeTo(::std::ostream* os) const { 18766a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan *os << "equals "; 1877b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan UniversalPrinter<StlContainer>::Print(rhs_, os); 18786a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan } 18796a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan void DescribeNegationTo(::std::ostream* os) const { 18806a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan *os << "does not equal "; 1881b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan UniversalPrinter<StlContainer>::Print(rhs_, os); 18826a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan } 18836a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan 1884b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan template <typename LhsContainer> 1885e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan bool MatchAndExplain(const LhsContainer& lhs, 1886e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan MatchResultListener* listener) const { 1887b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // GMOCK_REMOVE_CONST_() is needed to work around an MSVC 8.0 bug 1888b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // that causes LhsContainer to be a const type sometimes. 1889b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef internal::StlContainerView<GMOCK_REMOVE_CONST_(LhsContainer)> 1890b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan LhsView; 1891b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef typename LhsView::type LhsStlContainer; 1892b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StlContainerReference lhs_stl_container = LhsView::ConstReference(lhs); 1893e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan if (lhs_stl_container == rhs_) 1894e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan return true; 1895b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 1896e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan ::std::ostream* const os = listener->stream(); 1897e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan if (os != NULL) { 1898e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan // Something is different. Check for missing values first. 1899e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan bool printed_header = false; 1900e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan for (typename LhsStlContainer::const_iterator it = 1901e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan lhs_stl_container.begin(); 1902e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan it != lhs_stl_container.end(); ++it) { 1903e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan if (internal::ArrayAwareFind(rhs_.begin(), rhs_.end(), *it) == 1904e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan rhs_.end()) { 1905e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan if (printed_header) { 1906e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan *os << ", "; 1907e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan } else { 1908e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan *os << "Only in actual: "; 1909e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan printed_header = true; 1910e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan } 19116953a725fc2151eff18078f8315d92811cd4d90ezhanyong.wan UniversalPrinter<typename LhsStlContainer::value_type>:: 19126953a725fc2151eff18078f8315d92811cd4d90ezhanyong.wan Print(*it, os); 19136a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan } 19146a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan } 19156a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan 1916e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan // Now check for extra values. 1917e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan bool printed_header2 = false; 1918e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan for (typename StlContainer::const_iterator it = rhs_.begin(); 1919e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan it != rhs_.end(); ++it) { 1920e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan if (internal::ArrayAwareFind( 1921e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan lhs_stl_container.begin(), lhs_stl_container.end(), *it) == 1922e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan lhs_stl_container.end()) { 1923e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan if (printed_header2) { 1924e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan *os << ", "; 1925e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan } else { 1926e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan *os << (printed_header ? "; not" : "Not") << " in actual: "; 1927e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan printed_header2 = true; 1928e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan } 1929e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan UniversalPrinter<typename StlContainer::value_type>::Print(*it, os); 19306a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan } 19316a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan } 19326a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan } 1933e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan 1934e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan return false; 19356a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan } 193632de5f53763125925e078498250f7e73a88de9edzhanyong.wan 19376a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan private: 1938b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const StlContainer rhs_; 193932de5f53763125925e078498250f7e73a88de9edzhanyong.wan 194032de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(ContainerEqMatcher); 19416a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan}; 19426a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan 1943b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Implements Contains(element_matcher) for the given argument type Container. 1944b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename Container> 1945b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanclass ContainsMatcherImpl : public MatcherInterface<Container> { 1946b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan public: 1947b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(Container)) RawContainer; 1948b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef StlContainerView<RawContainer> View; 1949b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef typename View::type StlContainer; 1950b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef typename View::const_reference StlContainerReference; 1951b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef typename StlContainer::value_type Element; 1952b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 1953b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan template <typename InnerMatcher> 1954b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan explicit ContainsMatcherImpl(InnerMatcher inner_matcher) 1955b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan : inner_matcher_( 1956b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan testing::SafeMatcherCast<const Element&>(inner_matcher)) {} 1957b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 1958b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // Describes what this matcher does. 1959b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan virtual void DescribeTo(::std::ostream* os) const { 1960b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan *os << "contains at least one element that "; 1961b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan inner_matcher_.DescribeTo(os); 1962b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } 1963b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 1964b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // Describes what the negation of this matcher does. 1965b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan virtual void DescribeNegationTo(::std::ostream* os) const { 1966b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan *os << "doesn't contain any element that "; 1967b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan inner_matcher_.DescribeTo(os); 1968b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } 1969b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 1970821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual bool MatchAndExplain(Container container, 1971821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan MatchResultListener* listener) const { 1972b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StlContainerReference stl_container = View::ConstReference(container); 1973821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan size_t i = 0; 1974821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan for (typename StlContainer::const_iterator it = stl_container.begin(); 1975821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan it != stl_container.end(); ++it, ++i) { 1976b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan if (inner_matcher_.Matches(*it)) { 1977821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << "element " << i << " matches"; 1978821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return true; 1979b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } 1980b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } 1981821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return false; 1982b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } 1983b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 1984b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan private: 1985b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const Matcher<const Element&> inner_matcher_; 198632de5f53763125925e078498250f7e73a88de9edzhanyong.wan 198732de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(ContainsMatcherImpl); 1988b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}; 1989b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 1990b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Implements polymorphic Contains(element_matcher). 1991b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename M> 1992b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanclass ContainsMatcher { 1993b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan public: 1994b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan explicit ContainsMatcher(M m) : inner_matcher_(m) {} 1995b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 1996b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan template <typename Container> 1997b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan operator Matcher<Container>() const { 1998b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan return MakeMatcher(new ContainsMatcherImpl<Container>(inner_matcher_)); 1999b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } 2000b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 2001b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan private: 2002b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const M inner_matcher_; 200332de5f53763125925e078498250f7e73a88de9edzhanyong.wan 200432de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(ContainsMatcher); 2005b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}; 2006b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 2007b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// Implements Key(inner_matcher) for the given argument pair type. 2008b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// Key(inner_matcher) matches an std::pair whose 'first' field matches 2009b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// inner_matcher. For example, Contains(Key(Ge(5))) can be used to match an 2010b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// std::map that contains at least one element whose key is >= 5. 2011b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wantemplate <typename PairType> 2012b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wanclass KeyMatcherImpl : public MatcherInterface<PairType> { 2013b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan public: 2014b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(PairType)) RawPairType; 2015b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan typedef typename RawPairType::first_type KeyType; 2016b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan 2017b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan template <typename InnerMatcher> 2018b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan explicit KeyMatcherImpl(InnerMatcher inner_matcher) 2019b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan : inner_matcher_( 2020b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan testing::SafeMatcherCast<const KeyType&>(inner_matcher)) { 2021b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan } 2022b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan 2023b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan // Returns true iff 'key_value.first' (the key) matches the inner matcher. 2024821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual bool MatchAndExplain(PairType key_value, 2025821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan MatchResultListener* listener) const { 2026821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return inner_matcher_.MatchAndExplain(key_value.first, listener); 2027b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan } 2028b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan 2029b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan // Describes what this matcher does. 2030b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan virtual void DescribeTo(::std::ostream* os) const { 2031b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan *os << "has a key that "; 2032b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan inner_matcher_.DescribeTo(os); 2033b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan } 2034b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan 2035b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan // Describes what the negation of this matcher does. 2036b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan virtual void DescribeNegationTo(::std::ostream* os) const { 2037b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan *os << "doesn't have a key that "; 2038b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan inner_matcher_.DescribeTo(os); 2039b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan } 2040b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan 2041b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan private: 2042b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan const Matcher<const KeyType&> inner_matcher_; 204332de5f53763125925e078498250f7e73a88de9edzhanyong.wan 204432de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(KeyMatcherImpl); 2045b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan}; 2046b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan 2047b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// Implements polymorphic Key(matcher_for_key). 2048b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wantemplate <typename M> 2049b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wanclass KeyMatcher { 2050b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan public: 2051b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan explicit KeyMatcher(M m) : matcher_for_key_(m) {} 2052b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan 2053b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan template <typename PairType> 2054b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan operator Matcher<PairType>() const { 2055b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan return MakeMatcher(new KeyMatcherImpl<PairType>(matcher_for_key_)); 2056b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan } 2057b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan 2058b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan private: 2059b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan const M matcher_for_key_; 206032de5f53763125925e078498250f7e73a88de9edzhanyong.wan 206132de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(KeyMatcher); 2062b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan}; 2063b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan 2064f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// Implements Pair(first_matcher, second_matcher) for the given argument pair 2065f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// type with its two matchers. See Pair() function below. 2066f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wantemplate <typename PairType> 2067f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wanclass PairMatcherImpl : public MatcherInterface<PairType> { 2068f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan public: 2069f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(PairType)) RawPairType; 2070f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan typedef typename RawPairType::first_type FirstType; 2071f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan typedef typename RawPairType::second_type SecondType; 2072f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan 2073f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan template <typename FirstMatcher, typename SecondMatcher> 2074f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan PairMatcherImpl(FirstMatcher first_matcher, SecondMatcher second_matcher) 2075f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan : first_matcher_( 2076f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan testing::SafeMatcherCast<const FirstType&>(first_matcher)), 2077f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan second_matcher_( 2078f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan testing::SafeMatcherCast<const SecondType&>(second_matcher)) { 2079f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan } 2080f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan 2081f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan // Describes what this matcher does. 2082f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan virtual void DescribeTo(::std::ostream* os) const { 2083f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan *os << "has a first field that "; 2084f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan first_matcher_.DescribeTo(os); 2085f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan *os << ", and has a second field that "; 2086f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan second_matcher_.DescribeTo(os); 2087f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan } 2088f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan 2089f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan // Describes what the negation of this matcher does. 2090f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan virtual void DescribeNegationTo(::std::ostream* os) const { 2091f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan *os << "has a first field that "; 2092f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan first_matcher_.DescribeNegationTo(os); 2093f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan *os << ", or has a second field that "; 2094f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan second_matcher_.DescribeNegationTo(os); 2095f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan } 2096f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan 2097821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // Returns true iff 'a_pair.first' matches first_matcher and 'a_pair.second' 2098821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // matches second_matcher. 2099821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual bool MatchAndExplain(PairType a_pair, 2100821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan MatchResultListener* listener) const { 2101821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan StringMatchResultListener listener1; 2102821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan const bool match1 = first_matcher_.MatchAndExplain(a_pair.first, 2103821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan &listener1); 2104821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan internal::string s1 = listener1.str(); 2105f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan if (s1 != "") { 2106821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan s1 = "the first field " + s1; 2107821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan } 2108821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (!match1) { 2109821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << s1; 2110821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return false; 2111f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan } 2112f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan 2113821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan StringMatchResultListener listener2; 2114821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan const bool match2 = second_matcher_.MatchAndExplain(a_pair.second, 2115821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan &listener2); 2116821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan internal::string s2 = listener2.str(); 2117f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan if (s2 != "") { 2118821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan s2 = "the second field " + s2; 2119821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan } 2120821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (!match2) { 2121821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << s2; 2122821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return false; 2123f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan } 2124f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan 2125821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << s1; 2126f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan if (s1 != "" && s2 != "") { 2127821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << ", and "; 2128f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan } 2129821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << s2; 2130821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return true; 2131f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan } 2132f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan 2133f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan private: 2134f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan const Matcher<const FirstType&> first_matcher_; 2135f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan const Matcher<const SecondType&> second_matcher_; 213632de5f53763125925e078498250f7e73a88de9edzhanyong.wan 213732de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(PairMatcherImpl); 2138f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan}; 2139f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan 2140f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// Implements polymorphic Pair(first_matcher, second_matcher). 2141f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wantemplate <typename FirstMatcher, typename SecondMatcher> 2142f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wanclass PairMatcher { 2143f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan public: 2144f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan PairMatcher(FirstMatcher first_matcher, SecondMatcher second_matcher) 2145f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan : first_matcher_(first_matcher), second_matcher_(second_matcher) {} 2146f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan 2147f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan template <typename PairType> 2148f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan operator Matcher<PairType> () const { 2149f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan return MakeMatcher( 2150f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan new PairMatcherImpl<PairType>( 2151f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan first_matcher_, second_matcher_)); 2152f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan } 2153f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan 2154f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan private: 2155f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan const FirstMatcher first_matcher_; 2156f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan const SecondMatcher second_matcher_; 215732de5f53763125925e078498250f7e73a88de9edzhanyong.wan 215832de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(PairMatcher); 2159f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan}; 2160f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan 21611afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// Implements ElementsAre() and ElementsAreArray(). 21621afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wantemplate <typename Container> 21631afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wanclass ElementsAreMatcherImpl : public MatcherInterface<Container> { 21641afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan public: 21651afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(Container)) RawContainer; 21661afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan typedef internal::StlContainerView<RawContainer> View; 21671afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan typedef typename View::type StlContainer; 21681afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan typedef typename View::const_reference StlContainerReference; 21691afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan typedef typename StlContainer::value_type Element; 21701afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 21711afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan // Constructs the matcher from a sequence of element values or 21721afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan // element matchers. 21731afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan template <typename InputIter> 217432de5f53763125925e078498250f7e73a88de9edzhanyong.wan ElementsAreMatcherImpl(InputIter first, size_t a_count) { 217532de5f53763125925e078498250f7e73a88de9edzhanyong.wan matchers_.reserve(a_count); 21761afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan InputIter it = first; 217732de5f53763125925e078498250f7e73a88de9edzhanyong.wan for (size_t i = 0; i != a_count; ++i, ++it) { 21781afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan matchers_.push_back(MatcherCast<const Element&>(*it)); 21791afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 21801afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 21811afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 21821afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan // Describes what this matcher does. 21831afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan virtual void DescribeTo(::std::ostream* os) const { 21841afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan if (count() == 0) { 21851afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan *os << "is empty"; 21861afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } else if (count() == 1) { 21871afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan *os << "has 1 element that "; 21881afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan matchers_[0].DescribeTo(os); 21891afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } else { 21901afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan *os << "has " << Elements(count()) << " where\n"; 21911afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan for (size_t i = 0; i != count(); ++i) { 21921afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan *os << "element " << i << " "; 21931afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan matchers_[i].DescribeTo(os); 21941afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan if (i + 1 < count()) { 21951afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan *os << ",\n"; 21961afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 21971afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 21981afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 21991afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 22001afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 22011afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan // Describes what the negation of this matcher does. 22021afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan virtual void DescribeNegationTo(::std::ostream* os) const { 22031afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan if (count() == 0) { 22041afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan *os << "is not empty"; 22051afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan return; 22061afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 22071afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 22081afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan *os << "does not have " << Elements(count()) << ", or\n"; 22091afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan for (size_t i = 0; i != count(); ++i) { 22101afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan *os << "element " << i << " "; 22111afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan matchers_[i].DescribeNegationTo(os); 22121afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan if (i + 1 < count()) { 22131afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan *os << ", or\n"; 22141afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 22151afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 22161afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 22171afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 2218821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual bool MatchAndExplain(Container container, 2219821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan MatchResultListener* listener) const { 22201afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan StlContainerReference stl_container = View::ConstReference(container); 2221821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan const size_t actual_count = stl_container.size(); 2222821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (actual_count != count()) { 2223821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // The element count doesn't match. If the container is empty, 2224821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // there's no need to explain anything as Google Mock already 2225821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // prints the empty container. Otherwise we just need to show 2226821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // how many elements there actually are. 2227821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (actual_count != 0) { 2228821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << "has " << Elements(actual_count); 22291afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 2230821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return false; 2231821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan } 22321afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 2233821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan typename StlContainer::const_iterator it = stl_container.begin(); 2234821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // explanations[i] is the explanation of the element at index i. 2235821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan std::vector<internal::string> explanations(count()); 2236821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan for (size_t i = 0; i != count(); ++it, ++i) { 2237821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan StringMatchResultListener s; 2238821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (matchers_[i].MatchAndExplain(*it, &s)) { 2239821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan explanations[i] = s.str(); 2240821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan } else { 2241821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // The container has the right size but the i-th element 2242821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // doesn't match its expectation. 2243821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << "element " << i << " doesn't match"; 2244821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 2245821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan StreamInParensAsNeeded(s.str(), listener->stream()); 2246821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return false; 2247821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan } 2248821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan } 22491afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 2250821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // Every element matches its expectation. We need to explain why 2251821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // (the obvious ones can be skipped). 22521afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 2253821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan bool reason_printed = false; 2254821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan for (size_t i = 0; i != count(); ++i) { 2255821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan const internal::string& s = explanations[i]; 2256821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (!s.empty()) { 2257821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (reason_printed) { 2258821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << ",\n"; 22591afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 2260821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << "element " << i << " " << s; 2261821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan reason_printed = true; 22621afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 22631afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 2264821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 2265821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return true; 22661afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 22671afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 22681afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan private: 22691afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan static Message Elements(size_t count) { 22701afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan return Message() << count << (count == 1 ? " element" : " elements"); 22711afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 22721afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 22731afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan size_t count() const { return matchers_.size(); } 22741afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan std::vector<Matcher<const Element&> > matchers_; 227532de5f53763125925e078498250f7e73a88de9edzhanyong.wan 227632de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(ElementsAreMatcherImpl); 22771afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan}; 22781afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 22791afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// Implements ElementsAre() of 0 arguments. 22801afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wanclass ElementsAreMatcher0 { 22811afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan public: 22821afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan ElementsAreMatcher0() {} 22831afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 22841afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan template <typename Container> 22851afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan operator Matcher<Container>() const { 22861afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(Container)) 22871afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan RawContainer; 22881afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan typedef typename internal::StlContainerView<RawContainer>::type::value_type 22891afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan Element; 22901afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 22911afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan const Matcher<const Element&>* const matchers = NULL; 22921afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan return MakeMatcher(new ElementsAreMatcherImpl<Container>(matchers, 0)); 22931afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 22941afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan}; 22951afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 22961afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// Implements ElementsAreArray(). 22971afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wantemplate <typename T> 22981afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wanclass ElementsAreArrayMatcher { 22991afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan public: 23001afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan ElementsAreArrayMatcher(const T* first, size_t count) : 23011afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan first_(first), count_(count) {} 23021afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 23031afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan template <typename Container> 23041afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan operator Matcher<Container>() const { 23051afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(Container)) 23061afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan RawContainer; 23071afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan typedef typename internal::StlContainerView<RawContainer>::type::value_type 23081afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan Element; 23091afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 23101afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan return MakeMatcher(new ElementsAreMatcherImpl<Container>(first_, count_)); 23111afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 23121afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 23131afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan private: 23141afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan const T* const first_; 23151afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan const size_t count_; 231632de5f53763125925e078498250f7e73a88de9edzhanyong.wan 231732de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(ElementsAreArrayMatcher); 23181afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan}; 23191afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 23201afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// Constants denoting interpolations in a matcher description string. 23211afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wanconst int kTupleInterpolation = -1; // "%(*)s" 23221afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wanconst int kPercentInterpolation = -2; // "%%" 23231afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wanconst int kInvalidInterpolation = -3; // "%" followed by invalid text 23241afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 23251afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// Records the location and content of an interpolation. 23261afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wanstruct Interpolation { 23271afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan Interpolation(const char* start, const char* end, int param) 23281afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan : start_pos(start), end_pos(end), param_index(param) {} 23291afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 23301afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan // Points to the start of the interpolation (the '%' character). 23311afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan const char* start_pos; 23321afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan // Points to the first character after the interpolation. 23331afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan const char* end_pos; 23341afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan // 0-based index of the interpolated matcher parameter; 23351afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan // kTupleInterpolation for "%(*)s"; kPercentInterpolation for "%%". 23361afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan int param_index; 23371afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan}; 23381afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 23391afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wantypedef ::std::vector<Interpolation> Interpolations; 23401afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 23411afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// Parses a matcher description string and returns a vector of 23421afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// interpolations that appear in the string; generates non-fatal 23431afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// failures iff 'description' is an invalid matcher description. 23441afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// 'param_names' is a NULL-terminated array of parameter names in the 23451afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// order they appear in the MATCHER_P*() parameter list. 23461afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wanInterpolations ValidateMatcherDescription( 23471afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan const char* param_names[], const char* description); 23481afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 23491afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// Returns the actual matcher description, given the matcher name, 23501afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// user-supplied description template string, interpolations in the 23511afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// string, and the printed values of the matcher parameters. 23521afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wanstring FormatMatcherDescription( 23531afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan const char* matcher_name, const char* description, 23541afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan const Interpolations& interp, const Strings& param_values); 23551afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 2356e35fdd936d133bf8a48de140a3c666897588a05shiqian} // namespace internal 2357e35fdd936d133bf8a48de140a3c666897588a05shiqian 2358e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements MatcherCast(). 2359e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T, typename M> 2360e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<T> MatcherCast(M matcher) { 2361e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::MatcherCastImpl<T, M>::Cast(matcher); 2362e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2363e35fdd936d133bf8a48de140a3c666897588a05shiqian 2364e35fdd936d133bf8a48de140a3c666897588a05shiqian// _ is a matcher that matches anything of any type. 2365e35fdd936d133bf8a48de140a3c666897588a05shiqian// 2366e35fdd936d133bf8a48de140a3c666897588a05shiqian// This definition is fine as: 2367e35fdd936d133bf8a48de140a3c666897588a05shiqian// 2368e35fdd936d133bf8a48de140a3c666897588a05shiqian// 1. The C++ standard permits using the name _ in a namespace that 2369e35fdd936d133bf8a48de140a3c666897588a05shiqian// is not the global namespace or ::std. 2370e35fdd936d133bf8a48de140a3c666897588a05shiqian// 2. The AnythingMatcher class has no data member or constructor, 2371e35fdd936d133bf8a48de140a3c666897588a05shiqian// so it's OK to create global variables of this type. 2372e35fdd936d133bf8a48de140a3c666897588a05shiqian// 3. c-style has approved of using _ in this case. 2373e35fdd936d133bf8a48de140a3c666897588a05shiqianconst internal::AnythingMatcher _ = {}; 2374e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any value of the given type T. 2375e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 2376e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<T> A() { return MakeMatcher(new internal::AnyMatcherImpl<T>()); } 2377e35fdd936d133bf8a48de140a3c666897588a05shiqian 2378e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any value of the given type T. 2379e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 2380e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<T> An() { return A<T>(); } 2381e35fdd936d133bf8a48de140a3c666897588a05shiqian 2382e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything equal to x. 2383e35fdd936d133bf8a48de140a3c666897588a05shiqian// Note: if the parameter of Eq() were declared as const T&, Eq("foo") 2384e35fdd936d133bf8a48de140a3c666897588a05shiqian// wouldn't compile. 2385e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 2386e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::EqMatcher<T> Eq(T x) { return internal::EqMatcher<T>(x); } 2387e35fdd936d133bf8a48de140a3c666897588a05shiqian 2388e35fdd936d133bf8a48de140a3c666897588a05shiqian// Constructs a Matcher<T> from a 'value' of type T. The constructed 2389e35fdd936d133bf8a48de140a3c666897588a05shiqian// matcher matches any value that's equal to 'value'. 2390e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 2391e35fdd936d133bf8a48de140a3c666897588a05shiqianMatcher<T>::Matcher(T value) { *this = Eq(value); } 2392e35fdd936d133bf8a48de140a3c666897588a05shiqian 2393e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a monomorphic matcher that matches anything with type Lhs 2394e35fdd936d133bf8a48de140a3c666897588a05shiqian// and equal to rhs. A user may need to use this instead of Eq(...) 2395e35fdd936d133bf8a48de140a3c666897588a05shiqian// in order to resolve an overloading ambiguity. 2396e35fdd936d133bf8a48de140a3c666897588a05shiqian// 2397e35fdd936d133bf8a48de140a3c666897588a05shiqian// TypedEq<T>(x) is just a convenient short-hand for Matcher<T>(Eq(x)) 2398e35fdd936d133bf8a48de140a3c666897588a05shiqian// or Matcher<T>(x), but more readable than the latter. 2399e35fdd936d133bf8a48de140a3c666897588a05shiqian// 2400e35fdd936d133bf8a48de140a3c666897588a05shiqian// We could define similar monomorphic matchers for other comparison 2401e35fdd936d133bf8a48de140a3c666897588a05shiqian// operations (e.g. TypedLt, TypedGe, and etc), but decided not to do 2402e35fdd936d133bf8a48de140a3c666897588a05shiqian// it yet as those are used much less than Eq() in practice. A user 2403e35fdd936d133bf8a48de140a3c666897588a05shiqian// can always write Matcher<T>(Lt(5)) to be explicit about the type, 2404e35fdd936d133bf8a48de140a3c666897588a05shiqian// for example. 2405e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Lhs, typename Rhs> 2406e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<Lhs> TypedEq(const Rhs& rhs) { return Eq(rhs); } 2407e35fdd936d133bf8a48de140a3c666897588a05shiqian 2408e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything >= x. 2409e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs> 2410e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::GeMatcher<Rhs> Ge(Rhs x) { 2411e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::GeMatcher<Rhs>(x); 2412e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2413e35fdd936d133bf8a48de140a3c666897588a05shiqian 2414e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything > x. 2415e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs> 2416e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::GtMatcher<Rhs> Gt(Rhs x) { 2417e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::GtMatcher<Rhs>(x); 2418e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2419e35fdd936d133bf8a48de140a3c666897588a05shiqian 2420e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything <= x. 2421e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs> 2422e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::LeMatcher<Rhs> Le(Rhs x) { 2423e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::LeMatcher<Rhs>(x); 2424e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2425e35fdd936d133bf8a48de140a3c666897588a05shiqian 2426e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything < x. 2427e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs> 2428e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::LtMatcher<Rhs> Lt(Rhs x) { 2429e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::LtMatcher<Rhs>(x); 2430e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2431e35fdd936d133bf8a48de140a3c666897588a05shiqian 2432e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything != x. 2433e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs> 2434e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::NeMatcher<Rhs> Ne(Rhs x) { 2435e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::NeMatcher<Rhs>(x); 2436e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2437e35fdd936d133bf8a48de140a3c666897588a05shiqian 24382d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan// Creates a polymorphic matcher that matches any NULL pointer. 24392d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.waninline PolymorphicMatcher<internal::IsNullMatcher > IsNull() { 24402d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan return MakePolymorphicMatcher(internal::IsNullMatcher()); 24412d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan} 24422d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan 2443e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches any non-NULL pointer. 2444e35fdd936d133bf8a48de140a3c666897588a05shiqian// This is convenient as Not(NULL) doesn't compile (the compiler 2445e35fdd936d133bf8a48de140a3c666897588a05shiqian// thinks that that expression is comparing a pointer with an integer). 2446e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::NotNullMatcher > NotNull() { 2447e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::NotNullMatcher()); 2448e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2449e35fdd936d133bf8a48de140a3c666897588a05shiqian 2450e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches any argument that 2451e35fdd936d133bf8a48de140a3c666897588a05shiqian// references variable x. 2452e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 2453e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::RefMatcher<T&> Ref(T& x) { // NOLINT 2454e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::RefMatcher<T&>(x); 2455e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2456e35fdd936d133bf8a48de140a3c666897588a05shiqian 2457e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any double argument approximately 2458e35fdd936d133bf8a48de140a3c666897588a05shiqian// equal to rhs, where two NANs are considered unequal. 2459e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::FloatingEqMatcher<double> DoubleEq(double rhs) { 2460e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::FloatingEqMatcher<double>(rhs, false); 2461e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2462e35fdd936d133bf8a48de140a3c666897588a05shiqian 2463e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any double argument approximately 2464e35fdd936d133bf8a48de140a3c666897588a05shiqian// equal to rhs, including NaN values when rhs is NaN. 2465e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::FloatingEqMatcher<double> NanSensitiveDoubleEq(double rhs) { 2466e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::FloatingEqMatcher<double>(rhs, true); 2467e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2468e35fdd936d133bf8a48de140a3c666897588a05shiqian 2469e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any float argument approximately 2470e35fdd936d133bf8a48de140a3c666897588a05shiqian// equal to rhs, where two NANs are considered unequal. 2471e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::FloatingEqMatcher<float> FloatEq(float rhs) { 2472e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::FloatingEqMatcher<float>(rhs, false); 2473e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2474e35fdd936d133bf8a48de140a3c666897588a05shiqian 2475e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any double argument approximately 2476e35fdd936d133bf8a48de140a3c666897588a05shiqian// equal to rhs, including NaN values when rhs is NaN. 2477e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::FloatingEqMatcher<float> NanSensitiveFloatEq(float rhs) { 2478e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::FloatingEqMatcher<float>(rhs, true); 2479e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2480e35fdd936d133bf8a48de140a3c666897588a05shiqian 2481e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches a pointer (raw or smart) that points 2482e35fdd936d133bf8a48de140a3c666897588a05shiqian// to a value that matches inner_matcher. 2483e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename InnerMatcher> 2484e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::PointeeMatcher<InnerMatcher> Pointee( 2485e35fdd936d133bf8a48de140a3c666897588a05shiqian const InnerMatcher& inner_matcher) { 2486e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::PointeeMatcher<InnerMatcher>(inner_matcher); 2487e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2488e35fdd936d133bf8a48de140a3c666897588a05shiqian 2489e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches an object whose given field matches 2490e35fdd936d133bf8a48de140a3c666897588a05shiqian// 'matcher'. For example, 2491e35fdd936d133bf8a48de140a3c666897588a05shiqian// Field(&Foo::number, Ge(5)) 2492e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a Foo object x iff x.number >= 5. 2493e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Class, typename FieldType, typename FieldMatcher> 2494e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher< 2495e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::FieldMatcher<Class, FieldType> > Field( 2496e35fdd936d133bf8a48de140a3c666897588a05shiqian FieldType Class::*field, const FieldMatcher& matcher) { 2497e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher( 2498e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::FieldMatcher<Class, FieldType>( 2499e35fdd936d133bf8a48de140a3c666897588a05shiqian field, MatcherCast<const FieldType&>(matcher))); 2500e35fdd936d133bf8a48de140a3c666897588a05shiqian // The call to MatcherCast() is required for supporting inner 2501e35fdd936d133bf8a48de140a3c666897588a05shiqian // matchers of compatible types. For example, it allows 2502e35fdd936d133bf8a48de140a3c666897588a05shiqian // Field(&Foo::bar, m) 2503e35fdd936d133bf8a48de140a3c666897588a05shiqian // to compile where bar is an int32 and m is a matcher for int64. 2504e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2505e35fdd936d133bf8a48de140a3c666897588a05shiqian 2506e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches an object whose given property 2507e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches 'matcher'. For example, 2508e35fdd936d133bf8a48de140a3c666897588a05shiqian// Property(&Foo::str, StartsWith("hi")) 2509e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a Foo object x iff x.str() starts with "hi". 2510e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Class, typename PropertyType, typename PropertyMatcher> 2511e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher< 2512e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::PropertyMatcher<Class, PropertyType> > Property( 2513e35fdd936d133bf8a48de140a3c666897588a05shiqian PropertyType (Class::*property)() const, const PropertyMatcher& matcher) { 2514e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher( 2515e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::PropertyMatcher<Class, PropertyType>( 2516e35fdd936d133bf8a48de140a3c666897588a05shiqian property, 2517e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan MatcherCast<GMOCK_REFERENCE_TO_CONST_(PropertyType)>(matcher))); 2518e35fdd936d133bf8a48de140a3c666897588a05shiqian // The call to MatcherCast() is required for supporting inner 2519e35fdd936d133bf8a48de140a3c666897588a05shiqian // matchers of compatible types. For example, it allows 2520e35fdd936d133bf8a48de140a3c666897588a05shiqian // Property(&Foo::bar, m) 2521e35fdd936d133bf8a48de140a3c666897588a05shiqian // to compile where bar() returns an int32 and m is a matcher for int64. 2522e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2523e35fdd936d133bf8a48de140a3c666897588a05shiqian 2524e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches an object iff the result of applying 2525e35fdd936d133bf8a48de140a3c666897588a05shiqian// a callable to x matches 'matcher'. 2526e35fdd936d133bf8a48de140a3c666897588a05shiqian// For example, 2527e35fdd936d133bf8a48de140a3c666897588a05shiqian// ResultOf(f, StartsWith("hi")) 2528e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a Foo object x iff f(x) starts with "hi". 2529e35fdd936d133bf8a48de140a3c666897588a05shiqian// callable parameter can be a function, function pointer, or a functor. 2530e35fdd936d133bf8a48de140a3c666897588a05shiqian// Callable has to satisfy the following conditions: 2531e35fdd936d133bf8a48de140a3c666897588a05shiqian// * It is required to keep no state affecting the results of 2532e35fdd936d133bf8a48de140a3c666897588a05shiqian// the calls on it and make no assumptions about how many calls 2533e35fdd936d133bf8a48de140a3c666897588a05shiqian// will be made. Any state it keeps must be protected from the 2534e35fdd936d133bf8a48de140a3c666897588a05shiqian// concurrent access. 2535e35fdd936d133bf8a48de140a3c666897588a05shiqian// * If it is a function object, it has to define type result_type. 2536e35fdd936d133bf8a48de140a3c666897588a05shiqian// We recommend deriving your functor classes from std::unary_function. 2537e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Callable, typename ResultOfMatcher> 2538e35fdd936d133bf8a48de140a3c666897588a05shiqianinternal::ResultOfMatcher<Callable> ResultOf( 2539e35fdd936d133bf8a48de140a3c666897588a05shiqian Callable callable, const ResultOfMatcher& matcher) { 2540e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::ResultOfMatcher<Callable>( 2541e35fdd936d133bf8a48de140a3c666897588a05shiqian callable, 2542e35fdd936d133bf8a48de140a3c666897588a05shiqian MatcherCast<typename internal::CallableTraits<Callable>::ResultType>( 2543e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher)); 2544e35fdd936d133bf8a48de140a3c666897588a05shiqian // The call to MatcherCast() is required for supporting inner 2545e35fdd936d133bf8a48de140a3c666897588a05shiqian // matchers of compatible types. For example, it allows 2546e35fdd936d133bf8a48de140a3c666897588a05shiqian // ResultOf(Function, m) 2547e35fdd936d133bf8a48de140a3c666897588a05shiqian // to compile where Function() returns an int32 and m is a matcher for int64. 2548e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2549e35fdd936d133bf8a48de140a3c666897588a05shiqian 2550e35fdd936d133bf8a48de140a3c666897588a05shiqian// String matchers. 2551e35fdd936d133bf8a48de140a3c666897588a05shiqian 2552e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string equal to str. 2553e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> > 2554e35fdd936d133bf8a48de140a3c666897588a05shiqian StrEq(const internal::string& str) { 2555e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>( 2556e35fdd936d133bf8a48de140a3c666897588a05shiqian str, true, true)); 2557e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2558e35fdd936d133bf8a48de140a3c666897588a05shiqian 2559e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string not equal to str. 2560e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> > 2561e35fdd936d133bf8a48de140a3c666897588a05shiqian StrNe(const internal::string& str) { 2562e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>( 2563e35fdd936d133bf8a48de140a3c666897588a05shiqian str, false, true)); 2564e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2565e35fdd936d133bf8a48de140a3c666897588a05shiqian 2566e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string equal to str, ignoring case. 2567e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> > 2568e35fdd936d133bf8a48de140a3c666897588a05shiqian StrCaseEq(const internal::string& str) { 2569e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>( 2570e35fdd936d133bf8a48de140a3c666897588a05shiqian str, true, false)); 2571e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2572e35fdd936d133bf8a48de140a3c666897588a05shiqian 2573e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string not equal to str, ignoring case. 2574e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> > 2575e35fdd936d133bf8a48de140a3c666897588a05shiqian StrCaseNe(const internal::string& str) { 2576e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>( 2577e35fdd936d133bf8a48de140a3c666897588a05shiqian str, false, false)); 2578e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2579e35fdd936d133bf8a48de140a3c666897588a05shiqian 2580e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any string, std::string, or C string 2581e35fdd936d133bf8a48de140a3c666897588a05shiqian// that contains the given substring. 2582e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::HasSubstrMatcher<internal::string> > 2583e35fdd936d133bf8a48de140a3c666897588a05shiqian HasSubstr(const internal::string& substring) { 2584e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::HasSubstrMatcher<internal::string>( 2585e35fdd936d133bf8a48de140a3c666897588a05shiqian substring)); 2586e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2587e35fdd936d133bf8a48de140a3c666897588a05shiqian 2588e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that starts with 'prefix' (case-sensitive). 2589e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StartsWithMatcher<internal::string> > 2590e35fdd936d133bf8a48de140a3c666897588a05shiqian StartsWith(const internal::string& prefix) { 2591e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::StartsWithMatcher<internal::string>( 2592e35fdd936d133bf8a48de140a3c666897588a05shiqian prefix)); 2593e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2594e35fdd936d133bf8a48de140a3c666897588a05shiqian 2595e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that ends with 'suffix' (case-sensitive). 2596e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::EndsWithMatcher<internal::string> > 2597e35fdd936d133bf8a48de140a3c666897588a05shiqian EndsWith(const internal::string& suffix) { 2598e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::EndsWithMatcher<internal::string>( 2599e35fdd936d133bf8a48de140a3c666897588a05shiqian suffix)); 2600e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2601e35fdd936d133bf8a48de140a3c666897588a05shiqian 2602e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that fully matches regular expression 'regex'. 2603e35fdd936d133bf8a48de140a3c666897588a05shiqian// The matcher takes ownership of 'regex'. 2604e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex( 2605e35fdd936d133bf8a48de140a3c666897588a05shiqian const internal::RE* regex) { 2606e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, true)); 2607e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2608e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex( 2609e35fdd936d133bf8a48de140a3c666897588a05shiqian const internal::string& regex) { 2610e35fdd936d133bf8a48de140a3c666897588a05shiqian return MatchesRegex(new internal::RE(regex)); 2611e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2612e35fdd936d133bf8a48de140a3c666897588a05shiqian 2613e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that contains regular expression 'regex'. 2614e35fdd936d133bf8a48de140a3c666897588a05shiqian// The matcher takes ownership of 'regex'. 2615e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex( 2616e35fdd936d133bf8a48de140a3c666897588a05shiqian const internal::RE* regex) { 2617e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, false)); 2618e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2619e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex( 2620e35fdd936d133bf8a48de140a3c666897588a05shiqian const internal::string& regex) { 2621e35fdd936d133bf8a48de140a3c666897588a05shiqian return ContainsRegex(new internal::RE(regex)); 2622e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2623e35fdd936d133bf8a48de140a3c666897588a05shiqian 2624e35fdd936d133bf8a48de140a3c666897588a05shiqian#if GTEST_HAS_GLOBAL_WSTRING || GTEST_HAS_STD_WSTRING 2625e35fdd936d133bf8a48de140a3c666897588a05shiqian// Wide string matchers. 2626e35fdd936d133bf8a48de140a3c666897588a05shiqian 2627e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string equal to str. 2628e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> > 2629e35fdd936d133bf8a48de140a3c666897588a05shiqian StrEq(const internal::wstring& str) { 2630e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>( 2631e35fdd936d133bf8a48de140a3c666897588a05shiqian str, true, true)); 2632e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2633e35fdd936d133bf8a48de140a3c666897588a05shiqian 2634e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string not equal to str. 2635e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> > 2636e35fdd936d133bf8a48de140a3c666897588a05shiqian StrNe(const internal::wstring& str) { 2637e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>( 2638e35fdd936d133bf8a48de140a3c666897588a05shiqian str, false, true)); 2639e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2640e35fdd936d133bf8a48de140a3c666897588a05shiqian 2641e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string equal to str, ignoring case. 2642e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> > 2643e35fdd936d133bf8a48de140a3c666897588a05shiqian StrCaseEq(const internal::wstring& str) { 2644e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>( 2645e35fdd936d133bf8a48de140a3c666897588a05shiqian str, true, false)); 2646e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2647e35fdd936d133bf8a48de140a3c666897588a05shiqian 2648e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string not equal to str, ignoring case. 2649e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> > 2650e35fdd936d133bf8a48de140a3c666897588a05shiqian StrCaseNe(const internal::wstring& str) { 2651e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>( 2652e35fdd936d133bf8a48de140a3c666897588a05shiqian str, false, false)); 2653e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2654e35fdd936d133bf8a48de140a3c666897588a05shiqian 2655e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any wstring, std::wstring, or C wide string 2656e35fdd936d133bf8a48de140a3c666897588a05shiqian// that contains the given substring. 2657e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::HasSubstrMatcher<internal::wstring> > 2658e35fdd936d133bf8a48de140a3c666897588a05shiqian HasSubstr(const internal::wstring& substring) { 2659e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::HasSubstrMatcher<internal::wstring>( 2660e35fdd936d133bf8a48de140a3c666897588a05shiqian substring)); 2661e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2662e35fdd936d133bf8a48de140a3c666897588a05shiqian 2663e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that starts with 'prefix' (case-sensitive). 2664e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StartsWithMatcher<internal::wstring> > 2665e35fdd936d133bf8a48de140a3c666897588a05shiqian StartsWith(const internal::wstring& prefix) { 2666e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::StartsWithMatcher<internal::wstring>( 2667e35fdd936d133bf8a48de140a3c666897588a05shiqian prefix)); 2668e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2669e35fdd936d133bf8a48de140a3c666897588a05shiqian 2670e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that ends with 'suffix' (case-sensitive). 2671e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::EndsWithMatcher<internal::wstring> > 2672e35fdd936d133bf8a48de140a3c666897588a05shiqian EndsWith(const internal::wstring& suffix) { 2673e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::EndsWithMatcher<internal::wstring>( 2674e35fdd936d133bf8a48de140a3c666897588a05shiqian suffix)); 2675e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2676e35fdd936d133bf8a48de140a3c666897588a05shiqian 2677e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif // GTEST_HAS_GLOBAL_WSTRING || GTEST_HAS_STD_WSTRING 2678e35fdd936d133bf8a48de140a3c666897588a05shiqian 2679e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the 2680e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field == the second field. 2681e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Eq2Matcher Eq() { return internal::Eq2Matcher(); } 2682e35fdd936d133bf8a48de140a3c666897588a05shiqian 2683e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the 2684e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field >= the second field. 2685e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Ge2Matcher Ge() { return internal::Ge2Matcher(); } 2686e35fdd936d133bf8a48de140a3c666897588a05shiqian 2687e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the 2688e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field > the second field. 2689e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Gt2Matcher Gt() { return internal::Gt2Matcher(); } 2690e35fdd936d133bf8a48de140a3c666897588a05shiqian 2691e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the 2692e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field <= the second field. 2693e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Le2Matcher Le() { return internal::Le2Matcher(); } 2694e35fdd936d133bf8a48de140a3c666897588a05shiqian 2695e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the 2696e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field < the second field. 2697e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Lt2Matcher Lt() { return internal::Lt2Matcher(); } 2698e35fdd936d133bf8a48de140a3c666897588a05shiqian 2699e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the 2700e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field != the second field. 2701e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Ne2Matcher Ne() { return internal::Ne2Matcher(); } 2702e35fdd936d133bf8a48de140a3c666897588a05shiqian 2703e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any value of type T that m doesn't 2704e35fdd936d133bf8a48de140a3c666897588a05shiqian// match. 2705e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename InnerMatcher> 2706e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::NotMatcher<InnerMatcher> Not(InnerMatcher m) { 2707e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::NotMatcher<InnerMatcher>(m); 2708e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2709e35fdd936d133bf8a48de140a3c666897588a05shiqian 2710e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any value that matches all of the 2711e35fdd936d133bf8a48de140a3c666897588a05shiqian// given matchers. 2712e35fdd936d133bf8a48de140a3c666897588a05shiqian// 2713e35fdd936d133bf8a48de140a3c666897588a05shiqian// For now we only support up to 5 matchers. Support for more 2714e35fdd936d133bf8a48de140a3c666897588a05shiqian// matchers can be added as needed, or the user can use nested 2715e35fdd936d133bf8a48de140a3c666897588a05shiqian// AllOf()s. 2716e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2> 2717e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::BothOfMatcher<Matcher1, Matcher2> 2718e35fdd936d133bf8a48de140a3c666897588a05shiqianAllOf(Matcher1 m1, Matcher2 m2) { 2719e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::BothOfMatcher<Matcher1, Matcher2>(m1, m2); 2720e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2721e35fdd936d133bf8a48de140a3c666897588a05shiqian 2722e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2, typename Matcher3> 2723e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::BothOfMatcher<Matcher1, 2724e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::BothOfMatcher<Matcher2, Matcher3> > 2725e35fdd936d133bf8a48de140a3c666897588a05shiqianAllOf(Matcher1 m1, Matcher2 m2, Matcher3 m3) { 2726e35fdd936d133bf8a48de140a3c666897588a05shiqian return AllOf(m1, AllOf(m2, m3)); 2727e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2728e35fdd936d133bf8a48de140a3c666897588a05shiqian 2729e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2, typename Matcher3, 2730e35fdd936d133bf8a48de140a3c666897588a05shiqian typename Matcher4> 2731e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::BothOfMatcher<Matcher1, 2732e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::BothOfMatcher<Matcher2, 2733e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::BothOfMatcher<Matcher3, Matcher4> > > 2734e35fdd936d133bf8a48de140a3c666897588a05shiqianAllOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4) { 2735e35fdd936d133bf8a48de140a3c666897588a05shiqian return AllOf(m1, AllOf(m2, m3, m4)); 2736e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2737e35fdd936d133bf8a48de140a3c666897588a05shiqian 2738e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2, typename Matcher3, 2739e35fdd936d133bf8a48de140a3c666897588a05shiqian typename Matcher4, typename Matcher5> 2740e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::BothOfMatcher<Matcher1, 2741e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::BothOfMatcher<Matcher2, 2742e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::BothOfMatcher<Matcher3, 2743e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::BothOfMatcher<Matcher4, Matcher5> > > > 2744e35fdd936d133bf8a48de140a3c666897588a05shiqianAllOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4, Matcher5 m5) { 2745e35fdd936d133bf8a48de140a3c666897588a05shiqian return AllOf(m1, AllOf(m2, m3, m4, m5)); 2746e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2747e35fdd936d133bf8a48de140a3c666897588a05shiqian 2748e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any value that matches at least one 2749e35fdd936d133bf8a48de140a3c666897588a05shiqian// of the given matchers. 2750e35fdd936d133bf8a48de140a3c666897588a05shiqian// 2751e35fdd936d133bf8a48de140a3c666897588a05shiqian// For now we only support up to 5 matchers. Support for more 2752e35fdd936d133bf8a48de140a3c666897588a05shiqian// matchers can be added as needed, or the user can use nested 2753e35fdd936d133bf8a48de140a3c666897588a05shiqian// AnyOf()s. 2754e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2> 2755e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::EitherOfMatcher<Matcher1, Matcher2> 2756e35fdd936d133bf8a48de140a3c666897588a05shiqianAnyOf(Matcher1 m1, Matcher2 m2) { 2757e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::EitherOfMatcher<Matcher1, Matcher2>(m1, m2); 2758e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2759e35fdd936d133bf8a48de140a3c666897588a05shiqian 2760e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2, typename Matcher3> 2761e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::EitherOfMatcher<Matcher1, 2762e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::EitherOfMatcher<Matcher2, Matcher3> > 2763e35fdd936d133bf8a48de140a3c666897588a05shiqianAnyOf(Matcher1 m1, Matcher2 m2, Matcher3 m3) { 2764e35fdd936d133bf8a48de140a3c666897588a05shiqian return AnyOf(m1, AnyOf(m2, m3)); 2765e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2766e35fdd936d133bf8a48de140a3c666897588a05shiqian 2767e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2, typename Matcher3, 2768e35fdd936d133bf8a48de140a3c666897588a05shiqian typename Matcher4> 2769e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::EitherOfMatcher<Matcher1, 2770e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::EitherOfMatcher<Matcher2, 2771e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::EitherOfMatcher<Matcher3, Matcher4> > > 2772e35fdd936d133bf8a48de140a3c666897588a05shiqianAnyOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4) { 2773e35fdd936d133bf8a48de140a3c666897588a05shiqian return AnyOf(m1, AnyOf(m2, m3, m4)); 2774e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2775e35fdd936d133bf8a48de140a3c666897588a05shiqian 2776e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2, typename Matcher3, 2777e35fdd936d133bf8a48de140a3c666897588a05shiqian typename Matcher4, typename Matcher5> 2778e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::EitherOfMatcher<Matcher1, 2779e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::EitherOfMatcher<Matcher2, 2780e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::EitherOfMatcher<Matcher3, 2781e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::EitherOfMatcher<Matcher4, Matcher5> > > > 2782e35fdd936d133bf8a48de140a3c666897588a05shiqianAnyOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4, Matcher5 m5) { 2783e35fdd936d133bf8a48de140a3c666897588a05shiqian return AnyOf(m1, AnyOf(m2, m3, m4, m5)); 2784e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2785e35fdd936d133bf8a48de140a3c666897588a05shiqian 2786e35fdd936d133bf8a48de140a3c666897588a05shiqian// Returns a matcher that matches anything that satisfies the given 2787e35fdd936d133bf8a48de140a3c666897588a05shiqian// predicate. The predicate can be any unary function or functor 2788e35fdd936d133bf8a48de140a3c666897588a05shiqian// whose return type can be implicitly converted to bool. 2789e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Predicate> 2790e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::TrulyMatcher<Predicate> > 2791e35fdd936d133bf8a48de140a3c666897588a05shiqianTruly(Predicate pred) { 2792e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::TrulyMatcher<Predicate>(pred)); 2793e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2794e35fdd936d133bf8a48de140a3c666897588a05shiqian 27956a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// Returns a matcher that matches an equal container. 27966a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// This matcher behaves like Eq(), but in the event of mismatch lists the 27976a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// values that are included in one container but not the other. (Duplicate 27986a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// values and order differences are not explained.) 27996a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wantemplate <typename Container> 2800821133180ccdb9eb062548d964929ba3b354eb84zhanyong.waninline PolymorphicMatcher<internal::ContainerEqMatcher< // NOLINT 2801b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan GMOCK_REMOVE_CONST_(Container)> > 28026a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan ContainerEq(const Container& rhs) { 2803b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // This following line is for working around a bug in MSVC 8.0, 2804b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // which causes Container to be a const type sometimes. 2805b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef GMOCK_REMOVE_CONST_(Container) RawContainer; 2806821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return MakePolymorphicMatcher( 2807821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan internal::ContainerEqMatcher<RawContainer>(rhs)); 2808b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 2809b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 2810b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Matches an STL-style container or a native array that contains at 2811b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// least one element matching the given value or matcher. 2812b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// 2813b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Examples: 2814b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// ::std::set<int> page_ids; 2815b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// page_ids.insert(3); 2816b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// page_ids.insert(1); 2817b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// EXPECT_THAT(page_ids, Contains(1)); 2818b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// EXPECT_THAT(page_ids, Contains(Gt(2))); 2819b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// EXPECT_THAT(page_ids, Not(Contains(4))); 2820b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// 2821b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// ::std::map<int, size_t> page_lengths; 2822b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// page_lengths[1] = 100; 28234019819dbb5369b1d638503523a04aaf7eb2a5adzhanyong.wan// EXPECT_THAT(page_lengths, 28244019819dbb5369b1d638503523a04aaf7eb2a5adzhanyong.wan// Contains(::std::pair<const int, size_t>(1, 100))); 2825b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// 2826b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// const char* user_ids[] = { "joe", "mike", "tom" }; 2827b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// EXPECT_THAT(user_ids, Contains(Eq(::std::string("tom")))); 2828b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename M> 2829b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.waninline internal::ContainsMatcher<M> Contains(M matcher) { 2830b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan return internal::ContainsMatcher<M>(matcher); 28316a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan} 28326a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan 2833b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// Key(inner_matcher) matches an std::pair whose 'first' field matches 2834b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// inner_matcher. For example, Contains(Key(Ge(5))) can be used to match an 2835b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// std::map that contains at least one element whose key is >= 5. 2836b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wantemplate <typename M> 2837b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.waninline internal::KeyMatcher<M> Key(M inner_matcher) { 2838b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan return internal::KeyMatcher<M>(inner_matcher); 2839b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan} 2840b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan 2841f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// Pair(first_matcher, second_matcher) matches a std::pair whose 'first' field 2842f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// matches first_matcher and whose 'second' field matches second_matcher. For 2843f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// example, EXPECT_THAT(map_type, ElementsAre(Pair(Ge(5), "foo"))) can be used 2844f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// to match a std::map<int, string> that contains exactly one element whose key 2845f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// is >= 5 and whose value equals "foo". 2846f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wantemplate <typename FirstMatcher, typename SecondMatcher> 2847f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.waninline internal::PairMatcher<FirstMatcher, SecondMatcher> 2848f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wanPair(FirstMatcher first_matcher, SecondMatcher second_matcher) { 2849f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan return internal::PairMatcher<FirstMatcher, SecondMatcher>( 2850f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan first_matcher, second_matcher); 2851f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan} 2852f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan 2853e35fdd936d133bf8a48de140a3c666897588a05shiqian// Returns a predicate that is satisfied by anything that matches the 2854e35fdd936d133bf8a48de140a3c666897588a05shiqian// given matcher. 2855e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename M> 2856e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::MatcherAsPredicate<M> Matches(M matcher) { 2857e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::MatcherAsPredicate<M>(matcher); 2858e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2859e35fdd936d133bf8a48de140a3c666897588a05shiqian 2860b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Returns true iff the value matches the matcher. 2861b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename T, typename M> 2862b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.waninline bool Value(const T& value, M matcher) { 2863b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan return testing::Matches(matcher)(value); 2864b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 2865b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 286634b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wan// Matches the value against the given matcher and explains the match 286734b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wan// result to listener. 286834b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wantemplate <typename T, typename M> 2869a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.waninline bool ExplainMatchResult( 287034b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wan M matcher, const T& value, MatchResultListener* listener) { 287134b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wan return SafeMatcherCast<const T&>(matcher).MatchAndExplain(value, listener); 287234b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wan} 287334b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wan 2874bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan// AllArgs(m) is a synonym of m. This is useful in 2875bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan// 2876bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan// EXPECT_CALL(foo, Bar(_, _)).With(AllArgs(Eq())); 2877bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan// 2878bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan// which is easier to read than 2879bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan// 2880bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan// EXPECT_CALL(foo, Bar(_, _)).With(Eq()); 2881bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wantemplate <typename InnerMatcher> 2882bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.waninline InnerMatcher AllArgs(const InnerMatcher& matcher) { return matcher; } 2883bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan 2884e35fdd936d133bf8a48de140a3c666897588a05shiqian// These macros allow using matchers to check values in Google Test 2885e35fdd936d133bf8a48de140a3c666897588a05shiqian// tests. ASSERT_THAT(value, matcher) and EXPECT_THAT(value, matcher) 2886e35fdd936d133bf8a48de140a3c666897588a05shiqian// succeed iff the value matches the matcher. If the assertion fails, 2887e35fdd936d133bf8a48de140a3c666897588a05shiqian// the value and the description of the matcher will be printed. 2888e35fdd936d133bf8a48de140a3c666897588a05shiqian#define ASSERT_THAT(value, matcher) ASSERT_PRED_FORMAT1(\ 2889e35fdd936d133bf8a48de140a3c666897588a05shiqian ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value) 2890e35fdd936d133bf8a48de140a3c666897588a05shiqian#define EXPECT_THAT(value, matcher) EXPECT_PRED_FORMAT1(\ 2891e35fdd936d133bf8a48de140a3c666897588a05shiqian ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value) 2892e35fdd936d133bf8a48de140a3c666897588a05shiqian 2893e35fdd936d133bf8a48de140a3c666897588a05shiqian} // namespace testing 2894e35fdd936d133bf8a48de140a3c666897588a05shiqian 2895e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif // GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_ 2896