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