38 # pragma warning(push)
39 # pragma warning(disable:4244)
40 # pragma warning(disable:4100)
43 #include "gmock/gmock-matchers.h"
44 #include "gmock/gmock-more-matchers.h"
49 #include <forward_list>
60 #include <type_traits>
63 #include "gmock/gmock.h"
64 #include "gtest/gtest.h"
65 #include "gtest/gtest-spi.h"
68 namespace gmock_matchers_test {
81 using std::stringstream;
99 struct ContainerHelper {
103 std::vector<std::unique_ptr<int>> MakeUniquePtrs(
const std::vector<int>& ints) {
104 std::vector<std::unique_ptr<int>> pointers;
105 for (
int i : ints) pointers.emplace_back(
new int(
i));
112 explicit GreaterThanMatcher(
int rhs) :
rhs_(rhs) {}
114 void DescribeTo(ostream* os)
const override { *os <<
"is > " <<
rhs_; }
119 *listener <<
"which is " <<
diff <<
" more than " <<
rhs_;
120 }
else if (
diff == 0) {
121 *listener <<
"which is the same as " <<
rhs_;
123 *listener <<
"which is " << -
diff <<
" less than " <<
rhs_;
133 Matcher<int> GreaterThan(
int n) {
146 template <
typename T>
148 return DescribeMatcher<T>(
m);
152 template <
typename T>
154 return DescribeMatcher<T>(
m,
true);
158 template <
typename MatcherType,
typename Value>
160 StringMatchResultListener listener;
162 return listener.str();
165 TEST(MonotonicMatcherTest, IsPrintable) {
167 ss << GreaterThan(5);
171 TEST(MatchResultListenerTest, StreamingWorks) {
172 StringMatchResultListener listener;
173 listener <<
"hi" << 5;
183 DummyMatchResultListener
dummy;
187 TEST(MatchResultListenerTest, CanAccessUnderlyingStream) {
194 TEST(MatchResultListenerTest, IsInterestedWorks) {
195 EXPECT_TRUE(StringMatchResultListener().IsInterested());
196 EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested());
198 EXPECT_FALSE(DummyMatchResultListener().IsInterested());
199 EXPECT_FALSE(StreamMatchResultListener(
nullptr).IsInterested());
204 class EvenMatcherImpl :
public MatcherInterface<int> {
206 bool MatchAndExplain(
int x,
207 MatchResultListener* )
const override {
211 void DescribeTo(ostream* os)
const override { *os <<
"is an even number"; }
219 TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) {
225 class NewEvenMatcherImpl :
public MatcherInterface<int> {
227 bool MatchAndExplain(
int x, MatchResultListener* listener)
const override {
228 const bool match =
x % 2 == 0;
230 *listener <<
"value % " << 2;
231 if (listener->stream() !=
nullptr) {
234 *listener->stream() <<
" == " << (
x % 2);
239 void DescribeTo(ostream* os)
const override { *os <<
"is an even number"; }
242 TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
251 TEST(MatcherTest, CanBeDefaultConstructed) {
256 TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {
257 const MatcherInterface<int>* impl =
new EvenMatcherImpl;
258 Matcher<int>
m(impl);
264 TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) {
271 TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) {
272 Matcher<int*> m1 =
nullptr;
281 virtual ~Undefined() = 0;
285 TEST(MatcherTest, CanBeConstructedFromUndefinedVariable) {
292 TEST(MatcherTest, CanAcceptAbstractClass) { Matcher<const Undefined&>
m =
_; }
295 TEST(MatcherTest, IsCopyable) {
297 Matcher<bool> m1 =
Eq(
false);
309 TEST(MatcherTest, CanDescribeItself) {
311 Describe(Matcher<int>(
new EvenMatcherImpl)));
315 TEST(MatcherTest, MatchAndExplain) {
316 Matcher<int>
m = GreaterThan(0);
317 StringMatchResultListener listener1;
319 EXPECT_EQ(
"which is 42 more than 0", listener1.str());
321 StringMatchResultListener listener2;
323 EXPECT_EQ(
"which is 9 less than 0", listener2.str());
328 TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
329 Matcher<std::string> m1 =
"hi";
333 Matcher<const std::string&> m2 =
"hi";
340 TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
345 Matcher<const std::string&> m2 =
std::string(
"hi");
353 TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
354 Matcher<absl::string_view> m1 =
"cats";
358 Matcher<const absl::string_view&> m2 =
"cats";
365 TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromString) {
366 Matcher<absl::string_view> m1 =
std::string(
"cats");
370 Matcher<const absl::string_view&> m2 =
std::string(
"cats");
377 TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromStringView) {
386 #endif // GTEST_HAS_ABSL
390 TEST(StringMatcherTest,
391 CanBeImplicitlyConstructedFromEqReferenceWrapperString) {
405 TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {
406 const MatcherInterface<int>* dummy_impl =
nullptr;
413 class ReferencesBarOrIsZeroImpl {
415 template <
typename T>
416 bool MatchAndExplain(
const T& x,
417 MatchResultListener* )
const {
419 return p == &g_bar ||
x == 0;
422 void DescribeTo(ostream* os)
const { *os <<
"g_bar or zero"; }
424 void DescribeNegationTo(ostream* os)
const {
425 *os <<
"doesn't reference g_bar and is not zero";
431 PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {
435 TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {
437 Matcher<const int&> m1 = ReferencesBarOrIsZero();
442 EXPECT_EQ(
"g_bar or zero", Describe(m1));
445 Matcher<double> m2 = ReferencesBarOrIsZero();
448 EXPECT_EQ(
"g_bar or zero", Describe(m2));
453 class PolymorphicIsEvenImpl {
455 void DescribeTo(ostream* os)
const { *os <<
"is even"; }
457 void DescribeNegationTo(ostream* os)
const {
461 template <
typename T>
462 bool MatchAndExplain(
const T& x, MatchResultListener* listener)
const {
464 *listener <<
"% " << 2;
465 if (listener->stream() !=
nullptr) {
468 *listener->stream() <<
" == " << (
x % 2);
474 PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {
478 TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
480 const Matcher<int> m1 = PolymorphicIsEven();
485 const Matcher<int> not_m1 =
Not(m1);
491 const Matcher<char> m2 = PolymorphicIsEven();
496 const Matcher<char> not_m2 =
Not(m2);
499 EXPECT_EQ(
"% 2 == 0", Explain(m2,
'\x42'));
503 TEST(MatcherCastTest, FromPolymorphicMatcher) {
504 Matcher<int>
m = MatcherCast<int>(
Eq(5));
514 explicit IntValue(
int a_value) :
value_(a_value) {}
522 bool IsPositiveIntValue(
const IntValue&
foo) {
523 return foo.value() > 0;
528 TEST(MatcherCastTest, FromCompatibleType) {
529 Matcher<double> m1 =
Eq(2.0);
530 Matcher<int> m2 = MatcherCast<int>(m1);
534 Matcher<IntValue> m3 =
Truly(IsPositiveIntValue);
535 Matcher<int> m4 = MatcherCast<int>(m3);
544 TEST(MatcherCastTest, FromConstReferenceToNonReference) {
545 Matcher<const int&> m1 =
Eq(0);
546 Matcher<int> m2 = MatcherCast<int>(m1);
552 TEST(MatcherCastTest, FromReferenceToNonReference) {
553 Matcher<int&> m1 =
Eq(0);
554 Matcher<int> m2 = MatcherCast<int>(m1);
560 TEST(MatcherCastTest, FromNonReferenceToConstReference) {
561 Matcher<int> m1 =
Eq(0);
562 Matcher<const int&> m2 = MatcherCast<const int&>(m1);
568 TEST(MatcherCastTest, FromNonReferenceToReference) {
569 Matcher<int> m1 =
Eq(0);
570 Matcher<int&> m2 = MatcherCast<int&>(m1);
578 TEST(MatcherCastTest, FromSameType) {
579 Matcher<int> m1 =
Eq(0);
580 Matcher<int> m2 = MatcherCast<int>(m1);
587 TEST(MatcherCastTest, FromAValue) {
588 Matcher<int>
m = MatcherCast<int>(42);
595 TEST(MatcherCastTest, FromAnImplicitlyConvertibleValue) {
596 const int kExpected =
'c';
597 Matcher<int>
m = MatcherCast<int>(
'c');
602 struct NonImplicitlyConstructibleTypeWithOperatorEq {
604 const NonImplicitlyConstructibleTypeWithOperatorEq& ,
610 const NonImplicitlyConstructibleTypeWithOperatorEq& ) {
618 TEST(MatcherCastTest, NonImplicitlyConstructibleTypeWithOperatorEq) {
619 Matcher<NonImplicitlyConstructibleTypeWithOperatorEq> m1 =
620 MatcherCast<NonImplicitlyConstructibleTypeWithOperatorEq>(42);
621 EXPECT_TRUE(m1.Matches(NonImplicitlyConstructibleTypeWithOperatorEq()));
623 Matcher<NonImplicitlyConstructibleTypeWithOperatorEq> m2 =
624 MatcherCast<NonImplicitlyConstructibleTypeWithOperatorEq>(239);
625 EXPECT_FALSE(m2.Matches(NonImplicitlyConstructibleTypeWithOperatorEq()));
630 MatcherCast<int>(NonImplicitlyConstructibleTypeWithOperatorEq());
640 #if !defined _MSC_VER
649 namespace convertible_from_any {
651 struct ConvertibleFromAny {
652 ConvertibleFromAny(
int a_value) :
value(a_value) {}
653 template <
typename T>
654 ConvertibleFromAny(
const T& ) :
value(-1) {
660 bool operator==(
const ConvertibleFromAny& a,
const ConvertibleFromAny&
b) {
661 return a.value ==
b.value;
664 ostream&
operator<<(ostream& os,
const ConvertibleFromAny& a) {
665 return os <<
a.value;
668 TEST(MatcherCastTest, ConversionConstructorIsUsed) {
669 Matcher<ConvertibleFromAny>
m = MatcherCast<ConvertibleFromAny>(1);
674 TEST(MatcherCastTest, FromConvertibleFromAny) {
675 Matcher<ConvertibleFromAny>
m =
676 MatcherCast<ConvertibleFromAny>(
Eq(ConvertibleFromAny(1)));
682 #endif // !defined _MSC_VER
684 struct IntReferenceWrapper {
685 IntReferenceWrapper(
const int& a_value) :
value(&a_value) {}
689 bool operator==(
const IntReferenceWrapper& a,
const IntReferenceWrapper&
b) {
690 return a.value ==
b.value;
693 TEST(MatcherCastTest, ValueIsNotCopied) {
695 Matcher<IntReferenceWrapper>
m = MatcherCast<IntReferenceWrapper>(n);
708 class Derived :
public Base {
710 Derived() : Base() {}
714 class OtherDerived :
public Base {};
717 TEST(SafeMatcherCastTest, FromPolymorphicMatcher) {
718 Matcher<char> m2 = SafeMatcherCast<char>(
Eq(32));
726 TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {
728 Matcher<float> m2 = SafeMatcherCast<float>(m1);
732 Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>(
'a'));
739 TEST(SafeMatcherCastTest, FromBaseClass) {
741 Matcher<Base*> m1 =
Eq(&d);
742 Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);
746 Matcher<Base&> m3 =
Ref(d);
747 Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);
753 TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
755 Matcher<const int&> m1 =
Ref(n);
756 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
763 TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {
764 Matcher<int> m1 =
Eq(0);
765 Matcher<const int&> m2 = SafeMatcherCast<const int&>(m1);
771 TEST(SafeMatcherCastTest, FromNonReferenceToReference) {
772 Matcher<int> m1 =
Eq(0);
773 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
781 TEST(SafeMatcherCastTest, FromSameType) {
782 Matcher<int> m1 =
Eq(0);
783 Matcher<int> m2 = SafeMatcherCast<int>(m1);
788 #if !defined _MSC_VER
790 namespace convertible_from_any {
791 TEST(SafeMatcherCastTest, ConversionConstructorIsUsed) {
792 Matcher<ConvertibleFromAny>
m = SafeMatcherCast<ConvertibleFromAny>(1);
797 TEST(SafeMatcherCastTest, FromConvertibleFromAny) {
798 Matcher<ConvertibleFromAny>
m =
799 SafeMatcherCast<ConvertibleFromAny>(
Eq(ConvertibleFromAny(1)));
805 #endif // !defined _MSC_VER
807 TEST(SafeMatcherCastTest, ValueIsNotCopied) {
809 Matcher<IntReferenceWrapper>
m = SafeMatcherCast<IntReferenceWrapper>(n);
814 TEST(ExpectThat, TakesLiterals) {
820 TEST(ExpectThat, TakesFunctions) {
822 static void Func() {}
830 TEST(ATest, MatchesAnyValue) {
844 TEST(ATest, WorksForDerivedClass) {
854 TEST(ATest, CanDescribeSelf) {
859 TEST(AnTest, MatchesAnyValue) {
861 Matcher<int> m1 = An<int>();
868 Matcher<int&> m2 = An<int&>();
874 TEST(AnTest, CanDescribeSelf) {
875 EXPECT_EQ(
"is anything", Describe(An<int>()));
880 TEST(UnderscoreTest, MatchesAnyValue) {
889 Matcher<const bool&> m2 =
_;
895 TEST(UnderscoreTest, CanDescribeSelf) {
901 TEST(EqTest, MatchesEqualValue) {
903 const char a1[] =
"hi";
904 const char a2[] =
"hi";
906 Matcher<const char*> m1 =
Eq(
a1);
915 Unprintable() :
c_(
'a') {}
917 bool operator==(
const Unprintable& )
const {
return true; }
919 char dummy_c() {
return c_; }
924 TEST(EqTest, CanDescribeSelf) {
925 Matcher<Unprintable>
m =
Eq(Unprintable());
926 EXPECT_EQ(
"is equal to 1-byte object <61>", Describe(
m));
931 TEST(EqTest, IsPolymorphic) {
932 Matcher<int> m1 =
Eq(1);
936 Matcher<char> m2 =
Eq(1);
942 TEST(TypedEqTest, ChecksEqualityForGivenType) {
943 Matcher<char> m1 = TypedEq<char>(
'a');
947 Matcher<int> m2 = TypedEq<int>(6);
953 TEST(TypedEqTest, CanDescribeSelf) {
954 EXPECT_EQ(
"is equal to 2", Describe(TypedEq<int>(2)));
963 template <
typename T>
965 static bool IsTypeOf(
const T& ) {
return true; }
967 template <
typename T2>
968 static void IsTypeOf(
T2 v);
971 TEST(TypedEqTest, HasSpecifiedType) {
978 TEST(GeTest, ImplementsGreaterThanOrEqual) {
979 Matcher<int> m1 =
Ge(0);
986 TEST(GeTest, CanDescribeSelf) {
987 Matcher<int>
m =
Ge(5);
992 TEST(GtTest, ImplementsGreaterThan) {
993 Matcher<double> m1 =
Gt(0);
1000 TEST(GtTest, CanDescribeSelf) {
1001 Matcher<int>
m =
Gt(5);
1006 TEST(LeTest, ImplementsLessThanOrEqual) {
1007 Matcher<char> m1 =
Le(
'b');
1014 TEST(LeTest, CanDescribeSelf) {
1015 Matcher<int>
m =
Le(5);
1020 TEST(LtTest, ImplementsLessThan) {
1021 Matcher<const std::string&> m1 =
Lt(
"Hello");
1028 TEST(LtTest, CanDescribeSelf) {
1029 Matcher<int>
m =
Lt(5);
1034 TEST(NeTest, ImplementsNotEqual) {
1035 Matcher<int> m1 =
Ne(0);
1042 TEST(NeTest, CanDescribeSelf) {
1043 Matcher<int>
m =
Ne(5);
1049 explicit MoveOnly(
int i) :
i_(
i) {}
1050 MoveOnly(
const MoveOnly&) =
delete;
1051 MoveOnly(MoveOnly&&) =
default;
1052 MoveOnly& operator=(
const MoveOnly&) =
delete;
1053 MoveOnly& operator=(MoveOnly&&) =
default;
1055 bool operator==(
const MoveOnly& other)
const {
return i_ == other.i_; }
1056 bool operator!=(
const MoveOnly& other)
const {
return i_ != other.i_; }
1057 bool operator<(
const MoveOnly& other)
const {
return i_ < other.i_; }
1058 bool operator<=(
const MoveOnly& other)
const {
return i_ <= other.i_; }
1059 bool operator>(
const MoveOnly& other)
const {
return i_ > other.i_; }
1060 bool operator>=(
const MoveOnly& other)
const {
return i_ >= other.i_; }
1070 TEST(ComparisonBaseTest, WorksWithMoveOnly) {
1075 helper.Call(MoveOnly(0));
1077 helper.Call(MoveOnly(1));
1079 helper.Call(MoveOnly(0));
1081 helper.Call(MoveOnly(-1));
1083 helper.Call(MoveOnly(0));
1085 helper.Call(MoveOnly(1));
1089 TEST(IsNullTest, MatchesNullPointer) {
1090 Matcher<int*> m1 =
IsNull();
1096 Matcher<const char*> m2 =
IsNull();
1097 const char* p2 =
nullptr;
1101 Matcher<void*> m3 =
IsNull();
1104 EXPECT_FALSE(m3.Matches(
reinterpret_cast<void*
>(0xbeef)));
1107 TEST(IsNullTest, StdFunction) {
1115 TEST(IsNullTest, CanDescribeSelf) {
1118 EXPECT_EQ(
"isn't NULL", DescribeNegation(
m));
1122 TEST(NotNullTest, MatchesNonNullPointer) {
1129 Matcher<const char*> m2 =
NotNull();
1130 const char* p2 =
nullptr;
1135 TEST(NotNullTest, LinkedPtr) {
1136 const Matcher<std::shared_ptr<int>>
m =
NotNull();
1137 const std::shared_ptr<int> null_p;
1138 const std::shared_ptr<int> non_null_p(
new int);
1144 TEST(NotNullTest, ReferenceToConstLinkedPtr) {
1145 const Matcher<const std::shared_ptr<double>&>
m =
NotNull();
1146 const std::shared_ptr<double> null_p;
1147 const std::shared_ptr<double> non_null_p(
new double);
1153 TEST(NotNullTest, StdFunction) {
1161 TEST(NotNullTest, CanDescribeSelf) {
1168 TEST(RefTest, MatchesSameVariable) {
1171 Matcher<int&>
m =
Ref(a);
1177 TEST(RefTest, CanDescribeSelf) {
1179 Matcher<int&>
m =
Ref(n);
1181 ss <<
"references the variable @" << &
n <<
" 5";
1187 TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
1190 Matcher<const int&>
m =
Ref(a);
1199 TEST(RefTest, IsCovariant) {
1202 Matcher<const Base&> m1 =
Ref(
base);
1213 TEST(RefTest, ExplainsResult) {
1225 TEST(StrEqTest, MatchesEqualString) {
1231 Matcher<const std::string&> m2 =
StrEq(
"Hello");
1236 Matcher<const absl::string_view&> m3 =
StrEq(
"Hello");
1241 Matcher<const absl::string_view&> m_empty =
StrEq(
"");
1245 #endif // GTEST_HAS_ABSL
1248 TEST(StrEqTest, CanDescribeSelf) {
1249 Matcher<std::string>
m =
StrEq(
"Hi-\'\"?\\\a\b\f\n\r\t\v\xD3");
1250 EXPECT_EQ(
"is equal to \"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
1255 Matcher<std::string> m2 =
StrEq(
str);
1256 EXPECT_EQ(
"is equal to \"012\\04500800\"", Describe(m2));
1258 Matcher<std::string> m3 =
StrEq(
str);
1259 EXPECT_EQ(
"is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));
1262 TEST(StrNeTest, MatchesUnequalString) {
1263 Matcher<const char*>
m =
StrNe(
"Hello");
1273 Matcher<const absl::string_view> m3 =
StrNe(
"Hello");
1277 #endif // GTEST_HAS_ABSL
1280 TEST(StrNeTest, CanDescribeSelf) {
1281 Matcher<const char*>
m =
StrNe(
"Hi");
1282 EXPECT_EQ(
"isn't equal to \"Hi\"", Describe(
m));
1285 TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
1292 Matcher<const std::string&> m2 =
StrCaseEq(
"Hello");
1302 #endif // GTEST_HAS_ABSL
1305 TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1308 Matcher<const std::string&> m0 =
StrCaseEq(str1);
1311 str1[3] = str2[3] =
'\0';
1312 Matcher<const std::string&> m1 =
StrCaseEq(str1);
1315 str1[0] = str1[6] = str1[7] = str1[10] =
'\0';
1316 str2[0] = str2[6] = str2[7] = str2[10] =
'\0';
1317 Matcher<const std::string&> m2 =
StrCaseEq(str1);
1318 str1[9] = str2[9] =
'\0';
1321 Matcher<const std::string&> m3 =
StrCaseEq(str1);
1325 str2.append(1,
'\0');
1330 TEST(StrCaseEqTest, CanDescribeSelf) {
1332 EXPECT_EQ(
"is equal to (ignoring case) \"Hi\"", Describe(
m));
1335 TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1347 Matcher<const absl::string_view> m3 =
StrCaseNe(
"Hello");
1352 #endif // GTEST_HAS_ABSL
1355 TEST(StrCaseNeTest, CanDescribeSelf) {
1357 EXPECT_EQ(
"isn't equal to (ignoring case) \"Hi\"", Describe(
m));
1361 TEST(HasSubstrTest, WorksForStringClasses) {
1362 const Matcher<std::string> m1 =
HasSubstr(
"foo");
1366 const Matcher<const std::string&> m2 =
HasSubstr(
"foo");
1370 const Matcher<std::string> m_empty =
HasSubstr(
"");
1376 TEST(HasSubstrTest, WorksForCStrings) {
1377 const Matcher<char*> m1 =
HasSubstr(
"foo");
1378 EXPECT_TRUE(m1.Matches(
const_cast<char*
>(
"I love food.")));
1382 const Matcher<const char*> m2 =
HasSubstr(
"foo");
1387 const Matcher<const char*> m_empty =
HasSubstr(
"");
1395 TEST(HasSubstrTest, WorksForStringViewClasses) {
1396 const Matcher<absl::string_view> m1 =
HasSubstr(
"foo");
1401 const Matcher<const absl::string_view&> m2 =
HasSubstr(
"foo");
1406 const Matcher<const absl::string_view&> m3 =
HasSubstr(
"");
1411 #endif // GTEST_HAS_ABSL
1414 TEST(HasSubstrTest, CanDescribeSelf) {
1415 Matcher<std::string>
m =
HasSubstr(
"foo\n\"");
1416 EXPECT_EQ(
"has substring \"foo\\n\\\"\"", Describe(
m));
1419 TEST(KeyTest, CanDescribeSelf) {
1420 Matcher<const pair<std::string, int>&>
m =
Key(
"foo");
1421 EXPECT_EQ(
"has a key that is equal to \"foo\"", Describe(
m));
1422 EXPECT_EQ(
"doesn't have a key that is equal to \"foo\"", DescribeNegation(
m));
1425 TEST(KeyTest, ExplainsResult) {
1426 Matcher<pair<int, bool> >
m =
Key(GreaterThan(10));
1427 EXPECT_EQ(
"whose first field is a value which is 5 less than 10",
1428 Explain(
m, make_pair(5,
true)));
1429 EXPECT_EQ(
"whose first field is a value which is 5 more than 10",
1430 Explain(
m, make_pair(15,
true)));
1433 TEST(KeyTest, MatchesCorrectly) {
1434 pair<int, std::string>
p(25,
"foo");
1441 TEST(KeyTest, WorksWithMoveOnly) {
1442 pair<std::unique_ptr<int>, std::unique_ptr<int>>
p;
1449 struct PairWithGet {
1452 using first_type =
int;
1455 const int& GetImpl(Tag<0>)
const {
return member_1; }
1459 auto get(
const PairWithGet&
value) -> decltype(
value.GetImpl(Tag<I>())) {
1460 return value.GetImpl(Tag<I>());
1462 TEST(PairTest, MatchesPairWithGetCorrectly) {
1463 PairWithGet
p{25,
"foo"};
1469 std::vector<PairWithGet>
v = {{11,
"Foo"}, {29,
"gMockIsBestMock"}};
1473 TEST(KeyTest, SafelyCastsInnerMatcher) {
1474 Matcher<int> is_positive =
Gt(0);
1475 Matcher<int> is_negative =
Lt(0);
1476 pair<char, bool>
p(
'a',
true);
1481 TEST(KeyTest, InsideContainsUsingMap) {
1490 TEST(KeyTest, InsideContainsUsingMultimap) {
1506 TEST(PairTest, Typing) {
1508 Matcher<const pair<const char*, int>&> m1 =
Pair(
"foo", 42);
1509 Matcher<const pair<const char*, int> > m2 =
Pair(
"foo", 42);
1510 Matcher<pair<const char*, int> > m3 =
Pair(
"foo", 42);
1512 Matcher<pair<int, const std::string> > m4 =
Pair(25,
"42");
1513 Matcher<pair<const std::string, int> > m5 =
Pair(
"25", 42);
1516 TEST(PairTest, CanDescribeSelf) {
1517 Matcher<const pair<std::string, int>&> m1 =
Pair(
"foo", 42);
1518 EXPECT_EQ(
"has a first field that is equal to \"foo\""
1519 ", and has a second field that is equal to 42",
1521 EXPECT_EQ(
"has a first field that isn't equal to \"foo\""
1522 ", or has a second field that isn't equal to 42",
1523 DescribeNegation(m1));
1525 Matcher<const pair<int, int>&> m2 =
Not(
Pair(
Not(13), 42));
1526 EXPECT_EQ(
"has a first field that isn't equal to 13"
1527 ", and has a second field that is equal to 42",
1528 DescribeNegation(m2));
1531 TEST(PairTest, CanExplainMatchResultTo) {
1534 const Matcher<pair<int, int> >
m =
Pair(GreaterThan(0), GreaterThan(0));
1535 EXPECT_EQ(
"whose first field does not match, which is 1 less than 0",
1536 Explain(
m, make_pair(-1, -2)));
1540 EXPECT_EQ(
"whose second field does not match, which is 2 less than 0",
1541 Explain(
m, make_pair(1, -2)));
1545 EXPECT_EQ(
"whose first field does not match, which is 1 less than 0",
1546 Explain(
m, make_pair(-1, 2)));
1549 EXPECT_EQ(
"whose both fields match, where the first field is a value "
1550 "which is 1 more than 0, and the second field is a value "
1551 "which is 2 more than 0",
1552 Explain(
m, make_pair(1, 2)));
1556 const Matcher<pair<int, int> > explain_first =
Pair(GreaterThan(0), 0);
1557 EXPECT_EQ(
"whose both fields match, where the first field is a value "
1558 "which is 1 more than 0",
1559 Explain(explain_first, make_pair(1, 0)));
1563 const Matcher<pair<int, int> > explain_second =
Pair(0, GreaterThan(0));
1564 EXPECT_EQ(
"whose both fields match, where the second field is a value "
1565 "which is 1 more than 0",
1566 Explain(explain_second, make_pair(0, 1)));
1569 TEST(PairTest, MatchesCorrectly) {
1570 pair<int, std::string>
p(25,
"foo");
1589 TEST(PairTest, WorksWithMoveOnly) {
1590 pair<std::unique_ptr<int>, std::unique_ptr<int>>
p;
1591 p.second.reset(
new int(7));
1595 TEST(PairTest, SafelyCastsInnerMatchers) {
1596 Matcher<int> is_positive =
Gt(0);
1597 Matcher<int> is_negative =
Lt(0);
1598 pair<char, bool>
p(
'a',
true);
1605 TEST(PairTest, InsideContainsUsingMap) {
1616 TEST(ContainsTest, WorksWithMoveOnly) {
1617 ContainerHelper helper;
1619 helper.Call(MakeUniquePtrs({1, 2}));
1622 TEST(PairTest, UseGetInsteadOfMembers) {
1623 PairWithGet
pair{7,
"ABC"};
1628 std::vector<PairWithGet>
v = {{11,
"Foo"}, {29,
"gMockIsBestMock"}};
1635 TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
1641 const Matcher<const std::string&> m2 =
StartsWith(
"Hi");
1649 const Matcher<absl::string_view> m_empty =
StartsWith(
"");
1653 #endif // GTEST_HAS_ABSL
1656 TEST(StartsWithTest, CanDescribeSelf) {
1658 EXPECT_EQ(
"starts with \"Hi\"", Describe(
m));
1663 TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
1664 const Matcher<const char*> m1 =
EndsWith(
"");
1677 const Matcher<const absl::string_view&> m4 =
EndsWith(
"");
1682 #endif // GTEST_HAS_ABSL
1685 TEST(EndsWithTest, CanDescribeSelf) {
1686 Matcher<const std::string>
m =
EndsWith(
"Hi");
1692 TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
1698 const Matcher<const std::string&> m2 =
MatchesRegex(
new RE(
"a.*z"));
1704 const Matcher<const absl::string_view&> m3 =
MatchesRegex(
"a.*z");
1709 const Matcher<const absl::string_view&> m4 =
MatchesRegex(
"");
1712 #endif // GTEST_HAS_ABSL
1715 TEST(MatchesRegexTest, CanDescribeSelf) {
1717 EXPECT_EQ(
"matches regular expression \"Hi.*\"", Describe(m1));
1720 EXPECT_EQ(
"matches regular expression \"a.*\"", Describe(m2));
1723 Matcher<const absl::string_view> m3 =
MatchesRegex(
new RE(
"0.*"));
1724 EXPECT_EQ(
"matches regular expression \"0.*\"", Describe(m3));
1725 #endif // GTEST_HAS_ABSL
1730 TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
1736 const Matcher<const std::string&> m2 =
ContainsRegex(
new RE(
"a.*z"));
1742 const Matcher<const absl::string_view&> m3 =
ContainsRegex(
new RE(
"a.*z"));
1747 const Matcher<const absl::string_view&> m4 =
ContainsRegex(
"");
1750 #endif // GTEST_HAS_ABSL
1753 TEST(ContainsRegexTest, CanDescribeSelf) {
1755 EXPECT_EQ(
"contains regular expression \"Hi.*\"", Describe(m1));
1758 EXPECT_EQ(
"contains regular expression \"a.*\"", Describe(m2));
1761 Matcher<const absl::string_view> m3 =
ContainsRegex(
new RE(
"0.*"));
1762 EXPECT_EQ(
"contains regular expression \"0.*\"", Describe(m3));
1763 #endif // GTEST_HAS_ABSL
1767 #if GTEST_HAS_STD_WSTRING
1768 TEST(StdWideStrEqTest, MatchesEqual) {
1774 Matcher<const ::std::wstring&> m2 =
StrEq(L
"Hello");
1778 Matcher<const ::std::wstring&> m3 =
StrEq(L
"\xD3\x576\x8D3\xC74D");
1784 Matcher<const ::std::wstring&> m4 =
StrEq(
str);
1787 Matcher<const ::std::wstring&> m5 =
StrEq(
str);
1791 TEST(StdWideStrEqTest, CanDescribeSelf) {
1792 Matcher< ::std::wstring>
m =
StrEq(L
"Hi-\'\"?\\\a\b\f\n\r\t\v");
1793 EXPECT_EQ(
"is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
1796 Matcher< ::std::wstring> m2 =
StrEq(L
"\xD3\x576\x8D3\xC74D");
1797 EXPECT_EQ(
"is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1802 Matcher<const ::std::wstring&> m4 =
StrEq(
str);
1803 EXPECT_EQ(
"is equal to L\"012\\04500800\"", Describe(m4));
1805 Matcher<const ::std::wstring&> m5 =
StrEq(
str);
1806 EXPECT_EQ(
"is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1809 TEST(StdWideStrNeTest, MatchesUnequalString) {
1810 Matcher<const wchar_t*>
m =
StrNe(L
"Hello");
1820 TEST(StdWideStrNeTest, CanDescribeSelf) {
1821 Matcher<const wchar_t*>
m =
StrNe(L
"Hi");
1822 EXPECT_EQ(
"isn't equal to L\"Hi\"", Describe(
m));
1825 TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1832 Matcher<const ::std::wstring&> m2 =
StrCaseEq(L
"Hello");
1837 TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1840 Matcher<const ::std::wstring&> m0 =
StrCaseEq(str1);
1843 str1[3] = str2[3] =
L'\0';
1844 Matcher<const ::std::wstring&> m1 =
StrCaseEq(str1);
1847 str1[0] = str1[6] = str1[7] = str1[10] =
L'\0';
1848 str2[0] = str2[6] = str2[7] = str2[10] =
L'\0';
1849 Matcher<const ::std::wstring&> m2 =
StrCaseEq(str1);
1850 str1[9] = str2[9] =
L'\0';
1853 Matcher<const ::std::wstring&> m3 =
StrCaseEq(str1);
1857 str2.append(1, L
'\0');
1862 TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
1863 Matcher< ::std::wstring>
m =
StrCaseEq(L
"Hi");
1864 EXPECT_EQ(
"is equal to (ignoring case) L\"Hi\"", Describe(
m));
1867 TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1868 Matcher<const wchar_t*>
m =
StrCaseNe(L
"Hello");
1879 TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
1880 Matcher<const wchar_t*>
m =
StrCaseNe(L
"Hi");
1881 EXPECT_EQ(
"isn't equal to (ignoring case) L\"Hi\"", Describe(
m));
1885 TEST(StdWideHasSubstrTest, WorksForStringClasses) {
1886 const Matcher< ::std::wstring> m1 =
HasSubstr(L
"foo");
1890 const Matcher<const ::std::wstring&> m2 =
HasSubstr(L
"foo");
1896 TEST(StdWideHasSubstrTest, WorksForCStrings) {
1897 const Matcher<wchar_t*> m1 =
HasSubstr(L
"foo");
1898 EXPECT_TRUE(m1.Matches(
const_cast<wchar_t*
>(L
"I love food.")));
1899 EXPECT_FALSE(m1.Matches(
const_cast<wchar_t*
>(L
"tofo")));
1902 const Matcher<const wchar_t*> m2 =
HasSubstr(L
"foo");
1909 TEST(StdWideHasSubstrTest, CanDescribeSelf) {
1910 Matcher< ::std::wstring>
m =
HasSubstr(L
"foo\n\"");
1911 EXPECT_EQ(
"has substring L\"foo\\n\\\"\"", Describe(
m));
1916 TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {
1922 const Matcher<const ::std::wstring&> m2 =
StartsWith(L
"Hi");
1930 TEST(StdWideStartsWithTest, CanDescribeSelf) {
1931 Matcher<const ::std::wstring>
m =
StartsWith(L
"Hi");
1932 EXPECT_EQ(
"starts with L\"Hi\"", Describe(
m));
1937 TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {
1938 const Matcher<const wchar_t*> m1 =
EndsWith(L
"");
1951 TEST(StdWideEndsWithTest, CanDescribeSelf) {
1952 Matcher<const ::std::wstring>
m =
EndsWith(L
"Hi");
1956 #endif // GTEST_HAS_STD_WSTRING
1958 typedef ::std::tuple<long, int> Tuple2;
1962 TEST(Eq2Test, MatchesEqualArguments) {
1963 Matcher<const Tuple2&>
m =
Eq();
1969 TEST(Eq2Test, CanDescribeSelf) {
1970 Matcher<const Tuple2&>
m =
Eq();
1976 TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) {
1977 Matcher<const Tuple2&>
m =
Ge();
1984 TEST(Ge2Test, CanDescribeSelf) {
1985 Matcher<const Tuple2&>
m =
Ge();
1986 EXPECT_EQ(
"are a pair where the first >= the second", Describe(
m));
1991 TEST(Gt2Test, MatchesGreaterThanArguments) {
1992 Matcher<const Tuple2&>
m =
Gt();
1999 TEST(Gt2Test, CanDescribeSelf) {
2000 Matcher<const Tuple2&>
m =
Gt();
2001 EXPECT_EQ(
"are a pair where the first > the second", Describe(
m));
2006 TEST(Le2Test, MatchesLessThanOrEqualArguments) {
2007 Matcher<const Tuple2&>
m =
Le();
2014 TEST(Le2Test, CanDescribeSelf) {
2015 Matcher<const Tuple2&>
m =
Le();
2016 EXPECT_EQ(
"are a pair where the first <= the second", Describe(
m));
2021 TEST(Lt2Test, MatchesLessThanArguments) {
2022 Matcher<const Tuple2&>
m =
Lt();
2029 TEST(Lt2Test, CanDescribeSelf) {
2030 Matcher<const Tuple2&>
m =
Lt();
2031 EXPECT_EQ(
"are a pair where the first < the second", Describe(
m));
2036 TEST(Ne2Test, MatchesUnequalArguments) {
2037 Matcher<const Tuple2&>
m =
Ne();
2044 TEST(Ne2Test, CanDescribeSelf) {
2045 Matcher<const Tuple2&>
m =
Ne();
2046 EXPECT_EQ(
"are an unequal pair", Describe(
m));
2049 TEST(PairMatchBaseTest, WorksWithMoveOnly) {
2050 using Pointers = std::tuple<std::unique_ptr<int>, std::unique_ptr<int>>;
2051 Matcher<Pointers> matcher =
Eq();
2060 TEST(FloatEq2Test, MatchesEqualArguments) {
2061 typedef ::std::tuple<float, float> Tpl;
2069 TEST(FloatEq2Test, CanDescribeSelf) {
2070 Matcher<const ::std::tuple<float, float>&>
m =
FloatEq();
2071 EXPECT_EQ(
"are an almost-equal pair", Describe(
m));
2076 TEST(NanSensitiveFloatEqTest, MatchesEqualArgumentsWithNaN) {
2077 typedef ::std::tuple<float, float> Tpl;
2080 EXPECT_TRUE(
m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(),
2081 std::numeric_limits<float>::quiet_NaN())));
2083 EXPECT_FALSE(
m.Matches(Tpl(1.0f, std::numeric_limits<float>::quiet_NaN())));
2084 EXPECT_FALSE(
m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), 1.0f)));
2088 TEST(NanSensitiveFloatEqTest, CanDescribeSelfWithNaNs) {
2090 EXPECT_EQ(
"are an almost-equal pair", Describe(
m));
2095 TEST(DoubleEq2Test, MatchesEqualArguments) {
2096 typedef ::std::tuple<double, double> Tpl;
2104 TEST(DoubleEq2Test, CanDescribeSelf) {
2105 Matcher<const ::std::tuple<double, double>&>
m =
DoubleEq();
2106 EXPECT_EQ(
"are an almost-equal pair", Describe(
m));
2111 TEST(NanSensitiveDoubleEqTest, MatchesEqualArgumentsWithNaN) {
2112 typedef ::std::tuple<double, double> Tpl;
2115 EXPECT_TRUE(
m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(),
2116 std::numeric_limits<double>::quiet_NaN())));
2118 EXPECT_FALSE(
m.Matches(Tpl(1.0f, std::numeric_limits<double>::quiet_NaN())));
2119 EXPECT_FALSE(
m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), 1.0f)));
2123 TEST(NanSensitiveDoubleEqTest, CanDescribeSelfWithNaNs) {
2125 EXPECT_EQ(
"are an almost-equal pair", Describe(
m));
2130 TEST(FloatNear2Test, MatchesEqualArguments) {
2131 typedef ::std::tuple<float, float> Tpl;
2139 TEST(FloatNear2Test, CanDescribeSelf) {
2140 Matcher<const ::std::tuple<float, float>&>
m =
FloatNear(0.5f);
2141 EXPECT_EQ(
"are an almost-equal pair", Describe(
m));
2146 TEST(NanSensitiveFloatNearTest, MatchesNearbyArgumentsWithNaN) {
2147 typedef ::std::tuple<float, float> Tpl;
2151 EXPECT_TRUE(
m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(),
2152 std::numeric_limits<float>::quiet_NaN())));
2154 EXPECT_FALSE(
m.Matches(Tpl(1.0f, std::numeric_limits<float>::quiet_NaN())));
2155 EXPECT_FALSE(
m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), 1.0f)));
2159 TEST(NanSensitiveFloatNearTest, CanDescribeSelfWithNaNs) {
2161 EXPECT_EQ(
"are an almost-equal pair", Describe(
m));
2166 TEST(DoubleNear2Test, MatchesEqualArguments) {
2167 typedef ::std::tuple<double, double> Tpl;
2175 TEST(DoubleNear2Test, CanDescribeSelf) {
2176 Matcher<const ::std::tuple<double, double>&>
m =
DoubleNear(0.5);
2177 EXPECT_EQ(
"are an almost-equal pair", Describe(
m));
2182 TEST(NanSensitiveDoubleNearTest, MatchesNearbyArgumentsWithNaN) {
2183 typedef ::std::tuple<double, double> Tpl;
2187 EXPECT_TRUE(
m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(),
2188 std::numeric_limits<double>::quiet_NaN())));
2190 EXPECT_FALSE(
m.Matches(Tpl(1.0f, std::numeric_limits<double>::quiet_NaN())));
2191 EXPECT_FALSE(
m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), 1.0f)));
2195 TEST(NanSensitiveDoubleNearTest, CanDescribeSelfWithNaNs) {
2197 EXPECT_EQ(
"are an almost-equal pair", Describe(
m));
2201 TEST(NotTest, NegatesMatcher) {
2209 TEST(NotTest, CanDescribeSelf) {
2210 Matcher<int>
m =
Not(
Eq(5));
2215 TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
2217 Matcher<int> greater_than_5 =
Gt(5);
2219 Matcher<const int&>
m =
Not(greater_than_5);
2220 Matcher<int&> m2 =
Not(greater_than_5);
2221 Matcher<int&> m3 =
Not(
m);
2225 void AllOfMatches(
int num,
const Matcher<int>&
m) {
2228 for (
int i = 1;
i <=
num; ++
i) {
2236 TEST(AllOfTest, MatchesWhenAllMatch) {
2279 50,
AllOf(
Ne(1),
Ne(2),
Ne(3),
Ne(4),
Ne(5),
Ne(6),
Ne(7),
Ne(8),
Ne(9),
2290 TEST(AllOfTest, CanDescribeSelf) {
2293 EXPECT_EQ(
"(is <= 2) and (is >= 1)", Describe(
m));
2297 "(is > 0) and (isn't equal to 1) and (isn't equal to 2)";
2302 "(is > 0) and (isn't equal to 1) and (isn't equal to 2) and (isn't equal "
2308 "(is >= 0) and (is < 10) and (isn't equal to 3) and (isn't equal to 5) "
2309 "and (isn't equal to 7)";
2314 TEST(AllOfTest, CanDescribeNegation) {
2317 std::string expected_descr4 =
"(isn't <= 2) or (isn't >= 1)";
2318 EXPECT_EQ(expected_descr4, DescribeNegation(
m));
2322 "(isn't > 0) or (is equal to 1) or (is equal to 2)";
2323 EXPECT_EQ(expected_descr5, DescribeNegation(
m));
2327 "(isn't > 0) or (is equal to 1) or (is equal to 2) or (is equal to 3)";
2328 EXPECT_EQ(expected_descr6, DescribeNegation(
m));
2332 "(isn't >= 0) or (isn't < 10) or (is equal to 3) or (is equal to 5) or "
2334 EXPECT_EQ(expected_desr7, DescribeNegation(
m));
2338 AllOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
2340 AllOfMatches(11,
m);
2344 TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
2346 Matcher<int> greater_than_5 =
Gt(5);
2347 Matcher<int> less_than_10 =
Lt(10);
2349 Matcher<const int&>
m =
AllOf(greater_than_5, less_than_10);
2350 Matcher<int&> m2 =
AllOf(greater_than_5, less_than_10);
2351 Matcher<int&> m3 =
AllOf(greater_than_5, m2);
2354 Matcher<const int&> m4 =
AllOf(greater_than_5, less_than_10, less_than_10);
2355 Matcher<int&> m5 =
AllOf(greater_than_5, less_than_10, less_than_10);
2358 TEST(AllOfTest, ExplainsResult) {
2364 m =
AllOf(GreaterThan(10),
Lt(30));
2365 EXPECT_EQ(
"which is 15 more than 10", Explain(
m, 25));
2368 m =
AllOf(GreaterThan(10), GreaterThan(20));
2369 EXPECT_EQ(
"which is 20 more than 10, and which is 10 more than 20",
2374 m =
AllOf(GreaterThan(10),
Lt(30), GreaterThan(20));
2375 EXPECT_EQ(
"which is 15 more than 10, and which is 5 more than 20",
2379 m =
AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2380 EXPECT_EQ(
"which is 30 more than 10, and which is 20 more than 20, "
2381 "and which is 10 more than 30",
2386 m =
AllOf(GreaterThan(10), GreaterThan(20));
2387 EXPECT_EQ(
"which is 5 less than 10", Explain(
m, 5));
2392 m =
AllOf(GreaterThan(10),
Lt(30));
2397 m =
AllOf(GreaterThan(10), GreaterThan(20));
2398 EXPECT_EQ(
"which is 5 less than 20", Explain(
m, 15));
2402 static void AnyOfMatches(
int num,
const Matcher<int>&
m) {
2405 for (
int i = 1;
i <=
num; ++
i) {
2411 static void AnyOfStringMatches(
int num,
const Matcher<std::string>&
m) {
2415 for (
int i = 1;
i <=
num; ++
i) {
2423 TEST(AnyOfTest, MatchesWhenAnyMatches) {
2453 AnyOfMatches(2,
AnyOf(1, 2));
2454 AnyOfMatches(3,
AnyOf(1, 2, 3));
2455 AnyOfMatches(4,
AnyOf(1, 2, 3, 4));
2456 AnyOfMatches(5,
AnyOf(1, 2, 3, 4, 5));
2457 AnyOfMatches(6,
AnyOf(1, 2, 3, 4, 5, 6));
2458 AnyOfMatches(7,
AnyOf(1, 2, 3, 4, 5, 6, 7));
2459 AnyOfMatches(8,
AnyOf(1, 2, 3, 4, 5, 6, 7, 8));
2460 AnyOfMatches(9,
AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9));
2461 AnyOfMatches(10,
AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
2465 TEST(AnyOfTest, VariadicMatchesWhenAnyMatches) {
2468 Matcher<int>
m =
::testing::AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
2471 AnyOfMatches(11,
m);
2472 AnyOfMatches(50,
AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
2473 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
2474 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
2475 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
2476 41, 42, 43, 44, 45, 46, 47, 48, 49, 50));
2478 50,
AnyOf(
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
2479 "13",
"14",
"15",
"16",
"17",
"18",
"19",
"20",
"21",
"22",
2480 "23",
"24",
"25",
"26",
"27",
"28",
"29",
"30",
"31",
"32",
2481 "33",
"34",
"35",
"36",
"37",
"38",
"39",
"40",
"41",
"42",
2482 "43",
"44",
"45",
"46",
"47",
"48",
"49",
"50"));
2486 TEST(ElementsAreTest, HugeMatcher) {
2487 vector<int>
test_vector{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
2495 TEST(ElementsAreTest, HugeMatcherStr) {
2497 "literal_string",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
""};
2504 TEST(ElementsAreTest, HugeMatcherUnordered) {
2505 vector<int>
test_vector{2, 1, 8, 5, 4, 6, 7, 3, 9, 12, 11, 10};
2514 TEST(AnyOfTest, CanDescribeSelf) {
2522 EXPECT_EQ(
"(is < 0) or (is equal to 1) or (is equal to 2)", Describe(
m));
2525 EXPECT_EQ(
"(is < 0) or (is equal to 1) or (is equal to 2) or (is equal to 3)",
2530 "(is <= 0) or (is > 10) or (is equal to 3) or (is equal to 5) or (is "
2536 TEST(AnyOfTest, CanDescribeNegation) {
2539 EXPECT_EQ(
"(isn't <= 1) and (isn't >= 3)",
2540 DescribeNegation(
m));
2543 EXPECT_EQ(
"(isn't < 0) and (isn't equal to 1) and (isn't equal to 2)",
2544 DescribeNegation(
m));
2548 "(isn't < 0) and (isn't equal to 1) and (isn't equal to 2) and (isn't "
2550 DescribeNegation(
m));
2554 "(isn't <= 0) and (isn't > 10) and (isn't equal to 3) and (isn't equal "
2555 "to 5) and (isn't equal to 7)",
2556 DescribeNegation(
m));
2560 TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
2562 Matcher<int> greater_than_5 =
Gt(5);
2563 Matcher<int> less_than_10 =
Lt(10);
2565 Matcher<const int&>
m =
AnyOf(greater_than_5, less_than_10);
2566 Matcher<int&> m2 =
AnyOf(greater_than_5, less_than_10);
2567 Matcher<int&> m3 =
AnyOf(greater_than_5, m2);
2570 Matcher<const int&> m4 =
AnyOf(greater_than_5, less_than_10, less_than_10);
2571 Matcher<int&> m5 =
AnyOf(greater_than_5, less_than_10, less_than_10);
2574 TEST(AnyOfTest, ExplainsResult) {
2581 EXPECT_EQ(
"which is 5 less than 10", Explain(
m, 5));
2584 m =
AnyOf(GreaterThan(10), GreaterThan(20));
2585 EXPECT_EQ(
"which is 5 less than 10, and which is 15 less than 20",
2590 m =
AnyOf(GreaterThan(10),
Gt(20), GreaterThan(30));
2591 EXPECT_EQ(
"which is 5 less than 10, and which is 25 less than 30",
2595 m =
AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2596 EXPECT_EQ(
"which is 5 less than 10, and which is 15 less than 20, "
2597 "and which is 25 less than 30",
2602 m =
AnyOf(GreaterThan(10), GreaterThan(20));
2603 EXPECT_EQ(
"which is 5 more than 10", Explain(
m, 15));
2608 m =
AnyOf(GreaterThan(10),
Lt(30));
2613 m =
AnyOf(GreaterThan(30), GreaterThan(20));
2614 EXPECT_EQ(
"which is 5 more than 20", Explain(
m, 25));
2624 int IsPositive(
double x) {
2625 return x > 0 ? 1 : 0;
2630 class IsGreaterThan {
2632 explicit IsGreaterThan(
int threshold) :
threshold_(threshold) {}
2634 bool operator()(
int n)
const {
return n >
threshold_; }
2645 bool ReferencesFooAndIsZero(
const int& n) {
2646 return (&
n == &
foo) && (
n == 0);
2651 TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
2652 Matcher<double>
m =
Truly(IsPositive);
2658 TEST(TrulyTest, CanBeUsedWithFunctor) {
2659 Matcher<int>
m =
Truly(IsGreaterThan(5));
2665 class ConvertibleToBool {
2674 ConvertibleToBool IsNotZero(
int number) {
2675 return ConvertibleToBool(
number);
2681 TEST(TrulyTest, PredicateCanReturnAClassConvertibleToBool) {
2682 Matcher<int>
m =
Truly(IsNotZero);
2688 TEST(TrulyTest, CanDescribeSelf) {
2689 Matcher<double>
m =
Truly(IsPositive);
2690 EXPECT_EQ(
"satisfies the given predicate",
2696 TEST(TrulyTest, WorksForByRefArguments) {
2697 Matcher<const int&>
m =
Truly(ReferencesFooAndIsZero);
2705 TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
2712 TEST(MatchesTest, WorksOnByRefArguments) {
2720 TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
2721 Matcher<int> eq5 =
Eq(5);
2729 TEST(ValueTest, WorksWithPolymorphicMatcher) {
2734 TEST(ValueTest, WorksWithMonomorphicMatcher) {
2735 const Matcher<int> is_zero =
Eq(0);
2740 const Matcher<const int&> ref_n =
Ref(n);
2745 TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {
2746 StringMatchResultListener listener1;
2750 StringMatchResultListener listener2;
2755 TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {
2756 const Matcher<int> is_even = PolymorphicIsEven();
2757 StringMatchResultListener listener1;
2761 const Matcher<const double&> is_zero =
Eq(0);
2762 StringMatchResultListener listener2;
2771 TEST(ExplainMatchResultTest, WorksInsideMATCHER) {
2775 TEST(DescribeMatcherTest, WorksWithValue) {
2776 EXPECT_EQ(
"is equal to 42", DescribeMatcher<int>(42));
2777 EXPECT_EQ(
"isn't equal to 42", DescribeMatcher<int>(42,
true));
2780 TEST(DescribeMatcherTest, WorksWithMonomorphicMatcher) {
2781 const Matcher<int> monomorphic =
Le(0);
2782 EXPECT_EQ(
"is <= 0", DescribeMatcher<int>(monomorphic));
2783 EXPECT_EQ(
"isn't <= 0", DescribeMatcher<int>(monomorphic,
true));
2786 TEST(DescribeMatcherTest, WorksWithPolymorphicMatcher) {
2787 EXPECT_EQ(
"is even", DescribeMatcher<int>(PolymorphicIsEven()));
2788 EXPECT_EQ(
"is odd", DescribeMatcher<int>(PolymorphicIsEven(),
true));
2791 TEST(AllArgsTest, WorksForTuple) {
2796 TEST(AllArgsTest, WorksForNonTuple) {
2801 class AllArgsHelper {
2811 TEST(AllArgsTest, WorksInWithClause) {
2812 AllArgsHelper helper;
2815 .WillByDefault(
Return(1));
2825 class OptionalMatchersHelper {
2827 OptionalMatchersHelper() {}
2842 TEST(AllArgsTest, WorksWithoutMatchers) {
2843 OptionalMatchersHelper helper;
2865 TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
2874 TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
2877 static unsigned short n;
2887 "Expected: is > 10\n"
2888 " Actual: 5" + OfType(
"unsigned short"));
2893 "Expected: (is <= 7) and (is >= 5)\n"
2894 " Actual: 0" + OfType(
"unsigned short"));
2899 TEST(MatcherAssertionTest, WorksForByRefArguments) {
2907 "Expected: does not reference the variable @");
2910 "Actual: 0" + OfType(
"int") +
", which is located @");
2915 TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
2916 Matcher<const char*> starts_with_he =
StartsWith(
"he");
2919 Matcher<const std::string&> ends_with_ok =
EndsWith(
"ok");
2924 "Expected: ends with \"ok\"\n"
2925 " Actual: \"bad\"");
2926 Matcher<int> is_greater_than_5 =
Gt(5);
2929 "Expected: is > 5\n"
2930 " Actual: 5" + OfType(
"int"));
2934 template <
typename RawType>
2959 nan1_(Floating::ReinterpretBits(Floating::kExponentBitMask | 1)),
2960 nan2_(Floating::ReinterpretBits(Floating::kExponentBitMask | 200)) {
2964 EXPECT_EQ(
sizeof(RawType),
sizeof(Bits));
2971 Matcher<RawType> m1 = matcher_maker(0.0);
2980 Matcher<RawType> m3 = matcher_maker(1.0);
2987 Matcher<RawType> m4 = matcher_maker(-
infinity_);
2990 Matcher<RawType> m5 = matcher_maker(
infinity_);
2999 Matcher<const RawType&> m6 = matcher_maker(0.0);
3006 Matcher<RawType&> m7 = matcher_maker(0.0);
3044 template <
typename RawType>
3045 class FloatingPointNearTest :
public FloatingPointTest<RawType> {
3047 typedef FloatingPointTest<RawType> ParentType;
3051 void TestNearMatches(
3053 (*matcher_maker)(RawType, RawType)) {
3054 Matcher<RawType> m1 = matcher_maker(0.0, 0.0);
3061 Matcher<RawType> m2 = matcher_maker(0.0, 1.0);
3100 Matcher<RawType> m9 = matcher_maker(
3106 Matcher<const RawType&> m10 = matcher_maker(0.0, 1.0);
3113 Matcher<RawType&> m11 = matcher_maker(0.0, 1.0);
3128 typedef FloatingPointTest<float> FloatTest;
3130 TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) {
3134 TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
3138 TEST_F(FloatTest, FloatEqCannotMatchNaN) {
3146 TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
3154 TEST_F(FloatTest, FloatEqCanDescribeSelf) {
3155 Matcher<float> m1 =
FloatEq(2.0f);
3156 EXPECT_EQ(
"is approximately 2", Describe(m1));
3157 EXPECT_EQ(
"isn't approximately 2", DescribeNegation(m1));
3159 Matcher<float> m2 =
FloatEq(0.5f);
3160 EXPECT_EQ(
"is approximately 0.5", Describe(m2));
3161 EXPECT_EQ(
"isn't approximately 0.5", DescribeNegation(m2));
3164 EXPECT_EQ(
"never matches", Describe(m3));
3165 EXPECT_EQ(
"is anything", DescribeNegation(m3));
3168 TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
3170 EXPECT_EQ(
"is approximately 2", Describe(m1));
3171 EXPECT_EQ(
"isn't approximately 2", DescribeNegation(m1));
3174 EXPECT_EQ(
"is approximately 0.5", Describe(m2));
3175 EXPECT_EQ(
"isn't approximately 0.5", DescribeNegation(m2));
3179 EXPECT_EQ(
"isn't NaN", DescribeNegation(m3));
3184 typedef FloatingPointNearTest<float> FloatNearTest;
3186 TEST_F(FloatNearTest, FloatNearMatches) {
3190 TEST_F(FloatNearTest, NanSensitiveFloatNearApproximatelyMatchesFloats) {
3194 TEST_F(FloatNearTest, FloatNearCanDescribeSelf) {
3195 Matcher<float> m1 =
FloatNear(2.0f, 0.5f);
3196 EXPECT_EQ(
"is approximately 2 (absolute error <= 0.5)", Describe(m1));
3198 "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
3200 Matcher<float> m2 =
FloatNear(0.5f, 0.5f);
3201 EXPECT_EQ(
"is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
3203 "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
3206 EXPECT_EQ(
"never matches", Describe(m3));
3207 EXPECT_EQ(
"is anything", DescribeNegation(m3));
3210 TEST_F(FloatNearTest, NanSensitiveFloatNearCanDescribeSelf) {
3212 EXPECT_EQ(
"is approximately 2 (absolute error <= 0.5)", Describe(m1));
3214 "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
3217 EXPECT_EQ(
"is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
3219 "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
3223 EXPECT_EQ(
"isn't NaN", DescribeNegation(m3));
3226 TEST_F(FloatNearTest, FloatNearCannotMatchNaN) {
3234 TEST_F(FloatNearTest, NanSensitiveFloatNearCanMatchNaN) {
3243 typedef FloatingPointTest<double> DoubleTest;
3245 TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
3249 TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
3253 TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
3261 TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
3269 TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
3270 Matcher<double> m1 =
DoubleEq(2.0);
3271 EXPECT_EQ(
"is approximately 2", Describe(m1));
3272 EXPECT_EQ(
"isn't approximately 2", DescribeNegation(m1));
3274 Matcher<double> m2 =
DoubleEq(0.5);
3275 EXPECT_EQ(
"is approximately 0.5", Describe(m2));
3276 EXPECT_EQ(
"isn't approximately 0.5", DescribeNegation(m2));
3279 EXPECT_EQ(
"never matches", Describe(m3));
3280 EXPECT_EQ(
"is anything", DescribeNegation(m3));
3283 TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
3285 EXPECT_EQ(
"is approximately 2", Describe(m1));
3286 EXPECT_EQ(
"isn't approximately 2", DescribeNegation(m1));
3289 EXPECT_EQ(
"is approximately 0.5", Describe(m2));
3290 EXPECT_EQ(
"isn't approximately 0.5", DescribeNegation(m2));
3294 EXPECT_EQ(
"isn't NaN", DescribeNegation(m3));
3299 typedef FloatingPointNearTest<double> DoubleNearTest;
3301 TEST_F(DoubleNearTest, DoubleNearMatches) {
3305 TEST_F(DoubleNearTest, NanSensitiveDoubleNearApproximatelyMatchesDoubles) {
3309 TEST_F(DoubleNearTest, DoubleNearCanDescribeSelf) {
3311 EXPECT_EQ(
"is approximately 2 (absolute error <= 0.5)", Describe(m1));
3313 "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
3316 EXPECT_EQ(
"is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
3318 "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
3321 EXPECT_EQ(
"never matches", Describe(m3));
3322 EXPECT_EQ(
"is anything", DescribeNegation(m3));
3325 TEST_F(DoubleNearTest, ExplainsResultWhenMatchFails) {
3331 Explain(
DoubleNear(2.1, 1e-10), 2.1 + 1.2e-10);
3334 EXPECT_TRUE(explanation ==
"which is 1.2e-10 from 2.1" ||
3335 explanation ==
"which is 1.2e-010 from 2.1")
3336 <<
" where explanation is \"" << explanation <<
"\".";
3339 TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanDescribeSelf) {
3341 EXPECT_EQ(
"is approximately 2 (absolute error <= 0.5)", Describe(m1));
3343 "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
3346 EXPECT_EQ(
"is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
3348 "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
3352 EXPECT_EQ(
"isn't NaN", DescribeNegation(m3));
3355 TEST_F(DoubleNearTest, DoubleNearCannotMatchNaN) {
3363 TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanMatchNaN) {
3371 TEST(PointeeTest, RawPointer) {
3381 TEST(PointeeTest, RawPointerToConst) {
3382 const Matcher<const double*>
m =
Pointee(
Ge(0));
3391 TEST(PointeeTest, ReferenceToConstRawPointer) {
3392 const Matcher<int* const &>
m =
Pointee(
Ge(0));
3401 TEST(PointeeTest, ReferenceToNonConstRawPointer) {
3413 MATCHER_P(FieldIIs, inner_matcher,
"") {
3418 TEST(WhenDynamicCastToTest, SameType) {
3423 Base* as_base_ptr = &derived;
3427 Not(WhenDynamicCastTo<Derived*>(
Pointee(FieldIIs(5)))));
3430 TEST(WhenDynamicCastToTest, WrongTypes) {
3433 OtherDerived other_derived;
3438 Base* as_base_ptr = &derived;
3441 as_base_ptr = &other_derived;
3446 TEST(WhenDynamicCastToTest, AlreadyNull) {
3448 Base* as_base_ptr =
nullptr;
3452 struct AmbiguousCastTypes {
3453 class VirtualDerived :
public virtual Base {};
3454 class DerivedSub1 :
public VirtualDerived {};
3455 class DerivedSub2 :
public VirtualDerived {};
3456 class ManyDerivedInHierarchy :
public DerivedSub1,
public DerivedSub2 {};
3459 TEST(WhenDynamicCastToTest, AmbiguousCast) {
3460 AmbiguousCastTypes::DerivedSub1 sub1;
3461 AmbiguousCastTypes::ManyDerivedInHierarchy many_derived;
3464 static_cast<AmbiguousCastTypes::DerivedSub1*
>(&many_derived);
3466 WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(
IsNull()));
3467 as_base_ptr = &sub1;
3470 WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(
Not(
IsNull())));
3473 TEST(WhenDynamicCastToTest, Describe) {
3474 Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(
Pointee(
_));
3476 "when dynamic_cast to " + internal::GetTypeName<Derived*>() +
", ";
3477 EXPECT_EQ(
prefix +
"points to a value that is anything", Describe(matcher));
3479 DescribeNegation(matcher));
3482 TEST(WhenDynamicCastToTest, Explain) {
3483 Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(
Pointee(
_));
3484 Base*
null =
nullptr;
3491 Matcher<const Base&> ref_matcher = WhenDynamicCastTo<const OtherDerived&>(
_);
3493 HasSubstr(
"which cannot be dynamic_cast"));
3496 TEST(WhenDynamicCastToTest, GoodReference) {
3499 Base& as_base_ref = derived;
3500 EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(FieldIIs(4)));
3501 EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(
Not(FieldIIs(5))));
3504 TEST(WhenDynamicCastToTest, BadReference) {
3506 Base& as_base_ref = derived;
3507 EXPECT_THAT(as_base_ref,
Not(WhenDynamicCastTo<const OtherDerived&>(
_)));
3509 #endif // GTEST_HAS_RTTI
3512 template <
typename T>
3513 class ConstPropagatingPtr {
3515 typedef T element_type;
3517 ConstPropagatingPtr() :
val_() {}
3518 explicit ConstPropagatingPtr(
T* t) :
val_(
t) {}
3519 ConstPropagatingPtr(
const ConstPropagatingPtr& other) :
val_(other.
val_) {}
3524 const T*
get()
const {
return val_; }
3531 TEST(PointeeTest, WorksWithConstPropagatingPointers) {
3532 const Matcher< ConstPropagatingPtr<int> >
m =
Pointee(
Lt(5));
3534 const ConstPropagatingPtr<int> co(&three);
3535 ConstPropagatingPtr<int>
o(&three);
3543 TEST(PointeeTest, NeverMatchesNull) {
3544 const Matcher<const char*>
m =
Pointee(
_);
3549 TEST(PointeeTest, MatchesAgainstAValue) {
3550 const Matcher<int*>
m =
Pointee(5);
3559 TEST(PointeeTest, CanDescribeSelf) {
3561 EXPECT_EQ(
"points to a value that is > 3", Describe(
m));
3562 EXPECT_EQ(
"does not point to a value that is > 3",
3563 DescribeNegation(
m));
3566 TEST(PointeeTest, CanExplainMatchResult) {
3571 const Matcher<long*> m2 =
Pointee(GreaterThan(1));
3573 EXPECT_EQ(
"which points to 3" + OfType(
"long") +
", which is 2 more than 1",
3577 TEST(PointeeTest, AlwaysExplainsPointee) {
3578 const Matcher<int*>
m =
Pointee(0);
3580 EXPECT_EQ(
"which points to 42" + OfType(
"int"), Explain(
m, &n));
3586 Uncopyable() :
value_(-1) {}
3587 explicit Uncopyable(
int a_value) :
value_(a_value) {}
3590 void set_value(
int i) {
value_ =
i; }
3598 bool ValueIsPositive(
const Uncopyable& x) {
return x.value() > 0; }
3600 MATCHER_P(UncopyableIs, inner_matcher,
"") {
3606 AStruct() :
x(0),
y(1.0),
z(5),
p(nullptr) {}
3607 AStruct(
const AStruct& rhs)
3620 struct DerivedStruct :
public AStruct {
3628 TEST(FieldTest, WorksForNonConstField) {
3641 TEST(FieldTest, WorksForConstField) {
3655 TEST(FieldTest, WorksForUncopyableField) {
3665 TEST(FieldTest, WorksForPointerField) {
3667 Matcher<AStruct>
m =
Field(&
AStruct::p,
static_cast<const char*
>(
nullptr));
3682 TEST(FieldTest, WorksForByRefArgument) {
3693 TEST(FieldTest, WorksForArgumentOfSubType) {
3706 TEST(FieldTest, WorksForCompatibleMatcherType) {
3709 Matcher<signed char>(
Ge(0)));
3718 TEST(FieldTest, CanDescribeSelf) {
3721 EXPECT_EQ(
"is an object whose given field is >= 0", Describe(
m));
3722 EXPECT_EQ(
"is an object whose given field isn't >= 0", DescribeNegation(
m));
3725 TEST(FieldTest, CanDescribeSelfWithFieldName) {
3728 EXPECT_EQ(
"is an object whose field `field_name` is >= 0", Describe(
m));
3729 EXPECT_EQ(
"is an object whose field `field_name` isn't >= 0",
3730 DescribeNegation(
m));
3734 TEST(FieldTest, CanExplainMatchResult) {
3739 EXPECT_EQ(
"whose given field is 1" + OfType(
"int"), Explain(
m, a));
3743 "whose given field is 1" + OfType(
"int") +
", which is 1 more than 0",
3747 TEST(FieldTest, CanExplainMatchResultWithFieldName) {
3752 EXPECT_EQ(
"whose field `field_name` is 1" + OfType(
"int"), Explain(
m, a));
3755 EXPECT_EQ(
"whose field `field_name` is 1" + OfType(
"int") +
3756 ", which is 1 more than 0",
3761 TEST(FieldForPointerTest, WorksForPointerToConst) {
3771 TEST(FieldForPointerTest, WorksForPointerToNonConst) {
3781 TEST(FieldForPointerTest, WorksForReferenceToConstPointer) {
3791 TEST(FieldForPointerTest, DoesNotMatchNull) {
3798 TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
3810 TEST(FieldForPointerTest, CanDescribeSelf) {
3813 EXPECT_EQ(
"is an object whose given field is >= 0", Describe(
m));
3814 EXPECT_EQ(
"is an object whose given field isn't >= 0", DescribeNegation(
m));
3817 TEST(FieldForPointerTest, CanDescribeSelfWithFieldName) {
3820 EXPECT_EQ(
"is an object whose field `field_name` is >= 0", Describe(
m));
3821 EXPECT_EQ(
"is an object whose field `field_name` isn't >= 0",
3822 DescribeNegation(
m));
3826 TEST(FieldForPointerTest, CanExplainMatchResult) {
3831 EXPECT_EQ(
"", Explain(
m,
static_cast<const AStruct*
>(
nullptr)));
3832 EXPECT_EQ(
"which points to an object whose given field is 1" + OfType(
"int"),
3836 EXPECT_EQ(
"which points to an object whose given field is 1" + OfType(
"int") +
3837 ", which is 1 more than 0", Explain(
m, &a));
3840 TEST(FieldForPointerTest, CanExplainMatchResultWithFieldName) {
3845 EXPECT_EQ(
"", Explain(
m,
static_cast<const AStruct*
>(
nullptr)));
3847 "which points to an object whose field `field_name` is 1" + OfType(
"int"),
3851 EXPECT_EQ(
"which points to an object whose field `field_name` is 1" +
3852 OfType(
"int") +
", which is 1 more than 0",
3862 int n()
const {
return n_; }
3864 void set_n(
int new_n) {
n_ = new_n; }
3874 double&
x()
const {
return x_; }
3886 class DerivedClass :
public AClass {
3888 int k()
const {
return k_; }
3895 TEST(PropertyTest, WorksForNonReferenceProperty) {
3911 TEST(PropertyTest, WorksForReferenceToConstProperty) {
3913 Matcher<const AClass&> m_with_name =
3928 TEST(PropertyTest, WorksForRefQualifiedProperty) {
3930 Matcher<const AClass&> m_with_name =
3945 TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
3958 TEST(PropertyTest, WorksForByValueArgument) {
3971 TEST(PropertyTest, WorksForArgumentOfSubType) {
3986 TEST(PropertyTest, WorksForCompatibleMatcherType) {
3989 Matcher<signed char>(
Ge(0)));
3991 Matcher<const AClass&> m_with_name =
4003 TEST(PropertyTest, CanDescribeSelf) {
4006 EXPECT_EQ(
"is an object whose given property is >= 0", Describe(
m));
4007 EXPECT_EQ(
"is an object whose given property isn't >= 0",
4008 DescribeNegation(
m));
4011 TEST(PropertyTest, CanDescribeSelfWithPropertyName) {
4014 EXPECT_EQ(
"is an object whose property `fancy_name` is >= 0", Describe(
m));
4015 EXPECT_EQ(
"is an object whose property `fancy_name` isn't >= 0",
4016 DescribeNegation(
m));
4020 TEST(PropertyTest, CanExplainMatchResult) {
4025 EXPECT_EQ(
"whose given property is 1" + OfType(
"int"), Explain(
m, a));
4029 "whose given property is 1" + OfType(
"int") +
", which is 1 more than 0",
4033 TEST(PropertyTest, CanExplainMatchResultWithPropertyName) {
4038 EXPECT_EQ(
"whose property `fancy_name` is 1" + OfType(
"int"), Explain(
m, a));
4041 EXPECT_EQ(
"whose property `fancy_name` is 1" + OfType(
"int") +
4042 ", which is 1 more than 0",
4047 TEST(PropertyForPointerTest, WorksForPointerToConst) {
4059 TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
4072 TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) {
4084 TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
4091 TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
4105 TEST(PropertyForPointerTest, CanDescribeSelf) {
4108 EXPECT_EQ(
"is an object whose given property is >= 0", Describe(
m));
4109 EXPECT_EQ(
"is an object whose given property isn't >= 0",
4110 DescribeNegation(
m));
4113 TEST(PropertyForPointerTest, CanDescribeSelfWithPropertyDescription) {
4116 EXPECT_EQ(
"is an object whose property `fancy_name` is >= 0", Describe(
m));
4117 EXPECT_EQ(
"is an object whose property `fancy_name` isn't >= 0",
4118 DescribeNegation(
m));
4122 TEST(PropertyForPointerTest, CanExplainMatchResult) {
4127 EXPECT_EQ(
"", Explain(
m,
static_cast<const AClass*
>(
nullptr)));
4129 "which points to an object whose given property is 1" + OfType(
"int"),
4133 EXPECT_EQ(
"which points to an object whose given property is 1" +
4134 OfType(
"int") +
", which is 1 more than 0",
4138 TEST(PropertyForPointerTest, CanExplainMatchResultWithPropertyName) {
4143 EXPECT_EQ(
"", Explain(
m,
static_cast<const AClass*
>(
nullptr)));
4144 EXPECT_EQ(
"which points to an object whose property `fancy_name` is 1" +
4149 EXPECT_EQ(
"which points to an object whose property `fancy_name` is 1" +
4150 OfType(
"int") +
", which is 1 more than 0",
4159 return input == 1 ?
"foo" :
"bar";
4162 TEST(ResultOfTest, WorksForFunctionPointers) {
4170 TEST(ResultOfTest, CanDescribeItself) {
4171 Matcher<int> matcher =
ResultOf(&IntToStringFunction,
StrEq(
"foo"));
4173 EXPECT_EQ(
"is mapped by the given callable to a value that "
4174 "is equal to \"foo\"", Describe(matcher));
4175 EXPECT_EQ(
"is mapped by the given callable to a value that "
4176 "isn't equal to \"foo\"", DescribeNegation(matcher));
4180 int IntFunction(
int input) {
return input == 42 ? 80 : 90; }
4182 TEST(ResultOfTest, CanExplainMatchResult) {
4183 Matcher<int> matcher =
ResultOf(&IntFunction,
Ge(85));
4184 EXPECT_EQ(
"which is mapped by the given callable to 90" + OfType(
"int"),
4185 Explain(matcher, 36));
4187 matcher =
ResultOf(&IntFunction, GreaterThan(85));
4188 EXPECT_EQ(
"which is mapped by the given callable to 90" + OfType(
"int") +
4189 ", which is 5 more than 85", Explain(matcher, 36));
4194 TEST(ResultOfTest, WorksForNonReferenceResults) {
4195 Matcher<int> matcher =
ResultOf(&IntFunction,
Eq(80));
4203 double& DoubleFunction(
double&
input) {
return input; }
4205 Uncopyable& RefUncopyableFunction(Uncopyable&
obj) {
4209 TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
4212 Matcher<double&> matcher =
ResultOf(&DoubleFunction,
Ref(x));
4220 Matcher<Uncopyable&> matcher2 =
4231 TEST(ResultOfTest, WorksForReferenceToConstResults) {
4234 Matcher<const std::string&> matcher =
ResultOf(&StringFunction,
Ref(s));
4242 TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
4244 Matcher<int> matcher =
ResultOf(IntFunction, Matcher<signed char>(
Ge(85)));
4252 TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
4256 "NULL function pointer is passed into ResultOf\\(\\)\\.");
4261 TEST(ResultOfTest, WorksForFunctionReferences) {
4262 Matcher<int> matcher =
ResultOf(IntToStringFunction,
StrEq(
"foo"));
4271 return IntToStringFunction(
input);
4275 TEST(ResultOfTest, WorksForFunctors) {
4285 struct PolymorphicFunctor {
4287 int operator()(
int n) {
return n; }
4288 int operator()(
const char* s) {
return static_cast<int>(strlen(s)); }
4289 std::string operator()(
int *p) {
return p ?
"good ptr" :
"null"; }
4292 TEST(ResultOfTest, WorksForPolymorphicFunctors) {
4293 Matcher<int> matcher_int =
ResultOf(PolymorphicFunctor(),
Ge(5));
4298 Matcher<const char*> matcher_string =
ResultOf(PolymorphicFunctor(),
Ge(5));
4300 EXPECT_TRUE(matcher_string.Matches(
"long string"));
4304 TEST(ResultOfTest, WorksForPolymorphicFunctorsIgnoringResultType) {
4305 Matcher<int*> matcher =
ResultOf(PolymorphicFunctor(),
"good ptr");
4312 TEST(ResultOfTest, WorksForLambdas) {
4315 return std::string(
static_cast<size_t>(str_len),
'x');
4322 const int* ReferencingFunction(
const int& n) {
return &
n; }
4324 struct ReferencingFunctor {
4329 TEST(ResultOfTest, WorksForReferencingCallables) {
4332 Matcher<const int&> matcher2 =
ResultOf(ReferencingFunction,
Eq(&n));
4336 Matcher<const int&> matcher3 =
ResultOf(ReferencingFunctor(),
Eq(&n));
4341 class DivisibleByImpl {
4343 explicit DivisibleByImpl(
int a_divider) :
divider_(a_divider) {}
4346 template <
typename T>
4347 bool MatchAndExplain(
const T& n, MatchResultListener* listener)
const {
4348 *listener <<
"which is " << (
n %
divider_) <<
" modulo "
4353 void DescribeTo(ostream* os)
const {
4354 *os <<
"is divisible by " <<
divider_;
4357 void DescribeNegationTo(ostream* os)
const {
4358 *os <<
"is not divisible by " <<
divider_;
4361 void set_divider(
int a_divider) {
divider_ = a_divider; }
4362 int divider()
const {
return divider_; }
4368 PolymorphicMatcher<DivisibleByImpl> DivisibleBy(
int n) {
4374 TEST(ExplainMatchResultTest, AllOf_False_False) {
4375 const Matcher<int>
m =
AllOf(DivisibleBy(4), DivisibleBy(3));
4376 EXPECT_EQ(
"which is 1 modulo 4", Explain(
m, 5));
4381 TEST(ExplainMatchResultTest, AllOf_False_True) {
4382 const Matcher<int>
m =
AllOf(DivisibleBy(4), DivisibleBy(3));
4383 EXPECT_EQ(
"which is 2 modulo 4", Explain(
m, 6));
4388 TEST(ExplainMatchResultTest, AllOf_True_False) {
4389 const Matcher<int>
m =
AllOf(
Ge(1), DivisibleBy(3));
4390 EXPECT_EQ(
"which is 2 modulo 3", Explain(
m, 5));
4395 TEST(ExplainMatchResultTest, AllOf_True_True) {
4396 const Matcher<int>
m =
AllOf(DivisibleBy(2), DivisibleBy(3));
4397 EXPECT_EQ(
"which is 0 modulo 2, and which is 0 modulo 3", Explain(
m, 6));
4400 TEST(ExplainMatchResultTest, AllOf_True_True_2) {
4405 TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
4406 const Matcher<int>
m = GreaterThan(5);
4407 EXPECT_EQ(
"which is 1 more than 5", Explain(
m, 6));
4416 explicit NotCopyable(
int a_value) :
value_(a_value) {}
4420 bool operator==(
const NotCopyable& rhs)
const {
4421 return value() == rhs.value();
4424 bool operator>=(
const NotCopyable& rhs)
const {
4425 return value() >= rhs.value();
4433 TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) {
4434 const NotCopyable const_value1(1);
4435 const Matcher<const NotCopyable&>
m =
Eq(
ByRef(const_value1));
4437 const NotCopyable n1(1), n2(2);
4442 TEST(ByRefTest, AllowsNotCopyableValueInMatchers) {
4443 NotCopyable value2(2);
4444 const Matcher<NotCopyable&>
m =
Ge(
ByRef(value2));
4446 NotCopyable n1(1), n2(2);
4451 TEST(IsEmptyTest, ImplementsIsEmpty) {
4460 TEST(IsEmptyTest, WorksWithString) {
4469 TEST(IsEmptyTest, CanDescribeSelf) {
4470 Matcher<vector<int> >
m =
IsEmpty();
4472 EXPECT_EQ(
"isn't empty", DescribeNegation(
m));
4475 TEST(IsEmptyTest, ExplainsResult) {
4476 Matcher<vector<int> >
m =
IsEmpty();
4483 TEST(IsEmptyTest, WorksWithMoveOnly) {
4484 ContainerHelper helper;
4489 TEST(IsTrueTest, IsTrueIsFalse) {
4517 std::unique_ptr<int> null_unique;
4518 std::unique_ptr<int> nonnull_unique(
new int(0));
4525 TEST(SizeIsTest, ImplementsSizeIs) {
4537 TEST(SizeIsTest, WorksWithMap) {
4549 TEST(SizeIsTest, WorksWithReferences) {
4551 Matcher<const vector<int>&>
m =
SizeIs(1);
4557 TEST(SizeIsTest, WorksWithMoveOnly) {
4558 ContainerHelper helper;
4560 helper.Call(MakeUniquePtrs({1, 2, 3}));
4565 struct MinimalistCustomType {
4566 int size()
const {
return 1; }
4568 TEST(SizeIsTest, WorksWithMinimalistCustomType) {
4574 TEST(SizeIsTest, CanDescribeSelf) {
4575 Matcher<vector<int> >
m =
SizeIs(2);
4576 EXPECT_EQ(
"size is equal to 2", Describe(
m));
4577 EXPECT_EQ(
"size isn't equal to 2", DescribeNegation(
m));
4580 TEST(SizeIsTest, ExplainsResult) {
4581 Matcher<vector<int> > m1 =
SizeIs(2);
4582 Matcher<vector<int> > m2 =
SizeIs(
Lt(2u));
4584 Matcher<vector<int> > m4 =
SizeIs(GreaterThan(1));
4589 EXPECT_EQ(
"whose size 0 doesn't match, which is 1 less than 1",
4596 EXPECT_EQ(
"whose size 2 matches, which is 1 more than 1",
4600 #if GTEST_HAS_TYPED_TEST
4604 template <
typename T>
4612 ContainerEqTestTypes;
4618 static const int vals[] = {1, 1, 2, 3, 5, 8};
4627 static const int vals[] = {1, 1, 2, 3, 5, 8};
4628 static const int test_vals[] = {2, 1, 8, 5};
4630 TypeParam test_set(test_vals, test_vals + 4);
4633 EXPECT_EQ(
"which doesn't have these expected elements: 3",
4634 Explain(
m, test_set));
4639 static const int vals[] = {1, 1, 2, 3, 5, 8};
4640 static const int test_vals[] = {1, 2, 3, 5, 8, 46};
4642 TypeParam test_set(test_vals, test_vals + 6);
4643 const Matcher<const TypeParam&>
m =
ContainerEq(my_set);
4645 EXPECT_EQ(
"which has these unexpected elements: 46", Explain(
m, test_set));
4649 TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) {
4650 static const int vals[] = {1, 1, 2, 3, 5, 8};
4651 static const int test_vals[] = {1, 2, 3, 8, 46};
4653 TypeParam test_set(test_vals, test_vals + 5);
4656 EXPECT_EQ(
"which has these unexpected elements: 46,\n"
4657 "and doesn't have these expected elements: 5",
4658 Explain(
m, test_set));
4662 TYPED_TEST(ContainerEqTest, DuplicateDifference) {
4663 static const int vals[] = {1, 1, 2, 3, 5, 8};
4664 static const int test_vals[] = {1, 2, 3, 5, 8};
4666 TypeParam test_set(test_vals, test_vals + 5);
4667 const Matcher<const TypeParam&>
m =
ContainerEq(my_set);
4672 #endif // GTEST_HAS_TYPED_TEST
4676 TEST(ContainerEqExtraTest, MultipleValuesMissing) {
4677 static const int vals[] = {1, 1, 2, 3, 5, 8};
4678 static const int test_vals[] = {2, 1, 5};
4679 vector<int> my_set(
vals,
vals + 6);
4680 vector<int> test_set(test_vals, test_vals + 3);
4683 EXPECT_EQ(
"which doesn't have these expected elements: 3, 8",
4684 Explain(
m, test_set));
4689 TEST(ContainerEqExtraTest, MultipleValuesAdded) {
4690 static const int vals[] = {1, 1, 2, 3, 5, 8};
4691 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46};
4692 list<size_t> my_set(
vals,
vals + 6);
4693 list<size_t> test_set(test_vals, test_vals + 7);
4694 const Matcher<const list<size_t>&>
m =
ContainerEq(my_set);
4696 EXPECT_EQ(
"which has these unexpected elements: 92, 46",
4697 Explain(
m, test_set));
4701 TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) {
4702 static const int vals[] = {1, 1, 2, 3, 5, 8};
4703 static const int test_vals[] = {1, 2, 3, 92, 46};
4704 list<size_t> my_set(
vals,
vals + 6);
4705 list<size_t> test_set(test_vals, test_vals + 5);
4706 const Matcher<const list<size_t> >
m =
ContainerEq(my_set);
4708 EXPECT_EQ(
"which has these unexpected elements: 92, 46,\n"
4709 "and doesn't have these expected elements: 5, 8",
4710 Explain(
m, test_set));
4715 TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) {
4716 static const int vals[] = {1, 1, 2, 3, 5, 8};
4717 static const int test_vals[] = {1, 2, 3, 5, 8};
4718 vector<int> my_set(
vals,
vals + 6);
4719 vector<int> test_set(test_vals, test_vals + 5);
4729 TEST(ContainerEqExtraTest, WorksForMaps) {
4730 map<int, std::string> my_map;
4734 map<int, std::string> test_map;
4738 const Matcher<const map<int, std::string>&>
m =
ContainerEq(my_map);
4742 EXPECT_EQ(
"which has these unexpected elements: (0, \"aa\"),\n"
4743 "and doesn't have these expected elements: (0, \"a\")",
4744 Explain(
m, test_map));
4747 TEST(ContainerEqExtraTest, WorksForNativeArray) {
4748 int a1[] = {1, 2, 3};
4749 int a2[] = {1, 2, 3};
4750 int b[] = {1, 2, 4};
4756 TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) {
4757 const char a1[][3] = {
"hi",
"lo"};
4758 const char a2[][3] = {
"hi",
"lo"};
4759 const char b[][3] = {
"lo",
"hi"};
4770 TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) {
4771 const int a1[] = {1, 2, 3};
4772 const int a2[] = {1, 2, 3};
4773 const int b[] = {1, 2, 3, 4};
4775 const int*
const p1 =
a1;
4779 const int c[] = {1, 3, 2};
4783 TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) {
4785 {
"hi",
"hello",
"ciao"},
4786 {
"bye",
"see you",
"ciao"}
4790 {
"hi",
"hello",
"ciao"},
4791 {
"bye",
"see you",
"ciao"}
4801 TEST(WhenSortedByTest, WorksForEmptyContainer) {
4802 const vector<int> numbers;
4807 TEST(WhenSortedByTest, WorksForNonEmptyContainer) {
4808 vector<unsigned> numbers;
4809 numbers.push_back(3);
4810 numbers.push_back(1);
4811 numbers.push_back(2);
4812 numbers.push_back(2);
4819 TEST(WhenSortedByTest, WorksForNonVectorContainer) {
4820 list<std::string>
words;
4821 words.push_back(
"say");
4822 words.push_back(
"hello");
4823 words.push_back(
"world");
4830 TEST(WhenSortedByTest, WorksForNativeArray) {
4831 const int numbers[] = {1, 3, 2, 4};
4832 const int sorted_numbers[] = {1, 2, 3, 4};
4839 TEST(WhenSortedByTest, CanDescribeSelf) {
4841 EXPECT_EQ(
"(when sorted) has 2 elements where\n"
4842 "element #0 is equal to 1,\n"
4843 "element #1 is equal to 2",
4845 EXPECT_EQ(
"(when sorted) doesn't have 2 elements, or\n"
4846 "element #0 isn't equal to 1, or\n"
4847 "element #1 isn't equal to 2",
4848 DescribeNegation(
m));
4851 TEST(WhenSortedByTest, ExplainsMatchResult) {
4852 const int a[] = {2, 1};
4853 EXPECT_EQ(
"which is { 1, 2 } when sorted, whose element #0 doesn't match",
4855 EXPECT_EQ(
"which is { 1, 2 } when sorted",
4862 TEST(WhenSortedTest, WorksForEmptyContainer) {
4863 const vector<int> numbers;
4868 TEST(WhenSortedTest, WorksForNonEmptyContainer) {
4869 list<std::string>
words;
4870 words.push_back(
"3");
4871 words.push_back(
"1");
4872 words.push_back(
"2");
4873 words.push_back(
"2");
4878 TEST(WhenSortedTest, WorksForMapTypes) {
4879 map<std::string, int> word_counts;
4880 word_counts[
"and"] = 1;
4881 word_counts[
"the"] = 1;
4882 word_counts[
"buffalo"] = 2;
4888 Pair(
"buffalo", 2)))));
4891 TEST(WhenSortedTest, WorksForMultiMapTypes) {
4892 multimap<int, int> ifib;
4893 ifib.insert(make_pair(8, 6));
4894 ifib.insert(make_pair(2, 3));
4895 ifib.insert(make_pair(1, 1));
4896 ifib.insert(make_pair(3, 4));
4897 ifib.insert(make_pair(1, 2));
4898 ifib.insert(make_pair(5, 5));
4913 TEST(WhenSortedTest, WorksForPolymorphicMatcher) {
4921 TEST(WhenSortedTest, WorksForVectorConstRefMatcher) {
4925 Matcher<const std::vector<int>&> vector_match =
ElementsAre(1, 2);
4927 Matcher<const std::vector<int>&> not_vector_match =
ElementsAre(2, 1);
4933 template <
typename T>
4938 typedef ConstIter const_iterator;
4941 template <
typename InIter>
4944 const_iterator
begin()
const {
4945 return const_iterator(
this,
remainder_.begin());
4947 const_iterator
end()
const {
4948 return const_iterator(
this,
remainder_.end());
4952 class ConstIter :
public std::iterator<std::input_iterator_tag,
4956 const value_type&> {
4958 ConstIter(
const Streamlike* s,
4965 s_->remainder_.erase(
pos_++);
4971 class PostIncrProxy {
4979 PostIncrProxy proxy(**
this);
4984 friend bool operator==(
const ConstIter& a,
const ConstIter&
b) {
4985 return a.s_ ==
b.s_ &&
a.pos_ ==
b.pos_;
4987 friend bool operator!=(
const ConstIter& a,
const ConstIter&
b) {
4992 const Streamlike*
s_;
4996 friend std::ostream&
operator<<(std::ostream& os,
const Streamlike& s) {
4998 typedef typename std::list<value_type>::const_iterator
Iter;
4999 const char*
sep =
"";
5000 for (
Iter it =
s.remainder_.begin();
it !=
s.remainder_.end(); ++
it) {
5011 TEST(StreamlikeTest, Iteration) {
5012 const int a[5] = {2, 1, 4, 5, 3};
5013 Streamlike<int>
s(a, a + 5);
5014 Streamlike<int>::const_iterator
it =
s.begin();
5016 while (
it !=
s.end()) {
5022 TEST(BeginEndDistanceIsTest, WorksWithForwardList) {
5034 TEST(BeginEndDistanceIsTest, WorksWithNonStdList) {
5035 const int a[5] = {1, 2, 3, 4, 5};
5036 Streamlike<int>
s(a, a + 5);
5040 TEST(BeginEndDistanceIsTest, CanDescribeSelf) {
5042 EXPECT_EQ(
"distance between begin() and end() is equal to 2", Describe(
m));
5043 EXPECT_EQ(
"distance between begin() and end() isn't equal to 2",
5044 DescribeNegation(
m));
5047 TEST(BeginEndDistanceIsTest, WorksWithMoveOnly) {
5048 ContainerHelper helper;
5050 helper.Call(MakeUniquePtrs({1, 2}));
5053 TEST(BeginEndDistanceIsTest, ExplainsResult) {
5059 EXPECT_EQ(
"whose distance between begin() and end() 0 doesn't match",
5061 EXPECT_EQ(
"whose distance between begin() and end() 0 matches",
5063 EXPECT_EQ(
"whose distance between begin() and end() 0 matches",
5066 "whose distance between begin() and end() 0 doesn't match, which is 1 "
5071 EXPECT_EQ(
"whose distance between begin() and end() 2 matches",
5073 EXPECT_EQ(
"whose distance between begin() and end() 2 doesn't match",
5075 EXPECT_EQ(
"whose distance between begin() and end() 2 doesn't match",
5078 "whose distance between begin() and end() 2 matches, which is 1 more "
5083 TEST(WhenSortedTest, WorksForStreamlike) {
5086 const int a[5] = {2, 1, 4, 5, 3};
5092 TEST(WhenSortedTest, WorksForVectorConstRefMatcherOnStreamlike) {
5093 const int a[] = {2, 1, 4, 5, 3};
5095 Matcher<const std::vector<int>&> vector_match =
ElementsAre(1, 2, 3, 4, 5);
5100 TEST(IsSupersetOfTest, WorksForNativeArray) {
5101 const int subset[] = {1, 4};
5102 const int superset[] = {1, 2, 4};
5103 const int disjoint[] = {1, 0, 3};
5111 TEST(IsSupersetOfTest, WorksWithDuplicates) {
5112 const int not_enough[] = {1, 2};
5113 const int enough[] = {1, 1, 2};
5114 const int expected[] = {1, 1};
5119 TEST(IsSupersetOfTest, WorksForEmpty) {
5120 vector<int> numbers;
5121 vector<int> expected;
5123 expected.push_back(1);
5126 numbers.push_back(1);
5127 numbers.push_back(2);
5129 expected.push_back(1);
5131 expected.push_back(2);
5133 expected.push_back(3);
5137 TEST(IsSupersetOfTest, WorksForStreamlike) {
5138 const int a[5] = {1, 2, 3, 4, 5};
5141 vector<int> expected;
5142 expected.push_back(1);
5143 expected.push_back(2);
5144 expected.push_back(5);
5147 expected.push_back(0);
5151 TEST(IsSupersetOfTest, TakesStlContainer) {
5152 const int actual[] = {3, 1, 2};
5154 ::std::list<int> expected;
5155 expected.push_back(1);
5156 expected.push_back(3);
5159 expected.push_back(4);
5163 TEST(IsSupersetOfTest, Describe) {
5164 typedef std::vector<int> IntVec;
5166 expected.push_back(111);
5167 expected.push_back(222);
5168 expected.push_back(333);
5170 Describe<IntVec>(IsSupersetOf(expected)),
5171 Eq(
"a surjection from elements to requirements exists such that:\n"
5172 " - an element is equal to 111\n"
5173 " - an element is equal to 222\n"
5174 " - an element is equal to 333"));
5177 TEST(IsSupersetOfTest, DescribeNegation) {
5178 typedef std::vector<int> IntVec;
5180 expected.push_back(111);
5181 expected.push_back(222);
5182 expected.push_back(333);
5184 DescribeNegation<IntVec>(IsSupersetOf(expected)),
5185 Eq(
"no surjection from elements to requirements exists such that:\n"
5186 " - an element is equal to 111\n"
5187 " - an element is equal to 222\n"
5188 " - an element is equal to 333"));
5191 TEST(IsSupersetOfTest, MatchAndExplain) {
5195 std::vector<int> expected;
5196 expected.push_back(1);
5197 expected.push_back(2);
5198 StringMatchResultListener listener;
5202 Eq(
"where the following matchers don't match any elements:\n"
5203 "matcher #0: is equal to 1"));
5210 " - element #0 is matched by matcher #1,\n"
5211 " - element #2 is matched by matcher #0"));
5214 TEST(IsSupersetOfTest, WorksForRhsInitializerList) {
5215 const int numbers[] = {1, 3, 6, 2, 4, 5};
5220 TEST(IsSupersetOfTest, WorksWithMoveOnly) {
5221 ContainerHelper helper;
5223 helper.Call(MakeUniquePtrs({1, 2}));
5225 helper.Call(MakeUniquePtrs({2}));
5228 TEST(IsSubsetOfTest, WorksForNativeArray) {
5229 const int subset[] = {1, 4};
5230 const int superset[] = {1, 2, 4};
5231 const int disjoint[] = {1, 0, 3};
5239 TEST(IsSubsetOfTest, WorksWithDuplicates) {
5240 const int not_enough[] = {1, 2};
5241 const int enough[] = {1, 1, 2};
5242 const int actual[] = {1, 1};
5247 TEST(IsSubsetOfTest, WorksForEmpty) {
5248 vector<int> numbers;
5249 vector<int> expected;
5251 expected.push_back(1);
5254 numbers.push_back(1);
5255 numbers.push_back(2);
5257 expected.push_back(1);
5259 expected.push_back(2);
5261 expected.push_back(3);
5265 TEST(IsSubsetOfTest, WorksForStreamlike) {
5266 const int a[5] = {1, 2};
5269 vector<int> expected;
5270 expected.push_back(1);
5272 expected.push_back(2);
5273 expected.push_back(5);
5277 TEST(IsSubsetOfTest, TakesStlContainer) {
5278 const int actual[] = {3, 1, 2};
5280 ::std::list<int> expected;
5281 expected.push_back(1);
5282 expected.push_back(3);
5285 expected.push_back(2);
5286 expected.push_back(4);
5290 TEST(IsSubsetOfTest, Describe) {
5291 typedef std::vector<int> IntVec;
5293 expected.push_back(111);
5294 expected.push_back(222);
5295 expected.push_back(333);
5298 Describe<IntVec>(IsSubsetOf(expected)),
5299 Eq(
"an injection from elements to requirements exists such that:\n"
5300 " - an element is equal to 111\n"
5301 " - an element is equal to 222\n"
5302 " - an element is equal to 333"));
5305 TEST(IsSubsetOfTest, DescribeNegation) {
5306 typedef std::vector<int> IntVec;
5308 expected.push_back(111);
5309 expected.push_back(222);
5310 expected.push_back(333);
5312 DescribeNegation<IntVec>(IsSubsetOf(expected)),
5313 Eq(
"no injection from elements to requirements exists such that:\n"
5314 " - an element is equal to 111\n"
5315 " - an element is equal to 222\n"
5316 " - an element is equal to 333"));
5319 TEST(IsSubsetOfTest, MatchAndExplain) {
5323 std::vector<int> expected;
5324 expected.push_back(1);
5325 expected.push_back(2);
5326 StringMatchResultListener listener;
5330 Eq(
"where the following elements don't match any matchers:\n"
5333 expected.push_back(3);
5338 " - element #0 is matched by matcher #1,\n"
5339 " - element #1 is matched by matcher #2"));
5342 TEST(IsSubsetOfTest, WorksForRhsInitializerList) {
5343 const int numbers[] = {1, 2, 3};
5348 TEST(IsSubsetOfTest, WorksWithMoveOnly) {
5349 ContainerHelper helper;
5351 helper.Call(MakeUniquePtrs({1}));
5353 helper.Call(MakeUniquePtrs({2}));
5359 TEST(ElemensAreStreamTest, WorksForStreamlike) {
5360 const int a[5] = {1, 2, 3, 4, 5};
5366 TEST(ElemensAreArrayStreamTest, WorksForStreamlike) {
5367 const int a[5] = {1, 2, 3, 4, 5};
5370 vector<int> expected;
5371 expected.push_back(1);
5372 expected.push_back(2);
5373 expected.push_back(3);
5374 expected.push_back(4);
5375 expected.push_back(5);
5382 TEST(ElementsAreTest, WorksWithUncopyable) {
5384 objs[0].set_value(-3);
5385 objs[1].set_value(1);
5389 TEST(ElementsAreTest, WorksWithMoveOnly) {
5390 ContainerHelper helper;
5392 helper.Call(MakeUniquePtrs({1, 2}));
5395 helper.Call(MakeUniquePtrs({3, 4}));
5398 TEST(ElementsAreTest, TakesStlContainer) {
5399 const int actual[] = {3, 1, 2};
5401 ::std::list<int> expected;
5402 expected.push_back(3);
5403 expected.push_back(1);
5404 expected.push_back(2);
5407 expected.push_back(4);
5413 TEST(UnorderedElementsAreArrayTest, SucceedsWhenExpected) {
5414 const int a[] = {0, 1, 2, 3, 4};
5417 StringMatchResultListener listener;
5419 s, &listener)) << listener.str();
5420 }
while (std::next_permutation(
s.begin(),
s.end()));
5423 TEST(UnorderedElementsAreArrayTest, VectorBool) {
5424 const bool a[] = {0, 1, 0, 1, 1};
5425 const bool b[] = {1, 0, 1, 1, 0};
5428 StringMatchResultListener listener;
5430 actual, &listener)) << listener.str();
5433 TEST(UnorderedElementsAreArrayTest, WorksForStreamlike) {
5437 const int a[5] = {2, 1, 4, 5, 3};
5440 ::std::vector<int> expected;
5441 expected.push_back(1);
5442 expected.push_back(2);
5443 expected.push_back(3);
5444 expected.push_back(4);
5445 expected.push_back(5);
5448 expected.push_back(6);
5452 TEST(UnorderedElementsAreArrayTest, TakesStlContainer) {
5453 const int actual[] = {3, 1, 2};
5455 ::std::list<int> expected;
5456 expected.push_back(1);
5457 expected.push_back(2);
5458 expected.push_back(3);
5461 expected.push_back(4);
5466 TEST(UnorderedElementsAreArrayTest, TakesInitializerList) {
5467 const int a[5] = {2, 1, 4, 5, 3};
5472 TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfCStrings) {
5478 TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {
5479 const int a[5] = {2, 1, 4, 5, 3};
5486 TEST(UnorderedElementsAreArrayTest,
5487 TakesInitializerListOfDifferentTypedMatchers) {
5488 const int a[5] = {2, 1, 4, 5, 3};
5499 TEST(UnorderedElementsAreArrayTest, WorksWithMoveOnly) {
5500 ContainerHelper helper;
5503 helper.Call(MakeUniquePtrs({2, 1}));
5508 typedef std::vector<int> IntVec;
5511 TEST_F(UnorderedElementsAreTest, WorksWithUncopyable) {
5513 objs[0].set_value(-3);
5514 objs[1].set_value(1);
5519 TEST_F(UnorderedElementsAreTest, SucceedsWhenExpected) {
5520 const int a[] = {1, 2, 3};
5523 StringMatchResultListener listener;
5525 s, &listener)) << listener.str();
5526 }
while (std::next_permutation(
s.begin(),
s.end()));
5529 TEST_F(UnorderedElementsAreTest, FailsWhenAnElementMatchesNoMatcher) {
5530 const int a[] = {1, 2, 3};
5532 std::vector<Matcher<int> > mv;
5537 StringMatchResultListener listener;
5539 s, &listener)) << listener.str();
5542 TEST_F(UnorderedElementsAreTest, WorksForStreamlike) {
5546 const int a[5] = {2, 1, 4, 5, 3};
5553 TEST_F(UnorderedElementsAreTest, WorksWithMoveOnly) {
5554 ContainerHelper helper;
5556 helper.Call(MakeUniquePtrs({2, 1}));
5565 TEST_F(UnorderedElementsAreTest, Performance) {
5567 std::vector<Matcher<int> > mv;
5568 for (
int i = 0;
i < 100; ++
i) {
5573 StringMatchResultListener listener;
5575 s, &listener)) << listener.str();
5581 TEST_F(UnorderedElementsAreTest, PerformanceHalfStrict) {
5583 std::vector<Matcher<int> > mv;
5584 for (
int i = 0;
i < 100; ++
i) {
5592 StringMatchResultListener listener;
5594 s, &listener)) << listener.str();
5597 TEST_F(UnorderedElementsAreTest, FailMessageCountWrong) {
5600 StringMatchResultListener listener;
5602 v, &listener)) << listener.str();
5606 TEST_F(UnorderedElementsAreTest, FailMessageCountWrongZero) {
5608 StringMatchResultListener listener;
5610 v, &listener)) << listener.str();
5614 TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatchers) {
5618 StringMatchResultListener listener;
5620 v, &listener)) << listener.str();
5623 Eq(
"where the following matchers don't match any elements:\n"
5624 "matcher #1: is equal to 2"));
5627 TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedElements) {
5631 StringMatchResultListener listener;
5633 v, &listener)) << listener.str();
5636 Eq(
"where the following elements don't match any matchers:\n"
5640 TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatcherAndElement) {
5644 StringMatchResultListener listener;
5646 v, &listener)) << listener.str();
5650 " the following matchers don't match any elements:\n"
5651 "matcher #0: is equal to 1\n"
5654 " the following elements don't match any matchers:\n"
5661 ss <<
"(element #" <<
element <<
", matcher #" << matcher <<
")";
5665 TEST_F(UnorderedElementsAreTest, FailMessageImperfectMatchOnly) {
5668 std::vector<std::string>
v;
5672 StringMatchResultListener listener;
5678 "where no permutation of the elements can satisfy all matchers, "
5679 "and the closest match is 2 of 3 matchers with the "
5686 ",\n " + EMString(1, 2) +
"\n}",
5687 prefix +
"{\n " + EMString(0, 1) +
5688 ",\n " + EMString(1, 2) +
"\n}",
5689 prefix +
"{\n " + EMString(0, 0) +
5690 ",\n " + EMString(2, 2) +
"\n}",
5691 prefix +
"{\n " + EMString(0, 1) +
5692 ",\n " + EMString(2, 2) +
"\n}"));
5695 TEST_F(UnorderedElementsAreTest, Describe) {
5700 Eq(
"has 1 element and that element is equal to 345"));
5703 Eq(
"has 3 elements and there exists some permutation "
5704 "of elements such that:\n"
5705 " - element #0 is equal to 111, and\n"
5706 " - element #1 is equal to 222, and\n"
5707 " - element #2 is equal to 333"));
5710 TEST_F(UnorderedElementsAreTest, DescribeNegation) {
5715 Eq(
"doesn't have 1 element, or has 1 element that isn't equal to 345"));
5718 Eq(
"doesn't have 3 elements, or there exists no permutation "
5719 "of elements such that:\n"
5720 " - element #0 is equal to 123, and\n"
5721 " - element #1 is equal to 234, and\n"
5722 " - element #2 is equal to 345"));
5731 template <
typename Graph>
5732 class BacktrackingMaxBPMState {
5735 explicit BacktrackingMaxBPMState(
const Graph* g) :
graph_(
g) { }
5738 if (
graph_->LhsSize() == 0 ||
graph_->RhsSize() == 0) {
5743 for (
size_t irhs = 0; irhs <
graph_->RhsSize(); ++irhs) {
5753 static const size_t kUnused =
static_cast<size_t>(-1);
5755 void PushMatch(
size_t lhs,
size_t rhs) {
5771 bool RecurseInto(
size_t irhs) {
5775 for (
size_t ilhs = 0; ilhs <
graph_->LhsSize(); ++ilhs) {
5779 if (!
graph_->HasEdge(ilhs, irhs)) {
5782 PushMatch(ilhs, irhs);
5786 for (
size_t mi = irhs + 1; mi <
graph_->RhsSize(); ++mi) {
5787 if (!RecurseInto(mi))
return false;
5801 template <
typename Graph>
5808 template <
typename Graph>
5810 FindBacktrackingMaxBPM(
const Graph& g) {
5811 return BacktrackingMaxBPMState<Graph>(&
g).Compute();
5821 TEST_P(BipartiteTest, Exhaustive) {
5822 size_t nodes = GetParam();
5823 MatchMatrix graph(nodes, nodes);
5827 EXPECT_EQ(FindBacktrackingMaxBPM(graph).
size(), matches.size())
5828 <<
"graph: " << graph.DebugString();
5831 std::vector<bool> seen_element(graph.LhsSize());
5832 std::vector<bool> seen_matcher(graph.RhsSize());
5834 for (
size_t i = 0;
i < matches.size(); ++
i) {
5835 size_t ilhs = matches[
i].first;
5836 size_t irhs = matches[
i].second;
5840 seen_element[ilhs] =
true;
5841 seen_matcher[irhs] =
true;
5843 }
while (graph.NextGraph());
5850 class BipartiteNonSquareTest
5854 TEST_F(BipartiteNonSquareTest, SimpleBacktracking) {
5862 MatchMatrix
g(4, 3);
5863 static const size_t kEdges[][2] = {{0, 2}, {1, 1}, {2, 1}, {3, 0}};
5865 g.SetEdge(kEdges[i][0], kEdges[i][1],
true);
5870 Pair(0, 2))) <<
g.DebugString();
5874 TEST_P(BipartiteNonSquareTest, Exhaustive) {
5875 size_t nlhs = GetParam().first;
5876 size_t nrhs = GetParam().second;
5877 MatchMatrix graph(nlhs, nrhs);
5881 <<
"graph: " << graph.DebugString()
5882 <<
"\nbacktracking: "
5886 }
while (graph.NextGraph());
5891 std::make_pair(1, 2),
5892 std::make_pair(2, 1),
5893 std::make_pair(3, 2),
5894 std::make_pair(2, 3),
5895 std::make_pair(4, 1),
5896 std::make_pair(1, 4),
5897 std::make_pair(4, 3),
5898 std::make_pair(3, 4)));
5900 class BipartiteRandomTest
5905 TEST_P(BipartiteRandomTest, LargerNets) {
5906 int nodes = GetParam().first;
5907 int iters = GetParam().second;
5908 MatchMatrix graph(
static_cast<size_t>(nodes),
static_cast<size_t>(nodes));
5915 for (; iters > 0; --iters, ++
seed) {
5916 srand(
static_cast<unsigned int>(
seed));
5920 <<
" graph: " << graph.DebugString()
5921 <<
"\nTo reproduce the failure, rerun the test with the flag"
5929 std::make_pair(5, 10000),
5930 std::make_pair(6, 5000),
5931 std::make_pair(7, 2000),
5932 std::make_pair(8, 500),
5933 std::make_pair(9, 100)));
5937 TEST(IsReadableTypeNameTest, ReturnsTrueForShortNames) {
5944 TEST(IsReadableTypeNameTest, ReturnsTrueForLongNonTemplateNonFunctionNames) {
5950 TEST(IsReadableTypeNameTest, ReturnsFalseForLongTemplateNames) {
5956 TEST(IsReadableTypeNameTest, ReturnsFalseForLongFunctionTypeNames) {
5962 TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) {
5968 const char* params[] = {
"5"};
5971 Strings(params, params + 1)));
5973 const char* params2[] = {
"5",
"8"};
5976 Strings(params2, params2 + 2)));
5980 TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
5981 PolymorphicMatcher<DivisibleByImpl>
m(DivisibleByImpl(42));
5982 DivisibleByImpl& impl =
m.mutable_impl();
5985 impl.set_divider(0);
5990 TEST(PolymorphicMatcherTest, CanAccessImpl) {
5991 const PolymorphicMatcher<DivisibleByImpl>
m(DivisibleByImpl(42));
5992 const DivisibleByImpl& impl =
m.impl();
5996 TEST(MatcherTupleTest, ExplainsMatchFailure) {
6008 " Actual: 2, which is 3 less than 5\n"
6009 " Expected arg #1: is equal to 'a' (97, 0x61)\n"
6010 " Actual: 'b' (98, 0x62)\n",
6018 " Actual: 2, which is 3 less than 5\n",
6025 TEST(EachTest, ExplainsMatchResultCorrectly) {
6028 Matcher<set<int> >
m =
Each(2);
6031 Matcher<
const int(&)[1]>
n =
Each(1);
6033 const int b[1] = {1};
6037 EXPECT_EQ(
"whose element #0 doesn't match", Explain(n,
b));
6042 m =
Each(GreaterThan(0));
6045 m =
Each(GreaterThan(10));
6046 EXPECT_EQ(
"whose element #0 doesn't match, which is 9 less than 10",
6050 TEST(EachTest, DescribesItselfCorrectly) {
6051 Matcher<vector<int> >
m =
Each(1);
6052 EXPECT_EQ(
"only contains elements that is equal to 1", Describe(
m));
6054 Matcher<vector<int> > m2 =
Not(
m);
6055 EXPECT_EQ(
"contains some element that isn't equal to 1", Describe(m2));
6058 TEST(EachTest, MatchesVectorWhenAllElementsMatch) {
6059 vector<int> some_vector;
6061 some_vector.push_back(3);
6064 some_vector.push_back(1);
6065 some_vector.push_back(2);
6069 vector<std::string> another_vector;
6070 another_vector.push_back(
"fee");
6072 another_vector.push_back(
"fie");
6073 another_vector.push_back(
"foe");
6074 another_vector.push_back(
"fum");
6078 TEST(EachTest, MatchesMapWhenAllElementsMatch) {
6079 map<const char*, int> my_map;
6080 const char*
bar =
"a string";
6084 map<std::string, int> another_map;
6086 another_map[
"fee"] = 1;
6088 another_map[
"fie"] = 2;
6089 another_map[
"foe"] = 3;
6090 another_map[
"fum"] = 4;
6096 TEST(EachTest, AcceptsMatcher) {
6097 const int a[] = {1, 2, 3};
6102 TEST(EachTest, WorksForNativeArrayAsTuple) {
6103 const int a[] = {1, 2};
6104 const int*
const pointer =
a;
6109 TEST(EachTest, WorksWithMoveOnly) {
6110 ContainerHelper helper;
6112 helper.Call(MakeUniquePtrs({1, 2}));
6116 class IsHalfOfMatcher {
6118 template <
typename T1,
typename T2>
6119 bool MatchAndExplain(
const std::tuple<T1, T2>& a_pair,
6120 MatchResultListener* listener)
const {
6121 if (std::get<0>(a_pair) == std::get<1>(a_pair) / 2) {
6122 *listener <<
"where the second is " << std::get<1>(a_pair);
6125 *listener <<
"where the second/2 is " << std::get<1>(a_pair) / 2;
6130 void DescribeTo(ostream* os)
const {
6131 *os <<
"are a pair where the first is half of the second";
6134 void DescribeNegationTo(ostream* os)
const {
6135 *os <<
"are a pair where the first isn't half of the second";
6139 PolymorphicMatcher<IsHalfOfMatcher> IsHalfOf() {
6143 TEST(PointwiseTest, DescribesSelf) {
6148 const Matcher<const vector<int>&>
m =
Pointwise(IsHalfOf(), rhs);
6149 EXPECT_EQ(
"contains 3 values, where each value and its corresponding value "
6150 "in { 1, 2, 3 } are a pair where the first is half of the second",
6152 EXPECT_EQ(
"doesn't contain exactly 3 values, or contains a value x at some "
6153 "index i where x and the i-th value of { 1, 2, 3 } are a pair "
6154 "where the first isn't half of the second",
6155 DescribeNegation(
m));
6158 TEST(PointwiseTest, MakesCopyOfRhs) {
6159 list<signed char> rhs;
6164 const Matcher<
const int (&)[2]>
m =
Pointwise(IsHalfOf(), rhs);
6172 TEST(PointwiseTest, WorksForLhsNativeArray) {
6173 const int lhs[] = {1, 2, 3};
6182 TEST(PointwiseTest, WorksForRhsNativeArray) {
6183 const int rhs[] = {1, 2, 3};
6193 TEST(PointwiseTest, WorksForVectorOfBool) {
6194 vector<bool> rhs(3,
false);
6196 vector<bool> lhs = rhs;
6203 TEST(PointwiseTest, WorksForRhsInitializerList) {
6204 const vector<int> lhs{2, 4, 6};
6210 TEST(PointwiseTest, RejectsWrongSize) {
6211 const double lhs[2] = {1, 2};
6212 const int rhs[1] = {0};
6217 const int rhs2[3] = {0, 1, 2};
6221 TEST(PointwiseTest, RejectsWrongContent) {
6222 const double lhs[3] = {1, 2, 3};
6223 const int rhs[3] = {2, 6, 4};
6225 EXPECT_EQ(
"where the value pair (2, 6) at index #1 don't match, "
6226 "where the second/2 is 3",
6227 Explain(
Pointwise(IsHalfOf(), rhs), lhs));
6230 TEST(PointwiseTest, AcceptsCorrectContent) {
6231 const double lhs[3] = {1, 2, 3};
6232 const int rhs[3] = {2, 4, 6};
6237 TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) {
6238 const double lhs[3] = {1, 2, 3};
6239 const int rhs[3] = {2, 4, 6};
6240 const Matcher<std::tuple<const double&, const int&>> m1 = IsHalfOf();
6246 const Matcher<std::tuple<double, int>> m2 = IsHalfOf();
6251 MATCHER(PointeeEquals,
"Points to an equal value") {
6253 ::testing::get<0>(
arg), result_listener);
6256 TEST(PointwiseTest, WorksWithMoveOnly) {
6257 ContainerHelper helper;
6259 helper.Call(MakeUniquePtrs({1, 2}));
6262 TEST(UnorderedPointwiseTest, DescribesSelf) {
6269 "has 3 elements and there exists some permutation of elements such "
6271 " - element #0 and 1 are a pair where the first is half of the second, "
6273 " - element #1 and 2 are a pair where the first is half of the second, "
6275 " - element #2 and 3 are a pair where the first is half of the second",
6278 "doesn't have 3 elements, or there exists no permutation of elements "
6280 " - element #0 and 1 are a pair where the first is half of the second, "
6282 " - element #1 and 2 are a pair where the first is half of the second, "
6284 " - element #2 and 3 are a pair where the first is half of the second",
6285 DescribeNegation(
m));
6288 TEST(UnorderedPointwiseTest, MakesCopyOfRhs) {
6289 list<signed char> rhs;
6302 TEST(UnorderedPointwiseTest, WorksForLhsNativeArray) {
6303 const int lhs[] = {1, 2, 3};
6312 TEST(UnorderedPointwiseTest, WorksForRhsNativeArray) {
6313 const int rhs[] = {1, 2, 3};
6323 TEST(UnorderedPointwiseTest, WorksForRhsInitializerList) {
6324 const vector<int> lhs{2, 4, 6};
6330 TEST(UnorderedPointwiseTest, RejectsWrongSize) {
6331 const double lhs[2] = {1, 2};
6332 const int rhs[1] = {0};
6337 const int rhs2[3] = {0, 1, 2};
6341 TEST(UnorderedPointwiseTest, RejectsWrongContent) {
6342 const double lhs[3] = {1, 2, 3};
6343 const int rhs[3] = {2, 6, 6};
6345 EXPECT_EQ(
"where the following elements don't match any matchers:\n"
6350 TEST(UnorderedPointwiseTest, AcceptsCorrectContentInSameOrder) {
6351 const double lhs[3] = {1, 2, 3};
6352 const int rhs[3] = {2, 4, 6};
6356 TEST(UnorderedPointwiseTest, AcceptsCorrectContentInDifferentOrder) {
6357 const double lhs[3] = {1, 2, 3};
6358 const int rhs[3] = {6, 4, 2};
6362 TEST(UnorderedPointwiseTest, AllowsMonomorphicInnerMatcher) {
6363 const double lhs[3] = {1, 2, 3};
6364 const int rhs[3] = {4, 6, 2};
6365 const Matcher<std::tuple<const double&, const int&>> m1 = IsHalfOf();
6370 const Matcher<std::tuple<double, int>> m2 = IsHalfOf();
6374 TEST(UnorderedPointwiseTest, WorksWithMoveOnly) {
6375 ContainerHelper helper;
6377 std::vector<int>{1, 2})));
6378 helper.Call(MakeUniquePtrs({2, 1}));
6383 template <
typename T>
6384 class SampleOptional {
6387 explicit SampleOptional(
T value)
6398 TEST(OptionalTest, DescribesSelf) {
6399 const Matcher<SampleOptional<int>>
m =
Optional(
Eq(1));
6400 EXPECT_EQ(
"value is equal to 1", Describe(
m));
6403 TEST(OptionalTest, ExplainsSelf) {
6404 const Matcher<SampleOptional<int>>
m =
Optional(
Eq(1));
6405 EXPECT_EQ(
"whose value 1 matches", Explain(
m, SampleOptional<int>(1)));
6406 EXPECT_EQ(
"whose value 2 doesn't match", Explain(
m, SampleOptional<int>(2)));
6409 TEST(OptionalTest, MatchesNonEmptyOptional) {
6410 const Matcher<SampleOptional<int>> m1 =
Optional(1);
6411 const Matcher<SampleOptional<int>> m2 =
Optional(
Eq(2));
6412 const Matcher<SampleOptional<int>> m3 =
Optional(
Lt(3));
6413 SampleOptional<int> opt(1);
6419 TEST(OptionalTest, DoesNotMatchNullopt) {
6420 const Matcher<SampleOptional<int>>
m =
Optional(1);
6421 SampleOptional<int>
empty;
6425 TEST(OptionalTest, WorksWithMoveOnly) {
6426 Matcher<SampleOptional<std::unique_ptr<int>>>
m =
Optional(
Eq(
nullptr));
6427 EXPECT_TRUE(
m.Matches(SampleOptional<std::unique_ptr<int>>(
nullptr)));
6430 class SampleVariantIntString {
6435 template <
typename T>
6440 template <
typename T>
6441 friend const T&
get(
const SampleVariantIntString&
value) {
6442 return value.get_impl(
static_cast<T*
>(
nullptr));
6446 const int& get_impl(
int*)
const {
return i_; }
6454 TEST(VariantTest, DescribesSelf) {
6455 const Matcher<SampleVariantIntString>
m = VariantWith<int>(
Eq(1));
6457 "'.*' and the value is equal to 1"));
6460 TEST(VariantTest, ExplainsSelf) {
6461 const Matcher<SampleVariantIntString>
m = VariantWith<int>(
Eq(1));
6465 HasSubstr(
"whose value is not of type '"));
6467 "whose value 2 doesn't match");
6470 TEST(VariantTest, FullMatch) {
6471 Matcher<SampleVariantIntString>
m = VariantWith<int>(
Eq(1));
6474 m = VariantWith<std::string>(
Eq(
"1"));
6478 TEST(VariantTest, TypeDoesNotMatch) {
6479 Matcher<SampleVariantIntString>
m = VariantWith<int>(
Eq(1));
6482 m = VariantWith<std::string>(
Eq(
"1"));
6486 TEST(VariantTest, InnerDoesNotMatch) {
6487 Matcher<SampleVariantIntString>
m = VariantWith<int>(
Eq(1));
6490 m = VariantWith<std::string>(
Eq(
"1"));
6494 class SampleAnyType {
6496 explicit SampleAnyType(
int i) :
index_(0),
i_(
i) {}
6499 template <
typename T>
6500 friend const T*
any_cast(
const SampleAnyType* any) {
6501 return any->get_impl(
static_cast<T*
>(
nullptr));
6509 const int* get_impl(
int*)
const {
return index_ == 0 ? &
i_ :
nullptr; }
6511 return index_ == 1 ? &
s_ :
nullptr;
6515 TEST(AnyWithTest, FullMatch) {
6516 Matcher<SampleAnyType>
m = AnyWith<int>(
Eq(1));
6520 TEST(AnyWithTest, TestBadCastType) {
6521 Matcher<SampleAnyType>
m = AnyWith<std::string>(
Eq(
"fail"));
6525 TEST(AnyWithTest, TestUseInContainers) {
6526 std::vector<SampleAnyType>
a;
6533 std::vector<SampleAnyType>
b;
6534 b.emplace_back(
"hello");
6535 b.emplace_back(
"merhaba");
6536 b.emplace_back(
"salut");
6538 AnyWith<std::string>(
"merhaba"),
6539 AnyWith<std::string>(
"salut")}));
6541 TEST(AnyWithTest, TestCompare) {
6545 TEST(AnyWithTest, DescribesSelf) {
6546 const Matcher<const SampleAnyType&>
m = AnyWith<int>(
Eq(1));
6548 "'.*' and the value is equal to 1"));
6551 TEST(AnyWithTest, ExplainsSelf) {
6552 const Matcher<const SampleAnyType&>
m = AnyWith<int>(
Eq(1));
6556 HasSubstr(
"whose value is not of type '"));
6557 EXPECT_THAT(Explain(
m, SampleAnyType(2)),
"whose value 2 doesn't match");
6560 TEST(PointeeTest, WorksOnMoveOnlyType) {
6561 std::unique_ptr<int>
p(
new int(3));
6566 TEST(NotTest, WorksOnMoveOnlyType) {
6567 std::unique_ptr<int>
p(
new int(3));
6574 TEST(ArgsTest, AcceptsZeroTemplateArg) {
6575 const std::tuple<int, bool>
t(5,
true);
6580 TEST(ArgsTest, AcceptsOneTemplateArg) {
6581 const std::tuple<int, bool>
t(5,
true);
6587 TEST(ArgsTest, AcceptsTwoTemplateArgs) {
6588 const std::tuple<short, int, long>
t(4, 5, 6L);
6595 TEST(ArgsTest, AcceptsRepeatedTemplateArgs) {
6596 const std::tuple<short, int, long>
t(4, 5, 6L);
6601 TEST(ArgsTest, AcceptsDecreasingTemplateArgs) {
6602 const std::tuple<short, int, long>
t(4, 5, 6L);
6608 return std::get<0>(
arg) + std::get<1>(
arg) + std::get<2>(
arg) == 0;
6611 TEST(ArgsTest, AcceptsMoreTemplateArgsThanArityOfOriginalTuple) {
6616 TEST(ArgsTest, CanBeNested) {
6617 const std::tuple<short, int, long, int>
t(4, 5, 6L, 6);
6622 TEST(ArgsTest, CanMatchTupleByValue) {
6623 typedef std::tuple<char, int, int> Tuple3;
6624 const Matcher<Tuple3>
m = Args<1, 2>(
Lt());
6629 TEST(ArgsTest, CanMatchTupleByReference) {
6630 typedef std::tuple<char, char, int> Tuple3;
6631 const Matcher<const Tuple3&>
m = Args<0, 1>(
Lt());
6641 TEST(ArgsTest, AcceptsTenTemplateArgs) {
6642 EXPECT_THAT(
std::make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
6643 (Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
6644 PrintsAs(
"(9, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
6645 EXPECT_THAT(
std::make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
6646 Not(Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
6647 PrintsAs(
"(0, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
6650 TEST(ArgsTest, DescirbesSelfCorrectly) {
6651 const Matcher<std::tuple<int, bool, char> >
m = Args<2, 0>(
Lt());
6652 EXPECT_EQ(
"are a tuple whose fields (#2, #0) are a pair where "
6653 "the first < the second",
6657 TEST(ArgsTest, DescirbesNestedArgsCorrectly) {
6658 const Matcher<const std::tuple<int, bool, char, int>&>
m =
6659 Args<0, 2, 3>(Args<2, 0>(
Lt()));
6660 EXPECT_EQ(
"are a tuple whose fields (#0, #2, #3) are a tuple "
6661 "whose fields (#2, #0) are a pair where the first < the second",
6665 TEST(ArgsTest, DescribesNegationCorrectly) {
6666 const Matcher<std::tuple<int, char> >
m = Args<1, 0>(
Gt());
6667 EXPECT_EQ(
"are a tuple whose fields (#1, #0) aren't a pair "
6668 "where the first > the second",
6669 DescribeNegation(
m));
6672 TEST(ArgsTest, ExplainsMatchResultWithoutInnerExplanation) {
6673 const Matcher<std::tuple<bool, int, int> >
m = Args<1, 2>(
Eq());
6674 EXPECT_EQ(
"whose fields (#1, #2) are (42, 42)",
6676 EXPECT_EQ(
"whose fields (#1, #2) are (42, 43)",
6681 class LessThanMatcher :
public MatcherInterface<std::tuple<char, int> > {
6683 void DescribeTo(::std::ostream* )
const override {}
6685 bool MatchAndExplain(std::tuple<char, int>
value,
6686 MatchResultListener* listener)
const override {
6689 *listener <<
"where the first value is " <<
diff
6690 <<
" more than the second";
6696 Matcher<std::tuple<char, int> > LessThan() {
6700 TEST(ArgsTest, ExplainsMatchResultWithInnerExplanation) {
6701 const Matcher<std::tuple<char, int, int> >
m = Args<0, 2>(LessThan());
6703 "whose fields (#0, #2) are ('a' (97, 0x61), 42), "
6704 "where the first value is 55 more than the second",
6706 EXPECT_EQ(
"whose fields (#0, #2) are ('\\0', 43)",
6712 enum Behavior { kInitialSuccess, kAlwaysFail, kFlaky };
6717 class MockMatcher :
public MatcherInterface<Behavior> {
6719 bool MatchAndExplain(Behavior behavior,
6720 MatchResultListener* listener)
const override {
6721 *listener <<
"[MatchAndExplain]";
6723 case kInitialSuccess:
6727 return !listener->IsInterested();
6737 return listener->IsInterested();
6744 void DescribeTo(ostream* os)
const override { *os <<
"[DescribeTo]"; }
6746 void DescribeNegationTo(ostream* os)
const override {
6747 *os <<
"[DescribeNegationTo]";
6751 AssertionResult RunPredicateFormatter(Behavior behavior) {
6753 PredicateFormatterFromMatcher<Matcher<Behavior>> predicate_formatter(
6755 return predicate_formatter(
"dummy-name", behavior);
6759 TEST_F(PredicateFormatterFromMatcherTest, ShortCircuitOnSuccess) {
6760 AssertionResult
result = RunPredicateFormatter(kInitialSuccess);
6766 TEST_F(PredicateFormatterFromMatcherTest, NoShortCircuitOnFailure) {
6767 AssertionResult
result = RunPredicateFormatter(kAlwaysFail);
6770 "Value of: dummy-name\nExpected: [DescribeTo]\n"
6771 " Actual: 1, [MatchAndExplain]";
6775 TEST_F(PredicateFormatterFromMatcherTest, DetectsFlakyShortCircuit) {
6776 AssertionResult
result = RunPredicateFormatter(kFlaky);
6779 "Value of: dummy-name\nExpected: [DescribeTo]\n"
6780 " The matcher failed on the initial attempt; but passed when rerun to "
6781 "generate the explanation.\n"
6782 " Actual: 2, [MatchAndExplain]";
6791 # pragma warning(pop)