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