38# pragma warning(disable:4244)
39# pragma warning(disable:4100)
63using std::stringstream;
66using testing::make_tuple;
96std::string
Describe(
const Matcher<T>& m) {
106 m.DescribeNegationTo(&ss);
111template <
typename MatcherType,
typename Value>
112std::string
Explain(
const MatcherType& m,
const Value& x) {
114 m.ExplainMatchResultTo(x, &ss);
120TEST(ArgsTest, AcceptsZeroTemplateArg) {
121 const tuple<int, bool> t(5,
true);
126TEST(ArgsTest, AcceptsOneTemplateArg) {
127 const tuple<int, bool> t(5,
true);
130 EXPECT_THAT(t, Not(Args<1>(Eq(make_tuple(
false)))));
133TEST(ArgsTest, AcceptsTwoTemplateArgs) {
134 const tuple<short, int, long> t(4, 5, 6L);
141TEST(ArgsTest, AcceptsRepeatedTemplateArgs) {
142 const tuple<short, int, long> t(4, 5, 6L);
147TEST(ArgsTest, AcceptsDecreasingTemplateArgs) {
148 const tuple<short, int, long> t(4, 5, 6L);
159# pragma warning(push)
160# pragma warning(disable:4100)
164 return get<0>(arg) + get<1>(arg) + get<2>(arg) == 0;
167TEST(ArgsTest, AcceptsMoreTemplateArgsThanArityOfOriginalTuple) {
168 EXPECT_THAT(make_tuple(-1, 2), (Args<0, 0, 1>(SumIsZero())));
169 EXPECT_THAT(make_tuple(1, 2), Not(Args<0, 0, 1>(SumIsZero())));
172TEST(ArgsTest, CanBeNested) {
173 const tuple<short, int, long, int> t(4, 5, 6L, 6);
178TEST(ArgsTest, CanMatchTupleByValue) {
179 typedef tuple<char, int, int> Tuple3;
180 const Matcher<Tuple3> m = Args<1, 2>(Lt());
185TEST(ArgsTest, CanMatchTupleByReference) {
186 typedef tuple<char, char, int> Tuple3;
187 const Matcher<const Tuple3&> m = Args<0, 1>(Lt());
197TEST(ArgsTest, AcceptsTenTemplateArgs) {
198 EXPECT_THAT(make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
199 (Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
200 PrintsAs(
"(9, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
201 EXPECT_THAT(make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
202 Not(Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
203 PrintsAs(
"(0, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
206TEST(ArgsTest, DescirbesSelfCorrectly) {
207 const Matcher<tuple<int, bool, char> > m = Args<2, 0>(Lt());
208 EXPECT_EQ(
"are a tuple whose fields (#2, #0) are a pair where "
209 "the first < the second",
213TEST(ArgsTest, DescirbesNestedArgsCorrectly) {
214 const Matcher<const tuple<int, bool, char, int>&> m =
215 Args<0, 2, 3>(Args<2, 0>(Lt()));
216 EXPECT_EQ(
"are a tuple whose fields (#0, #2, #3) are a tuple "
217 "whose fields (#2, #0) are a pair where the first < the second",
221TEST(ArgsTest, DescribesNegationCorrectly) {
222 const Matcher<tuple<int, char> > m = Args<1, 0>(Gt());
223 EXPECT_EQ(
"are a tuple whose fields (#1, #0) aren't a pair "
224 "where the first > the second",
228TEST(ArgsTest, ExplainsMatchResultWithoutInnerExplanation) {
229 const Matcher<tuple<bool, int, int> > m = Args<1, 2>(Eq());
230 EXPECT_EQ(
"whose fields (#1, #2) are (42, 42)",
231 Explain(m, make_tuple(
false, 42, 42)));
232 EXPECT_EQ(
"whose fields (#1, #2) are (42, 43)",
233 Explain(m, make_tuple(
false, 42, 43)));
237class LessThanMatcher :
public MatcherInterface<tuple<char, int> > {
239 virtual void DescribeTo(::std::ostream*
os)
const {}
241 virtual bool MatchAndExplain(tuple<char, int>
value,
242 MatchResultListener* listener)
const {
245 *listener <<
"where the first value is " <<
diff
246 <<
" more than the second";
252Matcher<tuple<char, int> > LessThan() {
253 return MakeMatcher(
new LessThanMatcher);
256TEST(ArgsTest, ExplainsMatchResultWithInnerExplanation) {
257 const Matcher<tuple<char, int, int> > m = Args<0, 2>(LessThan());
258 EXPECT_EQ(
"whose fields (#0, #2) are ('a' (97, 0x61), 42), "
259 "where the first value is 55 more than the second",
260 Explain(m, make_tuple(
'a', 42, 42)));
261 EXPECT_EQ(
"whose fields (#0, #2) are ('\\0', 43)",
262 Explain(m, make_tuple(
'\0', 42, 43)));
266class GreaterThanMatcher :
public MatcherInterface<int> {
268 explicit GreaterThanMatcher(
int rhs) : rhs_(rhs) {}
270 virtual void DescribeTo(::std::ostream*
os)
const {
271 *
os <<
"is greater than " << rhs_;
274 virtual bool MatchAndExplain(
int lhs,
275 MatchResultListener* listener)
const {
276 const int diff = lhs - rhs_;
278 *listener <<
"which is " <<
diff <<
" more than " << rhs_;
279 }
else if (
diff == 0) {
280 *listener <<
"which is the same as " << rhs_;
282 *listener <<
"which is " << -
diff <<
" less than " << rhs_;
293 return MakeMatcher(
new GreaterThanMatcher(n));
298TEST(ElementsAreTest, CanDescribeExpectingNoElement) {
299 Matcher<const vector<int>&> m = ElementsAre();
303TEST(ElementsAreTest, CanDescribeExpectingOneElement) {
304 Matcher<vector<int> > m = ElementsAre(Gt(5));
308TEST(ElementsAreTest, CanDescribeExpectingManyElements) {
309 Matcher<list<std::string> > m = ElementsAre(StrEq(
"one"),
"two");
311 "element #0 is equal to \"one\",\n"
312 "element #1 is equal to \"two\"",
Describe(m));
315TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) {
316 Matcher<vector<int> > m = ElementsAre();
320TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElment) {
321 Matcher<const list<int>& > m = ElementsAre(Gt(5));
326TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) {
327 Matcher<const list<std::string>&> m = ElementsAre(
"one",
"two");
328 EXPECT_EQ(
"doesn't have 2 elements, or\n"
329 "element #0 isn't equal to \"one\", or\n"
333TEST(ElementsAreTest, DoesNotExplainTrivialMatch) {
334 Matcher<const list<int>& > m = ElementsAre(1, Ne(2));
337 test_list.push_back(1);
338 test_list.push_back(3);
342TEST(ElementsAreTest, ExplainsNonTrivialMatch) {
343 Matcher<const vector<int>& > m =
346 const int a[] = { 10, 0, 100 };
348 EXPECT_EQ(
"whose element #0 matches, which is 9 more than 1,\n"
349 "and whose element #2 matches, which is 98 more than 2",
353TEST(ElementsAreTest, CanExplainMismatchWrongSize) {
354 Matcher<const list<int>& > m = ElementsAre(1, 3);
360 test_list.push_back(1);
364TEST(ElementsAreTest, CanExplainMismatchRightSize) {
365 Matcher<const vector<int>& > m = ElementsAre(1,
GreaterThan(5));
373 EXPECT_EQ(
"whose element #1 doesn't match, which is 4 less than 5",
377TEST(ElementsAreTest, MatchesOneElementVector) {
378 vector<std::string> test_vector;
379 test_vector.push_back(
"test string");
381 EXPECT_THAT(test_vector, ElementsAre(StrEq(
"test string")));
384TEST(ElementsAreTest, MatchesOneElementList) {
385 list<std::string> test_list;
386 test_list.push_back(
"test string");
388 EXPECT_THAT(test_list, ElementsAre(
"test string"));
391TEST(ElementsAreTest, MatchesThreeElementVector) {
392 vector<std::string> test_vector;
393 test_vector.push_back(
"one");
394 test_vector.push_back(
"two");
395 test_vector.push_back(
"three");
397 EXPECT_THAT(test_vector, ElementsAre(
"one", StrEq(
"two"), _));
400TEST(ElementsAreTest, MatchesOneElementEqMatcher) {
401 vector<int> test_vector;
402 test_vector.push_back(4);
407TEST(ElementsAreTest, MatchesOneElementAnyMatcher) {
408 vector<int> test_vector;
409 test_vector.push_back(4);
414TEST(ElementsAreTest, MatchesOneElementValue) {
415 vector<int> test_vector;
416 test_vector.push_back(4);
421TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) {
422 vector<int> test_vector;
423 test_vector.push_back(1);
424 test_vector.push_back(2);
425 test_vector.push_back(3);
427 EXPECT_THAT(test_vector, ElementsAre(1, Eq(2), _));
430TEST(ElementsAreTest, MatchesTenElementVector) {
431 const int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
437 ElementsAre(0, Ge(0), _, 3, 4, Ne(2), Eq(6), 7, 8, _));
440TEST(ElementsAreTest, DoesNotMatchWrongSize) {
441 vector<std::string> test_vector;
442 test_vector.push_back(
"test string");
443 test_vector.push_back(
"test string");
445 Matcher<vector<std::string> > m = ElementsAre(StrEq(
"test string"));
449TEST(ElementsAreTest, DoesNotMatchWrongValue) {
450 vector<std::string> test_vector;
451 test_vector.push_back(
"other string");
453 Matcher<vector<std::string> > m = ElementsAre(StrEq(
"test string"));
457TEST(ElementsAreTest, DoesNotMatchWrongOrder) {
458 vector<std::string> test_vector;
459 test_vector.push_back(
"one");
460 test_vector.push_back(
"three");
461 test_vector.push_back(
"two");
463 Matcher<vector<std::string> > m =
464 ElementsAre(StrEq(
"one"), StrEq(
"two"), StrEq(
"three"));
468TEST(ElementsAreTest, WorksForNestedContainer) {
469 const char* strings[] = {
474 vector<list<char> > nested;
476 nested.push_back(list<char>(strings[i], strings[i] + strlen(strings[i])));
479 EXPECT_THAT(nested, ElementsAre(ElementsAre(
'H', Ne(
'e')),
480 ElementsAre(
'w',
'o', _, _,
'd')));
481 EXPECT_THAT(nested, Not(ElementsAre(ElementsAre(
'H',
'e'),
482 ElementsAre(
'w',
'o', _, _,
'd'))));
485TEST(ElementsAreTest, WorksWithByRefElementMatchers) {
486 int a[] = { 0, 1, 2 };
489 EXPECT_THAT(v, ElementsAre(Ref(v[0]), Ref(v[1]), Ref(v[2])));
490 EXPECT_THAT(v, Not(ElementsAre(Ref(v[0]), Ref(v[1]), Ref(
a[2]))));
493TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) {
494 int a[] = { 0, 1, 2 };
498 EXPECT_THAT(&v, Not(Pointee(ElementsAre(0, _, 3))));
501TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) {
502 int array[] = { 0, 1, 2 };
508class NativeArrayPassedAsPointerAndSize {
510 NativeArrayPassedAsPointerAndSize() {}
518TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) {
519 int array[] = { 0, 1 };
520 ::testing::tuple<int*, size_t> array_as_tuple(array, 2);
524 NativeArrayPassedAsPointerAndSize helper;
526 .With(ElementsAre(0, 1));
527 helper.Helper(array, 2);
530TEST(ElementsAreTest, WorksWithTwoDimensionalNativeArray) {
531 const char a2[][3] = {
"hi",
"lo" };
532 EXPECT_THAT(a2, ElementsAre(ElementsAre(
'h',
'i',
'\0'),
533 ElementsAre(
'l',
'o',
'\0')));
534 EXPECT_THAT(a2, ElementsAre(StrEq(
"hi"), StrEq(
"lo")));
535 EXPECT_THAT(a2, ElementsAre(Not(ElementsAre(
'h',
'o',
'\0')),
536 ElementsAre(
'l',
'o',
'\0')));
539TEST(ElementsAreTest, AcceptsStringLiteral) {
540 std::string
array[] = {
"hi",
"one",
"two"};
541 EXPECT_THAT(array, ElementsAre(
"hi",
"one",
"two"));
542 EXPECT_THAT(array, Not(ElementsAre(
"hi",
"one",
"too")));
553extern const char kHi[];
555TEST(ElementsAreTest, AcceptsArrayWithUnknownSize) {
559 std::string array1[] = {
"hi"};
562 std::string array2[] = {
"ho"};
566const char kHi[] =
"hi";
570TEST(ElementsAreTest, MakesCopyOfArguments) {
575 polymorphic_matcher = ElementsAre(x, y);
578 const int array1[] = { 1, 2 };
580 const int array2[] = { 0, 0 };
589TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) {
590 const int a[] = { 1, 2, 3 };
599TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) {
600 const char*
a[] = {
"one",
"two",
"three" };
606 test_vector[0] =
"1";
610TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) {
611 const char*
a[] = {
"one",
"two",
"three" };
616 test_vector[0] =
"1";
620TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) {
621 const Matcher<std::string> kMatcherArray[] = {StrEq(
"one"), StrEq(
"two"),
624 vector<std::string> test_vector;
625 test_vector.push_back(
"one");
626 test_vector.push_back(
"two");
627 test_vector.push_back(
"three");
628 EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray));
630 test_vector.push_back(
"three");
631 EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray)));
634TEST(ElementsAreArrayTest, CanBeCreatedWithVector) {
635 const int a[] = { 1, 2, 3 };
638 EXPECT_THAT(test_vector, ElementsAreArray(expected));
639 test_vector.push_back(4);
640 EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));
643#if GTEST_HAS_STD_INITIALIZER_LIST_
645TEST(ElementsAreArrayTest, TakesInitializerList) {
646 const int a[5] = { 1, 2, 3, 4, 5 };
648 EXPECT_THAT(
a, Not(ElementsAreArray({ 1, 2, 3, 5, 4 })));
649 EXPECT_THAT(
a, Not(ElementsAreArray({ 1, 2, 3, 4, 6 })));
652TEST(ElementsAreArrayTest, TakesInitializerListOfCStrings) {
653 const std::string
a[5] = {
"a",
"b",
"c",
"d",
"e"};
654 EXPECT_THAT(
a, ElementsAreArray({
"a",
"b",
"c",
"d",
"e" }));
655 EXPECT_THAT(
a, Not(ElementsAreArray({
"a",
"b",
"c",
"e",
"d" })));
656 EXPECT_THAT(
a, Not(ElementsAreArray({
"a",
"b",
"c",
"d",
"ef" })));
659TEST(ElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {
660 const int a[5] = { 1, 2, 3, 4, 5 };
662 { Eq(1), Eq(2), Eq(3), Eq(4), Eq(5) }));
664 { Eq(1), Eq(2), Eq(3), Eq(4), Eq(6) })));
667TEST(ElementsAreArrayTest,
668 TakesInitializerListOfDifferentTypedMatchers) {
669 const int a[5] = { 1, 2, 3, 4, 5 };
674 { Eq(1), Ne(-2), Ge(3), Le(4), Eq(5) }));
676 { Eq(1), Ne(-2), Ge(3), Le(4), Eq(6) })));
681TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherVector) {
682 const int a[] = { 1, 2, 3 };
683 const Matcher<int> kMatchers[] = { Eq(1), Eq(2), Eq(3) };
685 const vector<Matcher<int> > expected(
687 EXPECT_THAT(test_vector, ElementsAreArray(expected));
688 test_vector.push_back(4);
689 EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));
692TEST(ElementsAreArrayTest, CanBeCreatedWithIteratorRange) {
693 const int a[] = { 1, 2, 3 };
696 EXPECT_THAT(test_vector, ElementsAreArray(expected.begin(), expected.end()));
700 int*
const null_int = NULL;
701 EXPECT_THAT(test_vector, Not(ElementsAreArray(null_int, null_int)));
702 EXPECT_THAT((vector<int>()), ElementsAreArray(null_int, null_int));
707TEST(ElementsAreArrayTest, WorksWithNativeArray) {
708 ::std::string
a[] = {
"hi",
"ho" };
709 ::std::string b[] = {
"hi",
"ho" };
716TEST(ElementsAreArrayTest, SourceLifeSpan) {
717 const int a[] = { 1, 2, 3 };
720 ElementsAreArrayMatcher<int> matcher_maker =
725 typedef vector<int>::iterator Iter;
726 for (Iter it =
expect.begin(); it !=
expect.end(); ++it) { *it += 10; }
728 test_vector.push_back(3);
736MATCHER(IsEven,
"") {
return (arg % 2) == 0; }
738TEST(MatcherMacroTest, Works) {
739 const Matcher<int> m = IsEven();
750MATCHER(IsEven2, negation ?
"is odd" :
"is even") {
751 if ((arg % 2) == 0) {
754 *result_listener <<
"OK";
757 *result_listener <<
"% 2 == " << (arg % 2);
764MATCHER_P2(EqSumOf, x, y, std::string(negation ?
"doesn't equal" :
"equals") +
765 " the sum of " + PrintToString(x) +
" and " +
767 if (arg == (x + y)) {
768 *result_listener <<
"OK";
773 if (result_listener->stream() != NULL) {
774 *result_listener->stream() <<
"diff == " << (x +
y - arg);
782TEST(MatcherMacroTest, DescriptionCanReferenceNegationAndParameters) {
783 const Matcher<int> m1 = IsEven2();
787 const Matcher<int> m2 = EqSumOf(5, 9);
793TEST(MatcherMacroTest, CanExplainMatchResult) {
794 const Matcher<int> m1 = IsEven2();
798 const Matcher<int> m2 = EqSumOf(1, 2);
807 StaticAssertTypeEq< ::std::string, arg_type>();
811MATCHER(IsEmptyStringByRef,
"") {
812 StaticAssertTypeEq<const ::std::string&, arg_type>();
816TEST(MatcherMacroTest, CanReferenceArgType) {
817 const Matcher< ::std::string> m1 = IsEmptyString();
820 const Matcher<const ::std::string&> m2 = IsEmptyStringByRef();
827MATCHER(IsOdd,
"") {
return (arg % 2) != 0; }
830TEST(MatcherMacroTest, WorksInNamespace) {
831 Matcher<int> m = matcher_test::IsOdd();
838 return Value(arg, matcher_test::IsOdd()) && arg > 0;
841TEST(MatcherMacroTest, CanBeComposedUsingValue) {
849MATCHER_P(IsGreaterThan32And, n,
"") {
return arg > 32 && arg > n; }
851TEST(MatcherPMacroTest, Works) {
852 const Matcher<int> m = IsGreaterThan32And(5);
863MATCHER_P(_is_Greater_Than32and_, n,
"") {
return arg > 32 && arg > n; }
865TEST(MatcherPMacroTest, GeneratesCorrectDescription) {
866 const Matcher<int> m = _is_Greater_Than32and_(5);
879 explicit UncopyableFoo(
char value) : value_(
value) {}
881 UncopyableFoo(
const UncopyableFoo&);
882 void operator=(
const UncopyableFoo&);
887MATCHER_P(ReferencesUncopyable, variable,
"") {
return &arg == &variable; }
889TEST(MatcherPMacroTest, WorksWhenExplicitlyInstantiatedWithReference) {
890 UncopyableFoo foo1(
'1'), foo2(
'2');
891 const Matcher<const UncopyableFoo&> m =
892 ReferencesUncopyable<const UncopyableFoo&>(foo1);
909 StaticAssertTypeEq<int, foo_type>();
910 StaticAssertTypeEq<long, bar_type>();
911 StaticAssertTypeEq<char, baz_type>();
915TEST(MatcherPnMacroTest, CanReferenceParamTypes) {
916 EXPECT_THAT(0, ParamTypesAreIntLongAndChar(10, 20L,
'a'));
922MATCHER_P2(ReferencesAnyOf, variable1, variable2,
"") {
923 return &arg == &variable1 || &arg == &variable2;
926TEST(MatcherPnMacroTest, WorksWhenExplicitlyInstantiatedWithReferences) {
927 UncopyableFoo foo1(
'1'), foo2(
'2'), foo3(
'3');
928 const Matcher<const UncopyableFoo&> m =
929 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
936TEST(MatcherPnMacroTest,
937 GeneratesCorretDescriptionWhenExplicitlyInstantiatedWithReferences) {
938 UncopyableFoo foo1(
'1'), foo2(
'2');
939 const Matcher<const UncopyableFoo&> m =
940 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
946 EXPECT_EQ(
"references any of (1-byte object <31>, 1-byte object <32>)",
952MATCHER_P2(IsNotInClosedRange, low, hi,
"") {
return arg < low || arg > hi; }
954TEST(MatcherPnMacroTest, Works) {
955 const Matcher<const long&> m = IsNotInClosedRange(10, 20);
968MATCHER(EqualsSumOf,
"") {
return arg == 0; }
970MATCHER_P2(EqualsSumOf,
a, b,
"") {
return arg ==
a + b; }
971MATCHER_P3(EqualsSumOf,
a, b, c,
"") {
return arg ==
a + b + c; }
972MATCHER_P4(EqualsSumOf,
a, b, c, d,
"") {
return arg ==
a + b + c +
d; }
973MATCHER_P5(EqualsSumOf,
a, b, c, d, e,
"") {
return arg ==
a + b + c +
d + e; }
975 return arg ==
a + b + c +
d + e +
f;
978 return arg ==
a + b + c +
d + e +
f + g;
981 return arg ==
a + b + c +
d + e +
f + g + h;
983MATCHER_P9(EqualsSumOf,
a, b, c, d, e,
f, g, h, i,
"") {
984 return arg ==
a + b + c +
d + e +
f + g + h + i;
986MATCHER_P10(EqualsSumOf,
a, b, c, d, e,
f, g, h, i,
j,
"") {
987 return arg ==
a + b + c +
d + e +
f + g + h + i +
j;
990TEST(MatcherPnMacroTest, CanBeOverloadedOnNumberOfParameters) {
996 EXPECT_THAT(12345, EqualsSumOf(10000, 2000, 300, 40, 5));
998 EqualsSumOf(::std::string(
"a"),
'b',
'c',
"d",
"e",
'f'));
1000 EqualsSumOf(::std::string(
"a"),
'b',
'c',
"d",
"e",
'f',
'g'));
1002 EqualsSumOf(::std::string(
"a"),
'b',
'c',
"d",
"e",
'f',
'g',
1005 EqualsSumOf(::std::string(
"a"),
'b',
'c',
"d",
"e",
'f',
'g',
1008 EqualsSumOf(::std::string(
"a"),
'b',
'c',
"d",
"e",
'f',
'g',
1009 "h",
'i', ::std::string(
"j")));
1015 EXPECT_THAT(-1234, Not(EqualsSumOf(1000, 200, 30, 4)));
1016 EXPECT_THAT(-12345, Not(EqualsSumOf(10000, 2000, 300, 40, 5)));
1018 Not(EqualsSumOf(::std::string(
"a"),
'b',
'c',
"d",
"e",
'f')));
1020 Not(EqualsSumOf(::std::string(
"a"),
'b',
'c',
"d",
"e",
'f',
1023 Not(EqualsSumOf(::std::string(
"a"),
'b',
'c',
"d",
"e",
'f',
'g',
1026 Not(EqualsSumOf(::std::string(
"a"),
'b',
'c',
"d",
"e",
'f',
'g',
1029 Not(EqualsSumOf(::std::string(
"a"),
'b',
'c',
"d",
"e",
'f',
'g',
1030 "h",
'i', ::std::string(
"j"))));
1035TEST(MatcherPnMacroTest, WorksForDifferentParameterTypes) {
1036 EXPECT_THAT(123, EqualsSumOf(100L, 20,
static_cast<char>(3)));
1037 EXPECT_THAT(
"abcd", EqualsSumOf(::std::string(
"a"),
"b",
'c',
"d"));
1039 EXPECT_THAT(124, Not(EqualsSumOf(100L, 20,
static_cast<char>(3))));
1040 EXPECT_THAT(
"abcde", Not(EqualsSumOf(::std::string(
"a"),
"b",
'c',
"d")));
1047 std::string prefix_str(prefix);
1048 char suffix_char =
static_cast<char>(suffix);
1049 return arg == prefix_str + suffix_char;
1052TEST(MatcherPnMacroTest, SimpleTypePromotion) {
1053 Matcher<std::string> no_promo =
1054 EqConcat(std::string(
"foo"),
't');
1055 Matcher<const std::string&> promo =
1056 EqConcat(
"foo",
static_cast<int>(
't'));
1065TEST(MatcherPnMacroTest, TypesAreCorrect) {
1067 EqualsSumOfMatcher a0 = EqualsSumOf();
1070 EqualsSumOfMatcherP<int> a1 = EqualsSumOf(1);
1074 EqualsSumOfMatcherP2<int, char> a2 = EqualsSumOf(1,
'2');
1075 EqualsSumOfMatcherP3<int, int, char> a3 = EqualsSumOf(1, 2,
'3');
1076 EqualsSumOfMatcherP4<int, int, int, char> a4 = EqualsSumOf(1, 2, 3,
'4');
1077 EqualsSumOfMatcherP5<int, int, int, int, char> a5 =
1078 EqualsSumOf(1, 2, 3, 4,
'5');
1079 EqualsSumOfMatcherP6<int, int, int, int, int, char> a6 =
1080 EqualsSumOf(1, 2, 3, 4, 5,
'6');
1081 EqualsSumOfMatcherP7<int, int, int, int, int, int, char> a7 =
1082 EqualsSumOf(1, 2, 3, 4, 5, 6,
'7');
1083 EqualsSumOfMatcherP8<int, int, int, int, int, int, int, char> a8 =
1084 EqualsSumOf(1, 2, 3, 4, 5, 6, 7,
'8');
1085 EqualsSumOfMatcherP9<int, int, int, int, int, int, int, int, char> a9 =
1086 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8,
'9');
1087 EqualsSumOfMatcherP10<int, int, int, int, int, int, int, int, int, char> a10 =
1088 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, 9,
'0');
1109 const int count =
static_cast<int>(
Value(arg, m1))
1110 +
static_cast<int>(
Value(arg, m2)) +
static_cast<int>(
Value(arg, m3));
1114TEST(MatcherPnMacroTest, CanUseMatcherTypedParameterInValue) {
1121TEST(ContainsTest, ListMatchesWhenElementIsInContainer) {
1122 list<int> some_list;
1123 some_list.push_back(3);
1124 some_list.push_back(1);
1125 some_list.push_back(2);
1130 list<std::string> another_list;
1131 another_list.push_back(
"fee");
1132 another_list.push_back(
"fie");
1133 another_list.push_back(
"foe");
1134 another_list.push_back(
"fum");
1135 EXPECT_THAT(another_list, Contains(std::string(
"fee")));
1138TEST(ContainsTest, ListDoesNotMatchWhenElementIsNotInContainer) {
1139 list<int> some_list;
1140 some_list.push_back(3);
1141 some_list.push_back(1);
1145TEST(ContainsTest, SetMatchesWhenElementIsInContainer) {
1154 set<const char*> another_set;
1155 another_set.insert(
"fee");
1156 another_set.insert(
"fie");
1157 another_set.insert(
"foe");
1158 another_set.insert(
"fum");
1159 EXPECT_THAT(another_set, Contains(Eq(std::string(
"fum"))));
1162TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) {
1168 set<const char*> c_string_set;
1169 c_string_set.insert(
"hello");
1170 EXPECT_THAT(c_string_set, Not(Contains(std::string(
"hello").c_str())));
1173TEST(ContainsTest, ExplainsMatchResultCorrectly) {
1174 const int a[2] = { 1, 2 };
1175 Matcher<
const int (&)[2]> m = Contains(2);
1188TEST(ContainsTest, DescribesItselfCorrectly) {
1189 Matcher<vector<int> > m = Contains(1);
1192 Matcher<vector<int> > m2 = Not(m);
1196TEST(ContainsTest, MapMatchesWhenElementIsInContainer) {
1197 map<const char*, int> my_map;
1198 const char*
bar =
"a string";
1200 EXPECT_THAT(my_map, Contains(pair<const char* const, int>(
bar, 2)));
1202 map<std::string, int> another_map;
1203 another_map[
"fee"] = 1;
1204 another_map[
"fie"] = 2;
1205 another_map[
"foe"] = 3;
1206 another_map[
"fum"] = 4;
1208 Contains(pair<const std::string, int>(std::string(
"fee"), 1)));
1209 EXPECT_THAT(another_map, Contains(pair<const std::string, int>(
"fie", 2)));
1212TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) {
1213 map<int, int> some_map;
1216 EXPECT_THAT(some_map, Not(Contains(pair<const int, int>(2, 23))));
1219TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) {
1220 const char* string_array[] = {
"fee",
"fie",
"foe",
"fum" };
1221 EXPECT_THAT(string_array, Contains(Eq(std::string(
"fum"))));
1224TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) {
1225 int int_array[] = { 1, 2, 3, 4 };
1229TEST(ContainsTest, AcceptsMatcher) {
1230 const int a[] = { 1, 2, 3 };
1235TEST(ContainsTest, WorksForNativeArrayAsTuple) {
1236 const int a[] = { 1, 2 };
1242TEST(ContainsTest, WorksForTwoDimensionalNativeArray) {
1243 int a[][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
1250TEST(AllOfTest, HugeMatcher) {
1253 EXPECT_THAT(0,
testing::AllOf(_, _, _, _, _, _, _, _, _,
1257TEST(AnyOfTest, HugeMatcher) {
1260 EXPECT_THAT(0,
testing::AnyOf(_, _, _, _, _, _, _, _, _,
1273MATCHER(M,
"") {
return true; }
1275template <
typename T1,
typename T2>
1276bool AllOf(
const T1& t1,
const T2& t2) {
return true; }
1278TEST(AllOfTest, DoesNotCallAllOfUnqualified) {
1280 M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
1283template <
typename T1,
typename T2>
bool
1284AnyOf(
const T1& t1,
const T2& t2) {
return true; }
1286TEST(AnyOfTest, DoesNotCallAnyOfUnqualified) {
1288 M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
1294# pragma warning(pop)
1299TEST(AllOfTest, WorksOnMoveOnlyType) {
1300 std::unique_ptr<int>
p(
new int(3));
1301 EXPECT_THAT(
p, AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(5))));
1302 EXPECT_THAT(
p, Not(AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(3)))));
1305TEST(AnyOfTest, WorksOnMoveOnlyType) {
1306 std::unique_ptr<int>
p(
new int(3));
1307 EXPECT_THAT(
p, AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Lt(5))));
1308 EXPECT_THAT(
p, Not(AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Gt(5)))));
1312 return arg !=
nullptr;
1317TEST(MatcherMacroTest, WorksOnMoveOnlyType) {
1318 std::unique_ptr<int>
p(
new int(3));
1320 EXPECT_THAT(std::unique_ptr<int>(), Not(IsNotNull()));
1324 return *arg == pointee;
1329TEST(MatcherPMacroTest, WorksOnMoveOnlyType) {
1330 std::unique_ptr<int>
p(
new int(3));
1340# pragma warning(pop)
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
#define MOCK_METHOD2(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 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 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)
<< DiffStrings(str, arg);
#define EXPECT_THAT(value, matcher)
#define EXPECT_CALL(obj, call)
#define GTEST_ARRAY_SIZE_(array)
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
#define EXPECT_EQ(val1, val2)
#define EXPECT_TRUE(condition)
#define TEST(test_case_name, test_name)
#define EXPECT_FALSE(condition)
void diff(const std::string &a, const std::string &b)
static const Segment ss(Segment::ss)
std::string Explain(const MatcherType &m, const Value &x)
Matcher< int > GreaterThan(int n)
std::string Describe(const Matcher< T > &m)
std::string DescribeNegation(const Matcher< T > &m)
internal::NotMatcher< InnerMatcher > Not(InnerMatcher m)
internal::Le2Matcher Le()
internal::Ne2Matcher Ne()
internal::Lt2Matcher Lt()
internal::Gt2Matcher Gt()
const internal::AnythingMatcher _
internal::AllOfResult2< M1, M2 >::type AllOf(M1 m1, M2 m2)
internal::ArgsMatcher< InnerMatcher > Args(const InnerMatcher &matcher)
bool StaticAssertTypeEq()
internal::ElementsAreArrayMatcher< typename ::std::iterator_traits< Iter >::value_type > ElementsAreArray(Iter first, Iter last)
internal::ElementsAreMatcher< ::testing::tuple<> > ElementsAre()
internal::Ge2Matcher Ge()
::std::string PrintToString(const T &value)
internal::ContainsMatcher< M > Contains(M matcher)
internal::Eq2Matcher Eq()
Matcher< T > MakeMatcher(const MatcherInterface< T > *impl)
PolymorphicMatcher< internal::StrEqualityMatcher< std::string > > StrEq(const std::string &str)
internal::AnyOfResult2< M1, M2 >::type AnyOf(M1 m1, M2 m2)
bool Value(const T &value, M matcher)
internal::RefMatcher< T & > Ref(T &x)
internal::PointeeMatcher< InnerMatcher > Pointee(const InnerMatcher &inner_matcher)
const GenericPointer< typename T::ValueType > & pointer
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a