blob: 0a750de1dcc514e3a69351f2b003854677ff3a1d [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +00001// Copyright 2008, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
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
13// distribution.
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.
17//
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.
29
30// Google Mock - a framework for writing C++ mock classes.
31//
32// This file tests the built-in matchers generated by a script.
33
zhanyong.wan53e08c42010-09-14 05:38:21 +000034#include "gmock/gmock-generated-matchers.h"
shiqiane35fdd92008-12-10 05:08:54 +000035
36#include <list>
zhanyong.wan1bee7b22009-02-20 18:31:04 +000037#include <map>
38#include <set>
shiqiane35fdd92008-12-10 05:08:54 +000039#include <sstream>
40#include <string>
zhanyong.wan1bee7b22009-02-20 18:31:04 +000041#include <utility>
shiqiane35fdd92008-12-10 05:08:54 +000042#include <vector>
43
zhanyong.wan53e08c42010-09-14 05:38:21 +000044#include "gmock/gmock.h"
45#include "gtest/gtest.h"
46#include "gtest/gtest-spi.h"
shiqiane35fdd92008-12-10 05:08:54 +000047
48namespace {
49
50using std::list;
zhanyong.wan1bee7b22009-02-20 18:31:04 +000051using std::map;
52using std::pair;
53using std::set;
shiqiane35fdd92008-12-10 05:08:54 +000054using std::stringstream;
55using std::vector;
zhanyong.wan2661c682009-06-09 05:42:12 +000056using std::tr1::get;
zhanyong.wanb8243162009-06-04 05:48:20 +000057using std::tr1::make_tuple;
zhanyong.wan2661c682009-06-09 05:42:12 +000058using std::tr1::tuple;
shiqiane35fdd92008-12-10 05:08:54 +000059using testing::_;
zhanyong.wan2661c682009-06-09 05:42:12 +000060using testing::Args;
zhanyong.wan1bee7b22009-02-20 18:31:04 +000061using testing::Contains;
shiqiane35fdd92008-12-10 05:08:54 +000062using testing::ElementsAre;
63using testing::ElementsAreArray;
64using testing::Eq;
65using testing::Ge;
66using testing::Gt;
zhanyong.wanb8243162009-06-04 05:48:20 +000067using testing::Lt;
shiqiane35fdd92008-12-10 05:08:54 +000068using testing::MakeMatcher;
69using testing::Matcher;
70using testing::MatcherInterface;
zhanyong.wandb22c222010-01-28 21:52:29 +000071using testing::MatchResultListener;
shiqiane35fdd92008-12-10 05:08:54 +000072using testing::Ne;
73using testing::Not;
74using testing::Pointee;
zhanyong.wanb4140802010-06-08 22:53:57 +000075using testing::PrintToString;
shiqiane35fdd92008-12-10 05:08:54 +000076using testing::Ref;
zhanyong.wance198ff2009-02-12 01:34:27 +000077using testing::StaticAssertTypeEq;
shiqiane35fdd92008-12-10 05:08:54 +000078using testing::StrEq;
zhanyong.wanb8243162009-06-04 05:48:20 +000079using testing::Value;
jgm38513a82012-11-15 15:50:36 +000080using testing::internal::ElementsAreArrayMatcher;
shiqiane35fdd92008-12-10 05:08:54 +000081using testing::internal::string;
82
83// Returns the description of the given matcher.
84template <typename T>
85string Describe(const Matcher<T>& m) {
86 stringstream ss;
87 m.DescribeTo(&ss);
88 return ss.str();
89}
90
91// Returns the description of the negation of the given matcher.
92template <typename T>
93string DescribeNegation(const Matcher<T>& m) {
94 stringstream ss;
95 m.DescribeNegationTo(&ss);
96 return ss.str();
97}
98
99// Returns the reason why x matches, or doesn't match, m.
100template <typename MatcherType, typename Value>
101string Explain(const MatcherType& m, const Value& x) {
102 stringstream ss;
103 m.ExplainMatchResultTo(x, &ss);
104 return ss.str();
105}
106
zhanyong.wan2661c682009-06-09 05:42:12 +0000107// Tests Args<k0, ..., kn>(m).
108
109TEST(ArgsTest, AcceptsZeroTemplateArg) {
110 const tuple<int, bool> t(5, true);
111 EXPECT_THAT(t, Args<>(Eq(tuple<>())));
112 EXPECT_THAT(t, Not(Args<>(Ne(tuple<>()))));
113}
114
115TEST(ArgsTest, AcceptsOneTemplateArg) {
116 const tuple<int, bool> t(5, true);
117 EXPECT_THAT(t, Args<0>(Eq(make_tuple(5))));
118 EXPECT_THAT(t, Args<1>(Eq(make_tuple(true))));
119 EXPECT_THAT(t, Not(Args<1>(Eq(make_tuple(false)))));
120}
121
122TEST(ArgsTest, AcceptsTwoTemplateArgs) {
123 const tuple<short, int, long> t(4, 5, 6L); // NOLINT
124
125 EXPECT_THAT(t, (Args<0, 1>(Lt())));
126 EXPECT_THAT(t, (Args<1, 2>(Lt())));
127 EXPECT_THAT(t, Not(Args<0, 2>(Gt())));
128}
129
130TEST(ArgsTest, AcceptsRepeatedTemplateArgs) {
131 const tuple<short, int, long> t(4, 5, 6L); // NOLINT
132 EXPECT_THAT(t, (Args<0, 0>(Eq())));
133 EXPECT_THAT(t, Not(Args<1, 1>(Ne())));
134}
135
136TEST(ArgsTest, AcceptsDecreasingTemplateArgs) {
137 const tuple<short, int, long> t(4, 5, 6L); // NOLINT
138 EXPECT_THAT(t, (Args<2, 0>(Gt())));
139 EXPECT_THAT(t, Not(Args<2, 1>(Lt())));
140}
141
zhanyong.wan82113312010-01-08 21:55:40 +0000142// The MATCHER*() macros trigger warning C4100 (unreferenced formal
143// parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
144// the macro definition, as the warnings are generated when the macro
145// is expanded and macro expansion cannot contain #pragma. Therefore
146// we suppress them here.
147#ifdef _MSC_VER
zhanyong.wan658ac0b2011-02-24 07:29:13 +0000148# pragma warning(push)
149# pragma warning(disable:4100)
zhanyong.wan82113312010-01-08 21:55:40 +0000150#endif
151
zhanyong.wan2661c682009-06-09 05:42:12 +0000152MATCHER(SumIsZero, "") {
153 return get<0>(arg) + get<1>(arg) + get<2>(arg) == 0;
154}
155
156TEST(ArgsTest, AcceptsMoreTemplateArgsThanArityOfOriginalTuple) {
157 EXPECT_THAT(make_tuple(-1, 2), (Args<0, 0, 1>(SumIsZero())));
158 EXPECT_THAT(make_tuple(1, 2), Not(Args<0, 0, 1>(SumIsZero())));
159}
160
161TEST(ArgsTest, CanBeNested) {
162 const tuple<short, int, long, int> t(4, 5, 6L, 6); // NOLINT
163 EXPECT_THAT(t, (Args<1, 2, 3>(Args<1, 2>(Eq()))));
164 EXPECT_THAT(t, (Args<0, 1, 3>(Args<0, 2>(Lt()))));
165}
166
167TEST(ArgsTest, CanMatchTupleByValue) {
168 typedef tuple<char, int, int> Tuple3;
169 const Matcher<Tuple3> m = Args<1, 2>(Lt());
170 EXPECT_TRUE(m.Matches(Tuple3('a', 1, 2)));
171 EXPECT_FALSE(m.Matches(Tuple3('b', 2, 2)));
172}
173
174TEST(ArgsTest, CanMatchTupleByReference) {
175 typedef tuple<char, char, int> Tuple3;
176 const Matcher<const Tuple3&> m = Args<0, 1>(Lt());
177 EXPECT_TRUE(m.Matches(Tuple3('a', 'b', 2)));
178 EXPECT_FALSE(m.Matches(Tuple3('b', 'b', 2)));
179}
180
181// Validates that arg is printed as str.
182MATCHER_P(PrintsAs, str, "") {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000183 return testing::PrintToString(arg) == str;
zhanyong.wan2661c682009-06-09 05:42:12 +0000184}
185
186TEST(ArgsTest, AcceptsTenTemplateArgs) {
187 EXPECT_THAT(make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
188 (Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
189 PrintsAs("(9, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
190 EXPECT_THAT(make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
191 Not(Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
192 PrintsAs("(0, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
193}
194
195TEST(ArgsTest, DescirbesSelfCorrectly) {
196 const Matcher<tuple<int, bool, char> > m = Args<2, 0>(Lt());
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000197 EXPECT_EQ("are a tuple whose fields (#2, #0) are a pair where "
198 "the first < the second",
zhanyong.wan2661c682009-06-09 05:42:12 +0000199 Describe(m));
200}
201
202TEST(ArgsTest, DescirbesNestedArgsCorrectly) {
203 const Matcher<const tuple<int, bool, char, int>&> m =
204 Args<0, 2, 3>(Args<2, 0>(Lt()));
205 EXPECT_EQ("are a tuple whose fields (#0, #2, #3) are a tuple "
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000206 "whose fields (#2, #0) are a pair where the first < the second",
zhanyong.wan2661c682009-06-09 05:42:12 +0000207 Describe(m));
208}
209
210TEST(ArgsTest, DescribesNegationCorrectly) {
211 const Matcher<tuple<int, char> > m = Args<1, 0>(Gt());
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000212 EXPECT_EQ("are a tuple whose fields (#1, #0) aren't a pair "
213 "where the first > the second",
zhanyong.wan2661c682009-06-09 05:42:12 +0000214 DescribeNegation(m));
215}
216
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000217TEST(ArgsTest, ExplainsMatchResultWithoutInnerExplanation) {
218 const Matcher<tuple<bool, int, int> > m = Args<1, 2>(Eq());
219 EXPECT_EQ("whose fields (#1, #2) are (42, 42)",
220 Explain(m, make_tuple(false, 42, 42)));
221 EXPECT_EQ("whose fields (#1, #2) are (42, 43)",
222 Explain(m, make_tuple(false, 42, 43)));
223}
224
225// For testing Args<>'s explanation.
226class LessThanMatcher : public MatcherInterface<tuple<char, int> > {
227 public:
228 virtual void DescribeTo(::std::ostream* os) const {}
229
230 virtual bool MatchAndExplain(tuple<char, int> value,
231 MatchResultListener* listener) const {
232 const int diff = get<0>(value) - get<1>(value);
233 if (diff > 0) {
234 *listener << "where the first value is " << diff
235 << " more than the second";
236 }
237 return diff < 0;
238 }
239};
240
241Matcher<tuple<char, int> > LessThan() {
242 return MakeMatcher(new LessThanMatcher);
243}
244
245TEST(ArgsTest, ExplainsMatchResultWithInnerExplanation) {
246 const Matcher<tuple<char, int, int> > m = Args<0, 2>(LessThan());
zhanyong.wand60c5f42010-07-21 22:21:07 +0000247 EXPECT_EQ("whose fields (#0, #2) are ('a' (97, 0x61), 42), "
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000248 "where the first value is 55 more than the second",
249 Explain(m, make_tuple('a', 42, 42)));
250 EXPECT_EQ("whose fields (#0, #2) are ('\\0', 43)",
251 Explain(m, make_tuple('\0', 42, 43)));
252}
253
shiqiane35fdd92008-12-10 05:08:54 +0000254// For testing ExplainMatchResultTo().
255class GreaterThanMatcher : public MatcherInterface<int> {
256 public:
257 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
258
shiqiane35fdd92008-12-10 05:08:54 +0000259 virtual void DescribeTo(::std::ostream* os) const {
260 *os << "is greater than " << rhs_;
261 }
262
zhanyong.wandb22c222010-01-28 21:52:29 +0000263 virtual bool MatchAndExplain(int lhs,
264 MatchResultListener* listener) const {
shiqiane35fdd92008-12-10 05:08:54 +0000265 const int diff = lhs - rhs_;
266 if (diff > 0) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000267 *listener << "which is " << diff << " more than " << rhs_;
shiqiane35fdd92008-12-10 05:08:54 +0000268 } else if (diff == 0) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000269 *listener << "which is the same as " << rhs_;
shiqiane35fdd92008-12-10 05:08:54 +0000270 } else {
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000271 *listener << "which is " << -diff << " less than " << rhs_;
shiqiane35fdd92008-12-10 05:08:54 +0000272 }
zhanyong.wandb22c222010-01-28 21:52:29 +0000273
274 return lhs > rhs_;
shiqiane35fdd92008-12-10 05:08:54 +0000275 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000276
shiqiane35fdd92008-12-10 05:08:54 +0000277 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000278 int rhs_;
shiqiane35fdd92008-12-10 05:08:54 +0000279};
280
281Matcher<int> GreaterThan(int n) {
282 return MakeMatcher(new GreaterThanMatcher(n));
283}
284
285// Tests for ElementsAre().
286
287// Evaluates to the number of elements in 'array'.
288#define GMOCK_ARRAY_SIZE_(array) (sizeof(array)/sizeof(array[0]))
289
290TEST(ElementsAreTest, CanDescribeExpectingNoElement) {
291 Matcher<const vector<int>&> m = ElementsAre();
292 EXPECT_EQ("is empty", Describe(m));
293}
294
295TEST(ElementsAreTest, CanDescribeExpectingOneElement) {
296 Matcher<vector<int> > m = ElementsAre(Gt(5));
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000297 EXPECT_EQ("has 1 element that is > 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000298}
299
300TEST(ElementsAreTest, CanDescribeExpectingManyElements) {
301 Matcher<list<string> > m = ElementsAre(StrEq("one"), "two");
302 EXPECT_EQ("has 2 elements where\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000303 "element #0 is equal to \"one\",\n"
304 "element #1 is equal to \"two\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000305}
306
307TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) {
308 Matcher<vector<int> > m = ElementsAre();
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000309 EXPECT_EQ("isn't empty", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +0000310}
311
312TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElment) {
313 Matcher<const list<int>& > m = ElementsAre(Gt(5));
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000314 EXPECT_EQ("doesn't have 1 element, or\n"
315 "element #0 isn't > 5", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +0000316}
317
318TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) {
319 Matcher<const list<string>& > m = ElementsAre("one", "two");
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000320 EXPECT_EQ("doesn't have 2 elements, or\n"
321 "element #0 isn't equal to \"one\", or\n"
322 "element #1 isn't equal to \"two\"", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +0000323}
324
325TEST(ElementsAreTest, DoesNotExplainTrivialMatch) {
326 Matcher<const list<int>& > m = ElementsAre(1, Ne(2));
327
328 list<int> test_list;
329 test_list.push_back(1);
330 test_list.push_back(3);
331 EXPECT_EQ("", Explain(m, test_list)); // No need to explain anything.
332}
333
334TEST(ElementsAreTest, ExplainsNonTrivialMatch) {
335 Matcher<const vector<int>& > m =
336 ElementsAre(GreaterThan(1), 0, GreaterThan(2));
337
338 const int a[] = { 10, 0, 100 };
339 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000340 EXPECT_EQ("whose element #0 matches, which is 9 more than 1,\n"
341 "and whose element #2 matches, which is 98 more than 2",
342 Explain(m, test_vector));
shiqiane35fdd92008-12-10 05:08:54 +0000343}
344
345TEST(ElementsAreTest, CanExplainMismatchWrongSize) {
346 Matcher<const list<int>& > m = ElementsAre(1, 3);
347
348 list<int> test_list;
349 // No need to explain when the container is empty.
350 EXPECT_EQ("", Explain(m, test_list));
351
352 test_list.push_back(1);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000353 EXPECT_EQ("which has 1 element", Explain(m, test_list));
shiqiane35fdd92008-12-10 05:08:54 +0000354}
355
356TEST(ElementsAreTest, CanExplainMismatchRightSize) {
357 Matcher<const vector<int>& > m = ElementsAre(1, GreaterThan(5));
358
359 vector<int> v;
360 v.push_back(2);
361 v.push_back(1);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000362 EXPECT_EQ("whose element #0 doesn't match", Explain(m, v));
shiqiane35fdd92008-12-10 05:08:54 +0000363
364 v[0] = 1;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000365 EXPECT_EQ("whose element #1 doesn't match, which is 4 less than 5",
366 Explain(m, v));
shiqiane35fdd92008-12-10 05:08:54 +0000367}
368
369TEST(ElementsAreTest, MatchesOneElementVector) {
370 vector<string> test_vector;
371 test_vector.push_back("test string");
372
373 EXPECT_THAT(test_vector, ElementsAre(StrEq("test string")));
374}
375
376TEST(ElementsAreTest, MatchesOneElementList) {
377 list<string> test_list;
378 test_list.push_back("test string");
379
380 EXPECT_THAT(test_list, ElementsAre("test string"));
381}
382
383TEST(ElementsAreTest, MatchesThreeElementVector) {
384 vector<string> test_vector;
385 test_vector.push_back("one");
386 test_vector.push_back("two");
387 test_vector.push_back("three");
388
389 EXPECT_THAT(test_vector, ElementsAre("one", StrEq("two"), _));
390}
391
392TEST(ElementsAreTest, MatchesOneElementEqMatcher) {
393 vector<int> test_vector;
394 test_vector.push_back(4);
395
396 EXPECT_THAT(test_vector, ElementsAre(Eq(4)));
397}
398
399TEST(ElementsAreTest, MatchesOneElementAnyMatcher) {
400 vector<int> test_vector;
401 test_vector.push_back(4);
402
403 EXPECT_THAT(test_vector, ElementsAre(_));
404}
405
406TEST(ElementsAreTest, MatchesOneElementValue) {
407 vector<int> test_vector;
408 test_vector.push_back(4);
409
410 EXPECT_THAT(test_vector, ElementsAre(4));
411}
412
413TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) {
414 vector<int> test_vector;
415 test_vector.push_back(1);
416 test_vector.push_back(2);
417 test_vector.push_back(3);
418
419 EXPECT_THAT(test_vector, ElementsAre(1, Eq(2), _));
420}
421
422TEST(ElementsAreTest, MatchesTenElementVector) {
423 const int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
424 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
425
426 EXPECT_THAT(test_vector,
427 // The element list can contain values and/or matchers
428 // of different types.
429 ElementsAre(0, Ge(0), _, 3, 4, Ne(2), Eq(6), 7, 8, _));
430}
431
432TEST(ElementsAreTest, DoesNotMatchWrongSize) {
433 vector<string> test_vector;
434 test_vector.push_back("test string");
435 test_vector.push_back("test string");
436
437 Matcher<vector<string> > m = ElementsAre(StrEq("test string"));
438 EXPECT_FALSE(m.Matches(test_vector));
439}
440
441TEST(ElementsAreTest, DoesNotMatchWrongValue) {
442 vector<string> test_vector;
443 test_vector.push_back("other string");
444
445 Matcher<vector<string> > m = ElementsAre(StrEq("test string"));
446 EXPECT_FALSE(m.Matches(test_vector));
447}
448
449TEST(ElementsAreTest, DoesNotMatchWrongOrder) {
450 vector<string> test_vector;
451 test_vector.push_back("one");
452 test_vector.push_back("three");
453 test_vector.push_back("two");
454
455 Matcher<vector<string> > m = ElementsAre(
456 StrEq("one"), StrEq("two"), StrEq("three"));
457 EXPECT_FALSE(m.Matches(test_vector));
458}
459
460TEST(ElementsAreTest, WorksForNestedContainer) {
461 const char* strings[] = {
462 "Hi",
463 "world"
464 };
465
466 vector<list<char> > nested;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000467 for (size_t i = 0; i < GMOCK_ARRAY_SIZE_(strings); i++) {
shiqiane35fdd92008-12-10 05:08:54 +0000468 nested.push_back(list<char>(strings[i], strings[i] + strlen(strings[i])));
469 }
470
471 EXPECT_THAT(nested, ElementsAre(ElementsAre('H', Ne('e')),
472 ElementsAre('w', 'o', _, _, 'd')));
473 EXPECT_THAT(nested, Not(ElementsAre(ElementsAre('H', 'e'),
474 ElementsAre('w', 'o', _, _, 'd'))));
475}
476
477TEST(ElementsAreTest, WorksWithByRefElementMatchers) {
478 int a[] = { 0, 1, 2 };
479 vector<int> v(a, a + GMOCK_ARRAY_SIZE_(a));
480
481 EXPECT_THAT(v, ElementsAre(Ref(v[0]), Ref(v[1]), Ref(v[2])));
482 EXPECT_THAT(v, Not(ElementsAre(Ref(v[0]), Ref(v[1]), Ref(a[2]))));
483}
484
485TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) {
486 int a[] = { 0, 1, 2 };
487 vector<int> v(a, a + GMOCK_ARRAY_SIZE_(a));
488
489 EXPECT_THAT(&v, Pointee(ElementsAre(0, 1, _)));
490 EXPECT_THAT(&v, Not(Pointee(ElementsAre(0, _, 3))));
491}
492
zhanyong.wanb8243162009-06-04 05:48:20 +0000493TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) {
494 int array[] = { 0, 1, 2 };
495 EXPECT_THAT(array, ElementsAre(0, 1, _));
496 EXPECT_THAT(array, Not(ElementsAre(1, _, _)));
497 EXPECT_THAT(array, Not(ElementsAre(0, _)));
498}
499
500class NativeArrayPassedAsPointerAndSize {
501 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000502 NativeArrayPassedAsPointerAndSize() {}
503
zhanyong.wanb8243162009-06-04 05:48:20 +0000504 MOCK_METHOD2(Helper, void(int* array, int size));
zhanyong.wan32de5f52009-12-23 00:13:23 +0000505
506 private:
507 GTEST_DISALLOW_COPY_AND_ASSIGN_(NativeArrayPassedAsPointerAndSize);
zhanyong.wanb8243162009-06-04 05:48:20 +0000508};
509
510TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) {
511 int array[] = { 0, 1 };
512 ::std::tr1::tuple<int*, size_t> array_as_tuple(array, 2);
513 EXPECT_THAT(array_as_tuple, ElementsAre(0, 1));
514 EXPECT_THAT(array_as_tuple, Not(ElementsAre(0)));
515
516 NativeArrayPassedAsPointerAndSize helper;
517 EXPECT_CALL(helper, Helper(_, _))
zhanyong.wanbf550852009-06-09 06:09:53 +0000518 .With(ElementsAre(0, 1));
zhanyong.wanb8243162009-06-04 05:48:20 +0000519 helper.Helper(array, 2);
520}
521
522TEST(ElementsAreTest, WorksWithTwoDimensionalNativeArray) {
523 const char a2[][3] = { "hi", "lo" };
524 EXPECT_THAT(a2, ElementsAre(ElementsAre('h', 'i', '\0'),
525 ElementsAre('l', 'o', '\0')));
526 EXPECT_THAT(a2, ElementsAre(StrEq("hi"), StrEq("lo")));
527 EXPECT_THAT(a2, ElementsAre(Not(ElementsAre('h', 'o', '\0')),
528 ElementsAre('l', 'o', '\0')));
529}
530
jgm38513a82012-11-15 15:50:36 +0000531TEST(ElementsAreTest, AcceptsStringLiteral) {
532 string array[] = { "hi", "one", "two" };
533 EXPECT_THAT(array, ElementsAre("hi", "one", "two"));
534 EXPECT_THAT(array, Not(ElementsAre("hi", "one", "too")));
535}
536
537#ifndef _MSC_VER
538
539// The following test passes a value of type const char[] to a
540// function template that expects const T&. Some versions of MSVC
541// generates a compiler error C2665 for that. We believe it's a bug
542// in MSVC. Therefore this test is #if-ed out for MSVC.
543
544// Declared here with the size unknown. Defined AFTER the following test.
545extern const char kHi[];
546
547TEST(ElementsAreTest, AcceptsArrayWithUnknownSize) {
548 // The size of kHi is not known in this test, but ElementsAre() should
549 // still accept it.
550
551 string array1[] = { "hi" };
552 EXPECT_THAT(array1, ElementsAre(kHi));
553
554 string array2[] = { "ho" };
555 EXPECT_THAT(array2, Not(ElementsAre(kHi)));
556}
557
558const char kHi[] = "hi";
559
560#endif // _MSC_VER
561
562TEST(ElementsAreTest, MakesCopyOfArguments) {
563 int x = 1;
564 int y = 2;
565 // This should make a copy of x and y.
566 ::testing::internal::ElementsAreMatcher2<int, int> polymorphic_matcher =
567 ElementsAre(x, y);
568 // Changing x and y now shouldn't affect the meaning of the above matcher.
569 x = y = 0;
570 const int array1[] = { 1, 2 };
571 EXPECT_THAT(array1, polymorphic_matcher);
572 const int array2[] = { 0, 0 };
573 EXPECT_THAT(array2, Not(polymorphic_matcher));
574}
575
shiqiane35fdd92008-12-10 05:08:54 +0000576// Tests for ElementsAreArray(). Since ElementsAreArray() shares most
577// of the implementation with ElementsAre(), we don't test it as
578// thoroughly here.
579
580TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) {
581 const int a[] = { 1, 2, 3 };
582
583 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
584 EXPECT_THAT(test_vector, ElementsAreArray(a));
585
586 test_vector[2] = 0;
587 EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
588}
589
590TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) {
591 const char* a[] = { "one", "two", "three" };
592
593 vector<string> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
594 EXPECT_THAT(test_vector, ElementsAreArray(a, GMOCK_ARRAY_SIZE_(a)));
595
596 const char** p = a;
597 test_vector[0] = "1";
598 EXPECT_THAT(test_vector, Not(ElementsAreArray(p, GMOCK_ARRAY_SIZE_(a))));
599}
600
601TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) {
602 const char* a[] = { "one", "two", "three" };
603
604 vector<string> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
605 EXPECT_THAT(test_vector, ElementsAreArray(a));
606
607 test_vector[0] = "1";
608 EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
609}
610
611TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) {
612 const Matcher<string> kMatcherArray[] =
613 { StrEq("one"), StrEq("two"), StrEq("three") };
614
615 vector<string> test_vector;
616 test_vector.push_back("one");
617 test_vector.push_back("two");
618 test_vector.push_back("three");
619 EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray));
620
621 test_vector.push_back("three");
622 EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray)));
623}
624
jgm38513a82012-11-15 15:50:36 +0000625TEST(ElementsAreArrayTest, CanBeCreatedWithVector) {
626 const int a[] = { 1, 2, 3 };
627 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
628 const vector<int> expected(a, a + GMOCK_ARRAY_SIZE_(a));
629 EXPECT_THAT(test_vector, ElementsAreArray(expected));
630 test_vector.push_back(4);
631 EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));
632}
633
634TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherVector) {
635 const int a[] = { 1, 2, 3 };
636 const Matcher<int> kMatchers[] = { Eq(1), Eq(2), Eq(3) };
637 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
638 const vector<Matcher<int> > expected(
639 kMatchers, kMatchers + GMOCK_ARRAY_SIZE_(kMatchers));
640 EXPECT_THAT(test_vector, ElementsAreArray(expected));
641 test_vector.push_back(4);
642 EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));
643}
644
645TEST(ElementsAreArrayTest, CanBeCreatedWithIteratorRange) {
646 const int a[] = { 1, 2, 3 };
647 const vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
648 const vector<int> expected(a, a + GMOCK_ARRAY_SIZE_(a));
649 EXPECT_THAT(test_vector, ElementsAreArray(expected.begin(), expected.end()));
650 // Pointers are iterators, too.
651 EXPECT_THAT(test_vector, ElementsAreArray(a, a + GMOCK_ARRAY_SIZE_(a)));
652 // The empty range of NULL pointers should also be okay.
653 int* const null_int = NULL;
654 EXPECT_THAT(test_vector, Not(ElementsAreArray(null_int, null_int)));
655 EXPECT_THAT((vector<int>()), ElementsAreArray(null_int, null_int));
656}
657
zhanyong.wanb8243162009-06-04 05:48:20 +0000658// Since ElementsAre() and ElementsAreArray() share much of the
659// implementation, we only do a sanity test for native arrays here.
660TEST(ElementsAreArrayTest, WorksWithNativeArray) {
661 ::std::string a[] = { "hi", "ho" };
662 ::std::string b[] = { "hi", "ho" };
663
664 EXPECT_THAT(a, ElementsAreArray(b));
665 EXPECT_THAT(a, ElementsAreArray(b, 2));
666 EXPECT_THAT(a, Not(ElementsAreArray(b, 1)));
667}
668
jgm38513a82012-11-15 15:50:36 +0000669TEST(ElementsAreArrayTest, SourceLifeSpan) {
670 const int a[] = { 1, 2, 3 };
671 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
672 vector<int> expect(a, a + GMOCK_ARRAY_SIZE_(a));
673 ElementsAreArrayMatcher<int> matcher_maker =
674 ElementsAreArray(expect.begin(), expect.end());
675 EXPECT_THAT(test_vector, matcher_maker);
676 // Changing in place the values that initialized matcher_maker should not
677 // affect matcher_maker anymore. It should have made its own copy of them.
678 typedef vector<int>::iterator Iter;
679 for (Iter it = expect.begin(); it != expect.end(); ++it) { *it += 10; }
680 EXPECT_THAT(test_vector, matcher_maker);
681 test_vector.push_back(3);
682 EXPECT_THAT(test_vector, Not(matcher_maker));
683}
684
zhanyong.wance198ff2009-02-12 01:34:27 +0000685// Tests for the MATCHER*() macro family.
686
687// Tests that a simple MATCHER() definition works.
688
689MATCHER(IsEven, "") { return (arg % 2) == 0; }
690
691TEST(MatcherMacroTest, Works) {
692 const Matcher<int> m = IsEven();
693 EXPECT_TRUE(m.Matches(6));
694 EXPECT_FALSE(m.Matches(7));
695
696 EXPECT_EQ("is even", Describe(m));
697 EXPECT_EQ("not (is even)", DescribeNegation(m));
698 EXPECT_EQ("", Explain(m, 6));
699 EXPECT_EQ("", Explain(m, 7));
700}
701
zhanyong.wanb4140802010-06-08 22:53:57 +0000702// This also tests that the description string can reference 'negation'.
703MATCHER(IsEven2, negation ? "is odd" : "is even") {
zhanyong.wan82113312010-01-08 21:55:40 +0000704 if ((arg % 2) == 0) {
705 // Verifies that we can stream to result_listener, a listener
706 // supplied by the MATCHER macro implicitly.
707 *result_listener << "OK";
708 return true;
709 } else {
710 *result_listener << "% 2 == " << (arg % 2);
711 return false;
712 }
713}
714
zhanyong.wanb4140802010-06-08 22:53:57 +0000715// This also tests that the description string can reference matcher
716// parameters.
717MATCHER_P2(EqSumOf, x, y,
718 string(negation ? "doesn't equal" : "equals") + " the sum of " +
719 PrintToString(x) + " and " + PrintToString(y)) {
zhanyong.wan82113312010-01-08 21:55:40 +0000720 if (arg == (x + y)) {
721 *result_listener << "OK";
722 return true;
723 } else {
724 // Verifies that we can stream to the underlying stream of
725 // result_listener.
726 if (result_listener->stream() != NULL) {
727 *result_listener->stream() << "diff == " << (x + y - arg);
728 }
729 return false;
730 }
731}
732
zhanyong.wanb4140802010-06-08 22:53:57 +0000733// Tests that the matcher description can reference 'negation' and the
734// matcher parameters.
735TEST(MatcherMacroTest, DescriptionCanReferenceNegationAndParameters) {
736 const Matcher<int> m1 = IsEven2();
737 EXPECT_EQ("is even", Describe(m1));
738 EXPECT_EQ("is odd", DescribeNegation(m1));
739
740 const Matcher<int> m2 = EqSumOf(5, 9);
741 EXPECT_EQ("equals the sum of 5 and 9", Describe(m2));
742 EXPECT_EQ("doesn't equal the sum of 5 and 9", DescribeNegation(m2));
743}
744
745// Tests explaining match result in a MATCHER* macro.
zhanyong.wan82113312010-01-08 21:55:40 +0000746TEST(MatcherMacroTest, CanExplainMatchResult) {
747 const Matcher<int> m1 = IsEven2();
748 EXPECT_EQ("OK", Explain(m1, 4));
749 EXPECT_EQ("% 2 == 1", Explain(m1, 5));
750
751 const Matcher<int> m2 = EqSumOf(1, 2);
752 EXPECT_EQ("OK", Explain(m2, 3));
753 EXPECT_EQ("diff == -1", Explain(m2, 4));
754}
755
zhanyong.wance198ff2009-02-12 01:34:27 +0000756// Tests that the body of MATCHER() can reference the type of the
757// value being matched.
758
759MATCHER(IsEmptyString, "") {
760 StaticAssertTypeEq< ::std::string, arg_type>();
761 return arg == "";
762}
763
764MATCHER(IsEmptyStringByRef, "") {
765 StaticAssertTypeEq<const ::std::string&, arg_type>();
766 return arg == "";
767}
768
769TEST(MatcherMacroTest, CanReferenceArgType) {
770 const Matcher< ::std::string> m1 = IsEmptyString();
771 EXPECT_TRUE(m1.Matches(""));
772
773 const Matcher<const ::std::string&> m2 = IsEmptyStringByRef();
774 EXPECT_TRUE(m2.Matches(""));
775}
776
777// Tests that MATCHER() can be used in a namespace.
778
779namespace matcher_test {
780MATCHER(IsOdd, "") { return (arg % 2) != 0; }
781} // namespace matcher_test
782
zhanyong.wanb8243162009-06-04 05:48:20 +0000783TEST(MatcherMacroTest, WorksInNamespace) {
zhanyong.wance198ff2009-02-12 01:34:27 +0000784 Matcher<int> m = matcher_test::IsOdd();
785 EXPECT_FALSE(m.Matches(4));
786 EXPECT_TRUE(m.Matches(5));
787}
788
zhanyong.wanb8243162009-06-04 05:48:20 +0000789// Tests that Value() can be used to compose matchers.
790MATCHER(IsPositiveOdd, "") {
791 return Value(arg, matcher_test::IsOdd()) && arg > 0;
792}
793
794TEST(MatcherMacroTest, CanBeComposedUsingValue) {
795 EXPECT_THAT(3, IsPositiveOdd());
796 EXPECT_THAT(4, Not(IsPositiveOdd()));
797 EXPECT_THAT(-1, Not(IsPositiveOdd()));
798}
799
zhanyong.wance198ff2009-02-12 01:34:27 +0000800// Tests that a simple MATCHER_P() definition works.
801
802MATCHER_P(IsGreaterThan32And, n, "") { return arg > 32 && arg > n; }
803
804TEST(MatcherPMacroTest, Works) {
805 const Matcher<int> m = IsGreaterThan32And(5);
806 EXPECT_TRUE(m.Matches(36));
807 EXPECT_FALSE(m.Matches(5));
808
809 EXPECT_EQ("is greater than 32 and 5", Describe(m));
810 EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m));
811 EXPECT_EQ("", Explain(m, 36));
812 EXPECT_EQ("", Explain(m, 5));
813}
814
zhanyong.wance198ff2009-02-12 01:34:27 +0000815// Tests that the description is calculated correctly from the matcher name.
816MATCHER_P(_is_Greater_Than32and_, n, "") { return arg > 32 && arg > n; }
817
818TEST(MatcherPMacroTest, GeneratesCorrectDescription) {
819 const Matcher<int> m = _is_Greater_Than32and_(5);
820
821 EXPECT_EQ("is greater than 32 and 5", Describe(m));
822 EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m));
823 EXPECT_EQ("", Explain(m, 36));
824 EXPECT_EQ("", Explain(m, 5));
825}
826
827// Tests that a MATCHER_P matcher can be explicitly instantiated with
828// a reference parameter type.
829
830class UncopyableFoo {
831 public:
832 explicit UncopyableFoo(char value) : value_(value) {}
833 private:
834 UncopyableFoo(const UncopyableFoo&);
835 void operator=(const UncopyableFoo&);
836
837 char value_;
838};
839
840MATCHER_P(ReferencesUncopyable, variable, "") { return &arg == &variable; }
841
842TEST(MatcherPMacroTest, WorksWhenExplicitlyInstantiatedWithReference) {
843 UncopyableFoo foo1('1'), foo2('2');
844 const Matcher<const UncopyableFoo&> m =
845 ReferencesUncopyable<const UncopyableFoo&>(foo1);
846
847 EXPECT_TRUE(m.Matches(foo1));
848 EXPECT_FALSE(m.Matches(foo2));
849
850 // We don't want the address of the parameter printed, as most
851 // likely it will just annoy the user. If the address is
852 // interesting, the user should consider passing the parameter by
853 // pointer instead.
854 EXPECT_EQ("references uncopyable 1-byte object <31>", Describe(m));
855}
856
857
zhanyong.wance198ff2009-02-12 01:34:27 +0000858// Tests that the body of MATCHER_Pn() can reference the parameter
859// types.
860
861MATCHER_P3(ParamTypesAreIntLongAndChar, foo, bar, baz, "") {
862 StaticAssertTypeEq<int, foo_type>();
863 StaticAssertTypeEq<long, bar_type>(); // NOLINT
864 StaticAssertTypeEq<char, baz_type>();
865 return arg == 0;
866}
867
868TEST(MatcherPnMacroTest, CanReferenceParamTypes) {
869 EXPECT_THAT(0, ParamTypesAreIntLongAndChar(10, 20L, 'a'));
870}
871
872// Tests that a MATCHER_Pn matcher can be explicitly instantiated with
873// reference parameter types.
874
875MATCHER_P2(ReferencesAnyOf, variable1, variable2, "") {
876 return &arg == &variable1 || &arg == &variable2;
877}
878
879TEST(MatcherPnMacroTest, WorksWhenExplicitlyInstantiatedWithReferences) {
880 UncopyableFoo foo1('1'), foo2('2'), foo3('3');
881 const Matcher<const UncopyableFoo&> m =
882 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
883
884 EXPECT_TRUE(m.Matches(foo1));
885 EXPECT_TRUE(m.Matches(foo2));
886 EXPECT_FALSE(m.Matches(foo3));
887}
888
889TEST(MatcherPnMacroTest,
890 GeneratesCorretDescriptionWhenExplicitlyInstantiatedWithReferences) {
891 UncopyableFoo foo1('1'), foo2('2');
892 const Matcher<const UncopyableFoo&> m =
893 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
894
895 // We don't want the addresses of the parameters printed, as most
896 // likely they will just annoy the user. If the addresses are
897 // interesting, the user should consider passing the parameters by
898 // pointers instead.
899 EXPECT_EQ("references any of (1-byte object <31>, 1-byte object <32>)",
900 Describe(m));
901}
902
903// Tests that a simple MATCHER_P2() definition works.
904
905MATCHER_P2(IsNotInClosedRange, low, hi, "") { return arg < low || arg > hi; }
906
907TEST(MatcherPnMacroTest, Works) {
908 const Matcher<const long&> m = IsNotInClosedRange(10, 20); // NOLINT
909 EXPECT_TRUE(m.Matches(36L));
910 EXPECT_FALSE(m.Matches(15L));
911
912 EXPECT_EQ("is not in closed range (10, 20)", Describe(m));
913 EXPECT_EQ("not (is not in closed range (10, 20))", DescribeNegation(m));
914 EXPECT_EQ("", Explain(m, 36L));
915 EXPECT_EQ("", Explain(m, 15L));
916}
917
918// Tests that MATCHER*() definitions can be overloaded on the number
919// of parameters; also tests MATCHER_Pn() where n >= 3.
920
921MATCHER(EqualsSumOf, "") { return arg == 0; }
922MATCHER_P(EqualsSumOf, a, "") { return arg == a; }
923MATCHER_P2(EqualsSumOf, a, b, "") { return arg == a + b; }
924MATCHER_P3(EqualsSumOf, a, b, c, "") { return arg == a + b + c; }
925MATCHER_P4(EqualsSumOf, a, b, c, d, "") { return arg == a + b + c + d; }
926MATCHER_P5(EqualsSumOf, a, b, c, d, e, "") { return arg == a + b + c + d + e; }
927MATCHER_P6(EqualsSumOf, a, b, c, d, e, f, "") {
928 return arg == a + b + c + d + e + f;
929}
930MATCHER_P7(EqualsSumOf, a, b, c, d, e, f, g, "") {
931 return arg == a + b + c + d + e + f + g;
932}
933MATCHER_P8(EqualsSumOf, a, b, c, d, e, f, g, h, "") {
934 return arg == a + b + c + d + e + f + g + h;
935}
936MATCHER_P9(EqualsSumOf, a, b, c, d, e, f, g, h, i, "") {
937 return arg == a + b + c + d + e + f + g + h + i;
938}
939MATCHER_P10(EqualsSumOf, a, b, c, d, e, f, g, h, i, j, "") {
940 return arg == a + b + c + d + e + f + g + h + i + j;
941}
942
943TEST(MatcherPnMacroTest, CanBeOverloadedOnNumberOfParameters) {
944 EXPECT_THAT(0, EqualsSumOf());
945 EXPECT_THAT(1, EqualsSumOf(1));
946 EXPECT_THAT(12, EqualsSumOf(10, 2));
947 EXPECT_THAT(123, EqualsSumOf(100, 20, 3));
948 EXPECT_THAT(1234, EqualsSumOf(1000, 200, 30, 4));
949 EXPECT_THAT(12345, EqualsSumOf(10000, 2000, 300, 40, 5));
950 EXPECT_THAT("abcdef",
951 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f'));
952 EXPECT_THAT("abcdefg",
953 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g'));
954 EXPECT_THAT("abcdefgh",
955 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
956 "h"));
957 EXPECT_THAT("abcdefghi",
958 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
959 "h", 'i'));
960 EXPECT_THAT("abcdefghij",
961 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
962 "h", 'i', ::std::string("j")));
963
964 EXPECT_THAT(1, Not(EqualsSumOf()));
965 EXPECT_THAT(-1, Not(EqualsSumOf(1)));
966 EXPECT_THAT(-12, Not(EqualsSumOf(10, 2)));
967 EXPECT_THAT(-123, Not(EqualsSumOf(100, 20, 3)));
968 EXPECT_THAT(-1234, Not(EqualsSumOf(1000, 200, 30, 4)));
969 EXPECT_THAT(-12345, Not(EqualsSumOf(10000, 2000, 300, 40, 5)));
970 EXPECT_THAT("abcdef ",
971 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f')));
972 EXPECT_THAT("abcdefg ",
973 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f',
974 'g')));
975 EXPECT_THAT("abcdefgh ",
976 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
977 "h")));
978 EXPECT_THAT("abcdefghi ",
979 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
980 "h", 'i')));
981 EXPECT_THAT("abcdefghij ",
982 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
983 "h", 'i', ::std::string("j"))));
984}
985
986// Tests that a MATCHER_Pn() definition can be instantiated with any
987// compatible parameter types.
988TEST(MatcherPnMacroTest, WorksForDifferentParameterTypes) {
989 EXPECT_THAT(123, EqualsSumOf(100L, 20, static_cast<char>(3)));
990 EXPECT_THAT("abcd", EqualsSumOf(::std::string("a"), "b", 'c', "d"));
991
992 EXPECT_THAT(124, Not(EqualsSumOf(100L, 20, static_cast<char>(3))));
993 EXPECT_THAT("abcde", Not(EqualsSumOf(::std::string("a"), "b", 'c', "d")));
994}
995
996// Tests that the matcher body can promote the parameter types.
997
998MATCHER_P2(EqConcat, prefix, suffix, "") {
999 // The following lines promote the two parameters to desired types.
1000 std::string prefix_str(prefix);
zhanyong.wan32de5f52009-12-23 00:13:23 +00001001 char suffix_char = static_cast<char>(suffix);
zhanyong.wance198ff2009-02-12 01:34:27 +00001002 return arg == prefix_str + suffix_char;
1003}
1004
1005TEST(MatcherPnMacroTest, SimpleTypePromotion) {
1006 Matcher<std::string> no_promo =
1007 EqConcat(std::string("foo"), 't');
1008 Matcher<const std::string&> promo =
1009 EqConcat("foo", static_cast<int>('t'));
1010 EXPECT_FALSE(no_promo.Matches("fool"));
1011 EXPECT_FALSE(promo.Matches("fool"));
1012 EXPECT_TRUE(no_promo.Matches("foot"));
1013 EXPECT_TRUE(promo.Matches("foot"));
1014}
1015
1016// Verifies the type of a MATCHER*.
1017
1018TEST(MatcherPnMacroTest, TypesAreCorrect) {
1019 // EqualsSumOf() must be assignable to a EqualsSumOfMatcher variable.
1020 EqualsSumOfMatcher a0 = EqualsSumOf();
1021
1022 // EqualsSumOf(1) must be assignable to a EqualsSumOfMatcherP variable.
1023 EqualsSumOfMatcherP<int> a1 = EqualsSumOf(1);
1024
1025 // EqualsSumOf(p1, ..., pk) must be assignable to a EqualsSumOfMatcherPk
1026 // variable, and so on.
1027 EqualsSumOfMatcherP2<int, char> a2 = EqualsSumOf(1, '2');
1028 EqualsSumOfMatcherP3<int, int, char> a3 = EqualsSumOf(1, 2, '3');
1029 EqualsSumOfMatcherP4<int, int, int, char> a4 = EqualsSumOf(1, 2, 3, '4');
1030 EqualsSumOfMatcherP5<int, int, int, int, char> a5 =
1031 EqualsSumOf(1, 2, 3, 4, '5');
1032 EqualsSumOfMatcherP6<int, int, int, int, int, char> a6 =
1033 EqualsSumOf(1, 2, 3, 4, 5, '6');
1034 EqualsSumOfMatcherP7<int, int, int, int, int, int, char> a7 =
1035 EqualsSumOf(1, 2, 3, 4, 5, 6, '7');
1036 EqualsSumOfMatcherP8<int, int, int, int, int, int, int, char> a8 =
1037 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, '8');
1038 EqualsSumOfMatcherP9<int, int, int, int, int, int, int, int, char> a9 =
1039 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, '9');
1040 EqualsSumOfMatcherP10<int, int, int, int, int, int, int, int, int, char> a10 =
1041 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, 9, '0');
zhanyong.wan29be9232013-03-01 06:53:35 +00001042
1043 // Avoid "unused variable" warnings.
1044 (void)a0;
1045 (void)a1;
1046 (void)a2;
1047 (void)a3;
1048 (void)a4;
1049 (void)a5;
1050 (void)a6;
1051 (void)a7;
1052 (void)a8;
1053 (void)a9;
1054 (void)a10;
zhanyong.wance198ff2009-02-12 01:34:27 +00001055}
1056
zhanyong.wanb8243162009-06-04 05:48:20 +00001057// Tests that matcher-typed parameters can be used in Value() inside a
1058// MATCHER_Pn definition.
1059
1060// Succeeds if arg matches exactly 2 of the 3 matchers.
1061MATCHER_P3(TwoOf, m1, m2, m3, "") {
1062 const int count = static_cast<int>(Value(arg, m1))
1063 + static_cast<int>(Value(arg, m2)) + static_cast<int>(Value(arg, m3));
1064 return count == 2;
1065}
1066
1067TEST(MatcherPnMacroTest, CanUseMatcherTypedParameterInValue) {
1068 EXPECT_THAT(42, TwoOf(Gt(0), Lt(50), Eq(10)));
1069 EXPECT_THAT(0, Not(TwoOf(Gt(-1), Lt(1), Eq(0))));
1070}
1071
1072// Tests Contains().
1073
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001074TEST(ContainsTest, ListMatchesWhenElementIsInContainer) {
1075 list<int> some_list;
1076 some_list.push_back(3);
1077 some_list.push_back(1);
1078 some_list.push_back(2);
1079 EXPECT_THAT(some_list, Contains(1));
zhanyong.wanb8243162009-06-04 05:48:20 +00001080 EXPECT_THAT(some_list, Contains(Gt(2.5)));
1081 EXPECT_THAT(some_list, Contains(Eq(2.0f)));
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001082
1083 list<string> another_list;
1084 another_list.push_back("fee");
1085 another_list.push_back("fie");
1086 another_list.push_back("foe");
1087 another_list.push_back("fum");
1088 EXPECT_THAT(another_list, Contains(string("fee")));
1089}
1090
1091TEST(ContainsTest, ListDoesNotMatchWhenElementIsNotInContainer) {
1092 list<int> some_list;
1093 some_list.push_back(3);
1094 some_list.push_back(1);
1095 EXPECT_THAT(some_list, Not(Contains(4)));
1096}
1097
1098TEST(ContainsTest, SetMatchesWhenElementIsInContainer) {
1099 set<int> some_set;
1100 some_set.insert(3);
1101 some_set.insert(1);
1102 some_set.insert(2);
zhanyong.wanb8243162009-06-04 05:48:20 +00001103 EXPECT_THAT(some_set, Contains(Eq(1.0)));
1104 EXPECT_THAT(some_set, Contains(Eq(3.0f)));
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001105 EXPECT_THAT(some_set, Contains(2));
1106
1107 set<const char*> another_set;
1108 another_set.insert("fee");
1109 another_set.insert("fie");
1110 another_set.insert("foe");
1111 another_set.insert("fum");
zhanyong.wanb8243162009-06-04 05:48:20 +00001112 EXPECT_THAT(another_set, Contains(Eq(string("fum"))));
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001113}
1114
1115TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) {
1116 set<int> some_set;
1117 some_set.insert(3);
1118 some_set.insert(1);
1119 EXPECT_THAT(some_set, Not(Contains(4)));
1120
1121 set<const char*> c_string_set;
1122 c_string_set.insert("hello");
1123 EXPECT_THAT(c_string_set, Not(Contains(string("hello").c_str())));
1124}
1125
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001126TEST(ContainsTest, ExplainsMatchResultCorrectly) {
zhanyong.wanb8243162009-06-04 05:48:20 +00001127 const int a[2] = { 1, 2 };
jgm38513a82012-11-15 15:50:36 +00001128 Matcher<const int (&)[2]> m = Contains(2);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001129 EXPECT_EQ("whose element #1 matches", Explain(m, a));
zhanyong.wanb8243162009-06-04 05:48:20 +00001130
1131 m = Contains(3);
1132 EXPECT_EQ("", Explain(m, a));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001133
1134 m = Contains(GreaterThan(0));
1135 EXPECT_EQ("whose element #0 matches, which is 1 more than 0", Explain(m, a));
1136
1137 m = Contains(GreaterThan(10));
1138 EXPECT_EQ("", Explain(m, a));
zhanyong.wanb8243162009-06-04 05:48:20 +00001139}
1140
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001141TEST(ContainsTest, DescribesItselfCorrectly) {
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001142 Matcher<vector<int> > m = Contains(1);
zhanyong.wanb8243162009-06-04 05:48:20 +00001143 EXPECT_EQ("contains at least one element that is equal to 1", Describe(m));
1144
1145 Matcher<vector<int> > m2 = Not(m);
1146 EXPECT_EQ("doesn't contain any element that is equal to 1", Describe(m2));
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001147}
1148
1149TEST(ContainsTest, MapMatchesWhenElementIsInContainer) {
1150 map<const char*, int> my_map;
1151 const char* bar = "a string";
1152 my_map[bar] = 2;
1153 EXPECT_THAT(my_map, Contains(pair<const char* const, int>(bar, 2)));
1154
1155 map<string, int> another_map;
1156 another_map["fee"] = 1;
1157 another_map["fie"] = 2;
1158 another_map["foe"] = 3;
1159 another_map["fum"] = 4;
1160 EXPECT_THAT(another_map, Contains(pair<const string, int>(string("fee"), 1)));
1161 EXPECT_THAT(another_map, Contains(pair<const string, int>("fie", 2)));
1162}
1163
1164TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) {
1165 map<int, int> some_map;
1166 some_map[1] = 11;
1167 some_map[2] = 22;
1168 EXPECT_THAT(some_map, Not(Contains(pair<const int, int>(2, 23))));
1169}
1170
1171TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) {
1172 const char* string_array[] = { "fee", "fie", "foe", "fum" };
zhanyong.wanb8243162009-06-04 05:48:20 +00001173 EXPECT_THAT(string_array, Contains(Eq(string("fum"))));
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001174}
1175
1176TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) {
1177 int int_array[] = { 1, 2, 3, 4 };
1178 EXPECT_THAT(int_array, Not(Contains(5)));
1179}
1180
zhanyong.wanb8243162009-06-04 05:48:20 +00001181TEST(ContainsTest, AcceptsMatcher) {
1182 const int a[] = { 1, 2, 3 };
1183 EXPECT_THAT(a, Contains(Gt(2)));
1184 EXPECT_THAT(a, Not(Contains(Gt(4))));
1185}
1186
1187TEST(ContainsTest, WorksForNativeArrayAsTuple) {
1188 const int a[] = { 1, 2 };
zhanyong.wan2661c682009-06-09 05:42:12 +00001189 const int* const pointer = a;
1190 EXPECT_THAT(make_tuple(pointer, 2), Contains(1));
1191 EXPECT_THAT(make_tuple(pointer, 2), Not(Contains(Gt(3))));
zhanyong.wanb8243162009-06-04 05:48:20 +00001192}
1193
1194TEST(ContainsTest, WorksForTwoDimensionalNativeArray) {
1195 int a[][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
1196 EXPECT_THAT(a, Contains(ElementsAre(4, 5, 6)));
1197 EXPECT_THAT(a, Contains(Contains(5)));
1198 EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5))));
1199 EXPECT_THAT(a, Contains(Not(Contains(5))));
1200}
1201
jgm79a367e2012-04-10 16:02:11 +00001202TEST(AllOfTest, HugeMatcher) {
1203 // Verify that using AllOf with many arguments doesn't cause
1204 // the compiler to exceed template instantiation depth limit.
1205 EXPECT_THAT(0, testing::AllOf(_, _, _, _, _, _, _, _, _,
1206 testing::AllOf(_, _, _, _, _, _, _, _, _, _)));
1207}
1208
1209TEST(AnyOfTest, HugeMatcher) {
1210 // Verify that using AnyOf with many arguments doesn't cause
1211 // the compiler to exceed template instantiation depth limit.
1212 EXPECT_THAT(0, testing::AnyOf(_, _, _, _, _, _, _, _, _,
1213 testing::AnyOf(_, _, _, _, _, _, _, _, _, _)));
1214}
1215
zhanyong.wan86d2eeb2011-03-16 17:10:39 +00001216namespace adl_test {
1217
1218// Verifies that the implementation of ::testing::AllOf and ::testing::AnyOf
1219// don't issue unqualified recursive calls. If they do, the argument dependent
1220// name lookup will cause AllOf/AnyOf in the 'adl_test' namespace to be found
1221// as a candidate and the compilation will break due to an ambiguous overload.
1222
1223// The matcher must be in the same namespace as AllOf/AnyOf to make argument
1224// dependent lookup find those.
1225MATCHER(M, "") { return true; }
1226
1227template <typename T1, typename T2>
1228bool AllOf(const T1& t1, const T2& t2) { return true; }
1229
1230TEST(AllOfTest, DoesNotCallAllOfUnqualified) {
1231 EXPECT_THAT(42, testing::AllOf(
1232 M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
1233}
1234
1235template <typename T1, typename T2> bool
1236AnyOf(const T1& t1, const T2& t2) { return true; }
1237
1238TEST(AnyOfTest, DoesNotCallAnyOfUnqualified) {
1239 EXPECT_THAT(42, testing::AnyOf(
1240 M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
1241}
1242
1243} // namespace adl_test
1244
zhanyong.wan82113312010-01-08 21:55:40 +00001245#ifdef _MSC_VER
zhanyong.wan658ac0b2011-02-24 07:29:13 +00001246# pragma warning(pop)
zhanyong.wan82113312010-01-08 21:55:40 +00001247#endif
1248
shiqiane35fdd92008-12-10 05:08:54 +00001249} // namespace