37 # pragma warning(push)
38 # pragma warning(disable:4244)
39 # pragma warning(disable:4100)
42 #include "gmock/gmock-generated-matchers.h"
53 #include "gmock/gmock.h"
54 #include "gtest/gtest.h"
55 #include "gtest/gtest-spi.h"
63 using std::stringstream;
67 using testing::AllOfArray;
69 using testing::AnyOfArray;
102 template <
typename T>
105 m.DescribeNegationTo(&ss);
110 template <
typename MatcherType,
typename Value>
113 m.ExplainMatchResultTo(
x, &ss);
118 class GreaterThanMatcher :
public MatcherInterface<int> {
120 explicit GreaterThanMatcher(
int rhs) :
rhs_(rhs) {}
122 void DescribeTo(::std::ostream* os)
const override {
123 *os <<
"is greater than " <<
rhs_;
126 bool MatchAndExplain(
int lhs, MatchResultListener* listener)
const override {
129 *listener <<
"which is " <<
diff <<
" more than " <<
rhs_;
130 }
else if (
diff == 0) {
131 *listener <<
"which is the same as " <<
rhs_;
133 *listener <<
"which is " << -
diff <<
" less than " <<
rhs_;
143 Matcher<int> GreaterThan(
int n) {
149 TEST(ElementsAreTest, CanDescribeExpectingNoElement) {
154 TEST(ElementsAreTest, CanDescribeExpectingOneElement) {
156 EXPECT_EQ(
"has 1 element that is > 5", Describe(
m));
159 TEST(ElementsAreTest, CanDescribeExpectingManyElements) {
162 "element #0 is equal to \"one\",\n"
163 "element #1 is equal to \"two\"", Describe(
m));
166 TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) {
168 EXPECT_EQ(
"isn't empty", DescribeNegation(
m));
171 TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElment) {
174 "element #0 isn't > 5", DescribeNegation(
m));
177 TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) {
178 Matcher<const list<std::string>&>
m =
ElementsAre(
"one",
"two");
179 EXPECT_EQ(
"doesn't have 2 elements, or\n"
180 "element #0 isn't equal to \"one\", or\n"
181 "element #1 isn't equal to \"two\"", DescribeNegation(
m));
184 TEST(ElementsAreTest, DoesNotExplainTrivialMatch) {
193 TEST(ElementsAreTest, ExplainsNonTrivialMatch) {
194 Matcher<const vector<int>& >
m =
197 const int a[] = { 10, 0, 100 };
199 EXPECT_EQ(
"whose element #0 matches, which is 9 more than 1,\n"
200 "and whose element #2 matches, which is 98 more than 2",
204 TEST(ElementsAreTest, CanExplainMismatchWrongSize) {
215 TEST(ElementsAreTest, CanExplainMismatchRightSize) {
216 Matcher<const vector<int>& >
m =
ElementsAre(1, GreaterThan(5));
221 EXPECT_EQ(
"whose element #0 doesn't match", Explain(
m,
v));
224 EXPECT_EQ(
"whose element #1 doesn't match, which is 4 less than 5",
228 TEST(ElementsAreTest, MatchesOneElementVector) {
235 TEST(ElementsAreTest, MatchesOneElementList) {
242 TEST(ElementsAreTest, MatchesThreeElementVector) {
251 TEST(ElementsAreTest, MatchesOneElementEqMatcher) {
258 TEST(ElementsAreTest, MatchesOneElementAnyMatcher) {
265 TEST(ElementsAreTest, MatchesOneElementValue) {
272 TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) {
281 TEST(ElementsAreTest, MatchesTenElementVector) {
282 const int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
291 TEST(ElementsAreTest, DoesNotMatchWrongSize) {
300 TEST(ElementsAreTest, DoesNotMatchWrongValue) {
308 TEST(ElementsAreTest, DoesNotMatchWrongOrder) {
314 Matcher<vector<std::string> >
m =
319 TEST(ElementsAreTest, WorksForNestedContainer) {
320 const char* strings[] = {
325 vector<list<char> >
nested;
327 nested.push_back(list<char>(strings[
i], strings[
i] + strlen(strings[
i])));
336 TEST(ElementsAreTest, WorksWithByRefElementMatchers) {
337 int a[] = { 0, 1, 2 };
344 TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) {
345 int a[] = { 0, 1, 2 };
352 TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) {
353 int array[] = { 0, 1, 2 };
359 class NativeArrayPassedAsPointerAndSize {
361 NativeArrayPassedAsPointerAndSize() {}
369 TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) {
370 int array[] = { 0, 1 };
371 ::std::tuple<int*, size_t> array_as_tuple(
array, 2);
375 NativeArrayPassedAsPointerAndSize helper;
378 helper.Helper(
array, 2);
381 TEST(ElementsAreTest, WorksWithTwoDimensionalNativeArray) {
382 const char a2[][3] = {
"hi",
"lo" };
390 TEST(ElementsAreTest, AcceptsStringLiteral) {
404 extern const char kHi[];
406 TEST(ElementsAreTest, AcceptsArrayWithUnknownSize) {
417 const char kHi[] =
"hi";
421 TEST(ElementsAreTest, MakesCopyOfArguments) {
429 const int array1[] = { 1, 2 };
431 const int array2[] = { 0, 0 };
440 TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) {
441 const int a[] = { 1, 2, 3 };
450 TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) {
451 const char*
a[] = {
"one",
"two",
"three" };
461 TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) {
462 const char*
a[] = {
"one",
"two",
"three" };
471 TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) {
472 const Matcher<std::string> kMatcherArray[] = {
StrEq(
"one"),
StrEq(
"two"),
485 TEST(ElementsAreArrayTest, CanBeCreatedWithVector) {
486 const int a[] = { 1, 2, 3 };
495 TEST(ElementsAreArrayTest, TakesInitializerList) {
496 const int a[5] = { 1, 2, 3, 4, 5 };
502 TEST(ElementsAreArrayTest, TakesInitializerListOfCStrings) {
509 TEST(ElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {
510 const int a[5] = { 1, 2, 3, 4, 5 };
517 TEST(ElementsAreArrayTest,
518 TakesInitializerListOfDifferentTypedMatchers) {
519 const int a[5] = { 1, 2, 3, 4, 5 };
530 TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherVector) {
531 const int a[] = { 1, 2, 3 };
532 const Matcher<int> kMatchers[] = {
Eq(1),
Eq(2),
Eq(3) };
534 const vector<Matcher<int> > expected(
541 TEST(ElementsAreArrayTest, CanBeCreatedWithIteratorRange) {
542 const int a[] = { 1, 2, 3 };
549 int*
const null_int =
nullptr;
556 TEST(ElementsAreArrayTest, WorksWithNativeArray) {
565 TEST(ElementsAreArrayTest, SourceLifeSpan) {
566 const int a[] = { 1, 2, 3 };
569 ElementsAreArrayMatcher<int> matcher_maker =
575 for (
Iter it = expect.begin();
it != expect.end(); ++
it) { *
it += 10; }
585 MATCHER(IsEven,
"") {
return (
arg % 2) == 0; }
587 TEST(MatcherMacroTest, Works) {
588 const Matcher<int>
m = IsEven();
593 EXPECT_EQ(
"not (is even)", DescribeNegation(
m));
599 MATCHER(IsEven2, negation ?
"is odd" :
"is even") {
600 if ((
arg % 2) == 0) {
603 *result_listener <<
"OK";
606 *result_listener <<
"% 2 == " << (
arg % 2);
616 if (
arg == (
x +
y)) {
617 *result_listener <<
"OK";
622 if (result_listener->stream() !=
nullptr) {
623 *result_listener->stream() <<
"diff == " << (
x +
y -
arg);
631 TEST(MatcherMacroTest, DescriptionCanReferenceNegationAndParameters) {
632 const Matcher<int> m1 = IsEven2();
634 EXPECT_EQ(
"is odd", DescribeNegation(m1));
636 const Matcher<int> m2 = EqSumOf(5, 9);
637 EXPECT_EQ(
"equals the sum of 5 and 9", Describe(m2));
638 EXPECT_EQ(
"doesn't equal the sum of 5 and 9", DescribeNegation(m2));
642 TEST(MatcherMacroTest, CanExplainMatchResult) {
643 const Matcher<int> m1 = IsEven2();
647 const Matcher<int> m2 = EqSumOf(1, 2);
656 StaticAssertTypeEq< ::std::string, arg_type>();
660 MATCHER(IsEmptyStringByRef,
"") {
661 StaticAssertTypeEq<const ::std::string&, arg_type>();
665 TEST(MatcherMacroTest, CanReferenceArgType) {
666 const Matcher< ::std::string> m1 = IsEmptyString();
669 const Matcher<const ::std::string&> m2 = IsEmptyStringByRef();
679 TEST(MatcherMacroTest, WorksInNamespace) {
680 Matcher<int>
m = matcher_test::IsOdd();
687 return Value(
arg, matcher_test::IsOdd()) &&
arg > 0;
690 TEST(MatcherMacroTest, CanBeComposedUsingValue) {
700 TEST(MatcherPMacroTest, Works) {
701 const Matcher<int>
m = IsGreaterThan32And(5);
705 EXPECT_EQ(
"is greater than 32 and 5", Describe(
m));
706 EXPECT_EQ(
"not (is greater than 32 and 5)", DescribeNegation(
m));
714 TEST(MatcherPMacroTest, GeneratesCorrectDescription) {
715 const Matcher<int>
m = _is_Greater_Than32and_(5);
717 EXPECT_EQ(
"is greater than 32 and 5", Describe(
m));
718 EXPECT_EQ(
"not (is greater than 32 and 5)", DescribeNegation(
m));
726 class UncopyableFoo {
730 UncopyableFoo(
const UncopyableFoo&);
731 void operator=(
const UncopyableFoo&);
736 MATCHER_P(ReferencesUncopyable, variable,
"") {
return &
arg == &variable; }
738 TEST(MatcherPMacroTest, WorksWhenExplicitlyInstantiatedWithReference) {
739 UncopyableFoo foo1(
'1'), foo2(
'2');
740 const Matcher<const UncopyableFoo&>
m =
741 ReferencesUncopyable<const UncopyableFoo&>(foo1);
750 EXPECT_EQ(
"references uncopyable 1-byte object <31>", Describe(
m));
758 StaticAssertTypeEq<int, foo_type>();
759 StaticAssertTypeEq<long, bar_type>();
760 StaticAssertTypeEq<char, baz_type>();
764 TEST(MatcherPnMacroTest, CanReferenceParamTypes) {
765 EXPECT_THAT(0, ParamTypesAreIntLongAndChar(10, 20
L,
'a'));
771 MATCHER_P2(ReferencesAnyOf, variable1, variable2,
"") {
772 return &
arg == &variable1 || &
arg == &variable2;
775 TEST(MatcherPnMacroTest, WorksWhenExplicitlyInstantiatedWithReferences) {
776 UncopyableFoo foo1(
'1'), foo2(
'2'), foo3(
'3');
777 const Matcher<const UncopyableFoo&>
m =
778 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
785 TEST(MatcherPnMacroTest,
786 GeneratesCorretDescriptionWhenExplicitlyInstantiatedWithReferences) {
787 UncopyableFoo foo1(
'1'), foo2(
'2');
788 const Matcher<const UncopyableFoo&>
m =
789 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
795 EXPECT_EQ(
"references any of (1-byte object <31>, 1-byte object <32>)",
803 TEST(MatcherPnMacroTest, Works) {
804 const Matcher<const long&>
m = IsNotInClosedRange(10, 20);
808 EXPECT_EQ(
"is not in closed range (10, 20)", Describe(
m));
809 EXPECT_EQ(
"not (is not in closed range (10, 20))", DescribeNegation(
m));
832 MATCHER_P9(EqualsSumOf,
a,
b,
c,
d, e,
f,
g, h,
i,
"") {
835 MATCHER_P10(EqualsSumOf,
a,
b,
c,
d, e,
f,
g, h,
i, j,
"") {
839 TEST(MatcherPnMacroTest, CanBeOverloadedOnNumberOfParameters) {
845 EXPECT_THAT(12345, EqualsSumOf(10000, 2000, 300, 40, 5));
847 EqualsSumOf(::
std::string(
"a"),
'b',
'c',
"d",
"e",
'f'));
849 EqualsSumOf(::
std::string(
"a"),
'b',
'c',
"d",
"e",
'f',
'g'));
851 EqualsSumOf(::
std::string(
"a"),
'b',
'c',
"d",
"e",
'f',
'g',
854 EqualsSumOf(::
std::string(
"a"),
'b',
'c',
"d",
"e",
'f',
'g',
857 EqualsSumOf(::
std::string(
"a"),
'b',
'c',
"d",
"e",
'f',
'g',
884 TEST(MatcherPnMacroTest, WorksForDifferentParameterTypes) {
885 EXPECT_THAT(123, EqualsSumOf(100
L, 20,
static_cast<char>(3)));
897 char suffix_char =
static_cast<char>(
suffix);
898 return arg == prefix_str + suffix_char;
901 TEST(MatcherPnMacroTest, SimpleTypePromotion) {
902 Matcher<std::string> no_promo =
904 Matcher<const std::string&> promo =
905 EqConcat(
"foo",
static_cast<int>(
't'));
914 TEST(MatcherPnMacroTest, TypesAreCorrect) {
916 EqualsSumOfMatcher a0 = EqualsSumOf();
919 EqualsSumOfMatcherP<int>
a1 = EqualsSumOf(1);
923 EqualsSumOfMatcherP2<int, char>
a2 = EqualsSumOf(1,
'2');
924 EqualsSumOfMatcherP3<int, int, char> a3 = EqualsSumOf(1, 2,
'3');
925 EqualsSumOfMatcherP4<int, int, int, char> a4 = EqualsSumOf(1, 2, 3,
'4');
926 EqualsSumOfMatcherP5<int, int, int, int, char> a5 =
927 EqualsSumOf(1, 2, 3, 4,
'5');
928 EqualsSumOfMatcherP6<int, int, int, int, int, char> a6 =
929 EqualsSumOf(1, 2, 3, 4, 5,
'6');
930 EqualsSumOfMatcherP7<int, int, int, int, int, int, char> a7 =
931 EqualsSumOf(1, 2, 3, 4, 5, 6,
'7');
932 EqualsSumOfMatcherP8<int, int, int, int, int, int, int, char> a8 =
933 EqualsSumOf(1, 2, 3, 4, 5, 6, 7,
'8');
934 EqualsSumOfMatcherP9<int, int, int, int, int, int, int, int, char> a9 =
935 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8,
'9');
936 EqualsSumOfMatcherP10<int, int, int, int, int, int, int, int, int, char> a10 =
937 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, 9,
'0');
963 TEST(MatcherPnMacroTest, CanUseMatcherTypedParameterInValue) {
970 TEST(ContainsTest, ListMatchesWhenElementIsInContainer) {
972 some_list.push_back(3);
973 some_list.push_back(1);
974 some_list.push_back(2);
979 list<std::string> another_list;
980 another_list.push_back(
"fee");
981 another_list.push_back(
"fie");
982 another_list.push_back(
"foe");
983 another_list.push_back(
"fum");
987 TEST(ContainsTest, ListDoesNotMatchWhenElementIsNotInContainer) {
989 some_list.push_back(3);
990 some_list.push_back(1);
994 TEST(ContainsTest, SetMatchesWhenElementIsInContainer) {
1003 set<const char*> another_set;
1004 another_set.insert(
"fee");
1005 another_set.insert(
"fie");
1006 another_set.insert(
"foe");
1007 another_set.insert(
"fum");
1011 TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) {
1017 set<const char*> c_string_set;
1018 c_string_set.insert(
"hello");
1022 TEST(ContainsTest, ExplainsMatchResultCorrectly) {
1023 const int a[2] = { 1, 2 };
1025 EXPECT_EQ(
"whose element #1 matches", Explain(
m,
a));
1031 EXPECT_EQ(
"whose element #0 matches, which is 1 more than 0", Explain(
m,
a));
1037 TEST(ContainsTest, DescribesItselfCorrectly) {
1039 EXPECT_EQ(
"contains at least one element that is equal to 1", Describe(
m));
1041 Matcher<vector<int> > m2 =
Not(
m);
1042 EXPECT_EQ(
"doesn't contain any element that is equal to 1", Describe(m2));
1045 TEST(ContainsTest, MapMatchesWhenElementIsInContainer) {
1046 map<const char*, int> my_map;
1047 const char*
bar =
"a string";
1051 map<std::string, int> another_map;
1052 another_map[
"fee"] = 1;
1053 another_map[
"fie"] = 2;
1054 another_map[
"foe"] = 3;
1055 another_map[
"fum"] = 4;
1061 TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) {
1062 map<int, int> some_map;
1068 TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) {
1069 const char* string_array[] = {
"fee",
"fie",
"foe",
"fum" };
1073 TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) {
1074 int int_array[] = { 1, 2, 3, 4 };
1078 TEST(ContainsTest, AcceptsMatcher) {
1079 const int a[] = { 1, 2, 3 };
1084 TEST(ContainsTest, WorksForNativeArrayAsTuple) {
1085 const int a[] = { 1, 2 };
1086 const int*
const pointer =
a;
1091 TEST(ContainsTest, WorksForTwoDimensionalNativeArray) {
1092 int a[][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
1099 TEST(AllOfArrayTest, BasicForms) {
1101 std::vector<int> v0{};
1102 std::vector<int> v1{1};
1103 std::vector<int> v2{2, 3};
1104 std::vector<int> v3{4, 4, 4};
1111 int ar[6] = {1, 2, 3, 4, 4, 4};
1120 int ar2[2] = {2, 3};
1121 int ar3[3] = {4, 4, 4};
1141 TEST(AllOfArrayTest, Matchers) {
1152 TEST(AnyOfArrayTest, BasicForms) {
1154 std::vector<int> v0{};
1155 std::vector<int> v1{1};
1156 std::vector<int> v2{2, 3};
1163 int ar[3] = {1, 2, 3};
1172 int ar2[2] = {2, 3};
1192 TEST(AnyOfArrayTest, Matchers) {
1204 TEST(AnyOfArrayTest, ExplainsMatchResultCorrectly) {
1207 const std::vector<int> v0{};
1208 const std::vector<int> v1{1};
1209 const std::vector<int> v2{2, 3};
1210 const Matcher<int> m0 = AnyOfArray(v0);
1211 const Matcher<int> m1 = AnyOfArray(v1);
1212 const Matcher<int> m2 = AnyOfArray(v2);
1219 EXPECT_EQ(
"(is equal to 1)", Describe(m1));
1220 EXPECT_EQ(
"(is equal to 2) or (is equal to 3)", Describe(m2));
1222 EXPECT_EQ(
"(isn't equal to 1)", DescribeNegation(m1));
1223 EXPECT_EQ(
"(isn't equal to 2) and (isn't equal to 3)", DescribeNegation(m2));
1225 const Matcher<int> g1 = AnyOfArray({GreaterThan(1)});
1226 const Matcher<int> g2 = AnyOfArray({GreaterThan(1), GreaterThan(2)});
1228 EXPECT_EQ(
"which is 1 less than 1", Explain(g1, 0));
1229 EXPECT_EQ(
"which is the same as 1", Explain(g1, 1));
1230 EXPECT_EQ(
"which is 1 more than 1", Explain(g1, 2));
1231 EXPECT_EQ(
"which is 1 less than 1, and which is 2 less than 2",
1233 EXPECT_EQ(
"which is the same as 1, and which is 1 less than 2",
1239 TEST(AllOfTest, HugeMatcher) {
1242 EXPECT_THAT(0,
testing::AllOf(
_,
_,
_,
_,
_,
_,
_,
_,
_,
1246 TEST(AnyOfTest, HugeMatcher) {
1249 EXPECT_THAT(0,
testing::AnyOf(
_,
_,
_,
_,
_,
_,
_,
_,
_,
1262 MATCHER(M,
"") {
return true; }
1264 template <
typename T1,
typename T2>
1265 bool AllOf(
const T1& ,
const T2& ) {
return true; }
1267 TEST(AllOfTest, DoesNotCallAllOfUnqualified) {
1269 M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
1272 template <
typename T1,
typename T2>
bool
1273 AnyOf(
const T1&
t1,
const T2& t2) {
return true; }
1275 TEST(AnyOfTest, DoesNotCallAnyOfUnqualified) {
1277 M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
1283 TEST(AllOfTest, WorksOnMoveOnlyType) {
1284 std::unique_ptr<int>
p(
new int(3));
1289 TEST(AnyOfTest, WorksOnMoveOnlyType) {
1290 std::unique_ptr<int>
p(
new int(3));
1296 return arg !=
nullptr;
1301 TEST(MatcherMacroTest, WorksOnMoveOnlyType) {
1302 std::unique_ptr<int>
p(
new int(3));
1308 return *
arg == pointee;
1313 TEST(MatcherPMacroTest, WorksOnMoveOnlyType) {
1314 std::unique_ptr<int>
p(
new int(3));
1323 # pragma warning(pop)