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 #include "gmock/gmock-generated-matchers.h"
44 #include "gmock/gmock.h"
45 #include "gtest/gtest.h"
46 #include "gtest/gtest-spi.h"
54 using std::stringstream
;
57 using testing::make_tuple
;
61 using testing::Contains
;
62 using testing::ElementsAre
;
63 using testing::ElementsAreArray
;
69 using testing::MakeMatcher
;
70 using testing::Matcher
;
71 using testing::MatcherInterface
;
72 using testing::MatchResultListener
;
75 using testing::Pointee
;
76 using testing::PrintToString
;
78 using testing::StaticAssertTypeEq
;
81 using testing::internal::ElementsAreArrayMatcher
;
82 using testing::internal::string
;
84 // Returns the description of the given matcher.
86 string
Describe(const Matcher
<T
>& m
) {
92 // Returns the description of the negation of the given matcher.
94 string
DescribeNegation(const Matcher
<T
>& m
) {
96 m
.DescribeNegationTo(&ss
);
100 // Returns the reason why x matches, or doesn't match, m.
101 template <typename MatcherType
, typename Value
>
102 string
Explain(const MatcherType
& m
, const Value
& x
) {
104 m
.ExplainMatchResultTo(x
, &ss
);
108 // Tests Args<k0, ..., kn>(m).
110 TEST(ArgsTest
, AcceptsZeroTemplateArg
) {
111 const tuple
<int, bool> t(5, true);
112 EXPECT_THAT(t
, Args
<>(Eq(tuple
<>())));
113 EXPECT_THAT(t
, Not(Args
<>(Ne(tuple
<>()))));
116 TEST(ArgsTest
, AcceptsOneTemplateArg
) {
117 const tuple
<int, bool> t(5, true);
118 EXPECT_THAT(t
, Args
<0>(Eq(make_tuple(5))));
119 EXPECT_THAT(t
, Args
<1>(Eq(make_tuple(true))));
120 EXPECT_THAT(t
, Not(Args
<1>(Eq(make_tuple(false)))));
123 TEST(ArgsTest
, AcceptsTwoTemplateArgs
) {
124 const tuple
<short, int, long> t(4, 5, 6L); // NOLINT
126 EXPECT_THAT(t
, (Args
<0, 1>(Lt())));
127 EXPECT_THAT(t
, (Args
<1, 2>(Lt())));
128 EXPECT_THAT(t
, Not(Args
<0, 2>(Gt())));
131 TEST(ArgsTest
, AcceptsRepeatedTemplateArgs
) {
132 const tuple
<short, int, long> t(4, 5, 6L); // NOLINT
133 EXPECT_THAT(t
, (Args
<0, 0>(Eq())));
134 EXPECT_THAT(t
, Not(Args
<1, 1>(Ne())));
137 TEST(ArgsTest
, AcceptsDecreasingTemplateArgs
) {
138 const tuple
<short, int, long> t(4, 5, 6L); // NOLINT
139 EXPECT_THAT(t
, (Args
<2, 0>(Gt())));
140 EXPECT_THAT(t
, Not(Args
<2, 1>(Lt())));
143 // The MATCHER*() macros trigger warning C4100 (unreferenced formal
144 // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
145 // the macro definition, as the warnings are generated when the macro
146 // is expanded and macro expansion cannot contain #pragma. Therefore
147 // we suppress them here.
149 # pragma warning(push)
150 # pragma warning(disable:4100)
153 MATCHER(SumIsZero
, "") {
154 return get
<0>(arg
) + get
<1>(arg
) + get
<2>(arg
) == 0;
157 TEST(ArgsTest
, AcceptsMoreTemplateArgsThanArityOfOriginalTuple
) {
158 EXPECT_THAT(make_tuple(-1, 2), (Args
<0, 0, 1>(SumIsZero())));
159 EXPECT_THAT(make_tuple(1, 2), Not(Args
<0, 0, 1>(SumIsZero())));
162 TEST(ArgsTest
, CanBeNested
) {
163 const tuple
<short, int, long, int> t(4, 5, 6L, 6); // NOLINT
164 EXPECT_THAT(t
, (Args
<1, 2, 3>(Args
<1, 2>(Eq()))));
165 EXPECT_THAT(t
, (Args
<0, 1, 3>(Args
<0, 2>(Lt()))));
168 TEST(ArgsTest
, CanMatchTupleByValue
) {
169 typedef tuple
<char, int, int> Tuple3
;
170 const Matcher
<Tuple3
> m
= Args
<1, 2>(Lt());
171 EXPECT_TRUE(m
.Matches(Tuple3('a', 1, 2)));
172 EXPECT_FALSE(m
.Matches(Tuple3('b', 2, 2)));
175 TEST(ArgsTest
, CanMatchTupleByReference
) {
176 typedef tuple
<char, char, int> Tuple3
;
177 const Matcher
<const Tuple3
&> m
= Args
<0, 1>(Lt());
178 EXPECT_TRUE(m
.Matches(Tuple3('a', 'b', 2)));
179 EXPECT_FALSE(m
.Matches(Tuple3('b', 'b', 2)));
182 // Validates that arg is printed as str.
183 MATCHER_P(PrintsAs
, str
, "") {
184 return testing::PrintToString(arg
) == str
;
187 TEST(ArgsTest
, AcceptsTenTemplateArgs
) {
188 EXPECT_THAT(make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
189 (Args
<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
190 PrintsAs("(9, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
191 EXPECT_THAT(make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
192 Not(Args
<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
193 PrintsAs("(0, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
196 TEST(ArgsTest
, DescirbesSelfCorrectly
) {
197 const Matcher
<tuple
<int, bool, char> > m
= Args
<2, 0>(Lt());
198 EXPECT_EQ("are a tuple whose fields (#2, #0) are a pair where "
199 "the first < the second",
203 TEST(ArgsTest
, DescirbesNestedArgsCorrectly
) {
204 const Matcher
<const tuple
<int, bool, char, int>&> m
=
205 Args
<0, 2, 3>(Args
<2, 0>(Lt()));
206 EXPECT_EQ("are a tuple whose fields (#0, #2, #3) are a tuple "
207 "whose fields (#2, #0) are a pair where the first < the second",
211 TEST(ArgsTest
, DescribesNegationCorrectly
) {
212 const Matcher
<tuple
<int, char> > m
= Args
<1, 0>(Gt());
213 EXPECT_EQ("are a tuple whose fields (#1, #0) aren't a pair "
214 "where the first > the second",
215 DescribeNegation(m
));
218 TEST(ArgsTest
, ExplainsMatchResultWithoutInnerExplanation
) {
219 const Matcher
<tuple
<bool, int, int> > m
= Args
<1, 2>(Eq());
220 EXPECT_EQ("whose fields (#1, #2) are (42, 42)",
221 Explain(m
, make_tuple(false, 42, 42)));
222 EXPECT_EQ("whose fields (#1, #2) are (42, 43)",
223 Explain(m
, make_tuple(false, 42, 43)));
226 // For testing Args<>'s explanation.
227 class LessThanMatcher
: public MatcherInterface
<tuple
<char, int> > {
229 virtual void DescribeTo(::std::ostream
* os
) const {}
231 virtual bool MatchAndExplain(tuple
<char, int> value
,
232 MatchResultListener
* listener
) const {
233 const int diff
= get
<0>(value
) - get
<1>(value
);
235 *listener
<< "where the first value is " << diff
236 << " more than the second";
242 Matcher
<tuple
<char, int> > LessThan() {
243 return MakeMatcher(new LessThanMatcher
);
246 TEST(ArgsTest
, ExplainsMatchResultWithInnerExplanation
) {
247 const Matcher
<tuple
<char, int, int> > m
= Args
<0, 2>(LessThan());
248 EXPECT_EQ("whose fields (#0, #2) are ('a' (97, 0x61), 42), "
249 "where the first value is 55 more than the second",
250 Explain(m
, make_tuple('a', 42, 42)));
251 EXPECT_EQ("whose fields (#0, #2) are ('\\0', 43)",
252 Explain(m
, make_tuple('\0', 42, 43)));
255 // For testing ExplainMatchResultTo().
256 class GreaterThanMatcher
: public MatcherInterface
<int> {
258 explicit GreaterThanMatcher(int rhs
) : rhs_(rhs
) {}
260 virtual void DescribeTo(::std::ostream
* os
) const {
261 *os
<< "is greater than " << rhs_
;
264 virtual bool MatchAndExplain(int lhs
,
265 MatchResultListener
* listener
) const {
266 const int diff
= lhs
- rhs_
;
268 *listener
<< "which is " << diff
<< " more than " << rhs_
;
269 } else if (diff
== 0) {
270 *listener
<< "which is the same as " << rhs_
;
272 *listener
<< "which is " << -diff
<< " less than " << rhs_
;
282 Matcher
<int> GreaterThan(int n
) {
283 return MakeMatcher(new GreaterThanMatcher(n
));
286 // Tests for ElementsAre().
288 TEST(ElementsAreTest
, CanDescribeExpectingNoElement
) {
289 Matcher
<const vector
<int>&> m
= ElementsAre();
290 EXPECT_EQ("is empty", Describe(m
));
293 TEST(ElementsAreTest
, CanDescribeExpectingOneElement
) {
294 Matcher
<vector
<int> > m
= ElementsAre(Gt(5));
295 EXPECT_EQ("has 1 element that is > 5", Describe(m
));
298 TEST(ElementsAreTest
, CanDescribeExpectingManyElements
) {
299 Matcher
<list
<string
> > m
= ElementsAre(StrEq("one"), "two");
300 EXPECT_EQ("has 2 elements where\n"
301 "element #0 is equal to \"one\",\n"
302 "element #1 is equal to \"two\"", Describe(m
));
305 TEST(ElementsAreTest
, CanDescribeNegationOfExpectingNoElement
) {
306 Matcher
<vector
<int> > m
= ElementsAre();
307 EXPECT_EQ("isn't empty", DescribeNegation(m
));
310 TEST(ElementsAreTest
, CanDescribeNegationOfExpectingOneElment
) {
311 Matcher
<const list
<int>& > m
= ElementsAre(Gt(5));
312 EXPECT_EQ("doesn't have 1 element, or\n"
313 "element #0 isn't > 5", DescribeNegation(m
));
316 TEST(ElementsAreTest
, CanDescribeNegationOfExpectingManyElements
) {
317 Matcher
<const list
<string
>& > m
= ElementsAre("one", "two");
318 EXPECT_EQ("doesn't have 2 elements, or\n"
319 "element #0 isn't equal to \"one\", or\n"
320 "element #1 isn't equal to \"two\"", DescribeNegation(m
));
323 TEST(ElementsAreTest
, DoesNotExplainTrivialMatch
) {
324 Matcher
<const list
<int>& > m
= ElementsAre(1, Ne(2));
327 test_list
.push_back(1);
328 test_list
.push_back(3);
329 EXPECT_EQ("", Explain(m
, test_list
)); // No need to explain anything.
332 TEST(ElementsAreTest
, ExplainsNonTrivialMatch
) {
333 Matcher
<const vector
<int>& > m
=
334 ElementsAre(GreaterThan(1), 0, GreaterThan(2));
336 const int a
[] = { 10, 0, 100 };
337 vector
<int> test_vector(a
, a
+ GTEST_ARRAY_SIZE_(a
));
338 EXPECT_EQ("whose element #0 matches, which is 9 more than 1,\n"
339 "and whose element #2 matches, which is 98 more than 2",
340 Explain(m
, test_vector
));
343 TEST(ElementsAreTest
, CanExplainMismatchWrongSize
) {
344 Matcher
<const list
<int>& > m
= ElementsAre(1, 3);
347 // No need to explain when the container is empty.
348 EXPECT_EQ("", Explain(m
, test_list
));
350 test_list
.push_back(1);
351 EXPECT_EQ("which has 1 element", Explain(m
, test_list
));
354 TEST(ElementsAreTest
, CanExplainMismatchRightSize
) {
355 Matcher
<const vector
<int>& > m
= ElementsAre(1, GreaterThan(5));
360 EXPECT_EQ("whose element #0 doesn't match", Explain(m
, v
));
363 EXPECT_EQ("whose element #1 doesn't match, which is 4 less than 5",
367 TEST(ElementsAreTest
, MatchesOneElementVector
) {
368 vector
<string
> test_vector
;
369 test_vector
.push_back("test string");
371 EXPECT_THAT(test_vector
, ElementsAre(StrEq("test string")));
374 TEST(ElementsAreTest
, MatchesOneElementList
) {
375 list
<string
> test_list
;
376 test_list
.push_back("test string");
378 EXPECT_THAT(test_list
, ElementsAre("test string"));
381 TEST(ElementsAreTest
, MatchesThreeElementVector
) {
382 vector
<string
> test_vector
;
383 test_vector
.push_back("one");
384 test_vector
.push_back("two");
385 test_vector
.push_back("three");
387 EXPECT_THAT(test_vector
, ElementsAre("one", StrEq("two"), _
));
390 TEST(ElementsAreTest
, MatchesOneElementEqMatcher
) {
391 vector
<int> test_vector
;
392 test_vector
.push_back(4);
394 EXPECT_THAT(test_vector
, ElementsAre(Eq(4)));
397 TEST(ElementsAreTest
, MatchesOneElementAnyMatcher
) {
398 vector
<int> test_vector
;
399 test_vector
.push_back(4);
401 EXPECT_THAT(test_vector
, ElementsAre(_
));
404 TEST(ElementsAreTest
, MatchesOneElementValue
) {
405 vector
<int> test_vector
;
406 test_vector
.push_back(4);
408 EXPECT_THAT(test_vector
, ElementsAre(4));
411 TEST(ElementsAreTest
, MatchesThreeElementsMixedMatchers
) {
412 vector
<int> test_vector
;
413 test_vector
.push_back(1);
414 test_vector
.push_back(2);
415 test_vector
.push_back(3);
417 EXPECT_THAT(test_vector
, ElementsAre(1, Eq(2), _
));
420 TEST(ElementsAreTest
, MatchesTenElementVector
) {
421 const int a
[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
422 vector
<int> test_vector(a
, a
+ GTEST_ARRAY_SIZE_(a
));
424 EXPECT_THAT(test_vector
,
425 // The element list can contain values and/or matchers
426 // of different types.
427 ElementsAre(0, Ge(0), _
, 3, 4, Ne(2), Eq(6), 7, 8, _
));
430 TEST(ElementsAreTest
, DoesNotMatchWrongSize
) {
431 vector
<string
> test_vector
;
432 test_vector
.push_back("test string");
433 test_vector
.push_back("test string");
435 Matcher
<vector
<string
> > m
= ElementsAre(StrEq("test string"));
436 EXPECT_FALSE(m
.Matches(test_vector
));
439 TEST(ElementsAreTest
, DoesNotMatchWrongValue
) {
440 vector
<string
> test_vector
;
441 test_vector
.push_back("other string");
443 Matcher
<vector
<string
> > m
= ElementsAre(StrEq("test string"));
444 EXPECT_FALSE(m
.Matches(test_vector
));
447 TEST(ElementsAreTest
, DoesNotMatchWrongOrder
) {
448 vector
<string
> test_vector
;
449 test_vector
.push_back("one");
450 test_vector
.push_back("three");
451 test_vector
.push_back("two");
453 Matcher
<vector
<string
> > m
= ElementsAre(
454 StrEq("one"), StrEq("two"), StrEq("three"));
455 EXPECT_FALSE(m
.Matches(test_vector
));
458 TEST(ElementsAreTest
, WorksForNestedContainer
) {
459 const char* strings
[] = {
464 vector
<list
<char> > nested
;
465 for (size_t i
= 0; i
< GTEST_ARRAY_SIZE_(strings
); i
++) {
466 nested
.push_back(list
<char>(strings
[i
], strings
[i
] + strlen(strings
[i
])));
469 EXPECT_THAT(nested
, ElementsAre(ElementsAre('H', Ne('e')),
470 ElementsAre('w', 'o', _
, _
, 'd')));
471 EXPECT_THAT(nested
, Not(ElementsAre(ElementsAre('H', 'e'),
472 ElementsAre('w', 'o', _
, _
, 'd'))));
475 TEST(ElementsAreTest
, WorksWithByRefElementMatchers
) {
476 int a
[] = { 0, 1, 2 };
477 vector
<int> v(a
, a
+ GTEST_ARRAY_SIZE_(a
));
479 EXPECT_THAT(v
, ElementsAre(Ref(v
[0]), Ref(v
[1]), Ref(v
[2])));
480 EXPECT_THAT(v
, Not(ElementsAre(Ref(v
[0]), Ref(v
[1]), Ref(a
[2]))));
483 TEST(ElementsAreTest
, WorksWithContainerPointerUsingPointee
) {
484 int a
[] = { 0, 1, 2 };
485 vector
<int> v(a
, a
+ GTEST_ARRAY_SIZE_(a
));
487 EXPECT_THAT(&v
, Pointee(ElementsAre(0, 1, _
)));
488 EXPECT_THAT(&v
, Not(Pointee(ElementsAre(0, _
, 3))));
491 TEST(ElementsAreTest
, WorksWithNativeArrayPassedByReference
) {
492 int array
[] = { 0, 1, 2 };
493 EXPECT_THAT(array
, ElementsAre(0, 1, _
));
494 EXPECT_THAT(array
, Not(ElementsAre(1, _
, _
)));
495 EXPECT_THAT(array
, Not(ElementsAre(0, _
)));
498 class NativeArrayPassedAsPointerAndSize
{
500 NativeArrayPassedAsPointerAndSize() {}
502 MOCK_METHOD2(Helper
, void(int* array
, int size
));
505 GTEST_DISALLOW_COPY_AND_ASSIGN_(NativeArrayPassedAsPointerAndSize
);
508 TEST(ElementsAreTest
, WorksWithNativeArrayPassedAsPointerAndSize
) {
509 int array
[] = { 0, 1 };
510 ::testing::tuple
<int*, size_t> array_as_tuple(array
, 2);
511 EXPECT_THAT(array_as_tuple
, ElementsAre(0, 1));
512 EXPECT_THAT(array_as_tuple
, Not(ElementsAre(0)));
514 NativeArrayPassedAsPointerAndSize helper
;
515 EXPECT_CALL(helper
, Helper(_
, _
))
516 .With(ElementsAre(0, 1));
517 helper
.Helper(array
, 2);
520 TEST(ElementsAreTest
, WorksWithTwoDimensionalNativeArray
) {
521 const char a2
[][3] = { "hi", "lo" };
522 EXPECT_THAT(a2
, ElementsAre(ElementsAre('h', 'i', '\0'),
523 ElementsAre('l', 'o', '\0')));
524 EXPECT_THAT(a2
, ElementsAre(StrEq("hi"), StrEq("lo")));
525 EXPECT_THAT(a2
, ElementsAre(Not(ElementsAre('h', 'o', '\0')),
526 ElementsAre('l', 'o', '\0')));
529 TEST(ElementsAreTest
, AcceptsStringLiteral
) {
530 string array
[] = { "hi", "one", "two" };
531 EXPECT_THAT(array
, ElementsAre("hi", "one", "two"));
532 EXPECT_THAT(array
, Not(ElementsAre("hi", "one", "too")));
537 // The following test passes a value of type const char[] to a
538 // function template that expects const T&. Some versions of MSVC
539 // generates a compiler error C2665 for that. We believe it's a bug
540 // in MSVC. Therefore this test is #if-ed out for MSVC.
542 // Declared here with the size unknown. Defined AFTER the following test.
543 extern const char kHi
[];
545 TEST(ElementsAreTest
, AcceptsArrayWithUnknownSize
) {
546 // The size of kHi is not known in this test, but ElementsAre() should
549 string array1
[] = { "hi" };
550 EXPECT_THAT(array1
, ElementsAre(kHi
));
552 string array2
[] = { "ho" };
553 EXPECT_THAT(array2
, Not(ElementsAre(kHi
)));
556 const char kHi
[] = "hi";
560 TEST(ElementsAreTest
, MakesCopyOfArguments
) {
563 // This should make a copy of x and y.
564 ::testing::internal::ElementsAreMatcher
<testing::tuple
<int, int> >
565 polymorphic_matcher
= ElementsAre(x
, y
);
566 // Changing x and y now shouldn't affect the meaning of the above matcher.
568 const int array1
[] = { 1, 2 };
569 EXPECT_THAT(array1
, polymorphic_matcher
);
570 const int array2
[] = { 0, 0 };
571 EXPECT_THAT(array2
, Not(polymorphic_matcher
));
575 // Tests for ElementsAreArray(). Since ElementsAreArray() shares most
576 // of the implementation with ElementsAre(), we don't test it as
579 TEST(ElementsAreArrayTest
, CanBeCreatedWithValueArray
) {
580 const int a
[] = { 1, 2, 3 };
582 vector
<int> test_vector(a
, a
+ GTEST_ARRAY_SIZE_(a
));
583 EXPECT_THAT(test_vector
, ElementsAreArray(a
));
586 EXPECT_THAT(test_vector
, Not(ElementsAreArray(a
)));
589 TEST(ElementsAreArrayTest
, CanBeCreatedWithArraySize
) {
590 const char* a
[] = { "one", "two", "three" };
592 vector
<string
> test_vector(a
, a
+ GTEST_ARRAY_SIZE_(a
));
593 EXPECT_THAT(test_vector
, ElementsAreArray(a
, GTEST_ARRAY_SIZE_(a
)));
596 test_vector
[0] = "1";
597 EXPECT_THAT(test_vector
, Not(ElementsAreArray(p
, GTEST_ARRAY_SIZE_(a
))));
600 TEST(ElementsAreArrayTest
, CanBeCreatedWithoutArraySize
) {
601 const char* a
[] = { "one", "two", "three" };
603 vector
<string
> test_vector(a
, a
+ GTEST_ARRAY_SIZE_(a
));
604 EXPECT_THAT(test_vector
, ElementsAreArray(a
));
606 test_vector
[0] = "1";
607 EXPECT_THAT(test_vector
, Not(ElementsAreArray(a
)));
610 TEST(ElementsAreArrayTest
, CanBeCreatedWithMatcherArray
) {
611 const Matcher
<string
> kMatcherArray
[] =
612 { StrEq("one"), StrEq("two"), StrEq("three") };
614 vector
<string
> test_vector
;
615 test_vector
.push_back("one");
616 test_vector
.push_back("two");
617 test_vector
.push_back("three");
618 EXPECT_THAT(test_vector
, ElementsAreArray(kMatcherArray
));
620 test_vector
.push_back("three");
621 EXPECT_THAT(test_vector
, Not(ElementsAreArray(kMatcherArray
)));
624 TEST(ElementsAreArrayTest
, CanBeCreatedWithVector
) {
625 const int a
[] = { 1, 2, 3 };
626 vector
<int> test_vector(a
, a
+ GTEST_ARRAY_SIZE_(a
));
627 const vector
<int> expected(a
, a
+ GTEST_ARRAY_SIZE_(a
));
628 EXPECT_THAT(test_vector
, ElementsAreArray(expected
));
629 test_vector
.push_back(4);
630 EXPECT_THAT(test_vector
, Not(ElementsAreArray(expected
)));
633 #if GTEST_HAS_STD_INITIALIZER_LIST_
635 TEST(ElementsAreArrayTest
, TakesInitializerList
) {
636 const int a
[5] = { 1, 2, 3, 4, 5 };
637 EXPECT_THAT(a
, ElementsAreArray({ 1, 2, 3, 4, 5 }));
638 EXPECT_THAT(a
, Not(ElementsAreArray({ 1, 2, 3, 5, 4 })));
639 EXPECT_THAT(a
, Not(ElementsAreArray({ 1, 2, 3, 4, 6 })));
642 TEST(ElementsAreArrayTest
, TakesInitializerListOfCStrings
) {
643 const string a
[5] = { "a", "b", "c", "d", "e" };
644 EXPECT_THAT(a
, ElementsAreArray({ "a", "b", "c", "d", "e" }));
645 EXPECT_THAT(a
, Not(ElementsAreArray({ "a", "b", "c", "e", "d" })));
646 EXPECT_THAT(a
, Not(ElementsAreArray({ "a", "b", "c", "d", "ef" })));
649 TEST(ElementsAreArrayTest
, TakesInitializerListOfSameTypedMatchers
) {
650 const int a
[5] = { 1, 2, 3, 4, 5 };
651 EXPECT_THAT(a
, ElementsAreArray(
652 { Eq(1), Eq(2), Eq(3), Eq(4), Eq(5) }));
653 EXPECT_THAT(a
, Not(ElementsAreArray(
654 { Eq(1), Eq(2), Eq(3), Eq(4), Eq(6) })));
657 TEST(ElementsAreArrayTest
,
658 TakesInitializerListOfDifferentTypedMatchers
) {
659 const int a
[5] = { 1, 2, 3, 4, 5 };
660 // The compiler cannot infer the type of the initializer list if its
661 // elements have different types. We must explicitly specify the
662 // unified element type in this case.
663 EXPECT_THAT(a
, ElementsAreArray
<Matcher
<int> >(
664 { Eq(1), Ne(-2), Ge(3), Le(4), Eq(5) }));
665 EXPECT_THAT(a
, Not(ElementsAreArray
<Matcher
<int> >(
666 { Eq(1), Ne(-2), Ge(3), Le(4), Eq(6) })));
669 #endif // GTEST_HAS_STD_INITIALIZER_LIST_
671 TEST(ElementsAreArrayTest
, CanBeCreatedWithMatcherVector
) {
672 const int a
[] = { 1, 2, 3 };
673 const Matcher
<int> kMatchers
[] = { Eq(1), Eq(2), Eq(3) };
674 vector
<int> test_vector(a
, a
+ GTEST_ARRAY_SIZE_(a
));
675 const vector
<Matcher
<int> > expected(
676 kMatchers
, kMatchers
+ GTEST_ARRAY_SIZE_(kMatchers
));
677 EXPECT_THAT(test_vector
, ElementsAreArray(expected
));
678 test_vector
.push_back(4);
679 EXPECT_THAT(test_vector
, Not(ElementsAreArray(expected
)));
682 TEST(ElementsAreArrayTest
, CanBeCreatedWithIteratorRange
) {
683 const int a
[] = { 1, 2, 3 };
684 const vector
<int> test_vector(a
, a
+ GTEST_ARRAY_SIZE_(a
));
685 const vector
<int> expected(a
, a
+ GTEST_ARRAY_SIZE_(a
));
686 EXPECT_THAT(test_vector
, ElementsAreArray(expected
.begin(), expected
.end()));
687 // Pointers are iterators, too.
688 EXPECT_THAT(test_vector
, ElementsAreArray(a
, a
+ GTEST_ARRAY_SIZE_(a
)));
689 // The empty range of NULL pointers should also be okay.
690 int* const null_int
= NULL
;
691 EXPECT_THAT(test_vector
, Not(ElementsAreArray(null_int
, null_int
)));
692 EXPECT_THAT((vector
<int>()), ElementsAreArray(null_int
, null_int
));
695 // Since ElementsAre() and ElementsAreArray() share much of the
696 // implementation, we only do a sanity test for native arrays here.
697 TEST(ElementsAreArrayTest
, WorksWithNativeArray
) {
698 ::std::string a
[] = { "hi", "ho" };
699 ::std::string b
[] = { "hi", "ho" };
701 EXPECT_THAT(a
, ElementsAreArray(b
));
702 EXPECT_THAT(a
, ElementsAreArray(b
, 2));
703 EXPECT_THAT(a
, Not(ElementsAreArray(b
, 1)));
706 TEST(ElementsAreArrayTest
, SourceLifeSpan
) {
707 const int a
[] = { 1, 2, 3 };
708 vector
<int> test_vector(a
, a
+ GTEST_ARRAY_SIZE_(a
));
709 vector
<int> expect(a
, a
+ GTEST_ARRAY_SIZE_(a
));
710 ElementsAreArrayMatcher
<int> matcher_maker
=
711 ElementsAreArray(expect
.begin(), expect
.end());
712 EXPECT_THAT(test_vector
, matcher_maker
);
713 // Changing in place the values that initialized matcher_maker should not
714 // affect matcher_maker anymore. It should have made its own copy of them.
715 typedef vector
<int>::iterator Iter
;
716 for (Iter it
= expect
.begin(); it
!= expect
.end(); ++it
) { *it
+= 10; }
717 EXPECT_THAT(test_vector
, matcher_maker
);
718 test_vector
.push_back(3);
719 EXPECT_THAT(test_vector
, Not(matcher_maker
));
722 // Tests for the MATCHER*() macro family.
724 // Tests that a simple MATCHER() definition works.
726 MATCHER(IsEven
, "") { return (arg
% 2) == 0; }
728 TEST(MatcherMacroTest
, Works
) {
729 const Matcher
<int> m
= IsEven();
730 EXPECT_TRUE(m
.Matches(6));
731 EXPECT_FALSE(m
.Matches(7));
733 EXPECT_EQ("is even", Describe(m
));
734 EXPECT_EQ("not (is even)", DescribeNegation(m
));
735 EXPECT_EQ("", Explain(m
, 6));
736 EXPECT_EQ("", Explain(m
, 7));
739 // This also tests that the description string can reference 'negation'.
740 MATCHER(IsEven2
, negation
? "is odd" : "is even") {
741 if ((arg
% 2) == 0) {
742 // Verifies that we can stream to result_listener, a listener
743 // supplied by the MATCHER macro implicitly.
744 *result_listener
<< "OK";
747 *result_listener
<< "% 2 == " << (arg
% 2);
752 // This also tests that the description string can reference matcher
754 MATCHER_P2(EqSumOf
, x
, y
,
755 string(negation
? "doesn't equal" : "equals") + " the sum of " +
756 PrintToString(x
) + " and " + PrintToString(y
)) {
757 if (arg
== (x
+ y
)) {
758 *result_listener
<< "OK";
761 // Verifies that we can stream to the underlying stream of
763 if (result_listener
->stream() != NULL
) {
764 *result_listener
->stream() << "diff == " << (x
+ y
- arg
);
770 // Tests that the matcher description can reference 'negation' and the
771 // matcher parameters.
772 TEST(MatcherMacroTest
, DescriptionCanReferenceNegationAndParameters
) {
773 const Matcher
<int> m1
= IsEven2();
774 EXPECT_EQ("is even", Describe(m1
));
775 EXPECT_EQ("is odd", DescribeNegation(m1
));
777 const Matcher
<int> m2
= EqSumOf(5, 9);
778 EXPECT_EQ("equals the sum of 5 and 9", Describe(m2
));
779 EXPECT_EQ("doesn't equal the sum of 5 and 9", DescribeNegation(m2
));
782 // Tests explaining match result in a MATCHER* macro.
783 TEST(MatcherMacroTest
, CanExplainMatchResult
) {
784 const Matcher
<int> m1
= IsEven2();
785 EXPECT_EQ("OK", Explain(m1
, 4));
786 EXPECT_EQ("% 2 == 1", Explain(m1
, 5));
788 const Matcher
<int> m2
= EqSumOf(1, 2);
789 EXPECT_EQ("OK", Explain(m2
, 3));
790 EXPECT_EQ("diff == -1", Explain(m2
, 4));
793 // Tests that the body of MATCHER() can reference the type of the
794 // value being matched.
796 MATCHER(IsEmptyString
, "") {
797 StaticAssertTypeEq
< ::std::string
, arg_type
>();
801 MATCHER(IsEmptyStringByRef
, "") {
802 StaticAssertTypeEq
<const ::std::string
&, arg_type
>();
806 TEST(MatcherMacroTest
, CanReferenceArgType
) {
807 const Matcher
< ::std::string
> m1
= IsEmptyString();
808 EXPECT_TRUE(m1
.Matches(""));
810 const Matcher
<const ::std::string
&> m2
= IsEmptyStringByRef();
811 EXPECT_TRUE(m2
.Matches(""));
814 // Tests that MATCHER() can be used in a namespace.
816 namespace matcher_test
{
817 MATCHER(IsOdd
, "") { return (arg
% 2) != 0; }
818 } // namespace matcher_test
820 TEST(MatcherMacroTest
, WorksInNamespace
) {
821 Matcher
<int> m
= matcher_test::IsOdd();
822 EXPECT_FALSE(m
.Matches(4));
823 EXPECT_TRUE(m
.Matches(5));
826 // Tests that Value() can be used to compose matchers.
827 MATCHER(IsPositiveOdd
, "") {
828 return Value(arg
, matcher_test::IsOdd()) && arg
> 0;
831 TEST(MatcherMacroTest
, CanBeComposedUsingValue
) {
832 EXPECT_THAT(3, IsPositiveOdd());
833 EXPECT_THAT(4, Not(IsPositiveOdd()));
834 EXPECT_THAT(-1, Not(IsPositiveOdd()));
837 // Tests that a simple MATCHER_P() definition works.
839 MATCHER_P(IsGreaterThan32And
, n
, "") { return arg
> 32 && arg
> n
; }
841 TEST(MatcherPMacroTest
, Works
) {
842 const Matcher
<int> m
= IsGreaterThan32And(5);
843 EXPECT_TRUE(m
.Matches(36));
844 EXPECT_FALSE(m
.Matches(5));
846 EXPECT_EQ("is greater than 32 and 5", Describe(m
));
847 EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m
));
848 EXPECT_EQ("", Explain(m
, 36));
849 EXPECT_EQ("", Explain(m
, 5));
852 // Tests that the description is calculated correctly from the matcher name.
853 MATCHER_P(_is_Greater_Than32and_
, n
, "") { return arg
> 32 && arg
> n
; }
855 TEST(MatcherPMacroTest
, GeneratesCorrectDescription
) {
856 const Matcher
<int> m
= _is_Greater_Than32and_(5);
858 EXPECT_EQ("is greater than 32 and 5", Describe(m
));
859 EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m
));
860 EXPECT_EQ("", Explain(m
, 36));
861 EXPECT_EQ("", Explain(m
, 5));
864 // Tests that a MATCHER_P matcher can be explicitly instantiated with
865 // a reference parameter type.
867 class UncopyableFoo
{
869 explicit UncopyableFoo(char value
) : value_(value
) {}
871 UncopyableFoo(const UncopyableFoo
&);
872 void operator=(const UncopyableFoo
&);
877 MATCHER_P(ReferencesUncopyable
, variable
, "") { return &arg
== &variable
; }
879 TEST(MatcherPMacroTest
, WorksWhenExplicitlyInstantiatedWithReference
) {
880 UncopyableFoo
foo1('1'), foo2('2');
881 const Matcher
<const UncopyableFoo
&> m
=
882 ReferencesUncopyable
<const UncopyableFoo
&>(foo1
);
884 EXPECT_TRUE(m
.Matches(foo1
));
885 EXPECT_FALSE(m
.Matches(foo2
));
887 // We don't want the address of the parameter printed, as most
888 // likely it will just annoy the user. If the address is
889 // interesting, the user should consider passing the parameter by
891 EXPECT_EQ("references uncopyable 1-byte object <31>", Describe(m
));
895 // Tests that the body of MATCHER_Pn() can reference the parameter
898 MATCHER_P3(ParamTypesAreIntLongAndChar
, foo
, bar
, baz
, "") {
899 StaticAssertTypeEq
<int, foo_type
>();
900 StaticAssertTypeEq
<long, bar_type
>(); // NOLINT
901 StaticAssertTypeEq
<char, baz_type
>();
905 TEST(MatcherPnMacroTest
, CanReferenceParamTypes
) {
906 EXPECT_THAT(0, ParamTypesAreIntLongAndChar(10, 20L, 'a'));
909 // Tests that a MATCHER_Pn matcher can be explicitly instantiated with
910 // reference parameter types.
912 MATCHER_P2(ReferencesAnyOf
, variable1
, variable2
, "") {
913 return &arg
== &variable1
|| &arg
== &variable2
;
916 TEST(MatcherPnMacroTest
, WorksWhenExplicitlyInstantiatedWithReferences
) {
917 UncopyableFoo
foo1('1'), foo2('2'), foo3('3');
918 const Matcher
<const UncopyableFoo
&> m
=
919 ReferencesAnyOf
<const UncopyableFoo
&, const UncopyableFoo
&>(foo1
, foo2
);
921 EXPECT_TRUE(m
.Matches(foo1
));
922 EXPECT_TRUE(m
.Matches(foo2
));
923 EXPECT_FALSE(m
.Matches(foo3
));
926 TEST(MatcherPnMacroTest
,
927 GeneratesCorretDescriptionWhenExplicitlyInstantiatedWithReferences
) {
928 UncopyableFoo
foo1('1'), foo2('2');
929 const Matcher
<const UncopyableFoo
&> m
=
930 ReferencesAnyOf
<const UncopyableFoo
&, const UncopyableFoo
&>(foo1
, foo2
);
932 // We don't want the addresses of the parameters printed, as most
933 // likely they will just annoy the user. If the addresses are
934 // interesting, the user should consider passing the parameters by
936 EXPECT_EQ("references any of (1-byte object <31>, 1-byte object <32>)",
940 // Tests that a simple MATCHER_P2() definition works.
942 MATCHER_P2(IsNotInClosedRange
, low
, hi
, "") { return arg
< low
|| arg
> hi
; }
944 TEST(MatcherPnMacroTest
, Works
) {
945 const Matcher
<const long&> m
= IsNotInClosedRange(10, 20); // NOLINT
946 EXPECT_TRUE(m
.Matches(36L));
947 EXPECT_FALSE(m
.Matches(15L));
949 EXPECT_EQ("is not in closed range (10, 20)", Describe(m
));
950 EXPECT_EQ("not (is not in closed range (10, 20))", DescribeNegation(m
));
951 EXPECT_EQ("", Explain(m
, 36L));
952 EXPECT_EQ("", Explain(m
, 15L));
955 // Tests that MATCHER*() definitions can be overloaded on the number
956 // of parameters; also tests MATCHER_Pn() where n >= 3.
958 MATCHER(EqualsSumOf
, "") { return arg
== 0; }
959 MATCHER_P(EqualsSumOf
, a
, "") { return arg
== a
; }
960 MATCHER_P2(EqualsSumOf
, a
, b
, "") { return arg
== a
+ b
; }
961 MATCHER_P3(EqualsSumOf
, a
, b
, c
, "") { return arg
== a
+ b
+ c
; }
962 MATCHER_P4(EqualsSumOf
, a
, b
, c
, d
, "") { return arg
== a
+ b
+ c
+ d
; }
963 MATCHER_P5(EqualsSumOf
, a
, b
, c
, d
, e
, "") { return arg
== a
+ b
+ c
+ d
+ e
; }
964 MATCHER_P6(EqualsSumOf
, a
, b
, c
, d
, e
, f
, "") {
965 return arg
== a
+ b
+ c
+ d
+ e
+ f
;
967 MATCHER_P7(EqualsSumOf
, a
, b
, c
, d
, e
, f
, g
, "") {
968 return arg
== a
+ b
+ c
+ d
+ e
+ f
+ g
;
970 MATCHER_P8(EqualsSumOf
, a
, b
, c
, d
, e
, f
, g
, h
, "") {
971 return arg
== a
+ b
+ c
+ d
+ e
+ f
+ g
+ h
;
973 MATCHER_P9(EqualsSumOf
, a
, b
, c
, d
, e
, f
, g
, h
, i
, "") {
974 return arg
== a
+ b
+ c
+ d
+ e
+ f
+ g
+ h
+ i
;
976 MATCHER_P10(EqualsSumOf
, a
, b
, c
, d
, e
, f
, g
, h
, i
, j
, "") {
977 return arg
== a
+ b
+ c
+ d
+ e
+ f
+ g
+ h
+ i
+ j
;
980 TEST(MatcherPnMacroTest
, CanBeOverloadedOnNumberOfParameters
) {
981 EXPECT_THAT(0, EqualsSumOf());
982 EXPECT_THAT(1, EqualsSumOf(1));
983 EXPECT_THAT(12, EqualsSumOf(10, 2));
984 EXPECT_THAT(123, EqualsSumOf(100, 20, 3));
985 EXPECT_THAT(1234, EqualsSumOf(1000, 200, 30, 4));
986 EXPECT_THAT(12345, EqualsSumOf(10000, 2000, 300, 40, 5));
987 EXPECT_THAT("abcdef",
988 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f'));
989 EXPECT_THAT("abcdefg",
990 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g'));
991 EXPECT_THAT("abcdefgh",
992 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
994 EXPECT_THAT("abcdefghi",
995 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
997 EXPECT_THAT("abcdefghij",
998 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
999 "h", 'i', ::std::string("j")));
1001 EXPECT_THAT(1, Not(EqualsSumOf()));
1002 EXPECT_THAT(-1, Not(EqualsSumOf(1)));
1003 EXPECT_THAT(-12, Not(EqualsSumOf(10, 2)));
1004 EXPECT_THAT(-123, Not(EqualsSumOf(100, 20, 3)));
1005 EXPECT_THAT(-1234, Not(EqualsSumOf(1000, 200, 30, 4)));
1006 EXPECT_THAT(-12345, Not(EqualsSumOf(10000, 2000, 300, 40, 5)));
1007 EXPECT_THAT("abcdef ",
1008 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f')));
1009 EXPECT_THAT("abcdefg ",
1010 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f',
1012 EXPECT_THAT("abcdefgh ",
1013 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
1015 EXPECT_THAT("abcdefghi ",
1016 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
1018 EXPECT_THAT("abcdefghij ",
1019 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
1020 "h", 'i', ::std::string("j"))));
1023 // Tests that a MATCHER_Pn() definition can be instantiated with any
1024 // compatible parameter types.
1025 TEST(MatcherPnMacroTest
, WorksForDifferentParameterTypes
) {
1026 EXPECT_THAT(123, EqualsSumOf(100L, 20, static_cast<char>(3)));
1027 EXPECT_THAT("abcd", EqualsSumOf(::std::string("a"), "b", 'c', "d"));
1029 EXPECT_THAT(124, Not(EqualsSumOf(100L, 20, static_cast<char>(3))));
1030 EXPECT_THAT("abcde", Not(EqualsSumOf(::std::string("a"), "b", 'c', "d")));
1033 // Tests that the matcher body can promote the parameter types.
1035 MATCHER_P2(EqConcat
, prefix
, suffix
, "") {
1036 // The following lines promote the two parameters to desired types.
1037 std::string
prefix_str(prefix
);
1038 char suffix_char
= static_cast<char>(suffix
);
1039 return arg
== prefix_str
+ suffix_char
;
1042 TEST(MatcherPnMacroTest
, SimpleTypePromotion
) {
1043 Matcher
<std::string
> no_promo
=
1044 EqConcat(std::string("foo"), 't');
1045 Matcher
<const std::string
&> promo
=
1046 EqConcat("foo", static_cast<int>('t'));
1047 EXPECT_FALSE(no_promo
.Matches("fool"));
1048 EXPECT_FALSE(promo
.Matches("fool"));
1049 EXPECT_TRUE(no_promo
.Matches("foot"));
1050 EXPECT_TRUE(promo
.Matches("foot"));
1053 // Verifies the type of a MATCHER*.
1055 TEST(MatcherPnMacroTest
, TypesAreCorrect
) {
1056 // EqualsSumOf() must be assignable to a EqualsSumOfMatcher variable.
1057 EqualsSumOfMatcher a0
= EqualsSumOf();
1059 // EqualsSumOf(1) must be assignable to a EqualsSumOfMatcherP variable.
1060 EqualsSumOfMatcherP
<int> a1
= EqualsSumOf(1);
1062 // EqualsSumOf(p1, ..., pk) must be assignable to a EqualsSumOfMatcherPk
1063 // variable, and so on.
1064 EqualsSumOfMatcherP2
<int, char> a2
= EqualsSumOf(1, '2');
1065 EqualsSumOfMatcherP3
<int, int, char> a3
= EqualsSumOf(1, 2, '3');
1066 EqualsSumOfMatcherP4
<int, int, int, char> a4
= EqualsSumOf(1, 2, 3, '4');
1067 EqualsSumOfMatcherP5
<int, int, int, int, char> a5
=
1068 EqualsSumOf(1, 2, 3, 4, '5');
1069 EqualsSumOfMatcherP6
<int, int, int, int, int, char> a6
=
1070 EqualsSumOf(1, 2, 3, 4, 5, '6');
1071 EqualsSumOfMatcherP7
<int, int, int, int, int, int, char> a7
=
1072 EqualsSumOf(1, 2, 3, 4, 5, 6, '7');
1073 EqualsSumOfMatcherP8
<int, int, int, int, int, int, int, char> a8
=
1074 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, '8');
1075 EqualsSumOfMatcherP9
<int, int, int, int, int, int, int, int, char> a9
=
1076 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, '9');
1077 EqualsSumOfMatcherP10
<int, int, int, int, int, int, int, int, int, char> a10
=
1078 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, 9, '0');
1080 // Avoid "unused variable" warnings.
1094 // Tests that matcher-typed parameters can be used in Value() inside a
1095 // MATCHER_Pn definition.
1097 // Succeeds if arg matches exactly 2 of the 3 matchers.
1098 MATCHER_P3(TwoOf
, m1
, m2
, m3
, "") {
1099 const int count
= static_cast<int>(Value(arg
, m1
))
1100 + static_cast<int>(Value(arg
, m2
)) + static_cast<int>(Value(arg
, m3
));
1104 TEST(MatcherPnMacroTest
, CanUseMatcherTypedParameterInValue
) {
1105 EXPECT_THAT(42, TwoOf(Gt(0), Lt(50), Eq(10)));
1106 EXPECT_THAT(0, Not(TwoOf(Gt(-1), Lt(1), Eq(0))));
1109 // Tests Contains().
1111 TEST(ContainsTest
, ListMatchesWhenElementIsInContainer
) {
1112 list
<int> some_list
;
1113 some_list
.push_back(3);
1114 some_list
.push_back(1);
1115 some_list
.push_back(2);
1116 EXPECT_THAT(some_list
, Contains(1));
1117 EXPECT_THAT(some_list
, Contains(Gt(2.5)));
1118 EXPECT_THAT(some_list
, Contains(Eq(2.0f
)));
1120 list
<string
> another_list
;
1121 another_list
.push_back("fee");
1122 another_list
.push_back("fie");
1123 another_list
.push_back("foe");
1124 another_list
.push_back("fum");
1125 EXPECT_THAT(another_list
, Contains(string("fee")));
1128 TEST(ContainsTest
, ListDoesNotMatchWhenElementIsNotInContainer
) {
1129 list
<int> some_list
;
1130 some_list
.push_back(3);
1131 some_list
.push_back(1);
1132 EXPECT_THAT(some_list
, Not(Contains(4)));
1135 TEST(ContainsTest
, SetMatchesWhenElementIsInContainer
) {
1140 EXPECT_THAT(some_set
, Contains(Eq(1.0)));
1141 EXPECT_THAT(some_set
, Contains(Eq(3.0f
)));
1142 EXPECT_THAT(some_set
, Contains(2));
1144 set
<const char*> another_set
;
1145 another_set
.insert("fee");
1146 another_set
.insert("fie");
1147 another_set
.insert("foe");
1148 another_set
.insert("fum");
1149 EXPECT_THAT(another_set
, Contains(Eq(string("fum"))));
1152 TEST(ContainsTest
, SetDoesNotMatchWhenElementIsNotInContainer
) {
1156 EXPECT_THAT(some_set
, Not(Contains(4)));
1158 set
<const char*> c_string_set
;
1159 c_string_set
.insert("hello");
1160 EXPECT_THAT(c_string_set
, Not(Contains(string("hello").c_str())));
1163 TEST(ContainsTest
, ExplainsMatchResultCorrectly
) {
1164 const int a
[2] = { 1, 2 };
1165 Matcher
<const int (&)[2]> m
= Contains(2);
1166 EXPECT_EQ("whose element #1 matches", Explain(m
, a
));
1169 EXPECT_EQ("", Explain(m
, a
));
1171 m
= Contains(GreaterThan(0));
1172 EXPECT_EQ("whose element #0 matches, which is 1 more than 0", Explain(m
, a
));
1174 m
= Contains(GreaterThan(10));
1175 EXPECT_EQ("", Explain(m
, a
));
1178 TEST(ContainsTest
, DescribesItselfCorrectly
) {
1179 Matcher
<vector
<int> > m
= Contains(1);
1180 EXPECT_EQ("contains at least one element that is equal to 1", Describe(m
));
1182 Matcher
<vector
<int> > m2
= Not(m
);
1183 EXPECT_EQ("doesn't contain any element that is equal to 1", Describe(m2
));
1186 TEST(ContainsTest
, MapMatchesWhenElementIsInContainer
) {
1187 map
<const char*, int> my_map
;
1188 const char* bar
= "a string";
1190 EXPECT_THAT(my_map
, Contains(pair
<const char* const, int>(bar
, 2)));
1192 map
<string
, int> another_map
;
1193 another_map
["fee"] = 1;
1194 another_map
["fie"] = 2;
1195 another_map
["foe"] = 3;
1196 another_map
["fum"] = 4;
1197 EXPECT_THAT(another_map
, Contains(pair
<const string
, int>(string("fee"), 1)));
1198 EXPECT_THAT(another_map
, Contains(pair
<const string
, int>("fie", 2)));
1201 TEST(ContainsTest
, MapDoesNotMatchWhenElementIsNotInContainer
) {
1202 map
<int, int> some_map
;
1205 EXPECT_THAT(some_map
, Not(Contains(pair
<const int, int>(2, 23))));
1208 TEST(ContainsTest
, ArrayMatchesWhenElementIsInContainer
) {
1209 const char* string_array
[] = { "fee", "fie", "foe", "fum" };
1210 EXPECT_THAT(string_array
, Contains(Eq(string("fum"))));
1213 TEST(ContainsTest
, ArrayDoesNotMatchWhenElementIsNotInContainer
) {
1214 int int_array
[] = { 1, 2, 3, 4 };
1215 EXPECT_THAT(int_array
, Not(Contains(5)));
1218 TEST(ContainsTest
, AcceptsMatcher
) {
1219 const int a
[] = { 1, 2, 3 };
1220 EXPECT_THAT(a
, Contains(Gt(2)));
1221 EXPECT_THAT(a
, Not(Contains(Gt(4))));
1224 TEST(ContainsTest
, WorksForNativeArrayAsTuple
) {
1225 const int a
[] = { 1, 2 };
1226 const int* const pointer
= a
;
1227 EXPECT_THAT(make_tuple(pointer
, 2), Contains(1));
1228 EXPECT_THAT(make_tuple(pointer
, 2), Not(Contains(Gt(3))));
1231 TEST(ContainsTest
, WorksForTwoDimensionalNativeArray
) {
1232 int a
[][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
1233 EXPECT_THAT(a
, Contains(ElementsAre(4, 5, 6)));
1234 EXPECT_THAT(a
, Contains(Contains(5)));
1235 EXPECT_THAT(a
, Not(Contains(ElementsAre(3, 4, 5))));
1236 EXPECT_THAT(a
, Contains(Not(Contains(5))));
1239 TEST(AllOfTest
, HugeMatcher
) {
1240 // Verify that using AllOf with many arguments doesn't cause
1241 // the compiler to exceed template instantiation depth limit.
1242 EXPECT_THAT(0, testing::AllOf(_
, _
, _
, _
, _
, _
, _
, _
, _
,
1243 testing::AllOf(_
, _
, _
, _
, _
, _
, _
, _
, _
, _
)));
1246 TEST(AnyOfTest
, HugeMatcher
) {
1247 // Verify that using AnyOf with many arguments doesn't cause
1248 // the compiler to exceed template instantiation depth limit.
1249 EXPECT_THAT(0, testing::AnyOf(_
, _
, _
, _
, _
, _
, _
, _
, _
,
1250 testing::AnyOf(_
, _
, _
, _
, _
, _
, _
, _
, _
, _
)));
1253 namespace adl_test
{
1255 // Verifies that the implementation of ::testing::AllOf and ::testing::AnyOf
1256 // don't issue unqualified recursive calls. If they do, the argument dependent
1257 // name lookup will cause AllOf/AnyOf in the 'adl_test' namespace to be found
1258 // as a candidate and the compilation will break due to an ambiguous overload.
1260 // The matcher must be in the same namespace as AllOf/AnyOf to make argument
1261 // dependent lookup find those.
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
) {
1268 EXPECT_THAT(42, testing::AllOf(
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
) {
1276 EXPECT_THAT(42, testing::AnyOf(
1277 M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
1280 } // namespace adl_test
1283 # pragma warning(pop)