1*28f6c2f2SEnji Cooper // Copyright 2007, Google Inc. 2*28f6c2f2SEnji Cooper // All rights reserved. 3*28f6c2f2SEnji Cooper // 4*28f6c2f2SEnji Cooper // Redistribution and use in source and binary forms, with or without 5*28f6c2f2SEnji Cooper // modification, are permitted provided that the following conditions are 6*28f6c2f2SEnji Cooper // met: 7*28f6c2f2SEnji Cooper // 8*28f6c2f2SEnji Cooper // * Redistributions of source code must retain the above copyright 9*28f6c2f2SEnji Cooper // notice, this list of conditions and the following disclaimer. 10*28f6c2f2SEnji Cooper // * Redistributions in binary form must reproduce the above 11*28f6c2f2SEnji Cooper // copyright notice, this list of conditions and the following disclaimer 12*28f6c2f2SEnji Cooper // in the documentation and/or other materials provided with the 13*28f6c2f2SEnji Cooper // distribution. 14*28f6c2f2SEnji Cooper // * Neither the name of Google Inc. nor the names of its 15*28f6c2f2SEnji Cooper // contributors may be used to endorse or promote products derived from 16*28f6c2f2SEnji Cooper // this software without specific prior written permission. 17*28f6c2f2SEnji Cooper // 18*28f6c2f2SEnji Cooper // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19*28f6c2f2SEnji Cooper // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20*28f6c2f2SEnji Cooper // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21*28f6c2f2SEnji Cooper // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22*28f6c2f2SEnji Cooper // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23*28f6c2f2SEnji Cooper // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24*28f6c2f2SEnji Cooper // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25*28f6c2f2SEnji Cooper // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26*28f6c2f2SEnji Cooper // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27*28f6c2f2SEnji Cooper // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28*28f6c2f2SEnji Cooper // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29*28f6c2f2SEnji Cooper 30*28f6c2f2SEnji Cooper // Google Mock - a framework for writing C++ mock classes. 31*28f6c2f2SEnji Cooper // 32*28f6c2f2SEnji Cooper // This file tests some commonly used argument matchers. 33*28f6c2f2SEnji Cooper 34*28f6c2f2SEnji Cooper #include <functional> 35*28f6c2f2SEnji Cooper #include <memory> 36*28f6c2f2SEnji Cooper #include <string> 37*28f6c2f2SEnji Cooper #include <tuple> 38*28f6c2f2SEnji Cooper #include <vector> 39*28f6c2f2SEnji Cooper 40*28f6c2f2SEnji Cooper #include "test/gmock-matchers_test.h" 41*28f6c2f2SEnji Cooper 42*28f6c2f2SEnji Cooper // Silence warning C4244: 'initializing': conversion from 'int' to 'short', 43*28f6c2f2SEnji Cooper // possible loss of data and C4100, unreferenced local parameter 44*28f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_PUSH_(4244 4100) 45*28f6c2f2SEnji Cooper 46*28f6c2f2SEnji Cooper 47*28f6c2f2SEnji Cooper namespace testing { 48*28f6c2f2SEnji Cooper namespace gmock_matchers_test { 49*28f6c2f2SEnji Cooper namespace { 50*28f6c2f2SEnji Cooper 51*28f6c2f2SEnji Cooper INSTANTIATE_GTEST_MATCHER_TEST_P(MonotonicMatcherTest); 52*28f6c2f2SEnji Cooper 53*28f6c2f2SEnji Cooper TEST_P(MonotonicMatcherTestP, IsPrintable) { 54*28f6c2f2SEnji Cooper stringstream ss; 55*28f6c2f2SEnji Cooper ss << GreaterThan(5); 56*28f6c2f2SEnji Cooper EXPECT_EQ("is > 5", ss.str()); 57*28f6c2f2SEnji Cooper } 58*28f6c2f2SEnji Cooper 59*28f6c2f2SEnji Cooper TEST(MatchResultListenerTest, StreamingWorks) { 60*28f6c2f2SEnji Cooper StringMatchResultListener listener; 61*28f6c2f2SEnji Cooper listener << "hi" << 5; 62*28f6c2f2SEnji Cooper EXPECT_EQ("hi5", listener.str()); 63*28f6c2f2SEnji Cooper 64*28f6c2f2SEnji Cooper listener.Clear(); 65*28f6c2f2SEnji Cooper EXPECT_EQ("", listener.str()); 66*28f6c2f2SEnji Cooper 67*28f6c2f2SEnji Cooper listener << 42; 68*28f6c2f2SEnji Cooper EXPECT_EQ("42", listener.str()); 69*28f6c2f2SEnji Cooper 70*28f6c2f2SEnji Cooper // Streaming shouldn't crash when the underlying ostream is NULL. 71*28f6c2f2SEnji Cooper DummyMatchResultListener dummy; 72*28f6c2f2SEnji Cooper dummy << "hi" << 5; 73*28f6c2f2SEnji Cooper } 74*28f6c2f2SEnji Cooper 75*28f6c2f2SEnji Cooper TEST(MatchResultListenerTest, CanAccessUnderlyingStream) { 76*28f6c2f2SEnji Cooper EXPECT_TRUE(DummyMatchResultListener().stream() == nullptr); 77*28f6c2f2SEnji Cooper EXPECT_TRUE(StreamMatchResultListener(nullptr).stream() == nullptr); 78*28f6c2f2SEnji Cooper 79*28f6c2f2SEnji Cooper EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream()); 80*28f6c2f2SEnji Cooper } 81*28f6c2f2SEnji Cooper 82*28f6c2f2SEnji Cooper TEST(MatchResultListenerTest, IsInterestedWorks) { 83*28f6c2f2SEnji Cooper EXPECT_TRUE(StringMatchResultListener().IsInterested()); 84*28f6c2f2SEnji Cooper EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested()); 85*28f6c2f2SEnji Cooper 86*28f6c2f2SEnji Cooper EXPECT_FALSE(DummyMatchResultListener().IsInterested()); 87*28f6c2f2SEnji Cooper EXPECT_FALSE(StreamMatchResultListener(nullptr).IsInterested()); 88*28f6c2f2SEnji Cooper } 89*28f6c2f2SEnji Cooper 90*28f6c2f2SEnji Cooper // Makes sure that the MatcherInterface<T> interface doesn't 91*28f6c2f2SEnji Cooper // change. 92*28f6c2f2SEnji Cooper class EvenMatcherImpl : public MatcherInterface<int> { 93*28f6c2f2SEnji Cooper public: 94*28f6c2f2SEnji Cooper bool MatchAndExplain(int x, 95*28f6c2f2SEnji Cooper MatchResultListener* /* listener */) const override { 96*28f6c2f2SEnji Cooper return x % 2 == 0; 97*28f6c2f2SEnji Cooper } 98*28f6c2f2SEnji Cooper 99*28f6c2f2SEnji Cooper void DescribeTo(ostream* os) const override { *os << "is an even number"; } 100*28f6c2f2SEnji Cooper 101*28f6c2f2SEnji Cooper // We deliberately don't define DescribeNegationTo() and 102*28f6c2f2SEnji Cooper // ExplainMatchResultTo() here, to make sure the definition of these 103*28f6c2f2SEnji Cooper // two methods is optional. 104*28f6c2f2SEnji Cooper }; 105*28f6c2f2SEnji Cooper 106*28f6c2f2SEnji Cooper // Makes sure that the MatcherInterface API doesn't change. 107*28f6c2f2SEnji Cooper TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) { 108*28f6c2f2SEnji Cooper EvenMatcherImpl m; 109*28f6c2f2SEnji Cooper } 110*28f6c2f2SEnji Cooper 111*28f6c2f2SEnji Cooper // Tests implementing a monomorphic matcher using MatchAndExplain(). 112*28f6c2f2SEnji Cooper 113*28f6c2f2SEnji Cooper class NewEvenMatcherImpl : public MatcherInterface<int> { 114*28f6c2f2SEnji Cooper public: 115*28f6c2f2SEnji Cooper bool MatchAndExplain(int x, MatchResultListener* listener) const override { 116*28f6c2f2SEnji Cooper const bool match = x % 2 == 0; 117*28f6c2f2SEnji Cooper // Verifies that we can stream to a listener directly. 118*28f6c2f2SEnji Cooper *listener << "value % " << 2; 119*28f6c2f2SEnji Cooper if (listener->stream() != nullptr) { 120*28f6c2f2SEnji Cooper // Verifies that we can stream to a listener's underlying stream 121*28f6c2f2SEnji Cooper // too. 122*28f6c2f2SEnji Cooper *listener->stream() << " == " << (x % 2); 123*28f6c2f2SEnji Cooper } 124*28f6c2f2SEnji Cooper return match; 125*28f6c2f2SEnji Cooper } 126*28f6c2f2SEnji Cooper 127*28f6c2f2SEnji Cooper void DescribeTo(ostream* os) const override { *os << "is an even number"; } 128*28f6c2f2SEnji Cooper }; 129*28f6c2f2SEnji Cooper 130*28f6c2f2SEnji Cooper TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) { 131*28f6c2f2SEnji Cooper Matcher<int> m = MakeMatcher(new NewEvenMatcherImpl); 132*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(2)); 133*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(3)); 134*28f6c2f2SEnji Cooper EXPECT_EQ("value % 2 == 0", Explain(m, 2)); 135*28f6c2f2SEnji Cooper EXPECT_EQ("value % 2 == 1", Explain(m, 3)); 136*28f6c2f2SEnji Cooper } 137*28f6c2f2SEnji Cooper 138*28f6c2f2SEnji Cooper INSTANTIATE_GTEST_MATCHER_TEST_P(MatcherTest); 139*28f6c2f2SEnji Cooper 140*28f6c2f2SEnji Cooper // Tests default-constructing a matcher. 141*28f6c2f2SEnji Cooper TEST(MatcherTest, CanBeDefaultConstructed) { Matcher<double> m; } 142*28f6c2f2SEnji Cooper 143*28f6c2f2SEnji Cooper // Tests that Matcher<T> can be constructed from a MatcherInterface<T>*. 144*28f6c2f2SEnji Cooper TEST(MatcherTest, CanBeConstructedFromMatcherInterface) { 145*28f6c2f2SEnji Cooper const MatcherInterface<int>* impl = new EvenMatcherImpl; 146*28f6c2f2SEnji Cooper Matcher<int> m(impl); 147*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(4)); 148*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(5)); 149*28f6c2f2SEnji Cooper } 150*28f6c2f2SEnji Cooper 151*28f6c2f2SEnji Cooper // Tests that value can be used in place of Eq(value). 152*28f6c2f2SEnji Cooper TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) { 153*28f6c2f2SEnji Cooper Matcher<int> m1 = 5; 154*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(5)); 155*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(6)); 156*28f6c2f2SEnji Cooper } 157*28f6c2f2SEnji Cooper 158*28f6c2f2SEnji Cooper // Tests that NULL can be used in place of Eq(NULL). 159*28f6c2f2SEnji Cooper TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) { 160*28f6c2f2SEnji Cooper Matcher<int*> m1 = nullptr; 161*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(nullptr)); 162*28f6c2f2SEnji Cooper int n = 0; 163*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(&n)); 164*28f6c2f2SEnji Cooper } 165*28f6c2f2SEnji Cooper 166*28f6c2f2SEnji Cooper // Tests that matchers can be constructed from a variable that is not properly 167*28f6c2f2SEnji Cooper // defined. This should be illegal, but many users rely on this accidentally. 168*28f6c2f2SEnji Cooper struct Undefined { 169*28f6c2f2SEnji Cooper virtual ~Undefined() = 0; 170*28f6c2f2SEnji Cooper static const int kInt = 1; 171*28f6c2f2SEnji Cooper }; 172*28f6c2f2SEnji Cooper 173*28f6c2f2SEnji Cooper TEST(MatcherTest, CanBeConstructedFromUndefinedVariable) { 174*28f6c2f2SEnji Cooper Matcher<int> m1 = Undefined::kInt; 175*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(1)); 176*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(2)); 177*28f6c2f2SEnji Cooper } 178*28f6c2f2SEnji Cooper 179*28f6c2f2SEnji Cooper // Test that a matcher parameterized with an abstract class compiles. 180*28f6c2f2SEnji Cooper TEST(MatcherTest, CanAcceptAbstractClass) { Matcher<const Undefined&> m = _; } 181*28f6c2f2SEnji Cooper 182*28f6c2f2SEnji Cooper // Tests that matchers are copyable. 183*28f6c2f2SEnji Cooper TEST(MatcherTest, IsCopyable) { 184*28f6c2f2SEnji Cooper // Tests the copy constructor. 185*28f6c2f2SEnji Cooper Matcher<bool> m1 = Eq(false); 186*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(false)); 187*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(true)); 188*28f6c2f2SEnji Cooper 189*28f6c2f2SEnji Cooper // Tests the assignment operator. 190*28f6c2f2SEnji Cooper m1 = Eq(true); 191*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(true)); 192*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(false)); 193*28f6c2f2SEnji Cooper } 194*28f6c2f2SEnji Cooper 195*28f6c2f2SEnji Cooper // Tests that Matcher<T>::DescribeTo() calls 196*28f6c2f2SEnji Cooper // MatcherInterface<T>::DescribeTo(). 197*28f6c2f2SEnji Cooper TEST(MatcherTest, CanDescribeItself) { 198*28f6c2f2SEnji Cooper EXPECT_EQ("is an even number", Describe(Matcher<int>(new EvenMatcherImpl))); 199*28f6c2f2SEnji Cooper } 200*28f6c2f2SEnji Cooper 201*28f6c2f2SEnji Cooper // Tests Matcher<T>::MatchAndExplain(). 202*28f6c2f2SEnji Cooper TEST_P(MatcherTestP, MatchAndExplain) { 203*28f6c2f2SEnji Cooper Matcher<int> m = GreaterThan(0); 204*28f6c2f2SEnji Cooper StringMatchResultListener listener1; 205*28f6c2f2SEnji Cooper EXPECT_TRUE(m.MatchAndExplain(42, &listener1)); 206*28f6c2f2SEnji Cooper EXPECT_EQ("which is 42 more than 0", listener1.str()); 207*28f6c2f2SEnji Cooper 208*28f6c2f2SEnji Cooper StringMatchResultListener listener2; 209*28f6c2f2SEnji Cooper EXPECT_FALSE(m.MatchAndExplain(-9, &listener2)); 210*28f6c2f2SEnji Cooper EXPECT_EQ("which is 9 less than 0", listener2.str()); 211*28f6c2f2SEnji Cooper } 212*28f6c2f2SEnji Cooper 213*28f6c2f2SEnji Cooper // Tests that a C-string literal can be implicitly converted to a 214*28f6c2f2SEnji Cooper // Matcher<std::string> or Matcher<const std::string&>. 215*28f6c2f2SEnji Cooper TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) { 216*28f6c2f2SEnji Cooper Matcher<std::string> m1 = "hi"; 217*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches("hi")); 218*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches("hello")); 219*28f6c2f2SEnji Cooper 220*28f6c2f2SEnji Cooper Matcher<const std::string&> m2 = "hi"; 221*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches("hi")); 222*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches("hello")); 223*28f6c2f2SEnji Cooper } 224*28f6c2f2SEnji Cooper 225*28f6c2f2SEnji Cooper // Tests that a string object can be implicitly converted to a 226*28f6c2f2SEnji Cooper // Matcher<std::string> or Matcher<const std::string&>. 227*28f6c2f2SEnji Cooper TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) { 228*28f6c2f2SEnji Cooper Matcher<std::string> m1 = std::string("hi"); 229*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches("hi")); 230*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches("hello")); 231*28f6c2f2SEnji Cooper 232*28f6c2f2SEnji Cooper Matcher<const std::string&> m2 = std::string("hi"); 233*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches("hi")); 234*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches("hello")); 235*28f6c2f2SEnji Cooper } 236*28f6c2f2SEnji Cooper 237*28f6c2f2SEnji Cooper #if GTEST_INTERNAL_HAS_STRING_VIEW 238*28f6c2f2SEnji Cooper // Tests that a C-string literal can be implicitly converted to a 239*28f6c2f2SEnji Cooper // Matcher<StringView> or Matcher<const StringView&>. 240*28f6c2f2SEnji Cooper TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) { 241*28f6c2f2SEnji Cooper Matcher<internal::StringView> m1 = "cats"; 242*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches("cats")); 243*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches("dogs")); 244*28f6c2f2SEnji Cooper 245*28f6c2f2SEnji Cooper Matcher<const internal::StringView&> m2 = "cats"; 246*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches("cats")); 247*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches("dogs")); 248*28f6c2f2SEnji Cooper } 249*28f6c2f2SEnji Cooper 250*28f6c2f2SEnji Cooper // Tests that a std::string object can be implicitly converted to a 251*28f6c2f2SEnji Cooper // Matcher<StringView> or Matcher<const StringView&>. 252*28f6c2f2SEnji Cooper TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromString) { 253*28f6c2f2SEnji Cooper Matcher<internal::StringView> m1 = std::string("cats"); 254*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches("cats")); 255*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches("dogs")); 256*28f6c2f2SEnji Cooper 257*28f6c2f2SEnji Cooper Matcher<const internal::StringView&> m2 = std::string("cats"); 258*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches("cats")); 259*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches("dogs")); 260*28f6c2f2SEnji Cooper } 261*28f6c2f2SEnji Cooper 262*28f6c2f2SEnji Cooper // Tests that a StringView object can be implicitly converted to a 263*28f6c2f2SEnji Cooper // Matcher<StringView> or Matcher<const StringView&>. 264*28f6c2f2SEnji Cooper TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromStringView) { 265*28f6c2f2SEnji Cooper Matcher<internal::StringView> m1 = internal::StringView("cats"); 266*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches("cats")); 267*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches("dogs")); 268*28f6c2f2SEnji Cooper 269*28f6c2f2SEnji Cooper Matcher<const internal::StringView&> m2 = internal::StringView("cats"); 270*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches("cats")); 271*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches("dogs")); 272*28f6c2f2SEnji Cooper } 273*28f6c2f2SEnji Cooper #endif // GTEST_INTERNAL_HAS_STRING_VIEW 274*28f6c2f2SEnji Cooper 275*28f6c2f2SEnji Cooper // Tests that a std::reference_wrapper<std::string> object can be implicitly 276*28f6c2f2SEnji Cooper // converted to a Matcher<std::string> or Matcher<const std::string&> via Eq(). 277*28f6c2f2SEnji Cooper TEST(StringMatcherTest, 278*28f6c2f2SEnji Cooper CanBeImplicitlyConstructedFromEqReferenceWrapperString) { 279*28f6c2f2SEnji Cooper std::string value = "cats"; 280*28f6c2f2SEnji Cooper Matcher<std::string> m1 = Eq(std::ref(value)); 281*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches("cats")); 282*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches("dogs")); 283*28f6c2f2SEnji Cooper 284*28f6c2f2SEnji Cooper Matcher<const std::string&> m2 = Eq(std::ref(value)); 285*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches("cats")); 286*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches("dogs")); 287*28f6c2f2SEnji Cooper } 288*28f6c2f2SEnji Cooper 289*28f6c2f2SEnji Cooper // Tests that MakeMatcher() constructs a Matcher<T> from a 290*28f6c2f2SEnji Cooper // MatcherInterface* without requiring the user to explicitly 291*28f6c2f2SEnji Cooper // write the type. 292*28f6c2f2SEnji Cooper TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) { 293*28f6c2f2SEnji Cooper const MatcherInterface<int>* dummy_impl = new EvenMatcherImpl; 294*28f6c2f2SEnji Cooper Matcher<int> m = MakeMatcher(dummy_impl); 295*28f6c2f2SEnji Cooper } 296*28f6c2f2SEnji Cooper 297*28f6c2f2SEnji Cooper // Tests that MakePolymorphicMatcher() can construct a polymorphic 298*28f6c2f2SEnji Cooper // matcher from its implementation using the old API. 299*28f6c2f2SEnji Cooper const int g_bar = 1; 300*28f6c2f2SEnji Cooper class ReferencesBarOrIsZeroImpl { 301*28f6c2f2SEnji Cooper public: 302*28f6c2f2SEnji Cooper template <typename T> 303*28f6c2f2SEnji Cooper bool MatchAndExplain(const T& x, MatchResultListener* /* listener */) const { 304*28f6c2f2SEnji Cooper const void* p = &x; 305*28f6c2f2SEnji Cooper return p == &g_bar || x == 0; 306*28f6c2f2SEnji Cooper } 307*28f6c2f2SEnji Cooper 308*28f6c2f2SEnji Cooper void DescribeTo(ostream* os) const { *os << "g_bar or zero"; } 309*28f6c2f2SEnji Cooper 310*28f6c2f2SEnji Cooper void DescribeNegationTo(ostream* os) const { 311*28f6c2f2SEnji Cooper *os << "doesn't reference g_bar and is not zero"; 312*28f6c2f2SEnji Cooper } 313*28f6c2f2SEnji Cooper }; 314*28f6c2f2SEnji Cooper 315*28f6c2f2SEnji Cooper // This function verifies that MakePolymorphicMatcher() returns a 316*28f6c2f2SEnji Cooper // PolymorphicMatcher<T> where T is the argument's type. 317*28f6c2f2SEnji Cooper PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() { 318*28f6c2f2SEnji Cooper return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl()); 319*28f6c2f2SEnji Cooper } 320*28f6c2f2SEnji Cooper 321*28f6c2f2SEnji Cooper TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) { 322*28f6c2f2SEnji Cooper // Using a polymorphic matcher to match a reference type. 323*28f6c2f2SEnji Cooper Matcher<const int&> m1 = ReferencesBarOrIsZero(); 324*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(0)); 325*28f6c2f2SEnji Cooper // Verifies that the identity of a by-reference argument is preserved. 326*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(g_bar)); 327*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(1)); 328*28f6c2f2SEnji Cooper EXPECT_EQ("g_bar or zero", Describe(m1)); 329*28f6c2f2SEnji Cooper 330*28f6c2f2SEnji Cooper // Using a polymorphic matcher to match a value type. 331*28f6c2f2SEnji Cooper Matcher<double> m2 = ReferencesBarOrIsZero(); 332*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(0.0)); 333*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(0.1)); 334*28f6c2f2SEnji Cooper EXPECT_EQ("g_bar or zero", Describe(m2)); 335*28f6c2f2SEnji Cooper } 336*28f6c2f2SEnji Cooper 337*28f6c2f2SEnji Cooper // Tests implementing a polymorphic matcher using MatchAndExplain(). 338*28f6c2f2SEnji Cooper 339*28f6c2f2SEnji Cooper class PolymorphicIsEvenImpl { 340*28f6c2f2SEnji Cooper public: 341*28f6c2f2SEnji Cooper void DescribeTo(ostream* os) const { *os << "is even"; } 342*28f6c2f2SEnji Cooper 343*28f6c2f2SEnji Cooper void DescribeNegationTo(ostream* os) const { *os << "is odd"; } 344*28f6c2f2SEnji Cooper 345*28f6c2f2SEnji Cooper template <typename T> 346*28f6c2f2SEnji Cooper bool MatchAndExplain(const T& x, MatchResultListener* listener) const { 347*28f6c2f2SEnji Cooper // Verifies that we can stream to the listener directly. 348*28f6c2f2SEnji Cooper *listener << "% " << 2; 349*28f6c2f2SEnji Cooper if (listener->stream() != nullptr) { 350*28f6c2f2SEnji Cooper // Verifies that we can stream to the listener's underlying stream 351*28f6c2f2SEnji Cooper // too. 352*28f6c2f2SEnji Cooper *listener->stream() << " == " << (x % 2); 353*28f6c2f2SEnji Cooper } 354*28f6c2f2SEnji Cooper return (x % 2) == 0; 355*28f6c2f2SEnji Cooper } 356*28f6c2f2SEnji Cooper }; 357*28f6c2f2SEnji Cooper 358*28f6c2f2SEnji Cooper PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() { 359*28f6c2f2SEnji Cooper return MakePolymorphicMatcher(PolymorphicIsEvenImpl()); 360*28f6c2f2SEnji Cooper } 361*28f6c2f2SEnji Cooper 362*28f6c2f2SEnji Cooper TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) { 363*28f6c2f2SEnji Cooper // Using PolymorphicIsEven() as a Matcher<int>. 364*28f6c2f2SEnji Cooper const Matcher<int> m1 = PolymorphicIsEven(); 365*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(42)); 366*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(43)); 367*28f6c2f2SEnji Cooper EXPECT_EQ("is even", Describe(m1)); 368*28f6c2f2SEnji Cooper 369*28f6c2f2SEnji Cooper const Matcher<int> not_m1 = Not(m1); 370*28f6c2f2SEnji Cooper EXPECT_EQ("is odd", Describe(not_m1)); 371*28f6c2f2SEnji Cooper 372*28f6c2f2SEnji Cooper EXPECT_EQ("% 2 == 0", Explain(m1, 42)); 373*28f6c2f2SEnji Cooper 374*28f6c2f2SEnji Cooper // Using PolymorphicIsEven() as a Matcher<char>. 375*28f6c2f2SEnji Cooper const Matcher<char> m2 = PolymorphicIsEven(); 376*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches('\x42')); 377*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches('\x43')); 378*28f6c2f2SEnji Cooper EXPECT_EQ("is even", Describe(m2)); 379*28f6c2f2SEnji Cooper 380*28f6c2f2SEnji Cooper const Matcher<char> not_m2 = Not(m2); 381*28f6c2f2SEnji Cooper EXPECT_EQ("is odd", Describe(not_m2)); 382*28f6c2f2SEnji Cooper 383*28f6c2f2SEnji Cooper EXPECT_EQ("% 2 == 0", Explain(m2, '\x42')); 384*28f6c2f2SEnji Cooper } 385*28f6c2f2SEnji Cooper 386*28f6c2f2SEnji Cooper INSTANTIATE_GTEST_MATCHER_TEST_P(MatcherCastTest); 387*28f6c2f2SEnji Cooper 388*28f6c2f2SEnji Cooper // Tests that MatcherCast<T>(m) works when m is a polymorphic matcher. 389*28f6c2f2SEnji Cooper TEST_P(MatcherCastTestP, FromPolymorphicMatcher) { 390*28f6c2f2SEnji Cooper Matcher<int16_t> m; 391*28f6c2f2SEnji Cooper if (use_gtest_matcher_) { 392*28f6c2f2SEnji Cooper m = MatcherCast<int16_t>(GtestGreaterThan(int64_t{5})); 393*28f6c2f2SEnji Cooper } else { 394*28f6c2f2SEnji Cooper m = MatcherCast<int16_t>(Gt(int64_t{5})); 395*28f6c2f2SEnji Cooper } 396*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(6)); 397*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(4)); 398*28f6c2f2SEnji Cooper } 399*28f6c2f2SEnji Cooper 400*28f6c2f2SEnji Cooper // For testing casting matchers between compatible types. 401*28f6c2f2SEnji Cooper class IntValue { 402*28f6c2f2SEnji Cooper public: 403*28f6c2f2SEnji Cooper // An int can be statically (although not implicitly) cast to a 404*28f6c2f2SEnji Cooper // IntValue. 405*28f6c2f2SEnji Cooper explicit IntValue(int a_value) : value_(a_value) {} 406*28f6c2f2SEnji Cooper 407*28f6c2f2SEnji Cooper int value() const { return value_; } 408*28f6c2f2SEnji Cooper 409*28f6c2f2SEnji Cooper private: 410*28f6c2f2SEnji Cooper int value_; 411*28f6c2f2SEnji Cooper }; 412*28f6c2f2SEnji Cooper 413*28f6c2f2SEnji Cooper // For testing casting matchers between compatible types. 414*28f6c2f2SEnji Cooper bool IsPositiveIntValue(const IntValue& foo) { return foo.value() > 0; } 415*28f6c2f2SEnji Cooper 416*28f6c2f2SEnji Cooper // Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T 417*28f6c2f2SEnji Cooper // can be statically converted to U. 418*28f6c2f2SEnji Cooper TEST(MatcherCastTest, FromCompatibleType) { 419*28f6c2f2SEnji Cooper Matcher<double> m1 = Eq(2.0); 420*28f6c2f2SEnji Cooper Matcher<int> m2 = MatcherCast<int>(m1); 421*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(2)); 422*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(3)); 423*28f6c2f2SEnji Cooper 424*28f6c2f2SEnji Cooper Matcher<IntValue> m3 = Truly(IsPositiveIntValue); 425*28f6c2f2SEnji Cooper Matcher<int> m4 = MatcherCast<int>(m3); 426*28f6c2f2SEnji Cooper // In the following, the arguments 1 and 0 are statically converted 427*28f6c2f2SEnji Cooper // to IntValue objects, and then tested by the IsPositiveIntValue() 428*28f6c2f2SEnji Cooper // predicate. 429*28f6c2f2SEnji Cooper EXPECT_TRUE(m4.Matches(1)); 430*28f6c2f2SEnji Cooper EXPECT_FALSE(m4.Matches(0)); 431*28f6c2f2SEnji Cooper } 432*28f6c2f2SEnji Cooper 433*28f6c2f2SEnji Cooper // Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>. 434*28f6c2f2SEnji Cooper TEST(MatcherCastTest, FromConstReferenceToNonReference) { 435*28f6c2f2SEnji Cooper Matcher<const int&> m1 = Eq(0); 436*28f6c2f2SEnji Cooper Matcher<int> m2 = MatcherCast<int>(m1); 437*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(0)); 438*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(1)); 439*28f6c2f2SEnji Cooper } 440*28f6c2f2SEnji Cooper 441*28f6c2f2SEnji Cooper // Tests that MatcherCast<T>(m) works when m is a Matcher<T&>. 442*28f6c2f2SEnji Cooper TEST(MatcherCastTest, FromReferenceToNonReference) { 443*28f6c2f2SEnji Cooper Matcher<int&> m1 = Eq(0); 444*28f6c2f2SEnji Cooper Matcher<int> m2 = MatcherCast<int>(m1); 445*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(0)); 446*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(1)); 447*28f6c2f2SEnji Cooper } 448*28f6c2f2SEnji Cooper 449*28f6c2f2SEnji Cooper // Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>. 450*28f6c2f2SEnji Cooper TEST(MatcherCastTest, FromNonReferenceToConstReference) { 451*28f6c2f2SEnji Cooper Matcher<int> m1 = Eq(0); 452*28f6c2f2SEnji Cooper Matcher<const int&> m2 = MatcherCast<const int&>(m1); 453*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(0)); 454*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(1)); 455*28f6c2f2SEnji Cooper } 456*28f6c2f2SEnji Cooper 457*28f6c2f2SEnji Cooper // Tests that MatcherCast<T&>(m) works when m is a Matcher<T>. 458*28f6c2f2SEnji Cooper TEST(MatcherCastTest, FromNonReferenceToReference) { 459*28f6c2f2SEnji Cooper Matcher<int> m1 = Eq(0); 460*28f6c2f2SEnji Cooper Matcher<int&> m2 = MatcherCast<int&>(m1); 461*28f6c2f2SEnji Cooper int n = 0; 462*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(n)); 463*28f6c2f2SEnji Cooper n = 1; 464*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(n)); 465*28f6c2f2SEnji Cooper } 466*28f6c2f2SEnji Cooper 467*28f6c2f2SEnji Cooper // Tests that MatcherCast<T>(m) works when m is a Matcher<T>. 468*28f6c2f2SEnji Cooper TEST(MatcherCastTest, FromSameType) { 469*28f6c2f2SEnji Cooper Matcher<int> m1 = Eq(0); 470*28f6c2f2SEnji Cooper Matcher<int> m2 = MatcherCast<int>(m1); 471*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(0)); 472*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(1)); 473*28f6c2f2SEnji Cooper } 474*28f6c2f2SEnji Cooper 475*28f6c2f2SEnji Cooper // Tests that MatcherCast<T>(m) works when m is a value of the same type as the 476*28f6c2f2SEnji Cooper // value type of the Matcher. 477*28f6c2f2SEnji Cooper TEST(MatcherCastTest, FromAValue) { 478*28f6c2f2SEnji Cooper Matcher<int> m = MatcherCast<int>(42); 479*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(42)); 480*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(239)); 481*28f6c2f2SEnji Cooper } 482*28f6c2f2SEnji Cooper 483*28f6c2f2SEnji Cooper // Tests that MatcherCast<T>(m) works when m is a value of the type implicitly 484*28f6c2f2SEnji Cooper // convertible to the value type of the Matcher. 485*28f6c2f2SEnji Cooper TEST(MatcherCastTest, FromAnImplicitlyConvertibleValue) { 486*28f6c2f2SEnji Cooper const int kExpected = 'c'; 487*28f6c2f2SEnji Cooper Matcher<int> m = MatcherCast<int>('c'); 488*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(kExpected)); 489*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(kExpected + 1)); 490*28f6c2f2SEnji Cooper } 491*28f6c2f2SEnji Cooper 492*28f6c2f2SEnji Cooper struct NonImplicitlyConstructibleTypeWithOperatorEq { 493*28f6c2f2SEnji Cooper friend bool operator==( 494*28f6c2f2SEnji Cooper const NonImplicitlyConstructibleTypeWithOperatorEq& /* ignored */, 495*28f6c2f2SEnji Cooper int rhs) { 496*28f6c2f2SEnji Cooper return 42 == rhs; 497*28f6c2f2SEnji Cooper } 498*28f6c2f2SEnji Cooper friend bool operator==( 499*28f6c2f2SEnji Cooper int lhs, 500*28f6c2f2SEnji Cooper const NonImplicitlyConstructibleTypeWithOperatorEq& /* ignored */) { 501*28f6c2f2SEnji Cooper return lhs == 42; 502*28f6c2f2SEnji Cooper } 503*28f6c2f2SEnji Cooper }; 504*28f6c2f2SEnji Cooper 505*28f6c2f2SEnji Cooper // Tests that MatcherCast<T>(m) works when m is a neither a matcher nor 506*28f6c2f2SEnji Cooper // implicitly convertible to the value type of the Matcher, but the value type 507*28f6c2f2SEnji Cooper // of the matcher has operator==() overload accepting m. 508*28f6c2f2SEnji Cooper TEST(MatcherCastTest, NonImplicitlyConstructibleTypeWithOperatorEq) { 509*28f6c2f2SEnji Cooper Matcher<NonImplicitlyConstructibleTypeWithOperatorEq> m1 = 510*28f6c2f2SEnji Cooper MatcherCast<NonImplicitlyConstructibleTypeWithOperatorEq>(42); 511*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(NonImplicitlyConstructibleTypeWithOperatorEq())); 512*28f6c2f2SEnji Cooper 513*28f6c2f2SEnji Cooper Matcher<NonImplicitlyConstructibleTypeWithOperatorEq> m2 = 514*28f6c2f2SEnji Cooper MatcherCast<NonImplicitlyConstructibleTypeWithOperatorEq>(239); 515*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(NonImplicitlyConstructibleTypeWithOperatorEq())); 516*28f6c2f2SEnji Cooper 517*28f6c2f2SEnji Cooper // When updating the following lines please also change the comment to 518*28f6c2f2SEnji Cooper // namespace convertible_from_any. 519*28f6c2f2SEnji Cooper Matcher<int> m3 = 520*28f6c2f2SEnji Cooper MatcherCast<int>(NonImplicitlyConstructibleTypeWithOperatorEq()); 521*28f6c2f2SEnji Cooper EXPECT_TRUE(m3.Matches(42)); 522*28f6c2f2SEnji Cooper EXPECT_FALSE(m3.Matches(239)); 523*28f6c2f2SEnji Cooper } 524*28f6c2f2SEnji Cooper 525*28f6c2f2SEnji Cooper // ConvertibleFromAny does not work with MSVC. resulting in 526*28f6c2f2SEnji Cooper // error C2440: 'initializing': cannot convert from 'Eq' to 'M' 527*28f6c2f2SEnji Cooper // No constructor could take the source type, or constructor overload 528*28f6c2f2SEnji Cooper // resolution was ambiguous 529*28f6c2f2SEnji Cooper 530*28f6c2f2SEnji Cooper #if !defined _MSC_VER 531*28f6c2f2SEnji Cooper 532*28f6c2f2SEnji Cooper // The below ConvertibleFromAny struct is implicitly constructible from anything 533*28f6c2f2SEnji Cooper // and when in the same namespace can interact with other tests. In particular, 534*28f6c2f2SEnji Cooper // if it is in the same namespace as other tests and one removes 535*28f6c2f2SEnji Cooper // NonImplicitlyConstructibleTypeWithOperatorEq::operator==(int lhs, ...); 536*28f6c2f2SEnji Cooper // then the corresponding test still compiles (and it should not!) by implicitly 537*28f6c2f2SEnji Cooper // converting NonImplicitlyConstructibleTypeWithOperatorEq to ConvertibleFromAny 538*28f6c2f2SEnji Cooper // in m3.Matcher(). 539*28f6c2f2SEnji Cooper namespace convertible_from_any { 540*28f6c2f2SEnji Cooper // Implicitly convertible from any type. 541*28f6c2f2SEnji Cooper struct ConvertibleFromAny { 542*28f6c2f2SEnji Cooper ConvertibleFromAny(int a_value) : value(a_value) {} 543*28f6c2f2SEnji Cooper template <typename T> 544*28f6c2f2SEnji Cooper ConvertibleFromAny(const T& /*a_value*/) : value(-1) { 545*28f6c2f2SEnji Cooper ADD_FAILURE() << "Conversion constructor called"; 546*28f6c2f2SEnji Cooper } 547*28f6c2f2SEnji Cooper int value; 548*28f6c2f2SEnji Cooper }; 549*28f6c2f2SEnji Cooper 550*28f6c2f2SEnji Cooper bool operator==(const ConvertibleFromAny& a, const ConvertibleFromAny& b) { 551*28f6c2f2SEnji Cooper return a.value == b.value; 552*28f6c2f2SEnji Cooper } 553*28f6c2f2SEnji Cooper 554*28f6c2f2SEnji Cooper ostream& operator<<(ostream& os, const ConvertibleFromAny& a) { 555*28f6c2f2SEnji Cooper return os << a.value; 556*28f6c2f2SEnji Cooper } 557*28f6c2f2SEnji Cooper 558*28f6c2f2SEnji Cooper TEST(MatcherCastTest, ConversionConstructorIsUsed) { 559*28f6c2f2SEnji Cooper Matcher<ConvertibleFromAny> m = MatcherCast<ConvertibleFromAny>(1); 560*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(ConvertibleFromAny(1))); 561*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(ConvertibleFromAny(2))); 562*28f6c2f2SEnji Cooper } 563*28f6c2f2SEnji Cooper 564*28f6c2f2SEnji Cooper TEST(MatcherCastTest, FromConvertibleFromAny) { 565*28f6c2f2SEnji Cooper Matcher<ConvertibleFromAny> m = 566*28f6c2f2SEnji Cooper MatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1))); 567*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(ConvertibleFromAny(1))); 568*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(ConvertibleFromAny(2))); 569*28f6c2f2SEnji Cooper } 570*28f6c2f2SEnji Cooper } // namespace convertible_from_any 571*28f6c2f2SEnji Cooper 572*28f6c2f2SEnji Cooper #endif // !defined _MSC_VER 573*28f6c2f2SEnji Cooper 574*28f6c2f2SEnji Cooper struct IntReferenceWrapper { 575*28f6c2f2SEnji Cooper IntReferenceWrapper(const int& a_value) : value(&a_value) {} 576*28f6c2f2SEnji Cooper const int* value; 577*28f6c2f2SEnji Cooper }; 578*28f6c2f2SEnji Cooper 579*28f6c2f2SEnji Cooper bool operator==(const IntReferenceWrapper& a, const IntReferenceWrapper& b) { 580*28f6c2f2SEnji Cooper return a.value == b.value; 581*28f6c2f2SEnji Cooper } 582*28f6c2f2SEnji Cooper 583*28f6c2f2SEnji Cooper TEST(MatcherCastTest, ValueIsNotCopied) { 584*28f6c2f2SEnji Cooper int n = 42; 585*28f6c2f2SEnji Cooper Matcher<IntReferenceWrapper> m = MatcherCast<IntReferenceWrapper>(n); 586*28f6c2f2SEnji Cooper // Verify that the matcher holds a reference to n, not to its temporary copy. 587*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(n)); 588*28f6c2f2SEnji Cooper } 589*28f6c2f2SEnji Cooper 590*28f6c2f2SEnji Cooper class Base { 591*28f6c2f2SEnji Cooper public: 592*28f6c2f2SEnji Cooper virtual ~Base() = default; 593*28f6c2f2SEnji Cooper Base() = default; 594*28f6c2f2SEnji Cooper 595*28f6c2f2SEnji Cooper private: 596*28f6c2f2SEnji Cooper Base(const Base&) = delete; 597*28f6c2f2SEnji Cooper Base& operator=(const Base&) = delete; 598*28f6c2f2SEnji Cooper }; 599*28f6c2f2SEnji Cooper 600*28f6c2f2SEnji Cooper class Derived : public Base { 601*28f6c2f2SEnji Cooper public: 602*28f6c2f2SEnji Cooper Derived() : Base() {} 603*28f6c2f2SEnji Cooper int i; 604*28f6c2f2SEnji Cooper }; 605*28f6c2f2SEnji Cooper 606*28f6c2f2SEnji Cooper class OtherDerived : public Base {}; 607*28f6c2f2SEnji Cooper 608*28f6c2f2SEnji Cooper INSTANTIATE_GTEST_MATCHER_TEST_P(SafeMatcherCastTest); 609*28f6c2f2SEnji Cooper 610*28f6c2f2SEnji Cooper // Tests that SafeMatcherCast<T>(m) works when m is a polymorphic matcher. 611*28f6c2f2SEnji Cooper TEST_P(SafeMatcherCastTestP, FromPolymorphicMatcher) { 612*28f6c2f2SEnji Cooper Matcher<char> m2; 613*28f6c2f2SEnji Cooper if (use_gtest_matcher_) { 614*28f6c2f2SEnji Cooper m2 = SafeMatcherCast<char>(GtestGreaterThan(32)); 615*28f6c2f2SEnji Cooper } else { 616*28f6c2f2SEnji Cooper m2 = SafeMatcherCast<char>(Gt(32)); 617*28f6c2f2SEnji Cooper } 618*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches('A')); 619*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches('\n')); 620*28f6c2f2SEnji Cooper } 621*28f6c2f2SEnji Cooper 622*28f6c2f2SEnji Cooper // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where 623*28f6c2f2SEnji Cooper // T and U are arithmetic types and T can be losslessly converted to 624*28f6c2f2SEnji Cooper // U. 625*28f6c2f2SEnji Cooper TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) { 626*28f6c2f2SEnji Cooper Matcher<double> m1 = DoubleEq(1.0); 627*28f6c2f2SEnji Cooper Matcher<float> m2 = SafeMatcherCast<float>(m1); 628*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(1.0f)); 629*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(2.0f)); 630*28f6c2f2SEnji Cooper 631*28f6c2f2SEnji Cooper Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>('a')); 632*28f6c2f2SEnji Cooper EXPECT_TRUE(m3.Matches('a')); 633*28f6c2f2SEnji Cooper EXPECT_FALSE(m3.Matches('b')); 634*28f6c2f2SEnji Cooper } 635*28f6c2f2SEnji Cooper 636*28f6c2f2SEnji Cooper // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where T and U 637*28f6c2f2SEnji Cooper // are pointers or references to a derived and a base class, correspondingly. 638*28f6c2f2SEnji Cooper TEST(SafeMatcherCastTest, FromBaseClass) { 639*28f6c2f2SEnji Cooper Derived d, d2; 640*28f6c2f2SEnji Cooper Matcher<Base*> m1 = Eq(&d); 641*28f6c2f2SEnji Cooper Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1); 642*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(&d)); 643*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(&d2)); 644*28f6c2f2SEnji Cooper 645*28f6c2f2SEnji Cooper Matcher<Base&> m3 = Ref(d); 646*28f6c2f2SEnji Cooper Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3); 647*28f6c2f2SEnji Cooper EXPECT_TRUE(m4.Matches(d)); 648*28f6c2f2SEnji Cooper EXPECT_FALSE(m4.Matches(d2)); 649*28f6c2f2SEnji Cooper } 650*28f6c2f2SEnji Cooper 651*28f6c2f2SEnji Cooper // Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>. 652*28f6c2f2SEnji Cooper TEST(SafeMatcherCastTest, FromConstReferenceToReference) { 653*28f6c2f2SEnji Cooper int n = 0; 654*28f6c2f2SEnji Cooper Matcher<const int&> m1 = Ref(n); 655*28f6c2f2SEnji Cooper Matcher<int&> m2 = SafeMatcherCast<int&>(m1); 656*28f6c2f2SEnji Cooper int n1 = 0; 657*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(n)); 658*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(n1)); 659*28f6c2f2SEnji Cooper } 660*28f6c2f2SEnji Cooper 661*28f6c2f2SEnji Cooper // Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>. 662*28f6c2f2SEnji Cooper TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) { 663*28f6c2f2SEnji Cooper Matcher<std::unique_ptr<int>> m1 = IsNull(); 664*28f6c2f2SEnji Cooper Matcher<const std::unique_ptr<int>&> m2 = 665*28f6c2f2SEnji Cooper SafeMatcherCast<const std::unique_ptr<int>&>(m1); 666*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(std::unique_ptr<int>())); 667*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(std::unique_ptr<int>(new int))); 668*28f6c2f2SEnji Cooper } 669*28f6c2f2SEnji Cooper 670*28f6c2f2SEnji Cooper // Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<T>. 671*28f6c2f2SEnji Cooper TEST(SafeMatcherCastTest, FromNonReferenceToReference) { 672*28f6c2f2SEnji Cooper Matcher<int> m1 = Eq(0); 673*28f6c2f2SEnji Cooper Matcher<int&> m2 = SafeMatcherCast<int&>(m1); 674*28f6c2f2SEnji Cooper int n = 0; 675*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(n)); 676*28f6c2f2SEnji Cooper n = 1; 677*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(n)); 678*28f6c2f2SEnji Cooper } 679*28f6c2f2SEnji Cooper 680*28f6c2f2SEnji Cooper // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<T>. 681*28f6c2f2SEnji Cooper TEST(SafeMatcherCastTest, FromSameType) { 682*28f6c2f2SEnji Cooper Matcher<int> m1 = Eq(0); 683*28f6c2f2SEnji Cooper Matcher<int> m2 = SafeMatcherCast<int>(m1); 684*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(0)); 685*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(1)); 686*28f6c2f2SEnji Cooper } 687*28f6c2f2SEnji Cooper 688*28f6c2f2SEnji Cooper #if !defined _MSC_VER 689*28f6c2f2SEnji Cooper 690*28f6c2f2SEnji Cooper namespace convertible_from_any { 691*28f6c2f2SEnji Cooper TEST(SafeMatcherCastTest, ConversionConstructorIsUsed) { 692*28f6c2f2SEnji Cooper Matcher<ConvertibleFromAny> m = SafeMatcherCast<ConvertibleFromAny>(1); 693*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(ConvertibleFromAny(1))); 694*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(ConvertibleFromAny(2))); 695*28f6c2f2SEnji Cooper } 696*28f6c2f2SEnji Cooper 697*28f6c2f2SEnji Cooper TEST(SafeMatcherCastTest, FromConvertibleFromAny) { 698*28f6c2f2SEnji Cooper Matcher<ConvertibleFromAny> m = 699*28f6c2f2SEnji Cooper SafeMatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1))); 700*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(ConvertibleFromAny(1))); 701*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(ConvertibleFromAny(2))); 702*28f6c2f2SEnji Cooper } 703*28f6c2f2SEnji Cooper } // namespace convertible_from_any 704*28f6c2f2SEnji Cooper 705*28f6c2f2SEnji Cooper #endif // !defined _MSC_VER 706*28f6c2f2SEnji Cooper 707*28f6c2f2SEnji Cooper TEST(SafeMatcherCastTest, ValueIsNotCopied) { 708*28f6c2f2SEnji Cooper int n = 42; 709*28f6c2f2SEnji Cooper Matcher<IntReferenceWrapper> m = SafeMatcherCast<IntReferenceWrapper>(n); 710*28f6c2f2SEnji Cooper // Verify that the matcher holds a reference to n, not to its temporary copy. 711*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(n)); 712*28f6c2f2SEnji Cooper } 713*28f6c2f2SEnji Cooper 714*28f6c2f2SEnji Cooper TEST(ExpectThat, TakesLiterals) { 715*28f6c2f2SEnji Cooper EXPECT_THAT(1, 1); 716*28f6c2f2SEnji Cooper EXPECT_THAT(1.0, 1.0); 717*28f6c2f2SEnji Cooper EXPECT_THAT(std::string(), ""); 718*28f6c2f2SEnji Cooper } 719*28f6c2f2SEnji Cooper 720*28f6c2f2SEnji Cooper TEST(ExpectThat, TakesFunctions) { 721*28f6c2f2SEnji Cooper struct Helper { 722*28f6c2f2SEnji Cooper static void Func() {} 723*28f6c2f2SEnji Cooper }; 724*28f6c2f2SEnji Cooper void (*func)() = Helper::Func; 725*28f6c2f2SEnji Cooper EXPECT_THAT(func, Helper::Func); 726*28f6c2f2SEnji Cooper EXPECT_THAT(func, &Helper::Func); 727*28f6c2f2SEnji Cooper } 728*28f6c2f2SEnji Cooper 729*28f6c2f2SEnji Cooper // Tests that A<T>() matches any value of type T. 730*28f6c2f2SEnji Cooper TEST(ATest, MatchesAnyValue) { 731*28f6c2f2SEnji Cooper // Tests a matcher for a value type. 732*28f6c2f2SEnji Cooper Matcher<double> m1 = A<double>(); 733*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(91.43)); 734*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(-15.32)); 735*28f6c2f2SEnji Cooper 736*28f6c2f2SEnji Cooper // Tests a matcher for a reference type. 737*28f6c2f2SEnji Cooper int a = 2; 738*28f6c2f2SEnji Cooper int b = -6; 739*28f6c2f2SEnji Cooper Matcher<int&> m2 = A<int&>(); 740*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(a)); 741*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(b)); 742*28f6c2f2SEnji Cooper } 743*28f6c2f2SEnji Cooper 744*28f6c2f2SEnji Cooper TEST(ATest, WorksForDerivedClass) { 745*28f6c2f2SEnji Cooper Base base; 746*28f6c2f2SEnji Cooper Derived derived; 747*28f6c2f2SEnji Cooper EXPECT_THAT(&base, A<Base*>()); 748*28f6c2f2SEnji Cooper // This shouldn't compile: EXPECT_THAT(&base, A<Derived*>()); 749*28f6c2f2SEnji Cooper EXPECT_THAT(&derived, A<Base*>()); 750*28f6c2f2SEnji Cooper EXPECT_THAT(&derived, A<Derived*>()); 751*28f6c2f2SEnji Cooper } 752*28f6c2f2SEnji Cooper 753*28f6c2f2SEnji Cooper // Tests that A<T>() describes itself properly. 754*28f6c2f2SEnji Cooper TEST(ATest, CanDescribeSelf) { EXPECT_EQ("is anything", Describe(A<bool>())); } 755*28f6c2f2SEnji Cooper 756*28f6c2f2SEnji Cooper // Tests that An<T>() matches any value of type T. 757*28f6c2f2SEnji Cooper TEST(AnTest, MatchesAnyValue) { 758*28f6c2f2SEnji Cooper // Tests a matcher for a value type. 759*28f6c2f2SEnji Cooper Matcher<int> m1 = An<int>(); 760*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(9143)); 761*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(-1532)); 762*28f6c2f2SEnji Cooper 763*28f6c2f2SEnji Cooper // Tests a matcher for a reference type. 764*28f6c2f2SEnji Cooper int a = 2; 765*28f6c2f2SEnji Cooper int b = -6; 766*28f6c2f2SEnji Cooper Matcher<int&> m2 = An<int&>(); 767*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(a)); 768*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(b)); 769*28f6c2f2SEnji Cooper } 770*28f6c2f2SEnji Cooper 771*28f6c2f2SEnji Cooper // Tests that An<T>() describes itself properly. 772*28f6c2f2SEnji Cooper TEST(AnTest, CanDescribeSelf) { EXPECT_EQ("is anything", Describe(An<int>())); } 773*28f6c2f2SEnji Cooper 774*28f6c2f2SEnji Cooper // Tests that _ can be used as a matcher for any type and matches any 775*28f6c2f2SEnji Cooper // value of that type. 776*28f6c2f2SEnji Cooper TEST(UnderscoreTest, MatchesAnyValue) { 777*28f6c2f2SEnji Cooper // Uses _ as a matcher for a value type. 778*28f6c2f2SEnji Cooper Matcher<int> m1 = _; 779*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(123)); 780*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(-242)); 781*28f6c2f2SEnji Cooper 782*28f6c2f2SEnji Cooper // Uses _ as a matcher for a reference type. 783*28f6c2f2SEnji Cooper bool a = false; 784*28f6c2f2SEnji Cooper const bool b = true; 785*28f6c2f2SEnji Cooper Matcher<const bool&> m2 = _; 786*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(a)); 787*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(b)); 788*28f6c2f2SEnji Cooper } 789*28f6c2f2SEnji Cooper 790*28f6c2f2SEnji Cooper // Tests that _ describes itself properly. 791*28f6c2f2SEnji Cooper TEST(UnderscoreTest, CanDescribeSelf) { 792*28f6c2f2SEnji Cooper Matcher<int> m = _; 793*28f6c2f2SEnji Cooper EXPECT_EQ("is anything", Describe(m)); 794*28f6c2f2SEnji Cooper } 795*28f6c2f2SEnji Cooper 796*28f6c2f2SEnji Cooper // Tests that Eq(x) matches any value equal to x. 797*28f6c2f2SEnji Cooper TEST(EqTest, MatchesEqualValue) { 798*28f6c2f2SEnji Cooper // 2 C-strings with same content but different addresses. 799*28f6c2f2SEnji Cooper const char a1[] = "hi"; 800*28f6c2f2SEnji Cooper const char a2[] = "hi"; 801*28f6c2f2SEnji Cooper 802*28f6c2f2SEnji Cooper Matcher<const char*> m1 = Eq(a1); 803*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(a1)); 804*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(a2)); 805*28f6c2f2SEnji Cooper } 806*28f6c2f2SEnji Cooper 807*28f6c2f2SEnji Cooper // Tests that Eq(v) describes itself properly. 808*28f6c2f2SEnji Cooper 809*28f6c2f2SEnji Cooper class Unprintable { 810*28f6c2f2SEnji Cooper public: 811*28f6c2f2SEnji Cooper Unprintable() : c_('a') {} 812*28f6c2f2SEnji Cooper 813*28f6c2f2SEnji Cooper bool operator==(const Unprintable& /* rhs */) const { return true; } 814*28f6c2f2SEnji Cooper // -Wunused-private-field: dummy accessor for `c_`. 815*28f6c2f2SEnji Cooper char dummy_c() { return c_; } 816*28f6c2f2SEnji Cooper 817*28f6c2f2SEnji Cooper private: 818*28f6c2f2SEnji Cooper char c_; 819*28f6c2f2SEnji Cooper }; 820*28f6c2f2SEnji Cooper 821*28f6c2f2SEnji Cooper TEST(EqTest, CanDescribeSelf) { 822*28f6c2f2SEnji Cooper Matcher<Unprintable> m = Eq(Unprintable()); 823*28f6c2f2SEnji Cooper EXPECT_EQ("is equal to 1-byte object <61>", Describe(m)); 824*28f6c2f2SEnji Cooper } 825*28f6c2f2SEnji Cooper 826*28f6c2f2SEnji Cooper // Tests that Eq(v) can be used to match any type that supports 827*28f6c2f2SEnji Cooper // comparing with type T, where T is v's type. 828*28f6c2f2SEnji Cooper TEST(EqTest, IsPolymorphic) { 829*28f6c2f2SEnji Cooper Matcher<int> m1 = Eq(1); 830*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(1)); 831*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(2)); 832*28f6c2f2SEnji Cooper 833*28f6c2f2SEnji Cooper Matcher<char> m2 = Eq(1); 834*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches('\1')); 835*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches('a')); 836*28f6c2f2SEnji Cooper } 837*28f6c2f2SEnji Cooper 838*28f6c2f2SEnji Cooper // Tests that TypedEq<T>(v) matches values of type T that's equal to v. 839*28f6c2f2SEnji Cooper TEST(TypedEqTest, ChecksEqualityForGivenType) { 840*28f6c2f2SEnji Cooper Matcher<char> m1 = TypedEq<char>('a'); 841*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches('a')); 842*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches('b')); 843*28f6c2f2SEnji Cooper 844*28f6c2f2SEnji Cooper Matcher<int> m2 = TypedEq<int>(6); 845*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(6)); 846*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(7)); 847*28f6c2f2SEnji Cooper } 848*28f6c2f2SEnji Cooper 849*28f6c2f2SEnji Cooper // Tests that TypedEq(v) describes itself properly. 850*28f6c2f2SEnji Cooper TEST(TypedEqTest, CanDescribeSelf) { 851*28f6c2f2SEnji Cooper EXPECT_EQ("is equal to 2", Describe(TypedEq<int>(2))); 852*28f6c2f2SEnji Cooper } 853*28f6c2f2SEnji Cooper 854*28f6c2f2SEnji Cooper // Tests that TypedEq<T>(v) has type Matcher<T>. 855*28f6c2f2SEnji Cooper 856*28f6c2f2SEnji Cooper // Type<T>::IsTypeOf(v) compiles if and only if the type of value v is T, where 857*28f6c2f2SEnji Cooper // T is a "bare" type (i.e. not in the form of const U or U&). If v's type is 858*28f6c2f2SEnji Cooper // not T, the compiler will generate a message about "undefined reference". 859*28f6c2f2SEnji Cooper template <typename T> 860*28f6c2f2SEnji Cooper struct Type { 861*28f6c2f2SEnji Cooper static bool IsTypeOf(const T& /* v */) { return true; } 862*28f6c2f2SEnji Cooper 863*28f6c2f2SEnji Cooper template <typename T2> 864*28f6c2f2SEnji Cooper static void IsTypeOf(T2 v); 865*28f6c2f2SEnji Cooper }; 866*28f6c2f2SEnji Cooper 867*28f6c2f2SEnji Cooper TEST(TypedEqTest, HasSpecifiedType) { 868*28f6c2f2SEnji Cooper // Verifies that the type of TypedEq<T>(v) is Matcher<T>. 869*28f6c2f2SEnji Cooper Type<Matcher<int>>::IsTypeOf(TypedEq<int>(5)); 870*28f6c2f2SEnji Cooper Type<Matcher<double>>::IsTypeOf(TypedEq<double>(5)); 871*28f6c2f2SEnji Cooper } 872*28f6c2f2SEnji Cooper 873*28f6c2f2SEnji Cooper // Tests that Ge(v) matches anything >= v. 874*28f6c2f2SEnji Cooper TEST(GeTest, ImplementsGreaterThanOrEqual) { 875*28f6c2f2SEnji Cooper Matcher<int> m1 = Ge(0); 876*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(1)); 877*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(0)); 878*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(-1)); 879*28f6c2f2SEnji Cooper } 880*28f6c2f2SEnji Cooper 881*28f6c2f2SEnji Cooper // Tests that Ge(v) describes itself properly. 882*28f6c2f2SEnji Cooper TEST(GeTest, CanDescribeSelf) { 883*28f6c2f2SEnji Cooper Matcher<int> m = Ge(5); 884*28f6c2f2SEnji Cooper EXPECT_EQ("is >= 5", Describe(m)); 885*28f6c2f2SEnji Cooper } 886*28f6c2f2SEnji Cooper 887*28f6c2f2SEnji Cooper // Tests that Gt(v) matches anything > v. 888*28f6c2f2SEnji Cooper TEST(GtTest, ImplementsGreaterThan) { 889*28f6c2f2SEnji Cooper Matcher<double> m1 = Gt(0); 890*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(1.0)); 891*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(0.0)); 892*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(-1.0)); 893*28f6c2f2SEnji Cooper } 894*28f6c2f2SEnji Cooper 895*28f6c2f2SEnji Cooper // Tests that Gt(v) describes itself properly. 896*28f6c2f2SEnji Cooper TEST(GtTest, CanDescribeSelf) { 897*28f6c2f2SEnji Cooper Matcher<int> m = Gt(5); 898*28f6c2f2SEnji Cooper EXPECT_EQ("is > 5", Describe(m)); 899*28f6c2f2SEnji Cooper } 900*28f6c2f2SEnji Cooper 901*28f6c2f2SEnji Cooper // Tests that Le(v) matches anything <= v. 902*28f6c2f2SEnji Cooper TEST(LeTest, ImplementsLessThanOrEqual) { 903*28f6c2f2SEnji Cooper Matcher<char> m1 = Le('b'); 904*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches('a')); 905*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches('b')); 906*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches('c')); 907*28f6c2f2SEnji Cooper } 908*28f6c2f2SEnji Cooper 909*28f6c2f2SEnji Cooper // Tests that Le(v) describes itself properly. 910*28f6c2f2SEnji Cooper TEST(LeTest, CanDescribeSelf) { 911*28f6c2f2SEnji Cooper Matcher<int> m = Le(5); 912*28f6c2f2SEnji Cooper EXPECT_EQ("is <= 5", Describe(m)); 913*28f6c2f2SEnji Cooper } 914*28f6c2f2SEnji Cooper 915*28f6c2f2SEnji Cooper // Tests that Lt(v) matches anything < v. 916*28f6c2f2SEnji Cooper TEST(LtTest, ImplementsLessThan) { 917*28f6c2f2SEnji Cooper Matcher<const std::string&> m1 = Lt("Hello"); 918*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches("Abc")); 919*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches("Hello")); 920*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches("Hello, world!")); 921*28f6c2f2SEnji Cooper } 922*28f6c2f2SEnji Cooper 923*28f6c2f2SEnji Cooper // Tests that Lt(v) describes itself properly. 924*28f6c2f2SEnji Cooper TEST(LtTest, CanDescribeSelf) { 925*28f6c2f2SEnji Cooper Matcher<int> m = Lt(5); 926*28f6c2f2SEnji Cooper EXPECT_EQ("is < 5", Describe(m)); 927*28f6c2f2SEnji Cooper } 928*28f6c2f2SEnji Cooper 929*28f6c2f2SEnji Cooper // Tests that Ne(v) matches anything != v. 930*28f6c2f2SEnji Cooper TEST(NeTest, ImplementsNotEqual) { 931*28f6c2f2SEnji Cooper Matcher<int> m1 = Ne(0); 932*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(1)); 933*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(-1)); 934*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(0)); 935*28f6c2f2SEnji Cooper } 936*28f6c2f2SEnji Cooper 937*28f6c2f2SEnji Cooper // Tests that Ne(v) describes itself properly. 938*28f6c2f2SEnji Cooper TEST(NeTest, CanDescribeSelf) { 939*28f6c2f2SEnji Cooper Matcher<int> m = Ne(5); 940*28f6c2f2SEnji Cooper EXPECT_EQ("isn't equal to 5", Describe(m)); 941*28f6c2f2SEnji Cooper } 942*28f6c2f2SEnji Cooper 943*28f6c2f2SEnji Cooper class MoveOnly { 944*28f6c2f2SEnji Cooper public: 945*28f6c2f2SEnji Cooper explicit MoveOnly(int i) : i_(i) {} 946*28f6c2f2SEnji Cooper MoveOnly(const MoveOnly&) = delete; 947*28f6c2f2SEnji Cooper MoveOnly(MoveOnly&&) = default; 948*28f6c2f2SEnji Cooper MoveOnly& operator=(const MoveOnly&) = delete; 949*28f6c2f2SEnji Cooper MoveOnly& operator=(MoveOnly&&) = default; 950*28f6c2f2SEnji Cooper 951*28f6c2f2SEnji Cooper bool operator==(const MoveOnly& other) const { return i_ == other.i_; } 952*28f6c2f2SEnji Cooper bool operator!=(const MoveOnly& other) const { return i_ != other.i_; } 953*28f6c2f2SEnji Cooper bool operator<(const MoveOnly& other) const { return i_ < other.i_; } 954*28f6c2f2SEnji Cooper bool operator<=(const MoveOnly& other) const { return i_ <= other.i_; } 955*28f6c2f2SEnji Cooper bool operator>(const MoveOnly& other) const { return i_ > other.i_; } 956*28f6c2f2SEnji Cooper bool operator>=(const MoveOnly& other) const { return i_ >= other.i_; } 957*28f6c2f2SEnji Cooper 958*28f6c2f2SEnji Cooper private: 959*28f6c2f2SEnji Cooper int i_; 960*28f6c2f2SEnji Cooper }; 961*28f6c2f2SEnji Cooper 962*28f6c2f2SEnji Cooper struct MoveHelper { 963*28f6c2f2SEnji Cooper MOCK_METHOD1(Call, void(MoveOnly)); 964*28f6c2f2SEnji Cooper }; 965*28f6c2f2SEnji Cooper 966*28f6c2f2SEnji Cooper // Disable this test in VS 2015 (version 14), where it fails when SEH is enabled 967*28f6c2f2SEnji Cooper #if defined(_MSC_VER) && (_MSC_VER < 1910) 968*28f6c2f2SEnji Cooper TEST(ComparisonBaseTest, DISABLED_WorksWithMoveOnly) { 969*28f6c2f2SEnji Cooper #else 970*28f6c2f2SEnji Cooper TEST(ComparisonBaseTest, WorksWithMoveOnly) { 971*28f6c2f2SEnji Cooper #endif 972*28f6c2f2SEnji Cooper MoveOnly m{0}; 973*28f6c2f2SEnji Cooper MoveHelper helper; 974*28f6c2f2SEnji Cooper 975*28f6c2f2SEnji Cooper EXPECT_CALL(helper, Call(Eq(ByRef(m)))); 976*28f6c2f2SEnji Cooper helper.Call(MoveOnly(0)); 977*28f6c2f2SEnji Cooper EXPECT_CALL(helper, Call(Ne(ByRef(m)))); 978*28f6c2f2SEnji Cooper helper.Call(MoveOnly(1)); 979*28f6c2f2SEnji Cooper EXPECT_CALL(helper, Call(Le(ByRef(m)))); 980*28f6c2f2SEnji Cooper helper.Call(MoveOnly(0)); 981*28f6c2f2SEnji Cooper EXPECT_CALL(helper, Call(Lt(ByRef(m)))); 982*28f6c2f2SEnji Cooper helper.Call(MoveOnly(-1)); 983*28f6c2f2SEnji Cooper EXPECT_CALL(helper, Call(Ge(ByRef(m)))); 984*28f6c2f2SEnji Cooper helper.Call(MoveOnly(0)); 985*28f6c2f2SEnji Cooper EXPECT_CALL(helper, Call(Gt(ByRef(m)))); 986*28f6c2f2SEnji Cooper helper.Call(MoveOnly(1)); 987*28f6c2f2SEnji Cooper } 988*28f6c2f2SEnji Cooper 989*28f6c2f2SEnji Cooper TEST(IsEmptyTest, MatchesContainer) { 990*28f6c2f2SEnji Cooper const Matcher<std::vector<int>> m = IsEmpty(); 991*28f6c2f2SEnji Cooper std::vector<int> a = {}; 992*28f6c2f2SEnji Cooper std::vector<int> b = {1}; 993*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(a)); 994*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(b)); 995*28f6c2f2SEnji Cooper } 996*28f6c2f2SEnji Cooper 997*28f6c2f2SEnji Cooper TEST(IsEmptyTest, MatchesStdString) { 998*28f6c2f2SEnji Cooper const Matcher<std::string> m = IsEmpty(); 999*28f6c2f2SEnji Cooper std::string a = "z"; 1000*28f6c2f2SEnji Cooper std::string b = ""; 1001*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(a)); 1002*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(b)); 1003*28f6c2f2SEnji Cooper } 1004*28f6c2f2SEnji Cooper 1005*28f6c2f2SEnji Cooper TEST(IsEmptyTest, MatchesCString) { 1006*28f6c2f2SEnji Cooper const Matcher<const char*> m = IsEmpty(); 1007*28f6c2f2SEnji Cooper const char a[] = ""; 1008*28f6c2f2SEnji Cooper const char b[] = "x"; 1009*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(a)); 1010*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(b)); 1011*28f6c2f2SEnji Cooper } 1012*28f6c2f2SEnji Cooper 1013*28f6c2f2SEnji Cooper // Tests that IsNull() matches any NULL pointer of any type. 1014*28f6c2f2SEnji Cooper TEST(IsNullTest, MatchesNullPointer) { 1015*28f6c2f2SEnji Cooper Matcher<int*> m1 = IsNull(); 1016*28f6c2f2SEnji Cooper int* p1 = nullptr; 1017*28f6c2f2SEnji Cooper int n = 0; 1018*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(p1)); 1019*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(&n)); 1020*28f6c2f2SEnji Cooper 1021*28f6c2f2SEnji Cooper Matcher<const char*> m2 = IsNull(); 1022*28f6c2f2SEnji Cooper const char* p2 = nullptr; 1023*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(p2)); 1024*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches("hi")); 1025*28f6c2f2SEnji Cooper 1026*28f6c2f2SEnji Cooper Matcher<void*> m3 = IsNull(); 1027*28f6c2f2SEnji Cooper void* p3 = nullptr; 1028*28f6c2f2SEnji Cooper EXPECT_TRUE(m3.Matches(p3)); 1029*28f6c2f2SEnji Cooper EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef))); 1030*28f6c2f2SEnji Cooper } 1031*28f6c2f2SEnji Cooper 1032*28f6c2f2SEnji Cooper TEST(IsNullTest, StdFunction) { 1033*28f6c2f2SEnji Cooper const Matcher<std::function<void()>> m = IsNull(); 1034*28f6c2f2SEnji Cooper 1035*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(std::function<void()>())); 1036*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches([] {})); 1037*28f6c2f2SEnji Cooper } 1038*28f6c2f2SEnji Cooper 1039*28f6c2f2SEnji Cooper // Tests that IsNull() describes itself properly. 1040*28f6c2f2SEnji Cooper TEST(IsNullTest, CanDescribeSelf) { 1041*28f6c2f2SEnji Cooper Matcher<int*> m = IsNull(); 1042*28f6c2f2SEnji Cooper EXPECT_EQ("is NULL", Describe(m)); 1043*28f6c2f2SEnji Cooper EXPECT_EQ("isn't NULL", DescribeNegation(m)); 1044*28f6c2f2SEnji Cooper } 1045*28f6c2f2SEnji Cooper 1046*28f6c2f2SEnji Cooper // Tests that NotNull() matches any non-NULL pointer of any type. 1047*28f6c2f2SEnji Cooper TEST(NotNullTest, MatchesNonNullPointer) { 1048*28f6c2f2SEnji Cooper Matcher<int*> m1 = NotNull(); 1049*28f6c2f2SEnji Cooper int* p1 = nullptr; 1050*28f6c2f2SEnji Cooper int n = 0; 1051*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(p1)); 1052*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(&n)); 1053*28f6c2f2SEnji Cooper 1054*28f6c2f2SEnji Cooper Matcher<const char*> m2 = NotNull(); 1055*28f6c2f2SEnji Cooper const char* p2 = nullptr; 1056*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(p2)); 1057*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches("hi")); 1058*28f6c2f2SEnji Cooper } 1059*28f6c2f2SEnji Cooper 1060*28f6c2f2SEnji Cooper TEST(NotNullTest, LinkedPtr) { 1061*28f6c2f2SEnji Cooper const Matcher<std::shared_ptr<int>> m = NotNull(); 1062*28f6c2f2SEnji Cooper const std::shared_ptr<int> null_p; 1063*28f6c2f2SEnji Cooper const std::shared_ptr<int> non_null_p(new int); 1064*28f6c2f2SEnji Cooper 1065*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(null_p)); 1066*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(non_null_p)); 1067*28f6c2f2SEnji Cooper } 1068*28f6c2f2SEnji Cooper 1069*28f6c2f2SEnji Cooper TEST(NotNullTest, ReferenceToConstLinkedPtr) { 1070*28f6c2f2SEnji Cooper const Matcher<const std::shared_ptr<double>&> m = NotNull(); 1071*28f6c2f2SEnji Cooper const std::shared_ptr<double> null_p; 1072*28f6c2f2SEnji Cooper const std::shared_ptr<double> non_null_p(new double); 1073*28f6c2f2SEnji Cooper 1074*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(null_p)); 1075*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(non_null_p)); 1076*28f6c2f2SEnji Cooper } 1077*28f6c2f2SEnji Cooper 1078*28f6c2f2SEnji Cooper TEST(NotNullTest, StdFunction) { 1079*28f6c2f2SEnji Cooper const Matcher<std::function<void()>> m = NotNull(); 1080*28f6c2f2SEnji Cooper 1081*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches([] {})); 1082*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(std::function<void()>())); 1083*28f6c2f2SEnji Cooper } 1084*28f6c2f2SEnji Cooper 1085*28f6c2f2SEnji Cooper // Tests that NotNull() describes itself properly. 1086*28f6c2f2SEnji Cooper TEST(NotNullTest, CanDescribeSelf) { 1087*28f6c2f2SEnji Cooper Matcher<int*> m = NotNull(); 1088*28f6c2f2SEnji Cooper EXPECT_EQ("isn't NULL", Describe(m)); 1089*28f6c2f2SEnji Cooper } 1090*28f6c2f2SEnji Cooper 1091*28f6c2f2SEnji Cooper // Tests that Ref(variable) matches an argument that references 1092*28f6c2f2SEnji Cooper // 'variable'. 1093*28f6c2f2SEnji Cooper TEST(RefTest, MatchesSameVariable) { 1094*28f6c2f2SEnji Cooper int a = 0; 1095*28f6c2f2SEnji Cooper int b = 0; 1096*28f6c2f2SEnji Cooper Matcher<int&> m = Ref(a); 1097*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(a)); 1098*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(b)); 1099*28f6c2f2SEnji Cooper } 1100*28f6c2f2SEnji Cooper 1101*28f6c2f2SEnji Cooper // Tests that Ref(variable) describes itself properly. 1102*28f6c2f2SEnji Cooper TEST(RefTest, CanDescribeSelf) { 1103*28f6c2f2SEnji Cooper int n = 5; 1104*28f6c2f2SEnji Cooper Matcher<int&> m = Ref(n); 1105*28f6c2f2SEnji Cooper stringstream ss; 1106*28f6c2f2SEnji Cooper ss << "references the variable @" << &n << " 5"; 1107*28f6c2f2SEnji Cooper EXPECT_EQ(ss.str(), Describe(m)); 1108*28f6c2f2SEnji Cooper } 1109*28f6c2f2SEnji Cooper 1110*28f6c2f2SEnji Cooper // Test that Ref(non_const_varialbe) can be used as a matcher for a 1111*28f6c2f2SEnji Cooper // const reference. 1112*28f6c2f2SEnji Cooper TEST(RefTest, CanBeUsedAsMatcherForConstReference) { 1113*28f6c2f2SEnji Cooper int a = 0; 1114*28f6c2f2SEnji Cooper int b = 0; 1115*28f6c2f2SEnji Cooper Matcher<const int&> m = Ref(a); 1116*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(a)); 1117*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(b)); 1118*28f6c2f2SEnji Cooper } 1119*28f6c2f2SEnji Cooper 1120*28f6c2f2SEnji Cooper // Tests that Ref(variable) is covariant, i.e. Ref(derived) can be 1121*28f6c2f2SEnji Cooper // used wherever Ref(base) can be used (Ref(derived) is a sub-type 1122*28f6c2f2SEnji Cooper // of Ref(base), but not vice versa. 1123*28f6c2f2SEnji Cooper 1124*28f6c2f2SEnji Cooper TEST(RefTest, IsCovariant) { 1125*28f6c2f2SEnji Cooper Base base, base2; 1126*28f6c2f2SEnji Cooper Derived derived; 1127*28f6c2f2SEnji Cooper Matcher<const Base&> m1 = Ref(base); 1128*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(base)); 1129*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(base2)); 1130*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(derived)); 1131*28f6c2f2SEnji Cooper 1132*28f6c2f2SEnji Cooper m1 = Ref(derived); 1133*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(derived)); 1134*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(base)); 1135*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(base2)); 1136*28f6c2f2SEnji Cooper } 1137*28f6c2f2SEnji Cooper 1138*28f6c2f2SEnji Cooper TEST(RefTest, ExplainsResult) { 1139*28f6c2f2SEnji Cooper int n = 0; 1140*28f6c2f2SEnji Cooper EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n), 1141*28f6c2f2SEnji Cooper StartsWith("which is located @")); 1142*28f6c2f2SEnji Cooper 1143*28f6c2f2SEnji Cooper int m = 0; 1144*28f6c2f2SEnji Cooper EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m), 1145*28f6c2f2SEnji Cooper StartsWith("which is located @")); 1146*28f6c2f2SEnji Cooper } 1147*28f6c2f2SEnji Cooper 1148*28f6c2f2SEnji Cooper // Tests string comparison matchers. 1149*28f6c2f2SEnji Cooper 1150*28f6c2f2SEnji Cooper template <typename T = std::string> 1151*28f6c2f2SEnji Cooper std::string FromStringLike(internal::StringLike<T> str) { 1152*28f6c2f2SEnji Cooper return std::string(str); 1153*28f6c2f2SEnji Cooper } 1154*28f6c2f2SEnji Cooper 1155*28f6c2f2SEnji Cooper TEST(StringLike, TestConversions) { 1156*28f6c2f2SEnji Cooper EXPECT_EQ("foo", FromStringLike("foo")); 1157*28f6c2f2SEnji Cooper EXPECT_EQ("foo", FromStringLike(std::string("foo"))); 1158*28f6c2f2SEnji Cooper #if GTEST_INTERNAL_HAS_STRING_VIEW 1159*28f6c2f2SEnji Cooper EXPECT_EQ("foo", FromStringLike(internal::StringView("foo"))); 1160*28f6c2f2SEnji Cooper #endif // GTEST_INTERNAL_HAS_STRING_VIEW 1161*28f6c2f2SEnji Cooper 1162*28f6c2f2SEnji Cooper // Non deducible types. 1163*28f6c2f2SEnji Cooper EXPECT_EQ("", FromStringLike({})); 1164*28f6c2f2SEnji Cooper EXPECT_EQ("foo", FromStringLike({'f', 'o', 'o'})); 1165*28f6c2f2SEnji Cooper const char buf[] = "foo"; 1166*28f6c2f2SEnji Cooper EXPECT_EQ("foo", FromStringLike({buf, buf + 3})); 1167*28f6c2f2SEnji Cooper } 1168*28f6c2f2SEnji Cooper 1169*28f6c2f2SEnji Cooper TEST(StrEqTest, MatchesEqualString) { 1170*28f6c2f2SEnji Cooper Matcher<const char*> m = StrEq(std::string("Hello")); 1171*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches("Hello")); 1172*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches("hello")); 1173*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(nullptr)); 1174*28f6c2f2SEnji Cooper 1175*28f6c2f2SEnji Cooper Matcher<const std::string&> m2 = StrEq("Hello"); 1176*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches("Hello")); 1177*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches("Hi")); 1178*28f6c2f2SEnji Cooper 1179*28f6c2f2SEnji Cooper #if GTEST_INTERNAL_HAS_STRING_VIEW 1180*28f6c2f2SEnji Cooper Matcher<const internal::StringView&> m3 = 1181*28f6c2f2SEnji Cooper StrEq(internal::StringView("Hello")); 1182*28f6c2f2SEnji Cooper EXPECT_TRUE(m3.Matches(internal::StringView("Hello"))); 1183*28f6c2f2SEnji Cooper EXPECT_FALSE(m3.Matches(internal::StringView("hello"))); 1184*28f6c2f2SEnji Cooper EXPECT_FALSE(m3.Matches(internal::StringView())); 1185*28f6c2f2SEnji Cooper 1186*28f6c2f2SEnji Cooper Matcher<const internal::StringView&> m_empty = StrEq(""); 1187*28f6c2f2SEnji Cooper EXPECT_TRUE(m_empty.Matches(internal::StringView(""))); 1188*28f6c2f2SEnji Cooper EXPECT_TRUE(m_empty.Matches(internal::StringView())); 1189*28f6c2f2SEnji Cooper EXPECT_FALSE(m_empty.Matches(internal::StringView("hello"))); 1190*28f6c2f2SEnji Cooper #endif // GTEST_INTERNAL_HAS_STRING_VIEW 1191*28f6c2f2SEnji Cooper } 1192*28f6c2f2SEnji Cooper 1193*28f6c2f2SEnji Cooper TEST(StrEqTest, CanDescribeSelf) { 1194*28f6c2f2SEnji Cooper Matcher<std::string> m = StrEq("Hi-\'\"?\\\a\b\f\n\r\t\v\xD3"); 1195*28f6c2f2SEnji Cooper EXPECT_EQ("is equal to \"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"", 1196*28f6c2f2SEnji Cooper Describe(m)); 1197*28f6c2f2SEnji Cooper 1198*28f6c2f2SEnji Cooper std::string str("01204500800"); 1199*28f6c2f2SEnji Cooper str[3] = '\0'; 1200*28f6c2f2SEnji Cooper Matcher<std::string> m2 = StrEq(str); 1201*28f6c2f2SEnji Cooper EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2)); 1202*28f6c2f2SEnji Cooper str[0] = str[6] = str[7] = str[9] = str[10] = '\0'; 1203*28f6c2f2SEnji Cooper Matcher<std::string> m3 = StrEq(str); 1204*28f6c2f2SEnji Cooper EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3)); 1205*28f6c2f2SEnji Cooper } 1206*28f6c2f2SEnji Cooper 1207*28f6c2f2SEnji Cooper TEST(StrNeTest, MatchesUnequalString) { 1208*28f6c2f2SEnji Cooper Matcher<const char*> m = StrNe("Hello"); 1209*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches("")); 1210*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(nullptr)); 1211*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches("Hello")); 1212*28f6c2f2SEnji Cooper 1213*28f6c2f2SEnji Cooper Matcher<std::string> m2 = StrNe(std::string("Hello")); 1214*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches("hello")); 1215*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches("Hello")); 1216*28f6c2f2SEnji Cooper 1217*28f6c2f2SEnji Cooper #if GTEST_INTERNAL_HAS_STRING_VIEW 1218*28f6c2f2SEnji Cooper Matcher<const internal::StringView> m3 = StrNe(internal::StringView("Hello")); 1219*28f6c2f2SEnji Cooper EXPECT_TRUE(m3.Matches(internal::StringView(""))); 1220*28f6c2f2SEnji Cooper EXPECT_TRUE(m3.Matches(internal::StringView())); 1221*28f6c2f2SEnji Cooper EXPECT_FALSE(m3.Matches(internal::StringView("Hello"))); 1222*28f6c2f2SEnji Cooper #endif // GTEST_INTERNAL_HAS_STRING_VIEW 1223*28f6c2f2SEnji Cooper } 1224*28f6c2f2SEnji Cooper 1225*28f6c2f2SEnji Cooper TEST(StrNeTest, CanDescribeSelf) { 1226*28f6c2f2SEnji Cooper Matcher<const char*> m = StrNe("Hi"); 1227*28f6c2f2SEnji Cooper EXPECT_EQ("isn't equal to \"Hi\"", Describe(m)); 1228*28f6c2f2SEnji Cooper } 1229*28f6c2f2SEnji Cooper 1230*28f6c2f2SEnji Cooper TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) { 1231*28f6c2f2SEnji Cooper Matcher<const char*> m = StrCaseEq(std::string("Hello")); 1232*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches("Hello")); 1233*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches("hello")); 1234*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches("Hi")); 1235*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(nullptr)); 1236*28f6c2f2SEnji Cooper 1237*28f6c2f2SEnji Cooper Matcher<const std::string&> m2 = StrCaseEq("Hello"); 1238*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches("hello")); 1239*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches("Hi")); 1240*28f6c2f2SEnji Cooper 1241*28f6c2f2SEnji Cooper #if GTEST_INTERNAL_HAS_STRING_VIEW 1242*28f6c2f2SEnji Cooper Matcher<const internal::StringView&> m3 = 1243*28f6c2f2SEnji Cooper StrCaseEq(internal::StringView("Hello")); 1244*28f6c2f2SEnji Cooper EXPECT_TRUE(m3.Matches(internal::StringView("Hello"))); 1245*28f6c2f2SEnji Cooper EXPECT_TRUE(m3.Matches(internal::StringView("hello"))); 1246*28f6c2f2SEnji Cooper EXPECT_FALSE(m3.Matches(internal::StringView("Hi"))); 1247*28f6c2f2SEnji Cooper EXPECT_FALSE(m3.Matches(internal::StringView())); 1248*28f6c2f2SEnji Cooper #endif // GTEST_INTERNAL_HAS_STRING_VIEW 1249*28f6c2f2SEnji Cooper } 1250*28f6c2f2SEnji Cooper 1251*28f6c2f2SEnji Cooper TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) { 1252*28f6c2f2SEnji Cooper std::string str1("oabocdooeoo"); 1253*28f6c2f2SEnji Cooper std::string str2("OABOCDOOEOO"); 1254*28f6c2f2SEnji Cooper Matcher<const std::string&> m0 = StrCaseEq(str1); 1255*28f6c2f2SEnji Cooper EXPECT_FALSE(m0.Matches(str2 + std::string(1, '\0'))); 1256*28f6c2f2SEnji Cooper 1257*28f6c2f2SEnji Cooper str1[3] = str2[3] = '\0'; 1258*28f6c2f2SEnji Cooper Matcher<const std::string&> m1 = StrCaseEq(str1); 1259*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(str2)); 1260*28f6c2f2SEnji Cooper 1261*28f6c2f2SEnji Cooper str1[0] = str1[6] = str1[7] = str1[10] = '\0'; 1262*28f6c2f2SEnji Cooper str2[0] = str2[6] = str2[7] = str2[10] = '\0'; 1263*28f6c2f2SEnji Cooper Matcher<const std::string&> m2 = StrCaseEq(str1); 1264*28f6c2f2SEnji Cooper str1[9] = str2[9] = '\0'; 1265*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(str2)); 1266*28f6c2f2SEnji Cooper 1267*28f6c2f2SEnji Cooper Matcher<const std::string&> m3 = StrCaseEq(str1); 1268*28f6c2f2SEnji Cooper EXPECT_TRUE(m3.Matches(str2)); 1269*28f6c2f2SEnji Cooper 1270*28f6c2f2SEnji Cooper EXPECT_FALSE(m3.Matches(str2 + "x")); 1271*28f6c2f2SEnji Cooper str2.append(1, '\0'); 1272*28f6c2f2SEnji Cooper EXPECT_FALSE(m3.Matches(str2)); 1273*28f6c2f2SEnji Cooper EXPECT_FALSE(m3.Matches(std::string(str2, 0, 9))); 1274*28f6c2f2SEnji Cooper } 1275*28f6c2f2SEnji Cooper 1276*28f6c2f2SEnji Cooper TEST(StrCaseEqTest, CanDescribeSelf) { 1277*28f6c2f2SEnji Cooper Matcher<std::string> m = StrCaseEq("Hi"); 1278*28f6c2f2SEnji Cooper EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m)); 1279*28f6c2f2SEnji Cooper } 1280*28f6c2f2SEnji Cooper 1281*28f6c2f2SEnji Cooper TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) { 1282*28f6c2f2SEnji Cooper Matcher<const char*> m = StrCaseNe("Hello"); 1283*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches("Hi")); 1284*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(nullptr)); 1285*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches("Hello")); 1286*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches("hello")); 1287*28f6c2f2SEnji Cooper 1288*28f6c2f2SEnji Cooper Matcher<std::string> m2 = StrCaseNe(std::string("Hello")); 1289*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches("")); 1290*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches("Hello")); 1291*28f6c2f2SEnji Cooper 1292*28f6c2f2SEnji Cooper #if GTEST_INTERNAL_HAS_STRING_VIEW 1293*28f6c2f2SEnji Cooper Matcher<const internal::StringView> m3 = 1294*28f6c2f2SEnji Cooper StrCaseNe(internal::StringView("Hello")); 1295*28f6c2f2SEnji Cooper EXPECT_TRUE(m3.Matches(internal::StringView("Hi"))); 1296*28f6c2f2SEnji Cooper EXPECT_TRUE(m3.Matches(internal::StringView())); 1297*28f6c2f2SEnji Cooper EXPECT_FALSE(m3.Matches(internal::StringView("Hello"))); 1298*28f6c2f2SEnji Cooper EXPECT_FALSE(m3.Matches(internal::StringView("hello"))); 1299*28f6c2f2SEnji Cooper #endif // GTEST_INTERNAL_HAS_STRING_VIEW 1300*28f6c2f2SEnji Cooper } 1301*28f6c2f2SEnji Cooper 1302*28f6c2f2SEnji Cooper TEST(StrCaseNeTest, CanDescribeSelf) { 1303*28f6c2f2SEnji Cooper Matcher<const char*> m = StrCaseNe("Hi"); 1304*28f6c2f2SEnji Cooper EXPECT_EQ("isn't equal to (ignoring case) \"Hi\"", Describe(m)); 1305*28f6c2f2SEnji Cooper } 1306*28f6c2f2SEnji Cooper 1307*28f6c2f2SEnji Cooper // Tests that HasSubstr() works for matching string-typed values. 1308*28f6c2f2SEnji Cooper TEST(HasSubstrTest, WorksForStringClasses) { 1309*28f6c2f2SEnji Cooper const Matcher<std::string> m1 = HasSubstr("foo"); 1310*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(std::string("I love food."))); 1311*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(std::string("tofo"))); 1312*28f6c2f2SEnji Cooper 1313*28f6c2f2SEnji Cooper const Matcher<const std::string&> m2 = HasSubstr("foo"); 1314*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(std::string("I love food."))); 1315*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(std::string("tofo"))); 1316*28f6c2f2SEnji Cooper 1317*28f6c2f2SEnji Cooper const Matcher<std::string> m_empty = HasSubstr(""); 1318*28f6c2f2SEnji Cooper EXPECT_TRUE(m_empty.Matches(std::string())); 1319*28f6c2f2SEnji Cooper EXPECT_TRUE(m_empty.Matches(std::string("not empty"))); 1320*28f6c2f2SEnji Cooper } 1321*28f6c2f2SEnji Cooper 1322*28f6c2f2SEnji Cooper // Tests that HasSubstr() works for matching C-string-typed values. 1323*28f6c2f2SEnji Cooper TEST(HasSubstrTest, WorksForCStrings) { 1324*28f6c2f2SEnji Cooper const Matcher<char*> m1 = HasSubstr("foo"); 1325*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food."))); 1326*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo"))); 1327*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(nullptr)); 1328*28f6c2f2SEnji Cooper 1329*28f6c2f2SEnji Cooper const Matcher<const char*> m2 = HasSubstr("foo"); 1330*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches("I love food.")); 1331*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches("tofo")); 1332*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(nullptr)); 1333*28f6c2f2SEnji Cooper 1334*28f6c2f2SEnji Cooper const Matcher<const char*> m_empty = HasSubstr(""); 1335*28f6c2f2SEnji Cooper EXPECT_TRUE(m_empty.Matches("not empty")); 1336*28f6c2f2SEnji Cooper EXPECT_TRUE(m_empty.Matches("")); 1337*28f6c2f2SEnji Cooper EXPECT_FALSE(m_empty.Matches(nullptr)); 1338*28f6c2f2SEnji Cooper } 1339*28f6c2f2SEnji Cooper 1340*28f6c2f2SEnji Cooper #if GTEST_INTERNAL_HAS_STRING_VIEW 1341*28f6c2f2SEnji Cooper // Tests that HasSubstr() works for matching StringView-typed values. 1342*28f6c2f2SEnji Cooper TEST(HasSubstrTest, WorksForStringViewClasses) { 1343*28f6c2f2SEnji Cooper const Matcher<internal::StringView> m1 = 1344*28f6c2f2SEnji Cooper HasSubstr(internal::StringView("foo")); 1345*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(internal::StringView("I love food."))); 1346*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(internal::StringView("tofo"))); 1347*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(internal::StringView())); 1348*28f6c2f2SEnji Cooper 1349*28f6c2f2SEnji Cooper const Matcher<const internal::StringView&> m2 = HasSubstr("foo"); 1350*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(internal::StringView("I love food."))); 1351*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(internal::StringView("tofo"))); 1352*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(internal::StringView())); 1353*28f6c2f2SEnji Cooper 1354*28f6c2f2SEnji Cooper const Matcher<const internal::StringView&> m3 = HasSubstr(""); 1355*28f6c2f2SEnji Cooper EXPECT_TRUE(m3.Matches(internal::StringView("foo"))); 1356*28f6c2f2SEnji Cooper EXPECT_TRUE(m3.Matches(internal::StringView(""))); 1357*28f6c2f2SEnji Cooper EXPECT_TRUE(m3.Matches(internal::StringView())); 1358*28f6c2f2SEnji Cooper } 1359*28f6c2f2SEnji Cooper #endif // GTEST_INTERNAL_HAS_STRING_VIEW 1360*28f6c2f2SEnji Cooper 1361*28f6c2f2SEnji Cooper // Tests that HasSubstr(s) describes itself properly. 1362*28f6c2f2SEnji Cooper TEST(HasSubstrTest, CanDescribeSelf) { 1363*28f6c2f2SEnji Cooper Matcher<std::string> m = HasSubstr("foo\n\""); 1364*28f6c2f2SEnji Cooper EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m)); 1365*28f6c2f2SEnji Cooper } 1366*28f6c2f2SEnji Cooper 1367*28f6c2f2SEnji Cooper INSTANTIATE_GTEST_MATCHER_TEST_P(KeyTest); 1368*28f6c2f2SEnji Cooper 1369*28f6c2f2SEnji Cooper TEST(KeyTest, CanDescribeSelf) { 1370*28f6c2f2SEnji Cooper Matcher<const pair<std::string, int>&> m = Key("foo"); 1371*28f6c2f2SEnji Cooper EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m)); 1372*28f6c2f2SEnji Cooper EXPECT_EQ("doesn't have a key that is equal to \"foo\"", DescribeNegation(m)); 1373*28f6c2f2SEnji Cooper } 1374*28f6c2f2SEnji Cooper 1375*28f6c2f2SEnji Cooper TEST_P(KeyTestP, ExplainsResult) { 1376*28f6c2f2SEnji Cooper Matcher<pair<int, bool>> m = Key(GreaterThan(10)); 1377*28f6c2f2SEnji Cooper EXPECT_EQ("whose first field is a value which is 5 less than 10", 1378*28f6c2f2SEnji Cooper Explain(m, make_pair(5, true))); 1379*28f6c2f2SEnji Cooper EXPECT_EQ("whose first field is a value which is 5 more than 10", 1380*28f6c2f2SEnji Cooper Explain(m, make_pair(15, true))); 1381*28f6c2f2SEnji Cooper } 1382*28f6c2f2SEnji Cooper 1383*28f6c2f2SEnji Cooper TEST(KeyTest, MatchesCorrectly) { 1384*28f6c2f2SEnji Cooper pair<int, std::string> p(25, "foo"); 1385*28f6c2f2SEnji Cooper EXPECT_THAT(p, Key(25)); 1386*28f6c2f2SEnji Cooper EXPECT_THAT(p, Not(Key(42))); 1387*28f6c2f2SEnji Cooper EXPECT_THAT(p, Key(Ge(20))); 1388*28f6c2f2SEnji Cooper EXPECT_THAT(p, Not(Key(Lt(25)))); 1389*28f6c2f2SEnji Cooper } 1390*28f6c2f2SEnji Cooper 1391*28f6c2f2SEnji Cooper TEST(KeyTest, WorksWithMoveOnly) { 1392*28f6c2f2SEnji Cooper pair<std::unique_ptr<int>, std::unique_ptr<int>> p; 1393*28f6c2f2SEnji Cooper EXPECT_THAT(p, Key(Eq(nullptr))); 1394*28f6c2f2SEnji Cooper } 1395*28f6c2f2SEnji Cooper 1396*28f6c2f2SEnji Cooper INSTANTIATE_GTEST_MATCHER_TEST_P(PairTest); 1397*28f6c2f2SEnji Cooper 1398*28f6c2f2SEnji Cooper template <size_t I> 1399*28f6c2f2SEnji Cooper struct Tag {}; 1400*28f6c2f2SEnji Cooper 1401*28f6c2f2SEnji Cooper struct PairWithGet { 1402*28f6c2f2SEnji Cooper int member_1; 1403*28f6c2f2SEnji Cooper std::string member_2; 1404*28f6c2f2SEnji Cooper using first_type = int; 1405*28f6c2f2SEnji Cooper using second_type = std::string; 1406*28f6c2f2SEnji Cooper 1407*28f6c2f2SEnji Cooper const int& GetImpl(Tag<0>) const { return member_1; } 1408*28f6c2f2SEnji Cooper const std::string& GetImpl(Tag<1>) const { return member_2; } 1409*28f6c2f2SEnji Cooper }; 1410*28f6c2f2SEnji Cooper template <size_t I> 1411*28f6c2f2SEnji Cooper auto get(const PairWithGet& value) -> decltype(value.GetImpl(Tag<I>())) { 1412*28f6c2f2SEnji Cooper return value.GetImpl(Tag<I>()); 1413*28f6c2f2SEnji Cooper } 1414*28f6c2f2SEnji Cooper TEST(PairTest, MatchesPairWithGetCorrectly) { 1415*28f6c2f2SEnji Cooper PairWithGet p{25, "foo"}; 1416*28f6c2f2SEnji Cooper EXPECT_THAT(p, Key(25)); 1417*28f6c2f2SEnji Cooper EXPECT_THAT(p, Not(Key(42))); 1418*28f6c2f2SEnji Cooper EXPECT_THAT(p, Key(Ge(20))); 1419*28f6c2f2SEnji Cooper EXPECT_THAT(p, Not(Key(Lt(25)))); 1420*28f6c2f2SEnji Cooper 1421*28f6c2f2SEnji Cooper std::vector<PairWithGet> v = {{11, "Foo"}, {29, "gMockIsBestMock"}}; 1422*28f6c2f2SEnji Cooper EXPECT_THAT(v, Contains(Key(29))); 1423*28f6c2f2SEnji Cooper } 1424*28f6c2f2SEnji Cooper 1425*28f6c2f2SEnji Cooper TEST(KeyTest, SafelyCastsInnerMatcher) { 1426*28f6c2f2SEnji Cooper Matcher<int> is_positive = Gt(0); 1427*28f6c2f2SEnji Cooper Matcher<int> is_negative = Lt(0); 1428*28f6c2f2SEnji Cooper pair<char, bool> p('a', true); 1429*28f6c2f2SEnji Cooper EXPECT_THAT(p, Key(is_positive)); 1430*28f6c2f2SEnji Cooper EXPECT_THAT(p, Not(Key(is_negative))); 1431*28f6c2f2SEnji Cooper } 1432*28f6c2f2SEnji Cooper 1433*28f6c2f2SEnji Cooper TEST(KeyTest, InsideContainsUsingMap) { 1434*28f6c2f2SEnji Cooper map<int, char> container; 1435*28f6c2f2SEnji Cooper container.insert(make_pair(1, 'a')); 1436*28f6c2f2SEnji Cooper container.insert(make_pair(2, 'b')); 1437*28f6c2f2SEnji Cooper container.insert(make_pair(4, 'c')); 1438*28f6c2f2SEnji Cooper EXPECT_THAT(container, Contains(Key(1))); 1439*28f6c2f2SEnji Cooper EXPECT_THAT(container, Not(Contains(Key(3)))); 1440*28f6c2f2SEnji Cooper } 1441*28f6c2f2SEnji Cooper 1442*28f6c2f2SEnji Cooper TEST(KeyTest, InsideContainsUsingMultimap) { 1443*28f6c2f2SEnji Cooper multimap<int, char> container; 1444*28f6c2f2SEnji Cooper container.insert(make_pair(1, 'a')); 1445*28f6c2f2SEnji Cooper container.insert(make_pair(2, 'b')); 1446*28f6c2f2SEnji Cooper container.insert(make_pair(4, 'c')); 1447*28f6c2f2SEnji Cooper 1448*28f6c2f2SEnji Cooper EXPECT_THAT(container, Not(Contains(Key(25)))); 1449*28f6c2f2SEnji Cooper container.insert(make_pair(25, 'd')); 1450*28f6c2f2SEnji Cooper EXPECT_THAT(container, Contains(Key(25))); 1451*28f6c2f2SEnji Cooper container.insert(make_pair(25, 'e')); 1452*28f6c2f2SEnji Cooper EXPECT_THAT(container, Contains(Key(25))); 1453*28f6c2f2SEnji Cooper 1454*28f6c2f2SEnji Cooper EXPECT_THAT(container, Contains(Key(1))); 1455*28f6c2f2SEnji Cooper EXPECT_THAT(container, Not(Contains(Key(3)))); 1456*28f6c2f2SEnji Cooper } 1457*28f6c2f2SEnji Cooper 1458*28f6c2f2SEnji Cooper TEST(PairTest, Typing) { 1459*28f6c2f2SEnji Cooper // Test verifies the following type conversions can be compiled. 1460*28f6c2f2SEnji Cooper Matcher<const pair<const char*, int>&> m1 = Pair("foo", 42); 1461*28f6c2f2SEnji Cooper Matcher<const pair<const char*, int>> m2 = Pair("foo", 42); 1462*28f6c2f2SEnji Cooper Matcher<pair<const char*, int>> m3 = Pair("foo", 42); 1463*28f6c2f2SEnji Cooper 1464*28f6c2f2SEnji Cooper Matcher<pair<int, const std::string>> m4 = Pair(25, "42"); 1465*28f6c2f2SEnji Cooper Matcher<pair<const std::string, int>> m5 = Pair("25", 42); 1466*28f6c2f2SEnji Cooper } 1467*28f6c2f2SEnji Cooper 1468*28f6c2f2SEnji Cooper TEST(PairTest, CanDescribeSelf) { 1469*28f6c2f2SEnji Cooper Matcher<const pair<std::string, int>&> m1 = Pair("foo", 42); 1470*28f6c2f2SEnji Cooper EXPECT_EQ( 1471*28f6c2f2SEnji Cooper "has a first field that is equal to \"foo\"" 1472*28f6c2f2SEnji Cooper ", and has a second field that is equal to 42", 1473*28f6c2f2SEnji Cooper Describe(m1)); 1474*28f6c2f2SEnji Cooper EXPECT_EQ( 1475*28f6c2f2SEnji Cooper "has a first field that isn't equal to \"foo\"" 1476*28f6c2f2SEnji Cooper ", or has a second field that isn't equal to 42", 1477*28f6c2f2SEnji Cooper DescribeNegation(m1)); 1478*28f6c2f2SEnji Cooper // Double and triple negation (1 or 2 times not and description of negation). 1479*28f6c2f2SEnji Cooper Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42)); 1480*28f6c2f2SEnji Cooper EXPECT_EQ( 1481*28f6c2f2SEnji Cooper "has a first field that isn't equal to 13" 1482*28f6c2f2SEnji Cooper ", and has a second field that is equal to 42", 1483*28f6c2f2SEnji Cooper DescribeNegation(m2)); 1484*28f6c2f2SEnji Cooper } 1485*28f6c2f2SEnji Cooper 1486*28f6c2f2SEnji Cooper TEST_P(PairTestP, CanExplainMatchResultTo) { 1487*28f6c2f2SEnji Cooper // If neither field matches, Pair() should explain about the first 1488*28f6c2f2SEnji Cooper // field. 1489*28f6c2f2SEnji Cooper const Matcher<pair<int, int>> m = Pair(GreaterThan(0), GreaterThan(0)); 1490*28f6c2f2SEnji Cooper EXPECT_EQ("whose first field does not match, which is 1 less than 0", 1491*28f6c2f2SEnji Cooper Explain(m, make_pair(-1, -2))); 1492*28f6c2f2SEnji Cooper 1493*28f6c2f2SEnji Cooper // If the first field matches but the second doesn't, Pair() should 1494*28f6c2f2SEnji Cooper // explain about the second field. 1495*28f6c2f2SEnji Cooper EXPECT_EQ("whose second field does not match, which is 2 less than 0", 1496*28f6c2f2SEnji Cooper Explain(m, make_pair(1, -2))); 1497*28f6c2f2SEnji Cooper 1498*28f6c2f2SEnji Cooper // If the first field doesn't match but the second does, Pair() 1499*28f6c2f2SEnji Cooper // should explain about the first field. 1500*28f6c2f2SEnji Cooper EXPECT_EQ("whose first field does not match, which is 1 less than 0", 1501*28f6c2f2SEnji Cooper Explain(m, make_pair(-1, 2))); 1502*28f6c2f2SEnji Cooper 1503*28f6c2f2SEnji Cooper // If both fields match, Pair() should explain about them both. 1504*28f6c2f2SEnji Cooper EXPECT_EQ( 1505*28f6c2f2SEnji Cooper "whose both fields match, where the first field is a value " 1506*28f6c2f2SEnji Cooper "which is 1 more than 0, and the second field is a value " 1507*28f6c2f2SEnji Cooper "which is 2 more than 0", 1508*28f6c2f2SEnji Cooper Explain(m, make_pair(1, 2))); 1509*28f6c2f2SEnji Cooper 1510*28f6c2f2SEnji Cooper // If only the first match has an explanation, only this explanation should 1511*28f6c2f2SEnji Cooper // be printed. 1512*28f6c2f2SEnji Cooper const Matcher<pair<int, int>> explain_first = Pair(GreaterThan(0), 0); 1513*28f6c2f2SEnji Cooper EXPECT_EQ( 1514*28f6c2f2SEnji Cooper "whose both fields match, where the first field is a value " 1515*28f6c2f2SEnji Cooper "which is 1 more than 0", 1516*28f6c2f2SEnji Cooper Explain(explain_first, make_pair(1, 0))); 1517*28f6c2f2SEnji Cooper 1518*28f6c2f2SEnji Cooper // If only the second match has an explanation, only this explanation should 1519*28f6c2f2SEnji Cooper // be printed. 1520*28f6c2f2SEnji Cooper const Matcher<pair<int, int>> explain_second = Pair(0, GreaterThan(0)); 1521*28f6c2f2SEnji Cooper EXPECT_EQ( 1522*28f6c2f2SEnji Cooper "whose both fields match, where the second field is a value " 1523*28f6c2f2SEnji Cooper "which is 1 more than 0", 1524*28f6c2f2SEnji Cooper Explain(explain_second, make_pair(0, 1))); 1525*28f6c2f2SEnji Cooper } 1526*28f6c2f2SEnji Cooper 1527*28f6c2f2SEnji Cooper TEST(PairTest, MatchesCorrectly) { 1528*28f6c2f2SEnji Cooper pair<int, std::string> p(25, "foo"); 1529*28f6c2f2SEnji Cooper 1530*28f6c2f2SEnji Cooper // Both fields match. 1531*28f6c2f2SEnji Cooper EXPECT_THAT(p, Pair(25, "foo")); 1532*28f6c2f2SEnji Cooper EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o"))); 1533*28f6c2f2SEnji Cooper 1534*28f6c2f2SEnji Cooper // 'first' doesn't match, but 'second' matches. 1535*28f6c2f2SEnji Cooper EXPECT_THAT(p, Not(Pair(42, "foo"))); 1536*28f6c2f2SEnji Cooper EXPECT_THAT(p, Not(Pair(Lt(25), "foo"))); 1537*28f6c2f2SEnji Cooper 1538*28f6c2f2SEnji Cooper // 'first' matches, but 'second' doesn't match. 1539*28f6c2f2SEnji Cooper EXPECT_THAT(p, Not(Pair(25, "bar"))); 1540*28f6c2f2SEnji Cooper EXPECT_THAT(p, Not(Pair(25, Not("foo")))); 1541*28f6c2f2SEnji Cooper 1542*28f6c2f2SEnji Cooper // Neither field matches. 1543*28f6c2f2SEnji Cooper EXPECT_THAT(p, Not(Pair(13, "bar"))); 1544*28f6c2f2SEnji Cooper EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a")))); 1545*28f6c2f2SEnji Cooper } 1546*28f6c2f2SEnji Cooper 1547*28f6c2f2SEnji Cooper TEST(PairTest, WorksWithMoveOnly) { 1548*28f6c2f2SEnji Cooper pair<std::unique_ptr<int>, std::unique_ptr<int>> p; 1549*28f6c2f2SEnji Cooper p.second = std::make_unique<int>(7); 1550*28f6c2f2SEnji Cooper EXPECT_THAT(p, Pair(Eq(nullptr), Ne(nullptr))); 1551*28f6c2f2SEnji Cooper } 1552*28f6c2f2SEnji Cooper 1553*28f6c2f2SEnji Cooper TEST(PairTest, SafelyCastsInnerMatchers) { 1554*28f6c2f2SEnji Cooper Matcher<int> is_positive = Gt(0); 1555*28f6c2f2SEnji Cooper Matcher<int> is_negative = Lt(0); 1556*28f6c2f2SEnji Cooper pair<char, bool> p('a', true); 1557*28f6c2f2SEnji Cooper EXPECT_THAT(p, Pair(is_positive, _)); 1558*28f6c2f2SEnji Cooper EXPECT_THAT(p, Not(Pair(is_negative, _))); 1559*28f6c2f2SEnji Cooper EXPECT_THAT(p, Pair(_, is_positive)); 1560*28f6c2f2SEnji Cooper EXPECT_THAT(p, Not(Pair(_, is_negative))); 1561*28f6c2f2SEnji Cooper } 1562*28f6c2f2SEnji Cooper 1563*28f6c2f2SEnji Cooper TEST(PairTest, InsideContainsUsingMap) { 1564*28f6c2f2SEnji Cooper map<int, char> container; 1565*28f6c2f2SEnji Cooper container.insert(make_pair(1, 'a')); 1566*28f6c2f2SEnji Cooper container.insert(make_pair(2, 'b')); 1567*28f6c2f2SEnji Cooper container.insert(make_pair(4, 'c')); 1568*28f6c2f2SEnji Cooper EXPECT_THAT(container, Contains(Pair(1, 'a'))); 1569*28f6c2f2SEnji Cooper EXPECT_THAT(container, Contains(Pair(1, _))); 1570*28f6c2f2SEnji Cooper EXPECT_THAT(container, Contains(Pair(_, 'a'))); 1571*28f6c2f2SEnji Cooper EXPECT_THAT(container, Not(Contains(Pair(3, _)))); 1572*28f6c2f2SEnji Cooper } 1573*28f6c2f2SEnji Cooper 1574*28f6c2f2SEnji Cooper INSTANTIATE_GTEST_MATCHER_TEST_P(FieldsAreTest); 1575*28f6c2f2SEnji Cooper 1576*28f6c2f2SEnji Cooper TEST(FieldsAreTest, MatchesCorrectly) { 1577*28f6c2f2SEnji Cooper std::tuple<int, std::string, double> p(25, "foo", .5); 1578*28f6c2f2SEnji Cooper 1579*28f6c2f2SEnji Cooper // All fields match. 1580*28f6c2f2SEnji Cooper EXPECT_THAT(p, FieldsAre(25, "foo", .5)); 1581*28f6c2f2SEnji Cooper EXPECT_THAT(p, FieldsAre(Ge(20), HasSubstr("o"), DoubleEq(.5))); 1582*28f6c2f2SEnji Cooper 1583*28f6c2f2SEnji Cooper // Some don't match. 1584*28f6c2f2SEnji Cooper EXPECT_THAT(p, Not(FieldsAre(26, "foo", .5))); 1585*28f6c2f2SEnji Cooper EXPECT_THAT(p, Not(FieldsAre(25, "fo", .5))); 1586*28f6c2f2SEnji Cooper EXPECT_THAT(p, Not(FieldsAre(25, "foo", .6))); 1587*28f6c2f2SEnji Cooper } 1588*28f6c2f2SEnji Cooper 1589*28f6c2f2SEnji Cooper TEST(FieldsAreTest, CanDescribeSelf) { 1590*28f6c2f2SEnji Cooper Matcher<const pair<std::string, int>&> m1 = FieldsAre("foo", 42); 1591*28f6c2f2SEnji Cooper EXPECT_EQ( 1592*28f6c2f2SEnji Cooper "has field #0 that is equal to \"foo\"" 1593*28f6c2f2SEnji Cooper ", and has field #1 that is equal to 42", 1594*28f6c2f2SEnji Cooper Describe(m1)); 1595*28f6c2f2SEnji Cooper EXPECT_EQ( 1596*28f6c2f2SEnji Cooper "has field #0 that isn't equal to \"foo\"" 1597*28f6c2f2SEnji Cooper ", or has field #1 that isn't equal to 42", 1598*28f6c2f2SEnji Cooper DescribeNegation(m1)); 1599*28f6c2f2SEnji Cooper } 1600*28f6c2f2SEnji Cooper 1601*28f6c2f2SEnji Cooper TEST_P(FieldsAreTestP, CanExplainMatchResultTo) { 1602*28f6c2f2SEnji Cooper // The first one that fails is the one that gives the error. 1603*28f6c2f2SEnji Cooper Matcher<std::tuple<int, int, int>> m = 1604*28f6c2f2SEnji Cooper FieldsAre(GreaterThan(0), GreaterThan(0), GreaterThan(0)); 1605*28f6c2f2SEnji Cooper 1606*28f6c2f2SEnji Cooper EXPECT_EQ("whose field #0 does not match, which is 1 less than 0", 1607*28f6c2f2SEnji Cooper Explain(m, std::make_tuple(-1, -2, -3))); 1608*28f6c2f2SEnji Cooper EXPECT_EQ("whose field #1 does not match, which is 2 less than 0", 1609*28f6c2f2SEnji Cooper Explain(m, std::make_tuple(1, -2, -3))); 1610*28f6c2f2SEnji Cooper EXPECT_EQ("whose field #2 does not match, which is 3 less than 0", 1611*28f6c2f2SEnji Cooper Explain(m, std::make_tuple(1, 2, -3))); 1612*28f6c2f2SEnji Cooper 1613*28f6c2f2SEnji Cooper // If they all match, we get a long explanation of success. 1614*28f6c2f2SEnji Cooper EXPECT_EQ( 1615*28f6c2f2SEnji Cooper "whose all elements match, " 1616*28f6c2f2SEnji Cooper "where field #0 is a value which is 1 more than 0" 1617*28f6c2f2SEnji Cooper ", and field #1 is a value which is 2 more than 0" 1618*28f6c2f2SEnji Cooper ", and field #2 is a value which is 3 more than 0", 1619*28f6c2f2SEnji Cooper Explain(m, std::make_tuple(1, 2, 3))); 1620*28f6c2f2SEnji Cooper 1621*28f6c2f2SEnji Cooper // Only print those that have an explanation. 1622*28f6c2f2SEnji Cooper m = FieldsAre(GreaterThan(0), 0, GreaterThan(0)); 1623*28f6c2f2SEnji Cooper EXPECT_EQ( 1624*28f6c2f2SEnji Cooper "whose all elements match, " 1625*28f6c2f2SEnji Cooper "where field #0 is a value which is 1 more than 0" 1626*28f6c2f2SEnji Cooper ", and field #2 is a value which is 3 more than 0", 1627*28f6c2f2SEnji Cooper Explain(m, std::make_tuple(1, 0, 3))); 1628*28f6c2f2SEnji Cooper 1629*28f6c2f2SEnji Cooper // If only one has an explanation, then print that one. 1630*28f6c2f2SEnji Cooper m = FieldsAre(0, GreaterThan(0), 0); 1631*28f6c2f2SEnji Cooper EXPECT_EQ( 1632*28f6c2f2SEnji Cooper "whose all elements match, " 1633*28f6c2f2SEnji Cooper "where field #1 is a value which is 1 more than 0", 1634*28f6c2f2SEnji Cooper Explain(m, std::make_tuple(0, 1, 0))); 1635*28f6c2f2SEnji Cooper } 1636*28f6c2f2SEnji Cooper 1637*28f6c2f2SEnji Cooper #if defined(__cpp_structured_bindings) && __cpp_structured_bindings >= 201606 1638*28f6c2f2SEnji Cooper TEST(FieldsAreTest, StructuredBindings) { 1639*28f6c2f2SEnji Cooper // testing::FieldsAre can also match aggregates and such with C++17 and up. 1640*28f6c2f2SEnji Cooper struct MyType { 1641*28f6c2f2SEnji Cooper int i; 1642*28f6c2f2SEnji Cooper std::string str; 1643*28f6c2f2SEnji Cooper }; 1644*28f6c2f2SEnji Cooper EXPECT_THAT((MyType{17, "foo"}), FieldsAre(Eq(17), HasSubstr("oo"))); 1645*28f6c2f2SEnji Cooper 1646*28f6c2f2SEnji Cooper // Test all the supported arities. 1647*28f6c2f2SEnji Cooper struct MyVarType1 { 1648*28f6c2f2SEnji Cooper int a; 1649*28f6c2f2SEnji Cooper }; 1650*28f6c2f2SEnji Cooper EXPECT_THAT(MyVarType1{}, FieldsAre(0)); 1651*28f6c2f2SEnji Cooper struct MyVarType2 { 1652*28f6c2f2SEnji Cooper int a, b; 1653*28f6c2f2SEnji Cooper }; 1654*28f6c2f2SEnji Cooper EXPECT_THAT(MyVarType2{}, FieldsAre(0, 0)); 1655*28f6c2f2SEnji Cooper struct MyVarType3 { 1656*28f6c2f2SEnji Cooper int a, b, c; 1657*28f6c2f2SEnji Cooper }; 1658*28f6c2f2SEnji Cooper EXPECT_THAT(MyVarType3{}, FieldsAre(0, 0, 0)); 1659*28f6c2f2SEnji Cooper struct MyVarType4 { 1660*28f6c2f2SEnji Cooper int a, b, c, d; 1661*28f6c2f2SEnji Cooper }; 1662*28f6c2f2SEnji Cooper EXPECT_THAT(MyVarType4{}, FieldsAre(0, 0, 0, 0)); 1663*28f6c2f2SEnji Cooper struct MyVarType5 { 1664*28f6c2f2SEnji Cooper int a, b, c, d, e; 1665*28f6c2f2SEnji Cooper }; 1666*28f6c2f2SEnji Cooper EXPECT_THAT(MyVarType5{}, FieldsAre(0, 0, 0, 0, 0)); 1667*28f6c2f2SEnji Cooper struct MyVarType6 { 1668*28f6c2f2SEnji Cooper int a, b, c, d, e, f; 1669*28f6c2f2SEnji Cooper }; 1670*28f6c2f2SEnji Cooper EXPECT_THAT(MyVarType6{}, FieldsAre(0, 0, 0, 0, 0, 0)); 1671*28f6c2f2SEnji Cooper struct MyVarType7 { 1672*28f6c2f2SEnji Cooper int a, b, c, d, e, f, g; 1673*28f6c2f2SEnji Cooper }; 1674*28f6c2f2SEnji Cooper EXPECT_THAT(MyVarType7{}, FieldsAre(0, 0, 0, 0, 0, 0, 0)); 1675*28f6c2f2SEnji Cooper struct MyVarType8 { 1676*28f6c2f2SEnji Cooper int a, b, c, d, e, f, g, h; 1677*28f6c2f2SEnji Cooper }; 1678*28f6c2f2SEnji Cooper EXPECT_THAT(MyVarType8{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0)); 1679*28f6c2f2SEnji Cooper struct MyVarType9 { 1680*28f6c2f2SEnji Cooper int a, b, c, d, e, f, g, h, i; 1681*28f6c2f2SEnji Cooper }; 1682*28f6c2f2SEnji Cooper EXPECT_THAT(MyVarType9{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0)); 1683*28f6c2f2SEnji Cooper struct MyVarType10 { 1684*28f6c2f2SEnji Cooper int a, b, c, d, e, f, g, h, i, j; 1685*28f6c2f2SEnji Cooper }; 1686*28f6c2f2SEnji Cooper EXPECT_THAT(MyVarType10{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); 1687*28f6c2f2SEnji Cooper struct MyVarType11 { 1688*28f6c2f2SEnji Cooper int a, b, c, d, e, f, g, h, i, j, k; 1689*28f6c2f2SEnji Cooper }; 1690*28f6c2f2SEnji Cooper EXPECT_THAT(MyVarType11{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); 1691*28f6c2f2SEnji Cooper struct MyVarType12 { 1692*28f6c2f2SEnji Cooper int a, b, c, d, e, f, g, h, i, j, k, l; 1693*28f6c2f2SEnji Cooper }; 1694*28f6c2f2SEnji Cooper EXPECT_THAT(MyVarType12{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); 1695*28f6c2f2SEnji Cooper struct MyVarType13 { 1696*28f6c2f2SEnji Cooper int a, b, c, d, e, f, g, h, i, j, k, l, m; 1697*28f6c2f2SEnji Cooper }; 1698*28f6c2f2SEnji Cooper EXPECT_THAT(MyVarType13{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); 1699*28f6c2f2SEnji Cooper struct MyVarType14 { 1700*28f6c2f2SEnji Cooper int a, b, c, d, e, f, g, h, i, j, k, l, m, n; 1701*28f6c2f2SEnji Cooper }; 1702*28f6c2f2SEnji Cooper EXPECT_THAT(MyVarType14{}, 1703*28f6c2f2SEnji Cooper FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); 1704*28f6c2f2SEnji Cooper struct MyVarType15 { 1705*28f6c2f2SEnji Cooper int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o; 1706*28f6c2f2SEnji Cooper }; 1707*28f6c2f2SEnji Cooper EXPECT_THAT(MyVarType15{}, 1708*28f6c2f2SEnji Cooper FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); 1709*28f6c2f2SEnji Cooper struct MyVarType16 { 1710*28f6c2f2SEnji Cooper int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p; 1711*28f6c2f2SEnji Cooper }; 1712*28f6c2f2SEnji Cooper EXPECT_THAT(MyVarType16{}, 1713*28f6c2f2SEnji Cooper FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); 1714*28f6c2f2SEnji Cooper struct MyVarType17 { 1715*28f6c2f2SEnji Cooper int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q; 1716*28f6c2f2SEnji Cooper }; 1717*28f6c2f2SEnji Cooper EXPECT_THAT(MyVarType17{}, 1718*28f6c2f2SEnji Cooper FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); 1719*28f6c2f2SEnji Cooper struct MyVarType18 { 1720*28f6c2f2SEnji Cooper int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r; 1721*28f6c2f2SEnji Cooper }; 1722*28f6c2f2SEnji Cooper EXPECT_THAT(MyVarType18{}, 1723*28f6c2f2SEnji Cooper FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); 1724*28f6c2f2SEnji Cooper struct MyVarType19 { 1725*28f6c2f2SEnji Cooper int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s; 1726*28f6c2f2SEnji Cooper }; 1727*28f6c2f2SEnji Cooper EXPECT_THAT(MyVarType19{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1728*28f6c2f2SEnji Cooper 0, 0, 0, 0, 0)); 1729*28f6c2f2SEnji Cooper } 1730*28f6c2f2SEnji Cooper #endif 1731*28f6c2f2SEnji Cooper 1732*28f6c2f2SEnji Cooper TEST(PairTest, UseGetInsteadOfMembers) { 1733*28f6c2f2SEnji Cooper PairWithGet pair{7, "ABC"}; 1734*28f6c2f2SEnji Cooper EXPECT_THAT(pair, Pair(7, "ABC")); 1735*28f6c2f2SEnji Cooper EXPECT_THAT(pair, Pair(Ge(7), HasSubstr("AB"))); 1736*28f6c2f2SEnji Cooper EXPECT_THAT(pair, Not(Pair(Lt(7), "ABC"))); 1737*28f6c2f2SEnji Cooper 1738*28f6c2f2SEnji Cooper std::vector<PairWithGet> v = {{11, "Foo"}, {29, "gMockIsBestMock"}}; 1739*28f6c2f2SEnji Cooper EXPECT_THAT(v, 1740*28f6c2f2SEnji Cooper ElementsAre(Pair(11, std::string("Foo")), Pair(Ge(10), Not("")))); 1741*28f6c2f2SEnji Cooper } 1742*28f6c2f2SEnji Cooper 1743*28f6c2f2SEnji Cooper // Tests StartsWith(s). 1744*28f6c2f2SEnji Cooper 1745*28f6c2f2SEnji Cooper TEST(StartsWithTest, MatchesStringWithGivenPrefix) { 1746*28f6c2f2SEnji Cooper const Matcher<const char*> m1 = StartsWith(std::string("")); 1747*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches("Hi")); 1748*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches("")); 1749*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(nullptr)); 1750*28f6c2f2SEnji Cooper 1751*28f6c2f2SEnji Cooper const Matcher<const std::string&> m2 = StartsWith("Hi"); 1752*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches("Hi")); 1753*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches("Hi Hi!")); 1754*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches("High")); 1755*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches("H")); 1756*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(" Hi")); 1757*28f6c2f2SEnji Cooper 1758*28f6c2f2SEnji Cooper #if GTEST_INTERNAL_HAS_STRING_VIEW 1759*28f6c2f2SEnji Cooper const Matcher<internal::StringView> m_empty = 1760*28f6c2f2SEnji Cooper StartsWith(internal::StringView("")); 1761*28f6c2f2SEnji Cooper EXPECT_TRUE(m_empty.Matches(internal::StringView())); 1762*28f6c2f2SEnji Cooper EXPECT_TRUE(m_empty.Matches(internal::StringView(""))); 1763*28f6c2f2SEnji Cooper EXPECT_TRUE(m_empty.Matches(internal::StringView("not empty"))); 1764*28f6c2f2SEnji Cooper #endif // GTEST_INTERNAL_HAS_STRING_VIEW 1765*28f6c2f2SEnji Cooper } 1766*28f6c2f2SEnji Cooper 1767*28f6c2f2SEnji Cooper TEST(StartsWithTest, CanDescribeSelf) { 1768*28f6c2f2SEnji Cooper Matcher<const std::string> m = StartsWith("Hi"); 1769*28f6c2f2SEnji Cooper EXPECT_EQ("starts with \"Hi\"", Describe(m)); 1770*28f6c2f2SEnji Cooper } 1771*28f6c2f2SEnji Cooper 1772*28f6c2f2SEnji Cooper // Tests EndsWith(s). 1773*28f6c2f2SEnji Cooper 1774*28f6c2f2SEnji Cooper TEST(EndsWithTest, MatchesStringWithGivenSuffix) { 1775*28f6c2f2SEnji Cooper const Matcher<const char*> m1 = EndsWith(""); 1776*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches("Hi")); 1777*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches("")); 1778*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(nullptr)); 1779*28f6c2f2SEnji Cooper 1780*28f6c2f2SEnji Cooper const Matcher<const std::string&> m2 = EndsWith(std::string("Hi")); 1781*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches("Hi")); 1782*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches("Wow Hi Hi")); 1783*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches("Super Hi")); 1784*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches("i")); 1785*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches("Hi ")); 1786*28f6c2f2SEnji Cooper 1787*28f6c2f2SEnji Cooper #if GTEST_INTERNAL_HAS_STRING_VIEW 1788*28f6c2f2SEnji Cooper const Matcher<const internal::StringView&> m4 = 1789*28f6c2f2SEnji Cooper EndsWith(internal::StringView("")); 1790*28f6c2f2SEnji Cooper EXPECT_TRUE(m4.Matches("Hi")); 1791*28f6c2f2SEnji Cooper EXPECT_TRUE(m4.Matches("")); 1792*28f6c2f2SEnji Cooper EXPECT_TRUE(m4.Matches(internal::StringView())); 1793*28f6c2f2SEnji Cooper EXPECT_TRUE(m4.Matches(internal::StringView(""))); 1794*28f6c2f2SEnji Cooper #endif // GTEST_INTERNAL_HAS_STRING_VIEW 1795*28f6c2f2SEnji Cooper } 1796*28f6c2f2SEnji Cooper 1797*28f6c2f2SEnji Cooper TEST(EndsWithTest, CanDescribeSelf) { 1798*28f6c2f2SEnji Cooper Matcher<const std::string> m = EndsWith("Hi"); 1799*28f6c2f2SEnji Cooper EXPECT_EQ("ends with \"Hi\"", Describe(m)); 1800*28f6c2f2SEnji Cooper } 1801*28f6c2f2SEnji Cooper 1802*28f6c2f2SEnji Cooper // Tests WhenBase64Unescaped. 1803*28f6c2f2SEnji Cooper 1804*28f6c2f2SEnji Cooper TEST(WhenBase64UnescapedTest, MatchesUnescapedBase64Strings) { 1805*28f6c2f2SEnji Cooper const Matcher<const char*> m1 = WhenBase64Unescaped(EndsWith("!")); 1806*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches("invalid base64")); 1807*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches("aGVsbG8gd29ybGQ=")); // hello world 1808*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches("aGVsbG8gd29ybGQh")); // hello world! 1809*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches("+/-_IQ")); // \xfb\xff\xbf! 1810*28f6c2f2SEnji Cooper 1811*28f6c2f2SEnji Cooper const Matcher<const std::string&> m2 = WhenBase64Unescaped(EndsWith("!")); 1812*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches("invalid base64")); 1813*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches("aGVsbG8gd29ybGQ=")); // hello world 1814*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches("aGVsbG8gd29ybGQh")); // hello world! 1815*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches("+/-_IQ")); // \xfb\xff\xbf! 1816*28f6c2f2SEnji Cooper 1817*28f6c2f2SEnji Cooper #if GTEST_INTERNAL_HAS_STRING_VIEW 1818*28f6c2f2SEnji Cooper const Matcher<const internal::StringView&> m3 = 1819*28f6c2f2SEnji Cooper WhenBase64Unescaped(EndsWith("!")); 1820*28f6c2f2SEnji Cooper EXPECT_FALSE(m3.Matches("invalid base64")); 1821*28f6c2f2SEnji Cooper EXPECT_FALSE(m3.Matches("aGVsbG8gd29ybGQ=")); // hello world 1822*28f6c2f2SEnji Cooper EXPECT_TRUE(m3.Matches("aGVsbG8gd29ybGQh")); // hello world! 1823*28f6c2f2SEnji Cooper EXPECT_TRUE(m3.Matches("+/-_IQ")); // \xfb\xff\xbf! 1824*28f6c2f2SEnji Cooper #endif // GTEST_INTERNAL_HAS_STRING_VIEW 1825*28f6c2f2SEnji Cooper } 1826*28f6c2f2SEnji Cooper 1827*28f6c2f2SEnji Cooper TEST(WhenBase64UnescapedTest, CanDescribeSelf) { 1828*28f6c2f2SEnji Cooper const Matcher<const char*> m = WhenBase64Unescaped(EndsWith("!")); 1829*28f6c2f2SEnji Cooper EXPECT_EQ("matches after Base64Unescape ends with \"!\"", Describe(m)); 1830*28f6c2f2SEnji Cooper } 1831*28f6c2f2SEnji Cooper 1832*28f6c2f2SEnji Cooper // Tests MatchesRegex(). 1833*28f6c2f2SEnji Cooper 1834*28f6c2f2SEnji Cooper TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) { 1835*28f6c2f2SEnji Cooper const Matcher<const char*> m1 = MatchesRegex("a.*z"); 1836*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches("az")); 1837*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches("abcz")); 1838*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(nullptr)); 1839*28f6c2f2SEnji Cooper 1840*28f6c2f2SEnji Cooper const Matcher<const std::string&> m2 = MatchesRegex(new RE("a.*z")); 1841*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches("azbz")); 1842*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches("az1")); 1843*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches("1az")); 1844*28f6c2f2SEnji Cooper 1845*28f6c2f2SEnji Cooper #if GTEST_INTERNAL_HAS_STRING_VIEW 1846*28f6c2f2SEnji Cooper const Matcher<const internal::StringView&> m3 = MatchesRegex("a.*z"); 1847*28f6c2f2SEnji Cooper EXPECT_TRUE(m3.Matches(internal::StringView("az"))); 1848*28f6c2f2SEnji Cooper EXPECT_TRUE(m3.Matches(internal::StringView("abcz"))); 1849*28f6c2f2SEnji Cooper EXPECT_FALSE(m3.Matches(internal::StringView("1az"))); 1850*28f6c2f2SEnji Cooper EXPECT_FALSE(m3.Matches(internal::StringView())); 1851*28f6c2f2SEnji Cooper const Matcher<const internal::StringView&> m4 = 1852*28f6c2f2SEnji Cooper MatchesRegex(internal::StringView("")); 1853*28f6c2f2SEnji Cooper EXPECT_TRUE(m4.Matches(internal::StringView(""))); 1854*28f6c2f2SEnji Cooper EXPECT_TRUE(m4.Matches(internal::StringView())); 1855*28f6c2f2SEnji Cooper #endif // GTEST_INTERNAL_HAS_STRING_VIEW 1856*28f6c2f2SEnji Cooper } 1857*28f6c2f2SEnji Cooper 1858*28f6c2f2SEnji Cooper TEST(MatchesRegexTest, CanDescribeSelf) { 1859*28f6c2f2SEnji Cooper Matcher<const std::string> m1 = MatchesRegex(std::string("Hi.*")); 1860*28f6c2f2SEnji Cooper EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1)); 1861*28f6c2f2SEnji Cooper 1862*28f6c2f2SEnji Cooper Matcher<const char*> m2 = MatchesRegex(new RE("a.*")); 1863*28f6c2f2SEnji Cooper EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2)); 1864*28f6c2f2SEnji Cooper 1865*28f6c2f2SEnji Cooper #if GTEST_INTERNAL_HAS_STRING_VIEW 1866*28f6c2f2SEnji Cooper Matcher<const internal::StringView> m3 = MatchesRegex(new RE("0.*")); 1867*28f6c2f2SEnji Cooper EXPECT_EQ("matches regular expression \"0.*\"", Describe(m3)); 1868*28f6c2f2SEnji Cooper #endif // GTEST_INTERNAL_HAS_STRING_VIEW 1869*28f6c2f2SEnji Cooper } 1870*28f6c2f2SEnji Cooper 1871*28f6c2f2SEnji Cooper // Tests ContainsRegex(). 1872*28f6c2f2SEnji Cooper 1873*28f6c2f2SEnji Cooper TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) { 1874*28f6c2f2SEnji Cooper const Matcher<const char*> m1 = ContainsRegex(std::string("a.*z")); 1875*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches("az")); 1876*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches("0abcz1")); 1877*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(nullptr)); 1878*28f6c2f2SEnji Cooper 1879*28f6c2f2SEnji Cooper const Matcher<const std::string&> m2 = ContainsRegex(new RE("a.*z")); 1880*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches("azbz")); 1881*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches("az1")); 1882*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches("1a")); 1883*28f6c2f2SEnji Cooper 1884*28f6c2f2SEnji Cooper #if GTEST_INTERNAL_HAS_STRING_VIEW 1885*28f6c2f2SEnji Cooper const Matcher<const internal::StringView&> m3 = ContainsRegex(new RE("a.*z")); 1886*28f6c2f2SEnji Cooper EXPECT_TRUE(m3.Matches(internal::StringView("azbz"))); 1887*28f6c2f2SEnji Cooper EXPECT_TRUE(m3.Matches(internal::StringView("az1"))); 1888*28f6c2f2SEnji Cooper EXPECT_FALSE(m3.Matches(internal::StringView("1a"))); 1889*28f6c2f2SEnji Cooper EXPECT_FALSE(m3.Matches(internal::StringView())); 1890*28f6c2f2SEnji Cooper const Matcher<const internal::StringView&> m4 = 1891*28f6c2f2SEnji Cooper ContainsRegex(internal::StringView("")); 1892*28f6c2f2SEnji Cooper EXPECT_TRUE(m4.Matches(internal::StringView(""))); 1893*28f6c2f2SEnji Cooper EXPECT_TRUE(m4.Matches(internal::StringView())); 1894*28f6c2f2SEnji Cooper #endif // GTEST_INTERNAL_HAS_STRING_VIEW 1895*28f6c2f2SEnji Cooper } 1896*28f6c2f2SEnji Cooper 1897*28f6c2f2SEnji Cooper TEST(ContainsRegexTest, CanDescribeSelf) { 1898*28f6c2f2SEnji Cooper Matcher<const std::string> m1 = ContainsRegex("Hi.*"); 1899*28f6c2f2SEnji Cooper EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1)); 1900*28f6c2f2SEnji Cooper 1901*28f6c2f2SEnji Cooper Matcher<const char*> m2 = ContainsRegex(new RE("a.*")); 1902*28f6c2f2SEnji Cooper EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2)); 1903*28f6c2f2SEnji Cooper 1904*28f6c2f2SEnji Cooper #if GTEST_INTERNAL_HAS_STRING_VIEW 1905*28f6c2f2SEnji Cooper Matcher<const internal::StringView> m3 = ContainsRegex(new RE("0.*")); 1906*28f6c2f2SEnji Cooper EXPECT_EQ("contains regular expression \"0.*\"", Describe(m3)); 1907*28f6c2f2SEnji Cooper #endif // GTEST_INTERNAL_HAS_STRING_VIEW 1908*28f6c2f2SEnji Cooper } 1909*28f6c2f2SEnji Cooper 1910*28f6c2f2SEnji Cooper // Tests for wide strings. 1911*28f6c2f2SEnji Cooper #if GTEST_HAS_STD_WSTRING 1912*28f6c2f2SEnji Cooper TEST(StdWideStrEqTest, MatchesEqual) { 1913*28f6c2f2SEnji Cooper Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello")); 1914*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(L"Hello")); 1915*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(L"hello")); 1916*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(nullptr)); 1917*28f6c2f2SEnji Cooper 1918*28f6c2f2SEnji Cooper Matcher<const ::std::wstring&> m2 = StrEq(L"Hello"); 1919*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(L"Hello")); 1920*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(L"Hi")); 1921*28f6c2f2SEnji Cooper 1922*28f6c2f2SEnji Cooper Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D"); 1923*28f6c2f2SEnji Cooper EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D")); 1924*28f6c2f2SEnji Cooper EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E")); 1925*28f6c2f2SEnji Cooper 1926*28f6c2f2SEnji Cooper ::std::wstring str(L"01204500800"); 1927*28f6c2f2SEnji Cooper str[3] = L'\0'; 1928*28f6c2f2SEnji Cooper Matcher<const ::std::wstring&> m4 = StrEq(str); 1929*28f6c2f2SEnji Cooper EXPECT_TRUE(m4.Matches(str)); 1930*28f6c2f2SEnji Cooper str[0] = str[6] = str[7] = str[9] = str[10] = L'\0'; 1931*28f6c2f2SEnji Cooper Matcher<const ::std::wstring&> m5 = StrEq(str); 1932*28f6c2f2SEnji Cooper EXPECT_TRUE(m5.Matches(str)); 1933*28f6c2f2SEnji Cooper } 1934*28f6c2f2SEnji Cooper 1935*28f6c2f2SEnji Cooper TEST(StdWideStrEqTest, CanDescribeSelf) { 1936*28f6c2f2SEnji Cooper Matcher<::std::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v"); 1937*28f6c2f2SEnji Cooper EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"", 1938*28f6c2f2SEnji Cooper Describe(m)); 1939*28f6c2f2SEnji Cooper 1940*28f6c2f2SEnji Cooper Matcher<::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D"); 1941*28f6c2f2SEnji Cooper EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"", Describe(m2)); 1942*28f6c2f2SEnji Cooper 1943*28f6c2f2SEnji Cooper ::std::wstring str(L"01204500800"); 1944*28f6c2f2SEnji Cooper str[3] = L'\0'; 1945*28f6c2f2SEnji Cooper Matcher<const ::std::wstring&> m4 = StrEq(str); 1946*28f6c2f2SEnji Cooper EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4)); 1947*28f6c2f2SEnji Cooper str[0] = str[6] = str[7] = str[9] = str[10] = L'\0'; 1948*28f6c2f2SEnji Cooper Matcher<const ::std::wstring&> m5 = StrEq(str); 1949*28f6c2f2SEnji Cooper EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5)); 1950*28f6c2f2SEnji Cooper } 1951*28f6c2f2SEnji Cooper 1952*28f6c2f2SEnji Cooper TEST(StdWideStrNeTest, MatchesUnequalString) { 1953*28f6c2f2SEnji Cooper Matcher<const wchar_t*> m = StrNe(L"Hello"); 1954*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(L"")); 1955*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(nullptr)); 1956*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(L"Hello")); 1957*28f6c2f2SEnji Cooper 1958*28f6c2f2SEnji Cooper Matcher<::std::wstring> m2 = StrNe(::std::wstring(L"Hello")); 1959*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(L"hello")); 1960*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(L"Hello")); 1961*28f6c2f2SEnji Cooper } 1962*28f6c2f2SEnji Cooper 1963*28f6c2f2SEnji Cooper TEST(StdWideStrNeTest, CanDescribeSelf) { 1964*28f6c2f2SEnji Cooper Matcher<const wchar_t*> m = StrNe(L"Hi"); 1965*28f6c2f2SEnji Cooper EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m)); 1966*28f6c2f2SEnji Cooper } 1967*28f6c2f2SEnji Cooper 1968*28f6c2f2SEnji Cooper TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) { 1969*28f6c2f2SEnji Cooper Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello")); 1970*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(L"Hello")); 1971*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(L"hello")); 1972*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(L"Hi")); 1973*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(nullptr)); 1974*28f6c2f2SEnji Cooper 1975*28f6c2f2SEnji Cooper Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello"); 1976*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(L"hello")); 1977*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(L"Hi")); 1978*28f6c2f2SEnji Cooper } 1979*28f6c2f2SEnji Cooper 1980*28f6c2f2SEnji Cooper TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) { 1981*28f6c2f2SEnji Cooper ::std::wstring str1(L"oabocdooeoo"); 1982*28f6c2f2SEnji Cooper ::std::wstring str2(L"OABOCDOOEOO"); 1983*28f6c2f2SEnji Cooper Matcher<const ::std::wstring&> m0 = StrCaseEq(str1); 1984*28f6c2f2SEnji Cooper EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0'))); 1985*28f6c2f2SEnji Cooper 1986*28f6c2f2SEnji Cooper str1[3] = str2[3] = L'\0'; 1987*28f6c2f2SEnji Cooper Matcher<const ::std::wstring&> m1 = StrCaseEq(str1); 1988*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(str2)); 1989*28f6c2f2SEnji Cooper 1990*28f6c2f2SEnji Cooper str1[0] = str1[6] = str1[7] = str1[10] = L'\0'; 1991*28f6c2f2SEnji Cooper str2[0] = str2[6] = str2[7] = str2[10] = L'\0'; 1992*28f6c2f2SEnji Cooper Matcher<const ::std::wstring&> m2 = StrCaseEq(str1); 1993*28f6c2f2SEnji Cooper str1[9] = str2[9] = L'\0'; 1994*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(str2)); 1995*28f6c2f2SEnji Cooper 1996*28f6c2f2SEnji Cooper Matcher<const ::std::wstring&> m3 = StrCaseEq(str1); 1997*28f6c2f2SEnji Cooper EXPECT_TRUE(m3.Matches(str2)); 1998*28f6c2f2SEnji Cooper 1999*28f6c2f2SEnji Cooper EXPECT_FALSE(m3.Matches(str2 + L"x")); 2000*28f6c2f2SEnji Cooper str2.append(1, L'\0'); 2001*28f6c2f2SEnji Cooper EXPECT_FALSE(m3.Matches(str2)); 2002*28f6c2f2SEnji Cooper EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9))); 2003*28f6c2f2SEnji Cooper } 2004*28f6c2f2SEnji Cooper 2005*28f6c2f2SEnji Cooper TEST(StdWideStrCaseEqTest, CanDescribeSelf) { 2006*28f6c2f2SEnji Cooper Matcher<::std::wstring> m = StrCaseEq(L"Hi"); 2007*28f6c2f2SEnji Cooper EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m)); 2008*28f6c2f2SEnji Cooper } 2009*28f6c2f2SEnji Cooper 2010*28f6c2f2SEnji Cooper TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) { 2011*28f6c2f2SEnji Cooper Matcher<const wchar_t*> m = StrCaseNe(L"Hello"); 2012*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(L"Hi")); 2013*28f6c2f2SEnji Cooper EXPECT_TRUE(m.Matches(nullptr)); 2014*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(L"Hello")); 2015*28f6c2f2SEnji Cooper EXPECT_FALSE(m.Matches(L"hello")); 2016*28f6c2f2SEnji Cooper 2017*28f6c2f2SEnji Cooper Matcher<::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello")); 2018*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(L"")); 2019*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(L"Hello")); 2020*28f6c2f2SEnji Cooper } 2021*28f6c2f2SEnji Cooper 2022*28f6c2f2SEnji Cooper TEST(StdWideStrCaseNeTest, CanDescribeSelf) { 2023*28f6c2f2SEnji Cooper Matcher<const wchar_t*> m = StrCaseNe(L"Hi"); 2024*28f6c2f2SEnji Cooper EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m)); 2025*28f6c2f2SEnji Cooper } 2026*28f6c2f2SEnji Cooper 2027*28f6c2f2SEnji Cooper // Tests that HasSubstr() works for matching wstring-typed values. 2028*28f6c2f2SEnji Cooper TEST(StdWideHasSubstrTest, WorksForStringClasses) { 2029*28f6c2f2SEnji Cooper const Matcher<::std::wstring> m1 = HasSubstr(L"foo"); 2030*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food."))); 2031*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo"))); 2032*28f6c2f2SEnji Cooper 2033*28f6c2f2SEnji Cooper const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo"); 2034*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food."))); 2035*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(::std::wstring(L"tofo"))); 2036*28f6c2f2SEnji Cooper } 2037*28f6c2f2SEnji Cooper 2038*28f6c2f2SEnji Cooper // Tests that HasSubstr() works for matching C-wide-string-typed values. 2039*28f6c2f2SEnji Cooper TEST(StdWideHasSubstrTest, WorksForCStrings) { 2040*28f6c2f2SEnji Cooper const Matcher<wchar_t*> m1 = HasSubstr(L"foo"); 2041*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food."))); 2042*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo"))); 2043*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(nullptr)); 2044*28f6c2f2SEnji Cooper 2045*28f6c2f2SEnji Cooper const Matcher<const wchar_t*> m2 = HasSubstr(L"foo"); 2046*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(L"I love food.")); 2047*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(L"tofo")); 2048*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(nullptr)); 2049*28f6c2f2SEnji Cooper } 2050*28f6c2f2SEnji Cooper 2051*28f6c2f2SEnji Cooper // Tests that HasSubstr(s) describes itself properly. 2052*28f6c2f2SEnji Cooper TEST(StdWideHasSubstrTest, CanDescribeSelf) { 2053*28f6c2f2SEnji Cooper Matcher<::std::wstring> m = HasSubstr(L"foo\n\""); 2054*28f6c2f2SEnji Cooper EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m)); 2055*28f6c2f2SEnji Cooper } 2056*28f6c2f2SEnji Cooper 2057*28f6c2f2SEnji Cooper // Tests StartsWith(s). 2058*28f6c2f2SEnji Cooper 2059*28f6c2f2SEnji Cooper TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) { 2060*28f6c2f2SEnji Cooper const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L"")); 2061*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(L"Hi")); 2062*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(L"")); 2063*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(nullptr)); 2064*28f6c2f2SEnji Cooper 2065*28f6c2f2SEnji Cooper const Matcher<const ::std::wstring&> m2 = StartsWith(L"Hi"); 2066*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(L"Hi")); 2067*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(L"Hi Hi!")); 2068*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(L"High")); 2069*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(L"H")); 2070*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(L" Hi")); 2071*28f6c2f2SEnji Cooper } 2072*28f6c2f2SEnji Cooper 2073*28f6c2f2SEnji Cooper TEST(StdWideStartsWithTest, CanDescribeSelf) { 2074*28f6c2f2SEnji Cooper Matcher<const ::std::wstring> m = StartsWith(L"Hi"); 2075*28f6c2f2SEnji Cooper EXPECT_EQ("starts with L\"Hi\"", Describe(m)); 2076*28f6c2f2SEnji Cooper } 2077*28f6c2f2SEnji Cooper 2078*28f6c2f2SEnji Cooper // Tests EndsWith(s). 2079*28f6c2f2SEnji Cooper 2080*28f6c2f2SEnji Cooper TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) { 2081*28f6c2f2SEnji Cooper const Matcher<const wchar_t*> m1 = EndsWith(L""); 2082*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(L"Hi")); 2083*28f6c2f2SEnji Cooper EXPECT_TRUE(m1.Matches(L"")); 2084*28f6c2f2SEnji Cooper EXPECT_FALSE(m1.Matches(nullptr)); 2085*28f6c2f2SEnji Cooper 2086*28f6c2f2SEnji Cooper const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L"Hi")); 2087*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(L"Hi")); 2088*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(L"Wow Hi Hi")); 2089*28f6c2f2SEnji Cooper EXPECT_TRUE(m2.Matches(L"Super Hi")); 2090*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(L"i")); 2091*28f6c2f2SEnji Cooper EXPECT_FALSE(m2.Matches(L"Hi ")); 2092*28f6c2f2SEnji Cooper } 2093*28f6c2f2SEnji Cooper 2094*28f6c2f2SEnji Cooper TEST(StdWideEndsWithTest, CanDescribeSelf) { 2095*28f6c2f2SEnji Cooper Matcher<const ::std::wstring> m = EndsWith(L"Hi"); 2096*28f6c2f2SEnji Cooper EXPECT_EQ("ends with L\"Hi\"", Describe(m)); 2097*28f6c2f2SEnji Cooper } 2098*28f6c2f2SEnji Cooper 2099*28f6c2f2SEnji Cooper #endif // GTEST_HAS_STD_WSTRING 2100*28f6c2f2SEnji Cooper 2101*28f6c2f2SEnji Cooper TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) { 2102*28f6c2f2SEnji Cooper StringMatchResultListener listener1; 2103*28f6c2f2SEnji Cooper EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1)); 2104*28f6c2f2SEnji Cooper EXPECT_EQ("% 2 == 0", listener1.str()); 2105*28f6c2f2SEnji Cooper 2106*28f6c2f2SEnji Cooper StringMatchResultListener listener2; 2107*28f6c2f2SEnji Cooper EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2)); 2108*28f6c2f2SEnji Cooper EXPECT_EQ("", listener2.str()); 2109*28f6c2f2SEnji Cooper } 2110*28f6c2f2SEnji Cooper 2111*28f6c2f2SEnji Cooper TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) { 2112*28f6c2f2SEnji Cooper const Matcher<int> is_even = PolymorphicIsEven(); 2113*28f6c2f2SEnji Cooper StringMatchResultListener listener1; 2114*28f6c2f2SEnji Cooper EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1)); 2115*28f6c2f2SEnji Cooper EXPECT_EQ("% 2 == 0", listener1.str()); 2116*28f6c2f2SEnji Cooper 2117*28f6c2f2SEnji Cooper const Matcher<const double&> is_zero = Eq(0); 2118*28f6c2f2SEnji Cooper StringMatchResultListener listener2; 2119*28f6c2f2SEnji Cooper EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2)); 2120*28f6c2f2SEnji Cooper EXPECT_EQ("", listener2.str()); 2121*28f6c2f2SEnji Cooper } 2122*28f6c2f2SEnji Cooper 2123*28f6c2f2SEnji Cooper MATCHER(ConstructNoArg, "") { return true; } 2124*28f6c2f2SEnji Cooper MATCHER_P(Construct1Arg, arg1, "") { return true; } 2125*28f6c2f2SEnji Cooper MATCHER_P2(Construct2Args, arg1, arg2, "") { return true; } 2126*28f6c2f2SEnji Cooper 2127*28f6c2f2SEnji Cooper TEST(MatcherConstruct, ExplicitVsImplicit) { 2128*28f6c2f2SEnji Cooper { 2129*28f6c2f2SEnji Cooper // No arg constructor can be constructed with empty brace. 2130*28f6c2f2SEnji Cooper ConstructNoArgMatcher m = {}; 2131*28f6c2f2SEnji Cooper (void)m; 2132*28f6c2f2SEnji Cooper // And with no args 2133*28f6c2f2SEnji Cooper ConstructNoArgMatcher m2; 2134*28f6c2f2SEnji Cooper (void)m2; 2135*28f6c2f2SEnji Cooper } 2136*28f6c2f2SEnji Cooper { 2137*28f6c2f2SEnji Cooper // The one arg constructor has an explicit constructor. 2138*28f6c2f2SEnji Cooper // This is to prevent the implicit conversion. 2139*28f6c2f2SEnji Cooper using M = Construct1ArgMatcherP<int>; 2140*28f6c2f2SEnji Cooper EXPECT_TRUE((std::is_constructible<M, int>::value)); 2141*28f6c2f2SEnji Cooper EXPECT_FALSE((std::is_convertible<int, M>::value)); 2142*28f6c2f2SEnji Cooper } 2143*28f6c2f2SEnji Cooper { 2144*28f6c2f2SEnji Cooper // Multiple arg matchers can be constructed with an implicit construction. 2145*28f6c2f2SEnji Cooper Construct2ArgsMatcherP2<int, double> m = {1, 2.2}; 2146*28f6c2f2SEnji Cooper (void)m; 2147*28f6c2f2SEnji Cooper } 2148*28f6c2f2SEnji Cooper } 2149*28f6c2f2SEnji Cooper 2150*28f6c2f2SEnji Cooper MATCHER_P(Really, inner_matcher, "") { 2151*28f6c2f2SEnji Cooper return ExplainMatchResult(inner_matcher, arg, result_listener); 2152*28f6c2f2SEnji Cooper } 2153*28f6c2f2SEnji Cooper 2154*28f6c2f2SEnji Cooper TEST(ExplainMatchResultTest, WorksInsideMATCHER) { 2155*28f6c2f2SEnji Cooper EXPECT_THAT(0, Really(Eq(0))); 2156*28f6c2f2SEnji Cooper } 2157*28f6c2f2SEnji Cooper 2158*28f6c2f2SEnji Cooper TEST(DescribeMatcherTest, WorksWithValue) { 2159*28f6c2f2SEnji Cooper EXPECT_EQ("is equal to 42", DescribeMatcher<int>(42)); 2160*28f6c2f2SEnji Cooper EXPECT_EQ("isn't equal to 42", DescribeMatcher<int>(42, true)); 2161*28f6c2f2SEnji Cooper } 2162*28f6c2f2SEnji Cooper 2163*28f6c2f2SEnji Cooper TEST(DescribeMatcherTest, WorksWithMonomorphicMatcher) { 2164*28f6c2f2SEnji Cooper const Matcher<int> monomorphic = Le(0); 2165*28f6c2f2SEnji Cooper EXPECT_EQ("is <= 0", DescribeMatcher<int>(monomorphic)); 2166*28f6c2f2SEnji Cooper EXPECT_EQ("isn't <= 0", DescribeMatcher<int>(monomorphic, true)); 2167*28f6c2f2SEnji Cooper } 2168*28f6c2f2SEnji Cooper 2169*28f6c2f2SEnji Cooper TEST(DescribeMatcherTest, WorksWithPolymorphicMatcher) { 2170*28f6c2f2SEnji Cooper EXPECT_EQ("is even", DescribeMatcher<int>(PolymorphicIsEven())); 2171*28f6c2f2SEnji Cooper EXPECT_EQ("is odd", DescribeMatcher<int>(PolymorphicIsEven(), true)); 2172*28f6c2f2SEnji Cooper } 2173*28f6c2f2SEnji Cooper 2174*28f6c2f2SEnji Cooper MATCHER_P(FieldIIs, inner_matcher, "") { 2175*28f6c2f2SEnji Cooper return ExplainMatchResult(inner_matcher, arg.i, result_listener); 2176*28f6c2f2SEnji Cooper } 2177*28f6c2f2SEnji Cooper 2178*28f6c2f2SEnji Cooper #if GTEST_HAS_RTTI 2179*28f6c2f2SEnji Cooper TEST(WhenDynamicCastToTest, SameType) { 2180*28f6c2f2SEnji Cooper Derived derived; 2181*28f6c2f2SEnji Cooper derived.i = 4; 2182*28f6c2f2SEnji Cooper 2183*28f6c2f2SEnji Cooper // Right type. A pointer is passed down. 2184*28f6c2f2SEnji Cooper Base* as_base_ptr = &derived; 2185*28f6c2f2SEnji Cooper EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(Not(IsNull()))); 2186*28f6c2f2SEnji Cooper EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(Pointee(FieldIIs(4)))); 2187*28f6c2f2SEnji Cooper EXPECT_THAT(as_base_ptr, 2188*28f6c2f2SEnji Cooper Not(WhenDynamicCastTo<Derived*>(Pointee(FieldIIs(5))))); 2189*28f6c2f2SEnji Cooper } 2190*28f6c2f2SEnji Cooper 2191*28f6c2f2SEnji Cooper TEST(WhenDynamicCastToTest, WrongTypes) { 2192*28f6c2f2SEnji Cooper Base base; 2193*28f6c2f2SEnji Cooper Derived derived; 2194*28f6c2f2SEnji Cooper OtherDerived other_derived; 2195*28f6c2f2SEnji Cooper 2196*28f6c2f2SEnji Cooper // Wrong types. NULL is passed. 2197*28f6c2f2SEnji Cooper EXPECT_THAT(&base, Not(WhenDynamicCastTo<Derived*>(Pointee(_)))); 2198*28f6c2f2SEnji Cooper EXPECT_THAT(&base, WhenDynamicCastTo<Derived*>(IsNull())); 2199*28f6c2f2SEnji Cooper Base* as_base_ptr = &derived; 2200*28f6c2f2SEnji Cooper EXPECT_THAT(as_base_ptr, Not(WhenDynamicCastTo<OtherDerived*>(Pointee(_)))); 2201*28f6c2f2SEnji Cooper EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<OtherDerived*>(IsNull())); 2202*28f6c2f2SEnji Cooper as_base_ptr = &other_derived; 2203*28f6c2f2SEnji Cooper EXPECT_THAT(as_base_ptr, Not(WhenDynamicCastTo<Derived*>(Pointee(_)))); 2204*28f6c2f2SEnji Cooper EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(IsNull())); 2205*28f6c2f2SEnji Cooper } 2206*28f6c2f2SEnji Cooper 2207*28f6c2f2SEnji Cooper TEST(WhenDynamicCastToTest, AlreadyNull) { 2208*28f6c2f2SEnji Cooper // Already NULL. 2209*28f6c2f2SEnji Cooper Base* as_base_ptr = nullptr; 2210*28f6c2f2SEnji Cooper EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(IsNull())); 2211*28f6c2f2SEnji Cooper } 2212*28f6c2f2SEnji Cooper 2213*28f6c2f2SEnji Cooper struct AmbiguousCastTypes { 2214*28f6c2f2SEnji Cooper class VirtualDerived : public virtual Base {}; 2215*28f6c2f2SEnji Cooper class DerivedSub1 : public VirtualDerived {}; 2216*28f6c2f2SEnji Cooper class DerivedSub2 : public VirtualDerived {}; 2217*28f6c2f2SEnji Cooper class ManyDerivedInHierarchy : public DerivedSub1, public DerivedSub2 {}; 2218*28f6c2f2SEnji Cooper }; 2219*28f6c2f2SEnji Cooper 2220*28f6c2f2SEnji Cooper TEST(WhenDynamicCastToTest, AmbiguousCast) { 2221*28f6c2f2SEnji Cooper AmbiguousCastTypes::DerivedSub1 sub1; 2222*28f6c2f2SEnji Cooper AmbiguousCastTypes::ManyDerivedInHierarchy many_derived; 2223*28f6c2f2SEnji Cooper 2224*28f6c2f2SEnji Cooper // This testcase fails on FreeBSD. See this GitHub issue for more details: 2225*28f6c2f2SEnji Cooper // https://github.com/google/googletest/issues/2172 2226*28f6c2f2SEnji Cooper #ifdef __FreeBSD__ 2227*28f6c2f2SEnji Cooper EXPECT_NONFATAL_FAILURE({ 2228*28f6c2f2SEnji Cooper #endif 2229*28f6c2f2SEnji Cooper // Multiply derived from Base. dynamic_cast<> returns NULL. 2230*28f6c2f2SEnji Cooper Base* as_base_ptr = 2231*28f6c2f2SEnji Cooper static_cast<AmbiguousCastTypes::DerivedSub1*>(&many_derived); 2232*28f6c2f2SEnji Cooper 2233*28f6c2f2SEnji Cooper EXPECT_THAT(as_base_ptr, 2234*28f6c2f2SEnji Cooper WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(IsNull())); 2235*28f6c2f2SEnji Cooper as_base_ptr = &sub1; 2236*28f6c2f2SEnji Cooper EXPECT_THAT( 2237*28f6c2f2SEnji Cooper as_base_ptr, 2238*28f6c2f2SEnji Cooper WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(Not(IsNull()))); 2239*28f6c2f2SEnji Cooper #ifdef __FreeBSD__ 2240*28f6c2f2SEnji Cooper }, ""); 2241*28f6c2f2SEnji Cooper #endif 2242*28f6c2f2SEnji Cooper } 2243*28f6c2f2SEnji Cooper 2244*28f6c2f2SEnji Cooper TEST(WhenDynamicCastToTest, Describe) { 2245*28f6c2f2SEnji Cooper Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(Pointee(_)); 2246*28f6c2f2SEnji Cooper const std::string prefix = 2247*28f6c2f2SEnji Cooper "when dynamic_cast to " + internal::GetTypeName<Derived*>() + ", "; 2248*28f6c2f2SEnji Cooper EXPECT_EQ(prefix + "points to a value that is anything", Describe(matcher)); 2249*28f6c2f2SEnji Cooper EXPECT_EQ(prefix + "does not point to a value that is anything", 2250*28f6c2f2SEnji Cooper DescribeNegation(matcher)); 2251*28f6c2f2SEnji Cooper } 2252*28f6c2f2SEnji Cooper 2253*28f6c2f2SEnji Cooper TEST(WhenDynamicCastToTest, Explain) { 2254*28f6c2f2SEnji Cooper Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(Pointee(_)); 2255*28f6c2f2SEnji Cooper Base* null = nullptr; 2256*28f6c2f2SEnji Cooper EXPECT_THAT(Explain(matcher, null), HasSubstr("NULL")); 2257*28f6c2f2SEnji Cooper Derived derived; 2258*28f6c2f2SEnji Cooper EXPECT_TRUE(matcher.Matches(&derived)); 2259*28f6c2f2SEnji Cooper EXPECT_THAT(Explain(matcher, &derived), HasSubstr("which points to ")); 2260*28f6c2f2SEnji Cooper 2261*28f6c2f2SEnji Cooper // With references, the matcher itself can fail. Test for that one. 2262*28f6c2f2SEnji Cooper Matcher<const Base&> ref_matcher = WhenDynamicCastTo<const OtherDerived&>(_); 2263*28f6c2f2SEnji Cooper EXPECT_THAT(Explain(ref_matcher, derived), 2264*28f6c2f2SEnji Cooper HasSubstr("which cannot be dynamic_cast")); 2265*28f6c2f2SEnji Cooper } 2266*28f6c2f2SEnji Cooper 2267*28f6c2f2SEnji Cooper TEST(WhenDynamicCastToTest, GoodReference) { 2268*28f6c2f2SEnji Cooper Derived derived; 2269*28f6c2f2SEnji Cooper derived.i = 4; 2270*28f6c2f2SEnji Cooper Base& as_base_ref = derived; 2271*28f6c2f2SEnji Cooper EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(FieldIIs(4))); 2272*28f6c2f2SEnji Cooper EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(Not(FieldIIs(5)))); 2273*28f6c2f2SEnji Cooper } 2274*28f6c2f2SEnji Cooper 2275*28f6c2f2SEnji Cooper TEST(WhenDynamicCastToTest, BadReference) { 2276*28f6c2f2SEnji Cooper Derived derived; 2277*28f6c2f2SEnji Cooper Base& as_base_ref = derived; 2278*28f6c2f2SEnji Cooper EXPECT_THAT(as_base_ref, Not(WhenDynamicCastTo<const OtherDerived&>(_))); 2279*28f6c2f2SEnji Cooper } 2280*28f6c2f2SEnji Cooper #endif // GTEST_HAS_RTTI 2281*28f6c2f2SEnji Cooper 2282*28f6c2f2SEnji Cooper class DivisibleByImpl { 2283*28f6c2f2SEnji Cooper public: 2284*28f6c2f2SEnji Cooper explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {} 2285*28f6c2f2SEnji Cooper 2286*28f6c2f2SEnji Cooper // For testing using ExplainMatchResultTo() with polymorphic matchers. 2287*28f6c2f2SEnji Cooper template <typename T> 2288*28f6c2f2SEnji Cooper bool MatchAndExplain(const T& n, MatchResultListener* listener) const { 2289*28f6c2f2SEnji Cooper *listener << "which is " << (n % divider_) << " modulo " << divider_; 2290*28f6c2f2SEnji Cooper return (n % divider_) == 0; 2291*28f6c2f2SEnji Cooper } 2292*28f6c2f2SEnji Cooper 2293*28f6c2f2SEnji Cooper void DescribeTo(ostream* os) const { *os << "is divisible by " << divider_; } 2294*28f6c2f2SEnji Cooper 2295*28f6c2f2SEnji Cooper void DescribeNegationTo(ostream* os) const { 2296*28f6c2f2SEnji Cooper *os << "is not divisible by " << divider_; 2297*28f6c2f2SEnji Cooper } 2298*28f6c2f2SEnji Cooper 2299*28f6c2f2SEnji Cooper void set_divider(int a_divider) { divider_ = a_divider; } 2300*28f6c2f2SEnji Cooper int divider() const { return divider_; } 2301*28f6c2f2SEnji Cooper 2302*28f6c2f2SEnji Cooper private: 2303*28f6c2f2SEnji Cooper int divider_; 2304*28f6c2f2SEnji Cooper }; 2305*28f6c2f2SEnji Cooper 2306*28f6c2f2SEnji Cooper PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) { 2307*28f6c2f2SEnji Cooper return MakePolymorphicMatcher(DivisibleByImpl(n)); 2308*28f6c2f2SEnji Cooper } 2309*28f6c2f2SEnji Cooper 2310*28f6c2f2SEnji Cooper // Tests that when AllOf() fails, only the first failing matcher is 2311*28f6c2f2SEnji Cooper // asked to explain why. 2312*28f6c2f2SEnji Cooper TEST(ExplainMatchResultTest, AllOf_False_False) { 2313*28f6c2f2SEnji Cooper const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3)); 2314*28f6c2f2SEnji Cooper EXPECT_EQ("which is 1 modulo 4", Explain(m, 5)); 2315*28f6c2f2SEnji Cooper } 2316*28f6c2f2SEnji Cooper 2317*28f6c2f2SEnji Cooper // Tests that when AllOf() fails, only the first failing matcher is 2318*28f6c2f2SEnji Cooper // asked to explain why. 2319*28f6c2f2SEnji Cooper TEST(ExplainMatchResultTest, AllOf_False_True) { 2320*28f6c2f2SEnji Cooper const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3)); 2321*28f6c2f2SEnji Cooper EXPECT_EQ("which is 2 modulo 4", Explain(m, 6)); 2322*28f6c2f2SEnji Cooper } 2323*28f6c2f2SEnji Cooper 2324*28f6c2f2SEnji Cooper // Tests that when AllOf() fails, only the first failing matcher is 2325*28f6c2f2SEnji Cooper // asked to explain why. 2326*28f6c2f2SEnji Cooper TEST(ExplainMatchResultTest, AllOf_True_False) { 2327*28f6c2f2SEnji Cooper const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3)); 2328*28f6c2f2SEnji Cooper EXPECT_EQ("which is 2 modulo 3", Explain(m, 5)); 2329*28f6c2f2SEnji Cooper } 2330*28f6c2f2SEnji Cooper 2331*28f6c2f2SEnji Cooper // Tests that when AllOf() succeeds, all matchers are asked to explain 2332*28f6c2f2SEnji Cooper // why. 2333*28f6c2f2SEnji Cooper TEST(ExplainMatchResultTest, AllOf_True_True) { 2334*28f6c2f2SEnji Cooper const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3)); 2335*28f6c2f2SEnji Cooper EXPECT_EQ("which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6)); 2336*28f6c2f2SEnji Cooper } 2337*28f6c2f2SEnji Cooper 2338*28f6c2f2SEnji Cooper TEST(ExplainMatchResultTest, AllOf_True_True_2) { 2339*28f6c2f2SEnji Cooper const Matcher<int> m = AllOf(Ge(2), Le(3)); 2340*28f6c2f2SEnji Cooper EXPECT_EQ("", Explain(m, 2)); 2341*28f6c2f2SEnji Cooper } 2342*28f6c2f2SEnji Cooper 2343*28f6c2f2SEnji Cooper INSTANTIATE_GTEST_MATCHER_TEST_P(ExplainmatcherResultTest); 2344*28f6c2f2SEnji Cooper 2345*28f6c2f2SEnji Cooper TEST_P(ExplainmatcherResultTestP, MonomorphicMatcher) { 2346*28f6c2f2SEnji Cooper const Matcher<int> m = GreaterThan(5); 2347*28f6c2f2SEnji Cooper EXPECT_EQ("which is 1 more than 5", Explain(m, 6)); 2348*28f6c2f2SEnji Cooper } 2349*28f6c2f2SEnji Cooper 2350*28f6c2f2SEnji Cooper // Tests PolymorphicMatcher::mutable_impl(). 2351*28f6c2f2SEnji Cooper TEST(PolymorphicMatcherTest, CanAccessMutableImpl) { 2352*28f6c2f2SEnji Cooper PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42)); 2353*28f6c2f2SEnji Cooper DivisibleByImpl& impl = m.mutable_impl(); 2354*28f6c2f2SEnji Cooper EXPECT_EQ(42, impl.divider()); 2355*28f6c2f2SEnji Cooper 2356*28f6c2f2SEnji Cooper impl.set_divider(0); 2357*28f6c2f2SEnji Cooper EXPECT_EQ(0, m.mutable_impl().divider()); 2358*28f6c2f2SEnji Cooper } 2359*28f6c2f2SEnji Cooper 2360*28f6c2f2SEnji Cooper // Tests PolymorphicMatcher::impl(). 2361*28f6c2f2SEnji Cooper TEST(PolymorphicMatcherTest, CanAccessImpl) { 2362*28f6c2f2SEnji Cooper const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42)); 2363*28f6c2f2SEnji Cooper const DivisibleByImpl& impl = m.impl(); 2364*28f6c2f2SEnji Cooper EXPECT_EQ(42, impl.divider()); 2365*28f6c2f2SEnji Cooper } 2366*28f6c2f2SEnji Cooper 2367*28f6c2f2SEnji Cooper } // namespace 2368*28f6c2f2SEnji Cooper } // namespace gmock_matchers_test 2369*28f6c2f2SEnji Cooper } // namespace testing 2370*28f6c2f2SEnji Cooper 2371*28f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_POP_() // 4244 4100 2372