37#include <forward_list>
52#include "test/gmock-matchers_test.h"
55namespace gmock_matchers_test {
58std::vector<std::unique_ptr<int>> MakeUniquePtrs(
const std::vector<int>& ints) {
59 std::vector<std::unique_ptr<int>> pointers;
60 for (
int i : ints) pointers.emplace_back(new int(
i));
64std::string OfType(
const std::string& type_name) {
66 return IsReadableTypeName(type_name) ?
" (of type " + type_name +
")" :
"";
72TEST(ContainsTest, WorksWithMoveOnly) {
73 ContainerHelper helper;
75 helper.Call(MakeUniquePtrs({1, 2}));
81TEST(ElementsAreTest, HugeMatcher) {
82 vector<int> test_vector{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
85 ElementsAre(Eq(1), Eq(2), Lt(13), Eq(4), Eq(5), Eq(6), Eq(7),
86 Eq(8), Eq(9), Eq(10), Gt(1), Eq(12)));
90TEST(ElementsAreTest, HugeMatcherStr) {
91 vector<std::string> test_vector{
92 "literal_string",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
""};
94 EXPECT_THAT(test_vector, UnorderedElementsAre(
"literal_string", _, _, _, _, _,
99TEST(ElementsAreTest, HugeMatcherUnordered) {
100 vector<int> test_vector{2, 1, 8, 5, 4, 6, 7, 3, 9, 12, 11, 10};
103 Eq(2), Eq(1), Gt(7), Eq(5), Eq(4), Eq(6), Eq(7),
104 Eq(3), Eq(9), Eq(12), Eq(11), Ne(122)));
109TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
112 EXPECT_THAT(2, AllOf(Le(7), Ge(0))) <<
"This should succeed too.";
118TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
121 static unsigned short n;
126 "Expected: is > 10\n"
128 OfType(
"unsigned short"));
132 "Expected: (is <= 7) and (is >= 5)\n"
134 OfType(
"unsigned short"));
139TEST(MatcherAssertionTest, WorksForByRefArguments) {
147 "Expected: does not reference the variable @");
150 "Actual: 0" + OfType(
"int") +
", which is located @");
155TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
156 Matcher<const char*> starts_with_he = StartsWith(
"he");
159 Matcher<const std::string&> ends_with_ok = EndsWith(
"ok");
161 const std::string bad =
"bad";
164 "Expected: ends with \"ok\"\n"
166 Matcher<int> is_greater_than_5 = Gt(5);
174TEST(PointeeTest, RawPointer) {
175 const Matcher<int*> m = Pointee(Ge(0));
184TEST(PointeeTest, RawPointerToConst) {
185 const Matcher<const double*> m = Pointee(Ge(0));
194TEST(PointeeTest, ReferenceToConstRawPointer) {
195 const Matcher<int* const&> m = Pointee(Ge(0));
204TEST(PointeeTest, ReferenceToNonConstRawPointer) {
205 const Matcher<double*&> m = Pointee(Ge(0));
216TEST(PointeeTest, SmartPointer) {
217 const Matcher<std::unique_ptr<int>> m = Pointee(Ge(0));
219 std::unique_ptr<int> n(
new int(1));
223TEST(PointeeTest, SmartPointerToConst) {
224 const Matcher<std::unique_ptr<const int>> m = Pointee(Ge(0));
229 std::unique_ptr<const int> n(
new int(1));
233TEST(PointerTest, RawPointer) {
235 const Matcher<int*> m = Pointer(Eq(&n));
244TEST(PointerTest, RawPointerToConst) {
246 const Matcher<const int*> m = Pointer(Eq(&n));
255TEST(PointerTest, SmartPointer) {
256 std::unique_ptr<int> n(
new int(10));
257 int* raw_n = n.get();
258 const Matcher<std::unique_ptr<int>> m = Pointer(Eq(raw_n));
263TEST(PointerTest, SmartPointerToConst) {
264 std::unique_ptr<const int> n(
new int(10));
265 const int* raw_n = n.get();
266 const Matcher<std::unique_ptr<const int>> m = Pointer(Eq(raw_n));
271 std::unique_ptr<const int>
p(
new int(10));
277class ConstPropagatingPtr {
279 typedef T element_type;
281 ConstPropagatingPtr() : val_() {}
282 explicit ConstPropagatingPtr(
T* t) : val_(t) {}
283 ConstPropagatingPtr(
const ConstPropagatingPtr& other) : val_(other.val_) {}
285 T* get() {
return val_; }
286 T& operator*() {
return *val_; }
288 const T* get()
const {
return val_; }
289 const T& operator*()
const {
return *val_; }
297TEST(PointeeTest, WorksWithConstPropagatingPointers) {
298 const Matcher<ConstPropagatingPtr<int>> m = Pointee(Lt(5));
300 const ConstPropagatingPtr<int> co(&three);
301 ConstPropagatingPtr<int> o(&three);
309TEST(PointeeTest, NeverMatchesNull) {
310 const Matcher<const char*> m = Pointee(_);
315TEST(PointeeTest, MatchesAgainstAValue) {
316 const Matcher<int*> m = Pointee(5);
325TEST(PointeeTest, CanDescribeSelf) {
326 const Matcher<int*> m = Pointee(Gt(3));
331TEST_P(PointeeTestP, CanExplainMatchResult) {
332 const Matcher<const std::string*> m = Pointee(StartsWith(
"Hi"));
336 const Matcher<long*> m2 = Pointee(GreaterThan(1));
338 EXPECT_EQ(
"which points to 3" + OfType(
"long") +
", which is 2 more than 1",
342TEST(PointeeTest, AlwaysExplainsPointee) {
343 const Matcher<int*> m = Pointee(0);
351 Uncopyable() : value_(-1) {}
352 explicit Uncopyable(
int a_value) : value_(a_value) {}
354 int value()
const {
return value_; }
355 void set_value(
int i) { value_ =
i; }
359 Uncopyable(
const Uncopyable&) =
delete;
360 Uncopyable& operator=(
const Uncopyable&) =
delete;
364bool ValueIsPositive(
const Uncopyable&
x) {
return x.value() > 0; }
366MATCHER_P(UncopyableIs, inner_matcher,
"") {
367 return ExplainMatchResult(inner_matcher, arg.value(), result_listener);
372 AStruct() :
x(0),
y(1.0),
z(5),
p(nullptr) {}
373 AStruct(
const AStruct& rhs)
383struct DerivedStruct :
public AStruct {
390TEST(FieldTest, WorksForNonConstField) {
391 Matcher<AStruct> m = Field(&AStruct::x, Ge(0));
392 Matcher<AStruct> m_with_name = Field(
"x", &AStruct::x, Ge(0));
403TEST(FieldTest, WorksForConstField) {
406 Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0));
407 Matcher<AStruct> m_with_name = Field(
"y", &AStruct::y, Ge(0.0));
410 m = Field(&AStruct::y, Le(0.0));
411 m_with_name = Field(
"y", &AStruct::y, Le(0.0));
417TEST(FieldTest, WorksForUncopyableField) {
420 Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive));
422 m = Field(&AStruct::z, Not(Truly(ValueIsPositive)));
427TEST(FieldTest, WorksForPointerField) {
429 Matcher<AStruct> m = Field(&AStruct::p,
static_cast<const char*
>(
nullptr));
436 m = Field(&AStruct::p, StartsWith(
"hi"));
444TEST(FieldTest, WorksForByRefArgument) {
445 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
455TEST(FieldTest, WorksForArgumentOfSubType) {
458 Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0));
468TEST(FieldTest, WorksForCompatibleMatcherType) {
470 Matcher<const AStruct&> m = Field(&AStruct::x, Matcher<signed char>(Ge(0)));
479TEST(FieldTest, CanDescribeSelf) {
480 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
486TEST(FieldTest, CanDescribeSelfWithFieldName) {
487 Matcher<const AStruct&> m = Field(
"field_name", &AStruct::x, Ge(0));
490 EXPECT_EQ(
"is an object whose field `field_name` isn't >= 0",
495TEST_P(FieldTestP, CanExplainMatchResult) {
496 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
502 m = Field(&AStruct::x, GreaterThan(0));
504 "whose given field is 1" + OfType(
"int") +
", which is 1 more than 0",
508TEST_P(FieldTestP, CanExplainMatchResultWithFieldName) {
509 Matcher<const AStruct&> m = Field(
"field_name", &AStruct::x, Ge(0));
515 m = Field(
"field_name", &AStruct::x, GreaterThan(0));
516 EXPECT_EQ(
"whose field `field_name` is 1" + OfType(
"int") +
517 ", which is 1 more than 0",
524TEST(FieldForPointerTest, WorksForPointerToConst) {
525 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
534TEST(FieldForPointerTest, WorksForPointerToNonConst) {
535 Matcher<AStruct*> m = Field(&AStruct::x, Ge(0));
544TEST(FieldForPointerTest, WorksForReferenceToConstPointer) {
545 Matcher<AStruct* const&> m = Field(&AStruct::x, Ge(0));
554TEST(FieldForPointerTest, DoesNotMatchNull) {
555 Matcher<const AStruct*> m = Field(&AStruct::x, _);
561TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
564 Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0));
573TEST(FieldForPointerTest, CanDescribeSelf) {
574 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
580TEST(FieldForPointerTest, CanDescribeSelfWithFieldName) {
581 Matcher<const AStruct*> m = Field(
"field_name", &AStruct::x, Ge(0));
584 EXPECT_EQ(
"is an object whose field `field_name` isn't >= 0",
589TEST_P(FieldForPointerTestP, CanExplainMatchResult) {
590 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
595 EXPECT_EQ(
"which points to an object whose given field is 1" + OfType(
"int"),
598 m = Field(&AStruct::x, GreaterThan(0));
599 EXPECT_EQ(
"which points to an object whose given field is 1" + OfType(
"int") +
600 ", which is 1 more than 0",
604TEST_P(FieldForPointerTestP, CanExplainMatchResultWithFieldName) {
605 Matcher<const AStruct*> m = Field(
"field_name", &AStruct::x, Ge(0));
611 "which points to an object whose field `field_name` is 1" + OfType(
"int"),
614 m = Field(
"field_name", &AStruct::x, GreaterThan(0));
615 EXPECT_EQ(
"which points to an object whose field `field_name` is 1" +
616 OfType(
"int") +
", which is 1 more than 0",
626 int n()
const {
return n_; }
628 void set_n(
int new_n) { n_ = new_n; }
631 const std::string& s()
const {
return s_; }
633 const std::string& s_ref() const& {
return s_; }
635 void set_s(
const std::string& new_s) { s_ = new_s; }
638 double&
x()
const {
return x_; }
647double AClass::x_ = 0.0;
650class DerivedClass :
public AClass {
652 int k()
const {
return k_; }
662TEST(PropertyTest, WorksForNonReferenceProperty) {
663 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
664 Matcher<const AClass&> m_with_name = Property(
"n", &AClass::n, Ge(0));
678TEST(PropertyTest, WorksForReferenceToConstProperty) {
679 Matcher<const AClass&> m = Property(&AClass::s, StartsWith(
"hi"));
680 Matcher<const AClass&> m_with_name =
681 Property(
"s", &AClass::s, StartsWith(
"hi"));
695TEST(PropertyTest, WorksForRefQualifiedProperty) {
696 Matcher<const AClass&> m = Property(&AClass::s_ref, StartsWith(
"hi"));
697 Matcher<const AClass&> m_with_name =
698 Property(
"s", &AClass::s_ref, StartsWith(
"hi"));
712TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
716 Matcher<const AClass&> m = Property(&AClass::x, Ref(
x));
719 m = Property(&AClass::x, Not(Ref(
x)));
725TEST(PropertyTest, WorksForByValueArgument) {
726 Matcher<AClass> m = Property(&AClass::s, StartsWith(
"hi"));
738TEST(PropertyTest, WorksForArgumentOfSubType) {
741 Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0));
753TEST(PropertyTest, WorksForCompatibleMatcherType) {
755 Matcher<const AClass&> m = Property(&AClass::n, Matcher<signed char>(Ge(0)));
757 Matcher<const AClass&> m_with_name =
758 Property(
"n", &AClass::n, Matcher<signed char>(Ge(0)));
769TEST(PropertyTest, CanDescribeSelf) {
770 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
773 EXPECT_EQ(
"is an object whose given property isn't >= 0",
777TEST(PropertyTest, CanDescribeSelfWithPropertyName) {
778 Matcher<const AClass&> m = Property(
"fancy_name", &AClass::n, Ge(0));
781 EXPECT_EQ(
"is an object whose property `fancy_name` isn't >= 0",
786TEST_P(PropertyTestP, CanExplainMatchResult) {
787 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
793 m = Property(&AClass::n, GreaterThan(0));
795 "whose given property is 1" + OfType(
"int") +
", which is 1 more than 0",
799TEST_P(PropertyTestP, CanExplainMatchResultWithPropertyName) {
800 Matcher<const AClass&> m = Property(
"fancy_name", &AClass::n, Ge(0));
804 EXPECT_EQ(
"whose property `fancy_name` is 1" + OfType(
"int"),
Explain(m, a));
806 m = Property(
"fancy_name", &AClass::n, GreaterThan(0));
807 EXPECT_EQ(
"whose property `fancy_name` is 1" + OfType(
"int") +
808 ", which is 1 more than 0",
815TEST(PropertyForPointerTest, WorksForPointerToConst) {
816 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
827TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
828 Matcher<AClass*> m = Property(&AClass::s, StartsWith(
"hi"));
840TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) {
841 Matcher<AClass* const&> m = Property(&AClass::s, StartsWith(
"hi"));
852TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
853 Matcher<const AClass*> m = Property(&AClass::x, _);
859TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
862 Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));
873TEST(PropertyForPointerTest, CanDescribeSelf) {
874 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
877 EXPECT_EQ(
"is an object whose given property isn't >= 0",
881TEST(PropertyForPointerTest, CanDescribeSelfWithPropertyDescription) {
882 Matcher<const AClass*> m = Property(
"fancy_name", &AClass::n, Ge(0));
885 EXPECT_EQ(
"is an object whose property `fancy_name` isn't >= 0",
890TEST_P(PropertyForPointerTestP, CanExplainMatchResult) {
891 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
897 "which points to an object whose given property is 1" + OfType(
"int"),
900 m = Property(&AClass::n, GreaterThan(0));
901 EXPECT_EQ(
"which points to an object whose given property is 1" +
902 OfType(
"int") +
", which is 1 more than 0",
906TEST_P(PropertyForPointerTestP, CanExplainMatchResultWithPropertyName) {
907 Matcher<const AClass*> m = Property(
"fancy_name", &AClass::n, Ge(0));
912 EXPECT_EQ(
"which points to an object whose property `fancy_name` is 1" +
916 m = Property(
"fancy_name", &AClass::n, GreaterThan(0));
917 EXPECT_EQ(
"which points to an object whose property `fancy_name` is 1" +
918 OfType(
"int") +
", which is 1 more than 0",
926std::string IntToStringFunction(
int input) {
927 return input == 1 ?
"foo" :
"bar";
932TEST(ResultOfTest, WorksForFunctionPointers) {
933 Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(std::string(
"foo")));
940TEST(ResultOfTest, CanDescribeItself) {
941 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq(
"foo"));
944 "is mapped by the given callable to a value that "
945 "is equal to \"foo\"",
948 "is mapped by the given callable to a value that "
949 "isn't equal to \"foo\"",
954TEST(ResultOfTest, CanDescribeItselfWithResultDescription) {
955 Matcher<int> matcher =
956 ResultOf(
"string conversion", &IntToStringFunction, StrEq(
"foo"));
959 EXPECT_EQ(
"whose string conversion isn't equal to \"foo\"",
964int IntFunction(
int input) {
return input == 42 ? 80 : 90; }
966TEST_P(ResultOfTestP, CanExplainMatchResult) {
967 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
968 EXPECT_EQ(
"which is mapped by the given callable to 90" + OfType(
"int"),
971 matcher = ResultOf(&IntFunction, GreaterThan(85));
972 EXPECT_EQ(
"which is mapped by the given callable to 90" + OfType(
"int") +
973 ", which is 5 more than 85",
977TEST_P(ResultOfTestP, CanExplainMatchResultWithResultDescription) {
978 Matcher<int> matcher = ResultOf(
"magic int conversion", &IntFunction, Ge(85));
979 EXPECT_EQ(
"whose magic int conversion is 90" + OfType(
"int"),
982 matcher = ResultOf(
"magic int conversion", &IntFunction, GreaterThan(85));
983 EXPECT_EQ(
"whose magic int conversion is 90" + OfType(
"int") +
984 ", which is 5 more than 85",
990TEST(ResultOfTest, WorksForNonReferenceResults) {
991 Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
999double& DoubleFunction(
double& input) {
return input; }
1001Uncopyable& RefUncopyableFunction(Uncopyable& obj) {
1005TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
1008 Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(
x));
1016 Matcher<Uncopyable&> matcher2 = ResultOf(&RefUncopyableFunction, Ref(obj));
1024const std::string& StringFunction(
const std::string& input) {
return input; }
1026TEST(ResultOfTest, WorksForReferenceToConstResults) {
1027 std::string s =
"foo";
1029 Matcher<const std::string&> matcher = ResultOf(&StringFunction, Ref(s));
1037TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
1039 Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));
1047TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
1049 ResultOf(
static_cast<std::string (*)(
int dummy)
>(
nullptr),
1050 Eq(std::string(
"foo"))),
1051 "NULL function pointer is passed into ResultOf\\(\\)\\.");
1056TEST(ResultOfTest, WorksForFunctionReferences) {
1057 Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq(
"foo"));
1065 std::string operator()(
int input)
const {
return IntToStringFunction(input); }
1068TEST(ResultOfTest, WorksForFunctors) {
1069 Matcher<int> matcher = ResultOf(Functor(), Eq(std::string(
"foo")));
1078struct PolymorphicFunctor {
1079 typedef int result_type;
1080 int operator()(
int n) {
return n; }
1081 int operator()(
const char* s) {
return static_cast<int>(strlen(s)); }
1082 std::string operator()(
int*
p) {
return p ?
"good ptr" :
"null"; }
1085TEST(ResultOfTest, WorksForPolymorphicFunctors) {
1086 Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));
1091 Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));
1093 EXPECT_TRUE(matcher_string.Matches(
"long string"));
1097TEST(ResultOfTest, WorksForPolymorphicFunctorsIgnoringResultType) {
1098 Matcher<int*> matcher = ResultOf(PolymorphicFunctor(),
"good ptr");
1105TEST(ResultOfTest, WorksForLambdas) {
1106 Matcher<int> matcher = ResultOf(
1108 return std::string(
static_cast<size_t>(str_len),
'x');
1115TEST(ResultOfTest, WorksForNonCopyableArguments) {
1116 Matcher<std::unique_ptr<int>> matcher = ResultOf(
1117 [](
const std::unique_ptr<int>& str_len) {
1118 return std::string(
static_cast<size_t>(*str_len),
'x');
1121 EXPECT_TRUE(matcher.Matches(std::unique_ptr<int>(
new int(3))));
1122 EXPECT_FALSE(matcher.Matches(std::unique_ptr<int>(
new int(1))));
1125const int* ReferencingFunction(
const int& n) {
return &n; }
1127struct ReferencingFunctor {
1128 typedef const int* result_type;
1129 result_type operator()(
const int& n) {
return &n; }
1132TEST(ResultOfTest, WorksForReferencingCallables) {
1135 Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n));
1139 Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));
1144TEST(SizeIsTest, ImplementsSizeIs) {
1145 vector<int> container;
1148 container.push_back(0);
1151 container.push_back(0);
1156TEST(SizeIsTest, WorksWithMap) {
1157 map<std::string, int> container;
1160 container.insert(make_pair(
"foo", 1));
1163 container.insert(make_pair(
"bar", 2));
1168TEST(SizeIsTest, WorksWithReferences) {
1169 vector<int> container;
1170 Matcher<const vector<int>&> m = SizeIs(1);
1172 container.push_back(0);
1176TEST(SizeIsTest, WorksWithMoveOnly) {
1177 ContainerHelper helper;
1179 helper.Call(MakeUniquePtrs({1, 2, 3}));
1184struct MinimalistCustomType {
1185 int size()
const {
return 1; }
1187TEST(SizeIsTest, WorksWithMinimalistCustomType) {
1188 MinimalistCustomType container;
1193TEST(SizeIsTest, CanDescribeSelf) {
1194 Matcher<vector<int>> m = SizeIs(2);
1199TEST(SizeIsTest, ExplainsResult) {
1200 Matcher<vector<int>> m1 = SizeIs(2);
1201 Matcher<vector<int>> m2 = SizeIs(Lt(2u));
1202 Matcher<vector<int>> m3 = SizeIs(AnyOf(0, 3));
1203 Matcher<vector<int>> m4 = SizeIs(Gt(1u));
1204 vector<int> container;
1209 container.push_back(0);
1210 container.push_back(0);
1217TEST(WhenSortedByTest, WorksForEmptyContainer) {
1218 const vector<int> numbers;
1219 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre()));
1220 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1))));
1223TEST(WhenSortedByTest, WorksForNonEmptyContainer) {
1224 vector<unsigned> numbers;
1225 numbers.push_back(3);
1226 numbers.push_back(1);
1227 numbers.push_back(2);
1228 numbers.push_back(2);
1230 WhenSortedBy(greater<unsigned>(), ElementsAre(3, 2, 2, 1)));
1232 Not(WhenSortedBy(greater<unsigned>(), ElementsAre(1, 2, 2, 3))));
1235TEST(WhenSortedByTest, WorksForNonVectorContainer) {
1236 list<std::string> words;
1237 words.push_back(
"say");
1238 words.push_back(
"hello");
1239 words.push_back(
"world");
1240 EXPECT_THAT(words, WhenSortedBy(less<std::string>(),
1241 ElementsAre(
"hello",
"say",
"world")));
1242 EXPECT_THAT(words, Not(WhenSortedBy(less<std::string>(),
1243 ElementsAre(
"say",
"hello",
"world"))));
1246TEST(WhenSortedByTest, WorksForNativeArray) {
1247 const int numbers[] = {1, 3, 2, 4};
1248 const int sorted_numbers[] = {1, 2, 3, 4};
1249 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre(1, 2, 3, 4)));
1251 WhenSortedBy(less<int>(), ElementsAreArray(sorted_numbers)));
1252 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1, 3, 2, 4))));
1255TEST(WhenSortedByTest, CanDescribeSelf) {
1256 const Matcher<vector<int>> m = WhenSortedBy(less<int>(), ElementsAre(1, 2));
1258 "(when sorted) has 2 elements where\n"
1259 "element #0 is equal to 1,\n"
1260 "element #1 is equal to 2",
1263 "(when sorted) doesn't have 2 elements, or\n"
1264 "element #0 isn't equal to 1, or\n"
1265 "element #1 isn't equal to 2",
1269TEST(WhenSortedByTest, ExplainsMatchResult) {
1270 const int a[] = {2, 1};
1271 EXPECT_EQ(
"which is { 1, 2 } when sorted, whose element #0 doesn't match",
1272 Explain(WhenSortedBy(less<int>(), ElementsAre(2, 3)), a));
1273 EXPECT_EQ(
"which is { 1, 2 } when sorted",
1274 Explain(WhenSortedBy(less<int>(), ElementsAre(1, 2)), a));
1280TEST(WhenSortedTest, WorksForEmptyContainer) {
1281 const vector<int> numbers;
1283 EXPECT_THAT(numbers, Not(WhenSorted(ElementsAre(1))));
1286TEST(WhenSortedTest, WorksForNonEmptyContainer) {
1287 list<std::string> words;
1288 words.push_back(
"3");
1289 words.push_back(
"1");
1290 words.push_back(
"2");
1291 words.push_back(
"2");
1292 EXPECT_THAT(words, WhenSorted(ElementsAre(
"1",
"2",
"2",
"3")));
1293 EXPECT_THAT(words, Not(WhenSorted(ElementsAre(
"3",
"1",
"2",
"2"))));
1296TEST(WhenSortedTest, WorksForMapTypes) {
1297 map<std::string, int> word_counts;
1298 word_counts[
"and"] = 1;
1299 word_counts[
"the"] = 1;
1300 word_counts[
"buffalo"] = 2;
1302 WhenSorted(ElementsAre(Pair(
"and", 1), Pair(
"buffalo", 2),
1305 Not(WhenSorted(ElementsAre(Pair(
"and", 1), Pair(
"the", 1),
1306 Pair(
"buffalo", 2)))));
1309TEST(WhenSortedTest, WorksForMultiMapTypes) {
1310 multimap<int, int> ifib;
1311 ifib.insert(make_pair(8, 6));
1312 ifib.insert(make_pair(2, 3));
1313 ifib.insert(make_pair(1, 1));
1314 ifib.insert(make_pair(3, 4));
1315 ifib.insert(make_pair(1, 2));
1316 ifib.insert(make_pair(5, 5));
1318 WhenSorted(ElementsAre(Pair(1, 1), Pair(1, 2), Pair(2, 3),
1319 Pair(3, 4), Pair(5, 5), Pair(8, 6))));
1321 Not(WhenSorted(ElementsAre(Pair(8, 6), Pair(2, 3), Pair(1, 1),
1322 Pair(3, 4), Pair(1, 2), Pair(5, 5)))));
1325TEST(WhenSortedTest, WorksForPolymorphicMatcher) {
1330 EXPECT_THAT(d, Not(WhenSorted(ElementsAre(2, 1))));
1333TEST(WhenSortedTest, WorksForVectorConstRefMatcher) {
1337 Matcher<const std::vector<int>&> vector_match = ElementsAre(1, 2);
1339 Matcher<const std::vector<int>&> not_vector_match = ElementsAre(2, 1);
1340 EXPECT_THAT(d, Not(WhenSorted(not_vector_match)));
1345template <
typename T>
1351 typedef ConstIter const_iterator;
1352 typedef T value_type;
1354 template <
typename InIter>
1355 Streamlike(InIter first, InIter last) : remainder_(first, last) {}
1357 const_iterator begin()
const {
1358 return const_iterator(
this, remainder_.begin());
1360 const_iterator end()
const {
return const_iterator(
this, remainder_.end()); }
1365 using iterator_category = std::input_iterator_tag;
1366 using value_type =
T;
1367 using difference_type = ptrdiff_t;
1368 using pointer =
const value_type*;
1369 using reference =
const value_type&;
1371 ConstIter(
const Streamlike* s,
typename std::list<value_type>::iterator pos)
1372 : s_(s), pos_(pos) {}
1374 const value_type& operator*()
const {
return *pos_; }
1375 const value_type* operator->()
const {
return &*pos_; }
1376 ConstIter& operator++() {
1377 s_->remainder_.erase(pos_++);
1383 class PostIncrProxy {
1385 explicit PostIncrProxy(
const value_type&
value) : value_(
value) {}
1386 value_type operator*()
const {
return value_; }
1391 PostIncrProxy operator++(
int) {
1392 PostIncrProxy proxy(**
this);
1397 friend bool operator==(
const ConstIter& a,
const ConstIter& b) {
1398 return a.s_ == b.s_ && a.pos_ == b.pos_;
1400 friend bool operator!=(
const ConstIter& a,
const ConstIter& b) {
1405 const Streamlike* s_;
1406 typename std::list<value_type>::iterator pos_;
1409 friend std::ostream&
operator<<(std::ostream& os,
const Streamlike& s) {
1411 typedef typename std::list<value_type>::const_iterator Iter;
1412 const char* sep =
"";
1413 for (Iter it = s.remainder_.begin(); it != s.remainder_.end(); ++it) {
1421 mutable std::list<value_type> remainder_;
1424TEST(StreamlikeTest, Iteration) {
1425 const int a[5] = {2, 1, 4, 5, 3};
1426 Streamlike<int> s(a, a + 5);
1427 Streamlike<int>::const_iterator it = s.begin();
1429 while (it != s.end()) {
1437TEST(BeginEndDistanceIsTest, WorksWithForwardList) {
1438 std::forward_list<int> container;
1440 EXPECT_THAT(container, Not(BeginEndDistanceIs(1)));
1441 container.push_front(0);
1442 EXPECT_THAT(container, Not(BeginEndDistanceIs(0)));
1444 container.push_front(0);
1445 EXPECT_THAT(container, Not(BeginEndDistanceIs(0)));
1449TEST(BeginEndDistanceIsTest, WorksWithNonStdList) {
1450 const int a[5] = {1, 2, 3, 4, 5};
1451 Streamlike<int> s(a, a + 5);
1455TEST(BeginEndDistanceIsTest, CanDescribeSelf) {
1456 Matcher<vector<int>> m = BeginEndDistanceIs(2);
1458 EXPECT_EQ(
"distance between begin() and end() isn't equal to 2",
1462TEST(BeginEndDistanceIsTest, WorksWithMoveOnly) {
1463 ContainerHelper helper;
1465 helper.Call(MakeUniquePtrs({1, 2}));
1468TEST_P(BeginEndDistanceIsTestP, ExplainsResult) {
1469 Matcher<vector<int>> m1 = BeginEndDistanceIs(2);
1470 Matcher<vector<int>> m2 = BeginEndDistanceIs(Lt(2));
1471 Matcher<vector<int>> m3 = BeginEndDistanceIs(AnyOf(0, 3));
1472 Matcher<vector<int>> m4 = BeginEndDistanceIs(GreaterThan(1));
1473 vector<int> container;
1474 EXPECT_EQ(
"whose distance between begin() and end() 0 doesn't match",
1476 EXPECT_EQ(
"whose distance between begin() and end() 0 matches",
1478 EXPECT_EQ(
"whose distance between begin() and end() 0 matches",
1481 "whose distance between begin() and end() 0 doesn't match, which is 1 "
1484 container.push_back(0);
1485 container.push_back(0);
1486 EXPECT_EQ(
"whose distance between begin() and end() 2 matches",
1488 EXPECT_EQ(
"whose distance between begin() and end() 2 doesn't match",
1490 EXPECT_EQ(
"whose distance between begin() and end() 2 doesn't match",
1493 "whose distance between begin() and end() 2 matches, which is 1 more "
1498TEST(WhenSortedTest, WorksForStreamlike) {
1501 const int a[5] = {2, 1, 4, 5, 3};
1502 Streamlike<int> s(std::begin(a), std::end(a));
1503 EXPECT_THAT(s, WhenSorted(ElementsAre(1, 2, 3, 4, 5)));
1504 EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3))));
1507TEST(WhenSortedTest, WorksForVectorConstRefMatcherOnStreamlike) {
1508 const int a[] = {2, 1, 4, 5, 3};
1509 Streamlike<int> s(std::begin(a), std::end(a));
1510 Matcher<const std::vector<int>&> vector_match = ElementsAre(1, 2, 3, 4, 5);
1512 EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3))));
1515TEST(IsSupersetOfTest, WorksForNativeArray) {
1516 const int subset[] = {1, 4};
1517 const int superset[] = {1, 2, 4};
1518 const int disjoint[] = {1, 0, 3};
1526TEST(IsSupersetOfTest, WorksWithDuplicates) {
1527 const int not_enough[] = {1, 2};
1528 const int enough[] = {1, 1, 2};
1529 const int expected[] = {1, 1};
1530 EXPECT_THAT(not_enough, Not(IsSupersetOf(expected)));
1534TEST(IsSupersetOfTest, WorksForEmpty) {
1535 vector<int> numbers;
1536 vector<int> expected;
1538 expected.push_back(1);
1539 EXPECT_THAT(numbers, Not(IsSupersetOf(expected)));
1541 numbers.push_back(1);
1542 numbers.push_back(2);
1544 expected.push_back(1);
1546 expected.push_back(2);
1548 expected.push_back(3);
1549 EXPECT_THAT(numbers, Not(IsSupersetOf(expected)));
1552TEST(IsSupersetOfTest, WorksForStreamlike) {
1553 const int a[5] = {1, 2, 3, 4, 5};
1554 Streamlike<int> s(std::begin(a), std::end(a));
1556 vector<int> expected;
1557 expected.push_back(1);
1558 expected.push_back(2);
1559 expected.push_back(5);
1562 expected.push_back(0);
1566TEST(IsSupersetOfTest, TakesStlContainer) {
1567 const int actual[] = {3, 1, 2};
1569 ::std::list<int> expected;
1570 expected.push_back(1);
1571 expected.push_back(3);
1574 expected.push_back(4);
1579 typedef std::vector<int> IntVec;
1581 expected.push_back(111);
1582 expected.push_back(222);
1583 expected.push_back(333);
1586 Eq(
"a surjection from elements to requirements exists such that:\n"
1587 " - an element is equal to 111\n"
1588 " - an element is equal to 222\n"
1589 " - an element is equal to 333"));
1593 typedef std::vector<int> IntVec;
1595 expected.push_back(111);
1596 expected.push_back(222);
1597 expected.push_back(333);
1600 Eq(
"no surjection from elements to requirements exists such that:\n"
1601 " - an element is equal to 111\n"
1602 " - an element is equal to 222\n"
1603 " - an element is equal to 333"));
1606TEST(IsSupersetOfTest, MatchAndExplain) {
1610 std::vector<int> expected;
1611 expected.push_back(1);
1612 expected.push_back(2);
1613 StringMatchResultListener listener;
1614 ASSERT_FALSE(ExplainMatchResult(IsSupersetOf(expected), v, &listener))
1617 Eq(
"where the following matchers don't match any elements:\n"
1618 "matcher #0: is equal to 1"));
1622 ASSERT_TRUE(ExplainMatchResult(IsSupersetOf(expected), v, &listener))
1625 " - element #0 is matched by matcher #1,\n"
1626 " - element #2 is matched by matcher #0"));
1629TEST(IsSupersetOfTest, WorksForRhsInitializerList) {
1630 const int numbers[] = {1, 3, 6, 2, 4, 5};
1635TEST(IsSupersetOfTest, WorksWithMoveOnly) {
1636 ContainerHelper helper;
1637 EXPECT_CALL(helper, Call(IsSupersetOf({Pointee(1)})));
1638 helper.Call(MakeUniquePtrs({1, 2}));
1639 EXPECT_CALL(helper, Call(Not(IsSupersetOf({Pointee(1), Pointee(2)}))));
1640 helper.Call(MakeUniquePtrs({2}));
1643TEST(IsSubsetOfTest, WorksForNativeArray) {
1644 const int subset[] = {1, 4};
1645 const int superset[] = {1, 2, 4};
1646 const int disjoint[] = {1, 0, 3};
1654TEST(IsSubsetOfTest, WorksWithDuplicates) {
1655 const int not_enough[] = {1, 2};
1656 const int enough[] = {1, 1, 2};
1657 const int actual[] = {1, 1};
1662TEST(IsSubsetOfTest, WorksForEmpty) {
1663 vector<int> numbers;
1664 vector<int> expected;
1666 expected.push_back(1);
1669 numbers.push_back(1);
1670 numbers.push_back(2);
1672 expected.push_back(1);
1674 expected.push_back(2);
1676 expected.push_back(3);
1680TEST(IsSubsetOfTest, WorksForStreamlike) {
1681 const int a[5] = {1, 2};
1682 Streamlike<int> s(std::begin(a), std::end(a));
1684 vector<int> expected;
1685 expected.push_back(1);
1687 expected.push_back(2);
1688 expected.push_back(5);
1692TEST(IsSubsetOfTest, TakesStlContainer) {
1693 const int actual[] = {3, 1, 2};
1695 ::std::list<int> expected;
1696 expected.push_back(1);
1697 expected.push_back(3);
1700 expected.push_back(2);
1701 expected.push_back(4);
1706 typedef std::vector<int> IntVec;
1708 expected.push_back(111);
1709 expected.push_back(222);
1710 expected.push_back(333);
1714 Eq(
"an injection from elements to requirements exists such that:\n"
1715 " - an element is equal to 111\n"
1716 " - an element is equal to 222\n"
1717 " - an element is equal to 333"));
1721 typedef std::vector<int> IntVec;
1723 expected.push_back(111);
1724 expected.push_back(222);
1725 expected.push_back(333);
1728 Eq(
"no injection from elements to requirements exists such that:\n"
1729 " - an element is equal to 111\n"
1730 " - an element is equal to 222\n"
1731 " - an element is equal to 333"));
1734TEST(IsSubsetOfTest, MatchAndExplain) {
1738 std::vector<int> expected;
1739 expected.push_back(1);
1740 expected.push_back(2);
1741 StringMatchResultListener listener;
1742 ASSERT_FALSE(ExplainMatchResult(IsSubsetOf(expected), v, &listener))
1745 Eq(
"where the following elements don't match any matchers:\n"
1748 expected.push_back(3);
1750 ASSERT_TRUE(ExplainMatchResult(IsSubsetOf(expected), v, &listener))
1753 " - element #0 is matched by matcher #1,\n"
1754 " - element #1 is matched by matcher #2"));
1757TEST(IsSubsetOfTest, WorksForRhsInitializerList) {
1758 const int numbers[] = {1, 2, 3};
1763TEST(IsSubsetOfTest, WorksWithMoveOnly) {
1764 ContainerHelper helper;
1765 EXPECT_CALL(helper, Call(IsSubsetOf({Pointee(1), Pointee(2)})));
1766 helper.Call(MakeUniquePtrs({1}));
1767 EXPECT_CALL(helper, Call(Not(IsSubsetOf({Pointee(1)}))));
1768 helper.Call(MakeUniquePtrs({2}));
1774TEST(ElemensAreStreamTest, WorksForStreamlike) {
1775 const int a[5] = {1, 2, 3, 4, 5};
1776 Streamlike<int> s(std::begin(a), std::end(a));
1781TEST(ElemensAreArrayStreamTest, WorksForStreamlike) {
1782 const int a[5] = {1, 2, 3, 4, 5};
1783 Streamlike<int> s(std::begin(a), std::end(a));
1785 vector<int> expected;
1786 expected.push_back(1);
1787 expected.push_back(2);
1788 expected.push_back(3);
1789 expected.push_back(4);
1790 expected.push_back(5);
1797TEST(ElementsAreTest, WorksWithUncopyable) {
1799 objs[0].set_value(-3);
1800 objs[1].set_value(1);
1801 EXPECT_THAT(objs, ElementsAre(UncopyableIs(-3), Truly(ValueIsPositive)));
1804TEST(ElementsAreTest, WorksWithMoveOnly) {
1805 ContainerHelper helper;
1806 EXPECT_CALL(helper, Call(ElementsAre(Pointee(1), Pointee(2))));
1807 helper.Call(MakeUniquePtrs({1, 2}));
1809 EXPECT_CALL(helper, Call(ElementsAreArray({Pointee(3), Pointee(4)})));
1810 helper.Call(MakeUniquePtrs({3, 4}));
1813TEST(ElementsAreTest, TakesStlContainer) {
1814 const int actual[] = {3, 1, 2};
1816 ::std::list<int> expected;
1817 expected.push_back(3);
1818 expected.push_back(1);
1819 expected.push_back(2);
1822 expected.push_back(4);
1823 EXPECT_THAT(actual, Not(ElementsAreArray(expected)));
1828TEST(UnorderedElementsAreArrayTest, SucceedsWhenExpected) {
1829 const int a[] = {0, 1, 2, 3, 4};
1830 std::vector<int> s(std::begin(a), std::end(a));
1832 StringMatchResultListener listener;
1833 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(a), s, &listener))
1835 }
while (std::next_permutation(s.begin(), s.end()));
1838TEST(UnorderedElementsAreArrayTest, VectorBool) {
1839 const bool a[] = {
false,
true,
false,
true,
true};
1840 const bool b[] = {
true,
false,
true,
true,
false};
1841 std::vector<bool> expected(std::begin(a), std::end(a));
1842 std::vector<bool> actual(std::begin(b), std::end(b));
1843 StringMatchResultListener listener;
1844 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(expected), actual,
1849TEST(UnorderedElementsAreArrayTest, WorksForStreamlike) {
1853 const int a[5] = {2, 1, 4, 5, 3};
1854 Streamlike<int> s(std::begin(a), std::end(a));
1856 ::std::vector<int> expected;
1857 expected.push_back(1);
1858 expected.push_back(2);
1859 expected.push_back(3);
1860 expected.push_back(4);
1861 expected.push_back(5);
1862 EXPECT_THAT(s, UnorderedElementsAreArray(expected));
1864 expected.push_back(6);
1865 EXPECT_THAT(s, Not(UnorderedElementsAreArray(expected)));
1868TEST(UnorderedElementsAreArrayTest, TakesStlContainer) {
1869 const int actual[] = {3, 1, 2};
1871 ::std::list<int> expected;
1872 expected.push_back(1);
1873 expected.push_back(2);
1874 expected.push_back(3);
1875 EXPECT_THAT(actual, UnorderedElementsAreArray(expected));
1877 expected.push_back(4);
1878 EXPECT_THAT(actual, Not(UnorderedElementsAreArray(expected)));
1881TEST(UnorderedElementsAreArrayTest, TakesInitializerList) {
1882 const int a[5] = {2, 1, 4, 5, 3};
1883 EXPECT_THAT(a, UnorderedElementsAreArray({1, 2, 3, 4, 5}));
1884 EXPECT_THAT(a, Not(UnorderedElementsAreArray({1, 2, 3, 4, 6})));
1887TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfCStrings) {
1888 const std::string a[5] = {
"a",
"b",
"c",
"d",
"e"};
1889 EXPECT_THAT(a, UnorderedElementsAreArray({
"a",
"b",
"c",
"d",
"e"}));
1890 EXPECT_THAT(a, Not(UnorderedElementsAreArray({
"a",
"b",
"c",
"d",
"ef"})));
1893TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {
1894 const int a[5] = {2, 1, 4, 5, 3};
1896 UnorderedElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(5)}));
1898 a, Not(UnorderedElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(6)})));
1901TEST(UnorderedElementsAreArrayTest,
1902 TakesInitializerListOfDifferentTypedMatchers) {
1903 const int a[5] = {2, 1, 4, 5, 3};
1907 EXPECT_THAT(a, UnorderedElementsAreArray<Matcher<int>>(
1908 {Eq(1), Ne(-2), Ge(3), Le(4), Eq(5)}));
1909 EXPECT_THAT(a, Not(UnorderedElementsAreArray<Matcher<int>>(
1910 {Eq(1), Ne(-2), Ge(3), Le(4), Eq(6)})));
1913TEST(UnorderedElementsAreArrayTest, WorksWithMoveOnly) {
1914 ContainerHelper helper;
1916 Call(UnorderedElementsAreArray({Pointee(1), Pointee(2)})));
1917 helper.Call(MakeUniquePtrs({2, 1}));
1922 typedef std::vector<int> IntVec;
1925TEST_F(UnorderedElementsAreTest, WorksWithUncopyable) {
1927 objs[0].set_value(-3);
1928 objs[1].set_value(1);
1930 UnorderedElementsAre(Truly(ValueIsPositive), UncopyableIs(-3)));
1933TEST_F(UnorderedElementsAreTest, SucceedsWhenExpected) {
1934 const int a[] = {1, 2, 3};
1935 std::vector<int> s(std::begin(a), std::end(a));
1937 StringMatchResultListener listener;
1938 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3), s, &listener))
1940 }
while (std::next_permutation(s.begin(), s.end()));
1943TEST_F(UnorderedElementsAreTest, FailsWhenAnElementMatchesNoMatcher) {
1944 const int a[] = {1, 2, 3};
1945 std::vector<int> s(std::begin(a), std::end(a));
1946 std::vector<Matcher<int>> mv;
1951 StringMatchResultListener listener;
1952 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAreArray(mv), s, &listener))
1956TEST_F(UnorderedElementsAreTest, WorksForStreamlike) {
1960 const int a[5] = {2, 1, 4, 5, 3};
1961 Streamlike<int> s(std::begin(a), std::end(a));
1963 EXPECT_THAT(s, UnorderedElementsAre(1, 2, 3, 4, 5));
1964 EXPECT_THAT(s, Not(UnorderedElementsAre(2, 2, 3, 4, 5)));
1967TEST_F(UnorderedElementsAreTest, WorksWithMoveOnly) {
1968 ContainerHelper helper;
1969 EXPECT_CALL(helper, Call(UnorderedElementsAre(Pointee(1), Pointee(2))));
1970 helper.Call(MakeUniquePtrs({2, 1}));
1979TEST_F(UnorderedElementsAreTest, Performance) {
1981 std::vector<Matcher<int>> mv;
1982 for (
int i = 0;
i < 100; ++
i) {
1987 StringMatchResultListener listener;
1988 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(mv), s, &listener))
1995TEST_F(UnorderedElementsAreTest, PerformanceHalfStrict) {
1997 std::vector<Matcher<int>> mv;
1998 for (
int i = 0;
i < 100; ++
i) {
2006 StringMatchResultListener listener;
2007 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(mv), s, &listener))
2011TEST_F(UnorderedElementsAreTest, FailMessageCountWrong) {
2014 StringMatchResultListener listener;
2015 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3), v, &listener))
2017 EXPECT_THAT(listener.str(), Eq(
"which has 1 element"));
2020TEST_F(UnorderedElementsAreTest, FailMessageCountWrongZero) {
2022 StringMatchResultListener listener;
2023 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3), v, &listener))
2028TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatchers) {
2032 StringMatchResultListener listener;
2033 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2), v, &listener))
2036 Eq(
"where the following matchers don't match any elements:\n"
2037 "matcher #1: is equal to 2"));
2040TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedElements) {
2044 StringMatchResultListener listener;
2045 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 1), v, &listener))
2048 Eq(
"where the following elements don't match any matchers:\n"
2052TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatcherAndElement) {
2056 StringMatchResultListener listener;
2057 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2), v, &listener))
2061 " the following matchers don't match any elements:\n"
2062 "matcher #0: is equal to 1\n"
2065 " the following elements don't match any matchers:\n"
2070static std::string EMString(
int element,
int matcher) {
2072 ss <<
"(element #" << element <<
", matcher #" << matcher <<
")";
2076TEST_F(UnorderedElementsAreTest, FailMessageImperfectMatchOnly) {
2079 std::vector<std::string> v;
2083 StringMatchResultListener listener;
2085 UnorderedElementsAre(
"a",
"a", AnyOf(
"b",
"c")), v, &listener))
2088 std::string prefix =
2089 "where no permutation of the elements can satisfy all matchers, "
2090 "and the closest match is 2 of 3 matchers with the "
2097 prefix +
"{\n " + EMString(0, 0) +
",\n " + EMString(1, 2) +
"\n}",
2098 prefix +
"{\n " + EMString(0, 1) +
",\n " + EMString(1, 2) +
"\n}",
2099 prefix +
"{\n " + EMString(0, 0) +
",\n " + EMString(2, 2) +
"\n}",
2100 prefix +
"{\n " + EMString(0, 1) +
",\n " + EMString(2, 2) +
2107 Eq(
"has 1 element and that element is equal to 345"));
2109 Eq(
"has 3 elements and there exists some permutation "
2110 "of elements such that:\n"
2111 " - element #0 is equal to 111, and\n"
2112 " - element #1 is equal to 222, and\n"
2113 " - element #2 is equal to 333"));
2121 Eq(
"doesn't have 1 element, or has 1 element that isn't equal to 345"));
2123 Eq(
"doesn't have 3 elements, or there exists no permutation "
2124 "of elements such that:\n"
2125 " - element #0 is equal to 123, and\n"
2126 " - element #1 is equal to 234, and\n"
2127 " - element #2 is equal to 345"));
2134TEST_P(EachTestP, ExplainsMatchResultCorrectly) {
2137 Matcher<set<int>> m = Each(2);
2140 Matcher<
const int(&)[1]> n = Each(1);
2142 const int b[1] = {1};
2151 m = Each(GreaterThan(0));
2154 m = Each(GreaterThan(10));
2155 EXPECT_EQ(
"whose element #0 doesn't match, which is 9 less than 10",
2159TEST(EachTest, DescribesItselfCorrectly) {
2160 Matcher<vector<int>> m = Each(1);
2163 Matcher<vector<int>> m2 = Not(m);
2167TEST(EachTest, MatchesVectorWhenAllElementsMatch) {
2168 vector<int> some_vector;
2170 some_vector.push_back(3);
2173 some_vector.push_back(1);
2174 some_vector.push_back(2);
2178 vector<std::string> another_vector;
2179 another_vector.push_back(
"fee");
2180 EXPECT_THAT(another_vector, Each(std::string(
"fee")));
2181 another_vector.push_back(
"fie");
2182 another_vector.push_back(
"foe");
2183 another_vector.push_back(
"fum");
2184 EXPECT_THAT(another_vector, Not(Each(std::string(
"fee"))));
2187TEST(EachTest, MatchesMapWhenAllElementsMatch) {
2188 map<const char*, int> my_map;
2189 const char*
bar =
"a string";
2193 map<std::string, int> another_map;
2194 EXPECT_THAT(another_map, Each(make_pair(std::string(
"fee"), 1)));
2195 another_map[
"fee"] = 1;
2196 EXPECT_THAT(another_map, Each(make_pair(std::string(
"fee"), 1)));
2197 another_map[
"fie"] = 2;
2198 another_map[
"foe"] = 3;
2199 another_map[
"fum"] = 4;
2200 EXPECT_THAT(another_map, Not(Each(make_pair(std::string(
"fee"), 1))));
2201 EXPECT_THAT(another_map, Not(Each(make_pair(std::string(
"fum"), 1))));
2205TEST(EachTest, AcceptsMatcher) {
2206 const int a[] = {1, 2, 3};
2211TEST(EachTest, WorksForNativeArrayAsTuple) {
2212 const int a[] = {1, 2};
2213 const int*
const pointer = a;
2214 EXPECT_THAT(std::make_tuple(pointer, 2), Each(Gt(0)));
2215 EXPECT_THAT(std::make_tuple(pointer, 2), Not(Each(Gt(1))));
2218TEST(EachTest, WorksWithMoveOnly) {
2219 ContainerHelper helper;
2221 helper.Call(MakeUniquePtrs({1, 2}));
2225class IsHalfOfMatcher {
2227 template <
typename T1,
typename T2>
2228 bool MatchAndExplain(
const std::tuple<T1, T2>& a_pair,
2229 MatchResultListener* listener)
const {
2230 if (std::get<0>(a_pair) == std::get<1>(a_pair) / 2) {
2231 *listener <<
"where the second is " << std::get<1>(a_pair);
2234 *listener <<
"where the second/2 is " << std::get<1>(a_pair) / 2;
2239 void DescribeTo(ostream* os)
const {
2240 *os <<
"are a pair where the first is half of the second";
2243 void DescribeNegationTo(ostream* os)
const {
2244 *os <<
"are a pair where the first isn't half of the second";
2248PolymorphicMatcher<IsHalfOfMatcher> IsHalfOf() {
2249 return MakePolymorphicMatcher(IsHalfOfMatcher());
2252TEST(PointwiseTest, DescribesSelf) {
2257 const Matcher<const vector<int>&> m = Pointwise(IsHalfOf(), rhs);
2259 "contains 3 values, where each value and its corresponding value "
2260 "in { 1, 2, 3 } are a pair where the first is half of the second",
2263 "doesn't contain exactly 3 values, or contains a value x at some "
2264 "index i where x and the i-th value of { 1, 2, 3 } are a pair "
2265 "where the first isn't half of the second",
2269TEST(PointwiseTest, MakesCopyOfRhs) {
2270 list<signed char> rhs;
2275 const Matcher<
const int(&)[2]> m = Pointwise(IsHalfOf(), rhs);
2283TEST(PointwiseTest, WorksForLhsNativeArray) {
2284 const int lhs[] = {1, 2, 3};
2293TEST(PointwiseTest, WorksForRhsNativeArray) {
2294 const int rhs[] = {1, 2, 3};
2304TEST(PointwiseTest, WorksForVectorOfBool) {
2305 vector<bool> rhs(3,
false);
2307 vector<bool> lhs = rhs;
2313TEST(PointwiseTest, WorksForRhsInitializerList) {
2314 const vector<int> lhs{2, 4, 6};
2316 EXPECT_THAT(lhs, Not(Pointwise(Lt(), {3, 3, 7})));
2319TEST(PointwiseTest, RejectsWrongSize) {
2320 const double lhs[2] = {1, 2};
2321 const int rhs[1] = {0};
2325 const int rhs2[3] = {0, 1, 2};
2329TEST(PointwiseTest, RejectsWrongContent) {
2330 const double lhs[3] = {1, 2, 3};
2331 const int rhs[3] = {2, 6, 4};
2332 EXPECT_THAT(lhs, Not(Pointwise(IsHalfOf(), rhs)));
2334 "where the value pair (2, 6) at index #1 don't match, "
2335 "where the second/2 is 3",
2336 Explain(Pointwise(IsHalfOf(), rhs), lhs));
2339TEST(PointwiseTest, AcceptsCorrectContent) {
2340 const double lhs[3] = {1, 2, 3};
2341 const int rhs[3] = {2, 4, 6};
2346TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) {
2347 const double lhs[3] = {1, 2, 3};
2348 const int rhs[3] = {2, 4, 6};
2349 const Matcher<std::tuple<const double&, const int&>> m1 = IsHalfOf();
2355 const Matcher<std::tuple<double, int>> m2 = IsHalfOf();
2360MATCHER(PointeeEquals,
"Points to an equal value") {
2361 return ExplainMatchResult(::testing::Pointee(::testing::get<1>(arg)),
2362 ::testing::get<0>(arg), result_listener);
2365TEST(PointwiseTest, WorksWithMoveOnly) {
2366 ContainerHelper helper;
2367 EXPECT_CALL(helper, Call(Pointwise(PointeeEquals(), std::vector<int>{1, 2})));
2368 helper.Call(MakeUniquePtrs({1, 2}));
2371TEST(UnorderedPointwiseTest, DescribesSelf) {
2376 const Matcher<const vector<int>&> m = UnorderedPointwise(IsHalfOf(), rhs);
2378 "has 3 elements and there exists some permutation of elements such "
2380 " - element #0 and 1 are a pair where the first is half of the second, "
2382 " - element #1 and 2 are a pair where the first is half of the second, "
2384 " - element #2 and 3 are a pair where the first is half of the second",
2387 "doesn't have 3 elements, or there exists no permutation of elements "
2389 " - element #0 and 1 are a pair where the first is half of the second, "
2391 " - element #1 and 2 are a pair where the first is half of the second, "
2393 " - element #2 and 3 are a pair where the first is half of the second",
2397TEST(UnorderedPointwiseTest, MakesCopyOfRhs) {
2398 list<signed char> rhs;
2403 const Matcher<
const int(&)[2]> m = UnorderedPointwise(IsHalfOf(), rhs);
2411TEST(UnorderedPointwiseTest, WorksForLhsNativeArray) {
2412 const int lhs[] = {1, 2, 3};
2418 EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs)));
2421TEST(UnorderedPointwiseTest, WorksForRhsNativeArray) {
2422 const int rhs[] = {1, 2, 3};
2428 EXPECT_THAT(lhs, Not(UnorderedPointwise(Lt(), rhs)));
2431TEST(UnorderedPointwiseTest, WorksForRhsInitializerList) {
2432 const vector<int> lhs{2, 4, 6};
2433 EXPECT_THAT(lhs, UnorderedPointwise(Gt(), {5, 1, 3}));
2434 EXPECT_THAT(lhs, Not(UnorderedPointwise(Lt(), {1, 1, 7})));
2437TEST(UnorderedPointwiseTest, RejectsWrongSize) {
2438 const double lhs[2] = {1, 2};
2439 const int rhs[1] = {0};
2440 EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs)));
2442 Explain(UnorderedPointwise(Gt(), rhs), lhs));
2444 const int rhs2[3] = {0, 1, 2};
2445 EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs2)));
2448TEST(UnorderedPointwiseTest, RejectsWrongContent) {
2449 const double lhs[3] = {1, 2, 3};
2450 const int rhs[3] = {2, 6, 6};
2451 EXPECT_THAT(lhs, Not(UnorderedPointwise(IsHalfOf(), rhs)));
2453 "where the following elements don't match any matchers:\n"
2455 Explain(UnorderedPointwise(IsHalfOf(), rhs), lhs));
2458TEST(UnorderedPointwiseTest, AcceptsCorrectContentInSameOrder) {
2459 const double lhs[3] = {1, 2, 3};
2460 const int rhs[3] = {2, 4, 6};
2461 EXPECT_THAT(lhs, UnorderedPointwise(IsHalfOf(), rhs));
2464TEST(UnorderedPointwiseTest, AcceptsCorrectContentInDifferentOrder) {
2465 const double lhs[3] = {1, 2, 3};
2466 const int rhs[3] = {6, 4, 2};
2467 EXPECT_THAT(lhs, UnorderedPointwise(IsHalfOf(), rhs));
2470TEST(UnorderedPointwiseTest, AllowsMonomorphicInnerMatcher) {
2471 const double lhs[3] = {1, 2, 3};
2472 const int rhs[3] = {4, 6, 2};
2473 const Matcher<std::tuple<const double&, const int&>> m1 = IsHalfOf();
2478 const Matcher<std::tuple<double, int>> m2 = IsHalfOf();
2482TEST(UnorderedPointwiseTest, WorksWithMoveOnly) {
2483 ContainerHelper helper;
2484 EXPECT_CALL(helper, Call(UnorderedPointwise(PointeeEquals(),
2485 std::vector<int>{1, 2})));
2486 helper.Call(MakeUniquePtrs({2, 1}));
2489TEST(PointeeTest, WorksOnMoveOnlyType) {
2490 std::unique_ptr<int>
p(
new int(3));
2497 enum Behavior { kInitialSuccess, kAlwaysFail, kFlaky };
2502 class MockMatcher :
public MatcherInterface<Behavior> {
2504 bool MatchAndExplain(Behavior behavior,
2505 MatchResultListener* listener)
const override {
2506 *listener <<
"[MatchAndExplain]";
2508 case kInitialSuccess:
2512 return !listener->IsInterested();
2522 return listener->IsInterested();
2525 GTEST_LOG_(FATAL) <<
"This should never be reached";
2529 void DescribeTo(ostream* os)
const override { *os <<
"[DescribeTo]"; }
2531 void DescribeNegationTo(ostream* os)
const override {
2532 *os <<
"[DescribeNegationTo]";
2536 AssertionResult RunPredicateFormatter(Behavior behavior) {
2537 auto matcher = MakeMatcher(
new MockMatcher);
2538 PredicateFormatterFromMatcher<Matcher<Behavior>> predicate_formatter(
2540 return predicate_formatter(
"dummy-name", behavior);
2544TEST_F(PredicateFormatterFromMatcherTest, ShortCircuitOnSuccess) {
2545 AssertionResult result = RunPredicateFormatter(kInitialSuccess);
2551TEST_F(PredicateFormatterFromMatcherTest, NoShortCircuitOnFailure) {
2552 AssertionResult result = RunPredicateFormatter(kAlwaysFail);
2554 std::string expect =
2555 "Value of: dummy-name\nExpected: [DescribeTo]\n"
2561TEST_F(PredicateFormatterFromMatcherTest, DetectsFlakyShortCircuit) {
2562 AssertionResult result = RunPredicateFormatter(kFlaky);
2564 std::string expect =
2565 "Value of: dummy-name\nExpected: [DescribeTo]\n"
2566 " The matcher failed on the initial attempt; but passed when rerun to "
2567 "generate the explanation.\n"
2575TEST(ElementsAreTest, CanDescribeExpectingNoElement) {
2576 Matcher<const vector<int>&> m = ElementsAre();
2580TEST(ElementsAreTest, CanDescribeExpectingOneElement) {
2581 Matcher<vector<int>> m = ElementsAre(Gt(5));
2585TEST(ElementsAreTest, CanDescribeExpectingManyElements) {
2586 Matcher<list<std::string>> m = ElementsAre(StrEq(
"one"),
"two");
2588 "has 2 elements where\n"
2589 "element #0 is equal to \"one\",\n"
2590 "element #1 is equal to \"two\"",
2594TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) {
2595 Matcher<vector<int>> m = ElementsAre();
2599TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElement) {
2600 Matcher<const list<int>&> m = ElementsAre(Gt(5));
2602 "doesn't have 1 element, or\n"
2603 "element #0 isn't > 5",
2607TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) {
2608 Matcher<const list<std::string>&> m = ElementsAre(
"one",
"two");
2610 "doesn't have 2 elements, or\n"
2611 "element #0 isn't equal to \"one\", or\n"
2612 "element #1 isn't equal to \"two\"",
2616TEST(ElementsAreTest, DoesNotExplainTrivialMatch) {
2617 Matcher<const list<int>&> m = ElementsAre(1, Ne(2));
2625TEST_P(ElementsAreTestP, ExplainsNonTrivialMatch) {
2626 Matcher<const vector<int>&> m =
2627 ElementsAre(GreaterThan(1), 0, GreaterThan(2));
2629 const int a[] = {10, 0, 100};
2630 vector<int> test_vector(std::begin(a), std::end(a));
2632 "whose element #0 matches, which is 9 more than 1,\n"
2633 "and whose element #2 matches, which is 98 more than 2",
2637TEST(ElementsAreTest, CanExplainMismatchWrongSize) {
2638 Matcher<const list<int>&> m = ElementsAre(1, 3);
2648TEST_P(ElementsAreTestP, CanExplainMismatchRightSize) {
2649 Matcher<const vector<int>&> m = ElementsAre(1, GreaterThan(5));
2657 EXPECT_EQ(
"whose element #1 doesn't match, which is 4 less than 5",
2661TEST(ElementsAreTest, MatchesOneElementVector) {
2662 vector<std::string> test_vector;
2663 test_vector.push_back(
"test string");
2665 EXPECT_THAT(test_vector, ElementsAre(StrEq(
"test string")));
2668TEST(ElementsAreTest, MatchesOneElementList) {
2672 EXPECT_THAT(test_list, ElementsAre(
"test string"));
2675TEST(ElementsAreTest, MatchesThreeElementVector) {
2676 vector<std::string> test_vector;
2677 test_vector.push_back(
"one");
2678 test_vector.push_back(
"two");
2679 test_vector.push_back(
"three");
2681 EXPECT_THAT(test_vector, ElementsAre(
"one", StrEq(
"two"), _));
2684TEST(ElementsAreTest, MatchesOneElementEqMatcher) {
2685 vector<int> test_vector;
2686 test_vector.push_back(4);
2691TEST(ElementsAreTest, MatchesOneElementAnyMatcher) {
2692 vector<int> test_vector;
2693 test_vector.push_back(4);
2698TEST(ElementsAreTest, MatchesOneElementValue) {
2699 vector<int> test_vector;
2700 test_vector.push_back(4);
2705TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) {
2706 vector<int> test_vector;
2707 test_vector.push_back(1);
2708 test_vector.push_back(2);
2709 test_vector.push_back(3);
2711 EXPECT_THAT(test_vector, ElementsAre(1, Eq(2), _));
2714TEST(ElementsAreTest, MatchesTenElementVector) {
2715 const int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
2716 vector<int> test_vector(std::begin(a), std::end(a));
2721 ElementsAre(0, Ge(0), _, 3, 4, Ne(2), Eq(6), 7, 8, _));
2724TEST(ElementsAreTest, DoesNotMatchWrongSize) {
2725 vector<std::string> test_vector;
2726 test_vector.push_back(
"test string");
2727 test_vector.push_back(
"test string");
2729 Matcher<vector<std::string>> m = ElementsAre(StrEq(
"test string"));
2733TEST(ElementsAreTest, DoesNotMatchWrongValue) {
2734 vector<std::string> test_vector;
2735 test_vector.push_back(
"other string");
2737 Matcher<vector<std::string>> m = ElementsAre(StrEq(
"test string"));
2741TEST(ElementsAreTest, DoesNotMatchWrongOrder) {
2742 vector<std::string> test_vector;
2743 test_vector.push_back(
"one");
2744 test_vector.push_back(
"three");
2745 test_vector.push_back(
"two");
2747 Matcher<vector<std::string>> m =
2748 ElementsAre(StrEq(
"one"), StrEq(
"two"), StrEq(
"three"));
2752TEST(ElementsAreTest, WorksForNestedContainer) {
2753 constexpr std::array<const char*, 2> strings = {{
"Hi",
"world"}};
2755 vector<list<char>> nested;
2756 for (
const auto& s : strings) {
2757 nested.emplace_back(s, s + strlen(s));
2760 EXPECT_THAT(nested, ElementsAre(ElementsAre(
'H', Ne(
'e')),
2761 ElementsAre(
'w',
'o', _, _,
'd')));
2762 EXPECT_THAT(nested, Not(ElementsAre(ElementsAre(
'H',
'e'),
2763 ElementsAre(
'w',
'o', _, _,
'd'))));
2766TEST(ElementsAreTest, WorksWithByRefElementMatchers) {
2767 int a[] = {0, 1, 2};
2768 vector<int> v(std::begin(a), std::end(a));
2770 EXPECT_THAT(v, ElementsAre(Ref(v[0]), Ref(v[1]), Ref(v[2])));
2771 EXPECT_THAT(v, Not(ElementsAre(Ref(v[0]), Ref(v[1]), Ref(a[2]))));
2774TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) {
2775 int a[] = {0, 1, 2};
2776 vector<int> v(std::begin(a), std::end(a));
2779 EXPECT_THAT(&v, Not(Pointee(ElementsAre(0, _, 3))));
2782TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) {
2783 int array[] = {0, 1, 2};
2789class NativeArrayPassedAsPointerAndSize {
2791 NativeArrayPassedAsPointerAndSize() =
default;
2793 MOCK_METHOD(
void, Helper, (
int* array,
int size));
2796 NativeArrayPassedAsPointerAndSize(
const NativeArrayPassedAsPointerAndSize&) =
2798 NativeArrayPassedAsPointerAndSize& operator=(
2799 const NativeArrayPassedAsPointerAndSize&) =
delete;
2802TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) {
2803 int array[] = {0, 1};
2804 ::std::tuple<int*, size_t> array_as_tuple(array, 2);
2808 NativeArrayPassedAsPointerAndSize helper;
2809 EXPECT_CALL(helper, Helper(_, _)).With(ElementsAre(0, 1));
2810 helper.Helper(array, 2);
2813TEST(ElementsAreTest, WorksWithTwoDimensionalNativeArray) {
2814 const char a2[][3] = {
"hi",
"lo"};
2815 EXPECT_THAT(a2, ElementsAre(ElementsAre(
'h',
'i',
'\0'),
2816 ElementsAre(
'l',
'o',
'\0')));
2817 EXPECT_THAT(a2, ElementsAre(StrEq(
"hi"), StrEq(
"lo")));
2818 EXPECT_THAT(a2, ElementsAre(Not(ElementsAre(
'h',
'o',
'\0')),
2819 ElementsAre(
'l',
'o',
'\0')));
2822TEST(ElementsAreTest, AcceptsStringLiteral) {
2823 std::string array[] = {
"hi",
"one",
"two"};
2824 EXPECT_THAT(array, ElementsAre(
"hi",
"one",
"two"));
2825 EXPECT_THAT(array, Not(ElementsAre(
"hi",
"one",
"too")));
2829extern const char kHi[];
2831TEST(ElementsAreTest, AcceptsArrayWithUnknownSize) {
2835 std::string array1[] = {
"hi"};
2838 std::string array2[] = {
"ho"};
2842const char kHi[] =
"hi";
2844TEST(ElementsAreTest, MakesCopyOfArguments) {
2848 ::testing::internal::ElementsAreMatcher<std::tuple<int, int>>
2849 polymorphic_matcher = ElementsAre(
x,
y);
2852 const int array1[] = {1, 2};
2854 const int array2[] = {0, 0};
2862TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) {
2863 const int a[] = {1, 2, 3};
2865 vector<int> test_vector(std::begin(a), std::end(a));
2869 EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
2872TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) {
2873 std::array<const char*, 3> a = {{
"one",
"two",
"three"}};
2875 vector<std::string> test_vector(std::begin(a), std::end(a));
2876 EXPECT_THAT(test_vector, ElementsAreArray(a.data(), a.size()));
2878 const char**
p = a.data();
2879 test_vector[0] =
"1";
2880 EXPECT_THAT(test_vector, Not(ElementsAreArray(
p, a.size())));
2883TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) {
2884 const char* a[] = {
"one",
"two",
"three"};
2886 vector<std::string> test_vector(std::begin(a), std::end(a));
2889 test_vector[0] =
"1";
2890 EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
2893TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) {
2894 const Matcher<std::string> kMatcherArray[] = {StrEq(
"one"), StrEq(
"two"),
2897 vector<std::string> test_vector;
2898 test_vector.push_back(
"one");
2899 test_vector.push_back(
"two");
2900 test_vector.push_back(
"three");
2901 EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray));
2903 test_vector.push_back(
"three");
2904 EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray)));
2907TEST(ElementsAreArrayTest, CanBeCreatedWithVector) {
2908 const int a[] = {1, 2, 3};
2909 vector<int> test_vector(std::begin(a), std::end(a));
2910 const vector<int> expected(std::begin(a), std::end(a));
2911 EXPECT_THAT(test_vector, ElementsAreArray(expected));
2912 test_vector.push_back(4);
2913 EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));
2916TEST(ElementsAreArrayTest, TakesInitializerList) {
2917 const int a[5] = {1, 2, 3, 4, 5};
2918 EXPECT_THAT(a, ElementsAreArray({1, 2, 3, 4, 5}));
2919 EXPECT_THAT(a, Not(ElementsAreArray({1, 2, 3, 5, 4})));
2920 EXPECT_THAT(a, Not(ElementsAreArray({1, 2, 3, 4, 6})));
2923TEST(ElementsAreArrayTest, TakesInitializerListOfCStrings) {
2924 const std::string a[5] = {
"a",
"b",
"c",
"d",
"e"};
2925 EXPECT_THAT(a, ElementsAreArray({
"a",
"b",
"c",
"d",
"e"}));
2926 EXPECT_THAT(a, Not(ElementsAreArray({
"a",
"b",
"c",
"e",
"d"})));
2927 EXPECT_THAT(a, Not(ElementsAreArray({
"a",
"b",
"c",
"d",
"ef"})));
2930TEST(ElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {
2931 const int a[5] = {1, 2, 3, 4, 5};
2932 EXPECT_THAT(a, ElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(5)}));
2933 EXPECT_THAT(a, Not(ElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(6)})));
2936TEST(ElementsAreArrayTest, TakesInitializerListOfDifferentTypedMatchers) {
2937 const int a[5] = {1, 2, 3, 4, 5};
2942 a, ElementsAreArray<Matcher<int>>({Eq(1), Ne(-2), Ge(3), Le(4), Eq(5)}));
2943 EXPECT_THAT(a, Not(ElementsAreArray<Matcher<int>>(
2944 {Eq(1), Ne(-2), Ge(3), Le(4), Eq(6)})));
2947TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherVector) {
2948 const int a[] = {1, 2, 3};
2949 const Matcher<int> kMatchers[] = {Eq(1), Eq(2), Eq(3)};
2950 vector<int> test_vector(std::begin(a), std::end(a));
2951 const vector<Matcher<int>> expected(std::begin(kMatchers),
2952 std::end(kMatchers));
2953 EXPECT_THAT(test_vector, ElementsAreArray(expected));
2954 test_vector.push_back(4);
2955 EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));
2958TEST(ElementsAreArrayTest, CanBeCreatedWithIteratorRange) {
2959 const int a[] = {1, 2, 3};
2960 const vector<int> test_vector(std::begin(a), std::end(a));
2961 const vector<int> expected(std::begin(a), std::end(a));
2962 EXPECT_THAT(test_vector, ElementsAreArray(expected.begin(), expected.end()));
2964 EXPECT_THAT(test_vector, ElementsAreArray(std::begin(a), std::end(a)));
2966 int*
const null_int =
nullptr;
2967 EXPECT_THAT(test_vector, Not(ElementsAreArray(null_int, null_int)));
2968 EXPECT_THAT((vector<int>()), ElementsAreArray(null_int, null_int));
2973TEST(ElementsAreArrayTest, WorksWithNativeArray) {
2974 ::std::string a[] = {
"hi",
"ho"};
2975 ::std::string b[] = {
"hi",
"ho"};
2982TEST(ElementsAreArrayTest, SourceLifeSpan) {
2983 const int a[] = {1, 2, 3};
2984 vector<int> test_vector(std::begin(a), std::end(a));
2985 vector<int> expect(std::begin(a), std::end(a));
2986 ElementsAreArrayMatcher<int> matcher_maker =
2987 ElementsAreArray(expect.begin(), expect.end());
2991 for (
int&
i : expect) {
2995 test_vector.push_back(3);
3003TEST(ContainsTest, ListMatchesWhenElementIsInContainer) {
3004 list<int> some_list;
3005 some_list.push_back(3);
3006 some_list.push_back(1);
3007 some_list.push_back(2);
3008 some_list.push_back(3);
3013 list<std::string> another_list;
3014 another_list.push_back(
"fee");
3015 another_list.push_back(
"fie");
3016 another_list.push_back(
"foe");
3017 another_list.push_back(
"fum");
3018 EXPECT_THAT(another_list, Contains(std::string(
"fee")));
3021TEST(ContainsTest, ListDoesNotMatchWhenElementIsNotInContainer) {
3022 list<int> some_list;
3023 some_list.push_back(3);
3024 some_list.push_back(1);
3028TEST(ContainsTest, SetMatchesWhenElementIsInContainer) {
3037 set<std::string> another_set;
3038 another_set.insert(
"fee");
3039 another_set.insert(
"fie");
3040 another_set.insert(
"foe");
3041 another_set.insert(
"fum");
3042 EXPECT_THAT(another_set, Contains(Eq(std::string(
"fum"))));
3045TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) {
3051 set<std::string> c_string_set;
3052 c_string_set.insert(
"hello");
3053 EXPECT_THAT(c_string_set, Not(Contains(std::string(
"goodbye"))));
3056TEST_P(ContainsTestP, ExplainsMatchResultCorrectly) {
3057 const int a[2] = {1, 2};
3058 Matcher<
const int(&)[2]> m = Contains(2);
3064 m = Contains(GreaterThan(0));
3065 EXPECT_EQ(
"whose element #0 matches, which is 1 more than 0",
Explain(m, a));
3067 m = Contains(GreaterThan(10));
3071TEST(ContainsTest, DescribesItselfCorrectly) {
3072 Matcher<vector<int>> m = Contains(1);
3075 Matcher<vector<int>> m2 = Not(m);
3079TEST(ContainsTest, MapMatchesWhenElementIsInContainer) {
3080 map<std::string, int> my_map;
3081 const char*
bar =
"a string";
3083 EXPECT_THAT(my_map, Contains(pair<const char* const, int>(
bar, 2)));
3085 map<std::string, int> another_map;
3086 another_map[
"fee"] = 1;
3087 another_map[
"fie"] = 2;
3088 another_map[
"foe"] = 3;
3089 another_map[
"fum"] = 4;
3091 Contains(pair<const std::string, int>(std::string(
"fee"), 1)));
3092 EXPECT_THAT(another_map, Contains(pair<const std::string, int>(
"fie", 2)));
3095TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) {
3096 map<int, int> some_map;
3099 EXPECT_THAT(some_map, Not(Contains(pair<const int, int>(2, 23))));
3102TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) {
3103 const char* string_array[] = {
"fee",
"fie",
"foe",
"fum"};
3104 EXPECT_THAT(string_array, Contains(Eq(std::string(
"fum"))));
3107TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) {
3108 int int_array[] = {1, 2, 3, 4};
3112TEST(ContainsTest, AcceptsMatcher) {
3113 const int a[] = {1, 2, 3};
3118TEST(ContainsTest, WorksForNativeArrayAsTuple) {
3119 const int a[] = {1, 2};
3120 const int*
const pointer = a;
3121 EXPECT_THAT(std::make_tuple(pointer, 2), Contains(1));
3122 EXPECT_THAT(std::make_tuple(pointer, 2), Not(Contains(Gt(3))));
3125TEST(ContainsTest, WorksForTwoDimensionalNativeArray) {
3126 int a[][3] = {{1, 2, 3}, {4, 5, 6}};
3129 EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5))));
pRC::Float<> T
Definition externs_nonTT.hpp:1
int value
Definition gmock-actions_test.cc:1714
#define MOCK_METHOD(...)
Definition gmock-function-mocker.h:111
int i
Definition gmock-matchers-comparisons_test.cc:603
Uncopyable z
Definition gmock-matchers-containers_test.cc:378
const double y
Definition gmock-matchers-containers_test.cc:377
int x
Definition gmock-matchers-containers_test.cc:376
const char * p
Definition gmock-matchers-containers_test.cc:379
char ch
Definition gmock-matchers-containers_test.cc:384
#define ASSERT_THAT(value, matcher)
#define EXPECT_THAT(value, matcher)
#define MATCHER_P(name, p0, description)
#define MATCHER(name, description)
#define INSTANTIATE_GTEST_MATCHER_TEST_P(TestSuite)
Definition gmock-matchers_test.h:151
#define EXPECT_CALL(obj, call)
Definition gmock-spec-builders.h:2145
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
Definition gtest-death-test.h:337
#define TEST_P(test_suite_name, test_name)
Definition gtest-param-test.h:450
#define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
Definition gtest-port.h:360
#define GTEST_LOG_(severity)
Definition gtest-port.h:1053
#define GTEST_DISABLE_MSC_WARNINGS_POP_()
Definition gtest-port.h:361
#define EXPECT_FATAL_FAILURE(statement, substr)
Definition gtest-spi.h:149
#define EXPECT_NONFATAL_FAILURE(statement, substr)
Definition gtest-spi.h:217
#define TEST_F(test_fixture, test_name)
Definition gtest.h:2208
#define EXPECT_EQ(val1, val2)
Definition gtest.h:1868
#define SCOPED_TRACE(message)
Definition gtest.h:2104
#define ASSERT_FALSE(condition)
Definition gtest.h:1819
#define TEST(test_suite_name, test_name)
Definition gtest.h:2176
#define EXPECT_TRUE(condition)
Definition gtest.h:1807
#define ASSERT_TRUE(condition)
Definition gtest.h:1815
#define EXPECT_FALSE(condition)
Definition gtest.h:1811
Definition googletest-output-test_.cc:485
test_list
Definition googletest-output-test.py:270
constexpr auto operator!=(GaussianDistribution< T > const &lhs, GaussianDistribution< T > const &rhs)
Definition gaussian.hpp:114
constexpr auto operator==(GaussianDistribution< T > const &lhs, GaussianDistribution< T > const &rhs)
Definition gaussian.hpp:96
std::string Explain(const MatcherType &m, const Value &x)
Definition gmock-matchers_test.h:183
std::string Describe(const Matcher< T > &m)
Definition gmock-matchers_test.h:171
std::string DescribeNegation(const Matcher< T > &m)
Definition gmock-matchers_test.h:177
std::string GetTypeName()
Definition gtest-type-util.h:129
Definition gmock-actions.h:151
std::ostream & operator<<(std::ostream &os, const Message &sb)
Definition gtest-message.h:232