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