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