37 # pragma warning(push)
38 # pragma warning(disable:4244)
39 # pragma warning(disable:4100)
63 using std::stringstream;
67 using testing::AllOfArray;
69 using testing::AnyOfArray;
72 using testing::ElementsAre;
73 using testing::ElementsAreArray;
79 using testing::MakeMatcher;
81 using testing::MatcherInterface;
82 using testing::MatchResultListener;
85 using testing::Pointee;
91 using testing::internal::ElementsAreArrayMatcher;
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 {
127 const int diff = lhs -
rhs_;
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) {
144 return MakeMatcher(
new GreaterThanMatcher(
n));
149 TEST(ElementsAreTest, CanDescribeExpectingNoElement) {
150 Matcher<const vector<int>&>
m = ElementsAre();
154 TEST(ElementsAreTest, CanDescribeExpectingOneElement) {
155 Matcher<vector<int> >
m = ElementsAre(Gt(5));
156 EXPECT_EQ(
"has 1 element that is > 5", Describe(
m));
159 TEST(ElementsAreTest, CanDescribeExpectingManyElements) {
160 Matcher<list<std::string> >
m = ElementsAre(StrEq(
"one"),
"two");
162 "element #0 is equal to \"one\",\n"
163 "element #1 is equal to \"two\"", Describe(
m));
166 TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) {
167 Matcher<vector<int> >
m = ElementsAre();
168 EXPECT_EQ(
"isn't empty", DescribeNegation(
m));
171 TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElment) {
172 Matcher<const list<int>& >
m = ElementsAre(Gt(5));
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) {
185 Matcher<const list<int>& >
m = ElementsAre(1, Ne(2));
193 TEST(ElementsAreTest, ExplainsNonTrivialMatch) {
194 Matcher<const vector<int>& >
m =
195 ElementsAre(GreaterThan(1), 0, GreaterThan(2));
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",
201 Explain(
m, test_vector));
204 TEST(ElementsAreTest, CanExplainMismatchWrongSize) {
205 Matcher<const list<int>& >
m = ElementsAre(1, 3);
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) {
229 vector<std::string> test_vector;
230 test_vector.push_back(
"test string");
232 EXPECT_THAT(test_vector, ElementsAre(StrEq(
"test string")));
235 TEST(ElementsAreTest, MatchesOneElementList) {
242 TEST(ElementsAreTest, MatchesThreeElementVector) {
243 vector<std::string> test_vector;
244 test_vector.push_back(
"one");
245 test_vector.push_back(
"two");
246 test_vector.push_back(
"three");
248 EXPECT_THAT(test_vector, ElementsAre(
"one", StrEq(
"two"),
_));
251 TEST(ElementsAreTest, MatchesOneElementEqMatcher) {
252 vector<int> test_vector;
253 test_vector.push_back(4);
258 TEST(ElementsAreTest, MatchesOneElementAnyMatcher) {
259 vector<int> test_vector;
260 test_vector.push_back(4);
265 TEST(ElementsAreTest, MatchesOneElementValue) {
266 vector<int> test_vector;
267 test_vector.push_back(4);
272 TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) {
273 vector<int> test_vector;
274 test_vector.push_back(1);
275 test_vector.push_back(2);
276 test_vector.push_back(3);
281 TEST(ElementsAreTest, MatchesTenElementVector) {
282 const int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
288 ElementsAre(0, Ge(0),
_, 3, 4, Ne(2), Eq(6), 7, 8,
_));
291 TEST(ElementsAreTest, DoesNotMatchWrongSize) {
292 vector<std::string> test_vector;
293 test_vector.push_back(
"test string");
294 test_vector.push_back(
"test string");
296 Matcher<vector<std::string> >
m = ElementsAre(StrEq(
"test string"));
300 TEST(ElementsAreTest, DoesNotMatchWrongValue) {
301 vector<std::string> test_vector;
302 test_vector.push_back(
"other string");
304 Matcher<vector<std::string> >
m = ElementsAre(StrEq(
"test string"));
308 TEST(ElementsAreTest, DoesNotMatchWrongOrder) {
309 vector<std::string> test_vector;
310 test_vector.push_back(
"one");
311 test_vector.push_back(
"three");
312 test_vector.push_back(
"two");
314 Matcher<vector<std::string> >
m =
315 ElementsAre(StrEq(
"one"), StrEq(
"two"), StrEq(
"three"));
319 TEST(ElementsAreTest, WorksForNestedContainer) {
325 vector<list<char> > nested;
330 EXPECT_THAT(nested, ElementsAre(ElementsAre(
'H', Ne(
'e')),
331 ElementsAre(
'w',
'o',
_,
_,
'd')));
332 EXPECT_THAT(nested, Not(ElementsAre(ElementsAre(
'H',
'e'),
333 ElementsAre(
'w',
'o',
_,
_,
'd'))));
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;
377 .With(ElementsAre(0, 1));
378 helper.Helper(
array, 2);
381 TEST(ElementsAreTest, WorksWithTwoDimensionalNativeArray) {
382 const char a2[][3] = {
"hi",
"lo" };
383 EXPECT_THAT(a2, ElementsAre(ElementsAre(
'h',
'i',
'\0'),
384 ElementsAre(
'l',
'o',
'\0')));
385 EXPECT_THAT(a2, ElementsAre(StrEq(
"hi"), StrEq(
"lo")));
386 EXPECT_THAT(a2, ElementsAre(Not(ElementsAre(
'h',
'o',
'\0')),
387 ElementsAre(
'l',
'o',
'\0')));
390 TEST(ElementsAreTest, AcceptsStringLiteral) {
404 extern const char kHi[];
406 TEST(ElementsAreTest, AcceptsArrayWithUnknownSize) {
417 const char kHi[] =
"hi";
421 TEST(ElementsAreTest, MakesCopyOfArguments) {
425 ::testing::internal::ElementsAreMatcher<std::tuple<int, int> >
426 polymorphic_matcher = ElementsAre(
x,
y);
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" };
457 test_vector[0] =
"1";
461 TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) {
462 const char*
a[] = {
"one",
"two",
"three" };
467 test_vector[0] =
"1";
471 TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) {
472 const Matcher<std::string> kMatcherArray[] = {StrEq(
"one"), StrEq(
"two"),
475 vector<std::string> test_vector;
476 test_vector.push_back(
"one");
477 test_vector.push_back(
"two");
478 test_vector.push_back(
"three");
479 EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray));
481 test_vector.push_back(
"three");
482 EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray)));
485 TEST(ElementsAreArrayTest, CanBeCreatedWithVector) {
486 const int a[] = { 1, 2, 3 };
489 EXPECT_THAT(test_vector, ElementsAreArray(expected));
490 test_vector.push_back(4);
491 EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));
495 TEST(ElementsAreArrayTest, TakesInitializerList) {
496 const int a[5] = { 1, 2, 3, 4, 5 };
498 EXPECT_THAT(
a, Not(ElementsAreArray({ 1, 2, 3, 5, 4 })));
499 EXPECT_THAT(
a, Not(ElementsAreArray({ 1, 2, 3, 4, 6 })));
502 TEST(ElementsAreArrayTest, TakesInitializerListOfCStrings) {
504 EXPECT_THAT(
a, ElementsAreArray({
"a",
"b",
"c",
"d",
"e" }));
505 EXPECT_THAT(
a, Not(ElementsAreArray({
"a",
"b",
"c",
"e",
"d" })));
506 EXPECT_THAT(
a, Not(ElementsAreArray({
"a",
"b",
"c",
"d",
"ef" })));
509 TEST(ElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {
510 const int a[5] = { 1, 2, 3, 4, 5 };
512 { Eq(1), Eq(2), Eq(3), Eq(4), Eq(5) }));
514 { Eq(1), Eq(2), Eq(3), Eq(4), Eq(6) })));
517 TEST(ElementsAreArrayTest,
518 TakesInitializerListOfDifferentTypedMatchers) {
519 const int a[5] = { 1, 2, 3, 4, 5 };
524 { Eq(1), Ne(-2), Ge(3), Le(4), Eq(5) }));
526 { Eq(1), Ne(-2), Ge(3), Le(4), Eq(6) })));
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(
536 EXPECT_THAT(test_vector, ElementsAreArray(expected));
537 test_vector.push_back(4);
538 EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));
541 TEST(ElementsAreArrayTest, CanBeCreatedWithIteratorRange) {
542 const int a[] = { 1, 2, 3 };
545 EXPECT_THAT(test_vector, ElementsAreArray(expected.begin(), expected.end()));
549 int*
const null_int =
nullptr;
550 EXPECT_THAT(test_vector, Not(ElementsAreArray(null_int, null_int)));
551 EXPECT_THAT((vector<int>()), ElementsAreArray(null_int, null_int));
556 TEST(ElementsAreArrayTest, WorksWithNativeArray) {
565 TEST(ElementsAreArrayTest, SourceLifeSpan) {
566 const int a[] = { 1, 2, 3 };
569 ElementsAreArrayMatcher<int> matcher_maker =
570 ElementsAreArray(expect.begin(), expect.end());
574 typedef vector<int>::iterator
Iter;
575 for (
Iter it = expect.begin();
it != expect.end(); ++
it) { *
it += 10; }
577 test_vector.push_back(3);
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();
676 MATCHER(IsOdd,
"") {
return (arg % 2) != 0; }
679 TEST(MatcherMacroTest, WorksInNamespace) {
680 Matcher<int>
m = matcher_test::IsOdd();
687 return Value(arg, matcher_test::IsOdd()) && arg > 0;
690 TEST(MatcherMacroTest, CanBeComposedUsingValue) {
698 MATCHER_P(IsGreaterThan32And,
n,
"") {
return arg > 32 && arg >
n; }
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));
712 MATCHER_P(_is_Greater_Than32and_,
n,
"") {
return arg > 32 && arg >
n; }
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, 20L,
'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>)",
801 MATCHER_P2(IsNotInClosedRange, low, hi,
"") {
return arg < low || arg > hi; }
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));
817 MATCHER(EqualsSumOf,
"") {
return arg == 0; }
821 MATCHER_P4(EqualsSumOf,
a,
b, c, d,
"") {
return arg ==
a +
b + c +
d; }
822 MATCHER_P5(EqualsSumOf,
a,
b, c, d, e,
"") {
return arg ==
a +
b + c +
d + e; }
824 return arg ==
a +
b + c +
d + e +
f;
827 return arg ==
a +
b + c +
d + e +
f +
g;
830 return arg ==
a +
b + c +
d + e +
f +
g +
h;
832 MATCHER_P9(EqualsSumOf,
a,
b, c, d, e,
f,
g,
h,
i,
"") {
833 return arg ==
a +
b + c +
d + e +
f +
g +
h +
i;
835 MATCHER_P10(EqualsSumOf,
a,
b, c, d, e,
f,
g,
h,
i, j,
"") {
836 return arg ==
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',
864 EXPECT_THAT(-1234, Not(EqualsSumOf(1000, 200, 30, 4)));
865 EXPECT_THAT(-12345, Not(EqualsSumOf(10000, 2000, 300, 40, 5)));
867 Not(EqualsSumOf(::
std::string(
"a"),
'b',
'c',
"d",
"e",
'f')));
869 Not(EqualsSumOf(::
std::string(
"a"),
'b',
'c',
"d",
"e",
'f',
872 Not(EqualsSumOf(::
std::string(
"a"),
'b',
'c',
"d",
"e",
'f',
'g',
875 Not(EqualsSumOf(::
std::string(
"a"),
'b',
'c',
"d",
"e",
'f',
'g',
878 Not(EqualsSumOf(::
std::string(
"a"),
'b',
'c',
"d",
"e",
'f',
'g',
884 TEST(MatcherPnMacroTest, WorksForDifferentParameterTypes) {
885 EXPECT_THAT(123, EqualsSumOf(100L, 20,
static_cast<char>(3)));
888 EXPECT_THAT(124, Not(EqualsSumOf(100L, 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');
958 const int count =
static_cast<int>(
Value(arg, m1))
959 +
static_cast<int>(
Value(arg, m2)) +
static_cast<int>(
Value(arg, m3));
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 };
1024 Matcher<
const int (&)[2]>
m =
Contains(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 };
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) {
1143 std::vector<Matcher<int>> matchers{Ge(1), Lt(2)};
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) {
1195 std::vector<Matcher<int>> matchers{Lt(1), Ge(2)};
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(
_,
_,
_,
_,
_,
_,
_,
_,
_,
1243 testing::AllOf(
_,
_,
_,
_,
_,
_,
_,
_,
_,
_)));
1246 TEST(AnyOfTest, HugeMatcher) {
1249 EXPECT_THAT(0, testing::AnyOf(
_,
_,
_,
_,
_,
_,
_,
_,
_,
1250 testing::AnyOf(
_,
_,
_,
_,
_,
_,
_,
_,
_,
_)));
1262 MATCHER(M,
"") {
return true; }
1264 template <
typename T1,
typename T2>
1265 bool AllOf(
const T1& t1,
const T2& 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));
1285 EXPECT_THAT(
p, AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(5))));
1286 EXPECT_THAT(
p, Not(AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(3)))));
1289 TEST(AnyOfTest, WorksOnMoveOnlyType) {
1290 std::unique_ptr<int>
p(
new int(3));
1291 EXPECT_THAT(
p, AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Lt(5))));
1292 EXPECT_THAT(
p, Not(AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Gt(5)))));
1296 return arg !=
nullptr;
1301 TEST(MatcherMacroTest, WorksOnMoveOnlyType) {
1302 std::unique_ptr<int>
p(
new int(3));
1304 EXPECT_THAT(std::unique_ptr<int>(), Not(IsNotNull()));
1308 return *arg == pointee;
1313 TEST(MatcherPMacroTest, WorksOnMoveOnlyType) {
1314 std::unique_ptr<int>
p(
new int(3));
1323 # pragma warning(pop)