gmock-matchers.h revision c6a412397bc98f120d5e79d4a64e3972854b5af3
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> 42e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <ostream> // NOLINT 43e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <sstream> 44e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <string> 45e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <vector> 46e35fdd936d133bf8a48de140a3c666897588a05shiqian 47e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <gmock/gmock-printers.h> 48e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <gmock/internal/gmock-internal-utils.h> 49e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <gmock/internal/gmock-port.h> 50e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <gtest/gtest.h> 51e35fdd936d133bf8a48de140a3c666897588a05shiqian 52e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace testing { 53e35fdd936d133bf8a48de140a3c666897588a05shiqian 54e35fdd936d133bf8a48de140a3c666897588a05shiqian// To implement a matcher Foo for type T, define: 55e35fdd936d133bf8a48de140a3c666897588a05shiqian// 1. a class FooMatcherImpl that implements the 56e35fdd936d133bf8a48de140a3c666897588a05shiqian// MatcherInterface<T> interface, and 57e35fdd936d133bf8a48de140a3c666897588a05shiqian// 2. a factory function that creates a Matcher<T> object from a 58e35fdd936d133bf8a48de140a3c666897588a05shiqian// FooMatcherImpl*. 59e35fdd936d133bf8a48de140a3c666897588a05shiqian// 60e35fdd936d133bf8a48de140a3c666897588a05shiqian// The two-level delegation design makes it possible to allow a user 61e35fdd936d133bf8a48de140a3c666897588a05shiqian// to write "v" instead of "Eq(v)" where a Matcher is expected, which 62e35fdd936d133bf8a48de140a3c666897588a05shiqian// is impossible if we pass matchers by pointers. It also eases 63e35fdd936d133bf8a48de140a3c666897588a05shiqian// ownership management as Matcher objects can now be copied like 64e35fdd936d133bf8a48de140a3c666897588a05shiqian// plain values. 65e35fdd936d133bf8a48de140a3c666897588a05shiqian 66e35fdd936d133bf8a48de140a3c666897588a05shiqian// The implementation of a matcher. 67e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 68e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatcherInterface { 69e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 70e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual ~MatcherInterface() {} 71e35fdd936d133bf8a48de140a3c666897588a05shiqian 72e35fdd936d133bf8a48de140a3c666897588a05shiqian // Returns true iff the matcher matches x. 73e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual bool Matches(T x) const = 0; 74e35fdd936d133bf8a48de140a3c666897588a05shiqian 75e35fdd936d133bf8a48de140a3c666897588a05shiqian // Describes this matcher to an ostream. 76e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeTo(::std::ostream* os) const = 0; 77e35fdd936d133bf8a48de140a3c666897588a05shiqian 78e35fdd936d133bf8a48de140a3c666897588a05shiqian // Describes the negation of this matcher to an ostream. For 79e35fdd936d133bf8a48de140a3c666897588a05shiqian // example, if the description of this matcher is "is greater than 80e35fdd936d133bf8a48de140a3c666897588a05shiqian // 7", the negated description could be "is not greater than 7". 81e35fdd936d133bf8a48de140a3c666897588a05shiqian // You are not required to override this when implementing 82e35fdd936d133bf8a48de140a3c666897588a05shiqian // MatcherInterface, but it is highly advised so that your matcher 83e35fdd936d133bf8a48de140a3c666897588a05shiqian // can produce good error messages. 84e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeNegationTo(::std::ostream* os) const { 85e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "not ("; 86e35fdd936d133bf8a48de140a3c666897588a05shiqian DescribeTo(os); 87e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << ")"; 88e35fdd936d133bf8a48de140a3c666897588a05shiqian } 89e35fdd936d133bf8a48de140a3c666897588a05shiqian 90e35fdd936d133bf8a48de140a3c666897588a05shiqian // Explains why x matches, or doesn't match, the matcher. Override 91e35fdd936d133bf8a48de140a3c666897588a05shiqian // this to provide any additional information that helps a user 92e35fdd936d133bf8a48de140a3c666897588a05shiqian // understand the match result. 933fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.wan virtual void ExplainMatchResultTo(T /* x */, ::std::ostream* /* os */) const { 94e35fdd936d133bf8a48de140a3c666897588a05shiqian // By default, nothing more needs to be explained, as Google Mock 95e35fdd936d133bf8a48de140a3c666897588a05shiqian // has already printed the value of x when this function is 96e35fdd936d133bf8a48de140a3c666897588a05shiqian // called. 97e35fdd936d133bf8a48de140a3c666897588a05shiqian } 98e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 99e35fdd936d133bf8a48de140a3c666897588a05shiqian 100e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace internal { 101e35fdd936d133bf8a48de140a3c666897588a05shiqian 102e35fdd936d133bf8a48de140a3c666897588a05shiqian// An internal class for implementing Matcher<T>, which will derive 103e35fdd936d133bf8a48de140a3c666897588a05shiqian// from it. We put functionalities common to all Matcher<T> 104e35fdd936d133bf8a48de140a3c666897588a05shiqian// specializations here to avoid code duplication. 105e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 106e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatcherBase { 107e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 108e35fdd936d133bf8a48de140a3c666897588a05shiqian // Returns true iff this matcher matches x. 109e35fdd936d133bf8a48de140a3c666897588a05shiqian bool Matches(T x) const { return impl_->Matches(x); } 110e35fdd936d133bf8a48de140a3c666897588a05shiqian 111e35fdd936d133bf8a48de140a3c666897588a05shiqian // Describes this matcher to an ostream. 112e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeTo(::std::ostream* os) const { impl_->DescribeTo(os); } 113e35fdd936d133bf8a48de140a3c666897588a05shiqian 114e35fdd936d133bf8a48de140a3c666897588a05shiqian // Describes the negation of this matcher to an ostream. 115e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeNegationTo(::std::ostream* os) const { 116e35fdd936d133bf8a48de140a3c666897588a05shiqian impl_->DescribeNegationTo(os); 117e35fdd936d133bf8a48de140a3c666897588a05shiqian } 118e35fdd936d133bf8a48de140a3c666897588a05shiqian 119e35fdd936d133bf8a48de140a3c666897588a05shiqian // Explains why x matches, or doesn't match, the matcher. 120e35fdd936d133bf8a48de140a3c666897588a05shiqian void ExplainMatchResultTo(T x, ::std::ostream* os) const { 121e35fdd936d133bf8a48de140a3c666897588a05shiqian impl_->ExplainMatchResultTo(x, os); 122e35fdd936d133bf8a48de140a3c666897588a05shiqian } 123e35fdd936d133bf8a48de140a3c666897588a05shiqian protected: 124e35fdd936d133bf8a48de140a3c666897588a05shiqian MatcherBase() {} 125e35fdd936d133bf8a48de140a3c666897588a05shiqian 126e35fdd936d133bf8a48de140a3c666897588a05shiqian // Constructs a matcher from its implementation. 127e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit MatcherBase(const MatcherInterface<T>* impl) 128e35fdd936d133bf8a48de140a3c666897588a05shiqian : impl_(impl) {} 129e35fdd936d133bf8a48de140a3c666897588a05shiqian 130e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual ~MatcherBase() {} 131e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 132e35fdd936d133bf8a48de140a3c666897588a05shiqian // shared_ptr (util/gtl/shared_ptr.h) and linked_ptr have similar 133e35fdd936d133bf8a48de140a3c666897588a05shiqian // interfaces. The former dynamically allocates a chunk of memory 134e35fdd936d133bf8a48de140a3c666897588a05shiqian // to hold the reference count, while the latter tracks all 135e35fdd936d133bf8a48de140a3c666897588a05shiqian // references using a circular linked list without allocating 136e35fdd936d133bf8a48de140a3c666897588a05shiqian // memory. It has been observed that linked_ptr performs better in 137e35fdd936d133bf8a48de140a3c666897588a05shiqian // typical scenarios. However, shared_ptr can out-perform 138e35fdd936d133bf8a48de140a3c666897588a05shiqian // linked_ptr when there are many more uses of the copy constructor 139e35fdd936d133bf8a48de140a3c666897588a05shiqian // than the default constructor. 140e35fdd936d133bf8a48de140a3c666897588a05shiqian // 141e35fdd936d133bf8a48de140a3c666897588a05shiqian // If performance becomes a problem, we should see if using 142e35fdd936d133bf8a48de140a3c666897588a05shiqian // shared_ptr helps. 143e35fdd936d133bf8a48de140a3c666897588a05shiqian ::testing::internal::linked_ptr<const MatcherInterface<T> > impl_; 144e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 145e35fdd936d133bf8a48de140a3c666897588a05shiqian 146e35fdd936d133bf8a48de140a3c666897588a05shiqian// The default implementation of ExplainMatchResultTo() for 147e35fdd936d133bf8a48de140a3c666897588a05shiqian// polymorphic matchers. 148e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename PolymorphicMatcherImpl, typename T> 1493fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.waninline void ExplainMatchResultTo(const PolymorphicMatcherImpl& /* impl */, 1503fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.wan const T& /* x */, 1513fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.wan ::std::ostream* /* os */) { 152e35fdd936d133bf8a48de140a3c666897588a05shiqian // By default, nothing more needs to be said, as Google Mock already 153e35fdd936d133bf8a48de140a3c666897588a05shiqian // prints the value of x elsewhere. 154e35fdd936d133bf8a48de140a3c666897588a05shiqian} 155e35fdd936d133bf8a48de140a3c666897588a05shiqian 156e35fdd936d133bf8a48de140a3c666897588a05shiqian} // namespace internal 157e35fdd936d133bf8a48de140a3c666897588a05shiqian 158e35fdd936d133bf8a48de140a3c666897588a05shiqian// A Matcher<T> is a copyable and IMMUTABLE (except by assignment) 159e35fdd936d133bf8a48de140a3c666897588a05shiqian// object that can check whether a value of type T matches. The 160e35fdd936d133bf8a48de140a3c666897588a05shiqian// implementation of Matcher<T> is just a linked_ptr to const 161e35fdd936d133bf8a48de140a3c666897588a05shiqian// MatcherInterface<T>, so copying is fairly cheap. Don't inherit 162e35fdd936d133bf8a48de140a3c666897588a05shiqian// from Matcher! 163e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 164e35fdd936d133bf8a48de140a3c666897588a05shiqianclass Matcher : public internal::MatcherBase<T> { 165e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 166e35fdd936d133bf8a48de140a3c666897588a05shiqian // Constructs a null matcher. Needed for storing Matcher objects in 167e35fdd936d133bf8a48de140a3c666897588a05shiqian // STL containers. 168e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher() {} 169e35fdd936d133bf8a48de140a3c666897588a05shiqian 170e35fdd936d133bf8a48de140a3c666897588a05shiqian // Constructs a matcher from its implementation. 171e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit Matcher(const MatcherInterface<T>* impl) 172e35fdd936d133bf8a48de140a3c666897588a05shiqian : internal::MatcherBase<T>(impl) {} 173e35fdd936d133bf8a48de140a3c666897588a05shiqian 17418490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan // Implicit constructor here allows people to write 175e35fdd936d133bf8a48de140a3c666897588a05shiqian // EXPECT_CALL(foo, Bar(5)) instead of EXPECT_CALL(foo, Bar(Eq(5))) sometimes 176e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher(T value); // NOLINT 177e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 178e35fdd936d133bf8a48de140a3c666897588a05shiqian 179e35fdd936d133bf8a48de140a3c666897588a05shiqian// The following two specializations allow the user to write str 180e35fdd936d133bf8a48de140a3c666897588a05shiqian// instead of Eq(str) and "foo" instead of Eq("foo") when a string 181e35fdd936d133bf8a48de140a3c666897588a05shiqian// matcher is expected. 182e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <> 183e35fdd936d133bf8a48de140a3c666897588a05shiqianclass Matcher<const internal::string&> 184e35fdd936d133bf8a48de140a3c666897588a05shiqian : public internal::MatcherBase<const internal::string&> { 185e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 186e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher() {} 187e35fdd936d133bf8a48de140a3c666897588a05shiqian 188e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit Matcher(const MatcherInterface<const internal::string&>* impl) 189e35fdd936d133bf8a48de140a3c666897588a05shiqian : internal::MatcherBase<const internal::string&>(impl) {} 190e35fdd936d133bf8a48de140a3c666897588a05shiqian 191e35fdd936d133bf8a48de140a3c666897588a05shiqian // Allows the user to write str instead of Eq(str) sometimes, where 192e35fdd936d133bf8a48de140a3c666897588a05shiqian // str is a string object. 193e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher(const internal::string& s); // NOLINT 194e35fdd936d133bf8a48de140a3c666897588a05shiqian 195e35fdd936d133bf8a48de140a3c666897588a05shiqian // Allows the user to write "foo" instead of Eq("foo") sometimes. 196e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher(const char* s); // NOLINT 197e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 198e35fdd936d133bf8a48de140a3c666897588a05shiqian 199e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <> 200e35fdd936d133bf8a48de140a3c666897588a05shiqianclass Matcher<internal::string> 201e35fdd936d133bf8a48de140a3c666897588a05shiqian : public internal::MatcherBase<internal::string> { 202e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 203e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher() {} 204e35fdd936d133bf8a48de140a3c666897588a05shiqian 205e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit Matcher(const MatcherInterface<internal::string>* impl) 206e35fdd936d133bf8a48de140a3c666897588a05shiqian : internal::MatcherBase<internal::string>(impl) {} 207e35fdd936d133bf8a48de140a3c666897588a05shiqian 208e35fdd936d133bf8a48de140a3c666897588a05shiqian // Allows the user to write str instead of Eq(str) sometimes, where 209e35fdd936d133bf8a48de140a3c666897588a05shiqian // str is a string object. 210e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher(const internal::string& s); // NOLINT 211e35fdd936d133bf8a48de140a3c666897588a05shiqian 212e35fdd936d133bf8a48de140a3c666897588a05shiqian // Allows the user to write "foo" instead of Eq("foo") sometimes. 213e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher(const char* s); // NOLINT 214e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 215e35fdd936d133bf8a48de140a3c666897588a05shiqian 216e35fdd936d133bf8a48de140a3c666897588a05shiqian// The PolymorphicMatcher class template makes it easy to implement a 217e35fdd936d133bf8a48de140a3c666897588a05shiqian// polymorphic matcher (i.e. a matcher that can match values of more 218e35fdd936d133bf8a48de140a3c666897588a05shiqian// than one type, e.g. Eq(n) and NotNull()). 219e35fdd936d133bf8a48de140a3c666897588a05shiqian// 220e35fdd936d133bf8a48de140a3c666897588a05shiqian// To define a polymorphic matcher, a user first provides a Impl class 221e35fdd936d133bf8a48de140a3c666897588a05shiqian// that has a Matches() method, a DescribeTo() method, and a 222e35fdd936d133bf8a48de140a3c666897588a05shiqian// DescribeNegationTo() method. The Matches() method is usually a 223e35fdd936d133bf8a48de140a3c666897588a05shiqian// method template (such that it works with multiple types). Then the 224e35fdd936d133bf8a48de140a3c666897588a05shiqian// user creates the polymorphic matcher using 225e35fdd936d133bf8a48de140a3c666897588a05shiqian// MakePolymorphicMatcher(). To provide additional explanation to the 226e35fdd936d133bf8a48de140a3c666897588a05shiqian// match result, define a FREE function (or function template) 227e35fdd936d133bf8a48de140a3c666897588a05shiqian// 228e35fdd936d133bf8a48de140a3c666897588a05shiqian// void ExplainMatchResultTo(const Impl& matcher, const Value& value, 229e35fdd936d133bf8a48de140a3c666897588a05shiqian// ::std::ostream* os); 230e35fdd936d133bf8a48de140a3c666897588a05shiqian// 231e35fdd936d133bf8a48de140a3c666897588a05shiqian// in the SAME NAME SPACE where Impl is defined. See the definition 232e35fdd936d133bf8a48de140a3c666897588a05shiqian// of NotNull() for a complete example. 233e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <class Impl> 234e35fdd936d133bf8a48de140a3c666897588a05shiqianclass PolymorphicMatcher { 235e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 236e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit PolymorphicMatcher(const Impl& impl) : impl_(impl) {} 237e35fdd936d133bf8a48de140a3c666897588a05shiqian 238e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 239e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<T>() const { 240e35fdd936d133bf8a48de140a3c666897588a05shiqian return Matcher<T>(new MonomorphicImpl<T>(impl_)); 241e35fdd936d133bf8a48de140a3c666897588a05shiqian } 242e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 243e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 244e35fdd936d133bf8a48de140a3c666897588a05shiqian class MonomorphicImpl : public MatcherInterface<T> { 245e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 246e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {} 247e35fdd936d133bf8a48de140a3c666897588a05shiqian 248e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual bool Matches(T x) const { return impl_.Matches(x); } 249e35fdd936d133bf8a48de140a3c666897588a05shiqian 250e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeTo(::std::ostream* os) const { 251e35fdd936d133bf8a48de140a3c666897588a05shiqian impl_.DescribeTo(os); 252e35fdd936d133bf8a48de140a3c666897588a05shiqian } 253e35fdd936d133bf8a48de140a3c666897588a05shiqian 254e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeNegationTo(::std::ostream* os) const { 255e35fdd936d133bf8a48de140a3c666897588a05shiqian impl_.DescribeNegationTo(os); 256e35fdd936d133bf8a48de140a3c666897588a05shiqian } 257e35fdd936d133bf8a48de140a3c666897588a05shiqian 258e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void ExplainMatchResultTo(T x, ::std::ostream* os) const { 259e35fdd936d133bf8a48de140a3c666897588a05shiqian using ::testing::internal::ExplainMatchResultTo; 260e35fdd936d133bf8a48de140a3c666897588a05shiqian 261e35fdd936d133bf8a48de140a3c666897588a05shiqian // C++ uses Argument-Dependent Look-up (aka Koenig Look-up) to 262e35fdd936d133bf8a48de140a3c666897588a05shiqian // resolve the call to ExplainMatchResultTo() here. This 263e35fdd936d133bf8a48de140a3c666897588a05shiqian // means that if there's a ExplainMatchResultTo() function 264e35fdd936d133bf8a48de140a3c666897588a05shiqian // defined in the name space where class Impl is defined, it 265e35fdd936d133bf8a48de140a3c666897588a05shiqian // will be picked by the compiler as the better match. 266e35fdd936d133bf8a48de140a3c666897588a05shiqian // Otherwise the default implementation of it in 267e35fdd936d133bf8a48de140a3c666897588a05shiqian // ::testing::internal will be picked. 268e35fdd936d133bf8a48de140a3c666897588a05shiqian // 269e35fdd936d133bf8a48de140a3c666897588a05shiqian // This look-up rule lets a writer of a polymorphic matcher 270e35fdd936d133bf8a48de140a3c666897588a05shiqian // customize the behavior of ExplainMatchResultTo() when he 271e35fdd936d133bf8a48de140a3c666897588a05shiqian // cares to. Nothing needs to be done by the writer if he 272e35fdd936d133bf8a48de140a3c666897588a05shiqian // doesn't need to customize it. 273e35fdd936d133bf8a48de140a3c666897588a05shiqian ExplainMatchResultTo(impl_, x, os); 274e35fdd936d133bf8a48de140a3c666897588a05shiqian } 275e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 276e35fdd936d133bf8a48de140a3c666897588a05shiqian const Impl impl_; 277e35fdd936d133bf8a48de140a3c666897588a05shiqian }; 278e35fdd936d133bf8a48de140a3c666897588a05shiqian 279e35fdd936d133bf8a48de140a3c666897588a05shiqian const Impl impl_; 280e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 281e35fdd936d133bf8a48de140a3c666897588a05shiqian 282e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher from its implementation. This is easier to use 283e35fdd936d133bf8a48de140a3c666897588a05shiqian// than the Matcher<T> constructor as it doesn't require you to 284e35fdd936d133bf8a48de140a3c666897588a05shiqian// explicitly write the template argument, e.g. 285e35fdd936d133bf8a48de140a3c666897588a05shiqian// 286e35fdd936d133bf8a48de140a3c666897588a05shiqian// MakeMatcher(foo); 287e35fdd936d133bf8a48de140a3c666897588a05shiqian// vs 288e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matcher<const string&>(foo); 289e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 290e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<T> MakeMatcher(const MatcherInterface<T>* impl) { 291e35fdd936d133bf8a48de140a3c666897588a05shiqian return Matcher<T>(impl); 292e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 293e35fdd936d133bf8a48de140a3c666897588a05shiqian 294e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher from its implementation. This is 295e35fdd936d133bf8a48de140a3c666897588a05shiqian// easier to use than the PolymorphicMatcher<Impl> constructor as it 296e35fdd936d133bf8a48de140a3c666897588a05shiqian// doesn't require you to explicitly write the template argument, e.g. 297e35fdd936d133bf8a48de140a3c666897588a05shiqian// 298e35fdd936d133bf8a48de140a3c666897588a05shiqian// MakePolymorphicMatcher(foo); 299e35fdd936d133bf8a48de140a3c666897588a05shiqian// vs 300e35fdd936d133bf8a48de140a3c666897588a05shiqian// PolymorphicMatcher<TypeOfFoo>(foo); 301e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <class Impl> 302e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<Impl> MakePolymorphicMatcher(const Impl& impl) { 303e35fdd936d133bf8a48de140a3c666897588a05shiqian return PolymorphicMatcher<Impl>(impl); 304e35fdd936d133bf8a48de140a3c666897588a05shiqian} 305e35fdd936d133bf8a48de140a3c666897588a05shiqian 306e35fdd936d133bf8a48de140a3c666897588a05shiqian// In order to be safe and clear, casting between different matcher 307e35fdd936d133bf8a48de140a3c666897588a05shiqian// types is done explicitly via MatcherCast<T>(m), which takes a 308e35fdd936d133bf8a48de140a3c666897588a05shiqian// matcher m and returns a Matcher<T>. It compiles only when T can be 309e35fdd936d133bf8a48de140a3c666897588a05shiqian// statically converted to the argument type of m. 310e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T, typename M> 311e35fdd936d133bf8a48de140a3c666897588a05shiqianMatcher<T> MatcherCast(M m); 312e35fdd936d133bf8a48de140a3c666897588a05shiqian 31318490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// TODO(vladl@google.com): Modify the implementation to reject casting 31418490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// Matcher<int> to Matcher<double>. 31518490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// Implements SafeMatcherCast(). 31618490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// 31718490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// This overload handles polymorphic matchers only since monomorphic 31818490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// matchers are handled by the next one. 31918490653e80d484b4650d8799184fd1e021efc7bzhanyong.wantemplate <typename T, typename M> 32018490653e80d484b4650d8799184fd1e021efc7bzhanyong.waninline Matcher<T> SafeMatcherCast(M polymorphic_matcher) { 32118490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan return Matcher<T>(polymorphic_matcher); 32218490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan} 32318490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan 32418490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// This overload handles monomorphic matchers. 32518490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// 32618490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// In general, if type T can be implicitly converted to type U, we can 32718490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// safely convert a Matcher<U> to a Matcher<T> (i.e. Matcher is 32818490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// contravariant): just keep a copy of the original Matcher<U>, convert the 32918490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// argument from type T to U, and then pass it to the underlying Matcher<U>. 33018490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// The only exception is when U is a reference and T is not, as the 33118490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// underlying Matcher<U> may be interested in the argument's address, which 33218490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// is not preserved in the conversion from T to U. 33318490653e80d484b4650d8799184fd1e021efc7bzhanyong.wantemplate <typename T, typename U> 33418490653e80d484b4650d8799184fd1e021efc7bzhanyong.wanMatcher<T> SafeMatcherCast(const Matcher<U>& matcher) { 33518490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan // Enforce that T can be implicitly converted to U. 33618490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan GMOCK_COMPILE_ASSERT_((internal::ImplicitlyConvertible<T, U>::value), 33718490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan T_must_be_implicitly_convertible_to_U); 33818490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan // Enforce that we are not converting a non-reference type T to a reference 33918490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan // type U. 34018490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan GMOCK_COMPILE_ASSERT_( 34118490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan internal::is_reference<T>::value || !internal::is_reference<U>::value, 34218490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan cannot_convert_non_referentce_arg_to_reference); 34318490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan return MatcherCast<T>(matcher); 34418490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan} 34518490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan 346e35fdd936d133bf8a48de140a3c666897588a05shiqian// A<T>() returns a matcher that matches any value of type T. 347e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 348e35fdd936d133bf8a48de140a3c666897588a05shiqianMatcher<T> A(); 349e35fdd936d133bf8a48de140a3c666897588a05shiqian 350e35fdd936d133bf8a48de140a3c666897588a05shiqian// Anything inside the 'internal' namespace IS INTERNAL IMPLEMENTATION 351e35fdd936d133bf8a48de140a3c666897588a05shiqian// and MUST NOT BE USED IN USER CODE!!! 352e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace internal { 353e35fdd936d133bf8a48de140a3c666897588a05shiqian 354e35fdd936d133bf8a48de140a3c666897588a05shiqian// Appends the explanation on the result of matcher.Matches(value) to 355e35fdd936d133bf8a48de140a3c666897588a05shiqian// os iff the explanation is not empty. 356e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 357e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid ExplainMatchResultAsNeededTo(const Matcher<T>& matcher, T value, 358e35fdd936d133bf8a48de140a3c666897588a05shiqian ::std::ostream* os) { 359e35fdd936d133bf8a48de140a3c666897588a05shiqian ::std::stringstream reason; 360e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher.ExplainMatchResultTo(value, &reason); 361e35fdd936d133bf8a48de140a3c666897588a05shiqian const internal::string s = reason.str(); 362e35fdd936d133bf8a48de140a3c666897588a05shiqian if (s != "") { 363e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << " (" << s << ")"; 364e35fdd936d133bf8a48de140a3c666897588a05shiqian } 365e35fdd936d133bf8a48de140a3c666897588a05shiqian} 366e35fdd936d133bf8a48de140a3c666897588a05shiqian 367e35fdd936d133bf8a48de140a3c666897588a05shiqian// An internal helper class for doing compile-time loop on a tuple's 368e35fdd936d133bf8a48de140a3c666897588a05shiqian// fields. 369e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <size_t N> 370e35fdd936d133bf8a48de140a3c666897588a05shiqianclass TuplePrefix { 371e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 372e35fdd936d133bf8a48de140a3c666897588a05shiqian // TuplePrefix<N>::Matches(matcher_tuple, value_tuple) returns true 373e35fdd936d133bf8a48de140a3c666897588a05shiqian // iff the first N fields of matcher_tuple matches the first N 374e35fdd936d133bf8a48de140a3c666897588a05shiqian // fields of value_tuple, respectively. 375e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename MatcherTuple, typename ValueTuple> 376e35fdd936d133bf8a48de140a3c666897588a05shiqian static bool Matches(const MatcherTuple& matcher_tuple, 377e35fdd936d133bf8a48de140a3c666897588a05shiqian const ValueTuple& value_tuple) { 378e35fdd936d133bf8a48de140a3c666897588a05shiqian using ::std::tr1::get; 379e35fdd936d133bf8a48de140a3c666897588a05shiqian return TuplePrefix<N - 1>::Matches(matcher_tuple, value_tuple) 380e35fdd936d133bf8a48de140a3c666897588a05shiqian && get<N - 1>(matcher_tuple).Matches(get<N - 1>(value_tuple)); 381e35fdd936d133bf8a48de140a3c666897588a05shiqian } 382e35fdd936d133bf8a48de140a3c666897588a05shiqian 383e35fdd936d133bf8a48de140a3c666897588a05shiqian // TuplePrefix<N>::DescribeMatchFailuresTo(matchers, values, os) 384e35fdd936d133bf8a48de140a3c666897588a05shiqian // describes failures in matching the first N fields of matchers 385e35fdd936d133bf8a48de140a3c666897588a05shiqian // against the first N fields of values. If there is no failure, 386e35fdd936d133bf8a48de140a3c666897588a05shiqian // nothing will be streamed to os. 387e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename MatcherTuple, typename ValueTuple> 388e35fdd936d133bf8a48de140a3c666897588a05shiqian static void DescribeMatchFailuresTo(const MatcherTuple& matchers, 389e35fdd936d133bf8a48de140a3c666897588a05shiqian const ValueTuple& values, 390e35fdd936d133bf8a48de140a3c666897588a05shiqian ::std::ostream* os) { 391e35fdd936d133bf8a48de140a3c666897588a05shiqian using ::std::tr1::tuple_element; 392e35fdd936d133bf8a48de140a3c666897588a05shiqian using ::std::tr1::get; 393e35fdd936d133bf8a48de140a3c666897588a05shiqian 394e35fdd936d133bf8a48de140a3c666897588a05shiqian // First, describes failures in the first N - 1 fields. 395e35fdd936d133bf8a48de140a3c666897588a05shiqian TuplePrefix<N - 1>::DescribeMatchFailuresTo(matchers, values, os); 396e35fdd936d133bf8a48de140a3c666897588a05shiqian 397e35fdd936d133bf8a48de140a3c666897588a05shiqian // Then describes the failure (if any) in the (N - 1)-th (0-based) 398e35fdd936d133bf8a48de140a3c666897588a05shiqian // field. 399e35fdd936d133bf8a48de140a3c666897588a05shiqian typename tuple_element<N - 1, MatcherTuple>::type matcher = 400e35fdd936d133bf8a48de140a3c666897588a05shiqian get<N - 1>(matchers); 401e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef typename tuple_element<N - 1, ValueTuple>::type Value; 402e35fdd936d133bf8a48de140a3c666897588a05shiqian Value value = get<N - 1>(values); 403e35fdd936d133bf8a48de140a3c666897588a05shiqian if (!matcher.Matches(value)) { 404e35fdd936d133bf8a48de140a3c666897588a05shiqian // TODO(wan): include in the message the name of the parameter 405e35fdd936d133bf8a48de140a3c666897588a05shiqian // as used in MOCK_METHOD*() when possible. 406e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << " Expected arg #" << N - 1 << ": "; 407e35fdd936d133bf8a48de140a3c666897588a05shiqian get<N - 1>(matchers).DescribeTo(os); 408e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "\n Actual: "; 409e35fdd936d133bf8a48de140a3c666897588a05shiqian // We remove the reference in type Value to prevent the 410e35fdd936d133bf8a48de140a3c666897588a05shiqian // universal printer from printing the address of value, which 411e35fdd936d133bf8a48de140a3c666897588a05shiqian // isn't interesting to the user most of the time. The 412e35fdd936d133bf8a48de140a3c666897588a05shiqian // matcher's ExplainMatchResultTo() method handles the case when 413e35fdd936d133bf8a48de140a3c666897588a05shiqian // the address is interesting. 414e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan internal::UniversalPrinter<GMOCK_REMOVE_REFERENCE_(Value)>:: 415e35fdd936d133bf8a48de140a3c666897588a05shiqian Print(value, os); 416e35fdd936d133bf8a48de140a3c666897588a05shiqian ExplainMatchResultAsNeededTo<Value>(matcher, value, os); 417e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "\n"; 418e35fdd936d133bf8a48de140a3c666897588a05shiqian } 419e35fdd936d133bf8a48de140a3c666897588a05shiqian } 420e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 421e35fdd936d133bf8a48de140a3c666897588a05shiqian 422e35fdd936d133bf8a48de140a3c666897588a05shiqian// The base case. 423e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <> 424e35fdd936d133bf8a48de140a3c666897588a05shiqianclass TuplePrefix<0> { 425e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 426e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename MatcherTuple, typename ValueTuple> 4273fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.wan static bool Matches(const MatcherTuple& /* matcher_tuple */, 4283fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.wan const ValueTuple& /* value_tuple */) { 429e35fdd936d133bf8a48de140a3c666897588a05shiqian return true; 430e35fdd936d133bf8a48de140a3c666897588a05shiqian } 431e35fdd936d133bf8a48de140a3c666897588a05shiqian 432e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename MatcherTuple, typename ValueTuple> 4333fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.wan static void DescribeMatchFailuresTo(const MatcherTuple& /* matchers */, 4343fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.wan const ValueTuple& /* values */, 4353fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.wan ::std::ostream* /* os */) {} 436e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 437e35fdd936d133bf8a48de140a3c666897588a05shiqian 438e35fdd936d133bf8a48de140a3c666897588a05shiqian// TupleMatches(matcher_tuple, value_tuple) returns true iff all 439e35fdd936d133bf8a48de140a3c666897588a05shiqian// matchers in matcher_tuple match the corresponding fields in 440e35fdd936d133bf8a48de140a3c666897588a05shiqian// value_tuple. It is a compiler error if matcher_tuple and 441e35fdd936d133bf8a48de140a3c666897588a05shiqian// value_tuple have different number of fields or incompatible field 442e35fdd936d133bf8a48de140a3c666897588a05shiqian// types. 443e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename MatcherTuple, typename ValueTuple> 444e35fdd936d133bf8a48de140a3c666897588a05shiqianbool TupleMatches(const MatcherTuple& matcher_tuple, 445e35fdd936d133bf8a48de140a3c666897588a05shiqian const ValueTuple& value_tuple) { 446e35fdd936d133bf8a48de140a3c666897588a05shiqian using ::std::tr1::tuple_size; 447e35fdd936d133bf8a48de140a3c666897588a05shiqian // Makes sure that matcher_tuple and value_tuple have the same 448e35fdd936d133bf8a48de140a3c666897588a05shiqian // number of fields. 449e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan GMOCK_COMPILE_ASSERT_(tuple_size<MatcherTuple>::value == 450e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan tuple_size<ValueTuple>::value, 451e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan matcher_and_value_have_different_numbers_of_fields); 452e35fdd936d133bf8a48de140a3c666897588a05shiqian return TuplePrefix<tuple_size<ValueTuple>::value>:: 453e35fdd936d133bf8a48de140a3c666897588a05shiqian Matches(matcher_tuple, value_tuple); 454e35fdd936d133bf8a48de140a3c666897588a05shiqian} 455e35fdd936d133bf8a48de140a3c666897588a05shiqian 456e35fdd936d133bf8a48de140a3c666897588a05shiqian// Describes failures in matching matchers against values. If there 457e35fdd936d133bf8a48de140a3c666897588a05shiqian// is no failure, nothing will be streamed to os. 458e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename MatcherTuple, typename ValueTuple> 459e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid DescribeMatchFailureTupleTo(const MatcherTuple& matchers, 460e35fdd936d133bf8a48de140a3c666897588a05shiqian const ValueTuple& values, 461e35fdd936d133bf8a48de140a3c666897588a05shiqian ::std::ostream* os) { 462e35fdd936d133bf8a48de140a3c666897588a05shiqian using ::std::tr1::tuple_size; 463e35fdd936d133bf8a48de140a3c666897588a05shiqian TuplePrefix<tuple_size<MatcherTuple>::value>::DescribeMatchFailuresTo( 464e35fdd936d133bf8a48de140a3c666897588a05shiqian matchers, values, os); 465e35fdd936d133bf8a48de140a3c666897588a05shiqian} 466e35fdd936d133bf8a48de140a3c666897588a05shiqian 467e35fdd936d133bf8a48de140a3c666897588a05shiqian// The MatcherCastImpl class template is a helper for implementing 468e35fdd936d133bf8a48de140a3c666897588a05shiqian// MatcherCast(). We need this helper in order to partially 469e35fdd936d133bf8a48de140a3c666897588a05shiqian// specialize the implementation of MatcherCast() (C++ allows 470e35fdd936d133bf8a48de140a3c666897588a05shiqian// class/struct templates to be partially specialized, but not 471e35fdd936d133bf8a48de140a3c666897588a05shiqian// function templates.). 472e35fdd936d133bf8a48de140a3c666897588a05shiqian 473e35fdd936d133bf8a48de140a3c666897588a05shiqian// This general version is used when MatcherCast()'s argument is a 474e35fdd936d133bf8a48de140a3c666897588a05shiqian// polymorphic matcher (i.e. something that can be converted to a 475e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matcher but is not one yet; for example, Eq(value)). 476e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T, typename M> 477e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatcherCastImpl { 478e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 479e35fdd936d133bf8a48de140a3c666897588a05shiqian static Matcher<T> Cast(M polymorphic_matcher) { 480e35fdd936d133bf8a48de140a3c666897588a05shiqian return Matcher<T>(polymorphic_matcher); 481e35fdd936d133bf8a48de140a3c666897588a05shiqian } 482e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 483e35fdd936d133bf8a48de140a3c666897588a05shiqian 484e35fdd936d133bf8a48de140a3c666897588a05shiqian// This more specialized version is used when MatcherCast()'s argument 485e35fdd936d133bf8a48de140a3c666897588a05shiqian// is already a Matcher. This only compiles when type T can be 486e35fdd936d133bf8a48de140a3c666897588a05shiqian// statically converted to type U. 487e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T, typename U> 488e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatcherCastImpl<T, Matcher<U> > { 489e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 490e35fdd936d133bf8a48de140a3c666897588a05shiqian static Matcher<T> Cast(const Matcher<U>& source_matcher) { 491e35fdd936d133bf8a48de140a3c666897588a05shiqian return Matcher<T>(new Impl(source_matcher)); 492e35fdd936d133bf8a48de140a3c666897588a05shiqian } 493e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 494e35fdd936d133bf8a48de140a3c666897588a05shiqian class Impl : public MatcherInterface<T> { 495e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 496e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit Impl(const Matcher<U>& source_matcher) 497e35fdd936d133bf8a48de140a3c666897588a05shiqian : source_matcher_(source_matcher) {} 498e35fdd936d133bf8a48de140a3c666897588a05shiqian 499e35fdd936d133bf8a48de140a3c666897588a05shiqian // We delegate the matching logic to the source matcher. 500e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual bool Matches(T x) const { 501e35fdd936d133bf8a48de140a3c666897588a05shiqian return source_matcher_.Matches(static_cast<U>(x)); 502e35fdd936d133bf8a48de140a3c666897588a05shiqian } 503e35fdd936d133bf8a48de140a3c666897588a05shiqian 504e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeTo(::std::ostream* os) const { 505e35fdd936d133bf8a48de140a3c666897588a05shiqian source_matcher_.DescribeTo(os); 506e35fdd936d133bf8a48de140a3c666897588a05shiqian } 507e35fdd936d133bf8a48de140a3c666897588a05shiqian 508e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeNegationTo(::std::ostream* os) const { 509e35fdd936d133bf8a48de140a3c666897588a05shiqian source_matcher_.DescribeNegationTo(os); 510e35fdd936d133bf8a48de140a3c666897588a05shiqian } 511e35fdd936d133bf8a48de140a3c666897588a05shiqian 512e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void ExplainMatchResultTo(T x, ::std::ostream* os) const { 513e35fdd936d133bf8a48de140a3c666897588a05shiqian source_matcher_.ExplainMatchResultTo(static_cast<U>(x), os); 514e35fdd936d133bf8a48de140a3c666897588a05shiqian } 515e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 516e35fdd936d133bf8a48de140a3c666897588a05shiqian const Matcher<U> source_matcher_; 517e35fdd936d133bf8a48de140a3c666897588a05shiqian }; 518e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 519e35fdd936d133bf8a48de140a3c666897588a05shiqian 520e35fdd936d133bf8a48de140a3c666897588a05shiqian// This even more specialized version is used for efficiently casting 521e35fdd936d133bf8a48de140a3c666897588a05shiqian// a matcher to its own type. 522e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 523e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatcherCastImpl<T, Matcher<T> > { 524e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 525e35fdd936d133bf8a48de140a3c666897588a05shiqian static Matcher<T> Cast(const Matcher<T>& matcher) { return matcher; } 526e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 527e35fdd936d133bf8a48de140a3c666897588a05shiqian 528e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements A<T>(). 529e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 530e35fdd936d133bf8a48de140a3c666897588a05shiqianclass AnyMatcherImpl : public MatcherInterface<T> { 531e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 5323fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.wan virtual bool Matches(T /* x */) const { return true; } 533e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeTo(::std::ostream* os) const { *os << "is anything"; } 534e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeNegationTo(::std::ostream* os) const { 535e35fdd936d133bf8a48de140a3c666897588a05shiqian // This is mostly for completeness' safe, as it's not very useful 536e35fdd936d133bf8a48de140a3c666897588a05shiqian // to write Not(A<bool>()). However we cannot completely rule out 537e35fdd936d133bf8a48de140a3c666897588a05shiqian // such a possibility, and it doesn't hurt to be prepared. 538e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "never matches"; 539e35fdd936d133bf8a48de140a3c666897588a05shiqian } 540e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 541e35fdd936d133bf8a48de140a3c666897588a05shiqian 542e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements _, a matcher that matches any value of any 543e35fdd936d133bf8a48de140a3c666897588a05shiqian// type. This is a polymorphic matcher, so we need a template type 544e35fdd936d133bf8a48de140a3c666897588a05shiqian// conversion operator to make it appearing as a Matcher<T> for any 545e35fdd936d133bf8a48de140a3c666897588a05shiqian// type T. 546e35fdd936d133bf8a48de140a3c666897588a05shiqianclass AnythingMatcher { 547e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 548e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 549e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<T>() const { return A<T>(); } 550e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 551e35fdd936d133bf8a48de140a3c666897588a05shiqian 552e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements a matcher that compares a given value with a 553e35fdd936d133bf8a48de140a3c666897588a05shiqian// pre-supplied value using one of the ==, <=, <, etc, operators. The 554e35fdd936d133bf8a48de140a3c666897588a05shiqian// two values being compared don't have to have the same type. 555e35fdd936d133bf8a48de140a3c666897588a05shiqian// 556e35fdd936d133bf8a48de140a3c666897588a05shiqian// The matcher defined here is polymorphic (for example, Eq(5) can be 557e35fdd936d133bf8a48de140a3c666897588a05shiqian// used to match an int, a short, a double, etc). Therefore we use 558e35fdd936d133bf8a48de140a3c666897588a05shiqian// a template type conversion operator in the implementation. 559e35fdd936d133bf8a48de140a3c666897588a05shiqian// 560e35fdd936d133bf8a48de140a3c666897588a05shiqian// We define this as a macro in order to eliminate duplicated source 561e35fdd936d133bf8a48de140a3c666897588a05shiqian// code. 562e35fdd936d133bf8a48de140a3c666897588a05shiqian// 563e35fdd936d133bf8a48de140a3c666897588a05shiqian// The following template definition assumes that the Rhs parameter is 564e35fdd936d133bf8a48de140a3c666897588a05shiqian// a "bare" type (i.e. neither 'const T' nor 'T&'). 565e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan#define GMOCK_IMPLEMENT_COMPARISON_MATCHER_(name, op, relation) \ 566e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename Rhs> class name##Matcher { \ 567e35fdd936d133bf8a48de140a3c666897588a05shiqian public: \ 568e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit name##Matcher(const Rhs& rhs) : rhs_(rhs) {} \ 569e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename Lhs> \ 570e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<Lhs>() const { \ 571e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakeMatcher(new Impl<Lhs>(rhs_)); \ 572e35fdd936d133bf8a48de140a3c666897588a05shiqian } \ 573e35fdd936d133bf8a48de140a3c666897588a05shiqian private: \ 574e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename Lhs> \ 575e35fdd936d133bf8a48de140a3c666897588a05shiqian class Impl : public MatcherInterface<Lhs> { \ 576e35fdd936d133bf8a48de140a3c666897588a05shiqian public: \ 577e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit Impl(const Rhs& rhs) : rhs_(rhs) {} \ 578e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual bool Matches(Lhs lhs) const { return lhs op rhs_; } \ 579e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeTo(::std::ostream* os) const { \ 580e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "is " relation " "; \ 581e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<Rhs>::Print(rhs_, os); \ 582e35fdd936d133bf8a48de140a3c666897588a05shiqian } \ 583e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeNegationTo(::std::ostream* os) const { \ 584e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "is not " relation " "; \ 585e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<Rhs>::Print(rhs_, os); \ 586e35fdd936d133bf8a48de140a3c666897588a05shiqian } \ 587e35fdd936d133bf8a48de140a3c666897588a05shiqian private: \ 588e35fdd936d133bf8a48de140a3c666897588a05shiqian Rhs rhs_; \ 589e35fdd936d133bf8a48de140a3c666897588a05shiqian }; \ 590e35fdd936d133bf8a48de140a3c666897588a05shiqian Rhs rhs_; \ 591e35fdd936d133bf8a48de140a3c666897588a05shiqian } 592e35fdd936d133bf8a48de140a3c666897588a05shiqian 593e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements Eq(v), Ge(v), Gt(v), Le(v), Lt(v), and Ne(v) 594e35fdd936d133bf8a48de140a3c666897588a05shiqian// respectively. 595e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Eq, ==, "equal to"); 596e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Ge, >=, "greater than or equal to"); 597e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Gt, >, "greater than"); 598e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Le, <=, "less than or equal to"); 599e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Lt, <, "less than"); 600e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Ne, !=, "not equal to"); 601e35fdd936d133bf8a48de140a3c666897588a05shiqian 602e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan#undef GMOCK_IMPLEMENT_COMPARISON_MATCHER_ 603e35fdd936d133bf8a48de140a3c666897588a05shiqian 604e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the polymorphic NotNull() matcher, which matches any 605e35fdd936d133bf8a48de140a3c666897588a05shiqian// pointer that is not NULL. 606e35fdd936d133bf8a48de140a3c666897588a05shiqianclass NotNullMatcher { 607e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 608e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 609e35fdd936d133bf8a48de140a3c666897588a05shiqian bool Matches(T* p) const { return p != NULL; } 610e35fdd936d133bf8a48de140a3c666897588a05shiqian 611e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeTo(::std::ostream* os) const { *os << "is not NULL"; } 612e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeNegationTo(::std::ostream* os) const { 613e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "is NULL"; 614e35fdd936d133bf8a48de140a3c666897588a05shiqian } 615e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 616e35fdd936d133bf8a48de140a3c666897588a05shiqian 617e35fdd936d133bf8a48de140a3c666897588a05shiqian// Ref(variable) matches any argument that is a reference to 618e35fdd936d133bf8a48de140a3c666897588a05shiqian// 'variable'. This matcher is polymorphic as it can match any 619e35fdd936d133bf8a48de140a3c666897588a05shiqian// super type of the type of 'variable'. 620e35fdd936d133bf8a48de140a3c666897588a05shiqian// 621e35fdd936d133bf8a48de140a3c666897588a05shiqian// The RefMatcher template class implements Ref(variable). It can 622e35fdd936d133bf8a48de140a3c666897588a05shiqian// only be instantiated with a reference type. This prevents a user 623e35fdd936d133bf8a48de140a3c666897588a05shiqian// from mistakenly using Ref(x) to match a non-reference function 624e35fdd936d133bf8a48de140a3c666897588a05shiqian// argument. For example, the following will righteously cause a 625e35fdd936d133bf8a48de140a3c666897588a05shiqian// compiler error: 626e35fdd936d133bf8a48de140a3c666897588a05shiqian// 627e35fdd936d133bf8a48de140a3c666897588a05shiqian// int n; 628e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matcher<int> m1 = Ref(n); // This won't compile. 629e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matcher<int&> m2 = Ref(n); // This will compile. 630e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 631e35fdd936d133bf8a48de140a3c666897588a05shiqianclass RefMatcher; 632e35fdd936d133bf8a48de140a3c666897588a05shiqian 633e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 634e35fdd936d133bf8a48de140a3c666897588a05shiqianclass RefMatcher<T&> { 635e35fdd936d133bf8a48de140a3c666897588a05shiqian // Google Mock is a generic framework and thus needs to support 636e35fdd936d133bf8a48de140a3c666897588a05shiqian // mocking any function types, including those that take non-const 637e35fdd936d133bf8a48de140a3c666897588a05shiqian // reference arguments. Therefore the template parameter T (and 638e35fdd936d133bf8a48de140a3c666897588a05shiqian // Super below) can be instantiated to either a const type or a 639e35fdd936d133bf8a48de140a3c666897588a05shiqian // non-const type. 640e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 641e35fdd936d133bf8a48de140a3c666897588a05shiqian // RefMatcher() takes a T& instead of const T&, as we want the 642e35fdd936d133bf8a48de140a3c666897588a05shiqian // compiler to catch using Ref(const_value) as a matcher for a 643e35fdd936d133bf8a48de140a3c666897588a05shiqian // non-const reference. 644e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit RefMatcher(T& x) : object_(x) {} // NOLINT 645e35fdd936d133bf8a48de140a3c666897588a05shiqian 646e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename Super> 647e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<Super&>() const { 648e35fdd936d133bf8a48de140a3c666897588a05shiqian // By passing object_ (type T&) to Impl(), which expects a Super&, 649e35fdd936d133bf8a48de140a3c666897588a05shiqian // we make sure that Super is a super type of T. In particular, 650e35fdd936d133bf8a48de140a3c666897588a05shiqian // this catches using Ref(const_value) as a matcher for a 651e35fdd936d133bf8a48de140a3c666897588a05shiqian // non-const reference, as you cannot implicitly convert a const 652e35fdd936d133bf8a48de140a3c666897588a05shiqian // reference to a non-const reference. 653e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakeMatcher(new Impl<Super>(object_)); 654e35fdd936d133bf8a48de140a3c666897588a05shiqian } 655e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 656e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename Super> 657e35fdd936d133bf8a48de140a3c666897588a05shiqian class Impl : public MatcherInterface<Super&> { 658e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 659e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit Impl(Super& x) : object_(x) {} // NOLINT 660e35fdd936d133bf8a48de140a3c666897588a05shiqian 661e35fdd936d133bf8a48de140a3c666897588a05shiqian // Matches() takes a Super& (as opposed to const Super&) in 662e35fdd936d133bf8a48de140a3c666897588a05shiqian // order to match the interface MatcherInterface<Super&>. 663e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual bool Matches(Super& x) const { return &x == &object_; } // NOLINT 664e35fdd936d133bf8a48de140a3c666897588a05shiqian 665e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeTo(::std::ostream* os) const { 666e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "references the variable "; 667e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<Super&>::Print(object_, os); 668e35fdd936d133bf8a48de140a3c666897588a05shiqian } 669e35fdd936d133bf8a48de140a3c666897588a05shiqian 670e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeNegationTo(::std::ostream* os) const { 671e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "does not reference the variable "; 672e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<Super&>::Print(object_, os); 673e35fdd936d133bf8a48de140a3c666897588a05shiqian } 674e35fdd936d133bf8a48de140a3c666897588a05shiqian 675e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void ExplainMatchResultTo(Super& x, // NOLINT 676e35fdd936d133bf8a48de140a3c666897588a05shiqian ::std::ostream* os) const { 677e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "is located @" << static_cast<const void*>(&x); 678e35fdd936d133bf8a48de140a3c666897588a05shiqian } 679e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 680e35fdd936d133bf8a48de140a3c666897588a05shiqian const Super& object_; 681e35fdd936d133bf8a48de140a3c666897588a05shiqian }; 682e35fdd936d133bf8a48de140a3c666897588a05shiqian 683e35fdd936d133bf8a48de140a3c666897588a05shiqian T& object_; 684e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 685e35fdd936d133bf8a48de140a3c666897588a05shiqian 686e35fdd936d133bf8a48de140a3c666897588a05shiqian// Polymorphic helper functions for narrow and wide string matchers. 687e35fdd936d133bf8a48de140a3c666897588a05shiqianinline bool CaseInsensitiveCStringEquals(const char* lhs, const char* rhs) { 688e35fdd936d133bf8a48de140a3c666897588a05shiqian return String::CaseInsensitiveCStringEquals(lhs, rhs); 689e35fdd936d133bf8a48de140a3c666897588a05shiqian} 690e35fdd936d133bf8a48de140a3c666897588a05shiqian 691e35fdd936d133bf8a48de140a3c666897588a05shiqianinline bool CaseInsensitiveCStringEquals(const wchar_t* lhs, 692e35fdd936d133bf8a48de140a3c666897588a05shiqian const wchar_t* rhs) { 693e35fdd936d133bf8a48de140a3c666897588a05shiqian return String::CaseInsensitiveWideCStringEquals(lhs, rhs); 694e35fdd936d133bf8a48de140a3c666897588a05shiqian} 695e35fdd936d133bf8a48de140a3c666897588a05shiqian 696e35fdd936d133bf8a48de140a3c666897588a05shiqian// String comparison for narrow or wide strings that can have embedded NUL 697e35fdd936d133bf8a48de140a3c666897588a05shiqian// characters. 698e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType> 699e35fdd936d133bf8a48de140a3c666897588a05shiqianbool CaseInsensitiveStringEquals(const StringType& s1, 700e35fdd936d133bf8a48de140a3c666897588a05shiqian const StringType& s2) { 701e35fdd936d133bf8a48de140a3c666897588a05shiqian // Are the heads equal? 702e35fdd936d133bf8a48de140a3c666897588a05shiqian if (!CaseInsensitiveCStringEquals(s1.c_str(), s2.c_str())) { 703e35fdd936d133bf8a48de140a3c666897588a05shiqian return false; 704e35fdd936d133bf8a48de140a3c666897588a05shiqian } 705e35fdd936d133bf8a48de140a3c666897588a05shiqian 706e35fdd936d133bf8a48de140a3c666897588a05shiqian // Skip the equal heads. 707e35fdd936d133bf8a48de140a3c666897588a05shiqian const typename StringType::value_type nul = 0; 708e35fdd936d133bf8a48de140a3c666897588a05shiqian const size_t i1 = s1.find(nul), i2 = s2.find(nul); 709e35fdd936d133bf8a48de140a3c666897588a05shiqian 710e35fdd936d133bf8a48de140a3c666897588a05shiqian // Are we at the end of either s1 or s2? 711e35fdd936d133bf8a48de140a3c666897588a05shiqian if (i1 == StringType::npos || i2 == StringType::npos) { 712e35fdd936d133bf8a48de140a3c666897588a05shiqian return i1 == i2; 713e35fdd936d133bf8a48de140a3c666897588a05shiqian } 714e35fdd936d133bf8a48de140a3c666897588a05shiqian 715e35fdd936d133bf8a48de140a3c666897588a05shiqian // Are the tails equal? 716e35fdd936d133bf8a48de140a3c666897588a05shiqian return CaseInsensitiveStringEquals(s1.substr(i1 + 1), s2.substr(i2 + 1)); 717e35fdd936d133bf8a48de140a3c666897588a05shiqian} 718e35fdd936d133bf8a48de140a3c666897588a05shiqian 719e35fdd936d133bf8a48de140a3c666897588a05shiqian// String matchers. 720e35fdd936d133bf8a48de140a3c666897588a05shiqian 721e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements equality-based string matchers like StrEq, StrCaseNe, and etc. 722e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType> 723e35fdd936d133bf8a48de140a3c666897588a05shiqianclass StrEqualityMatcher { 724e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 725e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef typename StringType::const_pointer ConstCharPointer; 726e35fdd936d133bf8a48de140a3c666897588a05shiqian 727e35fdd936d133bf8a48de140a3c666897588a05shiqian StrEqualityMatcher(const StringType& str, bool expect_eq, 728e35fdd936d133bf8a48de140a3c666897588a05shiqian bool case_sensitive) 729e35fdd936d133bf8a48de140a3c666897588a05shiqian : string_(str), expect_eq_(expect_eq), case_sensitive_(case_sensitive) {} 730e35fdd936d133bf8a48de140a3c666897588a05shiqian 731e35fdd936d133bf8a48de140a3c666897588a05shiqian // When expect_eq_ is true, returns true iff s is equal to string_; 732e35fdd936d133bf8a48de140a3c666897588a05shiqian // otherwise returns true iff s is not equal to string_. 733e35fdd936d133bf8a48de140a3c666897588a05shiqian bool Matches(ConstCharPointer s) const { 734e35fdd936d133bf8a48de140a3c666897588a05shiqian if (s == NULL) { 735e35fdd936d133bf8a48de140a3c666897588a05shiqian return !expect_eq_; 736e35fdd936d133bf8a48de140a3c666897588a05shiqian } 737e35fdd936d133bf8a48de140a3c666897588a05shiqian return Matches(StringType(s)); 738e35fdd936d133bf8a48de140a3c666897588a05shiqian } 739e35fdd936d133bf8a48de140a3c666897588a05shiqian 740e35fdd936d133bf8a48de140a3c666897588a05shiqian bool Matches(const StringType& s) const { 741e35fdd936d133bf8a48de140a3c666897588a05shiqian const bool eq = case_sensitive_ ? s == string_ : 742e35fdd936d133bf8a48de140a3c666897588a05shiqian CaseInsensitiveStringEquals(s, string_); 743e35fdd936d133bf8a48de140a3c666897588a05shiqian return expect_eq_ == eq; 744e35fdd936d133bf8a48de140a3c666897588a05shiqian } 745e35fdd936d133bf8a48de140a3c666897588a05shiqian 746e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeTo(::std::ostream* os) const { 747e35fdd936d133bf8a48de140a3c666897588a05shiqian DescribeToHelper(expect_eq_, os); 748e35fdd936d133bf8a48de140a3c666897588a05shiqian } 749e35fdd936d133bf8a48de140a3c666897588a05shiqian 750e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeNegationTo(::std::ostream* os) const { 751e35fdd936d133bf8a48de140a3c666897588a05shiqian DescribeToHelper(!expect_eq_, os); 752e35fdd936d133bf8a48de140a3c666897588a05shiqian } 753e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 754e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeToHelper(bool expect_eq, ::std::ostream* os) const { 755e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "is "; 756e35fdd936d133bf8a48de140a3c666897588a05shiqian if (!expect_eq) { 757e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "not "; 758e35fdd936d133bf8a48de140a3c666897588a05shiqian } 759e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "equal to "; 760e35fdd936d133bf8a48de140a3c666897588a05shiqian if (!case_sensitive_) { 761e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "(ignoring case) "; 762e35fdd936d133bf8a48de140a3c666897588a05shiqian } 763e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<StringType>::Print(string_, os); 764e35fdd936d133bf8a48de140a3c666897588a05shiqian } 765e35fdd936d133bf8a48de140a3c666897588a05shiqian 766e35fdd936d133bf8a48de140a3c666897588a05shiqian const StringType string_; 767e35fdd936d133bf8a48de140a3c666897588a05shiqian const bool expect_eq_; 768e35fdd936d133bf8a48de140a3c666897588a05shiqian const bool case_sensitive_; 769e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 770e35fdd936d133bf8a48de140a3c666897588a05shiqian 771e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the polymorphic HasSubstr(substring) matcher, which 772e35fdd936d133bf8a48de140a3c666897588a05shiqian// can be used as a Matcher<T> as long as T can be converted to a 773e35fdd936d133bf8a48de140a3c666897588a05shiqian// string. 774e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType> 775e35fdd936d133bf8a48de140a3c666897588a05shiqianclass HasSubstrMatcher { 776e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 777e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef typename StringType::const_pointer ConstCharPointer; 778e35fdd936d133bf8a48de140a3c666897588a05shiqian 779e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit HasSubstrMatcher(const StringType& substring) 780e35fdd936d133bf8a48de140a3c666897588a05shiqian : substring_(substring) {} 781e35fdd936d133bf8a48de140a3c666897588a05shiqian 782e35fdd936d133bf8a48de140a3c666897588a05shiqian // These overloaded methods allow HasSubstr(substring) to be used as a 783e35fdd936d133bf8a48de140a3c666897588a05shiqian // Matcher<T> as long as T can be converted to string. Returns true 784e35fdd936d133bf8a48de140a3c666897588a05shiqian // iff s contains substring_ as a substring. 785e35fdd936d133bf8a48de140a3c666897588a05shiqian bool Matches(ConstCharPointer s) const { 786e35fdd936d133bf8a48de140a3c666897588a05shiqian return s != NULL && Matches(StringType(s)); 787e35fdd936d133bf8a48de140a3c666897588a05shiqian } 788e35fdd936d133bf8a48de140a3c666897588a05shiqian 789e35fdd936d133bf8a48de140a3c666897588a05shiqian bool Matches(const StringType& s) const { 790e35fdd936d133bf8a48de140a3c666897588a05shiqian return s.find(substring_) != StringType::npos; 791e35fdd936d133bf8a48de140a3c666897588a05shiqian } 792e35fdd936d133bf8a48de140a3c666897588a05shiqian 793e35fdd936d133bf8a48de140a3c666897588a05shiqian // Describes what this matcher matches. 794e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeTo(::std::ostream* os) const { 795e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "has substring "; 796e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<StringType>::Print(substring_, os); 797e35fdd936d133bf8a48de140a3c666897588a05shiqian } 798e35fdd936d133bf8a48de140a3c666897588a05shiqian 799e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeNegationTo(::std::ostream* os) const { 800e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "has no substring "; 801e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<StringType>::Print(substring_, os); 802e35fdd936d133bf8a48de140a3c666897588a05shiqian } 803e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 804e35fdd936d133bf8a48de140a3c666897588a05shiqian const StringType substring_; 805e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 806e35fdd936d133bf8a48de140a3c666897588a05shiqian 807e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the polymorphic StartsWith(substring) matcher, which 808e35fdd936d133bf8a48de140a3c666897588a05shiqian// can be used as a Matcher<T> as long as T can be converted to a 809e35fdd936d133bf8a48de140a3c666897588a05shiqian// string. 810e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType> 811e35fdd936d133bf8a48de140a3c666897588a05shiqianclass StartsWithMatcher { 812e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 813e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef typename StringType::const_pointer ConstCharPointer; 814e35fdd936d133bf8a48de140a3c666897588a05shiqian 815e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit StartsWithMatcher(const StringType& prefix) : prefix_(prefix) { 816e35fdd936d133bf8a48de140a3c666897588a05shiqian } 817e35fdd936d133bf8a48de140a3c666897588a05shiqian 818e35fdd936d133bf8a48de140a3c666897588a05shiqian // These overloaded methods allow StartsWith(prefix) to be used as a 819e35fdd936d133bf8a48de140a3c666897588a05shiqian // Matcher<T> as long as T can be converted to string. Returns true 820e35fdd936d133bf8a48de140a3c666897588a05shiqian // iff s starts with prefix_. 821e35fdd936d133bf8a48de140a3c666897588a05shiqian bool Matches(ConstCharPointer s) const { 822e35fdd936d133bf8a48de140a3c666897588a05shiqian return s != NULL && Matches(StringType(s)); 823e35fdd936d133bf8a48de140a3c666897588a05shiqian } 824e35fdd936d133bf8a48de140a3c666897588a05shiqian 825e35fdd936d133bf8a48de140a3c666897588a05shiqian bool Matches(const StringType& s) const { 826e35fdd936d133bf8a48de140a3c666897588a05shiqian return s.length() >= prefix_.length() && 827e35fdd936d133bf8a48de140a3c666897588a05shiqian s.substr(0, prefix_.length()) == prefix_; 828e35fdd936d133bf8a48de140a3c666897588a05shiqian } 829e35fdd936d133bf8a48de140a3c666897588a05shiqian 830e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeTo(::std::ostream* os) const { 831e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "starts with "; 832e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<StringType>::Print(prefix_, os); 833e35fdd936d133bf8a48de140a3c666897588a05shiqian } 834e35fdd936d133bf8a48de140a3c666897588a05shiqian 835e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeNegationTo(::std::ostream* os) const { 836e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "doesn't start with "; 837e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<StringType>::Print(prefix_, os); 838e35fdd936d133bf8a48de140a3c666897588a05shiqian } 839e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 840e35fdd936d133bf8a48de140a3c666897588a05shiqian const StringType prefix_; 841e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 842e35fdd936d133bf8a48de140a3c666897588a05shiqian 843e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the polymorphic EndsWith(substring) matcher, which 844e35fdd936d133bf8a48de140a3c666897588a05shiqian// can be used as a Matcher<T> as long as T can be converted to a 845e35fdd936d133bf8a48de140a3c666897588a05shiqian// string. 846e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType> 847e35fdd936d133bf8a48de140a3c666897588a05shiqianclass EndsWithMatcher { 848e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 849e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef typename StringType::const_pointer ConstCharPointer; 850e35fdd936d133bf8a48de140a3c666897588a05shiqian 851e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit EndsWithMatcher(const StringType& suffix) : suffix_(suffix) {} 852e35fdd936d133bf8a48de140a3c666897588a05shiqian 853e35fdd936d133bf8a48de140a3c666897588a05shiqian // These overloaded methods allow EndsWith(suffix) to be used as a 854e35fdd936d133bf8a48de140a3c666897588a05shiqian // Matcher<T> as long as T can be converted to string. Returns true 855e35fdd936d133bf8a48de140a3c666897588a05shiqian // iff s ends with suffix_. 856e35fdd936d133bf8a48de140a3c666897588a05shiqian bool Matches(ConstCharPointer s) const { 857e35fdd936d133bf8a48de140a3c666897588a05shiqian return s != NULL && Matches(StringType(s)); 858e35fdd936d133bf8a48de140a3c666897588a05shiqian } 859e35fdd936d133bf8a48de140a3c666897588a05shiqian 860e35fdd936d133bf8a48de140a3c666897588a05shiqian bool Matches(const StringType& s) const { 861e35fdd936d133bf8a48de140a3c666897588a05shiqian return s.length() >= suffix_.length() && 862e35fdd936d133bf8a48de140a3c666897588a05shiqian s.substr(s.length() - suffix_.length()) == suffix_; 863e35fdd936d133bf8a48de140a3c666897588a05shiqian } 864e35fdd936d133bf8a48de140a3c666897588a05shiqian 865e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeTo(::std::ostream* os) const { 866e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "ends with "; 867e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<StringType>::Print(suffix_, os); 868e35fdd936d133bf8a48de140a3c666897588a05shiqian } 869e35fdd936d133bf8a48de140a3c666897588a05shiqian 870e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeNegationTo(::std::ostream* os) const { 871e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "doesn't end with "; 872e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<StringType>::Print(suffix_, os); 873e35fdd936d133bf8a48de140a3c666897588a05shiqian } 874e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 875e35fdd936d133bf8a48de140a3c666897588a05shiqian const StringType suffix_; 876e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 877e35fdd936d133bf8a48de140a3c666897588a05shiqian 878e35fdd936d133bf8a48de140a3c666897588a05shiqian#if GMOCK_HAS_REGEX 879e35fdd936d133bf8a48de140a3c666897588a05shiqian 880e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements polymorphic matchers MatchesRegex(regex) and 881e35fdd936d133bf8a48de140a3c666897588a05shiqian// ContainsRegex(regex), which can be used as a Matcher<T> as long as 882e35fdd936d133bf8a48de140a3c666897588a05shiqian// T can be converted to a string. 883e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatchesRegexMatcher { 884e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 885e35fdd936d133bf8a48de140a3c666897588a05shiqian MatchesRegexMatcher(const RE* regex, bool full_match) 886e35fdd936d133bf8a48de140a3c666897588a05shiqian : regex_(regex), full_match_(full_match) {} 887e35fdd936d133bf8a48de140a3c666897588a05shiqian 888e35fdd936d133bf8a48de140a3c666897588a05shiqian // These overloaded methods allow MatchesRegex(regex) to be used as 889e35fdd936d133bf8a48de140a3c666897588a05shiqian // a Matcher<T> as long as T can be converted to string. Returns 890e35fdd936d133bf8a48de140a3c666897588a05shiqian // true iff s matches regular expression regex. When full_match_ is 891e35fdd936d133bf8a48de140a3c666897588a05shiqian // true, a full match is done; otherwise a partial match is done. 892e35fdd936d133bf8a48de140a3c666897588a05shiqian bool Matches(const char* s) const { 893e35fdd936d133bf8a48de140a3c666897588a05shiqian return s != NULL && Matches(internal::string(s)); 894e35fdd936d133bf8a48de140a3c666897588a05shiqian } 895e35fdd936d133bf8a48de140a3c666897588a05shiqian 896e35fdd936d133bf8a48de140a3c666897588a05shiqian bool Matches(const internal::string& s) const { 897e35fdd936d133bf8a48de140a3c666897588a05shiqian return full_match_ ? RE::FullMatch(s, *regex_) : 898e35fdd936d133bf8a48de140a3c666897588a05shiqian RE::PartialMatch(s, *regex_); 899e35fdd936d133bf8a48de140a3c666897588a05shiqian } 900e35fdd936d133bf8a48de140a3c666897588a05shiqian 901e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeTo(::std::ostream* os) const { 902e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << (full_match_ ? "matches" : "contains") 903e35fdd936d133bf8a48de140a3c666897588a05shiqian << " regular expression "; 904e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<internal::string>::Print(regex_->pattern(), os); 905e35fdd936d133bf8a48de140a3c666897588a05shiqian } 906e35fdd936d133bf8a48de140a3c666897588a05shiqian 907e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeNegationTo(::std::ostream* os) const { 908e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "doesn't " << (full_match_ ? "match" : "contain") 909e35fdd936d133bf8a48de140a3c666897588a05shiqian << " regular expression "; 910e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<internal::string>::Print(regex_->pattern(), os); 911e35fdd936d133bf8a48de140a3c666897588a05shiqian } 912e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 913e35fdd936d133bf8a48de140a3c666897588a05shiqian const internal::linked_ptr<const RE> regex_; 914e35fdd936d133bf8a48de140a3c666897588a05shiqian const bool full_match_; 915e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 916e35fdd936d133bf8a48de140a3c666897588a05shiqian 917e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif // GMOCK_HAS_REGEX 918e35fdd936d133bf8a48de140a3c666897588a05shiqian 919e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements a matcher that compares the two fields of a 2-tuple 920e35fdd936d133bf8a48de140a3c666897588a05shiqian// using one of the ==, <=, <, etc, operators. The two fields being 921e35fdd936d133bf8a48de140a3c666897588a05shiqian// compared don't have to have the same type. 922e35fdd936d133bf8a48de140a3c666897588a05shiqian// 923e35fdd936d133bf8a48de140a3c666897588a05shiqian// The matcher defined here is polymorphic (for example, Eq() can be 924e35fdd936d133bf8a48de140a3c666897588a05shiqian// used to match a tuple<int, short>, a tuple<const long&, double>, 925e35fdd936d133bf8a48de140a3c666897588a05shiqian// etc). Therefore we use a template type conversion operator in the 926e35fdd936d133bf8a48de140a3c666897588a05shiqian// implementation. 927e35fdd936d133bf8a48de140a3c666897588a05shiqian// 928e35fdd936d133bf8a48de140a3c666897588a05shiqian// We define this as a macro in order to eliminate duplicated source 929e35fdd936d133bf8a48de140a3c666897588a05shiqian// code. 930e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan#define GMOCK_IMPLEMENT_COMPARISON2_MATCHER_(name, op, relation) \ 931e35fdd936d133bf8a48de140a3c666897588a05shiqian class name##2Matcher { \ 932e35fdd936d133bf8a48de140a3c666897588a05shiqian public: \ 933e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T1, typename T2> \ 934e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<const ::std::tr1::tuple<T1, T2>&>() const { \ 935e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakeMatcher(new Impl<T1, T2>); \ 936e35fdd936d133bf8a48de140a3c666897588a05shiqian } \ 937e35fdd936d133bf8a48de140a3c666897588a05shiqian private: \ 938e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T1, typename T2> \ 939e35fdd936d133bf8a48de140a3c666897588a05shiqian class Impl : public MatcherInterface<const ::std::tr1::tuple<T1, T2>&> { \ 940e35fdd936d133bf8a48de140a3c666897588a05shiqian public: \ 941e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual bool Matches(const ::std::tr1::tuple<T1, T2>& args) const { \ 942e35fdd936d133bf8a48de140a3c666897588a05shiqian return ::std::tr1::get<0>(args) op ::std::tr1::get<1>(args); \ 943e35fdd936d133bf8a48de140a3c666897588a05shiqian } \ 944e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeTo(::std::ostream* os) const { \ 945e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "argument #0 is " relation " argument #1"; \ 946e35fdd936d133bf8a48de140a3c666897588a05shiqian } \ 947e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeNegationTo(::std::ostream* os) const { \ 948e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "argument #0 is not " relation " argument #1"; \ 949e35fdd936d133bf8a48de140a3c666897588a05shiqian } \ 950e35fdd936d133bf8a48de140a3c666897588a05shiqian }; \ 951e35fdd936d133bf8a48de140a3c666897588a05shiqian } 952e35fdd936d133bf8a48de140a3c666897588a05shiqian 953e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements Eq(), Ge(), Gt(), Le(), Lt(), and Ne() respectively. 954e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Eq, ==, "equal to"); 955e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Ge, >=, "greater than or equal to"); 956e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Gt, >, "greater than"); 957e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Le, <=, "less than or equal to"); 958e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Lt, <, "less than"); 959e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Ne, !=, "not equal to"); 960e35fdd936d133bf8a48de140a3c666897588a05shiqian 961e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan#undef GMOCK_IMPLEMENT_COMPARISON2_MATCHER_ 962e35fdd936d133bf8a48de140a3c666897588a05shiqian 963c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// Implements the Not(...) matcher for a particular argument type T. 964c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// We do not nest it inside the NotMatcher class template, as that 965c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// will prevent different instantiations of NotMatcher from sharing 966c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// the same NotMatcherImpl<T> class. 967c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wantemplate <typename T> 968c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wanclass NotMatcherImpl : public MatcherInterface<T> { 969c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan public: 970c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan explicit NotMatcherImpl(const Matcher<T>& matcher) 971c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan : matcher_(matcher) {} 972c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan 973c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan virtual bool Matches(T x) const { 974c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan return !matcher_.Matches(x); 975c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 976c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan 977c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan virtual void DescribeTo(::std::ostream* os) const { 978c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan matcher_.DescribeNegationTo(os); 979c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 980c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan 981c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan virtual void DescribeNegationTo(::std::ostream* os) const { 982c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan matcher_.DescribeTo(os); 983c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 984c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan 985c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan virtual void ExplainMatchResultTo(T x, ::std::ostream* os) const { 986c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan matcher_.ExplainMatchResultTo(x, os); 987c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 988c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan private: 989c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan const Matcher<T> matcher_; 990c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan}; 991c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan 992e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the Not(m) matcher, which matches a value that doesn't 993e35fdd936d133bf8a48de140a3c666897588a05shiqian// match matcher m. 994e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename InnerMatcher> 995e35fdd936d133bf8a48de140a3c666897588a05shiqianclass NotMatcher { 996e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 997e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit NotMatcher(InnerMatcher matcher) : matcher_(matcher) {} 998e35fdd936d133bf8a48de140a3c666897588a05shiqian 999e35fdd936d133bf8a48de140a3c666897588a05shiqian // This template type conversion operator allows Not(m) to be used 1000e35fdd936d133bf8a48de140a3c666897588a05shiqian // to match any type m can match. 1001e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 1002e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<T>() const { 1003c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan return Matcher<T>(new NotMatcherImpl<T>(SafeMatcherCast<T>(matcher_))); 1004e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1005e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1006c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan InnerMatcher matcher_; 1007c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan}; 1008e35fdd936d133bf8a48de140a3c666897588a05shiqian 1009c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// Implements the AllOf(m1, m2) matcher for a particular argument type 1010c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// T. We do not nest it inside the BothOfMatcher class template, as 1011c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// that will prevent different instantiations of BothOfMatcher from 1012c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// sharing the same BothOfMatcherImpl<T> class. 1013c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wantemplate <typename T> 1014c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wanclass BothOfMatcherImpl : public MatcherInterface<T> { 1015c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan public: 1016c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan BothOfMatcherImpl(const Matcher<T>& matcher1, const Matcher<T>& matcher2) 1017c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan : matcher1_(matcher1), matcher2_(matcher2) {} 1018e35fdd936d133bf8a48de140a3c666897588a05shiqian 1019c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan virtual bool Matches(T x) const { 1020c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan return matcher1_.Matches(x) && matcher2_.Matches(x); 1021c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 1022e35fdd936d133bf8a48de140a3c666897588a05shiqian 1023c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan virtual void DescribeTo(::std::ostream* os) const { 1024c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan *os << "("; 1025c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan matcher1_.DescribeTo(os); 1026c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan *os << ") and ("; 1027c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan matcher2_.DescribeTo(os); 1028c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan *os << ")"; 1029c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 1030e35fdd936d133bf8a48de140a3c666897588a05shiqian 1031c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan virtual void DescribeNegationTo(::std::ostream* os) const { 1032c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan *os << "not "; 1033c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan DescribeTo(os); 1034c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 1035e35fdd936d133bf8a48de140a3c666897588a05shiqian 1036c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan virtual void ExplainMatchResultTo(T x, ::std::ostream* os) const { 1037c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan if (Matches(x)) { 1038c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan // When both matcher1_ and matcher2_ match x, we need to 1039c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan // explain why *both* of them match. 1040c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan ::std::stringstream ss1; 1041c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan matcher1_.ExplainMatchResultTo(x, &ss1); 1042c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan const internal::string s1 = ss1.str(); 1043c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan 1044c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan ::std::stringstream ss2; 1045c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan matcher2_.ExplainMatchResultTo(x, &ss2); 1046c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan const internal::string s2 = ss2.str(); 1047c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan 1048c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan if (s1 == "") { 1049c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan *os << s2; 1050c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } else { 1051c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan *os << s1; 1052c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan if (s2 != "") { 1053c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan *os << "; " << s2; 1054c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 1055c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 1056c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } else { 1057c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan // Otherwise we only need to explain why *one* of them fails 1058c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan // to match. 1059c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan if (!matcher1_.Matches(x)) { 1060c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan matcher1_.ExplainMatchResultTo(x, os); 1061c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } else { 1062c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan matcher2_.ExplainMatchResultTo(x, os); 1063c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 1064c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 1065c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 1066c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan private: 1067c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan const Matcher<T> matcher1_; 1068c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan const Matcher<T> matcher2_; 1069e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1070e35fdd936d133bf8a48de140a3c666897588a05shiqian 1071e35fdd936d133bf8a48de140a3c666897588a05shiqian// Used for implementing the AllOf(m_1, ..., m_n) matcher, which 1072e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a value that matches all of the matchers m_1, ..., and m_n. 1073e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2> 1074e35fdd936d133bf8a48de140a3c666897588a05shiqianclass BothOfMatcher { 1075e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1076e35fdd936d133bf8a48de140a3c666897588a05shiqian BothOfMatcher(Matcher1 matcher1, Matcher2 matcher2) 1077e35fdd936d133bf8a48de140a3c666897588a05shiqian : matcher1_(matcher1), matcher2_(matcher2) {} 1078e35fdd936d133bf8a48de140a3c666897588a05shiqian 1079e35fdd936d133bf8a48de140a3c666897588a05shiqian // This template type conversion operator allows a 1080e35fdd936d133bf8a48de140a3c666897588a05shiqian // BothOfMatcher<Matcher1, Matcher2> object to match any type that 1081e35fdd936d133bf8a48de140a3c666897588a05shiqian // both Matcher1 and Matcher2 can match. 1082e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 1083e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<T>() const { 1084c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan return Matcher<T>(new BothOfMatcherImpl<T>(SafeMatcherCast<T>(matcher1_), 1085c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan SafeMatcherCast<T>(matcher2_))); 1086e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1087e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1088c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan Matcher1 matcher1_; 1089c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan Matcher2 matcher2_; 1090c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan}; 1091e35fdd936d133bf8a48de140a3c666897588a05shiqian 1092c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// Implements the AnyOf(m1, m2) matcher for a particular argument type 1093c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// T. We do not nest it inside the AnyOfMatcher class template, as 1094c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// that will prevent different instantiations of AnyOfMatcher from 1095c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// sharing the same EitherOfMatcherImpl<T> class. 1096c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wantemplate <typename T> 1097c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wanclass EitherOfMatcherImpl : public MatcherInterface<T> { 1098c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan public: 1099c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan EitherOfMatcherImpl(const Matcher<T>& matcher1, const Matcher<T>& matcher2) 1100c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan : matcher1_(matcher1), matcher2_(matcher2) {} 1101e35fdd936d133bf8a48de140a3c666897588a05shiqian 1102c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan virtual bool Matches(T x) const { 1103c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan return matcher1_.Matches(x) || matcher2_.Matches(x); 1104c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 1105e35fdd936d133bf8a48de140a3c666897588a05shiqian 1106c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan virtual void DescribeTo(::std::ostream* os) const { 1107c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan *os << "("; 1108c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan matcher1_.DescribeTo(os); 1109c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan *os << ") or ("; 1110c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan matcher2_.DescribeTo(os); 1111c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan *os << ")"; 1112c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 1113e35fdd936d133bf8a48de140a3c666897588a05shiqian 1114c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan virtual void DescribeNegationTo(::std::ostream* os) const { 1115c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan *os << "not "; 1116c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan DescribeTo(os); 1117c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 1118c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan 1119c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan virtual void ExplainMatchResultTo(T x, ::std::ostream* os) const { 1120c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan if (Matches(x)) { 1121c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan // If either matcher1_ or matcher2_ matches x, we just need 1122c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan // to explain why *one* of them matches. 1123c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan if (matcher1_.Matches(x)) { 1124c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan matcher1_.ExplainMatchResultTo(x, os); 1125e35fdd936d133bf8a48de140a3c666897588a05shiqian } else { 1126c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan matcher2_.ExplainMatchResultTo(x, os); 1127c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 1128c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } else { 1129c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan // Otherwise we need to explain why *neither* matches. 1130c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan ::std::stringstream ss1; 1131c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan matcher1_.ExplainMatchResultTo(x, &ss1); 1132c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan const internal::string s1 = ss1.str(); 1133c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan 1134c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan ::std::stringstream ss2; 1135c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan matcher2_.ExplainMatchResultTo(x, &ss2); 1136c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan const internal::string s2 = ss2.str(); 1137c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan 1138c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan if (s1 == "") { 1139c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan *os << s2; 1140c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } else { 1141c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan *os << s1; 1142c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan if (s2 != "") { 1143c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan *os << "; " << s2; 1144e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1145e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1146e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1147c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 1148c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan private: 1149c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan const Matcher<T> matcher1_; 1150c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan const Matcher<T> matcher2_; 1151e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1152e35fdd936d133bf8a48de140a3c666897588a05shiqian 1153e35fdd936d133bf8a48de140a3c666897588a05shiqian// Used for implementing the AnyOf(m_1, ..., m_n) matcher, which 1154e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a value that matches at least one of the matchers m_1, ..., 1155e35fdd936d133bf8a48de140a3c666897588a05shiqian// and m_n. 1156e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2> 1157e35fdd936d133bf8a48de140a3c666897588a05shiqianclass EitherOfMatcher { 1158e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1159e35fdd936d133bf8a48de140a3c666897588a05shiqian EitherOfMatcher(Matcher1 matcher1, Matcher2 matcher2) 1160e35fdd936d133bf8a48de140a3c666897588a05shiqian : matcher1_(matcher1), matcher2_(matcher2) {} 1161e35fdd936d133bf8a48de140a3c666897588a05shiqian 1162e35fdd936d133bf8a48de140a3c666897588a05shiqian // This template type conversion operator allows a 1163e35fdd936d133bf8a48de140a3c666897588a05shiqian // EitherOfMatcher<Matcher1, Matcher2> object to match any type that 1164e35fdd936d133bf8a48de140a3c666897588a05shiqian // both Matcher1 and Matcher2 can match. 1165e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 1166e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<T>() const { 1167c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan return Matcher<T>(new EitherOfMatcherImpl<T>(SafeMatcherCast<T>(matcher1_), 1168c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan SafeMatcherCast<T>(matcher2_))); 1169e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1170e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1171e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher1 matcher1_; 1172e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher2 matcher2_; 1173e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1174e35fdd936d133bf8a48de140a3c666897588a05shiqian 1175e35fdd936d133bf8a48de140a3c666897588a05shiqian// Used for implementing Truly(pred), which turns a predicate into a 1176e35fdd936d133bf8a48de140a3c666897588a05shiqian// matcher. 1177e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Predicate> 1178e35fdd936d133bf8a48de140a3c666897588a05shiqianclass TrulyMatcher { 1179e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1180e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit TrulyMatcher(Predicate pred) : predicate_(pred) {} 1181e35fdd936d133bf8a48de140a3c666897588a05shiqian 1182e35fdd936d133bf8a48de140a3c666897588a05shiqian // This method template allows Truly(pred) to be used as a matcher 1183e35fdd936d133bf8a48de140a3c666897588a05shiqian // for type T where T is the argument type of predicate 'pred'. The 1184e35fdd936d133bf8a48de140a3c666897588a05shiqian // argument is passed by reference as the predicate may be 1185e35fdd936d133bf8a48de140a3c666897588a05shiqian // interested in the address of the argument. 1186e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 1187e35fdd936d133bf8a48de140a3c666897588a05shiqian bool Matches(T& x) const { 1188652540a278e56528e576f9ea77515f386698a326zhanyong.wan#if GTEST_OS_WINDOWS 1189e35fdd936d133bf8a48de140a3c666897588a05shiqian // MSVC warns about converting a value into bool (warning 4800). 1190e35fdd936d133bf8a48de140a3c666897588a05shiqian#pragma warning(push) // Saves the current warning state. 1191e35fdd936d133bf8a48de140a3c666897588a05shiqian#pragma warning(disable:4800) // Temporarily disables warning 4800. 1192e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif // GTEST_OS_WINDOWS 1193e35fdd936d133bf8a48de140a3c666897588a05shiqian return predicate_(x); 1194652540a278e56528e576f9ea77515f386698a326zhanyong.wan#if GTEST_OS_WINDOWS 1195e35fdd936d133bf8a48de140a3c666897588a05shiqian#pragma warning(pop) // Restores the warning state. 1196e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif // GTEST_OS_WINDOWS 1197e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1198e35fdd936d133bf8a48de140a3c666897588a05shiqian 1199e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeTo(::std::ostream* os) const { 1200e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "satisfies the given predicate"; 1201e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1202e35fdd936d133bf8a48de140a3c666897588a05shiqian 1203e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeNegationTo(::std::ostream* os) const { 1204e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "doesn't satisfy the given predicate"; 1205e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1206e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1207e35fdd936d133bf8a48de140a3c666897588a05shiqian Predicate predicate_; 1208e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1209e35fdd936d133bf8a48de140a3c666897588a05shiqian 1210e35fdd936d133bf8a48de140a3c666897588a05shiqian// Used for implementing Matches(matcher), which turns a matcher into 1211e35fdd936d133bf8a48de140a3c666897588a05shiqian// a predicate. 1212e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename M> 1213e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatcherAsPredicate { 1214e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1215e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit MatcherAsPredicate(M matcher) : matcher_(matcher) {} 1216e35fdd936d133bf8a48de140a3c666897588a05shiqian 1217e35fdd936d133bf8a48de140a3c666897588a05shiqian // This template operator() allows Matches(m) to be used as a 1218e35fdd936d133bf8a48de140a3c666897588a05shiqian // predicate on type T where m is a matcher on type T. 1219e35fdd936d133bf8a48de140a3c666897588a05shiqian // 1220e35fdd936d133bf8a48de140a3c666897588a05shiqian // The argument x is passed by reference instead of by value, as 1221e35fdd936d133bf8a48de140a3c666897588a05shiqian // some matcher may be interested in its address (e.g. as in 1222e35fdd936d133bf8a48de140a3c666897588a05shiqian // Matches(Ref(n))(x)). 1223e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 1224e35fdd936d133bf8a48de140a3c666897588a05shiqian bool operator()(const T& x) const { 1225e35fdd936d133bf8a48de140a3c666897588a05shiqian // We let matcher_ commit to a particular type here instead of 1226e35fdd936d133bf8a48de140a3c666897588a05shiqian // when the MatcherAsPredicate object was constructed. This 1227e35fdd936d133bf8a48de140a3c666897588a05shiqian // allows us to write Matches(m) where m is a polymorphic matcher 1228e35fdd936d133bf8a48de140a3c666897588a05shiqian // (e.g. Eq(5)). 1229e35fdd936d133bf8a48de140a3c666897588a05shiqian // 1230e35fdd936d133bf8a48de140a3c666897588a05shiqian // If we write Matcher<T>(matcher_).Matches(x) here, it won't 1231e35fdd936d133bf8a48de140a3c666897588a05shiqian // compile when matcher_ has type Matcher<const T&>; if we write 1232e35fdd936d133bf8a48de140a3c666897588a05shiqian // Matcher<const T&>(matcher_).Matches(x) here, it won't compile 1233e35fdd936d133bf8a48de140a3c666897588a05shiqian // when matcher_ has type Matcher<T>; if we just write 1234e35fdd936d133bf8a48de140a3c666897588a05shiqian // matcher_.Matches(x), it won't compile when matcher_ is 1235e35fdd936d133bf8a48de140a3c666897588a05shiqian // polymorphic, e.g. Eq(5). 1236e35fdd936d133bf8a48de140a3c666897588a05shiqian // 1237e35fdd936d133bf8a48de140a3c666897588a05shiqian // MatcherCast<const T&>() is necessary for making the code work 1238e35fdd936d133bf8a48de140a3c666897588a05shiqian // in all of the above situations. 1239e35fdd936d133bf8a48de140a3c666897588a05shiqian return MatcherCast<const T&>(matcher_).Matches(x); 1240e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1241e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1242e35fdd936d133bf8a48de140a3c666897588a05shiqian M matcher_; 1243e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1244e35fdd936d133bf8a48de140a3c666897588a05shiqian 1245e35fdd936d133bf8a48de140a3c666897588a05shiqian// For implementing ASSERT_THAT() and EXPECT_THAT(). The template 1246e35fdd936d133bf8a48de140a3c666897588a05shiqian// argument M must be a type that can be converted to a matcher. 1247e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename M> 1248e35fdd936d133bf8a48de140a3c666897588a05shiqianclass PredicateFormatterFromMatcher { 1249e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1250e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit PredicateFormatterFromMatcher(const M& m) : matcher_(m) {} 1251e35fdd936d133bf8a48de140a3c666897588a05shiqian 1252e35fdd936d133bf8a48de140a3c666897588a05shiqian // This template () operator allows a PredicateFormatterFromMatcher 1253e35fdd936d133bf8a48de140a3c666897588a05shiqian // object to act as a predicate-formatter suitable for using with 1254e35fdd936d133bf8a48de140a3c666897588a05shiqian // Google Test's EXPECT_PRED_FORMAT1() macro. 1255e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 1256e35fdd936d133bf8a48de140a3c666897588a05shiqian AssertionResult operator()(const char* value_text, const T& x) const { 1257e35fdd936d133bf8a48de140a3c666897588a05shiqian // We convert matcher_ to a Matcher<const T&> *now* instead of 1258e35fdd936d133bf8a48de140a3c666897588a05shiqian // when the PredicateFormatterFromMatcher object was constructed, 1259e35fdd936d133bf8a48de140a3c666897588a05shiqian // as matcher_ may be polymorphic (e.g. NotNull()) and we won't 1260e35fdd936d133bf8a48de140a3c666897588a05shiqian // know which type to instantiate it to until we actually see the 1261e35fdd936d133bf8a48de140a3c666897588a05shiqian // type of x here. 1262e35fdd936d133bf8a48de140a3c666897588a05shiqian // 1263e35fdd936d133bf8a48de140a3c666897588a05shiqian // We write MatcherCast<const T&>(matcher_) instead of 1264e35fdd936d133bf8a48de140a3c666897588a05shiqian // Matcher<const T&>(matcher_), as the latter won't compile when 1265e35fdd936d133bf8a48de140a3c666897588a05shiqian // matcher_ has type Matcher<T> (e.g. An<int>()). 1266e35fdd936d133bf8a48de140a3c666897588a05shiqian const Matcher<const T&> matcher = MatcherCast<const T&>(matcher_); 1267e35fdd936d133bf8a48de140a3c666897588a05shiqian if (matcher.Matches(x)) { 1268e35fdd936d133bf8a48de140a3c666897588a05shiqian return AssertionSuccess(); 1269e35fdd936d133bf8a48de140a3c666897588a05shiqian } else { 1270e35fdd936d133bf8a48de140a3c666897588a05shiqian ::std::stringstream ss; 1271e35fdd936d133bf8a48de140a3c666897588a05shiqian ss << "Value of: " << value_text << "\n" 1272e35fdd936d133bf8a48de140a3c666897588a05shiqian << "Expected: "; 1273e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher.DescribeTo(&ss); 1274e35fdd936d133bf8a48de140a3c666897588a05shiqian ss << "\n Actual: "; 1275e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<T>::Print(x, &ss); 1276e35fdd936d133bf8a48de140a3c666897588a05shiqian ExplainMatchResultAsNeededTo<const T&>(matcher, x, &ss); 1277e35fdd936d133bf8a48de140a3c666897588a05shiqian return AssertionFailure(Message() << ss.str()); 1278e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1279e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1280e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1281e35fdd936d133bf8a48de140a3c666897588a05shiqian const M matcher_; 1282e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1283e35fdd936d133bf8a48de140a3c666897588a05shiqian 1284e35fdd936d133bf8a48de140a3c666897588a05shiqian// A helper function for converting a matcher to a predicate-formatter 1285e35fdd936d133bf8a48de140a3c666897588a05shiqian// without the user needing to explicitly write the type. This is 1286e35fdd936d133bf8a48de140a3c666897588a05shiqian// used for implementing ASSERT_THAT() and EXPECT_THAT(). 1287e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename M> 1288e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PredicateFormatterFromMatcher<M> 1289e35fdd936d133bf8a48de140a3c666897588a05shiqianMakePredicateFormatterFromMatcher(const M& matcher) { 1290e35fdd936d133bf8a48de140a3c666897588a05shiqian return PredicateFormatterFromMatcher<M>(matcher); 1291e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1292e35fdd936d133bf8a48de140a3c666897588a05shiqian 1293e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the polymorphic floating point equality matcher, which 1294e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches two float values using ULP-based approximation. The 1295e35fdd936d133bf8a48de140a3c666897588a05shiqian// template is meant to be instantiated with FloatType being either 1296e35fdd936d133bf8a48de140a3c666897588a05shiqian// float or double. 1297e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename FloatType> 1298e35fdd936d133bf8a48de140a3c666897588a05shiqianclass FloatingEqMatcher { 1299e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1300e35fdd936d133bf8a48de140a3c666897588a05shiqian // Constructor for FloatingEqMatcher. 1301e35fdd936d133bf8a48de140a3c666897588a05shiqian // The matcher's input will be compared with rhs. The matcher treats two 1302e35fdd936d133bf8a48de140a3c666897588a05shiqian // NANs as equal if nan_eq_nan is true. Otherwise, under IEEE standards, 1303e35fdd936d133bf8a48de140a3c666897588a05shiqian // equality comparisons between NANs will always return false. 1304e35fdd936d133bf8a48de140a3c666897588a05shiqian FloatingEqMatcher(FloatType rhs, bool nan_eq_nan) : 1305e35fdd936d133bf8a48de140a3c666897588a05shiqian rhs_(rhs), nan_eq_nan_(nan_eq_nan) {} 1306e35fdd936d133bf8a48de140a3c666897588a05shiqian 1307e35fdd936d133bf8a48de140a3c666897588a05shiqian // Implements floating point equality matcher as a Matcher<T>. 1308e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 1309e35fdd936d133bf8a48de140a3c666897588a05shiqian class Impl : public MatcherInterface<T> { 1310e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1311e35fdd936d133bf8a48de140a3c666897588a05shiqian Impl(FloatType rhs, bool nan_eq_nan) : 1312e35fdd936d133bf8a48de140a3c666897588a05shiqian rhs_(rhs), nan_eq_nan_(nan_eq_nan) {} 1313e35fdd936d133bf8a48de140a3c666897588a05shiqian 1314e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual bool Matches(T value) const { 1315e35fdd936d133bf8a48de140a3c666897588a05shiqian const FloatingPoint<FloatType> lhs(value), rhs(rhs_); 1316e35fdd936d133bf8a48de140a3c666897588a05shiqian 1317e35fdd936d133bf8a48de140a3c666897588a05shiqian // Compares NaNs first, if nan_eq_nan_ is true. 1318e35fdd936d133bf8a48de140a3c666897588a05shiqian if (nan_eq_nan_ && lhs.is_nan()) { 1319e35fdd936d133bf8a48de140a3c666897588a05shiqian return rhs.is_nan(); 1320e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1321e35fdd936d133bf8a48de140a3c666897588a05shiqian 1322e35fdd936d133bf8a48de140a3c666897588a05shiqian return lhs.AlmostEquals(rhs); 1323e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1324e35fdd936d133bf8a48de140a3c666897588a05shiqian 1325e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeTo(::std::ostream* os) const { 1326e35fdd936d133bf8a48de140a3c666897588a05shiqian // os->precision() returns the previously set precision, which we 1327e35fdd936d133bf8a48de140a3c666897588a05shiqian // store to restore the ostream to its original configuration 1328e35fdd936d133bf8a48de140a3c666897588a05shiqian // after outputting. 1329e35fdd936d133bf8a48de140a3c666897588a05shiqian const ::std::streamsize old_precision = os->precision( 1330e35fdd936d133bf8a48de140a3c666897588a05shiqian ::std::numeric_limits<FloatType>::digits10 + 2); 1331e35fdd936d133bf8a48de140a3c666897588a05shiqian if (FloatingPoint<FloatType>(rhs_).is_nan()) { 1332e35fdd936d133bf8a48de140a3c666897588a05shiqian if (nan_eq_nan_) { 1333e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "is NaN"; 1334e35fdd936d133bf8a48de140a3c666897588a05shiqian } else { 1335e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "never matches"; 1336e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1337e35fdd936d133bf8a48de140a3c666897588a05shiqian } else { 1338e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "is approximately " << rhs_; 1339e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1340e35fdd936d133bf8a48de140a3c666897588a05shiqian os->precision(old_precision); 1341e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1342e35fdd936d133bf8a48de140a3c666897588a05shiqian 1343e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeNegationTo(::std::ostream* os) const { 1344e35fdd936d133bf8a48de140a3c666897588a05shiqian // As before, get original precision. 1345e35fdd936d133bf8a48de140a3c666897588a05shiqian const ::std::streamsize old_precision = os->precision( 1346e35fdd936d133bf8a48de140a3c666897588a05shiqian ::std::numeric_limits<FloatType>::digits10 + 2); 1347e35fdd936d133bf8a48de140a3c666897588a05shiqian if (FloatingPoint<FloatType>(rhs_).is_nan()) { 1348e35fdd936d133bf8a48de140a3c666897588a05shiqian if (nan_eq_nan_) { 1349e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "is not NaN"; 1350e35fdd936d133bf8a48de140a3c666897588a05shiqian } else { 1351e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "is anything"; 1352e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1353e35fdd936d133bf8a48de140a3c666897588a05shiqian } else { 1354e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "is not approximately " << rhs_; 1355e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1356e35fdd936d133bf8a48de140a3c666897588a05shiqian // Restore original precision. 1357e35fdd936d133bf8a48de140a3c666897588a05shiqian os->precision(old_precision); 1358e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1359e35fdd936d133bf8a48de140a3c666897588a05shiqian 1360e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1361e35fdd936d133bf8a48de140a3c666897588a05shiqian const FloatType rhs_; 1362e35fdd936d133bf8a48de140a3c666897588a05shiqian const bool nan_eq_nan_; 1363e35fdd936d133bf8a48de140a3c666897588a05shiqian }; 1364e35fdd936d133bf8a48de140a3c666897588a05shiqian 1365e35fdd936d133bf8a48de140a3c666897588a05shiqian // The following 3 type conversion operators allow FloatEq(rhs) and 1366e35fdd936d133bf8a48de140a3c666897588a05shiqian // NanSensitiveFloatEq(rhs) to be used as a Matcher<float>, a 1367e35fdd936d133bf8a48de140a3c666897588a05shiqian // Matcher<const float&>, or a Matcher<float&>, but nothing else. 1368e35fdd936d133bf8a48de140a3c666897588a05shiqian // (While Google's C++ coding style doesn't allow arguments passed 1369e35fdd936d133bf8a48de140a3c666897588a05shiqian // by non-const reference, we may see them in code not conforming to 1370e35fdd936d133bf8a48de140a3c666897588a05shiqian // the style. Therefore Google Mock needs to support them.) 1371e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<FloatType>() const { 1372e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakeMatcher(new Impl<FloatType>(rhs_, nan_eq_nan_)); 1373e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1374e35fdd936d133bf8a48de140a3c666897588a05shiqian 1375e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<const FloatType&>() const { 1376e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakeMatcher(new Impl<const FloatType&>(rhs_, nan_eq_nan_)); 1377e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1378e35fdd936d133bf8a48de140a3c666897588a05shiqian 1379e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<FloatType&>() const { 1380e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakeMatcher(new Impl<FloatType&>(rhs_, nan_eq_nan_)); 1381e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1382e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1383e35fdd936d133bf8a48de140a3c666897588a05shiqian const FloatType rhs_; 1384e35fdd936d133bf8a48de140a3c666897588a05shiqian const bool nan_eq_nan_; 1385e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1386e35fdd936d133bf8a48de140a3c666897588a05shiqian 1387e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the Pointee(m) matcher for matching a pointer whose 1388e35fdd936d133bf8a48de140a3c666897588a05shiqian// pointee matches matcher m. The pointer can be either raw or smart. 1389e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename InnerMatcher> 1390e35fdd936d133bf8a48de140a3c666897588a05shiqianclass PointeeMatcher { 1391e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1392e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit PointeeMatcher(const InnerMatcher& matcher) : matcher_(matcher) {} 1393e35fdd936d133bf8a48de140a3c666897588a05shiqian 1394e35fdd936d133bf8a48de140a3c666897588a05shiqian // This type conversion operator template allows Pointee(m) to be 1395e35fdd936d133bf8a48de140a3c666897588a05shiqian // used as a matcher for any pointer type whose pointee type is 1396e35fdd936d133bf8a48de140a3c666897588a05shiqian // compatible with the inner matcher, where type Pointer can be 1397e35fdd936d133bf8a48de140a3c666897588a05shiqian // either a raw pointer or a smart pointer. 1398e35fdd936d133bf8a48de140a3c666897588a05shiqian // 1399e35fdd936d133bf8a48de140a3c666897588a05shiqian // The reason we do this instead of relying on 1400e35fdd936d133bf8a48de140a3c666897588a05shiqian // MakePolymorphicMatcher() is that the latter is not flexible 1401e35fdd936d133bf8a48de140a3c666897588a05shiqian // enough for implementing the DescribeTo() method of Pointee(). 1402e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename Pointer> 1403e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<Pointer>() const { 1404e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakeMatcher(new Impl<Pointer>(matcher_)); 1405e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1406e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1407e35fdd936d133bf8a48de140a3c666897588a05shiqian // The monomorphic implementation that works for a particular pointer type. 1408e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename Pointer> 1409e35fdd936d133bf8a48de140a3c666897588a05shiqian class Impl : public MatcherInterface<Pointer> { 1410e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1411e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan typedef typename PointeeOf<GMOCK_REMOVE_CONST_( // NOLINT 1412e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan GMOCK_REMOVE_REFERENCE_(Pointer))>::type Pointee; 1413e35fdd936d133bf8a48de140a3c666897588a05shiqian 1414e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit Impl(const InnerMatcher& matcher) 1415e35fdd936d133bf8a48de140a3c666897588a05shiqian : matcher_(MatcherCast<const Pointee&>(matcher)) {} 1416e35fdd936d133bf8a48de140a3c666897588a05shiqian 1417e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual bool Matches(Pointer p) const { 1418e35fdd936d133bf8a48de140a3c666897588a05shiqian return GetRawPointer(p) != NULL && matcher_.Matches(*p); 1419e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1420e35fdd936d133bf8a48de140a3c666897588a05shiqian 1421e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeTo(::std::ostream* os) const { 1422e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "points to a value that "; 1423e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher_.DescribeTo(os); 1424e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1425e35fdd936d133bf8a48de140a3c666897588a05shiqian 1426e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeNegationTo(::std::ostream* os) const { 1427e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "does not point to a value that "; 1428e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher_.DescribeTo(os); 1429e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1430e35fdd936d133bf8a48de140a3c666897588a05shiqian 1431e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void ExplainMatchResultTo(Pointer pointer, 1432e35fdd936d133bf8a48de140a3c666897588a05shiqian ::std::ostream* os) const { 1433e35fdd936d133bf8a48de140a3c666897588a05shiqian if (GetRawPointer(pointer) == NULL) 1434e35fdd936d133bf8a48de140a3c666897588a05shiqian return; 1435e35fdd936d133bf8a48de140a3c666897588a05shiqian 1436e35fdd936d133bf8a48de140a3c666897588a05shiqian ::std::stringstream ss; 1437e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher_.ExplainMatchResultTo(*pointer, &ss); 1438e35fdd936d133bf8a48de140a3c666897588a05shiqian const internal::string s = ss.str(); 1439e35fdd936d133bf8a48de140a3c666897588a05shiqian if (s != "") { 1440e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "points to a value that " << s; 1441e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1442e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1443e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1444e35fdd936d133bf8a48de140a3c666897588a05shiqian const Matcher<const Pointee&> matcher_; 1445e35fdd936d133bf8a48de140a3c666897588a05shiqian }; 1446e35fdd936d133bf8a48de140a3c666897588a05shiqian 1447e35fdd936d133bf8a48de140a3c666897588a05shiqian const InnerMatcher matcher_; 1448e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1449e35fdd936d133bf8a48de140a3c666897588a05shiqian 1450e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the Field() matcher for matching a field (i.e. member 1451e35fdd936d133bf8a48de140a3c666897588a05shiqian// variable) of an object. 1452e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Class, typename FieldType> 1453e35fdd936d133bf8a48de140a3c666897588a05shiqianclass FieldMatcher { 1454e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1455e35fdd936d133bf8a48de140a3c666897588a05shiqian FieldMatcher(FieldType Class::*field, 1456e35fdd936d133bf8a48de140a3c666897588a05shiqian const Matcher<const FieldType&>& matcher) 1457e35fdd936d133bf8a48de140a3c666897588a05shiqian : field_(field), matcher_(matcher) {} 1458e35fdd936d133bf8a48de140a3c666897588a05shiqian 1459e35fdd936d133bf8a48de140a3c666897588a05shiqian // Returns true iff the inner matcher matches obj.field. 1460e35fdd936d133bf8a48de140a3c666897588a05shiqian bool Matches(const Class& obj) const { 1461e35fdd936d133bf8a48de140a3c666897588a05shiqian return matcher_.Matches(obj.*field_); 1462e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1463e35fdd936d133bf8a48de140a3c666897588a05shiqian 1464e35fdd936d133bf8a48de140a3c666897588a05shiqian // Returns true iff the inner matcher matches obj->field. 1465e35fdd936d133bf8a48de140a3c666897588a05shiqian bool Matches(const Class* p) const { 1466e35fdd936d133bf8a48de140a3c666897588a05shiqian return (p != NULL) && matcher_.Matches(p->*field_); 1467e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1468e35fdd936d133bf8a48de140a3c666897588a05shiqian 1469e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeTo(::std::ostream* os) const { 1470e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "the given field "; 1471e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher_.DescribeTo(os); 1472e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1473e35fdd936d133bf8a48de140a3c666897588a05shiqian 1474e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeNegationTo(::std::ostream* os) const { 1475e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "the given field "; 1476e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher_.DescribeNegationTo(os); 1477e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1478e35fdd936d133bf8a48de140a3c666897588a05shiqian 147918490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan // The first argument of ExplainMatchResultTo() is needed to help 148018490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan // Symbian's C++ compiler choose which overload to use. Its type is 148118490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan // true_type iff the Field() matcher is used to match a pointer. 148218490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan void ExplainMatchResultTo(false_type /* is_not_pointer */, const Class& obj, 148318490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan ::std::ostream* os) const { 1484e35fdd936d133bf8a48de140a3c666897588a05shiqian ::std::stringstream ss; 1485e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher_.ExplainMatchResultTo(obj.*field_, &ss); 1486e35fdd936d133bf8a48de140a3c666897588a05shiqian const internal::string s = ss.str(); 1487e35fdd936d133bf8a48de140a3c666897588a05shiqian if (s != "") { 1488e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "the given field " << s; 1489e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1490e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1491e35fdd936d133bf8a48de140a3c666897588a05shiqian 149218490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan void ExplainMatchResultTo(true_type /* is_pointer */, const Class* p, 149318490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan ::std::ostream* os) const { 1494e35fdd936d133bf8a48de140a3c666897588a05shiqian if (p != NULL) { 149518490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan // Since *p has a field, it must be a class/struct/union type 149618490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan // and thus cannot be a pointer. Therefore we pass false_type() 149718490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan // as the first argument. 149818490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan ExplainMatchResultTo(false_type(), *p, os); 1499e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1500e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1501e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1502e35fdd936d133bf8a48de140a3c666897588a05shiqian const FieldType Class::*field_; 1503e35fdd936d133bf8a48de140a3c666897588a05shiqian const Matcher<const FieldType&> matcher_; 1504e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1505e35fdd936d133bf8a48de140a3c666897588a05shiqian 150618490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// Explains the result of matching an object or pointer against a field matcher. 150718490653e80d484b4650d8799184fd1e021efc7bzhanyong.wantemplate <typename Class, typename FieldType, typename T> 1508e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid ExplainMatchResultTo(const FieldMatcher<Class, FieldType>& matcher, 150918490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan const T& value, ::std::ostream* os) { 151018490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan matcher.ExplainMatchResultTo( 151118490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan typename ::testing::internal::is_pointer<T>::type(), value, os); 1512e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1513e35fdd936d133bf8a48de140a3c666897588a05shiqian 1514e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the Property() matcher for matching a property 1515e35fdd936d133bf8a48de140a3c666897588a05shiqian// (i.e. return value of a getter method) of an object. 1516e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Class, typename PropertyType> 1517e35fdd936d133bf8a48de140a3c666897588a05shiqianclass PropertyMatcher { 1518e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1519e35fdd936d133bf8a48de140a3c666897588a05shiqian // The property may have a reference type, so 'const PropertyType&' 1520e35fdd936d133bf8a48de140a3c666897588a05shiqian // may cause double references and fail to compile. That's why we 1521e35fdd936d133bf8a48de140a3c666897588a05shiqian // need GMOCK_REFERENCE_TO_CONST, which works regardless of 1522e35fdd936d133bf8a48de140a3c666897588a05shiqian // PropertyType being a reference or not. 1523e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan typedef GMOCK_REFERENCE_TO_CONST_(PropertyType) RefToConstProperty; 1524e35fdd936d133bf8a48de140a3c666897588a05shiqian 1525e35fdd936d133bf8a48de140a3c666897588a05shiqian PropertyMatcher(PropertyType (Class::*property)() const, 1526e35fdd936d133bf8a48de140a3c666897588a05shiqian const Matcher<RefToConstProperty>& matcher) 1527e35fdd936d133bf8a48de140a3c666897588a05shiqian : property_(property), matcher_(matcher) {} 1528e35fdd936d133bf8a48de140a3c666897588a05shiqian 1529e35fdd936d133bf8a48de140a3c666897588a05shiqian // Returns true iff obj.property() matches the inner matcher. 1530e35fdd936d133bf8a48de140a3c666897588a05shiqian bool Matches(const Class& obj) const { 1531e35fdd936d133bf8a48de140a3c666897588a05shiqian return matcher_.Matches((obj.*property_)()); 1532e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1533e35fdd936d133bf8a48de140a3c666897588a05shiqian 1534e35fdd936d133bf8a48de140a3c666897588a05shiqian // Returns true iff p->property() matches the inner matcher. 1535e35fdd936d133bf8a48de140a3c666897588a05shiqian bool Matches(const Class* p) const { 1536e35fdd936d133bf8a48de140a3c666897588a05shiqian return (p != NULL) && matcher_.Matches((p->*property_)()); 1537e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1538e35fdd936d133bf8a48de140a3c666897588a05shiqian 1539e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeTo(::std::ostream* os) const { 1540e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "the given property "; 1541e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher_.DescribeTo(os); 1542e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1543e35fdd936d133bf8a48de140a3c666897588a05shiqian 1544e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeNegationTo(::std::ostream* os) const { 1545e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "the given property "; 1546e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher_.DescribeNegationTo(os); 1547e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1548e35fdd936d133bf8a48de140a3c666897588a05shiqian 154918490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan // The first argument of ExplainMatchResultTo() is needed to help 155018490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan // Symbian's C++ compiler choose which overload to use. Its type is 155118490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan // true_type iff the Property() matcher is used to match a pointer. 155218490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan void ExplainMatchResultTo(false_type /* is_not_pointer */, const Class& obj, 155318490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan ::std::ostream* os) const { 1554e35fdd936d133bf8a48de140a3c666897588a05shiqian ::std::stringstream ss; 1555e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher_.ExplainMatchResultTo((obj.*property_)(), &ss); 1556e35fdd936d133bf8a48de140a3c666897588a05shiqian const internal::string s = ss.str(); 1557e35fdd936d133bf8a48de140a3c666897588a05shiqian if (s != "") { 1558e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "the given property " << s; 1559e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1560e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1561e35fdd936d133bf8a48de140a3c666897588a05shiqian 156218490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan void ExplainMatchResultTo(true_type /* is_pointer */, const Class* p, 156318490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan ::std::ostream* os) const { 1564e35fdd936d133bf8a48de140a3c666897588a05shiqian if (p != NULL) { 156518490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan // Since *p has a property method, it must be a 156618490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan // class/struct/union type and thus cannot be a pointer. 156718490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan // Therefore we pass false_type() as the first argument. 156818490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan ExplainMatchResultTo(false_type(), *p, os); 1569e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1570e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1571e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1572e35fdd936d133bf8a48de140a3c666897588a05shiqian PropertyType (Class::*property_)() const; 1573e35fdd936d133bf8a48de140a3c666897588a05shiqian const Matcher<RefToConstProperty> matcher_; 1574e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1575e35fdd936d133bf8a48de140a3c666897588a05shiqian 157618490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// Explains the result of matching an object or pointer against a 157718490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// property matcher. 157818490653e80d484b4650d8799184fd1e021efc7bzhanyong.wantemplate <typename Class, typename PropertyType, typename T> 1579e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid ExplainMatchResultTo(const PropertyMatcher<Class, PropertyType>& matcher, 158018490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan const T& value, ::std::ostream* os) { 158118490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan matcher.ExplainMatchResultTo( 158218490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan typename ::testing::internal::is_pointer<T>::type(), value, os); 1583e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1584e35fdd936d133bf8a48de140a3c666897588a05shiqian 1585e35fdd936d133bf8a48de140a3c666897588a05shiqian// Type traits specifying various features of different functors for ResultOf. 1586e35fdd936d133bf8a48de140a3c666897588a05shiqian// The default template specifies features for functor objects. 1587e35fdd936d133bf8a48de140a3c666897588a05shiqian// Functor classes have to typedef argument_type and result_type 1588e35fdd936d133bf8a48de140a3c666897588a05shiqian// to be compatible with ResultOf. 1589e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Functor> 1590e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct CallableTraits { 1591e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef typename Functor::result_type ResultType; 1592e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef Functor StorageType; 1593e35fdd936d133bf8a48de140a3c666897588a05shiqian 1594e35fdd936d133bf8a48de140a3c666897588a05shiqian static void CheckIsValid(Functor functor) {} 1595e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 1596e35fdd936d133bf8a48de140a3c666897588a05shiqian static ResultType Invoke(Functor f, T arg) { return f(arg); } 1597e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1598e35fdd936d133bf8a48de140a3c666897588a05shiqian 1599e35fdd936d133bf8a48de140a3c666897588a05shiqian// Specialization for function pointers. 1600e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename ArgType, typename ResType> 1601e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct CallableTraits<ResType(*)(ArgType)> { 1602e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef ResType ResultType; 1603e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef ResType(*StorageType)(ArgType); 1604e35fdd936d133bf8a48de140a3c666897588a05shiqian 1605e35fdd936d133bf8a48de140a3c666897588a05shiqian static void CheckIsValid(ResType(*f)(ArgType)) { 1606e35fdd936d133bf8a48de140a3c666897588a05shiqian GMOCK_CHECK_(f != NULL) 1607e35fdd936d133bf8a48de140a3c666897588a05shiqian << "NULL function pointer is passed into ResultOf()."; 1608e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1609e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 1610e35fdd936d133bf8a48de140a3c666897588a05shiqian static ResType Invoke(ResType(*f)(ArgType), T arg) { 1611e35fdd936d133bf8a48de140a3c666897588a05shiqian return (*f)(arg); 1612e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1613e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1614e35fdd936d133bf8a48de140a3c666897588a05shiqian 1615e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the ResultOf() matcher for matching a return value of a 1616e35fdd936d133bf8a48de140a3c666897588a05shiqian// unary function of an object. 1617e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Callable> 1618e35fdd936d133bf8a48de140a3c666897588a05shiqianclass ResultOfMatcher { 1619e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1620e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef typename CallableTraits<Callable>::ResultType ResultType; 1621e35fdd936d133bf8a48de140a3c666897588a05shiqian 1622e35fdd936d133bf8a48de140a3c666897588a05shiqian ResultOfMatcher(Callable callable, const Matcher<ResultType>& matcher) 1623e35fdd936d133bf8a48de140a3c666897588a05shiqian : callable_(callable), matcher_(matcher) { 1624e35fdd936d133bf8a48de140a3c666897588a05shiqian CallableTraits<Callable>::CheckIsValid(callable_); 1625e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1626e35fdd936d133bf8a48de140a3c666897588a05shiqian 1627e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 1628e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<T>() const { 1629e35fdd936d133bf8a48de140a3c666897588a05shiqian return Matcher<T>(new Impl<T>(callable_, matcher_)); 1630e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1631e35fdd936d133bf8a48de140a3c666897588a05shiqian 1632e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1633e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef typename CallableTraits<Callable>::StorageType CallableStorageType; 1634e35fdd936d133bf8a48de140a3c666897588a05shiqian 1635e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 1636e35fdd936d133bf8a48de140a3c666897588a05shiqian class Impl : public MatcherInterface<T> { 1637e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1638e35fdd936d133bf8a48de140a3c666897588a05shiqian Impl(CallableStorageType callable, const Matcher<ResultType>& matcher) 1639e35fdd936d133bf8a48de140a3c666897588a05shiqian : callable_(callable), matcher_(matcher) {} 1640e35fdd936d133bf8a48de140a3c666897588a05shiqian // Returns true iff callable_(obj) matches the inner matcher. 1641e35fdd936d133bf8a48de140a3c666897588a05shiqian // The calling syntax is different for different types of callables 1642e35fdd936d133bf8a48de140a3c666897588a05shiqian // so we abstract it in CallableTraits<Callable>::Invoke(). 1643e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual bool Matches(T obj) const { 1644e35fdd936d133bf8a48de140a3c666897588a05shiqian return matcher_.Matches( 1645e35fdd936d133bf8a48de140a3c666897588a05shiqian CallableTraits<Callable>::template Invoke<T>(callable_, obj)); 1646e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1647e35fdd936d133bf8a48de140a3c666897588a05shiqian 1648e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeTo(::std::ostream* os) const { 1649e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "result of the given callable "; 1650e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher_.DescribeTo(os); 1651e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1652e35fdd936d133bf8a48de140a3c666897588a05shiqian 1653e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeNegationTo(::std::ostream* os) const { 1654e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "result of the given callable "; 1655e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher_.DescribeNegationTo(os); 1656e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1657e35fdd936d133bf8a48de140a3c666897588a05shiqian 1658e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void ExplainMatchResultTo(T obj, ::std::ostream* os) const { 1659e35fdd936d133bf8a48de140a3c666897588a05shiqian ::std::stringstream ss; 1660e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher_.ExplainMatchResultTo( 1661e35fdd936d133bf8a48de140a3c666897588a05shiqian CallableTraits<Callable>::template Invoke<T>(callable_, obj), 1662e35fdd936d133bf8a48de140a3c666897588a05shiqian &ss); 1663e35fdd936d133bf8a48de140a3c666897588a05shiqian const internal::string s = ss.str(); 1664e35fdd936d133bf8a48de140a3c666897588a05shiqian if (s != "") 1665e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "result of the given callable " << s; 1666e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1667e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1668e35fdd936d133bf8a48de140a3c666897588a05shiqian // Functors often define operator() as non-const method even though 1669e35fdd936d133bf8a48de140a3c666897588a05shiqian // they are actualy stateless. But we need to use them even when 1670e35fdd936d133bf8a48de140a3c666897588a05shiqian // 'this' is a const pointer. It's the user's responsibility not to 1671e35fdd936d133bf8a48de140a3c666897588a05shiqian // use stateful callables with ResultOf(), which does't guarantee 1672e35fdd936d133bf8a48de140a3c666897588a05shiqian // how many times the callable will be invoked. 1673e35fdd936d133bf8a48de140a3c666897588a05shiqian mutable CallableStorageType callable_; 1674e35fdd936d133bf8a48de140a3c666897588a05shiqian const Matcher<ResultType> matcher_; 1675e35fdd936d133bf8a48de140a3c666897588a05shiqian }; // class Impl 1676e35fdd936d133bf8a48de140a3c666897588a05shiqian 1677e35fdd936d133bf8a48de140a3c666897588a05shiqian const CallableStorageType callable_; 1678e35fdd936d133bf8a48de140a3c666897588a05shiqian const Matcher<ResultType> matcher_; 1679e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1680e35fdd936d133bf8a48de140a3c666897588a05shiqian 1681e35fdd936d133bf8a48de140a3c666897588a05shiqian// Explains the result of matching a value against a functor matcher. 1682e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T, typename Callable> 1683e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid ExplainMatchResultTo(const ResultOfMatcher<Callable>& matcher, 1684e35fdd936d133bf8a48de140a3c666897588a05shiqian T obj, ::std::ostream* os) { 1685e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher.ExplainMatchResultTo(obj, os); 1686e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1687e35fdd936d133bf8a48de140a3c666897588a05shiqian 16886a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// Implements an equality matcher for any STL-style container whose elements 16896a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// support ==. This matcher is like Eq(), but its failure explanations provide 16906a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// more detailed information that is useful when the container is used as a set. 16916a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// The failure message reports elements that are in one of the operands but not 16926a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// the other. The failure messages do not report duplicate or out-of-order 16936a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// elements in the containers (which don't properly matter to sets, but can 16946a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// occur if the containers are vectors or lists, for example). 16956a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// 16966a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// Uses the container's const_iterator, value_type, operator ==, 16976a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// begin(), and end(). 16986a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wantemplate <typename Container> 16996a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wanclass ContainerEqMatcher { 17006a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan public: 17016a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan explicit ContainerEqMatcher(const Container& rhs) : rhs_(rhs) {} 17026a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan bool Matches(const Container& lhs) const { return lhs == rhs_; } 17036a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan void DescribeTo(::std::ostream* os) const { 17046a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan *os << "equals "; 17056a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan UniversalPrinter<Container>::Print(rhs_, os); 17066a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan } 17076a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan void DescribeNegationTo(::std::ostream* os) const { 17086a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan *os << "does not equal "; 17096a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan UniversalPrinter<Container>::Print(rhs_, os); 17106a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan } 17116a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan 17126a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan void ExplainMatchResultTo(const Container& lhs, 17136a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan ::std::ostream* os) const { 17146a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan // Something is different. Check for missing values first. 17156a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan bool printed_header = false; 17166a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan for (typename Container::const_iterator it = lhs.begin(); 17176a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan it != lhs.end(); ++it) { 17186a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan if (std::find(rhs_.begin(), rhs_.end(), *it) == rhs_.end()) { 17196a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan if (printed_header) { 17206a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan *os << ", "; 17216a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan } else { 17226a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan *os << "Only in actual: "; 17236a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan printed_header = true; 17246a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan } 17256a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan UniversalPrinter<typename Container::value_type>::Print(*it, os); 17266a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan } 17276a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan } 17286a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan 17296a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan // Now check for extra values. 17306a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan bool printed_header2 = false; 17316a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan for (typename Container::const_iterator it = rhs_.begin(); 17326a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan it != rhs_.end(); ++it) { 17336a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan if (std::find(lhs.begin(), lhs.end(), *it) == lhs.end()) { 17346a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan if (printed_header2) { 17356a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan *os << ", "; 17366a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan } else { 17376a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan *os << (printed_header ? "; not" : "Not") << " in actual: "; 17386a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan printed_header2 = true; 17396a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan } 17406a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan UniversalPrinter<typename Container::value_type>::Print(*it, os); 17416a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan } 17426a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan } 17436a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan } 17446a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan private: 17456a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan const Container rhs_; 17466a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan}; 17476a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan 17486a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wantemplate <typename Container> 17496a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wanvoid ExplainMatchResultTo(const ContainerEqMatcher<Container>& matcher, 17506a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan const Container& lhs, 17516a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan ::std::ostream* os) { 17526a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan matcher.ExplainMatchResultTo(lhs, os); 17536a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan} 17546a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan 1755e35fdd936d133bf8a48de140a3c666897588a05shiqian} // namespace internal 1756e35fdd936d133bf8a48de140a3c666897588a05shiqian 1757e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements MatcherCast(). 1758e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T, typename M> 1759e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<T> MatcherCast(M matcher) { 1760e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::MatcherCastImpl<T, M>::Cast(matcher); 1761e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1762e35fdd936d133bf8a48de140a3c666897588a05shiqian 1763e35fdd936d133bf8a48de140a3c666897588a05shiqian// _ is a matcher that matches anything of any type. 1764e35fdd936d133bf8a48de140a3c666897588a05shiqian// 1765e35fdd936d133bf8a48de140a3c666897588a05shiqian// This definition is fine as: 1766e35fdd936d133bf8a48de140a3c666897588a05shiqian// 1767e35fdd936d133bf8a48de140a3c666897588a05shiqian// 1. The C++ standard permits using the name _ in a namespace that 1768e35fdd936d133bf8a48de140a3c666897588a05shiqian// is not the global namespace or ::std. 1769e35fdd936d133bf8a48de140a3c666897588a05shiqian// 2. The AnythingMatcher class has no data member or constructor, 1770e35fdd936d133bf8a48de140a3c666897588a05shiqian// so it's OK to create global variables of this type. 1771e35fdd936d133bf8a48de140a3c666897588a05shiqian// 3. c-style has approved of using _ in this case. 1772e35fdd936d133bf8a48de140a3c666897588a05shiqianconst internal::AnythingMatcher _ = {}; 1773e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any value of the given type T. 1774e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 1775e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<T> A() { return MakeMatcher(new internal::AnyMatcherImpl<T>()); } 1776e35fdd936d133bf8a48de140a3c666897588a05shiqian 1777e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any value of the given type T. 1778e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 1779e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<T> An() { return A<T>(); } 1780e35fdd936d133bf8a48de140a3c666897588a05shiqian 1781e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything equal to x. 1782e35fdd936d133bf8a48de140a3c666897588a05shiqian// Note: if the parameter of Eq() were declared as const T&, Eq("foo") 1783e35fdd936d133bf8a48de140a3c666897588a05shiqian// wouldn't compile. 1784e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 1785e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::EqMatcher<T> Eq(T x) { return internal::EqMatcher<T>(x); } 1786e35fdd936d133bf8a48de140a3c666897588a05shiqian 1787e35fdd936d133bf8a48de140a3c666897588a05shiqian// Constructs a Matcher<T> from a 'value' of type T. The constructed 1788e35fdd936d133bf8a48de140a3c666897588a05shiqian// matcher matches any value that's equal to 'value'. 1789e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 1790e35fdd936d133bf8a48de140a3c666897588a05shiqianMatcher<T>::Matcher(T value) { *this = Eq(value); } 1791e35fdd936d133bf8a48de140a3c666897588a05shiqian 1792e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a monomorphic matcher that matches anything with type Lhs 1793e35fdd936d133bf8a48de140a3c666897588a05shiqian// and equal to rhs. A user may need to use this instead of Eq(...) 1794e35fdd936d133bf8a48de140a3c666897588a05shiqian// in order to resolve an overloading ambiguity. 1795e35fdd936d133bf8a48de140a3c666897588a05shiqian// 1796e35fdd936d133bf8a48de140a3c666897588a05shiqian// TypedEq<T>(x) is just a convenient short-hand for Matcher<T>(Eq(x)) 1797e35fdd936d133bf8a48de140a3c666897588a05shiqian// or Matcher<T>(x), but more readable than the latter. 1798e35fdd936d133bf8a48de140a3c666897588a05shiqian// 1799e35fdd936d133bf8a48de140a3c666897588a05shiqian// We could define similar monomorphic matchers for other comparison 1800e35fdd936d133bf8a48de140a3c666897588a05shiqian// operations (e.g. TypedLt, TypedGe, and etc), but decided not to do 1801e35fdd936d133bf8a48de140a3c666897588a05shiqian// it yet as those are used much less than Eq() in practice. A user 1802e35fdd936d133bf8a48de140a3c666897588a05shiqian// can always write Matcher<T>(Lt(5)) to be explicit about the type, 1803e35fdd936d133bf8a48de140a3c666897588a05shiqian// for example. 1804e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Lhs, typename Rhs> 1805e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<Lhs> TypedEq(const Rhs& rhs) { return Eq(rhs); } 1806e35fdd936d133bf8a48de140a3c666897588a05shiqian 1807e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything >= x. 1808e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs> 1809e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::GeMatcher<Rhs> Ge(Rhs x) { 1810e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::GeMatcher<Rhs>(x); 1811e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1812e35fdd936d133bf8a48de140a3c666897588a05shiqian 1813e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything > x. 1814e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs> 1815e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::GtMatcher<Rhs> Gt(Rhs x) { 1816e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::GtMatcher<Rhs>(x); 1817e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1818e35fdd936d133bf8a48de140a3c666897588a05shiqian 1819e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything <= x. 1820e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs> 1821e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::LeMatcher<Rhs> Le(Rhs x) { 1822e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::LeMatcher<Rhs>(x); 1823e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1824e35fdd936d133bf8a48de140a3c666897588a05shiqian 1825e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything < x. 1826e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs> 1827e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::LtMatcher<Rhs> Lt(Rhs x) { 1828e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::LtMatcher<Rhs>(x); 1829e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1830e35fdd936d133bf8a48de140a3c666897588a05shiqian 1831e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything != x. 1832e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs> 1833e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::NeMatcher<Rhs> Ne(Rhs x) { 1834e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::NeMatcher<Rhs>(x); 1835e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1836e35fdd936d133bf8a48de140a3c666897588a05shiqian 1837e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches any non-NULL pointer. 1838e35fdd936d133bf8a48de140a3c666897588a05shiqian// This is convenient as Not(NULL) doesn't compile (the compiler 1839e35fdd936d133bf8a48de140a3c666897588a05shiqian// thinks that that expression is comparing a pointer with an integer). 1840e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::NotNullMatcher > NotNull() { 1841e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::NotNullMatcher()); 1842e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1843e35fdd936d133bf8a48de140a3c666897588a05shiqian 1844e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches any argument that 1845e35fdd936d133bf8a48de140a3c666897588a05shiqian// references variable x. 1846e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 1847e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::RefMatcher<T&> Ref(T& x) { // NOLINT 1848e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::RefMatcher<T&>(x); 1849e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1850e35fdd936d133bf8a48de140a3c666897588a05shiqian 1851e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any double argument approximately 1852e35fdd936d133bf8a48de140a3c666897588a05shiqian// equal to rhs, where two NANs are considered unequal. 1853e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::FloatingEqMatcher<double> DoubleEq(double rhs) { 1854e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::FloatingEqMatcher<double>(rhs, false); 1855e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1856e35fdd936d133bf8a48de140a3c666897588a05shiqian 1857e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any double argument approximately 1858e35fdd936d133bf8a48de140a3c666897588a05shiqian// equal to rhs, including NaN values when rhs is NaN. 1859e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::FloatingEqMatcher<double> NanSensitiveDoubleEq(double rhs) { 1860e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::FloatingEqMatcher<double>(rhs, true); 1861e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1862e35fdd936d133bf8a48de140a3c666897588a05shiqian 1863e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any float argument approximately 1864e35fdd936d133bf8a48de140a3c666897588a05shiqian// equal to rhs, where two NANs are considered unequal. 1865e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::FloatingEqMatcher<float> FloatEq(float rhs) { 1866e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::FloatingEqMatcher<float>(rhs, false); 1867e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1868e35fdd936d133bf8a48de140a3c666897588a05shiqian 1869e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any double argument approximately 1870e35fdd936d133bf8a48de140a3c666897588a05shiqian// equal to rhs, including NaN values when rhs is NaN. 1871e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::FloatingEqMatcher<float> NanSensitiveFloatEq(float rhs) { 1872e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::FloatingEqMatcher<float>(rhs, true); 1873e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1874e35fdd936d133bf8a48de140a3c666897588a05shiqian 1875e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches a pointer (raw or smart) that points 1876e35fdd936d133bf8a48de140a3c666897588a05shiqian// to a value that matches inner_matcher. 1877e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename InnerMatcher> 1878e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::PointeeMatcher<InnerMatcher> Pointee( 1879e35fdd936d133bf8a48de140a3c666897588a05shiqian const InnerMatcher& inner_matcher) { 1880e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::PointeeMatcher<InnerMatcher>(inner_matcher); 1881e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1882e35fdd936d133bf8a48de140a3c666897588a05shiqian 1883e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches an object whose given field matches 1884e35fdd936d133bf8a48de140a3c666897588a05shiqian// 'matcher'. For example, 1885e35fdd936d133bf8a48de140a3c666897588a05shiqian// Field(&Foo::number, Ge(5)) 1886e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a Foo object x iff x.number >= 5. 1887e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Class, typename FieldType, typename FieldMatcher> 1888e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher< 1889e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::FieldMatcher<Class, FieldType> > Field( 1890e35fdd936d133bf8a48de140a3c666897588a05shiqian FieldType Class::*field, const FieldMatcher& matcher) { 1891e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher( 1892e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::FieldMatcher<Class, FieldType>( 1893e35fdd936d133bf8a48de140a3c666897588a05shiqian field, MatcherCast<const FieldType&>(matcher))); 1894e35fdd936d133bf8a48de140a3c666897588a05shiqian // The call to MatcherCast() is required for supporting inner 1895e35fdd936d133bf8a48de140a3c666897588a05shiqian // matchers of compatible types. For example, it allows 1896e35fdd936d133bf8a48de140a3c666897588a05shiqian // Field(&Foo::bar, m) 1897e35fdd936d133bf8a48de140a3c666897588a05shiqian // to compile where bar is an int32 and m is a matcher for int64. 1898e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1899e35fdd936d133bf8a48de140a3c666897588a05shiqian 1900e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches an object whose given property 1901e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches 'matcher'. For example, 1902e35fdd936d133bf8a48de140a3c666897588a05shiqian// Property(&Foo::str, StartsWith("hi")) 1903e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a Foo object x iff x.str() starts with "hi". 1904e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Class, typename PropertyType, typename PropertyMatcher> 1905e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher< 1906e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::PropertyMatcher<Class, PropertyType> > Property( 1907e35fdd936d133bf8a48de140a3c666897588a05shiqian PropertyType (Class::*property)() const, const PropertyMatcher& matcher) { 1908e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher( 1909e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::PropertyMatcher<Class, PropertyType>( 1910e35fdd936d133bf8a48de140a3c666897588a05shiqian property, 1911e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan MatcherCast<GMOCK_REFERENCE_TO_CONST_(PropertyType)>(matcher))); 1912e35fdd936d133bf8a48de140a3c666897588a05shiqian // The call to MatcherCast() is required for supporting inner 1913e35fdd936d133bf8a48de140a3c666897588a05shiqian // matchers of compatible types. For example, it allows 1914e35fdd936d133bf8a48de140a3c666897588a05shiqian // Property(&Foo::bar, m) 1915e35fdd936d133bf8a48de140a3c666897588a05shiqian // to compile where bar() returns an int32 and m is a matcher for int64. 1916e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1917e35fdd936d133bf8a48de140a3c666897588a05shiqian 1918e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches an object iff the result of applying 1919e35fdd936d133bf8a48de140a3c666897588a05shiqian// a callable to x matches 'matcher'. 1920e35fdd936d133bf8a48de140a3c666897588a05shiqian// For example, 1921e35fdd936d133bf8a48de140a3c666897588a05shiqian// ResultOf(f, StartsWith("hi")) 1922e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a Foo object x iff f(x) starts with "hi". 1923e35fdd936d133bf8a48de140a3c666897588a05shiqian// callable parameter can be a function, function pointer, or a functor. 1924e35fdd936d133bf8a48de140a3c666897588a05shiqian// Callable has to satisfy the following conditions: 1925e35fdd936d133bf8a48de140a3c666897588a05shiqian// * It is required to keep no state affecting the results of 1926e35fdd936d133bf8a48de140a3c666897588a05shiqian// the calls on it and make no assumptions about how many calls 1927e35fdd936d133bf8a48de140a3c666897588a05shiqian// will be made. Any state it keeps must be protected from the 1928e35fdd936d133bf8a48de140a3c666897588a05shiqian// concurrent access. 1929e35fdd936d133bf8a48de140a3c666897588a05shiqian// * If it is a function object, it has to define type result_type. 1930e35fdd936d133bf8a48de140a3c666897588a05shiqian// We recommend deriving your functor classes from std::unary_function. 1931e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Callable, typename ResultOfMatcher> 1932e35fdd936d133bf8a48de140a3c666897588a05shiqianinternal::ResultOfMatcher<Callable> ResultOf( 1933e35fdd936d133bf8a48de140a3c666897588a05shiqian Callable callable, const ResultOfMatcher& matcher) { 1934e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::ResultOfMatcher<Callable>( 1935e35fdd936d133bf8a48de140a3c666897588a05shiqian callable, 1936e35fdd936d133bf8a48de140a3c666897588a05shiqian MatcherCast<typename internal::CallableTraits<Callable>::ResultType>( 1937e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher)); 1938e35fdd936d133bf8a48de140a3c666897588a05shiqian // The call to MatcherCast() is required for supporting inner 1939e35fdd936d133bf8a48de140a3c666897588a05shiqian // matchers of compatible types. For example, it allows 1940e35fdd936d133bf8a48de140a3c666897588a05shiqian // ResultOf(Function, m) 1941e35fdd936d133bf8a48de140a3c666897588a05shiqian // to compile where Function() returns an int32 and m is a matcher for int64. 1942e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1943e35fdd936d133bf8a48de140a3c666897588a05shiqian 1944e35fdd936d133bf8a48de140a3c666897588a05shiqian// String matchers. 1945e35fdd936d133bf8a48de140a3c666897588a05shiqian 1946e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string equal to str. 1947e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> > 1948e35fdd936d133bf8a48de140a3c666897588a05shiqian StrEq(const internal::string& str) { 1949e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>( 1950e35fdd936d133bf8a48de140a3c666897588a05shiqian str, true, true)); 1951e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1952e35fdd936d133bf8a48de140a3c666897588a05shiqian 1953e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string not equal to str. 1954e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> > 1955e35fdd936d133bf8a48de140a3c666897588a05shiqian StrNe(const internal::string& str) { 1956e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>( 1957e35fdd936d133bf8a48de140a3c666897588a05shiqian str, false, true)); 1958e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1959e35fdd936d133bf8a48de140a3c666897588a05shiqian 1960e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string equal to str, ignoring case. 1961e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> > 1962e35fdd936d133bf8a48de140a3c666897588a05shiqian StrCaseEq(const internal::string& str) { 1963e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>( 1964e35fdd936d133bf8a48de140a3c666897588a05shiqian str, true, false)); 1965e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1966e35fdd936d133bf8a48de140a3c666897588a05shiqian 1967e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string not equal to str, ignoring case. 1968e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> > 1969e35fdd936d133bf8a48de140a3c666897588a05shiqian StrCaseNe(const internal::string& str) { 1970e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>( 1971e35fdd936d133bf8a48de140a3c666897588a05shiqian str, false, false)); 1972e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1973e35fdd936d133bf8a48de140a3c666897588a05shiqian 1974e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any string, std::string, or C string 1975e35fdd936d133bf8a48de140a3c666897588a05shiqian// that contains the given substring. 1976e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::HasSubstrMatcher<internal::string> > 1977e35fdd936d133bf8a48de140a3c666897588a05shiqian HasSubstr(const internal::string& substring) { 1978e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::HasSubstrMatcher<internal::string>( 1979e35fdd936d133bf8a48de140a3c666897588a05shiqian substring)); 1980e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1981e35fdd936d133bf8a48de140a3c666897588a05shiqian 1982e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that starts with 'prefix' (case-sensitive). 1983e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StartsWithMatcher<internal::string> > 1984e35fdd936d133bf8a48de140a3c666897588a05shiqian StartsWith(const internal::string& prefix) { 1985e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::StartsWithMatcher<internal::string>( 1986e35fdd936d133bf8a48de140a3c666897588a05shiqian prefix)); 1987e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1988e35fdd936d133bf8a48de140a3c666897588a05shiqian 1989e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that ends with 'suffix' (case-sensitive). 1990e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::EndsWithMatcher<internal::string> > 1991e35fdd936d133bf8a48de140a3c666897588a05shiqian EndsWith(const internal::string& suffix) { 1992e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::EndsWithMatcher<internal::string>( 1993e35fdd936d133bf8a48de140a3c666897588a05shiqian suffix)); 1994e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1995e35fdd936d133bf8a48de140a3c666897588a05shiqian 1996e35fdd936d133bf8a48de140a3c666897588a05shiqian#ifdef GMOCK_HAS_REGEX 1997e35fdd936d133bf8a48de140a3c666897588a05shiqian 1998e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that fully matches regular expression 'regex'. 1999e35fdd936d133bf8a48de140a3c666897588a05shiqian// The matcher takes ownership of 'regex'. 2000e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex( 2001e35fdd936d133bf8a48de140a3c666897588a05shiqian const internal::RE* regex) { 2002e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, true)); 2003e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2004e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex( 2005e35fdd936d133bf8a48de140a3c666897588a05shiqian const internal::string& regex) { 2006e35fdd936d133bf8a48de140a3c666897588a05shiqian return MatchesRegex(new internal::RE(regex)); 2007e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2008e35fdd936d133bf8a48de140a3c666897588a05shiqian 2009e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that contains regular expression 'regex'. 2010e35fdd936d133bf8a48de140a3c666897588a05shiqian// The matcher takes ownership of 'regex'. 2011e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex( 2012e35fdd936d133bf8a48de140a3c666897588a05shiqian const internal::RE* regex) { 2013e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, false)); 2014e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2015e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex( 2016e35fdd936d133bf8a48de140a3c666897588a05shiqian const internal::string& regex) { 2017e35fdd936d133bf8a48de140a3c666897588a05shiqian return ContainsRegex(new internal::RE(regex)); 2018e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2019e35fdd936d133bf8a48de140a3c666897588a05shiqian 2020e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif // GMOCK_HAS_REGEX 2021e35fdd936d133bf8a48de140a3c666897588a05shiqian 2022e35fdd936d133bf8a48de140a3c666897588a05shiqian#if GTEST_HAS_GLOBAL_WSTRING || GTEST_HAS_STD_WSTRING 2023e35fdd936d133bf8a48de140a3c666897588a05shiqian// Wide string matchers. 2024e35fdd936d133bf8a48de140a3c666897588a05shiqian 2025e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string equal to str. 2026e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> > 2027e35fdd936d133bf8a48de140a3c666897588a05shiqian StrEq(const internal::wstring& str) { 2028e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>( 2029e35fdd936d133bf8a48de140a3c666897588a05shiqian str, true, true)); 2030e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2031e35fdd936d133bf8a48de140a3c666897588a05shiqian 2032e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string not equal to str. 2033e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> > 2034e35fdd936d133bf8a48de140a3c666897588a05shiqian StrNe(const internal::wstring& str) { 2035e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>( 2036e35fdd936d133bf8a48de140a3c666897588a05shiqian str, false, true)); 2037e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2038e35fdd936d133bf8a48de140a3c666897588a05shiqian 2039e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string equal to str, ignoring case. 2040e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> > 2041e35fdd936d133bf8a48de140a3c666897588a05shiqian StrCaseEq(const internal::wstring& str) { 2042e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>( 2043e35fdd936d133bf8a48de140a3c666897588a05shiqian str, true, false)); 2044e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2045e35fdd936d133bf8a48de140a3c666897588a05shiqian 2046e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string not equal to str, ignoring case. 2047e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> > 2048e35fdd936d133bf8a48de140a3c666897588a05shiqian StrCaseNe(const internal::wstring& str) { 2049e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>( 2050e35fdd936d133bf8a48de140a3c666897588a05shiqian str, false, false)); 2051e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2052e35fdd936d133bf8a48de140a3c666897588a05shiqian 2053e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any wstring, std::wstring, or C wide string 2054e35fdd936d133bf8a48de140a3c666897588a05shiqian// that contains the given substring. 2055e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::HasSubstrMatcher<internal::wstring> > 2056e35fdd936d133bf8a48de140a3c666897588a05shiqian HasSubstr(const internal::wstring& substring) { 2057e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::HasSubstrMatcher<internal::wstring>( 2058e35fdd936d133bf8a48de140a3c666897588a05shiqian substring)); 2059e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2060e35fdd936d133bf8a48de140a3c666897588a05shiqian 2061e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that starts with 'prefix' (case-sensitive). 2062e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StartsWithMatcher<internal::wstring> > 2063e35fdd936d133bf8a48de140a3c666897588a05shiqian StartsWith(const internal::wstring& prefix) { 2064e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::StartsWithMatcher<internal::wstring>( 2065e35fdd936d133bf8a48de140a3c666897588a05shiqian prefix)); 2066e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2067e35fdd936d133bf8a48de140a3c666897588a05shiqian 2068e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that ends with 'suffix' (case-sensitive). 2069e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::EndsWithMatcher<internal::wstring> > 2070e35fdd936d133bf8a48de140a3c666897588a05shiqian EndsWith(const internal::wstring& suffix) { 2071e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::EndsWithMatcher<internal::wstring>( 2072e35fdd936d133bf8a48de140a3c666897588a05shiqian suffix)); 2073e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2074e35fdd936d133bf8a48de140a3c666897588a05shiqian 2075e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif // GTEST_HAS_GLOBAL_WSTRING || GTEST_HAS_STD_WSTRING 2076e35fdd936d133bf8a48de140a3c666897588a05shiqian 2077e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the 2078e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field == the second field. 2079e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Eq2Matcher Eq() { return internal::Eq2Matcher(); } 2080e35fdd936d133bf8a48de140a3c666897588a05shiqian 2081e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the 2082e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field >= the second field. 2083e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Ge2Matcher Ge() { return internal::Ge2Matcher(); } 2084e35fdd936d133bf8a48de140a3c666897588a05shiqian 2085e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the 2086e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field > the second field. 2087e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Gt2Matcher Gt() { return internal::Gt2Matcher(); } 2088e35fdd936d133bf8a48de140a3c666897588a05shiqian 2089e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the 2090e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field <= the second field. 2091e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Le2Matcher Le() { return internal::Le2Matcher(); } 2092e35fdd936d133bf8a48de140a3c666897588a05shiqian 2093e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the 2094e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field < the second field. 2095e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Lt2Matcher Lt() { return internal::Lt2Matcher(); } 2096e35fdd936d133bf8a48de140a3c666897588a05shiqian 2097e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the 2098e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field != the second field. 2099e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Ne2Matcher Ne() { return internal::Ne2Matcher(); } 2100e35fdd936d133bf8a48de140a3c666897588a05shiqian 2101e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any value of type T that m doesn't 2102e35fdd936d133bf8a48de140a3c666897588a05shiqian// match. 2103e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename InnerMatcher> 2104e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::NotMatcher<InnerMatcher> Not(InnerMatcher m) { 2105e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::NotMatcher<InnerMatcher>(m); 2106e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2107e35fdd936d133bf8a48de140a3c666897588a05shiqian 2108e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any value that matches all of the 2109e35fdd936d133bf8a48de140a3c666897588a05shiqian// given matchers. 2110e35fdd936d133bf8a48de140a3c666897588a05shiqian// 2111e35fdd936d133bf8a48de140a3c666897588a05shiqian// For now we only support up to 5 matchers. Support for more 2112e35fdd936d133bf8a48de140a3c666897588a05shiqian// matchers can be added as needed, or the user can use nested 2113e35fdd936d133bf8a48de140a3c666897588a05shiqian// AllOf()s. 2114e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2> 2115e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::BothOfMatcher<Matcher1, Matcher2> 2116e35fdd936d133bf8a48de140a3c666897588a05shiqianAllOf(Matcher1 m1, Matcher2 m2) { 2117e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::BothOfMatcher<Matcher1, Matcher2>(m1, m2); 2118e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2119e35fdd936d133bf8a48de140a3c666897588a05shiqian 2120e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2, typename Matcher3> 2121e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::BothOfMatcher<Matcher1, 2122e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::BothOfMatcher<Matcher2, Matcher3> > 2123e35fdd936d133bf8a48de140a3c666897588a05shiqianAllOf(Matcher1 m1, Matcher2 m2, Matcher3 m3) { 2124e35fdd936d133bf8a48de140a3c666897588a05shiqian return AllOf(m1, AllOf(m2, m3)); 2125e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2126e35fdd936d133bf8a48de140a3c666897588a05shiqian 2127e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2, typename Matcher3, 2128e35fdd936d133bf8a48de140a3c666897588a05shiqian typename Matcher4> 2129e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::BothOfMatcher<Matcher1, 2130e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::BothOfMatcher<Matcher2, 2131e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::BothOfMatcher<Matcher3, Matcher4> > > 2132e35fdd936d133bf8a48de140a3c666897588a05shiqianAllOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4) { 2133e35fdd936d133bf8a48de140a3c666897588a05shiqian return AllOf(m1, AllOf(m2, m3, m4)); 2134e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2135e35fdd936d133bf8a48de140a3c666897588a05shiqian 2136e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2, typename Matcher3, 2137e35fdd936d133bf8a48de140a3c666897588a05shiqian typename Matcher4, typename Matcher5> 2138e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::BothOfMatcher<Matcher1, 2139e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::BothOfMatcher<Matcher2, 2140e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::BothOfMatcher<Matcher3, 2141e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::BothOfMatcher<Matcher4, Matcher5> > > > 2142e35fdd936d133bf8a48de140a3c666897588a05shiqianAllOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4, Matcher5 m5) { 2143e35fdd936d133bf8a48de140a3c666897588a05shiqian return AllOf(m1, AllOf(m2, m3, m4, m5)); 2144e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2145e35fdd936d133bf8a48de140a3c666897588a05shiqian 2146e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any value that matches at least one 2147e35fdd936d133bf8a48de140a3c666897588a05shiqian// of the given matchers. 2148e35fdd936d133bf8a48de140a3c666897588a05shiqian// 2149e35fdd936d133bf8a48de140a3c666897588a05shiqian// For now we only support up to 5 matchers. Support for more 2150e35fdd936d133bf8a48de140a3c666897588a05shiqian// matchers can be added as needed, or the user can use nested 2151e35fdd936d133bf8a48de140a3c666897588a05shiqian// AnyOf()s. 2152e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2> 2153e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::EitherOfMatcher<Matcher1, Matcher2> 2154e35fdd936d133bf8a48de140a3c666897588a05shiqianAnyOf(Matcher1 m1, Matcher2 m2) { 2155e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::EitherOfMatcher<Matcher1, Matcher2>(m1, m2); 2156e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2157e35fdd936d133bf8a48de140a3c666897588a05shiqian 2158e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2, typename Matcher3> 2159e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::EitherOfMatcher<Matcher1, 2160e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::EitherOfMatcher<Matcher2, Matcher3> > 2161e35fdd936d133bf8a48de140a3c666897588a05shiqianAnyOf(Matcher1 m1, Matcher2 m2, Matcher3 m3) { 2162e35fdd936d133bf8a48de140a3c666897588a05shiqian return AnyOf(m1, AnyOf(m2, m3)); 2163e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2164e35fdd936d133bf8a48de140a3c666897588a05shiqian 2165e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2, typename Matcher3, 2166e35fdd936d133bf8a48de140a3c666897588a05shiqian typename Matcher4> 2167e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::EitherOfMatcher<Matcher1, 2168e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::EitherOfMatcher<Matcher2, 2169e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::EitherOfMatcher<Matcher3, Matcher4> > > 2170e35fdd936d133bf8a48de140a3c666897588a05shiqianAnyOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4) { 2171e35fdd936d133bf8a48de140a3c666897588a05shiqian return AnyOf(m1, AnyOf(m2, m3, m4)); 2172e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2173e35fdd936d133bf8a48de140a3c666897588a05shiqian 2174e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2, typename Matcher3, 2175e35fdd936d133bf8a48de140a3c666897588a05shiqian typename Matcher4, typename Matcher5> 2176e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::EitherOfMatcher<Matcher1, 2177e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::EitherOfMatcher<Matcher2, 2178e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::EitherOfMatcher<Matcher3, 2179e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::EitherOfMatcher<Matcher4, Matcher5> > > > 2180e35fdd936d133bf8a48de140a3c666897588a05shiqianAnyOf(Matcher1 m1, Matcher2 m2, Matcher3 m3, Matcher4 m4, Matcher5 m5) { 2181e35fdd936d133bf8a48de140a3c666897588a05shiqian return AnyOf(m1, AnyOf(m2, m3, m4, m5)); 2182e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2183e35fdd936d133bf8a48de140a3c666897588a05shiqian 2184e35fdd936d133bf8a48de140a3c666897588a05shiqian// Returns a matcher that matches anything that satisfies the given 2185e35fdd936d133bf8a48de140a3c666897588a05shiqian// predicate. The predicate can be any unary function or functor 2186e35fdd936d133bf8a48de140a3c666897588a05shiqian// whose return type can be implicitly converted to bool. 2187e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Predicate> 2188e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::TrulyMatcher<Predicate> > 2189e35fdd936d133bf8a48de140a3c666897588a05shiqianTruly(Predicate pred) { 2190e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::TrulyMatcher<Predicate>(pred)); 2191e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2192e35fdd936d133bf8a48de140a3c666897588a05shiqian 21936a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// Returns a matcher that matches an equal container. 21946a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// This matcher behaves like Eq(), but in the event of mismatch lists the 21956a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// values that are included in one container but not the other. (Duplicate 21966a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// values and order differences are not explained.) 21976a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wantemplate <typename Container> 21986a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.waninline PolymorphicMatcher<internal::ContainerEqMatcher<Container> > 21996a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan ContainerEq(const Container& rhs) { 22006a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan return MakePolymorphicMatcher(internal::ContainerEqMatcher<Container>(rhs)); 22016a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan} 22026a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan 2203e35fdd936d133bf8a48de140a3c666897588a05shiqian// Returns a predicate that is satisfied by anything that matches the 2204e35fdd936d133bf8a48de140a3c666897588a05shiqian// given matcher. 2205e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename M> 2206e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::MatcherAsPredicate<M> Matches(M matcher) { 2207e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::MatcherAsPredicate<M>(matcher); 2208e35fdd936d133bf8a48de140a3c666897588a05shiqian} 2209e35fdd936d133bf8a48de140a3c666897588a05shiqian 2210e35fdd936d133bf8a48de140a3c666897588a05shiqian// These macros allow using matchers to check values in Google Test 2211e35fdd936d133bf8a48de140a3c666897588a05shiqian// tests. ASSERT_THAT(value, matcher) and EXPECT_THAT(value, matcher) 2212e35fdd936d133bf8a48de140a3c666897588a05shiqian// succeed iff the value matches the matcher. If the assertion fails, 2213e35fdd936d133bf8a48de140a3c666897588a05shiqian// the value and the description of the matcher will be printed. 2214e35fdd936d133bf8a48de140a3c666897588a05shiqian#define ASSERT_THAT(value, matcher) ASSERT_PRED_FORMAT1(\ 2215e35fdd936d133bf8a48de140a3c666897588a05shiqian ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value) 2216e35fdd936d133bf8a48de140a3c666897588a05shiqian#define EXPECT_THAT(value, matcher) EXPECT_PRED_FORMAT1(\ 2217e35fdd936d133bf8a48de140a3c666897588a05shiqian ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value) 2218e35fdd936d133bf8a48de140a3c666897588a05shiqian 2219e35fdd936d133bf8a48de140a3c666897588a05shiqian} // namespace testing 2220e35fdd936d133bf8a48de140a3c666897588a05shiqian 2221e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif // GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_ 2222