blob: dba74ecbfe6388387f48b799c82744634c52642c [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.wan1cc1d4b2013-08-08 18:41:51 +000067using testing::Le;
zhanyong.wanb8243162009-06-04 05:48:20 +000068using testing::Lt;
shiqiane35fdd92008-12-10 05:08:54 +000069using testing::MakeMatcher;
70using testing::Matcher;
71using testing::MatcherInterface;
zhanyong.wandb22c222010-01-28 21:52:29 +000072using testing::MatchResultListener;
shiqiane35fdd92008-12-10 05:08:54 +000073using testing::Ne;
74using testing::Not;
75using testing::Pointee;
zhanyong.wanb4140802010-06-08 22:53:57 +000076using testing::PrintToString;
shiqiane35fdd92008-12-10 05:08:54 +000077using testing::Ref;
zhanyong.wance198ff2009-02-12 01:34:27 +000078using testing::StaticAssertTypeEq;
shiqiane35fdd92008-12-10 05:08:54 +000079using testing::StrEq;
zhanyong.wanb8243162009-06-04 05:48:20 +000080using testing::Value;
jgm38513a82012-11-15 15:50:36 +000081using testing::internal::ElementsAreArrayMatcher;
shiqiane35fdd92008-12-10 05:08:54 +000082using testing::internal::string;
83
zhanyong.wanfb25d532013-07-28 08:24:00 +000084// Evaluates to the number of elements in 'array'.
85#define GMOCK_ARRAY_SIZE_(a) (sizeof(a) / sizeof(a[0]))
86
shiqiane35fdd92008-12-10 05:08:54 +000087// Returns the description of the given matcher.
88template <typename T>
89string Describe(const Matcher<T>& m) {
90 stringstream ss;
91 m.DescribeTo(&ss);
92 return ss.str();
93}
94
95// Returns the description of the negation of the given matcher.
96template <typename T>
97string DescribeNegation(const Matcher<T>& m) {
98 stringstream ss;
99 m.DescribeNegationTo(&ss);
100 return ss.str();
101}
102
103// Returns the reason why x matches, or doesn't match, m.
104template <typename MatcherType, typename Value>
105string Explain(const MatcherType& m, const Value& x) {
106 stringstream ss;
107 m.ExplainMatchResultTo(x, &ss);
108 return ss.str();
109}
110
zhanyong.wan2661c682009-06-09 05:42:12 +0000111// Tests Args<k0, ..., kn>(m).
112
113TEST(ArgsTest, AcceptsZeroTemplateArg) {
114 const tuple<int, bool> t(5, true);
115 EXPECT_THAT(t, Args<>(Eq(tuple<>())));
116 EXPECT_THAT(t, Not(Args<>(Ne(tuple<>()))));
117}
118
119TEST(ArgsTest, AcceptsOneTemplateArg) {
120 const tuple<int, bool> t(5, true);
121 EXPECT_THAT(t, Args<0>(Eq(make_tuple(5))));
122 EXPECT_THAT(t, Args<1>(Eq(make_tuple(true))));
123 EXPECT_THAT(t, Not(Args<1>(Eq(make_tuple(false)))));
124}
125
126TEST(ArgsTest, AcceptsTwoTemplateArgs) {
127 const tuple<short, int, long> t(4, 5, 6L); // NOLINT
128
129 EXPECT_THAT(t, (Args<0, 1>(Lt())));
130 EXPECT_THAT(t, (Args<1, 2>(Lt())));
131 EXPECT_THAT(t, Not(Args<0, 2>(Gt())));
132}
133
134TEST(ArgsTest, AcceptsRepeatedTemplateArgs) {
135 const tuple<short, int, long> t(4, 5, 6L); // NOLINT
136 EXPECT_THAT(t, (Args<0, 0>(Eq())));
137 EXPECT_THAT(t, Not(Args<1, 1>(Ne())));
138}
139
140TEST(ArgsTest, AcceptsDecreasingTemplateArgs) {
141 const tuple<short, int, long> t(4, 5, 6L); // NOLINT
142 EXPECT_THAT(t, (Args<2, 0>(Gt())));
143 EXPECT_THAT(t, Not(Args<2, 1>(Lt())));
144}
145
zhanyong.wan82113312010-01-08 21:55:40 +0000146// The MATCHER*() macros trigger warning C4100 (unreferenced formal
147// parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
148// the macro definition, as the warnings are generated when the macro
149// is expanded and macro expansion cannot contain #pragma. Therefore
150// we suppress them here.
151#ifdef _MSC_VER
zhanyong.wan658ac0b2011-02-24 07:29:13 +0000152# pragma warning(push)
153# pragma warning(disable:4100)
zhanyong.wan82113312010-01-08 21:55:40 +0000154#endif
155
zhanyong.wan2661c682009-06-09 05:42:12 +0000156MATCHER(SumIsZero, "") {
157 return get<0>(arg) + get<1>(arg) + get<2>(arg) == 0;
158}
159
160TEST(ArgsTest, AcceptsMoreTemplateArgsThanArityOfOriginalTuple) {
161 EXPECT_THAT(make_tuple(-1, 2), (Args<0, 0, 1>(SumIsZero())));
162 EXPECT_THAT(make_tuple(1, 2), Not(Args<0, 0, 1>(SumIsZero())));
163}
164
165TEST(ArgsTest, CanBeNested) {
166 const tuple<short, int, long, int> t(4, 5, 6L, 6); // NOLINT
167 EXPECT_THAT(t, (Args<1, 2, 3>(Args<1, 2>(Eq()))));
168 EXPECT_THAT(t, (Args<0, 1, 3>(Args<0, 2>(Lt()))));
169}
170
171TEST(ArgsTest, CanMatchTupleByValue) {
172 typedef tuple<char, int, int> Tuple3;
173 const Matcher<Tuple3> m = Args<1, 2>(Lt());
174 EXPECT_TRUE(m.Matches(Tuple3('a', 1, 2)));
175 EXPECT_FALSE(m.Matches(Tuple3('b', 2, 2)));
176}
177
178TEST(ArgsTest, CanMatchTupleByReference) {
179 typedef tuple<char, char, int> Tuple3;
180 const Matcher<const Tuple3&> m = Args<0, 1>(Lt());
181 EXPECT_TRUE(m.Matches(Tuple3('a', 'b', 2)));
182 EXPECT_FALSE(m.Matches(Tuple3('b', 'b', 2)));
183}
184
185// Validates that arg is printed as str.
186MATCHER_P(PrintsAs, str, "") {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000187 return testing::PrintToString(arg) == str;
zhanyong.wan2661c682009-06-09 05:42:12 +0000188}
189
190TEST(ArgsTest, AcceptsTenTemplateArgs) {
191 EXPECT_THAT(make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
192 (Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
193 PrintsAs("(9, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
194 EXPECT_THAT(make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
195 Not(Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
196 PrintsAs("(0, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
197}
198
199TEST(ArgsTest, DescirbesSelfCorrectly) {
200 const Matcher<tuple<int, bool, char> > m = Args<2, 0>(Lt());
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000201 EXPECT_EQ("are a tuple whose fields (#2, #0) are a pair where "
202 "the first < the second",
zhanyong.wan2661c682009-06-09 05:42:12 +0000203 Describe(m));
204}
205
206TEST(ArgsTest, DescirbesNestedArgsCorrectly) {
207 const Matcher<const tuple<int, bool, char, int>&> m =
208 Args<0, 2, 3>(Args<2, 0>(Lt()));
209 EXPECT_EQ("are a tuple whose fields (#0, #2, #3) are a tuple "
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000210 "whose fields (#2, #0) are a pair where the first < the second",
zhanyong.wan2661c682009-06-09 05:42:12 +0000211 Describe(m));
212}
213
214TEST(ArgsTest, DescribesNegationCorrectly) {
215 const Matcher<tuple<int, char> > m = Args<1, 0>(Gt());
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000216 EXPECT_EQ("are a tuple whose fields (#1, #0) aren't a pair "
217 "where the first > the second",
zhanyong.wan2661c682009-06-09 05:42:12 +0000218 DescribeNegation(m));
219}
220
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000221TEST(ArgsTest, ExplainsMatchResultWithoutInnerExplanation) {
222 const Matcher<tuple<bool, int, int> > m = Args<1, 2>(Eq());
223 EXPECT_EQ("whose fields (#1, #2) are (42, 42)",
224 Explain(m, make_tuple(false, 42, 42)));
225 EXPECT_EQ("whose fields (#1, #2) are (42, 43)",
226 Explain(m, make_tuple(false, 42, 43)));
227}
228
229// For testing Args<>'s explanation.
230class LessThanMatcher : public MatcherInterface<tuple<char, int> > {
231 public:
232 virtual void DescribeTo(::std::ostream* os) const {}
233
234 virtual bool MatchAndExplain(tuple<char, int> value,
235 MatchResultListener* listener) const {
236 const int diff = get<0>(value) - get<1>(value);
237 if (diff > 0) {
238 *listener << "where the first value is " << diff
239 << " more than the second";
240 }
241 return diff < 0;
242 }
243};
244
245Matcher<tuple<char, int> > LessThan() {
246 return MakeMatcher(new LessThanMatcher);
247}
248
249TEST(ArgsTest, ExplainsMatchResultWithInnerExplanation) {
250 const Matcher<tuple<char, int, int> > m = Args<0, 2>(LessThan());
zhanyong.wand60c5f42010-07-21 22:21:07 +0000251 EXPECT_EQ("whose fields (#0, #2) are ('a' (97, 0x61), 42), "
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000252 "where the first value is 55 more than the second",
253 Explain(m, make_tuple('a', 42, 42)));
254 EXPECT_EQ("whose fields (#0, #2) are ('\\0', 43)",
255 Explain(m, make_tuple('\0', 42, 43)));
256}
257
shiqiane35fdd92008-12-10 05:08:54 +0000258// For testing ExplainMatchResultTo().
259class GreaterThanMatcher : public MatcherInterface<int> {
260 public:
261 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
262
shiqiane35fdd92008-12-10 05:08:54 +0000263 virtual void DescribeTo(::std::ostream* os) const {
264 *os << "is greater than " << rhs_;
265 }
266
zhanyong.wandb22c222010-01-28 21:52:29 +0000267 virtual bool MatchAndExplain(int lhs,
268 MatchResultListener* listener) const {
shiqiane35fdd92008-12-10 05:08:54 +0000269 const int diff = lhs - rhs_;
270 if (diff > 0) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000271 *listener << "which is " << diff << " more than " << rhs_;
shiqiane35fdd92008-12-10 05:08:54 +0000272 } else if (diff == 0) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000273 *listener << "which is the same as " << rhs_;
shiqiane35fdd92008-12-10 05:08:54 +0000274 } else {
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000275 *listener << "which is " << -diff << " less than " << rhs_;
shiqiane35fdd92008-12-10 05:08:54 +0000276 }
zhanyong.wandb22c222010-01-28 21:52:29 +0000277
278 return lhs > rhs_;
shiqiane35fdd92008-12-10 05:08:54 +0000279 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000280
shiqiane35fdd92008-12-10 05:08:54 +0000281 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000282 int rhs_;
shiqiane35fdd92008-12-10 05:08:54 +0000283};
284
285Matcher<int> GreaterThan(int n) {
286 return MakeMatcher(new GreaterThanMatcher(n));
287}
288
289// Tests for ElementsAre().
290
shiqiane35fdd92008-12-10 05:08:54 +0000291TEST(ElementsAreTest, CanDescribeExpectingNoElement) {
292 Matcher<const vector<int>&> m = ElementsAre();
293 EXPECT_EQ("is empty", Describe(m));
294}
295
296TEST(ElementsAreTest, CanDescribeExpectingOneElement) {
297 Matcher<vector<int> > m = ElementsAre(Gt(5));
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000298 EXPECT_EQ("has 1 element that is > 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000299}
300
301TEST(ElementsAreTest, CanDescribeExpectingManyElements) {
302 Matcher<list<string> > m = ElementsAre(StrEq("one"), "two");
303 EXPECT_EQ("has 2 elements where\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000304 "element #0 is equal to \"one\",\n"
305 "element #1 is equal to \"two\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000306}
307
308TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) {
309 Matcher<vector<int> > m = ElementsAre();
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000310 EXPECT_EQ("isn't empty", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +0000311}
312
313TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElment) {
314 Matcher<const list<int>& > m = ElementsAre(Gt(5));
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000315 EXPECT_EQ("doesn't have 1 element, or\n"
316 "element #0 isn't > 5", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +0000317}
318
319TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) {
320 Matcher<const list<string>& > m = ElementsAre("one", "two");
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000321 EXPECT_EQ("doesn't have 2 elements, or\n"
322 "element #0 isn't equal to \"one\", or\n"
323 "element #1 isn't equal to \"two\"", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +0000324}
325
326TEST(ElementsAreTest, DoesNotExplainTrivialMatch) {
327 Matcher<const list<int>& > m = ElementsAre(1, Ne(2));
328
329 list<int> test_list;
330 test_list.push_back(1);
331 test_list.push_back(3);
332 EXPECT_EQ("", Explain(m, test_list)); // No need to explain anything.
333}
334
335TEST(ElementsAreTest, ExplainsNonTrivialMatch) {
336 Matcher<const vector<int>& > m =
337 ElementsAre(GreaterThan(1), 0, GreaterThan(2));
338
339 const int a[] = { 10, 0, 100 };
340 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000341 EXPECT_EQ("whose element #0 matches, which is 9 more than 1,\n"
342 "and whose element #2 matches, which is 98 more than 2",
343 Explain(m, test_vector));
shiqiane35fdd92008-12-10 05:08:54 +0000344}
345
346TEST(ElementsAreTest, CanExplainMismatchWrongSize) {
347 Matcher<const list<int>& > m = ElementsAre(1, 3);
348
349 list<int> test_list;
350 // No need to explain when the container is empty.
351 EXPECT_EQ("", Explain(m, test_list));
352
353 test_list.push_back(1);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000354 EXPECT_EQ("which has 1 element", Explain(m, test_list));
shiqiane35fdd92008-12-10 05:08:54 +0000355}
356
357TEST(ElementsAreTest, CanExplainMismatchRightSize) {
358 Matcher<const vector<int>& > m = ElementsAre(1, GreaterThan(5));
359
360 vector<int> v;
361 v.push_back(2);
362 v.push_back(1);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000363 EXPECT_EQ("whose element #0 doesn't match", Explain(m, v));
shiqiane35fdd92008-12-10 05:08:54 +0000364
365 v[0] = 1;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000366 EXPECT_EQ("whose element #1 doesn't match, which is 4 less than 5",
367 Explain(m, v));
shiqiane35fdd92008-12-10 05:08:54 +0000368}
369
370TEST(ElementsAreTest, MatchesOneElementVector) {
371 vector<string> test_vector;
372 test_vector.push_back("test string");
373
374 EXPECT_THAT(test_vector, ElementsAre(StrEq("test string")));
375}
376
377TEST(ElementsAreTest, MatchesOneElementList) {
378 list<string> test_list;
379 test_list.push_back("test string");
380
381 EXPECT_THAT(test_list, ElementsAre("test string"));
382}
383
384TEST(ElementsAreTest, MatchesThreeElementVector) {
385 vector<string> test_vector;
386 test_vector.push_back("one");
387 test_vector.push_back("two");
388 test_vector.push_back("three");
389
390 EXPECT_THAT(test_vector, ElementsAre("one", StrEq("two"), _));
391}
392
393TEST(ElementsAreTest, MatchesOneElementEqMatcher) {
394 vector<int> test_vector;
395 test_vector.push_back(4);
396
397 EXPECT_THAT(test_vector, ElementsAre(Eq(4)));
398}
399
400TEST(ElementsAreTest, MatchesOneElementAnyMatcher) {
401 vector<int> test_vector;
402 test_vector.push_back(4);
403
404 EXPECT_THAT(test_vector, ElementsAre(_));
405}
406
407TEST(ElementsAreTest, MatchesOneElementValue) {
408 vector<int> test_vector;
409 test_vector.push_back(4);
410
411 EXPECT_THAT(test_vector, ElementsAre(4));
412}
413
414TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) {
415 vector<int> test_vector;
416 test_vector.push_back(1);
417 test_vector.push_back(2);
418 test_vector.push_back(3);
419
420 EXPECT_THAT(test_vector, ElementsAre(1, Eq(2), _));
421}
422
423TEST(ElementsAreTest, MatchesTenElementVector) {
424 const int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
425 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
426
427 EXPECT_THAT(test_vector,
428 // The element list can contain values and/or matchers
429 // of different types.
430 ElementsAre(0, Ge(0), _, 3, 4, Ne(2), Eq(6), 7, 8, _));
431}
432
433TEST(ElementsAreTest, DoesNotMatchWrongSize) {
434 vector<string> test_vector;
435 test_vector.push_back("test string");
436 test_vector.push_back("test string");
437
438 Matcher<vector<string> > m = ElementsAre(StrEq("test string"));
439 EXPECT_FALSE(m.Matches(test_vector));
440}
441
442TEST(ElementsAreTest, DoesNotMatchWrongValue) {
443 vector<string> test_vector;
444 test_vector.push_back("other string");
445
446 Matcher<vector<string> > m = ElementsAre(StrEq("test string"));
447 EXPECT_FALSE(m.Matches(test_vector));
448}
449
450TEST(ElementsAreTest, DoesNotMatchWrongOrder) {
451 vector<string> test_vector;
452 test_vector.push_back("one");
453 test_vector.push_back("three");
454 test_vector.push_back("two");
455
456 Matcher<vector<string> > m = ElementsAre(
457 StrEq("one"), StrEq("two"), StrEq("three"));
458 EXPECT_FALSE(m.Matches(test_vector));
459}
460
461TEST(ElementsAreTest, WorksForNestedContainer) {
462 const char* strings[] = {
463 "Hi",
464 "world"
465 };
466
467 vector<list<char> > nested;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000468 for (size_t i = 0; i < GMOCK_ARRAY_SIZE_(strings); i++) {
shiqiane35fdd92008-12-10 05:08:54 +0000469 nested.push_back(list<char>(strings[i], strings[i] + strlen(strings[i])));
470 }
471
472 EXPECT_THAT(nested, ElementsAre(ElementsAre('H', Ne('e')),
473 ElementsAre('w', 'o', _, _, 'd')));
474 EXPECT_THAT(nested, Not(ElementsAre(ElementsAre('H', 'e'),
475 ElementsAre('w', 'o', _, _, 'd'))));
476}
477
478TEST(ElementsAreTest, WorksWithByRefElementMatchers) {
479 int a[] = { 0, 1, 2 };
480 vector<int> v(a, a + GMOCK_ARRAY_SIZE_(a));
481
482 EXPECT_THAT(v, ElementsAre(Ref(v[0]), Ref(v[1]), Ref(v[2])));
483 EXPECT_THAT(v, Not(ElementsAre(Ref(v[0]), Ref(v[1]), Ref(a[2]))));
484}
485
486TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) {
487 int a[] = { 0, 1, 2 };
488 vector<int> v(a, a + GMOCK_ARRAY_SIZE_(a));
489
490 EXPECT_THAT(&v, Pointee(ElementsAre(0, 1, _)));
491 EXPECT_THAT(&v, Not(Pointee(ElementsAre(0, _, 3))));
492}
493
zhanyong.wanb8243162009-06-04 05:48:20 +0000494TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) {
495 int array[] = { 0, 1, 2 };
496 EXPECT_THAT(array, ElementsAre(0, 1, _));
497 EXPECT_THAT(array, Not(ElementsAre(1, _, _)));
498 EXPECT_THAT(array, Not(ElementsAre(0, _)));
499}
500
501class NativeArrayPassedAsPointerAndSize {
502 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000503 NativeArrayPassedAsPointerAndSize() {}
504
zhanyong.wanb8243162009-06-04 05:48:20 +0000505 MOCK_METHOD2(Helper, void(int* array, int size));
zhanyong.wan32de5f52009-12-23 00:13:23 +0000506
507 private:
508 GTEST_DISALLOW_COPY_AND_ASSIGN_(NativeArrayPassedAsPointerAndSize);
zhanyong.wanb8243162009-06-04 05:48:20 +0000509};
510
511TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) {
512 int array[] = { 0, 1 };
513 ::std::tr1::tuple<int*, size_t> array_as_tuple(array, 2);
514 EXPECT_THAT(array_as_tuple, ElementsAre(0, 1));
515 EXPECT_THAT(array_as_tuple, Not(ElementsAre(0)));
516
517 NativeArrayPassedAsPointerAndSize helper;
518 EXPECT_CALL(helper, Helper(_, _))
zhanyong.wanbf550852009-06-09 06:09:53 +0000519 .With(ElementsAre(0, 1));
zhanyong.wanb8243162009-06-04 05:48:20 +0000520 helper.Helper(array, 2);
521}
522
523TEST(ElementsAreTest, WorksWithTwoDimensionalNativeArray) {
524 const char a2[][3] = { "hi", "lo" };
525 EXPECT_THAT(a2, ElementsAre(ElementsAre('h', 'i', '\0'),
526 ElementsAre('l', 'o', '\0')));
527 EXPECT_THAT(a2, ElementsAre(StrEq("hi"), StrEq("lo")));
528 EXPECT_THAT(a2, ElementsAre(Not(ElementsAre('h', 'o', '\0')),
529 ElementsAre('l', 'o', '\0')));
530}
531
jgm38513a82012-11-15 15:50:36 +0000532TEST(ElementsAreTest, AcceptsStringLiteral) {
533 string array[] = { "hi", "one", "two" };
534 EXPECT_THAT(array, ElementsAre("hi", "one", "two"));
535 EXPECT_THAT(array, Not(ElementsAre("hi", "one", "too")));
536}
537
538#ifndef _MSC_VER
539
540// The following test passes a value of type const char[] to a
541// function template that expects const T&. Some versions of MSVC
542// generates a compiler error C2665 for that. We believe it's a bug
543// in MSVC. Therefore this test is #if-ed out for MSVC.
544
545// Declared here with the size unknown. Defined AFTER the following test.
546extern const char kHi[];
547
548TEST(ElementsAreTest, AcceptsArrayWithUnknownSize) {
549 // The size of kHi is not known in this test, but ElementsAre() should
550 // still accept it.
551
552 string array1[] = { "hi" };
553 EXPECT_THAT(array1, ElementsAre(kHi));
554
555 string array2[] = { "ho" };
556 EXPECT_THAT(array2, Not(ElementsAre(kHi)));
557}
558
559const char kHi[] = "hi";
560
561#endif // _MSC_VER
562
563TEST(ElementsAreTest, MakesCopyOfArguments) {
564 int x = 1;
565 int y = 2;
566 // This should make a copy of x and y.
zhanyong.wanfb25d532013-07-28 08:24:00 +0000567 ::testing::internal::ElementsAreMatcher<std::tr1::tuple<int, int> >
568 polymorphic_matcher = ElementsAre(x, y);
jgm38513a82012-11-15 15:50:36 +0000569 // Changing x and y now shouldn't affect the meaning of the above matcher.
570 x = y = 0;
571 const int array1[] = { 1, 2 };
572 EXPECT_THAT(array1, polymorphic_matcher);
573 const int array2[] = { 0, 0 };
574 EXPECT_THAT(array2, Not(polymorphic_matcher));
575}
576
zhanyong.wanfb25d532013-07-28 08:24:00 +0000577
shiqiane35fdd92008-12-10 05:08:54 +0000578// Tests for ElementsAreArray(). Since ElementsAreArray() shares most
579// of the implementation with ElementsAre(), we don't test it as
580// thoroughly here.
581
582TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) {
583 const int a[] = { 1, 2, 3 };
584
585 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
586 EXPECT_THAT(test_vector, ElementsAreArray(a));
587
588 test_vector[2] = 0;
589 EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
590}
591
592TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) {
593 const char* a[] = { "one", "two", "three" };
594
595 vector<string> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
596 EXPECT_THAT(test_vector, ElementsAreArray(a, GMOCK_ARRAY_SIZE_(a)));
597
598 const char** p = a;
599 test_vector[0] = "1";
600 EXPECT_THAT(test_vector, Not(ElementsAreArray(p, GMOCK_ARRAY_SIZE_(a))));
601}
602
603TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) {
604 const char* a[] = { "one", "two", "three" };
605
606 vector<string> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
607 EXPECT_THAT(test_vector, ElementsAreArray(a));
608
609 test_vector[0] = "1";
610 EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
611}
612
613TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) {
614 const Matcher<string> kMatcherArray[] =
615 { StrEq("one"), StrEq("two"), StrEq("three") };
616
617 vector<string> test_vector;
618 test_vector.push_back("one");
619 test_vector.push_back("two");
620 test_vector.push_back("three");
621 EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray));
622
623 test_vector.push_back("three");
624 EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray)));
625}
626
jgm38513a82012-11-15 15:50:36 +0000627TEST(ElementsAreArrayTest, CanBeCreatedWithVector) {
628 const int a[] = { 1, 2, 3 };
629 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
630 const vector<int> expected(a, a + GMOCK_ARRAY_SIZE_(a));
631 EXPECT_THAT(test_vector, ElementsAreArray(expected));
632 test_vector.push_back(4);
633 EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));
634}
635
zhanyong.wan1cc1d4b2013-08-08 18:41:51 +0000636#if GTEST_LANG_CXX11
637
638TEST(ElementsAreArrayTest, TakesInitializerList) {
639 const int a[5] = { 1, 2, 3, 4, 5 };
640 EXPECT_THAT(a, ElementsAreArray({ 1, 2, 3, 4, 5 }));
641 EXPECT_THAT(a, Not(ElementsAreArray({ 1, 2, 3, 5, 4 })));
642 EXPECT_THAT(a, Not(ElementsAreArray({ 1, 2, 3, 4, 6 })));
643}
644
645TEST(ElementsAreArrayTest, TakesInitializerListOfCStrings) {
646 const string a[5] = { "a", "b", "c", "d", "e" };
647 EXPECT_THAT(a, ElementsAreArray({ "a", "b", "c", "d", "e" }));
648 EXPECT_THAT(a, Not(ElementsAreArray({ "a", "b", "c", "e", "d" })));
649 EXPECT_THAT(a, Not(ElementsAreArray({ "a", "b", "c", "d", "ef" })));
650}
651
652TEST(ElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {
653 const int a[5] = { 1, 2, 3, 4, 5 };
654 EXPECT_THAT(a, ElementsAreArray(
655 { Eq(1), Eq(2), Eq(3), Eq(4), Eq(5) }));
656 EXPECT_THAT(a, Not(ElementsAreArray(
657 { Eq(1), Eq(2), Eq(3), Eq(4), Eq(6) })));
658}
659
660TEST(ElementsAreArrayTest,
661 TakesInitializerListOfDifferentTypedMatchers) {
662 const int a[5] = { 1, 2, 3, 4, 5 };
663 // The compiler cannot infer the type of the initializer list if its
664 // elements have different types. We must explicitly specify the
665 // unified element type in this case.
666 EXPECT_THAT(a, ElementsAreArray<Matcher<int> >(
667 { Eq(1), Ne(-2), Ge(3), Le(4), Eq(5) }));
668 EXPECT_THAT(a, Not(ElementsAreArray<Matcher<int> >(
669 { Eq(1), Ne(-2), Ge(3), Le(4), Eq(6) })));
670}
671
672#endif // GTEST_LANG_CXX11
673
jgm38513a82012-11-15 15:50:36 +0000674TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherVector) {
675 const int a[] = { 1, 2, 3 };
676 const Matcher<int> kMatchers[] = { Eq(1), Eq(2), Eq(3) };
677 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
678 const vector<Matcher<int> > expected(
679 kMatchers, kMatchers + GMOCK_ARRAY_SIZE_(kMatchers));
680 EXPECT_THAT(test_vector, ElementsAreArray(expected));
681 test_vector.push_back(4);
682 EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));
683}
684
685TEST(ElementsAreArrayTest, CanBeCreatedWithIteratorRange) {
686 const int a[] = { 1, 2, 3 };
687 const vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
688 const vector<int> expected(a, a + GMOCK_ARRAY_SIZE_(a));
689 EXPECT_THAT(test_vector, ElementsAreArray(expected.begin(), expected.end()));
690 // Pointers are iterators, too.
691 EXPECT_THAT(test_vector, ElementsAreArray(a, a + GMOCK_ARRAY_SIZE_(a)));
692 // The empty range of NULL pointers should also be okay.
693 int* const null_int = NULL;
694 EXPECT_THAT(test_vector, Not(ElementsAreArray(null_int, null_int)));
695 EXPECT_THAT((vector<int>()), ElementsAreArray(null_int, null_int));
696}
697
zhanyong.wanb8243162009-06-04 05:48:20 +0000698// Since ElementsAre() and ElementsAreArray() share much of the
699// implementation, we only do a sanity test for native arrays here.
700TEST(ElementsAreArrayTest, WorksWithNativeArray) {
701 ::std::string a[] = { "hi", "ho" };
702 ::std::string b[] = { "hi", "ho" };
703
704 EXPECT_THAT(a, ElementsAreArray(b));
705 EXPECT_THAT(a, ElementsAreArray(b, 2));
706 EXPECT_THAT(a, Not(ElementsAreArray(b, 1)));
707}
708
jgm38513a82012-11-15 15:50:36 +0000709TEST(ElementsAreArrayTest, SourceLifeSpan) {
710 const int a[] = { 1, 2, 3 };
711 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
712 vector<int> expect(a, a + GMOCK_ARRAY_SIZE_(a));
713 ElementsAreArrayMatcher<int> matcher_maker =
714 ElementsAreArray(expect.begin(), expect.end());
715 EXPECT_THAT(test_vector, matcher_maker);
716 // Changing in place the values that initialized matcher_maker should not
717 // affect matcher_maker anymore. It should have made its own copy of them.
718 typedef vector<int>::iterator Iter;
719 for (Iter it = expect.begin(); it != expect.end(); ++it) { *it += 10; }
720 EXPECT_THAT(test_vector, matcher_maker);
721 test_vector.push_back(3);
722 EXPECT_THAT(test_vector, Not(matcher_maker));
723}
724
zhanyong.wance198ff2009-02-12 01:34:27 +0000725// Tests for the MATCHER*() macro family.
726
727// Tests that a simple MATCHER() definition works.
728
729MATCHER(IsEven, "") { return (arg % 2) == 0; }
730
731TEST(MatcherMacroTest, Works) {
732 const Matcher<int> m = IsEven();
733 EXPECT_TRUE(m.Matches(6));
734 EXPECT_FALSE(m.Matches(7));
735
736 EXPECT_EQ("is even", Describe(m));
737 EXPECT_EQ("not (is even)", DescribeNegation(m));
738 EXPECT_EQ("", Explain(m, 6));
739 EXPECT_EQ("", Explain(m, 7));
740}
741
zhanyong.wanb4140802010-06-08 22:53:57 +0000742// This also tests that the description string can reference 'negation'.
743MATCHER(IsEven2, negation ? "is odd" : "is even") {
zhanyong.wan82113312010-01-08 21:55:40 +0000744 if ((arg % 2) == 0) {
745 // Verifies that we can stream to result_listener, a listener
746 // supplied by the MATCHER macro implicitly.
747 *result_listener << "OK";
748 return true;
749 } else {
750 *result_listener << "% 2 == " << (arg % 2);
751 return false;
752 }
753}
754
zhanyong.wanb4140802010-06-08 22:53:57 +0000755// This also tests that the description string can reference matcher
756// parameters.
757MATCHER_P2(EqSumOf, x, y,
758 string(negation ? "doesn't equal" : "equals") + " the sum of " +
759 PrintToString(x) + " and " + PrintToString(y)) {
zhanyong.wan82113312010-01-08 21:55:40 +0000760 if (arg == (x + y)) {
761 *result_listener << "OK";
762 return true;
763 } else {
764 // Verifies that we can stream to the underlying stream of
765 // result_listener.
766 if (result_listener->stream() != NULL) {
767 *result_listener->stream() << "diff == " << (x + y - arg);
768 }
769 return false;
770 }
771}
772
zhanyong.wanb4140802010-06-08 22:53:57 +0000773// Tests that the matcher description can reference 'negation' and the
774// matcher parameters.
775TEST(MatcherMacroTest, DescriptionCanReferenceNegationAndParameters) {
776 const Matcher<int> m1 = IsEven2();
777 EXPECT_EQ("is even", Describe(m1));
778 EXPECT_EQ("is odd", DescribeNegation(m1));
779
780 const Matcher<int> m2 = EqSumOf(5, 9);
781 EXPECT_EQ("equals the sum of 5 and 9", Describe(m2));
782 EXPECT_EQ("doesn't equal the sum of 5 and 9", DescribeNegation(m2));
783}
784
785// Tests explaining match result in a MATCHER* macro.
zhanyong.wan82113312010-01-08 21:55:40 +0000786TEST(MatcherMacroTest, CanExplainMatchResult) {
787 const Matcher<int> m1 = IsEven2();
788 EXPECT_EQ("OK", Explain(m1, 4));
789 EXPECT_EQ("% 2 == 1", Explain(m1, 5));
790
791 const Matcher<int> m2 = EqSumOf(1, 2);
792 EXPECT_EQ("OK", Explain(m2, 3));
793 EXPECT_EQ("diff == -1", Explain(m2, 4));
794}
795
zhanyong.wance198ff2009-02-12 01:34:27 +0000796// Tests that the body of MATCHER() can reference the type of the
797// value being matched.
798
799MATCHER(IsEmptyString, "") {
800 StaticAssertTypeEq< ::std::string, arg_type>();
801 return arg == "";
802}
803
804MATCHER(IsEmptyStringByRef, "") {
805 StaticAssertTypeEq<const ::std::string&, arg_type>();
806 return arg == "";
807}
808
809TEST(MatcherMacroTest, CanReferenceArgType) {
810 const Matcher< ::std::string> m1 = IsEmptyString();
811 EXPECT_TRUE(m1.Matches(""));
812
813 const Matcher<const ::std::string&> m2 = IsEmptyStringByRef();
814 EXPECT_TRUE(m2.Matches(""));
815}
816
817// Tests that MATCHER() can be used in a namespace.
818
819namespace matcher_test {
820MATCHER(IsOdd, "") { return (arg % 2) != 0; }
821} // namespace matcher_test
822
zhanyong.wanb8243162009-06-04 05:48:20 +0000823TEST(MatcherMacroTest, WorksInNamespace) {
zhanyong.wance198ff2009-02-12 01:34:27 +0000824 Matcher<int> m = matcher_test::IsOdd();
825 EXPECT_FALSE(m.Matches(4));
826 EXPECT_TRUE(m.Matches(5));
827}
828
zhanyong.wanb8243162009-06-04 05:48:20 +0000829// Tests that Value() can be used to compose matchers.
830MATCHER(IsPositiveOdd, "") {
831 return Value(arg, matcher_test::IsOdd()) && arg > 0;
832}
833
834TEST(MatcherMacroTest, CanBeComposedUsingValue) {
835 EXPECT_THAT(3, IsPositiveOdd());
836 EXPECT_THAT(4, Not(IsPositiveOdd()));
837 EXPECT_THAT(-1, Not(IsPositiveOdd()));
838}
839
zhanyong.wance198ff2009-02-12 01:34:27 +0000840// Tests that a simple MATCHER_P() definition works.
841
842MATCHER_P(IsGreaterThan32And, n, "") { return arg > 32 && arg > n; }
843
844TEST(MatcherPMacroTest, Works) {
845 const Matcher<int> m = IsGreaterThan32And(5);
846 EXPECT_TRUE(m.Matches(36));
847 EXPECT_FALSE(m.Matches(5));
848
849 EXPECT_EQ("is greater than 32 and 5", Describe(m));
850 EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m));
851 EXPECT_EQ("", Explain(m, 36));
852 EXPECT_EQ("", Explain(m, 5));
853}
854
zhanyong.wance198ff2009-02-12 01:34:27 +0000855// Tests that the description is calculated correctly from the matcher name.
856MATCHER_P(_is_Greater_Than32and_, n, "") { return arg > 32 && arg > n; }
857
858TEST(MatcherPMacroTest, GeneratesCorrectDescription) {
859 const Matcher<int> m = _is_Greater_Than32and_(5);
860
861 EXPECT_EQ("is greater than 32 and 5", Describe(m));
862 EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m));
863 EXPECT_EQ("", Explain(m, 36));
864 EXPECT_EQ("", Explain(m, 5));
865}
866
867// Tests that a MATCHER_P matcher can be explicitly instantiated with
868// a reference parameter type.
869
870class UncopyableFoo {
871 public:
872 explicit UncopyableFoo(char value) : value_(value) {}
873 private:
874 UncopyableFoo(const UncopyableFoo&);
875 void operator=(const UncopyableFoo&);
876
877 char value_;
878};
879
880MATCHER_P(ReferencesUncopyable, variable, "") { return &arg == &variable; }
881
882TEST(MatcherPMacroTest, WorksWhenExplicitlyInstantiatedWithReference) {
883 UncopyableFoo foo1('1'), foo2('2');
884 const Matcher<const UncopyableFoo&> m =
885 ReferencesUncopyable<const UncopyableFoo&>(foo1);
886
887 EXPECT_TRUE(m.Matches(foo1));
888 EXPECT_FALSE(m.Matches(foo2));
889
890 // We don't want the address of the parameter printed, as most
891 // likely it will just annoy the user. If the address is
892 // interesting, the user should consider passing the parameter by
893 // pointer instead.
894 EXPECT_EQ("references uncopyable 1-byte object <31>", Describe(m));
895}
896
897
zhanyong.wance198ff2009-02-12 01:34:27 +0000898// Tests that the body of MATCHER_Pn() can reference the parameter
899// types.
900
901MATCHER_P3(ParamTypesAreIntLongAndChar, foo, bar, baz, "") {
902 StaticAssertTypeEq<int, foo_type>();
903 StaticAssertTypeEq<long, bar_type>(); // NOLINT
904 StaticAssertTypeEq<char, baz_type>();
905 return arg == 0;
906}
907
908TEST(MatcherPnMacroTest, CanReferenceParamTypes) {
909 EXPECT_THAT(0, ParamTypesAreIntLongAndChar(10, 20L, 'a'));
910}
911
912// Tests that a MATCHER_Pn matcher can be explicitly instantiated with
913// reference parameter types.
914
915MATCHER_P2(ReferencesAnyOf, variable1, variable2, "") {
916 return &arg == &variable1 || &arg == &variable2;
917}
918
919TEST(MatcherPnMacroTest, WorksWhenExplicitlyInstantiatedWithReferences) {
920 UncopyableFoo foo1('1'), foo2('2'), foo3('3');
921 const Matcher<const UncopyableFoo&> m =
922 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
923
924 EXPECT_TRUE(m.Matches(foo1));
925 EXPECT_TRUE(m.Matches(foo2));
926 EXPECT_FALSE(m.Matches(foo3));
927}
928
929TEST(MatcherPnMacroTest,
930 GeneratesCorretDescriptionWhenExplicitlyInstantiatedWithReferences) {
931 UncopyableFoo foo1('1'), foo2('2');
932 const Matcher<const UncopyableFoo&> m =
933 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
934
935 // We don't want the addresses of the parameters printed, as most
936 // likely they will just annoy the user. If the addresses are
937 // interesting, the user should consider passing the parameters by
938 // pointers instead.
939 EXPECT_EQ("references any of (1-byte object <31>, 1-byte object <32>)",
940 Describe(m));
941}
942
943// Tests that a simple MATCHER_P2() definition works.
944
945MATCHER_P2(IsNotInClosedRange, low, hi, "") { return arg < low || arg > hi; }
946
947TEST(MatcherPnMacroTest, Works) {
948 const Matcher<const long&> m = IsNotInClosedRange(10, 20); // NOLINT
949 EXPECT_TRUE(m.Matches(36L));
950 EXPECT_FALSE(m.Matches(15L));
951
952 EXPECT_EQ("is not in closed range (10, 20)", Describe(m));
953 EXPECT_EQ("not (is not in closed range (10, 20))", DescribeNegation(m));
954 EXPECT_EQ("", Explain(m, 36L));
955 EXPECT_EQ("", Explain(m, 15L));
956}
957
958// Tests that MATCHER*() definitions can be overloaded on the number
959// of parameters; also tests MATCHER_Pn() where n >= 3.
960
961MATCHER(EqualsSumOf, "") { return arg == 0; }
962MATCHER_P(EqualsSumOf, a, "") { return arg == a; }
963MATCHER_P2(EqualsSumOf, a, b, "") { return arg == a + b; }
964MATCHER_P3(EqualsSumOf, a, b, c, "") { return arg == a + b + c; }
965MATCHER_P4(EqualsSumOf, a, b, c, d, "") { return arg == a + b + c + d; }
966MATCHER_P5(EqualsSumOf, a, b, c, d, e, "") { return arg == a + b + c + d + e; }
967MATCHER_P6(EqualsSumOf, a, b, c, d, e, f, "") {
968 return arg == a + b + c + d + e + f;
969}
970MATCHER_P7(EqualsSumOf, a, b, c, d, e, f, g, "") {
971 return arg == a + b + c + d + e + f + g;
972}
973MATCHER_P8(EqualsSumOf, a, b, c, d, e, f, g, h, "") {
974 return arg == a + b + c + d + e + f + g + h;
975}
976MATCHER_P9(EqualsSumOf, a, b, c, d, e, f, g, h, i, "") {
977 return arg == a + b + c + d + e + f + g + h + i;
978}
979MATCHER_P10(EqualsSumOf, a, b, c, d, e, f, g, h, i, j, "") {
980 return arg == a + b + c + d + e + f + g + h + i + j;
981}
982
983TEST(MatcherPnMacroTest, CanBeOverloadedOnNumberOfParameters) {
984 EXPECT_THAT(0, EqualsSumOf());
985 EXPECT_THAT(1, EqualsSumOf(1));
986 EXPECT_THAT(12, EqualsSumOf(10, 2));
987 EXPECT_THAT(123, EqualsSumOf(100, 20, 3));
988 EXPECT_THAT(1234, EqualsSumOf(1000, 200, 30, 4));
989 EXPECT_THAT(12345, EqualsSumOf(10000, 2000, 300, 40, 5));
990 EXPECT_THAT("abcdef",
991 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f'));
992 EXPECT_THAT("abcdefg",
993 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g'));
994 EXPECT_THAT("abcdefgh",
995 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
996 "h"));
997 EXPECT_THAT("abcdefghi",
998 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
999 "h", 'i'));
1000 EXPECT_THAT("abcdefghij",
1001 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
1002 "h", 'i', ::std::string("j")));
1003
1004 EXPECT_THAT(1, Not(EqualsSumOf()));
1005 EXPECT_THAT(-1, Not(EqualsSumOf(1)));
1006 EXPECT_THAT(-12, Not(EqualsSumOf(10, 2)));
1007 EXPECT_THAT(-123, Not(EqualsSumOf(100, 20, 3)));
1008 EXPECT_THAT(-1234, Not(EqualsSumOf(1000, 200, 30, 4)));
1009 EXPECT_THAT(-12345, Not(EqualsSumOf(10000, 2000, 300, 40, 5)));
1010 EXPECT_THAT("abcdef ",
1011 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f')));
1012 EXPECT_THAT("abcdefg ",
1013 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f',
1014 'g')));
1015 EXPECT_THAT("abcdefgh ",
1016 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
1017 "h")));
1018 EXPECT_THAT("abcdefghi ",
1019 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
1020 "h", 'i')));
1021 EXPECT_THAT("abcdefghij ",
1022 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
1023 "h", 'i', ::std::string("j"))));
1024}
1025
1026// Tests that a MATCHER_Pn() definition can be instantiated with any
1027// compatible parameter types.
1028TEST(MatcherPnMacroTest, WorksForDifferentParameterTypes) {
1029 EXPECT_THAT(123, EqualsSumOf(100L, 20, static_cast<char>(3)));
1030 EXPECT_THAT("abcd", EqualsSumOf(::std::string("a"), "b", 'c', "d"));
1031
1032 EXPECT_THAT(124, Not(EqualsSumOf(100L, 20, static_cast<char>(3))));
1033 EXPECT_THAT("abcde", Not(EqualsSumOf(::std::string("a"), "b", 'c', "d")));
1034}
1035
1036// Tests that the matcher body can promote the parameter types.
1037
1038MATCHER_P2(EqConcat, prefix, suffix, "") {
1039 // The following lines promote the two parameters to desired types.
1040 std::string prefix_str(prefix);
zhanyong.wan32de5f52009-12-23 00:13:23 +00001041 char suffix_char = static_cast<char>(suffix);
zhanyong.wance198ff2009-02-12 01:34:27 +00001042 return arg == prefix_str + suffix_char;
1043}
1044
1045TEST(MatcherPnMacroTest, SimpleTypePromotion) {
1046 Matcher<std::string> no_promo =
1047 EqConcat(std::string("foo"), 't');
1048 Matcher<const std::string&> promo =
1049 EqConcat("foo", static_cast<int>('t'));
1050 EXPECT_FALSE(no_promo.Matches("fool"));
1051 EXPECT_FALSE(promo.Matches("fool"));
1052 EXPECT_TRUE(no_promo.Matches("foot"));
1053 EXPECT_TRUE(promo.Matches("foot"));
1054}
1055
1056// Verifies the type of a MATCHER*.
1057
1058TEST(MatcherPnMacroTest, TypesAreCorrect) {
1059 // EqualsSumOf() must be assignable to a EqualsSumOfMatcher variable.
1060 EqualsSumOfMatcher a0 = EqualsSumOf();
1061
1062 // EqualsSumOf(1) must be assignable to a EqualsSumOfMatcherP variable.
1063 EqualsSumOfMatcherP<int> a1 = EqualsSumOf(1);
1064
1065 // EqualsSumOf(p1, ..., pk) must be assignable to a EqualsSumOfMatcherPk
1066 // variable, and so on.
1067 EqualsSumOfMatcherP2<int, char> a2 = EqualsSumOf(1, '2');
1068 EqualsSumOfMatcherP3<int, int, char> a3 = EqualsSumOf(1, 2, '3');
1069 EqualsSumOfMatcherP4<int, int, int, char> a4 = EqualsSumOf(1, 2, 3, '4');
1070 EqualsSumOfMatcherP5<int, int, int, int, char> a5 =
1071 EqualsSumOf(1, 2, 3, 4, '5');
1072 EqualsSumOfMatcherP6<int, int, int, int, int, char> a6 =
1073 EqualsSumOf(1, 2, 3, 4, 5, '6');
1074 EqualsSumOfMatcherP7<int, int, int, int, int, int, char> a7 =
1075 EqualsSumOf(1, 2, 3, 4, 5, 6, '7');
1076 EqualsSumOfMatcherP8<int, int, int, int, int, int, int, char> a8 =
1077 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, '8');
1078 EqualsSumOfMatcherP9<int, int, int, int, int, int, int, int, char> a9 =
1079 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, '9');
1080 EqualsSumOfMatcherP10<int, int, int, int, int, int, int, int, int, char> a10 =
1081 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, 9, '0');
zhanyong.wan29be9232013-03-01 06:53:35 +00001082
1083 // Avoid "unused variable" warnings.
1084 (void)a0;
1085 (void)a1;
1086 (void)a2;
1087 (void)a3;
1088 (void)a4;
1089 (void)a5;
1090 (void)a6;
1091 (void)a7;
1092 (void)a8;
1093 (void)a9;
1094 (void)a10;
zhanyong.wance198ff2009-02-12 01:34:27 +00001095}
1096
zhanyong.wanb8243162009-06-04 05:48:20 +00001097// Tests that matcher-typed parameters can be used in Value() inside a
1098// MATCHER_Pn definition.
1099
1100// Succeeds if arg matches exactly 2 of the 3 matchers.
1101MATCHER_P3(TwoOf, m1, m2, m3, "") {
1102 const int count = static_cast<int>(Value(arg, m1))
1103 + static_cast<int>(Value(arg, m2)) + static_cast<int>(Value(arg, m3));
1104 return count == 2;
1105}
1106
1107TEST(MatcherPnMacroTest, CanUseMatcherTypedParameterInValue) {
1108 EXPECT_THAT(42, TwoOf(Gt(0), Lt(50), Eq(10)));
1109 EXPECT_THAT(0, Not(TwoOf(Gt(-1), Lt(1), Eq(0))));
1110}
1111
1112// Tests Contains().
1113
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001114TEST(ContainsTest, ListMatchesWhenElementIsInContainer) {
1115 list<int> some_list;
1116 some_list.push_back(3);
1117 some_list.push_back(1);
1118 some_list.push_back(2);
1119 EXPECT_THAT(some_list, Contains(1));
zhanyong.wanb8243162009-06-04 05:48:20 +00001120 EXPECT_THAT(some_list, Contains(Gt(2.5)));
1121 EXPECT_THAT(some_list, Contains(Eq(2.0f)));
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001122
1123 list<string> another_list;
1124 another_list.push_back("fee");
1125 another_list.push_back("fie");
1126 another_list.push_back("foe");
1127 another_list.push_back("fum");
1128 EXPECT_THAT(another_list, Contains(string("fee")));
1129}
1130
1131TEST(ContainsTest, ListDoesNotMatchWhenElementIsNotInContainer) {
1132 list<int> some_list;
1133 some_list.push_back(3);
1134 some_list.push_back(1);
1135 EXPECT_THAT(some_list, Not(Contains(4)));
1136}
1137
1138TEST(ContainsTest, SetMatchesWhenElementIsInContainer) {
1139 set<int> some_set;
1140 some_set.insert(3);
1141 some_set.insert(1);
1142 some_set.insert(2);
zhanyong.wanb8243162009-06-04 05:48:20 +00001143 EXPECT_THAT(some_set, Contains(Eq(1.0)));
1144 EXPECT_THAT(some_set, Contains(Eq(3.0f)));
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001145 EXPECT_THAT(some_set, Contains(2));
1146
1147 set<const char*> another_set;
1148 another_set.insert("fee");
1149 another_set.insert("fie");
1150 another_set.insert("foe");
1151 another_set.insert("fum");
zhanyong.wanb8243162009-06-04 05:48:20 +00001152 EXPECT_THAT(another_set, Contains(Eq(string("fum"))));
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001153}
1154
1155TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) {
1156 set<int> some_set;
1157 some_set.insert(3);
1158 some_set.insert(1);
1159 EXPECT_THAT(some_set, Not(Contains(4)));
1160
1161 set<const char*> c_string_set;
1162 c_string_set.insert("hello");
1163 EXPECT_THAT(c_string_set, Not(Contains(string("hello").c_str())));
1164}
1165
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001166TEST(ContainsTest, ExplainsMatchResultCorrectly) {
zhanyong.wanb8243162009-06-04 05:48:20 +00001167 const int a[2] = { 1, 2 };
jgm38513a82012-11-15 15:50:36 +00001168 Matcher<const int (&)[2]> m = Contains(2);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001169 EXPECT_EQ("whose element #1 matches", Explain(m, a));
zhanyong.wanb8243162009-06-04 05:48:20 +00001170
1171 m = Contains(3);
1172 EXPECT_EQ("", Explain(m, a));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001173
1174 m = Contains(GreaterThan(0));
1175 EXPECT_EQ("whose element #0 matches, which is 1 more than 0", Explain(m, a));
1176
1177 m = Contains(GreaterThan(10));
1178 EXPECT_EQ("", Explain(m, a));
zhanyong.wanb8243162009-06-04 05:48:20 +00001179}
1180
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001181TEST(ContainsTest, DescribesItselfCorrectly) {
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001182 Matcher<vector<int> > m = Contains(1);
zhanyong.wanb8243162009-06-04 05:48:20 +00001183 EXPECT_EQ("contains at least one element that is equal to 1", Describe(m));
1184
1185 Matcher<vector<int> > m2 = Not(m);
1186 EXPECT_EQ("doesn't contain any element that is equal to 1", Describe(m2));
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001187}
1188
1189TEST(ContainsTest, MapMatchesWhenElementIsInContainer) {
1190 map<const char*, int> my_map;
1191 const char* bar = "a string";
1192 my_map[bar] = 2;
1193 EXPECT_THAT(my_map, Contains(pair<const char* const, int>(bar, 2)));
1194
1195 map<string, int> another_map;
1196 another_map["fee"] = 1;
1197 another_map["fie"] = 2;
1198 another_map["foe"] = 3;
1199 another_map["fum"] = 4;
1200 EXPECT_THAT(another_map, Contains(pair<const string, int>(string("fee"), 1)));
1201 EXPECT_THAT(another_map, Contains(pair<const string, int>("fie", 2)));
1202}
1203
1204TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) {
1205 map<int, int> some_map;
1206 some_map[1] = 11;
1207 some_map[2] = 22;
1208 EXPECT_THAT(some_map, Not(Contains(pair<const int, int>(2, 23))));
1209}
1210
1211TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) {
1212 const char* string_array[] = { "fee", "fie", "foe", "fum" };
zhanyong.wanb8243162009-06-04 05:48:20 +00001213 EXPECT_THAT(string_array, Contains(Eq(string("fum"))));
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001214}
1215
1216TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) {
1217 int int_array[] = { 1, 2, 3, 4 };
1218 EXPECT_THAT(int_array, Not(Contains(5)));
1219}
1220
zhanyong.wanb8243162009-06-04 05:48:20 +00001221TEST(ContainsTest, AcceptsMatcher) {
1222 const int a[] = { 1, 2, 3 };
1223 EXPECT_THAT(a, Contains(Gt(2)));
1224 EXPECT_THAT(a, Not(Contains(Gt(4))));
1225}
1226
1227TEST(ContainsTest, WorksForNativeArrayAsTuple) {
1228 const int a[] = { 1, 2 };
zhanyong.wan2661c682009-06-09 05:42:12 +00001229 const int* const pointer = a;
1230 EXPECT_THAT(make_tuple(pointer, 2), Contains(1));
1231 EXPECT_THAT(make_tuple(pointer, 2), Not(Contains(Gt(3))));
zhanyong.wanb8243162009-06-04 05:48:20 +00001232}
1233
1234TEST(ContainsTest, WorksForTwoDimensionalNativeArray) {
1235 int a[][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
1236 EXPECT_THAT(a, Contains(ElementsAre(4, 5, 6)));
1237 EXPECT_THAT(a, Contains(Contains(5)));
1238 EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5))));
1239 EXPECT_THAT(a, Contains(Not(Contains(5))));
1240}
1241
jgm79a367e2012-04-10 16:02:11 +00001242TEST(AllOfTest, HugeMatcher) {
1243 // Verify that using AllOf with many arguments doesn't cause
1244 // the compiler to exceed template instantiation depth limit.
1245 EXPECT_THAT(0, testing::AllOf(_, _, _, _, _, _, _, _, _,
1246 testing::AllOf(_, _, _, _, _, _, _, _, _, _)));
1247}
1248
1249TEST(AnyOfTest, HugeMatcher) {
1250 // Verify that using AnyOf with many arguments doesn't cause
1251 // the compiler to exceed template instantiation depth limit.
1252 EXPECT_THAT(0, testing::AnyOf(_, _, _, _, _, _, _, _, _,
1253 testing::AnyOf(_, _, _, _, _, _, _, _, _, _)));
1254}
1255
zhanyong.wan86d2eeb2011-03-16 17:10:39 +00001256namespace adl_test {
1257
1258// Verifies that the implementation of ::testing::AllOf and ::testing::AnyOf
1259// don't issue unqualified recursive calls. If they do, the argument dependent
1260// name lookup will cause AllOf/AnyOf in the 'adl_test' namespace to be found
1261// as a candidate and the compilation will break due to an ambiguous overload.
1262
1263// The matcher must be in the same namespace as AllOf/AnyOf to make argument
1264// dependent lookup find those.
1265MATCHER(M, "") { return true; }
1266
1267template <typename T1, typename T2>
1268bool AllOf(const T1& t1, const T2& t2) { return true; }
1269
1270TEST(AllOfTest, DoesNotCallAllOfUnqualified) {
1271 EXPECT_THAT(42, testing::AllOf(
1272 M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
1273}
1274
1275template <typename T1, typename T2> bool
1276AnyOf(const T1& t1, const T2& t2) { return true; }
1277
1278TEST(AnyOfTest, DoesNotCallAnyOfUnqualified) {
1279 EXPECT_THAT(42, testing::AnyOf(
1280 M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
1281}
1282
1283} // namespace adl_test
1284
zhanyong.wan82113312010-01-08 21:55:40 +00001285#ifdef _MSC_VER
zhanyong.wan658ac0b2011-02-24 07:29:13 +00001286# pragma warning(pop)
zhanyong.wan82113312010-01-08 21:55:40 +00001287#endif
1288
shiqiane35fdd92008-12-10 05:08:54 +00001289} // namespace