1 // Copyright 2008, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 // Google Mock - a framework for writing C++ mock classes.
32 // This file tests the built-in matchers generated by a script.
34 // Silence warning C4244: 'initializing': conversion from 'int' to 'short',
35 // possible loss of data and C4100, unreferenced local parameter
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;
66 using testing::make_tuple;
72 using testing::Contains;
73 using testing::ElementsAre;
74 using testing::ElementsAreArray;
80 using testing::MakeMatcher;
81 using testing::Matcher;
82 using testing::MatcherInterface;
83 using testing::MatchResultListener;
86 using testing::Pointee;
87 using testing::PrintToString;
89 using testing::StaticAssertTypeEq;
92 using testing::internal::ElementsAreArrayMatcher;
94 // Returns the description of the given matcher.
96 std::string Describe(const Matcher<T>& m) {
102 // Returns the description of the negation of the given matcher.
103 template <typename T>
104 std::string DescribeNegation(const Matcher<T>& m) {
106 m.DescribeNegationTo(&ss);
110 // Returns the reason why x matches, or doesn't match, m.
111 template <typename MatcherType, typename Value>
112 std::string Explain(const MatcherType& m, const Value& x) {
114 m.ExplainMatchResultTo(x, &ss);
118 // Tests Args<k0, ..., kn>(m).
120 TEST(ArgsTest, AcceptsZeroTemplateArg) {
121 const tuple<int, bool> t(5, true);
122 EXPECT_THAT(t, Args<>(Eq(tuple<>())));
123 EXPECT_THAT(t, Not(Args<>(Ne(tuple<>()))));
126 TEST(ArgsTest, AcceptsOneTemplateArg) {
127 const tuple<int, bool> t(5, true);
128 EXPECT_THAT(t, Args<0>(Eq(make_tuple(5))));
129 EXPECT_THAT(t, Args<1>(Eq(make_tuple(true))));
130 EXPECT_THAT(t, Not(Args<1>(Eq(make_tuple(false)))));
133 TEST(ArgsTest, AcceptsTwoTemplateArgs) {
134 const tuple<short, int, long> t(4, 5, 6L); // NOLINT
136 EXPECT_THAT(t, (Args<0, 1>(Lt())));
137 EXPECT_THAT(t, (Args<1, 2>(Lt())));
138 EXPECT_THAT(t, Not(Args<0, 2>(Gt())));
141 TEST(ArgsTest, AcceptsRepeatedTemplateArgs) {
142 const tuple<short, int, long> t(4, 5, 6L); // NOLINT
143 EXPECT_THAT(t, (Args<0, 0>(Eq())));
144 EXPECT_THAT(t, Not(Args<1, 1>(Ne())));
147 TEST(ArgsTest, AcceptsDecreasingTemplateArgs) {
148 const tuple<short, int, long> t(4, 5, 6L); // NOLINT
149 EXPECT_THAT(t, (Args<2, 0>(Gt())));
150 EXPECT_THAT(t, Not(Args<2, 1>(Lt())));
153 // The MATCHER*() macros trigger warning C4100 (unreferenced formal
154 // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
155 // the macro definition, as the warnings are generated when the macro
156 // is expanded and macro expansion cannot contain #pragma. Therefore
157 // we suppress them here.
159 # pragma warning(push)
160 # pragma warning(disable:4100)
163 MATCHER(SumIsZero, "") {
164 return get<0>(arg) + get<1>(arg) + get<2>(arg) == 0;
167 TEST(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())));
172 TEST(ArgsTest, CanBeNested) {
173 const tuple<short, int, long, int> t(4, 5, 6L, 6); // NOLINT
174 EXPECT_THAT(t, (Args<1, 2, 3>(Args<1, 2>(Eq()))));
175 EXPECT_THAT(t, (Args<0, 1, 3>(Args<0, 2>(Lt()))));
178 TEST(ArgsTest, CanMatchTupleByValue) {
179 typedef tuple<char, int, int> Tuple3;
180 const Matcher<Tuple3> m = Args<1, 2>(Lt());
181 EXPECT_TRUE(m.Matches(Tuple3('a', 1, 2)));
182 EXPECT_FALSE(m.Matches(Tuple3('b', 2, 2)));
185 TEST(ArgsTest, CanMatchTupleByReference) {
186 typedef tuple<char, char, int> Tuple3;
187 const Matcher<const Tuple3&> m = Args<0, 1>(Lt());
188 EXPECT_TRUE(m.Matches(Tuple3('a', 'b', 2)));
189 EXPECT_FALSE(m.Matches(Tuple3('b', 'b', 2)));
192 // Validates that arg is printed as str.
193 MATCHER_P(PrintsAs, str, "") {
194 return testing::PrintToString(arg) == str;
197 TEST(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)"))));
206 TEST(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",
213 TEST(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",
221 TEST(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",
225 DescribeNegation(m));
228 TEST(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)));
236 // For testing Args<>'s explanation.
237 class 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 {
243 const int diff = get<0>(value) - get<1>(value);
245 *listener << "where the first value is " << diff
246 << " more than the second";
252 Matcher<tuple<char, int> > LessThan() {
253 return MakeMatcher(new LessThanMatcher);
256 TEST(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)));
265 // For testing ExplainMatchResultTo().
266 class 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_;
292 Matcher<int> GreaterThan(int n) {
293 return MakeMatcher(new GreaterThanMatcher(n));
296 // Tests for ElementsAre().
298 TEST(ElementsAreTest, CanDescribeExpectingNoElement) {
299 Matcher<const vector<int>&> m = ElementsAre();
300 EXPECT_EQ("is empty", Describe(m));
303 TEST(ElementsAreTest, CanDescribeExpectingOneElement) {
304 Matcher<vector<int> > m = ElementsAre(Gt(5));
305 EXPECT_EQ("has 1 element that is > 5", Describe(m));
308 TEST(ElementsAreTest, CanDescribeExpectingManyElements) {
309 Matcher<list<std::string> > m = ElementsAre(StrEq("one"), "two");
310 EXPECT_EQ("has 2 elements where\n"
311 "element #0 is equal to \"one\",\n"
312 "element #1 is equal to \"two\"", Describe(m));
315 TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) {
316 Matcher<vector<int> > m = ElementsAre();
317 EXPECT_EQ("isn't empty", DescribeNegation(m));
320 TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElment) {
321 Matcher<const list<int>& > m = ElementsAre(Gt(5));
322 EXPECT_EQ("doesn't have 1 element, or\n"
323 "element #0 isn't > 5", DescribeNegation(m));
326 TEST(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"
330 "element #1 isn't equal to \"two\"", DescribeNegation(m));
333 TEST(ElementsAreTest, DoesNotExplainTrivialMatch) {
334 Matcher<const list<int>& > m = ElementsAre(1, Ne(2));
337 test_list.push_back(1);
338 test_list.push_back(3);
339 EXPECT_EQ("", Explain(m, test_list)); // No need to explain anything.
342 TEST(ElementsAreTest, ExplainsNonTrivialMatch) {
343 Matcher<const vector<int>& > m =
344 ElementsAre(GreaterThan(1), 0, GreaterThan(2));
346 const int a[] = { 10, 0, 100 };
347 vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a));
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",
350 Explain(m, test_vector));
353 TEST(ElementsAreTest, CanExplainMismatchWrongSize) {
354 Matcher<const list<int>& > m = ElementsAre(1, 3);
357 // No need to explain when the container is empty.
358 EXPECT_EQ("", Explain(m, test_list));
360 test_list.push_back(1);
361 EXPECT_EQ("which has 1 element", Explain(m, test_list));
364 TEST(ElementsAreTest, CanExplainMismatchRightSize) {
365 Matcher<const vector<int>& > m = ElementsAre(1, GreaterThan(5));
370 EXPECT_EQ("whose element #0 doesn't match", Explain(m, v));
373 EXPECT_EQ("whose element #1 doesn't match, which is 4 less than 5",
377 TEST(ElementsAreTest, MatchesOneElementVector) {
378 vector<std::string> test_vector;
379 test_vector.push_back("test string");
381 EXPECT_THAT(test_vector, ElementsAre(StrEq("test string")));
384 TEST(ElementsAreTest, MatchesOneElementList) {
385 list<std::string> test_list;
386 test_list.push_back("test string");
388 EXPECT_THAT(test_list, ElementsAre("test string"));
391 TEST(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"), _));
400 TEST(ElementsAreTest, MatchesOneElementEqMatcher) {
401 vector<int> test_vector;
402 test_vector.push_back(4);
404 EXPECT_THAT(test_vector, ElementsAre(Eq(4)));
407 TEST(ElementsAreTest, MatchesOneElementAnyMatcher) {
408 vector<int> test_vector;
409 test_vector.push_back(4);
411 EXPECT_THAT(test_vector, ElementsAre(_));
414 TEST(ElementsAreTest, MatchesOneElementValue) {
415 vector<int> test_vector;
416 test_vector.push_back(4);
418 EXPECT_THAT(test_vector, ElementsAre(4));
421 TEST(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), _));
430 TEST(ElementsAreTest, MatchesTenElementVector) {
431 const int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
432 vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a));
434 EXPECT_THAT(test_vector,
435 // The element list can contain values and/or matchers
436 // of different types.
437 ElementsAre(0, Ge(0), _, 3, 4, Ne(2), Eq(6), 7, 8, _));
440 TEST(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"));
446 EXPECT_FALSE(m.Matches(test_vector));
449 TEST(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"));
454 EXPECT_FALSE(m.Matches(test_vector));
457 TEST(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"));
465 EXPECT_FALSE(m.Matches(test_vector));
468 TEST(ElementsAreTest, WorksForNestedContainer) {
469 const char* strings[] = {
474 vector<list<char> > nested;
475 for (size_t i = 0; i < GTEST_ARRAY_SIZE_(strings); i++) {
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'))));
485 TEST(ElementsAreTest, WorksWithByRefElementMatchers) {
486 int a[] = { 0, 1, 2 };
487 vector<int> v(a, a + GTEST_ARRAY_SIZE_(a));
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]))));
493 TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) {
494 int a[] = { 0, 1, 2 };
495 vector<int> v(a, a + GTEST_ARRAY_SIZE_(a));
497 EXPECT_THAT(&v, Pointee(ElementsAre(0, 1, _)));
498 EXPECT_THAT(&v, Not(Pointee(ElementsAre(0, _, 3))));
501 TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) {
502 int array[] = { 0, 1, 2 };
503 EXPECT_THAT(array, ElementsAre(0, 1, _));
504 EXPECT_THAT(array, Not(ElementsAre(1, _, _)));
505 EXPECT_THAT(array, Not(ElementsAre(0, _)));
508 class NativeArrayPassedAsPointerAndSize {
510 NativeArrayPassedAsPointerAndSize() {}
512 MOCK_METHOD2(Helper, void(int* array, int size));
515 GTEST_DISALLOW_COPY_AND_ASSIGN_(NativeArrayPassedAsPointerAndSize);
518 TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) {
519 int array[] = { 0, 1 };
520 ::testing::tuple<int*, size_t> array_as_tuple(array, 2);
521 EXPECT_THAT(array_as_tuple, ElementsAre(0, 1));
522 EXPECT_THAT(array_as_tuple, Not(ElementsAre(0)));
524 NativeArrayPassedAsPointerAndSize helper;
525 EXPECT_CALL(helper, Helper(_, _))
526 .With(ElementsAre(0, 1));
527 helper.Helper(array, 2);
530 TEST(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')));
539 TEST(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")));
547 // The following test passes a value of type const char[] to a
548 // function template that expects const T&. Some versions of MSVC
549 // generates a compiler error C2665 for that. We believe it's a bug
550 // in MSVC. Therefore this test is #if-ed out for MSVC.
552 // Declared here with the size unknown. Defined AFTER the following test.
553 extern const char kHi[];
555 TEST(ElementsAreTest, AcceptsArrayWithUnknownSize) {
556 // The size of kHi is not known in this test, but ElementsAre() should
559 std::string array1[] = {"hi"};
560 EXPECT_THAT(array1, ElementsAre(kHi));
562 std::string array2[] = {"ho"};
563 EXPECT_THAT(array2, Not(ElementsAre(kHi)));
566 const char kHi[] = "hi";
570 TEST(ElementsAreTest, MakesCopyOfArguments) {
573 // This should make a copy of x and y.
574 ::testing::internal::ElementsAreMatcher<testing::tuple<int, int> >
575 polymorphic_matcher = ElementsAre(x, y);
576 // Changing x and y now shouldn't affect the meaning of the above matcher.
578 const int array1[] = { 1, 2 };
579 EXPECT_THAT(array1, polymorphic_matcher);
580 const int array2[] = { 0, 0 };
581 EXPECT_THAT(array2, Not(polymorphic_matcher));
585 // Tests for ElementsAreArray(). Since ElementsAreArray() shares most
586 // of the implementation with ElementsAre(), we don't test it as
589 TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) {
590 const int a[] = { 1, 2, 3 };
592 vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a));
593 EXPECT_THAT(test_vector, ElementsAreArray(a));
596 EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
599 TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) {
600 const char* a[] = { "one", "two", "three" };
602 vector<std::string> test_vector(a, a + GTEST_ARRAY_SIZE_(a));
603 EXPECT_THAT(test_vector, ElementsAreArray(a, GTEST_ARRAY_SIZE_(a)));
606 test_vector[0] = "1";
607 EXPECT_THAT(test_vector, Not(ElementsAreArray(p, GTEST_ARRAY_SIZE_(a))));
610 TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) {
611 const char* a[] = { "one", "two", "three" };
613 vector<std::string> test_vector(a, a + GTEST_ARRAY_SIZE_(a));
614 EXPECT_THAT(test_vector, ElementsAreArray(a));
616 test_vector[0] = "1";
617 EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
620 TEST(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)));
634 TEST(ElementsAreArrayTest, CanBeCreatedWithVector) {
635 const int a[] = { 1, 2, 3 };
636 vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a));
637 const vector<int> expected(a, a + GTEST_ARRAY_SIZE_(a));
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_
645 TEST(ElementsAreArrayTest, TakesInitializerList) {
646 const int a[5] = { 1, 2, 3, 4, 5 };
647 EXPECT_THAT(a, ElementsAreArray({ 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 })));
652 TEST(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" })));
659 TEST(ElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {
660 const int a[5] = { 1, 2, 3, 4, 5 };
661 EXPECT_THAT(a, ElementsAreArray(
662 { Eq(1), Eq(2), Eq(3), Eq(4), Eq(5) }));
663 EXPECT_THAT(a, Not(ElementsAreArray(
664 { Eq(1), Eq(2), Eq(3), Eq(4), Eq(6) })));
667 TEST(ElementsAreArrayTest,
668 TakesInitializerListOfDifferentTypedMatchers) {
669 const int a[5] = { 1, 2, 3, 4, 5 };
670 // The compiler cannot infer the type of the initializer list if its
671 // elements have different types. We must explicitly specify the
672 // unified element type in this case.
673 EXPECT_THAT(a, ElementsAreArray<Matcher<int> >(
674 { Eq(1), Ne(-2), Ge(3), Le(4), Eq(5) }));
675 EXPECT_THAT(a, Not(ElementsAreArray<Matcher<int> >(
676 { Eq(1), Ne(-2), Ge(3), Le(4), Eq(6) })));
679 #endif // GTEST_HAS_STD_INITIALIZER_LIST_
681 TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherVector) {
682 const int a[] = { 1, 2, 3 };
683 const Matcher<int> kMatchers[] = { Eq(1), Eq(2), Eq(3) };
684 vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a));
685 const vector<Matcher<int> > expected(
686 kMatchers, kMatchers + GTEST_ARRAY_SIZE_(kMatchers));
687 EXPECT_THAT(test_vector, ElementsAreArray(expected));
688 test_vector.push_back(4);
689 EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));
692 TEST(ElementsAreArrayTest, CanBeCreatedWithIteratorRange) {
693 const int a[] = { 1, 2, 3 };
694 const vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a));
695 const vector<int> expected(a, a + GTEST_ARRAY_SIZE_(a));
696 EXPECT_THAT(test_vector, ElementsAreArray(expected.begin(), expected.end()));
697 // Pointers are iterators, too.
698 EXPECT_THAT(test_vector, ElementsAreArray(a, a + GTEST_ARRAY_SIZE_(a)));
699 // The empty range of NULL pointers should also be okay.
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));
705 // Since ElementsAre() and ElementsAreArray() share much of the
706 // implementation, we only do a sanity test for native arrays here.
707 TEST(ElementsAreArrayTest, WorksWithNativeArray) {
708 ::std::string a[] = { "hi", "ho" };
709 ::std::string b[] = { "hi", "ho" };
711 EXPECT_THAT(a, ElementsAreArray(b));
712 EXPECT_THAT(a, ElementsAreArray(b, 2));
713 EXPECT_THAT(a, Not(ElementsAreArray(b, 1)));
716 TEST(ElementsAreArrayTest, SourceLifeSpan) {
717 const int a[] = { 1, 2, 3 };
718 vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a));
719 vector<int> expect(a, a + GTEST_ARRAY_SIZE_(a));
720 ElementsAreArrayMatcher<int> matcher_maker =
721 ElementsAreArray(expect.begin(), expect.end());
722 EXPECT_THAT(test_vector, matcher_maker);
723 // Changing in place the values that initialized matcher_maker should not
724 // affect matcher_maker anymore. It should have made its own copy of them.
725 typedef vector<int>::iterator Iter;
726 for (Iter it = expect.begin(); it != expect.end(); ++it) { *it += 10; }
727 EXPECT_THAT(test_vector, matcher_maker);
728 test_vector.push_back(3);
729 EXPECT_THAT(test_vector, Not(matcher_maker));
732 // Tests for the MATCHER*() macro family.
734 // Tests that a simple MATCHER() definition works.
736 MATCHER(IsEven, "") { return (arg % 2) == 0; }
738 TEST(MatcherMacroTest, Works) {
739 const Matcher<int> m = IsEven();
740 EXPECT_TRUE(m.Matches(6));
741 EXPECT_FALSE(m.Matches(7));
743 EXPECT_EQ("is even", Describe(m));
744 EXPECT_EQ("not (is even)", DescribeNegation(m));
745 EXPECT_EQ("", Explain(m, 6));
746 EXPECT_EQ("", Explain(m, 7));
749 // This also tests that the description string can reference 'negation'.
750 MATCHER(IsEven2, negation ? "is odd" : "is even") {
751 if ((arg % 2) == 0) {
752 // Verifies that we can stream to result_listener, a listener
753 // supplied by the MATCHER macro implicitly.
754 *result_listener << "OK";
757 *result_listener << "% 2 == " << (arg % 2);
762 // This also tests that the description string can reference matcher
764 MATCHER_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";
771 // Verifies that we can stream to the underlying stream of
773 if (result_listener->stream() != NULL) {
774 *result_listener->stream() << "diff == " << (x + y - arg);
780 // Tests that the matcher description can reference 'negation' and the
781 // matcher parameters.
782 TEST(MatcherMacroTest, DescriptionCanReferenceNegationAndParameters) {
783 const Matcher<int> m1 = IsEven2();
784 EXPECT_EQ("is even", Describe(m1));
785 EXPECT_EQ("is odd", DescribeNegation(m1));
787 const Matcher<int> m2 = EqSumOf(5, 9);
788 EXPECT_EQ("equals the sum of 5 and 9", Describe(m2));
789 EXPECT_EQ("doesn't equal the sum of 5 and 9", DescribeNegation(m2));
792 // Tests explaining match result in a MATCHER* macro.
793 TEST(MatcherMacroTest, CanExplainMatchResult) {
794 const Matcher<int> m1 = IsEven2();
795 EXPECT_EQ("OK", Explain(m1, 4));
796 EXPECT_EQ("% 2 == 1", Explain(m1, 5));
798 const Matcher<int> m2 = EqSumOf(1, 2);
799 EXPECT_EQ("OK", Explain(m2, 3));
800 EXPECT_EQ("diff == -1", Explain(m2, 4));
803 // Tests that the body of MATCHER() can reference the type of the
804 // value being matched.
806 MATCHER(IsEmptyString, "") {
807 StaticAssertTypeEq< ::std::string, arg_type>();
811 MATCHER(IsEmptyStringByRef, "") {
812 StaticAssertTypeEq<const ::std::string&, arg_type>();
816 TEST(MatcherMacroTest, CanReferenceArgType) {
817 const Matcher< ::std::string> m1 = IsEmptyString();
818 EXPECT_TRUE(m1.Matches(""));
820 const Matcher<const ::std::string&> m2 = IsEmptyStringByRef();
821 EXPECT_TRUE(m2.Matches(""));
824 // Tests that MATCHER() can be used in a namespace.
826 namespace matcher_test {
827 MATCHER(IsOdd, "") { return (arg % 2) != 0; }
828 } // namespace matcher_test
830 TEST(MatcherMacroTest, WorksInNamespace) {
831 Matcher<int> m = matcher_test::IsOdd();
832 EXPECT_FALSE(m.Matches(4));
833 EXPECT_TRUE(m.Matches(5));
836 // Tests that Value() can be used to compose matchers.
837 MATCHER(IsPositiveOdd, "") {
838 return Value(arg, matcher_test::IsOdd()) && arg > 0;
841 TEST(MatcherMacroTest, CanBeComposedUsingValue) {
842 EXPECT_THAT(3, IsPositiveOdd());
843 EXPECT_THAT(4, Not(IsPositiveOdd()));
844 EXPECT_THAT(-1, Not(IsPositiveOdd()));
847 // Tests that a simple MATCHER_P() definition works.
849 MATCHER_P(IsGreaterThan32And, n, "") { return arg > 32 && arg > n; }
851 TEST(MatcherPMacroTest, Works) {
852 const Matcher<int> m = IsGreaterThan32And(5);
853 EXPECT_TRUE(m.Matches(36));
854 EXPECT_FALSE(m.Matches(5));
856 EXPECT_EQ("is greater than 32 and 5", Describe(m));
857 EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m));
858 EXPECT_EQ("", Explain(m, 36));
859 EXPECT_EQ("", Explain(m, 5));
862 // Tests that the description is calculated correctly from the matcher name.
863 MATCHER_P(_is_Greater_Than32and_, n, "") { return arg > 32 && arg > n; }
865 TEST(MatcherPMacroTest, GeneratesCorrectDescription) {
866 const Matcher<int> m = _is_Greater_Than32and_(5);
868 EXPECT_EQ("is greater than 32 and 5", Describe(m));
869 EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m));
870 EXPECT_EQ("", Explain(m, 36));
871 EXPECT_EQ("", Explain(m, 5));
874 // Tests that a MATCHER_P matcher can be explicitly instantiated with
875 // a reference parameter type.
877 class UncopyableFoo {
879 explicit UncopyableFoo(char value) : value_(value) {}
881 UncopyableFoo(const UncopyableFoo&);
882 void operator=(const UncopyableFoo&);
887 MATCHER_P(ReferencesUncopyable, variable, "") { return &arg == &variable; }
889 TEST(MatcherPMacroTest, WorksWhenExplicitlyInstantiatedWithReference) {
890 UncopyableFoo foo1('1'), foo2('2');
891 const Matcher<const UncopyableFoo&> m =
892 ReferencesUncopyable<const UncopyableFoo&>(foo1);
894 EXPECT_TRUE(m.Matches(foo1));
895 EXPECT_FALSE(m.Matches(foo2));
897 // We don't want the address of the parameter printed, as most
898 // likely it will just annoy the user. If the address is
899 // interesting, the user should consider passing the parameter by
901 EXPECT_EQ("references uncopyable 1-byte object <31>", Describe(m));
905 // Tests that the body of MATCHER_Pn() can reference the parameter
908 MATCHER_P3(ParamTypesAreIntLongAndChar, foo, bar, baz, "") {
909 StaticAssertTypeEq<int, foo_type>();
910 StaticAssertTypeEq<long, bar_type>(); // NOLINT
911 StaticAssertTypeEq<char, baz_type>();
915 TEST(MatcherPnMacroTest, CanReferenceParamTypes) {
916 EXPECT_THAT(0, ParamTypesAreIntLongAndChar(10, 20L, 'a'));
919 // Tests that a MATCHER_Pn matcher can be explicitly instantiated with
920 // reference parameter types.
922 MATCHER_P2(ReferencesAnyOf, variable1, variable2, "") {
923 return &arg == &variable1 || &arg == &variable2;
926 TEST(MatcherPnMacroTest, WorksWhenExplicitlyInstantiatedWithReferences) {
927 UncopyableFoo foo1('1'), foo2('2'), foo3('3');
928 const Matcher<const UncopyableFoo&> m =
929 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
931 EXPECT_TRUE(m.Matches(foo1));
932 EXPECT_TRUE(m.Matches(foo2));
933 EXPECT_FALSE(m.Matches(foo3));
936 TEST(MatcherPnMacroTest,
937 GeneratesCorretDescriptionWhenExplicitlyInstantiatedWithReferences) {
938 UncopyableFoo foo1('1'), foo2('2');
939 const Matcher<const UncopyableFoo&> m =
940 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
942 // We don't want the addresses of the parameters printed, as most
943 // likely they will just annoy the user. If the addresses are
944 // interesting, the user should consider passing the parameters by
946 EXPECT_EQ("references any of (1-byte object <31>, 1-byte object <32>)",
950 // Tests that a simple MATCHER_P2() definition works.
952 MATCHER_P2(IsNotInClosedRange, low, hi, "") { return arg < low || arg > hi; }
954 TEST(MatcherPnMacroTest, Works) {
955 const Matcher<const long&> m = IsNotInClosedRange(10, 20); // NOLINT
956 EXPECT_TRUE(m.Matches(36L));
957 EXPECT_FALSE(m.Matches(15L));
959 EXPECT_EQ("is not in closed range (10, 20)", Describe(m));
960 EXPECT_EQ("not (is not in closed range (10, 20))", DescribeNegation(m));
961 EXPECT_EQ("", Explain(m, 36L));
962 EXPECT_EQ("", Explain(m, 15L));
965 // Tests that MATCHER*() definitions can be overloaded on the number
966 // of parameters; also tests MATCHER_Pn() where n >= 3.
968 MATCHER(EqualsSumOf, "") { return arg == 0; }
969 MATCHER_P(EqualsSumOf, a, "") { return arg == a; }
970 MATCHER_P2(EqualsSumOf, a, b, "") { return arg == a + b; }
971 MATCHER_P3(EqualsSumOf, a, b, c, "") { return arg == a + b + c; }
972 MATCHER_P4(EqualsSumOf, a, b, c, d, "") { return arg == a + b + c + d; }
973 MATCHER_P5(EqualsSumOf, a, b, c, d, e, "") { return arg == a + b + c + d + e; }
974 MATCHER_P6(EqualsSumOf, a, b, c, d, e, f, "") {
975 return arg == a + b + c + d + e + f;
977 MATCHER_P7(EqualsSumOf, a, b, c, d, e, f, g, "") {
978 return arg == a + b + c + d + e + f + g;
980 MATCHER_P8(EqualsSumOf, a, b, c, d, e, f, g, h, "") {
981 return arg == a + b + c + d + e + f + g + h;
983 MATCHER_P9(EqualsSumOf, a, b, c, d, e, f, g, h, i, "") {
984 return arg == a + b + c + d + e + f + g + h + i;
986 MATCHER_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;
990 TEST(MatcherPnMacroTest, CanBeOverloadedOnNumberOfParameters) {
991 EXPECT_THAT(0, EqualsSumOf());
992 EXPECT_THAT(1, EqualsSumOf(1));
993 EXPECT_THAT(12, EqualsSumOf(10, 2));
994 EXPECT_THAT(123, EqualsSumOf(100, 20, 3));
995 EXPECT_THAT(1234, EqualsSumOf(1000, 200, 30, 4));
996 EXPECT_THAT(12345, EqualsSumOf(10000, 2000, 300, 40, 5));
997 EXPECT_THAT("abcdef",
998 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f'));
999 EXPECT_THAT("abcdefg",
1000 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g'));
1001 EXPECT_THAT("abcdefgh",
1002 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
1004 EXPECT_THAT("abcdefghi",
1005 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
1007 EXPECT_THAT("abcdefghij",
1008 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
1009 "h", 'i', ::std::string("j")));
1011 EXPECT_THAT(1, Not(EqualsSumOf()));
1012 EXPECT_THAT(-1, Not(EqualsSumOf(1)));
1013 EXPECT_THAT(-12, Not(EqualsSumOf(10, 2)));
1014 EXPECT_THAT(-123, Not(EqualsSumOf(100, 20, 3)));
1015 EXPECT_THAT(-1234, Not(EqualsSumOf(1000, 200, 30, 4)));
1016 EXPECT_THAT(-12345, Not(EqualsSumOf(10000, 2000, 300, 40, 5)));
1017 EXPECT_THAT("abcdef ",
1018 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f')));
1019 EXPECT_THAT("abcdefg ",
1020 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f',
1022 EXPECT_THAT("abcdefgh ",
1023 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
1025 EXPECT_THAT("abcdefghi ",
1026 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
1028 EXPECT_THAT("abcdefghij ",
1029 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
1030 "h", 'i', ::std::string("j"))));
1033 // Tests that a MATCHER_Pn() definition can be instantiated with any
1034 // compatible parameter types.
1035 TEST(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")));
1043 // Tests that the matcher body can promote the parameter types.
1045 MATCHER_P2(EqConcat, prefix, suffix, "") {
1046 // The following lines promote the two parameters to desired types.
1047 std::string prefix_str(prefix);
1048 char suffix_char = static_cast<char>(suffix);
1049 return arg == prefix_str + suffix_char;
1052 TEST(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'));
1057 EXPECT_FALSE(no_promo.Matches("fool"));
1058 EXPECT_FALSE(promo.Matches("fool"));
1059 EXPECT_TRUE(no_promo.Matches("foot"));
1060 EXPECT_TRUE(promo.Matches("foot"));
1063 // Verifies the type of a MATCHER*.
1065 TEST(MatcherPnMacroTest, TypesAreCorrect) {
1066 // EqualsSumOf() must be assignable to a EqualsSumOfMatcher variable.
1067 EqualsSumOfMatcher a0 = EqualsSumOf();
1069 // EqualsSumOf(1) must be assignable to a EqualsSumOfMatcherP variable.
1070 EqualsSumOfMatcherP<int> a1 = EqualsSumOf(1);
1072 // EqualsSumOf(p1, ..., pk) must be assignable to a EqualsSumOfMatcherPk
1073 // variable, and so on.
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');
1090 // Avoid "unused variable" warnings.
1104 // Tests that matcher-typed parameters can be used in Value() inside a
1105 // MATCHER_Pn definition.
1107 // Succeeds if arg matches exactly 2 of the 3 matchers.
1108 MATCHER_P3(TwoOf, m1, m2, m3, "") {
1109 const int count = static_cast<int>(Value(arg, m1))
1110 + static_cast<int>(Value(arg, m2)) + static_cast<int>(Value(arg, m3));
1114 TEST(MatcherPnMacroTest, CanUseMatcherTypedParameterInValue) {
1115 EXPECT_THAT(42, TwoOf(Gt(0), Lt(50), Eq(10)));
1116 EXPECT_THAT(0, Not(TwoOf(Gt(-1), Lt(1), Eq(0))));
1119 // Tests Contains().
1121 TEST(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);
1126 EXPECT_THAT(some_list, Contains(1));
1127 EXPECT_THAT(some_list, Contains(Gt(2.5)));
1128 EXPECT_THAT(some_list, Contains(Eq(2.0f)));
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")));
1138 TEST(ContainsTest, ListDoesNotMatchWhenElementIsNotInContainer) {
1139 list<int> some_list;
1140 some_list.push_back(3);
1141 some_list.push_back(1);
1142 EXPECT_THAT(some_list, Not(Contains(4)));
1145 TEST(ContainsTest, SetMatchesWhenElementIsInContainer) {
1150 EXPECT_THAT(some_set, Contains(Eq(1.0)));
1151 EXPECT_THAT(some_set, Contains(Eq(3.0f)));
1152 EXPECT_THAT(some_set, Contains(2));
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"))));
1162 TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) {
1166 EXPECT_THAT(some_set, Not(Contains(4)));
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())));
1173 TEST(ContainsTest, ExplainsMatchResultCorrectly) {
1174 const int a[2] = { 1, 2 };
1175 Matcher<const int (&)[2]> m = Contains(2);
1176 EXPECT_EQ("whose element #1 matches", Explain(m, a));
1179 EXPECT_EQ("", Explain(m, a));
1181 m = Contains(GreaterThan(0));
1182 EXPECT_EQ("whose element #0 matches, which is 1 more than 0", Explain(m, a));
1184 m = Contains(GreaterThan(10));
1185 EXPECT_EQ("", Explain(m, a));
1188 TEST(ContainsTest, DescribesItselfCorrectly) {
1189 Matcher<vector<int> > m = Contains(1);
1190 EXPECT_EQ("contains at least one element that is equal to 1", Describe(m));
1192 Matcher<vector<int> > m2 = Not(m);
1193 EXPECT_EQ("doesn't contain any element that is equal to 1", Describe(m2));
1196 TEST(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;
1207 EXPECT_THAT(another_map,
1208 Contains(pair<const std::string, int>(std::string("fee"), 1)));
1209 EXPECT_THAT(another_map, Contains(pair<const std::string, int>("fie", 2)));
1212 TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) {
1213 map<int, int> some_map;
1216 EXPECT_THAT(some_map, Not(Contains(pair<const int, int>(2, 23))));
1219 TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) {
1220 const char* string_array[] = { "fee", "fie", "foe", "fum" };
1221 EXPECT_THAT(string_array, Contains(Eq(std::string("fum"))));
1224 TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) {
1225 int int_array[] = { 1, 2, 3, 4 };
1226 EXPECT_THAT(int_array, Not(Contains(5)));
1229 TEST(ContainsTest, AcceptsMatcher) {
1230 const int a[] = { 1, 2, 3 };
1231 EXPECT_THAT(a, Contains(Gt(2)));
1232 EXPECT_THAT(a, Not(Contains(Gt(4))));
1235 TEST(ContainsTest, WorksForNativeArrayAsTuple) {
1236 const int a[] = { 1, 2 };
1237 const int* const pointer = a;
1238 EXPECT_THAT(make_tuple(pointer, 2), Contains(1));
1239 EXPECT_THAT(make_tuple(pointer, 2), Not(Contains(Gt(3))));
1242 TEST(ContainsTest, WorksForTwoDimensionalNativeArray) {
1243 int a[][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
1244 EXPECT_THAT(a, Contains(ElementsAre(4, 5, 6)));
1245 EXPECT_THAT(a, Contains(Contains(5)));
1246 EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5))));
1247 EXPECT_THAT(a, Contains(Not(Contains(5))));
1250 TEST(AllOfTest, HugeMatcher) {
1251 // Verify that using AllOf with many arguments doesn't cause
1252 // the compiler to exceed template instantiation depth limit.
1253 EXPECT_THAT(0, testing::AllOf(_, _, _, _, _, _, _, _, _,
1254 testing::AllOf(_, _, _, _, _, _, _, _, _, _)));
1257 TEST(AnyOfTest, HugeMatcher) {
1258 // Verify that using AnyOf with many arguments doesn't cause
1259 // the compiler to exceed template instantiation depth limit.
1260 EXPECT_THAT(0, testing::AnyOf(_, _, _, _, _, _, _, _, _,
1261 testing::AnyOf(_, _, _, _, _, _, _, _, _, _)));
1264 namespace adl_test {
1266 // Verifies that the implementation of ::testing::AllOf and ::testing::AnyOf
1267 // don't issue unqualified recursive calls. If they do, the argument dependent
1268 // name lookup will cause AllOf/AnyOf in the 'adl_test' namespace to be found
1269 // as a candidate and the compilation will break due to an ambiguous overload.
1271 // The matcher must be in the same namespace as AllOf/AnyOf to make argument
1272 // dependent lookup find those.
1273 MATCHER(M, "") { return true; }
1275 template <typename T1, typename T2>
1276 bool AllOf(const T1& t1, const T2& t2) { return true; }
1278 TEST(AllOfTest, DoesNotCallAllOfUnqualified) {
1279 EXPECT_THAT(42, testing::AllOf(
1280 M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
1283 template <typename T1, typename T2> bool
1284 AnyOf(const T1& t1, const T2& t2) { return true; }
1286 TEST(AnyOfTest, DoesNotCallAnyOfUnqualified) {
1287 EXPECT_THAT(42, testing::AnyOf(
1288 M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
1291 } // namespace adl_test
1294 # pragma warning(pop)
1297 #if GTEST_LANG_CXX11
1299 TEST(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)))));
1305 TEST(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)))));
1311 MATCHER(IsNotNull, "") {
1312 return arg != nullptr;
1315 // Verifies that a matcher defined using MATCHER() can work on
1317 TEST(MatcherMacroTest, WorksOnMoveOnlyType) {
1318 std::unique_ptr<int> p(new int(3));
1319 EXPECT_THAT(p, IsNotNull());
1320 EXPECT_THAT(std::unique_ptr<int>(), Not(IsNotNull()));
1323 MATCHER_P(UniquePointee, pointee, "") {
1324 return *arg == pointee;
1327 // Verifies that a matcher defined using MATCHER_P*() can work on
1329 TEST(MatcherPMacroTest, WorksOnMoveOnlyType) {
1330 std::unique_ptr<int> p(new int(3));
1331 EXPECT_THAT(p, UniquePointee(3));
1332 EXPECT_THAT(p, Not(UniquePointee(2)));
1335 #endif // GTEST_LASNG_CXX11
1340 # pragma warning(pop)