Path: blob/main/contrib/googletest/googlemock/test/gmock-matchers-comparisons_test.cc
111863 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 <optional>36#include <string>37#include <tuple>38#include <vector>3940#include "gmock/gmock.h"41#include "test/gmock-matchers_test.h"42#include "gtest/gtest.h"4344// Silence warning C4244: 'initializing': conversion from 'int' to 'short',45// possible loss of data and C4100, unreferenced local parameter46GTEST_DISABLE_MSC_WARNINGS_PUSH_(4244 4100)4748namespace testing {49namespace gmock_matchers_test {50namespace {5152INSTANTIATE_GTEST_MATCHER_TEST_P(MonotonicMatcherTest);5354TEST_P(MonotonicMatcherTestP, IsPrintable) {55stringstream ss;56ss << GreaterThan(5);57EXPECT_EQ("is > 5", ss.str());58}5960TEST(MatchResultListenerTest, StreamingWorks) {61StringMatchResultListener listener;62listener << "hi" << 5;63EXPECT_EQ("hi5", listener.str());6465listener.Clear();66EXPECT_EQ("", listener.str());6768listener << 42;69EXPECT_EQ("42", listener.str());7071// Streaming shouldn't crash when the underlying ostream is NULL.72DummyMatchResultListener dummy;73dummy << "hi" << 5;74}7576TEST(MatchResultListenerTest, CanAccessUnderlyingStream) {77EXPECT_TRUE(DummyMatchResultListener().stream() == nullptr);78EXPECT_TRUE(StreamMatchResultListener(nullptr).stream() == nullptr);7980EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream());81}8283TEST(MatchResultListenerTest, IsInterestedWorks) {84EXPECT_TRUE(StringMatchResultListener().IsInterested());85EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested());8687EXPECT_FALSE(DummyMatchResultListener().IsInterested());88EXPECT_FALSE(StreamMatchResultListener(nullptr).IsInterested());89}9091// Makes sure that the MatcherInterface<T> interface doesn't92// change.93class EvenMatcherImpl : public MatcherInterface<int> {94public:95bool MatchAndExplain(int x,96MatchResultListener* /* listener */) const override {97return x % 2 == 0;98}99100void DescribeTo(ostream* os) const override { *os << "is an even number"; }101102// We deliberately don't define DescribeNegationTo() and103// ExplainMatchResultTo() here, to make sure the definition of these104// two methods is optional.105};106107// Makes sure that the MatcherInterface API doesn't change.108TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) {109EvenMatcherImpl m;110}111112// Tests implementing a monomorphic matcher using MatchAndExplain().113114class NewEvenMatcherImpl : public MatcherInterface<int> {115public:116bool MatchAndExplain(int x, MatchResultListener* listener) const override {117const bool match = x % 2 == 0;118// Verifies that we can stream to a listener directly.119*listener << "value % " << 2;120if (listener->stream() != nullptr) {121// Verifies that we can stream to a listener's underlying stream122// too.123*listener->stream() << " == " << (x % 2);124}125return match;126}127128void DescribeTo(ostream* os) const override { *os << "is an even number"; }129};130131TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {132Matcher<int> m = MakeMatcher(new NewEvenMatcherImpl);133EXPECT_TRUE(m.Matches(2));134EXPECT_FALSE(m.Matches(3));135EXPECT_EQ("value % 2 == 0", Explain(m, 2));136EXPECT_EQ("value % 2 == 1", Explain(m, 3));137}138139INSTANTIATE_GTEST_MATCHER_TEST_P(MatcherTest);140141// Tests default-constructing a matcher.142TEST(MatcherTest, CanBeDefaultConstructed) { Matcher<double> m; }143144// Tests that Matcher<T> can be constructed from a MatcherInterface<T>*.145TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {146const MatcherInterface<int>* impl = new EvenMatcherImpl;147Matcher<int> m(impl);148EXPECT_TRUE(m.Matches(4));149EXPECT_FALSE(m.Matches(5));150}151152// Tests that value can be used in place of Eq(value).153TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) {154Matcher<int> m1 = 5;155EXPECT_TRUE(m1.Matches(5));156EXPECT_FALSE(m1.Matches(6));157}158159// Tests that NULL can be used in place of Eq(NULL).160TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) {161Matcher<int*> m1 = nullptr;162EXPECT_TRUE(m1.Matches(nullptr));163int n = 0;164EXPECT_FALSE(m1.Matches(&n));165}166167// Tests that matchers can be constructed from a variable that is not properly168// defined. This should be illegal, but many users rely on this accidentally.169struct Undefined {170virtual ~Undefined() = 0;171static const int kInt = 1;172};173174TEST(MatcherTest, CanBeConstructedFromUndefinedVariable) {175Matcher<int> m1 = Undefined::kInt;176EXPECT_TRUE(m1.Matches(1));177EXPECT_FALSE(m1.Matches(2));178}179180// Test that a matcher parameterized with an abstract class compiles.181TEST(MatcherTest, CanAcceptAbstractClass) { Matcher<const Undefined&> m = _; }182183// Tests that matchers are copyable.184TEST(MatcherTest, IsCopyable) {185// Tests the copy constructor.186Matcher<bool> m1 = Eq(false);187EXPECT_TRUE(m1.Matches(false));188EXPECT_FALSE(m1.Matches(true));189190// Tests the assignment operator.191m1 = Eq(true);192EXPECT_TRUE(m1.Matches(true));193EXPECT_FALSE(m1.Matches(false));194}195196// Tests that Matcher<T>::DescribeTo() calls197// MatcherInterface<T>::DescribeTo().198TEST(MatcherTest, CanDescribeItself) {199EXPECT_EQ("is an even number", Describe(Matcher<int>(new EvenMatcherImpl)));200}201202// Tests Matcher<T>::MatchAndExplain().203TEST_P(MatcherTestP, MatchAndExplain) {204Matcher<int> m = GreaterThan(0);205StringMatchResultListener listener1;206EXPECT_TRUE(m.MatchAndExplain(42, &listener1));207EXPECT_EQ("which is 42 more than 0", listener1.str());208209StringMatchResultListener listener2;210EXPECT_FALSE(m.MatchAndExplain(-9, &listener2));211EXPECT_EQ("which is 9 less than 0", listener2.str());212}213214// Tests that a C-string literal can be implicitly converted to a215// Matcher<std::string> or Matcher<const std::string&>.216TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {217Matcher<std::string> m1 = "hi";218EXPECT_TRUE(m1.Matches("hi"));219EXPECT_FALSE(m1.Matches("hello"));220221Matcher<const std::string&> m2 = "hi";222EXPECT_TRUE(m2.Matches("hi"));223EXPECT_FALSE(m2.Matches("hello"));224}225226// Tests that a string object can be implicitly converted to a227// Matcher<std::string> or Matcher<const std::string&>.228TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {229Matcher<std::string> m1 = std::string("hi");230EXPECT_TRUE(m1.Matches("hi"));231EXPECT_FALSE(m1.Matches("hello"));232233Matcher<const std::string&> m2 = std::string("hi");234EXPECT_TRUE(m2.Matches("hi"));235EXPECT_FALSE(m2.Matches("hello"));236}237238#if GTEST_INTERNAL_HAS_STRING_VIEW239// Tests that a C-string literal can be implicitly converted to a240// Matcher<StringView> or Matcher<const StringView&>.241TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {242Matcher<internal::StringView> m1 = "cats";243EXPECT_TRUE(m1.Matches("cats"));244EXPECT_FALSE(m1.Matches("dogs"));245246Matcher<const internal::StringView&> m2 = "cats";247EXPECT_TRUE(m2.Matches("cats"));248EXPECT_FALSE(m2.Matches("dogs"));249}250251// Tests that a std::string object can be implicitly converted to a252// Matcher<StringView> or Matcher<const StringView&>.253TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromString) {254Matcher<internal::StringView> m1 = std::string("cats");255EXPECT_TRUE(m1.Matches("cats"));256EXPECT_FALSE(m1.Matches("dogs"));257258Matcher<const internal::StringView&> m2 = std::string("cats");259EXPECT_TRUE(m2.Matches("cats"));260EXPECT_FALSE(m2.Matches("dogs"));261}262263// Tests that a StringView object can be implicitly converted to a264// Matcher<StringView> or Matcher<const StringView&>.265TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromStringView) {266Matcher<internal::StringView> m1 = internal::StringView("cats");267EXPECT_TRUE(m1.Matches("cats"));268EXPECT_FALSE(m1.Matches("dogs"));269270Matcher<const internal::StringView&> m2 = internal::StringView("cats");271EXPECT_TRUE(m2.Matches("cats"));272EXPECT_FALSE(m2.Matches("dogs"));273}274#endif // GTEST_INTERNAL_HAS_STRING_VIEW275276// Tests that a std::reference_wrapper<std::string> object can be implicitly277// converted to a Matcher<std::string> or Matcher<const std::string&> via Eq().278TEST(StringMatcherTest,279CanBeImplicitlyConstructedFromEqReferenceWrapperString) {280std::string value = "cats";281Matcher<std::string> m1 = Eq(std::ref(value));282EXPECT_TRUE(m1.Matches("cats"));283EXPECT_FALSE(m1.Matches("dogs"));284285Matcher<const std::string&> m2 = Eq(std::ref(value));286EXPECT_TRUE(m2.Matches("cats"));287EXPECT_FALSE(m2.Matches("dogs"));288}289290// Tests that MakeMatcher() constructs a Matcher<T> from a291// MatcherInterface* without requiring the user to explicitly292// write the type.293TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {294const MatcherInterface<int>* dummy_impl = new EvenMatcherImpl;295Matcher<int> m = MakeMatcher(dummy_impl);296}297298// Tests that MakePolymorphicMatcher() can construct a polymorphic299// matcher from its implementation using the old API.300const int g_bar = 1;301class ReferencesBarOrIsZeroImpl {302public:303template <typename T>304bool MatchAndExplain(const T& x, MatchResultListener* /* listener */) const {305const void* p = &x;306return p == &g_bar || x == 0;307}308309void DescribeTo(ostream* os) const { *os << "g_bar or zero"; }310311void DescribeNegationTo(ostream* os) const {312*os << "doesn't reference g_bar and is not zero";313}314};315316// This function verifies that MakePolymorphicMatcher() returns a317// PolymorphicMatcher<T> where T is the argument's type.318PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {319return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl());320}321322TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {323// Using a polymorphic matcher to match a reference type.324Matcher<const int&> m1 = ReferencesBarOrIsZero();325EXPECT_TRUE(m1.Matches(0));326// Verifies that the identity of a by-reference argument is preserved.327EXPECT_TRUE(m1.Matches(g_bar));328EXPECT_FALSE(m1.Matches(1));329EXPECT_EQ("g_bar or zero", Describe(m1));330331// Using a polymorphic matcher to match a value type.332Matcher<double> m2 = ReferencesBarOrIsZero();333EXPECT_TRUE(m2.Matches(0.0));334EXPECT_FALSE(m2.Matches(0.1));335EXPECT_EQ("g_bar or zero", Describe(m2));336}337338// Tests implementing a polymorphic matcher using MatchAndExplain().339340class PolymorphicIsEvenImpl {341public:342void DescribeTo(ostream* os) const { *os << "is even"; }343344void DescribeNegationTo(ostream* os) const { *os << "is odd"; }345346template <typename T>347bool MatchAndExplain(const T& x, MatchResultListener* listener) const {348// Verifies that we can stream to the listener directly.349*listener << "% " << 2;350if (listener->stream() != nullptr) {351// Verifies that we can stream to the listener's underlying stream352// too.353*listener->stream() << " == " << (x % 2);354}355return (x % 2) == 0;356}357};358359PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {360return MakePolymorphicMatcher(PolymorphicIsEvenImpl());361}362363TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {364// Using PolymorphicIsEven() as a Matcher<int>.365const Matcher<int> m1 = PolymorphicIsEven();366EXPECT_TRUE(m1.Matches(42));367EXPECT_FALSE(m1.Matches(43));368EXPECT_EQ("is even", Describe(m1));369370const Matcher<int> not_m1 = Not(m1);371EXPECT_EQ("is odd", Describe(not_m1));372373EXPECT_EQ("% 2 == 0", Explain(m1, 42));374375// Using PolymorphicIsEven() as a Matcher<char>.376const Matcher<char> m2 = PolymorphicIsEven();377EXPECT_TRUE(m2.Matches('\x42'));378EXPECT_FALSE(m2.Matches('\x43'));379EXPECT_EQ("is even", Describe(m2));380381const Matcher<char> not_m2 = Not(m2);382EXPECT_EQ("is odd", Describe(not_m2));383384EXPECT_EQ("% 2 == 0", Explain(m2, '\x42'));385}386387INSTANTIATE_GTEST_MATCHER_TEST_P(MatcherCastTest);388389// Tests that MatcherCast<T>(m) works when m is a polymorphic matcher.390TEST_P(MatcherCastTestP, FromPolymorphicMatcher) {391Matcher<int16_t> m;392if (use_gtest_matcher_) {393m = MatcherCast<int16_t>(GtestGreaterThan(int64_t{5}));394} else {395m = MatcherCast<int16_t>(Gt(int64_t{5}));396}397EXPECT_TRUE(m.Matches(6));398EXPECT_FALSE(m.Matches(4));399}400401// For testing casting matchers between compatible types.402class IntValue {403public:404// An int can be statically (although not implicitly) cast to a405// IntValue.406explicit IntValue(int a_value) : value_(a_value) {}407408int value() const { return value_; }409410private:411int value_;412};413414// For testing casting matchers between compatible types. This is similar to415// IntValue, but takes a non-const reference to the value, showing MatcherCast416// works with such types (and doesn't, for example, use a const ref internally).417class MutableIntView {418public:419// An int& can be statically (although not implicitly) cast to a420// MutableIntView.421explicit MutableIntView(int& a_value) : value_(a_value) {}422423int& value() const { return value_; }424425private:426int& value_;427};428429// For testing casting matchers between compatible types.430bool IsPositiveIntValue(const IntValue& foo) { return foo.value() > 0; }431432// For testing casting matchers between compatible types.433bool IsPositiveMutableIntView(MutableIntView foo) { return foo.value() > 0; }434435// Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T436// can be statically converted to U.437TEST(MatcherCastTest, FromCompatibleType) {438Matcher<double> m1 = Eq(2.0);439Matcher<int> m2 = MatcherCast<int>(m1);440EXPECT_TRUE(m2.Matches(2));441EXPECT_FALSE(m2.Matches(3));442443Matcher<IntValue> m3 = Truly(IsPositiveIntValue);444Matcher<int> m4 = MatcherCast<int>(m3);445// In the following, the arguments 1 and 0 are statically converted446// to IntValue objects, and then tested by the IsPositiveIntValue()447// predicate.448EXPECT_TRUE(m4.Matches(1));449EXPECT_FALSE(m4.Matches(0));450451Matcher<MutableIntView> m5 = Truly(IsPositiveMutableIntView);452Matcher<int> m6 = MatcherCast<int>(m5);453// In the following, the arguments 1 and 0 are statically converted to454// MutableIntView objects, and then tested by the IsPositiveMutableIntView()455// predicate.456EXPECT_TRUE(m6.Matches(1));457EXPECT_FALSE(m6.Matches(0));458}459460// Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>.461TEST(MatcherCastTest, FromConstReferenceToNonReference) {462int n = 0;463Matcher<const int&> m1 = Ref(n);464Matcher<int> m2 = MatcherCast<int>(m1);465int n1 = 0;466EXPECT_TRUE(m2.Matches(n));467EXPECT_FALSE(m2.Matches(n1));468}469470// Tests that MatcherCast<T&>(m) works when m is a Matcher<const T&>.471TEST(MatcherCastTest, FromConstReferenceToReference) {472int n = 0;473Matcher<const int&> m1 = Ref(n);474Matcher<int&> m2 = MatcherCast<int&>(m1);475int n1 = 0;476EXPECT_TRUE(m2.Matches(n));477EXPECT_FALSE(m2.Matches(n1));478}479480// Tests that MatcherCast<T>(m) works when m is a Matcher<T&>.481TEST(MatcherCastTest, FromReferenceToNonReference) {482Matcher<int&> m1 = Eq(0);483Matcher<int> m2 = MatcherCast<int>(m1);484EXPECT_TRUE(m2.Matches(0));485EXPECT_FALSE(m2.Matches(1));486487// Of course, reference identity isn't preserved since a copy is required.488int n = 0;489Matcher<int&> m3 = Ref(n);490Matcher<int> m4 = MatcherCast<int>(m3);491EXPECT_FALSE(m4.Matches(n));492}493494// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.495TEST(MatcherCastTest, FromNonReferenceToConstReference) {496Matcher<int> m1 = Eq(0);497Matcher<const int&> m2 = MatcherCast<const int&>(m1);498EXPECT_TRUE(m2.Matches(0));499EXPECT_FALSE(m2.Matches(1));500}501502// Tests that MatcherCast<T&>(m) works when m is a Matcher<T>.503TEST(MatcherCastTest, FromNonReferenceToReference) {504Matcher<int> m1 = Eq(0);505Matcher<int&> m2 = MatcherCast<int&>(m1);506int n = 0;507EXPECT_TRUE(m2.Matches(n));508n = 1;509EXPECT_FALSE(m2.Matches(n));510}511512// Tests that MatcherCast<T>(m) works when m is a Matcher<T>.513TEST(MatcherCastTest, FromSameType) {514Matcher<int> m1 = Eq(0);515Matcher<int> m2 = MatcherCast<int>(m1);516EXPECT_TRUE(m2.Matches(0));517EXPECT_FALSE(m2.Matches(1));518}519520// Tests that MatcherCast<T>(m) works when m is a value of the same type as the521// value type of the Matcher.522TEST(MatcherCastTest, FromAValue) {523Matcher<int> m = MatcherCast<int>(42);524EXPECT_TRUE(m.Matches(42));525EXPECT_FALSE(m.Matches(239));526}527528// Tests that MatcherCast<T>(m) works when m is a value of the type implicitly529// convertible to the value type of the Matcher.530TEST(MatcherCastTest, FromAnImplicitlyConvertibleValue) {531const int kExpected = 'c';532Matcher<int> m = MatcherCast<int>('c');533EXPECT_TRUE(m.Matches(kExpected));534EXPECT_FALSE(m.Matches(kExpected + 1));535}536537struct NonImplicitlyConstructibleTypeWithOperatorEq {538friend bool operator==(539const NonImplicitlyConstructibleTypeWithOperatorEq& /* ignored */,540int rhs) {541return 42 == rhs;542}543friend bool operator==(544int lhs,545const NonImplicitlyConstructibleTypeWithOperatorEq& /* ignored */) {546return lhs == 42;547}548};549550// Tests that MatcherCast<T>(m) works when m is a neither a matcher nor551// implicitly convertible to the value type of the Matcher, but the value type552// of the matcher has operator==() overload accepting m.553TEST(MatcherCastTest, NonImplicitlyConstructibleTypeWithOperatorEq) {554Matcher<NonImplicitlyConstructibleTypeWithOperatorEq> m1 =555MatcherCast<NonImplicitlyConstructibleTypeWithOperatorEq>(42);556EXPECT_TRUE(m1.Matches(NonImplicitlyConstructibleTypeWithOperatorEq()));557558Matcher<NonImplicitlyConstructibleTypeWithOperatorEq> m2 =559MatcherCast<NonImplicitlyConstructibleTypeWithOperatorEq>(239);560EXPECT_FALSE(m2.Matches(NonImplicitlyConstructibleTypeWithOperatorEq()));561562// When updating the following lines please also change the comment to563// namespace convertible_from_any.564Matcher<int> m3 =565MatcherCast<int>(NonImplicitlyConstructibleTypeWithOperatorEq());566EXPECT_TRUE(m3.Matches(42));567EXPECT_FALSE(m3.Matches(239));568}569570// ConvertibleFromAny does not work with MSVC. resulting in571// error C2440: 'initializing': cannot convert from 'Eq' to 'M'572// No constructor could take the source type, or constructor overload573// resolution was ambiguous574575#if !defined _MSC_VER576577// The below ConvertibleFromAny struct is implicitly constructible from anything578// and when in the same namespace can interact with other tests. In particular,579// if it is in the same namespace as other tests and one removes580// NonImplicitlyConstructibleTypeWithOperatorEq::operator==(int lhs, ...);581// then the corresponding test still compiles (and it should not!) by implicitly582// converting NonImplicitlyConstructibleTypeWithOperatorEq to ConvertibleFromAny583// in m3.Matcher().584namespace convertible_from_any {585// Implicitly convertible from any type.586struct ConvertibleFromAny {587ConvertibleFromAny(int a_value) : value(a_value) {}588template <typename T>589ConvertibleFromAny(const T& /*a_value*/) : value(-1) {590ADD_FAILURE() << "Conversion constructor called";591}592int value;593};594595bool operator==(const ConvertibleFromAny& a, const ConvertibleFromAny& b) {596return a.value == b.value;597}598599ostream& operator<<(ostream& os, const ConvertibleFromAny& a) {600return os << a.value;601}602603TEST(MatcherCastTest, ConversionConstructorIsUsed) {604Matcher<ConvertibleFromAny> m = MatcherCast<ConvertibleFromAny>(1);605EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));606EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));607}608609TEST(MatcherCastTest, FromConvertibleFromAny) {610Matcher<ConvertibleFromAny> m =611MatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));612EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));613EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));614}615} // namespace convertible_from_any616617#endif // !defined _MSC_VER618619struct IntReferenceWrapper {620IntReferenceWrapper(const int& a_value) : value(&a_value) {}621const int* value;622};623624bool operator==(const IntReferenceWrapper& a, const IntReferenceWrapper& b) {625return a.value == b.value;626}627628TEST(MatcherCastTest, ValueIsNotCopied) {629int n = 42;630Matcher<IntReferenceWrapper> m = MatcherCast<IntReferenceWrapper>(n);631// Verify that the matcher holds a reference to n, not to its temporary copy.632EXPECT_TRUE(m.Matches(n));633}634635class Base {636public:637virtual ~Base() = default;638Base() = default;639640private:641Base(const Base&) = delete;642Base& operator=(const Base&) = delete;643};644645class Derived : public Base {646public:647Derived() : Base() {}648int i;649};650651class OtherDerived : public Base {};652653INSTANTIATE_GTEST_MATCHER_TEST_P(SafeMatcherCastTest);654655// Tests that SafeMatcherCast<T>(m) works when m is a polymorphic matcher.656TEST_P(SafeMatcherCastTestP, FromPolymorphicMatcher) {657Matcher<char> m2;658if (use_gtest_matcher_) {659m2 = SafeMatcherCast<char>(GtestGreaterThan(32));660} else {661m2 = SafeMatcherCast<char>(Gt(32));662}663EXPECT_TRUE(m2.Matches('A'));664EXPECT_FALSE(m2.Matches('\n'));665}666667// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where668// T and U are arithmetic types and T can be losslessly converted to669// U.670TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {671Matcher<double> m1 = DoubleEq(1.0);672Matcher<float> m2 = SafeMatcherCast<float>(m1);673EXPECT_TRUE(m2.Matches(1.0f));674EXPECT_FALSE(m2.Matches(2.0f));675676Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>('a'));677EXPECT_TRUE(m3.Matches('a'));678EXPECT_FALSE(m3.Matches('b'));679}680681// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where T and U682// are pointers or references to a derived and a base class, correspondingly.683TEST(SafeMatcherCastTest, FromBaseClass) {684Derived d, d2;685Matcher<Base*> m1 = Eq(&d);686Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);687EXPECT_TRUE(m2.Matches(&d));688EXPECT_FALSE(m2.Matches(&d2));689690Matcher<Base&> m3 = Ref(d);691Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);692EXPECT_TRUE(m4.Matches(d));693EXPECT_FALSE(m4.Matches(d2));694}695696// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<const T&>.697TEST(SafeMatcherCastTest, FromConstReferenceToNonReference) {698int n = 0;699Matcher<const int&> m1 = Ref(n);700Matcher<int> m2 = SafeMatcherCast<int>(m1);701int n1 = 0;702EXPECT_TRUE(m2.Matches(n));703EXPECT_FALSE(m2.Matches(n1));704}705706// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>.707TEST(SafeMatcherCastTest, FromConstReferenceToReference) {708int n = 0;709Matcher<const int&> m1 = Ref(n);710Matcher<int&> m2 = SafeMatcherCast<int&>(m1);711int n1 = 0;712EXPECT_TRUE(m2.Matches(n));713EXPECT_FALSE(m2.Matches(n1));714}715716// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.717TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {718Matcher<std::unique_ptr<int>> m1 = IsNull();719Matcher<const std::unique_ptr<int>&> m2 =720SafeMatcherCast<const std::unique_ptr<int>&>(m1);721EXPECT_TRUE(m2.Matches(std::unique_ptr<int>()));722EXPECT_FALSE(m2.Matches(std::unique_ptr<int>(new int)));723}724725// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<T>.726TEST(SafeMatcherCastTest, FromNonReferenceToReference) {727Matcher<int> m1 = Eq(0);728Matcher<int&> m2 = SafeMatcherCast<int&>(m1);729int n = 0;730EXPECT_TRUE(m2.Matches(n));731n = 1;732EXPECT_FALSE(m2.Matches(n));733}734735// Tests that SafeMatcherCast<T>(m) works when m is a Matcher<T>.736TEST(SafeMatcherCastTest, FromSameType) {737Matcher<int> m1 = Eq(0);738Matcher<int> m2 = SafeMatcherCast<int>(m1);739EXPECT_TRUE(m2.Matches(0));740EXPECT_FALSE(m2.Matches(1));741}742743#if !defined _MSC_VER744745namespace convertible_from_any {746TEST(SafeMatcherCastTest, ConversionConstructorIsUsed) {747Matcher<ConvertibleFromAny> m = SafeMatcherCast<ConvertibleFromAny>(1);748EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));749EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));750}751752TEST(SafeMatcherCastTest, FromConvertibleFromAny) {753Matcher<ConvertibleFromAny> m =754SafeMatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));755EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));756EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));757}758} // namespace convertible_from_any759760#endif // !defined _MSC_VER761762TEST(SafeMatcherCastTest, ValueIsNotCopied) {763int n = 42;764Matcher<IntReferenceWrapper> m = SafeMatcherCast<IntReferenceWrapper>(n);765// Verify that the matcher holds a reference to n, not to its temporary copy.766EXPECT_TRUE(m.Matches(n));767}768769TEST(ExpectThat, TakesLiterals) {770EXPECT_THAT(1, 1);771EXPECT_THAT(1.0, 1.0);772EXPECT_THAT(std::string(), "");773}774775TEST(ExpectThat, TakesFunctions) {776struct Helper {777static void Func() {}778};779void (*func)() = Helper::Func;780EXPECT_THAT(func, Helper::Func);781EXPECT_THAT(func, &Helper::Func);782}783784// Tests that A<T>() matches any value of type T.785TEST(ATest, MatchesAnyValue) {786// Tests a matcher for a value type.787Matcher<double> m1 = A<double>();788EXPECT_TRUE(m1.Matches(91.43));789EXPECT_TRUE(m1.Matches(-15.32));790791// Tests a matcher for a reference type.792int a = 2;793int b = -6;794Matcher<int&> m2 = A<int&>();795EXPECT_TRUE(m2.Matches(a));796EXPECT_TRUE(m2.Matches(b));797}798799TEST(ATest, WorksForDerivedClass) {800Base base;801Derived derived;802EXPECT_THAT(&base, A<Base*>());803// This shouldn't compile: EXPECT_THAT(&base, A<Derived*>());804EXPECT_THAT(&derived, A<Base*>());805EXPECT_THAT(&derived, A<Derived*>());806}807808// Tests that A<T>() describes itself properly.809TEST(ATest, CanDescribeSelf) { EXPECT_EQ("is anything", Describe(A<bool>())); }810811// Tests that An<T>() matches any value of type T.812TEST(AnTest, MatchesAnyValue) {813// Tests a matcher for a value type.814Matcher<int> m1 = An<int>();815EXPECT_TRUE(m1.Matches(9143));816EXPECT_TRUE(m1.Matches(-1532));817818// Tests a matcher for a reference type.819int a = 2;820int b = -6;821Matcher<int&> m2 = An<int&>();822EXPECT_TRUE(m2.Matches(a));823EXPECT_TRUE(m2.Matches(b));824}825826// Tests that An<T>() describes itself properly.827TEST(AnTest, CanDescribeSelf) { EXPECT_EQ("is anything", Describe(An<int>())); }828829// Tests that _ can be used as a matcher for any type and matches any830// value of that type.831TEST(UnderscoreTest, MatchesAnyValue) {832// Uses _ as a matcher for a value type.833Matcher<int> m1 = _;834EXPECT_TRUE(m1.Matches(123));835EXPECT_TRUE(m1.Matches(-242));836837// Uses _ as a matcher for a reference type.838bool a = false;839const bool b = true;840Matcher<const bool&> m2 = _;841EXPECT_TRUE(m2.Matches(a));842EXPECT_TRUE(m2.Matches(b));843}844845// Tests that _ describes itself properly.846TEST(UnderscoreTest, CanDescribeSelf) {847Matcher<int> m = _;848EXPECT_EQ("is anything", Describe(m));849}850851// Tests that Eq(x) matches any value equal to x.852TEST(EqTest, MatchesEqualValue) {853// 2 C-strings with same content but different addresses.854const char a1[] = "hi";855const char a2[] = "hi";856857Matcher<const char*> m1 = Eq(a1);858EXPECT_TRUE(m1.Matches(a1));859EXPECT_FALSE(m1.Matches(a2));860}861862// Tests that Eq(v) describes itself properly.863864class Unprintable {865public:866Unprintable() : c_('a') {}867868bool operator==(const Unprintable& /* rhs */) const { return true; }869// -Wunused-private-field: dummy accessor for `c_`.870char dummy_c() { return c_; }871872private:873char c_;874};875876TEST(EqTest, CanDescribeSelf) {877Matcher<Unprintable> m = Eq(Unprintable());878EXPECT_EQ("is equal to 1-byte object <61>", Describe(m));879}880881// Tests that Eq(v) can be used to match any type that supports882// comparing with type T, where T is v's type.883TEST(EqTest, IsPolymorphic) {884Matcher<int> m1 = Eq(1);885EXPECT_TRUE(m1.Matches(1));886EXPECT_FALSE(m1.Matches(2));887888Matcher<char> m2 = Eq(1);889EXPECT_TRUE(m2.Matches('\1'));890EXPECT_FALSE(m2.Matches('a'));891}892893// Tests that TypedEq<T>(v) matches values of type T that's equal to v.894TEST(TypedEqTest, ChecksEqualityForGivenType) {895Matcher<char> m1 = TypedEq<char>('a');896EXPECT_TRUE(m1.Matches('a'));897EXPECT_FALSE(m1.Matches('b'));898899Matcher<int> m2 = TypedEq<int>(6);900EXPECT_TRUE(m2.Matches(6));901EXPECT_FALSE(m2.Matches(7));902}903904// Tests that TypedEq(v) describes itself properly.905TEST(TypedEqTest, CanDescribeSelf) {906EXPECT_EQ("is equal to 2", Describe(TypedEq<int>(2)));907}908909// Tests that TypedEq<T>(v) has type Matcher<T>.910911// Type<T>::IsTypeOf(v) compiles if and only if the type of value v is T, where912// T is a "bare" type (i.e. not in the form of const U or U&). If v's type is913// not T, the compiler will generate a message about "undefined reference".914template <typename T>915struct Type {916static bool IsTypeOf(const T& /* v */) { return true; }917918template <typename T2>919static void IsTypeOf(T2 v);920};921922TEST(TypedEqTest, HasSpecifiedType) {923// Verifies that the type of TypedEq<T>(v) is Matcher<T>.924Type<Matcher<int>>::IsTypeOf(TypedEq<int>(5));925Type<Matcher<double>>::IsTypeOf(TypedEq<double>(5));926}927928// Tests that Ge(v) matches anything >= v.929TEST(GeTest, ImplementsGreaterThanOrEqual) {930Matcher<int> m1 = Ge(0);931EXPECT_TRUE(m1.Matches(1));932EXPECT_TRUE(m1.Matches(0));933EXPECT_FALSE(m1.Matches(-1));934}935936// Tests that Ge(v) describes itself properly.937TEST(GeTest, CanDescribeSelf) {938Matcher<int> m = Ge(5);939EXPECT_EQ("is >= 5", Describe(m));940}941942// Tests that Gt(v) matches anything > v.943TEST(GtTest, ImplementsGreaterThan) {944Matcher<double> m1 = Gt(0);945EXPECT_TRUE(m1.Matches(1.0));946EXPECT_FALSE(m1.Matches(0.0));947EXPECT_FALSE(m1.Matches(-1.0));948}949950// Tests that Gt(v) describes itself properly.951TEST(GtTest, CanDescribeSelf) {952Matcher<int> m = Gt(5);953EXPECT_EQ("is > 5", Describe(m));954}955956// Tests that Le(v) matches anything <= v.957TEST(LeTest, ImplementsLessThanOrEqual) {958Matcher<char> m1 = Le('b');959EXPECT_TRUE(m1.Matches('a'));960EXPECT_TRUE(m1.Matches('b'));961EXPECT_FALSE(m1.Matches('c'));962}963964// Tests that Le(v) describes itself properly.965TEST(LeTest, CanDescribeSelf) {966Matcher<int> m = Le(5);967EXPECT_EQ("is <= 5", Describe(m));968}969970// Tests that Lt(v) matches anything < v.971TEST(LtTest, ImplementsLessThan) {972Matcher<const std::string&> m1 = Lt("Hello");973EXPECT_TRUE(m1.Matches("Abc"));974EXPECT_FALSE(m1.Matches("Hello"));975EXPECT_FALSE(m1.Matches("Hello, world!"));976}977978// Tests that Lt(v) describes itself properly.979TEST(LtTest, CanDescribeSelf) {980Matcher<int> m = Lt(5);981EXPECT_EQ("is < 5", Describe(m));982}983984// Tests that Ne(v) matches anything != v.985TEST(NeTest, ImplementsNotEqual) {986Matcher<int> m1 = Ne(0);987EXPECT_TRUE(m1.Matches(1));988EXPECT_TRUE(m1.Matches(-1));989EXPECT_FALSE(m1.Matches(0));990}991992// Tests that Ne(v) describes itself properly.993TEST(NeTest, CanDescribeSelf) {994Matcher<int> m = Ne(5);995EXPECT_EQ("isn't equal to 5", Describe(m));996}997998class MoveOnly {999public:1000explicit MoveOnly(int i) : i_(i) {}1001MoveOnly(const MoveOnly&) = delete;1002MoveOnly(MoveOnly&&) = default;1003MoveOnly& operator=(const MoveOnly&) = delete;1004MoveOnly& operator=(MoveOnly&&) = default;10051006bool operator==(const MoveOnly& other) const { return i_ == other.i_; }1007bool operator!=(const MoveOnly& other) const { return i_ != other.i_; }1008bool operator<(const MoveOnly& other) const { return i_ < other.i_; }1009bool operator<=(const MoveOnly& other) const { return i_ <= other.i_; }1010bool operator>(const MoveOnly& other) const { return i_ > other.i_; }1011bool operator>=(const MoveOnly& other) const { return i_ >= other.i_; }10121013private:1014int i_;1015};10161017struct MoveHelper {1018MOCK_METHOD1(Call, void(MoveOnly));1019};10201021// Disable this test in VS 2015 (version 14), where it fails when SEH is enabled1022#if defined(_MSC_VER) && (_MSC_VER < 1910)1023TEST(ComparisonBaseTest, DISABLED_WorksWithMoveOnly) {1024#else1025TEST(ComparisonBaseTest, WorksWithMoveOnly) {1026#endif1027MoveOnly m{0};1028MoveHelper helper;10291030EXPECT_CALL(helper, Call(Eq(ByRef(m))));1031helper.Call(MoveOnly(0));1032EXPECT_CALL(helper, Call(Ne(ByRef(m))));1033helper.Call(MoveOnly(1));1034EXPECT_CALL(helper, Call(Le(ByRef(m))));1035helper.Call(MoveOnly(0));1036EXPECT_CALL(helper, Call(Lt(ByRef(m))));1037helper.Call(MoveOnly(-1));1038EXPECT_CALL(helper, Call(Ge(ByRef(m))));1039helper.Call(MoveOnly(0));1040EXPECT_CALL(helper, Call(Gt(ByRef(m))));1041helper.Call(MoveOnly(1));1042}10431044TEST(IsEmptyTest, MatchesContainer) {1045const Matcher<std::vector<int>> m = IsEmpty();1046std::vector<int> a = {};1047std::vector<int> b = {1};1048EXPECT_TRUE(m.Matches(a));1049EXPECT_FALSE(m.Matches(b));1050}10511052TEST(IsEmptyTest, MatchesStdString) {1053const Matcher<std::string> m = IsEmpty();1054std::string a = "z";1055std::string b = "";1056EXPECT_FALSE(m.Matches(a));1057EXPECT_TRUE(m.Matches(b));1058}10591060TEST(IsEmptyTest, MatchesCString) {1061const Matcher<const char*> m = IsEmpty();1062const char a[] = "";1063const char b[] = "x";1064EXPECT_TRUE(m.Matches(a));1065EXPECT_FALSE(m.Matches(b));1066}10671068// Tests that IsNull() matches any NULL pointer of any type.1069TEST(IsNullTest, MatchesNullPointer) {1070Matcher<int*> m1 = IsNull();1071int* p1 = nullptr;1072int n = 0;1073EXPECT_TRUE(m1.Matches(p1));1074EXPECT_FALSE(m1.Matches(&n));10751076Matcher<const char*> m2 = IsNull();1077const char* p2 = nullptr;1078EXPECT_TRUE(m2.Matches(p2));1079EXPECT_FALSE(m2.Matches("hi"));10801081Matcher<void*> m3 = IsNull();1082void* p3 = nullptr;1083EXPECT_TRUE(m3.Matches(p3));1084EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef)));1085}10861087TEST(IsNullTest, StdFunction) {1088const Matcher<std::function<void()>> m = IsNull();10891090EXPECT_TRUE(m.Matches(std::function<void()>()));1091EXPECT_FALSE(m.Matches([] {}));1092}10931094// Tests that IsNull() describes itself properly.1095TEST(IsNullTest, CanDescribeSelf) {1096Matcher<int*> m = IsNull();1097EXPECT_EQ("is NULL", Describe(m));1098EXPECT_EQ("isn't NULL", DescribeNegation(m));1099}11001101// Tests that NotNull() matches any non-NULL pointer of any type.1102TEST(NotNullTest, MatchesNonNullPointer) {1103Matcher<int*> m1 = NotNull();1104int* p1 = nullptr;1105int n = 0;1106EXPECT_FALSE(m1.Matches(p1));1107EXPECT_TRUE(m1.Matches(&n));11081109Matcher<const char*> m2 = NotNull();1110const char* p2 = nullptr;1111EXPECT_FALSE(m2.Matches(p2));1112EXPECT_TRUE(m2.Matches("hi"));1113}11141115TEST(NotNullTest, LinkedPtr) {1116const Matcher<std::shared_ptr<int>> m = NotNull();1117const std::shared_ptr<int> null_p;1118const std::shared_ptr<int> non_null_p(new int);11191120EXPECT_FALSE(m.Matches(null_p));1121EXPECT_TRUE(m.Matches(non_null_p));1122}11231124TEST(NotNullTest, ReferenceToConstLinkedPtr) {1125const Matcher<const std::shared_ptr<double>&> m = NotNull();1126const std::shared_ptr<double> null_p;1127const std::shared_ptr<double> non_null_p(new double);11281129EXPECT_FALSE(m.Matches(null_p));1130EXPECT_TRUE(m.Matches(non_null_p));1131}11321133TEST(NotNullTest, StdFunction) {1134const Matcher<std::function<void()>> m = NotNull();11351136EXPECT_TRUE(m.Matches([] {}));1137EXPECT_FALSE(m.Matches(std::function<void()>()));1138}11391140// Tests that NotNull() describes itself properly.1141TEST(NotNullTest, CanDescribeSelf) {1142Matcher<int*> m = NotNull();1143EXPECT_EQ("isn't NULL", Describe(m));1144}11451146// Tests that Ref(variable) matches an argument that references1147// 'variable'.1148TEST(RefTest, MatchesSameVariable) {1149int a = 0;1150int b = 0;1151Matcher<int&> m = Ref(a);1152EXPECT_TRUE(m.Matches(a));1153EXPECT_FALSE(m.Matches(b));1154}11551156// Tests that Ref(variable) describes itself properly.1157TEST(RefTest, CanDescribeSelf) {1158int n = 5;1159Matcher<int&> m = Ref(n);1160stringstream ss;1161ss << "references the variable @" << &n << " 5";1162EXPECT_EQ(ss.str(), Describe(m));1163}11641165// Test that Ref(non_const_varialbe) can be used as a matcher for a1166// const reference.1167TEST(RefTest, CanBeUsedAsMatcherForConstReference) {1168int a = 0;1169int b = 0;1170Matcher<const int&> m = Ref(a);1171EXPECT_TRUE(m.Matches(a));1172EXPECT_FALSE(m.Matches(b));1173}11741175// Tests that Ref(variable) is covariant, i.e. Ref(derived) can be1176// used wherever Ref(base) can be used (Ref(derived) is a sub-type1177// of Ref(base), but not vice versa.11781179TEST(RefTest, IsCovariant) {1180Base base, base2;1181Derived derived;1182Matcher<const Base&> m1 = Ref(base);1183EXPECT_TRUE(m1.Matches(base));1184EXPECT_FALSE(m1.Matches(base2));1185EXPECT_FALSE(m1.Matches(derived));11861187m1 = Ref(derived);1188EXPECT_TRUE(m1.Matches(derived));1189EXPECT_FALSE(m1.Matches(base));1190EXPECT_FALSE(m1.Matches(base2));1191}11921193TEST(RefTest, ExplainsResult) {1194int n = 0;1195EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n),1196StartsWith("which is located @"));11971198int m = 0;1199EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m),1200StartsWith("which is located @"));1201}12021203// Tests string comparison matchers.12041205template <typename T = std::string>1206std::string FromStringLike(internal::StringLike<T> str) {1207return std::string(str);1208}12091210TEST(StringLike, TestConversions) {1211EXPECT_EQ("foo", FromStringLike("foo"));1212EXPECT_EQ("foo", FromStringLike(std::string("foo")));1213#if GTEST_INTERNAL_HAS_STRING_VIEW1214EXPECT_EQ("foo", FromStringLike(internal::StringView("foo")));1215#endif // GTEST_INTERNAL_HAS_STRING_VIEW12161217// Non deducible types.1218EXPECT_EQ("", FromStringLike({}));1219EXPECT_EQ("foo", FromStringLike({'f', 'o', 'o'}));1220const char buf[] = "foo";1221EXPECT_EQ("foo", FromStringLike({buf, buf + 3}));1222}12231224TEST(StrEqTest, MatchesEqualString) {1225Matcher<const char*> m = StrEq(std::string("Hello"));1226EXPECT_TRUE(m.Matches("Hello"));1227EXPECT_FALSE(m.Matches("hello"));1228EXPECT_FALSE(m.Matches(nullptr));12291230Matcher<const std::string&> m2 = StrEq("Hello");1231EXPECT_TRUE(m2.Matches("Hello"));1232EXPECT_FALSE(m2.Matches("Hi"));12331234#if GTEST_INTERNAL_HAS_STRING_VIEW1235Matcher<const internal::StringView&> m3 =1236StrEq(internal::StringView("Hello"));1237EXPECT_TRUE(m3.Matches(internal::StringView("Hello")));1238EXPECT_FALSE(m3.Matches(internal::StringView("hello")));1239EXPECT_FALSE(m3.Matches(internal::StringView()));12401241Matcher<const internal::StringView&> m_empty = StrEq("");1242EXPECT_TRUE(m_empty.Matches(internal::StringView("")));1243EXPECT_TRUE(m_empty.Matches(internal::StringView()));1244EXPECT_FALSE(m_empty.Matches(internal::StringView("hello")));1245#endif // GTEST_INTERNAL_HAS_STRING_VIEW1246}12471248TEST(StrEqTest, CanDescribeSelf) {1249Matcher<std::string> m = StrEq("Hi-\'\"?\\\a\b\f\n\r\t\v\xD3");1250EXPECT_EQ("is equal to \"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",1251Describe(m));12521253std::string str("01204500800");1254str[3] = '\0';1255Matcher<std::string> m2 = StrEq(str);1256EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2));1257str[0] = str[6] = str[7] = str[9] = str[10] = '\0';1258Matcher<std::string> m3 = StrEq(str);1259EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));1260}12611262TEST(StrNeTest, MatchesUnequalString) {1263Matcher<const char*> m = StrNe("Hello");1264EXPECT_TRUE(m.Matches(""));1265EXPECT_TRUE(m.Matches(nullptr));1266EXPECT_FALSE(m.Matches("Hello"));12671268Matcher<std::string> m2 = StrNe(std::string("Hello"));1269EXPECT_TRUE(m2.Matches("hello"));1270EXPECT_FALSE(m2.Matches("Hello"));12711272#if GTEST_INTERNAL_HAS_STRING_VIEW1273Matcher<const internal::StringView> m3 = StrNe(internal::StringView("Hello"));1274EXPECT_TRUE(m3.Matches(internal::StringView("")));1275EXPECT_TRUE(m3.Matches(internal::StringView()));1276EXPECT_FALSE(m3.Matches(internal::StringView("Hello")));1277#endif // GTEST_INTERNAL_HAS_STRING_VIEW1278}12791280TEST(StrNeTest, CanDescribeSelf) {1281Matcher<const char*> m = StrNe("Hi");1282EXPECT_EQ("isn't equal to \"Hi\"", Describe(m));1283}12841285TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {1286Matcher<const char*> m = StrCaseEq(std::string("Hello"));1287EXPECT_TRUE(m.Matches("Hello"));1288EXPECT_TRUE(m.Matches("hello"));1289EXPECT_FALSE(m.Matches("Hi"));1290EXPECT_FALSE(m.Matches(nullptr));12911292Matcher<const std::string&> m2 = StrCaseEq("Hello");1293EXPECT_TRUE(m2.Matches("hello"));1294EXPECT_FALSE(m2.Matches("Hi"));12951296#if GTEST_INTERNAL_HAS_STRING_VIEW1297Matcher<const internal::StringView&> m3 =1298StrCaseEq(internal::StringView("Hello"));1299EXPECT_TRUE(m3.Matches(internal::StringView("Hello")));1300EXPECT_TRUE(m3.Matches(internal::StringView("hello")));1301EXPECT_FALSE(m3.Matches(internal::StringView("Hi")));1302EXPECT_FALSE(m3.Matches(internal::StringView()));1303#endif // GTEST_INTERNAL_HAS_STRING_VIEW1304}13051306TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {1307std::string str1("oabocdooeoo");1308std::string str2("OABOCDOOEOO");1309Matcher<const std::string&> m0 = StrCaseEq(str1);1310EXPECT_FALSE(m0.Matches(str2 + std::string(1, '\0')));13111312str1[3] = str2[3] = '\0';1313Matcher<const std::string&> m1 = StrCaseEq(str1);1314EXPECT_TRUE(m1.Matches(str2));13151316str1[0] = str1[6] = str1[7] = str1[10] = '\0';1317str2[0] = str2[6] = str2[7] = str2[10] = '\0';1318Matcher<const std::string&> m2 = StrCaseEq(str1);1319str1[9] = str2[9] = '\0';1320EXPECT_FALSE(m2.Matches(str2));13211322Matcher<const std::string&> m3 = StrCaseEq(str1);1323EXPECT_TRUE(m3.Matches(str2));13241325EXPECT_FALSE(m3.Matches(str2 + "x"));1326str2.append(1, '\0');1327EXPECT_FALSE(m3.Matches(str2));1328EXPECT_FALSE(m3.Matches(std::string(str2, 0, 9)));1329}13301331TEST(StrCaseEqTest, CanDescribeSelf) {1332Matcher<std::string> m = StrCaseEq("Hi");1333EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m));1334}13351336TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {1337Matcher<const char*> m = StrCaseNe("Hello");1338EXPECT_TRUE(m.Matches("Hi"));1339EXPECT_TRUE(m.Matches(nullptr));1340EXPECT_FALSE(m.Matches("Hello"));1341EXPECT_FALSE(m.Matches("hello"));13421343Matcher<std::string> m2 = StrCaseNe(std::string("Hello"));1344EXPECT_TRUE(m2.Matches(""));1345EXPECT_FALSE(m2.Matches("Hello"));13461347#if GTEST_INTERNAL_HAS_STRING_VIEW1348Matcher<const internal::StringView> m3 =1349StrCaseNe(internal::StringView("Hello"));1350EXPECT_TRUE(m3.Matches(internal::StringView("Hi")));1351EXPECT_TRUE(m3.Matches(internal::StringView()));1352EXPECT_FALSE(m3.Matches(internal::StringView("Hello")));1353EXPECT_FALSE(m3.Matches(internal::StringView("hello")));1354#endif // GTEST_INTERNAL_HAS_STRING_VIEW1355}13561357TEST(StrCaseNeTest, CanDescribeSelf) {1358Matcher<const char*> m = StrCaseNe("Hi");1359EXPECT_EQ("isn't equal to (ignoring case) \"Hi\"", Describe(m));1360}13611362// Tests that HasSubstr() works for matching string-typed values.1363TEST(HasSubstrTest, WorksForStringClasses) {1364const Matcher<std::string> m1 = HasSubstr("foo");1365EXPECT_TRUE(m1.Matches(std::string("I love food.")));1366EXPECT_FALSE(m1.Matches(std::string("tofo")));13671368const Matcher<const std::string&> m2 = HasSubstr("foo");1369EXPECT_TRUE(m2.Matches(std::string("I love food.")));1370EXPECT_FALSE(m2.Matches(std::string("tofo")));13711372const Matcher<std::string> m_empty = HasSubstr("");1373EXPECT_TRUE(m_empty.Matches(std::string()));1374EXPECT_TRUE(m_empty.Matches(std::string("not empty")));1375}13761377// Tests that HasSubstr() works for matching C-string-typed values.1378TEST(HasSubstrTest, WorksForCStrings) {1379const Matcher<char*> m1 = HasSubstr("foo");1380EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food.")));1381EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo")));1382EXPECT_FALSE(m1.Matches(nullptr));13831384const Matcher<const char*> m2 = HasSubstr("foo");1385EXPECT_TRUE(m2.Matches("I love food."));1386EXPECT_FALSE(m2.Matches("tofo"));1387EXPECT_FALSE(m2.Matches(nullptr));13881389const Matcher<const char*> m_empty = HasSubstr("");1390EXPECT_TRUE(m_empty.Matches("not empty"));1391EXPECT_TRUE(m_empty.Matches(""));1392EXPECT_FALSE(m_empty.Matches(nullptr));1393}13941395#if GTEST_INTERNAL_HAS_STRING_VIEW1396// Tests that HasSubstr() works for matching StringView-typed values.1397TEST(HasSubstrTest, WorksForStringViewClasses) {1398const Matcher<internal::StringView> m1 =1399HasSubstr(internal::StringView("foo"));1400EXPECT_TRUE(m1.Matches(internal::StringView("I love food.")));1401EXPECT_FALSE(m1.Matches(internal::StringView("tofo")));1402EXPECT_FALSE(m1.Matches(internal::StringView()));14031404const Matcher<const internal::StringView&> m2 = HasSubstr("foo");1405EXPECT_TRUE(m2.Matches(internal::StringView("I love food.")));1406EXPECT_FALSE(m2.Matches(internal::StringView("tofo")));1407EXPECT_FALSE(m2.Matches(internal::StringView()));14081409const Matcher<const internal::StringView&> m3 = HasSubstr("");1410EXPECT_TRUE(m3.Matches(internal::StringView("foo")));1411EXPECT_TRUE(m3.Matches(internal::StringView("")));1412EXPECT_TRUE(m3.Matches(internal::StringView()));1413}1414#endif // GTEST_INTERNAL_HAS_STRING_VIEW14151416// Tests that HasSubstr(s) describes itself properly.1417TEST(HasSubstrTest, CanDescribeSelf) {1418Matcher<std::string> m = HasSubstr("foo\n\"");1419EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m));1420}14211422INSTANTIATE_GTEST_MATCHER_TEST_P(KeyTest);14231424TEST(KeyTest, CanDescribeSelf) {1425Matcher<const pair<std::string, int>&> m = Key("foo");1426EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m));1427EXPECT_EQ("doesn't have a key that is equal to \"foo\"", DescribeNegation(m));1428}14291430TEST_P(KeyTestP, ExplainsResult) {1431Matcher<pair<int, bool>> m = Key(GreaterThan(10));1432EXPECT_EQ("whose first field is a value which is 5 less than 10",1433Explain(m, make_pair(5, true)));1434EXPECT_EQ("whose first field is a value which is 5 more than 10",1435Explain(m, make_pair(15, true)));1436}14371438TEST(KeyTest, MatchesCorrectly) {1439pair<int, std::string> p(25, "foo");1440EXPECT_THAT(p, Key(25));1441EXPECT_THAT(p, Not(Key(42)));1442EXPECT_THAT(p, Key(Ge(20)));1443EXPECT_THAT(p, Not(Key(Lt(25))));1444}14451446TEST(KeyTest, WorksWithMoveOnly) {1447pair<std::unique_ptr<int>, std::unique_ptr<int>> p;1448EXPECT_THAT(p, Key(Eq(nullptr)));1449}14501451INSTANTIATE_GTEST_MATCHER_TEST_P(PairTest);14521453template <size_t I>1454struct Tag {};14551456struct PairWithGet {1457int member_1;1458std::string member_2;1459using first_type = int;1460using second_type = std::string;14611462const int& GetImpl(Tag<0>) const { return member_1; }1463const std::string& GetImpl(Tag<1>) const { return member_2; }1464};1465template <size_t I>1466auto get(const PairWithGet& value) -> decltype(value.GetImpl(Tag<I>())) {1467return value.GetImpl(Tag<I>());1468}1469TEST(PairTest, MatchesPairWithGetCorrectly) {1470PairWithGet p{25, "foo"};1471EXPECT_THAT(p, Key(25));1472EXPECT_THAT(p, Not(Key(42)));1473EXPECT_THAT(p, Key(Ge(20)));1474EXPECT_THAT(p, Not(Key(Lt(25))));14751476std::vector<PairWithGet> v = {{11, "Foo"}, {29, "gMockIsBestMock"}};1477EXPECT_THAT(v, Contains(Key(29)));1478}14791480TEST(KeyTest, SafelyCastsInnerMatcher) {1481Matcher<int> is_positive = Gt(0);1482Matcher<int> is_negative = Lt(0);1483pair<char, bool> p('a', true);1484EXPECT_THAT(p, Key(is_positive));1485EXPECT_THAT(p, Not(Key(is_negative)));1486}14871488TEST(KeyTest, InsideContainsUsingMap) {1489map<int, char> container;1490container.insert(make_pair(1, 'a'));1491container.insert(make_pair(2, 'b'));1492container.insert(make_pair(4, 'c'));1493EXPECT_THAT(container, Contains(Key(1)));1494EXPECT_THAT(container, Not(Contains(Key(3))));1495}14961497TEST(KeyTest, InsideContainsUsingMultimap) {1498multimap<int, char> container;1499container.insert(make_pair(1, 'a'));1500container.insert(make_pair(2, 'b'));1501container.insert(make_pair(4, 'c'));15021503EXPECT_THAT(container, Not(Contains(Key(25))));1504container.insert(make_pair(25, 'd'));1505EXPECT_THAT(container, Contains(Key(25)));1506container.insert(make_pair(25, 'e'));1507EXPECT_THAT(container, Contains(Key(25)));15081509EXPECT_THAT(container, Contains(Key(1)));1510EXPECT_THAT(container, Not(Contains(Key(3))));1511}15121513TEST(PairTest, Typing) {1514// Test verifies the following type conversions can be compiled.1515Matcher<const pair<const char*, int>&> m1 = Pair("foo", 42);1516Matcher<const pair<const char*, int>> m2 = Pair("foo", 42);1517Matcher<pair<const char*, int>> m3 = Pair("foo", 42);15181519Matcher<pair<int, const std::string>> m4 = Pair(25, "42");1520Matcher<pair<const std::string, int>> m5 = Pair("25", 42);1521}15221523TEST(PairTest, CanDescribeSelf) {1524Matcher<const pair<std::string, int>&> m1 = Pair("foo", 42);1525EXPECT_EQ(1526"has a first field that is equal to \"foo\""1527", and has a second field that is equal to 42",1528Describe(m1));1529EXPECT_EQ(1530"has a first field that isn't equal to \"foo\""1531", or has a second field that isn't equal to 42",1532DescribeNegation(m1));1533// Double and triple negation (1 or 2 times not and description of negation).1534Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42));1535EXPECT_EQ(1536"has a first field that isn't equal to 13"1537", and has a second field that is equal to 42",1538DescribeNegation(m2));1539}15401541TEST_P(PairTestP, CanExplainMatchResultTo) {1542// If neither field matches, Pair() should explain about the first1543// field.1544const Matcher<pair<int, int>> m = Pair(GreaterThan(0), GreaterThan(0));1545EXPECT_EQ("whose first field does not match, which is 1 less than 0",1546Explain(m, make_pair(-1, -2)));15471548// If the first field matches but the second doesn't, Pair() should1549// explain about the second field.1550EXPECT_EQ("whose second field does not match, which is 2 less than 0",1551Explain(m, make_pair(1, -2)));15521553// If the first field doesn't match but the second does, Pair()1554// should explain about the first field.1555EXPECT_EQ("whose first field does not match, which is 1 less than 0",1556Explain(m, make_pair(-1, 2)));15571558// If both fields match, Pair() should explain about them both.1559EXPECT_EQ(1560"whose both fields match, where the first field is a value "1561"which is 1 more than 0, and the second field is a value "1562"which is 2 more than 0",1563Explain(m, make_pair(1, 2)));15641565// If only the first match has an explanation, only this explanation should1566// be printed.1567const Matcher<pair<int, int>> explain_first = Pair(GreaterThan(0), 0);1568EXPECT_EQ(1569"whose both fields match, where the first field is a value "1570"which is 1 more than 0",1571Explain(explain_first, make_pair(1, 0)));15721573// If only the second match has an explanation, only this explanation should1574// be printed.1575const Matcher<pair<int, int>> explain_second = Pair(0, GreaterThan(0));1576EXPECT_EQ(1577"whose both fields match, where the second field is a value "1578"which is 1 more than 0",1579Explain(explain_second, make_pair(0, 1)));1580}15811582TEST(PairTest, MatchesCorrectly) {1583pair<int, std::string> p(25, "foo");15841585// Both fields match.1586EXPECT_THAT(p, Pair(25, "foo"));1587EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o")));15881589// 'first' doesn't match, but 'second' matches.1590EXPECT_THAT(p, Not(Pair(42, "foo")));1591EXPECT_THAT(p, Not(Pair(Lt(25), "foo")));15921593// 'first' matches, but 'second' doesn't match.1594EXPECT_THAT(p, Not(Pair(25, "bar")));1595EXPECT_THAT(p, Not(Pair(25, Not("foo"))));15961597// Neither field matches.1598EXPECT_THAT(p, Not(Pair(13, "bar")));1599EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a"))));1600}16011602TEST(PairTest, WorksWithMoveOnly) {1603pair<std::unique_ptr<int>, std::unique_ptr<int>> p;1604p.second = std::make_unique<int>(7);1605EXPECT_THAT(p, Pair(Eq(nullptr), Ne(nullptr)));1606}16071608TEST(PairTest, SafelyCastsInnerMatchers) {1609Matcher<int> is_positive = Gt(0);1610Matcher<int> is_negative = Lt(0);1611pair<char, bool> p('a', true);1612EXPECT_THAT(p, Pair(is_positive, _));1613EXPECT_THAT(p, Not(Pair(is_negative, _)));1614EXPECT_THAT(p, Pair(_, is_positive));1615EXPECT_THAT(p, Not(Pair(_, is_negative)));1616}16171618TEST(PairTest, InsideContainsUsingMap) {1619map<int, char> container;1620container.insert(make_pair(1, 'a'));1621container.insert(make_pair(2, 'b'));1622container.insert(make_pair(4, 'c'));1623EXPECT_THAT(container, Contains(Pair(1, 'a')));1624EXPECT_THAT(container, Contains(Pair(1, _)));1625EXPECT_THAT(container, Contains(Pair(_, 'a')));1626EXPECT_THAT(container, Not(Contains(Pair(3, _))));1627}16281629INSTANTIATE_GTEST_MATCHER_TEST_P(FieldsAreTest);16301631TEST(FieldsAreTest, MatchesCorrectly) {1632std::tuple<int, std::string, double> p(25, "foo", .5);16331634// All fields match.1635EXPECT_THAT(p, FieldsAre(25, "foo", .5));1636EXPECT_THAT(p, FieldsAre(Ge(20), HasSubstr("o"), DoubleEq(.5)));16371638// Some don't match.1639EXPECT_THAT(p, Not(FieldsAre(26, "foo", .5)));1640EXPECT_THAT(p, Not(FieldsAre(25, "fo", .5)));1641EXPECT_THAT(p, Not(FieldsAre(25, "foo", .6)));1642}16431644TEST(FieldsAreTest, CanDescribeSelf) {1645Matcher<const pair<std::string, int>&> m1 = FieldsAre("foo", 42);1646EXPECT_EQ(1647"has field #0 that is equal to \"foo\""1648", and has field #1 that is equal to 42",1649Describe(m1));1650EXPECT_EQ(1651"has field #0 that isn't equal to \"foo\""1652", or has field #1 that isn't equal to 42",1653DescribeNegation(m1));1654}16551656TEST_P(FieldsAreTestP, CanExplainMatchResultTo) {1657// The first one that fails is the one that gives the error.1658Matcher<std::tuple<int, int, int>> m =1659FieldsAre(GreaterThan(0), GreaterThan(0), GreaterThan(0));16601661EXPECT_EQ("whose field #0 does not match, which is 1 less than 0",1662Explain(m, std::make_tuple(-1, -2, -3)));1663EXPECT_EQ("whose field #1 does not match, which is 2 less than 0",1664Explain(m, std::make_tuple(1, -2, -3)));1665EXPECT_EQ("whose field #2 does not match, which is 3 less than 0",1666Explain(m, std::make_tuple(1, 2, -3)));16671668// If they all match, we get a long explanation of success.1669EXPECT_EQ(1670"whose all elements match, "1671"where field #0 is a value which is 1 more than 0"1672", and field #1 is a value which is 2 more than 0"1673", and field #2 is a value which is 3 more than 0",1674Explain(m, std::make_tuple(1, 2, 3)));16751676// Only print those that have an explanation.1677m = FieldsAre(GreaterThan(0), 0, GreaterThan(0));1678EXPECT_EQ(1679"whose all elements match, "1680"where field #0 is a value which is 1 more than 0"1681", and field #2 is a value which is 3 more than 0",1682Explain(m, std::make_tuple(1, 0, 3)));16831684// If only one has an explanation, then print that one.1685m = FieldsAre(0, GreaterThan(0), 0);1686EXPECT_EQ(1687"whose all elements match, "1688"where field #1 is a value which is 1 more than 0",1689Explain(m, std::make_tuple(0, 1, 0)));1690}16911692#if defined(__cpp_structured_bindings) && __cpp_structured_bindings >= 2016061693TEST(FieldsAreTest, StructuredBindings) {1694// testing::FieldsAre can also match aggregates and such with C++17 and up.1695struct MyType {1696int i;1697std::string str;1698};1699EXPECT_THAT((MyType{17, "foo"}), FieldsAre(Eq(17), HasSubstr("oo")));17001701// Test all the supported arities.1702struct MyVarType1 {1703int a;1704};1705EXPECT_THAT(MyVarType1{}, FieldsAre(0));1706struct MyVarType2 {1707int a, b;1708};1709EXPECT_THAT(MyVarType2{}, FieldsAre(0, 0));1710struct MyVarType3 {1711int a, b, c;1712};1713EXPECT_THAT(MyVarType3{}, FieldsAre(0, 0, 0));1714struct MyVarType4 {1715int a, b, c, d;1716};1717EXPECT_THAT(MyVarType4{}, FieldsAre(0, 0, 0, 0));1718struct MyVarType5 {1719int a, b, c, d, e;1720};1721EXPECT_THAT(MyVarType5{}, FieldsAre(0, 0, 0, 0, 0));1722struct MyVarType6 {1723int a, b, c, d, e, f;1724};1725EXPECT_THAT(MyVarType6{}, FieldsAre(0, 0, 0, 0, 0, 0));1726struct MyVarType7 {1727int a, b, c, d, e, f, g;1728};1729EXPECT_THAT(MyVarType7{}, FieldsAre(0, 0, 0, 0, 0, 0, 0));1730struct MyVarType8 {1731int a, b, c, d, e, f, g, h;1732};1733EXPECT_THAT(MyVarType8{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0));1734struct MyVarType9 {1735int a, b, c, d, e, f, g, h, i;1736};1737EXPECT_THAT(MyVarType9{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0));1738struct MyVarType10 {1739int a, b, c, d, e, f, g, h, i, j;1740};1741EXPECT_THAT(MyVarType10{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0));1742struct MyVarType11 {1743int a, b, c, d, e, f, g, h, i, j, k;1744};1745EXPECT_THAT(MyVarType11{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));1746struct MyVarType12 {1747int a, b, c, d, e, f, g, h, i, j, k, l;1748};1749EXPECT_THAT(MyVarType12{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));1750struct MyVarType13 {1751int a, b, c, d, e, f, g, h, i, j, k, l, m;1752};1753EXPECT_THAT(MyVarType13{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));1754struct MyVarType14 {1755int a, b, c, d, e, f, g, h, i, j, k, l, m, n;1756};1757EXPECT_THAT(MyVarType14{},1758FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));1759struct MyVarType15 {1760int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o;1761};1762EXPECT_THAT(MyVarType15{},1763FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));1764struct MyVarType16 {1765int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p;1766};1767EXPECT_THAT(MyVarType16{},1768FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));1769struct MyVarType17 {1770int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q;1771};1772EXPECT_THAT(MyVarType17{},1773FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));1774struct MyVarType18 {1775int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r;1776};1777EXPECT_THAT(MyVarType18{},1778FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));1779struct MyVarType19 {1780int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s;1781};1782EXPECT_THAT(MyVarType19{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,17830, 0, 0, 0, 0));1784}1785#endif17861787TEST(PairTest, UseGetInsteadOfMembers) {1788PairWithGet pair{7, "ABC"};1789EXPECT_THAT(pair, Pair(7, "ABC"));1790EXPECT_THAT(pair, Pair(Ge(7), HasSubstr("AB")));1791EXPECT_THAT(pair, Not(Pair(Lt(7), "ABC")));17921793std::vector<PairWithGet> v = {{11, "Foo"}, {29, "gMockIsBestMock"}};1794EXPECT_THAT(v,1795ElementsAre(Pair(11, std::string("Foo")), Pair(Ge(10), Not(""))));1796}17971798// Tests StartsWith(s).17991800TEST(StartsWithTest, MatchesStringWithGivenPrefix) {1801const Matcher<const char*> m1 = StartsWith(std::string(""));1802EXPECT_TRUE(m1.Matches("Hi"));1803EXPECT_TRUE(m1.Matches(""));1804EXPECT_FALSE(m1.Matches(nullptr));18051806const Matcher<const std::string&> m2 = StartsWith("Hi");1807EXPECT_TRUE(m2.Matches("Hi"));1808EXPECT_TRUE(m2.Matches("Hi Hi!"));1809EXPECT_TRUE(m2.Matches("High"));1810EXPECT_FALSE(m2.Matches("H"));1811EXPECT_FALSE(m2.Matches(" Hi"));18121813#if GTEST_INTERNAL_HAS_STRING_VIEW1814const Matcher<internal::StringView> m_empty =1815StartsWith(internal::StringView(""));1816EXPECT_TRUE(m_empty.Matches(internal::StringView()));1817EXPECT_TRUE(m_empty.Matches(internal::StringView("")));1818EXPECT_TRUE(m_empty.Matches(internal::StringView("not empty")));1819#endif // GTEST_INTERNAL_HAS_STRING_VIEW1820}18211822TEST(StartsWithTest, CanDescribeSelf) {1823Matcher<const std::string> m = StartsWith("Hi");1824EXPECT_EQ("starts with \"Hi\"", Describe(m));1825}18261827TEST(StartsWithTest, WorksWithStringMatcherOnStringViewMatchee) {1828#if GTEST_INTERNAL_HAS_STRING_VIEW1829EXPECT_THAT(internal::StringView("talk to me goose"),1830StartsWith(std::string("talk")));1831#else1832GTEST_SKIP() << "Not applicable without internal::StringView.";1833#endif // GTEST_INTERNAL_HAS_STRING_VIEW1834}18351836// Tests EndsWith(s).18371838TEST(EndsWithTest, MatchesStringWithGivenSuffix) {1839const Matcher<const char*> m1 = EndsWith("");1840EXPECT_TRUE(m1.Matches("Hi"));1841EXPECT_TRUE(m1.Matches(""));1842EXPECT_FALSE(m1.Matches(nullptr));18431844const Matcher<const std::string&> m2 = EndsWith(std::string("Hi"));1845EXPECT_TRUE(m2.Matches("Hi"));1846EXPECT_TRUE(m2.Matches("Wow Hi Hi"));1847EXPECT_TRUE(m2.Matches("Super Hi"));1848EXPECT_FALSE(m2.Matches("i"));1849EXPECT_FALSE(m2.Matches("Hi "));18501851#if GTEST_INTERNAL_HAS_STRING_VIEW1852const Matcher<const internal::StringView&> m4 =1853EndsWith(internal::StringView(""));1854EXPECT_TRUE(m4.Matches("Hi"));1855EXPECT_TRUE(m4.Matches(""));1856EXPECT_TRUE(m4.Matches(internal::StringView()));1857EXPECT_TRUE(m4.Matches(internal::StringView("")));1858#endif // GTEST_INTERNAL_HAS_STRING_VIEW1859}18601861TEST(EndsWithTest, CanDescribeSelf) {1862Matcher<const std::string> m = EndsWith("Hi");1863EXPECT_EQ("ends with \"Hi\"", Describe(m));1864}18651866// Tests WhenBase64Unescaped.18671868TEST(WhenBase64UnescapedTest, MatchesUnescapedBase64Strings) {1869const Matcher<const char*> m1 = WhenBase64Unescaped(EndsWith("!"));1870EXPECT_FALSE(m1.Matches("invalid base64"));1871EXPECT_FALSE(m1.Matches("aGVsbG8gd29ybGQ=")); // hello world1872EXPECT_TRUE(m1.Matches("aGVsbG8gd29ybGQh")); // hello world!1873EXPECT_TRUE(m1.Matches("+/-_IQ")); // \xfb\xff\xbf!18741875const Matcher<const std::string&> m2 = WhenBase64Unescaped(EndsWith("!"));1876EXPECT_FALSE(m2.Matches("invalid base64"));1877EXPECT_FALSE(m2.Matches("aGVsbG8gd29ybGQ=")); // hello world1878EXPECT_TRUE(m2.Matches("aGVsbG8gd29ybGQh")); // hello world!1879EXPECT_TRUE(m2.Matches("+/-_IQ")); // \xfb\xff\xbf!18801881#if GTEST_INTERNAL_HAS_STRING_VIEW1882const Matcher<const internal::StringView&> m3 =1883WhenBase64Unescaped(EndsWith("!"));1884EXPECT_FALSE(m3.Matches("invalid base64"));1885EXPECT_FALSE(m3.Matches("aGVsbG8gd29ybGQ=")); // hello world1886EXPECT_TRUE(m3.Matches("aGVsbG8gd29ybGQh")); // hello world!1887EXPECT_TRUE(m3.Matches("+/-_IQ")); // \xfb\xff\xbf!1888#endif // GTEST_INTERNAL_HAS_STRING_VIEW1889}18901891TEST(WhenBase64UnescapedTest, CanDescribeSelf) {1892const Matcher<const char*> m = WhenBase64Unescaped(EndsWith("!"));1893EXPECT_EQ("matches after Base64Unescape ends with \"!\"", Describe(m));1894}18951896// Tests MatchesRegex().18971898TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {1899const Matcher<const char*> m1 = MatchesRegex("a.*z");1900EXPECT_TRUE(m1.Matches("az"));1901EXPECT_TRUE(m1.Matches("abcz"));1902EXPECT_FALSE(m1.Matches(nullptr));19031904const Matcher<const std::string&> m2 = MatchesRegex(new RE("a.*z"));1905EXPECT_TRUE(m2.Matches("azbz"));1906EXPECT_FALSE(m2.Matches("az1"));1907EXPECT_FALSE(m2.Matches("1az"));19081909#if GTEST_INTERNAL_HAS_STRING_VIEW1910const Matcher<const internal::StringView&> m3 = MatchesRegex("a.*z");1911EXPECT_TRUE(m3.Matches(internal::StringView("az")));1912EXPECT_TRUE(m3.Matches(internal::StringView("abcz")));1913EXPECT_FALSE(m3.Matches(internal::StringView("1az")));1914EXPECT_FALSE(m3.Matches(internal::StringView()));1915const Matcher<const internal::StringView&> m4 =1916MatchesRegex(internal::StringView(""));1917EXPECT_TRUE(m4.Matches(internal::StringView("")));1918EXPECT_TRUE(m4.Matches(internal::StringView()));1919#endif // GTEST_INTERNAL_HAS_STRING_VIEW1920}19211922TEST(MatchesRegexTest, CanDescribeSelf) {1923Matcher<const std::string> m1 = MatchesRegex(std::string("Hi.*"));1924EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1));19251926Matcher<const char*> m2 = MatchesRegex(new RE("a.*"));1927EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2));19281929#if GTEST_INTERNAL_HAS_STRING_VIEW1930Matcher<const internal::StringView> m3 = MatchesRegex(new RE("0.*"));1931EXPECT_EQ("matches regular expression \"0.*\"", Describe(m3));1932#endif // GTEST_INTERNAL_HAS_STRING_VIEW1933}19341935// Tests ContainsRegex().19361937TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {1938const Matcher<const char*> m1 = ContainsRegex(std::string("a.*z"));1939EXPECT_TRUE(m1.Matches("az"));1940EXPECT_TRUE(m1.Matches("0abcz1"));1941EXPECT_FALSE(m1.Matches(nullptr));19421943const Matcher<const std::string&> m2 = ContainsRegex(new RE("a.*z"));1944EXPECT_TRUE(m2.Matches("azbz"));1945EXPECT_TRUE(m2.Matches("az1"));1946EXPECT_FALSE(m2.Matches("1a"));19471948#if GTEST_INTERNAL_HAS_STRING_VIEW1949const Matcher<const internal::StringView&> m3 = ContainsRegex(new RE("a.*z"));1950EXPECT_TRUE(m3.Matches(internal::StringView("azbz")));1951EXPECT_TRUE(m3.Matches(internal::StringView("az1")));1952EXPECT_FALSE(m3.Matches(internal::StringView("1a")));1953EXPECT_FALSE(m3.Matches(internal::StringView()));1954const Matcher<const internal::StringView&> m4 =1955ContainsRegex(internal::StringView(""));1956EXPECT_TRUE(m4.Matches(internal::StringView("")));1957EXPECT_TRUE(m4.Matches(internal::StringView()));1958#endif // GTEST_INTERNAL_HAS_STRING_VIEW1959}19601961TEST(ContainsRegexTest, CanDescribeSelf) {1962Matcher<const std::string> m1 = ContainsRegex("Hi.*");1963EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1));19641965Matcher<const char*> m2 = ContainsRegex(new RE("a.*"));1966EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2));19671968#if GTEST_INTERNAL_HAS_STRING_VIEW1969Matcher<const internal::StringView> m3 = ContainsRegex(new RE("0.*"));1970EXPECT_EQ("contains regular expression \"0.*\"", Describe(m3));1971#endif // GTEST_INTERNAL_HAS_STRING_VIEW1972}19731974// Tests for wide strings.1975#if GTEST_HAS_STD_WSTRING1976TEST(StdWideStrEqTest, MatchesEqual) {1977Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello"));1978EXPECT_TRUE(m.Matches(L"Hello"));1979EXPECT_FALSE(m.Matches(L"hello"));1980EXPECT_FALSE(m.Matches(nullptr));19811982Matcher<const ::std::wstring&> m2 = StrEq(L"Hello");1983EXPECT_TRUE(m2.Matches(L"Hello"));1984EXPECT_FALSE(m2.Matches(L"Hi"));19851986Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");1987EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));1988EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));19891990::std::wstring str(L"01204500800");1991str[3] = L'\0';1992Matcher<const ::std::wstring&> m4 = StrEq(str);1993EXPECT_TRUE(m4.Matches(str));1994str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';1995Matcher<const ::std::wstring&> m5 = StrEq(str);1996EXPECT_TRUE(m5.Matches(str));1997}19981999TEST(StdWideStrEqTest, CanDescribeSelf) {2000Matcher<::std::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v");2001EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",2002Describe(m));20032004Matcher<::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");2005EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"", Describe(m2));20062007::std::wstring str(L"01204500800");2008str[3] = L'\0';2009Matcher<const ::std::wstring&> m4 = StrEq(str);2010EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));2011str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';2012Matcher<const ::std::wstring&> m5 = StrEq(str);2013EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));2014}20152016TEST(StdWideStrNeTest, MatchesUnequalString) {2017Matcher<const wchar_t*> m = StrNe(L"Hello");2018EXPECT_TRUE(m.Matches(L""));2019EXPECT_TRUE(m.Matches(nullptr));2020EXPECT_FALSE(m.Matches(L"Hello"));20212022Matcher<::std::wstring> m2 = StrNe(::std::wstring(L"Hello"));2023EXPECT_TRUE(m2.Matches(L"hello"));2024EXPECT_FALSE(m2.Matches(L"Hello"));2025}20262027TEST(StdWideStrNeTest, CanDescribeSelf) {2028Matcher<const wchar_t*> m = StrNe(L"Hi");2029EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));2030}20312032TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {2033Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello"));2034EXPECT_TRUE(m.Matches(L"Hello"));2035EXPECT_TRUE(m.Matches(L"hello"));2036EXPECT_FALSE(m.Matches(L"Hi"));2037EXPECT_FALSE(m.Matches(nullptr));20382039Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello");2040EXPECT_TRUE(m2.Matches(L"hello"));2041EXPECT_FALSE(m2.Matches(L"Hi"));2042}20432044TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {2045::std::wstring str1(L"oabocdooeoo");2046::std::wstring str2(L"OABOCDOOEOO");2047Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);2048EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0')));20492050str1[3] = str2[3] = L'\0';2051Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);2052EXPECT_TRUE(m1.Matches(str2));20532054str1[0] = str1[6] = str1[7] = str1[10] = L'\0';2055str2[0] = str2[6] = str2[7] = str2[10] = L'\0';2056Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);2057str1[9] = str2[9] = L'\0';2058EXPECT_FALSE(m2.Matches(str2));20592060Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);2061EXPECT_TRUE(m3.Matches(str2));20622063EXPECT_FALSE(m3.Matches(str2 + L"x"));2064str2.append(1, L'\0');2065EXPECT_FALSE(m3.Matches(str2));2066EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9)));2067}20682069TEST(StdWideStrCaseEqTest, CanDescribeSelf) {2070Matcher<::std::wstring> m = StrCaseEq(L"Hi");2071EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));2072}20732074TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {2075Matcher<const wchar_t*> m = StrCaseNe(L"Hello");2076EXPECT_TRUE(m.Matches(L"Hi"));2077EXPECT_TRUE(m.Matches(nullptr));2078EXPECT_FALSE(m.Matches(L"Hello"));2079EXPECT_FALSE(m.Matches(L"hello"));20802081Matcher<::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello"));2082EXPECT_TRUE(m2.Matches(L""));2083EXPECT_FALSE(m2.Matches(L"Hello"));2084}20852086TEST(StdWideStrCaseNeTest, CanDescribeSelf) {2087Matcher<const wchar_t*> m = StrCaseNe(L"Hi");2088EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));2089}20902091// Tests that HasSubstr() works for matching wstring-typed values.2092TEST(StdWideHasSubstrTest, WorksForStringClasses) {2093const Matcher<::std::wstring> m1 = HasSubstr(L"foo");2094EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food.")));2095EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo")));20962097const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo");2098EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food.")));2099EXPECT_FALSE(m2.Matches(::std::wstring(L"tofo")));2100}21012102// Tests that HasSubstr() works for matching C-wide-string-typed values.2103TEST(StdWideHasSubstrTest, WorksForCStrings) {2104const Matcher<wchar_t*> m1 = HasSubstr(L"foo");2105EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));2106EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));2107EXPECT_FALSE(m1.Matches(nullptr));21082109const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");2110EXPECT_TRUE(m2.Matches(L"I love food."));2111EXPECT_FALSE(m2.Matches(L"tofo"));2112EXPECT_FALSE(m2.Matches(nullptr));2113}21142115// Tests that HasSubstr(s) describes itself properly.2116TEST(StdWideHasSubstrTest, CanDescribeSelf) {2117Matcher<::std::wstring> m = HasSubstr(L"foo\n\"");2118EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));2119}21202121// Tests StartsWith(s).21222123TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {2124const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L""));2125EXPECT_TRUE(m1.Matches(L"Hi"));2126EXPECT_TRUE(m1.Matches(L""));2127EXPECT_FALSE(m1.Matches(nullptr));21282129const Matcher<const ::std::wstring&> m2 = StartsWith(L"Hi");2130EXPECT_TRUE(m2.Matches(L"Hi"));2131EXPECT_TRUE(m2.Matches(L"Hi Hi!"));2132EXPECT_TRUE(m2.Matches(L"High"));2133EXPECT_FALSE(m2.Matches(L"H"));2134EXPECT_FALSE(m2.Matches(L" Hi"));2135}21362137TEST(StdWideStartsWithTest, CanDescribeSelf) {2138Matcher<const ::std::wstring> m = StartsWith(L"Hi");2139EXPECT_EQ("starts with L\"Hi\"", Describe(m));2140}21412142// Tests EndsWith(s).21432144TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {2145const Matcher<const wchar_t*> m1 = EndsWith(L"");2146EXPECT_TRUE(m1.Matches(L"Hi"));2147EXPECT_TRUE(m1.Matches(L""));2148EXPECT_FALSE(m1.Matches(nullptr));21492150const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L"Hi"));2151EXPECT_TRUE(m2.Matches(L"Hi"));2152EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));2153EXPECT_TRUE(m2.Matches(L"Super Hi"));2154EXPECT_FALSE(m2.Matches(L"i"));2155EXPECT_FALSE(m2.Matches(L"Hi "));2156}21572158TEST(StdWideEndsWithTest, CanDescribeSelf) {2159Matcher<const ::std::wstring> m = EndsWith(L"Hi");2160EXPECT_EQ("ends with L\"Hi\"", Describe(m));2161}21622163#endif // GTEST_HAS_STD_WSTRING21642165TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {2166StringMatchResultListener listener1;2167EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1));2168EXPECT_EQ("% 2 == 0", listener1.str());21692170StringMatchResultListener listener2;2171EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2));2172EXPECT_EQ("", listener2.str());2173}21742175TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {2176const Matcher<int> is_even = PolymorphicIsEven();2177StringMatchResultListener listener1;2178EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1));2179EXPECT_EQ("% 2 == 0", listener1.str());21802181const Matcher<const double&> is_zero = Eq(0);2182StringMatchResultListener listener2;2183EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2));2184EXPECT_EQ("", listener2.str());2185}21862187MATCHER(ConstructNoArg, "") { return true; }2188MATCHER_P(Construct1Arg, arg1, "") { return true; }2189MATCHER_P2(Construct2Args, arg1, arg2, "") { return true; }21902191TEST(MatcherConstruct, ExplicitVsImplicit) {2192{2193// No arg constructor can be constructed with empty brace.2194ConstructNoArgMatcher m = {};2195(void)m;2196// And with no args2197ConstructNoArgMatcher m2;2198(void)m2;2199}2200{2201// The one arg constructor has an explicit constructor.2202// This is to prevent the implicit conversion.2203using M = Construct1ArgMatcherP<int>;2204EXPECT_TRUE((std::is_constructible<M, int>::value));2205EXPECT_FALSE((std::is_convertible<int, M>::value));2206}2207{2208// Multiple arg matchers can be constructed with an implicit construction.2209Construct2ArgsMatcherP2<int, double> m = {1, 2.2};2210(void)m;2211}2212}22132214MATCHER_P(Really, inner_matcher, "") {2215return ExplainMatchResult(inner_matcher, arg, result_listener);2216}22172218TEST(ExplainMatchResultTest, WorksInsideMATCHER) {2219EXPECT_THAT(0, Really(Eq(0)));2220}22212222TEST(DescribeMatcherTest, WorksWithValue) {2223EXPECT_EQ("is equal to 42", DescribeMatcher<int>(42));2224EXPECT_EQ("isn't equal to 42", DescribeMatcher<int>(42, true));2225}22262227TEST(DescribeMatcherTest, WorksWithMonomorphicMatcher) {2228const Matcher<int> monomorphic = Le(0);2229EXPECT_EQ("is <= 0", DescribeMatcher<int>(monomorphic));2230EXPECT_EQ("isn't <= 0", DescribeMatcher<int>(monomorphic, true));2231}22322233TEST(DescribeMatcherTest, WorksWithPolymorphicMatcher) {2234EXPECT_EQ("is even", DescribeMatcher<int>(PolymorphicIsEven()));2235EXPECT_EQ("is odd", DescribeMatcher<int>(PolymorphicIsEven(), true));2236}22372238MATCHER_P(FieldIIs, inner_matcher, "") {2239return ExplainMatchResult(inner_matcher, arg.i, result_listener);2240}22412242#if GTEST_HAS_RTTI2243TEST(WhenDynamicCastToTest, SameType) {2244Derived derived;2245derived.i = 4;22462247// Right type. A pointer is passed down.2248Base* as_base_ptr = &derived;2249EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(Not(IsNull())));2250EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(Pointee(FieldIIs(4))));2251EXPECT_THAT(as_base_ptr,2252Not(WhenDynamicCastTo<Derived*>(Pointee(FieldIIs(5)))));2253}22542255TEST(WhenDynamicCastToTest, WrongTypes) {2256Base base;2257Derived derived;2258OtherDerived other_derived;22592260// Wrong types. NULL is passed.2261EXPECT_THAT(&base, Not(WhenDynamicCastTo<Derived*>(Pointee(_))));2262EXPECT_THAT(&base, WhenDynamicCastTo<Derived*>(IsNull()));2263Base* as_base_ptr = &derived;2264EXPECT_THAT(as_base_ptr, Not(WhenDynamicCastTo<OtherDerived*>(Pointee(_))));2265EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<OtherDerived*>(IsNull()));2266as_base_ptr = &other_derived;2267EXPECT_THAT(as_base_ptr, Not(WhenDynamicCastTo<Derived*>(Pointee(_))));2268EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(IsNull()));2269}22702271TEST(WhenDynamicCastToTest, AlreadyNull) {2272// Already NULL.2273Base* as_base_ptr = nullptr;2274EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(IsNull()));2275}22762277struct AmbiguousCastTypes {2278class VirtualDerived : public virtual Base {};2279class DerivedSub1 : public VirtualDerived {};2280class DerivedSub2 : public VirtualDerived {};2281class ManyDerivedInHierarchy : public DerivedSub1, public DerivedSub2 {};2282};22832284TEST(WhenDynamicCastToTest, AmbiguousCast) {2285AmbiguousCastTypes::DerivedSub1 sub1;2286AmbiguousCastTypes::ManyDerivedInHierarchy many_derived;22872288// This testcase fails on FreeBSD. See this GitHub issue for more details:2289// https://github.com/google/googletest/issues/21722290#ifdef __FreeBSD__2291EXPECT_NONFATAL_FAILURE({2292#endif2293// Multiply derived from Base. dynamic_cast<> returns NULL.2294Base* as_base_ptr =2295static_cast<AmbiguousCastTypes::DerivedSub1*>(&many_derived);22962297EXPECT_THAT(as_base_ptr,2298WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(IsNull()));2299as_base_ptr = &sub1;2300EXPECT_THAT(2301as_base_ptr,2302WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(Not(IsNull())));2303#ifdef __FreeBSD__2304}, "");2305#endif2306}23072308TEST(WhenDynamicCastToTest, Describe) {2309Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(Pointee(_));2310const std::string prefix =2311"when dynamic_cast to " + internal::GetTypeName<Derived*>() + ", ";2312EXPECT_EQ(prefix + "points to a value that is anything", Describe(matcher));2313EXPECT_EQ(prefix + "does not point to a value that is anything",2314DescribeNegation(matcher));2315}23162317TEST(WhenDynamicCastToTest, Explain) {2318Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(Pointee(_));2319Base* null = nullptr;2320EXPECT_THAT(Explain(matcher, null), HasSubstr("NULL"));2321Derived derived;2322EXPECT_TRUE(matcher.Matches(&derived));2323EXPECT_THAT(Explain(matcher, &derived), HasSubstr("which points to "));23242325// With references, the matcher itself can fail. Test for that one.2326Matcher<const Base&> ref_matcher = WhenDynamicCastTo<const OtherDerived&>(_);2327EXPECT_THAT(Explain(ref_matcher, derived),2328HasSubstr("which cannot be dynamic_cast"));2329}23302331TEST(WhenDynamicCastToTest, GoodReference) {2332Derived derived;2333derived.i = 4;2334Base& as_base_ref = derived;2335EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(FieldIIs(4)));2336EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(Not(FieldIIs(5))));2337}23382339TEST(WhenDynamicCastToTest, BadReference) {2340Derived derived;2341Base& as_base_ref = derived;2342EXPECT_THAT(as_base_ref, Not(WhenDynamicCastTo<const OtherDerived&>(_)));2343}2344#endif // GTEST_HAS_RTTI23452346class DivisibleByImpl {2347public:2348explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {}23492350// For testing using ExplainMatchResultTo() with polymorphic matchers.2351template <typename T>2352bool MatchAndExplain(const T& n, MatchResultListener* listener) const {2353*listener << "which is " << (n % divider_) << " modulo " << divider_;2354return (n % divider_) == 0;2355}23562357void DescribeTo(ostream* os) const { *os << "is divisible by " << divider_; }23582359void DescribeNegationTo(ostream* os) const {2360*os << "is not divisible by " << divider_;2361}23622363void set_divider(int a_divider) { divider_ = a_divider; }2364int divider() const { return divider_; }23652366private:2367int divider_;2368};23692370PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) {2371return MakePolymorphicMatcher(DivisibleByImpl(n));2372}23732374// Tests that when AllOf() fails, only the first failing matcher is2375// asked to explain why.2376TEST(ExplainMatchResultTest, AllOf_False_False) {2377const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));2378EXPECT_EQ("which is 1 modulo 4", Explain(m, 5));2379}23802381// Tests that when AllOf() fails, only the first failing matcher is2382// asked to explain why.2383TEST(ExplainMatchResultTest, AllOf_False_True) {2384const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));2385EXPECT_EQ("which is 2 modulo 4", Explain(m, 6));2386}23872388// Tests that when AllOf() fails, only the first failing matcher is2389// asked to explain why.2390TEST(ExplainMatchResultTest, AllOf_True_False) {2391const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));2392EXPECT_EQ("which is 2 modulo 3", Explain(m, 5));2393}23942395// Tests that when AllOf() succeeds, all matchers are asked to explain2396// why.2397TEST(ExplainMatchResultTest, AllOf_True_True) {2398const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));2399EXPECT_EQ("which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6));2400}24012402// Tests that when AllOf() succeeds, but matchers have no explanation,2403// the matcher description is used.2404TEST(ExplainMatchResultTest, AllOf_True_True_2) {2405const Matcher<int> m = AllOf(Ge(2), Le(3));2406EXPECT_EQ("is >= 2, and is <= 3", Explain(m, 2));2407}24082409// A matcher that records whether the listener was interested.2410template <typename T>2411class CountingMatcher : public MatcherInterface<T> {2412public:2413explicit CountingMatcher(const Matcher<T>& base_matcher,2414std::vector<bool>* listener_interested)2415: base_matcher_(base_matcher),2416listener_interested_(listener_interested) {}24172418bool MatchAndExplain(T x, MatchResultListener* listener) const override {2419listener_interested_->push_back(listener->IsInterested());2420return base_matcher_.MatchAndExplain(x, listener);2421}24222423void DescribeTo(ostream* os) const override { base_matcher_.DescribeTo(os); }24242425private:2426Matcher<T> base_matcher_;2427std::vector<bool>* listener_interested_;2428};24292430TEST(AllOfTest, DoesNotFormatChildMatchersWhenNotInterested) {2431std::vector<bool> listener_interested;2432Matcher<int> matcher =2433MakeMatcher(new CountingMatcher<int>(Eq(1), &listener_interested));2434EXPECT_TRUE(matcher.Matches(1));2435EXPECT_THAT(listener_interested, ElementsAre(false));2436listener_interested.clear();2437Matcher<int> all_of_matcher = AllOf(matcher, matcher);2438EXPECT_TRUE(all_of_matcher.Matches(1));2439EXPECT_THAT(listener_interested, ElementsAre(false, false));2440listener_interested.clear();2441EXPECT_FALSE(all_of_matcher.Matches(0));2442EXPECT_THAT(listener_interested, ElementsAre(false));2443}24442445TEST(AnyOfTest, DoesNotFormatChildMatchersWhenNotInterested) {2446std::vector<bool> listener_interested;2447Matcher<int> matcher =2448MakeMatcher(new CountingMatcher<int>(Eq(1), &listener_interested));2449EXPECT_TRUE(matcher.Matches(1));2450EXPECT_THAT(listener_interested, ElementsAre(false));2451listener_interested.clear();2452Matcher<int> any_of_matcher = AnyOf(matcher, matcher);2453EXPECT_TRUE(any_of_matcher.Matches(1));2454EXPECT_THAT(listener_interested, ElementsAre(false));2455listener_interested.clear();2456EXPECT_FALSE(any_of_matcher.Matches(0));2457EXPECT_THAT(listener_interested, ElementsAre(false, false));2458}24592460TEST(OptionalTest, DoesNotFormatChildMatcherWhenNotInterested) {2461std::vector<bool> listener_interested;2462Matcher<int> matcher =2463MakeMatcher(new CountingMatcher<int>(Eq(1), &listener_interested));2464EXPECT_TRUE(matcher.Matches(1));2465EXPECT_THAT(listener_interested, ElementsAre(false));2466listener_interested.clear();2467Matcher<std::optional<int>> optional_matcher = Optional(matcher);2468EXPECT_FALSE(optional_matcher.Matches(std::nullopt));2469EXPECT_THAT(listener_interested, ElementsAre());2470EXPECT_TRUE(optional_matcher.Matches(1));2471EXPECT_THAT(listener_interested, ElementsAre(false));2472listener_interested.clear();2473EXPECT_FALSE(matcher.Matches(0));2474EXPECT_THAT(listener_interested, ElementsAre(false));2475}24762477INSTANTIATE_GTEST_MATCHER_TEST_P(ExplainmatcherResultTest);24782479TEST_P(ExplainmatcherResultTestP, MonomorphicMatcher) {2480const Matcher<int> m = GreaterThan(5);2481EXPECT_EQ("which is 1 more than 5", Explain(m, 6));2482}24832484// Tests PolymorphicMatcher::mutable_impl().2485TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {2486PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));2487DivisibleByImpl& impl = m.mutable_impl();2488EXPECT_EQ(42, impl.divider());24892490impl.set_divider(0);2491EXPECT_EQ(0, m.mutable_impl().divider());2492}24932494// Tests PolymorphicMatcher::impl().2495TEST(PolymorphicMatcherTest, CanAccessImpl) {2496const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));2497const DivisibleByImpl& impl = m.impl();2498EXPECT_EQ(42, impl.divider());2499}25002501} // namespace2502} // namespace gmock_matchers_test2503} // namespace testing25042505GTEST_DISABLE_MSC_WARNINGS_POP_() // 4244 4100250625072508