Path: blob/main/contrib/googletest/googlemock/test/gmock-matchers-comparisons_test.cc
48255 views
// Copyright 2007, Google Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above10// copyright notice, this list of conditions and the following disclaimer11// in the documentation and/or other materials provided with the12// distribution.13// * Neither the name of Google Inc. nor the names of its14// contributors may be used to endorse or promote products derived from15// this software without specific prior written permission.16//17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2829// Google Mock - a framework for writing C++ mock classes.30//31// This file tests some commonly used argument matchers.3233#include <functional>34#include <memory>35#include <string>36#include <tuple>37#include <vector>3839#include "test/gmock-matchers_test.h"4041// Silence warning C4244: 'initializing': conversion from 'int' to 'short',42// possible loss of data and C4100, unreferenced local parameter43GTEST_DISABLE_MSC_WARNINGS_PUSH_(4244 4100)444546namespace testing {47namespace gmock_matchers_test {48namespace {4950INSTANTIATE_GTEST_MATCHER_TEST_P(MonotonicMatcherTest);5152TEST_P(MonotonicMatcherTestP, IsPrintable) {53stringstream ss;54ss << GreaterThan(5);55EXPECT_EQ("is > 5", ss.str());56}5758TEST(MatchResultListenerTest, StreamingWorks) {59StringMatchResultListener listener;60listener << "hi" << 5;61EXPECT_EQ("hi5", listener.str());6263listener.Clear();64EXPECT_EQ("", listener.str());6566listener << 42;67EXPECT_EQ("42", listener.str());6869// Streaming shouldn't crash when the underlying ostream is NULL.70DummyMatchResultListener dummy;71dummy << "hi" << 5;72}7374TEST(MatchResultListenerTest, CanAccessUnderlyingStream) {75EXPECT_TRUE(DummyMatchResultListener().stream() == nullptr);76EXPECT_TRUE(StreamMatchResultListener(nullptr).stream() == nullptr);7778EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream());79}8081TEST(MatchResultListenerTest, IsInterestedWorks) {82EXPECT_TRUE(StringMatchResultListener().IsInterested());83EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested());8485EXPECT_FALSE(DummyMatchResultListener().IsInterested());86EXPECT_FALSE(StreamMatchResultListener(nullptr).IsInterested());87}8889// Makes sure that the MatcherInterface<T> interface doesn't90// change.91class EvenMatcherImpl : public MatcherInterface<int> {92public:93bool MatchAndExplain(int x,94MatchResultListener* /* listener */) const override {95return x % 2 == 0;96}9798void DescribeTo(ostream* os) const override { *os << "is an even number"; }99100// We deliberately don't define DescribeNegationTo() and101// ExplainMatchResultTo() here, to make sure the definition of these102// two methods is optional.103};104105// Makes sure that the MatcherInterface API doesn't change.106TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) {107EvenMatcherImpl m;108}109110// Tests implementing a monomorphic matcher using MatchAndExplain().111112class NewEvenMatcherImpl : public MatcherInterface<int> {113public:114bool MatchAndExplain(int x, MatchResultListener* listener) const override {115const bool match = x % 2 == 0;116// Verifies that we can stream to a listener directly.117*listener << "value % " << 2;118if (listener->stream() != nullptr) {119// Verifies that we can stream to a listener's underlying stream120// too.121*listener->stream() << " == " << (x % 2);122}123return match;124}125126void DescribeTo(ostream* os) const override { *os << "is an even number"; }127};128129TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {130Matcher<int> m = MakeMatcher(new NewEvenMatcherImpl);131EXPECT_TRUE(m.Matches(2));132EXPECT_FALSE(m.Matches(3));133EXPECT_EQ("value % 2 == 0", Explain(m, 2));134EXPECT_EQ("value % 2 == 1", Explain(m, 3));135}136137INSTANTIATE_GTEST_MATCHER_TEST_P(MatcherTest);138139// Tests default-constructing a matcher.140TEST(MatcherTest, CanBeDefaultConstructed) { Matcher<double> m; }141142// Tests that Matcher<T> can be constructed from a MatcherInterface<T>*.143TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {144const MatcherInterface<int>* impl = new EvenMatcherImpl;145Matcher<int> m(impl);146EXPECT_TRUE(m.Matches(4));147EXPECT_FALSE(m.Matches(5));148}149150// Tests that value can be used in place of Eq(value).151TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) {152Matcher<int> m1 = 5;153EXPECT_TRUE(m1.Matches(5));154EXPECT_FALSE(m1.Matches(6));155}156157// Tests that NULL can be used in place of Eq(NULL).158TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) {159Matcher<int*> m1 = nullptr;160EXPECT_TRUE(m1.Matches(nullptr));161int n = 0;162EXPECT_FALSE(m1.Matches(&n));163}164165// Tests that matchers can be constructed from a variable that is not properly166// defined. This should be illegal, but many users rely on this accidentally.167struct Undefined {168virtual ~Undefined() = 0;169static const int kInt = 1;170};171172TEST(MatcherTest, CanBeConstructedFromUndefinedVariable) {173Matcher<int> m1 = Undefined::kInt;174EXPECT_TRUE(m1.Matches(1));175EXPECT_FALSE(m1.Matches(2));176}177178// Test that a matcher parameterized with an abstract class compiles.179TEST(MatcherTest, CanAcceptAbstractClass) { Matcher<const Undefined&> m = _; }180181// Tests that matchers are copyable.182TEST(MatcherTest, IsCopyable) {183// Tests the copy constructor.184Matcher<bool> m1 = Eq(false);185EXPECT_TRUE(m1.Matches(false));186EXPECT_FALSE(m1.Matches(true));187188// Tests the assignment operator.189m1 = Eq(true);190EXPECT_TRUE(m1.Matches(true));191EXPECT_FALSE(m1.Matches(false));192}193194// Tests that Matcher<T>::DescribeTo() calls195// MatcherInterface<T>::DescribeTo().196TEST(MatcherTest, CanDescribeItself) {197EXPECT_EQ("is an even number", Describe(Matcher<int>(new EvenMatcherImpl)));198}199200// Tests Matcher<T>::MatchAndExplain().201TEST_P(MatcherTestP, MatchAndExplain) {202Matcher<int> m = GreaterThan(0);203StringMatchResultListener listener1;204EXPECT_TRUE(m.MatchAndExplain(42, &listener1));205EXPECT_EQ("which is 42 more than 0", listener1.str());206207StringMatchResultListener listener2;208EXPECT_FALSE(m.MatchAndExplain(-9, &listener2));209EXPECT_EQ("which is 9 less than 0", listener2.str());210}211212// Tests that a C-string literal can be implicitly converted to a213// Matcher<std::string> or Matcher<const std::string&>.214TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {215Matcher<std::string> m1 = "hi";216EXPECT_TRUE(m1.Matches("hi"));217EXPECT_FALSE(m1.Matches("hello"));218219Matcher<const std::string&> m2 = "hi";220EXPECT_TRUE(m2.Matches("hi"));221EXPECT_FALSE(m2.Matches("hello"));222}223224// Tests that a string object can be implicitly converted to a225// Matcher<std::string> or Matcher<const std::string&>.226TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {227Matcher<std::string> m1 = std::string("hi");228EXPECT_TRUE(m1.Matches("hi"));229EXPECT_FALSE(m1.Matches("hello"));230231Matcher<const std::string&> m2 = std::string("hi");232EXPECT_TRUE(m2.Matches("hi"));233EXPECT_FALSE(m2.Matches("hello"));234}235236#if GTEST_INTERNAL_HAS_STRING_VIEW237// Tests that a C-string literal can be implicitly converted to a238// Matcher<StringView> or Matcher<const StringView&>.239TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {240Matcher<internal::StringView> m1 = "cats";241EXPECT_TRUE(m1.Matches("cats"));242EXPECT_FALSE(m1.Matches("dogs"));243244Matcher<const internal::StringView&> m2 = "cats";245EXPECT_TRUE(m2.Matches("cats"));246EXPECT_FALSE(m2.Matches("dogs"));247}248249// Tests that a std::string object can be implicitly converted to a250// Matcher<StringView> or Matcher<const StringView&>.251TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromString) {252Matcher<internal::StringView> m1 = std::string("cats");253EXPECT_TRUE(m1.Matches("cats"));254EXPECT_FALSE(m1.Matches("dogs"));255256Matcher<const internal::StringView&> m2 = std::string("cats");257EXPECT_TRUE(m2.Matches("cats"));258EXPECT_FALSE(m2.Matches("dogs"));259}260261// Tests that a StringView object can be implicitly converted to a262// Matcher<StringView> or Matcher<const StringView&>.263TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromStringView) {264Matcher<internal::StringView> m1 = internal::StringView("cats");265EXPECT_TRUE(m1.Matches("cats"));266EXPECT_FALSE(m1.Matches("dogs"));267268Matcher<const internal::StringView&> m2 = internal::StringView("cats");269EXPECT_TRUE(m2.Matches("cats"));270EXPECT_FALSE(m2.Matches("dogs"));271}272#endif // GTEST_INTERNAL_HAS_STRING_VIEW273274// Tests that a std::reference_wrapper<std::string> object can be implicitly275// converted to a Matcher<std::string> or Matcher<const std::string&> via Eq().276TEST(StringMatcherTest,277CanBeImplicitlyConstructedFromEqReferenceWrapperString) {278std::string value = "cats";279Matcher<std::string> m1 = Eq(std::ref(value));280EXPECT_TRUE(m1.Matches("cats"));281EXPECT_FALSE(m1.Matches("dogs"));282283Matcher<const std::string&> m2 = Eq(std::ref(value));284EXPECT_TRUE(m2.Matches("cats"));285EXPECT_FALSE(m2.Matches("dogs"));286}287288// Tests that MakeMatcher() constructs a Matcher<T> from a289// MatcherInterface* without requiring the user to explicitly290// write the type.291TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {292const MatcherInterface<int>* dummy_impl = new EvenMatcherImpl;293Matcher<int> m = MakeMatcher(dummy_impl);294}295296// Tests that MakePolymorphicMatcher() can construct a polymorphic297// matcher from its implementation using the old API.298const int g_bar = 1;299class ReferencesBarOrIsZeroImpl {300public:301template <typename T>302bool MatchAndExplain(const T& x, MatchResultListener* /* listener */) const {303const void* p = &x;304return p == &g_bar || x == 0;305}306307void DescribeTo(ostream* os) const { *os << "g_bar or zero"; }308309void DescribeNegationTo(ostream* os) const {310*os << "doesn't reference g_bar and is not zero";311}312};313314// This function verifies that MakePolymorphicMatcher() returns a315// PolymorphicMatcher<T> where T is the argument's type.316PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {317return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl());318}319320TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {321// Using a polymorphic matcher to match a reference type.322Matcher<const int&> m1 = ReferencesBarOrIsZero();323EXPECT_TRUE(m1.Matches(0));324// Verifies that the identity of a by-reference argument is preserved.325EXPECT_TRUE(m1.Matches(g_bar));326EXPECT_FALSE(m1.Matches(1));327EXPECT_EQ("g_bar or zero", Describe(m1));328329// Using a polymorphic matcher to match a value type.330Matcher<double> m2 = ReferencesBarOrIsZero();331EXPECT_TRUE(m2.Matches(0.0));332EXPECT_FALSE(m2.Matches(0.1));333EXPECT_EQ("g_bar or zero", Describe(m2));334}335336// Tests implementing a polymorphic matcher using MatchAndExplain().337338class PolymorphicIsEvenImpl {339public:340void DescribeTo(ostream* os) const { *os << "is even"; }341342void DescribeNegationTo(ostream* os) const { *os << "is odd"; }343344template <typename T>345bool MatchAndExplain(const T& x, MatchResultListener* listener) const {346// Verifies that we can stream to the listener directly.347*listener << "% " << 2;348if (listener->stream() != nullptr) {349// Verifies that we can stream to the listener's underlying stream350// too.351*listener->stream() << " == " << (x % 2);352}353return (x % 2) == 0;354}355};356357PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {358return MakePolymorphicMatcher(PolymorphicIsEvenImpl());359}360361TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {362// Using PolymorphicIsEven() as a Matcher<int>.363const Matcher<int> m1 = PolymorphicIsEven();364EXPECT_TRUE(m1.Matches(42));365EXPECT_FALSE(m1.Matches(43));366EXPECT_EQ("is even", Describe(m1));367368const Matcher<int> not_m1 = Not(m1);369EXPECT_EQ("is odd", Describe(not_m1));370371EXPECT_EQ("% 2 == 0", Explain(m1, 42));372373// Using PolymorphicIsEven() as a Matcher<char>.374const Matcher<char> m2 = PolymorphicIsEven();375EXPECT_TRUE(m2.Matches('\x42'));376EXPECT_FALSE(m2.Matches('\x43'));377EXPECT_EQ("is even", Describe(m2));378379const Matcher<char> not_m2 = Not(m2);380EXPECT_EQ("is odd", Describe(not_m2));381382EXPECT_EQ("% 2 == 0", Explain(m2, '\x42'));383}384385INSTANTIATE_GTEST_MATCHER_TEST_P(MatcherCastTest);386387// Tests that MatcherCast<T>(m) works when m is a polymorphic matcher.388TEST_P(MatcherCastTestP, FromPolymorphicMatcher) {389Matcher<int16_t> m;390if (use_gtest_matcher_) {391m = MatcherCast<int16_t>(GtestGreaterThan(int64_t{5}));392} else {393m = MatcherCast<int16_t>(Gt(int64_t{5}));394}395EXPECT_TRUE(m.Matches(6));396EXPECT_FALSE(m.Matches(4));397}398399// For testing casting matchers between compatible types.400class IntValue {401public:402// An int can be statically (although not implicitly) cast to a403// IntValue.404explicit IntValue(int a_value) : value_(a_value) {}405406int value() const { return value_; }407408private:409int value_;410};411412// For testing casting matchers between compatible types.413bool IsPositiveIntValue(const IntValue& foo) { return foo.value() > 0; }414415// Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T416// can be statically converted to U.417TEST(MatcherCastTest, FromCompatibleType) {418Matcher<double> m1 = Eq(2.0);419Matcher<int> m2 = MatcherCast<int>(m1);420EXPECT_TRUE(m2.Matches(2));421EXPECT_FALSE(m2.Matches(3));422423Matcher<IntValue> m3 = Truly(IsPositiveIntValue);424Matcher<int> m4 = MatcherCast<int>(m3);425// In the following, the arguments 1 and 0 are statically converted426// to IntValue objects, and then tested by the IsPositiveIntValue()427// predicate.428EXPECT_TRUE(m4.Matches(1));429EXPECT_FALSE(m4.Matches(0));430}431432// Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>.433TEST(MatcherCastTest, FromConstReferenceToNonReference) {434Matcher<const int&> m1 = Eq(0);435Matcher<int> m2 = MatcherCast<int>(m1);436EXPECT_TRUE(m2.Matches(0));437EXPECT_FALSE(m2.Matches(1));438}439440// Tests that MatcherCast<T>(m) works when m is a Matcher<T&>.441TEST(MatcherCastTest, FromReferenceToNonReference) {442Matcher<int&> m1 = Eq(0);443Matcher<int> m2 = MatcherCast<int>(m1);444EXPECT_TRUE(m2.Matches(0));445EXPECT_FALSE(m2.Matches(1));446}447448// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.449TEST(MatcherCastTest, FromNonReferenceToConstReference) {450Matcher<int> m1 = Eq(0);451Matcher<const int&> m2 = MatcherCast<const int&>(m1);452EXPECT_TRUE(m2.Matches(0));453EXPECT_FALSE(m2.Matches(1));454}455456// Tests that MatcherCast<T&>(m) works when m is a Matcher<T>.457TEST(MatcherCastTest, FromNonReferenceToReference) {458Matcher<int> m1 = Eq(0);459Matcher<int&> m2 = MatcherCast<int&>(m1);460int n = 0;461EXPECT_TRUE(m2.Matches(n));462n = 1;463EXPECT_FALSE(m2.Matches(n));464}465466// Tests that MatcherCast<T>(m) works when m is a Matcher<T>.467TEST(MatcherCastTest, FromSameType) {468Matcher<int> m1 = Eq(0);469Matcher<int> m2 = MatcherCast<int>(m1);470EXPECT_TRUE(m2.Matches(0));471EXPECT_FALSE(m2.Matches(1));472}473474// Tests that MatcherCast<T>(m) works when m is a value of the same type as the475// value type of the Matcher.476TEST(MatcherCastTest, FromAValue) {477Matcher<int> m = MatcherCast<int>(42);478EXPECT_TRUE(m.Matches(42));479EXPECT_FALSE(m.Matches(239));480}481482// Tests that MatcherCast<T>(m) works when m is a value of the type implicitly483// convertible to the value type of the Matcher.484TEST(MatcherCastTest, FromAnImplicitlyConvertibleValue) {485const int kExpected = 'c';486Matcher<int> m = MatcherCast<int>('c');487EXPECT_TRUE(m.Matches(kExpected));488EXPECT_FALSE(m.Matches(kExpected + 1));489}490491struct NonImplicitlyConstructibleTypeWithOperatorEq {492friend bool operator==(493const NonImplicitlyConstructibleTypeWithOperatorEq& /* ignored */,494int rhs) {495return 42 == rhs;496}497friend bool operator==(498int lhs,499const NonImplicitlyConstructibleTypeWithOperatorEq& /* ignored */) {500return lhs == 42;501}502};503504// Tests that MatcherCast<T>(m) works when m is a neither a matcher nor505// implicitly convertible to the value type of the Matcher, but the value type506// of the matcher has operator==() overload accepting m.507TEST(MatcherCastTest, NonImplicitlyConstructibleTypeWithOperatorEq) {508Matcher<NonImplicitlyConstructibleTypeWithOperatorEq> m1 =509MatcherCast<NonImplicitlyConstructibleTypeWithOperatorEq>(42);510EXPECT_TRUE(m1.Matches(NonImplicitlyConstructibleTypeWithOperatorEq()));511512Matcher<NonImplicitlyConstructibleTypeWithOperatorEq> m2 =513MatcherCast<NonImplicitlyConstructibleTypeWithOperatorEq>(239);514EXPECT_FALSE(m2.Matches(NonImplicitlyConstructibleTypeWithOperatorEq()));515516// When updating the following lines please also change the comment to517// namespace convertible_from_any.518Matcher<int> m3 =519MatcherCast<int>(NonImplicitlyConstructibleTypeWithOperatorEq());520EXPECT_TRUE(m3.Matches(42));521EXPECT_FALSE(m3.Matches(239));522}523524// ConvertibleFromAny does not work with MSVC. resulting in525// error C2440: 'initializing': cannot convert from 'Eq' to 'M'526// No constructor could take the source type, or constructor overload527// resolution was ambiguous528529#if !defined _MSC_VER530531// The below ConvertibleFromAny struct is implicitly constructible from anything532// and when in the same namespace can interact with other tests. In particular,533// if it is in the same namespace as other tests and one removes534// NonImplicitlyConstructibleTypeWithOperatorEq::operator==(int lhs, ...);535// then the corresponding test still compiles (and it should not!) by implicitly536// converting NonImplicitlyConstructibleTypeWithOperatorEq to ConvertibleFromAny537// in m3.Matcher().538namespace convertible_from_any {539// Implicitly convertible from any type.540struct ConvertibleFromAny {541ConvertibleFromAny(int a_value) : value(a_value) {}542template <typename T>543ConvertibleFromAny(const T& /*a_value*/) : value(-1) {544ADD_FAILURE() << "Conversion constructor called";545}546int value;547};548549bool operator==(const ConvertibleFromAny& a, const ConvertibleFromAny& b) {550return a.value == b.value;551}552553ostream& operator<<(ostream& os, const ConvertibleFromAny& a) {554return os << a.value;555}556557TEST(MatcherCastTest, ConversionConstructorIsUsed) {558Matcher<ConvertibleFromAny> m = MatcherCast<ConvertibleFromAny>(1);559EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));560EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));561}562563TEST(MatcherCastTest, FromConvertibleFromAny) {564Matcher<ConvertibleFromAny> m =565MatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));566EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));567EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));568}569} // namespace convertible_from_any570571#endif // !defined _MSC_VER572573struct IntReferenceWrapper {574IntReferenceWrapper(const int& a_value) : value(&a_value) {}575const int* value;576};577578bool operator==(const IntReferenceWrapper& a, const IntReferenceWrapper& b) {579return a.value == b.value;580}581582TEST(MatcherCastTest, ValueIsNotCopied) {583int n = 42;584Matcher<IntReferenceWrapper> m = MatcherCast<IntReferenceWrapper>(n);585// Verify that the matcher holds a reference to n, not to its temporary copy.586EXPECT_TRUE(m.Matches(n));587}588589class Base {590public:591virtual ~Base() = default;592Base() = default;593594private:595Base(const Base&) = delete;596Base& operator=(const Base&) = delete;597};598599class Derived : public Base {600public:601Derived() : Base() {}602int i;603};604605class OtherDerived : public Base {};606607INSTANTIATE_GTEST_MATCHER_TEST_P(SafeMatcherCastTest);608609// Tests that SafeMatcherCast<T>(m) works when m is a polymorphic matcher.610TEST_P(SafeMatcherCastTestP, FromPolymorphicMatcher) {611Matcher<char> m2;612if (use_gtest_matcher_) {613m2 = SafeMatcherCast<char>(GtestGreaterThan(32));614} else {615m2 = SafeMatcherCast<char>(Gt(32));616}617EXPECT_TRUE(m2.Matches('A'));618EXPECT_FALSE(m2.Matches('\n'));619}620621// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where622// T and U are arithmetic types and T can be losslessly converted to623// U.624TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {625Matcher<double> m1 = DoubleEq(1.0);626Matcher<float> m2 = SafeMatcherCast<float>(m1);627EXPECT_TRUE(m2.Matches(1.0f));628EXPECT_FALSE(m2.Matches(2.0f));629630Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>('a'));631EXPECT_TRUE(m3.Matches('a'));632EXPECT_FALSE(m3.Matches('b'));633}634635// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where T and U636// are pointers or references to a derived and a base class, correspondingly.637TEST(SafeMatcherCastTest, FromBaseClass) {638Derived d, d2;639Matcher<Base*> m1 = Eq(&d);640Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);641EXPECT_TRUE(m2.Matches(&d));642EXPECT_FALSE(m2.Matches(&d2));643644Matcher<Base&> m3 = Ref(d);645Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);646EXPECT_TRUE(m4.Matches(d));647EXPECT_FALSE(m4.Matches(d2));648}649650// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>.651TEST(SafeMatcherCastTest, FromConstReferenceToReference) {652int n = 0;653Matcher<const int&> m1 = Ref(n);654Matcher<int&> m2 = SafeMatcherCast<int&>(m1);655int n1 = 0;656EXPECT_TRUE(m2.Matches(n));657EXPECT_FALSE(m2.Matches(n1));658}659660// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.661TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {662Matcher<std::unique_ptr<int>> m1 = IsNull();663Matcher<const std::unique_ptr<int>&> m2 =664SafeMatcherCast<const std::unique_ptr<int>&>(m1);665EXPECT_TRUE(m2.Matches(std::unique_ptr<int>()));666EXPECT_FALSE(m2.Matches(std::unique_ptr<int>(new int)));667}668669// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<T>.670TEST(SafeMatcherCastTest, FromNonReferenceToReference) {671Matcher<int> m1 = Eq(0);672Matcher<int&> m2 = SafeMatcherCast<int&>(m1);673int n = 0;674EXPECT_TRUE(m2.Matches(n));675n = 1;676EXPECT_FALSE(m2.Matches(n));677}678679// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<T>.680TEST(SafeMatcherCastTest, FromSameType) {681Matcher<int> m1 = Eq(0);682Matcher<int> m2 = SafeMatcherCast<int>(m1);683EXPECT_TRUE(m2.Matches(0));684EXPECT_FALSE(m2.Matches(1));685}686687#if !defined _MSC_VER688689namespace convertible_from_any {690TEST(SafeMatcherCastTest, ConversionConstructorIsUsed) {691Matcher<ConvertibleFromAny> m = SafeMatcherCast<ConvertibleFromAny>(1);692EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));693EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));694}695696TEST(SafeMatcherCastTest, FromConvertibleFromAny) {697Matcher<ConvertibleFromAny> m =698SafeMatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));699EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));700EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));701}702} // namespace convertible_from_any703704#endif // !defined _MSC_VER705706TEST(SafeMatcherCastTest, ValueIsNotCopied) {707int n = 42;708Matcher<IntReferenceWrapper> m = SafeMatcherCast<IntReferenceWrapper>(n);709// Verify that the matcher holds a reference to n, not to its temporary copy.710EXPECT_TRUE(m.Matches(n));711}712713TEST(ExpectThat, TakesLiterals) {714EXPECT_THAT(1, 1);715EXPECT_THAT(1.0, 1.0);716EXPECT_THAT(std::string(), "");717}718719TEST(ExpectThat, TakesFunctions) {720struct Helper {721static void Func() {}722};723void (*func)() = Helper::Func;724EXPECT_THAT(func, Helper::Func);725EXPECT_THAT(func, &Helper::Func);726}727728// Tests that A<T>() matches any value of type T.729TEST(ATest, MatchesAnyValue) {730// Tests a matcher for a value type.731Matcher<double> m1 = A<double>();732EXPECT_TRUE(m1.Matches(91.43));733EXPECT_TRUE(m1.Matches(-15.32));734735// Tests a matcher for a reference type.736int a = 2;737int b = -6;738Matcher<int&> m2 = A<int&>();739EXPECT_TRUE(m2.Matches(a));740EXPECT_TRUE(m2.Matches(b));741}742743TEST(ATest, WorksForDerivedClass) {744Base base;745Derived derived;746EXPECT_THAT(&base, A<Base*>());747// This shouldn't compile: EXPECT_THAT(&base, A<Derived*>());748EXPECT_THAT(&derived, A<Base*>());749EXPECT_THAT(&derived, A<Derived*>());750}751752// Tests that A<T>() describes itself properly.753TEST(ATest, CanDescribeSelf) { EXPECT_EQ("is anything", Describe(A<bool>())); }754755// Tests that An<T>() matches any value of type T.756TEST(AnTest, MatchesAnyValue) {757// Tests a matcher for a value type.758Matcher<int> m1 = An<int>();759EXPECT_TRUE(m1.Matches(9143));760EXPECT_TRUE(m1.Matches(-1532));761762// Tests a matcher for a reference type.763int a = 2;764int b = -6;765Matcher<int&> m2 = An<int&>();766EXPECT_TRUE(m2.Matches(a));767EXPECT_TRUE(m2.Matches(b));768}769770// Tests that An<T>() describes itself properly.771TEST(AnTest, CanDescribeSelf) { EXPECT_EQ("is anything", Describe(An<int>())); }772773// Tests that _ can be used as a matcher for any type and matches any774// value of that type.775TEST(UnderscoreTest, MatchesAnyValue) {776// Uses _ as a matcher for a value type.777Matcher<int> m1 = _;778EXPECT_TRUE(m1.Matches(123));779EXPECT_TRUE(m1.Matches(-242));780781// Uses _ as a matcher for a reference type.782bool a = false;783const bool b = true;784Matcher<const bool&> m2 = _;785EXPECT_TRUE(m2.Matches(a));786EXPECT_TRUE(m2.Matches(b));787}788789// Tests that _ describes itself properly.790TEST(UnderscoreTest, CanDescribeSelf) {791Matcher<int> m = _;792EXPECT_EQ("is anything", Describe(m));793}794795// Tests that Eq(x) matches any value equal to x.796TEST(EqTest, MatchesEqualValue) {797// 2 C-strings with same content but different addresses.798const char a1[] = "hi";799const char a2[] = "hi";800801Matcher<const char*> m1 = Eq(a1);802EXPECT_TRUE(m1.Matches(a1));803EXPECT_FALSE(m1.Matches(a2));804}805806// Tests that Eq(v) describes itself properly.807808class Unprintable {809public:810Unprintable() : c_('a') {}811812bool operator==(const Unprintable& /* rhs */) const { return true; }813// -Wunused-private-field: dummy accessor for `c_`.814char dummy_c() { return c_; }815816private:817char c_;818};819820TEST(EqTest, CanDescribeSelf) {821Matcher<Unprintable> m = Eq(Unprintable());822EXPECT_EQ("is equal to 1-byte object <61>", Describe(m));823}824825// Tests that Eq(v) can be used to match any type that supports826// comparing with type T, where T is v's type.827TEST(EqTest, IsPolymorphic) {828Matcher<int> m1 = Eq(1);829EXPECT_TRUE(m1.Matches(1));830EXPECT_FALSE(m1.Matches(2));831832Matcher<char> m2 = Eq(1);833EXPECT_TRUE(m2.Matches('\1'));834EXPECT_FALSE(m2.Matches('a'));835}836837// Tests that TypedEq<T>(v) matches values of type T that's equal to v.838TEST(TypedEqTest, ChecksEqualityForGivenType) {839Matcher<char> m1 = TypedEq<char>('a');840EXPECT_TRUE(m1.Matches('a'));841EXPECT_FALSE(m1.Matches('b'));842843Matcher<int> m2 = TypedEq<int>(6);844EXPECT_TRUE(m2.Matches(6));845EXPECT_FALSE(m2.Matches(7));846}847848// Tests that TypedEq(v) describes itself properly.849TEST(TypedEqTest, CanDescribeSelf) {850EXPECT_EQ("is equal to 2", Describe(TypedEq<int>(2)));851}852853// Tests that TypedEq<T>(v) has type Matcher<T>.854855// Type<T>::IsTypeOf(v) compiles if and only if the type of value v is T, where856// T is a "bare" type (i.e. not in the form of const U or U&). If v's type is857// not T, the compiler will generate a message about "undefined reference".858template <typename T>859struct Type {860static bool IsTypeOf(const T& /* v */) { return true; }861862template <typename T2>863static void IsTypeOf(T2 v);864};865866TEST(TypedEqTest, HasSpecifiedType) {867// Verifies that the type of TypedEq<T>(v) is Matcher<T>.868Type<Matcher<int>>::IsTypeOf(TypedEq<int>(5));869Type<Matcher<double>>::IsTypeOf(TypedEq<double>(5));870}871872// Tests that Ge(v) matches anything >= v.873TEST(GeTest, ImplementsGreaterThanOrEqual) {874Matcher<int> m1 = Ge(0);875EXPECT_TRUE(m1.Matches(1));876EXPECT_TRUE(m1.Matches(0));877EXPECT_FALSE(m1.Matches(-1));878}879880// Tests that Ge(v) describes itself properly.881TEST(GeTest, CanDescribeSelf) {882Matcher<int> m = Ge(5);883EXPECT_EQ("is >= 5", Describe(m));884}885886// Tests that Gt(v) matches anything > v.887TEST(GtTest, ImplementsGreaterThan) {888Matcher<double> m1 = Gt(0);889EXPECT_TRUE(m1.Matches(1.0));890EXPECT_FALSE(m1.Matches(0.0));891EXPECT_FALSE(m1.Matches(-1.0));892}893894// Tests that Gt(v) describes itself properly.895TEST(GtTest, CanDescribeSelf) {896Matcher<int> m = Gt(5);897EXPECT_EQ("is > 5", Describe(m));898}899900// Tests that Le(v) matches anything <= v.901TEST(LeTest, ImplementsLessThanOrEqual) {902Matcher<char> m1 = Le('b');903EXPECT_TRUE(m1.Matches('a'));904EXPECT_TRUE(m1.Matches('b'));905EXPECT_FALSE(m1.Matches('c'));906}907908// Tests that Le(v) describes itself properly.909TEST(LeTest, CanDescribeSelf) {910Matcher<int> m = Le(5);911EXPECT_EQ("is <= 5", Describe(m));912}913914// Tests that Lt(v) matches anything < v.915TEST(LtTest, ImplementsLessThan) {916Matcher<const std::string&> m1 = Lt("Hello");917EXPECT_TRUE(m1.Matches("Abc"));918EXPECT_FALSE(m1.Matches("Hello"));919EXPECT_FALSE(m1.Matches("Hello, world!"));920}921922// Tests that Lt(v) describes itself properly.923TEST(LtTest, CanDescribeSelf) {924Matcher<int> m = Lt(5);925EXPECT_EQ("is < 5", Describe(m));926}927928// Tests that Ne(v) matches anything != v.929TEST(NeTest, ImplementsNotEqual) {930Matcher<int> m1 = Ne(0);931EXPECT_TRUE(m1.Matches(1));932EXPECT_TRUE(m1.Matches(-1));933EXPECT_FALSE(m1.Matches(0));934}935936// Tests that Ne(v) describes itself properly.937TEST(NeTest, CanDescribeSelf) {938Matcher<int> m = Ne(5);939EXPECT_EQ("isn't equal to 5", Describe(m));940}941942class MoveOnly {943public:944explicit MoveOnly(int i) : i_(i) {}945MoveOnly(const MoveOnly&) = delete;946MoveOnly(MoveOnly&&) = default;947MoveOnly& operator=(const MoveOnly&) = delete;948MoveOnly& operator=(MoveOnly&&) = default;949950bool operator==(const MoveOnly& other) const { return i_ == other.i_; }951bool operator!=(const MoveOnly& other) const { return i_ != other.i_; }952bool operator<(const MoveOnly& other) const { return i_ < other.i_; }953bool operator<=(const MoveOnly& other) const { return i_ <= other.i_; }954bool operator>(const MoveOnly& other) const { return i_ > other.i_; }955bool operator>=(const MoveOnly& other) const { return i_ >= other.i_; }956957private:958int i_;959};960961struct MoveHelper {962MOCK_METHOD1(Call, void(MoveOnly));963};964965// Disable this test in VS 2015 (version 14), where it fails when SEH is enabled966#if defined(_MSC_VER) && (_MSC_VER < 1910)967TEST(ComparisonBaseTest, DISABLED_WorksWithMoveOnly) {968#else969TEST(ComparisonBaseTest, WorksWithMoveOnly) {970#endif971MoveOnly m{0};972MoveHelper helper;973974EXPECT_CALL(helper, Call(Eq(ByRef(m))));975helper.Call(MoveOnly(0));976EXPECT_CALL(helper, Call(Ne(ByRef(m))));977helper.Call(MoveOnly(1));978EXPECT_CALL(helper, Call(Le(ByRef(m))));979helper.Call(MoveOnly(0));980EXPECT_CALL(helper, Call(Lt(ByRef(m))));981helper.Call(MoveOnly(-1));982EXPECT_CALL(helper, Call(Ge(ByRef(m))));983helper.Call(MoveOnly(0));984EXPECT_CALL(helper, Call(Gt(ByRef(m))));985helper.Call(MoveOnly(1));986}987988TEST(IsEmptyTest, MatchesContainer) {989const Matcher<std::vector<int>> m = IsEmpty();990std::vector<int> a = {};991std::vector<int> b = {1};992EXPECT_TRUE(m.Matches(a));993EXPECT_FALSE(m.Matches(b));994}995996TEST(IsEmptyTest, MatchesStdString) {997const Matcher<std::string> m = IsEmpty();998std::string a = "z";999std::string b = "";1000EXPECT_FALSE(m.Matches(a));1001EXPECT_TRUE(m.Matches(b));1002}10031004TEST(IsEmptyTest, MatchesCString) {1005const Matcher<const char*> m = IsEmpty();1006const char a[] = "";1007const char b[] = "x";1008EXPECT_TRUE(m.Matches(a));1009EXPECT_FALSE(m.Matches(b));1010}10111012// Tests that IsNull() matches any NULL pointer of any type.1013TEST(IsNullTest, MatchesNullPointer) {1014Matcher<int*> m1 = IsNull();1015int* p1 = nullptr;1016int n = 0;1017EXPECT_TRUE(m1.Matches(p1));1018EXPECT_FALSE(m1.Matches(&n));10191020Matcher<const char*> m2 = IsNull();1021const char* p2 = nullptr;1022EXPECT_TRUE(m2.Matches(p2));1023EXPECT_FALSE(m2.Matches("hi"));10241025Matcher<void*> m3 = IsNull();1026void* p3 = nullptr;1027EXPECT_TRUE(m3.Matches(p3));1028EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef)));1029}10301031TEST(IsNullTest, StdFunction) {1032const Matcher<std::function<void()>> m = IsNull();10331034EXPECT_TRUE(m.Matches(std::function<void()>()));1035EXPECT_FALSE(m.Matches([] {}));1036}10371038// Tests that IsNull() describes itself properly.1039TEST(IsNullTest, CanDescribeSelf) {1040Matcher<int*> m = IsNull();1041EXPECT_EQ("is NULL", Describe(m));1042EXPECT_EQ("isn't NULL", DescribeNegation(m));1043}10441045// Tests that NotNull() matches any non-NULL pointer of any type.1046TEST(NotNullTest, MatchesNonNullPointer) {1047Matcher<int*> m1 = NotNull();1048int* p1 = nullptr;1049int n = 0;1050EXPECT_FALSE(m1.Matches(p1));1051EXPECT_TRUE(m1.Matches(&n));10521053Matcher<const char*> m2 = NotNull();1054const char* p2 = nullptr;1055EXPECT_FALSE(m2.Matches(p2));1056EXPECT_TRUE(m2.Matches("hi"));1057}10581059TEST(NotNullTest, LinkedPtr) {1060const Matcher<std::shared_ptr<int>> m = NotNull();1061const std::shared_ptr<int> null_p;1062const std::shared_ptr<int> non_null_p(new int);10631064EXPECT_FALSE(m.Matches(null_p));1065EXPECT_TRUE(m.Matches(non_null_p));1066}10671068TEST(NotNullTest, ReferenceToConstLinkedPtr) {1069const Matcher<const std::shared_ptr<double>&> m = NotNull();1070const std::shared_ptr<double> null_p;1071const std::shared_ptr<double> non_null_p(new double);10721073EXPECT_FALSE(m.Matches(null_p));1074EXPECT_TRUE(m.Matches(non_null_p));1075}10761077TEST(NotNullTest, StdFunction) {1078const Matcher<std::function<void()>> m = NotNull();10791080EXPECT_TRUE(m.Matches([] {}));1081EXPECT_FALSE(m.Matches(std::function<void()>()));1082}10831084// Tests that NotNull() describes itself properly.1085TEST(NotNullTest, CanDescribeSelf) {1086Matcher<int*> m = NotNull();1087EXPECT_EQ("isn't NULL", Describe(m));1088}10891090// Tests that Ref(variable) matches an argument that references1091// 'variable'.1092TEST(RefTest, MatchesSameVariable) {1093int a = 0;1094int b = 0;1095Matcher<int&> m = Ref(a);1096EXPECT_TRUE(m.Matches(a));1097EXPECT_FALSE(m.Matches(b));1098}10991100// Tests that Ref(variable) describes itself properly.1101TEST(RefTest, CanDescribeSelf) {1102int n = 5;1103Matcher<int&> m = Ref(n);1104stringstream ss;1105ss << "references the variable @" << &n << " 5";1106EXPECT_EQ(ss.str(), Describe(m));1107}11081109// Test that Ref(non_const_varialbe) can be used as a matcher for a1110// const reference.1111TEST(RefTest, CanBeUsedAsMatcherForConstReference) {1112int a = 0;1113int b = 0;1114Matcher<const int&> m = Ref(a);1115EXPECT_TRUE(m.Matches(a));1116EXPECT_FALSE(m.Matches(b));1117}11181119// Tests that Ref(variable) is covariant, i.e. Ref(derived) can be1120// used wherever Ref(base) can be used (Ref(derived) is a sub-type1121// of Ref(base), but not vice versa.11221123TEST(RefTest, IsCovariant) {1124Base base, base2;1125Derived derived;1126Matcher<const Base&> m1 = Ref(base);1127EXPECT_TRUE(m1.Matches(base));1128EXPECT_FALSE(m1.Matches(base2));1129EXPECT_FALSE(m1.Matches(derived));11301131m1 = Ref(derived);1132EXPECT_TRUE(m1.Matches(derived));1133EXPECT_FALSE(m1.Matches(base));1134EXPECT_FALSE(m1.Matches(base2));1135}11361137TEST(RefTest, ExplainsResult) {1138int n = 0;1139EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n),1140StartsWith("which is located @"));11411142int m = 0;1143EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m),1144StartsWith("which is located @"));1145}11461147// Tests string comparison matchers.11481149template <typename T = std::string>1150std::string FromStringLike(internal::StringLike<T> str) {1151return std::string(str);1152}11531154TEST(StringLike, TestConversions) {1155EXPECT_EQ("foo", FromStringLike("foo"));1156EXPECT_EQ("foo", FromStringLike(std::string("foo")));1157#if GTEST_INTERNAL_HAS_STRING_VIEW1158EXPECT_EQ("foo", FromStringLike(internal::StringView("foo")));1159#endif // GTEST_INTERNAL_HAS_STRING_VIEW11601161// Non deducible types.1162EXPECT_EQ("", FromStringLike({}));1163EXPECT_EQ("foo", FromStringLike({'f', 'o', 'o'}));1164const char buf[] = "foo";1165EXPECT_EQ("foo", FromStringLike({buf, buf + 3}));1166}11671168TEST(StrEqTest, MatchesEqualString) {1169Matcher<const char*> m = StrEq(std::string("Hello"));1170EXPECT_TRUE(m.Matches("Hello"));1171EXPECT_FALSE(m.Matches("hello"));1172EXPECT_FALSE(m.Matches(nullptr));11731174Matcher<const std::string&> m2 = StrEq("Hello");1175EXPECT_TRUE(m2.Matches("Hello"));1176EXPECT_FALSE(m2.Matches("Hi"));11771178#if GTEST_INTERNAL_HAS_STRING_VIEW1179Matcher<const internal::StringView&> m3 =1180StrEq(internal::StringView("Hello"));1181EXPECT_TRUE(m3.Matches(internal::StringView("Hello")));1182EXPECT_FALSE(m3.Matches(internal::StringView("hello")));1183EXPECT_FALSE(m3.Matches(internal::StringView()));11841185Matcher<const internal::StringView&> m_empty = StrEq("");1186EXPECT_TRUE(m_empty.Matches(internal::StringView("")));1187EXPECT_TRUE(m_empty.Matches(internal::StringView()));1188EXPECT_FALSE(m_empty.Matches(internal::StringView("hello")));1189#endif // GTEST_INTERNAL_HAS_STRING_VIEW1190}11911192TEST(StrEqTest, CanDescribeSelf) {1193Matcher<std::string> m = StrEq("Hi-\'\"?\\\a\b\f\n\r\t\v\xD3");1194EXPECT_EQ("is equal to \"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",1195Describe(m));11961197std::string str("01204500800");1198str[3] = '\0';1199Matcher<std::string> m2 = StrEq(str);1200EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2));1201str[0] = str[6] = str[7] = str[9] = str[10] = '\0';1202Matcher<std::string> m3 = StrEq(str);1203EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));1204}12051206TEST(StrNeTest, MatchesUnequalString) {1207Matcher<const char*> m = StrNe("Hello");1208EXPECT_TRUE(m.Matches(""));1209EXPECT_TRUE(m.Matches(nullptr));1210EXPECT_FALSE(m.Matches("Hello"));12111212Matcher<std::string> m2 = StrNe(std::string("Hello"));1213EXPECT_TRUE(m2.Matches("hello"));1214EXPECT_FALSE(m2.Matches("Hello"));12151216#if GTEST_INTERNAL_HAS_STRING_VIEW1217Matcher<const internal::StringView> m3 = StrNe(internal::StringView("Hello"));1218EXPECT_TRUE(m3.Matches(internal::StringView("")));1219EXPECT_TRUE(m3.Matches(internal::StringView()));1220EXPECT_FALSE(m3.Matches(internal::StringView("Hello")));1221#endif // GTEST_INTERNAL_HAS_STRING_VIEW1222}12231224TEST(StrNeTest, CanDescribeSelf) {1225Matcher<const char*> m = StrNe("Hi");1226EXPECT_EQ("isn't equal to \"Hi\"", Describe(m));1227}12281229TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {1230Matcher<const char*> m = StrCaseEq(std::string("Hello"));1231EXPECT_TRUE(m.Matches("Hello"));1232EXPECT_TRUE(m.Matches("hello"));1233EXPECT_FALSE(m.Matches("Hi"));1234EXPECT_FALSE(m.Matches(nullptr));12351236Matcher<const std::string&> m2 = StrCaseEq("Hello");1237EXPECT_TRUE(m2.Matches("hello"));1238EXPECT_FALSE(m2.Matches("Hi"));12391240#if GTEST_INTERNAL_HAS_STRING_VIEW1241Matcher<const internal::StringView&> m3 =1242StrCaseEq(internal::StringView("Hello"));1243EXPECT_TRUE(m3.Matches(internal::StringView("Hello")));1244EXPECT_TRUE(m3.Matches(internal::StringView("hello")));1245EXPECT_FALSE(m3.Matches(internal::StringView("Hi")));1246EXPECT_FALSE(m3.Matches(internal::StringView()));1247#endif // GTEST_INTERNAL_HAS_STRING_VIEW1248}12491250TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {1251std::string str1("oabocdooeoo");1252std::string str2("OABOCDOOEOO");1253Matcher<const std::string&> m0 = StrCaseEq(str1);1254EXPECT_FALSE(m0.Matches(str2 + std::string(1, '\0')));12551256str1[3] = str2[3] = '\0';1257Matcher<const std::string&> m1 = StrCaseEq(str1);1258EXPECT_TRUE(m1.Matches(str2));12591260str1[0] = str1[6] = str1[7] = str1[10] = '\0';1261str2[0] = str2[6] = str2[7] = str2[10] = '\0';1262Matcher<const std::string&> m2 = StrCaseEq(str1);1263str1[9] = str2[9] = '\0';1264EXPECT_FALSE(m2.Matches(str2));12651266Matcher<const std::string&> m3 = StrCaseEq(str1);1267EXPECT_TRUE(m3.Matches(str2));12681269EXPECT_FALSE(m3.Matches(str2 + "x"));1270str2.append(1, '\0');1271EXPECT_FALSE(m3.Matches(str2));1272EXPECT_FALSE(m3.Matches(std::string(str2, 0, 9)));1273}12741275TEST(StrCaseEqTest, CanDescribeSelf) {1276Matcher<std::string> m = StrCaseEq("Hi");1277EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m));1278}12791280TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {1281Matcher<const char*> m = StrCaseNe("Hello");1282EXPECT_TRUE(m.Matches("Hi"));1283EXPECT_TRUE(m.Matches(nullptr));1284EXPECT_FALSE(m.Matches("Hello"));1285EXPECT_FALSE(m.Matches("hello"));12861287Matcher<std::string> m2 = StrCaseNe(std::string("Hello"));1288EXPECT_TRUE(m2.Matches(""));1289EXPECT_FALSE(m2.Matches("Hello"));12901291#if GTEST_INTERNAL_HAS_STRING_VIEW1292Matcher<const internal::StringView> m3 =1293StrCaseNe(internal::StringView("Hello"));1294EXPECT_TRUE(m3.Matches(internal::StringView("Hi")));1295EXPECT_TRUE(m3.Matches(internal::StringView()));1296EXPECT_FALSE(m3.Matches(internal::StringView("Hello")));1297EXPECT_FALSE(m3.Matches(internal::StringView("hello")));1298#endif // GTEST_INTERNAL_HAS_STRING_VIEW1299}13001301TEST(StrCaseNeTest, CanDescribeSelf) {1302Matcher<const char*> m = StrCaseNe("Hi");1303EXPECT_EQ("isn't equal to (ignoring case) \"Hi\"", Describe(m));1304}13051306// Tests that HasSubstr() works for matching string-typed values.1307TEST(HasSubstrTest, WorksForStringClasses) {1308const Matcher<std::string> m1 = HasSubstr("foo");1309EXPECT_TRUE(m1.Matches(std::string("I love food.")));1310EXPECT_FALSE(m1.Matches(std::string("tofo")));13111312const Matcher<const std::string&> m2 = HasSubstr("foo");1313EXPECT_TRUE(m2.Matches(std::string("I love food.")));1314EXPECT_FALSE(m2.Matches(std::string("tofo")));13151316const Matcher<std::string> m_empty = HasSubstr("");1317EXPECT_TRUE(m_empty.Matches(std::string()));1318EXPECT_TRUE(m_empty.Matches(std::string("not empty")));1319}13201321// Tests that HasSubstr() works for matching C-string-typed values.1322TEST(HasSubstrTest, WorksForCStrings) {1323const Matcher<char*> m1 = HasSubstr("foo");1324EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food.")));1325EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo")));1326EXPECT_FALSE(m1.Matches(nullptr));13271328const Matcher<const char*> m2 = HasSubstr("foo");1329EXPECT_TRUE(m2.Matches("I love food."));1330EXPECT_FALSE(m2.Matches("tofo"));1331EXPECT_FALSE(m2.Matches(nullptr));13321333const Matcher<const char*> m_empty = HasSubstr("");1334EXPECT_TRUE(m_empty.Matches("not empty"));1335EXPECT_TRUE(m_empty.Matches(""));1336EXPECT_FALSE(m_empty.Matches(nullptr));1337}13381339#if GTEST_INTERNAL_HAS_STRING_VIEW1340// Tests that HasSubstr() works for matching StringView-typed values.1341TEST(HasSubstrTest, WorksForStringViewClasses) {1342const Matcher<internal::StringView> m1 =1343HasSubstr(internal::StringView("foo"));1344EXPECT_TRUE(m1.Matches(internal::StringView("I love food.")));1345EXPECT_FALSE(m1.Matches(internal::StringView("tofo")));1346EXPECT_FALSE(m1.Matches(internal::StringView()));13471348const Matcher<const internal::StringView&> m2 = HasSubstr("foo");1349EXPECT_TRUE(m2.Matches(internal::StringView("I love food.")));1350EXPECT_FALSE(m2.Matches(internal::StringView("tofo")));1351EXPECT_FALSE(m2.Matches(internal::StringView()));13521353const Matcher<const internal::StringView&> m3 = HasSubstr("");1354EXPECT_TRUE(m3.Matches(internal::StringView("foo")));1355EXPECT_TRUE(m3.Matches(internal::StringView("")));1356EXPECT_TRUE(m3.Matches(internal::StringView()));1357}1358#endif // GTEST_INTERNAL_HAS_STRING_VIEW13591360// Tests that HasSubstr(s) describes itself properly.1361TEST(HasSubstrTest, CanDescribeSelf) {1362Matcher<std::string> m = HasSubstr("foo\n\"");1363EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m));1364}13651366INSTANTIATE_GTEST_MATCHER_TEST_P(KeyTest);13671368TEST(KeyTest, CanDescribeSelf) {1369Matcher<const pair<std::string, int>&> m = Key("foo");1370EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m));1371EXPECT_EQ("doesn't have a key that is equal to \"foo\"", DescribeNegation(m));1372}13731374TEST_P(KeyTestP, ExplainsResult) {1375Matcher<pair<int, bool>> m = Key(GreaterThan(10));1376EXPECT_EQ("whose first field is a value which is 5 less than 10",1377Explain(m, make_pair(5, true)));1378EXPECT_EQ("whose first field is a value which is 5 more than 10",1379Explain(m, make_pair(15, true)));1380}13811382TEST(KeyTest, MatchesCorrectly) {1383pair<int, std::string> p(25, "foo");1384EXPECT_THAT(p, Key(25));1385EXPECT_THAT(p, Not(Key(42)));1386EXPECT_THAT(p, Key(Ge(20)));1387EXPECT_THAT(p, Not(Key(Lt(25))));1388}13891390TEST(KeyTest, WorksWithMoveOnly) {1391pair<std::unique_ptr<int>, std::unique_ptr<int>> p;1392EXPECT_THAT(p, Key(Eq(nullptr)));1393}13941395INSTANTIATE_GTEST_MATCHER_TEST_P(PairTest);13961397template <size_t I>1398struct Tag {};13991400struct PairWithGet {1401int member_1;1402std::string member_2;1403using first_type = int;1404using second_type = std::string;14051406const int& GetImpl(Tag<0>) const { return member_1; }1407const std::string& GetImpl(Tag<1>) const { return member_2; }1408};1409template <size_t I>1410auto get(const PairWithGet& value) -> decltype(value.GetImpl(Tag<I>())) {1411return value.GetImpl(Tag<I>());1412}1413TEST(PairTest, MatchesPairWithGetCorrectly) {1414PairWithGet p{25, "foo"};1415EXPECT_THAT(p, Key(25));1416EXPECT_THAT(p, Not(Key(42)));1417EXPECT_THAT(p, Key(Ge(20)));1418EXPECT_THAT(p, Not(Key(Lt(25))));14191420std::vector<PairWithGet> v = {{11, "Foo"}, {29, "gMockIsBestMock"}};1421EXPECT_THAT(v, Contains(Key(29)));1422}14231424TEST(KeyTest, SafelyCastsInnerMatcher) {1425Matcher<int> is_positive = Gt(0);1426Matcher<int> is_negative = Lt(0);1427pair<char, bool> p('a', true);1428EXPECT_THAT(p, Key(is_positive));1429EXPECT_THAT(p, Not(Key(is_negative)));1430}14311432TEST(KeyTest, InsideContainsUsingMap) {1433map<int, char> container;1434container.insert(make_pair(1, 'a'));1435container.insert(make_pair(2, 'b'));1436container.insert(make_pair(4, 'c'));1437EXPECT_THAT(container, Contains(Key(1)));1438EXPECT_THAT(container, Not(Contains(Key(3))));1439}14401441TEST(KeyTest, InsideContainsUsingMultimap) {1442multimap<int, char> container;1443container.insert(make_pair(1, 'a'));1444container.insert(make_pair(2, 'b'));1445container.insert(make_pair(4, 'c'));14461447EXPECT_THAT(container, Not(Contains(Key(25))));1448container.insert(make_pair(25, 'd'));1449EXPECT_THAT(container, Contains(Key(25)));1450container.insert(make_pair(25, 'e'));1451EXPECT_THAT(container, Contains(Key(25)));14521453EXPECT_THAT(container, Contains(Key(1)));1454EXPECT_THAT(container, Not(Contains(Key(3))));1455}14561457TEST(PairTest, Typing) {1458// Test verifies the following type conversions can be compiled.1459Matcher<const pair<const char*, int>&> m1 = Pair("foo", 42);1460Matcher<const pair<const char*, int>> m2 = Pair("foo", 42);1461Matcher<pair<const char*, int>> m3 = Pair("foo", 42);14621463Matcher<pair<int, const std::string>> m4 = Pair(25, "42");1464Matcher<pair<const std::string, int>> m5 = Pair("25", 42);1465}14661467TEST(PairTest, CanDescribeSelf) {1468Matcher<const pair<std::string, int>&> m1 = Pair("foo", 42);1469EXPECT_EQ(1470"has a first field that is equal to \"foo\""1471", and has a second field that is equal to 42",1472Describe(m1));1473EXPECT_EQ(1474"has a first field that isn't equal to \"foo\""1475", or has a second field that isn't equal to 42",1476DescribeNegation(m1));1477// Double and triple negation (1 or 2 times not and description of negation).1478Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42));1479EXPECT_EQ(1480"has a first field that isn't equal to 13"1481", and has a second field that is equal to 42",1482DescribeNegation(m2));1483}14841485TEST_P(PairTestP, CanExplainMatchResultTo) {1486// If neither field matches, Pair() should explain about the first1487// field.1488const Matcher<pair<int, int>> m = Pair(GreaterThan(0), GreaterThan(0));1489EXPECT_EQ("whose first field does not match, which is 1 less than 0",1490Explain(m, make_pair(-1, -2)));14911492// If the first field matches but the second doesn't, Pair() should1493// explain about the second field.1494EXPECT_EQ("whose second field does not match, which is 2 less than 0",1495Explain(m, make_pair(1, -2)));14961497// If the first field doesn't match but the second does, Pair()1498// should explain about the first field.1499EXPECT_EQ("whose first field does not match, which is 1 less than 0",1500Explain(m, make_pair(-1, 2)));15011502// If both fields match, Pair() should explain about them both.1503EXPECT_EQ(1504"whose both fields match, where the first field is a value "1505"which is 1 more than 0, and the second field is a value "1506"which is 2 more than 0",1507Explain(m, make_pair(1, 2)));15081509// If only the first match has an explanation, only this explanation should1510// be printed.1511const Matcher<pair<int, int>> explain_first = Pair(GreaterThan(0), 0);1512EXPECT_EQ(1513"whose both fields match, where the first field is a value "1514"which is 1 more than 0",1515Explain(explain_first, make_pair(1, 0)));15161517// If only the second match has an explanation, only this explanation should1518// be printed.1519const Matcher<pair<int, int>> explain_second = Pair(0, GreaterThan(0));1520EXPECT_EQ(1521"whose both fields match, where the second field is a value "1522"which is 1 more than 0",1523Explain(explain_second, make_pair(0, 1)));1524}15251526TEST(PairTest, MatchesCorrectly) {1527pair<int, std::string> p(25, "foo");15281529// Both fields match.1530EXPECT_THAT(p, Pair(25, "foo"));1531EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o")));15321533// 'first' doesn't match, but 'second' matches.1534EXPECT_THAT(p, Not(Pair(42, "foo")));1535EXPECT_THAT(p, Not(Pair(Lt(25), "foo")));15361537// 'first' matches, but 'second' doesn't match.1538EXPECT_THAT(p, Not(Pair(25, "bar")));1539EXPECT_THAT(p, Not(Pair(25, Not("foo"))));15401541// Neither field matches.1542EXPECT_THAT(p, Not(Pair(13, "bar")));1543EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a"))));1544}15451546TEST(PairTest, WorksWithMoveOnly) {1547pair<std::unique_ptr<int>, std::unique_ptr<int>> p;1548p.second = std::make_unique<int>(7);1549EXPECT_THAT(p, Pair(Eq(nullptr), Ne(nullptr)));1550}15511552TEST(PairTest, SafelyCastsInnerMatchers) {1553Matcher<int> is_positive = Gt(0);1554Matcher<int> is_negative = Lt(0);1555pair<char, bool> p('a', true);1556EXPECT_THAT(p, Pair(is_positive, _));1557EXPECT_THAT(p, Not(Pair(is_negative, _)));1558EXPECT_THAT(p, Pair(_, is_positive));1559EXPECT_THAT(p, Not(Pair(_, is_negative)));1560}15611562TEST(PairTest, InsideContainsUsingMap) {1563map<int, char> container;1564container.insert(make_pair(1, 'a'));1565container.insert(make_pair(2, 'b'));1566container.insert(make_pair(4, 'c'));1567EXPECT_THAT(container, Contains(Pair(1, 'a')));1568EXPECT_THAT(container, Contains(Pair(1, _)));1569EXPECT_THAT(container, Contains(Pair(_, 'a')));1570EXPECT_THAT(container, Not(Contains(Pair(3, _))));1571}15721573INSTANTIATE_GTEST_MATCHER_TEST_P(FieldsAreTest);15741575TEST(FieldsAreTest, MatchesCorrectly) {1576std::tuple<int, std::string, double> p(25, "foo", .5);15771578// All fields match.1579EXPECT_THAT(p, FieldsAre(25, "foo", .5));1580EXPECT_THAT(p, FieldsAre(Ge(20), HasSubstr("o"), DoubleEq(.5)));15811582// Some don't match.1583EXPECT_THAT(p, Not(FieldsAre(26, "foo", .5)));1584EXPECT_THAT(p, Not(FieldsAre(25, "fo", .5)));1585EXPECT_THAT(p, Not(FieldsAre(25, "foo", .6)));1586}15871588TEST(FieldsAreTest, CanDescribeSelf) {1589Matcher<const pair<std::string, int>&> m1 = FieldsAre("foo", 42);1590EXPECT_EQ(1591"has field #0 that is equal to \"foo\""1592", and has field #1 that is equal to 42",1593Describe(m1));1594EXPECT_EQ(1595"has field #0 that isn't equal to \"foo\""1596", or has field #1 that isn't equal to 42",1597DescribeNegation(m1));1598}15991600TEST_P(FieldsAreTestP, CanExplainMatchResultTo) {1601// The first one that fails is the one that gives the error.1602Matcher<std::tuple<int, int, int>> m =1603FieldsAre(GreaterThan(0), GreaterThan(0), GreaterThan(0));16041605EXPECT_EQ("whose field #0 does not match, which is 1 less than 0",1606Explain(m, std::make_tuple(-1, -2, -3)));1607EXPECT_EQ("whose field #1 does not match, which is 2 less than 0",1608Explain(m, std::make_tuple(1, -2, -3)));1609EXPECT_EQ("whose field #2 does not match, which is 3 less than 0",1610Explain(m, std::make_tuple(1, 2, -3)));16111612// If they all match, we get a long explanation of success.1613EXPECT_EQ(1614"whose all elements match, "1615"where field #0 is a value which is 1 more than 0"1616", and field #1 is a value which is 2 more than 0"1617", and field #2 is a value which is 3 more than 0",1618Explain(m, std::make_tuple(1, 2, 3)));16191620// Only print those that have an explanation.1621m = FieldsAre(GreaterThan(0), 0, GreaterThan(0));1622EXPECT_EQ(1623"whose all elements match, "1624"where field #0 is a value which is 1 more than 0"1625", and field #2 is a value which is 3 more than 0",1626Explain(m, std::make_tuple(1, 0, 3)));16271628// If only one has an explanation, then print that one.1629m = FieldsAre(0, GreaterThan(0), 0);1630EXPECT_EQ(1631"whose all elements match, "1632"where field #1 is a value which is 1 more than 0",1633Explain(m, std::make_tuple(0, 1, 0)));1634}16351636#if defined(__cpp_structured_bindings) && __cpp_structured_bindings >= 2016061637TEST(FieldsAreTest, StructuredBindings) {1638// testing::FieldsAre can also match aggregates and such with C++17 and up.1639struct MyType {1640int i;1641std::string str;1642};1643EXPECT_THAT((MyType{17, "foo"}), FieldsAre(Eq(17), HasSubstr("oo")));16441645// Test all the supported arities.1646struct MyVarType1 {1647int a;1648};1649EXPECT_THAT(MyVarType1{}, FieldsAre(0));1650struct MyVarType2 {1651int a, b;1652};1653EXPECT_THAT(MyVarType2{}, FieldsAre(0, 0));1654struct MyVarType3 {1655int a, b, c;1656};1657EXPECT_THAT(MyVarType3{}, FieldsAre(0, 0, 0));1658struct MyVarType4 {1659int a, b, c, d;1660};1661EXPECT_THAT(MyVarType4{}, FieldsAre(0, 0, 0, 0));1662struct MyVarType5 {1663int a, b, c, d, e;1664};1665EXPECT_THAT(MyVarType5{}, FieldsAre(0, 0, 0, 0, 0));1666struct MyVarType6 {1667int a, b, c, d, e, f;1668};1669EXPECT_THAT(MyVarType6{}, FieldsAre(0, 0, 0, 0, 0, 0));1670struct MyVarType7 {1671int a, b, c, d, e, f, g;1672};1673EXPECT_THAT(MyVarType7{}, FieldsAre(0, 0, 0, 0, 0, 0, 0));1674struct MyVarType8 {1675int a, b, c, d, e, f, g, h;1676};1677EXPECT_THAT(MyVarType8{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0));1678struct MyVarType9 {1679int a, b, c, d, e, f, g, h, i;1680};1681EXPECT_THAT(MyVarType9{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0));1682struct MyVarType10 {1683int a, b, c, d, e, f, g, h, i, j;1684};1685EXPECT_THAT(MyVarType10{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0));1686struct MyVarType11 {1687int a, b, c, d, e, f, g, h, i, j, k;1688};1689EXPECT_THAT(MyVarType11{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));1690struct MyVarType12 {1691int a, b, c, d, e, f, g, h, i, j, k, l;1692};1693EXPECT_THAT(MyVarType12{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));1694struct MyVarType13 {1695int a, b, c, d, e, f, g, h, i, j, k, l, m;1696};1697EXPECT_THAT(MyVarType13{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));1698struct MyVarType14 {1699int a, b, c, d, e, f, g, h, i, j, k, l, m, n;1700};1701EXPECT_THAT(MyVarType14{},1702FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));1703struct MyVarType15 {1704int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o;1705};1706EXPECT_THAT(MyVarType15{},1707FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));1708struct MyVarType16 {1709int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p;1710};1711EXPECT_THAT(MyVarType16{},1712FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));1713struct MyVarType17 {1714int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q;1715};1716EXPECT_THAT(MyVarType17{},1717FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));1718struct MyVarType18 {1719int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r;1720};1721EXPECT_THAT(MyVarType18{},1722FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));1723struct MyVarType19 {1724int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s;1725};1726EXPECT_THAT(MyVarType19{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,17270, 0, 0, 0, 0));1728}1729#endif17301731TEST(PairTest, UseGetInsteadOfMembers) {1732PairWithGet pair{7, "ABC"};1733EXPECT_THAT(pair, Pair(7, "ABC"));1734EXPECT_THAT(pair, Pair(Ge(7), HasSubstr("AB")));1735EXPECT_THAT(pair, Not(Pair(Lt(7), "ABC")));17361737std::vector<PairWithGet> v = {{11, "Foo"}, {29, "gMockIsBestMock"}};1738EXPECT_THAT(v,1739ElementsAre(Pair(11, std::string("Foo")), Pair(Ge(10), Not(""))));1740}17411742// Tests StartsWith(s).17431744TEST(StartsWithTest, MatchesStringWithGivenPrefix) {1745const Matcher<const char*> m1 = StartsWith(std::string(""));1746EXPECT_TRUE(m1.Matches("Hi"));1747EXPECT_TRUE(m1.Matches(""));1748EXPECT_FALSE(m1.Matches(nullptr));17491750const Matcher<const std::string&> m2 = StartsWith("Hi");1751EXPECT_TRUE(m2.Matches("Hi"));1752EXPECT_TRUE(m2.Matches("Hi Hi!"));1753EXPECT_TRUE(m2.Matches("High"));1754EXPECT_FALSE(m2.Matches("H"));1755EXPECT_FALSE(m2.Matches(" Hi"));17561757#if GTEST_INTERNAL_HAS_STRING_VIEW1758const Matcher<internal::StringView> m_empty =1759StartsWith(internal::StringView(""));1760EXPECT_TRUE(m_empty.Matches(internal::StringView()));1761EXPECT_TRUE(m_empty.Matches(internal::StringView("")));1762EXPECT_TRUE(m_empty.Matches(internal::StringView("not empty")));1763#endif // GTEST_INTERNAL_HAS_STRING_VIEW1764}17651766TEST(StartsWithTest, CanDescribeSelf) {1767Matcher<const std::string> m = StartsWith("Hi");1768EXPECT_EQ("starts with \"Hi\"", Describe(m));1769}17701771TEST(StartsWithTest, WorksWithStringMatcherOnStringViewMatchee) {1772#if GTEST_INTERNAL_HAS_STRING_VIEW1773EXPECT_THAT(internal::StringView("talk to me goose"),1774StartsWith(std::string("talk")));1775#else1776GTEST_SKIP() << "Not applicable without internal::StringView.";1777#endif // GTEST_INTERNAL_HAS_STRING_VIEW1778}17791780// Tests EndsWith(s).17811782TEST(EndsWithTest, MatchesStringWithGivenSuffix) {1783const Matcher<const char*> m1 = EndsWith("");1784EXPECT_TRUE(m1.Matches("Hi"));1785EXPECT_TRUE(m1.Matches(""));1786EXPECT_FALSE(m1.Matches(nullptr));17871788const Matcher<const std::string&> m2 = EndsWith(std::string("Hi"));1789EXPECT_TRUE(m2.Matches("Hi"));1790EXPECT_TRUE(m2.Matches("Wow Hi Hi"));1791EXPECT_TRUE(m2.Matches("Super Hi"));1792EXPECT_FALSE(m2.Matches("i"));1793EXPECT_FALSE(m2.Matches("Hi "));17941795#if GTEST_INTERNAL_HAS_STRING_VIEW1796const Matcher<const internal::StringView&> m4 =1797EndsWith(internal::StringView(""));1798EXPECT_TRUE(m4.Matches("Hi"));1799EXPECT_TRUE(m4.Matches(""));1800EXPECT_TRUE(m4.Matches(internal::StringView()));1801EXPECT_TRUE(m4.Matches(internal::StringView("")));1802#endif // GTEST_INTERNAL_HAS_STRING_VIEW1803}18041805TEST(EndsWithTest, CanDescribeSelf) {1806Matcher<const std::string> m = EndsWith("Hi");1807EXPECT_EQ("ends with \"Hi\"", Describe(m));1808}18091810// Tests WhenBase64Unescaped.18111812TEST(WhenBase64UnescapedTest, MatchesUnescapedBase64Strings) {1813const Matcher<const char*> m1 = WhenBase64Unescaped(EndsWith("!"));1814EXPECT_FALSE(m1.Matches("invalid base64"));1815EXPECT_FALSE(m1.Matches("aGVsbG8gd29ybGQ=")); // hello world1816EXPECT_TRUE(m1.Matches("aGVsbG8gd29ybGQh")); // hello world!1817EXPECT_TRUE(m1.Matches("+/-_IQ")); // \xfb\xff\xbf!18181819const Matcher<const std::string&> m2 = WhenBase64Unescaped(EndsWith("!"));1820EXPECT_FALSE(m2.Matches("invalid base64"));1821EXPECT_FALSE(m2.Matches("aGVsbG8gd29ybGQ=")); // hello world1822EXPECT_TRUE(m2.Matches("aGVsbG8gd29ybGQh")); // hello world!1823EXPECT_TRUE(m2.Matches("+/-_IQ")); // \xfb\xff\xbf!18241825#if GTEST_INTERNAL_HAS_STRING_VIEW1826const Matcher<const internal::StringView&> m3 =1827WhenBase64Unescaped(EndsWith("!"));1828EXPECT_FALSE(m3.Matches("invalid base64"));1829EXPECT_FALSE(m3.Matches("aGVsbG8gd29ybGQ=")); // hello world1830EXPECT_TRUE(m3.Matches("aGVsbG8gd29ybGQh")); // hello world!1831EXPECT_TRUE(m3.Matches("+/-_IQ")); // \xfb\xff\xbf!1832#endif // GTEST_INTERNAL_HAS_STRING_VIEW1833}18341835TEST(WhenBase64UnescapedTest, CanDescribeSelf) {1836const Matcher<const char*> m = WhenBase64Unescaped(EndsWith("!"));1837EXPECT_EQ("matches after Base64Unescape ends with \"!\"", Describe(m));1838}18391840// Tests MatchesRegex().18411842TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {1843const Matcher<const char*> m1 = MatchesRegex("a.*z");1844EXPECT_TRUE(m1.Matches("az"));1845EXPECT_TRUE(m1.Matches("abcz"));1846EXPECT_FALSE(m1.Matches(nullptr));18471848const Matcher<const std::string&> m2 = MatchesRegex(new RE("a.*z"));1849EXPECT_TRUE(m2.Matches("azbz"));1850EXPECT_FALSE(m2.Matches("az1"));1851EXPECT_FALSE(m2.Matches("1az"));18521853#if GTEST_INTERNAL_HAS_STRING_VIEW1854const Matcher<const internal::StringView&> m3 = MatchesRegex("a.*z");1855EXPECT_TRUE(m3.Matches(internal::StringView("az")));1856EXPECT_TRUE(m3.Matches(internal::StringView("abcz")));1857EXPECT_FALSE(m3.Matches(internal::StringView("1az")));1858EXPECT_FALSE(m3.Matches(internal::StringView()));1859const Matcher<const internal::StringView&> m4 =1860MatchesRegex(internal::StringView(""));1861EXPECT_TRUE(m4.Matches(internal::StringView("")));1862EXPECT_TRUE(m4.Matches(internal::StringView()));1863#endif // GTEST_INTERNAL_HAS_STRING_VIEW1864}18651866TEST(MatchesRegexTest, CanDescribeSelf) {1867Matcher<const std::string> m1 = MatchesRegex(std::string("Hi.*"));1868EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1));18691870Matcher<const char*> m2 = MatchesRegex(new RE("a.*"));1871EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2));18721873#if GTEST_INTERNAL_HAS_STRING_VIEW1874Matcher<const internal::StringView> m3 = MatchesRegex(new RE("0.*"));1875EXPECT_EQ("matches regular expression \"0.*\"", Describe(m3));1876#endif // GTEST_INTERNAL_HAS_STRING_VIEW1877}18781879// Tests ContainsRegex().18801881TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {1882const Matcher<const char*> m1 = ContainsRegex(std::string("a.*z"));1883EXPECT_TRUE(m1.Matches("az"));1884EXPECT_TRUE(m1.Matches("0abcz1"));1885EXPECT_FALSE(m1.Matches(nullptr));18861887const Matcher<const std::string&> m2 = ContainsRegex(new RE("a.*z"));1888EXPECT_TRUE(m2.Matches("azbz"));1889EXPECT_TRUE(m2.Matches("az1"));1890EXPECT_FALSE(m2.Matches("1a"));18911892#if GTEST_INTERNAL_HAS_STRING_VIEW1893const Matcher<const internal::StringView&> m3 = ContainsRegex(new RE("a.*z"));1894EXPECT_TRUE(m3.Matches(internal::StringView("azbz")));1895EXPECT_TRUE(m3.Matches(internal::StringView("az1")));1896EXPECT_FALSE(m3.Matches(internal::StringView("1a")));1897EXPECT_FALSE(m3.Matches(internal::StringView()));1898const Matcher<const internal::StringView&> m4 =1899ContainsRegex(internal::StringView(""));1900EXPECT_TRUE(m4.Matches(internal::StringView("")));1901EXPECT_TRUE(m4.Matches(internal::StringView()));1902#endif // GTEST_INTERNAL_HAS_STRING_VIEW1903}19041905TEST(ContainsRegexTest, CanDescribeSelf) {1906Matcher<const std::string> m1 = ContainsRegex("Hi.*");1907EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1));19081909Matcher<const char*> m2 = ContainsRegex(new RE("a.*"));1910EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2));19111912#if GTEST_INTERNAL_HAS_STRING_VIEW1913Matcher<const internal::StringView> m3 = ContainsRegex(new RE("0.*"));1914EXPECT_EQ("contains regular expression \"0.*\"", Describe(m3));1915#endif // GTEST_INTERNAL_HAS_STRING_VIEW1916}19171918// Tests for wide strings.1919#if GTEST_HAS_STD_WSTRING1920TEST(StdWideStrEqTest, MatchesEqual) {1921Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello"));1922EXPECT_TRUE(m.Matches(L"Hello"));1923EXPECT_FALSE(m.Matches(L"hello"));1924EXPECT_FALSE(m.Matches(nullptr));19251926Matcher<const ::std::wstring&> m2 = StrEq(L"Hello");1927EXPECT_TRUE(m2.Matches(L"Hello"));1928EXPECT_FALSE(m2.Matches(L"Hi"));19291930Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");1931EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));1932EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));19331934::std::wstring str(L"01204500800");1935str[3] = L'\0';1936Matcher<const ::std::wstring&> m4 = StrEq(str);1937EXPECT_TRUE(m4.Matches(str));1938str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';1939Matcher<const ::std::wstring&> m5 = StrEq(str);1940EXPECT_TRUE(m5.Matches(str));1941}19421943TEST(StdWideStrEqTest, CanDescribeSelf) {1944Matcher<::std::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v");1945EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",1946Describe(m));19471948Matcher<::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");1949EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"", Describe(m2));19501951::std::wstring str(L"01204500800");1952str[3] = L'\0';1953Matcher<const ::std::wstring&> m4 = StrEq(str);1954EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));1955str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';1956Matcher<const ::std::wstring&> m5 = StrEq(str);1957EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));1958}19591960TEST(StdWideStrNeTest, MatchesUnequalString) {1961Matcher<const wchar_t*> m = StrNe(L"Hello");1962EXPECT_TRUE(m.Matches(L""));1963EXPECT_TRUE(m.Matches(nullptr));1964EXPECT_FALSE(m.Matches(L"Hello"));19651966Matcher<::std::wstring> m2 = StrNe(::std::wstring(L"Hello"));1967EXPECT_TRUE(m2.Matches(L"hello"));1968EXPECT_FALSE(m2.Matches(L"Hello"));1969}19701971TEST(StdWideStrNeTest, CanDescribeSelf) {1972Matcher<const wchar_t*> m = StrNe(L"Hi");1973EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));1974}19751976TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {1977Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello"));1978EXPECT_TRUE(m.Matches(L"Hello"));1979EXPECT_TRUE(m.Matches(L"hello"));1980EXPECT_FALSE(m.Matches(L"Hi"));1981EXPECT_FALSE(m.Matches(nullptr));19821983Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello");1984EXPECT_TRUE(m2.Matches(L"hello"));1985EXPECT_FALSE(m2.Matches(L"Hi"));1986}19871988TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {1989::std::wstring str1(L"oabocdooeoo");1990::std::wstring str2(L"OABOCDOOEOO");1991Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);1992EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0')));19931994str1[3] = str2[3] = L'\0';1995Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);1996EXPECT_TRUE(m1.Matches(str2));19971998str1[0] = str1[6] = str1[7] = str1[10] = L'\0';1999str2[0] = str2[6] = str2[7] = str2[10] = L'\0';2000Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);2001str1[9] = str2[9] = L'\0';2002EXPECT_FALSE(m2.Matches(str2));20032004Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);2005EXPECT_TRUE(m3.Matches(str2));20062007EXPECT_FALSE(m3.Matches(str2 + L"x"));2008str2.append(1, L'\0');2009EXPECT_FALSE(m3.Matches(str2));2010EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9)));2011}20122013TEST(StdWideStrCaseEqTest, CanDescribeSelf) {2014Matcher<::std::wstring> m = StrCaseEq(L"Hi");2015EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));2016}20172018TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {2019Matcher<const wchar_t*> m = StrCaseNe(L"Hello");2020EXPECT_TRUE(m.Matches(L"Hi"));2021EXPECT_TRUE(m.Matches(nullptr));2022EXPECT_FALSE(m.Matches(L"Hello"));2023EXPECT_FALSE(m.Matches(L"hello"));20242025Matcher<::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello"));2026EXPECT_TRUE(m2.Matches(L""));2027EXPECT_FALSE(m2.Matches(L"Hello"));2028}20292030TEST(StdWideStrCaseNeTest, CanDescribeSelf) {2031Matcher<const wchar_t*> m = StrCaseNe(L"Hi");2032EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));2033}20342035// Tests that HasSubstr() works for matching wstring-typed values.2036TEST(StdWideHasSubstrTest, WorksForStringClasses) {2037const Matcher<::std::wstring> m1 = HasSubstr(L"foo");2038EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food.")));2039EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo")));20402041const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo");2042EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food.")));2043EXPECT_FALSE(m2.Matches(::std::wstring(L"tofo")));2044}20452046// Tests that HasSubstr() works for matching C-wide-string-typed values.2047TEST(StdWideHasSubstrTest, WorksForCStrings) {2048const Matcher<wchar_t*> m1 = HasSubstr(L"foo");2049EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));2050EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));2051EXPECT_FALSE(m1.Matches(nullptr));20522053const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");2054EXPECT_TRUE(m2.Matches(L"I love food."));2055EXPECT_FALSE(m2.Matches(L"tofo"));2056EXPECT_FALSE(m2.Matches(nullptr));2057}20582059// Tests that HasSubstr(s) describes itself properly.2060TEST(StdWideHasSubstrTest, CanDescribeSelf) {2061Matcher<::std::wstring> m = HasSubstr(L"foo\n\"");2062EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));2063}20642065// Tests StartsWith(s).20662067TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {2068const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L""));2069EXPECT_TRUE(m1.Matches(L"Hi"));2070EXPECT_TRUE(m1.Matches(L""));2071EXPECT_FALSE(m1.Matches(nullptr));20722073const Matcher<const ::std::wstring&> m2 = StartsWith(L"Hi");2074EXPECT_TRUE(m2.Matches(L"Hi"));2075EXPECT_TRUE(m2.Matches(L"Hi Hi!"));2076EXPECT_TRUE(m2.Matches(L"High"));2077EXPECT_FALSE(m2.Matches(L"H"));2078EXPECT_FALSE(m2.Matches(L" Hi"));2079}20802081TEST(StdWideStartsWithTest, CanDescribeSelf) {2082Matcher<const ::std::wstring> m = StartsWith(L"Hi");2083EXPECT_EQ("starts with L\"Hi\"", Describe(m));2084}20852086// Tests EndsWith(s).20872088TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {2089const Matcher<const wchar_t*> m1 = EndsWith(L"");2090EXPECT_TRUE(m1.Matches(L"Hi"));2091EXPECT_TRUE(m1.Matches(L""));2092EXPECT_FALSE(m1.Matches(nullptr));20932094const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L"Hi"));2095EXPECT_TRUE(m2.Matches(L"Hi"));2096EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));2097EXPECT_TRUE(m2.Matches(L"Super Hi"));2098EXPECT_FALSE(m2.Matches(L"i"));2099EXPECT_FALSE(m2.Matches(L"Hi "));2100}21012102TEST(StdWideEndsWithTest, CanDescribeSelf) {2103Matcher<const ::std::wstring> m = EndsWith(L"Hi");2104EXPECT_EQ("ends with L\"Hi\"", Describe(m));2105}21062107#endif // GTEST_HAS_STD_WSTRING21082109TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {2110StringMatchResultListener listener1;2111EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1));2112EXPECT_EQ("% 2 == 0", listener1.str());21132114StringMatchResultListener listener2;2115EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2));2116EXPECT_EQ("", listener2.str());2117}21182119TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {2120const Matcher<int> is_even = PolymorphicIsEven();2121StringMatchResultListener listener1;2122EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1));2123EXPECT_EQ("% 2 == 0", listener1.str());21242125const Matcher<const double&> is_zero = Eq(0);2126StringMatchResultListener listener2;2127EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2));2128EXPECT_EQ("", listener2.str());2129}21302131MATCHER(ConstructNoArg, "") { return true; }2132MATCHER_P(Construct1Arg, arg1, "") { return true; }2133MATCHER_P2(Construct2Args, arg1, arg2, "") { return true; }21342135TEST(MatcherConstruct, ExplicitVsImplicit) {2136{2137// No arg constructor can be constructed with empty brace.2138ConstructNoArgMatcher m = {};2139(void)m;2140// And with no args2141ConstructNoArgMatcher m2;2142(void)m2;2143}2144{2145// The one arg constructor has an explicit constructor.2146// This is to prevent the implicit conversion.2147using M = Construct1ArgMatcherP<int>;2148EXPECT_TRUE((std::is_constructible<M, int>::value));2149EXPECT_FALSE((std::is_convertible<int, M>::value));2150}2151{2152// Multiple arg matchers can be constructed with an implicit construction.2153Construct2ArgsMatcherP2<int, double> m = {1, 2.2};2154(void)m;2155}2156}21572158MATCHER_P(Really, inner_matcher, "") {2159return ExplainMatchResult(inner_matcher, arg, result_listener);2160}21612162TEST(ExplainMatchResultTest, WorksInsideMATCHER) {2163EXPECT_THAT(0, Really(Eq(0)));2164}21652166TEST(DescribeMatcherTest, WorksWithValue) {2167EXPECT_EQ("is equal to 42", DescribeMatcher<int>(42));2168EXPECT_EQ("isn't equal to 42", DescribeMatcher<int>(42, true));2169}21702171TEST(DescribeMatcherTest, WorksWithMonomorphicMatcher) {2172const Matcher<int> monomorphic = Le(0);2173EXPECT_EQ("is <= 0", DescribeMatcher<int>(monomorphic));2174EXPECT_EQ("isn't <= 0", DescribeMatcher<int>(monomorphic, true));2175}21762177TEST(DescribeMatcherTest, WorksWithPolymorphicMatcher) {2178EXPECT_EQ("is even", DescribeMatcher<int>(PolymorphicIsEven()));2179EXPECT_EQ("is odd", DescribeMatcher<int>(PolymorphicIsEven(), true));2180}21812182MATCHER_P(FieldIIs, inner_matcher, "") {2183return ExplainMatchResult(inner_matcher, arg.i, result_listener);2184}21852186#if GTEST_HAS_RTTI2187TEST(WhenDynamicCastToTest, SameType) {2188Derived derived;2189derived.i = 4;21902191// Right type. A pointer is passed down.2192Base* as_base_ptr = &derived;2193EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(Not(IsNull())));2194EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(Pointee(FieldIIs(4))));2195EXPECT_THAT(as_base_ptr,2196Not(WhenDynamicCastTo<Derived*>(Pointee(FieldIIs(5)))));2197}21982199TEST(WhenDynamicCastToTest, WrongTypes) {2200Base base;2201Derived derived;2202OtherDerived other_derived;22032204// Wrong types. NULL is passed.2205EXPECT_THAT(&base, Not(WhenDynamicCastTo<Derived*>(Pointee(_))));2206EXPECT_THAT(&base, WhenDynamicCastTo<Derived*>(IsNull()));2207Base* as_base_ptr = &derived;2208EXPECT_THAT(as_base_ptr, Not(WhenDynamicCastTo<OtherDerived*>(Pointee(_))));2209EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<OtherDerived*>(IsNull()));2210as_base_ptr = &other_derived;2211EXPECT_THAT(as_base_ptr, Not(WhenDynamicCastTo<Derived*>(Pointee(_))));2212EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(IsNull()));2213}22142215TEST(WhenDynamicCastToTest, AlreadyNull) {2216// Already NULL.2217Base* as_base_ptr = nullptr;2218EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(IsNull()));2219}22202221struct AmbiguousCastTypes {2222class VirtualDerived : public virtual Base {};2223class DerivedSub1 : public VirtualDerived {};2224class DerivedSub2 : public VirtualDerived {};2225class ManyDerivedInHierarchy : public DerivedSub1, public DerivedSub2 {};2226};22272228TEST(WhenDynamicCastToTest, AmbiguousCast) {2229AmbiguousCastTypes::DerivedSub1 sub1;2230AmbiguousCastTypes::ManyDerivedInHierarchy many_derived;22312232// This testcase fails on FreeBSD. See this GitHub issue for more details:2233// https://github.com/google/googletest/issues/21722234#ifdef __FreeBSD__2235EXPECT_NONFATAL_FAILURE({2236#endif2237// Multiply derived from Base. dynamic_cast<> returns NULL.2238Base* as_base_ptr =2239static_cast<AmbiguousCastTypes::DerivedSub1*>(&many_derived);22402241EXPECT_THAT(as_base_ptr,2242WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(IsNull()));2243as_base_ptr = &sub1;2244EXPECT_THAT(2245as_base_ptr,2246WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(Not(IsNull())));2247#ifdef __FreeBSD__2248}, "");2249#endif2250}22512252TEST(WhenDynamicCastToTest, Describe) {2253Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(Pointee(_));2254const std::string prefix =2255"when dynamic_cast to " + internal::GetTypeName<Derived*>() + ", ";2256EXPECT_EQ(prefix + "points to a value that is anything", Describe(matcher));2257EXPECT_EQ(prefix + "does not point to a value that is anything",2258DescribeNegation(matcher));2259}22602261TEST(WhenDynamicCastToTest, Explain) {2262Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(Pointee(_));2263Base* null = nullptr;2264EXPECT_THAT(Explain(matcher, null), HasSubstr("NULL"));2265Derived derived;2266EXPECT_TRUE(matcher.Matches(&derived));2267EXPECT_THAT(Explain(matcher, &derived), HasSubstr("which points to "));22682269// With references, the matcher itself can fail. Test for that one.2270Matcher<const Base&> ref_matcher = WhenDynamicCastTo<const OtherDerived&>(_);2271EXPECT_THAT(Explain(ref_matcher, derived),2272HasSubstr("which cannot be dynamic_cast"));2273}22742275TEST(WhenDynamicCastToTest, GoodReference) {2276Derived derived;2277derived.i = 4;2278Base& as_base_ref = derived;2279EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(FieldIIs(4)));2280EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(Not(FieldIIs(5))));2281}22822283TEST(WhenDynamicCastToTest, BadReference) {2284Derived derived;2285Base& as_base_ref = derived;2286EXPECT_THAT(as_base_ref, Not(WhenDynamicCastTo<const OtherDerived&>(_)));2287}2288#endif // GTEST_HAS_RTTI22892290class DivisibleByImpl {2291public:2292explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {}22932294// For testing using ExplainMatchResultTo() with polymorphic matchers.2295template <typename T>2296bool MatchAndExplain(const T& n, MatchResultListener* listener) const {2297*listener << "which is " << (n % divider_) << " modulo " << divider_;2298return (n % divider_) == 0;2299}23002301void DescribeTo(ostream* os) const { *os << "is divisible by " << divider_; }23022303void DescribeNegationTo(ostream* os) const {2304*os << "is not divisible by " << divider_;2305}23062307void set_divider(int a_divider) { divider_ = a_divider; }2308int divider() const { return divider_; }23092310private:2311int divider_;2312};23132314PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) {2315return MakePolymorphicMatcher(DivisibleByImpl(n));2316}23172318// Tests that when AllOf() fails, only the first failing matcher is2319// asked to explain why.2320TEST(ExplainMatchResultTest, AllOf_False_False) {2321const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));2322EXPECT_EQ("which is 1 modulo 4", Explain(m, 5));2323}23242325// Tests that when AllOf() fails, only the first failing matcher is2326// asked to explain why.2327TEST(ExplainMatchResultTest, AllOf_False_True) {2328const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));2329EXPECT_EQ("which is 2 modulo 4", Explain(m, 6));2330}23312332// Tests that when AllOf() fails, only the first failing matcher is2333// asked to explain why.2334TEST(ExplainMatchResultTest, AllOf_True_False) {2335const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));2336EXPECT_EQ("which is 2 modulo 3", Explain(m, 5));2337}23382339// Tests that when AllOf() succeeds, all matchers are asked to explain2340// why.2341TEST(ExplainMatchResultTest, AllOf_True_True) {2342const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));2343EXPECT_EQ("which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6));2344}23452346TEST(ExplainMatchResultTest, AllOf_True_True_2) {2347const Matcher<int> m = AllOf(Ge(2), Le(3));2348EXPECT_EQ("", Explain(m, 2));2349}23502351INSTANTIATE_GTEST_MATCHER_TEST_P(ExplainmatcherResultTest);23522353TEST_P(ExplainmatcherResultTestP, MonomorphicMatcher) {2354const Matcher<int> m = GreaterThan(5);2355EXPECT_EQ("which is 1 more than 5", Explain(m, 6));2356}23572358// Tests PolymorphicMatcher::mutable_impl().2359TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {2360PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));2361DivisibleByImpl& impl = m.mutable_impl();2362EXPECT_EQ(42, impl.divider());23632364impl.set_divider(0);2365EXPECT_EQ(0, m.mutable_impl().divider());2366}23672368// Tests PolymorphicMatcher::impl().2369TEST(PolymorphicMatcherTest, CanAccessImpl) {2370const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));2371const DivisibleByImpl& impl = m.impl();2372EXPECT_EQ(42, impl.divider());2373}23742375} // namespace2376} // namespace gmock_matchers_test2377} // namespace testing23782379GTEST_DISABLE_MSC_WARNINGS_POP_() // 4244 4100238023812382