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