gmock-matchers.h revision 5579c1a8b1591d4932495b8cb3cc61f3edca2555
1e35fdd936d133bf8a48de140a3c666897588a05shiqian// Copyright 2007, Google Inc. 2e35fdd936d133bf8a48de140a3c666897588a05shiqian// All rights reserved. 3e35fdd936d133bf8a48de140a3c666897588a05shiqian// 4e35fdd936d133bf8a48de140a3c666897588a05shiqian// Redistribution and use in source and binary forms, with or without 5e35fdd936d133bf8a48de140a3c666897588a05shiqian// modification, are permitted provided that the following conditions are 6e35fdd936d133bf8a48de140a3c666897588a05shiqian// met: 7e35fdd936d133bf8a48de140a3c666897588a05shiqian// 8e35fdd936d133bf8a48de140a3c666897588a05shiqian// * Redistributions of source code must retain the above copyright 9e35fdd936d133bf8a48de140a3c666897588a05shiqian// notice, this list of conditions and the following disclaimer. 10e35fdd936d133bf8a48de140a3c666897588a05shiqian// * Redistributions in binary form must reproduce the above 11e35fdd936d133bf8a48de140a3c666897588a05shiqian// copyright notice, this list of conditions and the following disclaimer 12e35fdd936d133bf8a48de140a3c666897588a05shiqian// in the documentation and/or other materials provided with the 13e35fdd936d133bf8a48de140a3c666897588a05shiqian// distribution. 14e35fdd936d133bf8a48de140a3c666897588a05shiqian// * Neither the name of Google Inc. nor the names of its 15e35fdd936d133bf8a48de140a3c666897588a05shiqian// contributors may be used to endorse or promote products derived from 16e35fdd936d133bf8a48de140a3c666897588a05shiqian// this software without specific prior written permission. 17e35fdd936d133bf8a48de140a3c666897588a05shiqian// 18e35fdd936d133bf8a48de140a3c666897588a05shiqian// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19e35fdd936d133bf8a48de140a3c666897588a05shiqian// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20e35fdd936d133bf8a48de140a3c666897588a05shiqian// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21e35fdd936d133bf8a48de140a3c666897588a05shiqian// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22e35fdd936d133bf8a48de140a3c666897588a05shiqian// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23e35fdd936d133bf8a48de140a3c666897588a05shiqian// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24e35fdd936d133bf8a48de140a3c666897588a05shiqian// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25e35fdd936d133bf8a48de140a3c666897588a05shiqian// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26e35fdd936d133bf8a48de140a3c666897588a05shiqian// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27e35fdd936d133bf8a48de140a3c666897588a05shiqian// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28e35fdd936d133bf8a48de140a3c666897588a05shiqian// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29e35fdd936d133bf8a48de140a3c666897588a05shiqian// 30e35fdd936d133bf8a48de140a3c666897588a05shiqian// Author: wan@google.com (Zhanyong Wan) 31e35fdd936d133bf8a48de140a3c666897588a05shiqian 32e35fdd936d133bf8a48de140a3c666897588a05shiqian// Google Mock - a framework for writing C++ mock classes. 33e35fdd936d133bf8a48de140a3c666897588a05shiqian// 34e35fdd936d133bf8a48de140a3c666897588a05shiqian// This file implements some commonly used argument matchers. More 35e35fdd936d133bf8a48de140a3c666897588a05shiqian// matchers can be defined by the user implementing the 36e35fdd936d133bf8a48de140a3c666897588a05shiqian// MatcherInterface<T> interface if necessary. 37e35fdd936d133bf8a48de140a3c666897588a05shiqian 38e35fdd936d133bf8a48de140a3c666897588a05shiqian#ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_ 39e35fdd936d133bf8a48de140a3c666897588a05shiqian#define GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_ 40e35fdd936d133bf8a48de140a3c666897588a05shiqian 41616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan#include <math.h> 426a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan#include <algorithm> 43fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan#include <iterator> 4416cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan#include <limits> 45e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <ostream> // NOLINT 46e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <sstream> 47e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <string> 48ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan#include <utility> 49e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <vector> 50e35fdd936d133bf8a48de140a3c666897588a05shiqian 5153e08c44dd34857ba57581d7c5774d6c96a8d0e1zhanyong.wan#include "gmock/internal/gmock-internal-utils.h" 5253e08c44dd34857ba57581d7c5774d6c96a8d0e1zhanyong.wan#include "gmock/internal/gmock-port.h" 5353e08c44dd34857ba57581d7c5774d6c96a8d0e1zhanyong.wan#include "gtest/gtest.h" 54e35fdd936d133bf8a48de140a3c666897588a05shiqian 55e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace testing { 56e35fdd936d133bf8a48de140a3c666897588a05shiqian 57e35fdd936d133bf8a48de140a3c666897588a05shiqian// To implement a matcher Foo for type T, define: 58e35fdd936d133bf8a48de140a3c666897588a05shiqian// 1. a class FooMatcherImpl that implements the 59e35fdd936d133bf8a48de140a3c666897588a05shiqian// MatcherInterface<T> interface, and 60e35fdd936d133bf8a48de140a3c666897588a05shiqian// 2. a factory function that creates a Matcher<T> object from a 61e35fdd936d133bf8a48de140a3c666897588a05shiqian// FooMatcherImpl*. 62e35fdd936d133bf8a48de140a3c666897588a05shiqian// 63e35fdd936d133bf8a48de140a3c666897588a05shiqian// The two-level delegation design makes it possible to allow a user 64e35fdd936d133bf8a48de140a3c666897588a05shiqian// to write "v" instead of "Eq(v)" where a Matcher is expected, which 65e35fdd936d133bf8a48de140a3c666897588a05shiqian// is impossible if we pass matchers by pointers. It also eases 66e35fdd936d133bf8a48de140a3c666897588a05shiqian// ownership management as Matcher objects can now be copied like 67e35fdd936d133bf8a48de140a3c666897588a05shiqian// plain values. 68e35fdd936d133bf8a48de140a3c666897588a05shiqian 69821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// MatchResultListener is an abstract class. Its << operator can be 70821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// used by a matcher to explain why a value matches or doesn't match. 71821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// 72821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// TODO(wan@google.com): add method 73821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// bool InterestedInWhy(bool result) const; 74821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// to indicate whether the listener is interested in why the match 75821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// result is 'result'. 76821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wanclass MatchResultListener { 77821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan public: 78821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // Creates a listener object with the given underlying ostream. The 79821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // listener does not own the ostream. 80821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan explicit MatchResultListener(::std::ostream* os) : stream_(os) {} 81821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual ~MatchResultListener() = 0; // Makes this class abstract. 82821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 83821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // Streams x to the underlying ostream; does nothing if the ostream 84821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // is NULL. 85821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan template <typename T> 86821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan MatchResultListener& operator<<(const T& x) { 87821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (stream_ != NULL) 88821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *stream_ << x; 89821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return *this; 90821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan } 91821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 92821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // Returns the underlying ostream. 93821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan ::std::ostream* stream() { return stream_; } 94821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 95a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan // Returns true iff the listener is interested in an explanation of 96a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan // the match result. A matcher's MatchAndExplain() method can use 97a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan // this information to avoid generating the explanation when no one 98a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan // intends to hear it. 99a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan bool IsInterested() const { return stream_ != NULL; } 100a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.wan 101821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan private: 102821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan ::std::ostream* const stream_; 103821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 104821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan GTEST_DISALLOW_COPY_AND_ASSIGN_(MatchResultListener); 105821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan}; 106821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 107821133180ccdb9eb062548d964929ba3b354eb84zhanyong.waninline MatchResultListener::~MatchResultListener() { 108821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan} 109821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 110fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// An instance of a subclass of this knows how to describe itself as a 111fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// matcher. 112fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanclass MatcherDescriberInterface { 113fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan public: 114fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan virtual ~MatcherDescriberInterface() {} 115fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 116fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // Describes this matcher to an ostream. The function should print 117fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // a verb phrase that describes the property a value matching this 118fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // matcher should have. The subject of the verb phrase is the value 119fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // being matched. For example, the DescribeTo() method of the Gt(7) 120fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // matcher prints "is greater than 7". 121fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan virtual void DescribeTo(::std::ostream* os) const = 0; 122fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 123fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // Describes the negation of this matcher to an ostream. For 124fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // example, if the description of this matcher is "is greater than 125fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // 7", the negated description could be "is not greater than 7". 126fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // You are not required to override this when implementing 127fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // MatcherInterface, but it is highly advised so that your matcher 128fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // can produce good error messages. 129fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan virtual void DescribeNegationTo(::std::ostream* os) const { 130fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan *os << "not ("; 131fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan DescribeTo(os); 132fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan *os << ")"; 133fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan } 134fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan}; 135fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 136e35fdd936d133bf8a48de140a3c666897588a05shiqian// The implementation of a matcher. 137e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 138fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanclass MatcherInterface : public MatcherDescriberInterface { 139e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 140821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // Returns true iff the matcher matches x; also explains the match 14183f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan // result to 'listener' if necessary (see the next paragraph), in 14283f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan // the form of a non-restrictive relative clause ("which ...", 14383f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan // "whose ...", etc) that describes x. For example, the 14483f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan // MatchAndExplain() method of the Pointee(...) matcher should 14583f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan // generate an explanation like "which points to ...". 14683f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan // 14783f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan // Implementations of MatchAndExplain() should add an explanation of 14883f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan // the match result *if and only if* they can provide additional 14983f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan // information that's not already present (or not obvious) in the 15083f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan // print-out of x and the matcher's description. Whether the match 15183f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan // succeeds is not a factor in deciding whether an explanation is 15283f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan // needed, as sometimes the caller needs to print a failure message 15383f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan // when the match succeeds (e.g. when the matcher is used inside 15483f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan // Not()). 15583f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan // 15683f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan // For example, a "has at least 10 elements" matcher should explain 15783f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan // what the actual element count is, regardless of the match result, 15883f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan // as it is useful information to the reader; on the other hand, an 15983f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan // "is empty" matcher probably only needs to explain what the actual 16083f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan // size is when the match fails, as it's redundant to say that the 16183f6b08b5f730c2bfaa288a17d5f12ca98901a00zhanyong.wan // size is 0 when the value is already known to be empty. 162821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // 163db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan // You should override this method when defining a new matcher. 164821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // 165821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // It's the responsibility of the caller (Google Mock) to guarantee 166821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // that 'listener' is not NULL. This helps to simplify a matcher's 167821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // implementation when it doesn't care about the performance, as it 168821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // can talk to 'listener' without checking its validity first. 169821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // However, in order to implement dummy listeners efficiently, 170821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // listener->stream() may be NULL. 171db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan virtual bool MatchAndExplain(T x, MatchResultListener* listener) const = 0; 172e35fdd936d133bf8a48de140a3c666897588a05shiqian 173fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // Inherits these methods from MatcherDescriberInterface: 174fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // virtual void DescribeTo(::std::ostream* os) const = 0; 175fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // virtual void DescribeNegationTo(::std::ostream* os) const; 176e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 177e35fdd936d133bf8a48de140a3c666897588a05shiqian 178e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace internal { 179e35fdd936d133bf8a48de140a3c666897588a05shiqian 180821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// A match result listener that ignores the explanation. 181821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wanclass DummyMatchResultListener : public MatchResultListener { 182821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan public: 183821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan DummyMatchResultListener() : MatchResultListener(NULL) {} 184821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 185821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan private: 186821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan GTEST_DISALLOW_COPY_AND_ASSIGN_(DummyMatchResultListener); 187821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan}; 188821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 189821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// A match result listener that forwards the explanation to a given 190821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// ostream. The difference between this and MatchResultListener is 191821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// that the former is concrete. 192821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wanclass StreamMatchResultListener : public MatchResultListener { 193821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan public: 194821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan explicit StreamMatchResultListener(::std::ostream* os) 195821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan : MatchResultListener(os) {} 196821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 197821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan private: 198821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan GTEST_DISALLOW_COPY_AND_ASSIGN_(StreamMatchResultListener); 199821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan}; 200821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 201821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// A match result listener that stores the explanation in a string. 202821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wanclass StringMatchResultListener : public MatchResultListener { 203821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan public: 204821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan StringMatchResultListener() : MatchResultListener(&ss_) {} 205821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 206821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // Returns the explanation heard so far. 207821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan internal::string str() const { return ss_.str(); } 208821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 209821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan private: 210821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan ::std::stringstream ss_; 211821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 212821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan GTEST_DISALLOW_COPY_AND_ASSIGN_(StringMatchResultListener); 213821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan}; 214821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 215e35fdd936d133bf8a48de140a3c666897588a05shiqian// An internal class for implementing Matcher<T>, which will derive 216e35fdd936d133bf8a48de140a3c666897588a05shiqian// from it. We put functionalities common to all Matcher<T> 217e35fdd936d133bf8a48de140a3c666897588a05shiqian// specializations here to avoid code duplication. 218e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 219e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatcherBase { 220e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 221821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // Returns true iff the matcher matches x; also explains the match 222821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // result to 'listener'. 223821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan bool MatchAndExplain(T x, MatchResultListener* listener) const { 224821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return impl_->MatchAndExplain(x, listener); 225821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan } 226821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 227e35fdd936d133bf8a48de140a3c666897588a05shiqian // Returns true iff this matcher matches x. 228821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan bool Matches(T x) const { 229821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan DummyMatchResultListener dummy; 230821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return MatchAndExplain(x, &dummy); 231821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan } 232e35fdd936d133bf8a48de140a3c666897588a05shiqian 233e35fdd936d133bf8a48de140a3c666897588a05shiqian // Describes this matcher to an ostream. 234e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeTo(::std::ostream* os) const { impl_->DescribeTo(os); } 235e35fdd936d133bf8a48de140a3c666897588a05shiqian 236e35fdd936d133bf8a48de140a3c666897588a05shiqian // Describes the negation of this matcher to an ostream. 237e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeNegationTo(::std::ostream* os) const { 238e35fdd936d133bf8a48de140a3c666897588a05shiqian impl_->DescribeNegationTo(os); 239e35fdd936d133bf8a48de140a3c666897588a05shiqian } 240e35fdd936d133bf8a48de140a3c666897588a05shiqian 241e35fdd936d133bf8a48de140a3c666897588a05shiqian // Explains why x matches, or doesn't match, the matcher. 242e35fdd936d133bf8a48de140a3c666897588a05shiqian void ExplainMatchResultTo(T x, ::std::ostream* os) const { 243821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan StreamMatchResultListener listener(os); 244821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan MatchAndExplain(x, &listener); 245e35fdd936d133bf8a48de140a3c666897588a05shiqian } 24632de5f53763125925e078498250f7e73a88de9edzhanyong.wan 247fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // Returns the describer for this matcher object; retains ownership 248fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // of the describer, which is only guaranteed to be alive when 249fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // this matcher object is alive. 250fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan const MatcherDescriberInterface* GetDescriber() const { 251fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan return impl_.get(); 252fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan } 253fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 254e35fdd936d133bf8a48de140a3c666897588a05shiqian protected: 255e35fdd936d133bf8a48de140a3c666897588a05shiqian MatcherBase() {} 256e35fdd936d133bf8a48de140a3c666897588a05shiqian 257e35fdd936d133bf8a48de140a3c666897588a05shiqian // Constructs a matcher from its implementation. 258e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit MatcherBase(const MatcherInterface<T>* impl) 259e35fdd936d133bf8a48de140a3c666897588a05shiqian : impl_(impl) {} 260e35fdd936d133bf8a48de140a3c666897588a05shiqian 261e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual ~MatcherBase() {} 26232de5f53763125925e078498250f7e73a88de9edzhanyong.wan 263e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 264e35fdd936d133bf8a48de140a3c666897588a05shiqian // shared_ptr (util/gtl/shared_ptr.h) and linked_ptr have similar 265e35fdd936d133bf8a48de140a3c666897588a05shiqian // interfaces. The former dynamically allocates a chunk of memory 266e35fdd936d133bf8a48de140a3c666897588a05shiqian // to hold the reference count, while the latter tracks all 267e35fdd936d133bf8a48de140a3c666897588a05shiqian // references using a circular linked list without allocating 268e35fdd936d133bf8a48de140a3c666897588a05shiqian // memory. It has been observed that linked_ptr performs better in 269e35fdd936d133bf8a48de140a3c666897588a05shiqian // typical scenarios. However, shared_ptr can out-perform 270e35fdd936d133bf8a48de140a3c666897588a05shiqian // linked_ptr when there are many more uses of the copy constructor 271e35fdd936d133bf8a48de140a3c666897588a05shiqian // than the default constructor. 272e35fdd936d133bf8a48de140a3c666897588a05shiqian // 273e35fdd936d133bf8a48de140a3c666897588a05shiqian // If performance becomes a problem, we should see if using 274e35fdd936d133bf8a48de140a3c666897588a05shiqian // shared_ptr helps. 275e35fdd936d133bf8a48de140a3c666897588a05shiqian ::testing::internal::linked_ptr<const MatcherInterface<T> > impl_; 276e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 277e35fdd936d133bf8a48de140a3c666897588a05shiqian 278e35fdd936d133bf8a48de140a3c666897588a05shiqian} // namespace internal 279e35fdd936d133bf8a48de140a3c666897588a05shiqian 280e35fdd936d133bf8a48de140a3c666897588a05shiqian// A Matcher<T> is a copyable and IMMUTABLE (except by assignment) 281e35fdd936d133bf8a48de140a3c666897588a05shiqian// object that can check whether a value of type T matches. The 282e35fdd936d133bf8a48de140a3c666897588a05shiqian// implementation of Matcher<T> is just a linked_ptr to const 283e35fdd936d133bf8a48de140a3c666897588a05shiqian// MatcherInterface<T>, so copying is fairly cheap. Don't inherit 284e35fdd936d133bf8a48de140a3c666897588a05shiqian// from Matcher! 285e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 286e35fdd936d133bf8a48de140a3c666897588a05shiqianclass Matcher : public internal::MatcherBase<T> { 287e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 28888032d8e0f6d4bc63b97aeeab84fdc44aeab1c89vladlosev // Constructs a null matcher. Needed for storing Matcher objects in STL 28988032d8e0f6d4bc63b97aeeab84fdc44aeab1c89vladlosev // containers. A default-constructed matcher is not yet initialized. You 29088032d8e0f6d4bc63b97aeeab84fdc44aeab1c89vladlosev // cannot use it until a valid value has been assigned to it. 291e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher() {} 292e35fdd936d133bf8a48de140a3c666897588a05shiqian 293e35fdd936d133bf8a48de140a3c666897588a05shiqian // Constructs a matcher from its implementation. 294e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit Matcher(const MatcherInterface<T>* impl) 295e35fdd936d133bf8a48de140a3c666897588a05shiqian : internal::MatcherBase<T>(impl) {} 296e35fdd936d133bf8a48de140a3c666897588a05shiqian 29718490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan // Implicit constructor here allows people to write 298e35fdd936d133bf8a48de140a3c666897588a05shiqian // EXPECT_CALL(foo, Bar(5)) instead of EXPECT_CALL(foo, Bar(Eq(5))) sometimes 299e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher(T value); // NOLINT 300e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 301e35fdd936d133bf8a48de140a3c666897588a05shiqian 302e35fdd936d133bf8a48de140a3c666897588a05shiqian// The following two specializations allow the user to write str 303e35fdd936d133bf8a48de140a3c666897588a05shiqian// instead of Eq(str) and "foo" instead of Eq("foo") when a string 304e35fdd936d133bf8a48de140a3c666897588a05shiqian// matcher is expected. 305e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <> 306587c1b37c2f0b6d430fb13ce09326db0135b557cvladlosevclass GTEST_API_ Matcher<const internal::string&> 307e35fdd936d133bf8a48de140a3c666897588a05shiqian : public internal::MatcherBase<const internal::string&> { 308e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 309e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher() {} 310e35fdd936d133bf8a48de140a3c666897588a05shiqian 311e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit Matcher(const MatcherInterface<const internal::string&>* impl) 312e35fdd936d133bf8a48de140a3c666897588a05shiqian : internal::MatcherBase<const internal::string&>(impl) {} 313e35fdd936d133bf8a48de140a3c666897588a05shiqian 314e35fdd936d133bf8a48de140a3c666897588a05shiqian // Allows the user to write str instead of Eq(str) sometimes, where 315e35fdd936d133bf8a48de140a3c666897588a05shiqian // str is a string object. 316e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher(const internal::string& s); // NOLINT 317e35fdd936d133bf8a48de140a3c666897588a05shiqian 318e35fdd936d133bf8a48de140a3c666897588a05shiqian // Allows the user to write "foo" instead of Eq("foo") sometimes. 319e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher(const char* s); // NOLINT 320e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 321e35fdd936d133bf8a48de140a3c666897588a05shiqian 322e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <> 323587c1b37c2f0b6d430fb13ce09326db0135b557cvladlosevclass GTEST_API_ Matcher<internal::string> 324e35fdd936d133bf8a48de140a3c666897588a05shiqian : public internal::MatcherBase<internal::string> { 325e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 326e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher() {} 327e35fdd936d133bf8a48de140a3c666897588a05shiqian 328e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit Matcher(const MatcherInterface<internal::string>* impl) 329e35fdd936d133bf8a48de140a3c666897588a05shiqian : internal::MatcherBase<internal::string>(impl) {} 330e35fdd936d133bf8a48de140a3c666897588a05shiqian 331e35fdd936d133bf8a48de140a3c666897588a05shiqian // Allows the user to write str instead of Eq(str) sometimes, where 332e35fdd936d133bf8a48de140a3c666897588a05shiqian // str is a string object. 333e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher(const internal::string& s); // NOLINT 334e35fdd936d133bf8a48de140a3c666897588a05shiqian 335e35fdd936d133bf8a48de140a3c666897588a05shiqian // Allows the user to write "foo" instead of Eq("foo") sometimes. 336e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher(const char* s); // NOLINT 337e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 338e35fdd936d133bf8a48de140a3c666897588a05shiqian 3391f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan#if GTEST_HAS_STRING_PIECE_ 3401f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan// The following two specializations allow the user to write str 3411f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan// instead of Eq(str) and "foo" instead of Eq("foo") when a StringPiece 3421f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan// matcher is expected. 3431f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wantemplate <> 3441f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wanclass GTEST_API_ Matcher<const StringPiece&> 3451f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan : public internal::MatcherBase<const StringPiece&> { 3461f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan public: 3471f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan Matcher() {} 3481f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan 3491f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan explicit Matcher(const MatcherInterface<const StringPiece&>* impl) 3501f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan : internal::MatcherBase<const StringPiece&>(impl) {} 3511f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan 3521f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan // Allows the user to write str instead of Eq(str) sometimes, where 3531f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan // str is a string object. 3541f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan Matcher(const internal::string& s); // NOLINT 3551f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan 3561f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan // Allows the user to write "foo" instead of Eq("foo") sometimes. 3571f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan Matcher(const char* s); // NOLINT 3581f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan 3591f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan // Allows the user to pass StringPieces directly. 3601f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan Matcher(StringPiece s); // NOLINT 3611f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan}; 3621f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan 3631f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wantemplate <> 3641f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wanclass GTEST_API_ Matcher<StringPiece> 3651f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan : public internal::MatcherBase<StringPiece> { 3661f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan public: 3671f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan Matcher() {} 3681f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan 3691f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan explicit Matcher(const MatcherInterface<StringPiece>* impl) 3701f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan : internal::MatcherBase<StringPiece>(impl) {} 3711f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan 3721f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan // Allows the user to write str instead of Eq(str) sometimes, where 3731f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan // str is a string object. 3741f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan Matcher(const internal::string& s); // NOLINT 3751f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan 3761f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan // Allows the user to write "foo" instead of Eq("foo") sometimes. 3771f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan Matcher(const char* s); // NOLINT 3781f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan 3791f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan // Allows the user to pass StringPieces directly. 3801f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan Matcher(StringPiece s); // NOLINT 3811f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan}; 3821f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan#endif // GTEST_HAS_STRING_PIECE_ 3831f122a06e6aad4d234123d2d8c1e352029ce0742zhanyong.wan 384e35fdd936d133bf8a48de140a3c666897588a05shiqian// The PolymorphicMatcher class template makes it easy to implement a 385e35fdd936d133bf8a48de140a3c666897588a05shiqian// polymorphic matcher (i.e. a matcher that can match values of more 386e35fdd936d133bf8a48de140a3c666897588a05shiqian// than one type, e.g. Eq(n) and NotNull()). 387e35fdd936d133bf8a48de140a3c666897588a05shiqian// 388db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan// To define a polymorphic matcher, a user should provide an Impl 389db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan// class that has a DescribeTo() method and a DescribeNegationTo() 390db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan// method, and define a member function (or member function template) 391821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// 392db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan// bool MatchAndExplain(const Value& value, 393db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan// MatchResultListener* listener) const; 394821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// 395821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan// See the definition of NotNull() for a complete example. 396e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <class Impl> 397e35fdd936d133bf8a48de140a3c666897588a05shiqianclass PolymorphicMatcher { 398e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 39932de5f53763125925e078498250f7e73a88de9edzhanyong.wan explicit PolymorphicMatcher(const Impl& an_impl) : impl_(an_impl) {} 400e35fdd936d133bf8a48de140a3c666897588a05shiqian 4012b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan // Returns a mutable reference to the underlying matcher 4022b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan // implementation object. 4032b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan Impl& mutable_impl() { return impl_; } 4042b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan 4052b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan // Returns an immutable reference to the underlying matcher 4062b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan // implementation object. 4072b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan const Impl& impl() const { return impl_; } 4082b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan 409e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 410e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<T>() const { 411e35fdd936d133bf8a48de140a3c666897588a05shiqian return Matcher<T>(new MonomorphicImpl<T>(impl_)); 412e35fdd936d133bf8a48de140a3c666897588a05shiqian } 41332de5f53763125925e078498250f7e73a88de9edzhanyong.wan 414e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 415e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 416e35fdd936d133bf8a48de140a3c666897588a05shiqian class MonomorphicImpl : public MatcherInterface<T> { 417e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 418e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {} 419e35fdd936d133bf8a48de140a3c666897588a05shiqian 420e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeTo(::std::ostream* os) const { 421e35fdd936d133bf8a48de140a3c666897588a05shiqian impl_.DescribeTo(os); 422e35fdd936d133bf8a48de140a3c666897588a05shiqian } 423e35fdd936d133bf8a48de140a3c666897588a05shiqian 424e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeNegationTo(::std::ostream* os) const { 425e35fdd936d133bf8a48de140a3c666897588a05shiqian impl_.DescribeNegationTo(os); 426e35fdd936d133bf8a48de140a3c666897588a05shiqian } 427e35fdd936d133bf8a48de140a3c666897588a05shiqian 428821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual bool MatchAndExplain(T x, MatchResultListener* listener) const { 429db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan return impl_.MatchAndExplain(x, listener); 430e35fdd936d133bf8a48de140a3c666897588a05shiqian } 4312b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan 432e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 433e35fdd936d133bf8a48de140a3c666897588a05shiqian const Impl impl_; 43432de5f53763125925e078498250f7e73a88de9edzhanyong.wan 43532de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(MonomorphicImpl); 436e35fdd936d133bf8a48de140a3c666897588a05shiqian }; 437e35fdd936d133bf8a48de140a3c666897588a05shiqian 4382b43a9ecd16edc1ec55429967e0f2de1aaf8e8bbzhanyong.wan Impl impl_; 43932de5f53763125925e078498250f7e73a88de9edzhanyong.wan 44032de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(PolymorphicMatcher); 441e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 442e35fdd936d133bf8a48de140a3c666897588a05shiqian 443e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher from its implementation. This is easier to use 444e35fdd936d133bf8a48de140a3c666897588a05shiqian// than the Matcher<T> constructor as it doesn't require you to 445e35fdd936d133bf8a48de140a3c666897588a05shiqian// explicitly write the template argument, e.g. 446e35fdd936d133bf8a48de140a3c666897588a05shiqian// 447e35fdd936d133bf8a48de140a3c666897588a05shiqian// MakeMatcher(foo); 448e35fdd936d133bf8a48de140a3c666897588a05shiqian// vs 449e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matcher<const string&>(foo); 450e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 451e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<T> MakeMatcher(const MatcherInterface<T>* impl) { 452e35fdd936d133bf8a48de140a3c666897588a05shiqian return Matcher<T>(impl); 4532eab17b76d350dac1b1c85879ec8e1135da615cezhanyong.wan} 454e35fdd936d133bf8a48de140a3c666897588a05shiqian 455e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher from its implementation. This is 456e35fdd936d133bf8a48de140a3c666897588a05shiqian// easier to use than the PolymorphicMatcher<Impl> constructor as it 457e35fdd936d133bf8a48de140a3c666897588a05shiqian// doesn't require you to explicitly write the template argument, e.g. 458e35fdd936d133bf8a48de140a3c666897588a05shiqian// 459e35fdd936d133bf8a48de140a3c666897588a05shiqian// MakePolymorphicMatcher(foo); 460e35fdd936d133bf8a48de140a3c666897588a05shiqian// vs 461e35fdd936d133bf8a48de140a3c666897588a05shiqian// PolymorphicMatcher<TypeOfFoo>(foo); 462e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <class Impl> 463e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<Impl> MakePolymorphicMatcher(const Impl& impl) { 464e35fdd936d133bf8a48de140a3c666897588a05shiqian return PolymorphicMatcher<Impl>(impl); 465e35fdd936d133bf8a48de140a3c666897588a05shiqian} 466e35fdd936d133bf8a48de140a3c666897588a05shiqian 46779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// Anything inside the 'internal' namespace IS INTERNAL IMPLEMENTATION 46879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// and MUST NOT BE USED IN USER CODE!!! 46979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgmnamespace internal { 47079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm 47179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// The MatcherCastImpl class template is a helper for implementing 47279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// MatcherCast(). We need this helper in order to partially 47379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// specialize the implementation of MatcherCast() (C++ allows 47479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// class/struct templates to be partially specialized, but not 47579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// function templates.). 47679a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm 47779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// This general version is used when MatcherCast()'s argument is a 47879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// polymorphic matcher (i.e. something that can be converted to a 47979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// Matcher but is not one yet; for example, Eq(value)) or a value (for 48079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// example, "hello"). 48179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgmtemplate <typename T, typename M> 48279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgmclass MatcherCastImpl { 48379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm public: 48479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm static Matcher<T> Cast(M polymorphic_matcher_or_value) { 48579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm // M can be a polymorhic matcher, in which case we want to use 48679a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm // its conversion operator to create Matcher<T>. Or it can be a value 48779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm // that should be passed to the Matcher<T>'s constructor. 48879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm // 48979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm // We can't call Matcher<T>(polymorphic_matcher_or_value) when M is a 49079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm // polymorphic matcher because it'll be ambiguous if T has an implicit 49179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm // constructor from M (this usually happens when T has an implicit 49279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm // constructor from any type). 49379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm // 49479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm // It won't work to unconditionally implict_cast 49579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm // polymorphic_matcher_or_value to Matcher<T> because it won't trigger 49679a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm // a user-defined conversion from M to T if one exists (assuming M is 49779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm // a value). 49879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm return CastImpl( 49979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm polymorphic_matcher_or_value, 50079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm BooleanConstant< 50179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm internal::ImplicitlyConvertible<M, Matcher<T> >::value>()); 50279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm } 50379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm 50479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm private: 50579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm static Matcher<T> CastImpl(M value, BooleanConstant<false>) { 50679a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm // M can't be implicitly converted to Matcher<T>, so M isn't a polymorphic 50779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm // matcher. It must be a value then. Use direct initialization to create 50879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm // a matcher. 50979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm return Matcher<T>(ImplicitCast_<T>(value)); 51079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm } 51179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm 51279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm static Matcher<T> CastImpl(M polymorphic_matcher_or_value, 51379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm BooleanConstant<true>) { 51479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm // M is implicitly convertible to Matcher<T>, which means that either 51579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm // M is a polymorhpic matcher or Matcher<T> has an implicit constructor 51679a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm // from M. In both cases using the implicit conversion will produce a 51779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm // matcher. 51879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm // 51979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm // Even if T has an implicit constructor from M, it won't be called because 52079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm // creating Matcher<T> would require a chain of two user-defined conversions 52179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm // (first to create T from M and then to create Matcher<T> from T). 52279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm return polymorphic_matcher_or_value; 52379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm } 52479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm}; 52579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm 52679a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// This more specialized version is used when MatcherCast()'s argument 52779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// is already a Matcher. This only compiles when type T can be 52879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// statically converted to type U. 52979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgmtemplate <typename T, typename U> 53079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgmclass MatcherCastImpl<T, Matcher<U> > { 53179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm public: 53279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm static Matcher<T> Cast(const Matcher<U>& source_matcher) { 53379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm return Matcher<T>(new Impl(source_matcher)); 53479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm } 53579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm 53679a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm private: 53779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm class Impl : public MatcherInterface<T> { 53879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm public: 53979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm explicit Impl(const Matcher<U>& source_matcher) 54079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm : source_matcher_(source_matcher) {} 54179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm 54279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm // We delegate the matching logic to the source matcher. 54379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm virtual bool MatchAndExplain(T x, MatchResultListener* listener) const { 54479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm return source_matcher_.MatchAndExplain(static_cast<U>(x), listener); 54579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm } 54679a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm 54779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm virtual void DescribeTo(::std::ostream* os) const { 54879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm source_matcher_.DescribeTo(os); 54979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm } 55079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm 55179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm virtual void DescribeNegationTo(::std::ostream* os) const { 55279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm source_matcher_.DescribeNegationTo(os); 55379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm } 55479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm 55579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm private: 55679a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm const Matcher<U> source_matcher_; 55779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm 55879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm GTEST_DISALLOW_ASSIGN_(Impl); 55979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm }; 56079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm}; 56179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm 56279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// This even more specialized version is used for efficiently casting 56379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm// a matcher to its own type. 56479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgmtemplate <typename T> 56579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgmclass MatcherCastImpl<T, Matcher<T> > { 56679a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm public: 56779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm static Matcher<T> Cast(const Matcher<T>& matcher) { return matcher; } 56879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm}; 56979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm 57079a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm} // namespace internal 57179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm 572e35fdd936d133bf8a48de140a3c666897588a05shiqian// In order to be safe and clear, casting between different matcher 573e35fdd936d133bf8a48de140a3c666897588a05shiqian// types is done explicitly via MatcherCast<T>(m), which takes a 574e35fdd936d133bf8a48de140a3c666897588a05shiqian// matcher m and returns a Matcher<T>. It compiles only when T can be 575e35fdd936d133bf8a48de140a3c666897588a05shiqian// statically converted to the argument type of m. 576e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T, typename M> 57779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgminline Matcher<T> MatcherCast(M matcher) { 57879a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm return internal::MatcherCastImpl<T, M>::Cast(matcher); 57979a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm} 580e35fdd936d133bf8a48de140a3c666897588a05shiqian 58118490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// Implements SafeMatcherCast(). 58218490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan// 58395b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// We use an intermediate class to do the actual safe casting as Nokia's 58495b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// Symbian compiler cannot decide between 58595b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// template <T, M> ... (M) and 58695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// template <T, U> ... (const Matcher<U>&) 58795b12332c345cee508a8e2b68e007280392506e0zhanyong.wan// for function templates but can for member function templates. 58895b12332c345cee508a8e2b68e007280392506e0zhanyong.wantemplate <typename T> 58995b12332c345cee508a8e2b68e007280392506e0zhanyong.wanclass SafeMatcherCastImpl { 59095b12332c345cee508a8e2b68e007280392506e0zhanyong.wan public: 59179a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm // This overload handles polymorphic matchers and values only since 59279a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm // monomorphic matchers are handled by the next one. 59395b12332c345cee508a8e2b68e007280392506e0zhanyong.wan template <typename M> 59479a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm static inline Matcher<T> Cast(M polymorphic_matcher_or_value) { 59579a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm return internal::MatcherCastImpl<T, M>::Cast(polymorphic_matcher_or_value); 59695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan } 59718490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan 59895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // This overload handles monomorphic matchers. 59995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // 60095b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // In general, if type T can be implicitly converted to type U, we can 60195b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // safely convert a Matcher<U> to a Matcher<T> (i.e. Matcher is 60295b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // contravariant): just keep a copy of the original Matcher<U>, convert the 60395b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // argument from type T to U, and then pass it to the underlying Matcher<U>. 60495b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // The only exception is when U is a reference and T is not, as the 60595b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // underlying Matcher<U> may be interested in the argument's address, which 60695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // is not preserved in the conversion from T to U. 60795b12332c345cee508a8e2b68e007280392506e0zhanyong.wan template <typename U> 60895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan static inline Matcher<T> Cast(const Matcher<U>& matcher) { 60995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // Enforce that T can be implicitly converted to U. 61002f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan GTEST_COMPILE_ASSERT_((internal::ImplicitlyConvertible<T, U>::value), 61195b12332c345cee508a8e2b68e007280392506e0zhanyong.wan T_must_be_implicitly_convertible_to_U); 61295b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // Enforce that we are not converting a non-reference type T to a reference 61395b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // type U. 61402f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan GTEST_COMPILE_ASSERT_( 61595b12332c345cee508a8e2b68e007280392506e0zhanyong.wan internal::is_reference<T>::value || !internal::is_reference<U>::value, 61695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan cannot_convert_non_referentce_arg_to_reference); 61795b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // In case both T and U are arithmetic types, enforce that the 61895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan // conversion is not lossy. 619ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan typedef GTEST_REMOVE_REFERENCE_AND_CONST_(T) RawT; 620ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan typedef GTEST_REMOVE_REFERENCE_AND_CONST_(U) RawU; 62195b12332c345cee508a8e2b68e007280392506e0zhanyong.wan const bool kTIsOther = GMOCK_KIND_OF_(RawT) == internal::kOther; 62295b12332c345cee508a8e2b68e007280392506e0zhanyong.wan const bool kUIsOther = GMOCK_KIND_OF_(RawU) == internal::kOther; 62302f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan GTEST_COMPILE_ASSERT_( 62495b12332c345cee508a8e2b68e007280392506e0zhanyong.wan kTIsOther || kUIsOther || 62595b12332c345cee508a8e2b68e007280392506e0zhanyong.wan (internal::LosslessArithmeticConvertible<RawT, RawU>::value), 62695b12332c345cee508a8e2b68e007280392506e0zhanyong.wan conversion_of_arithmetic_types_must_be_lossless); 62795b12332c345cee508a8e2b68e007280392506e0zhanyong.wan return MatcherCast<T>(matcher); 62895b12332c345cee508a8e2b68e007280392506e0zhanyong.wan } 62995b12332c345cee508a8e2b68e007280392506e0zhanyong.wan}; 63095b12332c345cee508a8e2b68e007280392506e0zhanyong.wan 63195b12332c345cee508a8e2b68e007280392506e0zhanyong.wantemplate <typename T, typename M> 63295b12332c345cee508a8e2b68e007280392506e0zhanyong.waninline Matcher<T> SafeMatcherCast(const M& polymorphic_matcher) { 63395b12332c345cee508a8e2b68e007280392506e0zhanyong.wan return SafeMatcherCastImpl<T>::Cast(polymorphic_matcher); 63418490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan} 63518490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan 636e35fdd936d133bf8a48de140a3c666897588a05shiqian// A<T>() returns a matcher that matches any value of type T. 637e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 638e35fdd936d133bf8a48de140a3c666897588a05shiqianMatcher<T> A(); 639e35fdd936d133bf8a48de140a3c666897588a05shiqian 640e35fdd936d133bf8a48de140a3c666897588a05shiqian// Anything inside the 'internal' namespace IS INTERNAL IMPLEMENTATION 641e35fdd936d133bf8a48de140a3c666897588a05shiqian// and MUST NOT BE USED IN USER CODE!!! 642e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace internal { 643e35fdd936d133bf8a48de140a3c666897588a05shiqian 644b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan// If the explanation is not empty, prints it to the ostream. 645b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.waninline void PrintIfNotEmpty(const internal::string& explanation, 646fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan ::std::ostream* os) { 647b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan if (explanation != "" && os != NULL) { 648b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan *os << ", " << explanation; 649676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan } 650676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan} 651676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan 652736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan// Returns true if the given type name is easy to read by a human. 653736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan// This is used to decide whether printing the type of a value might 654736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan// be helpful. 655736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.waninline bool IsReadableTypeName(const string& type_name) { 656736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan // We consider a type name readable if it's short or doesn't contain 657736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan // a template or function type. 658736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan return (type_name.length() <= 20 || 659736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan type_name.find_first_of("<(") == string::npos); 660736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan} 661736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan 662676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan// Matches the value against the given matcher, prints the value and explains 663676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan// the match result to the listener. Returns the match result. 664676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan// 'listener' must not be NULL. 665676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan// Value cannot be passed by const reference, because some matchers take a 666676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan// non-const argument. 667676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wantemplate <typename Value, typename T> 668676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wanbool MatchPrintAndExplain(Value& value, const Matcher<T>& matcher, 669676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan MatchResultListener* listener) { 670676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan if (!listener->IsInterested()) { 671676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan // If the listener is not interested, we do not need to construct the 672676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan // inner explanation. 673676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan return matcher.Matches(value); 674676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan } 675676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan 676676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan StringMatchResultListener inner_listener; 677676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan const bool match = matcher.MatchAndExplain(value, &inner_listener); 678676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan 679676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan UniversalPrint(value, listener->stream()); 680736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan#if GTEST_HAS_RTTI 681736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan const string& type_name = GetTypeName<Value>(); 682736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan if (IsReadableTypeName(type_name)) 683736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan *listener->stream() << " (of type " << type_name << ")"; 684736baa8ac028f01a5c79ebecb6fc7043b47d3c4ezhanyong.wan#endif 685b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan PrintIfNotEmpty(inner_listener.str(), listener->stream()); 686676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan 687676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan return match; 688676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan} 689676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan 690e35fdd936d133bf8a48de140a3c666897588a05shiqian// An internal helper class for doing compile-time loop on a tuple's 691e35fdd936d133bf8a48de140a3c666897588a05shiqian// fields. 692e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <size_t N> 693e35fdd936d133bf8a48de140a3c666897588a05shiqianclass TuplePrefix { 694e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 695e35fdd936d133bf8a48de140a3c666897588a05shiqian // TuplePrefix<N>::Matches(matcher_tuple, value_tuple) returns true 696e35fdd936d133bf8a48de140a3c666897588a05shiqian // iff the first N fields of matcher_tuple matches the first N 697e35fdd936d133bf8a48de140a3c666897588a05shiqian // fields of value_tuple, respectively. 698e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename MatcherTuple, typename ValueTuple> 699e35fdd936d133bf8a48de140a3c666897588a05shiqian static bool Matches(const MatcherTuple& matcher_tuple, 700e35fdd936d133bf8a48de140a3c666897588a05shiqian const ValueTuple& value_tuple) { 701e35fdd936d133bf8a48de140a3c666897588a05shiqian using ::std::tr1::get; 702e35fdd936d133bf8a48de140a3c666897588a05shiqian return TuplePrefix<N - 1>::Matches(matcher_tuple, value_tuple) 703e35fdd936d133bf8a48de140a3c666897588a05shiqian && get<N - 1>(matcher_tuple).Matches(get<N - 1>(value_tuple)); 704e35fdd936d133bf8a48de140a3c666897588a05shiqian } 705e35fdd936d133bf8a48de140a3c666897588a05shiqian 706b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan // TuplePrefix<N>::ExplainMatchFailuresTo(matchers, values, os) 707e35fdd936d133bf8a48de140a3c666897588a05shiqian // describes failures in matching the first N fields of matchers 708e35fdd936d133bf8a48de140a3c666897588a05shiqian // against the first N fields of values. If there is no failure, 709e35fdd936d133bf8a48de140a3c666897588a05shiqian // nothing will be streamed to os. 710e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename MatcherTuple, typename ValueTuple> 711b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan static void ExplainMatchFailuresTo(const MatcherTuple& matchers, 712b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan const ValueTuple& values, 713b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan ::std::ostream* os) { 714e35fdd936d133bf8a48de140a3c666897588a05shiqian using ::std::tr1::tuple_element; 715e35fdd936d133bf8a48de140a3c666897588a05shiqian using ::std::tr1::get; 716e35fdd936d133bf8a48de140a3c666897588a05shiqian 717e35fdd936d133bf8a48de140a3c666897588a05shiqian // First, describes failures in the first N - 1 fields. 718b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan TuplePrefix<N - 1>::ExplainMatchFailuresTo(matchers, values, os); 719e35fdd936d133bf8a48de140a3c666897588a05shiqian 720e35fdd936d133bf8a48de140a3c666897588a05shiqian // Then describes the failure (if any) in the (N - 1)-th (0-based) 721e35fdd936d133bf8a48de140a3c666897588a05shiqian // field. 722e35fdd936d133bf8a48de140a3c666897588a05shiqian typename tuple_element<N - 1, MatcherTuple>::type matcher = 723e35fdd936d133bf8a48de140a3c666897588a05shiqian get<N - 1>(matchers); 724e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef typename tuple_element<N - 1, ValueTuple>::type Value; 725e35fdd936d133bf8a48de140a3c666897588a05shiqian Value value = get<N - 1>(values); 726821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan StringMatchResultListener listener; 727821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (!matcher.MatchAndExplain(value, &listener)) { 728e35fdd936d133bf8a48de140a3c666897588a05shiqian // TODO(wan): include in the message the name of the parameter 729e35fdd936d133bf8a48de140a3c666897588a05shiqian // as used in MOCK_METHOD*() when possible. 730e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << " Expected arg #" << N - 1 << ": "; 731e35fdd936d133bf8a48de140a3c666897588a05shiqian get<N - 1>(matchers).DescribeTo(os); 732e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "\n Actual: "; 733e35fdd936d133bf8a48de140a3c666897588a05shiqian // We remove the reference in type Value to prevent the 734e35fdd936d133bf8a48de140a3c666897588a05shiqian // universal printer from printing the address of value, which 735e35fdd936d133bf8a48de140a3c666897588a05shiqian // isn't interesting to the user most of the time. The 736db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan // matcher's MatchAndExplain() method handles the case when 737e35fdd936d133bf8a48de140a3c666897588a05shiqian // the address is interesting. 738b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan internal::UniversalPrint(value, os); 739b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan PrintIfNotEmpty(listener.str(), os); 740e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "\n"; 741e35fdd936d133bf8a48de140a3c666897588a05shiqian } 742e35fdd936d133bf8a48de140a3c666897588a05shiqian } 743e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 744e35fdd936d133bf8a48de140a3c666897588a05shiqian 745e35fdd936d133bf8a48de140a3c666897588a05shiqian// The base case. 746e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <> 747e35fdd936d133bf8a48de140a3c666897588a05shiqianclass TuplePrefix<0> { 748e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 749e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename MatcherTuple, typename ValueTuple> 7503fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.wan static bool Matches(const MatcherTuple& /* matcher_tuple */, 7513fbd2dd020819fcfd7cef2aa6a17fad73c41a0eezhanyong.wan const ValueTuple& /* value_tuple */) { 752e35fdd936d133bf8a48de140a3c666897588a05shiqian return true; 753e35fdd936d133bf8a48de140a3c666897588a05shiqian } 754e35fdd936d133bf8a48de140a3c666897588a05shiqian 755e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename MatcherTuple, typename ValueTuple> 756b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan static void ExplainMatchFailuresTo(const MatcherTuple& /* matchers */, 757b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan const ValueTuple& /* values */, 758b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan ::std::ostream* /* os */) {} 759e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 760e35fdd936d133bf8a48de140a3c666897588a05shiqian 761e35fdd936d133bf8a48de140a3c666897588a05shiqian// TupleMatches(matcher_tuple, value_tuple) returns true iff all 762e35fdd936d133bf8a48de140a3c666897588a05shiqian// matchers in matcher_tuple match the corresponding fields in 763e35fdd936d133bf8a48de140a3c666897588a05shiqian// value_tuple. It is a compiler error if matcher_tuple and 764e35fdd936d133bf8a48de140a3c666897588a05shiqian// value_tuple have different number of fields or incompatible field 765e35fdd936d133bf8a48de140a3c666897588a05shiqian// types. 766e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename MatcherTuple, typename ValueTuple> 767e35fdd936d133bf8a48de140a3c666897588a05shiqianbool TupleMatches(const MatcherTuple& matcher_tuple, 768e35fdd936d133bf8a48de140a3c666897588a05shiqian const ValueTuple& value_tuple) { 769e35fdd936d133bf8a48de140a3c666897588a05shiqian using ::std::tr1::tuple_size; 770e35fdd936d133bf8a48de140a3c666897588a05shiqian // Makes sure that matcher_tuple and value_tuple have the same 771e35fdd936d133bf8a48de140a3c666897588a05shiqian // number of fields. 77202f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan GTEST_COMPILE_ASSERT_(tuple_size<MatcherTuple>::value == 773e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan tuple_size<ValueTuple>::value, 774e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan matcher_and_value_have_different_numbers_of_fields); 775e35fdd936d133bf8a48de140a3c666897588a05shiqian return TuplePrefix<tuple_size<ValueTuple>::value>:: 776e35fdd936d133bf8a48de140a3c666897588a05shiqian Matches(matcher_tuple, value_tuple); 777e35fdd936d133bf8a48de140a3c666897588a05shiqian} 778e35fdd936d133bf8a48de140a3c666897588a05shiqian 779e35fdd936d133bf8a48de140a3c666897588a05shiqian// Describes failures in matching matchers against values. If there 780e35fdd936d133bf8a48de140a3c666897588a05shiqian// is no failure, nothing will be streamed to os. 781e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename MatcherTuple, typename ValueTuple> 782b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wanvoid ExplainMatchFailureTupleTo(const MatcherTuple& matchers, 783b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan const ValueTuple& values, 784b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan ::std::ostream* os) { 785e35fdd936d133bf8a48de140a3c666897588a05shiqian using ::std::tr1::tuple_size; 786b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan TuplePrefix<tuple_size<MatcherTuple>::value>::ExplainMatchFailuresTo( 787e35fdd936d133bf8a48de140a3c666897588a05shiqian matchers, values, os); 788e35fdd936d133bf8a48de140a3c666897588a05shiqian} 789e35fdd936d133bf8a48de140a3c666897588a05shiqian 790fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// TransformTupleValues and its helper. 791fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// 792fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// TransformTupleValuesHelper hides the internal machinery that 793fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// TransformTupleValues uses to implement a tuple traversal. 794fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantemplate <typename Tuple, typename Func, typename OutIter> 795fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanclass TransformTupleValuesHelper { 796fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan private: 797fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan typedef typename ::std::tr1::tuple_size<Tuple> TupleSize; 798fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 799fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan public: 800fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // For each member of tuple 't', taken in order, evaluates '*out++ = f(t)'. 801fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // Returns the final value of 'out' in case the caller needs it. 802fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan static OutIter Run(Func f, const Tuple& t, OutIter out) { 803fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan return IterateOverTuple<Tuple, TupleSize::value>()(f, t, out); 804fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan } 805fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 806fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan private: 807fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan template <typename Tup, size_t kRemainingSize> 808fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan struct IterateOverTuple { 809fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan OutIter operator() (Func f, const Tup& t, OutIter out) const { 810fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan *out++ = f(::std::tr1::get<TupleSize::value - kRemainingSize>(t)); 811fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan return IterateOverTuple<Tup, kRemainingSize - 1>()(f, t, out); 812fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan } 813fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan }; 814fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan template <typename Tup> 815fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan struct IterateOverTuple<Tup, 0> { 816fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan OutIter operator() (Func /* f */, const Tup& /* t */, OutIter out) const { 817fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan return out; 818fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan } 819fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan }; 820fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan}; 821fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 822fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// Successively invokes 'f(element)' on each element of the tuple 't', 823fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// appending each result to the 'out' iterator. Returns the final value 824fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// of 'out'. 825fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantemplate <typename Tuple, typename Func, typename OutIter> 826fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanOutIter TransformTupleValues(Func f, const Tuple& t, OutIter out) { 827fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan return TransformTupleValuesHelper<Tuple, Func, OutIter>::Run(f, t, out); 828fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan} 829fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 830e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements A<T>(). 831e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 832e35fdd936d133bf8a48de140a3c666897588a05shiqianclass AnyMatcherImpl : public MatcherInterface<T> { 833e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 834821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual bool MatchAndExplain( 835821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan T /* x */, MatchResultListener* /* listener */) const { return true; } 836e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeTo(::std::ostream* os) const { *os << "is anything"; } 837e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeNegationTo(::std::ostream* os) const { 838e35fdd936d133bf8a48de140a3c666897588a05shiqian // This is mostly for completeness' safe, as it's not very useful 839e35fdd936d133bf8a48de140a3c666897588a05shiqian // to write Not(A<bool>()). However we cannot completely rule out 840e35fdd936d133bf8a48de140a3c666897588a05shiqian // such a possibility, and it doesn't hurt to be prepared. 841e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "never matches"; 842e35fdd936d133bf8a48de140a3c666897588a05shiqian } 843e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 844e35fdd936d133bf8a48de140a3c666897588a05shiqian 845e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements _, a matcher that matches any value of any 846e35fdd936d133bf8a48de140a3c666897588a05shiqian// type. This is a polymorphic matcher, so we need a template type 847e35fdd936d133bf8a48de140a3c666897588a05shiqian// conversion operator to make it appearing as a Matcher<T> for any 848e35fdd936d133bf8a48de140a3c666897588a05shiqian// type T. 849e35fdd936d133bf8a48de140a3c666897588a05shiqianclass AnythingMatcher { 850e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 851e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 852e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<T>() const { return A<T>(); } 853e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 854e35fdd936d133bf8a48de140a3c666897588a05shiqian 855e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements a matcher that compares a given value with a 856e35fdd936d133bf8a48de140a3c666897588a05shiqian// pre-supplied value using one of the ==, <=, <, etc, operators. The 857e35fdd936d133bf8a48de140a3c666897588a05shiqian// two values being compared don't have to have the same type. 858e35fdd936d133bf8a48de140a3c666897588a05shiqian// 859e35fdd936d133bf8a48de140a3c666897588a05shiqian// The matcher defined here is polymorphic (for example, Eq(5) can be 860e35fdd936d133bf8a48de140a3c666897588a05shiqian// used to match an int, a short, a double, etc). Therefore we use 861e35fdd936d133bf8a48de140a3c666897588a05shiqian// a template type conversion operator in the implementation. 862e35fdd936d133bf8a48de140a3c666897588a05shiqian// 863e35fdd936d133bf8a48de140a3c666897588a05shiqian// We define this as a macro in order to eliminate duplicated source 864e35fdd936d133bf8a48de140a3c666897588a05shiqian// code. 865e35fdd936d133bf8a48de140a3c666897588a05shiqian// 866e35fdd936d133bf8a48de140a3c666897588a05shiqian// The following template definition assumes that the Rhs parameter is 867e35fdd936d133bf8a48de140a3c666897588a05shiqian// a "bare" type (i.e. neither 'const T' nor 'T&'). 868b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan#define GMOCK_IMPLEMENT_COMPARISON_MATCHER_( \ 869b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan name, op, relation, negated_relation) \ 870e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename Rhs> class name##Matcher { \ 871e35fdd936d133bf8a48de140a3c666897588a05shiqian public: \ 872e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit name##Matcher(const Rhs& rhs) : rhs_(rhs) {} \ 873e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename Lhs> \ 874e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<Lhs>() const { \ 875e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakeMatcher(new Impl<Lhs>(rhs_)); \ 876e35fdd936d133bf8a48de140a3c666897588a05shiqian } \ 877e35fdd936d133bf8a48de140a3c666897588a05shiqian private: \ 878e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename Lhs> \ 879e35fdd936d133bf8a48de140a3c666897588a05shiqian class Impl : public MatcherInterface<Lhs> { \ 880e35fdd936d133bf8a48de140a3c666897588a05shiqian public: \ 881e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit Impl(const Rhs& rhs) : rhs_(rhs) {} \ 882821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual bool MatchAndExplain(\ 883821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan Lhs lhs, MatchResultListener* /* listener */) const { \ 884821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return lhs op rhs_; \ 885821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan } \ 886e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeTo(::std::ostream* os) const { \ 887b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan *os << relation " "; \ 888e2e8ba401d198d1a8304c652e997505609b62696vladlosev UniversalPrint(rhs_, os); \ 889e35fdd936d133bf8a48de140a3c666897588a05shiqian } \ 890e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeNegationTo(::std::ostream* os) const { \ 891b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan *os << negated_relation " "; \ 892e2e8ba401d198d1a8304c652e997505609b62696vladlosev UniversalPrint(rhs_, os); \ 893e35fdd936d133bf8a48de140a3c666897588a05shiqian } \ 894e35fdd936d133bf8a48de140a3c666897588a05shiqian private: \ 895e35fdd936d133bf8a48de140a3c666897588a05shiqian Rhs rhs_; \ 89632de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(Impl); \ 897e35fdd936d133bf8a48de140a3c666897588a05shiqian }; \ 898e35fdd936d133bf8a48de140a3c666897588a05shiqian Rhs rhs_; \ 89932de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(name##Matcher); \ 900e35fdd936d133bf8a48de140a3c666897588a05shiqian } 901e35fdd936d133bf8a48de140a3c666897588a05shiqian 902e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements Eq(v), Ge(v), Gt(v), Le(v), Lt(v), and Ne(v) 903e35fdd936d133bf8a48de140a3c666897588a05shiqian// respectively. 904b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Eq, ==, "is equal to", "isn't equal to"); 905b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Ge, >=, "is >=", "isn't >="); 906b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Gt, >, "is >", "isn't >"); 907b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Le, <=, "is <=", "isn't <="); 908b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Lt, <, "is <", "isn't <"); 909b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wanGMOCK_IMPLEMENT_COMPARISON_MATCHER_(Ne, !=, "isn't equal to", "is equal to"); 910e35fdd936d133bf8a48de140a3c666897588a05shiqian 911e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan#undef GMOCK_IMPLEMENT_COMPARISON_MATCHER_ 912e35fdd936d133bf8a48de140a3c666897588a05shiqian 91379b83505bcf73bf2903ebf2e2f82cb1e1f181816vladlosev// Implements the polymorphic IsNull() matcher, which matches any raw or smart 9142d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan// pointer that is NULL. 9152d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wanclass IsNullMatcher { 9162d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan public: 91779b83505bcf73bf2903ebf2e2f82cb1e1f181816vladlosev template <typename Pointer> 918db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan bool MatchAndExplain(const Pointer& p, 919db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan MatchResultListener* /* listener */) const { 920db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan return GetRawPointer(p) == NULL; 921db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan } 9222d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan 9232d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan void DescribeTo(::std::ostream* os) const { *os << "is NULL"; } 9242d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan void DescribeNegationTo(::std::ostream* os) const { 925b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan *os << "isn't NULL"; 9262d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan } 9272d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan}; 9282d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan 92979b83505bcf73bf2903ebf2e2f82cb1e1f181816vladlosev// Implements the polymorphic NotNull() matcher, which matches any raw or smart 930e35fdd936d133bf8a48de140a3c666897588a05shiqian// pointer that is not NULL. 931e35fdd936d133bf8a48de140a3c666897588a05shiqianclass NotNullMatcher { 932e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 93379b83505bcf73bf2903ebf2e2f82cb1e1f181816vladlosev template <typename Pointer> 934db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan bool MatchAndExplain(const Pointer& p, 935db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan MatchResultListener* /* listener */) const { 936db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan return GetRawPointer(p) != NULL; 937db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan } 938e35fdd936d133bf8a48de140a3c666897588a05shiqian 939b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan void DescribeTo(::std::ostream* os) const { *os << "isn't NULL"; } 940e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeNegationTo(::std::ostream* os) const { 941e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "is NULL"; 942e35fdd936d133bf8a48de140a3c666897588a05shiqian } 943e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 944e35fdd936d133bf8a48de140a3c666897588a05shiqian 945e35fdd936d133bf8a48de140a3c666897588a05shiqian// Ref(variable) matches any argument that is a reference to 946e35fdd936d133bf8a48de140a3c666897588a05shiqian// 'variable'. This matcher is polymorphic as it can match any 947e35fdd936d133bf8a48de140a3c666897588a05shiqian// super type of the type of 'variable'. 948e35fdd936d133bf8a48de140a3c666897588a05shiqian// 949e35fdd936d133bf8a48de140a3c666897588a05shiqian// The RefMatcher template class implements Ref(variable). It can 950e35fdd936d133bf8a48de140a3c666897588a05shiqian// only be instantiated with a reference type. This prevents a user 951e35fdd936d133bf8a48de140a3c666897588a05shiqian// from mistakenly using Ref(x) to match a non-reference function 952e35fdd936d133bf8a48de140a3c666897588a05shiqian// argument. For example, the following will righteously cause a 953e35fdd936d133bf8a48de140a3c666897588a05shiqian// compiler error: 954e35fdd936d133bf8a48de140a3c666897588a05shiqian// 955e35fdd936d133bf8a48de140a3c666897588a05shiqian// int n; 956e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matcher<int> m1 = Ref(n); // This won't compile. 957e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matcher<int&> m2 = Ref(n); // This will compile. 958e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 959e35fdd936d133bf8a48de140a3c666897588a05shiqianclass RefMatcher; 960e35fdd936d133bf8a48de140a3c666897588a05shiqian 961e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 962e35fdd936d133bf8a48de140a3c666897588a05shiqianclass RefMatcher<T&> { 963e35fdd936d133bf8a48de140a3c666897588a05shiqian // Google Mock is a generic framework and thus needs to support 964e35fdd936d133bf8a48de140a3c666897588a05shiqian // mocking any function types, including those that take non-const 965e35fdd936d133bf8a48de140a3c666897588a05shiqian // reference arguments. Therefore the template parameter T (and 966e35fdd936d133bf8a48de140a3c666897588a05shiqian // Super below) can be instantiated to either a const type or a 967e35fdd936d133bf8a48de140a3c666897588a05shiqian // non-const type. 968e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 969e35fdd936d133bf8a48de140a3c666897588a05shiqian // RefMatcher() takes a T& instead of const T&, as we want the 970e35fdd936d133bf8a48de140a3c666897588a05shiqian // compiler to catch using Ref(const_value) as a matcher for a 971e35fdd936d133bf8a48de140a3c666897588a05shiqian // non-const reference. 972e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit RefMatcher(T& x) : object_(x) {} // NOLINT 973e35fdd936d133bf8a48de140a3c666897588a05shiqian 974e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename Super> 975e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<Super&>() const { 976e35fdd936d133bf8a48de140a3c666897588a05shiqian // By passing object_ (type T&) to Impl(), which expects a Super&, 977e35fdd936d133bf8a48de140a3c666897588a05shiqian // we make sure that Super is a super type of T. In particular, 978e35fdd936d133bf8a48de140a3c666897588a05shiqian // this catches using Ref(const_value) as a matcher for a 979e35fdd936d133bf8a48de140a3c666897588a05shiqian // non-const reference, as you cannot implicitly convert a const 980e35fdd936d133bf8a48de140a3c666897588a05shiqian // reference to a non-const reference. 981e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakeMatcher(new Impl<Super>(object_)); 982e35fdd936d133bf8a48de140a3c666897588a05shiqian } 98332de5f53763125925e078498250f7e73a88de9edzhanyong.wan 984e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 985e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename Super> 986e35fdd936d133bf8a48de140a3c666897588a05shiqian class Impl : public MatcherInterface<Super&> { 987e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 988e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit Impl(Super& x) : object_(x) {} // NOLINT 989e35fdd936d133bf8a48de140a3c666897588a05shiqian 990db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan // MatchAndExplain() takes a Super& (as opposed to const Super&) 991db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan // in order to match the interface MatcherInterface<Super&>. 992821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual bool MatchAndExplain( 993821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan Super& x, MatchResultListener* listener) const { 994b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan *listener << "which is located @" << static_cast<const void*>(&x); 995821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return &x == &object_; 996821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan } 997e35fdd936d133bf8a48de140a3c666897588a05shiqian 998e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeTo(::std::ostream* os) const { 999e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "references the variable "; 1000e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<Super&>::Print(object_, os); 1001e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1002e35fdd936d133bf8a48de140a3c666897588a05shiqian 1003e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeNegationTo(::std::ostream* os) const { 1004e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "does not reference the variable "; 1005e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<Super&>::Print(object_, os); 1006e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1007e35fdd936d133bf8a48de140a3c666897588a05shiqian 1008e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1009e35fdd936d133bf8a48de140a3c666897588a05shiqian const Super& object_; 101032de5f53763125925e078498250f7e73a88de9edzhanyong.wan 101132de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(Impl); 1012e35fdd936d133bf8a48de140a3c666897588a05shiqian }; 1013e35fdd936d133bf8a48de140a3c666897588a05shiqian 1014e35fdd936d133bf8a48de140a3c666897588a05shiqian T& object_; 101532de5f53763125925e078498250f7e73a88de9edzhanyong.wan 101632de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(RefMatcher); 1017e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1018e35fdd936d133bf8a48de140a3c666897588a05shiqian 1019e35fdd936d133bf8a48de140a3c666897588a05shiqian// Polymorphic helper functions for narrow and wide string matchers. 1020e35fdd936d133bf8a48de140a3c666897588a05shiqianinline bool CaseInsensitiveCStringEquals(const char* lhs, const char* rhs) { 1021e35fdd936d133bf8a48de140a3c666897588a05shiqian return String::CaseInsensitiveCStringEquals(lhs, rhs); 1022e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1023e35fdd936d133bf8a48de140a3c666897588a05shiqian 1024e35fdd936d133bf8a48de140a3c666897588a05shiqianinline bool CaseInsensitiveCStringEquals(const wchar_t* lhs, 1025e35fdd936d133bf8a48de140a3c666897588a05shiqian const wchar_t* rhs) { 1026e35fdd936d133bf8a48de140a3c666897588a05shiqian return String::CaseInsensitiveWideCStringEquals(lhs, rhs); 1027e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1028e35fdd936d133bf8a48de140a3c666897588a05shiqian 1029e35fdd936d133bf8a48de140a3c666897588a05shiqian// String comparison for narrow or wide strings that can have embedded NUL 1030e35fdd936d133bf8a48de140a3c666897588a05shiqian// characters. 1031e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType> 1032e35fdd936d133bf8a48de140a3c666897588a05shiqianbool CaseInsensitiveStringEquals(const StringType& s1, 1033e35fdd936d133bf8a48de140a3c666897588a05shiqian const StringType& s2) { 1034e35fdd936d133bf8a48de140a3c666897588a05shiqian // Are the heads equal? 1035e35fdd936d133bf8a48de140a3c666897588a05shiqian if (!CaseInsensitiveCStringEquals(s1.c_str(), s2.c_str())) { 1036e35fdd936d133bf8a48de140a3c666897588a05shiqian return false; 1037e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1038e35fdd936d133bf8a48de140a3c666897588a05shiqian 1039e35fdd936d133bf8a48de140a3c666897588a05shiqian // Skip the equal heads. 1040e35fdd936d133bf8a48de140a3c666897588a05shiqian const typename StringType::value_type nul = 0; 1041e35fdd936d133bf8a48de140a3c666897588a05shiqian const size_t i1 = s1.find(nul), i2 = s2.find(nul); 1042e35fdd936d133bf8a48de140a3c666897588a05shiqian 1043e35fdd936d133bf8a48de140a3c666897588a05shiqian // Are we at the end of either s1 or s2? 1044e35fdd936d133bf8a48de140a3c666897588a05shiqian if (i1 == StringType::npos || i2 == StringType::npos) { 1045e35fdd936d133bf8a48de140a3c666897588a05shiqian return i1 == i2; 1046e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1047e35fdd936d133bf8a48de140a3c666897588a05shiqian 1048e35fdd936d133bf8a48de140a3c666897588a05shiqian // Are the tails equal? 1049e35fdd936d133bf8a48de140a3c666897588a05shiqian return CaseInsensitiveStringEquals(s1.substr(i1 + 1), s2.substr(i2 + 1)); 1050e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1051e35fdd936d133bf8a48de140a3c666897588a05shiqian 1052e35fdd936d133bf8a48de140a3c666897588a05shiqian// String matchers. 1053e35fdd936d133bf8a48de140a3c666897588a05shiqian 1054e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements equality-based string matchers like StrEq, StrCaseNe, and etc. 1055e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType> 1056e35fdd936d133bf8a48de140a3c666897588a05shiqianclass StrEqualityMatcher { 1057e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1058e35fdd936d133bf8a48de140a3c666897588a05shiqian StrEqualityMatcher(const StringType& str, bool expect_eq, 1059e35fdd936d133bf8a48de140a3c666897588a05shiqian bool case_sensitive) 1060e35fdd936d133bf8a48de140a3c666897588a05shiqian : string_(str), expect_eq_(expect_eq), case_sensitive_(case_sensitive) {} 1061e35fdd936d133bf8a48de140a3c666897588a05shiqian 106238513a8bb154f0b6d0a4088814fe92552696d465jgm // Accepts pointer types, particularly: 106338513a8bb154f0b6d0a4088814fe92552696d465jgm // const char* 106438513a8bb154f0b6d0a4088814fe92552696d465jgm // char* 106538513a8bb154f0b6d0a4088814fe92552696d465jgm // const wchar_t* 106638513a8bb154f0b6d0a4088814fe92552696d465jgm // wchar_t* 106738513a8bb154f0b6d0a4088814fe92552696d465jgm template <typename CharType> 106838513a8bb154f0b6d0a4088814fe92552696d465jgm bool MatchAndExplain(CharType* s, MatchResultListener* listener) const { 1069e35fdd936d133bf8a48de140a3c666897588a05shiqian if (s == NULL) { 1070e35fdd936d133bf8a48de140a3c666897588a05shiqian return !expect_eq_; 1071e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1072db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan return MatchAndExplain(StringType(s), listener); 1073e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1074e35fdd936d133bf8a48de140a3c666897588a05shiqian 107538513a8bb154f0b6d0a4088814fe92552696d465jgm // Matches anything that can convert to StringType. 107638513a8bb154f0b6d0a4088814fe92552696d465jgm // 107738513a8bb154f0b6d0a4088814fe92552696d465jgm // This is a template, not just a plain function with const StringType&, 107838513a8bb154f0b6d0a4088814fe92552696d465jgm // because StringPiece has some interfering non-explicit constructors. 107938513a8bb154f0b6d0a4088814fe92552696d465jgm template <typename MatcheeStringType> 108038513a8bb154f0b6d0a4088814fe92552696d465jgm bool MatchAndExplain(const MatcheeStringType& s, 1081db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan MatchResultListener* /* listener */) const { 108238513a8bb154f0b6d0a4088814fe92552696d465jgm const StringType& s2(s); 108338513a8bb154f0b6d0a4088814fe92552696d465jgm const bool eq = case_sensitive_ ? s2 == string_ : 108438513a8bb154f0b6d0a4088814fe92552696d465jgm CaseInsensitiveStringEquals(s2, string_); 1085e35fdd936d133bf8a48de140a3c666897588a05shiqian return expect_eq_ == eq; 1086e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1087e35fdd936d133bf8a48de140a3c666897588a05shiqian 1088e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeTo(::std::ostream* os) const { 1089e35fdd936d133bf8a48de140a3c666897588a05shiqian DescribeToHelper(expect_eq_, os); 1090e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1091e35fdd936d133bf8a48de140a3c666897588a05shiqian 1092e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeNegationTo(::std::ostream* os) const { 1093e35fdd936d133bf8a48de140a3c666897588a05shiqian DescribeToHelper(!expect_eq_, os); 1094e35fdd936d133bf8a48de140a3c666897588a05shiqian } 109532de5f53763125925e078498250f7e73a88de9edzhanyong.wan 1096e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1097e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeToHelper(bool expect_eq, ::std::ostream* os) const { 1098b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan *os << (expect_eq ? "is " : "isn't "); 1099e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "equal to "; 1100e35fdd936d133bf8a48de140a3c666897588a05shiqian if (!case_sensitive_) { 1101e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "(ignoring case) "; 1102e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1103e2e8ba401d198d1a8304c652e997505609b62696vladlosev UniversalPrint(string_, os); 1104e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1105e35fdd936d133bf8a48de140a3c666897588a05shiqian 1106e35fdd936d133bf8a48de140a3c666897588a05shiqian const StringType string_; 1107e35fdd936d133bf8a48de140a3c666897588a05shiqian const bool expect_eq_; 1108e35fdd936d133bf8a48de140a3c666897588a05shiqian const bool case_sensitive_; 110932de5f53763125925e078498250f7e73a88de9edzhanyong.wan 111032de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(StrEqualityMatcher); 1111e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1112e35fdd936d133bf8a48de140a3c666897588a05shiqian 1113e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the polymorphic HasSubstr(substring) matcher, which 1114e35fdd936d133bf8a48de140a3c666897588a05shiqian// can be used as a Matcher<T> as long as T can be converted to a 1115e35fdd936d133bf8a48de140a3c666897588a05shiqian// string. 1116e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType> 1117e35fdd936d133bf8a48de140a3c666897588a05shiqianclass HasSubstrMatcher { 1118e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1119e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit HasSubstrMatcher(const StringType& substring) 1120e35fdd936d133bf8a48de140a3c666897588a05shiqian : substring_(substring) {} 1121e35fdd936d133bf8a48de140a3c666897588a05shiqian 112238513a8bb154f0b6d0a4088814fe92552696d465jgm // Accepts pointer types, particularly: 112338513a8bb154f0b6d0a4088814fe92552696d465jgm // const char* 112438513a8bb154f0b6d0a4088814fe92552696d465jgm // char* 112538513a8bb154f0b6d0a4088814fe92552696d465jgm // const wchar_t* 112638513a8bb154f0b6d0a4088814fe92552696d465jgm // wchar_t* 112738513a8bb154f0b6d0a4088814fe92552696d465jgm template <typename CharType> 112838513a8bb154f0b6d0a4088814fe92552696d465jgm bool MatchAndExplain(CharType* s, MatchResultListener* listener) const { 1129db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan return s != NULL && MatchAndExplain(StringType(s), listener); 1130e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1131e35fdd936d133bf8a48de140a3c666897588a05shiqian 113238513a8bb154f0b6d0a4088814fe92552696d465jgm // Matches anything that can convert to StringType. 113338513a8bb154f0b6d0a4088814fe92552696d465jgm // 113438513a8bb154f0b6d0a4088814fe92552696d465jgm // This is a template, not just a plain function with const StringType&, 113538513a8bb154f0b6d0a4088814fe92552696d465jgm // because StringPiece has some interfering non-explicit constructors. 113638513a8bb154f0b6d0a4088814fe92552696d465jgm template <typename MatcheeStringType> 113738513a8bb154f0b6d0a4088814fe92552696d465jgm bool MatchAndExplain(const MatcheeStringType& s, 1138db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan MatchResultListener* /* listener */) const { 113938513a8bb154f0b6d0a4088814fe92552696d465jgm const StringType& s2(s); 114038513a8bb154f0b6d0a4088814fe92552696d465jgm return s2.find(substring_) != StringType::npos; 1141e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1142e35fdd936d133bf8a48de140a3c666897588a05shiqian 1143e35fdd936d133bf8a48de140a3c666897588a05shiqian // Describes what this matcher matches. 1144e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeTo(::std::ostream* os) const { 1145e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "has substring "; 1146e2e8ba401d198d1a8304c652e997505609b62696vladlosev UniversalPrint(substring_, os); 1147e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1148e35fdd936d133bf8a48de140a3c666897588a05shiqian 1149e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeNegationTo(::std::ostream* os) const { 1150e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "has no substring "; 1151e2e8ba401d198d1a8304c652e997505609b62696vladlosev UniversalPrint(substring_, os); 1152e35fdd936d133bf8a48de140a3c666897588a05shiqian } 115332de5f53763125925e078498250f7e73a88de9edzhanyong.wan 1154e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1155e35fdd936d133bf8a48de140a3c666897588a05shiqian const StringType substring_; 115632de5f53763125925e078498250f7e73a88de9edzhanyong.wan 115732de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(HasSubstrMatcher); 1158e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1159e35fdd936d133bf8a48de140a3c666897588a05shiqian 1160e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the polymorphic StartsWith(substring) matcher, which 1161e35fdd936d133bf8a48de140a3c666897588a05shiqian// can be used as a Matcher<T> as long as T can be converted to a 1162e35fdd936d133bf8a48de140a3c666897588a05shiqian// string. 1163e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType> 1164e35fdd936d133bf8a48de140a3c666897588a05shiqianclass StartsWithMatcher { 1165e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1166e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit StartsWithMatcher(const StringType& prefix) : prefix_(prefix) { 1167e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1168e35fdd936d133bf8a48de140a3c666897588a05shiqian 116938513a8bb154f0b6d0a4088814fe92552696d465jgm // Accepts pointer types, particularly: 117038513a8bb154f0b6d0a4088814fe92552696d465jgm // const char* 117138513a8bb154f0b6d0a4088814fe92552696d465jgm // char* 117238513a8bb154f0b6d0a4088814fe92552696d465jgm // const wchar_t* 117338513a8bb154f0b6d0a4088814fe92552696d465jgm // wchar_t* 117438513a8bb154f0b6d0a4088814fe92552696d465jgm template <typename CharType> 117538513a8bb154f0b6d0a4088814fe92552696d465jgm bool MatchAndExplain(CharType* s, MatchResultListener* listener) const { 1176db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan return s != NULL && MatchAndExplain(StringType(s), listener); 1177e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1178e35fdd936d133bf8a48de140a3c666897588a05shiqian 117938513a8bb154f0b6d0a4088814fe92552696d465jgm // Matches anything that can convert to StringType. 118038513a8bb154f0b6d0a4088814fe92552696d465jgm // 118138513a8bb154f0b6d0a4088814fe92552696d465jgm // This is a template, not just a plain function with const StringType&, 118238513a8bb154f0b6d0a4088814fe92552696d465jgm // because StringPiece has some interfering non-explicit constructors. 118338513a8bb154f0b6d0a4088814fe92552696d465jgm template <typename MatcheeStringType> 118438513a8bb154f0b6d0a4088814fe92552696d465jgm bool MatchAndExplain(const MatcheeStringType& s, 1185db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan MatchResultListener* /* listener */) const { 118638513a8bb154f0b6d0a4088814fe92552696d465jgm const StringType& s2(s); 118738513a8bb154f0b6d0a4088814fe92552696d465jgm return s2.length() >= prefix_.length() && 118838513a8bb154f0b6d0a4088814fe92552696d465jgm s2.substr(0, prefix_.length()) == prefix_; 1189e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1190e35fdd936d133bf8a48de140a3c666897588a05shiqian 1191e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeTo(::std::ostream* os) const { 1192e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "starts with "; 1193e2e8ba401d198d1a8304c652e997505609b62696vladlosev UniversalPrint(prefix_, os); 1194e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1195e35fdd936d133bf8a48de140a3c666897588a05shiqian 1196e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeNegationTo(::std::ostream* os) const { 1197e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "doesn't start with "; 1198e2e8ba401d198d1a8304c652e997505609b62696vladlosev UniversalPrint(prefix_, os); 1199e35fdd936d133bf8a48de140a3c666897588a05shiqian } 120032de5f53763125925e078498250f7e73a88de9edzhanyong.wan 1201e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1202e35fdd936d133bf8a48de140a3c666897588a05shiqian const StringType prefix_; 120332de5f53763125925e078498250f7e73a88de9edzhanyong.wan 120432de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(StartsWithMatcher); 1205e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1206e35fdd936d133bf8a48de140a3c666897588a05shiqian 1207e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the polymorphic EndsWith(substring) matcher, which 1208e35fdd936d133bf8a48de140a3c666897588a05shiqian// can be used as a Matcher<T> as long as T can be converted to a 1209e35fdd936d133bf8a48de140a3c666897588a05shiqian// string. 1210e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename StringType> 1211e35fdd936d133bf8a48de140a3c666897588a05shiqianclass EndsWithMatcher { 1212e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1213e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit EndsWithMatcher(const StringType& suffix) : suffix_(suffix) {} 1214e35fdd936d133bf8a48de140a3c666897588a05shiqian 121538513a8bb154f0b6d0a4088814fe92552696d465jgm // Accepts pointer types, particularly: 121638513a8bb154f0b6d0a4088814fe92552696d465jgm // const char* 121738513a8bb154f0b6d0a4088814fe92552696d465jgm // char* 121838513a8bb154f0b6d0a4088814fe92552696d465jgm // const wchar_t* 121938513a8bb154f0b6d0a4088814fe92552696d465jgm // wchar_t* 122038513a8bb154f0b6d0a4088814fe92552696d465jgm template <typename CharType> 122138513a8bb154f0b6d0a4088814fe92552696d465jgm bool MatchAndExplain(CharType* s, MatchResultListener* listener) const { 1222db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan return s != NULL && MatchAndExplain(StringType(s), listener); 1223e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1224e35fdd936d133bf8a48de140a3c666897588a05shiqian 122538513a8bb154f0b6d0a4088814fe92552696d465jgm // Matches anything that can convert to StringType. 122638513a8bb154f0b6d0a4088814fe92552696d465jgm // 122738513a8bb154f0b6d0a4088814fe92552696d465jgm // This is a template, not just a plain function with const StringType&, 122838513a8bb154f0b6d0a4088814fe92552696d465jgm // because StringPiece has some interfering non-explicit constructors. 122938513a8bb154f0b6d0a4088814fe92552696d465jgm template <typename MatcheeStringType> 123038513a8bb154f0b6d0a4088814fe92552696d465jgm bool MatchAndExplain(const MatcheeStringType& s, 1231db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan MatchResultListener* /* listener */) const { 123238513a8bb154f0b6d0a4088814fe92552696d465jgm const StringType& s2(s); 123338513a8bb154f0b6d0a4088814fe92552696d465jgm return s2.length() >= suffix_.length() && 123438513a8bb154f0b6d0a4088814fe92552696d465jgm s2.substr(s2.length() - suffix_.length()) == suffix_; 1235e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1236e35fdd936d133bf8a48de140a3c666897588a05shiqian 1237e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeTo(::std::ostream* os) const { 1238e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "ends with "; 1239e2e8ba401d198d1a8304c652e997505609b62696vladlosev UniversalPrint(suffix_, os); 1240e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1241e35fdd936d133bf8a48de140a3c666897588a05shiqian 1242e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeNegationTo(::std::ostream* os) const { 1243e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "doesn't end with "; 1244e2e8ba401d198d1a8304c652e997505609b62696vladlosev UniversalPrint(suffix_, os); 1245e35fdd936d133bf8a48de140a3c666897588a05shiqian } 124632de5f53763125925e078498250f7e73a88de9edzhanyong.wan 1247e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1248e35fdd936d133bf8a48de140a3c666897588a05shiqian const StringType suffix_; 124932de5f53763125925e078498250f7e73a88de9edzhanyong.wan 125032de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(EndsWithMatcher); 1251e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1252e35fdd936d133bf8a48de140a3c666897588a05shiqian 1253e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements polymorphic matchers MatchesRegex(regex) and 1254e35fdd936d133bf8a48de140a3c666897588a05shiqian// ContainsRegex(regex), which can be used as a Matcher<T> as long as 1255e35fdd936d133bf8a48de140a3c666897588a05shiqian// T can be converted to a string. 1256e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatchesRegexMatcher { 1257e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1258e35fdd936d133bf8a48de140a3c666897588a05shiqian MatchesRegexMatcher(const RE* regex, bool full_match) 1259e35fdd936d133bf8a48de140a3c666897588a05shiqian : regex_(regex), full_match_(full_match) {} 1260e35fdd936d133bf8a48de140a3c666897588a05shiqian 126138513a8bb154f0b6d0a4088814fe92552696d465jgm // Accepts pointer types, particularly: 126238513a8bb154f0b6d0a4088814fe92552696d465jgm // const char* 126338513a8bb154f0b6d0a4088814fe92552696d465jgm // char* 126438513a8bb154f0b6d0a4088814fe92552696d465jgm // const wchar_t* 126538513a8bb154f0b6d0a4088814fe92552696d465jgm // wchar_t* 126638513a8bb154f0b6d0a4088814fe92552696d465jgm template <typename CharType> 126738513a8bb154f0b6d0a4088814fe92552696d465jgm bool MatchAndExplain(CharType* s, MatchResultListener* listener) const { 1268db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan return s != NULL && MatchAndExplain(internal::string(s), listener); 1269e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1270e35fdd936d133bf8a48de140a3c666897588a05shiqian 127138513a8bb154f0b6d0a4088814fe92552696d465jgm // Matches anything that can convert to internal::string. 127238513a8bb154f0b6d0a4088814fe92552696d465jgm // 127338513a8bb154f0b6d0a4088814fe92552696d465jgm // This is a template, not just a plain function with const internal::string&, 127438513a8bb154f0b6d0a4088814fe92552696d465jgm // because StringPiece has some interfering non-explicit constructors. 127538513a8bb154f0b6d0a4088814fe92552696d465jgm template <class MatcheeStringType> 127638513a8bb154f0b6d0a4088814fe92552696d465jgm bool MatchAndExplain(const MatcheeStringType& s, 1277db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan MatchResultListener* /* listener */) const { 127838513a8bb154f0b6d0a4088814fe92552696d465jgm const internal::string& s2(s); 127938513a8bb154f0b6d0a4088814fe92552696d465jgm return full_match_ ? RE::FullMatch(s2, *regex_) : 128038513a8bb154f0b6d0a4088814fe92552696d465jgm RE::PartialMatch(s2, *regex_); 1281e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1282e35fdd936d133bf8a48de140a3c666897588a05shiqian 1283e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeTo(::std::ostream* os) const { 1284e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << (full_match_ ? "matches" : "contains") 1285e35fdd936d133bf8a48de140a3c666897588a05shiqian << " regular expression "; 1286e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<internal::string>::Print(regex_->pattern(), os); 1287e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1288e35fdd936d133bf8a48de140a3c666897588a05shiqian 1289e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeNegationTo(::std::ostream* os) const { 1290e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "doesn't " << (full_match_ ? "match" : "contain") 1291e35fdd936d133bf8a48de140a3c666897588a05shiqian << " regular expression "; 1292e35fdd936d133bf8a48de140a3c666897588a05shiqian UniversalPrinter<internal::string>::Print(regex_->pattern(), os); 1293e35fdd936d133bf8a48de140a3c666897588a05shiqian } 129432de5f53763125925e078498250f7e73a88de9edzhanyong.wan 1295e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1296e35fdd936d133bf8a48de140a3c666897588a05shiqian const internal::linked_ptr<const RE> regex_; 1297e35fdd936d133bf8a48de140a3c666897588a05shiqian const bool full_match_; 129832de5f53763125925e078498250f7e73a88de9edzhanyong.wan 129932de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(MatchesRegexMatcher); 1300e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1301e35fdd936d133bf8a48de140a3c666897588a05shiqian 1302e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements a matcher that compares the two fields of a 2-tuple 1303e35fdd936d133bf8a48de140a3c666897588a05shiqian// using one of the ==, <=, <, etc, operators. The two fields being 1304e35fdd936d133bf8a48de140a3c666897588a05shiqian// compared don't have to have the same type. 1305e35fdd936d133bf8a48de140a3c666897588a05shiqian// 1306e35fdd936d133bf8a48de140a3c666897588a05shiqian// The matcher defined here is polymorphic (for example, Eq() can be 1307e35fdd936d133bf8a48de140a3c666897588a05shiqian// used to match a tuple<int, short>, a tuple<const long&, double>, 1308e35fdd936d133bf8a48de140a3c666897588a05shiqian// etc). Therefore we use a template type conversion operator in the 1309e35fdd936d133bf8a48de140a3c666897588a05shiqian// implementation. 1310e35fdd936d133bf8a48de140a3c666897588a05shiqian// 1311e35fdd936d133bf8a48de140a3c666897588a05shiqian// We define this as a macro in order to eliminate duplicated source 1312e35fdd936d133bf8a48de140a3c666897588a05shiqian// code. 1313ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan#define GMOCK_IMPLEMENT_COMPARISON2_MATCHER_(name, op, relation) \ 1314e35fdd936d133bf8a48de140a3c666897588a05shiqian class name##2Matcher { \ 1315e35fdd936d133bf8a48de140a3c666897588a05shiqian public: \ 1316e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T1, typename T2> \ 1317ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan operator Matcher< ::std::tr1::tuple<T1, T2> >() const { \ 1318ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan return MakeMatcher(new Impl< ::std::tr1::tuple<T1, T2> >); \ 1319ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan } \ 1320ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan template <typename T1, typename T2> \ 1321e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<const ::std::tr1::tuple<T1, T2>&>() const { \ 1322ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan return MakeMatcher(new Impl<const ::std::tr1::tuple<T1, T2>&>); \ 1323e35fdd936d133bf8a48de140a3c666897588a05shiqian } \ 1324e35fdd936d133bf8a48de140a3c666897588a05shiqian private: \ 1325ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan template <typename Tuple> \ 1326ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan class Impl : public MatcherInterface<Tuple> { \ 1327e35fdd936d133bf8a48de140a3c666897588a05shiqian public: \ 1328821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual bool MatchAndExplain( \ 1329ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan Tuple args, \ 1330821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan MatchResultListener* /* listener */) const { \ 1331e35fdd936d133bf8a48de140a3c666897588a05shiqian return ::std::tr1::get<0>(args) op ::std::tr1::get<1>(args); \ 1332e35fdd936d133bf8a48de140a3c666897588a05shiqian } \ 1333e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeTo(::std::ostream* os) const { \ 1334ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan *os << "are " relation; \ 1335e35fdd936d133bf8a48de140a3c666897588a05shiqian } \ 1336e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeNegationTo(::std::ostream* os) const { \ 1337ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan *os << "aren't " relation; \ 1338e35fdd936d133bf8a48de140a3c666897588a05shiqian } \ 1339e35fdd936d133bf8a48de140a3c666897588a05shiqian }; \ 1340e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1341e35fdd936d133bf8a48de140a3c666897588a05shiqian 1342e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements Eq(), Ge(), Gt(), Le(), Lt(), and Ne() respectively. 1343ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Eq, ==, "an equal pair"); 1344ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_( 1345ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan Ge, >=, "a pair where the first >= the second"); 1346ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_( 1347ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan Gt, >, "a pair where the first > the second"); 1348ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_( 1349ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan Le, <=, "a pair where the first <= the second"); 1350ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_( 1351ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan Lt, <, "a pair where the first < the second"); 1352ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wanGMOCK_IMPLEMENT_COMPARISON2_MATCHER_(Ne, !=, "an unequal pair"); 1353e35fdd936d133bf8a48de140a3c666897588a05shiqian 1354e0d051ea64dd5f32d5b6af9831747d1acb2a9c40zhanyong.wan#undef GMOCK_IMPLEMENT_COMPARISON2_MATCHER_ 1355e35fdd936d133bf8a48de140a3c666897588a05shiqian 1356c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// Implements the Not(...) matcher for a particular argument type T. 1357c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// We do not nest it inside the NotMatcher class template, as that 1358c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// will prevent different instantiations of NotMatcher from sharing 1359c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// the same NotMatcherImpl<T> class. 1360c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wantemplate <typename T> 1361c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wanclass NotMatcherImpl : public MatcherInterface<T> { 1362c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan public: 1363c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan explicit NotMatcherImpl(const Matcher<T>& matcher) 1364c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan : matcher_(matcher) {} 1365c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan 1366821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual bool MatchAndExplain(T x, MatchResultListener* listener) const { 1367821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return !matcher_.MatchAndExplain(x, listener); 1368c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 1369c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan 1370c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan virtual void DescribeTo(::std::ostream* os) const { 1371c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan matcher_.DescribeNegationTo(os); 1372c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 1373c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan 1374c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan virtual void DescribeNegationTo(::std::ostream* os) const { 1375c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan matcher_.DescribeTo(os); 1376c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 1377c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan 1378c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan private: 1379c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan const Matcher<T> matcher_; 138032de5f53763125925e078498250f7e73a88de9edzhanyong.wan 138132de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(NotMatcherImpl); 1382c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan}; 1383c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan 1384e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the Not(m) matcher, which matches a value that doesn't 1385e35fdd936d133bf8a48de140a3c666897588a05shiqian// match matcher m. 1386e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename InnerMatcher> 1387e35fdd936d133bf8a48de140a3c666897588a05shiqianclass NotMatcher { 1388e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1389e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit NotMatcher(InnerMatcher matcher) : matcher_(matcher) {} 1390e35fdd936d133bf8a48de140a3c666897588a05shiqian 1391e35fdd936d133bf8a48de140a3c666897588a05shiqian // This template type conversion operator allows Not(m) to be used 1392e35fdd936d133bf8a48de140a3c666897588a05shiqian // to match any type m can match. 1393e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 1394e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<T>() const { 1395c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan return Matcher<T>(new NotMatcherImpl<T>(SafeMatcherCast<T>(matcher_))); 1396e35fdd936d133bf8a48de140a3c666897588a05shiqian } 139732de5f53763125925e078498250f7e73a88de9edzhanyong.wan 1398e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1399c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan InnerMatcher matcher_; 140032de5f53763125925e078498250f7e73a88de9edzhanyong.wan 140132de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(NotMatcher); 1402c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan}; 1403e35fdd936d133bf8a48de140a3c666897588a05shiqian 1404c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// Implements the AllOf(m1, m2) matcher for a particular argument type 1405c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// T. We do not nest it inside the BothOfMatcher class template, as 1406c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// that will prevent different instantiations of BothOfMatcher from 1407c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// sharing the same BothOfMatcherImpl<T> class. 1408c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wantemplate <typename T> 1409c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wanclass BothOfMatcherImpl : public MatcherInterface<T> { 1410c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan public: 1411c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan BothOfMatcherImpl(const Matcher<T>& matcher1, const Matcher<T>& matcher2) 1412c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan : matcher1_(matcher1), matcher2_(matcher2) {} 1413e35fdd936d133bf8a48de140a3c666897588a05shiqian 1414c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan virtual void DescribeTo(::std::ostream* os) const { 1415c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan *os << "("; 1416c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan matcher1_.DescribeTo(os); 1417c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan *os << ") and ("; 1418c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan matcher2_.DescribeTo(os); 1419c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan *os << ")"; 1420c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 1421e35fdd936d133bf8a48de140a3c666897588a05shiqian 1422c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan virtual void DescribeNegationTo(::std::ostream* os) const { 1423b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan *os << "("; 1424b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan matcher1_.DescribeNegationTo(os); 1425b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan *os << ") or ("; 1426b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan matcher2_.DescribeNegationTo(os); 1427b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan *os << ")"; 1428c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 1429e35fdd936d133bf8a48de140a3c666897588a05shiqian 1430821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual bool MatchAndExplain(T x, MatchResultListener* listener) const { 1431821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // If either matcher1_ or matcher2_ doesn't match x, we only need 1432821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // to explain why one of them fails. 1433821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan StringMatchResultListener listener1; 1434821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (!matcher1_.MatchAndExplain(x, &listener1)) { 1435821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << listener1.str(); 1436821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return false; 1437821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan } 1438c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan 1439821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan StringMatchResultListener listener2; 1440821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (!matcher2_.MatchAndExplain(x, &listener2)) { 1441821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << listener2.str(); 1442821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return false; 1443821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan } 1444c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan 1445821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // Otherwise we need to explain why *both* of them match. 1446821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan const internal::string s1 = listener1.str(); 1447821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan const internal::string s2 = listener2.str(); 1448821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 1449821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (s1 == "") { 1450821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << s2; 1451c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } else { 1452821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << s1; 1453821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (s2 != "") { 1454b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan *listener << ", and " << s2; 1455c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 1456c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 1457821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return true; 1458c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 145932de5f53763125925e078498250f7e73a88de9edzhanyong.wan 1460c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan private: 1461c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan const Matcher<T> matcher1_; 1462c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan const Matcher<T> matcher2_; 146332de5f53763125925e078498250f7e73a88de9edzhanyong.wan 146432de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(BothOfMatcherImpl); 1465e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1466e35fdd936d133bf8a48de140a3c666897588a05shiqian 1467616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan#if GTEST_LANG_CXX11 1468616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// MatcherList provides mechanisms for storing a variable number of matchers in 1469616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// a list structure (ListType) and creating a combining matcher from such a 1470616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// list. 1471616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// The template is defined recursively using the following template paramters: 1472616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// * kSize is the length of the MatcherList. 1473616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// * Head is the type of the first matcher of the list. 1474616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// * Tail denotes the types of the remaining matchers of the list. 1475616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wantemplate <int kSize, typename Head, typename... Tail> 1476616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wanstruct MatcherList { 1477616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan typedef MatcherList<kSize - 1, Tail...> MatcherListTail; 14782989703ed85154ae7ef90e0d754590116ecb565dzhanyong.wan typedef ::std::pair<Head, typename MatcherListTail::ListType> ListType; 1479616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan 1480616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan // BuildList stores variadic type values in a nested pair structure. 1481616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan // Example: 1482616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan // MatcherList<3, int, string, float>::BuildList(5, "foo", 2.0) will return 1483616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan // the corresponding result of type pair<int, pair<string, float>>. 1484616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan static ListType BuildList(const Head& matcher, const Tail&... tail) { 1485616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan return ListType(matcher, MatcherListTail::BuildList(tail...)); 1486616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan } 1487616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan 1488616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan // CreateMatcher<T> creates a Matcher<T> from a given list of matchers (built 1489616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan // by BuildList()). CombiningMatcher<T> is used to combine the matchers of the 1490616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan // list. CombiningMatcher<T> must implement MatcherInterface<T> and have a 1491616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan // constructor taking two Matcher<T>s as input. 1492616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan template <typename T, template <typename /* T */> class CombiningMatcher> 1493616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan static Matcher<T> CreateMatcher(const ListType& matchers) { 1494616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan return Matcher<T>(new CombiningMatcher<T>( 1495616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan SafeMatcherCast<T>(matchers.first), 1496616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan MatcherListTail::template CreateMatcher<T, CombiningMatcher>( 1497616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan matchers.second))); 1498616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan } 1499616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan}; 1500616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan 1501616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// The following defines the base case for the recursive definition of 1502616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// MatcherList. 1503616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wantemplate <typename Matcher1, typename Matcher2> 1504616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wanstruct MatcherList<2, Matcher1, Matcher2> { 15052989703ed85154ae7ef90e0d754590116ecb565dzhanyong.wan typedef ::std::pair<Matcher1, Matcher2> ListType; 1506616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan 1507616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan static ListType BuildList(const Matcher1& matcher1, 1508616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan const Matcher2& matcher2) { 15092989703ed85154ae7ef90e0d754590116ecb565dzhanyong.wan return ::std::pair<Matcher1, Matcher2>(matcher1, matcher2); 1510616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan } 1511616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan 1512616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan template <typename T, template <typename /* T */> class CombiningMatcher> 1513616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan static Matcher<T> CreateMatcher(const ListType& matchers) { 1514616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan return Matcher<T>(new CombiningMatcher<T>( 1515616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan SafeMatcherCast<T>(matchers.first), 1516616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan SafeMatcherCast<T>(matchers.second))); 1517616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan } 1518616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan}; 1519616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan 1520616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// VariadicMatcher is used for the variadic implementation of 1521616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// AllOf(m_1, m_2, ...) and AnyOf(m_1, m_2, ...). 1522616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// CombiningMatcher<T> is used to recursively combine the provided matchers 1523616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// (of type Args...). 1524616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wantemplate <template <typename T> class CombiningMatcher, typename... Args> 1525616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wanclass VariadicMatcher { 1526616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan public: 1527616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan VariadicMatcher(const Args&... matchers) // NOLINT 1528616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan : matchers_(MatcherListType::BuildList(matchers...)) {} 1529616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan 1530616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan // This template type conversion operator allows an 1531616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan // VariadicMatcher<Matcher1, Matcher2...> object to match any type that 1532616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan // all of the provided matchers (Matcher1, Matcher2, ...) can match. 1533616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan template <typename T> 1534616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan operator Matcher<T>() const { 1535616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan return MatcherListType::template CreateMatcher<T, CombiningMatcher>( 1536616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan matchers_); 1537616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan } 1538616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan 1539616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan private: 1540616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan typedef MatcherList<sizeof...(Args), Args...> MatcherListType; 1541616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan 1542616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan const typename MatcherListType::ListType matchers_; 1543616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan 1544616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan GTEST_DISALLOW_ASSIGN_(VariadicMatcher); 1545616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan}; 1546616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan 1547616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wantemplate <typename... Args> 1548616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wanusing AllOfMatcher = VariadicMatcher<BothOfMatcherImpl, Args...>; 1549616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan 1550616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan#endif // GTEST_LANG_CXX11 1551616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan 1552e35fdd936d133bf8a48de140a3c666897588a05shiqian// Used for implementing the AllOf(m_1, ..., m_n) matcher, which 1553e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a value that matches all of the matchers m_1, ..., and m_n. 1554e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2> 1555e35fdd936d133bf8a48de140a3c666897588a05shiqianclass BothOfMatcher { 1556e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1557e35fdd936d133bf8a48de140a3c666897588a05shiqian BothOfMatcher(Matcher1 matcher1, Matcher2 matcher2) 1558e35fdd936d133bf8a48de140a3c666897588a05shiqian : matcher1_(matcher1), matcher2_(matcher2) {} 1559e35fdd936d133bf8a48de140a3c666897588a05shiqian 1560e35fdd936d133bf8a48de140a3c666897588a05shiqian // This template type conversion operator allows a 1561e35fdd936d133bf8a48de140a3c666897588a05shiqian // BothOfMatcher<Matcher1, Matcher2> object to match any type that 1562e35fdd936d133bf8a48de140a3c666897588a05shiqian // both Matcher1 and Matcher2 can match. 1563e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 1564e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<T>() const { 1565c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan return Matcher<T>(new BothOfMatcherImpl<T>(SafeMatcherCast<T>(matcher1_), 1566c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan SafeMatcherCast<T>(matcher2_))); 1567e35fdd936d133bf8a48de140a3c666897588a05shiqian } 156832de5f53763125925e078498250f7e73a88de9edzhanyong.wan 1569e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1570c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan Matcher1 matcher1_; 1571c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan Matcher2 matcher2_; 157232de5f53763125925e078498250f7e73a88de9edzhanyong.wan 157332de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(BothOfMatcher); 1574c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan}; 1575e35fdd936d133bf8a48de140a3c666897588a05shiqian 1576c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// Implements the AnyOf(m1, m2) matcher for a particular argument type 1577c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// T. We do not nest it inside the AnyOfMatcher class template, as 1578c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// that will prevent different instantiations of AnyOfMatcher from 1579c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan// sharing the same EitherOfMatcherImpl<T> class. 1580c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wantemplate <typename T> 1581c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wanclass EitherOfMatcherImpl : public MatcherInterface<T> { 1582c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan public: 1583c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan EitherOfMatcherImpl(const Matcher<T>& matcher1, const Matcher<T>& matcher2) 1584c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan : matcher1_(matcher1), matcher2_(matcher2) {} 1585e35fdd936d133bf8a48de140a3c666897588a05shiqian 1586c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan virtual void DescribeTo(::std::ostream* os) const { 1587c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan *os << "("; 1588c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan matcher1_.DescribeTo(os); 1589c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan *os << ") or ("; 1590c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan matcher2_.DescribeTo(os); 1591c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan *os << ")"; 1592c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 1593e35fdd936d133bf8a48de140a3c666897588a05shiqian 1594c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan virtual void DescribeNegationTo(::std::ostream* os) const { 1595b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan *os << "("; 1596b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan matcher1_.DescribeNegationTo(os); 1597b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan *os << ") and ("; 1598b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan matcher2_.DescribeNegationTo(os); 1599b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan *os << ")"; 1600c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 1601c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan 1602821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual bool MatchAndExplain(T x, MatchResultListener* listener) const { 1603821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // If either matcher1_ or matcher2_ matches x, we just need to 1604821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // explain why *one* of them matches. 1605821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan StringMatchResultListener listener1; 1606821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (matcher1_.MatchAndExplain(x, &listener1)) { 1607821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << listener1.str(); 1608821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return true; 1609821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan } 1610c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan 1611821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan StringMatchResultListener listener2; 1612821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (matcher2_.MatchAndExplain(x, &listener2)) { 1613821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << listener2.str(); 1614821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return true; 1615821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan } 1616c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan 1617821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // Otherwise we need to explain why *both* of them fail. 1618821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan const internal::string s1 = listener1.str(); 1619821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan const internal::string s2 = listener2.str(); 1620821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 1621821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (s1 == "") { 1622821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << s2; 1623821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan } else { 1624821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan *listener << s1; 1625821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (s2 != "") { 1626b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan *listener << ", and " << s2; 1627e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1628e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1629821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return false; 1630c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan } 163132de5f53763125925e078498250f7e73a88de9edzhanyong.wan 1632c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan private: 1633c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan const Matcher<T> matcher1_; 1634c6a412397bc98f120d5e79d4a64e3972854b5af3zhanyong.wan const Matcher<T> matcher2_; 163532de5f53763125925e078498250f7e73a88de9edzhanyong.wan 163632de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(EitherOfMatcherImpl); 1637e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1638e35fdd936d133bf8a48de140a3c666897588a05shiqian 1639616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan#if GTEST_LANG_CXX11 1640616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// AnyOfMatcher is used for the variadic implementation of AnyOf(m_1, m_2, ...). 1641616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wantemplate <typename... Args> 1642616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wanusing AnyOfMatcher = VariadicMatcher<EitherOfMatcherImpl, Args...>; 1643616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan 1644616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan#endif // GTEST_LANG_CXX11 1645616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan 1646e35fdd936d133bf8a48de140a3c666897588a05shiqian// Used for implementing the AnyOf(m_1, ..., m_n) matcher, which 1647e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a value that matches at least one of the matchers m_1, ..., 1648e35fdd936d133bf8a48de140a3c666897588a05shiqian// and m_n. 1649e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Matcher1, typename Matcher2> 1650e35fdd936d133bf8a48de140a3c666897588a05shiqianclass EitherOfMatcher { 1651e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1652e35fdd936d133bf8a48de140a3c666897588a05shiqian EitherOfMatcher(Matcher1 matcher1, Matcher2 matcher2) 1653e35fdd936d133bf8a48de140a3c666897588a05shiqian : matcher1_(matcher1), matcher2_(matcher2) {} 1654e35fdd936d133bf8a48de140a3c666897588a05shiqian 1655e35fdd936d133bf8a48de140a3c666897588a05shiqian // This template type conversion operator allows a 1656e35fdd936d133bf8a48de140a3c666897588a05shiqian // EitherOfMatcher<Matcher1, Matcher2> object to match any type that 1657e35fdd936d133bf8a48de140a3c666897588a05shiqian // both Matcher1 and Matcher2 can match. 1658e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 1659e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<T>() const { 166016cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan return Matcher<T>(new EitherOfMatcherImpl<T>( 166116cf473930c01cd7a1a51dff65f22c541fbad5b8zhanyong.wan SafeMatcherCast<T>(matcher1_), SafeMatcherCast<T>(matcher2_))); 1662e35fdd936d133bf8a48de140a3c666897588a05shiqian } 166332de5f53763125925e078498250f7e73a88de9edzhanyong.wan 1664e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1665e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher1 matcher1_; 1666e35fdd936d133bf8a48de140a3c666897588a05shiqian Matcher2 matcher2_; 166732de5f53763125925e078498250f7e73a88de9edzhanyong.wan 166832de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(EitherOfMatcher); 1669e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1670e35fdd936d133bf8a48de140a3c666897588a05shiqian 1671e35fdd936d133bf8a48de140a3c666897588a05shiqian// Used for implementing Truly(pred), which turns a predicate into a 1672e35fdd936d133bf8a48de140a3c666897588a05shiqian// matcher. 1673e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Predicate> 1674e35fdd936d133bf8a48de140a3c666897588a05shiqianclass TrulyMatcher { 1675e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1676e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit TrulyMatcher(Predicate pred) : predicate_(pred) {} 1677e35fdd936d133bf8a48de140a3c666897588a05shiqian 1678e35fdd936d133bf8a48de140a3c666897588a05shiqian // This method template allows Truly(pred) to be used as a matcher 1679e35fdd936d133bf8a48de140a3c666897588a05shiqian // for type T where T is the argument type of predicate 'pred'. The 1680e35fdd936d133bf8a48de140a3c666897588a05shiqian // argument is passed by reference as the predicate may be 1681e35fdd936d133bf8a48de140a3c666897588a05shiqian // interested in the address of the argument. 1682e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 1683db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan bool MatchAndExplain(T& x, // NOLINT 1684db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan MatchResultListener* /* listener */) const { 16858d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan // Without the if-statement, MSVC sometimes warns about converting 16868d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan // a value to bool (warning 4800). 16878d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan // 16888d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan // We cannot write 'return !!predicate_(x);' as that doesn't work 16898d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan // when predicate_(x) returns a class convertible to bool but 16908d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan // having no operator!(). 16918d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan if (predicate_(x)) 16928d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan return true; 16938d3dc0cdd870ecc53d8a8ba30566b3cd70910863zhanyong.wan return false; 1694e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1695e35fdd936d133bf8a48de140a3c666897588a05shiqian 1696e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeTo(::std::ostream* os) const { 1697e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "satisfies the given predicate"; 1698e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1699e35fdd936d133bf8a48de140a3c666897588a05shiqian 1700e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeNegationTo(::std::ostream* os) const { 1701e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "doesn't satisfy the given predicate"; 1702e35fdd936d133bf8a48de140a3c666897588a05shiqian } 170332de5f53763125925e078498250f7e73a88de9edzhanyong.wan 1704e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1705e35fdd936d133bf8a48de140a3c666897588a05shiqian Predicate predicate_; 170632de5f53763125925e078498250f7e73a88de9edzhanyong.wan 170732de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(TrulyMatcher); 1708e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1709e35fdd936d133bf8a48de140a3c666897588a05shiqian 1710e35fdd936d133bf8a48de140a3c666897588a05shiqian// Used for implementing Matches(matcher), which turns a matcher into 1711e35fdd936d133bf8a48de140a3c666897588a05shiqian// a predicate. 1712e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename M> 1713e35fdd936d133bf8a48de140a3c666897588a05shiqianclass MatcherAsPredicate { 1714e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1715e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit MatcherAsPredicate(M matcher) : matcher_(matcher) {} 1716e35fdd936d133bf8a48de140a3c666897588a05shiqian 1717e35fdd936d133bf8a48de140a3c666897588a05shiqian // This template operator() allows Matches(m) to be used as a 1718e35fdd936d133bf8a48de140a3c666897588a05shiqian // predicate on type T where m is a matcher on type T. 1719e35fdd936d133bf8a48de140a3c666897588a05shiqian // 1720e35fdd936d133bf8a48de140a3c666897588a05shiqian // The argument x is passed by reference instead of by value, as 1721e35fdd936d133bf8a48de140a3c666897588a05shiqian // some matcher may be interested in its address (e.g. as in 1722e35fdd936d133bf8a48de140a3c666897588a05shiqian // Matches(Ref(n))(x)). 1723e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 1724e35fdd936d133bf8a48de140a3c666897588a05shiqian bool operator()(const T& x) const { 1725e35fdd936d133bf8a48de140a3c666897588a05shiqian // We let matcher_ commit to a particular type here instead of 1726e35fdd936d133bf8a48de140a3c666897588a05shiqian // when the MatcherAsPredicate object was constructed. This 1727e35fdd936d133bf8a48de140a3c666897588a05shiqian // allows us to write Matches(m) where m is a polymorphic matcher 1728e35fdd936d133bf8a48de140a3c666897588a05shiqian // (e.g. Eq(5)). 1729e35fdd936d133bf8a48de140a3c666897588a05shiqian // 1730e35fdd936d133bf8a48de140a3c666897588a05shiqian // If we write Matcher<T>(matcher_).Matches(x) here, it won't 1731e35fdd936d133bf8a48de140a3c666897588a05shiqian // compile when matcher_ has type Matcher<const T&>; if we write 1732e35fdd936d133bf8a48de140a3c666897588a05shiqian // Matcher<const T&>(matcher_).Matches(x) here, it won't compile 1733e35fdd936d133bf8a48de140a3c666897588a05shiqian // when matcher_ has type Matcher<T>; if we just write 1734e35fdd936d133bf8a48de140a3c666897588a05shiqian // matcher_.Matches(x), it won't compile when matcher_ is 1735e35fdd936d133bf8a48de140a3c666897588a05shiqian // polymorphic, e.g. Eq(5). 1736e35fdd936d133bf8a48de140a3c666897588a05shiqian // 1737e35fdd936d133bf8a48de140a3c666897588a05shiqian // MatcherCast<const T&>() is necessary for making the code work 1738e35fdd936d133bf8a48de140a3c666897588a05shiqian // in all of the above situations. 1739e35fdd936d133bf8a48de140a3c666897588a05shiqian return MatcherCast<const T&>(matcher_).Matches(x); 1740e35fdd936d133bf8a48de140a3c666897588a05shiqian } 174132de5f53763125925e078498250f7e73a88de9edzhanyong.wan 1742e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1743e35fdd936d133bf8a48de140a3c666897588a05shiqian M matcher_; 174432de5f53763125925e078498250f7e73a88de9edzhanyong.wan 174532de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(MatcherAsPredicate); 1746e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1747e35fdd936d133bf8a48de140a3c666897588a05shiqian 1748e35fdd936d133bf8a48de140a3c666897588a05shiqian// For implementing ASSERT_THAT() and EXPECT_THAT(). The template 1749e35fdd936d133bf8a48de140a3c666897588a05shiqian// argument M must be a type that can be converted to a matcher. 1750e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename M> 1751e35fdd936d133bf8a48de140a3c666897588a05shiqianclass PredicateFormatterFromMatcher { 1752e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1753e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit PredicateFormatterFromMatcher(const M& m) : matcher_(m) {} 1754e35fdd936d133bf8a48de140a3c666897588a05shiqian 1755e35fdd936d133bf8a48de140a3c666897588a05shiqian // This template () operator allows a PredicateFormatterFromMatcher 1756e35fdd936d133bf8a48de140a3c666897588a05shiqian // object to act as a predicate-formatter suitable for using with 1757e35fdd936d133bf8a48de140a3c666897588a05shiqian // Google Test's EXPECT_PRED_FORMAT1() macro. 1758e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 1759e35fdd936d133bf8a48de140a3c666897588a05shiqian AssertionResult operator()(const char* value_text, const T& x) const { 1760e35fdd936d133bf8a48de140a3c666897588a05shiqian // We convert matcher_ to a Matcher<const T&> *now* instead of 1761e35fdd936d133bf8a48de140a3c666897588a05shiqian // when the PredicateFormatterFromMatcher object was constructed, 1762e35fdd936d133bf8a48de140a3c666897588a05shiqian // as matcher_ may be polymorphic (e.g. NotNull()) and we won't 1763e35fdd936d133bf8a48de140a3c666897588a05shiqian // know which type to instantiate it to until we actually see the 1764e35fdd936d133bf8a48de140a3c666897588a05shiqian // type of x here. 1765e35fdd936d133bf8a48de140a3c666897588a05shiqian // 1766f4274520da256a3e8dd4fe2aa2e714f44ad89d18zhanyong.wan // We write SafeMatcherCast<const T&>(matcher_) instead of 1767e35fdd936d133bf8a48de140a3c666897588a05shiqian // Matcher<const T&>(matcher_), as the latter won't compile when 1768e35fdd936d133bf8a48de140a3c666897588a05shiqian // matcher_ has type Matcher<T> (e.g. An<int>()). 1769f4274520da256a3e8dd4fe2aa2e714f44ad89d18zhanyong.wan // We don't write MatcherCast<const T&> either, as that allows 1770f4274520da256a3e8dd4fe2aa2e714f44ad89d18zhanyong.wan // potentially unsafe downcasting of the matcher argument. 1771f4274520da256a3e8dd4fe2aa2e714f44ad89d18zhanyong.wan const Matcher<const T&> matcher = SafeMatcherCast<const T&>(matcher_); 1772821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan StringMatchResultListener listener; 1773b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan if (MatchPrintAndExplain(x, matcher, &listener)) 1774e35fdd936d133bf8a48de140a3c666897588a05shiqian return AssertionSuccess(); 1775b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan 1776b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan ::std::stringstream ss; 1777b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan ss << "Value of: " << value_text << "\n" 1778b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan << "Expected: "; 1779b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan matcher.DescribeTo(&ss); 1780b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan ss << "\n Actual: " << listener.str(); 1781b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan return AssertionFailure() << ss.str(); 1782e35fdd936d133bf8a48de140a3c666897588a05shiqian } 178332de5f53763125925e078498250f7e73a88de9edzhanyong.wan 1784e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1785e35fdd936d133bf8a48de140a3c666897588a05shiqian const M matcher_; 178632de5f53763125925e078498250f7e73a88de9edzhanyong.wan 178732de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(PredicateFormatterFromMatcher); 1788e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1789e35fdd936d133bf8a48de140a3c666897588a05shiqian 1790e35fdd936d133bf8a48de140a3c666897588a05shiqian// A helper function for converting a matcher to a predicate-formatter 1791e35fdd936d133bf8a48de140a3c666897588a05shiqian// without the user needing to explicitly write the type. This is 1792e35fdd936d133bf8a48de140a3c666897588a05shiqian// used for implementing ASSERT_THAT() and EXPECT_THAT(). 1793e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename M> 1794e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PredicateFormatterFromMatcher<M> 1795e35fdd936d133bf8a48de140a3c666897588a05shiqianMakePredicateFormatterFromMatcher(const M& matcher) { 1796e35fdd936d133bf8a48de140a3c666897588a05shiqian return PredicateFormatterFromMatcher<M>(matcher); 1797e35fdd936d133bf8a48de140a3c666897588a05shiqian} 1798e35fdd936d133bf8a48de140a3c666897588a05shiqian 1799616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// Implements the polymorphic floating point equality matcher, which matches 1800616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// two float values using ULP-based approximation or, optionally, a 1801616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// user-specified epsilon. The template is meant to be instantiated with 1802616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// FloatType being either float or double. 1803e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename FloatType> 1804e35fdd936d133bf8a48de140a3c666897588a05shiqianclass FloatingEqMatcher { 1805e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1806e35fdd936d133bf8a48de140a3c666897588a05shiqian // Constructor for FloatingEqMatcher. 1807e35fdd936d133bf8a48de140a3c666897588a05shiqian // The matcher's input will be compared with rhs. The matcher treats two 1808e35fdd936d133bf8a48de140a3c666897588a05shiqian // NANs as equal if nan_eq_nan is true. Otherwise, under IEEE standards, 1809616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan // equality comparisons between NANs will always return false. We specify a 1810616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan // negative max_abs_error_ term to indicate that ULP-based approximation will 1811616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan // be used for comparison. 1812e35fdd936d133bf8a48de140a3c666897588a05shiqian FloatingEqMatcher(FloatType rhs, bool nan_eq_nan) : 1813616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan rhs_(rhs), nan_eq_nan_(nan_eq_nan), max_abs_error_(-1) { 1814616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan } 1815616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan 1816616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan // Constructor that supports a user-specified max_abs_error that will be used 1817616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan // for comparison instead of ULP-based approximation. The max absolute 1818616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan // should be non-negative. 1819616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan FloatingEqMatcher(FloatType rhs, bool nan_eq_nan, FloatType max_abs_error) : 1820616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan rhs_(rhs), nan_eq_nan_(nan_eq_nan), max_abs_error_(max_abs_error) { 1821616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan GTEST_CHECK_(max_abs_error >= 0) 1822616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan << ", where max_abs_error is" << max_abs_error; 1823616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan } 1824e35fdd936d133bf8a48de140a3c666897588a05shiqian 1825e35fdd936d133bf8a48de140a3c666897588a05shiqian // Implements floating point equality matcher as a Matcher<T>. 1826e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 1827e35fdd936d133bf8a48de140a3c666897588a05shiqian class Impl : public MatcherInterface<T> { 1828e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1829616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan Impl(FloatType rhs, bool nan_eq_nan, FloatType max_abs_error) : 1830616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan rhs_(rhs), nan_eq_nan_(nan_eq_nan), max_abs_error_(max_abs_error) {} 1831e35fdd936d133bf8a48de140a3c666897588a05shiqian 1832821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual bool MatchAndExplain(T value, 1833821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan MatchResultListener* /* listener */) const { 1834e35fdd936d133bf8a48de140a3c666897588a05shiqian const FloatingPoint<FloatType> lhs(value), rhs(rhs_); 1835e35fdd936d133bf8a48de140a3c666897588a05shiqian 1836e35fdd936d133bf8a48de140a3c666897588a05shiqian // Compares NaNs first, if nan_eq_nan_ is true. 1837616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan if (lhs.is_nan() || rhs.is_nan()) { 1838616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan if (lhs.is_nan() && rhs.is_nan()) { 1839616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan return nan_eq_nan_; 1840616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan } 1841616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan // One is nan; the other is not nan. 1842616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan return false; 1843616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan } 1844616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan if (HasMaxAbsError()) { 1845616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan // We perform an equality check so that inf will match inf, regardless 1846616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan // of error bounds. If the result of value - rhs_ would result in 1847616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan // overflow or if either value is inf, the default result is infinity, 1848616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan // which should only match if max_abs_error_ is also infinity. 1849616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan return value == rhs_ || fabs(value - rhs_) <= max_abs_error_; 1850616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan } else { 1851616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan return lhs.AlmostEquals(rhs); 1852e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1853e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1854e35fdd936d133bf8a48de140a3c666897588a05shiqian 1855e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeTo(::std::ostream* os) const { 1856e35fdd936d133bf8a48de140a3c666897588a05shiqian // os->precision() returns the previously set precision, which we 1857e35fdd936d133bf8a48de140a3c666897588a05shiqian // store to restore the ostream to its original configuration 1858e35fdd936d133bf8a48de140a3c666897588a05shiqian // after outputting. 1859e35fdd936d133bf8a48de140a3c666897588a05shiqian const ::std::streamsize old_precision = os->precision( 1860e35fdd936d133bf8a48de140a3c666897588a05shiqian ::std::numeric_limits<FloatType>::digits10 + 2); 1861e35fdd936d133bf8a48de140a3c666897588a05shiqian if (FloatingPoint<FloatType>(rhs_).is_nan()) { 1862e35fdd936d133bf8a48de140a3c666897588a05shiqian if (nan_eq_nan_) { 1863e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "is NaN"; 1864e35fdd936d133bf8a48de140a3c666897588a05shiqian } else { 1865e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "never matches"; 1866e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1867e35fdd936d133bf8a48de140a3c666897588a05shiqian } else { 1868e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "is approximately " << rhs_; 1869616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan if (HasMaxAbsError()) { 1870616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan *os << " (absolute error <= " << max_abs_error_ << ")"; 1871616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan } 1872e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1873e35fdd936d133bf8a48de140a3c666897588a05shiqian os->precision(old_precision); 1874e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1875e35fdd936d133bf8a48de140a3c666897588a05shiqian 1876e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeNegationTo(::std::ostream* os) const { 1877e35fdd936d133bf8a48de140a3c666897588a05shiqian // As before, get original precision. 1878e35fdd936d133bf8a48de140a3c666897588a05shiqian const ::std::streamsize old_precision = os->precision( 1879e35fdd936d133bf8a48de140a3c666897588a05shiqian ::std::numeric_limits<FloatType>::digits10 + 2); 1880e35fdd936d133bf8a48de140a3c666897588a05shiqian if (FloatingPoint<FloatType>(rhs_).is_nan()) { 1881e35fdd936d133bf8a48de140a3c666897588a05shiqian if (nan_eq_nan_) { 1882b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan *os << "isn't NaN"; 1883e35fdd936d133bf8a48de140a3c666897588a05shiqian } else { 1884e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "is anything"; 1885e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1886e35fdd936d133bf8a48de140a3c666897588a05shiqian } else { 1887b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan *os << "isn't approximately " << rhs_; 1888616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan if (HasMaxAbsError()) { 1889616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan *os << " (absolute error > " << max_abs_error_ << ")"; 1890616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan } 1891e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1892e35fdd936d133bf8a48de140a3c666897588a05shiqian // Restore original precision. 1893e35fdd936d133bf8a48de140a3c666897588a05shiqian os->precision(old_precision); 1894e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1895e35fdd936d133bf8a48de140a3c666897588a05shiqian 1896e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1897616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan bool HasMaxAbsError() const { 1898616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan return max_abs_error_ >= 0; 1899616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan } 1900616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan 1901e35fdd936d133bf8a48de140a3c666897588a05shiqian const FloatType rhs_; 1902e35fdd936d133bf8a48de140a3c666897588a05shiqian const bool nan_eq_nan_; 1903616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan // max_abs_error will be used for value comparison when >= 0. 1904616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan const FloatType max_abs_error_; 190532de5f53763125925e078498250f7e73a88de9edzhanyong.wan 190632de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(Impl); 1907e35fdd936d133bf8a48de140a3c666897588a05shiqian }; 1908e35fdd936d133bf8a48de140a3c666897588a05shiqian 1909e35fdd936d133bf8a48de140a3c666897588a05shiqian // The following 3 type conversion operators allow FloatEq(rhs) and 1910e35fdd936d133bf8a48de140a3c666897588a05shiqian // NanSensitiveFloatEq(rhs) to be used as a Matcher<float>, a 1911e35fdd936d133bf8a48de140a3c666897588a05shiqian // Matcher<const float&>, or a Matcher<float&>, but nothing else. 1912e35fdd936d133bf8a48de140a3c666897588a05shiqian // (While Google's C++ coding style doesn't allow arguments passed 1913e35fdd936d133bf8a48de140a3c666897588a05shiqian // by non-const reference, we may see them in code not conforming to 1914e35fdd936d133bf8a48de140a3c666897588a05shiqian // the style. Therefore Google Mock needs to support them.) 1915e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<FloatType>() const { 1916616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan return MakeMatcher(new Impl<FloatType>(rhs_, nan_eq_nan_, max_abs_error_)); 1917e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1918e35fdd936d133bf8a48de140a3c666897588a05shiqian 1919e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<const FloatType&>() const { 1920616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan return MakeMatcher( 1921616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan new Impl<const FloatType&>(rhs_, nan_eq_nan_, max_abs_error_)); 1922e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1923e35fdd936d133bf8a48de140a3c666897588a05shiqian 1924e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<FloatType&>() const { 1925616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan return MakeMatcher(new Impl<FloatType&>(rhs_, nan_eq_nan_, max_abs_error_)); 1926e35fdd936d133bf8a48de140a3c666897588a05shiqian } 192779a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm 1928e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1929e35fdd936d133bf8a48de140a3c666897588a05shiqian const FloatType rhs_; 1930e35fdd936d133bf8a48de140a3c666897588a05shiqian const bool nan_eq_nan_; 1931616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan // max_abs_error will be used for value comparison when >= 0. 1932616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan const FloatType max_abs_error_; 193332de5f53763125925e078498250f7e73a88de9edzhanyong.wan 193432de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(FloatingEqMatcher); 1935e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1936e35fdd936d133bf8a48de140a3c666897588a05shiqian 1937e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the Pointee(m) matcher for matching a pointer whose 1938e35fdd936d133bf8a48de140a3c666897588a05shiqian// pointee matches matcher m. The pointer can be either raw or smart. 1939e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename InnerMatcher> 1940e35fdd936d133bf8a48de140a3c666897588a05shiqianclass PointeeMatcher { 1941e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 1942e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit PointeeMatcher(const InnerMatcher& matcher) : matcher_(matcher) {} 1943e35fdd936d133bf8a48de140a3c666897588a05shiqian 1944e35fdd936d133bf8a48de140a3c666897588a05shiqian // This type conversion operator template allows Pointee(m) to be 1945e35fdd936d133bf8a48de140a3c666897588a05shiqian // used as a matcher for any pointer type whose pointee type is 1946e35fdd936d133bf8a48de140a3c666897588a05shiqian // compatible with the inner matcher, where type Pointer can be 1947e35fdd936d133bf8a48de140a3c666897588a05shiqian // either a raw pointer or a smart pointer. 1948e35fdd936d133bf8a48de140a3c666897588a05shiqian // 1949e35fdd936d133bf8a48de140a3c666897588a05shiqian // The reason we do this instead of relying on 1950e35fdd936d133bf8a48de140a3c666897588a05shiqian // MakePolymorphicMatcher() is that the latter is not flexible 1951e35fdd936d133bf8a48de140a3c666897588a05shiqian // enough for implementing the DescribeTo() method of Pointee(). 1952e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename Pointer> 1953e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<Pointer>() const { 1954e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakeMatcher(new Impl<Pointer>(matcher_)); 1955e35fdd936d133bf8a48de140a3c666897588a05shiqian } 195632de5f53763125925e078498250f7e73a88de9edzhanyong.wan 1957e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1958e35fdd936d133bf8a48de140a3c666897588a05shiqian // The monomorphic implementation that works for a particular pointer type. 1959e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename Pointer> 1960e35fdd936d133bf8a48de140a3c666897588a05shiqian class Impl : public MatcherInterface<Pointer> { 1961e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 196202f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan typedef typename PointeeOf<GTEST_REMOVE_CONST_( // NOLINT 196302f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan GTEST_REMOVE_REFERENCE_(Pointer))>::type Pointee; 1964e35fdd936d133bf8a48de140a3c666897588a05shiqian 1965e35fdd936d133bf8a48de140a3c666897588a05shiqian explicit Impl(const InnerMatcher& matcher) 1966e35fdd936d133bf8a48de140a3c666897588a05shiqian : matcher_(MatcherCast<const Pointee&>(matcher)) {} 1967e35fdd936d133bf8a48de140a3c666897588a05shiqian 1968e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeTo(::std::ostream* os) const { 1969e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "points to a value that "; 1970e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher_.DescribeTo(os); 1971e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1972e35fdd936d133bf8a48de140a3c666897588a05shiqian 1973e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeNegationTo(::std::ostream* os) const { 1974e35fdd936d133bf8a48de140a3c666897588a05shiqian *os << "does not point to a value that "; 1975e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher_.DescribeTo(os); 1976e35fdd936d133bf8a48de140a3c666897588a05shiqian } 1977e35fdd936d133bf8a48de140a3c666897588a05shiqian 1978821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual bool MatchAndExplain(Pointer pointer, 1979821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan MatchResultListener* listener) const { 1980e35fdd936d133bf8a48de140a3c666897588a05shiqian if (GetRawPointer(pointer) == NULL) 1981821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return false; 1982e35fdd936d133bf8a48de140a3c666897588a05shiqian 1983676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan *listener << "which points to "; 1984676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan return MatchPrintAndExplain(*pointer, matcher_, listener); 1985e35fdd936d133bf8a48de140a3c666897588a05shiqian } 198632de5f53763125925e078498250f7e73a88de9edzhanyong.wan 1987e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 1988e35fdd936d133bf8a48de140a3c666897588a05shiqian const Matcher<const Pointee&> matcher_; 198932de5f53763125925e078498250f7e73a88de9edzhanyong.wan 199032de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(Impl); 1991e35fdd936d133bf8a48de140a3c666897588a05shiqian }; 1992e35fdd936d133bf8a48de140a3c666897588a05shiqian 1993e35fdd936d133bf8a48de140a3c666897588a05shiqian const InnerMatcher matcher_; 199432de5f53763125925e078498250f7e73a88de9edzhanyong.wan 199532de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(PointeeMatcher); 1996e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 1997e35fdd936d133bf8a48de140a3c666897588a05shiqian 1998e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the Field() matcher for matching a field (i.e. member 1999e35fdd936d133bf8a48de140a3c666897588a05shiqian// variable) of an object. 2000e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Class, typename FieldType> 2001e35fdd936d133bf8a48de140a3c666897588a05shiqianclass FieldMatcher { 2002e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 2003e35fdd936d133bf8a48de140a3c666897588a05shiqian FieldMatcher(FieldType Class::*field, 2004e35fdd936d133bf8a48de140a3c666897588a05shiqian const Matcher<const FieldType&>& matcher) 2005e35fdd936d133bf8a48de140a3c666897588a05shiqian : field_(field), matcher_(matcher) {} 2006e35fdd936d133bf8a48de140a3c666897588a05shiqian 2007e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeTo(::std::ostream* os) const { 2008676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan *os << "is an object whose given field "; 2009e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher_.DescribeTo(os); 2010e35fdd936d133bf8a48de140a3c666897588a05shiqian } 2011e35fdd936d133bf8a48de140a3c666897588a05shiqian 2012e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeNegationTo(::std::ostream* os) const { 2013676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan *os << "is an object whose given field "; 2014e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher_.DescribeNegationTo(os); 2015e35fdd936d133bf8a48de140a3c666897588a05shiqian } 2016e35fdd936d133bf8a48de140a3c666897588a05shiqian 2017db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan template <typename T> 2018db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan bool MatchAndExplain(const T& value, MatchResultListener* listener) const { 2019db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan return MatchAndExplainImpl( 2020db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan typename ::testing::internal:: 202102f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan is_pointer<GTEST_REMOVE_CONST_(T)>::type(), 2022db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan value, listener); 2023db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan } 2024db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan 2025db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan private: 2026db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan // The first argument of MatchAndExplainImpl() is needed to help 202718490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan // Symbian's C++ compiler choose which overload to use. Its type is 202818490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan // true_type iff the Field() matcher is used to match a pointer. 2029db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan bool MatchAndExplainImpl(false_type /* is_not_pointer */, const Class& obj, 2030db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan MatchResultListener* listener) const { 2031676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan *listener << "whose given field is "; 2032676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan return MatchPrintAndExplain(obj.*field_, matcher_, listener); 2033e35fdd936d133bf8a48de140a3c666897588a05shiqian } 2034e35fdd936d133bf8a48de140a3c666897588a05shiqian 2035db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan bool MatchAndExplainImpl(true_type /* is_pointer */, const Class* p, 2036db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan MatchResultListener* listener) const { 2037821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (p == NULL) 2038821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return false; 2039821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 2040676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan *listener << "which points to an object "; 2041821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // Since *p has a field, it must be a class/struct/union type and 2042821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // thus cannot be a pointer. Therefore we pass false_type() as 2043821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // the first argument. 2044db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan return MatchAndExplainImpl(false_type(), *p, listener); 2045e35fdd936d133bf8a48de140a3c666897588a05shiqian } 204632de5f53763125925e078498250f7e73a88de9edzhanyong.wan 2047e35fdd936d133bf8a48de140a3c666897588a05shiqian const FieldType Class::*field_; 2048e35fdd936d133bf8a48de140a3c666897588a05shiqian const Matcher<const FieldType&> matcher_; 204932de5f53763125925e078498250f7e73a88de9edzhanyong.wan 205032de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(FieldMatcher); 2051e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 2052e35fdd936d133bf8a48de140a3c666897588a05shiqian 2053e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the Property() matcher for matching a property 2054e35fdd936d133bf8a48de140a3c666897588a05shiqian// (i.e. return value of a getter method) of an object. 2055e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Class, typename PropertyType> 2056e35fdd936d133bf8a48de140a3c666897588a05shiqianclass PropertyMatcher { 2057e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 2058e35fdd936d133bf8a48de140a3c666897588a05shiqian // The property may have a reference type, so 'const PropertyType&' 2059e35fdd936d133bf8a48de140a3c666897588a05shiqian // may cause double references and fail to compile. That's why we 206002f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan // need GTEST_REFERENCE_TO_CONST, which works regardless of 2061e35fdd936d133bf8a48de140a3c666897588a05shiqian // PropertyType being a reference or not. 206202f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan typedef GTEST_REFERENCE_TO_CONST_(PropertyType) RefToConstProperty; 2063e35fdd936d133bf8a48de140a3c666897588a05shiqian 2064e35fdd936d133bf8a48de140a3c666897588a05shiqian PropertyMatcher(PropertyType (Class::*property)() const, 2065e35fdd936d133bf8a48de140a3c666897588a05shiqian const Matcher<RefToConstProperty>& matcher) 2066e35fdd936d133bf8a48de140a3c666897588a05shiqian : property_(property), matcher_(matcher) {} 2067e35fdd936d133bf8a48de140a3c666897588a05shiqian 2068e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeTo(::std::ostream* os) const { 2069676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan *os << "is an object whose given property "; 2070e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher_.DescribeTo(os); 2071e35fdd936d133bf8a48de140a3c666897588a05shiqian } 2072e35fdd936d133bf8a48de140a3c666897588a05shiqian 2073e35fdd936d133bf8a48de140a3c666897588a05shiqian void DescribeNegationTo(::std::ostream* os) const { 2074676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan *os << "is an object whose given property "; 2075e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher_.DescribeNegationTo(os); 2076e35fdd936d133bf8a48de140a3c666897588a05shiqian } 2077e35fdd936d133bf8a48de140a3c666897588a05shiqian 2078db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan template <typename T> 2079db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan bool MatchAndExplain(const T&value, MatchResultListener* listener) const { 2080db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan return MatchAndExplainImpl( 2081db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan typename ::testing::internal:: 208202f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan is_pointer<GTEST_REMOVE_CONST_(T)>::type(), 2083db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan value, listener); 2084db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan } 2085db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan 2086db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan private: 2087db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan // The first argument of MatchAndExplainImpl() is needed to help 208818490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan // Symbian's C++ compiler choose which overload to use. Its type is 208918490653e80d484b4650d8799184fd1e021efc7bzhanyong.wan // true_type iff the Property() matcher is used to match a pointer. 2090db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan bool MatchAndExplainImpl(false_type /* is_not_pointer */, const Class& obj, 2091db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan MatchResultListener* listener) const { 2092676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan *listener << "whose given property is "; 2093676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan // Cannot pass the return value (for example, int) to MatchPrintAndExplain, 2094676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan // which takes a non-const reference as argument. 2095676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan RefToConstProperty result = (obj.*property_)(); 2096676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan return MatchPrintAndExplain(result, matcher_, listener); 2097e35fdd936d133bf8a48de140a3c666897588a05shiqian } 2098e35fdd936d133bf8a48de140a3c666897588a05shiqian 2099db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan bool MatchAndExplainImpl(true_type /* is_pointer */, const Class* p, 2100db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan MatchResultListener* listener) const { 2101821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (p == NULL) 2102821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return false; 2103821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 2104676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan *listener << "which points to an object "; 2105821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // Since *p has a property method, it must be a class/struct/union 2106821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // type and thus cannot be a pointer. Therefore we pass 2107821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // false_type() as the first argument. 2108db22c227826b82e1ad05d6c47facfef73c99e057zhanyong.wan return MatchAndExplainImpl(false_type(), *p, listener); 2109e35fdd936d133bf8a48de140a3c666897588a05shiqian } 211032de5f53763125925e078498250f7e73a88de9edzhanyong.wan 2111e35fdd936d133bf8a48de140a3c666897588a05shiqian PropertyType (Class::*property_)() const; 2112e35fdd936d133bf8a48de140a3c666897588a05shiqian const Matcher<RefToConstProperty> matcher_; 211332de5f53763125925e078498250f7e73a88de9edzhanyong.wan 211432de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(PropertyMatcher); 2115e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 2116e35fdd936d133bf8a48de140a3c666897588a05shiqian 2117e35fdd936d133bf8a48de140a3c666897588a05shiqian// Type traits specifying various features of different functors for ResultOf. 2118e35fdd936d133bf8a48de140a3c666897588a05shiqian// The default template specifies features for functor objects. 2119e35fdd936d133bf8a48de140a3c666897588a05shiqian// Functor classes have to typedef argument_type and result_type 2120e35fdd936d133bf8a48de140a3c666897588a05shiqian// to be compatible with ResultOf. 2121e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Functor> 2122e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct CallableTraits { 2123e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef typename Functor::result_type ResultType; 2124e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef Functor StorageType; 2125e35fdd936d133bf8a48de140a3c666897588a05shiqian 212632de5f53763125925e078498250f7e73a88de9edzhanyong.wan static void CheckIsValid(Functor /* functor */) {} 2127e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 2128e35fdd936d133bf8a48de140a3c666897588a05shiqian static ResultType Invoke(Functor f, T arg) { return f(arg); } 2129e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 2130e35fdd936d133bf8a48de140a3c666897588a05shiqian 2131e35fdd936d133bf8a48de140a3c666897588a05shiqian// Specialization for function pointers. 2132e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename ArgType, typename ResType> 2133e35fdd936d133bf8a48de140a3c666897588a05shiqianstruct CallableTraits<ResType(*)(ArgType)> { 2134e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef ResType ResultType; 2135e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef ResType(*StorageType)(ArgType); 2136e35fdd936d133bf8a48de140a3c666897588a05shiqian 2137e35fdd936d133bf8a48de140a3c666897588a05shiqian static void CheckIsValid(ResType(*f)(ArgType)) { 2138f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan GTEST_CHECK_(f != NULL) 2139e35fdd936d133bf8a48de140a3c666897588a05shiqian << "NULL function pointer is passed into ResultOf()."; 2140e35fdd936d133bf8a48de140a3c666897588a05shiqian } 2141e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 2142e35fdd936d133bf8a48de140a3c666897588a05shiqian static ResType Invoke(ResType(*f)(ArgType), T arg) { 2143e35fdd936d133bf8a48de140a3c666897588a05shiqian return (*f)(arg); 2144e35fdd936d133bf8a48de140a3c666897588a05shiqian } 2145e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 2146e35fdd936d133bf8a48de140a3c666897588a05shiqian 2147e35fdd936d133bf8a48de140a3c666897588a05shiqian// Implements the ResultOf() matcher for matching a return value of a 2148e35fdd936d133bf8a48de140a3c666897588a05shiqian// unary function of an object. 2149e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Callable> 2150e35fdd936d133bf8a48de140a3c666897588a05shiqianclass ResultOfMatcher { 2151e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 2152e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef typename CallableTraits<Callable>::ResultType ResultType; 2153e35fdd936d133bf8a48de140a3c666897588a05shiqian 2154e35fdd936d133bf8a48de140a3c666897588a05shiqian ResultOfMatcher(Callable callable, const Matcher<ResultType>& matcher) 2155e35fdd936d133bf8a48de140a3c666897588a05shiqian : callable_(callable), matcher_(matcher) { 2156e35fdd936d133bf8a48de140a3c666897588a05shiqian CallableTraits<Callable>::CheckIsValid(callable_); 2157e35fdd936d133bf8a48de140a3c666897588a05shiqian } 2158e35fdd936d133bf8a48de140a3c666897588a05shiqian 2159e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 2160e35fdd936d133bf8a48de140a3c666897588a05shiqian operator Matcher<T>() const { 2161e35fdd936d133bf8a48de140a3c666897588a05shiqian return Matcher<T>(new Impl<T>(callable_, matcher_)); 2162e35fdd936d133bf8a48de140a3c666897588a05shiqian } 2163e35fdd936d133bf8a48de140a3c666897588a05shiqian 2164e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 2165e35fdd936d133bf8a48de140a3c666897588a05shiqian typedef typename CallableTraits<Callable>::StorageType CallableStorageType; 2166e35fdd936d133bf8a48de140a3c666897588a05shiqian 2167e35fdd936d133bf8a48de140a3c666897588a05shiqian template <typename T> 2168e35fdd936d133bf8a48de140a3c666897588a05shiqian class Impl : public MatcherInterface<T> { 2169e35fdd936d133bf8a48de140a3c666897588a05shiqian public: 2170e35fdd936d133bf8a48de140a3c666897588a05shiqian Impl(CallableStorageType callable, const Matcher<ResultType>& matcher) 2171e35fdd936d133bf8a48de140a3c666897588a05shiqian : callable_(callable), matcher_(matcher) {} 2172e35fdd936d133bf8a48de140a3c666897588a05shiqian 2173e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeTo(::std::ostream* os) const { 2174676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan *os << "is mapped by the given callable to a value that "; 2175e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher_.DescribeTo(os); 2176e35fdd936d133bf8a48de140a3c666897588a05shiqian } 2177e35fdd936d133bf8a48de140a3c666897588a05shiqian 2178e35fdd936d133bf8a48de140a3c666897588a05shiqian virtual void DescribeNegationTo(::std::ostream* os) const { 2179676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan *os << "is mapped by the given callable to a value that "; 2180e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher_.DescribeNegationTo(os); 2181e35fdd936d133bf8a48de140a3c666897588a05shiqian } 2182e35fdd936d133bf8a48de140a3c666897588a05shiqian 2183821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual bool MatchAndExplain(T obj, MatchResultListener* listener) const { 2184676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan *listener << "which is mapped by the given callable to "; 2185676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan // Cannot pass the return value (for example, int) to 2186676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan // MatchPrintAndExplain, which takes a non-const reference as argument. 2187676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan ResultType result = 2188676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan CallableTraits<Callable>::template Invoke<T>(callable_, obj); 2189676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan return MatchPrintAndExplain(result, matcher_, listener); 2190e35fdd936d133bf8a48de140a3c666897588a05shiqian } 219132de5f53763125925e078498250f7e73a88de9edzhanyong.wan 2192e35fdd936d133bf8a48de140a3c666897588a05shiqian private: 2193e35fdd936d133bf8a48de140a3c666897588a05shiqian // Functors often define operator() as non-const method even though 2194e35fdd936d133bf8a48de140a3c666897588a05shiqian // they are actualy stateless. But we need to use them even when 2195e35fdd936d133bf8a48de140a3c666897588a05shiqian // 'this' is a const pointer. It's the user's responsibility not to 2196e35fdd936d133bf8a48de140a3c666897588a05shiqian // use stateful callables with ResultOf(), which does't guarantee 2197e35fdd936d133bf8a48de140a3c666897588a05shiqian // how many times the callable will be invoked. 2198e35fdd936d133bf8a48de140a3c666897588a05shiqian mutable CallableStorageType callable_; 2199e35fdd936d133bf8a48de140a3c666897588a05shiqian const Matcher<ResultType> matcher_; 220032de5f53763125925e078498250f7e73a88de9edzhanyong.wan 220132de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(Impl); 2202e35fdd936d133bf8a48de140a3c666897588a05shiqian }; // class Impl 2203e35fdd936d133bf8a48de140a3c666897588a05shiqian 2204e35fdd936d133bf8a48de140a3c666897588a05shiqian const CallableStorageType callable_; 2205e35fdd936d133bf8a48de140a3c666897588a05shiqian const Matcher<ResultType> matcher_; 220632de5f53763125925e078498250f7e73a88de9edzhanyong.wan 220732de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(ResultOfMatcher); 2208e35fdd936d133bf8a48de140a3c666897588a05shiqian}; 2209e35fdd936d133bf8a48de140a3c666897588a05shiqian 2210a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan// Implements a matcher that checks the size of an STL-style container. 2211a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wantemplate <typename SizeMatcher> 2212a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wanclass SizeIsMatcher { 2213a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan public: 2214a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan explicit SizeIsMatcher(const SizeMatcher& size_matcher) 2215a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan : size_matcher_(size_matcher) { 2216a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan } 2217a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan 2218a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan template <typename Container> 2219a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan operator Matcher<Container>() const { 2220a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan return MakeMatcher(new Impl<Container>(size_matcher_)); 2221a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan } 2222a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan 2223a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan template <typename Container> 2224a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan class Impl : public MatcherInterface<Container> { 2225a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan public: 2226a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan typedef internal::StlContainerView< 2227a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan GTEST_REMOVE_REFERENCE_AND_CONST_(Container)> ContainerView; 2228a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan typedef typename ContainerView::type::size_type SizeType; 2229a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan explicit Impl(const SizeMatcher& size_matcher) 2230a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan : size_matcher_(MatcherCast<SizeType>(size_matcher)) {} 2231a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan 2232a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan virtual void DescribeTo(::std::ostream* os) const { 2233a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan *os << "size "; 2234a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan size_matcher_.DescribeTo(os); 2235a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan } 2236a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan virtual void DescribeNegationTo(::std::ostream* os) const { 2237a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan *os << "size "; 2238a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan size_matcher_.DescribeNegationTo(os); 2239a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan } 2240a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan 2241a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan virtual bool MatchAndExplain(Container container, 2242a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan MatchResultListener* listener) const { 2243a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan SizeType size = container.size(); 2244a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan StringMatchResultListener size_listener; 2245a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan const bool result = size_matcher_.MatchAndExplain(size, &size_listener); 2246a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan *listener 2247a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan << "whose size " << size << (result ? " matches" : " doesn't match"); 2248a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan PrintIfNotEmpty(size_listener.str(), listener->stream()); 2249a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan return result; 2250a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan } 2251a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan 2252a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan private: 2253a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan const Matcher<SizeType> size_matcher_; 2254a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan GTEST_DISALLOW_ASSIGN_(Impl); 2255a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan }; 2256a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan 2257a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan private: 2258a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan const SizeMatcher size_matcher_; 2259a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan GTEST_DISALLOW_ASSIGN_(SizeIsMatcher); 2260a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan}; 2261a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan 22626a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// Implements an equality matcher for any STL-style container whose elements 22636a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// support ==. This matcher is like Eq(), but its failure explanations provide 22646a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// more detailed information that is useful when the container is used as a set. 22656a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// The failure message reports elements that are in one of the operands but not 22666a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// the other. The failure messages do not report duplicate or out-of-order 22676a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// elements in the containers (which don't properly matter to sets, but can 22686a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// occur if the containers are vectors or lists, for example). 22696a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// 22706a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// Uses the container's const_iterator, value_type, operator ==, 22716a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// begin(), and end(). 22726a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wantemplate <typename Container> 22736a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wanclass ContainerEqMatcher { 22746a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan public: 2275b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef internal::StlContainerView<Container> View; 2276b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef typename View::type StlContainer; 2277b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef typename View::const_reference StlContainerReference; 2278b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 2279b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // We make a copy of rhs in case the elements in it are modified 2280b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // after this matcher is created. 2281b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan explicit ContainerEqMatcher(const Container& rhs) : rhs_(View::Copy(rhs)) { 2282b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // Makes sure the user doesn't instantiate this class template 2283b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // with a const or reference type. 2284ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan (void)testing::StaticAssertTypeEq<Container, 2285ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan GTEST_REMOVE_REFERENCE_AND_CONST_(Container)>(); 2286b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } 2287b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 22886a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan void DescribeTo(::std::ostream* os) const { 22896a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan *os << "equals "; 2290e2e8ba401d198d1a8304c652e997505609b62696vladlosev UniversalPrint(rhs_, os); 22916a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan } 22926a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan void DescribeNegationTo(::std::ostream* os) const { 22936a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan *os << "does not equal "; 2294e2e8ba401d198d1a8304c652e997505609b62696vladlosev UniversalPrint(rhs_, os); 22956a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan } 22966a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan 2297b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan template <typename LhsContainer> 2298e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan bool MatchAndExplain(const LhsContainer& lhs, 2299e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan MatchResultListener* listener) const { 230002f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan // GTEST_REMOVE_CONST_() is needed to work around an MSVC 8.0 bug 2301b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // that causes LhsContainer to be a const type sometimes. 230202f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan typedef internal::StlContainerView<GTEST_REMOVE_CONST_(LhsContainer)> 2303b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan LhsView; 2304b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef typename LhsView::type LhsStlContainer; 2305b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan StlContainerReference lhs_stl_container = LhsView::ConstReference(lhs); 2306e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan if (lhs_stl_container == rhs_) 2307e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan return true; 2308b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 2309e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan ::std::ostream* const os = listener->stream(); 2310e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan if (os != NULL) { 2311b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan // Something is different. Check for extra values first. 2312e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan bool printed_header = false; 2313e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan for (typename LhsStlContainer::const_iterator it = 2314e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan lhs_stl_container.begin(); 2315e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan it != lhs_stl_container.end(); ++it) { 2316e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan if (internal::ArrayAwareFind(rhs_.begin(), rhs_.end(), *it) == 2317e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan rhs_.end()) { 2318e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan if (printed_header) { 2319e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan *os << ", "; 2320e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan } else { 2321b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan *os << "which has these unexpected elements: "; 2322e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan printed_header = true; 2323e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan } 2324e2e8ba401d198d1a8304c652e997505609b62696vladlosev UniversalPrint(*it, os); 23256a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan } 23266a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan } 23276a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan 2328b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan // Now check for missing values. 2329e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan bool printed_header2 = false; 2330e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan for (typename StlContainer::const_iterator it = rhs_.begin(); 2331e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan it != rhs_.end(); ++it) { 2332e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan if (internal::ArrayAwareFind( 2333e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan lhs_stl_container.begin(), lhs_stl_container.end(), *it) == 2334e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan lhs_stl_container.end()) { 2335e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan if (printed_header2) { 2336e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan *os << ", "; 2337e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan } else { 2338b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan *os << (printed_header ? ",\nand" : "which") 2339b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan << " doesn't have these expected elements: "; 2340e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan printed_header2 = true; 2341e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan } 2342e2e8ba401d198d1a8304c652e997505609b62696vladlosev UniversalPrint(*it, os); 23436a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan } 23446a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan } 23456a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan } 2346e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan 2347e122e457a6e890faac399c1f86d87cc8d3177ac2zhanyong.wan return false; 23486a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan } 234932de5f53763125925e078498250f7e73a88de9edzhanyong.wan 23506a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan private: 2351b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const StlContainer rhs_; 235232de5f53763125925e078498250f7e73a88de9edzhanyong.wan 235332de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(ContainerEqMatcher); 23546a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan}; 23556a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan 2356898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan// A comparator functor that uses the < operator to compare two values. 2357898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wanstruct LessComparator { 2358898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan template <typename T, typename U> 2359898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan bool operator()(const T& lhs, const U& rhs) const { return lhs < rhs; } 2360898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan}; 2361898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan 2362898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan// Implements WhenSortedBy(comparator, container_matcher). 2363898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wantemplate <typename Comparator, typename ContainerMatcher> 2364898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wanclass WhenSortedByMatcher { 2365898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan public: 2366898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan WhenSortedByMatcher(const Comparator& comparator, 2367898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan const ContainerMatcher& matcher) 2368898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan : comparator_(comparator), matcher_(matcher) {} 2369898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan 2370898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan template <typename LhsContainer> 2371898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan operator Matcher<LhsContainer>() const { 2372898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan return MakeMatcher(new Impl<LhsContainer>(comparator_, matcher_)); 2373898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan } 2374898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan 2375898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan template <typename LhsContainer> 2376898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan class Impl : public MatcherInterface<LhsContainer> { 2377898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan public: 2378898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan typedef internal::StlContainerView< 2379898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan GTEST_REMOVE_REFERENCE_AND_CONST_(LhsContainer)> LhsView; 2380898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan typedef typename LhsView::type LhsStlContainer; 2381898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan typedef typename LhsView::const_reference LhsStlContainerReference; 2382a9a59e06dd7cdfe52c988bf065bc156a7ed96a5czhanyong.wan // Transforms std::pair<const Key, Value> into std::pair<Key, Value> 2383a9a59e06dd7cdfe52c988bf065bc156a7ed96a5czhanyong.wan // so that we can match associative containers. 2384a9a59e06dd7cdfe52c988bf065bc156a7ed96a5czhanyong.wan typedef typename RemoveConstFromKey< 2385a9a59e06dd7cdfe52c988bf065bc156a7ed96a5czhanyong.wan typename LhsStlContainer::value_type>::type LhsValue; 2386898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan 2387898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan Impl(const Comparator& comparator, const ContainerMatcher& matcher) 2388898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan : comparator_(comparator), matcher_(matcher) {} 2389898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan 2390898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan virtual void DescribeTo(::std::ostream* os) const { 2391898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan *os << "(when sorted) "; 2392898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan matcher_.DescribeTo(os); 2393898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan } 2394898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan 2395898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan virtual void DescribeNegationTo(::std::ostream* os) const { 2396898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan *os << "(when sorted) "; 2397898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan matcher_.DescribeNegationTo(os); 2398898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan } 2399898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan 2400898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan virtual bool MatchAndExplain(LhsContainer lhs, 2401898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan MatchResultListener* listener) const { 2402898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs); 2403fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan ::std::vector<LhsValue> sorted_container(lhs_stl_container.begin(), 2404fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan lhs_stl_container.end()); 2405fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan ::std::sort( 2406fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan sorted_container.begin(), sorted_container.end(), comparator_); 2407898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan 2408898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan if (!listener->IsInterested()) { 2409898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan // If the listener is not interested, we do not need to 2410898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan // construct the inner explanation. 2411898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan return matcher_.Matches(sorted_container); 2412898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan } 2413898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan 2414898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan *listener << "which is "; 2415898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan UniversalPrint(sorted_container, listener->stream()); 2416898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan *listener << " when sorted"; 2417898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan 2418898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan StringMatchResultListener inner_listener; 2419898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan const bool match = matcher_.MatchAndExplain(sorted_container, 2420898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan &inner_listener); 2421898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan PrintIfNotEmpty(inner_listener.str(), listener->stream()); 2422898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan return match; 2423898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan } 2424898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan 2425898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan private: 2426898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan const Comparator comparator_; 2427fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan const Matcher<const ::std::vector<LhsValue>&> matcher_; 2428898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan 2429898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan GTEST_DISALLOW_COPY_AND_ASSIGN_(Impl); 2430898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan }; 2431898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan 2432898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan private: 2433898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan const Comparator comparator_; 2434898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan const ContainerMatcher matcher_; 2435898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan 2436898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan GTEST_DISALLOW_ASSIGN_(WhenSortedByMatcher); 2437898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan}; 2438898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan 2439ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// Implements Pointwise(tuple_matcher, rhs_container). tuple_matcher 2440ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// must be able to be safely cast to Matcher<tuple<const T1&, const 2441ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// T2&> >, where T1 and T2 are the types of elements in the LHS 2442ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// container and the RHS container respectively. 2443ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wantemplate <typename TupleMatcher, typename RhsContainer> 2444ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wanclass PointwiseMatcher { 2445ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan public: 2446ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan typedef internal::StlContainerView<RhsContainer> RhsView; 2447ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan typedef typename RhsView::type RhsStlContainer; 2448ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan typedef typename RhsStlContainer::value_type RhsValue; 2449ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan 2450ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan // Like ContainerEq, we make a copy of rhs in case the elements in 2451ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan // it are modified after this matcher is created. 2452ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan PointwiseMatcher(const TupleMatcher& tuple_matcher, const RhsContainer& rhs) 2453ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan : tuple_matcher_(tuple_matcher), rhs_(RhsView::Copy(rhs)) { 2454ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan // Makes sure the user doesn't instantiate this class template 2455ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan // with a const or reference type. 2456ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan (void)testing::StaticAssertTypeEq<RhsContainer, 2457ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan GTEST_REMOVE_REFERENCE_AND_CONST_(RhsContainer)>(); 2458ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan } 2459ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan 2460ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan template <typename LhsContainer> 2461ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan operator Matcher<LhsContainer>() const { 2462ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan return MakeMatcher(new Impl<LhsContainer>(tuple_matcher_, rhs_)); 2463ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan } 2464ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan 2465ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan template <typename LhsContainer> 2466ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan class Impl : public MatcherInterface<LhsContainer> { 2467ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan public: 2468ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan typedef internal::StlContainerView< 2469ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan GTEST_REMOVE_REFERENCE_AND_CONST_(LhsContainer)> LhsView; 2470ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan typedef typename LhsView::type LhsStlContainer; 2471ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan typedef typename LhsView::const_reference LhsStlContainerReference; 2472ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan typedef typename LhsStlContainer::value_type LhsValue; 2473ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan // We pass the LHS value and the RHS value to the inner matcher by 2474ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan // reference, as they may be expensive to copy. We must use tuple 2475ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan // instead of pair here, as a pair cannot hold references (C++ 98, 2476ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan // 20.2.2 [lib.pairs]). 2477fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan typedef ::std::tr1::tuple<const LhsValue&, const RhsValue&> InnerMatcherArg; 2478ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan 2479ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan Impl(const TupleMatcher& tuple_matcher, const RhsStlContainer& rhs) 2480ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan // mono_tuple_matcher_ holds a monomorphic version of the tuple matcher. 2481ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan : mono_tuple_matcher_(SafeMatcherCast<InnerMatcherArg>(tuple_matcher)), 2482ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan rhs_(rhs) {} 2483ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan 2484ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan virtual void DescribeTo(::std::ostream* os) const { 2485ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan *os << "contains " << rhs_.size() 2486ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan << " values, where each value and its corresponding value in "; 2487ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan UniversalPrinter<RhsStlContainer>::Print(rhs_, os); 2488ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan *os << " "; 2489ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan mono_tuple_matcher_.DescribeTo(os); 2490ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan } 2491ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan virtual void DescribeNegationTo(::std::ostream* os) const { 2492ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan *os << "doesn't contain exactly " << rhs_.size() 2493ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan << " values, or contains a value x at some index i" 2494ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan << " where x and the i-th value of "; 2495ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan UniversalPrint(rhs_, os); 2496ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan *os << " "; 2497ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan mono_tuple_matcher_.DescribeNegationTo(os); 2498ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan } 2499ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan 2500ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan virtual bool MatchAndExplain(LhsContainer lhs, 2501ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan MatchResultListener* listener) const { 2502ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan LhsStlContainerReference lhs_stl_container = LhsView::ConstReference(lhs); 2503ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan const size_t actual_size = lhs_stl_container.size(); 2504ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan if (actual_size != rhs_.size()) { 2505ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan *listener << "which contains " << actual_size << " values"; 2506ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan return false; 2507ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan } 2508ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan 2509ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan typename LhsStlContainer::const_iterator left = lhs_stl_container.begin(); 2510ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan typename RhsStlContainer::const_iterator right = rhs_.begin(); 2511ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan for (size_t i = 0; i != actual_size; ++i, ++left, ++right) { 2512ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan const InnerMatcherArg value_pair(*left, *right); 2513ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan 2514ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan if (listener->IsInterested()) { 2515ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan StringMatchResultListener inner_listener; 2516ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan if (!mono_tuple_matcher_.MatchAndExplain( 2517ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan value_pair, &inner_listener)) { 2518ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan *listener << "where the value pair ("; 2519ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan UniversalPrint(*left, listener->stream()); 2520ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan *listener << ", "; 2521ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan UniversalPrint(*right, listener->stream()); 2522ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan *listener << ") at index #" << i << " don't match"; 2523ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan PrintIfNotEmpty(inner_listener.str(), listener->stream()); 2524ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan return false; 2525ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan } 2526ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan } else { 2527ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan if (!mono_tuple_matcher_.Matches(value_pair)) 2528ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan return false; 2529ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan } 2530ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan } 2531ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan 2532ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan return true; 2533ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan } 2534ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan 2535ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan private: 2536ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan const Matcher<InnerMatcherArg> mono_tuple_matcher_; 2537ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan const RhsStlContainer rhs_; 2538ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan 2539ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan GTEST_DISALLOW_ASSIGN_(Impl); 2540ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan }; 2541ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan 2542ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan private: 2543ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan const TupleMatcher tuple_matcher_; 2544ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan const RhsStlContainer rhs_; 2545ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan 2546ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan GTEST_DISALLOW_ASSIGN_(PointwiseMatcher); 2547ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan}; 2548ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan 254933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Holds the logic common to ContainsMatcherImpl and EachMatcherImpl. 2550b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename Container> 255133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wanclass QuantifierMatcherImpl : public MatcherInterface<Container> { 2552b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan public: 2553ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer; 2554b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef StlContainerView<RawContainer> View; 2555b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef typename View::type StlContainer; 2556b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef typename View::const_reference StlContainerReference; 2557b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan typedef typename StlContainer::value_type Element; 2558b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 2559b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan template <typename InnerMatcher> 256033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan explicit QuantifierMatcherImpl(InnerMatcher inner_matcher) 2561b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan : inner_matcher_( 256233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan testing::SafeMatcherCast<const Element&>(inner_matcher)) {} 256333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan 256433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan // Checks whether: 256533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan // * All elements in the container match, if all_elements_should_match. 256633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan // * Any element in the container matches, if !all_elements_should_match. 256733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan bool MatchAndExplainImpl(bool all_elements_should_match, 256833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan Container container, 256933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan MatchResultListener* listener) const { 257033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan StlContainerReference stl_container = View::ConstReference(container); 257133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan size_t i = 0; 257233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan for (typename StlContainer::const_iterator it = stl_container.begin(); 257333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan it != stl_container.end(); ++it, ++i) { 257433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan StringMatchResultListener inner_listener; 257533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan const bool matches = inner_matcher_.MatchAndExplain(*it, &inner_listener); 257633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan 257733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan if (matches != all_elements_should_match) { 257833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan *listener << "whose element #" << i 257933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan << (matches ? " matches" : " doesn't match"); 258033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan PrintIfNotEmpty(inner_listener.str(), listener->stream()); 258133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan return !all_elements_should_match; 258233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan } 258333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan } 258433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan return all_elements_should_match; 258533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan } 258633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan 258733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan protected: 258833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan const Matcher<const Element&> inner_matcher_; 258933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan 259033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan GTEST_DISALLOW_ASSIGN_(QuantifierMatcherImpl); 259133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan}; 259233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan 259333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Implements Contains(element_matcher) for the given argument type Container. 259433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Symmetric to EachMatcherImpl. 259533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wantemplate <typename Container> 259633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wanclass ContainsMatcherImpl : public QuantifierMatcherImpl<Container> { 259733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan public: 259833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan template <typename InnerMatcher> 259933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan explicit ContainsMatcherImpl(InnerMatcher inner_matcher) 260033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan : QuantifierMatcherImpl<Container>(inner_matcher) {} 2601b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 2602b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // Describes what this matcher does. 2603b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan virtual void DescribeTo(::std::ostream* os) const { 2604b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan *os << "contains at least one element that "; 260533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan this->inner_matcher_.DescribeTo(os); 2606b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } 2607b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 2608b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan virtual void DescribeNegationTo(::std::ostream* os) const { 2609b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan *os << "doesn't contain any element that "; 261033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan this->inner_matcher_.DescribeTo(os); 2611b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } 2612b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 2613821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual bool MatchAndExplain(Container container, 2614821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan MatchResultListener* listener) const { 261533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan return this->MatchAndExplainImpl(false, container, listener); 2616b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } 2617b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 2618b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan private: 261932de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(ContainsMatcherImpl); 2620b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}; 2621b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 262233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Implements Each(element_matcher) for the given argument type Container. 262333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Symmetric to ContainsMatcherImpl. 262433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wantemplate <typename Container> 262533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wanclass EachMatcherImpl : public QuantifierMatcherImpl<Container> { 262633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan public: 262733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan template <typename InnerMatcher> 262833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan explicit EachMatcherImpl(InnerMatcher inner_matcher) 262933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan : QuantifierMatcherImpl<Container>(inner_matcher) {} 263033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan 263133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan // Describes what this matcher does. 263233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan virtual void DescribeTo(::std::ostream* os) const { 263333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan *os << "only contains elements that "; 263433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan this->inner_matcher_.DescribeTo(os); 263533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan } 263633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan 263733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan virtual void DescribeNegationTo(::std::ostream* os) const { 263833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan *os << "contains some element that "; 263933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan this->inner_matcher_.DescribeNegationTo(os); 264033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan } 264133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan 264233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan virtual bool MatchAndExplain(Container container, 264333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan MatchResultListener* listener) const { 264433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan return this->MatchAndExplainImpl(true, container, listener); 264533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan } 264633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan 264733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan private: 264833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan GTEST_DISALLOW_ASSIGN_(EachMatcherImpl); 264933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan}; 265033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan 2651b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Implements polymorphic Contains(element_matcher). 2652b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename M> 2653b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wanclass ContainsMatcher { 2654b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan public: 2655b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan explicit ContainsMatcher(M m) : inner_matcher_(m) {} 2656b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 2657b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan template <typename Container> 2658b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan operator Matcher<Container>() const { 2659b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan return MakeMatcher(new ContainsMatcherImpl<Container>(inner_matcher_)); 2660b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan } 2661b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 2662b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan private: 2663b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan const M inner_matcher_; 266432de5f53763125925e078498250f7e73a88de9edzhanyong.wan 266532de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(ContainsMatcher); 2666b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan}; 2667b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 266833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Implements polymorphic Each(element_matcher). 266933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wantemplate <typename M> 267033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wanclass EachMatcher { 267133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan public: 267233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan explicit EachMatcher(M m) : inner_matcher_(m) {} 267333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan 267433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan template <typename Container> 267533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan operator Matcher<Container>() const { 267633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan return MakeMatcher(new EachMatcherImpl<Container>(inner_matcher_)); 267733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan } 267833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan 267933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan private: 268033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan const M inner_matcher_; 268133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan 268233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan GTEST_DISALLOW_ASSIGN_(EachMatcher); 268333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan}; 268433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan 2685b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// Implements Key(inner_matcher) for the given argument pair type. 2686b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// Key(inner_matcher) matches an std::pair whose 'first' field matches 2687b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// inner_matcher. For example, Contains(Key(Ge(5))) can be used to match an 2688b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// std::map that contains at least one element whose key is >= 5. 2689b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wantemplate <typename PairType> 2690b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wanclass KeyMatcherImpl : public MatcherInterface<PairType> { 2691b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan public: 2692ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan typedef GTEST_REMOVE_REFERENCE_AND_CONST_(PairType) RawPairType; 2693b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan typedef typename RawPairType::first_type KeyType; 2694b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan 2695b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan template <typename InnerMatcher> 2696b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan explicit KeyMatcherImpl(InnerMatcher inner_matcher) 2697b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan : inner_matcher_( 2698b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan testing::SafeMatcherCast<const KeyType&>(inner_matcher)) { 2699b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan } 2700b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan 2701b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan // Returns true iff 'key_value.first' (the key) matches the inner matcher. 2702821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual bool MatchAndExplain(PairType key_value, 2703821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan MatchResultListener* listener) const { 2704b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan StringMatchResultListener inner_listener; 2705b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan const bool match = inner_matcher_.MatchAndExplain(key_value.first, 2706b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan &inner_listener); 2707b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan const internal::string explanation = inner_listener.str(); 2708b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan if (explanation != "") { 2709b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan *listener << "whose first field is a value " << explanation; 2710b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan } 2711b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan return match; 2712b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan } 2713b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan 2714b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan // Describes what this matcher does. 2715b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan virtual void DescribeTo(::std::ostream* os) const { 2716b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan *os << "has a key that "; 2717b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan inner_matcher_.DescribeTo(os); 2718b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan } 2719b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan 2720b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan // Describes what the negation of this matcher does. 2721b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan virtual void DescribeNegationTo(::std::ostream* os) const { 2722b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan *os << "doesn't have a key that "; 2723b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan inner_matcher_.DescribeTo(os); 2724b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan } 2725b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan 2726b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan private: 2727b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan const Matcher<const KeyType&> inner_matcher_; 272832de5f53763125925e078498250f7e73a88de9edzhanyong.wan 272932de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(KeyMatcherImpl); 2730b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan}; 2731b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan 2732b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// Implements polymorphic Key(matcher_for_key). 2733b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wantemplate <typename M> 2734b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wanclass KeyMatcher { 2735b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan public: 2736b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan explicit KeyMatcher(M m) : matcher_for_key_(m) {} 2737b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan 2738b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan template <typename PairType> 2739b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan operator Matcher<PairType>() const { 2740b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan return MakeMatcher(new KeyMatcherImpl<PairType>(matcher_for_key_)); 2741b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan } 2742b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan 2743b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan private: 2744b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan const M matcher_for_key_; 274532de5f53763125925e078498250f7e73a88de9edzhanyong.wan 274632de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(KeyMatcher); 2747b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan}; 2748b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan 2749f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// Implements Pair(first_matcher, second_matcher) for the given argument pair 2750f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// type with its two matchers. See Pair() function below. 2751f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wantemplate <typename PairType> 2752f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wanclass PairMatcherImpl : public MatcherInterface<PairType> { 2753f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan public: 2754ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan typedef GTEST_REMOVE_REFERENCE_AND_CONST_(PairType) RawPairType; 2755f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan typedef typename RawPairType::first_type FirstType; 2756f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan typedef typename RawPairType::second_type SecondType; 2757f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan 2758f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan template <typename FirstMatcher, typename SecondMatcher> 2759f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan PairMatcherImpl(FirstMatcher first_matcher, SecondMatcher second_matcher) 2760f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan : first_matcher_( 2761f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan testing::SafeMatcherCast<const FirstType&>(first_matcher)), 2762f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan second_matcher_( 2763f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan testing::SafeMatcherCast<const SecondType&>(second_matcher)) { 2764f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan } 2765f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan 2766f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan // Describes what this matcher does. 2767f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan virtual void DescribeTo(::std::ostream* os) const { 2768f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan *os << "has a first field that "; 2769f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan first_matcher_.DescribeTo(os); 2770f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan *os << ", and has a second field that "; 2771f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan second_matcher_.DescribeTo(os); 2772f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan } 2773f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan 2774f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan // Describes what the negation of this matcher does. 2775f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan virtual void DescribeNegationTo(::std::ostream* os) const { 2776f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan *os << "has a first field that "; 2777f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan first_matcher_.DescribeNegationTo(os); 2778f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan *os << ", or has a second field that "; 2779f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan second_matcher_.DescribeNegationTo(os); 2780f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan } 2781f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan 2782821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // Returns true iff 'a_pair.first' matches first_matcher and 'a_pair.second' 2783821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // matches second_matcher. 2784821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual bool MatchAndExplain(PairType a_pair, 2785821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan MatchResultListener* listener) const { 2786676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan if (!listener->IsInterested()) { 2787676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan // If the listener is not interested, we don't need to construct the 2788676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan // explanation. 2789676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan return first_matcher_.Matches(a_pair.first) && 2790676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan second_matcher_.Matches(a_pair.second); 2791821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan } 2792676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan StringMatchResultListener first_inner_listener; 2793676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan if (!first_matcher_.MatchAndExplain(a_pair.first, 2794676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan &first_inner_listener)) { 2795676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan *listener << "whose first field does not match"; 2796b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan PrintIfNotEmpty(first_inner_listener.str(), listener->stream()); 2797821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return false; 2798f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan } 2799676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan StringMatchResultListener second_inner_listener; 2800676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan if (!second_matcher_.MatchAndExplain(a_pair.second, 2801676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan &second_inner_listener)) { 2802676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan *listener << "whose second field does not match"; 2803b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan PrintIfNotEmpty(second_inner_listener.str(), listener->stream()); 2804821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return false; 2805f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan } 2806676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan ExplainSuccess(first_inner_listener.str(), second_inner_listener.str(), 2807676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan listener); 2808821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return true; 2809f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan } 2810f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan 2811f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan private: 2812676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan void ExplainSuccess(const internal::string& first_explanation, 2813676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan const internal::string& second_explanation, 2814676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan MatchResultListener* listener) const { 2815676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan *listener << "whose both fields match"; 2816676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan if (first_explanation != "") { 2817676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan *listener << ", where the first field is a value " << first_explanation; 2818676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan } 2819676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan if (second_explanation != "") { 2820676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan *listener << ", "; 2821676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan if (first_explanation != "") { 2822676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan *listener << "and "; 2823676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan } else { 2824676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan *listener << "where "; 2825676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan } 2826676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan *listener << "the second field is a value " << second_explanation; 2827676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan } 2828676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan } 2829676e8cc6092853c9dbf1eeab2402be0069d8fb7ezhanyong.wan 2830f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan const Matcher<const FirstType&> first_matcher_; 2831f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan const Matcher<const SecondType&> second_matcher_; 283232de5f53763125925e078498250f7e73a88de9edzhanyong.wan 283332de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(PairMatcherImpl); 2834f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan}; 2835f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan 2836f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// Implements polymorphic Pair(first_matcher, second_matcher). 2837f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wantemplate <typename FirstMatcher, typename SecondMatcher> 2838f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wanclass PairMatcher { 2839f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan public: 2840f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan PairMatcher(FirstMatcher first_matcher, SecondMatcher second_matcher) 2841f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan : first_matcher_(first_matcher), second_matcher_(second_matcher) {} 2842f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan 2843f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan template <typename PairType> 2844f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan operator Matcher<PairType> () const { 2845f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan return MakeMatcher( 2846f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan new PairMatcherImpl<PairType>( 2847f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan first_matcher_, second_matcher_)); 2848f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan } 2849f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan 2850f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan private: 2851f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan const FirstMatcher first_matcher_; 2852f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan const SecondMatcher second_matcher_; 285332de5f53763125925e078498250f7e73a88de9edzhanyong.wan 285432de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(PairMatcher); 2855f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan}; 2856f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan 28571afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// Implements ElementsAre() and ElementsAreArray(). 28581afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wantemplate <typename Container> 28591afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wanclass ElementsAreMatcherImpl : public MatcherInterface<Container> { 28601afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan public: 2861ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer; 28621afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan typedef internal::StlContainerView<RawContainer> View; 28631afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan typedef typename View::type StlContainer; 28641afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan typedef typename View::const_reference StlContainerReference; 28651afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan typedef typename StlContainer::value_type Element; 28661afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 28671afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan // Constructs the matcher from a sequence of element values or 28681afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan // element matchers. 28691afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan template <typename InputIter> 287038513a8bb154f0b6d0a4088814fe92552696d465jgm ElementsAreMatcherImpl(InputIter first, InputIter last) { 287138513a8bb154f0b6d0a4088814fe92552696d465jgm while (first != last) { 287238513a8bb154f0b6d0a4088814fe92552696d465jgm matchers_.push_back(MatcherCast<const Element&>(*first++)); 28731afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 28741afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 28751afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 28761afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan // Describes what this matcher does. 28771afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan virtual void DescribeTo(::std::ostream* os) const { 28781afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan if (count() == 0) { 28791afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan *os << "is empty"; 28801afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } else if (count() == 1) { 28811afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan *os << "has 1 element that "; 28821afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan matchers_[0].DescribeTo(os); 28831afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } else { 28841afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan *os << "has " << Elements(count()) << " where\n"; 28851afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan for (size_t i = 0; i != count(); ++i) { 2886b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan *os << "element #" << i << " "; 28871afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan matchers_[i].DescribeTo(os); 28881afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan if (i + 1 < count()) { 28891afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan *os << ",\n"; 28901afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 28911afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 28921afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 28931afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 28941afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 28951afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan // Describes what the negation of this matcher does. 28961afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan virtual void DescribeNegationTo(::std::ostream* os) const { 28971afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan if (count() == 0) { 2898b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan *os << "isn't empty"; 28991afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan return; 29001afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 29011afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 2902b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan *os << "doesn't have " << Elements(count()) << ", or\n"; 29031afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan for (size_t i = 0; i != count(); ++i) { 2904b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan *os << "element #" << i << " "; 29051afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan matchers_[i].DescribeNegationTo(os); 29061afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan if (i + 1 < count()) { 29071afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan *os << ", or\n"; 29081afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 29091afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 29101afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 29111afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 2912821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan virtual bool MatchAndExplain(Container container, 2913821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan MatchResultListener* listener) const { 29141afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan StlContainerReference stl_container = View::ConstReference(container); 2915821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan const size_t actual_count = stl_container.size(); 2916821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (actual_count != count()) { 2917821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // The element count doesn't match. If the container is empty, 2918821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // there's no need to explain anything as Google Mock already 2919821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // prints the empty container. Otherwise we just need to show 2920821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // how many elements there actually are. 2921fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan if (actual_count != 0 && listener->IsInterested()) { 2922b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan *listener << "which has " << Elements(actual_count); 29231afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 2924821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return false; 2925821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan } 29261afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 2927821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan typename StlContainer::const_iterator it = stl_container.begin(); 2928821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // explanations[i] is the explanation of the element at index i. 2929fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan ::std::vector<internal::string> explanations(count()); 2930821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan for (size_t i = 0; i != count(); ++it, ++i) { 2931821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan StringMatchResultListener s; 2932821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (matchers_[i].MatchAndExplain(*it, &s)) { 2933821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan explanations[i] = s.str(); 2934821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan } else { 2935821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // The container has the right size but the i-th element 2936821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // doesn't match its expectation. 2937b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan *listener << "whose element #" << i << " doesn't match"; 2938b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan PrintIfNotEmpty(s.str(), listener->stream()); 2939821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return false; 2940821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan } 2941821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan } 29421afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 2943821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // Every element matches its expectation. We need to explain why 2944821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan // (the obvious ones can be skipped). 2945821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan bool reason_printed = false; 2946821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan for (size_t i = 0; i != count(); ++i) { 2947821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan const internal::string& s = explanations[i]; 2948821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (!s.empty()) { 2949821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan if (reason_printed) { 2950b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan *listener << ",\nand "; 29511afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 2952b1c7f93c52d7fbf484f34d01a65cfaec03786564zhanyong.wan *listener << "whose element #" << i << " matches, " << s; 2953821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan reason_printed = true; 29541afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 29551afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 2956821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan 2957821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return true; 29581afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 29591afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 29601afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan private: 29611afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan static Message Elements(size_t count) { 29621afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan return Message() << count << (count == 1 ? " element" : " elements"); 29631afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 29641afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 29651afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan size_t count() const { return matchers_.size(); } 2966fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 2967fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan ::std::vector<Matcher<const Element&> > matchers_; 296832de5f53763125925e078498250f7e73a88de9edzhanyong.wan 296932de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(ElementsAreMatcherImpl); 29701afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan}; 29711afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 2972fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// Connectivity matrix of (elements X matchers), in element-major order. 2973fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// Initially, there are no edges. 2974fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// Use NextGraph() to iterate over all possible edge configurations. 2975fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// Use Randomize() to generate a random edge configuration. 2976fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanclass GTEST_API_ MatchMatrix { 29771afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan public: 2978fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan MatchMatrix(size_t num_elements, size_t num_matchers) 2979fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan : num_elements_(num_elements), 2980fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan num_matchers_(num_matchers), 2981fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan matched_(num_elements_* num_matchers_, 0) { 2982fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan } 2983fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 2984fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan size_t LhsSize() const { return num_elements_; } 2985fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan size_t RhsSize() const { return num_matchers_; } 2986fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan bool HasEdge(size_t ilhs, size_t irhs) const { 2987fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan return matched_[SpaceIndex(ilhs, irhs)] == 1; 2988fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan } 2989fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan void SetEdge(size_t ilhs, size_t irhs, bool b) { 2990fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan matched_[SpaceIndex(ilhs, irhs)] = b ? 1 : 0; 2991fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan } 2992fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 2993fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // Treating the connectivity matrix as a (LhsSize()*RhsSize())-bit number, 2994fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // adds 1 to that number; returns false if incrementing the graph left it 2995fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // empty. 2996fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan bool NextGraph(); 2997fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 2998fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan void Randomize(); 2999fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3000fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan string DebugString() const; 3001fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3002fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan private: 3003fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan size_t SpaceIndex(size_t ilhs, size_t irhs) const { 3004fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan return ilhs * num_matchers_ + irhs; 3005fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan } 3006fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3007fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan size_t num_elements_; 3008fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan size_t num_matchers_; 3009fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3010fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // Each element is a char interpreted as bool. They are stored as a 3011fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // flattened array in lhs-major order, use 'SpaceIndex()' to translate 3012fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // a (ilhs, irhs) matrix coordinate into an offset. 3013fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan ::std::vector<char> matched_; 3014fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan}; 3015fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3016fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantypedef ::std::pair<size_t, size_t> ElementMatcherPair; 3017fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantypedef ::std::vector<ElementMatcherPair> ElementMatcherPairs; 3018fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3019fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// Returns a maximum bipartite matching for the specified graph 'g'. 3020fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// The matching is represented as a vector of {element, matcher} pairs. 3021fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanGTEST_API_ ElementMatcherPairs 3022fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanFindMaxBipartiteMatching(const MatchMatrix& g); 3023fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3024fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanGTEST_API_ bool FindPairing(const MatchMatrix& matrix, 3025fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan MatchResultListener* listener); 3026fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3027fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// Untyped base class for implementing UnorderedElementsAre. By 3028fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// putting logic that's not specific to the element type here, we 3029fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// reduce binary bloat and increase compilation speed. 3030fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanclass GTEST_API_ UnorderedElementsAreMatcherImplBase { 3031fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan protected: 3032fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // A vector of matcher describers, one for each element matcher. 3033fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // Does not own the describers (and thus can be used only when the 3034fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // element matchers are alive). 3035fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan typedef ::std::vector<const MatcherDescriberInterface*> MatcherDescriberVec; 3036fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3037fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // Describes this UnorderedElementsAre matcher. 3038fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan void DescribeToImpl(::std::ostream* os) const; 3039fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3040fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // Describes the negation of this UnorderedElementsAre matcher. 3041fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan void DescribeNegationToImpl(::std::ostream* os) const; 3042fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3043fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan bool VerifyAllElementsAndMatchersAreMatched( 3044fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan const ::std::vector<string>& element_printouts, 3045fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan const MatchMatrix& matrix, 3046fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan MatchResultListener* listener) const; 3047fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3048fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan MatcherDescriberVec& matcher_describers() { 3049fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan return matcher_describers_; 3050fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan } 3051fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3052fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan static Message Elements(size_t n) { 3053fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan return Message() << n << " element" << (n == 1 ? "" : "s"); 3054fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan } 3055fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3056fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan private: 3057fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan MatcherDescriberVec matcher_describers_; 3058fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3059fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan GTEST_DISALLOW_ASSIGN_(UnorderedElementsAreMatcherImplBase); 3060fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan}; 3061fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3062fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// Implements unordered ElementsAre and unordered ElementsAreArray. 3063fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantemplate <typename Container> 3064fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanclass UnorderedElementsAreMatcherImpl 3065fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan : public MatcherInterface<Container>, 3066fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan public UnorderedElementsAreMatcherImplBase { 3067fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan public: 3068fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer; 3069fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan typedef internal::StlContainerView<RawContainer> View; 3070fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan typedef typename View::type StlContainer; 3071fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan typedef typename View::const_reference StlContainerReference; 3072fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan typedef typename StlContainer::const_iterator StlContainerConstIterator; 3073fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan typedef typename StlContainer::value_type Element; 3074fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3075fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // Constructs the matcher from a sequence of element values or 3076fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // element matchers. 3077fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan template <typename InputIter> 3078fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan UnorderedElementsAreMatcherImpl(InputIter first, InputIter last) { 3079fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan for (; first != last; ++first) { 3080fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan matchers_.push_back(MatcherCast<const Element&>(*first)); 3081fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan matcher_describers().push_back(matchers_.back().GetDescriber()); 3082fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan } 3083fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan } 3084fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3085fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // Describes what this matcher does. 3086fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan virtual void DescribeTo(::std::ostream* os) const { 3087fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan return UnorderedElementsAreMatcherImplBase::DescribeToImpl(os); 3088fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan } 3089fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3090fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // Describes what the negation of this matcher does. 3091fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan virtual void DescribeNegationTo(::std::ostream* os) const { 3092fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan return UnorderedElementsAreMatcherImplBase::DescribeNegationToImpl(os); 3093fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan } 3094fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3095fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan virtual bool MatchAndExplain(Container container, 3096fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan MatchResultListener* listener) const { 3097fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan StlContainerReference stl_container = View::ConstReference(container); 30985579c1a8b1591d4932495b8cb3cc61f3edca2555zhanyong.wan ::std::vector<string> element_printouts; 30995579c1a8b1591d4932495b8cb3cc61f3edca2555zhanyong.wan MatchMatrix matrix = AnalyzeElements(stl_container.begin(), 31005579c1a8b1591d4932495b8cb3cc61f3edca2555zhanyong.wan stl_container.end(), 31015579c1a8b1591d4932495b8cb3cc61f3edca2555zhanyong.wan &element_printouts, 31025579c1a8b1591d4932495b8cb3cc61f3edca2555zhanyong.wan listener); 3103fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 31045579c1a8b1591d4932495b8cb3cc61f3edca2555zhanyong.wan const size_t actual_count = matrix.LhsSize(); 3105fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan if (actual_count == 0 && matchers_.empty()) { 3106fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan return true; 3107fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan } 3108fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan if (actual_count != matchers_.size()) { 3109fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // The element count doesn't match. If the container is empty, 3110fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // there's no need to explain anything as Google Mock already 3111fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // prints the empty container. Otherwise we just need to show 3112fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan // how many elements there actually are. 3113fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan if (actual_count != 0 && listener->IsInterested()) { 3114fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan *listener << "which has " << Elements(actual_count); 3115fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan } 3116fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan return false; 3117fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan } 3118fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3119fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan return VerifyAllElementsAndMatchersAreMatched(element_printouts, 3120fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan matrix, listener) && 3121fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan FindPairing(matrix, listener); 3122fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan } 3123fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3124fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan private: 3125fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan typedef ::std::vector<Matcher<const Element&> > MatcherVec; 3126fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3127fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan template <typename ElementIter> 3128fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan MatchMatrix AnalyzeElements(ElementIter elem_first, ElementIter elem_last, 3129fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan ::std::vector<string>* element_printouts, 3130fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan MatchResultListener* listener) const { 31315579c1a8b1591d4932495b8cb3cc61f3edca2555zhanyong.wan element_printouts->clear(); 3132fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan ::std::vector<char> did_match; 3133fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan size_t num_elements = 0; 3134fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan for (; elem_first != elem_last; ++num_elements, ++elem_first) { 3135fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan if (listener->IsInterested()) { 3136fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan element_printouts->push_back(PrintToString(*elem_first)); 3137fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan } 3138fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan for (size_t irhs = 0; irhs != matchers_.size(); ++irhs) { 3139fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan did_match.push_back(Matches(matchers_[irhs])(*elem_first)); 3140fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan } 3141fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan } 3142fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3143fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan MatchMatrix matrix(num_elements, matchers_.size()); 3144fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan ::std::vector<char>::const_iterator did_match_iter = did_match.begin(); 3145fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan for (size_t ilhs = 0; ilhs != num_elements; ++ilhs) { 3146fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan for (size_t irhs = 0; irhs != matchers_.size(); ++irhs) { 3147fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan matrix.SetEdge(ilhs, irhs, *did_match_iter++ != 0); 3148fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan } 3149fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan } 3150fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan return matrix; 3151fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan } 3152fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3153fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan MatcherVec matchers_; 3154fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3155fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan GTEST_DISALLOW_ASSIGN_(UnorderedElementsAreMatcherImpl); 3156fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan}; 3157fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3158fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// Functor for use in TransformTuple. 3159fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// Performs MatcherCast<Target> on an input argument of any type. 3160fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantemplate <typename Target> 3161fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanstruct CastAndAppendTransform { 3162fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan template <typename Arg> 3163fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan Matcher<Target> operator()(const Arg& a) const { 3164fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan return MatcherCast<Target>(a); 3165fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan } 3166fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan}; 3167fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3168fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// Implements UnorderedElementsAre. 3169fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantemplate <typename MatcherTuple> 3170fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanclass UnorderedElementsAreMatcher { 3171fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan public: 3172fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan explicit UnorderedElementsAreMatcher(const MatcherTuple& args) 3173fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan : matchers_(args) {} 3174fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3175fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan template <typename Container> 3176fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan operator Matcher<Container>() const { 3177fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer; 3178fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan typedef typename internal::StlContainerView<RawContainer>::type View; 3179fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan typedef typename View::value_type Element; 3180fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan typedef ::std::vector<Matcher<const Element&> > MatcherVec; 3181fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan MatcherVec matchers; 3182fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan matchers.reserve(::std::tr1::tuple_size<MatcherTuple>::value); 3183fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan TransformTupleValues(CastAndAppendTransform<const Element&>(), matchers_, 3184fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan ::std::back_inserter(matchers)); 3185fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan return MakeMatcher(new UnorderedElementsAreMatcherImpl<Container>( 3186fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan matchers.begin(), matchers.end())); 3187fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan } 3188fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3189fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan private: 3190fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan const MatcherTuple matchers_; 3191fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan GTEST_DISALLOW_ASSIGN_(UnorderedElementsAreMatcher); 3192fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan}; 3193fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3194fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// Implements ElementsAre. 3195fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantemplate <typename MatcherTuple> 3196fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanclass ElementsAreMatcher { 3197fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan public: 3198fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan explicit ElementsAreMatcher(const MatcherTuple& args) : matchers_(args) {} 31991afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 32001afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan template <typename Container> 32011afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan operator Matcher<Container>() const { 3202ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer; 3203fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan typedef typename internal::StlContainerView<RawContainer>::type View; 3204fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan typedef typename View::value_type Element; 3205fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan typedef ::std::vector<Matcher<const Element&> > MatcherVec; 3206fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan MatcherVec matchers; 3207fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan matchers.reserve(::std::tr1::tuple_size<MatcherTuple>::value); 3208fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan TransformTupleValues(CastAndAppendTransform<const Element&>(), matchers_, 3209fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan ::std::back_inserter(matchers)); 3210fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan return MakeMatcher(new ElementsAreMatcherImpl<Container>( 3211fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan matchers.begin(), matchers.end())); 3212fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan } 3213fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3214fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan private: 3215fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan const MatcherTuple matchers_; 3216fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan GTEST_DISALLOW_ASSIGN_(ElementsAreMatcher); 3217fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan}; 32181afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 3219fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// Implements UnorderedElementsAreArray(). 3220fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantemplate <typename T> 3221fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanclass UnorderedElementsAreArrayMatcher { 3222fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan public: 3223fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan UnorderedElementsAreArrayMatcher() {} 3224fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3225fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan template <typename Iter> 3226fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan UnorderedElementsAreArrayMatcher(Iter first, Iter last) 3227fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan : matchers_(first, last) {} 3228fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3229fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan template <typename Container> 3230fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan operator Matcher<Container>() const { 3231fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan return MakeMatcher( 3232fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan new UnorderedElementsAreMatcherImpl<Container>(matchers_.begin(), 3233fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan matchers_.end())); 32341afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 3235fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3236fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan private: 3237fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan ::std::vector<T> matchers_; 3238fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3239fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan GTEST_DISALLOW_ASSIGN_(UnorderedElementsAreArrayMatcher); 32401afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan}; 32411afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 32421afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan// Implements ElementsAreArray(). 32431afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wantemplate <typename T> 32441afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wanclass ElementsAreArrayMatcher { 32451afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan public: 324638513a8bb154f0b6d0a4088814fe92552696d465jgm template <typename Iter> 324738513a8bb154f0b6d0a4088814fe92552696d465jgm ElementsAreArrayMatcher(Iter first, Iter last) : matchers_(first, last) {} 32481afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 32491afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan template <typename Container> 32501afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan operator Matcher<Container>() const { 325138513a8bb154f0b6d0a4088814fe92552696d465jgm return MakeMatcher(new ElementsAreMatcherImpl<Container>( 325238513a8bb154f0b6d0a4088814fe92552696d465jgm matchers_.begin(), matchers_.end())); 32531afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan } 32541afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 32551afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan private: 3256fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan const ::std::vector<T> matchers_; 325732de5f53763125925e078498250f7e73a88de9edzhanyong.wan 325832de5f53763125925e078498250f7e73a88de9edzhanyong.wan GTEST_DISALLOW_ASSIGN_(ElementsAreArrayMatcher); 32591afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan}; 32601afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 3261b4140808f98ff09c43ca1ddaa8ff13bc47cd0089zhanyong.wan// Returns the description for a matcher defined using the MATCHER*() 3262b4140808f98ff09c43ca1ddaa8ff13bc47cd0089zhanyong.wan// macro where the user-supplied description string is "", if 3263b4140808f98ff09c43ca1ddaa8ff13bc47cd0089zhanyong.wan// 'negation' is false; otherwise returns the description of the 3264b4140808f98ff09c43ca1ddaa8ff13bc47cd0089zhanyong.wan// negation of the matcher. 'param_values' contains a list of strings 3265b4140808f98ff09c43ca1ddaa8ff13bc47cd0089zhanyong.wan// that are the print-out of the matcher's parameters. 3266587c1b37c2f0b6d430fb13ce09326db0135b557cvladlosevGTEST_API_ string FormatMatcherDescription(bool negation, 3267587c1b37c2f0b6d430fb13ce09326db0135b557cvladlosev const char* matcher_name, 3268587c1b37c2f0b6d430fb13ce09326db0135b557cvladlosev const Strings& param_values); 32691afe1c7971e649ae8b85a39fc1ab6ac595e1dd58zhanyong.wan 3270e35fdd936d133bf8a48de140a3c666897588a05shiqian} // namespace internal 3271e35fdd936d133bf8a48de140a3c666897588a05shiqian 3272fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// ElementsAreArray(first, last) 3273fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// ElementsAreArray(pointer, count) 3274fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// ElementsAreArray(array) 3275fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// ElementsAreArray(vector) 3276fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// 3277fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// The ElementsAreArray() functions are like ElementsAre(...), except that 3278fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// they are given a homogeneous sequence rather than taking each element as 3279fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// a function argument. The sequence can be specified as an array, a 3280fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// pointer and count, a vector, or an STL iterator range. In each of these 3281fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// cases, the underlying sequence can be either a sequence of values or a 3282fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// sequence of matchers. 3283fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// 3284fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// * ElementsAreArray(array) deduces the size of the array. 3285fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// 3286fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// * ElementsAreArray(pointer, count) form takes a pointer and count. 3287fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// 3288fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// * ElementsAreArray(vector) takes a std::vector. 3289fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// 3290fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// * ElementsAreArray(first, last) takes any iterator range. 3291fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// 3292fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// All forms of ElementsAreArray() make a copy of the input matcher sequence. 3293fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3294fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantemplate <typename Iter> 3295fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.waninline internal::ElementsAreArrayMatcher< 3296fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan typename ::std::iterator_traits<Iter>::value_type> 3297fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanElementsAreArray(Iter first, Iter last) { 3298fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan typedef typename ::std::iterator_traits<Iter>::value_type T; 3299fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan return internal::ElementsAreArrayMatcher<T>(first, last); 3300fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan} 3301fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3302fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantemplate <typename T> 3303fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.waninline internal::ElementsAreArrayMatcher<T> ElementsAreArray( 3304fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan const T* pointer, size_t count) { 3305fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan return ElementsAreArray(pointer, pointer + count); 3306fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan} 3307fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3308fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantemplate <typename T, size_t N> 3309fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.waninline internal::ElementsAreArrayMatcher<T> ElementsAreArray( 3310fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan const T (&array)[N]) { 3311fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan return ElementsAreArray(array, N); 3312fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan} 3313fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3314fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantemplate <typename T, typename A> 3315fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.waninline internal::ElementsAreArrayMatcher<T> ElementsAreArray( 3316fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan const ::std::vector<T, A>& vec) { 3317fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan return ElementsAreArray(vec.begin(), vec.end()); 3318fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan} 3319fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3320fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// UnorderedElementsAreArray(first, last) 3321fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// UnorderedElementsAreArray(pointer, count) 3322fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// UnorderedElementsAreArray(array) 3323fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// UnorderedElementsAreArray(vector) 3324fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// 3325fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// The UnorderedElementsAreArray() functions are like 3326fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan// ElementsAreArray(...), but allow matching the elements in any order. 3327fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantemplate <typename Iter> 3328fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.waninline internal::UnorderedElementsAreArrayMatcher< 3329fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan typename ::std::iterator_traits<Iter>::value_type> 3330fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanUnorderedElementsAreArray(Iter first, Iter last) { 3331fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan typedef typename ::std::iterator_traits<Iter>::value_type T; 3332fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan return internal::UnorderedElementsAreArrayMatcher<T>(first, last); 3333fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan} 3334fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3335fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantemplate <typename T> 3336fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.waninline internal::UnorderedElementsAreArrayMatcher<T> 3337fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanUnorderedElementsAreArray(const T* pointer, size_t count) { 3338fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan return UnorderedElementsAreArray(pointer, pointer + count); 3339fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan} 3340fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3341fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantemplate <typename T, size_t N> 3342fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.waninline internal::UnorderedElementsAreArrayMatcher<T> 3343fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanUnorderedElementsAreArray(const T (&array)[N]) { 3344fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan return UnorderedElementsAreArray(array, N); 3345fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan} 3346fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3347fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wantemplate <typename T, typename A> 3348fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.waninline internal::UnorderedElementsAreArrayMatcher<T> 3349fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wanUnorderedElementsAreArray(const ::std::vector<T, A>& vec) { 3350fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan return UnorderedElementsAreArray(vec.begin(), vec.end()); 3351fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan} 3352fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3353fb25d5391143a0fd4cbce862f19472ddc2a1ecabzhanyong.wan 3354e35fdd936d133bf8a48de140a3c666897588a05shiqian// _ is a matcher that matches anything of any type. 3355e35fdd936d133bf8a48de140a3c666897588a05shiqian// 3356e35fdd936d133bf8a48de140a3c666897588a05shiqian// This definition is fine as: 3357e35fdd936d133bf8a48de140a3c666897588a05shiqian// 3358e35fdd936d133bf8a48de140a3c666897588a05shiqian// 1. The C++ standard permits using the name _ in a namespace that 3359e35fdd936d133bf8a48de140a3c666897588a05shiqian// is not the global namespace or ::std. 3360e35fdd936d133bf8a48de140a3c666897588a05shiqian// 2. The AnythingMatcher class has no data member or constructor, 3361e35fdd936d133bf8a48de140a3c666897588a05shiqian// so it's OK to create global variables of this type. 3362e35fdd936d133bf8a48de140a3c666897588a05shiqian// 3. c-style has approved of using _ in this case. 3363e35fdd936d133bf8a48de140a3c666897588a05shiqianconst internal::AnythingMatcher _ = {}; 3364e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any value of the given type T. 3365e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 3366e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<T> A() { return MakeMatcher(new internal::AnyMatcherImpl<T>()); } 3367e35fdd936d133bf8a48de140a3c666897588a05shiqian 3368e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any value of the given type T. 3369e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 3370e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<T> An() { return A<T>(); } 3371e35fdd936d133bf8a48de140a3c666897588a05shiqian 3372e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything equal to x. 3373e35fdd936d133bf8a48de140a3c666897588a05shiqian// Note: if the parameter of Eq() were declared as const T&, Eq("foo") 3374e35fdd936d133bf8a48de140a3c666897588a05shiqian// wouldn't compile. 3375e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 3376e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::EqMatcher<T> Eq(T x) { return internal::EqMatcher<T>(x); } 3377e35fdd936d133bf8a48de140a3c666897588a05shiqian 3378e35fdd936d133bf8a48de140a3c666897588a05shiqian// Constructs a Matcher<T> from a 'value' of type T. The constructed 3379e35fdd936d133bf8a48de140a3c666897588a05shiqian// matcher matches any value that's equal to 'value'. 3380e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 3381e35fdd936d133bf8a48de140a3c666897588a05shiqianMatcher<T>::Matcher(T value) { *this = Eq(value); } 3382e35fdd936d133bf8a48de140a3c666897588a05shiqian 3383e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a monomorphic matcher that matches anything with type Lhs 3384e35fdd936d133bf8a48de140a3c666897588a05shiqian// and equal to rhs. A user may need to use this instead of Eq(...) 3385e35fdd936d133bf8a48de140a3c666897588a05shiqian// in order to resolve an overloading ambiguity. 3386e35fdd936d133bf8a48de140a3c666897588a05shiqian// 3387e35fdd936d133bf8a48de140a3c666897588a05shiqian// TypedEq<T>(x) is just a convenient short-hand for Matcher<T>(Eq(x)) 3388e35fdd936d133bf8a48de140a3c666897588a05shiqian// or Matcher<T>(x), but more readable than the latter. 3389e35fdd936d133bf8a48de140a3c666897588a05shiqian// 3390e35fdd936d133bf8a48de140a3c666897588a05shiqian// We could define similar monomorphic matchers for other comparison 3391e35fdd936d133bf8a48de140a3c666897588a05shiqian// operations (e.g. TypedLt, TypedGe, and etc), but decided not to do 3392e35fdd936d133bf8a48de140a3c666897588a05shiqian// it yet as those are used much less than Eq() in practice. A user 3393e35fdd936d133bf8a48de140a3c666897588a05shiqian// can always write Matcher<T>(Lt(5)) to be explicit about the type, 3394e35fdd936d133bf8a48de140a3c666897588a05shiqian// for example. 3395e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Lhs, typename Rhs> 3396e35fdd936d133bf8a48de140a3c666897588a05shiqianinline Matcher<Lhs> TypedEq(const Rhs& rhs) { return Eq(rhs); } 3397e35fdd936d133bf8a48de140a3c666897588a05shiqian 3398e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything >= x. 3399e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs> 3400e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::GeMatcher<Rhs> Ge(Rhs x) { 3401e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::GeMatcher<Rhs>(x); 3402e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3403e35fdd936d133bf8a48de140a3c666897588a05shiqian 3404e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything > x. 3405e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs> 3406e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::GtMatcher<Rhs> Gt(Rhs x) { 3407e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::GtMatcher<Rhs>(x); 3408e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3409e35fdd936d133bf8a48de140a3c666897588a05shiqian 3410e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything <= x. 3411e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs> 3412e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::LeMatcher<Rhs> Le(Rhs x) { 3413e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::LeMatcher<Rhs>(x); 3414e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3415e35fdd936d133bf8a48de140a3c666897588a05shiqian 3416e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything < x. 3417e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs> 3418e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::LtMatcher<Rhs> Lt(Rhs x) { 3419e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::LtMatcher<Rhs>(x); 3420e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3421e35fdd936d133bf8a48de140a3c666897588a05shiqian 3422e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches anything != x. 3423e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Rhs> 3424e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::NeMatcher<Rhs> Ne(Rhs x) { 3425e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::NeMatcher<Rhs>(x); 3426e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3427e35fdd936d133bf8a48de140a3c666897588a05shiqian 34282d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan// Creates a polymorphic matcher that matches any NULL pointer. 34292d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.waninline PolymorphicMatcher<internal::IsNullMatcher > IsNull() { 34302d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan return MakePolymorphicMatcher(internal::IsNullMatcher()); 34312d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan} 34322d970ee3bad530703ff24bc3a011390b45cdd293zhanyong.wan 3433e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches any non-NULL pointer. 3434e35fdd936d133bf8a48de140a3c666897588a05shiqian// This is convenient as Not(NULL) doesn't compile (the compiler 3435e35fdd936d133bf8a48de140a3c666897588a05shiqian// thinks that that expression is comparing a pointer with an integer). 3436e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::NotNullMatcher > NotNull() { 3437e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::NotNullMatcher()); 3438e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3439e35fdd936d133bf8a48de140a3c666897588a05shiqian 3440e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches any argument that 3441e35fdd936d133bf8a48de140a3c666897588a05shiqian// references variable x. 3442e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename T> 3443e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::RefMatcher<T&> Ref(T& x) { // NOLINT 3444e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::RefMatcher<T&>(x); 3445e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3446e35fdd936d133bf8a48de140a3c666897588a05shiqian 3447e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any double argument approximately 3448e35fdd936d133bf8a48de140a3c666897588a05shiqian// equal to rhs, where two NANs are considered unequal. 3449e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::FloatingEqMatcher<double> DoubleEq(double rhs) { 3450e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::FloatingEqMatcher<double>(rhs, false); 3451e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3452e35fdd936d133bf8a48de140a3c666897588a05shiqian 3453e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any double argument approximately 3454e35fdd936d133bf8a48de140a3c666897588a05shiqian// equal to rhs, including NaN values when rhs is NaN. 3455e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::FloatingEqMatcher<double> NanSensitiveDoubleEq(double rhs) { 3456e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::FloatingEqMatcher<double>(rhs, true); 3457e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3458e35fdd936d133bf8a48de140a3c666897588a05shiqian 3459616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// Creates a matcher that matches any double argument approximately equal to 3460616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// rhs, up to the specified max absolute error bound, where two NANs are 3461616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// considered unequal. The max absolute error bound must be non-negative. 3462616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.waninline internal::FloatingEqMatcher<double> DoubleNear( 3463616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan double rhs, double max_abs_error) { 3464616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan return internal::FloatingEqMatcher<double>(rhs, false, max_abs_error); 3465616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan} 3466616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan 3467616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// Creates a matcher that matches any double argument approximately equal to 3468616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// rhs, up to the specified max absolute error bound, including NaN values when 3469616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// rhs is NaN. The max absolute error bound must be non-negative. 3470616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.waninline internal::FloatingEqMatcher<double> NanSensitiveDoubleNear( 3471616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan double rhs, double max_abs_error) { 3472616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan return internal::FloatingEqMatcher<double>(rhs, true, max_abs_error); 3473616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan} 3474616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan 3475e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any float argument approximately 3476e35fdd936d133bf8a48de140a3c666897588a05shiqian// equal to rhs, where two NANs are considered unequal. 3477e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::FloatingEqMatcher<float> FloatEq(float rhs) { 3478e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::FloatingEqMatcher<float>(rhs, false); 3479e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3480e35fdd936d133bf8a48de140a3c666897588a05shiqian 3481616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// Creates a matcher that matches any float argument approximately 3482e35fdd936d133bf8a48de140a3c666897588a05shiqian// equal to rhs, including NaN values when rhs is NaN. 3483e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::FloatingEqMatcher<float> NanSensitiveFloatEq(float rhs) { 3484e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::FloatingEqMatcher<float>(rhs, true); 3485e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3486e35fdd936d133bf8a48de140a3c666897588a05shiqian 3487616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// Creates a matcher that matches any float argument approximately equal to 3488616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// rhs, up to the specified max absolute error bound, where two NANs are 3489616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// considered unequal. The max absolute error bound must be non-negative. 3490616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.waninline internal::FloatingEqMatcher<float> FloatNear( 3491616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan float rhs, float max_abs_error) { 3492616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan return internal::FloatingEqMatcher<float>(rhs, false, max_abs_error); 3493616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan} 3494616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan 3495616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// Creates a matcher that matches any float argument approximately equal to 3496616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// rhs, up to the specified max absolute error bound, including NaN values when 3497616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// rhs is NaN. The max absolute error bound must be non-negative. 3498616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.waninline internal::FloatingEqMatcher<float> NanSensitiveFloatNear( 3499616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan float rhs, float max_abs_error) { 3500616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan return internal::FloatingEqMatcher<float>(rhs, true, max_abs_error); 3501616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan} 3502616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan 3503e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches a pointer (raw or smart) that points 3504e35fdd936d133bf8a48de140a3c666897588a05shiqian// to a value that matches inner_matcher. 3505e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename InnerMatcher> 3506e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::PointeeMatcher<InnerMatcher> Pointee( 3507e35fdd936d133bf8a48de140a3c666897588a05shiqian const InnerMatcher& inner_matcher) { 3508e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::PointeeMatcher<InnerMatcher>(inner_matcher); 3509e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3510e35fdd936d133bf8a48de140a3c666897588a05shiqian 3511e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches an object whose given field matches 3512e35fdd936d133bf8a48de140a3c666897588a05shiqian// 'matcher'. For example, 3513e35fdd936d133bf8a48de140a3c666897588a05shiqian// Field(&Foo::number, Ge(5)) 3514e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a Foo object x iff x.number >= 5. 3515e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Class, typename FieldType, typename FieldMatcher> 3516e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher< 3517e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::FieldMatcher<Class, FieldType> > Field( 3518e35fdd936d133bf8a48de140a3c666897588a05shiqian FieldType Class::*field, const FieldMatcher& matcher) { 3519e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher( 3520e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::FieldMatcher<Class, FieldType>( 3521e35fdd936d133bf8a48de140a3c666897588a05shiqian field, MatcherCast<const FieldType&>(matcher))); 3522e35fdd936d133bf8a48de140a3c666897588a05shiqian // The call to MatcherCast() is required for supporting inner 3523e35fdd936d133bf8a48de140a3c666897588a05shiqian // matchers of compatible types. For example, it allows 3524e35fdd936d133bf8a48de140a3c666897588a05shiqian // Field(&Foo::bar, m) 3525e35fdd936d133bf8a48de140a3c666897588a05shiqian // to compile where bar is an int32 and m is a matcher for int64. 3526e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3527e35fdd936d133bf8a48de140a3c666897588a05shiqian 3528e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches an object whose given property 3529e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches 'matcher'. For example, 3530e35fdd936d133bf8a48de140a3c666897588a05shiqian// Property(&Foo::str, StartsWith("hi")) 3531e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a Foo object x iff x.str() starts with "hi". 3532e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Class, typename PropertyType, typename PropertyMatcher> 3533e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher< 3534e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::PropertyMatcher<Class, PropertyType> > Property( 3535e35fdd936d133bf8a48de140a3c666897588a05shiqian PropertyType (Class::*property)() const, const PropertyMatcher& matcher) { 3536e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher( 3537e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::PropertyMatcher<Class, PropertyType>( 3538e35fdd936d133bf8a48de140a3c666897588a05shiqian property, 353902f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan MatcherCast<GTEST_REFERENCE_TO_CONST_(PropertyType)>(matcher))); 3540e35fdd936d133bf8a48de140a3c666897588a05shiqian // The call to MatcherCast() is required for supporting inner 3541e35fdd936d133bf8a48de140a3c666897588a05shiqian // matchers of compatible types. For example, it allows 3542e35fdd936d133bf8a48de140a3c666897588a05shiqian // Property(&Foo::bar, m) 3543e35fdd936d133bf8a48de140a3c666897588a05shiqian // to compile where bar() returns an int32 and m is a matcher for int64. 3544e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3545e35fdd936d133bf8a48de140a3c666897588a05shiqian 3546e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches an object iff the result of applying 3547e35fdd936d133bf8a48de140a3c666897588a05shiqian// a callable to x matches 'matcher'. 3548e35fdd936d133bf8a48de140a3c666897588a05shiqian// For example, 3549e35fdd936d133bf8a48de140a3c666897588a05shiqian// ResultOf(f, StartsWith("hi")) 3550e35fdd936d133bf8a48de140a3c666897588a05shiqian// matches a Foo object x iff f(x) starts with "hi". 3551e35fdd936d133bf8a48de140a3c666897588a05shiqian// callable parameter can be a function, function pointer, or a functor. 3552e35fdd936d133bf8a48de140a3c666897588a05shiqian// Callable has to satisfy the following conditions: 3553e35fdd936d133bf8a48de140a3c666897588a05shiqian// * It is required to keep no state affecting the results of 3554e35fdd936d133bf8a48de140a3c666897588a05shiqian// the calls on it and make no assumptions about how many calls 3555e35fdd936d133bf8a48de140a3c666897588a05shiqian// will be made. Any state it keeps must be protected from the 3556e35fdd936d133bf8a48de140a3c666897588a05shiqian// concurrent access. 3557e35fdd936d133bf8a48de140a3c666897588a05shiqian// * If it is a function object, it has to define type result_type. 3558e35fdd936d133bf8a48de140a3c666897588a05shiqian// We recommend deriving your functor classes from std::unary_function. 3559e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Callable, typename ResultOfMatcher> 3560e35fdd936d133bf8a48de140a3c666897588a05shiqianinternal::ResultOfMatcher<Callable> ResultOf( 3561e35fdd936d133bf8a48de140a3c666897588a05shiqian Callable callable, const ResultOfMatcher& matcher) { 3562e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::ResultOfMatcher<Callable>( 3563e35fdd936d133bf8a48de140a3c666897588a05shiqian callable, 3564e35fdd936d133bf8a48de140a3c666897588a05shiqian MatcherCast<typename internal::CallableTraits<Callable>::ResultType>( 3565e35fdd936d133bf8a48de140a3c666897588a05shiqian matcher)); 3566e35fdd936d133bf8a48de140a3c666897588a05shiqian // The call to MatcherCast() is required for supporting inner 3567e35fdd936d133bf8a48de140a3c666897588a05shiqian // matchers of compatible types. For example, it allows 3568e35fdd936d133bf8a48de140a3c666897588a05shiqian // ResultOf(Function, m) 3569e35fdd936d133bf8a48de140a3c666897588a05shiqian // to compile where Function() returns an int32 and m is a matcher for int64. 3570e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3571e35fdd936d133bf8a48de140a3c666897588a05shiqian 3572e35fdd936d133bf8a48de140a3c666897588a05shiqian// String matchers. 3573e35fdd936d133bf8a48de140a3c666897588a05shiqian 3574e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string equal to str. 3575e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> > 3576e35fdd936d133bf8a48de140a3c666897588a05shiqian StrEq(const internal::string& str) { 3577e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>( 3578e35fdd936d133bf8a48de140a3c666897588a05shiqian str, true, true)); 3579e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3580e35fdd936d133bf8a48de140a3c666897588a05shiqian 3581e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string not equal to str. 3582e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> > 3583e35fdd936d133bf8a48de140a3c666897588a05shiqian StrNe(const internal::string& str) { 3584e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>( 3585e35fdd936d133bf8a48de140a3c666897588a05shiqian str, false, true)); 3586e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3587e35fdd936d133bf8a48de140a3c666897588a05shiqian 3588e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string equal to str, ignoring case. 3589e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> > 3590e35fdd936d133bf8a48de140a3c666897588a05shiqian StrCaseEq(const internal::string& str) { 3591e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>( 3592e35fdd936d133bf8a48de140a3c666897588a05shiqian str, true, false)); 3593e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3594e35fdd936d133bf8a48de140a3c666897588a05shiqian 3595e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string not equal to str, ignoring case. 3596e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::string> > 3597e35fdd936d133bf8a48de140a3c666897588a05shiqian StrCaseNe(const internal::string& str) { 3598e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::string>( 3599e35fdd936d133bf8a48de140a3c666897588a05shiqian str, false, false)); 3600e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3601e35fdd936d133bf8a48de140a3c666897588a05shiqian 3602e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any string, std::string, or C string 3603e35fdd936d133bf8a48de140a3c666897588a05shiqian// that contains the given substring. 3604e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::HasSubstrMatcher<internal::string> > 3605e35fdd936d133bf8a48de140a3c666897588a05shiqian HasSubstr(const internal::string& substring) { 3606e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::HasSubstrMatcher<internal::string>( 3607e35fdd936d133bf8a48de140a3c666897588a05shiqian substring)); 3608e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3609e35fdd936d133bf8a48de140a3c666897588a05shiqian 3610e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that starts with 'prefix' (case-sensitive). 3611e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StartsWithMatcher<internal::string> > 3612e35fdd936d133bf8a48de140a3c666897588a05shiqian StartsWith(const internal::string& prefix) { 3613e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::StartsWithMatcher<internal::string>( 3614e35fdd936d133bf8a48de140a3c666897588a05shiqian prefix)); 3615e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3616e35fdd936d133bf8a48de140a3c666897588a05shiqian 3617e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that ends with 'suffix' (case-sensitive). 3618e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::EndsWithMatcher<internal::string> > 3619e35fdd936d133bf8a48de140a3c666897588a05shiqian EndsWith(const internal::string& suffix) { 3620e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::EndsWithMatcher<internal::string>( 3621e35fdd936d133bf8a48de140a3c666897588a05shiqian suffix)); 3622e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3623e35fdd936d133bf8a48de140a3c666897588a05shiqian 3624e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that fully matches regular expression 'regex'. 3625e35fdd936d133bf8a48de140a3c666897588a05shiqian// The matcher takes ownership of 'regex'. 3626e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex( 3627e35fdd936d133bf8a48de140a3c666897588a05shiqian const internal::RE* regex) { 3628e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, true)); 3629e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3630e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex( 3631e35fdd936d133bf8a48de140a3c666897588a05shiqian const internal::string& regex) { 3632e35fdd936d133bf8a48de140a3c666897588a05shiqian return MatchesRegex(new internal::RE(regex)); 3633e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3634e35fdd936d133bf8a48de140a3c666897588a05shiqian 3635e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that contains regular expression 'regex'. 3636e35fdd936d133bf8a48de140a3c666897588a05shiqian// The matcher takes ownership of 'regex'. 3637e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex( 3638e35fdd936d133bf8a48de140a3c666897588a05shiqian const internal::RE* regex) { 3639e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::MatchesRegexMatcher(regex, false)); 3640e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3641e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex( 3642e35fdd936d133bf8a48de140a3c666897588a05shiqian const internal::string& regex) { 3643e35fdd936d133bf8a48de140a3c666897588a05shiqian return ContainsRegex(new internal::RE(regex)); 3644e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3645e35fdd936d133bf8a48de140a3c666897588a05shiqian 3646e35fdd936d133bf8a48de140a3c666897588a05shiqian#if GTEST_HAS_GLOBAL_WSTRING || GTEST_HAS_STD_WSTRING 3647e35fdd936d133bf8a48de140a3c666897588a05shiqian// Wide string matchers. 3648e35fdd936d133bf8a48de140a3c666897588a05shiqian 3649e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string equal to str. 3650e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> > 3651e35fdd936d133bf8a48de140a3c666897588a05shiqian StrEq(const internal::wstring& str) { 3652e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>( 3653e35fdd936d133bf8a48de140a3c666897588a05shiqian str, true, true)); 3654e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3655e35fdd936d133bf8a48de140a3c666897588a05shiqian 3656e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string not equal to str. 3657e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> > 3658e35fdd936d133bf8a48de140a3c666897588a05shiqian StrNe(const internal::wstring& str) { 3659e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>( 3660e35fdd936d133bf8a48de140a3c666897588a05shiqian str, false, true)); 3661e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3662e35fdd936d133bf8a48de140a3c666897588a05shiqian 3663e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string equal to str, ignoring case. 3664e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> > 3665e35fdd936d133bf8a48de140a3c666897588a05shiqian StrCaseEq(const internal::wstring& str) { 3666e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>( 3667e35fdd936d133bf8a48de140a3c666897588a05shiqian str, true, false)); 3668e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3669e35fdd936d133bf8a48de140a3c666897588a05shiqian 3670e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string not equal to str, ignoring case. 3671e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StrEqualityMatcher<internal::wstring> > 3672e35fdd936d133bf8a48de140a3c666897588a05shiqian StrCaseNe(const internal::wstring& str) { 3673e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::StrEqualityMatcher<internal::wstring>( 3674e35fdd936d133bf8a48de140a3c666897588a05shiqian str, false, false)); 3675e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3676e35fdd936d133bf8a48de140a3c666897588a05shiqian 3677e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any wstring, std::wstring, or C wide string 3678e35fdd936d133bf8a48de140a3c666897588a05shiqian// that contains the given substring. 3679e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::HasSubstrMatcher<internal::wstring> > 3680e35fdd936d133bf8a48de140a3c666897588a05shiqian HasSubstr(const internal::wstring& substring) { 3681e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::HasSubstrMatcher<internal::wstring>( 3682e35fdd936d133bf8a48de140a3c666897588a05shiqian substring)); 3683e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3684e35fdd936d133bf8a48de140a3c666897588a05shiqian 3685e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that starts with 'prefix' (case-sensitive). 3686e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::StartsWithMatcher<internal::wstring> > 3687e35fdd936d133bf8a48de140a3c666897588a05shiqian StartsWith(const internal::wstring& prefix) { 3688e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::StartsWithMatcher<internal::wstring>( 3689e35fdd936d133bf8a48de140a3c666897588a05shiqian prefix)); 3690e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3691e35fdd936d133bf8a48de140a3c666897588a05shiqian 3692e35fdd936d133bf8a48de140a3c666897588a05shiqian// Matches a string that ends with 'suffix' (case-sensitive). 3693e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::EndsWithMatcher<internal::wstring> > 3694e35fdd936d133bf8a48de140a3c666897588a05shiqian EndsWith(const internal::wstring& suffix) { 3695e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::EndsWithMatcher<internal::wstring>( 3696e35fdd936d133bf8a48de140a3c666897588a05shiqian suffix)); 3697e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3698e35fdd936d133bf8a48de140a3c666897588a05shiqian 3699e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif // GTEST_HAS_GLOBAL_WSTRING || GTEST_HAS_STD_WSTRING 3700e35fdd936d133bf8a48de140a3c666897588a05shiqian 3701e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the 3702e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field == the second field. 3703e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Eq2Matcher Eq() { return internal::Eq2Matcher(); } 3704e35fdd936d133bf8a48de140a3c666897588a05shiqian 3705e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the 3706e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field >= the second field. 3707e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Ge2Matcher Ge() { return internal::Ge2Matcher(); } 3708e35fdd936d133bf8a48de140a3c666897588a05shiqian 3709e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the 3710e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field > the second field. 3711e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Gt2Matcher Gt() { return internal::Gt2Matcher(); } 3712e35fdd936d133bf8a48de140a3c666897588a05shiqian 3713e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the 3714e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field <= the second field. 3715e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Le2Matcher Le() { return internal::Le2Matcher(); } 3716e35fdd936d133bf8a48de140a3c666897588a05shiqian 3717e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the 3718e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field < the second field. 3719e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Lt2Matcher Lt() { return internal::Lt2Matcher(); } 3720e35fdd936d133bf8a48de140a3c666897588a05shiqian 3721e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a polymorphic matcher that matches a 2-tuple where the 3722e35fdd936d133bf8a48de140a3c666897588a05shiqian// first field != the second field. 3723e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::Ne2Matcher Ne() { return internal::Ne2Matcher(); } 3724e35fdd936d133bf8a48de140a3c666897588a05shiqian 3725e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates a matcher that matches any value of type T that m doesn't 3726e35fdd936d133bf8a48de140a3c666897588a05shiqian// match. 3727e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename InnerMatcher> 3728e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::NotMatcher<InnerMatcher> Not(InnerMatcher m) { 3729e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::NotMatcher<InnerMatcher>(m); 3730e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3731e35fdd936d133bf8a48de140a3c666897588a05shiqian 3732e35fdd936d133bf8a48de140a3c666897588a05shiqian// Returns a matcher that matches anything that satisfies the given 3733e35fdd936d133bf8a48de140a3c666897588a05shiqian// predicate. The predicate can be any unary function or functor 3734e35fdd936d133bf8a48de140a3c666897588a05shiqian// whose return type can be implicitly converted to bool. 3735e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename Predicate> 3736e35fdd936d133bf8a48de140a3c666897588a05shiqianinline PolymorphicMatcher<internal::TrulyMatcher<Predicate> > 3737e35fdd936d133bf8a48de140a3c666897588a05shiqianTruly(Predicate pred) { 3738e35fdd936d133bf8a48de140a3c666897588a05shiqian return MakePolymorphicMatcher(internal::TrulyMatcher<Predicate>(pred)); 3739e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3740e35fdd936d133bf8a48de140a3c666897588a05shiqian 3741a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan// Returns a matcher that matches the container size. The container must 3742a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan// support both size() and size_type which all STL-like containers provide. 3743a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan// Note that the parameter 'size' can be a value of type size_type as well as 3744a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan// matcher. For instance: 3745a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan// EXPECT_THAT(container, SizeIs(2)); // Checks container has 2 elements. 3746a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan// EXPECT_THAT(container, SizeIs(Le(2)); // Checks container has at most 2. 3747a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wantemplate <typename SizeMatcher> 3748a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.waninline internal::SizeIsMatcher<SizeMatcher> 3749a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wanSizeIs(const SizeMatcher& size_matcher) { 3750a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan return internal::SizeIsMatcher<SizeMatcher>(size_matcher); 3751a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan} 3752a31d9ce2900275c5b9aff2459664a50381c7cbb0zhanyong.wan 37536a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// Returns a matcher that matches an equal container. 37546a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// This matcher behaves like Eq(), but in the event of mismatch lists the 37556a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// values that are included in one container but not the other. (Duplicate 37566a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan// values and order differences are not explained.) 37576a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wantemplate <typename Container> 3758821133180ccdb9eb062548d964929ba3b354eb84zhanyong.waninline PolymorphicMatcher<internal::ContainerEqMatcher< // NOLINT 375902f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan GTEST_REMOVE_CONST_(Container)> > 37606a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan ContainerEq(const Container& rhs) { 3761b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // This following line is for working around a bug in MSVC 8.0, 3762b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan // which causes Container to be a const type sometimes. 376302f7106557fde1f1075dc53d65ef1f7a11851f93zhanyong.wan typedef GTEST_REMOVE_CONST_(Container) RawContainer; 3764821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan return MakePolymorphicMatcher( 3765821133180ccdb9eb062548d964929ba3b354eb84zhanyong.wan internal::ContainerEqMatcher<RawContainer>(rhs)); 3766b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 3767b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 3768898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan// Returns a matcher that matches a container that, when sorted using 3769898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan// the given comparator, matches container_matcher. 3770898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wantemplate <typename Comparator, typename ContainerMatcher> 3771898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.waninline internal::WhenSortedByMatcher<Comparator, ContainerMatcher> 3772898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wanWhenSortedBy(const Comparator& comparator, 3773898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan const ContainerMatcher& container_matcher) { 3774898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan return internal::WhenSortedByMatcher<Comparator, ContainerMatcher>( 3775898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan comparator, container_matcher); 3776898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan} 3777898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan 3778898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan// Returns a matcher that matches a container that, when sorted using 3779898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan// the < operator, matches container_matcher. 3780898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wantemplate <typename ContainerMatcher> 3781898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.waninline internal::WhenSortedByMatcher<internal::LessComparator, ContainerMatcher> 3782898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wanWhenSorted(const ContainerMatcher& container_matcher) { 3783898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan return 3784898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan internal::WhenSortedByMatcher<internal::LessComparator, ContainerMatcher>( 3785898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan internal::LessComparator(), container_matcher); 3786898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan} 3787898725cf47aacd45c385bb6c537c130ac8cd224czhanyong.wan 3788ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// Matches an STL-style container or a native array that contains the 3789ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// same number of elements as in rhs, where its i-th element and rhs's 3790ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// i-th element (as a pair) satisfy the given pair matcher, for all i. 3791ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// TupleMatcher must be able to be safely cast to Matcher<tuple<const 3792ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// T1&, const T2&> >, where T1 and T2 are the types of elements in the 3793ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan// LHS container and the RHS container respectively. 3794ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wantemplate <typename TupleMatcher, typename Container> 3795ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.waninline internal::PointwiseMatcher<TupleMatcher, 3796ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan GTEST_REMOVE_CONST_(Container)> 3797ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wanPointwise(const TupleMatcher& tuple_matcher, const Container& rhs) { 3798ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan // This following line is for working around a bug in MSVC 8.0, 3799ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan // which causes Container to be a const type sometimes. 3800ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan typedef GTEST_REMOVE_CONST_(Container) RawContainer; 3801ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan return internal::PointwiseMatcher<TupleMatcher, RawContainer>( 3802ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan tuple_matcher, rhs); 3803ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan} 3804ab5b77c179009b787d71ff934eaf4a0db6c24814zhanyong.wan 3805b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Matches an STL-style container or a native array that contains at 3806b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// least one element matching the given value or matcher. 3807b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// 3808b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Examples: 3809b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// ::std::set<int> page_ids; 3810b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// page_ids.insert(3); 3811b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// page_ids.insert(1); 3812b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// EXPECT_THAT(page_ids, Contains(1)); 3813b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// EXPECT_THAT(page_ids, Contains(Gt(2))); 3814b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// EXPECT_THAT(page_ids, Not(Contains(4))); 3815b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// 3816b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// ::std::map<int, size_t> page_lengths; 3817b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// page_lengths[1] = 100; 38184019819dbb5369b1d638503523a04aaf7eb2a5adzhanyong.wan// EXPECT_THAT(page_lengths, 38194019819dbb5369b1d638503523a04aaf7eb2a5adzhanyong.wan// Contains(::std::pair<const int, size_t>(1, 100))); 3820b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// 3821b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// const char* user_ids[] = { "joe", "mike", "tom" }; 3822b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// EXPECT_THAT(user_ids, Contains(Eq(::std::string("tom")))); 3823b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename M> 3824b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.waninline internal::ContainsMatcher<M> Contains(M matcher) { 3825b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan return internal::ContainsMatcher<M>(matcher); 38266a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan} 38276a896b5ec607a54d86bbd2efdbc0248754b042e1zhanyong.wan 382833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Matches an STL-style container or a native array that contains only 382933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// elements matching the given value or matcher. 383033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// 383133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Each(m) is semantically equivalent to Not(Contains(Not(m))). Only 383233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// the messages are different. 383333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// 383433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// Examples: 383533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// ::std::set<int> page_ids; 383633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// // Each(m) matches an empty container, regardless of what m is. 383733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// EXPECT_THAT(page_ids, Each(Eq(1))); 383833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// EXPECT_THAT(page_ids, Each(Eq(77))); 383933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// 384033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// page_ids.insert(3); 384133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// EXPECT_THAT(page_ids, Each(Gt(0))); 384233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// EXPECT_THAT(page_ids, Not(Each(Gt(4)))); 384333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// page_ids.insert(1); 384433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// EXPECT_THAT(page_ids, Not(Each(Lt(2)))); 384533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// 384633605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// ::std::map<int, size_t> page_lengths; 384733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// page_lengths[1] = 100; 384833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// page_lengths[2] = 200; 384933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// page_lengths[3] = 300; 385033605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// EXPECT_THAT(page_lengths, Not(Each(Pair(1, 100)))); 385133605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// EXPECT_THAT(page_lengths, Each(Key(Le(3)))); 385233605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// 385333605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// const char* user_ids[] = { "joe", "mike", "tom" }; 385433605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan// EXPECT_THAT(user_ids, Not(Each(Eq(::std::string("tom"))))); 385533605ba45417979cff7254c1196dfee65a7275b3zhanyong.wantemplate <typename M> 385633605ba45417979cff7254c1196dfee65a7275b3zhanyong.waninline internal::EachMatcher<M> Each(M matcher) { 385733605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan return internal::EachMatcher<M>(matcher); 385833605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan} 385933605ba45417979cff7254c1196dfee65a7275b3zhanyong.wan 3860b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// Key(inner_matcher) matches an std::pair whose 'first' field matches 3861b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// inner_matcher. For example, Contains(Key(Ge(5))) can be used to match an 3862b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan// std::map that contains at least one element whose key is >= 5. 3863b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wantemplate <typename M> 3864b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.waninline internal::KeyMatcher<M> Key(M inner_matcher) { 3865b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan return internal::KeyMatcher<M>(inner_matcher); 3866b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan} 3867b5937dab6969ca4b1d8304cc8939ce16c1fb62e5zhanyong.wan 3868f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// Pair(first_matcher, second_matcher) matches a std::pair whose 'first' field 3869f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// matches first_matcher and whose 'second' field matches second_matcher. For 3870f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// example, EXPECT_THAT(map_type, ElementsAre(Pair(Ge(5), "foo"))) can be used 3871f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// to match a std::map<int, string> that contains exactly one element whose key 3872f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan// is >= 5 and whose value equals "foo". 3873f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wantemplate <typename FirstMatcher, typename SecondMatcher> 3874f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.waninline internal::PairMatcher<FirstMatcher, SecondMatcher> 3875f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wanPair(FirstMatcher first_matcher, SecondMatcher second_matcher) { 3876f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan return internal::PairMatcher<FirstMatcher, SecondMatcher>( 3877f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan first_matcher, second_matcher); 3878f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan} 3879f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan 3880e35fdd936d133bf8a48de140a3c666897588a05shiqian// Returns a predicate that is satisfied by anything that matches the 3881e35fdd936d133bf8a48de140a3c666897588a05shiqian// given matcher. 3882e35fdd936d133bf8a48de140a3c666897588a05shiqiantemplate <typename M> 3883e35fdd936d133bf8a48de140a3c666897588a05shiqianinline internal::MatcherAsPredicate<M> Matches(M matcher) { 3884e35fdd936d133bf8a48de140a3c666897588a05shiqian return internal::MatcherAsPredicate<M>(matcher); 3885e35fdd936d133bf8a48de140a3c666897588a05shiqian} 3886e35fdd936d133bf8a48de140a3c666897588a05shiqian 3887b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan// Returns true iff the value matches the matcher. 3888b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wantemplate <typename T, typename M> 3889b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.waninline bool Value(const T& value, M matcher) { 3890b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan return testing::Matches(matcher)(value); 3891b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan} 3892b82431625d1842d1498f3c0e6f1923ce81837c6ezhanyong.wan 389334b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wan// Matches the value against the given matcher and explains the match 389434b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wan// result to listener. 389534b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wantemplate <typename T, typename M> 3896a862f1de30ed1ef259bcec61c5939200b29c765czhanyong.waninline bool ExplainMatchResult( 389734b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wan M matcher, const T& value, MatchResultListener* listener) { 389834b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wan return SafeMatcherCast<const T&>(matcher).MatchAndExplain(value, listener); 389934b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wan} 390034b034c21ef4af7c0100194ed6f85910fc99debbzhanyong.wan 3901616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan#if GTEST_LANG_CXX11 3902616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// Define variadic matcher versions. They are overloaded in 3903616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan// gmock-generated-matchers.h for the cases supported by pre C++11 compilers. 3904616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wantemplate <typename... Args> 3905616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.waninline internal::AllOfMatcher<Args...> AllOf(const Args&... matchers) { 3906616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan return internal::AllOfMatcher<Args...>(matchers...); 3907616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan} 3908616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan 3909616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wantemplate <typename... Args> 3910616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.waninline internal::AnyOfMatcher<Args...> AnyOf(const Args&... matchers) { 3911616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan return internal::AnyOfMatcher<Args...>(matchers...); 3912616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan} 3913616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan 3914616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan#endif // GTEST_LANG_CXX11 3915616180e6847dd3cc6517a59ff2d24e9fd5c8052czhanyong.wan 3916bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan// AllArgs(m) is a synonym of m. This is useful in 3917bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan// 3918bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan// EXPECT_CALL(foo, Bar(_, _)).With(AllArgs(Eq())); 3919bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan// 3920bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan// which is easier to read than 3921bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan// 3922bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan// EXPECT_CALL(foo, Bar(_, _)).With(Eq()); 3923bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wantemplate <typename InnerMatcher> 3924bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.waninline InnerMatcher AllArgs(const InnerMatcher& matcher) { return matcher; } 3925bf55085d456e3ee55eb234c98c435e54d0a2d5aazhanyong.wan 3926e35fdd936d133bf8a48de140a3c666897588a05shiqian// These macros allow using matchers to check values in Google Test 3927e35fdd936d133bf8a48de140a3c666897588a05shiqian// tests. ASSERT_THAT(value, matcher) and EXPECT_THAT(value, matcher) 3928e35fdd936d133bf8a48de140a3c666897588a05shiqian// succeed iff the value matches the matcher. If the assertion fails, 3929e35fdd936d133bf8a48de140a3c666897588a05shiqian// the value and the description of the matcher will be printed. 3930e35fdd936d133bf8a48de140a3c666897588a05shiqian#define ASSERT_THAT(value, matcher) ASSERT_PRED_FORMAT1(\ 3931e35fdd936d133bf8a48de140a3c666897588a05shiqian ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value) 3932e35fdd936d133bf8a48de140a3c666897588a05shiqian#define EXPECT_THAT(value, matcher) EXPECT_PRED_FORMAT1(\ 3933e35fdd936d133bf8a48de140a3c666897588a05shiqian ::testing::internal::MakePredicateFormatterFromMatcher(matcher), value) 3934e35fdd936d133bf8a48de140a3c666897588a05shiqian 3935e35fdd936d133bf8a48de140a3c666897588a05shiqian} // namespace testing 3936e35fdd936d133bf8a48de140a3c666897588a05shiqian 3937e35fdd936d133bf8a48de140a3c666897588a05shiqian#endif // GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_ 3938