1 // Copyright 2007, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
30 // Google Mock - a framework for writing C++ mock classes.
32 // This file tests some commonly used argument matchers.
34 // Silence warning C4244: 'initializing': conversion from 'int' to 'short',
35 // possible loss of data and C4100, unreferenced local parameter
38 #pragma warning(disable : 4244)
39 #pragma warning(disable : 4100)
42 #include "test/gmock-matchers_test.h"
45 namespace gmock_matchers_test {
48 INSTANTIATE_GTEST_MATCHER_TEST_P(MonotonicMatcherTest);
50 TEST_P(MonotonicMatcherTestP, IsPrintable) {
53 EXPECT_EQ("is > 5", ss.str());
56 TEST(MatchResultListenerTest, StreamingWorks) {
57 StringMatchResultListener listener;
58 listener << "hi" << 5;
59 EXPECT_EQ("hi5", listener.str());
62 EXPECT_EQ("", listener.str());
65 EXPECT_EQ("42", listener.str());
67 // Streaming shouldn't crash when the underlying ostream is NULL.
68 DummyMatchResultListener dummy;
72 TEST(MatchResultListenerTest, CanAccessUnderlyingStream) {
73 EXPECT_TRUE(DummyMatchResultListener().stream() == nullptr);
74 EXPECT_TRUE(StreamMatchResultListener(nullptr).stream() == nullptr);
76 EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream());
79 TEST(MatchResultListenerTest, IsInterestedWorks) {
80 EXPECT_TRUE(StringMatchResultListener().IsInterested());
81 EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested());
83 EXPECT_FALSE(DummyMatchResultListener().IsInterested());
84 EXPECT_FALSE(StreamMatchResultListener(nullptr).IsInterested());
87 // Makes sure that the MatcherInterface<T> interface doesn't
89 class EvenMatcherImpl : public MatcherInterface<int> {
91 bool MatchAndExplain(int x,
92 MatchResultListener* /* listener */) const override {
96 void DescribeTo(ostream* os) const override { *os << "is an even number"; }
98 // We deliberately don't define DescribeNegationTo() and
99 // ExplainMatchResultTo() here, to make sure the definition of these
100 // two methods is optional.
103 // Makes sure that the MatcherInterface API doesn't change.
104 TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) {
108 // Tests implementing a monomorphic matcher using MatchAndExplain().
110 class NewEvenMatcherImpl : public MatcherInterface<int> {
112 bool MatchAndExplain(int x, MatchResultListener* listener) const override {
113 const bool match = x % 2 == 0;
114 // Verifies that we can stream to a listener directly.
115 *listener << "value % " << 2;
116 if (listener->stream() != nullptr) {
117 // Verifies that we can stream to a listener's underlying stream
119 *listener->stream() << " == " << (x % 2);
124 void DescribeTo(ostream* os) const override { *os << "is an even number"; }
127 TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
128 Matcher<int> m = MakeMatcher(new NewEvenMatcherImpl);
129 EXPECT_TRUE(m.Matches(2));
130 EXPECT_FALSE(m.Matches(3));
131 EXPECT_EQ("value % 2 == 0", Explain(m, 2));
132 EXPECT_EQ("value % 2 == 1", Explain(m, 3));
135 INSTANTIATE_GTEST_MATCHER_TEST_P(MatcherTest);
137 // Tests default-constructing a matcher.
138 TEST(MatcherTest, CanBeDefaultConstructed) { Matcher<double> m; }
140 // Tests that Matcher<T> can be constructed from a MatcherInterface<T>*.
141 TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {
142 const MatcherInterface<int>* impl = new EvenMatcherImpl;
143 Matcher<int> m(impl);
144 EXPECT_TRUE(m.Matches(4));
145 EXPECT_FALSE(m.Matches(5));
148 // Tests that value can be used in place of Eq(value).
149 TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) {
151 EXPECT_TRUE(m1.Matches(5));
152 EXPECT_FALSE(m1.Matches(6));
155 // Tests that NULL can be used in place of Eq(NULL).
156 TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) {
157 Matcher<int*> m1 = nullptr;
158 EXPECT_TRUE(m1.Matches(nullptr));
160 EXPECT_FALSE(m1.Matches(&n));
163 // Tests that matchers can be constructed from a variable that is not properly
164 // defined. This should be illegal, but many users rely on this accidentally.
166 virtual ~Undefined() = 0;
167 static const int kInt = 1;
170 TEST(MatcherTest, CanBeConstructedFromUndefinedVariable) {
171 Matcher<int> m1 = Undefined::kInt;
172 EXPECT_TRUE(m1.Matches(1));
173 EXPECT_FALSE(m1.Matches(2));
176 // Test that a matcher parameterized with an abstract class compiles.
177 TEST(MatcherTest, CanAcceptAbstractClass) { Matcher<const Undefined&> m = _; }
179 // Tests that matchers are copyable.
180 TEST(MatcherTest, IsCopyable) {
181 // Tests the copy constructor.
182 Matcher<bool> m1 = Eq(false);
183 EXPECT_TRUE(m1.Matches(false));
184 EXPECT_FALSE(m1.Matches(true));
186 // Tests the assignment operator.
188 EXPECT_TRUE(m1.Matches(true));
189 EXPECT_FALSE(m1.Matches(false));
192 // Tests that Matcher<T>::DescribeTo() calls
193 // MatcherInterface<T>::DescribeTo().
194 TEST(MatcherTest, CanDescribeItself) {
195 EXPECT_EQ("is an even number", Describe(Matcher<int>(new EvenMatcherImpl)));
198 // Tests Matcher<T>::MatchAndExplain().
199 TEST_P(MatcherTestP, MatchAndExplain) {
200 Matcher<int> m = GreaterThan(0);
201 StringMatchResultListener listener1;
202 EXPECT_TRUE(m.MatchAndExplain(42, &listener1));
203 EXPECT_EQ("which is 42 more than 0", listener1.str());
205 StringMatchResultListener listener2;
206 EXPECT_FALSE(m.MatchAndExplain(-9, &listener2));
207 EXPECT_EQ("which is 9 less than 0", listener2.str());
210 // Tests that a C-string literal can be implicitly converted to a
211 // Matcher<std::string> or Matcher<const std::string&>.
212 TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
213 Matcher<std::string> m1 = "hi";
214 EXPECT_TRUE(m1.Matches("hi"));
215 EXPECT_FALSE(m1.Matches("hello"));
217 Matcher<const std::string&> m2 = "hi";
218 EXPECT_TRUE(m2.Matches("hi"));
219 EXPECT_FALSE(m2.Matches("hello"));
222 // Tests that a string object can be implicitly converted to a
223 // Matcher<std::string> or Matcher<const std::string&>.
224 TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
225 Matcher<std::string> m1 = std::string("hi");
226 EXPECT_TRUE(m1.Matches("hi"));
227 EXPECT_FALSE(m1.Matches("hello"));
229 Matcher<const std::string&> m2 = std::string("hi");
230 EXPECT_TRUE(m2.Matches("hi"));
231 EXPECT_FALSE(m2.Matches("hello"));
234 #if GTEST_INTERNAL_HAS_STRING_VIEW
235 // Tests that a C-string literal can be implicitly converted to a
236 // Matcher<StringView> or Matcher<const StringView&>.
237 TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
238 Matcher<internal::StringView> m1 = "cats";
239 EXPECT_TRUE(m1.Matches("cats"));
240 EXPECT_FALSE(m1.Matches("dogs"));
242 Matcher<const internal::StringView&> m2 = "cats";
243 EXPECT_TRUE(m2.Matches("cats"));
244 EXPECT_FALSE(m2.Matches("dogs"));
247 // Tests that a std::string object can be implicitly converted to a
248 // Matcher<StringView> or Matcher<const StringView&>.
249 TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromString) {
250 Matcher<internal::StringView> m1 = std::string("cats");
251 EXPECT_TRUE(m1.Matches("cats"));
252 EXPECT_FALSE(m1.Matches("dogs"));
254 Matcher<const internal::StringView&> m2 = std::string("cats");
255 EXPECT_TRUE(m2.Matches("cats"));
256 EXPECT_FALSE(m2.Matches("dogs"));
259 // Tests that a StringView object can be implicitly converted to a
260 // Matcher<StringView> or Matcher<const StringView&>.
261 TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromStringView) {
262 Matcher<internal::StringView> m1 = internal::StringView("cats");
263 EXPECT_TRUE(m1.Matches("cats"));
264 EXPECT_FALSE(m1.Matches("dogs"));
266 Matcher<const internal::StringView&> m2 = internal::StringView("cats");
267 EXPECT_TRUE(m2.Matches("cats"));
268 EXPECT_FALSE(m2.Matches("dogs"));
270 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
272 // Tests that a std::reference_wrapper<std::string> object can be implicitly
273 // converted to a Matcher<std::string> or Matcher<const std::string&> via Eq().
274 TEST(StringMatcherTest,
275 CanBeImplicitlyConstructedFromEqReferenceWrapperString) {
276 std::string value = "cats";
277 Matcher<std::string> m1 = Eq(std::ref(value));
278 EXPECT_TRUE(m1.Matches("cats"));
279 EXPECT_FALSE(m1.Matches("dogs"));
281 Matcher<const std::string&> m2 = Eq(std::ref(value));
282 EXPECT_TRUE(m2.Matches("cats"));
283 EXPECT_FALSE(m2.Matches("dogs"));
286 // Tests that MakeMatcher() constructs a Matcher<T> from a
287 // MatcherInterface* without requiring the user to explicitly
289 TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {
290 const MatcherInterface<int>* dummy_impl = new EvenMatcherImpl;
291 Matcher<int> m = MakeMatcher(dummy_impl);
294 // Tests that MakePolymorphicMatcher() can construct a polymorphic
295 // matcher from its implementation using the old API.
297 class ReferencesBarOrIsZeroImpl {
299 template <typename T>
300 bool MatchAndExplain(const T& x, MatchResultListener* /* listener */) const {
302 return p == &g_bar || x == 0;
305 void DescribeTo(ostream* os) const { *os << "g_bar or zero"; }
307 void DescribeNegationTo(ostream* os) const {
308 *os << "doesn't reference g_bar and is not zero";
312 // This function verifies that MakePolymorphicMatcher() returns a
313 // PolymorphicMatcher<T> where T is the argument's type.
314 PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {
315 return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl());
318 TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {
319 // Using a polymorphic matcher to match a reference type.
320 Matcher<const int&> m1 = ReferencesBarOrIsZero();
321 EXPECT_TRUE(m1.Matches(0));
322 // Verifies that the identity of a by-reference argument is preserved.
323 EXPECT_TRUE(m1.Matches(g_bar));
324 EXPECT_FALSE(m1.Matches(1));
325 EXPECT_EQ("g_bar or zero", Describe(m1));
327 // Using a polymorphic matcher to match a value type.
328 Matcher<double> m2 = ReferencesBarOrIsZero();
329 EXPECT_TRUE(m2.Matches(0.0));
330 EXPECT_FALSE(m2.Matches(0.1));
331 EXPECT_EQ("g_bar or zero", Describe(m2));
334 // Tests implementing a polymorphic matcher using MatchAndExplain().
336 class PolymorphicIsEvenImpl {
338 void DescribeTo(ostream* os) const { *os << "is even"; }
340 void DescribeNegationTo(ostream* os) const { *os << "is odd"; }
342 template <typename T>
343 bool MatchAndExplain(const T& x, MatchResultListener* listener) const {
344 // Verifies that we can stream to the listener directly.
345 *listener << "% " << 2;
346 if (listener->stream() != nullptr) {
347 // Verifies that we can stream to the listener's underlying stream
349 *listener->stream() << " == " << (x % 2);
355 PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {
356 return MakePolymorphicMatcher(PolymorphicIsEvenImpl());
359 TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
360 // Using PolymorphicIsEven() as a Matcher<int>.
361 const Matcher<int> m1 = PolymorphicIsEven();
362 EXPECT_TRUE(m1.Matches(42));
363 EXPECT_FALSE(m1.Matches(43));
364 EXPECT_EQ("is even", Describe(m1));
366 const Matcher<int> not_m1 = Not(m1);
367 EXPECT_EQ("is odd", Describe(not_m1));
369 EXPECT_EQ("% 2 == 0", Explain(m1, 42));
371 // Using PolymorphicIsEven() as a Matcher<char>.
372 const Matcher<char> m2 = PolymorphicIsEven();
373 EXPECT_TRUE(m2.Matches('\x42'));
374 EXPECT_FALSE(m2.Matches('\x43'));
375 EXPECT_EQ("is even", Describe(m2));
377 const Matcher<char> not_m2 = Not(m2);
378 EXPECT_EQ("is odd", Describe(not_m2));
380 EXPECT_EQ("% 2 == 0", Explain(m2, '\x42'));
383 INSTANTIATE_GTEST_MATCHER_TEST_P(MatcherCastTest);
385 // Tests that MatcherCast<T>(m) works when m is a polymorphic matcher.
386 TEST_P(MatcherCastTestP, FromPolymorphicMatcher) {
388 if (use_gtest_matcher_) {
389 m = MatcherCast<int16_t>(GtestGreaterThan(int64_t{5}));
391 m = MatcherCast<int16_t>(Gt(int64_t{5}));
393 EXPECT_TRUE(m.Matches(6));
394 EXPECT_FALSE(m.Matches(4));
397 // For testing casting matchers between compatible types.
400 // An int can be statically (although not implicitly) cast to a
402 explicit IntValue(int a_value) : value_(a_value) {}
404 int value() const { return value_; }
410 // For testing casting matchers between compatible types.
411 bool IsPositiveIntValue(const IntValue& foo) { return foo.value() > 0; }
413 // Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T
414 // can be statically converted to U.
415 TEST(MatcherCastTest, FromCompatibleType) {
416 Matcher<double> m1 = Eq(2.0);
417 Matcher<int> m2 = MatcherCast<int>(m1);
418 EXPECT_TRUE(m2.Matches(2));
419 EXPECT_FALSE(m2.Matches(3));
421 Matcher<IntValue> m3 = Truly(IsPositiveIntValue);
422 Matcher<int> m4 = MatcherCast<int>(m3);
423 // In the following, the arguments 1 and 0 are statically converted
424 // to IntValue objects, and then tested by the IsPositiveIntValue()
426 EXPECT_TRUE(m4.Matches(1));
427 EXPECT_FALSE(m4.Matches(0));
430 // Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>.
431 TEST(MatcherCastTest, FromConstReferenceToNonReference) {
432 Matcher<const int&> m1 = Eq(0);
433 Matcher<int> m2 = MatcherCast<int>(m1);
434 EXPECT_TRUE(m2.Matches(0));
435 EXPECT_FALSE(m2.Matches(1));
438 // Tests that MatcherCast<T>(m) works when m is a Matcher<T&>.
439 TEST(MatcherCastTest, FromReferenceToNonReference) {
440 Matcher<int&> m1 = Eq(0);
441 Matcher<int> m2 = MatcherCast<int>(m1);
442 EXPECT_TRUE(m2.Matches(0));
443 EXPECT_FALSE(m2.Matches(1));
446 // Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
447 TEST(MatcherCastTest, FromNonReferenceToConstReference) {
448 Matcher<int> m1 = Eq(0);
449 Matcher<const int&> m2 = MatcherCast<const int&>(m1);
450 EXPECT_TRUE(m2.Matches(0));
451 EXPECT_FALSE(m2.Matches(1));
454 // Tests that MatcherCast<T&>(m) works when m is a Matcher<T>.
455 TEST(MatcherCastTest, FromNonReferenceToReference) {
456 Matcher<int> m1 = Eq(0);
457 Matcher<int&> m2 = MatcherCast<int&>(m1);
459 EXPECT_TRUE(m2.Matches(n));
461 EXPECT_FALSE(m2.Matches(n));
464 // Tests that MatcherCast<T>(m) works when m is a Matcher<T>.
465 TEST(MatcherCastTest, FromSameType) {
466 Matcher<int> m1 = Eq(0);
467 Matcher<int> m2 = MatcherCast<int>(m1);
468 EXPECT_TRUE(m2.Matches(0));
469 EXPECT_FALSE(m2.Matches(1));
472 // Tests that MatcherCast<T>(m) works when m is a value of the same type as the
473 // value type of the Matcher.
474 TEST(MatcherCastTest, FromAValue) {
475 Matcher<int> m = MatcherCast<int>(42);
476 EXPECT_TRUE(m.Matches(42));
477 EXPECT_FALSE(m.Matches(239));
480 // Tests that MatcherCast<T>(m) works when m is a value of the type implicitly
481 // convertible to the value type of the Matcher.
482 TEST(MatcherCastTest, FromAnImplicitlyConvertibleValue) {
483 const int kExpected = 'c';
484 Matcher<int> m = MatcherCast<int>('c');
485 EXPECT_TRUE(m.Matches(kExpected));
486 EXPECT_FALSE(m.Matches(kExpected + 1));
489 struct NonImplicitlyConstructibleTypeWithOperatorEq {
490 friend bool operator==(
491 const NonImplicitlyConstructibleTypeWithOperatorEq& /* ignored */,
495 friend bool operator==(
497 const NonImplicitlyConstructibleTypeWithOperatorEq& /* ignored */) {
502 // Tests that MatcherCast<T>(m) works when m is a neither a matcher nor
503 // implicitly convertible to the value type of the Matcher, but the value type
504 // of the matcher has operator==() overload accepting m.
505 TEST(MatcherCastTest, NonImplicitlyConstructibleTypeWithOperatorEq) {
506 Matcher<NonImplicitlyConstructibleTypeWithOperatorEq> m1 =
507 MatcherCast<NonImplicitlyConstructibleTypeWithOperatorEq>(42);
508 EXPECT_TRUE(m1.Matches(NonImplicitlyConstructibleTypeWithOperatorEq()));
510 Matcher<NonImplicitlyConstructibleTypeWithOperatorEq> m2 =
511 MatcherCast<NonImplicitlyConstructibleTypeWithOperatorEq>(239);
512 EXPECT_FALSE(m2.Matches(NonImplicitlyConstructibleTypeWithOperatorEq()));
514 // When updating the following lines please also change the comment to
515 // namespace convertible_from_any.
517 MatcherCast<int>(NonImplicitlyConstructibleTypeWithOperatorEq());
518 EXPECT_TRUE(m3.Matches(42));
519 EXPECT_FALSE(m3.Matches(239));
522 // ConvertibleFromAny does not work with MSVC. resulting in
523 // error C2440: 'initializing': cannot convert from 'Eq' to 'M'
524 // No constructor could take the source type, or constructor overload
525 // resolution was ambiguous
527 #if !defined _MSC_VER
529 // The below ConvertibleFromAny struct is implicitly constructible from anything
530 // and when in the same namespace can interact with other tests. In particular,
531 // if it is in the same namespace as other tests and one removes
532 // NonImplicitlyConstructibleTypeWithOperatorEq::operator==(int lhs, ...);
533 // then the corresponding test still compiles (and it should not!) by implicitly
534 // converting NonImplicitlyConstructibleTypeWithOperatorEq to ConvertibleFromAny
536 namespace convertible_from_any {
537 // Implicitly convertible from any type.
538 struct ConvertibleFromAny {
539 ConvertibleFromAny(int a_value) : value(a_value) {}
540 template <typename T>
541 ConvertibleFromAny(const T& /*a_value*/) : value(-1) {
542 ADD_FAILURE() << "Conversion constructor called";
547 bool operator==(const ConvertibleFromAny& a, const ConvertibleFromAny& b) {
548 return a.value == b.value;
551 ostream& operator<<(ostream& os, const ConvertibleFromAny& a) {
552 return os << a.value;
555 TEST(MatcherCastTest, ConversionConstructorIsUsed) {
556 Matcher<ConvertibleFromAny> m = MatcherCast<ConvertibleFromAny>(1);
557 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
558 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
561 TEST(MatcherCastTest, FromConvertibleFromAny) {
562 Matcher<ConvertibleFromAny> m =
563 MatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
564 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
565 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
567 } // namespace convertible_from_any
569 #endif // !defined _MSC_VER
571 struct IntReferenceWrapper {
572 IntReferenceWrapper(const int& a_value) : value(&a_value) {}
576 bool operator==(const IntReferenceWrapper& a, const IntReferenceWrapper& b) {
577 return a.value == b.value;
580 TEST(MatcherCastTest, ValueIsNotCopied) {
582 Matcher<IntReferenceWrapper> m = MatcherCast<IntReferenceWrapper>(n);
583 // Verify that the matcher holds a reference to n, not to its temporary copy.
584 EXPECT_TRUE(m.Matches(n));
593 Base(const Base&) = delete;
594 Base& operator=(const Base&) = delete;
597 class Derived : public Base {
599 Derived() : Base() {}
603 class OtherDerived : public Base {};
605 INSTANTIATE_GTEST_MATCHER_TEST_P(SafeMatcherCastTest);
607 // Tests that SafeMatcherCast<T>(m) works when m is a polymorphic matcher.
608 TEST_P(SafeMatcherCastTestP, FromPolymorphicMatcher) {
610 if (use_gtest_matcher_) {
611 m2 = SafeMatcherCast<char>(GtestGreaterThan(32));
613 m2 = SafeMatcherCast<char>(Gt(32));
615 EXPECT_TRUE(m2.Matches('A'));
616 EXPECT_FALSE(m2.Matches('\n'));
619 // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where
620 // T and U are arithmetic types and T can be losslessly converted to
622 TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {
623 Matcher<double> m1 = DoubleEq(1.0);
624 Matcher<float> m2 = SafeMatcherCast<float>(m1);
625 EXPECT_TRUE(m2.Matches(1.0f));
626 EXPECT_FALSE(m2.Matches(2.0f));
628 Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>('a'));
629 EXPECT_TRUE(m3.Matches('a'));
630 EXPECT_FALSE(m3.Matches('b'));
633 // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where T and U
634 // are pointers or references to a derived and a base class, correspondingly.
635 TEST(SafeMatcherCastTest, FromBaseClass) {
637 Matcher<Base*> m1 = Eq(&d);
638 Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);
639 EXPECT_TRUE(m2.Matches(&d));
640 EXPECT_FALSE(m2.Matches(&d2));
642 Matcher<Base&> m3 = Ref(d);
643 Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);
644 EXPECT_TRUE(m4.Matches(d));
645 EXPECT_FALSE(m4.Matches(d2));
648 // Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>.
649 TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
651 Matcher<const int&> m1 = Ref(n);
652 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
654 EXPECT_TRUE(m2.Matches(n));
655 EXPECT_FALSE(m2.Matches(n1));
658 // Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
659 TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {
660 Matcher<std::unique_ptr<int>> m1 = IsNull();
661 Matcher<const std::unique_ptr<int>&> m2 =
662 SafeMatcherCast<const std::unique_ptr<int>&>(m1);
663 EXPECT_TRUE(m2.Matches(std::unique_ptr<int>()));
664 EXPECT_FALSE(m2.Matches(std::unique_ptr<int>(new int)));
667 // Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<T>.
668 TEST(SafeMatcherCastTest, FromNonReferenceToReference) {
669 Matcher<int> m1 = Eq(0);
670 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
672 EXPECT_TRUE(m2.Matches(n));
674 EXPECT_FALSE(m2.Matches(n));
677 // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<T>.
678 TEST(SafeMatcherCastTest, FromSameType) {
679 Matcher<int> m1 = Eq(0);
680 Matcher<int> m2 = SafeMatcherCast<int>(m1);
681 EXPECT_TRUE(m2.Matches(0));
682 EXPECT_FALSE(m2.Matches(1));
685 #if !defined _MSC_VER
687 namespace convertible_from_any {
688 TEST(SafeMatcherCastTest, ConversionConstructorIsUsed) {
689 Matcher<ConvertibleFromAny> m = SafeMatcherCast<ConvertibleFromAny>(1);
690 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
691 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
694 TEST(SafeMatcherCastTest, FromConvertibleFromAny) {
695 Matcher<ConvertibleFromAny> m =
696 SafeMatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
697 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
698 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
700 } // namespace convertible_from_any
702 #endif // !defined _MSC_VER
704 TEST(SafeMatcherCastTest, ValueIsNotCopied) {
706 Matcher<IntReferenceWrapper> m = SafeMatcherCast<IntReferenceWrapper>(n);
707 // Verify that the matcher holds a reference to n, not to its temporary copy.
708 EXPECT_TRUE(m.Matches(n));
711 TEST(ExpectThat, TakesLiterals) {
713 EXPECT_THAT(1.0, 1.0);
714 EXPECT_THAT(std::string(), "");
717 TEST(ExpectThat, TakesFunctions) {
719 static void Func() {}
721 void (*func)() = Helper::Func;
722 EXPECT_THAT(func, Helper::Func);
723 EXPECT_THAT(func, &Helper::Func);
726 // Tests that A<T>() matches any value of type T.
727 TEST(ATest, MatchesAnyValue) {
728 // Tests a matcher for a value type.
729 Matcher<double> m1 = A<double>();
730 EXPECT_TRUE(m1.Matches(91.43));
731 EXPECT_TRUE(m1.Matches(-15.32));
733 // Tests a matcher for a reference type.
736 Matcher<int&> m2 = A<int&>();
737 EXPECT_TRUE(m2.Matches(a));
738 EXPECT_TRUE(m2.Matches(b));
741 TEST(ATest, WorksForDerivedClass) {
744 EXPECT_THAT(&base, A<Base*>());
745 // This shouldn't compile: EXPECT_THAT(&base, A<Derived*>());
746 EXPECT_THAT(&derived, A<Base*>());
747 EXPECT_THAT(&derived, A<Derived*>());
750 // Tests that A<T>() describes itself properly.
751 TEST(ATest, CanDescribeSelf) { EXPECT_EQ("is anything", Describe(A<bool>())); }
753 // Tests that An<T>() matches any value of type T.
754 TEST(AnTest, MatchesAnyValue) {
755 // Tests a matcher for a value type.
756 Matcher<int> m1 = An<int>();
757 EXPECT_TRUE(m1.Matches(9143));
758 EXPECT_TRUE(m1.Matches(-1532));
760 // Tests a matcher for a reference type.
763 Matcher<int&> m2 = An<int&>();
764 EXPECT_TRUE(m2.Matches(a));
765 EXPECT_TRUE(m2.Matches(b));
768 // Tests that An<T>() describes itself properly.
769 TEST(AnTest, CanDescribeSelf) { EXPECT_EQ("is anything", Describe(An<int>())); }
771 // Tests that _ can be used as a matcher for any type and matches any
772 // value of that type.
773 TEST(UnderscoreTest, MatchesAnyValue) {
774 // Uses _ as a matcher for a value type.
776 EXPECT_TRUE(m1.Matches(123));
777 EXPECT_TRUE(m1.Matches(-242));
779 // Uses _ as a matcher for a reference type.
782 Matcher<const bool&> m2 = _;
783 EXPECT_TRUE(m2.Matches(a));
784 EXPECT_TRUE(m2.Matches(b));
787 // Tests that _ describes itself properly.
788 TEST(UnderscoreTest, CanDescribeSelf) {
790 EXPECT_EQ("is anything", Describe(m));
793 // Tests that Eq(x) matches any value equal to x.
794 TEST(EqTest, MatchesEqualValue) {
795 // 2 C-strings with same content but different addresses.
796 const char a1[] = "hi";
797 const char a2[] = "hi";
799 Matcher<const char*> m1 = Eq(a1);
800 EXPECT_TRUE(m1.Matches(a1));
801 EXPECT_FALSE(m1.Matches(a2));
804 // Tests that Eq(v) describes itself properly.
808 Unprintable() : c_('a') {}
810 bool operator==(const Unprintable& /* rhs */) const { return true; }
811 // -Wunused-private-field: dummy accessor for `c_`.
812 char dummy_c() { return c_; }
818 TEST(EqTest, CanDescribeSelf) {
819 Matcher<Unprintable> m = Eq(Unprintable());
820 EXPECT_EQ("is equal to 1-byte object <61>", Describe(m));
823 // Tests that Eq(v) can be used to match any type that supports
824 // comparing with type T, where T is v's type.
825 TEST(EqTest, IsPolymorphic) {
826 Matcher<int> m1 = Eq(1);
827 EXPECT_TRUE(m1.Matches(1));
828 EXPECT_FALSE(m1.Matches(2));
830 Matcher<char> m2 = Eq(1);
831 EXPECT_TRUE(m2.Matches('\1'));
832 EXPECT_FALSE(m2.Matches('a'));
835 // Tests that TypedEq<T>(v) matches values of type T that's equal to v.
836 TEST(TypedEqTest, ChecksEqualityForGivenType) {
837 Matcher<char> m1 = TypedEq<char>('a');
838 EXPECT_TRUE(m1.Matches('a'));
839 EXPECT_FALSE(m1.Matches('b'));
841 Matcher<int> m2 = TypedEq<int>(6);
842 EXPECT_TRUE(m2.Matches(6));
843 EXPECT_FALSE(m2.Matches(7));
846 // Tests that TypedEq(v) describes itself properly.
847 TEST(TypedEqTest, CanDescribeSelf) {
848 EXPECT_EQ("is equal to 2", Describe(TypedEq<int>(2)));
851 // Tests that TypedEq<T>(v) has type Matcher<T>.
853 // Type<T>::IsTypeOf(v) compiles if and only if the type of value v is T, where
854 // T is a "bare" type (i.e. not in the form of const U or U&). If v's type is
855 // not T, the compiler will generate a message about "undefined reference".
856 template <typename T>
858 static bool IsTypeOf(const T& /* v */) { return true; }
860 template <typename T2>
861 static void IsTypeOf(T2 v);
864 TEST(TypedEqTest, HasSpecifiedType) {
865 // Verfies that the type of TypedEq<T>(v) is Matcher<T>.
866 Type<Matcher<int>>::IsTypeOf(TypedEq<int>(5));
867 Type<Matcher<double>>::IsTypeOf(TypedEq<double>(5));
870 // Tests that Ge(v) matches anything >= v.
871 TEST(GeTest, ImplementsGreaterThanOrEqual) {
872 Matcher<int> m1 = Ge(0);
873 EXPECT_TRUE(m1.Matches(1));
874 EXPECT_TRUE(m1.Matches(0));
875 EXPECT_FALSE(m1.Matches(-1));
878 // Tests that Ge(v) describes itself properly.
879 TEST(GeTest, CanDescribeSelf) {
880 Matcher<int> m = Ge(5);
881 EXPECT_EQ("is >= 5", Describe(m));
884 // Tests that Gt(v) matches anything > v.
885 TEST(GtTest, ImplementsGreaterThan) {
886 Matcher<double> m1 = Gt(0);
887 EXPECT_TRUE(m1.Matches(1.0));
888 EXPECT_FALSE(m1.Matches(0.0));
889 EXPECT_FALSE(m1.Matches(-1.0));
892 // Tests that Gt(v) describes itself properly.
893 TEST(GtTest, CanDescribeSelf) {
894 Matcher<int> m = Gt(5);
895 EXPECT_EQ("is > 5", Describe(m));
898 // Tests that Le(v) matches anything <= v.
899 TEST(LeTest, ImplementsLessThanOrEqual) {
900 Matcher<char> m1 = Le('b');
901 EXPECT_TRUE(m1.Matches('a'));
902 EXPECT_TRUE(m1.Matches('b'));
903 EXPECT_FALSE(m1.Matches('c'));
906 // Tests that Le(v) describes itself properly.
907 TEST(LeTest, CanDescribeSelf) {
908 Matcher<int> m = Le(5);
909 EXPECT_EQ("is <= 5", Describe(m));
912 // Tests that Lt(v) matches anything < v.
913 TEST(LtTest, ImplementsLessThan) {
914 Matcher<const std::string&> m1 = Lt("Hello");
915 EXPECT_TRUE(m1.Matches("Abc"));
916 EXPECT_FALSE(m1.Matches("Hello"));
917 EXPECT_FALSE(m1.Matches("Hello, world!"));
920 // Tests that Lt(v) describes itself properly.
921 TEST(LtTest, CanDescribeSelf) {
922 Matcher<int> m = Lt(5);
923 EXPECT_EQ("is < 5", Describe(m));
926 // Tests that Ne(v) matches anything != v.
927 TEST(NeTest, ImplementsNotEqual) {
928 Matcher<int> m1 = Ne(0);
929 EXPECT_TRUE(m1.Matches(1));
930 EXPECT_TRUE(m1.Matches(-1));
931 EXPECT_FALSE(m1.Matches(0));
934 // Tests that Ne(v) describes itself properly.
935 TEST(NeTest, CanDescribeSelf) {
936 Matcher<int> m = Ne(5);
937 EXPECT_EQ("isn't equal to 5", Describe(m));
942 explicit MoveOnly(int i) : i_(i) {}
943 MoveOnly(const MoveOnly&) = delete;
944 MoveOnly(MoveOnly&&) = default;
945 MoveOnly& operator=(const MoveOnly&) = delete;
946 MoveOnly& operator=(MoveOnly&&) = default;
948 bool operator==(const MoveOnly& other) const { return i_ == other.i_; }
949 bool operator!=(const MoveOnly& other) const { return i_ != other.i_; }
950 bool operator<(const MoveOnly& other) const { return i_ < other.i_; }
951 bool operator<=(const MoveOnly& other) const { return i_ <= other.i_; }
952 bool operator>(const MoveOnly& other) const { return i_ > other.i_; }
953 bool operator>=(const MoveOnly& other) const { return i_ >= other.i_; }
960 MOCK_METHOD1(Call, void(MoveOnly));
963 // Disable this test in VS 2015 (version 14), where it fails when SEH is enabled
964 #if defined(_MSC_VER) && (_MSC_VER < 1910)
965 TEST(ComparisonBaseTest, DISABLED_WorksWithMoveOnly) {
967 TEST(ComparisonBaseTest, WorksWithMoveOnly) {
972 EXPECT_CALL(helper, Call(Eq(ByRef(m))));
973 helper.Call(MoveOnly(0));
974 EXPECT_CALL(helper, Call(Ne(ByRef(m))));
975 helper.Call(MoveOnly(1));
976 EXPECT_CALL(helper, Call(Le(ByRef(m))));
977 helper.Call(MoveOnly(0));
978 EXPECT_CALL(helper, Call(Lt(ByRef(m))));
979 helper.Call(MoveOnly(-1));
980 EXPECT_CALL(helper, Call(Ge(ByRef(m))));
981 helper.Call(MoveOnly(0));
982 EXPECT_CALL(helper, Call(Gt(ByRef(m))));
983 helper.Call(MoveOnly(1));
986 // Tests that IsNull() matches any NULL pointer of any type.
987 TEST(IsNullTest, MatchesNullPointer) {
988 Matcher<int*> m1 = IsNull();
991 EXPECT_TRUE(m1.Matches(p1));
992 EXPECT_FALSE(m1.Matches(&n));
994 Matcher<const char*> m2 = IsNull();
995 const char* p2 = nullptr;
996 EXPECT_TRUE(m2.Matches(p2));
997 EXPECT_FALSE(m2.Matches("hi"));
999 Matcher<void*> m3 = IsNull();
1001 EXPECT_TRUE(m3.Matches(p3));
1002 EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef)));
1005 TEST(IsNullTest, StdFunction) {
1006 const Matcher<std::function<void()>> m = IsNull();
1008 EXPECT_TRUE(m.Matches(std::function<void()>()));
1009 EXPECT_FALSE(m.Matches([] {}));
1012 // Tests that IsNull() describes itself properly.
1013 TEST(IsNullTest, CanDescribeSelf) {
1014 Matcher<int*> m = IsNull();
1015 EXPECT_EQ("is NULL", Describe(m));
1016 EXPECT_EQ("isn't NULL", DescribeNegation(m));
1019 // Tests that NotNull() matches any non-NULL pointer of any type.
1020 TEST(NotNullTest, MatchesNonNullPointer) {
1021 Matcher<int*> m1 = NotNull();
1024 EXPECT_FALSE(m1.Matches(p1));
1025 EXPECT_TRUE(m1.Matches(&n));
1027 Matcher<const char*> m2 = NotNull();
1028 const char* p2 = nullptr;
1029 EXPECT_FALSE(m2.Matches(p2));
1030 EXPECT_TRUE(m2.Matches("hi"));
1033 TEST(NotNullTest, LinkedPtr) {
1034 const Matcher<std::shared_ptr<int>> m = NotNull();
1035 const std::shared_ptr<int> null_p;
1036 const std::shared_ptr<int> non_null_p(new int);
1038 EXPECT_FALSE(m.Matches(null_p));
1039 EXPECT_TRUE(m.Matches(non_null_p));
1042 TEST(NotNullTest, ReferenceToConstLinkedPtr) {
1043 const Matcher<const std::shared_ptr<double>&> m = NotNull();
1044 const std::shared_ptr<double> null_p;
1045 const std::shared_ptr<double> non_null_p(new double);
1047 EXPECT_FALSE(m.Matches(null_p));
1048 EXPECT_TRUE(m.Matches(non_null_p));
1051 TEST(NotNullTest, StdFunction) {
1052 const Matcher<std::function<void()>> m = NotNull();
1054 EXPECT_TRUE(m.Matches([] {}));
1055 EXPECT_FALSE(m.Matches(std::function<void()>()));
1058 // Tests that NotNull() describes itself properly.
1059 TEST(NotNullTest, CanDescribeSelf) {
1060 Matcher<int*> m = NotNull();
1061 EXPECT_EQ("isn't NULL", Describe(m));
1064 // Tests that Ref(variable) matches an argument that references
1066 TEST(RefTest, MatchesSameVariable) {
1069 Matcher<int&> m = Ref(a);
1070 EXPECT_TRUE(m.Matches(a));
1071 EXPECT_FALSE(m.Matches(b));
1074 // Tests that Ref(variable) describes itself properly.
1075 TEST(RefTest, CanDescribeSelf) {
1077 Matcher<int&> m = Ref(n);
1079 ss << "references the variable @" << &n << " 5";
1080 EXPECT_EQ(ss.str(), Describe(m));
1083 // Test that Ref(non_const_varialbe) can be used as a matcher for a
1085 TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
1088 Matcher<const int&> m = Ref(a);
1089 EXPECT_TRUE(m.Matches(a));
1090 EXPECT_FALSE(m.Matches(b));
1093 // Tests that Ref(variable) is covariant, i.e. Ref(derived) can be
1094 // used wherever Ref(base) can be used (Ref(derived) is a sub-type
1095 // of Ref(base), but not vice versa.
1097 TEST(RefTest, IsCovariant) {
1100 Matcher<const Base&> m1 = Ref(base);
1101 EXPECT_TRUE(m1.Matches(base));
1102 EXPECT_FALSE(m1.Matches(base2));
1103 EXPECT_FALSE(m1.Matches(derived));
1106 EXPECT_TRUE(m1.Matches(derived));
1107 EXPECT_FALSE(m1.Matches(base));
1108 EXPECT_FALSE(m1.Matches(base2));
1111 TEST(RefTest, ExplainsResult) {
1113 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n),
1114 StartsWith("which is located @"));
1117 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m),
1118 StartsWith("which is located @"));
1121 // Tests string comparison matchers.
1123 template <typename T = std::string>
1124 std::string FromStringLike(internal::StringLike<T> str) {
1125 return std::string(str);
1128 TEST(StringLike, TestConversions) {
1129 EXPECT_EQ("foo", FromStringLike("foo"));
1130 EXPECT_EQ("foo", FromStringLike(std::string("foo")));
1131 #if GTEST_INTERNAL_HAS_STRING_VIEW
1132 EXPECT_EQ("foo", FromStringLike(internal::StringView("foo")));
1133 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1135 // Non deducible types.
1136 EXPECT_EQ("", FromStringLike({}));
1137 EXPECT_EQ("foo", FromStringLike({'f', 'o', 'o'}));
1138 const char buf[] = "foo";
1139 EXPECT_EQ("foo", FromStringLike({buf, buf + 3}));
1142 TEST(StrEqTest, MatchesEqualString) {
1143 Matcher<const char*> m = StrEq(std::string("Hello"));
1144 EXPECT_TRUE(m.Matches("Hello"));
1145 EXPECT_FALSE(m.Matches("hello"));
1146 EXPECT_FALSE(m.Matches(nullptr));
1148 Matcher<const std::string&> m2 = StrEq("Hello");
1149 EXPECT_TRUE(m2.Matches("Hello"));
1150 EXPECT_FALSE(m2.Matches("Hi"));
1152 #if GTEST_INTERNAL_HAS_STRING_VIEW
1153 Matcher<const internal::StringView&> m3 =
1154 StrEq(internal::StringView("Hello"));
1155 EXPECT_TRUE(m3.Matches(internal::StringView("Hello")));
1156 EXPECT_FALSE(m3.Matches(internal::StringView("hello")));
1157 EXPECT_FALSE(m3.Matches(internal::StringView()));
1159 Matcher<const internal::StringView&> m_empty = StrEq("");
1160 EXPECT_TRUE(m_empty.Matches(internal::StringView("")));
1161 EXPECT_TRUE(m_empty.Matches(internal::StringView()));
1162 EXPECT_FALSE(m_empty.Matches(internal::StringView("hello")));
1163 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1166 TEST(StrEqTest, CanDescribeSelf) {
1167 Matcher<std::string> m = StrEq("Hi-\'\"?\\\a\b\f\n\r\t\v\xD3");
1168 EXPECT_EQ("is equal to \"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
1171 std::string str("01204500800");
1173 Matcher<std::string> m2 = StrEq(str);
1174 EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2));
1175 str[0] = str[6] = str[7] = str[9] = str[10] = '\0';
1176 Matcher<std::string> m3 = StrEq(str);
1177 EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));
1180 TEST(StrNeTest, MatchesUnequalString) {
1181 Matcher<const char*> m = StrNe("Hello");
1182 EXPECT_TRUE(m.Matches(""));
1183 EXPECT_TRUE(m.Matches(nullptr));
1184 EXPECT_FALSE(m.Matches("Hello"));
1186 Matcher<std::string> m2 = StrNe(std::string("Hello"));
1187 EXPECT_TRUE(m2.Matches("hello"));
1188 EXPECT_FALSE(m2.Matches("Hello"));
1190 #if GTEST_INTERNAL_HAS_STRING_VIEW
1191 Matcher<const internal::StringView> m3 = StrNe(internal::StringView("Hello"));
1192 EXPECT_TRUE(m3.Matches(internal::StringView("")));
1193 EXPECT_TRUE(m3.Matches(internal::StringView()));
1194 EXPECT_FALSE(m3.Matches(internal::StringView("Hello")));
1195 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1198 TEST(StrNeTest, CanDescribeSelf) {
1199 Matcher<const char*> m = StrNe("Hi");
1200 EXPECT_EQ("isn't equal to \"Hi\"", Describe(m));
1203 TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
1204 Matcher<const char*> m = StrCaseEq(std::string("Hello"));
1205 EXPECT_TRUE(m.Matches("Hello"));
1206 EXPECT_TRUE(m.Matches("hello"));
1207 EXPECT_FALSE(m.Matches("Hi"));
1208 EXPECT_FALSE(m.Matches(nullptr));
1210 Matcher<const std::string&> m2 = StrCaseEq("Hello");
1211 EXPECT_TRUE(m2.Matches("hello"));
1212 EXPECT_FALSE(m2.Matches("Hi"));
1214 #if GTEST_INTERNAL_HAS_STRING_VIEW
1215 Matcher<const internal::StringView&> m3 =
1216 StrCaseEq(internal::StringView("Hello"));
1217 EXPECT_TRUE(m3.Matches(internal::StringView("Hello")));
1218 EXPECT_TRUE(m3.Matches(internal::StringView("hello")));
1219 EXPECT_FALSE(m3.Matches(internal::StringView("Hi")));
1220 EXPECT_FALSE(m3.Matches(internal::StringView()));
1221 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1224 TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1225 std::string str1("oabocdooeoo");
1226 std::string str2("OABOCDOOEOO");
1227 Matcher<const std::string&> m0 = StrCaseEq(str1);
1228 EXPECT_FALSE(m0.Matches(str2 + std::string(1, '\0')));
1230 str1[3] = str2[3] = '\0';
1231 Matcher<const std::string&> m1 = StrCaseEq(str1);
1232 EXPECT_TRUE(m1.Matches(str2));
1234 str1[0] = str1[6] = str1[7] = str1[10] = '\0';
1235 str2[0] = str2[6] = str2[7] = str2[10] = '\0';
1236 Matcher<const std::string&> m2 = StrCaseEq(str1);
1237 str1[9] = str2[9] = '\0';
1238 EXPECT_FALSE(m2.Matches(str2));
1240 Matcher<const std::string&> m3 = StrCaseEq(str1);
1241 EXPECT_TRUE(m3.Matches(str2));
1243 EXPECT_FALSE(m3.Matches(str2 + "x"));
1244 str2.append(1, '\0');
1245 EXPECT_FALSE(m3.Matches(str2));
1246 EXPECT_FALSE(m3.Matches(std::string(str2, 0, 9)));
1249 TEST(StrCaseEqTest, CanDescribeSelf) {
1250 Matcher<std::string> m = StrCaseEq("Hi");
1251 EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m));
1254 TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1255 Matcher<const char*> m = StrCaseNe("Hello");
1256 EXPECT_TRUE(m.Matches("Hi"));
1257 EXPECT_TRUE(m.Matches(nullptr));
1258 EXPECT_FALSE(m.Matches("Hello"));
1259 EXPECT_FALSE(m.Matches("hello"));
1261 Matcher<std::string> m2 = StrCaseNe(std::string("Hello"));
1262 EXPECT_TRUE(m2.Matches(""));
1263 EXPECT_FALSE(m2.Matches("Hello"));
1265 #if GTEST_INTERNAL_HAS_STRING_VIEW
1266 Matcher<const internal::StringView> m3 =
1267 StrCaseNe(internal::StringView("Hello"));
1268 EXPECT_TRUE(m3.Matches(internal::StringView("Hi")));
1269 EXPECT_TRUE(m3.Matches(internal::StringView()));
1270 EXPECT_FALSE(m3.Matches(internal::StringView("Hello")));
1271 EXPECT_FALSE(m3.Matches(internal::StringView("hello")));
1272 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1275 TEST(StrCaseNeTest, CanDescribeSelf) {
1276 Matcher<const char*> m = StrCaseNe("Hi");
1277 EXPECT_EQ("isn't equal to (ignoring case) \"Hi\"", Describe(m));
1280 // Tests that HasSubstr() works for matching string-typed values.
1281 TEST(HasSubstrTest, WorksForStringClasses) {
1282 const Matcher<std::string> m1 = HasSubstr("foo");
1283 EXPECT_TRUE(m1.Matches(std::string("I love food.")));
1284 EXPECT_FALSE(m1.Matches(std::string("tofo")));
1286 const Matcher<const std::string&> m2 = HasSubstr("foo");
1287 EXPECT_TRUE(m2.Matches(std::string("I love food.")));
1288 EXPECT_FALSE(m2.Matches(std::string("tofo")));
1290 const Matcher<std::string> m_empty = HasSubstr("");
1291 EXPECT_TRUE(m_empty.Matches(std::string()));
1292 EXPECT_TRUE(m_empty.Matches(std::string("not empty")));
1295 // Tests that HasSubstr() works for matching C-string-typed values.
1296 TEST(HasSubstrTest, WorksForCStrings) {
1297 const Matcher<char*> m1 = HasSubstr("foo");
1298 EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food.")));
1299 EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo")));
1300 EXPECT_FALSE(m1.Matches(nullptr));
1302 const Matcher<const char*> m2 = HasSubstr("foo");
1303 EXPECT_TRUE(m2.Matches("I love food."));
1304 EXPECT_FALSE(m2.Matches("tofo"));
1305 EXPECT_FALSE(m2.Matches(nullptr));
1307 const Matcher<const char*> m_empty = HasSubstr("");
1308 EXPECT_TRUE(m_empty.Matches("not empty"));
1309 EXPECT_TRUE(m_empty.Matches(""));
1310 EXPECT_FALSE(m_empty.Matches(nullptr));
1313 #if GTEST_INTERNAL_HAS_STRING_VIEW
1314 // Tests that HasSubstr() works for matching StringView-typed values.
1315 TEST(HasSubstrTest, WorksForStringViewClasses) {
1316 const Matcher<internal::StringView> m1 =
1317 HasSubstr(internal::StringView("foo"));
1318 EXPECT_TRUE(m1.Matches(internal::StringView("I love food.")));
1319 EXPECT_FALSE(m1.Matches(internal::StringView("tofo")));
1320 EXPECT_FALSE(m1.Matches(internal::StringView()));
1322 const Matcher<const internal::StringView&> m2 = HasSubstr("foo");
1323 EXPECT_TRUE(m2.Matches(internal::StringView("I love food.")));
1324 EXPECT_FALSE(m2.Matches(internal::StringView("tofo")));
1325 EXPECT_FALSE(m2.Matches(internal::StringView()));
1327 const Matcher<const internal::StringView&> m3 = HasSubstr("");
1328 EXPECT_TRUE(m3.Matches(internal::StringView("foo")));
1329 EXPECT_TRUE(m3.Matches(internal::StringView("")));
1330 EXPECT_TRUE(m3.Matches(internal::StringView()));
1332 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1334 // Tests that HasSubstr(s) describes itself properly.
1335 TEST(HasSubstrTest, CanDescribeSelf) {
1336 Matcher<std::string> m = HasSubstr("foo\n\"");
1337 EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m));
1340 INSTANTIATE_GTEST_MATCHER_TEST_P(KeyTest);
1342 TEST(KeyTest, CanDescribeSelf) {
1343 Matcher<const pair<std::string, int>&> m = Key("foo");
1344 EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m));
1345 EXPECT_EQ("doesn't have a key that is equal to \"foo\"", DescribeNegation(m));
1348 TEST_P(KeyTestP, ExplainsResult) {
1349 Matcher<pair<int, bool>> m = Key(GreaterThan(10));
1350 EXPECT_EQ("whose first field is a value which is 5 less than 10",
1351 Explain(m, make_pair(5, true)));
1352 EXPECT_EQ("whose first field is a value which is 5 more than 10",
1353 Explain(m, make_pair(15, true)));
1356 TEST(KeyTest, MatchesCorrectly) {
1357 pair<int, std::string> p(25, "foo");
1358 EXPECT_THAT(p, Key(25));
1359 EXPECT_THAT(p, Not(Key(42)));
1360 EXPECT_THAT(p, Key(Ge(20)));
1361 EXPECT_THAT(p, Not(Key(Lt(25))));
1364 TEST(KeyTest, WorksWithMoveOnly) {
1365 pair<std::unique_ptr<int>, std::unique_ptr<int>> p;
1366 EXPECT_THAT(p, Key(Eq(nullptr)));
1369 INSTANTIATE_GTEST_MATCHER_TEST_P(PairTest);
1374 struct PairWithGet {
1376 std::string member_2;
1377 using first_type = int;
1378 using second_type = std::string;
1380 const int& GetImpl(Tag<0>) const { return member_1; }
1381 const std::string& GetImpl(Tag<1>) const { return member_2; }
1384 auto get(const PairWithGet& value) -> decltype(value.GetImpl(Tag<I>())) {
1385 return value.GetImpl(Tag<I>());
1387 TEST(PairTest, MatchesPairWithGetCorrectly) {
1388 PairWithGet p{25, "foo"};
1389 EXPECT_THAT(p, Key(25));
1390 EXPECT_THAT(p, Not(Key(42)));
1391 EXPECT_THAT(p, Key(Ge(20)));
1392 EXPECT_THAT(p, Not(Key(Lt(25))));
1394 std::vector<PairWithGet> v = {{11, "Foo"}, {29, "gMockIsBestMock"}};
1395 EXPECT_THAT(v, Contains(Key(29)));
1398 TEST(KeyTest, SafelyCastsInnerMatcher) {
1399 Matcher<int> is_positive = Gt(0);
1400 Matcher<int> is_negative = Lt(0);
1401 pair<char, bool> p('a', true);
1402 EXPECT_THAT(p, Key(is_positive));
1403 EXPECT_THAT(p, Not(Key(is_negative)));
1406 TEST(KeyTest, InsideContainsUsingMap) {
1407 map<int, char> container;
1408 container.insert(make_pair(1, 'a'));
1409 container.insert(make_pair(2, 'b'));
1410 container.insert(make_pair(4, 'c'));
1411 EXPECT_THAT(container, Contains(Key(1)));
1412 EXPECT_THAT(container, Not(Contains(Key(3))));
1415 TEST(KeyTest, InsideContainsUsingMultimap) {
1416 multimap<int, char> container;
1417 container.insert(make_pair(1, 'a'));
1418 container.insert(make_pair(2, 'b'));
1419 container.insert(make_pair(4, 'c'));
1421 EXPECT_THAT(container, Not(Contains(Key(25))));
1422 container.insert(make_pair(25, 'd'));
1423 EXPECT_THAT(container, Contains(Key(25)));
1424 container.insert(make_pair(25, 'e'));
1425 EXPECT_THAT(container, Contains(Key(25)));
1427 EXPECT_THAT(container, Contains(Key(1)));
1428 EXPECT_THAT(container, Not(Contains(Key(3))));
1431 TEST(PairTest, Typing) {
1432 // Test verifies the following type conversions can be compiled.
1433 Matcher<const pair<const char*, int>&> m1 = Pair("foo", 42);
1434 Matcher<const pair<const char*, int>> m2 = Pair("foo", 42);
1435 Matcher<pair<const char*, int>> m3 = Pair("foo", 42);
1437 Matcher<pair<int, const std::string>> m4 = Pair(25, "42");
1438 Matcher<pair<const std::string, int>> m5 = Pair("25", 42);
1441 TEST(PairTest, CanDescribeSelf) {
1442 Matcher<const pair<std::string, int>&> m1 = Pair("foo", 42);
1444 "has a first field that is equal to \"foo\""
1445 ", and has a second field that is equal to 42",
1448 "has a first field that isn't equal to \"foo\""
1449 ", or has a second field that isn't equal to 42",
1450 DescribeNegation(m1));
1451 // Double and triple negation (1 or 2 times not and description of negation).
1452 Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42));
1454 "has a first field that isn't equal to 13"
1455 ", and has a second field that is equal to 42",
1456 DescribeNegation(m2));
1459 TEST_P(PairTestP, CanExplainMatchResultTo) {
1460 // If neither field matches, Pair() should explain about the first
1462 const Matcher<pair<int, int>> m = Pair(GreaterThan(0), GreaterThan(0));
1463 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
1464 Explain(m, make_pair(-1, -2)));
1466 // If the first field matches but the second doesn't, Pair() should
1467 // explain about the second field.
1468 EXPECT_EQ("whose second field does not match, which is 2 less than 0",
1469 Explain(m, make_pair(1, -2)));
1471 // If the first field doesn't match but the second does, Pair()
1472 // should explain about the first field.
1473 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
1474 Explain(m, make_pair(-1, 2)));
1476 // If both fields match, Pair() should explain about them both.
1478 "whose both fields match, where the first field is a value "
1479 "which is 1 more than 0, and the second field is a value "
1480 "which is 2 more than 0",
1481 Explain(m, make_pair(1, 2)));
1483 // If only the first match has an explanation, only this explanation should
1485 const Matcher<pair<int, int>> explain_first = Pair(GreaterThan(0), 0);
1487 "whose both fields match, where the first field is a value "
1488 "which is 1 more than 0",
1489 Explain(explain_first, make_pair(1, 0)));
1491 // If only the second match has an explanation, only this explanation should
1493 const Matcher<pair<int, int>> explain_second = Pair(0, GreaterThan(0));
1495 "whose both fields match, where the second field is a value "
1496 "which is 1 more than 0",
1497 Explain(explain_second, make_pair(0, 1)));
1500 TEST(PairTest, MatchesCorrectly) {
1501 pair<int, std::string> p(25, "foo");
1503 // Both fields match.
1504 EXPECT_THAT(p, Pair(25, "foo"));
1505 EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o")));
1507 // 'first' doesnt' match, but 'second' matches.
1508 EXPECT_THAT(p, Not(Pair(42, "foo")));
1509 EXPECT_THAT(p, Not(Pair(Lt(25), "foo")));
1511 // 'first' matches, but 'second' doesn't match.
1512 EXPECT_THAT(p, Not(Pair(25, "bar")));
1513 EXPECT_THAT(p, Not(Pair(25, Not("foo"))));
1515 // Neither field matches.
1516 EXPECT_THAT(p, Not(Pair(13, "bar")));
1517 EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a"))));
1520 TEST(PairTest, WorksWithMoveOnly) {
1521 pair<std::unique_ptr<int>, std::unique_ptr<int>> p;
1522 p.second.reset(new int(7));
1523 EXPECT_THAT(p, Pair(Eq(nullptr), Ne(nullptr)));
1526 TEST(PairTest, SafelyCastsInnerMatchers) {
1527 Matcher<int> is_positive = Gt(0);
1528 Matcher<int> is_negative = Lt(0);
1529 pair<char, bool> p('a', true);
1530 EXPECT_THAT(p, Pair(is_positive, _));
1531 EXPECT_THAT(p, Not(Pair(is_negative, _)));
1532 EXPECT_THAT(p, Pair(_, is_positive));
1533 EXPECT_THAT(p, Not(Pair(_, is_negative)));
1536 TEST(PairTest, InsideContainsUsingMap) {
1537 map<int, char> container;
1538 container.insert(make_pair(1, 'a'));
1539 container.insert(make_pair(2, 'b'));
1540 container.insert(make_pair(4, 'c'));
1541 EXPECT_THAT(container, Contains(Pair(1, 'a')));
1542 EXPECT_THAT(container, Contains(Pair(1, _)));
1543 EXPECT_THAT(container, Contains(Pair(_, 'a')));
1544 EXPECT_THAT(container, Not(Contains(Pair(3, _))));
1547 INSTANTIATE_GTEST_MATCHER_TEST_P(FieldsAreTest);
1549 TEST(FieldsAreTest, MatchesCorrectly) {
1550 std::tuple<int, std::string, double> p(25, "foo", .5);
1552 // All fields match.
1553 EXPECT_THAT(p, FieldsAre(25, "foo", .5));
1554 EXPECT_THAT(p, FieldsAre(Ge(20), HasSubstr("o"), DoubleEq(.5)));
1556 // Some don't match.
1557 EXPECT_THAT(p, Not(FieldsAre(26, "foo", .5)));
1558 EXPECT_THAT(p, Not(FieldsAre(25, "fo", .5)));
1559 EXPECT_THAT(p, Not(FieldsAre(25, "foo", .6)));
1562 TEST(FieldsAreTest, CanDescribeSelf) {
1563 Matcher<const pair<std::string, int>&> m1 = FieldsAre("foo", 42);
1565 "has field #0 that is equal to \"foo\""
1566 ", and has field #1 that is equal to 42",
1569 "has field #0 that isn't equal to \"foo\""
1570 ", or has field #1 that isn't equal to 42",
1571 DescribeNegation(m1));
1574 TEST_P(FieldsAreTestP, CanExplainMatchResultTo) {
1575 // The first one that fails is the one that gives the error.
1576 Matcher<std::tuple<int, int, int>> m =
1577 FieldsAre(GreaterThan(0), GreaterThan(0), GreaterThan(0));
1579 EXPECT_EQ("whose field #0 does not match, which is 1 less than 0",
1580 Explain(m, std::make_tuple(-1, -2, -3)));
1581 EXPECT_EQ("whose field #1 does not match, which is 2 less than 0",
1582 Explain(m, std::make_tuple(1, -2, -3)));
1583 EXPECT_EQ("whose field #2 does not match, which is 3 less than 0",
1584 Explain(m, std::make_tuple(1, 2, -3)));
1586 // If they all match, we get a long explanation of success.
1588 "whose all elements match, "
1589 "where field #0 is a value which is 1 more than 0"
1590 ", and field #1 is a value which is 2 more than 0"
1591 ", and field #2 is a value which is 3 more than 0",
1592 Explain(m, std::make_tuple(1, 2, 3)));
1594 // Only print those that have an explanation.
1595 m = FieldsAre(GreaterThan(0), 0, GreaterThan(0));
1597 "whose all elements match, "
1598 "where field #0 is a value which is 1 more than 0"
1599 ", and field #2 is a value which is 3 more than 0",
1600 Explain(m, std::make_tuple(1, 0, 3)));
1602 // If only one has an explanation, then print that one.
1603 m = FieldsAre(0, GreaterThan(0), 0);
1605 "whose all elements match, "
1606 "where field #1 is a value which is 1 more than 0",
1607 Explain(m, std::make_tuple(0, 1, 0)));
1610 #if defined(__cpp_structured_bindings) && __cpp_structured_bindings >= 201606
1611 TEST(FieldsAreTest, StructuredBindings) {
1612 // testing::FieldsAre can also match aggregates and such with C++17 and up.
1617 EXPECT_THAT((MyType{17, "foo"}), FieldsAre(Eq(17), HasSubstr("oo")));
1619 // Test all the supported arities.
1623 EXPECT_THAT(MyVarType1{}, FieldsAre(0));
1627 EXPECT_THAT(MyVarType2{}, FieldsAre(0, 0));
1631 EXPECT_THAT(MyVarType3{}, FieldsAre(0, 0, 0));
1635 EXPECT_THAT(MyVarType4{}, FieldsAre(0, 0, 0, 0));
1639 EXPECT_THAT(MyVarType5{}, FieldsAre(0, 0, 0, 0, 0));
1641 int a, b, c, d, e, f;
1643 EXPECT_THAT(MyVarType6{}, FieldsAre(0, 0, 0, 0, 0, 0));
1645 int a, b, c, d, e, f, g;
1647 EXPECT_THAT(MyVarType7{}, FieldsAre(0, 0, 0, 0, 0, 0, 0));
1649 int a, b, c, d, e, f, g, h;
1651 EXPECT_THAT(MyVarType8{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0));
1653 int a, b, c, d, e, f, g, h, i;
1655 EXPECT_THAT(MyVarType9{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0));
1656 struct MyVarType10 {
1657 int a, b, c, d, e, f, g, h, i, j;
1659 EXPECT_THAT(MyVarType10{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1660 struct MyVarType11 {
1661 int a, b, c, d, e, f, g, h, i, j, k;
1663 EXPECT_THAT(MyVarType11{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1664 struct MyVarType12 {
1665 int a, b, c, d, e, f, g, h, i, j, k, l;
1667 EXPECT_THAT(MyVarType12{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1668 struct MyVarType13 {
1669 int a, b, c, d, e, f, g, h, i, j, k, l, m;
1671 EXPECT_THAT(MyVarType13{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1672 struct MyVarType14 {
1673 int a, b, c, d, e, f, g, h, i, j, k, l, m, n;
1675 EXPECT_THAT(MyVarType14{},
1676 FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1677 struct MyVarType15 {
1678 int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o;
1680 EXPECT_THAT(MyVarType15{},
1681 FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1682 struct MyVarType16 {
1683 int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p;
1685 EXPECT_THAT(MyVarType16{},
1686 FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1690 TEST(PairTest, UseGetInsteadOfMembers) {
1691 PairWithGet pair{7, "ABC"};
1692 EXPECT_THAT(pair, Pair(7, "ABC"));
1693 EXPECT_THAT(pair, Pair(Ge(7), HasSubstr("AB")));
1694 EXPECT_THAT(pair, Not(Pair(Lt(7), "ABC")));
1696 std::vector<PairWithGet> v = {{11, "Foo"}, {29, "gMockIsBestMock"}};
1698 ElementsAre(Pair(11, std::string("Foo")), Pair(Ge(10), Not(""))));
1701 // Tests StartsWith(s).
1703 TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
1704 const Matcher<const char*> m1 = StartsWith(std::string(""));
1705 EXPECT_TRUE(m1.Matches("Hi"));
1706 EXPECT_TRUE(m1.Matches(""));
1707 EXPECT_FALSE(m1.Matches(nullptr));
1709 const Matcher<const std::string&> m2 = StartsWith("Hi");
1710 EXPECT_TRUE(m2.Matches("Hi"));
1711 EXPECT_TRUE(m2.Matches("Hi Hi!"));
1712 EXPECT_TRUE(m2.Matches("High"));
1713 EXPECT_FALSE(m2.Matches("H"));
1714 EXPECT_FALSE(m2.Matches(" Hi"));
1716 #if GTEST_INTERNAL_HAS_STRING_VIEW
1717 const Matcher<internal::StringView> m_empty =
1718 StartsWith(internal::StringView(""));
1719 EXPECT_TRUE(m_empty.Matches(internal::StringView()));
1720 EXPECT_TRUE(m_empty.Matches(internal::StringView("")));
1721 EXPECT_TRUE(m_empty.Matches(internal::StringView("not empty")));
1722 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1725 TEST(StartsWithTest, CanDescribeSelf) {
1726 Matcher<const std::string> m = StartsWith("Hi");
1727 EXPECT_EQ("starts with \"Hi\"", Describe(m));
1730 // Tests EndsWith(s).
1732 TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
1733 const Matcher<const char*> m1 = EndsWith("");
1734 EXPECT_TRUE(m1.Matches("Hi"));
1735 EXPECT_TRUE(m1.Matches(""));
1736 EXPECT_FALSE(m1.Matches(nullptr));
1738 const Matcher<const std::string&> m2 = EndsWith(std::string("Hi"));
1739 EXPECT_TRUE(m2.Matches("Hi"));
1740 EXPECT_TRUE(m2.Matches("Wow Hi Hi"));
1741 EXPECT_TRUE(m2.Matches("Super Hi"));
1742 EXPECT_FALSE(m2.Matches("i"));
1743 EXPECT_FALSE(m2.Matches("Hi "));
1745 #if GTEST_INTERNAL_HAS_STRING_VIEW
1746 const Matcher<const internal::StringView&> m4 =
1747 EndsWith(internal::StringView(""));
1748 EXPECT_TRUE(m4.Matches("Hi"));
1749 EXPECT_TRUE(m4.Matches(""));
1750 EXPECT_TRUE(m4.Matches(internal::StringView()));
1751 EXPECT_TRUE(m4.Matches(internal::StringView("")));
1752 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1755 TEST(EndsWithTest, CanDescribeSelf) {
1756 Matcher<const std::string> m = EndsWith("Hi");
1757 EXPECT_EQ("ends with \"Hi\"", Describe(m));
1760 // Tests WhenBase64Unescaped.
1762 TEST(WhenBase64UnescapedTest, MatchesUnescapedBase64Strings) {
1763 const Matcher<const char*> m1 = WhenBase64Unescaped(EndsWith("!"));
1764 EXPECT_FALSE(m1.Matches("invalid base64"));
1765 EXPECT_FALSE(m1.Matches("aGVsbG8gd29ybGQ=")); // hello world
1766 EXPECT_TRUE(m1.Matches("aGVsbG8gd29ybGQh")); // hello world!
1768 const Matcher<const std::string&> m2 = WhenBase64Unescaped(EndsWith("!"));
1769 EXPECT_FALSE(m2.Matches("invalid base64"));
1770 EXPECT_FALSE(m2.Matches("aGVsbG8gd29ybGQ=")); // hello world
1771 EXPECT_TRUE(m2.Matches("aGVsbG8gd29ybGQh")); // hello world!
1773 #if GTEST_INTERNAL_HAS_STRING_VIEW
1774 const Matcher<const internal::StringView&> m3 =
1775 WhenBase64Unescaped(EndsWith("!"));
1776 EXPECT_FALSE(m3.Matches("invalid base64"));
1777 EXPECT_FALSE(m3.Matches("aGVsbG8gd29ybGQ=")); // hello world
1778 EXPECT_TRUE(m3.Matches("aGVsbG8gd29ybGQh")); // hello world!
1779 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1782 TEST(WhenBase64UnescapedTest, CanDescribeSelf) {
1783 const Matcher<const char*> m = WhenBase64Unescaped(EndsWith("!"));
1784 EXPECT_EQ("matches after Base64Unescape ends with \"!\"", Describe(m));
1787 // Tests MatchesRegex().
1789 TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
1790 const Matcher<const char*> m1 = MatchesRegex("a.*z");
1791 EXPECT_TRUE(m1.Matches("az"));
1792 EXPECT_TRUE(m1.Matches("abcz"));
1793 EXPECT_FALSE(m1.Matches(nullptr));
1795 const Matcher<const std::string&> m2 = MatchesRegex(new RE("a.*z"));
1796 EXPECT_TRUE(m2.Matches("azbz"));
1797 EXPECT_FALSE(m2.Matches("az1"));
1798 EXPECT_FALSE(m2.Matches("1az"));
1800 #if GTEST_INTERNAL_HAS_STRING_VIEW
1801 const Matcher<const internal::StringView&> m3 = MatchesRegex("a.*z");
1802 EXPECT_TRUE(m3.Matches(internal::StringView("az")));
1803 EXPECT_TRUE(m3.Matches(internal::StringView("abcz")));
1804 EXPECT_FALSE(m3.Matches(internal::StringView("1az")));
1805 EXPECT_FALSE(m3.Matches(internal::StringView()));
1806 const Matcher<const internal::StringView&> m4 =
1807 MatchesRegex(internal::StringView(""));
1808 EXPECT_TRUE(m4.Matches(internal::StringView("")));
1809 EXPECT_TRUE(m4.Matches(internal::StringView()));
1810 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1813 TEST(MatchesRegexTest, CanDescribeSelf) {
1814 Matcher<const std::string> m1 = MatchesRegex(std::string("Hi.*"));
1815 EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1));
1817 Matcher<const char*> m2 = MatchesRegex(new RE("a.*"));
1818 EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2));
1820 #if GTEST_INTERNAL_HAS_STRING_VIEW
1821 Matcher<const internal::StringView> m3 = MatchesRegex(new RE("0.*"));
1822 EXPECT_EQ("matches regular expression \"0.*\"", Describe(m3));
1823 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1826 // Tests ContainsRegex().
1828 TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
1829 const Matcher<const char*> m1 = ContainsRegex(std::string("a.*z"));
1830 EXPECT_TRUE(m1.Matches("az"));
1831 EXPECT_TRUE(m1.Matches("0abcz1"));
1832 EXPECT_FALSE(m1.Matches(nullptr));
1834 const Matcher<const std::string&> m2 = ContainsRegex(new RE("a.*z"));
1835 EXPECT_TRUE(m2.Matches("azbz"));
1836 EXPECT_TRUE(m2.Matches("az1"));
1837 EXPECT_FALSE(m2.Matches("1a"));
1839 #if GTEST_INTERNAL_HAS_STRING_VIEW
1840 const Matcher<const internal::StringView&> m3 = ContainsRegex(new RE("a.*z"));
1841 EXPECT_TRUE(m3.Matches(internal::StringView("azbz")));
1842 EXPECT_TRUE(m3.Matches(internal::StringView("az1")));
1843 EXPECT_FALSE(m3.Matches(internal::StringView("1a")));
1844 EXPECT_FALSE(m3.Matches(internal::StringView()));
1845 const Matcher<const internal::StringView&> m4 =
1846 ContainsRegex(internal::StringView(""));
1847 EXPECT_TRUE(m4.Matches(internal::StringView("")));
1848 EXPECT_TRUE(m4.Matches(internal::StringView()));
1849 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1852 TEST(ContainsRegexTest, CanDescribeSelf) {
1853 Matcher<const std::string> m1 = ContainsRegex("Hi.*");
1854 EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1));
1856 Matcher<const char*> m2 = ContainsRegex(new RE("a.*"));
1857 EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2));
1859 #if GTEST_INTERNAL_HAS_STRING_VIEW
1860 Matcher<const internal::StringView> m3 = ContainsRegex(new RE("0.*"));
1861 EXPECT_EQ("contains regular expression \"0.*\"", Describe(m3));
1862 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1865 // Tests for wide strings.
1866 #if GTEST_HAS_STD_WSTRING
1867 TEST(StdWideStrEqTest, MatchesEqual) {
1868 Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello"));
1869 EXPECT_TRUE(m.Matches(L"Hello"));
1870 EXPECT_FALSE(m.Matches(L"hello"));
1871 EXPECT_FALSE(m.Matches(nullptr));
1873 Matcher<const ::std::wstring&> m2 = StrEq(L"Hello");
1874 EXPECT_TRUE(m2.Matches(L"Hello"));
1875 EXPECT_FALSE(m2.Matches(L"Hi"));
1877 Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1878 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1879 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1881 ::std::wstring str(L"01204500800");
1883 Matcher<const ::std::wstring&> m4 = StrEq(str);
1884 EXPECT_TRUE(m4.Matches(str));
1885 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1886 Matcher<const ::std::wstring&> m5 = StrEq(str);
1887 EXPECT_TRUE(m5.Matches(str));
1890 TEST(StdWideStrEqTest, CanDescribeSelf) {
1891 Matcher<::std::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v");
1892 EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
1895 Matcher<::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
1896 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"", Describe(m2));
1898 ::std::wstring str(L"01204500800");
1900 Matcher<const ::std::wstring&> m4 = StrEq(str);
1901 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
1902 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1903 Matcher<const ::std::wstring&> m5 = StrEq(str);
1904 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1907 TEST(StdWideStrNeTest, MatchesUnequalString) {
1908 Matcher<const wchar_t*> m = StrNe(L"Hello");
1909 EXPECT_TRUE(m.Matches(L""));
1910 EXPECT_TRUE(m.Matches(nullptr));
1911 EXPECT_FALSE(m.Matches(L"Hello"));
1913 Matcher<::std::wstring> m2 = StrNe(::std::wstring(L"Hello"));
1914 EXPECT_TRUE(m2.Matches(L"hello"));
1915 EXPECT_FALSE(m2.Matches(L"Hello"));
1918 TEST(StdWideStrNeTest, CanDescribeSelf) {
1919 Matcher<const wchar_t*> m = StrNe(L"Hi");
1920 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
1923 TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1924 Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello"));
1925 EXPECT_TRUE(m.Matches(L"Hello"));
1926 EXPECT_TRUE(m.Matches(L"hello"));
1927 EXPECT_FALSE(m.Matches(L"Hi"));
1928 EXPECT_FALSE(m.Matches(nullptr));
1930 Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello");
1931 EXPECT_TRUE(m2.Matches(L"hello"));
1932 EXPECT_FALSE(m2.Matches(L"Hi"));
1935 TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1936 ::std::wstring str1(L"oabocdooeoo");
1937 ::std::wstring str2(L"OABOCDOOEOO");
1938 Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);
1939 EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0')));
1941 str1[3] = str2[3] = L'\0';
1942 Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);
1943 EXPECT_TRUE(m1.Matches(str2));
1945 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
1946 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
1947 Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);
1948 str1[9] = str2[9] = L'\0';
1949 EXPECT_FALSE(m2.Matches(str2));
1951 Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);
1952 EXPECT_TRUE(m3.Matches(str2));
1954 EXPECT_FALSE(m3.Matches(str2 + L"x"));
1955 str2.append(1, L'\0');
1956 EXPECT_FALSE(m3.Matches(str2));
1957 EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9)));
1960 TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
1961 Matcher<::std::wstring> m = StrCaseEq(L"Hi");
1962 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
1965 TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1966 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
1967 EXPECT_TRUE(m.Matches(L"Hi"));
1968 EXPECT_TRUE(m.Matches(nullptr));
1969 EXPECT_FALSE(m.Matches(L"Hello"));
1970 EXPECT_FALSE(m.Matches(L"hello"));
1972 Matcher<::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello"));
1973 EXPECT_TRUE(m2.Matches(L""));
1974 EXPECT_FALSE(m2.Matches(L"Hello"));
1977 TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
1978 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
1979 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
1982 // Tests that HasSubstr() works for matching wstring-typed values.
1983 TEST(StdWideHasSubstrTest, WorksForStringClasses) {
1984 const Matcher<::std::wstring> m1 = HasSubstr(L"foo");
1985 EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food.")));
1986 EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo")));
1988 const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo");
1989 EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food.")));
1990 EXPECT_FALSE(m2.Matches(::std::wstring(L"tofo")));
1993 // Tests that HasSubstr() works for matching C-wide-string-typed values.
1994 TEST(StdWideHasSubstrTest, WorksForCStrings) {
1995 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
1996 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
1997 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
1998 EXPECT_FALSE(m1.Matches(nullptr));
2000 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
2001 EXPECT_TRUE(m2.Matches(L"I love food."));
2002 EXPECT_FALSE(m2.Matches(L"tofo"));
2003 EXPECT_FALSE(m2.Matches(nullptr));
2006 // Tests that HasSubstr(s) describes itself properly.
2007 TEST(StdWideHasSubstrTest, CanDescribeSelf) {
2008 Matcher<::std::wstring> m = HasSubstr(L"foo\n\"");
2009 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
2012 // Tests StartsWith(s).
2014 TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {
2015 const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L""));
2016 EXPECT_TRUE(m1.Matches(L"Hi"));
2017 EXPECT_TRUE(m1.Matches(L""));
2018 EXPECT_FALSE(m1.Matches(nullptr));
2020 const Matcher<const ::std::wstring&> m2 = StartsWith(L"Hi");
2021 EXPECT_TRUE(m2.Matches(L"Hi"));
2022 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
2023 EXPECT_TRUE(m2.Matches(L"High"));
2024 EXPECT_FALSE(m2.Matches(L"H"));
2025 EXPECT_FALSE(m2.Matches(L" Hi"));
2028 TEST(StdWideStartsWithTest, CanDescribeSelf) {
2029 Matcher<const ::std::wstring> m = StartsWith(L"Hi");
2030 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
2033 // Tests EndsWith(s).
2035 TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {
2036 const Matcher<const wchar_t*> m1 = EndsWith(L"");
2037 EXPECT_TRUE(m1.Matches(L"Hi"));
2038 EXPECT_TRUE(m1.Matches(L""));
2039 EXPECT_FALSE(m1.Matches(nullptr));
2041 const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L"Hi"));
2042 EXPECT_TRUE(m2.Matches(L"Hi"));
2043 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
2044 EXPECT_TRUE(m2.Matches(L"Super Hi"));
2045 EXPECT_FALSE(m2.Matches(L"i"));
2046 EXPECT_FALSE(m2.Matches(L"Hi "));
2049 TEST(StdWideEndsWithTest, CanDescribeSelf) {
2050 Matcher<const ::std::wstring> m = EndsWith(L"Hi");
2051 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
2054 #endif // GTEST_HAS_STD_WSTRING
2056 TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {
2057 StringMatchResultListener listener1;
2058 EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1));
2059 EXPECT_EQ("% 2 == 0", listener1.str());
2061 StringMatchResultListener listener2;
2062 EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2));
2063 EXPECT_EQ("", listener2.str());
2066 TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {
2067 const Matcher<int> is_even = PolymorphicIsEven();
2068 StringMatchResultListener listener1;
2069 EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1));
2070 EXPECT_EQ("% 2 == 0", listener1.str());
2072 const Matcher<const double&> is_zero = Eq(0);
2073 StringMatchResultListener listener2;
2074 EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2));
2075 EXPECT_EQ("", listener2.str());
2078 MATCHER(ConstructNoArg, "") { return true; }
2079 MATCHER_P(Construct1Arg, arg1, "") { return true; }
2080 MATCHER_P2(Construct2Args, arg1, arg2, "") { return true; }
2082 TEST(MatcherConstruct, ExplicitVsImplicit) {
2084 // No arg constructor can be constructed with empty brace.
2085 ConstructNoArgMatcher m = {};
2088 ConstructNoArgMatcher m2;
2092 // The one arg constructor has an explicit constructor.
2093 // This is to prevent the implicit conversion.
2094 using M = Construct1ArgMatcherP<int>;
2095 EXPECT_TRUE((std::is_constructible<M, int>::value));
2096 EXPECT_FALSE((std::is_convertible<int, M>::value));
2099 // Multiple arg matchers can be constructed with an implicit construction.
2100 Construct2ArgsMatcherP2<int, double> m = {1, 2.2};
2105 MATCHER_P(Really, inner_matcher, "") {
2106 return ExplainMatchResult(inner_matcher, arg, result_listener);
2109 TEST(ExplainMatchResultTest, WorksInsideMATCHER) {
2110 EXPECT_THAT(0, Really(Eq(0)));
2113 TEST(DescribeMatcherTest, WorksWithValue) {
2114 EXPECT_EQ("is equal to 42", DescribeMatcher<int>(42));
2115 EXPECT_EQ("isn't equal to 42", DescribeMatcher<int>(42, true));
2118 TEST(DescribeMatcherTest, WorksWithMonomorphicMatcher) {
2119 const Matcher<int> monomorphic = Le(0);
2120 EXPECT_EQ("is <= 0", DescribeMatcher<int>(monomorphic));
2121 EXPECT_EQ("isn't <= 0", DescribeMatcher<int>(monomorphic, true));
2124 TEST(DescribeMatcherTest, WorksWithPolymorphicMatcher) {
2125 EXPECT_EQ("is even", DescribeMatcher<int>(PolymorphicIsEven()));
2126 EXPECT_EQ("is odd", DescribeMatcher<int>(PolymorphicIsEven(), true));
2129 MATCHER_P(FieldIIs, inner_matcher, "") {
2130 return ExplainMatchResult(inner_matcher, arg.i, result_listener);
2134 TEST(WhenDynamicCastToTest, SameType) {
2138 // Right type. A pointer is passed down.
2139 Base* as_base_ptr = &derived;
2140 EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(Not(IsNull())));
2141 EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(Pointee(FieldIIs(4))));
2142 EXPECT_THAT(as_base_ptr,
2143 Not(WhenDynamicCastTo<Derived*>(Pointee(FieldIIs(5)))));
2146 TEST(WhenDynamicCastToTest, WrongTypes) {
2149 OtherDerived other_derived;
2151 // Wrong types. NULL is passed.
2152 EXPECT_THAT(&base, Not(WhenDynamicCastTo<Derived*>(Pointee(_))));
2153 EXPECT_THAT(&base, WhenDynamicCastTo<Derived*>(IsNull()));
2154 Base* as_base_ptr = &derived;
2155 EXPECT_THAT(as_base_ptr, Not(WhenDynamicCastTo<OtherDerived*>(Pointee(_))));
2156 EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<OtherDerived*>(IsNull()));
2157 as_base_ptr = &other_derived;
2158 EXPECT_THAT(as_base_ptr, Not(WhenDynamicCastTo<Derived*>(Pointee(_))));
2159 EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(IsNull()));
2162 TEST(WhenDynamicCastToTest, AlreadyNull) {
2164 Base* as_base_ptr = nullptr;
2165 EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(IsNull()));
2168 struct AmbiguousCastTypes {
2169 class VirtualDerived : public virtual Base {};
2170 class DerivedSub1 : public VirtualDerived {};
2171 class DerivedSub2 : public VirtualDerived {};
2172 class ManyDerivedInHierarchy : public DerivedSub1, public DerivedSub2 {};
2175 TEST(WhenDynamicCastToTest, AmbiguousCast) {
2176 AmbiguousCastTypes::DerivedSub1 sub1;
2177 AmbiguousCastTypes::ManyDerivedInHierarchy many_derived;
2178 // Multiply derived from Base. dynamic_cast<> returns NULL.
2180 static_cast<AmbiguousCastTypes::DerivedSub1*>(&many_derived);
2181 EXPECT_THAT(as_base_ptr,
2182 WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(IsNull()));
2183 as_base_ptr = &sub1;
2186 WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(Not(IsNull())));
2189 TEST(WhenDynamicCastToTest, Describe) {
2190 Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(Pointee(_));
2191 const std::string prefix =
2192 "when dynamic_cast to " + internal::GetTypeName<Derived*>() + ", ";
2193 EXPECT_EQ(prefix + "points to a value that is anything", Describe(matcher));
2194 EXPECT_EQ(prefix + "does not point to a value that is anything",
2195 DescribeNegation(matcher));
2198 TEST(WhenDynamicCastToTest, Explain) {
2199 Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(Pointee(_));
2200 Base* null = nullptr;
2201 EXPECT_THAT(Explain(matcher, null), HasSubstr("NULL"));
2203 EXPECT_TRUE(matcher.Matches(&derived));
2204 EXPECT_THAT(Explain(matcher, &derived), HasSubstr("which points to "));
2206 // With references, the matcher itself can fail. Test for that one.
2207 Matcher<const Base&> ref_matcher = WhenDynamicCastTo<const OtherDerived&>(_);
2208 EXPECT_THAT(Explain(ref_matcher, derived),
2209 HasSubstr("which cannot be dynamic_cast"));
2212 TEST(WhenDynamicCastToTest, GoodReference) {
2215 Base& as_base_ref = derived;
2216 EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(FieldIIs(4)));
2217 EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(Not(FieldIIs(5))));
2220 TEST(WhenDynamicCastToTest, BadReference) {
2222 Base& as_base_ref = derived;
2223 EXPECT_THAT(as_base_ref, Not(WhenDynamicCastTo<const OtherDerived&>(_)));
2225 #endif // GTEST_HAS_RTTI
2227 class DivisibleByImpl {
2229 explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {}
2231 // For testing using ExplainMatchResultTo() with polymorphic matchers.
2232 template <typename T>
2233 bool MatchAndExplain(const T& n, MatchResultListener* listener) const {
2234 *listener << "which is " << (n % divider_) << " modulo " << divider_;
2235 return (n % divider_) == 0;
2238 void DescribeTo(ostream* os) const { *os << "is divisible by " << divider_; }
2240 void DescribeNegationTo(ostream* os) const {
2241 *os << "is not divisible by " << divider_;
2244 void set_divider(int a_divider) { divider_ = a_divider; }
2245 int divider() const { return divider_; }
2251 PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) {
2252 return MakePolymorphicMatcher(DivisibleByImpl(n));
2255 // Tests that when AllOf() fails, only the first failing matcher is
2256 // asked to explain why.
2257 TEST(ExplainMatchResultTest, AllOf_False_False) {
2258 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
2259 EXPECT_EQ("which is 1 modulo 4", Explain(m, 5));
2262 // Tests that when AllOf() fails, only the first failing matcher is
2263 // asked to explain why.
2264 TEST(ExplainMatchResultTest, AllOf_False_True) {
2265 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
2266 EXPECT_EQ("which is 2 modulo 4", Explain(m, 6));
2269 // Tests that when AllOf() fails, only the first failing matcher is
2270 // asked to explain why.
2271 TEST(ExplainMatchResultTest, AllOf_True_False) {
2272 const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
2273 EXPECT_EQ("which is 2 modulo 3", Explain(m, 5));
2276 // Tests that when AllOf() succeeds, all matchers are asked to explain
2278 TEST(ExplainMatchResultTest, AllOf_True_True) {
2279 const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));
2280 EXPECT_EQ("which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6));
2283 TEST(ExplainMatchResultTest, AllOf_True_True_2) {
2284 const Matcher<int> m = AllOf(Ge(2), Le(3));
2285 EXPECT_EQ("", Explain(m, 2));
2288 INSTANTIATE_GTEST_MATCHER_TEST_P(ExplainmatcherResultTest);
2290 TEST_P(ExplainmatcherResultTestP, MonomorphicMatcher) {
2291 const Matcher<int> m = GreaterThan(5);
2292 EXPECT_EQ("which is 1 more than 5", Explain(m, 6));
2295 // Tests PolymorphicMatcher::mutable_impl().
2296 TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
2297 PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
2298 DivisibleByImpl& impl = m.mutable_impl();
2299 EXPECT_EQ(42, impl.divider());
2301 impl.set_divider(0);
2302 EXPECT_EQ(0, m.mutable_impl().divider());
2305 // Tests PolymorphicMatcher::impl().
2306 TEST(PolymorphicMatcherTest, CanAccessImpl) {
2307 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
2308 const DivisibleByImpl& impl = m.impl();
2309 EXPECT_EQ(42, impl.divider());
2313 } // namespace gmock_matchers_test
2314 } // namespace testing
2317 #pragma warning(pop)