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