39# pragma warning(disable:4244)
40# pragma warning(disable:4100)
51#include <forward_list>
63#include <unordered_map>
64#include <unordered_set>
74namespace gmock_matchers_test {
87using std::stringstream;
89using testing::internal::DummyMatchResultListener;
90using testing::internal::ElementMatcherPair;
91using testing::internal::ElementMatcherPairs;
92using testing::internal::ElementsAreArrayMatcher;
93using testing::internal::ExplainMatchFailureTupleTo;
94using testing::internal::FloatingEqMatcher;
96using testing::internal::IsReadableTypeName;
97using testing::internal::MatchMatrix;
98using testing::internal::PredicateFormatterFromMatcher;
99using testing::internal::RE;
100using testing::internal::StreamMatchResultListener;
106struct ContainerHelper {
107 MOCK_METHOD1(Call,
void(std::vector<std::unique_ptr<int>>));
110std::vector<std::unique_ptr<int>> MakeUniquePtrs(
const std::vector<int>& ints) {
111 std::vector<std::unique_ptr<int>> pointers;
112 for (
int i : ints) pointers.emplace_back(
new int(
i));
117class GreaterThanMatcher :
public MatcherInterface<int> {
119 explicit GreaterThanMatcher(
int rhs) :
rhs_(rhs) {}
121 void DescribeTo(ostream* os)
const override { *os <<
"is > " <<
rhs_; }
123 bool MatchAndExplain(
int lhs, MatchResultListener* listener)
const override {
124 const int diff = lhs -
rhs_;
126 *listener <<
"which is " << diff <<
" more than " <<
rhs_;
127 }
else if (diff == 0) {
128 *listener <<
"which is the same as " <<
rhs_;
130 *listener <<
"which is " << -diff <<
" less than " <<
rhs_;
140Matcher<int> GreaterThan(
int n) {
141 return MakeMatcher(
new GreaterThanMatcher(n));
144std::string OfType(
const std::string& type_name) {
146 return IsReadableTypeName(type_name) ?
" (of type " + type_name +
")" :
"";
154std::string Describe(
const Matcher<T>& m) {
155 return DescribeMatcher<T>(m);
160std::string DescribeNegation(
const Matcher<T>& m) {
161 return DescribeMatcher<T>(m,
true);
165template <
typename MatcherType,
typename Value>
166std::string Explain(
const MatcherType& m,
const Value&
x) {
167 StringMatchResultListener listener;
168 ExplainMatchResult(m,
x, &listener);
169 return listener.str();
172TEST(MonotonicMatcherTest, IsPrintable) {
174 ss << GreaterThan(5);
178TEST(MatchResultListenerTest, StreamingWorks) {
179 StringMatchResultListener listener;
180 listener <<
"hi" << 5;
190 DummyMatchResultListener dummy;
194TEST(MatchResultListenerTest, CanAccessUnderlyingStream) {
195 EXPECT_TRUE(DummyMatchResultListener().stream() ==
nullptr);
196 EXPECT_TRUE(StreamMatchResultListener(
nullptr).stream() ==
nullptr);
198 EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream());
201TEST(MatchResultListenerTest, IsInterestedWorks) {
202 EXPECT_TRUE(StringMatchResultListener().IsInterested());
203 EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested());
205 EXPECT_FALSE(DummyMatchResultListener().IsInterested());
206 EXPECT_FALSE(StreamMatchResultListener(
nullptr).IsInterested());
211class EvenMatcherImpl :
public MatcherInterface<int> {
213 bool MatchAndExplain(
int x,
214 MatchResultListener* )
const override {
218 void DescribeTo(ostream* os)
const override { *os <<
"is an even number"; }
226TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) {
232class NewEvenMatcherImpl :
public MatcherInterface<int> {
234 bool MatchAndExplain(
int x, MatchResultListener* listener)
const override {
235 const bool match =
x % 2 == 0;
237 *listener <<
"value % " << 2;
238 if (listener->stream() !=
nullptr) {
241 *listener->stream() <<
" == " << (
x % 2);
246 void DescribeTo(ostream* os)
const override { *os <<
"is an even number"; }
249TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
250 Matcher<int> m = MakeMatcher(
new NewEvenMatcherImpl);
253 EXPECT_EQ(
"value % 2 == 0", Explain(m, 2));
254 EXPECT_EQ(
"value % 2 == 1", Explain(m, 3));
258TEST(MatcherTest, CanBeDefaultConstructed) {
263TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {
264 const MatcherInterface<int>* impl =
new EvenMatcherImpl;
265 Matcher<int> m(impl);
271TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) {
278TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) {
279 Matcher<int*> m1 =
nullptr;
288 virtual ~Undefined() = 0;
292TEST(MatcherTest, CanBeConstructedFromUndefinedVariable) {
293 Matcher<int> m1 = Undefined::kInt;
299TEST(MatcherTest, CanAcceptAbstractClass) { Matcher<const Undefined&> m =
_; }
302TEST(MatcherTest, IsCopyable) {
304 Matcher<bool> m1 = Eq(
false);
316TEST(MatcherTest, CanDescribeItself) {
318 Describe(Matcher<int>(
new EvenMatcherImpl)));
322TEST(MatcherTest, MatchAndExplain) {
323 Matcher<int> m = GreaterThan(0);
324 StringMatchResultListener listener1;
326 EXPECT_EQ(
"which is 42 more than 0", listener1.str());
328 StringMatchResultListener listener2;
330 EXPECT_EQ(
"which is 9 less than 0", listener2.str());
335TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
336 Matcher<std::string> m1 =
"hi";
340 Matcher<const std::string&> m2 =
"hi";
347TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
348 Matcher<std::string> m1 = std::string(
"hi");
352 Matcher<const std::string&> m2 = std::string(
"hi");
357#if GTEST_INTERNAL_HAS_STRING_VIEW
360TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
361 Matcher<internal::StringView> m1 =
"cats";
365 Matcher<const internal::StringView&> m2 =
"cats";
372TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromString) {
373 Matcher<internal::StringView> m1 = std::string(
"cats");
377 Matcher<const internal::StringView&> m2 = std::string(
"cats");
384TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromStringView) {
385 Matcher<internal::StringView> m1 = internal::StringView(
"cats");
389 Matcher<const internal::StringView&> m2 = internal::StringView(
"cats");
397TEST(StringMatcherTest,
398 CanBeImplicitlyConstructedFromEqReferenceWrapperString) {
399 std::string
value =
"cats";
400 Matcher<std::string> m1 = Eq(std::ref(
value));
404 Matcher<const std::string&> m2 = Eq(std::ref(
value));
412TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {
413 const MatcherInterface<int>* dummy_impl =
nullptr;
414 Matcher<int> m = MakeMatcher(dummy_impl);
420class ReferencesBarOrIsZeroImpl {
422 template <
typename T>
423 bool MatchAndExplain(
const T&
x,
424 MatchResultListener* )
const {
426 return p == &g_bar ||
x == 0;
429 void DescribeTo(ostream* os)
const { *os <<
"g_bar or zero"; }
431 void DescribeNegationTo(ostream* os)
const {
432 *os <<
"doesn't reference g_bar and is not zero";
438PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {
439 return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl());
442TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {
444 Matcher<const int&> m1 = ReferencesBarOrIsZero();
449 EXPECT_EQ(
"g_bar or zero", Describe(m1));
452 Matcher<double> m2 = ReferencesBarOrIsZero();
455 EXPECT_EQ(
"g_bar or zero", Describe(m2));
460class PolymorphicIsEvenImpl {
462 void DescribeTo(ostream* os)
const { *os <<
"is even"; }
464 void DescribeNegationTo(ostream* os)
const {
468 template <
typename T>
469 bool MatchAndExplain(
const T&
x, MatchResultListener* listener)
const {
471 *listener <<
"% " << 2;
472 if (listener->stream() !=
nullptr) {
475 *listener->stream() <<
" == " << (
x % 2);
481PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {
482 return MakePolymorphicMatcher(PolymorphicIsEvenImpl());
485TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
487 const Matcher<int> m1 = PolymorphicIsEven();
492 const Matcher<int> not_m1 = Not(m1);
498 const Matcher<char> m2 = PolymorphicIsEven();
503 const Matcher<char> not_m2 = Not(m2);
506 EXPECT_EQ(
"% 2 == 0", Explain(m2,
'\x42'));
510TEST(MatcherCastTest, FromPolymorphicMatcher) {
511 Matcher<int> m = MatcherCast<int>(Eq(5));
521 explicit IntValue(
int a_value) :
value_(a_value) {}
529bool IsPositiveIntValue(
const IntValue&
foo) {
530 return foo.value() > 0;
535TEST(MatcherCastTest, FromCompatibleType) {
536 Matcher<double> m1 = Eq(2.0);
537 Matcher<int> m2 = MatcherCast<int>(m1);
541 Matcher<IntValue> m3 = Truly(IsPositiveIntValue);
542 Matcher<int> m4 = MatcherCast<int>(m3);
551TEST(MatcherCastTest, FromConstReferenceToNonReference) {
552 Matcher<const int&> m1 = Eq(0);
553 Matcher<int> m2 = MatcherCast<int>(m1);
559TEST(MatcherCastTest, FromReferenceToNonReference) {
560 Matcher<int&> m1 = Eq(0);
561 Matcher<int> m2 = MatcherCast<int>(m1);
567TEST(MatcherCastTest, FromNonReferenceToConstReference) {
568 Matcher<int> m1 = Eq(0);
569 Matcher<const int&> m2 = MatcherCast<const int&>(m1);
575TEST(MatcherCastTest, FromNonReferenceToReference) {
576 Matcher<int> m1 = Eq(0);
577 Matcher<int&> m2 = MatcherCast<int&>(m1);
585TEST(MatcherCastTest, FromSameType) {
586 Matcher<int> m1 = Eq(0);
587 Matcher<int> m2 = MatcherCast<int>(m1);
594TEST(MatcherCastTest, FromAValue) {
595 Matcher<int> m = MatcherCast<int>(42);
602TEST(MatcherCastTest, FromAnImplicitlyConvertibleValue) {
603 const int kExpected =
'c';
604 Matcher<int> m = MatcherCast<int>(
'c');
609struct NonImplicitlyConstructibleTypeWithOperatorEq {
611 const NonImplicitlyConstructibleTypeWithOperatorEq& ,
617 const NonImplicitlyConstructibleTypeWithOperatorEq& ) {
625TEST(MatcherCastTest, NonImplicitlyConstructibleTypeWithOperatorEq) {
626 Matcher<NonImplicitlyConstructibleTypeWithOperatorEq> m1 =
627 MatcherCast<NonImplicitlyConstructibleTypeWithOperatorEq>(42);
628 EXPECT_TRUE(m1.Matches(NonImplicitlyConstructibleTypeWithOperatorEq()));
630 Matcher<NonImplicitlyConstructibleTypeWithOperatorEq> m2 =
631 MatcherCast<NonImplicitlyConstructibleTypeWithOperatorEq>(239);
632 EXPECT_FALSE(m2.Matches(NonImplicitlyConstructibleTypeWithOperatorEq()));
637 MatcherCast<int>(NonImplicitlyConstructibleTypeWithOperatorEq());
656namespace convertible_from_any {
658struct ConvertibleFromAny {
659 ConvertibleFromAny(
int a_value) :
value(a_value) {}
660 template <
typename T>
661 ConvertibleFromAny(
const T& ) :
value(-1) {
667bool operator==(
const ConvertibleFromAny&
a,
const ConvertibleFromAny& b) {
668 return a.value == b.value;
671ostream&
operator<<(ostream& os,
const ConvertibleFromAny&
a) {
672 return os <<
a.value;
675TEST(MatcherCastTest, ConversionConstructorIsUsed) {
676 Matcher<ConvertibleFromAny> m = MatcherCast<ConvertibleFromAny>(1);
681TEST(MatcherCastTest, FromConvertibleFromAny) {
682 Matcher<ConvertibleFromAny> m =
683 MatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
691struct IntReferenceWrapper {
692 IntReferenceWrapper(
const int& a_value) :
value(&a_value) {}
696bool operator==(
const IntReferenceWrapper&
a,
const IntReferenceWrapper& b) {
697 return a.value == b.value;
700TEST(MatcherCastTest, ValueIsNotCopied) {
702 Matcher<IntReferenceWrapper> m = MatcherCast<IntReferenceWrapper>(n);
715class Derived :
public Base {
717 Derived() : Base() {}
721class OtherDerived :
public Base {};
724TEST(SafeMatcherCastTest, FromPolymorphicMatcher) {
725 Matcher<char> m2 = SafeMatcherCast<char>(Eq(32));
733TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {
734 Matcher<double> m1 = DoubleEq(1.0);
735 Matcher<float> m2 = SafeMatcherCast<float>(m1);
739 Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>(
'a'));
746TEST(SafeMatcherCastTest, FromBaseClass) {
748 Matcher<Base*> m1 = Eq(&d);
749 Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);
753 Matcher<Base&> m3 = Ref(d);
754 Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);
760TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
762 Matcher<const int&> m1 = Ref(n);
763 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
770TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {
771 Matcher<std::unique_ptr<int>> m1 =
IsNull();
772 Matcher<const std::unique_ptr<int>&> m2 =
773 SafeMatcherCast<const std::unique_ptr<int>&>(m1);
775 EXPECT_FALSE(m2.Matches(std::unique_ptr<int>(
new int)));
779TEST(SafeMatcherCastTest, FromNonReferenceToReference) {
780 Matcher<int> m1 = Eq(0);
781 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
789TEST(SafeMatcherCastTest, FromSameType) {
790 Matcher<int> m1 = Eq(0);
791 Matcher<int> m2 = SafeMatcherCast<int>(m1);
798namespace convertible_from_any {
799TEST(SafeMatcherCastTest, ConversionConstructorIsUsed) {
800 Matcher<ConvertibleFromAny> m = SafeMatcherCast<ConvertibleFromAny>(1);
805TEST(SafeMatcherCastTest, FromConvertibleFromAny) {
806 Matcher<ConvertibleFromAny> m =
807 SafeMatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
815TEST(SafeMatcherCastTest, ValueIsNotCopied) {
817 Matcher<IntReferenceWrapper> m = SafeMatcherCast<IntReferenceWrapper>(n);
822TEST(ExpectThat, TakesLiterals) {
828TEST(ExpectThat, TakesFunctions) {
830 static void Func() {}
832 void (*
func)() = Helper::Func;
838TEST(ATest, MatchesAnyValue) {
840 Matcher<double> m1 = A<double>();
847 Matcher<int&> m2 = A<int&>();
852TEST(ATest, WorksForDerivedClass) {
862TEST(ATest, CanDescribeSelf) {
863 EXPECT_EQ(
"is anything", Describe(A<bool>()));
867TEST(AnTest, MatchesAnyValue) {
869 Matcher<int> m1 = An<int>();
876 Matcher<int&> m2 = An<int&>();
882TEST(AnTest, CanDescribeSelf) {
883 EXPECT_EQ(
"is anything", Describe(An<int>()));
888TEST(UnderscoreTest, MatchesAnyValue) {
897 Matcher<const bool&> m2 =
_;
903TEST(UnderscoreTest, CanDescribeSelf) {
909TEST(EqTest, MatchesEqualValue) {
911 const char a1[] =
"hi";
912 const char a2[] =
"hi";
914 Matcher<const char*> m1 = Eq(a1);
923 Unprintable() :
c_(
'a') {}
925 bool operator==(
const Unprintable& )
const {
return true; }
927 char dummy_c() {
return c_; }
932TEST(EqTest, CanDescribeSelf) {
933 Matcher<Unprintable> m = Eq(Unprintable());
934 EXPECT_EQ(
"is equal to 1-byte object <61>", Describe(m));
939TEST(EqTest, IsPolymorphic) {
940 Matcher<int> m1 = Eq(1);
944 Matcher<char> m2 = Eq(1);
950TEST(TypedEqTest, ChecksEqualityForGivenType) {
951 Matcher<char> m1 = TypedEq<char>(
'a');
955 Matcher<int> m2 = TypedEq<int>(6);
961TEST(TypedEqTest, CanDescribeSelf) {
962 EXPECT_EQ(
"is equal to 2", Describe(TypedEq<int>(2)));
972 static bool IsTypeOf(
const T& ) {
return true; }
974 template <
typename T2>
975 static void IsTypeOf(
T2 v);
978TEST(TypedEqTest, HasSpecifiedType) {
980 Type<Matcher<int> >::IsTypeOf(TypedEq<int>(5));
981 Type<Matcher<double> >::IsTypeOf(TypedEq<double>(5));
985TEST(GeTest, ImplementsGreaterThanOrEqual) {
986 Matcher<int> m1 = Ge(0);
993TEST(GeTest, CanDescribeSelf) {
994 Matcher<int> m = Ge(5);
999TEST(GtTest, ImplementsGreaterThan) {
1000 Matcher<double> m1 = Gt(0);
1007TEST(GtTest, CanDescribeSelf) {
1008 Matcher<int> m = Gt(5);
1013TEST(LeTest, ImplementsLessThanOrEqual) {
1014 Matcher<char> m1 = Le(
'b');
1021TEST(LeTest, CanDescribeSelf) {
1022 Matcher<int> m = Le(5);
1027TEST(LtTest, ImplementsLessThan) {
1028 Matcher<const std::string&> m1 = Lt(
"Hello");
1035TEST(LtTest, CanDescribeSelf) {
1036 Matcher<int> m = Lt(5);
1041TEST(NeTest, ImplementsNotEqual) {
1042 Matcher<int> m1 = Ne(0);
1049TEST(NeTest, CanDescribeSelf) {
1050 Matcher<int> m = Ne(5);
1051 EXPECT_EQ(
"isn't equal to 5", Describe(m));
1056 explicit MoveOnly(
int i) :
i_(
i) {}
1057 MoveOnly(
const MoveOnly&) =
delete;
1058 MoveOnly(MoveOnly&&) =
default;
1059 MoveOnly& operator=(
const MoveOnly&) =
delete;
1060 MoveOnly& operator=(MoveOnly&&) =
default;
1062 bool operator==(
const MoveOnly& other)
const {
return i_ == other.i_; }
1063 bool operator!=(
const MoveOnly& other)
const {
return i_ != other.i_; }
1064 bool operator<(
const MoveOnly& other)
const {
return i_ < other.i_; }
1065 bool operator<=(
const MoveOnly& other)
const {
return i_ <= other.i_; }
1066 bool operator>(
const MoveOnly& other)
const {
return i_ > other.i_; }
1067 bool operator>=(
const MoveOnly& other)
const {
return i_ >= other.i_; }
1077TEST(ComparisonBaseTest, WorksWithMoveOnly) {
1082 helper.Call(MoveOnly(0));
1084 helper.Call(MoveOnly(1));
1086 helper.Call(MoveOnly(0));
1088 helper.Call(MoveOnly(-1));
1090 helper.Call(MoveOnly(0));
1092 helper.Call(MoveOnly(1));
1096TEST(IsNullTest, MatchesNullPointer) {
1097 Matcher<int*> m1 =
IsNull();
1103 Matcher<const char*> m2 =
IsNull();
1104 const char* p2 =
nullptr;
1108 Matcher<void*> m3 =
IsNull();
1111 EXPECT_FALSE(m3.Matches(
reinterpret_cast<void*
>(0xbeef)));
1114TEST(IsNullTest, StdFunction) {
1115 const Matcher<std::function<void()>> m =
IsNull();
1122TEST(IsNullTest, CanDescribeSelf) {
1123 Matcher<int*> m =
IsNull();
1125 EXPECT_EQ(
"isn't NULL", DescribeNegation(m));
1129TEST(NotNullTest, MatchesNonNullPointer) {
1130 Matcher<int*> m1 = NotNull();
1136 Matcher<const char*> m2 = NotNull();
1137 const char* p2 =
nullptr;
1142TEST(NotNullTest, LinkedPtr) {
1143 const Matcher<std::shared_ptr<int>> m = NotNull();
1144 const std::shared_ptr<int> null_p;
1145 const std::shared_ptr<int> non_null_p(
new int);
1151TEST(NotNullTest, ReferenceToConstLinkedPtr) {
1152 const Matcher<const std::shared_ptr<double>&> m = NotNull();
1153 const std::shared_ptr<double> null_p;
1154 const std::shared_ptr<double> non_null_p(
new double);
1160TEST(NotNullTest, StdFunction) {
1161 const Matcher<std::function<void()>> m = NotNull();
1168TEST(NotNullTest, CanDescribeSelf) {
1169 Matcher<int*> m = NotNull();
1175TEST(RefTest, MatchesSameVariable) {
1178 Matcher<int&> m = Ref(
a);
1184TEST(RefTest, CanDescribeSelf) {
1186 Matcher<int&> m = Ref(n);
1188 ss <<
"references the variable @" << &n <<
" 5";
1194TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
1197 Matcher<const int&> m = Ref(
a);
1206TEST(RefTest, IsCovariant) {
1209 Matcher<const Base&> m1 = Ref(base);
1220TEST(RefTest, ExplainsResult) {
1222 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n),
1223 StartsWith(
"which is located @"));
1226 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m),
1227 StartsWith(
"which is located @"));
1232template <
typename T = std::
string>
1233std::string FromStringLike(internal::StringLike<T> str) {
1234 return std::string(str);
1237TEST(StringLike, TestConversions) {
1238 EXPECT_EQ(
"foo", FromStringLike(
"foo"));
1239 EXPECT_EQ(
"foo", FromStringLike(std::string(
"foo")));
1240#if GTEST_INTERNAL_HAS_STRING_VIEW
1241 EXPECT_EQ(
"foo", FromStringLike(internal::StringView(
"foo")));
1246 EXPECT_EQ(
"foo", FromStringLike({
'f',
'o',
'o'}));
1247 const char buf[] =
"foo";
1248 EXPECT_EQ(
"foo", FromStringLike({buf, buf + 3}));
1251TEST(StrEqTest, MatchesEqualString) {
1252 Matcher<const char*> m = StrEq(std::string(
"Hello"));
1257 Matcher<const std::string&> m2 = StrEq(
"Hello");
1261#if GTEST_INTERNAL_HAS_STRING_VIEW
1262 Matcher<const internal::StringView&> m3 =
1263 StrEq(internal::StringView(
"Hello"));
1264 EXPECT_TRUE(m3.Matches(internal::StringView(
"Hello")));
1265 EXPECT_FALSE(m3.Matches(internal::StringView(
"hello")));
1268 Matcher<const internal::StringView&> m_empty = StrEq(
"");
1269 EXPECT_TRUE(m_empty.Matches(internal::StringView(
"")));
1270 EXPECT_TRUE(m_empty.Matches(internal::StringView()));
1271 EXPECT_FALSE(m_empty.Matches(internal::StringView(
"hello")));
1275TEST(StrEqTest, CanDescribeSelf) {
1276 Matcher<std::string> m = StrEq(
"Hi-\'\"?\\\a\b\f\n\r\t\v\xD3");
1277 EXPECT_EQ(
"is equal to \"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
1280 std::string str(
"01204500800");
1282 Matcher<std::string> m2 = StrEq(str);
1283 EXPECT_EQ(
"is equal to \"012\\04500800\"", Describe(m2));
1284 str[0] = str[6] = str[7] = str[9] = str[10] =
'\0';
1285 Matcher<std::string> m3 = StrEq(str);
1286 EXPECT_EQ(
"is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));
1289TEST(StrNeTest, MatchesUnequalString) {
1290 Matcher<const char*> m = StrNe(
"Hello");
1295 Matcher<std::string> m2 = StrNe(std::string(
"Hello"));
1299#if GTEST_INTERNAL_HAS_STRING_VIEW
1300 Matcher<const internal::StringView> m3 = StrNe(internal::StringView(
"Hello"));
1301 EXPECT_TRUE(m3.Matches(internal::StringView(
"")));
1303 EXPECT_FALSE(m3.Matches(internal::StringView(
"Hello")));
1307TEST(StrNeTest, CanDescribeSelf) {
1308 Matcher<const char*> m = StrNe(
"Hi");
1309 EXPECT_EQ(
"isn't equal to \"Hi\"", Describe(m));
1312TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
1313 Matcher<const char*> m = StrCaseEq(std::string(
"Hello"));
1319 Matcher<const std::string&> m2 = StrCaseEq(
"Hello");
1323#if GTEST_INTERNAL_HAS_STRING_VIEW
1324 Matcher<const internal::StringView&> m3 =
1325 StrCaseEq(internal::StringView(
"Hello"));
1326 EXPECT_TRUE(m3.Matches(internal::StringView(
"Hello")));
1327 EXPECT_TRUE(m3.Matches(internal::StringView(
"hello")));
1333TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1334 std::string str1(
"oabocdooeoo");
1335 std::string str2(
"OABOCDOOEOO");
1336 Matcher<const std::string&> m0 = StrCaseEq(str1);
1339 str1[3] = str2[3] =
'\0';
1340 Matcher<const std::string&> m1 = StrCaseEq(str1);
1343 str1[0] = str1[6] = str1[7] = str1[10] =
'\0';
1344 str2[0] = str2[6] = str2[7] = str2[10] =
'\0';
1345 Matcher<const std::string&> m2 = StrCaseEq(str1);
1346 str1[9] = str2[9] =
'\0';
1349 Matcher<const std::string&> m3 = StrCaseEq(str1);
1353 str2.append(1,
'\0');
1358TEST(StrCaseEqTest, CanDescribeSelf) {
1359 Matcher<std::string> m = StrCaseEq(
"Hi");
1360 EXPECT_EQ(
"is equal to (ignoring case) \"Hi\"", Describe(m));
1363TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1364 Matcher<const char*> m = StrCaseNe(
"Hello");
1370 Matcher<std::string> m2 = StrCaseNe(std::string(
"Hello"));
1374#if GTEST_INTERNAL_HAS_STRING_VIEW
1375 Matcher<const internal::StringView> m3 =
1376 StrCaseNe(internal::StringView(
"Hello"));
1377 EXPECT_TRUE(m3.Matches(internal::StringView(
"Hi")));
1379 EXPECT_FALSE(m3.Matches(internal::StringView(
"Hello")));
1380 EXPECT_FALSE(m3.Matches(internal::StringView(
"hello")));
1384TEST(StrCaseNeTest, CanDescribeSelf) {
1385 Matcher<const char*> m = StrCaseNe(
"Hi");
1386 EXPECT_EQ(
"isn't equal to (ignoring case) \"Hi\"", Describe(m));
1390TEST(HasSubstrTest, WorksForStringClasses) {
1391 const Matcher<std::string> m1 = HasSubstr(
"foo");
1392 EXPECT_TRUE(m1.Matches(std::string(
"I love food.")));
1395 const Matcher<const std::string&> m2 = HasSubstr(
"foo");
1396 EXPECT_TRUE(m2.Matches(std::string(
"I love food.")));
1399 const Matcher<std::string> m_empty = HasSubstr(
"");
1401 EXPECT_TRUE(m_empty.Matches(std::string(
"not empty")));
1405TEST(HasSubstrTest, WorksForCStrings) {
1406 const Matcher<char*> m1 = HasSubstr(
"foo");
1407 EXPECT_TRUE(m1.Matches(
const_cast<char*
>(
"I love food.")));
1411 const Matcher<const char*> m2 = HasSubstr(
"foo");
1416 const Matcher<const char*> m_empty = HasSubstr(
"");
1422#if GTEST_INTERNAL_HAS_STRING_VIEW
1424TEST(HasSubstrTest, WorksForStringViewClasses) {
1425 const Matcher<internal::StringView> m1 =
1426 HasSubstr(internal::StringView(
"foo"));
1427 EXPECT_TRUE(m1.Matches(internal::StringView(
"I love food.")));
1428 EXPECT_FALSE(m1.Matches(internal::StringView(
"tofo")));
1431 const Matcher<const internal::StringView&> m2 = HasSubstr(
"foo");
1432 EXPECT_TRUE(m2.Matches(internal::StringView(
"I love food.")));
1433 EXPECT_FALSE(m2.Matches(internal::StringView(
"tofo")));
1436 const Matcher<const internal::StringView&> m3 = HasSubstr(
"");
1437 EXPECT_TRUE(m3.Matches(internal::StringView(
"foo")));
1438 EXPECT_TRUE(m3.Matches(internal::StringView(
"")));
1444TEST(HasSubstrTest, CanDescribeSelf) {
1445 Matcher<std::string> m = HasSubstr(
"foo\n\"");
1446 EXPECT_EQ(
"has substring \"foo\\n\\\"\"", Describe(m));
1449TEST(KeyTest, CanDescribeSelf) {
1450 Matcher<const pair<std::string, int>&> m = Key(
"foo");
1451 EXPECT_EQ(
"has a key that is equal to \"foo\"", Describe(m));
1452 EXPECT_EQ(
"doesn't have a key that is equal to \"foo\"", DescribeNegation(m));
1455TEST(KeyTest, ExplainsResult) {
1456 Matcher<pair<int, bool> > m = Key(GreaterThan(10));
1457 EXPECT_EQ(
"whose first field is a value which is 5 less than 10",
1458 Explain(m, make_pair(5,
true)));
1459 EXPECT_EQ(
"whose first field is a value which is 5 more than 10",
1460 Explain(m, make_pair(15,
true)));
1463TEST(KeyTest, MatchesCorrectly) {
1464 pair<int, std::string>
p(25,
"foo");
1471TEST(KeyTest, WorksWithMoveOnly) {
1472 pair<std::unique_ptr<int>, std::unique_ptr<int>>
p;
1482 using first_type = int;
1483 using second_type = std::string;
1485 const int& GetImpl(Tag<0>)
const {
return member_1; }
1486 const std::string& GetImpl(Tag<1>)
const {
return member_2; }
1489auto get(
const PairWithGet&
value) ->
decltype(
value.GetImpl(Tag<I>())) {
1490 return value.GetImpl(Tag<I>());
1492TEST(PairTest, MatchesPairWithGetCorrectly) {
1493 PairWithGet
p{25,
"foo"};
1499 std::vector<PairWithGet> v = {{11,
"Foo"}, {29,
"gMockIsBestMock"}};
1503TEST(KeyTest, SafelyCastsInnerMatcher) {
1504 Matcher<int> is_positive = Gt(0);
1505 Matcher<int> is_negative = Lt(0);
1506 pair<char, bool>
p(
'a',
true);
1511TEST(KeyTest, InsideContainsUsingMap) {
1520TEST(KeyTest, InsideContainsUsingMultimap) {
1536TEST(PairTest, Typing) {
1538 Matcher<const pair<const char*, int>&> m1 = Pair(
"foo", 42);
1539 Matcher<const pair<const char*, int> > m2 = Pair(
"foo", 42);
1540 Matcher<pair<const char*, int> > m3 = Pair(
"foo", 42);
1542 Matcher<pair<int, const std::string> > m4 = Pair(25,
"42");
1543 Matcher<pair<const std::string, int> > m5 = Pair(
"25", 42);
1546TEST(PairTest, CanDescribeSelf) {
1547 Matcher<const pair<std::string, int>&> m1 = Pair(
"foo", 42);
1548 EXPECT_EQ(
"has a first field that is equal to \"foo\""
1549 ", and has a second field that is equal to 42",
1551 EXPECT_EQ(
"has a first field that isn't equal to \"foo\""
1552 ", or has a second field that isn't equal to 42",
1553 DescribeNegation(m1));
1555 Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42));
1556 EXPECT_EQ(
"has a first field that isn't equal to 13"
1557 ", and has a second field that is equal to 42",
1558 DescribeNegation(m2));
1561TEST(PairTest, CanExplainMatchResultTo) {
1564 const Matcher<pair<int, int> > m = Pair(GreaterThan(0), GreaterThan(0));
1565 EXPECT_EQ(
"whose first field does not match, which is 1 less than 0",
1566 Explain(m, make_pair(-1, -2)));
1570 EXPECT_EQ(
"whose second field does not match, which is 2 less than 0",
1571 Explain(m, make_pair(1, -2)));
1575 EXPECT_EQ(
"whose first field does not match, which is 1 less than 0",
1576 Explain(m, make_pair(-1, 2)));
1579 EXPECT_EQ(
"whose both fields match, where the first field is a value "
1580 "which is 1 more than 0, and the second field is a value "
1581 "which is 2 more than 0",
1582 Explain(m, make_pair(1, 2)));
1586 const Matcher<pair<int, int> > explain_first = Pair(GreaterThan(0), 0);
1587 EXPECT_EQ(
"whose both fields match, where the first field is a value "
1588 "which is 1 more than 0",
1589 Explain(explain_first, make_pair(1, 0)));
1593 const Matcher<pair<int, int> > explain_second = Pair(0, GreaterThan(0));
1594 EXPECT_EQ(
"whose both fields match, where the second field is a value "
1595 "which is 1 more than 0",
1596 Explain(explain_second, make_pair(0, 1)));
1599TEST(PairTest, MatchesCorrectly) {
1600 pair<int, std::string>
p(25,
"foo");
1619TEST(PairTest, WorksWithMoveOnly) {
1620 pair<std::unique_ptr<int>, std::unique_ptr<int>>
p;
1621 p.second.reset(
new int(7));
1625TEST(PairTest, SafelyCastsInnerMatchers) {
1626 Matcher<int> is_positive = Gt(0);
1627 Matcher<int> is_negative = Lt(0);
1628 pair<char, bool>
p(
'a',
true);
1635TEST(PairTest, InsideContainsUsingMap) {
1646TEST(ContainsTest, WorksWithMoveOnly) {
1647 ContainerHelper helper;
1649 helper.Call(MakeUniquePtrs({1, 2}));
1652TEST(PairTest, UseGetInsteadOfMembers) {
1653 PairWithGet pair{7,
"ABC"};
1658 std::vector<PairWithGet> v = {{11,
"Foo"}, {29,
"gMockIsBestMock"}};
1660 ElementsAre(Pair(11, std::string(
"Foo")), Pair(Ge(10), Not(
""))));
1665TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
1666 const Matcher<const char*> m1 = StartsWith(std::string(
""));
1671 const Matcher<const std::string&> m2 = StartsWith(
"Hi");
1678#if GTEST_INTERNAL_HAS_STRING_VIEW
1679 const Matcher<internal::StringView> m_empty =
1680 StartsWith(internal::StringView(
""));
1681 EXPECT_TRUE(m_empty.Matches(internal::StringView()));
1682 EXPECT_TRUE(m_empty.Matches(internal::StringView(
"")));
1683 EXPECT_TRUE(m_empty.Matches(internal::StringView(
"not empty")));
1687TEST(StartsWithTest, CanDescribeSelf) {
1688 Matcher<const std::string> m = StartsWith(
"Hi");
1689 EXPECT_EQ(
"starts with \"Hi\"", Describe(m));
1694TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
1695 const Matcher<const char*> m1 = EndsWith(
"");
1700 const Matcher<const std::string&> m2 = EndsWith(std::string(
"Hi"));
1707#if GTEST_INTERNAL_HAS_STRING_VIEW
1708 const Matcher<const internal::StringView&> m4 =
1709 EndsWith(internal::StringView(
""));
1713 EXPECT_TRUE(m4.Matches(internal::StringView(
"")));
1717TEST(EndsWithTest, CanDescribeSelf) {
1718 Matcher<const std::string> m = EndsWith(
"Hi");
1719 EXPECT_EQ(
"ends with \"Hi\"", Describe(m));
1724TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
1725 const Matcher<const char*> m1 = MatchesRegex(
"a.*z");
1730 const Matcher<const std::string&> m2 = MatchesRegex(
new RE(
"a.*z"));
1735#if GTEST_INTERNAL_HAS_STRING_VIEW
1736 const Matcher<const internal::StringView&> m3 = MatchesRegex(
"a.*z");
1737 EXPECT_TRUE(m3.Matches(internal::StringView(
"az")));
1738 EXPECT_TRUE(m3.Matches(internal::StringView(
"abcz")));
1741 const Matcher<const internal::StringView&> m4 =
1742 MatchesRegex(internal::StringView(
""));
1743 EXPECT_TRUE(m4.Matches(internal::StringView(
"")));
1748TEST(MatchesRegexTest, CanDescribeSelf) {
1749 Matcher<const std::string> m1 = MatchesRegex(std::string(
"Hi.*"));
1750 EXPECT_EQ(
"matches regular expression \"Hi.*\"", Describe(m1));
1752 Matcher<const char*> m2 = MatchesRegex(
new RE(
"a.*"));
1753 EXPECT_EQ(
"matches regular expression \"a.*\"", Describe(m2));
1755#if GTEST_INTERNAL_HAS_STRING_VIEW
1756 Matcher<const internal::StringView> m3 = MatchesRegex(
new RE(
"0.*"));
1757 EXPECT_EQ(
"matches regular expression \"0.*\"", Describe(m3));
1763TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
1764 const Matcher<const char*> m1 = ContainsRegex(std::string(
"a.*z"));
1769 const Matcher<const std::string&> m2 = ContainsRegex(
new RE(
"a.*z"));
1774#if GTEST_INTERNAL_HAS_STRING_VIEW
1775 const Matcher<const internal::StringView&> m3 =
1776 ContainsRegex(
new RE(
"a.*z"));
1777 EXPECT_TRUE(m3.Matches(internal::StringView(
"azbz")));
1778 EXPECT_TRUE(m3.Matches(internal::StringView(
"az1")));
1781 const Matcher<const internal::StringView&> m4 =
1782 ContainsRegex(internal::StringView(
""));
1783 EXPECT_TRUE(m4.Matches(internal::StringView(
"")));
1788TEST(ContainsRegexTest, CanDescribeSelf) {
1789 Matcher<const std::string> m1 = ContainsRegex(
"Hi.*");
1790 EXPECT_EQ(
"contains regular expression \"Hi.*\"", Describe(m1));
1792 Matcher<const char*> m2 = ContainsRegex(
new RE(
"a.*"));
1793 EXPECT_EQ(
"contains regular expression \"a.*\"", Describe(m2));
1795#if GTEST_INTERNAL_HAS_STRING_VIEW
1796 Matcher<const internal::StringView> m3 = ContainsRegex(
new RE(
"0.*"));
1797 EXPECT_EQ(
"contains regular expression \"0.*\"", Describe(m3));
1802#if GTEST_HAS_STD_WSTRING
1803TEST(StdWideStrEqTest, MatchesEqual) {
1804 Matcher<const wchar_t*> m = StrEq(::std::wstring(L
"Hello"));
1809 Matcher<const ::std::wstring&> m2 = StrEq(L
"Hello");
1813 Matcher<const ::std::wstring&> m3 = StrEq(L
"\xD3\x576\x8D3\xC74D");
1817 ::std::wstring str(L
"01204500800");
1819 Matcher<const ::std::wstring&> m4 = StrEq(str);
1821 str[0] = str[6] = str[7] = str[9] = str[10] = L
'\0';
1822 Matcher<const ::std::wstring&> m5 = StrEq(str);
1826TEST(StdWideStrEqTest, CanDescribeSelf) {
1827 Matcher< ::std::wstring> m = StrEq(L
"Hi-\'\"?\\\a\b\f\n\r\t\v");
1828 EXPECT_EQ(
"is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
1831 Matcher< ::std::wstring> m2 = StrEq(L
"\xD3\x576\x8D3\xC74D");
1832 EXPECT_EQ(
"is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
1835 ::std::wstring str(L
"01204500800");
1837 Matcher<const ::std::wstring&> m4 = StrEq(str);
1838 EXPECT_EQ(
"is equal to L\"012\\04500800\"", Describe(m4));
1839 str[0] = str[6] = str[7] = str[9] = str[10] = L
'\0';
1840 Matcher<const ::std::wstring&> m5 = StrEq(str);
1841 EXPECT_EQ(
"is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
1844TEST(StdWideStrNeTest, MatchesUnequalString) {
1845 Matcher<const wchar_t*> m = StrNe(L
"Hello");
1850 Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L
"Hello"));
1855TEST(StdWideStrNeTest, CanDescribeSelf) {
1856 Matcher<const wchar_t*> m = StrNe(L
"Hi");
1857 EXPECT_EQ(
"isn't equal to L\"Hi\"", Describe(m));
1860TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
1861 Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L
"Hello"));
1867 Matcher<const ::std::wstring&> m2 = StrCaseEq(L
"Hello");
1872TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1873 ::std::wstring str1(L
"oabocdooeoo");
1874 ::std::wstring str2(L
"OABOCDOOEOO");
1875 Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);
1876 EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L
'\0')));
1878 str1[3] = str2[3] = L
'\0';
1879 Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);
1882 str1[0] = str1[6] = str1[7] = str1[10] = L
'\0';
1883 str2[0] = str2[6] = str2[7] = str2[10] = L
'\0';
1884 Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);
1885 str1[9] = str2[9] = L
'\0';
1888 Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);
1892 str2.append(1, L
'\0');
1897TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
1898 Matcher< ::std::wstring> m = StrCaseEq(L
"Hi");
1899 EXPECT_EQ(
"is equal to (ignoring case) L\"Hi\"", Describe(m));
1902TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1903 Matcher<const wchar_t*> m = StrCaseNe(L
"Hello");
1909 Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L
"Hello"));
1914TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
1915 Matcher<const wchar_t*> m = StrCaseNe(L
"Hi");
1916 EXPECT_EQ(
"isn't equal to (ignoring case) L\"Hi\"", Describe(m));
1920TEST(StdWideHasSubstrTest, WorksForStringClasses) {
1921 const Matcher< ::std::wstring> m1 = HasSubstr(L
"foo");
1922 EXPECT_TRUE(m1.Matches(::std::wstring(L
"I love food.")));
1925 const Matcher<const ::std::wstring&> m2 = HasSubstr(L
"foo");
1926 EXPECT_TRUE(m2.Matches(::std::wstring(L
"I love food.")));
1931TEST(StdWideHasSubstrTest, WorksForCStrings) {
1932 const Matcher<wchar_t*> m1 = HasSubstr(L
"foo");
1933 EXPECT_TRUE(m1.Matches(
const_cast<wchar_t*
>(L
"I love food.")));
1934 EXPECT_FALSE(m1.Matches(
const_cast<wchar_t*
>(L
"tofo")));
1937 const Matcher<const wchar_t*> m2 = HasSubstr(L
"foo");
1944TEST(StdWideHasSubstrTest, CanDescribeSelf) {
1945 Matcher< ::std::wstring> m = HasSubstr(L
"foo\n\"");
1946 EXPECT_EQ(
"has substring L\"foo\\n\\\"\"", Describe(m));
1951TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {
1952 const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L
""));
1957 const Matcher<const ::std::wstring&> m2 = StartsWith(L
"Hi");
1965TEST(StdWideStartsWithTest, CanDescribeSelf) {
1966 Matcher<const ::std::wstring> m = StartsWith(L
"Hi");
1967 EXPECT_EQ(
"starts with L\"Hi\"", Describe(m));
1972TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {
1973 const Matcher<const wchar_t*> m1 = EndsWith(L
"");
1978 const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L
"Hi"));
1986TEST(StdWideEndsWithTest, CanDescribeSelf) {
1987 Matcher<const ::std::wstring> m = EndsWith(L
"Hi");
1988 EXPECT_EQ(
"ends with L\"Hi\"", Describe(m));
1993typedef ::std::tuple<long, int> Tuple2;
1997TEST(Eq2Test, MatchesEqualArguments) {
1998 Matcher<const Tuple2&> m = Eq();
2004TEST(Eq2Test, CanDescribeSelf) {
2005 Matcher<const Tuple2&> m = Eq();
2006 EXPECT_EQ(
"are an equal pair", Describe(m));
2011TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) {
2012 Matcher<const Tuple2&> m = Ge();
2019TEST(Ge2Test, CanDescribeSelf) {
2020 Matcher<const Tuple2&> m = Ge();
2021 EXPECT_EQ(
"are a pair where the first >= the second", Describe(m));
2026TEST(Gt2Test, MatchesGreaterThanArguments) {
2027 Matcher<const Tuple2&> m = Gt();
2034TEST(Gt2Test, CanDescribeSelf) {
2035 Matcher<const Tuple2&> m = Gt();
2036 EXPECT_EQ(
"are a pair where the first > the second", Describe(m));
2041TEST(Le2Test, MatchesLessThanOrEqualArguments) {
2042 Matcher<const Tuple2&> m = Le();
2049TEST(Le2Test, CanDescribeSelf) {
2050 Matcher<const Tuple2&> m = Le();
2051 EXPECT_EQ(
"are a pair where the first <= the second", Describe(m));
2056TEST(Lt2Test, MatchesLessThanArguments) {
2057 Matcher<const Tuple2&> m = Lt();
2064TEST(Lt2Test, CanDescribeSelf) {
2065 Matcher<const Tuple2&> m = Lt();
2066 EXPECT_EQ(
"are a pair where the first < the second", Describe(m));
2071TEST(Ne2Test, MatchesUnequalArguments) {
2072 Matcher<const Tuple2&> m = Ne();
2079TEST(Ne2Test, CanDescribeSelf) {
2080 Matcher<const Tuple2&> m = Ne();
2081 EXPECT_EQ(
"are an unequal pair", Describe(m));
2084TEST(PairMatchBaseTest, WorksWithMoveOnly) {
2085 using Pointers = std::tuple<std::unique_ptr<int>, std::unique_ptr<int>>;
2086 Matcher<Pointers> matcher = Eq();
2094TEST(IsNan, FloatMatchesNan) {
2095 float quiet_nan = std::numeric_limits<float>::quiet_NaN();
2096 float other_nan = std::nanf(
"1");
2097 float real_value = 1.0f;
2099 Matcher<float> m = IsNan();
2104 Matcher<float&> m_ref = IsNan();
2109 Matcher<const float&> m_cref = IsNan();
2116TEST(IsNan, DoubleMatchesNan) {
2117 double quiet_nan = std::numeric_limits<double>::quiet_NaN();
2118 double other_nan = std::nan(
"1");
2119 double real_value = 1.0;
2121 Matcher<double> m = IsNan();
2126 Matcher<double&> m_ref = IsNan();
2131 Matcher<const double&> m_cref = IsNan();
2138TEST(IsNan, LongDoubleMatchesNan) {
2139 long double quiet_nan = std::numeric_limits<long double>::quiet_NaN();
2140 long double other_nan = std::nan(
"1");
2141 long double real_value = 1.0;
2143 Matcher<long double> m = IsNan();
2148 Matcher<long double&> m_ref = IsNan();
2153 Matcher<const long double&> m_cref = IsNan();
2160TEST(IsNan, NotMatchesNan) {
2161 Matcher<float> mf = Not(IsNan());
2162 EXPECT_FALSE(mf.Matches(std::numeric_limits<float>::quiet_NaN()));
2166 Matcher<double> md = Not(IsNan());
2167 EXPECT_FALSE(md.Matches(std::numeric_limits<double>::quiet_NaN()));
2171 Matcher<long double> mld = Not(IsNan());
2172 EXPECT_FALSE(mld.Matches(std::numeric_limits<long double>::quiet_NaN()));
2178TEST(IsNan, CanDescribeSelf) {
2179 Matcher<float> mf = IsNan();
2182 Matcher<double> md = IsNan();
2185 Matcher<long double> mld = IsNan();
2190TEST(IsNan, CanDescribeSelfWithNot) {
2191 Matcher<float> mf = Not(IsNan());
2194 Matcher<double> md = Not(IsNan());
2197 Matcher<long double> mld = Not(IsNan());
2203TEST(FloatEq2Test, MatchesEqualArguments) {
2204 typedef ::std::tuple<float, float> Tpl;
2205 Matcher<const Tpl&> m = FloatEq();
2207 EXPECT_TRUE(m.Matches(Tpl(0.3f, 0.1f + 0.1f + 0.1f)));
2212TEST(FloatEq2Test, CanDescribeSelf) {
2213 Matcher<const ::std::tuple<float, float>&> m = FloatEq();
2214 EXPECT_EQ(
"are an almost-equal pair", Describe(m));
2219TEST(NanSensitiveFloatEqTest, MatchesEqualArgumentsWithNaN) {
2220 typedef ::std::tuple<float, float> Tpl;
2221 Matcher<const Tpl&> m = NanSensitiveFloatEq();
2223 EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(),
2224 std::numeric_limits<float>::quiet_NaN())));
2226 EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<float>::quiet_NaN())));
2227 EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), 1.0f)));
2231TEST(NanSensitiveFloatEqTest, CanDescribeSelfWithNaNs) {
2232 Matcher<const ::std::tuple<float, float>&> m = NanSensitiveFloatEq();
2233 EXPECT_EQ(
"are an almost-equal pair", Describe(m));
2238TEST(DoubleEq2Test, MatchesEqualArguments) {
2239 typedef ::std::tuple<double, double> Tpl;
2240 Matcher<const Tpl&> m = DoubleEq();
2242 EXPECT_TRUE(m.Matches(Tpl(0.3, 0.1 + 0.1 + 0.1)));
2247TEST(DoubleEq2Test, CanDescribeSelf) {
2248 Matcher<const ::std::tuple<double, double>&> m = DoubleEq();
2249 EXPECT_EQ(
"are an almost-equal pair", Describe(m));
2254TEST(NanSensitiveDoubleEqTest, MatchesEqualArgumentsWithNaN) {
2255 typedef ::std::tuple<double, double> Tpl;
2256 Matcher<const Tpl&> m = NanSensitiveDoubleEq();
2258 EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(),
2259 std::numeric_limits<double>::quiet_NaN())));
2261 EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<double>::quiet_NaN())));
2262 EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), 1.0f)));
2266TEST(NanSensitiveDoubleEqTest, CanDescribeSelfWithNaNs) {
2267 Matcher<const ::std::tuple<double, double>&> m = NanSensitiveDoubleEq();
2268 EXPECT_EQ(
"are an almost-equal pair", Describe(m));
2273TEST(FloatNear2Test, MatchesEqualArguments) {
2274 typedef ::std::tuple<float, float> Tpl;
2275 Matcher<const Tpl&> m = FloatNear(0.5f);
2282TEST(FloatNear2Test, CanDescribeSelf) {
2283 Matcher<const ::std::tuple<float, float>&> m = FloatNear(0.5f);
2284 EXPECT_EQ(
"are an almost-equal pair", Describe(m));
2289TEST(NanSensitiveFloatNearTest, MatchesNearbyArgumentsWithNaN) {
2290 typedef ::std::tuple<float, float> Tpl;
2291 Matcher<const Tpl&> m = NanSensitiveFloatNear(0.5f);
2294 EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(),
2295 std::numeric_limits<float>::quiet_NaN())));
2297 EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<float>::quiet_NaN())));
2298 EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), 1.0f)));
2302TEST(NanSensitiveFloatNearTest, CanDescribeSelfWithNaNs) {
2303 Matcher<const ::std::tuple<float, float>&> m = NanSensitiveFloatNear(0.5f);
2304 EXPECT_EQ(
"are an almost-equal pair", Describe(m));
2309TEST(DoubleNear2Test, MatchesEqualArguments) {
2310 typedef ::std::tuple<double, double> Tpl;
2311 Matcher<const Tpl&> m = DoubleNear(0.5);
2318TEST(DoubleNear2Test, CanDescribeSelf) {
2319 Matcher<const ::std::tuple<double, double>&> m = DoubleNear(0.5);
2320 EXPECT_EQ(
"are an almost-equal pair", Describe(m));
2325TEST(NanSensitiveDoubleNearTest, MatchesNearbyArgumentsWithNaN) {
2326 typedef ::std::tuple<double, double> Tpl;
2327 Matcher<const Tpl&> m = NanSensitiveDoubleNear(0.5f);
2330 EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(),
2331 std::numeric_limits<double>::quiet_NaN())));
2333 EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<double>::quiet_NaN())));
2334 EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), 1.0f)));
2338TEST(NanSensitiveDoubleNearTest, CanDescribeSelfWithNaNs) {
2339 Matcher<const ::std::tuple<double, double>&> m = NanSensitiveDoubleNear(0.5f);
2340 EXPECT_EQ(
"are an almost-equal pair", Describe(m));
2344TEST(NotTest, NegatesMatcher) {
2352TEST(NotTest, CanDescribeSelf) {
2353 Matcher<int> m = Not(Eq(5));
2354 EXPECT_EQ(
"isn't equal to 5", Describe(m));
2358TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
2360 Matcher<int> greater_than_5 = Gt(5);
2362 Matcher<const int&> m = Not(greater_than_5);
2363 Matcher<int&> m2 = Not(greater_than_5);
2364 Matcher<int&> m3 = Not(m);
2368void AllOfMatches(
int num,
const Matcher<int>& m) {
2371 for (
int i = 1;
i <= num; ++
i) {
2379TEST(AllOfTest, MatchesWhenAllMatch) {
2381 m = AllOf(Le(2), Ge(1));
2387 m = AllOf(Gt(0), Ne(1), Ne(2));
2393 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
2400 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
2409 AllOfMatches(2, AllOf(Ne(1), Ne(2)));
2410 AllOfMatches(3, AllOf(Ne(1), Ne(2), Ne(3)));
2411 AllOfMatches(4, AllOf(Ne(1), Ne(2), Ne(3), Ne(4)));
2412 AllOfMatches(5, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5)));
2413 AllOfMatches(6, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6)));
2414 AllOfMatches(7, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7)));
2415 AllOfMatches(8, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
2417 AllOfMatches(9, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
2419 AllOfMatches(10, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8),
2422 50, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9),
2423 Ne(10), Ne(11), Ne(12), Ne(13), Ne(14), Ne(15), Ne(16), Ne(17),
2424 Ne(18), Ne(19), Ne(20), Ne(21), Ne(22), Ne(23), Ne(24), Ne(25),
2425 Ne(26), Ne(27), Ne(28), Ne(29), Ne(30), Ne(31), Ne(32), Ne(33),
2426 Ne(34), Ne(35), Ne(36), Ne(37), Ne(38), Ne(39), Ne(40), Ne(41),
2427 Ne(42), Ne(43), Ne(44), Ne(45), Ne(46), Ne(47), Ne(48), Ne(49),
2433TEST(AllOfTest, CanDescribeSelf) {
2435 m = AllOf(Le(2), Ge(1));
2436 EXPECT_EQ(
"(is <= 2) and (is >= 1)", Describe(m));
2438 m = AllOf(Gt(0), Ne(1), Ne(2));
2439 std::string expected_descr1 =
2440 "(is > 0) and (isn't equal to 1) and (isn't equal to 2)";
2441 EXPECT_EQ(expected_descr1, Describe(m));
2443 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
2444 std::string expected_descr2 =
2445 "(is > 0) and (isn't equal to 1) and (isn't equal to 2) and (isn't equal "
2447 EXPECT_EQ(expected_descr2, Describe(m));
2449 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
2450 std::string expected_descr3 =
2451 "(is >= 0) and (is < 10) and (isn't equal to 3) and (isn't equal to 5) "
2452 "and (isn't equal to 7)";
2453 EXPECT_EQ(expected_descr3, Describe(m));
2457TEST(AllOfTest, CanDescribeNegation) {
2459 m = AllOf(Le(2), Ge(1));
2460 std::string expected_descr4 =
"(isn't <= 2) or (isn't >= 1)";
2461 EXPECT_EQ(expected_descr4, DescribeNegation(m));
2463 m = AllOf(Gt(0), Ne(1), Ne(2));
2464 std::string expected_descr5 =
2465 "(isn't > 0) or (is equal to 1) or (is equal to 2)";
2466 EXPECT_EQ(expected_descr5, DescribeNegation(m));
2468 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
2469 std::string expected_descr6 =
2470 "(isn't > 0) or (is equal to 1) or (is equal to 2) or (is equal to 3)";
2471 EXPECT_EQ(expected_descr6, DescribeNegation(m));
2473 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
2474 std::string expected_desr7 =
2475 "(isn't >= 0) or (isn't < 10) or (is equal to 3) or (is equal to 5) or "
2477 EXPECT_EQ(expected_desr7, DescribeNegation(m));
2479 m = AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9),
2481 AllOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
2482 EXPECT_THAT(Describe(m), EndsWith(
"and (isn't equal to 11)"));
2483 AllOfMatches(11, m);
2487TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
2489 Matcher<int> greater_than_5 = Gt(5);
2490 Matcher<int> less_than_10 = Lt(10);
2492 Matcher<const int&> m = AllOf(greater_than_5, less_than_10);
2493 Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);
2494 Matcher<int&> m3 = AllOf(greater_than_5, m2);
2497 Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);
2498 Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
2501TEST(AllOfTest, ExplainsResult) {
2507 m = AllOf(GreaterThan(10), Lt(30));
2508 EXPECT_EQ(
"which is 15 more than 10", Explain(m, 25));
2511 m = AllOf(GreaterThan(10), GreaterThan(20));
2512 EXPECT_EQ(
"which is 20 more than 10, and which is 10 more than 20",
2517 m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20));
2518 EXPECT_EQ(
"which is 15 more than 10, and which is 5 more than 20",
2522 m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2523 EXPECT_EQ(
"which is 30 more than 10, and which is 20 more than 20, "
2524 "and which is 10 more than 30",
2529 m = AllOf(GreaterThan(10), GreaterThan(20));
2530 EXPECT_EQ(
"which is 5 less than 10", Explain(m, 5));
2535 m = AllOf(GreaterThan(10), Lt(30));
2540 m = AllOf(GreaterThan(10), GreaterThan(20));
2541 EXPECT_EQ(
"which is 5 less than 20", Explain(m, 15));
2545static void AnyOfMatches(
int num,
const Matcher<int>& m) {
2548 for (
int i = 1;
i <= num; ++
i) {
2554static void AnyOfStringMatches(
int num,
const Matcher<std::string>& m) {
2558 for (
int i = 1;
i <= num; ++
i) {
2566TEST(AnyOfTest, MatchesWhenAnyMatches) {
2568 m = AnyOf(Le(1), Ge(3));
2573 m = AnyOf(Lt(0), Eq(1), Eq(2));
2579 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2586 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2596 AnyOfMatches(2, AnyOf(1, 2));
2597 AnyOfMatches(3, AnyOf(1, 2, 3));
2598 AnyOfMatches(4, AnyOf(1, 2, 3, 4));
2599 AnyOfMatches(5, AnyOf(1, 2, 3, 4, 5));
2600 AnyOfMatches(6, AnyOf(1, 2, 3, 4, 5, 6));
2601 AnyOfMatches(7, AnyOf(1, 2, 3, 4, 5, 6, 7));
2602 AnyOfMatches(8, AnyOf(1, 2, 3, 4, 5, 6, 7, 8));
2603 AnyOfMatches(9, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9));
2604 AnyOfMatches(10, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
2608TEST(AnyOfTest, VariadicMatchesWhenAnyMatches) {
2611 Matcher<int> m = ::testing::AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
2613 EXPECT_THAT(Describe(m), EndsWith(
"or (is equal to 11)"));
2614 AnyOfMatches(11, m);
2615 AnyOfMatches(50, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
2616 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
2617 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
2618 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
2619 41, 42, 43, 44, 45, 46, 47, 48, 49, 50));
2621 50, AnyOf(
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
2622 "13",
"14",
"15",
"16",
"17",
"18",
"19",
"20",
"21",
"22",
2623 "23",
"24",
"25",
"26",
"27",
"28",
"29",
"30",
"31",
"32",
2624 "33",
"34",
"35",
"36",
"37",
"38",
"39",
"40",
"41",
"42",
2625 "43",
"44",
"45",
"46",
"47",
"48",
"49",
"50"));
2629TEST(ElementsAreTest, HugeMatcher) {
2630 vector<int> test_vector{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
2633 ElementsAre(Eq(1), Eq(2), Lt(13), Eq(4), Eq(5), Eq(6), Eq(7),
2634 Eq(8), Eq(9), Eq(10), Gt(1), Eq(12)));
2638TEST(ElementsAreTest, HugeMatcherStr) {
2639 vector<std::string> test_vector{
2640 "literal_string",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
""};
2642 EXPECT_THAT(test_vector, UnorderedElementsAre(
"literal_string", _, _, _, _, _,
2647TEST(ElementsAreTest, HugeMatcherUnordered) {
2648 vector<int> test_vector{2, 1, 8, 5, 4, 6, 7, 3, 9, 12, 11, 10};
2651 Eq(2), Eq(1), Gt(7), Eq(5), Eq(4), Eq(6), Eq(7),
2652 Eq(3), Eq(9), Eq(12), Eq(11), Ne(122)));
2657TEST(AnyOfTest, CanDescribeSelf) {
2659 m = AnyOf(Le(1), Ge(3));
2664 m = AnyOf(Lt(0), Eq(1), Eq(2));
2665 EXPECT_EQ(
"(is < 0) or (is equal to 1) or (is equal to 2)", Describe(m));
2667 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2668 EXPECT_EQ(
"(is < 0) or (is equal to 1) or (is equal to 2) or (is equal to 3)",
2671 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2673 "(is <= 0) or (is > 10) or (is equal to 3) or (is equal to 5) or (is "
2679TEST(AnyOfTest, CanDescribeNegation) {
2681 m = AnyOf(Le(1), Ge(3));
2682 EXPECT_EQ(
"(isn't <= 1) and (isn't >= 3)",
2683 DescribeNegation(m));
2685 m = AnyOf(Lt(0), Eq(1), Eq(2));
2686 EXPECT_EQ(
"(isn't < 0) and (isn't equal to 1) and (isn't equal to 2)",
2687 DescribeNegation(m));
2689 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2691 "(isn't < 0) and (isn't equal to 1) and (isn't equal to 2) and (isn't "
2693 DescribeNegation(m));
2695 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2697 "(isn't <= 0) and (isn't > 10) and (isn't equal to 3) and (isn't equal "
2698 "to 5) and (isn't equal to 7)",
2699 DescribeNegation(m));
2703TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
2705 Matcher<int> greater_than_5 = Gt(5);
2706 Matcher<int> less_than_10 = Lt(10);
2708 Matcher<const int&> m = AnyOf(greater_than_5, less_than_10);
2709 Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);
2710 Matcher<int&> m3 = AnyOf(greater_than_5, m2);
2713 Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);
2714 Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
2717TEST(AnyOfTest, ExplainsResult) {
2723 m = AnyOf(GreaterThan(10), Lt(0));
2724 EXPECT_EQ(
"which is 5 less than 10", Explain(m, 5));
2727 m = AnyOf(GreaterThan(10), GreaterThan(20));
2728 EXPECT_EQ(
"which is 5 less than 10, and which is 15 less than 20",
2733 m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30));
2734 EXPECT_EQ(
"which is 5 less than 10, and which is 25 less than 30",
2738 m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2739 EXPECT_EQ(
"which is 5 less than 10, and which is 15 less than 20, "
2740 "and which is 25 less than 30",
2745 m = AnyOf(GreaterThan(10), GreaterThan(20));
2746 EXPECT_EQ(
"which is 5 more than 10", Explain(m, 15));
2751 m = AnyOf(GreaterThan(10), Lt(30));
2756 m = AnyOf(GreaterThan(30), GreaterThan(20));
2757 EXPECT_EQ(
"which is 5 more than 20", Explain(m, 25));
2767int IsPositive(
double x) {
2768 return x > 0 ? 1 : 0;
2773class IsGreaterThan {
2775 explicit IsGreaterThan(
int threshold) :
threshold_(threshold) {}
2777 bool operator()(
int n)
const {
return n >
threshold_; }
2788bool ReferencesFooAndIsZero(
const int& n) {
2789 return (&n == &
foo) && (n == 0);
2794TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
2795 Matcher<double> m = Truly(IsPositive);
2801TEST(TrulyTest, CanBeUsedWithFunctor) {
2802 Matcher<int> m = Truly(IsGreaterThan(5));
2808class ConvertibleToBool {
2810 explicit ConvertibleToBool(
int number) :
number_(number) {}
2811 operator bool()
const {
return number_ != 0; }
2817ConvertibleToBool IsNotZero(
int number) {
2818 return ConvertibleToBool(number);
2824TEST(TrulyTest, PredicateCanReturnAClassConvertibleToBool) {
2825 Matcher<int> m = Truly(IsNotZero);
2831TEST(TrulyTest, CanDescribeSelf) {
2832 Matcher<double> m = Truly(IsPositive);
2833 EXPECT_EQ(
"satisfies the given predicate",
2839TEST(TrulyTest, WorksForByRefArguments) {
2840 Matcher<const int&> m = Truly(ReferencesFooAndIsZero);
2848TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
2855TEST(MatchesTest, WorksOnByRefArguments) {
2863TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
2864 Matcher<int> eq5 = Eq(5);
2872TEST(ValueTest, WorksWithPolymorphicMatcher) {
2877TEST(ValueTest, WorksWithMonomorphicMatcher) {
2878 const Matcher<int> is_zero = Eq(0);
2883 const Matcher<const int&> ref_n = Ref(n);
2888TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {
2889 StringMatchResultListener listener1;
2890 EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1));
2893 StringMatchResultListener listener2;
2894 EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2));
2898TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {
2899 const Matcher<int> is_even = PolymorphicIsEven();
2900 StringMatchResultListener listener1;
2901 EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1));
2904 const Matcher<const double&> is_zero = Eq(0);
2905 StringMatchResultListener listener2;
2906 EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2));
2910MATCHER(ConstructNoArg,
"") {
return true; }
2911MATCHER_P(Construct1Arg, arg1,
"") {
return true; }
2912MATCHER_P2(Construct2Args, arg1, arg2,
"") {
return true; }
2914TEST(MatcherConstruct, ExplicitVsImplicit) {
2917 ConstructNoArgMatcher m = {};
2920 ConstructNoArgMatcher m2;
2926 using M = Construct1ArgMatcherP<int>;
2927 EXPECT_TRUE((std::is_constructible<M, int>::value));
2932 Construct2ArgsMatcherP2<int, double> m = {1, 2.2};
2938 return ExplainMatchResult(inner_matcher, arg, result_listener);
2941TEST(ExplainMatchResultTest, WorksInsideMATCHER) {
2945TEST(DescribeMatcherTest, WorksWithValue) {
2946 EXPECT_EQ(
"is equal to 42", DescribeMatcher<int>(42));
2947 EXPECT_EQ(
"isn't equal to 42", DescribeMatcher<int>(42,
true));
2950TEST(DescribeMatcherTest, WorksWithMonomorphicMatcher) {
2951 const Matcher<int> monomorphic = Le(0);
2952 EXPECT_EQ(
"is <= 0", DescribeMatcher<int>(monomorphic));
2953 EXPECT_EQ(
"isn't <= 0", DescribeMatcher<int>(monomorphic,
true));
2956TEST(DescribeMatcherTest, WorksWithPolymorphicMatcher) {
2957 EXPECT_EQ(
"is even", DescribeMatcher<int>(PolymorphicIsEven()));
2958 EXPECT_EQ(
"is odd", DescribeMatcher<int>(PolymorphicIsEven(),
true));
2961TEST(AllArgsTest, WorksForTuple) {
2962 EXPECT_THAT(std::make_tuple(1, 2L), AllArgs(Lt()));
2963 EXPECT_THAT(std::make_tuple(2L, 1), Not(AllArgs(Lt())));
2966TEST(AllArgsTest, WorksForNonTuple) {
2971class AllArgsHelper {
2981TEST(AllArgsTest, WorksInWithClause) {
2982 AllArgsHelper helper;
2984 .With(AllArgs(Lt()))
2985 .WillByDefault(
Return(1));
2988 .With(AllArgs(Gt()))
2995class OptionalMatchersHelper {
2997 OptionalMatchersHelper() {}
3012TEST(AllArgsTest, WorksWithoutMatchers) {
3013 OptionalMatchersHelper helper;
3035TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
3038 EXPECT_THAT(2, AllOf(Le(7), Ge(0))) <<
"This should succeed too.";
3044TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
3047 static unsigned short n;
3052 "Expected: is > 10\n"
3053 " Actual: 5" + OfType(
"unsigned short"));
3058 "Expected: (is <= 7) and (is >= 5)\n"
3059 " Actual: 0" + OfType(
"unsigned short"));
3064TEST(MatcherAssertionTest, WorksForByRefArguments) {
3072 "Expected: does not reference the variable @");
3075 "Actual: 0" + OfType(
"int") +
", which is located @");
3080TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
3081 Matcher<const char*> starts_with_he = StartsWith(
"he");
3084 Matcher<const std::string&> ends_with_ok = EndsWith(
"ok");
3086 const std::string bad =
"bad";
3089 "Expected: ends with \"ok\"\n"
3090 " Actual: \"bad\"");
3091 Matcher<int> is_greater_than_5 = Gt(5);
3094 "Expected: is > 5\n"
3095 " Actual: 5" + OfType(
"int"));
3099template <
typename RawType>
3123 max_(Floating::Max()),
3124 nan1_(Floating::ReinterpretBits(Floating::kExponentBitMask | 1)),
3125 nan2_(Floating::ReinterpretBits(Floating::kExponentBitMask | 200)) {
3129 EXPECT_EQ(
sizeof(RawType),
sizeof(Bits));
3135 testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {
3136 Matcher<RawType> m1 = matcher_maker(0.0);
3142 Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_);
3145 Matcher<RawType> m3 = matcher_maker(1.0);
3152 Matcher<RawType> m4 = matcher_maker(-infinity_);
3155 Matcher<RawType> m5 = matcher_maker(infinity_);
3164 Matcher<const RawType&> m6 = matcher_maker(0.0);
3171 Matcher<RawType&> m7 = matcher_maker(0.0);
3209template <
typename RawType>
3210class FloatingPointNearTest :
public FloatingPointTest<RawType> {
3212 typedef FloatingPointTest<RawType> ParentType;
3216 void TestNearMatches(
3217 testing::internal::FloatingEqMatcher<RawType>
3218 (*matcher_maker)(RawType, RawType)) {
3219 Matcher<RawType> m1 = matcher_maker(0.0, 0.0);
3222 EXPECT_FALSE(m1.Matches(ParentType::close_to_positive_zero_));
3223 EXPECT_FALSE(m1.Matches(ParentType::close_to_negative_zero_));
3226 Matcher<RawType> m2 = matcher_maker(0.0, 1.0);
3236 Matcher<RawType> m3 = matcher_maker(ParentType::infinity_, 0.0);
3238 EXPECT_FALSE(m3.Matches(ParentType::close_to_infinity_));
3241 Matcher<RawType> m4 = matcher_maker(-ParentType::infinity_, 0.0);
3243 EXPECT_FALSE(m4.Matches(-ParentType::close_to_infinity_));
3247 Matcher<RawType> m5 = matcher_maker(ParentType::max_, ParentType::max_);
3251 Matcher<RawType> m6 = matcher_maker(-ParentType::max_, ParentType::max_);
3255 Matcher<RawType> m7 = matcher_maker(ParentType::max_, 0);
3259 Matcher<RawType> m8 = matcher_maker(-ParentType::max_, 0);
3265 Matcher<RawType> m9 = matcher_maker(
3266 ParentType::max_, ParentType::infinity_);
3271 Matcher<const RawType&> m10 = matcher_maker(0.0, 1.0);
3273 EXPECT_TRUE(m10.Matches(ParentType::close_to_positive_zero_));
3278 Matcher<RawType&> m11 = matcher_maker(0.0, 1.0);
3293typedef FloatingPointTest<float> FloatTest;
3295TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) {
3296 TestMatches(&FloatEq);
3299TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
3300 TestMatches(&NanSensitiveFloatEq);
3303TEST_F(FloatTest, FloatEqCannotMatchNaN) {
3305 Matcher<float> m = FloatEq(
nan1_);
3311TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
3313 Matcher<float> m = NanSensitiveFloatEq(
nan1_);
3319TEST_F(FloatTest, FloatEqCanDescribeSelf) {
3320 Matcher<float> m1 = FloatEq(2.0f);
3321 EXPECT_EQ(
"is approximately 2", Describe(m1));
3322 EXPECT_EQ(
"isn't approximately 2", DescribeNegation(m1));
3324 Matcher<float> m2 = FloatEq(0.5f);
3325 EXPECT_EQ(
"is approximately 0.5", Describe(m2));
3326 EXPECT_EQ(
"isn't approximately 0.5", DescribeNegation(m2));
3328 Matcher<float> m3 = FloatEq(
nan1_);
3329 EXPECT_EQ(
"never matches", Describe(m3));
3330 EXPECT_EQ(
"is anything", DescribeNegation(m3));
3333TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
3334 Matcher<float> m1 = NanSensitiveFloatEq(2.0f);
3335 EXPECT_EQ(
"is approximately 2", Describe(m1));
3336 EXPECT_EQ(
"isn't approximately 2", DescribeNegation(m1));
3338 Matcher<float> m2 = NanSensitiveFloatEq(0.5f);
3339 EXPECT_EQ(
"is approximately 0.5", Describe(m2));
3340 EXPECT_EQ(
"isn't approximately 0.5", DescribeNegation(m2));
3342 Matcher<float> m3 = NanSensitiveFloatEq(
nan1_);
3344 EXPECT_EQ(
"isn't NaN", DescribeNegation(m3));
3349typedef FloatingPointNearTest<float> FloatNearTest;
3351TEST_F(FloatNearTest, FloatNearMatches) {
3352 TestNearMatches(&FloatNear);
3355TEST_F(FloatNearTest, NanSensitiveFloatNearApproximatelyMatchesFloats) {
3356 TestNearMatches(&NanSensitiveFloatNear);
3359TEST_F(FloatNearTest, FloatNearCanDescribeSelf) {
3360 Matcher<float> m1 = FloatNear(2.0f, 0.5f);
3361 EXPECT_EQ(
"is approximately 2 (absolute error <= 0.5)", Describe(m1));
3363 "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
3365 Matcher<float> m2 = FloatNear(0.5f, 0.5f);
3366 EXPECT_EQ(
"is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
3368 "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
3370 Matcher<float> m3 = FloatNear(
nan1_, 0.0);
3371 EXPECT_EQ(
"never matches", Describe(m3));
3372 EXPECT_EQ(
"is anything", DescribeNegation(m3));
3375TEST_F(FloatNearTest, NanSensitiveFloatNearCanDescribeSelf) {
3376 Matcher<float> m1 = NanSensitiveFloatNear(2.0f, 0.5f);
3377 EXPECT_EQ(
"is approximately 2 (absolute error <= 0.5)", Describe(m1));
3379 "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
3381 Matcher<float> m2 = NanSensitiveFloatNear(0.5f, 0.5f);
3382 EXPECT_EQ(
"is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
3384 "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
3386 Matcher<float> m3 = NanSensitiveFloatNear(
nan1_, 0.1f);
3388 EXPECT_EQ(
"isn't NaN", DescribeNegation(m3));
3391TEST_F(FloatNearTest, FloatNearCannotMatchNaN) {
3393 Matcher<float> m = FloatNear(ParentType::nan1_, 0.1f);
3399TEST_F(FloatNearTest, NanSensitiveFloatNearCanMatchNaN) {
3401 Matcher<float> m = NanSensitiveFloatNear(
nan1_, 0.1f);
3408typedef FloatingPointTest<double> DoubleTest;
3410TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
3411 TestMatches(&DoubleEq);
3414TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
3415 TestMatches(&NanSensitiveDoubleEq);
3418TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
3420 Matcher<double> m = DoubleEq(
nan1_);
3426TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
3428 Matcher<double> m = NanSensitiveDoubleEq(
nan1_);
3434TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
3435 Matcher<double> m1 = DoubleEq(2.0);
3436 EXPECT_EQ(
"is approximately 2", Describe(m1));
3437 EXPECT_EQ(
"isn't approximately 2", DescribeNegation(m1));
3439 Matcher<double> m2 = DoubleEq(0.5);
3440 EXPECT_EQ(
"is approximately 0.5", Describe(m2));
3441 EXPECT_EQ(
"isn't approximately 0.5", DescribeNegation(m2));
3443 Matcher<double> m3 = DoubleEq(
nan1_);
3444 EXPECT_EQ(
"never matches", Describe(m3));
3445 EXPECT_EQ(
"is anything", DescribeNegation(m3));
3448TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
3449 Matcher<double> m1 = NanSensitiveDoubleEq(2.0);
3450 EXPECT_EQ(
"is approximately 2", Describe(m1));
3451 EXPECT_EQ(
"isn't approximately 2", DescribeNegation(m1));
3453 Matcher<double> m2 = NanSensitiveDoubleEq(0.5);
3454 EXPECT_EQ(
"is approximately 0.5", Describe(m2));
3455 EXPECT_EQ(
"isn't approximately 0.5", DescribeNegation(m2));
3457 Matcher<double> m3 = NanSensitiveDoubleEq(
nan1_);
3459 EXPECT_EQ(
"isn't NaN", DescribeNegation(m3));
3464typedef FloatingPointNearTest<double> DoubleNearTest;
3466TEST_F(DoubleNearTest, DoubleNearMatches) {
3467 TestNearMatches(&DoubleNear);
3470TEST_F(DoubleNearTest, NanSensitiveDoubleNearApproximatelyMatchesDoubles) {
3471 TestNearMatches(&NanSensitiveDoubleNear);
3474TEST_F(DoubleNearTest, DoubleNearCanDescribeSelf) {
3475 Matcher<double> m1 = DoubleNear(2.0, 0.5);
3476 EXPECT_EQ(
"is approximately 2 (absolute error <= 0.5)", Describe(m1));
3478 "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
3480 Matcher<double> m2 = DoubleNear(0.5, 0.5);
3481 EXPECT_EQ(
"is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
3483 "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
3485 Matcher<double> m3 = DoubleNear(
nan1_, 0.0);
3486 EXPECT_EQ(
"never matches", Describe(m3));
3487 EXPECT_EQ(
"is anything", DescribeNegation(m3));
3490TEST_F(DoubleNearTest, ExplainsResultWhenMatchFails) {
3491 EXPECT_EQ(
"", Explain(DoubleNear(2.0, 0.1), 2.05));
3492 EXPECT_EQ(
"which is 0.2 from 2", Explain(DoubleNear(2.0, 0.1), 2.2));
3493 EXPECT_EQ(
"which is -0.3 from 2", Explain(DoubleNear(2.0, 0.1), 1.7));
3495 const std::string explanation =
3496 Explain(DoubleNear(2.1, 1e-10), 2.1 + 1.2e-10);
3499 EXPECT_TRUE(explanation ==
"which is 1.2e-10 from 2.1" ||
3500 explanation ==
"which is 1.2e-010 from 2.1")
3501 <<
" where explanation is \"" << explanation <<
"\".";
3504TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanDescribeSelf) {
3505 Matcher<double> m1 = NanSensitiveDoubleNear(2.0, 0.5);
3506 EXPECT_EQ(
"is approximately 2 (absolute error <= 0.5)", Describe(m1));
3508 "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
3510 Matcher<double> m2 = NanSensitiveDoubleNear(0.5, 0.5);
3511 EXPECT_EQ(
"is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
3513 "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
3515 Matcher<double> m3 = NanSensitiveDoubleNear(
nan1_, 0.1);
3517 EXPECT_EQ(
"isn't NaN", DescribeNegation(m3));
3520TEST_F(DoubleNearTest, DoubleNearCannotMatchNaN) {
3522 Matcher<double> m = DoubleNear(ParentType::nan1_, 0.1);
3528TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanMatchNaN) {
3530 Matcher<double> m = NanSensitiveDoubleNear(
nan1_, 0.1);
3536TEST(PointeeTest, RawPointer) {
3537 const Matcher<int*> m = Pointee(Ge(0));
3546TEST(PointeeTest, RawPointerToConst) {
3547 const Matcher<const double*> m = Pointee(Ge(0));
3556TEST(PointeeTest, ReferenceToConstRawPointer) {
3557 const Matcher<int* const &> m = Pointee(Ge(0));
3566TEST(PointeeTest, ReferenceToNonConstRawPointer) {
3567 const Matcher<double* &> m = Pointee(Ge(0));
3579 return ExplainMatchResult(inner_matcher, arg.i, result_listener);
3583TEST(WhenDynamicCastToTest, SameType) {
3588 Base* as_base_ptr = &derived;
3590 EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(Pointee(FieldIIs(4))));
3592 Not(WhenDynamicCastTo<Derived*>(Pointee(FieldIIs(5)))));
3595TEST(WhenDynamicCastToTest, WrongTypes) {
3598 OtherDerived other_derived;
3601 EXPECT_THAT(&base, Not(WhenDynamicCastTo<Derived*>(Pointee(_))));
3603 Base* as_base_ptr = &derived;
3604 EXPECT_THAT(as_base_ptr, Not(WhenDynamicCastTo<OtherDerived*>(Pointee(_))));
3606 as_base_ptr = &other_derived;
3607 EXPECT_THAT(as_base_ptr, Not(WhenDynamicCastTo<Derived*>(Pointee(_))));
3611TEST(WhenDynamicCastToTest, AlreadyNull) {
3613 Base* as_base_ptr =
nullptr;
3617struct AmbiguousCastTypes {
3618 class VirtualDerived :
public virtual Base {};
3619 class DerivedSub1 :
public VirtualDerived {};
3620 class DerivedSub2 :
public VirtualDerived {};
3621 class ManyDerivedInHierarchy :
public DerivedSub1,
public DerivedSub2 {};
3624TEST(WhenDynamicCastToTest, AmbiguousCast) {
3625 AmbiguousCastTypes::DerivedSub1 sub1;
3626 AmbiguousCastTypes::ManyDerivedInHierarchy many_derived;
3629 static_cast<AmbiguousCastTypes::DerivedSub1*
>(&many_derived);
3631 WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(
IsNull()));
3632 as_base_ptr = &sub1;
3635 WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(Not(
IsNull())));
3638TEST(WhenDynamicCastToTest, Describe) {
3639 Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(Pointee(_));
3640 const std::string prefix =
3641 "when dynamic_cast to " + internal::GetTypeName<Derived*>() +
", ";
3642 EXPECT_EQ(prefix +
"points to a value that is anything", Describe(matcher));
3643 EXPECT_EQ(prefix +
"does not point to a value that is anything",
3644 DescribeNegation(matcher));
3647TEST(WhenDynamicCastToTest, Explain) {
3648 Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(Pointee(_));
3649 Base* null =
nullptr;
3650 EXPECT_THAT(Explain(matcher, null), HasSubstr(
"NULL"));
3653 EXPECT_THAT(Explain(matcher, &derived), HasSubstr(
"which points to "));
3656 Matcher<const Base&> ref_matcher = WhenDynamicCastTo<const OtherDerived&>(_);
3658 HasSubstr(
"which cannot be dynamic_cast"));
3661TEST(WhenDynamicCastToTest, GoodReference) {
3664 Base& as_base_ref = derived;
3665 EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(FieldIIs(4)));
3666 EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(Not(FieldIIs(5))));
3669TEST(WhenDynamicCastToTest, BadReference) {
3671 Base& as_base_ref = derived;
3672 EXPECT_THAT(as_base_ref, Not(WhenDynamicCastTo<const OtherDerived&>(_)));
3677template <
typename T>
3678class ConstPropagatingPtr {
3680 typedef T element_type;
3682 ConstPropagatingPtr() :
val_() {}
3683 explicit ConstPropagatingPtr(
T* t) :
val_(t) {}
3684 ConstPropagatingPtr(
const ConstPropagatingPtr& other) :
val_(other.
val_) {}
3686 T* get() {
return val_; }
3689 const T* get()
const {
return val_; }
3696TEST(PointeeTest, WorksWithConstPropagatingPointers) {
3697 const Matcher< ConstPropagatingPtr<int> > m = Pointee(Lt(5));
3699 const ConstPropagatingPtr<int> co(&three);
3700 ConstPropagatingPtr<int> o(&three);
3708TEST(PointeeTest, NeverMatchesNull) {
3709 const Matcher<const char*> m = Pointee(_);
3714TEST(PointeeTest, MatchesAgainstAValue) {
3715 const Matcher<int*> m = Pointee(5);
3724TEST(PointeeTest, CanDescribeSelf) {
3725 const Matcher<int*> m = Pointee(Gt(3));
3726 EXPECT_EQ(
"points to a value that is > 3", Describe(m));
3727 EXPECT_EQ(
"does not point to a value that is > 3",
3728 DescribeNegation(m));
3731TEST(PointeeTest, CanExplainMatchResult) {
3732 const Matcher<const std::string*> m = Pointee(StartsWith(
"Hi"));
3734 EXPECT_EQ(
"", Explain(m,
static_cast<const std::string*
>(
nullptr)));
3736 const Matcher<long*> m2 = Pointee(GreaterThan(1));
3738 EXPECT_EQ(
"which points to 3" + OfType(
"long") +
", which is 2 more than 1",
3742TEST(PointeeTest, AlwaysExplainsPointee) {
3743 const Matcher<int*> m = Pointee(0);
3745 EXPECT_EQ(
"which points to 42" + OfType(
"int"), Explain(m, &n));
3751 Uncopyable() :
value_(-1) {}
3752 explicit Uncopyable(
int a_value) :
value_(a_value) {}
3755 void set_value(
int i) {
value_ =
i; }
3763bool ValueIsPositive(
const Uncopyable&
x) {
return x.value() > 0; }
3765MATCHER_P(UncopyableIs, inner_matcher,
"") {
3766 return ExplainMatchResult(inner_matcher, arg.value(), result_listener);
3771 AStruct() :
x(0),
y(1.0),
z(5),
p(nullptr) {}
3772 AStruct(
const AStruct& rhs)
3782struct DerivedStruct :
public AStruct {
3787TEST(FieldTest, WorksForNonConstField) {
3788 Matcher<AStruct> m = Field(&AStruct::x, Ge(0));
3789 Matcher<AStruct> m_with_name = Field(
"x", &AStruct::x, Ge(0));
3800TEST(FieldTest, WorksForConstField) {
3803 Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0));
3804 Matcher<AStruct> m_with_name = Field(
"y", &AStruct::y, Ge(0.0));
3807 m = Field(&AStruct::y, Le(0.0));
3808 m_with_name = Field(
"y", &AStruct::y, Le(0.0));
3814TEST(FieldTest, WorksForUncopyableField) {
3817 Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive));
3819 m = Field(&AStruct::z, Not(Truly(ValueIsPositive)));
3824TEST(FieldTest, WorksForPointerField) {
3826 Matcher<AStruct> m = Field(&AStruct::p,
static_cast<const char*
>(
nullptr));
3833 m = Field(&AStruct::p, StartsWith(
"hi"));
3841TEST(FieldTest, WorksForByRefArgument) {
3842 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
3852TEST(FieldTest, WorksForArgumentOfSubType) {
3855 Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0));
3865TEST(FieldTest, WorksForCompatibleMatcherType) {
3867 Matcher<const AStruct&> m = Field(&AStruct::x,
3868 Matcher<signed char>(Ge(0)));
3877TEST(FieldTest, CanDescribeSelf) {
3878 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
3880 EXPECT_EQ(
"is an object whose given field is >= 0", Describe(m));
3881 EXPECT_EQ(
"is an object whose given field isn't >= 0", DescribeNegation(m));
3884TEST(FieldTest, CanDescribeSelfWithFieldName) {
3885 Matcher<const AStruct&> m = Field(
"field_name", &AStruct::x, Ge(0));
3887 EXPECT_EQ(
"is an object whose field `field_name` is >= 0", Describe(m));
3888 EXPECT_EQ(
"is an object whose field `field_name` isn't >= 0",
3889 DescribeNegation(m));
3893TEST(FieldTest, CanExplainMatchResult) {
3894 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
3898 EXPECT_EQ(
"whose given field is 1" + OfType(
"int"), Explain(m,
a));
3900 m = Field(&AStruct::x, GreaterThan(0));
3902 "whose given field is 1" + OfType(
"int") +
", which is 1 more than 0",
3906TEST(FieldTest, CanExplainMatchResultWithFieldName) {
3907 Matcher<const AStruct&> m = Field(
"field_name", &AStruct::x, Ge(0));
3911 EXPECT_EQ(
"whose field `field_name` is 1" + OfType(
"int"), Explain(m,
a));
3913 m = Field(
"field_name", &AStruct::x, GreaterThan(0));
3914 EXPECT_EQ(
"whose field `field_name` is 1" + OfType(
"int") +
3915 ", which is 1 more than 0",
3920TEST(FieldForPointerTest, WorksForPointerToConst) {
3921 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
3930TEST(FieldForPointerTest, WorksForPointerToNonConst) {
3931 Matcher<AStruct*> m = Field(&AStruct::x, Ge(0));
3940TEST(FieldForPointerTest, WorksForReferenceToConstPointer) {
3941 Matcher<AStruct* const&> m = Field(&AStruct::x, Ge(0));
3950TEST(FieldForPointerTest, DoesNotMatchNull) {
3951 Matcher<const AStruct*> m = Field(&AStruct::x, _);
3957TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
3960 Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0));
3969TEST(FieldForPointerTest, CanDescribeSelf) {
3970 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
3972 EXPECT_EQ(
"is an object whose given field is >= 0", Describe(m));
3973 EXPECT_EQ(
"is an object whose given field isn't >= 0", DescribeNegation(m));
3976TEST(FieldForPointerTest, CanDescribeSelfWithFieldName) {
3977 Matcher<const AStruct*> m = Field(
"field_name", &AStruct::x, Ge(0));
3979 EXPECT_EQ(
"is an object whose field `field_name` is >= 0", Describe(m));
3980 EXPECT_EQ(
"is an object whose field `field_name` isn't >= 0",
3981 DescribeNegation(m));
3985TEST(FieldForPointerTest, CanExplainMatchResult) {
3986 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
3990 EXPECT_EQ(
"", Explain(m,
static_cast<const AStruct*
>(
nullptr)));
3991 EXPECT_EQ(
"which points to an object whose given field is 1" + OfType(
"int"),
3994 m = Field(&AStruct::x, GreaterThan(0));
3995 EXPECT_EQ(
"which points to an object whose given field is 1" + OfType(
"int") +
3996 ", which is 1 more than 0", Explain(m, &
a));
3999TEST(FieldForPointerTest, CanExplainMatchResultWithFieldName) {
4000 Matcher<const AStruct*> m = Field(
"field_name", &AStruct::x, Ge(0));
4004 EXPECT_EQ(
"", Explain(m,
static_cast<const AStruct*
>(
nullptr)));
4006 "which points to an object whose field `field_name` is 1" + OfType(
"int"),
4009 m = Field(
"field_name", &AStruct::x, GreaterThan(0));
4010 EXPECT_EQ(
"which points to an object whose field `field_name` is 1" +
4011 OfType(
"int") +
", which is 1 more than 0",
4021 int n()
const {
return n_; }
4023 void set_n(
int new_n) {
n_ = new_n; }
4026 const std::string& s()
const {
return s_; }
4028 const std::string& s_ref() const & {
return s_; }
4030 void set_s(
const std::string& new_s) {
s_ = new_s; }
4033 double&
x()
const {
return x_; }
4042double AClass::x_ = 0.0;
4045class DerivedClass :
public AClass {
4047 int k()
const {
return k_; }
4054TEST(PropertyTest, WorksForNonReferenceProperty) {
4055 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
4056 Matcher<const AClass&> m_with_name = Property(
"n", &AClass::n, Ge(0));
4070TEST(PropertyTest, WorksForReferenceToConstProperty) {
4071 Matcher<const AClass&> m = Property(&AClass::s, StartsWith(
"hi"));
4072 Matcher<const AClass&> m_with_name =
4073 Property(
"s", &AClass::s, StartsWith(
"hi"));
4087TEST(PropertyTest, WorksForRefQualifiedProperty) {
4088 Matcher<const AClass&> m = Property(&AClass::s_ref, StartsWith(
"hi"));
4089 Matcher<const AClass&> m_with_name =
4090 Property(
"s", &AClass::s_ref, StartsWith(
"hi"));
4104TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
4108 Matcher<const AClass&> m = Property(&AClass::x, Ref(
x));
4111 m = Property(&AClass::x, Not(Ref(
x)));
4117TEST(PropertyTest, WorksForByValueArgument) {
4118 Matcher<AClass> m = Property(&AClass::s, StartsWith(
"hi"));
4130TEST(PropertyTest, WorksForArgumentOfSubType) {
4133 Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0));
4145TEST(PropertyTest, WorksForCompatibleMatcherType) {
4147 Matcher<const AClass&> m = Property(&AClass::n,
4148 Matcher<signed char>(Ge(0)));
4150 Matcher<const AClass&> m_with_name =
4151 Property(
"n", &AClass::n, Matcher<signed char>(Ge(0)));
4162TEST(PropertyTest, CanDescribeSelf) {
4163 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
4165 EXPECT_EQ(
"is an object whose given property is >= 0", Describe(m));
4166 EXPECT_EQ(
"is an object whose given property isn't >= 0",
4167 DescribeNegation(m));
4170TEST(PropertyTest, CanDescribeSelfWithPropertyName) {
4171 Matcher<const AClass&> m = Property(
"fancy_name", &AClass::n, Ge(0));
4173 EXPECT_EQ(
"is an object whose property `fancy_name` is >= 0", Describe(m));
4174 EXPECT_EQ(
"is an object whose property `fancy_name` isn't >= 0",
4175 DescribeNegation(m));
4179TEST(PropertyTest, CanExplainMatchResult) {
4180 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
4184 EXPECT_EQ(
"whose given property is 1" + OfType(
"int"), Explain(m,
a));
4186 m = Property(&AClass::n, GreaterThan(0));
4188 "whose given property is 1" + OfType(
"int") +
", which is 1 more than 0",
4192TEST(PropertyTest, CanExplainMatchResultWithPropertyName) {
4193 Matcher<const AClass&> m = Property(
"fancy_name", &AClass::n, Ge(0));
4197 EXPECT_EQ(
"whose property `fancy_name` is 1" + OfType(
"int"), Explain(m,
a));
4199 m = Property(
"fancy_name", &AClass::n, GreaterThan(0));
4200 EXPECT_EQ(
"whose property `fancy_name` is 1" + OfType(
"int") +
4201 ", which is 1 more than 0",
4206TEST(PropertyForPointerTest, WorksForPointerToConst) {
4207 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
4218TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
4219 Matcher<AClass*> m = Property(&AClass::s, StartsWith(
"hi"));
4231TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) {
4232 Matcher<AClass* const&> m = Property(&AClass::s, StartsWith(
"hi"));
4243TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
4244 Matcher<const AClass*> m = Property(&AClass::x, _);
4250TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
4253 Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));
4264TEST(PropertyForPointerTest, CanDescribeSelf) {
4265 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
4267 EXPECT_EQ(
"is an object whose given property is >= 0", Describe(m));
4268 EXPECT_EQ(
"is an object whose given property isn't >= 0",
4269 DescribeNegation(m));
4272TEST(PropertyForPointerTest, CanDescribeSelfWithPropertyDescription) {
4273 Matcher<const AClass*> m = Property(
"fancy_name", &AClass::n, Ge(0));
4275 EXPECT_EQ(
"is an object whose property `fancy_name` is >= 0", Describe(m));
4276 EXPECT_EQ(
"is an object whose property `fancy_name` isn't >= 0",
4277 DescribeNegation(m));
4281TEST(PropertyForPointerTest, CanExplainMatchResult) {
4282 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
4286 EXPECT_EQ(
"", Explain(m,
static_cast<const AClass*
>(
nullptr)));
4288 "which points to an object whose given property is 1" + OfType(
"int"),
4291 m = Property(&AClass::n, GreaterThan(0));
4292 EXPECT_EQ(
"which points to an object whose given property is 1" +
4293 OfType(
"int") +
", which is 1 more than 0",
4297TEST(PropertyForPointerTest, CanExplainMatchResultWithPropertyName) {
4298 Matcher<const AClass*> m = Property(
"fancy_name", &AClass::n, Ge(0));
4302 EXPECT_EQ(
"", Explain(m,
static_cast<const AClass*
>(
nullptr)));
4303 EXPECT_EQ(
"which points to an object whose property `fancy_name` is 1" +
4307 m = Property(
"fancy_name", &AClass::n, GreaterThan(0));
4308 EXPECT_EQ(
"which points to an object whose property `fancy_name` is 1" +
4309 OfType(
"int") +
", which is 1 more than 0",
4317std::string IntToStringFunction(
int input) {
4318 return input == 1 ?
"foo" :
"bar";
4321TEST(ResultOfTest, WorksForFunctionPointers) {
4322 Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(std::string(
"foo")));
4329TEST(ResultOfTest, CanDescribeItself) {
4330 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq(
"foo"));
4332 EXPECT_EQ(
"is mapped by the given callable to a value that "
4333 "is equal to \"foo\"", Describe(matcher));
4334 EXPECT_EQ(
"is mapped by the given callable to a value that "
4335 "isn't equal to \"foo\"", DescribeNegation(matcher));
4339int IntFunction(
int input) {
return input == 42 ? 80 : 90; }
4341TEST(ResultOfTest, CanExplainMatchResult) {
4342 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
4343 EXPECT_EQ(
"which is mapped by the given callable to 90" + OfType(
"int"),
4344 Explain(matcher, 36));
4346 matcher = ResultOf(&IntFunction, GreaterThan(85));
4347 EXPECT_EQ(
"which is mapped by the given callable to 90" + OfType(
"int") +
4348 ", which is 5 more than 85", Explain(matcher, 36));
4353TEST(ResultOfTest, WorksForNonReferenceResults) {
4354 Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
4362double& DoubleFunction(
double& input) {
return input; }
4364Uncopyable& RefUncopyableFunction(Uncopyable& obj) {
4368TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
4371 Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(
x));
4379 Matcher<Uncopyable&> matcher2 =
4380 ResultOf(&RefUncopyableFunction, Ref(obj));
4388const std::string& StringFunction(
const std::string& input) {
return input; }
4390TEST(ResultOfTest, WorksForReferenceToConstResults) {
4391 std::string s =
"foo";
4393 Matcher<const std::string&> matcher = ResultOf(&StringFunction, Ref(s));
4401TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
4403 Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));
4411TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
4413 ResultOf(
static_cast<std::string (*)(
int dummy)
>(
nullptr),
4414 Eq(std::string(
"foo"))),
4415 "NULL function pointer is passed into ResultOf\\(\\)\\.");
4420TEST(ResultOfTest, WorksForFunctionReferences) {
4421 Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq(
"foo"));
4429 std::string operator()(
int input)
const {
4430 return IntToStringFunction(input);
4434TEST(ResultOfTest, WorksForFunctors) {
4435 Matcher<int> matcher = ResultOf(Functor(), Eq(std::string(
"foo")));
4444struct PolymorphicFunctor {
4445 typedef int result_type;
4446 int operator()(
int n) {
return n; }
4447 int operator()(
const char* s) {
return static_cast<int>(strlen(s)); }
4448 std::string operator()(
int *
p) {
return p ?
"good ptr" :
"null"; }
4451TEST(ResultOfTest, WorksForPolymorphicFunctors) {
4452 Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));
4457 Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));
4459 EXPECT_TRUE(matcher_string.Matches(
"long string"));
4463TEST(ResultOfTest, WorksForPolymorphicFunctorsIgnoringResultType) {
4464 Matcher<int*> matcher = ResultOf(PolymorphicFunctor(),
"good ptr");
4471TEST(ResultOfTest, WorksForLambdas) {
4472 Matcher<int> matcher = ResultOf(
4474 return std::string(
static_cast<size_t>(str_len),
'x');
4481TEST(ResultOfTest, WorksForNonCopyableArguments) {
4482 Matcher<std::unique_ptr<int>> matcher = ResultOf(
4483 [](
const std::unique_ptr<int>& str_len) {
4484 return std::string(
static_cast<size_t>(*str_len),
'x');
4487 EXPECT_TRUE(matcher.Matches(std::unique_ptr<int>(
new int(3))));
4488 EXPECT_FALSE(matcher.Matches(std::unique_ptr<int>(
new int(1))));
4491const int* ReferencingFunction(
const int& n) {
return &n; }
4493struct ReferencingFunctor {
4494 typedef const int* result_type;
4495 result_type operator()(
const int& n) {
return &n; }
4498TEST(ResultOfTest, WorksForReferencingCallables) {
4501 Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n));
4505 Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));
4510class DivisibleByImpl {
4512 explicit DivisibleByImpl(
int a_divider) :
divider_(a_divider) {}
4515 template <
typename T>
4516 bool MatchAndExplain(
const T& n, MatchResultListener* listener)
const {
4517 *listener <<
"which is " << (n %
divider_) <<
" modulo "
4519 return (n % divider_) == 0;
4522 void DescribeTo(ostream* os)
const {
4523 *os <<
"is divisible by " <<
divider_;
4526 void DescribeNegationTo(ostream* os)
const {
4527 *os <<
"is not divisible by " <<
divider_;
4530 void set_divider(
int a_divider) {
divider_ = a_divider; }
4531 int divider()
const {
return divider_; }
4537PolymorphicMatcher<DivisibleByImpl> DivisibleBy(
int n) {
4538 return MakePolymorphicMatcher(DivisibleByImpl(n));
4543TEST(ExplainMatchResultTest, AllOf_False_False) {
4544 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
4545 EXPECT_EQ(
"which is 1 modulo 4", Explain(m, 5));
4550TEST(ExplainMatchResultTest, AllOf_False_True) {
4551 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
4552 EXPECT_EQ(
"which is 2 modulo 4", Explain(m, 6));
4557TEST(ExplainMatchResultTest, AllOf_True_False) {
4558 const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
4559 EXPECT_EQ(
"which is 2 modulo 3", Explain(m, 5));
4564TEST(ExplainMatchResultTest, AllOf_True_True) {
4565 const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));
4566 EXPECT_EQ(
"which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6));
4569TEST(ExplainMatchResultTest, AllOf_True_True_2) {
4570 const Matcher<int> m = AllOf(Ge(2), Le(3));
4574TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
4575 const Matcher<int> m = GreaterThan(5);
4576 EXPECT_EQ(
"which is 1 more than 5", Explain(m, 6));
4585 explicit NotCopyable(
int a_value) :
value_(a_value) {}
4589 bool operator==(
const NotCopyable& rhs)
const {
4590 return value() == rhs.value();
4593 bool operator>=(
const NotCopyable& rhs)
const {
4594 return value() >= rhs.value();
4602TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) {
4603 const NotCopyable const_value1(1);
4604 const Matcher<const NotCopyable&> m = Eq(
ByRef(const_value1));
4606 const NotCopyable n1(1), n2(2);
4611TEST(ByRefTest, AllowsNotCopyableValueInMatchers) {
4612 NotCopyable value2(2);
4613 const Matcher<NotCopyable&> m = Ge(
ByRef(value2));
4615 NotCopyable n1(1), n2(2);
4620TEST(IsEmptyTest, ImplementsIsEmpty) {
4629TEST(IsEmptyTest, WorksWithString) {
4634 text = std::string(
"\0", 1);
4638TEST(IsEmptyTest, CanDescribeSelf) {
4639 Matcher<vector<int> > m = IsEmpty();
4641 EXPECT_EQ(
"isn't empty", DescribeNegation(m));
4644TEST(IsEmptyTest, ExplainsResult) {
4645 Matcher<vector<int> > m = IsEmpty();
4652TEST(IsEmptyTest, WorksWithMoveOnly) {
4653 ContainerHelper helper;
4658TEST(IsTrueTest, IsTrueIsFalse) {
4686 std::unique_ptr<int> null_unique;
4687 std::unique_ptr<int> nonnull_unique(
new int(0));
4694TEST(SizeIsTest, ImplementsSizeIs) {
4706TEST(SizeIsTest, WorksWithMap) {
4718TEST(SizeIsTest, WorksWithReferences) {
4720 Matcher<const vector<int>&> m = SizeIs(1);
4726TEST(SizeIsTest, WorksWithMoveOnly) {
4727 ContainerHelper helper;
4729 helper.Call(MakeUniquePtrs({1, 2, 3}));
4734struct MinimalistCustomType {
4735 int size()
const {
return 1; }
4737TEST(SizeIsTest, WorksWithMinimalistCustomType) {
4743TEST(SizeIsTest, CanDescribeSelf) {
4744 Matcher<vector<int> > m = SizeIs(2);
4745 EXPECT_EQ(
"size is equal to 2", Describe(m));
4746 EXPECT_EQ(
"size isn't equal to 2", DescribeNegation(m));
4749TEST(SizeIsTest, ExplainsResult) {
4750 Matcher<vector<int> > m1 = SizeIs(2);
4751 Matcher<vector<int> > m2 = SizeIs(Lt(2u));
4752 Matcher<vector<int> > m3 = SizeIs(AnyOf(0, 3));
4753 Matcher<vector<int> > m4 = SizeIs(Gt(1u));
4767#if GTEST_HAS_TYPED_TEST
4771template <
typename T>
4779 ContainerEqTestTypes;
4785 static const int vals[] = {1, 1, 2, 3, 5, 8};
4786 TypeParam my_set(vals, vals + 6);
4787 const Matcher<TypeParam> m = ContainerEq(my_set);
4794 static const int vals[] = {1, 1, 2, 3, 5, 8};
4795 static const int test_vals[] = {2, 1, 8, 5};
4796 TypeParam my_set(vals, vals + 6);
4797 TypeParam test_set(test_vals, test_vals + 4);
4798 const Matcher<TypeParam> m = ContainerEq(my_set);
4800 EXPECT_EQ(
"which doesn't have these expected elements: 3",
4801 Explain(m, test_set));
4806 static const int vals[] = {1, 1, 2, 3, 5, 8};
4807 static const int test_vals[] = {1, 2, 3, 5, 8, 46};
4808 TypeParam my_set(vals, vals + 6);
4809 TypeParam test_set(test_vals, test_vals + 6);
4810 const Matcher<const TypeParam&> m = ContainerEq(my_set);
4812 EXPECT_EQ(
"which has these unexpected elements: 46", Explain(m, test_set));
4816TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) {
4817 static const int vals[] = {1, 1, 2, 3, 5, 8};
4818 static const int test_vals[] = {1, 2, 3, 8, 46};
4819 TypeParam my_set(vals, vals + 6);
4820 TypeParam test_set(test_vals, test_vals + 5);
4821 const Matcher<TypeParam> m = ContainerEq(my_set);
4823 EXPECT_EQ(
"which has these unexpected elements: 46,\n"
4824 "and doesn't have these expected elements: 5",
4825 Explain(m, test_set));
4829TYPED_TEST(ContainerEqTest, DuplicateDifference) {
4830 static const int vals[] = {1, 1, 2, 3, 5, 8};
4831 static const int test_vals[] = {1, 2, 3, 5, 8};
4832 TypeParam my_set(vals, vals + 6);
4833 TypeParam test_set(test_vals, test_vals + 5);
4834 const Matcher<const TypeParam&> m = ContainerEq(my_set);
4843TEST(ContainerEqExtraTest, MultipleValuesMissing) {
4844 static const int vals[] = {1, 1, 2, 3, 5, 8};
4845 static const int test_vals[] = {2, 1, 5};
4846 vector<int> my_set(vals, vals + 6);
4847 vector<int> test_set(test_vals, test_vals + 3);
4848 const Matcher<vector<int> > m = ContainerEq(my_set);
4850 EXPECT_EQ(
"which doesn't have these expected elements: 3, 8",
4851 Explain(m, test_set));
4856TEST(ContainerEqExtraTest, MultipleValuesAdded) {
4857 static const int vals[] = {1, 1, 2, 3, 5, 8};
4858 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46};
4859 list<size_t> my_set(vals, vals + 6);
4860 list<size_t> test_set(test_vals, test_vals + 7);
4861 const Matcher<const list<size_t>&> m = ContainerEq(my_set);
4863 EXPECT_EQ(
"which has these unexpected elements: 92, 46",
4864 Explain(m, test_set));
4868TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) {
4869 static const int vals[] = {1, 1, 2, 3, 5, 8};
4870 static const int test_vals[] = {1, 2, 3, 92, 46};
4871 list<size_t> my_set(vals, vals + 6);
4872 list<size_t> test_set(test_vals, test_vals + 5);
4873 const Matcher<const list<size_t> > m = ContainerEq(my_set);
4875 EXPECT_EQ(
"which has these unexpected elements: 92, 46,\n"
4876 "and doesn't have these expected elements: 5, 8",
4877 Explain(m, test_set));
4882TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) {
4883 static const int vals[] = {1, 1, 2, 3, 5, 8};
4884 static const int test_vals[] = {1, 2, 3, 5, 8};
4885 vector<int> my_set(vals, vals + 6);
4886 vector<int> test_set(test_vals, test_vals + 5);
4887 const Matcher<vector<int> > m = ContainerEq(my_set);
4896TEST(ContainerEqExtraTest, WorksForMaps) {
4897 map<int, std::string> my_map;
4901 map<int, std::string> test_map;
4905 const Matcher<const map<int, std::string>&> m = ContainerEq(my_map);
4909 EXPECT_EQ(
"which has these unexpected elements: (0, \"aa\"),\n"
4910 "and doesn't have these expected elements: (0, \"a\")",
4911 Explain(m, test_map));
4914TEST(ContainerEqExtraTest, WorksForNativeArray) {
4915 int a1[] = {1, 2, 3};
4916 int a2[] = {1, 2, 3};
4917 int b[] = {1, 2, 4};
4923TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) {
4924 const char a1[][3] = {
"hi",
"lo"};
4925 const char a2[][3] = {
"hi",
"lo"};
4926 const char b[][3] = {
"lo",
"hi"};
4933 EXPECT_THAT(a1, ElementsAre(ContainerEq(a2[0]), ContainerEq(a2[1])));
4934 EXPECT_THAT(a1, ElementsAre(Not(ContainerEq(b[0])), ContainerEq(a2[1])));
4937TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) {
4938 const int a1[] = {1, 2, 3};
4939 const int a2[] = {1, 2, 3};
4940 const int b[] = {1, 2, 3, 4};
4942 const int*
const p1 = a1;
4943 EXPECT_THAT(std::make_tuple(p1, 3), ContainerEq(a2));
4944 EXPECT_THAT(std::make_tuple(p1, 3), Not(ContainerEq(b)));
4946 const int c[] = {1, 3, 2};
4947 EXPECT_THAT(std::make_tuple(p1, 3), Not(ContainerEq(
c)));
4950TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) {
4951 std::string a1[][3] = {
4952 {
"hi",
"hello",
"ciao"},
4953 {
"bye",
"see you",
"ciao"}
4956 std::string a2[][3] = {
4957 {
"hi",
"hello",
"ciao"},
4958 {
"bye",
"see you",
"ciao"}
4961 const Matcher<
const std::string(&)[2][3]> m = ContainerEq(a2);
4968TEST(WhenSortedByTest, WorksForEmptyContainer) {
4969 const vector<int> numbers;
4970 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre()));
4971 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1))));
4974TEST(WhenSortedByTest, WorksForNonEmptyContainer) {
4975 vector<unsigned> numbers;
4976 numbers.push_back(3);
4977 numbers.push_back(1);
4978 numbers.push_back(2);
4979 numbers.push_back(2);
4980 EXPECT_THAT(numbers, WhenSortedBy(greater<unsigned>(),
4981 ElementsAre(3, 2, 2, 1)));
4982 EXPECT_THAT(numbers, Not(WhenSortedBy(greater<unsigned>(),
4983 ElementsAre(1, 2, 2, 3))));
4986TEST(WhenSortedByTest, WorksForNonVectorContainer) {
4987 list<std::string> words;
4988 words.push_back(
"say");
4989 words.push_back(
"hello");
4990 words.push_back(
"world");
4991 EXPECT_THAT(words, WhenSortedBy(less<std::string>(),
4992 ElementsAre(
"hello",
"say",
"world")));
4993 EXPECT_THAT(words, Not(WhenSortedBy(less<std::string>(),
4994 ElementsAre(
"say",
"hello",
"world"))));
4997TEST(WhenSortedByTest, WorksForNativeArray) {
4998 const int numbers[] = {1, 3, 2, 4};
4999 const int sorted_numbers[] = {1, 2, 3, 4};
5000 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre(1, 2, 3, 4)));
5002 ElementsAreArray(sorted_numbers)));
5003 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1, 3, 2, 4))));
5006TEST(WhenSortedByTest, CanDescribeSelf) {
5007 const Matcher<vector<int> > m = WhenSortedBy(less<int>(), ElementsAre(1, 2));
5008 EXPECT_EQ(
"(when sorted) has 2 elements where\n"
5009 "element #0 is equal to 1,\n"
5010 "element #1 is equal to 2",
5012 EXPECT_EQ(
"(when sorted) doesn't have 2 elements, or\n"
5013 "element #0 isn't equal to 1, or\n"
5014 "element #1 isn't equal to 2",
5015 DescribeNegation(m));
5018TEST(WhenSortedByTest, ExplainsMatchResult) {
5019 const int a[] = {2, 1};
5020 EXPECT_EQ(
"which is { 1, 2 } when sorted, whose element #0 doesn't match",
5021 Explain(WhenSortedBy(less<int>(), ElementsAre(2, 3)),
a));
5022 EXPECT_EQ(
"which is { 1, 2 } when sorted",
5023 Explain(WhenSortedBy(less<int>(), ElementsAre(1, 2)),
a));
5029TEST(WhenSortedTest, WorksForEmptyContainer) {
5030 const vector<int> numbers;
5032 EXPECT_THAT(numbers, Not(WhenSorted(ElementsAre(1))));
5035TEST(WhenSortedTest, WorksForNonEmptyContainer) {
5036 list<std::string> words;
5037 words.push_back(
"3");
5038 words.push_back(
"1");
5039 words.push_back(
"2");
5040 words.push_back(
"2");
5041 EXPECT_THAT(words, WhenSorted(ElementsAre(
"1",
"2",
"2",
"3")));
5042 EXPECT_THAT(words, Not(WhenSorted(ElementsAre(
"3",
"1",
"2",
"2"))));
5045TEST(WhenSortedTest, WorksForMapTypes) {
5046 map<std::string, int> word_counts;
5047 word_counts[
"and"] = 1;
5048 word_counts[
"the"] = 1;
5049 word_counts[
"buffalo"] = 2;
5051 WhenSorted(ElementsAre(Pair(
"and", 1), Pair(
"buffalo", 2),
5054 Not(WhenSorted(ElementsAre(Pair(
"and", 1), Pair(
"the", 1),
5055 Pair(
"buffalo", 2)))));
5058TEST(WhenSortedTest, WorksForMultiMapTypes) {
5059 multimap<int, int> ifib;
5060 ifib.insert(make_pair(8, 6));
5061 ifib.insert(make_pair(2, 3));
5062 ifib.insert(make_pair(1, 1));
5063 ifib.insert(make_pair(3, 4));
5064 ifib.insert(make_pair(1, 2));
5065 ifib.insert(make_pair(5, 5));
5066 EXPECT_THAT(ifib, WhenSorted(ElementsAre(Pair(1, 1),
5072 EXPECT_THAT(ifib, Not(WhenSorted(ElementsAre(Pair(8, 6),
5080TEST(WhenSortedTest, WorksForPolymorphicMatcher) {
5085 EXPECT_THAT(d, Not(WhenSorted(ElementsAre(2, 1))));
5088TEST(WhenSortedTest, WorksForVectorConstRefMatcher) {
5092 Matcher<const std::vector<int>&> vector_match = ElementsAre(1, 2);
5094 Matcher<const std::vector<int>&> not_vector_match = ElementsAre(2, 1);
5095 EXPECT_THAT(d, Not(WhenSorted(not_vector_match)));
5100template <
typename T>
5105 typedef ConstIter const_iterator;
5106 typedef T value_type;
5108 template <
typename InIter>
5109 Streamlike(InIter first, InIter last) :
remainder_(first, last) {}
5111 const_iterator begin()
const {
5112 return const_iterator(
this,
remainder_.begin());
5114 const_iterator end()
const {
5115 return const_iterator(
this,
remainder_.end());
5119 class ConstIter :
public std::iterator<std::input_iterator_tag,
5123 const value_type&> {
5125 ConstIter(
const Streamlike* s,
5126 typename std::list<value_type>::iterator pos)
5130 const value_type* operator->()
const {
return &*
pos_; }
5131 ConstIter& operator++() {
5132 s_->remainder_.erase(pos_++);
5138 class PostIncrProxy {
5145 PostIncrProxy operator++(
int) {
5146 PostIncrProxy proxy(**
this);
5151 friend bool operator==(
const ConstIter&
a,
const ConstIter& b) {
5152 return a.s_ == b.s_ &&
a.pos_ == b.pos_;
5154 friend bool operator!=(
const ConstIter&
a,
const ConstIter& b) {
5159 const Streamlike*
s_;
5160 typename std::list<value_type>::iterator
pos_;
5163 friend std::ostream&
operator<<(std::ostream& os,
const Streamlike& s) {
5165 typedef typename std::list<value_type>::const_iterator Iter;
5166 const char* sep =
"";
5167 for (Iter it = s.remainder_.begin(); it != s.remainder_.end(); ++it) {
5178TEST(StreamlikeTest, Iteration) {
5179 const int a[5] = {2, 1, 4, 5, 3};
5180 Streamlike<int> s(
a,
a + 5);
5181 Streamlike<int>::const_iterator it = s.begin();
5183 while (it != s.end()) {
5189TEST(BeginEndDistanceIsTest, WorksWithForwardList) {
5201TEST(BeginEndDistanceIsTest, WorksWithNonStdList) {
5202 const int a[5] = {1, 2, 3, 4, 5};
5203 Streamlike<int> s(
a,
a + 5);
5207TEST(BeginEndDistanceIsTest, CanDescribeSelf) {
5208 Matcher<vector<int> > m = BeginEndDistanceIs(2);
5209 EXPECT_EQ(
"distance between begin() and end() is equal to 2", Describe(m));
5210 EXPECT_EQ(
"distance between begin() and end() isn't equal to 2",
5211 DescribeNegation(m));
5214TEST(BeginEndDistanceIsTest, WorksWithMoveOnly) {
5215 ContainerHelper helper;
5217 helper.Call(MakeUniquePtrs({1, 2}));
5220TEST(BeginEndDistanceIsTest, ExplainsResult) {
5221 Matcher<vector<int> > m1 = BeginEndDistanceIs(2);
5222 Matcher<vector<int> > m2 = BeginEndDistanceIs(Lt(2));
5223 Matcher<vector<int> > m3 = BeginEndDistanceIs(AnyOf(0, 3));
5224 Matcher<vector<int> > m4 = BeginEndDistanceIs(GreaterThan(1));
5226 EXPECT_EQ(
"whose distance between begin() and end() 0 doesn't match",
5228 EXPECT_EQ(
"whose distance between begin() and end() 0 matches",
5230 EXPECT_EQ(
"whose distance between begin() and end() 0 matches",
5233 "whose distance between begin() and end() 0 doesn't match, which is 1 "
5238 EXPECT_EQ(
"whose distance between begin() and end() 2 matches",
5240 EXPECT_EQ(
"whose distance between begin() and end() 2 doesn't match",
5242 EXPECT_EQ(
"whose distance between begin() and end() 2 doesn't match",
5245 "whose distance between begin() and end() 2 matches, which is 1 more "
5250TEST(WhenSortedTest, WorksForStreamlike) {
5253 const int a[5] = {2, 1, 4, 5, 3};
5254 Streamlike<int> s(std::begin(
a), std::end(
a));
5255 EXPECT_THAT(s, WhenSorted(ElementsAre(1, 2, 3, 4, 5)));
5256 EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3))));
5259TEST(WhenSortedTest, WorksForVectorConstRefMatcherOnStreamlike) {
5260 const int a[] = {2, 1, 4, 5, 3};
5261 Streamlike<int> s(std::begin(
a), std::end(
a));
5262 Matcher<const std::vector<int>&> vector_match = ElementsAre(1, 2, 3, 4, 5);
5264 EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3))));
5267TEST(IsSupersetOfTest, WorksForNativeArray) {
5268 const int subset[] = {1, 4};
5269 const int superset[] = {1, 2, 4};
5270 const int disjoint[] = {1, 0, 3};
5278TEST(IsSupersetOfTest, WorksWithDuplicates) {
5279 const int not_enough[] = {1, 2};
5280 const int enough[] = {1, 1, 2};
5286TEST(IsSupersetOfTest, WorksForEmpty) {
5287 vector<int> numbers;
5293 numbers.push_back(1);
5294 numbers.push_back(2);
5304TEST(IsSupersetOfTest, WorksForStreamlike) {
5305 const int a[5] = {1, 2, 3, 4, 5};
5306 Streamlike<int> s(std::begin(
a), std::end(
a));
5318TEST(IsSupersetOfTest, TakesStlContainer) {
5319 const int actual[] = {3, 1, 2};
5330TEST(IsSupersetOfTest, Describe) {
5331 typedef std::vector<int> IntVec;
5337 Describe<IntVec>(IsSupersetOf(
expected)),
5338 Eq(
"a surjection from elements to requirements exists such that:\n"
5339 " - an element is equal to 111\n"
5340 " - an element is equal to 222\n"
5341 " - an element is equal to 333"));
5344TEST(IsSupersetOfTest, DescribeNegation) {
5345 typedef std::vector<int> IntVec;
5351 DescribeNegation<IntVec>(IsSupersetOf(
expected)),
5352 Eq(
"no surjection from elements to requirements exists such that:\n"
5353 " - an element is equal to 111\n"
5354 " - an element is equal to 222\n"
5355 " - an element is equal to 333"));
5358TEST(IsSupersetOfTest, MatchAndExplain) {
5365 StringMatchResultListener listener;
5369 Eq(
"where the following matchers don't match any elements:\n"
5370 "matcher #0: is equal to 1"));
5377 " - element #0 is matched by matcher #1,\n"
5378 " - element #2 is matched by matcher #0"));
5381TEST(IsSupersetOfTest, WorksForRhsInitializerList) {
5382 const int numbers[] = {1, 3, 6, 2, 4, 5};
5387TEST(IsSupersetOfTest, WorksWithMoveOnly) {
5388 ContainerHelper helper;
5389 EXPECT_CALL(helper, Call(IsSupersetOf({Pointee(1)})));
5390 helper.Call(MakeUniquePtrs({1, 2}));
5391 EXPECT_CALL(helper, Call(Not(IsSupersetOf({Pointee(1), Pointee(2)}))));
5392 helper.Call(MakeUniquePtrs({2}));
5395TEST(IsSubsetOfTest, WorksForNativeArray) {
5396 const int subset[] = {1, 4};
5397 const int superset[] = {1, 2, 4};
5398 const int disjoint[] = {1, 0, 3};
5406TEST(IsSubsetOfTest, WorksWithDuplicates) {
5407 const int not_enough[] = {1, 2};
5408 const int enough[] = {1, 1, 2};
5409 const int actual[] = {1, 1};
5414TEST(IsSubsetOfTest, WorksForEmpty) {
5415 vector<int> numbers;
5421 numbers.push_back(1);
5422 numbers.push_back(2);
5432TEST(IsSubsetOfTest, WorksForStreamlike) {
5433 const int a[5] = {1, 2};
5434 Streamlike<int> s(std::begin(
a), std::end(
a));
5444TEST(IsSubsetOfTest, TakesStlContainer) {
5445 const int actual[] = {3, 1, 2};
5457TEST(IsSubsetOfTest, Describe) {
5458 typedef std::vector<int> IntVec;
5465 Describe<IntVec>(IsSubsetOf(
expected)),
5466 Eq(
"an injection from elements to requirements exists such that:\n"
5467 " - an element is equal to 111\n"
5468 " - an element is equal to 222\n"
5469 " - an element is equal to 333"));
5472TEST(IsSubsetOfTest, DescribeNegation) {
5473 typedef std::vector<int> IntVec;
5479 DescribeNegation<IntVec>(IsSubsetOf(
expected)),
5480 Eq(
"no injection from elements to requirements exists such that:\n"
5481 " - an element is equal to 111\n"
5482 " - an element is equal to 222\n"
5483 " - an element is equal to 333"));
5486TEST(IsSubsetOfTest, MatchAndExplain) {
5493 StringMatchResultListener listener;
5497 Eq(
"where the following elements don't match any matchers:\n"
5505 " - element #0 is matched by matcher #1,\n"
5506 " - element #1 is matched by matcher #2"));
5509TEST(IsSubsetOfTest, WorksForRhsInitializerList) {
5510 const int numbers[] = {1, 2, 3};
5515TEST(IsSubsetOfTest, WorksWithMoveOnly) {
5516 ContainerHelper helper;
5517 EXPECT_CALL(helper, Call(IsSubsetOf({Pointee(1), Pointee(2)})));
5518 helper.Call(MakeUniquePtrs({1}));
5519 EXPECT_CALL(helper, Call(Not(IsSubsetOf({Pointee(1)}))));
5520 helper.Call(MakeUniquePtrs({2}));
5526TEST(ElemensAreStreamTest, WorksForStreamlike) {
5527 const int a[5] = {1, 2, 3, 4, 5};
5528 Streamlike<int> s(std::begin(
a), std::end(
a));
5533TEST(ElemensAreArrayStreamTest, WorksForStreamlike) {
5534 const int a[5] = {1, 2, 3, 4, 5};
5535 Streamlike<int> s(std::begin(
a), std::end(
a));
5549TEST(ElementsAreTest, WorksWithUncopyable) {
5551 objs[0].set_value(-3);
5552 objs[1].set_value(1);
5553 EXPECT_THAT(objs, ElementsAre(UncopyableIs(-3), Truly(ValueIsPositive)));
5556TEST(ElementsAreTest, WorksWithMoveOnly) {
5557 ContainerHelper helper;
5558 EXPECT_CALL(helper, Call(ElementsAre(Pointee(1), Pointee(2))));
5559 helper.Call(MakeUniquePtrs({1, 2}));
5561 EXPECT_CALL(helper, Call(ElementsAreArray({Pointee(3), Pointee(4)})));
5562 helper.Call(MakeUniquePtrs({3, 4}));
5565TEST(ElementsAreTest, TakesStlContainer) {
5566 const int actual[] = {3, 1, 2};
5580TEST(UnorderedElementsAreArrayTest, SucceedsWhenExpected) {
5581 const int a[] = {0, 1, 2, 3, 4};
5582 std::vector<int> s(std::begin(
a), std::end(
a));
5584 StringMatchResultListener listener;
5585 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(
a),
5586 s, &listener)) << listener.str();
5587 }
while (std::next_permutation(s.begin(), s.end()));
5590TEST(UnorderedElementsAreArrayTest, VectorBool) {
5591 const bool a[] = {0, 1, 0, 1, 1};
5592 const bool b[] = {1, 0, 1, 1, 0};
5593 std::vector<bool>
expected(std::begin(
a), std::end(
a));
5594 std::vector<bool> actual(std::begin(b), std::end(b));
5595 StringMatchResultListener listener;
5597 actual, &listener)) << listener.str();
5600TEST(UnorderedElementsAreArrayTest, WorksForStreamlike) {
5604 const int a[5] = {2, 1, 4, 5, 3};
5605 Streamlike<int> s(std::begin(
a), std::end(
a));
5619TEST(UnorderedElementsAreArrayTest, TakesStlContainer) {
5620 const int actual[] = {3, 1, 2};
5633TEST(UnorderedElementsAreArrayTest, TakesInitializerList) {
5634 const int a[5] = {2, 1, 4, 5, 3};
5635 EXPECT_THAT(
a, UnorderedElementsAreArray({1, 2, 3, 4, 5}));
5636 EXPECT_THAT(
a, Not(UnorderedElementsAreArray({1, 2, 3, 4, 6})));
5639TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfCStrings) {
5640 const std::string
a[5] = {
"a",
"b",
"c",
"d",
"e"};
5641 EXPECT_THAT(
a, UnorderedElementsAreArray({
"a",
"b",
"c",
"d",
"e"}));
5642 EXPECT_THAT(
a, Not(UnorderedElementsAreArray({
"a",
"b",
"c",
"d",
"ef"})));
5645TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {
5646 const int a[5] = {2, 1, 4, 5, 3};
5648 {Eq(1), Eq(2), Eq(3), Eq(4), Eq(5)}));
5650 {Eq(1), Eq(2), Eq(3), Eq(4), Eq(6)})));
5653TEST(UnorderedElementsAreArrayTest,
5654 TakesInitializerListOfDifferentTypedMatchers) {
5655 const int a[5] = {2, 1, 4, 5, 3};
5659 EXPECT_THAT(
a, UnorderedElementsAreArray<Matcher<int> >(
5660 {Eq(1), Ne(-2), Ge(3), Le(4), Eq(5)}));
5661 EXPECT_THAT(
a, Not(UnorderedElementsAreArray<Matcher<int> >(
5662 {Eq(1), Ne(-2), Ge(3), Le(4), Eq(6)})));
5666TEST(UnorderedElementsAreArrayTest, WorksWithMoveOnly) {
5667 ContainerHelper helper;
5669 Call(UnorderedElementsAreArray({Pointee(1), Pointee(2)})));
5670 helper.Call(MakeUniquePtrs({2, 1}));
5675 typedef std::vector<int> IntVec;
5678TEST_F(UnorderedElementsAreTest, WorksWithUncopyable) {
5680 objs[0].set_value(-3);
5681 objs[1].set_value(1);
5683 UnorderedElementsAre(Truly(ValueIsPositive), UncopyableIs(-3)));
5686TEST_F(UnorderedElementsAreTest, SucceedsWhenExpected) {
5687 const int a[] = {1, 2, 3};
5688 std::vector<int> s(std::begin(
a), std::end(
a));
5690 StringMatchResultListener listener;
5691 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3),
5692 s, &listener)) << listener.str();
5693 }
while (std::next_permutation(s.begin(), s.end()));
5696TEST_F(UnorderedElementsAreTest, FailsWhenAnElementMatchesNoMatcher) {
5697 const int a[] = {1, 2, 3};
5698 std::vector<int> s(std::begin(
a), std::end(
a));
5699 std::vector<Matcher<int> > mv;
5704 StringMatchResultListener listener;
5705 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAreArray(mv),
5706 s, &listener)) << listener.str();
5709TEST_F(UnorderedElementsAreTest, WorksForStreamlike) {
5713 const int a[5] = {2, 1, 4, 5, 3};
5714 Streamlike<int> s(std::begin(
a), std::end(
a));
5716 EXPECT_THAT(s, UnorderedElementsAre(1, 2, 3, 4, 5));
5717 EXPECT_THAT(s, Not(UnorderedElementsAre(2, 2, 3, 4, 5)));
5720TEST_F(UnorderedElementsAreTest, WorksWithMoveOnly) {
5721 ContainerHelper helper;
5722 EXPECT_CALL(helper, Call(UnorderedElementsAre(Pointee(1), Pointee(2))));
5723 helper.Call(MakeUniquePtrs({2, 1}));
5732TEST_F(UnorderedElementsAreTest, Performance) {
5734 std::vector<Matcher<int> > mv;
5735 for (
int i = 0;
i < 100; ++
i) {
5740 StringMatchResultListener listener;
5741 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(mv),
5742 s, &listener)) << listener.str();
5748TEST_F(UnorderedElementsAreTest, PerformanceHalfStrict) {
5750 std::vector<Matcher<int> > mv;
5751 for (
int i = 0;
i < 100; ++
i) {
5759 StringMatchResultListener listener;
5760 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(mv),
5761 s, &listener)) << listener.str();
5764TEST_F(UnorderedElementsAreTest, FailMessageCountWrong) {
5767 StringMatchResultListener listener;
5768 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3),
5769 v, &listener)) << listener.str();
5770 EXPECT_THAT(listener.str(), Eq(
"which has 1 element"));
5773TEST_F(UnorderedElementsAreTest, FailMessageCountWrongZero) {
5775 StringMatchResultListener listener;
5776 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3),
5777 v, &listener)) << listener.str();
5781TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatchers) {
5785 StringMatchResultListener listener;
5786 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2),
5787 v, &listener)) << listener.str();
5790 Eq(
"where the following matchers don't match any elements:\n"
5791 "matcher #1: is equal to 2"));
5794TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedElements) {
5798 StringMatchResultListener listener;
5799 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 1),
5800 v, &listener)) << listener.str();
5803 Eq(
"where the following elements don't match any matchers:\n"
5807TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatcherAndElement) {
5811 StringMatchResultListener listener;
5812 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2),
5813 v, &listener)) << listener.str();
5817 " the following matchers don't match any elements:\n"
5818 "matcher #0: is equal to 1\n"
5821 " the following elements don't match any matchers:\n"
5826static std::string EMString(
int element,
int matcher) {
5828 ss <<
"(element #" << element <<
", matcher #" << matcher <<
")";
5832TEST_F(UnorderedElementsAreTest, FailMessageImperfectMatchOnly) {
5835 std::vector<std::string> v;
5839 StringMatchResultListener listener;
5841 UnorderedElementsAre(
"a",
"a", AnyOf(
"b",
"c")), v, &listener))
5844 std::string prefix =
5845 "where no permutation of the elements can satisfy all matchers, "
5846 "and the closest match is 2 of 3 matchers with the "
5852 AnyOf(prefix +
"{\n " + EMString(0, 0) +
5853 ",\n " + EMString(1, 2) +
"\n}",
5854 prefix +
"{\n " + EMString(0, 1) +
5855 ",\n " + EMString(1, 2) +
"\n}",
5856 prefix +
"{\n " + EMString(0, 0) +
5857 ",\n " + EMString(2, 2) +
"\n}",
5858 prefix +
"{\n " + EMString(0, 1) +
5859 ",\n " + EMString(2, 2) +
"\n}"));
5862TEST_F(UnorderedElementsAreTest, Describe) {
5863 EXPECT_THAT(Describe<IntVec>(UnorderedElementsAre()),
5866 Describe<IntVec>(UnorderedElementsAre(345)),
5867 Eq(
"has 1 element and that element is equal to 345"));
5869 Describe<IntVec>(UnorderedElementsAre(111, 222, 333)),
5870 Eq(
"has 3 elements and there exists some permutation "
5871 "of elements such that:\n"
5872 " - element #0 is equal to 111, and\n"
5873 " - element #1 is equal to 222, and\n"
5874 " - element #2 is equal to 333"));
5877TEST_F(UnorderedElementsAreTest, DescribeNegation) {
5878 EXPECT_THAT(DescribeNegation<IntVec>(UnorderedElementsAre()),
5881 DescribeNegation<IntVec>(UnorderedElementsAre(345)),
5882 Eq(
"doesn't have 1 element, or has 1 element that isn't equal to 345"));
5884 DescribeNegation<IntVec>(UnorderedElementsAre(123, 234, 345)),
5885 Eq(
"doesn't have 3 elements, or there exists no permutation "
5886 "of elements such that:\n"
5887 " - element #0 is equal to 123, and\n"
5888 " - element #1 is equal to 234, and\n"
5889 " - element #2 is equal to 345"));
5898template <
typename Graph>
5899class BacktrackingMaxBPMState {
5902 explicit BacktrackingMaxBPMState(
const Graph* g) :
graph_(g) { }
5904 ElementMatcherPairs Compute() {
5905 if (
graph_->LhsSize() == 0 ||
graph_->RhsSize() == 0) {
5910 for (
size_t irhs = 0; irhs <
graph_->RhsSize(); ++irhs) {
5920 static const size_t kUnused =
static_cast<size_t>(-1);
5922 void PushMatch(
size_t lhs,
size_t rhs) {
5923 matches_.push_back(ElementMatcherPair(lhs, rhs));
5932 const ElementMatcherPair& back =
matches_.back();
5938 bool RecurseInto(
size_t irhs) {
5942 for (
size_t ilhs = 0; ilhs <
graph_->LhsSize(); ++ilhs) {
5946 if (!
graph_->HasEdge(ilhs, irhs)) {
5949 PushMatch(ilhs, irhs);
5953 for (
size_t mi = irhs + 1; mi <
graph_->RhsSize(); ++mi) {
5954 if (!RecurseInto(mi))
return false;
5968template <
typename Graph>
5969const size_t BacktrackingMaxBPMState<Graph>::kUnused;
5975template <
typename Graph>
5977FindBacktrackingMaxBPM(
const Graph& g) {
5978 return BacktrackingMaxBPMState<Graph>(&g).Compute();
5988TEST_P(BipartiteTest, Exhaustive) {
5989 size_t nodes = GetParam();
5990 MatchMatrix graph(nodes, nodes);
5992 ElementMatcherPairs matches =
5994 EXPECT_EQ(FindBacktrackingMaxBPM(graph).size(), matches.size())
5995 <<
"graph: " << graph.DebugString();
5998 std::vector<bool> seen_element(graph.LhsSize());
5999 std::vector<bool> seen_matcher(graph.RhsSize());
6001 for (
size_t i = 0;
i < matches.size(); ++
i) {
6002 size_t ilhs = matches[
i].first;
6003 size_t irhs = matches[
i].second;
6007 seen_element[ilhs] =
true;
6008 seen_matcher[irhs] =
true;
6010 }
while (graph.NextGraph());
6017class BipartiteNonSquareTest
6021TEST_F(BipartiteNonSquareTest, SimpleBacktracking) {
6029 MatchMatrix g(4, 3);
6030 constexpr std::array<std::array<size_t, 2>, 4> kEdges = {
6031 {{{0, 2}}, {{1, 1}}, {{2, 1}}, {{3, 0}}}};
6032 for (
size_t i = 0;
i < kEdges.size(); ++
i) {
6033 g.SetEdge(kEdges[
i][0], kEdges[
i][1],
true);
6036 ElementsAre(Pair(3, 0),
6037 Pair(AnyOf(1, 2), 1),
6038 Pair(0, 2))) << g.DebugString();
6042TEST_P(BipartiteNonSquareTest, Exhaustive) {
6043 size_t nlhs = GetParam().first;
6044 size_t nrhs = GetParam().second;
6045 MatchMatrix graph(nlhs, nrhs);
6047 EXPECT_EQ(FindBacktrackingMaxBPM(graph).size(),
6049 <<
"graph: " << graph.DebugString()
6050 <<
"\nbacktracking: "
6054 }
while (graph.NextGraph());
6059 std::make_pair(1, 2),
6060 std::make_pair(2, 1),
6061 std::make_pair(3, 2),
6062 std::make_pair(2, 3),
6063 std::make_pair(4, 1),
6064 std::make_pair(1, 4),
6065 std::make_pair(4, 3),
6066 std::make_pair(3, 4)));
6068class BipartiteRandomTest
6073TEST_P(BipartiteRandomTest, LargerNets) {
6074 int nodes = GetParam().first;
6075 int iters = GetParam().second;
6076 MatchMatrix graph(
static_cast<size_t>(nodes),
static_cast<size_t>(nodes));
6078 auto seed =
static_cast<uint32_t
>(
GTEST_FLAG(random_seed));
6080 seed =
static_cast<uint32_t
>(time(
nullptr));
6083 for (; iters > 0; --iters, ++seed) {
6084 srand(
static_cast<unsigned int>(seed));
6086 EXPECT_EQ(FindBacktrackingMaxBPM(graph).size(),
6088 <<
" graph: " << graph.DebugString()
6089 <<
"\nTo reproduce the failure, rerun the test with the flag"
6097 std::make_pair(5, 10000),
6098 std::make_pair(6, 5000),
6099 std::make_pair(7, 2000),
6100 std::make_pair(8, 500),
6101 std::make_pair(9, 100)));
6105TEST(IsReadableTypeNameTest, ReturnsTrueForShortNames) {
6107 EXPECT_TRUE(IsReadableTypeName(
"const unsigned char*"));
6108 EXPECT_TRUE(IsReadableTypeName(
"MyMap<int, void*>"));
6109 EXPECT_TRUE(IsReadableTypeName(
"void (*)(int, bool)"));
6112TEST(IsReadableTypeNameTest, ReturnsTrueForLongNonTemplateNonFunctionNames) {
6113 EXPECT_TRUE(IsReadableTypeName(
"my_long_namespace::MyClassName"));
6114 EXPECT_TRUE(IsReadableTypeName(
"int [5][6][7][8][9][10][11]"));
6115 EXPECT_TRUE(IsReadableTypeName(
"my_namespace::MyOuterClass::MyInnerClass"));
6118TEST(IsReadableTypeNameTest, ReturnsFalseForLongTemplateNames) {
6120 IsReadableTypeName(
"basic_string<char, std::char_traits<char> >"));
6121 EXPECT_FALSE(IsReadableTypeName(
"std::vector<int, std::alloc_traits<int> >"));
6124TEST(IsReadableTypeNameTest, ReturnsFalseForLongFunctionTypeNames) {
6125 EXPECT_FALSE(IsReadableTypeName(
"void (&)(int, bool, char, float)"));
6130TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) {
6136 const char* params[] = {
"5"};
6139 Strings(params, params + 1)));
6141 const char* params2[] = {
"5",
"8"};
6144 Strings(params2, params2 + 2)));
6148TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
6149 PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
6150 DivisibleByImpl& impl = m.mutable_impl();
6153 impl.set_divider(0);
6154 EXPECT_EQ(0, m.mutable_impl().divider());
6158TEST(PolymorphicMatcherTest, CanAccessImpl) {
6159 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
6160 const DivisibleByImpl& impl = m.impl();
6164TEST(MatcherTupleTest, ExplainsMatchFailure) {
6166 ExplainMatchFailureTupleTo(
6167 std::make_tuple(Matcher<char>(Eq(
'a')), GreaterThan(5)),
6168 std::make_tuple(
'a', 10), &ss1);
6172 ExplainMatchFailureTupleTo(
6173 std::make_tuple(GreaterThan(5), Matcher<char>(Eq(
'a'))),
6174 std::make_tuple(2,
'b'), &ss2);
6176 " Actual: 2, which is 3 less than 5\n"
6177 " Expected arg #1: is equal to 'a' (97, 0x61)\n"
6178 " Actual: 'b' (98, 0x62)\n",
6182 ExplainMatchFailureTupleTo(
6183 std::make_tuple(GreaterThan(5), Matcher<char>(Eq(
'a'))),
6184 std::make_tuple(2,
'a'), &ss3);
6186 " Actual: 2, which is 3 less than 5\n",
6193TEST(EachTest, ExplainsMatchResultCorrectly) {
6196 Matcher<set<int> > m = Each(2);
6199 Matcher<
const int(&)[1]> n = Each(1);
6201 const int b[1] = {1};
6205 EXPECT_EQ(
"whose element #0 doesn't match", Explain(n, b));
6210 m = Each(GreaterThan(0));
6213 m = Each(GreaterThan(10));
6214 EXPECT_EQ(
"whose element #0 doesn't match, which is 9 less than 10",
6218TEST(EachTest, DescribesItselfCorrectly) {
6219 Matcher<vector<int> > m = Each(1);
6220 EXPECT_EQ(
"only contains elements that is equal to 1", Describe(m));
6222 Matcher<vector<int> > m2 = Not(m);
6223 EXPECT_EQ(
"contains some element that isn't equal to 1", Describe(m2));
6226TEST(EachTest, MatchesVectorWhenAllElementsMatch) {
6227 vector<int> some_vector;
6229 some_vector.push_back(3);
6232 some_vector.push_back(1);
6233 some_vector.push_back(2);
6237 vector<std::string> another_vector;
6238 another_vector.push_back(
"fee");
6239 EXPECT_THAT(another_vector, Each(std::string(
"fee")));
6240 another_vector.push_back(
"fie");
6241 another_vector.push_back(
"foe");
6242 another_vector.push_back(
"fum");
6243 EXPECT_THAT(another_vector, Not(Each(std::string(
"fee"))));
6246TEST(EachTest, MatchesMapWhenAllElementsMatch) {
6247 map<const char*, int> my_map;
6248 const char*
bar =
"a string";
6252 map<std::string, int> another_map;
6253 EXPECT_THAT(another_map, Each(make_pair(std::string(
"fee"), 1)));
6254 another_map[
"fee"] = 1;
6255 EXPECT_THAT(another_map, Each(make_pair(std::string(
"fee"), 1)));
6256 another_map[
"fie"] = 2;
6257 another_map[
"foe"] = 3;
6258 another_map[
"fum"] = 4;
6259 EXPECT_THAT(another_map, Not(Each(make_pair(std::string(
"fee"), 1))));
6260 EXPECT_THAT(another_map, Not(Each(make_pair(std::string(
"fum"), 1))));
6264TEST(EachTest, AcceptsMatcher) {
6265 const int a[] = {1, 2, 3};
6270TEST(EachTest, WorksForNativeArrayAsTuple) {
6271 const int a[] = {1, 2};
6272 const int*
const pointer =
a;
6273 EXPECT_THAT(std::make_tuple(pointer, 2), Each(Gt(0)));
6274 EXPECT_THAT(std::make_tuple(pointer, 2), Not(Each(Gt(1))));
6277TEST(EachTest, WorksWithMoveOnly) {
6278 ContainerHelper helper;
6280 helper.Call(MakeUniquePtrs({1, 2}));
6284class IsHalfOfMatcher {
6286 template <
typename T1,
typename T2>
6287 bool MatchAndExplain(
const std::tuple<T1, T2>& a_pair,
6288 MatchResultListener* listener)
const {
6289 if (std::get<0>(a_pair) == std::get<1>(a_pair) / 2) {
6290 *listener <<
"where the second is " << std::get<1>(a_pair);
6293 *listener <<
"where the second/2 is " << std::get<1>(a_pair) / 2;
6298 void DescribeTo(ostream* os)
const {
6299 *os <<
"are a pair where the first is half of the second";
6302 void DescribeNegationTo(ostream* os)
const {
6303 *os <<
"are a pair where the first isn't half of the second";
6307PolymorphicMatcher<IsHalfOfMatcher> IsHalfOf() {
6308 return MakePolymorphicMatcher(IsHalfOfMatcher());
6311TEST(PointwiseTest, DescribesSelf) {
6316 const Matcher<const vector<int>&> m = Pointwise(IsHalfOf(), rhs);
6317 EXPECT_EQ(
"contains 3 values, where each value and its corresponding value "
6318 "in { 1, 2, 3 } are a pair where the first is half of the second",
6320 EXPECT_EQ(
"doesn't contain exactly 3 values, or contains a value x at some "
6321 "index i where x and the i-th value of { 1, 2, 3 } are a pair "
6322 "where the first isn't half of the second",
6323 DescribeNegation(m));
6326TEST(PointwiseTest, MakesCopyOfRhs) {
6327 list<signed char> rhs;
6332 const Matcher<
const int (&)[2]> m = Pointwise(IsHalfOf(), rhs);
6340TEST(PointwiseTest, WorksForLhsNativeArray) {
6341 const int lhs[] = {1, 2, 3};
6350TEST(PointwiseTest, WorksForRhsNativeArray) {
6351 const int rhs[] = {1, 2, 3};
6361TEST(PointwiseTest, WorksForVectorOfBool) {
6362 vector<bool> rhs(3,
false);
6364 vector<bool> lhs = rhs;
6371TEST(PointwiseTest, WorksForRhsInitializerList) {
6372 const vector<int> lhs{2, 4, 6};
6374 EXPECT_THAT(lhs, Not(Pointwise(Lt(), {3, 3, 7})));
6378TEST(PointwiseTest, RejectsWrongSize) {
6379 const double lhs[2] = {1, 2};
6380 const int rhs[1] = {0};
6383 Explain(Pointwise(Gt(), rhs), lhs));
6385 const int rhs2[3] = {0, 1, 2};
6389TEST(PointwiseTest, RejectsWrongContent) {
6390 const double lhs[3] = {1, 2, 3};
6391 const int rhs[3] = {2, 6, 4};
6392 EXPECT_THAT(lhs, Not(Pointwise(IsHalfOf(), rhs)));
6393 EXPECT_EQ(
"where the value pair (2, 6) at index #1 don't match, "
6394 "where the second/2 is 3",
6395 Explain(Pointwise(IsHalfOf(), rhs), lhs));
6398TEST(PointwiseTest, AcceptsCorrectContent) {
6399 const double lhs[3] = {1, 2, 3};
6400 const int rhs[3] = {2, 4, 6};
6402 EXPECT_EQ(
"", Explain(Pointwise(IsHalfOf(), rhs), lhs));
6405TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) {
6406 const double lhs[3] = {1, 2, 3};
6407 const int rhs[3] = {2, 4, 6};
6408 const Matcher<std::tuple<const double&, const int&>> m1 = IsHalfOf();
6410 EXPECT_EQ(
"", Explain(Pointwise(m1, rhs), lhs));
6414 const Matcher<std::tuple<double, int>> m2 = IsHalfOf();
6416 EXPECT_EQ(
"", Explain(Pointwise(m2, rhs), lhs));
6419MATCHER(PointeeEquals,
"Points to an equal value") {
6420 return ExplainMatchResult(::testing::Pointee(::testing::get<1>(arg)),
6421 ::testing::get<0>(arg), result_listener);
6424TEST(PointwiseTest, WorksWithMoveOnly) {
6425 ContainerHelper helper;
6426 EXPECT_CALL(helper, Call(Pointwise(PointeeEquals(), std::vector<int>{1, 2})));
6427 helper.Call(MakeUniquePtrs({1, 2}));
6430TEST(UnorderedPointwiseTest, DescribesSelf) {
6435 const Matcher<const vector<int>&> m = UnorderedPointwise(IsHalfOf(), rhs);
6437 "has 3 elements and there exists some permutation of elements such "
6439 " - element #0 and 1 are a pair where the first is half of the second, "
6441 " - element #1 and 2 are a pair where the first is half of the second, "
6443 " - element #2 and 3 are a pair where the first is half of the second",
6446 "doesn't have 3 elements, or there exists no permutation of elements "
6448 " - element #0 and 1 are a pair where the first is half of the second, "
6450 " - element #1 and 2 are a pair where the first is half of the second, "
6452 " - element #2 and 3 are a pair where the first is half of the second",
6453 DescribeNegation(m));
6456TEST(UnorderedPointwiseTest, MakesCopyOfRhs) {
6457 list<signed char> rhs;
6462 const Matcher<
const int (&)[2]> m = UnorderedPointwise(IsHalfOf(), rhs);
6470TEST(UnorderedPointwiseTest, WorksForLhsNativeArray) {
6471 const int lhs[] = {1, 2, 3};
6477 EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs)));
6480TEST(UnorderedPointwiseTest, WorksForRhsNativeArray) {
6481 const int rhs[] = {1, 2, 3};
6487 EXPECT_THAT(lhs, Not(UnorderedPointwise(Lt(), rhs)));
6491TEST(UnorderedPointwiseTest, WorksForRhsInitializerList) {
6492 const vector<int> lhs{2, 4, 6};
6493 EXPECT_THAT(lhs, UnorderedPointwise(Gt(), {5, 1, 3}));
6494 EXPECT_THAT(lhs, Not(UnorderedPointwise(Lt(), {1, 1, 7})));
6498TEST(UnorderedPointwiseTest, RejectsWrongSize) {
6499 const double lhs[2] = {1, 2};
6500 const int rhs[1] = {0};
6501 EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs)));
6503 Explain(UnorderedPointwise(Gt(), rhs), lhs));
6505 const int rhs2[3] = {0, 1, 2};
6506 EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs2)));
6509TEST(UnorderedPointwiseTest, RejectsWrongContent) {
6510 const double lhs[3] = {1, 2, 3};
6511 const int rhs[3] = {2, 6, 6};
6512 EXPECT_THAT(lhs, Not(UnorderedPointwise(IsHalfOf(), rhs)));
6513 EXPECT_EQ(
"where the following elements don't match any matchers:\n"
6515 Explain(UnorderedPointwise(IsHalfOf(), rhs), lhs));
6518TEST(UnorderedPointwiseTest, AcceptsCorrectContentInSameOrder) {
6519 const double lhs[3] = {1, 2, 3};
6520 const int rhs[3] = {2, 4, 6};
6521 EXPECT_THAT(lhs, UnorderedPointwise(IsHalfOf(), rhs));
6524TEST(UnorderedPointwiseTest, AcceptsCorrectContentInDifferentOrder) {
6525 const double lhs[3] = {1, 2, 3};
6526 const int rhs[3] = {6, 4, 2};
6527 EXPECT_THAT(lhs, UnorderedPointwise(IsHalfOf(), rhs));
6530TEST(UnorderedPointwiseTest, AllowsMonomorphicInnerMatcher) {
6531 const double lhs[3] = {1, 2, 3};
6532 const int rhs[3] = {4, 6, 2};
6533 const Matcher<std::tuple<const double&, const int&>> m1 = IsHalfOf();
6538 const Matcher<std::tuple<double, int>> m2 = IsHalfOf();
6542TEST(UnorderedPointwiseTest, WorksWithMoveOnly) {
6543 ContainerHelper helper;
6544 EXPECT_CALL(helper, Call(UnorderedPointwise(PointeeEquals(),
6545 std::vector<int>{1, 2})));
6546 helper.Call(MakeUniquePtrs({2, 1}));
6551template <
typename T>
6552class SampleOptional {
6554 using value_type =
T;
6555 explicit SampleOptional(
T value)
6566TEST(OptionalTest, DescribesSelf) {
6567 const Matcher<SampleOptional<int>> m = Optional(Eq(1));
6568 EXPECT_EQ(
"value is equal to 1", Describe(m));
6571TEST(OptionalTest, ExplainsSelf) {
6572 const Matcher<SampleOptional<int>> m = Optional(Eq(1));
6573 EXPECT_EQ(
"whose value 1 matches", Explain(m, SampleOptional<int>(1)));
6574 EXPECT_EQ(
"whose value 2 doesn't match", Explain(m, SampleOptional<int>(2)));
6577TEST(OptionalTest, MatchesNonEmptyOptional) {
6578 const Matcher<SampleOptional<int>> m1 = Optional(1);
6579 const Matcher<SampleOptional<int>> m2 = Optional(Eq(2));
6580 const Matcher<SampleOptional<int>> m3 = Optional(Lt(3));
6581 SampleOptional<int> opt(1);
6587TEST(OptionalTest, DoesNotMatchNullopt) {
6588 const Matcher<SampleOptional<int>> m = Optional(1);
6589 SampleOptional<int> empty;
6593TEST(OptionalTest, WorksWithMoveOnly) {
6594 Matcher<SampleOptional<std::unique_ptr<int>>> m = Optional(Eq(
nullptr));
6595 EXPECT_TRUE(m.Matches(SampleOptional<std::unique_ptr<int>>(
nullptr)));
6598class SampleVariantIntString {
6601 SampleVariantIntString(
const std::string& s) :
s_(s),
has_int_(
false) {}
6603 template <
typename T>
6604 friend bool holds_alternative(
const SampleVariantIntString&
value) {
6605 return value.has_int_ == std::is_same<T, int>::value;
6608 template <
typename T>
6609 friend const T& get(
const SampleVariantIntString&
value) {
6610 return value.get_impl(
static_cast<T*
>(
nullptr));
6614 const int& get_impl(
int*)
const {
return i_; }
6615 const std::string& get_impl(std::string*)
const {
return s_; }
6622TEST(VariantTest, DescribesSelf) {
6623 const Matcher<SampleVariantIntString> m = VariantWith<int>(Eq(1));
6624 EXPECT_THAT(Describe(m), ContainsRegex(
"is a variant<> with value of type "
6625 "'.*' and the value is equal to 1"));
6628TEST(VariantTest, ExplainsSelf) {
6629 const Matcher<SampleVariantIntString> m = VariantWith<int>(Eq(1));
6630 EXPECT_THAT(Explain(m, SampleVariantIntString(1)),
6631 ContainsRegex(
"whose value 1"));
6632 EXPECT_THAT(Explain(m, SampleVariantIntString(
"A")),
6633 HasSubstr(
"whose value is not of type '"));
6634 EXPECT_THAT(Explain(m, SampleVariantIntString(2)),
6635 "whose value 2 doesn't match");
6638TEST(VariantTest, FullMatch) {
6639 Matcher<SampleVariantIntString> m = VariantWith<int>(Eq(1));
6640 EXPECT_TRUE(m.Matches(SampleVariantIntString(1)));
6642 m = VariantWith<std::string>(Eq(
"1"));
6643 EXPECT_TRUE(m.Matches(SampleVariantIntString(
"1")));
6646TEST(VariantTest, TypeDoesNotMatch) {
6647 Matcher<SampleVariantIntString> m = VariantWith<int>(Eq(1));
6650 m = VariantWith<std::string>(Eq(
"1"));
6654TEST(VariantTest, InnerDoesNotMatch) {
6655 Matcher<SampleVariantIntString> m = VariantWith<int>(Eq(1));
6658 m = VariantWith<std::string>(Eq(
"1"));
6662class SampleAnyType {
6664 explicit SampleAnyType(
int i) :
index_(0),
i_(
i) {}
6665 explicit SampleAnyType(
const std::string& s) :
index_(1),
s_(s) {}
6667 template <
typename T>
6668 friend const T* any_cast(
const SampleAnyType* any) {
6669 return any->get_impl(
static_cast<T*
>(
nullptr));
6677 const int* get_impl(
int*)
const {
return index_ == 0 ? &
i_ :
nullptr; }
6678 const std::string* get_impl(std::string*)
const {
6679 return index_ == 1 ? &
s_ :
nullptr;
6683TEST(AnyWithTest, FullMatch) {
6684 Matcher<SampleAnyType> m = AnyWith<int>(Eq(1));
6688TEST(AnyWithTest, TestBadCastType) {
6689 Matcher<SampleAnyType> m = AnyWith<std::string>(Eq(
"fail"));
6693TEST(AnyWithTest, TestUseInContainers) {
6694 std::vector<SampleAnyType>
a;
6699 a, ElementsAreArray({AnyWith<int>(1), AnyWith<int>(2), AnyWith<int>(3)}));
6701 std::vector<SampleAnyType> b;
6702 b.emplace_back(
"hello");
6703 b.emplace_back(
"merhaba");
6704 b.emplace_back(
"salut");
6705 EXPECT_THAT(b, ElementsAreArray({AnyWith<std::string>(
"hello"),
6706 AnyWith<std::string>(
"merhaba"),
6707 AnyWith<std::string>(
"salut")}));
6709TEST(AnyWithTest, TestCompare) {
6710 EXPECT_THAT(SampleAnyType(1), AnyWith<int>(Gt(0)));
6713TEST(AnyWithTest, DescribesSelf) {
6714 const Matcher<const SampleAnyType&> m = AnyWith<int>(Eq(1));
6715 EXPECT_THAT(Describe(m), ContainsRegex(
"is an 'any' type with value of type "
6716 "'.*' and the value is equal to 1"));
6719TEST(AnyWithTest, ExplainsSelf) {
6720 const Matcher<const SampleAnyType&> m = AnyWith<int>(Eq(1));
6722 EXPECT_THAT(Explain(m, SampleAnyType(1)), ContainsRegex(
"whose value 1"));
6724 HasSubstr(
"whose value is not of type '"));
6725 EXPECT_THAT(Explain(m, SampleAnyType(2)),
"whose value 2 doesn't match");
6728TEST(PointeeTest, WorksOnMoveOnlyType) {
6729 std::unique_ptr<int>
p(
new int(3));
6734TEST(NotTest, WorksOnMoveOnlyType) {
6735 std::unique_ptr<int>
p(
new int(3));
6742TEST(ArgsTest, AcceptsZeroTemplateArg) {
6743 const std::tuple<int, bool> t(5,
true);
6748TEST(ArgsTest, AcceptsOneTemplateArg) {
6749 const std::tuple<int, bool> t(5,
true);
6751 EXPECT_THAT(t, Args<1>(Eq(std::make_tuple(
true))));
6752 EXPECT_THAT(t, Not(Args<1>(Eq(std::make_tuple(
false)))));
6755TEST(ArgsTest, AcceptsTwoTemplateArgs) {
6756 const std::tuple<short, int, long> t(4, 5, 6L);
6763TEST(ArgsTest, AcceptsRepeatedTemplateArgs) {
6764 const std::tuple<short, int, long> t(4, 5, 6L);
6769TEST(ArgsTest, AcceptsDecreasingTemplateArgs) {
6770 const std::tuple<short, int, long> t(4, 5, 6L);
6776 return std::get<0>(arg) + std::get<1>(arg) + std::get<2>(arg) == 0;
6779TEST(ArgsTest, AcceptsMoreTemplateArgsThanArityOfOriginalTuple) {
6780 EXPECT_THAT(std::make_tuple(-1, 2), (Args<0, 0, 1>(SumIsZero())));
6781 EXPECT_THAT(std::make_tuple(1, 2), Not(Args<0, 0, 1>(SumIsZero())));
6784TEST(ArgsTest, CanBeNested) {
6785 const std::tuple<short, int, long, int> t(4, 5, 6L, 6);
6786 EXPECT_THAT(t, (Args<1, 2, 3>(Args<1, 2>(Eq()))));
6787 EXPECT_THAT(t, (Args<0, 1, 3>(Args<0, 2>(Lt()))));
6790TEST(ArgsTest, CanMatchTupleByValue) {
6791 typedef std::tuple<char, int, int> Tuple3;
6792 const Matcher<Tuple3> m = Args<1, 2>(Lt());
6797TEST(ArgsTest, CanMatchTupleByReference) {
6798 typedef std::tuple<char, char, int> Tuple3;
6799 const Matcher<const Tuple3&> m = Args<0, 1>(Lt());
6809TEST(ArgsTest, AcceptsTenTemplateArgs) {
6810 EXPECT_THAT(std::make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
6811 (Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
6812 PrintsAs(
"(9, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
6813 EXPECT_THAT(std::make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
6814 Not(Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
6815 PrintsAs(
"(0, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
6818TEST(ArgsTest, DescirbesSelfCorrectly) {
6819 const Matcher<std::tuple<int, bool, char> > m = Args<2, 0>(Lt());
6820 EXPECT_EQ(
"are a tuple whose fields (#2, #0) are a pair where "
6821 "the first < the second",
6825TEST(ArgsTest, DescirbesNestedArgsCorrectly) {
6826 const Matcher<const std::tuple<int, bool, char, int>&> m =
6827 Args<0, 2, 3>(Args<2, 0>(Lt()));
6828 EXPECT_EQ(
"are a tuple whose fields (#0, #2, #3) are a tuple "
6829 "whose fields (#2, #0) are a pair where the first < the second",
6833TEST(ArgsTest, DescribesNegationCorrectly) {
6834 const Matcher<std::tuple<int, char> > m = Args<1, 0>(Gt());
6835 EXPECT_EQ(
"are a tuple whose fields (#1, #0) aren't a pair "
6836 "where the first > the second",
6837 DescribeNegation(m));
6840TEST(ArgsTest, ExplainsMatchResultWithoutInnerExplanation) {
6841 const Matcher<std::tuple<bool, int, int> > m = Args<1, 2>(Eq());
6842 EXPECT_EQ(
"whose fields (#1, #2) are (42, 42)",
6843 Explain(m, std::make_tuple(
false, 42, 42)));
6844 EXPECT_EQ(
"whose fields (#1, #2) are (42, 43)",
6845 Explain(m, std::make_tuple(
false, 42, 43)));
6849class LessThanMatcher :
public MatcherInterface<std::tuple<char, int> > {
6851 void DescribeTo(::std::ostream* )
const override {}
6853 bool MatchAndExplain(std::tuple<char, int>
value,
6854 MatchResultListener* listener)
const override {
6857 *listener <<
"where the first value is " <<
diff
6858 <<
" more than the second";
6864Matcher<std::tuple<char, int> > LessThan() {
6865 return MakeMatcher(
new LessThanMatcher);
6868TEST(ArgsTest, ExplainsMatchResultWithInnerExplanation) {
6869 const Matcher<std::tuple<char, int, int> > m = Args<0, 2>(LessThan());
6871 "whose fields (#0, #2) are ('a' (97, 0x61), 42), "
6872 "where the first value is 55 more than the second",
6873 Explain(m, std::make_tuple(
'a', 42, 42)));
6874 EXPECT_EQ(
"whose fields (#0, #2) are ('\\0', 43)",
6875 Explain(m, std::make_tuple(
'\0', 42, 43)));
6880 enum Behavior { kInitialSuccess, kAlwaysFail, kFlaky };
6885 class MockMatcher :
public MatcherInterface<Behavior> {
6887 bool MatchAndExplain(Behavior behavior,
6888 MatchResultListener* listener)
const override {
6889 *listener <<
"[MatchAndExplain]";
6891 case kInitialSuccess:
6895 return !listener->IsInterested();
6905 return listener->IsInterested();
6908 GTEST_LOG_(FATAL) <<
"This should never be reached";
6912 void DescribeTo(ostream* os)
const override { *os <<
"[DescribeTo]"; }
6914 void DescribeNegationTo(ostream* os)
const override {
6915 *os <<
"[DescribeNegationTo]";
6919 AssertionResult RunPredicateFormatter(Behavior behavior) {
6920 auto matcher = MakeMatcher(
new MockMatcher);
6921 PredicateFormatterFromMatcher<Matcher<Behavior>> predicate_formatter(
6923 return predicate_formatter(
"dummy-name", behavior);
6927TEST_F(PredicateFormatterFromMatcherTest, ShortCircuitOnSuccess) {
6928 AssertionResult result = RunPredicateFormatter(kInitialSuccess);
6934TEST_F(PredicateFormatterFromMatcherTest, NoShortCircuitOnFailure) {
6935 AssertionResult result = RunPredicateFormatter(kAlwaysFail);
6937 std::string expect =
6938 "Value of: dummy-name\nExpected: [DescribeTo]\n"
6940 OfType(internal::GetTypeName<Behavior>()) +
", [MatchAndExplain]";
6944TEST_F(PredicateFormatterFromMatcherTest, DetectsFlakyShortCircuit) {
6945 AssertionResult result = RunPredicateFormatter(kFlaky);
6947 std::string expect =
6948 "Value of: dummy-name\nExpected: [DescribeTo]\n"
6949 " The matcher failed on the initial attempt; but passed when rerun to "
6950 "generate the explanation.\n"
6952 OfType(internal::GetTypeName<Behavior>()) +
", [MatchAndExplain]";
6958TEST(ElementsAreTest, CanDescribeExpectingNoElement) {
6959 Matcher<const vector<int>&> m = ElementsAre();
6963TEST(ElementsAreTest, CanDescribeExpectingOneElement) {
6964 Matcher<vector<int>> m = ElementsAre(Gt(5));
6965 EXPECT_EQ(
"has 1 element that is > 5", Describe(m));
6968TEST(ElementsAreTest, CanDescribeExpectingManyElements) {
6969 Matcher<list<std::string>> m = ElementsAre(StrEq(
"one"),
"two");
6971 "has 2 elements where\n"
6972 "element #0 is equal to \"one\",\n"
6973 "element #1 is equal to \"two\"",
6977TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) {
6978 Matcher<vector<int>> m = ElementsAre();
6979 EXPECT_EQ(
"isn't empty", DescribeNegation(m));
6982TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElment) {
6983 Matcher<const list<int>&> m = ElementsAre(Gt(5));
6985 "doesn't have 1 element, or\n"
6986 "element #0 isn't > 5",
6987 DescribeNegation(m));
6990TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) {
6991 Matcher<const list<std::string>&> m = ElementsAre(
"one",
"two");
6993 "doesn't have 2 elements, or\n"
6994 "element #0 isn't equal to \"one\", or\n"
6995 "element #1 isn't equal to \"two\"",
6996 DescribeNegation(m));
6999TEST(ElementsAreTest, DoesNotExplainTrivialMatch) {
7000 Matcher<const list<int>&> m = ElementsAre(1, Ne(2));
7002 list<int> test_list;
7003 test_list.push_back(1);
7004 test_list.push_back(3);
7008TEST(ElementsAreTest, ExplainsNonTrivialMatch) {
7009 Matcher<const vector<int>&> m =
7010 ElementsAre(GreaterThan(1), 0, GreaterThan(2));
7012 const int a[] = {10, 0, 100};
7013 vector<int> test_vector(std::begin(
a), std::end(
a));
7015 "whose element #0 matches, which is 9 more than 1,\n"
7016 "and whose element #2 matches, which is 98 more than 2",
7017 Explain(m, test_vector));
7020TEST(ElementsAreTest, CanExplainMismatchWrongSize) {
7021 Matcher<const list<int>&> m = ElementsAre(1, 3);
7023 list<int> test_list;
7027 test_list.push_back(1);
7028 EXPECT_EQ(
"which has 1 element", Explain(m, test_list));
7031TEST(ElementsAreTest, CanExplainMismatchRightSize) {
7032 Matcher<const vector<int>&> m = ElementsAre(1, GreaterThan(5));
7037 EXPECT_EQ(
"whose element #0 doesn't match", Explain(m, v));
7040 EXPECT_EQ(
"whose element #1 doesn't match, which is 4 less than 5",
7044TEST(ElementsAreTest, MatchesOneElementVector) {
7045 vector<std::string> test_vector;
7046 test_vector.push_back(
"test string");
7048 EXPECT_THAT(test_vector, ElementsAre(StrEq(
"test string")));
7051TEST(ElementsAreTest, MatchesOneElementList) {
7052 list<std::string> test_list;
7053 test_list.push_back(
"test string");
7055 EXPECT_THAT(test_list, ElementsAre(
"test string"));
7058TEST(ElementsAreTest, MatchesThreeElementVector) {
7059 vector<std::string> test_vector;
7060 test_vector.push_back(
"one");
7061 test_vector.push_back(
"two");
7062 test_vector.push_back(
"three");
7064 EXPECT_THAT(test_vector, ElementsAre(
"one", StrEq(
"two"), _));
7067TEST(ElementsAreTest, MatchesOneElementEqMatcher) {
7068 vector<int> test_vector;
7069 test_vector.push_back(4);
7074TEST(ElementsAreTest, MatchesOneElementAnyMatcher) {
7075 vector<int> test_vector;
7076 test_vector.push_back(4);
7081TEST(ElementsAreTest, MatchesOneElementValue) {
7082 vector<int> test_vector;
7083 test_vector.push_back(4);
7088TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) {
7089 vector<int> test_vector;
7090 test_vector.push_back(1);
7091 test_vector.push_back(2);
7092 test_vector.push_back(3);
7094 EXPECT_THAT(test_vector, ElementsAre(1, Eq(2), _));
7097TEST(ElementsAreTest, MatchesTenElementVector) {
7098 const int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
7099 vector<int> test_vector(std::begin(
a), std::end(
a));
7104 ElementsAre(0, Ge(0), _, 3, 4, Ne(2), Eq(6), 7, 8, _));
7107TEST(ElementsAreTest, DoesNotMatchWrongSize) {
7108 vector<std::string> test_vector;
7109 test_vector.push_back(
"test string");
7110 test_vector.push_back(
"test string");
7112 Matcher<vector<std::string>> m = ElementsAre(StrEq(
"test string"));
7116TEST(ElementsAreTest, DoesNotMatchWrongValue) {
7117 vector<std::string> test_vector;
7118 test_vector.push_back(
"other string");
7120 Matcher<vector<std::string>> m = ElementsAre(StrEq(
"test string"));
7124TEST(ElementsAreTest, DoesNotMatchWrongOrder) {
7125 vector<std::string> test_vector;
7126 test_vector.push_back(
"one");
7127 test_vector.push_back(
"three");
7128 test_vector.push_back(
"two");
7130 Matcher<vector<std::string>> m =
7131 ElementsAre(StrEq(
"one"), StrEq(
"two"), StrEq(
"three"));
7135TEST(ElementsAreTest, WorksForNestedContainer) {
7136 constexpr std::array<const char*, 2> strings = {{
"Hi",
"world"}};
7138 vector<list<char>> nested;
7139 for (
const auto& s : strings) {
7140 nested.emplace_back(s, s + strlen(s));
7143 EXPECT_THAT(nested, ElementsAre(ElementsAre(
'H', Ne(
'e')),
7144 ElementsAre(
'w',
'o', _, _,
'd')));
7145 EXPECT_THAT(nested, Not(ElementsAre(ElementsAre(
'H',
'e'),
7146 ElementsAre(
'w',
'o', _, _,
'd'))));
7149TEST(ElementsAreTest, WorksWithByRefElementMatchers) {
7150 int a[] = {0, 1, 2};
7151 vector<int> v(std::begin(
a), std::end(
a));
7153 EXPECT_THAT(v, ElementsAre(Ref(v[0]), Ref(v[1]), Ref(v[2])));
7154 EXPECT_THAT(v, Not(ElementsAre(Ref(v[0]), Ref(v[1]), Ref(
a[2]))));
7157TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) {
7158 int a[] = {0, 1, 2};
7159 vector<int> v(std::begin(
a), std::end(
a));
7162 EXPECT_THAT(&v, Not(Pointee(ElementsAre(0, _, 3))));
7165TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) {
7166 int array[] = {0, 1, 2};
7172class NativeArrayPassedAsPointerAndSize {
7174 NativeArrayPassedAsPointerAndSize() {}
7176 MOCK_METHOD(
void, Helper, (
int* array,
int size));
7182TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) {
7183 int array[] = {0, 1};
7184 ::std::tuple<int*, size_t> array_as_tuple(array, 2);
7188 NativeArrayPassedAsPointerAndSize helper;
7189 EXPECT_CALL(helper, Helper(_, _)).With(ElementsAre(0, 1));
7190 helper.Helper(array, 2);
7193TEST(ElementsAreTest, WorksWithTwoDimensionalNativeArray) {
7194 const char a2[][3] = {
"hi",
"lo"};
7195 EXPECT_THAT(a2, ElementsAre(ElementsAre(
'h',
'i',
'\0'),
7196 ElementsAre(
'l',
'o',
'\0')));
7197 EXPECT_THAT(a2, ElementsAre(StrEq(
"hi"), StrEq(
"lo")));
7198 EXPECT_THAT(a2, ElementsAre(Not(ElementsAre(
'h',
'o',
'\0')),
7199 ElementsAre(
'l',
'o',
'\0')));
7202TEST(ElementsAreTest, AcceptsStringLiteral) {
7203 std::string array[] = {
"hi",
"one",
"two"};
7204 EXPECT_THAT(array, ElementsAre(
"hi",
"one",
"two"));
7205 EXPECT_THAT(array, Not(ElementsAre(
"hi",
"one",
"too")));
7209extern const char kHi[];
7211TEST(ElementsAreTest, AcceptsArrayWithUnknownSize) {
7215 std::string array1[] = {
"hi"};
7218 std::string array2[] = {
"ho"};
7222const char kHi[] =
"hi";
7224TEST(ElementsAreTest, MakesCopyOfArguments) {
7228 ::testing::internal::ElementsAreMatcher<std::tuple<int, int>>
7229 polymorphic_matcher = ElementsAre(
x,
y);
7232 const int array1[] = {1, 2};
7234 const int array2[] = {0, 0};
7242TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) {
7243 const int a[] = {1, 2, 3};
7245 vector<int> test_vector(std::begin(
a), std::end(
a));
7252TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) {
7253 std::array<const char*, 3>
a = {{
"one",
"two",
"three"}};
7255 vector<std::string> test_vector(std::begin(
a), std::end(
a));
7256 EXPECT_THAT(test_vector, ElementsAreArray(
a.data(),
a.size()));
7258 const char**
p =
a.data();
7259 test_vector[0] =
"1";
7260 EXPECT_THAT(test_vector, Not(ElementsAreArray(
p,
a.size())));
7263TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) {
7264 const char*
a[] = {
"one",
"two",
"three"};
7266 vector<std::string> test_vector(std::begin(
a), std::end(
a));
7269 test_vector[0] =
"1";
7273TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) {
7274 const Matcher<std::string> kMatcherArray[] = {StrEq(
"one"), StrEq(
"two"),
7277 vector<std::string> test_vector;
7278 test_vector.push_back(
"one");
7279 test_vector.push_back(
"two");
7280 test_vector.push_back(
"three");
7281 EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray));
7283 test_vector.push_back(
"three");
7284 EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray)));
7287TEST(ElementsAreArrayTest, CanBeCreatedWithVector) {
7288 const int a[] = {1, 2, 3};
7289 vector<int> test_vector(std::begin(
a), std::end(
a));
7290 const vector<int>
expected(std::begin(
a), std::end(
a));
7292 test_vector.push_back(4);
7296TEST(ElementsAreArrayTest, TakesInitializerList) {
7297 const int a[5] = {1, 2, 3, 4, 5};
7299 EXPECT_THAT(
a, Not(ElementsAreArray({1, 2, 3, 5, 4})));
7300 EXPECT_THAT(
a, Not(ElementsAreArray({1, 2, 3, 4, 6})));
7303TEST(ElementsAreArrayTest, TakesInitializerListOfCStrings) {
7304 const std::string
a[5] = {
"a",
"b",
"c",
"d",
"e"};
7305 EXPECT_THAT(
a, ElementsAreArray({
"a",
"b",
"c",
"d",
"e"}));
7306 EXPECT_THAT(
a, Not(ElementsAreArray({
"a",
"b",
"c",
"e",
"d"})));
7307 EXPECT_THAT(
a, Not(ElementsAreArray({
"a",
"b",
"c",
"d",
"ef"})));
7310TEST(ElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {
7311 const int a[5] = {1, 2, 3, 4, 5};
7312 EXPECT_THAT(
a, ElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(5)}));
7313 EXPECT_THAT(
a, Not(ElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(6)})));
7316TEST(ElementsAreArrayTest, TakesInitializerListOfDifferentTypedMatchers) {
7317 const int a[5] = {1, 2, 3, 4, 5};
7322 a, ElementsAreArray<Matcher<int>>({Eq(1), Ne(-2), Ge(3), Le(4), Eq(5)}));
7324 {Eq(1), Ne(-2), Ge(3), Le(4), Eq(6)})));
7327TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherVector) {
7328 const int a[] = {1, 2, 3};
7329 const Matcher<int> kMatchers[] = {Eq(1), Eq(2), Eq(3)};
7330 vector<int> test_vector(std::begin(
a), std::end(
a));
7331 const vector<Matcher<int>>
expected(std::begin(kMatchers),
7332 std::end(kMatchers));
7334 test_vector.push_back(4);
7338TEST(ElementsAreArrayTest, CanBeCreatedWithIteratorRange) {
7339 const int a[] = {1, 2, 3};
7340 const vector<int> test_vector(std::begin(
a), std::end(
a));
7341 const vector<int>
expected(std::begin(
a), std::end(
a));
7344 EXPECT_THAT(test_vector, ElementsAreArray(std::begin(
a), std::end(
a)));
7346 int*
const null_int =
nullptr;
7347 EXPECT_THAT(test_vector, Not(ElementsAreArray(null_int, null_int)));
7348 EXPECT_THAT((vector<int>()), ElementsAreArray(null_int, null_int));
7353TEST(ElementsAreArrayTest, WorksWithNativeArray) {
7354 ::std::string
a[] = {
"hi",
"ho"};
7355 ::std::string b[] = {
"hi",
"ho"};
7362TEST(ElementsAreArrayTest, SourceLifeSpan) {
7363 const int a[] = {1, 2, 3};
7364 vector<int> test_vector(std::begin(
a), std::end(
a));
7365 vector<int> expect(std::begin(
a), std::end(
a));
7366 ElementsAreArrayMatcher<int> matcher_maker =
7367 ElementsAreArray(expect.begin(), expect.end());
7371 for (
int&
i : expect) {
7375 test_vector.push_back(3);
7383MATCHER(IsEven,
"") {
return (arg % 2) == 0; }
7385TEST(MatcherMacroTest, Works) {
7386 const Matcher<int> m = IsEven();
7391 EXPECT_EQ(
"not (is even)", DescribeNegation(m));
7397MATCHER(IsEven2, negation ?
"is odd" :
"is even") {
7398 if ((arg % 2) == 0) {
7401 *result_listener <<
"OK";
7404 *result_listener <<
"% 2 == " << (arg % 2);
7412 std::string(negation ?
"doesn't equal" :
"equals") +
" the sum of " +
7414 if (arg == (
x +
y)) {
7415 *result_listener <<
"OK";
7420 if (result_listener->stream() !=
nullptr) {
7421 *result_listener->stream() <<
"diff == " << (
x +
y - arg);
7429TEST(MatcherMacroTest, DescriptionCanReferenceNegationAndParameters) {
7430 const Matcher<int> m1 = IsEven2();
7432 EXPECT_EQ(
"is odd", DescribeNegation(m1));
7434 const Matcher<int> m2 = EqSumOf(5, 9);
7435 EXPECT_EQ(
"equals the sum of 5 and 9", Describe(m2));
7436 EXPECT_EQ(
"doesn't equal the sum of 5 and 9", DescribeNegation(m2));
7440TEST(MatcherMacroTest, CanExplainMatchResult) {
7441 const Matcher<int> m1 = IsEven2();
7445 const Matcher<int> m2 = EqSumOf(1, 2);
7447 EXPECT_EQ(
"diff == -1", Explain(m2, 4));
7454 StaticAssertTypeEq<::std::string, arg_type>();
7458MATCHER(IsEmptyStringByRef,
"") {
7459 StaticAssertTypeEq<const ::std::string&, arg_type>();
7463TEST(MatcherMacroTest, CanReferenceArgType) {
7464 const Matcher<::std::string> m1 = IsEmptyString();
7467 const Matcher<const ::std::string&> m2 = IsEmptyStringByRef();
7473namespace matcher_test {
7474MATCHER(IsOdd,
"") {
return (arg % 2) != 0; }
7477TEST(MatcherMacroTest, WorksInNamespace) {
7485 return Value(arg, matcher_test::IsOdd()) && arg > 0;
7488TEST(MatcherMacroTest, CanBeComposedUsingValue) {
7496MATCHER_P(IsGreaterThan32And, n,
"") {
return arg > 32 && arg > n; }
7498TEST(MatcherPMacroTest, Works) {
7499 const Matcher<int> m = IsGreaterThan32And(5);
7503 EXPECT_EQ(
"is greater than 32 and 5", Describe(m));
7504 EXPECT_EQ(
"not (is greater than 32 and 5)", DescribeNegation(m));
7510MATCHER_P(_is_Greater_Than32and_, n,
"") {
return arg > 32 && arg > n; }
7512TEST(MatcherPMacroTest, GeneratesCorrectDescription) {
7513 const Matcher<int> m = _is_Greater_Than32and_(5);
7515 EXPECT_EQ(
"is greater than 32 and 5", Describe(m));
7516 EXPECT_EQ(
"not (is greater than 32 and 5)", DescribeNegation(m));
7524class UncopyableFoo {
7528 UncopyableFoo(
const UncopyableFoo&) =
delete;
7529 void operator=(
const UncopyableFoo&) =
delete;
7535MATCHER_P(ReferencesUncopyable, variable,
"") {
return &arg == &variable; }
7537TEST(MatcherPMacroTest, WorksWhenExplicitlyInstantiatedWithReference) {
7538 UncopyableFoo foo1(
'1'), foo2(
'2');
7539 const Matcher<const UncopyableFoo&> m =
7540 ReferencesUncopyable<const UncopyableFoo&>(foo1);
7549 EXPECT_EQ(
"references uncopyable 1-byte object <31>", Describe(m));
7556 StaticAssertTypeEq<int, foo_type>();
7557 StaticAssertTypeEq<long, bar_type>();
7558 StaticAssertTypeEq<char, baz_type>();
7562TEST(MatcherPnMacroTest, CanReferenceParamTypes) {
7563 EXPECT_THAT(0, ParamTypesAreIntLongAndChar(10, 20L,
'a'));
7569MATCHER_P2(ReferencesAnyOf, variable1, variable2,
"") {
7570 return &arg == &variable1 || &arg == &variable2;
7573TEST(MatcherPnMacroTest, WorksWhenExplicitlyInstantiatedWithReferences) {
7574 UncopyableFoo foo1(
'1'), foo2(
'2'), foo3(
'3');
7575 const Matcher<const UncopyableFoo&> const_m =
7576 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
7582 const Matcher<UncopyableFoo&> m =
7583 ReferencesAnyOf<UncopyableFoo&, UncopyableFoo&>(foo1, foo2);
7590TEST(MatcherPnMacroTest,
7591 GeneratesCorretDescriptionWhenExplicitlyInstantiatedWithReferences) {
7592 UncopyableFoo foo1(
'1'), foo2(
'2');
7593 const Matcher<const UncopyableFoo&> m =
7594 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
7600 EXPECT_EQ(
"references any of (1-byte object <31>, 1-byte object <32>)",
7606MATCHER_P2(IsNotInClosedRange, low, hi,
"") {
return arg < low || arg > hi; }
7608TEST(MatcherPnMacroTest, Works) {
7609 const Matcher<const long&> m = IsNotInClosedRange(10, 20);
7613 EXPECT_EQ(
"is not in closed range (10, 20)", Describe(m));
7614 EXPECT_EQ(
"not (is not in closed range (10, 20))", DescribeNegation(m));
7622MATCHER(EqualsSumOf,
"") {
return arg == 0; }
7624MATCHER_P2(EqualsSumOf,
a, b,
"") {
return arg ==
a + b; }
7626MATCHER_P4(EqualsSumOf,
a, b,
c, d,
"") {
return arg ==
a + b +
c + d; }
7627MATCHER_P5(EqualsSumOf,
a, b,
c, d, e,
"") {
return arg ==
a + b +
c + d + e; }
7629 return arg ==
a + b +
c + d + e + f;
7632 return arg ==
a + b +
c + d + e + f + g;
7635 return arg ==
a + b +
c + d + e + f + g + h;
7637MATCHER_P9(EqualsSumOf,
a, b,
c, d, e, f, g, h,
i,
"") {
7638 return arg ==
a + b +
c + d + e + f + g + h +
i;
7640MATCHER_P10(EqualsSumOf,
a, b,
c, d, e, f, g, h,
i, j,
"") {
7641 return arg ==
a + b +
c + d + e + f + g + h +
i + j;
7644TEST(MatcherPnMacroTest, CanBeOverloadedOnNumberOfParameters) {
7650 EXPECT_THAT(12345, EqualsSumOf(10000, 2000, 300, 40, 5));
7652 EqualsSumOf(::std::string(
"a"),
'b',
'c',
"d",
"e",
'f'));
7654 EqualsSumOf(::std::string(
"a"),
'b',
'c',
"d",
"e",
'f',
'g'));
7655 EXPECT_THAT(
"abcdefgh", EqualsSumOf(::std::string(
"a"),
'b',
'c',
"d",
"e",
7657 EXPECT_THAT(
"abcdefghi", EqualsSumOf(::std::string(
"a"),
'b',
'c',
"d",
"e",
7658 'f',
'g',
"h",
'i'));
7660 EqualsSumOf(::std::string(
"a"),
'b',
'c',
"d",
"e",
'f',
'g',
"h",
7661 'i', ::std::string(
"j")));
7667 EXPECT_THAT(-1234, Not(EqualsSumOf(1000, 200, 30, 4)));
7668 EXPECT_THAT(-12345, Not(EqualsSumOf(10000, 2000, 300, 40, 5)));
7670 Not(EqualsSumOf(::std::string(
"a"),
'b',
'c',
"d",
"e",
'f')));
7671 EXPECT_THAT(
"abcdefg ", Not(EqualsSumOf(::std::string(
"a"),
'b',
'c',
"d",
7673 EXPECT_THAT(
"abcdefgh ", Not(EqualsSumOf(::std::string(
"a"),
'b',
'c',
"d",
7674 "e",
'f',
'g',
"h")));
7675 EXPECT_THAT(
"abcdefghi ", Not(EqualsSumOf(::std::string(
"a"),
'b',
'c',
"d",
7676 "e",
'f',
'g',
"h",
'i')));
7678 Not(EqualsSumOf(::std::string(
"a"),
'b',
'c',
"d",
"e",
'f',
'g',
7679 "h",
'i', ::std::string(
"j"))));
7684TEST(MatcherPnMacroTest, WorksForDifferentParameterTypes) {
7685 EXPECT_THAT(123, EqualsSumOf(100L, 20,
static_cast<char>(3)));
7686 EXPECT_THAT(
"abcd", EqualsSumOf(::std::string(
"a"),
"b",
'c',
"d"));
7688 EXPECT_THAT(124, Not(EqualsSumOf(100L, 20,
static_cast<char>(3))));
7689 EXPECT_THAT(
"abcde", Not(EqualsSumOf(::std::string(
"a"),
"b",
'c',
"d")));
7696 std::string prefix_str(prefix);
7697 char suffix_char =
static_cast<char>(suffix);
7698 return arg == prefix_str + suffix_char;
7701TEST(MatcherPnMacroTest, SimpleTypePromotion) {
7702 Matcher<std::string> no_promo = EqConcat(std::string(
"foo"),
't');
7703 Matcher<const std::string&> promo = EqConcat(
"foo",
static_cast<int>(
't'));
7712TEST(MatcherPnMacroTest, TypesAreCorrect) {
7714 EqualsSumOfMatcher a0 = EqualsSumOf();
7717 EqualsSumOfMatcherP<int> a1 = EqualsSumOf(1);
7721 EqualsSumOfMatcherP2<int, char> a2 = EqualsSumOf(1,
'2');
7722 EqualsSumOfMatcherP3<int, int, char> a3 = EqualsSumOf(1, 2,
'3');
7723 EqualsSumOfMatcherP4<int, int, int, char> a4 = EqualsSumOf(1, 2, 3,
'4');
7724 EqualsSumOfMatcherP5<int, int, int, int, char> a5 =
7725 EqualsSumOf(1, 2, 3, 4,
'5');
7726 EqualsSumOfMatcherP6<int, int, int, int, int, char> a6 =
7727 EqualsSumOf(1, 2, 3, 4, 5,
'6');
7728 EqualsSumOfMatcherP7<int, int, int, int, int, int, char> a7 =
7729 EqualsSumOf(1, 2, 3, 4, 5, 6,
'7');
7730 EqualsSumOfMatcherP8<int, int, int, int, int, int, int, char> a8 =
7731 EqualsSumOf(1, 2, 3, 4, 5, 6, 7,
'8');
7732 EqualsSumOfMatcherP9<int, int, int, int, int, int, int, int, char> a9 =
7733 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8,
'9');
7734 EqualsSumOfMatcherP10<int, int, int, int, int, int, int, int, int, char> a10 =
7735 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, 9,
'0');
7756 const int count =
static_cast<int>(Value(arg, m1)) +
7757 static_cast<int>(Value(arg, m2)) +
7758 static_cast<int>(Value(arg, m3));
7762TEST(MatcherPnMacroTest, CanUseMatcherTypedParameterInValue) {
7769TEST(ContainsTest, ListMatchesWhenElementIsInContainer) {
7770 list<int> some_list;
7771 some_list.push_back(3);
7772 some_list.push_back(1);
7773 some_list.push_back(2);
7778 list<std::string> another_list;
7779 another_list.push_back(
"fee");
7780 another_list.push_back(
"fie");
7781 another_list.push_back(
"foe");
7782 another_list.push_back(
"fum");
7783 EXPECT_THAT(another_list, Contains(std::string(
"fee")));
7786TEST(ContainsTest, ListDoesNotMatchWhenElementIsNotInContainer) {
7787 list<int> some_list;
7788 some_list.push_back(3);
7789 some_list.push_back(1);
7793TEST(ContainsTest, SetMatchesWhenElementIsInContainer) {
7802 set<std::string> another_set;
7803 another_set.insert(
"fee");
7804 another_set.insert(
"fie");
7805 another_set.insert(
"foe");
7806 another_set.insert(
"fum");
7807 EXPECT_THAT(another_set, Contains(Eq(std::string(
"fum"))));
7810TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) {
7816 set<std::string> c_string_set;
7817 c_string_set.insert(
"hello");
7818 EXPECT_THAT(c_string_set, Not(Contains(std::string(
"goodbye"))));
7821TEST(ContainsTest, ExplainsMatchResultCorrectly) {
7822 const int a[2] = {1, 2};
7823 Matcher<
const int(&)[2]> m = Contains(2);
7824 EXPECT_EQ(
"whose element #1 matches", Explain(m,
a));
7829 m = Contains(GreaterThan(0));
7830 EXPECT_EQ(
"whose element #0 matches, which is 1 more than 0", Explain(m,
a));
7832 m = Contains(GreaterThan(10));
7836TEST(ContainsTest, DescribesItselfCorrectly) {
7837 Matcher<vector<int>> m = Contains(1);
7838 EXPECT_EQ(
"contains at least one element that is equal to 1", Describe(m));
7840 Matcher<vector<int>> m2 = Not(m);
7841 EXPECT_EQ(
"doesn't contain any element that is equal to 1", Describe(m2));
7844TEST(ContainsTest, MapMatchesWhenElementIsInContainer) {
7845 map<std::string, int> my_map;
7846 const char*
bar =
"a string";
7848 EXPECT_THAT(my_map, Contains(pair<const char* const, int>(
bar, 2)));
7850 map<std::string, int> another_map;
7851 another_map[
"fee"] = 1;
7852 another_map[
"fie"] = 2;
7853 another_map[
"foe"] = 3;
7854 another_map[
"fum"] = 4;
7856 Contains(pair<const std::string, int>(std::string(
"fee"), 1)));
7857 EXPECT_THAT(another_map, Contains(pair<const std::string, int>(
"fie", 2)));
7860TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) {
7861 map<int, int> some_map;
7864 EXPECT_THAT(some_map, Not(Contains(pair<const int, int>(2, 23))));
7867TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) {
7868 const char* string_array[] = {
"fee",
"fie",
"foe",
"fum"};
7869 EXPECT_THAT(string_array, Contains(Eq(std::string(
"fum"))));
7872TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) {
7873 int int_array[] = {1, 2, 3, 4};
7877TEST(ContainsTest, AcceptsMatcher) {
7878 const int a[] = {1, 2, 3};
7883TEST(ContainsTest, WorksForNativeArrayAsTuple) {
7884 const int a[] = {1, 2};
7885 const int*
const pointer =
a;
7886 EXPECT_THAT(std::make_tuple(pointer, 2), Contains(1));
7887 EXPECT_THAT(std::make_tuple(pointer, 2), Not(Contains(Gt(3))));
7890TEST(ContainsTest, WorksForTwoDimensionalNativeArray) {
7891 int a[][3] = {{1, 2, 3}, {4, 5, 6}};
7898TEST(AllOfArrayTest, BasicForms) {
7900 std::vector<int> v0{};
7901 std::vector<int> v1{1};
7902 std::vector<int> v2{2, 3};
7903 std::vector<int> v3{4, 4, 4};
7906 EXPECT_THAT(2, Not(AllOfArray(v1.begin(), v1.end())));
7907 EXPECT_THAT(3, Not(AllOfArray(v2.begin(), v2.end())));
7910 int ar[6] = {1, 2, 3, 4, 4, 4};
7919 int ar2[2] = {2, 3};
7920 int ar3[3] = {4, 4, 4};
7940TEST(AllOfArrayTest, Matchers) {
7942 std::vector<Matcher<int>> matchers{Ge(1), Lt(2)};
7951TEST(AnyOfArrayTest, BasicForms) {
7953 std::vector<int> v0{};
7954 std::vector<int> v1{1};
7955 std::vector<int> v2{2, 3};
7956 EXPECT_THAT(0, Not(AnyOfArray(v0.begin(), v0.end())));
7958 EXPECT_THAT(2, Not(AnyOfArray(v1.begin(), v1.end())));
7960 EXPECT_THAT(4, Not(AnyOfArray(v2.begin(), v2.end())));
7962 int ar[3] = {1, 2, 3};
7971 int ar2[2] = {2, 3};
7991TEST(AnyOfArrayTest, Matchers) {
7994 std::vector<Matcher<int>> matchers{Lt(1), Ge(2)};
8003TEST(AnyOfArrayTest, ExplainsMatchResultCorrectly) {
8006 const std::vector<int> v0{};
8007 const std::vector<int> v1{1};
8008 const std::vector<int> v2{2, 3};
8009 const Matcher<int> m0 = AnyOfArray(v0);
8010 const Matcher<int> m1 = AnyOfArray(v1);
8011 const Matcher<int> m2 = AnyOfArray(v2);
8018 EXPECT_EQ(
"(is equal to 1)", Describe(m1));
8019 EXPECT_EQ(
"(is equal to 2) or (is equal to 3)", Describe(m2));
8021 EXPECT_EQ(
"(isn't equal to 1)", DescribeNegation(m1));
8022 EXPECT_EQ(
"(isn't equal to 2) and (isn't equal to 3)", DescribeNegation(m2));
8024 const Matcher<int> g1 = AnyOfArray({GreaterThan(1)});
8025 const Matcher<int> g2 = AnyOfArray({GreaterThan(1), GreaterThan(2)});
8027 EXPECT_EQ(
"which is 1 less than 1", Explain(g1, 0));
8028 EXPECT_EQ(
"which is the same as 1", Explain(g1, 1));
8029 EXPECT_EQ(
"which is 1 more than 1", Explain(g1, 2));
8030 EXPECT_EQ(
"which is 1 less than 1, and which is 2 less than 2",
8032 EXPECT_EQ(
"which is the same as 1, and which is 1 less than 2",
8038TEST(AllOfTest, HugeMatcher) {
8041 EXPECT_THAT(0, testing::AllOf(_, _, _, _, _, _, _, _, _,
8042 testing::AllOf(_, _, _, _, _, _, _, _, _, _)));
8045TEST(AnyOfTest, HugeMatcher) {
8048 EXPECT_THAT(0, testing::AnyOf(_, _, _, _, _, _, _, _, _,
8049 testing::AnyOf(_, _, _, _, _, _, _, _, _, _)));
8066template <
typename T1,
typename T2>
8067bool AllOf(
const T1& ,
const T2& ) {
8071TEST(AllOfTest, DoesNotCallAllOfUnqualified) {
8073 testing::AllOf(M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
8076template <
typename T1,
typename T2>
8077bool AnyOf(
const T1&,
const T2&) {
8081TEST(AnyOfTest, DoesNotCallAnyOfUnqualified) {
8083 testing::AnyOf(M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
8088TEST(AllOfTest, WorksOnMoveOnlyType) {
8089 std::unique_ptr<int>
p(
new int(3));
8090 EXPECT_THAT(
p, AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(5))));
8091 EXPECT_THAT(
p, Not(AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(3)))));
8094TEST(AnyOfTest, WorksOnMoveOnlyType) {
8095 std::unique_ptr<int>
p(
new int(3));
8096 EXPECT_THAT(
p, AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Lt(5))));
8097 EXPECT_THAT(
p, Not(AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Gt(5)))));
8100MATCHER(IsNotNull,
"") {
return arg !=
nullptr; }
8104TEST(MatcherMacroTest, WorksOnMoveOnlyType) {
8105 std::unique_ptr<int>
p(
new int(3));
8107 EXPECT_THAT(std::unique_ptr<int>(), Not(IsNotNull()));
8110MATCHER_P(UniquePointee, pointee,
"") {
return *arg == pointee; }
8114TEST(MatcherPMacroTest, WorksOnMoveOnlyType) {
8115 std::unique_ptr<int>
p(
new int(3));
8125# pragma warning(pop)
expr expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c *expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 c
const T func(int n, T *x)
TypeWithSize< sizeof(RawType)>::UInt Bits
#define MOCK_METHOD0(m,...)
#define MOCK_METHOD2(m,...)
#define MOCK_METHOD1(m,...)
#define MATCHER_P7(name, p0, p1, p2, p3, p4, p5, p6, description)
#define MATCHER_P9(name, p0, p1, p2, p3, p4, p5, p6, p7, p8, description)
#define MATCHER_P5(name, p0, p1, p2, p3, p4, description)
#define ASSERT_THAT(value, matcher)
#define MATCHER_P3(name, p0, p1, p2, description)
#define MATCHER_P4(name, p0, p1, p2, p3, description)
#define MATCHER_P2(name, p0, p1, description)
#define MATCHER_P10(name, p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, description)
#define EXPECT_THAT(value, matcher)
#define MATCHER_P(name, p0, description)
#define MATCHER_P6(name, p0, p1, p2, p3, p4, p5, description)
#define MATCHER_P8(name, p0, p1, p2, p3, p4, p5, p6, p7, description)
#define MATCHER(name, description)
std::vector< size_t > lhs_used_
const RawType close_to_negative_zero_
const RawType further_from_one_
std::list< value_type > remainder_
const Bits infinity_bits_
static const size_t kUnused
std::list< value_type >::iterator pos_
const RawType close_to_positive_zero_
const RawType further_from_negative_zero_
std::vector< size_t > rhs_used_
ElementMatcherPairs matches_
const RawType close_to_one_
const RawType further_from_infinity_
const RawType close_to_infinity_
ElementMatcherPairs best_so_far_
#define EXPECT_CALL(obj, call)
#define ON_CALL(obj, call)
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
#define TEST_P(test_suite_name, test_name)
#define INSTANTIATE_TEST_SUITE_P(prefix, test_suite_name,...)
#define GTEST_FLAG_PREFIX_
#define GTEST_LOG_(severity)
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
#define EXPECT_FATAL_FAILURE(statement, substr)
#define EXPECT_NONFATAL_FAILURE(statement, substr)
#define TEST_F(test_fixture, test_name)
#define EXPECT_EQ(val1, val2)
#define SCOPED_TRACE(message)
#define ASSERT_FALSE(condition)
#define TEST(test_suite_name, test_name)
#define EXPECT_TRUE(condition)
#define ASSERT_TRUE(condition)
#define EXPECT_FALSE(condition)
SimpleFad< ValueT > operator*(const SimpleFad< ValueT > &a, const SimpleFad< ValueT > &b)
int operator<=(const ADvari &L, const ADvari &R)
int operator<(const ADvari &L, const ADvari &R)
int operator!=(const ADvari &L, const ADvari &R)
int operator>=(const ADvari &L, const ADvari &R)
int operator>(const ADvari &L, const ADvari &R)
CacheTaylor< T > diff(const CacheTaylor< T > &x, int n=1)
Compute Taylor series of n-th derivative of x.
bool operator==(const Handle< T > &h1, const Handle< T > &h2)
Compare two handles.
::std::vector< ::std::string > Strings
GTEST_API_ bool IsTrue(bool condition)
GTEST_API_ std::string FormatMatcherDescription(bool negation, const char *matcher_name, const Strings ¶m_values)
AssertionResult IsNull(const char *str)
GTEST_API_ ElementMatcherPairs FindMaxBipartiteMatching(const MatchMatrix &g)
internal::ProxyTypeList< Ts... > Types
std::ostream & operator<<(std::ostream &os, const Message &sb)
inline ::std::reference_wrapper< T > ByRef(T &l_value)
TYPED_TEST(CodeLocationForTYPEDTEST, Verify)
TYPED_TEST_SUITE(CodeLocationForTYPEDTEST, int)
PolymorphicAction< internal::ReturnVoidAction > Return()
internal::ParamGenerator< T > Range(T start, T end, IncrementT step)
::std::string PrintToString(const T &value)
internal::ValueArray< T... > Values(T... v)
static ExpectedAnswer expected[4]