blob: e43781b64fe732357510c3d4464afd2c2ac0e733 [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
zhanyong.wanfb25d532013-07-28 08:24:00 +000083// Evaluates to the number of elements in 'array'.
84#define GMOCK_ARRAY_SIZE_(a) (sizeof(a) / sizeof(a[0]))
85
shiqiane35fdd92008-12-10 05:08:54 +000086// Returns the description of the given matcher.
87template <typename T>
88string Describe(const Matcher<T>& m) {
89 stringstream ss;
90 m.DescribeTo(&ss);
91 return ss.str();
92}
93
94// Returns the description of the negation of the given matcher.
95template <typename T>
96string DescribeNegation(const Matcher<T>& m) {
97 stringstream ss;
98 m.DescribeNegationTo(&ss);
99 return ss.str();
100}
101
102// Returns the reason why x matches, or doesn't match, m.
103template <typename MatcherType, typename Value>
104string Explain(const MatcherType& m, const Value& x) {
105 stringstream ss;
106 m.ExplainMatchResultTo(x, &ss);
107 return ss.str();
108}
109
zhanyong.wan2661c682009-06-09 05:42:12 +0000110// Tests Args<k0, ..., kn>(m).
111
112TEST(ArgsTest, AcceptsZeroTemplateArg) {
113 const tuple<int, bool> t(5, true);
114 EXPECT_THAT(t, Args<>(Eq(tuple<>())));
115 EXPECT_THAT(t, Not(Args<>(Ne(tuple<>()))));
116}
117
118TEST(ArgsTest, AcceptsOneTemplateArg) {
119 const tuple<int, bool> t(5, true);
120 EXPECT_THAT(t, Args<0>(Eq(make_tuple(5))));
121 EXPECT_THAT(t, Args<1>(Eq(make_tuple(true))));
122 EXPECT_THAT(t, Not(Args<1>(Eq(make_tuple(false)))));
123}
124
125TEST(ArgsTest, AcceptsTwoTemplateArgs) {
126 const tuple<short, int, long> t(4, 5, 6L); // NOLINT
127
128 EXPECT_THAT(t, (Args<0, 1>(Lt())));
129 EXPECT_THAT(t, (Args<1, 2>(Lt())));
130 EXPECT_THAT(t, Not(Args<0, 2>(Gt())));
131}
132
133TEST(ArgsTest, AcceptsRepeatedTemplateArgs) {
134 const tuple<short, int, long> t(4, 5, 6L); // NOLINT
135 EXPECT_THAT(t, (Args<0, 0>(Eq())));
136 EXPECT_THAT(t, Not(Args<1, 1>(Ne())));
137}
138
139TEST(ArgsTest, AcceptsDecreasingTemplateArgs) {
140 const tuple<short, int, long> t(4, 5, 6L); // NOLINT
141 EXPECT_THAT(t, (Args<2, 0>(Gt())));
142 EXPECT_THAT(t, Not(Args<2, 1>(Lt())));
143}
144
zhanyong.wan82113312010-01-08 21:55:40 +0000145// The MATCHER*() macros trigger warning C4100 (unreferenced formal
146// parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
147// the macro definition, as the warnings are generated when the macro
148// is expanded and macro expansion cannot contain #pragma. Therefore
149// we suppress them here.
150#ifdef _MSC_VER
zhanyong.wan658ac0b2011-02-24 07:29:13 +0000151# pragma warning(push)
152# pragma warning(disable:4100)
zhanyong.wan82113312010-01-08 21:55:40 +0000153#endif
154
zhanyong.wan2661c682009-06-09 05:42:12 +0000155MATCHER(SumIsZero, "") {
156 return get<0>(arg) + get<1>(arg) + get<2>(arg) == 0;
157}
158
159TEST(ArgsTest, AcceptsMoreTemplateArgsThanArityOfOriginalTuple) {
160 EXPECT_THAT(make_tuple(-1, 2), (Args<0, 0, 1>(SumIsZero())));
161 EXPECT_THAT(make_tuple(1, 2), Not(Args<0, 0, 1>(SumIsZero())));
162}
163
164TEST(ArgsTest, CanBeNested) {
165 const tuple<short, int, long, int> t(4, 5, 6L, 6); // NOLINT
166 EXPECT_THAT(t, (Args<1, 2, 3>(Args<1, 2>(Eq()))));
167 EXPECT_THAT(t, (Args<0, 1, 3>(Args<0, 2>(Lt()))));
168}
169
170TEST(ArgsTest, CanMatchTupleByValue) {
171 typedef tuple<char, int, int> Tuple3;
172 const Matcher<Tuple3> m = Args<1, 2>(Lt());
173 EXPECT_TRUE(m.Matches(Tuple3('a', 1, 2)));
174 EXPECT_FALSE(m.Matches(Tuple3('b', 2, 2)));
175}
176
177TEST(ArgsTest, CanMatchTupleByReference) {
178 typedef tuple<char, char, int> Tuple3;
179 const Matcher<const Tuple3&> m = Args<0, 1>(Lt());
180 EXPECT_TRUE(m.Matches(Tuple3('a', 'b', 2)));
181 EXPECT_FALSE(m.Matches(Tuple3('b', 'b', 2)));
182}
183
184// Validates that arg is printed as str.
185MATCHER_P(PrintsAs, str, "") {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000186 return testing::PrintToString(arg) == str;
zhanyong.wan2661c682009-06-09 05:42:12 +0000187}
188
189TEST(ArgsTest, AcceptsTenTemplateArgs) {
190 EXPECT_THAT(make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
191 (Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
192 PrintsAs("(9, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
193 EXPECT_THAT(make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
194 Not(Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
195 PrintsAs("(0, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
196}
197
198TEST(ArgsTest, DescirbesSelfCorrectly) {
199 const Matcher<tuple<int, bool, char> > m = Args<2, 0>(Lt());
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000200 EXPECT_EQ("are a tuple whose fields (#2, #0) are a pair where "
201 "the first < the second",
zhanyong.wan2661c682009-06-09 05:42:12 +0000202 Describe(m));
203}
204
205TEST(ArgsTest, DescirbesNestedArgsCorrectly) {
206 const Matcher<const tuple<int, bool, char, int>&> m =
207 Args<0, 2, 3>(Args<2, 0>(Lt()));
208 EXPECT_EQ("are a tuple whose fields (#0, #2, #3) are a tuple "
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000209 "whose fields (#2, #0) are a pair where the first < the second",
zhanyong.wan2661c682009-06-09 05:42:12 +0000210 Describe(m));
211}
212
213TEST(ArgsTest, DescribesNegationCorrectly) {
214 const Matcher<tuple<int, char> > m = Args<1, 0>(Gt());
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000215 EXPECT_EQ("are a tuple whose fields (#1, #0) aren't a pair "
216 "where the first > the second",
zhanyong.wan2661c682009-06-09 05:42:12 +0000217 DescribeNegation(m));
218}
219
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000220TEST(ArgsTest, ExplainsMatchResultWithoutInnerExplanation) {
221 const Matcher<tuple<bool, int, int> > m = Args<1, 2>(Eq());
222 EXPECT_EQ("whose fields (#1, #2) are (42, 42)",
223 Explain(m, make_tuple(false, 42, 42)));
224 EXPECT_EQ("whose fields (#1, #2) are (42, 43)",
225 Explain(m, make_tuple(false, 42, 43)));
226}
227
228// For testing Args<>'s explanation.
229class LessThanMatcher : public MatcherInterface<tuple<char, int> > {
230 public:
231 virtual void DescribeTo(::std::ostream* os) const {}
232
233 virtual bool MatchAndExplain(tuple<char, int> value,
234 MatchResultListener* listener) const {
235 const int diff = get<0>(value) - get<1>(value);
236 if (diff > 0) {
237 *listener << "where the first value is " << diff
238 << " more than the second";
239 }
240 return diff < 0;
241 }
242};
243
244Matcher<tuple<char, int> > LessThan() {
245 return MakeMatcher(new LessThanMatcher);
246}
247
248TEST(ArgsTest, ExplainsMatchResultWithInnerExplanation) {
249 const Matcher<tuple<char, int, int> > m = Args<0, 2>(LessThan());
zhanyong.wand60c5f42010-07-21 22:21:07 +0000250 EXPECT_EQ("whose fields (#0, #2) are ('a' (97, 0x61), 42), "
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000251 "where the first value is 55 more than the second",
252 Explain(m, make_tuple('a', 42, 42)));
253 EXPECT_EQ("whose fields (#0, #2) are ('\\0', 43)",
254 Explain(m, make_tuple('\0', 42, 43)));
255}
256
shiqiane35fdd92008-12-10 05:08:54 +0000257// For testing ExplainMatchResultTo().
258class GreaterThanMatcher : public MatcherInterface<int> {
259 public:
260 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
261
shiqiane35fdd92008-12-10 05:08:54 +0000262 virtual void DescribeTo(::std::ostream* os) const {
263 *os << "is greater than " << rhs_;
264 }
265
zhanyong.wandb22c222010-01-28 21:52:29 +0000266 virtual bool MatchAndExplain(int lhs,
267 MatchResultListener* listener) const {
shiqiane35fdd92008-12-10 05:08:54 +0000268 const int diff = lhs - rhs_;
269 if (diff > 0) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000270 *listener << "which is " << diff << " more than " << rhs_;
shiqiane35fdd92008-12-10 05:08:54 +0000271 } else if (diff == 0) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000272 *listener << "which is the same as " << rhs_;
shiqiane35fdd92008-12-10 05:08:54 +0000273 } else {
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000274 *listener << "which is " << -diff << " less than " << rhs_;
shiqiane35fdd92008-12-10 05:08:54 +0000275 }
zhanyong.wandb22c222010-01-28 21:52:29 +0000276
277 return lhs > rhs_;
shiqiane35fdd92008-12-10 05:08:54 +0000278 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000279
shiqiane35fdd92008-12-10 05:08:54 +0000280 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000281 int rhs_;
shiqiane35fdd92008-12-10 05:08:54 +0000282};
283
284Matcher<int> GreaterThan(int n) {
285 return MakeMatcher(new GreaterThanMatcher(n));
286}
287
288// Tests for ElementsAre().
289
shiqiane35fdd92008-12-10 05:08:54 +0000290TEST(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.
zhanyong.wanfb25d532013-07-28 08:24:00 +0000566 ::testing::internal::ElementsAreMatcher<std::tr1::tuple<int, int> >
567 polymorphic_matcher = ElementsAre(x, y);
jgm38513a82012-11-15 15:50:36 +0000568 // 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
zhanyong.wanfb25d532013-07-28 08:24:00 +0000576
shiqiane35fdd92008-12-10 05:08:54 +0000577// Tests for ElementsAreArray(). Since ElementsAreArray() shares most
578// of the implementation with ElementsAre(), we don't test it as
579// thoroughly here.
580
581TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) {
582 const int a[] = { 1, 2, 3 };
583
584 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
585 EXPECT_THAT(test_vector, ElementsAreArray(a));
586
587 test_vector[2] = 0;
588 EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
589}
590
591TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) {
592 const char* a[] = { "one", "two", "three" };
593
594 vector<string> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
595 EXPECT_THAT(test_vector, ElementsAreArray(a, GMOCK_ARRAY_SIZE_(a)));
596
597 const char** p = a;
598 test_vector[0] = "1";
599 EXPECT_THAT(test_vector, Not(ElementsAreArray(p, GMOCK_ARRAY_SIZE_(a))));
600}
601
602TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) {
603 const char* a[] = { "one", "two", "three" };
604
605 vector<string> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
606 EXPECT_THAT(test_vector, ElementsAreArray(a));
607
608 test_vector[0] = "1";
609 EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
610}
611
612TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) {
613 const Matcher<string> kMatcherArray[] =
614 { StrEq("one"), StrEq("two"), StrEq("three") };
615
616 vector<string> test_vector;
617 test_vector.push_back("one");
618 test_vector.push_back("two");
619 test_vector.push_back("three");
620 EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray));
621
622 test_vector.push_back("three");
623 EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray)));
624}
625
jgm38513a82012-11-15 15:50:36 +0000626TEST(ElementsAreArrayTest, CanBeCreatedWithVector) {
627 const int a[] = { 1, 2, 3 };
628 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
629 const vector<int> expected(a, a + GMOCK_ARRAY_SIZE_(a));
630 EXPECT_THAT(test_vector, ElementsAreArray(expected));
631 test_vector.push_back(4);
632 EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));
633}
634
635TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherVector) {
636 const int a[] = { 1, 2, 3 };
637 const Matcher<int> kMatchers[] = { Eq(1), Eq(2), Eq(3) };
638 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
639 const vector<Matcher<int> > expected(
640 kMatchers, kMatchers + GMOCK_ARRAY_SIZE_(kMatchers));
641 EXPECT_THAT(test_vector, ElementsAreArray(expected));
642 test_vector.push_back(4);
643 EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));
644}
645
646TEST(ElementsAreArrayTest, CanBeCreatedWithIteratorRange) {
647 const int a[] = { 1, 2, 3 };
648 const vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
649 const vector<int> expected(a, a + GMOCK_ARRAY_SIZE_(a));
650 EXPECT_THAT(test_vector, ElementsAreArray(expected.begin(), expected.end()));
651 // Pointers are iterators, too.
652 EXPECT_THAT(test_vector, ElementsAreArray(a, a + GMOCK_ARRAY_SIZE_(a)));
653 // The empty range of NULL pointers should also be okay.
654 int* const null_int = NULL;
655 EXPECT_THAT(test_vector, Not(ElementsAreArray(null_int, null_int)));
656 EXPECT_THAT((vector<int>()), ElementsAreArray(null_int, null_int));
657}
658
zhanyong.wanb8243162009-06-04 05:48:20 +0000659// Since ElementsAre() and ElementsAreArray() share much of the
660// implementation, we only do a sanity test for native arrays here.
661TEST(ElementsAreArrayTest, WorksWithNativeArray) {
662 ::std::string a[] = { "hi", "ho" };
663 ::std::string b[] = { "hi", "ho" };
664
665 EXPECT_THAT(a, ElementsAreArray(b));
666 EXPECT_THAT(a, ElementsAreArray(b, 2));
667 EXPECT_THAT(a, Not(ElementsAreArray(b, 1)));
668}
669
jgm38513a82012-11-15 15:50:36 +0000670TEST(ElementsAreArrayTest, SourceLifeSpan) {
671 const int a[] = { 1, 2, 3 };
672 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
673 vector<int> expect(a, a + GMOCK_ARRAY_SIZE_(a));
674 ElementsAreArrayMatcher<int> matcher_maker =
675 ElementsAreArray(expect.begin(), expect.end());
676 EXPECT_THAT(test_vector, matcher_maker);
677 // Changing in place the values that initialized matcher_maker should not
678 // affect matcher_maker anymore. It should have made its own copy of them.
679 typedef vector<int>::iterator Iter;
680 for (Iter it = expect.begin(); it != expect.end(); ++it) { *it += 10; }
681 EXPECT_THAT(test_vector, matcher_maker);
682 test_vector.push_back(3);
683 EXPECT_THAT(test_vector, Not(matcher_maker));
684}
685
zhanyong.wance198ff2009-02-12 01:34:27 +0000686// Tests for the MATCHER*() macro family.
687
688// Tests that a simple MATCHER() definition works.
689
690MATCHER(IsEven, "") { return (arg % 2) == 0; }
691
692TEST(MatcherMacroTest, Works) {
693 const Matcher<int> m = IsEven();
694 EXPECT_TRUE(m.Matches(6));
695 EXPECT_FALSE(m.Matches(7));
696
697 EXPECT_EQ("is even", Describe(m));
698 EXPECT_EQ("not (is even)", DescribeNegation(m));
699 EXPECT_EQ("", Explain(m, 6));
700 EXPECT_EQ("", Explain(m, 7));
701}
702
zhanyong.wanb4140802010-06-08 22:53:57 +0000703// This also tests that the description string can reference 'negation'.
704MATCHER(IsEven2, negation ? "is odd" : "is even") {
zhanyong.wan82113312010-01-08 21:55:40 +0000705 if ((arg % 2) == 0) {
706 // Verifies that we can stream to result_listener, a listener
707 // supplied by the MATCHER macro implicitly.
708 *result_listener << "OK";
709 return true;
710 } else {
711 *result_listener << "% 2 == " << (arg % 2);
712 return false;
713 }
714}
715
zhanyong.wanb4140802010-06-08 22:53:57 +0000716// This also tests that the description string can reference matcher
717// parameters.
718MATCHER_P2(EqSumOf, x, y,
719 string(negation ? "doesn't equal" : "equals") + " the sum of " +
720 PrintToString(x) + " and " + PrintToString(y)) {
zhanyong.wan82113312010-01-08 21:55:40 +0000721 if (arg == (x + y)) {
722 *result_listener << "OK";
723 return true;
724 } else {
725 // Verifies that we can stream to the underlying stream of
726 // result_listener.
727 if (result_listener->stream() != NULL) {
728 *result_listener->stream() << "diff == " << (x + y - arg);
729 }
730 return false;
731 }
732}
733
zhanyong.wanb4140802010-06-08 22:53:57 +0000734// Tests that the matcher description can reference 'negation' and the
735// matcher parameters.
736TEST(MatcherMacroTest, DescriptionCanReferenceNegationAndParameters) {
737 const Matcher<int> m1 = IsEven2();
738 EXPECT_EQ("is even", Describe(m1));
739 EXPECT_EQ("is odd", DescribeNegation(m1));
740
741 const Matcher<int> m2 = EqSumOf(5, 9);
742 EXPECT_EQ("equals the sum of 5 and 9", Describe(m2));
743 EXPECT_EQ("doesn't equal the sum of 5 and 9", DescribeNegation(m2));
744}
745
746// Tests explaining match result in a MATCHER* macro.
zhanyong.wan82113312010-01-08 21:55:40 +0000747TEST(MatcherMacroTest, CanExplainMatchResult) {
748 const Matcher<int> m1 = IsEven2();
749 EXPECT_EQ("OK", Explain(m1, 4));
750 EXPECT_EQ("% 2 == 1", Explain(m1, 5));
751
752 const Matcher<int> m2 = EqSumOf(1, 2);
753 EXPECT_EQ("OK", Explain(m2, 3));
754 EXPECT_EQ("diff == -1", Explain(m2, 4));
755}
756
zhanyong.wance198ff2009-02-12 01:34:27 +0000757// Tests that the body of MATCHER() can reference the type of the
758// value being matched.
759
760MATCHER(IsEmptyString, "") {
761 StaticAssertTypeEq< ::std::string, arg_type>();
762 return arg == "";
763}
764
765MATCHER(IsEmptyStringByRef, "") {
766 StaticAssertTypeEq<const ::std::string&, arg_type>();
767 return arg == "";
768}
769
770TEST(MatcherMacroTest, CanReferenceArgType) {
771 const Matcher< ::std::string> m1 = IsEmptyString();
772 EXPECT_TRUE(m1.Matches(""));
773
774 const Matcher<const ::std::string&> m2 = IsEmptyStringByRef();
775 EXPECT_TRUE(m2.Matches(""));
776}
777
778// Tests that MATCHER() can be used in a namespace.
779
780namespace matcher_test {
781MATCHER(IsOdd, "") { return (arg % 2) != 0; }
782} // namespace matcher_test
783
zhanyong.wanb8243162009-06-04 05:48:20 +0000784TEST(MatcherMacroTest, WorksInNamespace) {
zhanyong.wance198ff2009-02-12 01:34:27 +0000785 Matcher<int> m = matcher_test::IsOdd();
786 EXPECT_FALSE(m.Matches(4));
787 EXPECT_TRUE(m.Matches(5));
788}
789
zhanyong.wanb8243162009-06-04 05:48:20 +0000790// Tests that Value() can be used to compose matchers.
791MATCHER(IsPositiveOdd, "") {
792 return Value(arg, matcher_test::IsOdd()) && arg > 0;
793}
794
795TEST(MatcherMacroTest, CanBeComposedUsingValue) {
796 EXPECT_THAT(3, IsPositiveOdd());
797 EXPECT_THAT(4, Not(IsPositiveOdd()));
798 EXPECT_THAT(-1, Not(IsPositiveOdd()));
799}
800
zhanyong.wance198ff2009-02-12 01:34:27 +0000801// Tests that a simple MATCHER_P() definition works.
802
803MATCHER_P(IsGreaterThan32And, n, "") { return arg > 32 && arg > n; }
804
805TEST(MatcherPMacroTest, Works) {
806 const Matcher<int> m = IsGreaterThan32And(5);
807 EXPECT_TRUE(m.Matches(36));
808 EXPECT_FALSE(m.Matches(5));
809
810 EXPECT_EQ("is greater than 32 and 5", Describe(m));
811 EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m));
812 EXPECT_EQ("", Explain(m, 36));
813 EXPECT_EQ("", Explain(m, 5));
814}
815
zhanyong.wance198ff2009-02-12 01:34:27 +0000816// Tests that the description is calculated correctly from the matcher name.
817MATCHER_P(_is_Greater_Than32and_, n, "") { return arg > 32 && arg > n; }
818
819TEST(MatcherPMacroTest, GeneratesCorrectDescription) {
820 const Matcher<int> m = _is_Greater_Than32and_(5);
821
822 EXPECT_EQ("is greater than 32 and 5", Describe(m));
823 EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m));
824 EXPECT_EQ("", Explain(m, 36));
825 EXPECT_EQ("", Explain(m, 5));
826}
827
828// Tests that a MATCHER_P matcher can be explicitly instantiated with
829// a reference parameter type.
830
831class UncopyableFoo {
832 public:
833 explicit UncopyableFoo(char value) : value_(value) {}
834 private:
835 UncopyableFoo(const UncopyableFoo&);
836 void operator=(const UncopyableFoo&);
837
838 char value_;
839};
840
841MATCHER_P(ReferencesUncopyable, variable, "") { return &arg == &variable; }
842
843TEST(MatcherPMacroTest, WorksWhenExplicitlyInstantiatedWithReference) {
844 UncopyableFoo foo1('1'), foo2('2');
845 const Matcher<const UncopyableFoo&> m =
846 ReferencesUncopyable<const UncopyableFoo&>(foo1);
847
848 EXPECT_TRUE(m.Matches(foo1));
849 EXPECT_FALSE(m.Matches(foo2));
850
851 // We don't want the address of the parameter printed, as most
852 // likely it will just annoy the user. If the address is
853 // interesting, the user should consider passing the parameter by
854 // pointer instead.
855 EXPECT_EQ("references uncopyable 1-byte object <31>", Describe(m));
856}
857
858
zhanyong.wance198ff2009-02-12 01:34:27 +0000859// Tests that the body of MATCHER_Pn() can reference the parameter
860// types.
861
862MATCHER_P3(ParamTypesAreIntLongAndChar, foo, bar, baz, "") {
863 StaticAssertTypeEq<int, foo_type>();
864 StaticAssertTypeEq<long, bar_type>(); // NOLINT
865 StaticAssertTypeEq<char, baz_type>();
866 return arg == 0;
867}
868
869TEST(MatcherPnMacroTest, CanReferenceParamTypes) {
870 EXPECT_THAT(0, ParamTypesAreIntLongAndChar(10, 20L, 'a'));
871}
872
873// Tests that a MATCHER_Pn matcher can be explicitly instantiated with
874// reference parameter types.
875
876MATCHER_P2(ReferencesAnyOf, variable1, variable2, "") {
877 return &arg == &variable1 || &arg == &variable2;
878}
879
880TEST(MatcherPnMacroTest, WorksWhenExplicitlyInstantiatedWithReferences) {
881 UncopyableFoo foo1('1'), foo2('2'), foo3('3');
882 const Matcher<const UncopyableFoo&> m =
883 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
884
885 EXPECT_TRUE(m.Matches(foo1));
886 EXPECT_TRUE(m.Matches(foo2));
887 EXPECT_FALSE(m.Matches(foo3));
888}
889
890TEST(MatcherPnMacroTest,
891 GeneratesCorretDescriptionWhenExplicitlyInstantiatedWithReferences) {
892 UncopyableFoo foo1('1'), foo2('2');
893 const Matcher<const UncopyableFoo&> m =
894 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
895
896 // We don't want the addresses of the parameters printed, as most
897 // likely they will just annoy the user. If the addresses are
898 // interesting, the user should consider passing the parameters by
899 // pointers instead.
900 EXPECT_EQ("references any of (1-byte object <31>, 1-byte object <32>)",
901 Describe(m));
902}
903
904// Tests that a simple MATCHER_P2() definition works.
905
906MATCHER_P2(IsNotInClosedRange, low, hi, "") { return arg < low || arg > hi; }
907
908TEST(MatcherPnMacroTest, Works) {
909 const Matcher<const long&> m = IsNotInClosedRange(10, 20); // NOLINT
910 EXPECT_TRUE(m.Matches(36L));
911 EXPECT_FALSE(m.Matches(15L));
912
913 EXPECT_EQ("is not in closed range (10, 20)", Describe(m));
914 EXPECT_EQ("not (is not in closed range (10, 20))", DescribeNegation(m));
915 EXPECT_EQ("", Explain(m, 36L));
916 EXPECT_EQ("", Explain(m, 15L));
917}
918
919// Tests that MATCHER*() definitions can be overloaded on the number
920// of parameters; also tests MATCHER_Pn() where n >= 3.
921
922MATCHER(EqualsSumOf, "") { return arg == 0; }
923MATCHER_P(EqualsSumOf, a, "") { return arg == a; }
924MATCHER_P2(EqualsSumOf, a, b, "") { return arg == a + b; }
925MATCHER_P3(EqualsSumOf, a, b, c, "") { return arg == a + b + c; }
926MATCHER_P4(EqualsSumOf, a, b, c, d, "") { return arg == a + b + c + d; }
927MATCHER_P5(EqualsSumOf, a, b, c, d, e, "") { return arg == a + b + c + d + e; }
928MATCHER_P6(EqualsSumOf, a, b, c, d, e, f, "") {
929 return arg == a + b + c + d + e + f;
930}
931MATCHER_P7(EqualsSumOf, a, b, c, d, e, f, g, "") {
932 return arg == a + b + c + d + e + f + g;
933}
934MATCHER_P8(EqualsSumOf, a, b, c, d, e, f, g, h, "") {
935 return arg == a + b + c + d + e + f + g + h;
936}
937MATCHER_P9(EqualsSumOf, a, b, c, d, e, f, g, h, i, "") {
938 return arg == a + b + c + d + e + f + g + h + i;
939}
940MATCHER_P10(EqualsSumOf, a, b, c, d, e, f, g, h, i, j, "") {
941 return arg == a + b + c + d + e + f + g + h + i + j;
942}
943
944TEST(MatcherPnMacroTest, CanBeOverloadedOnNumberOfParameters) {
945 EXPECT_THAT(0, EqualsSumOf());
946 EXPECT_THAT(1, EqualsSumOf(1));
947 EXPECT_THAT(12, EqualsSumOf(10, 2));
948 EXPECT_THAT(123, EqualsSumOf(100, 20, 3));
949 EXPECT_THAT(1234, EqualsSumOf(1000, 200, 30, 4));
950 EXPECT_THAT(12345, EqualsSumOf(10000, 2000, 300, 40, 5));
951 EXPECT_THAT("abcdef",
952 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f'));
953 EXPECT_THAT("abcdefg",
954 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g'));
955 EXPECT_THAT("abcdefgh",
956 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
957 "h"));
958 EXPECT_THAT("abcdefghi",
959 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
960 "h", 'i'));
961 EXPECT_THAT("abcdefghij",
962 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
963 "h", 'i', ::std::string("j")));
964
965 EXPECT_THAT(1, Not(EqualsSumOf()));
966 EXPECT_THAT(-1, Not(EqualsSumOf(1)));
967 EXPECT_THAT(-12, Not(EqualsSumOf(10, 2)));
968 EXPECT_THAT(-123, Not(EqualsSumOf(100, 20, 3)));
969 EXPECT_THAT(-1234, Not(EqualsSumOf(1000, 200, 30, 4)));
970 EXPECT_THAT(-12345, Not(EqualsSumOf(10000, 2000, 300, 40, 5)));
971 EXPECT_THAT("abcdef ",
972 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f')));
973 EXPECT_THAT("abcdefg ",
974 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f',
975 'g')));
976 EXPECT_THAT("abcdefgh ",
977 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
978 "h")));
979 EXPECT_THAT("abcdefghi ",
980 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
981 "h", 'i')));
982 EXPECT_THAT("abcdefghij ",
983 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
984 "h", 'i', ::std::string("j"))));
985}
986
987// Tests that a MATCHER_Pn() definition can be instantiated with any
988// compatible parameter types.
989TEST(MatcherPnMacroTest, WorksForDifferentParameterTypes) {
990 EXPECT_THAT(123, EqualsSumOf(100L, 20, static_cast<char>(3)));
991 EXPECT_THAT("abcd", EqualsSumOf(::std::string("a"), "b", 'c', "d"));
992
993 EXPECT_THAT(124, Not(EqualsSumOf(100L, 20, static_cast<char>(3))));
994 EXPECT_THAT("abcde", Not(EqualsSumOf(::std::string("a"), "b", 'c', "d")));
995}
996
997// Tests that the matcher body can promote the parameter types.
998
999MATCHER_P2(EqConcat, prefix, suffix, "") {
1000 // The following lines promote the two parameters to desired types.
1001 std::string prefix_str(prefix);
zhanyong.wan32de5f52009-12-23 00:13:23 +00001002 char suffix_char = static_cast<char>(suffix);
zhanyong.wance198ff2009-02-12 01:34:27 +00001003 return arg == prefix_str + suffix_char;
1004}
1005
1006TEST(MatcherPnMacroTest, SimpleTypePromotion) {
1007 Matcher<std::string> no_promo =
1008 EqConcat(std::string("foo"), 't');
1009 Matcher<const std::string&> promo =
1010 EqConcat("foo", static_cast<int>('t'));
1011 EXPECT_FALSE(no_promo.Matches("fool"));
1012 EXPECT_FALSE(promo.Matches("fool"));
1013 EXPECT_TRUE(no_promo.Matches("foot"));
1014 EXPECT_TRUE(promo.Matches("foot"));
1015}
1016
1017// Verifies the type of a MATCHER*.
1018
1019TEST(MatcherPnMacroTest, TypesAreCorrect) {
1020 // EqualsSumOf() must be assignable to a EqualsSumOfMatcher variable.
1021 EqualsSumOfMatcher a0 = EqualsSumOf();
1022
1023 // EqualsSumOf(1) must be assignable to a EqualsSumOfMatcherP variable.
1024 EqualsSumOfMatcherP<int> a1 = EqualsSumOf(1);
1025
1026 // EqualsSumOf(p1, ..., pk) must be assignable to a EqualsSumOfMatcherPk
1027 // variable, and so on.
1028 EqualsSumOfMatcherP2<int, char> a2 = EqualsSumOf(1, '2');
1029 EqualsSumOfMatcherP3<int, int, char> a3 = EqualsSumOf(1, 2, '3');
1030 EqualsSumOfMatcherP4<int, int, int, char> a4 = EqualsSumOf(1, 2, 3, '4');
1031 EqualsSumOfMatcherP5<int, int, int, int, char> a5 =
1032 EqualsSumOf(1, 2, 3, 4, '5');
1033 EqualsSumOfMatcherP6<int, int, int, int, int, char> a6 =
1034 EqualsSumOf(1, 2, 3, 4, 5, '6');
1035 EqualsSumOfMatcherP7<int, int, int, int, int, int, char> a7 =
1036 EqualsSumOf(1, 2, 3, 4, 5, 6, '7');
1037 EqualsSumOfMatcherP8<int, int, int, int, int, int, int, char> a8 =
1038 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, '8');
1039 EqualsSumOfMatcherP9<int, int, int, int, int, int, int, int, char> a9 =
1040 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, '9');
1041 EqualsSumOfMatcherP10<int, int, int, int, int, int, int, int, int, char> a10 =
1042 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, 9, '0');
zhanyong.wan29be9232013-03-01 06:53:35 +00001043
1044 // Avoid "unused variable" warnings.
1045 (void)a0;
1046 (void)a1;
1047 (void)a2;
1048 (void)a3;
1049 (void)a4;
1050 (void)a5;
1051 (void)a6;
1052 (void)a7;
1053 (void)a8;
1054 (void)a9;
1055 (void)a10;
zhanyong.wance198ff2009-02-12 01:34:27 +00001056}
1057
zhanyong.wanb8243162009-06-04 05:48:20 +00001058// Tests that matcher-typed parameters can be used in Value() inside a
1059// MATCHER_Pn definition.
1060
1061// Succeeds if arg matches exactly 2 of the 3 matchers.
1062MATCHER_P3(TwoOf, m1, m2, m3, "") {
1063 const int count = static_cast<int>(Value(arg, m1))
1064 + static_cast<int>(Value(arg, m2)) + static_cast<int>(Value(arg, m3));
1065 return count == 2;
1066}
1067
1068TEST(MatcherPnMacroTest, CanUseMatcherTypedParameterInValue) {
1069 EXPECT_THAT(42, TwoOf(Gt(0), Lt(50), Eq(10)));
1070 EXPECT_THAT(0, Not(TwoOf(Gt(-1), Lt(1), Eq(0))));
1071}
1072
1073// Tests Contains().
1074
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001075TEST(ContainsTest, ListMatchesWhenElementIsInContainer) {
1076 list<int> some_list;
1077 some_list.push_back(3);
1078 some_list.push_back(1);
1079 some_list.push_back(2);
1080 EXPECT_THAT(some_list, Contains(1));
zhanyong.wanb8243162009-06-04 05:48:20 +00001081 EXPECT_THAT(some_list, Contains(Gt(2.5)));
1082 EXPECT_THAT(some_list, Contains(Eq(2.0f)));
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001083
1084 list<string> another_list;
1085 another_list.push_back("fee");
1086 another_list.push_back("fie");
1087 another_list.push_back("foe");
1088 another_list.push_back("fum");
1089 EXPECT_THAT(another_list, Contains(string("fee")));
1090}
1091
1092TEST(ContainsTest, ListDoesNotMatchWhenElementIsNotInContainer) {
1093 list<int> some_list;
1094 some_list.push_back(3);
1095 some_list.push_back(1);
1096 EXPECT_THAT(some_list, Not(Contains(4)));
1097}
1098
1099TEST(ContainsTest, SetMatchesWhenElementIsInContainer) {
1100 set<int> some_set;
1101 some_set.insert(3);
1102 some_set.insert(1);
1103 some_set.insert(2);
zhanyong.wanb8243162009-06-04 05:48:20 +00001104 EXPECT_THAT(some_set, Contains(Eq(1.0)));
1105 EXPECT_THAT(some_set, Contains(Eq(3.0f)));
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001106 EXPECT_THAT(some_set, Contains(2));
1107
1108 set<const char*> another_set;
1109 another_set.insert("fee");
1110 another_set.insert("fie");
1111 another_set.insert("foe");
1112 another_set.insert("fum");
zhanyong.wanb8243162009-06-04 05:48:20 +00001113 EXPECT_THAT(another_set, Contains(Eq(string("fum"))));
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001114}
1115
1116TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) {
1117 set<int> some_set;
1118 some_set.insert(3);
1119 some_set.insert(1);
1120 EXPECT_THAT(some_set, Not(Contains(4)));
1121
1122 set<const char*> c_string_set;
1123 c_string_set.insert("hello");
1124 EXPECT_THAT(c_string_set, Not(Contains(string("hello").c_str())));
1125}
1126
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001127TEST(ContainsTest, ExplainsMatchResultCorrectly) {
zhanyong.wanb8243162009-06-04 05:48:20 +00001128 const int a[2] = { 1, 2 };
jgm38513a82012-11-15 15:50:36 +00001129 Matcher<const int (&)[2]> m = Contains(2);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001130 EXPECT_EQ("whose element #1 matches", Explain(m, a));
zhanyong.wanb8243162009-06-04 05:48:20 +00001131
1132 m = Contains(3);
1133 EXPECT_EQ("", Explain(m, a));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001134
1135 m = Contains(GreaterThan(0));
1136 EXPECT_EQ("whose element #0 matches, which is 1 more than 0", Explain(m, a));
1137
1138 m = Contains(GreaterThan(10));
1139 EXPECT_EQ("", Explain(m, a));
zhanyong.wanb8243162009-06-04 05:48:20 +00001140}
1141
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001142TEST(ContainsTest, DescribesItselfCorrectly) {
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001143 Matcher<vector<int> > m = Contains(1);
zhanyong.wanb8243162009-06-04 05:48:20 +00001144 EXPECT_EQ("contains at least one element that is equal to 1", Describe(m));
1145
1146 Matcher<vector<int> > m2 = Not(m);
1147 EXPECT_EQ("doesn't contain any element that is equal to 1", Describe(m2));
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001148}
1149
1150TEST(ContainsTest, MapMatchesWhenElementIsInContainer) {
1151 map<const char*, int> my_map;
1152 const char* bar = "a string";
1153 my_map[bar] = 2;
1154 EXPECT_THAT(my_map, Contains(pair<const char* const, int>(bar, 2)));
1155
1156 map<string, int> another_map;
1157 another_map["fee"] = 1;
1158 another_map["fie"] = 2;
1159 another_map["foe"] = 3;
1160 another_map["fum"] = 4;
1161 EXPECT_THAT(another_map, Contains(pair<const string, int>(string("fee"), 1)));
1162 EXPECT_THAT(another_map, Contains(pair<const string, int>("fie", 2)));
1163}
1164
1165TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) {
1166 map<int, int> some_map;
1167 some_map[1] = 11;
1168 some_map[2] = 22;
1169 EXPECT_THAT(some_map, Not(Contains(pair<const int, int>(2, 23))));
1170}
1171
1172TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) {
1173 const char* string_array[] = { "fee", "fie", "foe", "fum" };
zhanyong.wanb8243162009-06-04 05:48:20 +00001174 EXPECT_THAT(string_array, Contains(Eq(string("fum"))));
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001175}
1176
1177TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) {
1178 int int_array[] = { 1, 2, 3, 4 };
1179 EXPECT_THAT(int_array, Not(Contains(5)));
1180}
1181
zhanyong.wanb8243162009-06-04 05:48:20 +00001182TEST(ContainsTest, AcceptsMatcher) {
1183 const int a[] = { 1, 2, 3 };
1184 EXPECT_THAT(a, Contains(Gt(2)));
1185 EXPECT_THAT(a, Not(Contains(Gt(4))));
1186}
1187
1188TEST(ContainsTest, WorksForNativeArrayAsTuple) {
1189 const int a[] = { 1, 2 };
zhanyong.wan2661c682009-06-09 05:42:12 +00001190 const int* const pointer = a;
1191 EXPECT_THAT(make_tuple(pointer, 2), Contains(1));
1192 EXPECT_THAT(make_tuple(pointer, 2), Not(Contains(Gt(3))));
zhanyong.wanb8243162009-06-04 05:48:20 +00001193}
1194
1195TEST(ContainsTest, WorksForTwoDimensionalNativeArray) {
1196 int a[][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
1197 EXPECT_THAT(a, Contains(ElementsAre(4, 5, 6)));
1198 EXPECT_THAT(a, Contains(Contains(5)));
1199 EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5))));
1200 EXPECT_THAT(a, Contains(Not(Contains(5))));
1201}
1202
jgm79a367e2012-04-10 16:02:11 +00001203TEST(AllOfTest, HugeMatcher) {
1204 // Verify that using AllOf with many arguments doesn't cause
1205 // the compiler to exceed template instantiation depth limit.
1206 EXPECT_THAT(0, testing::AllOf(_, _, _, _, _, _, _, _, _,
1207 testing::AllOf(_, _, _, _, _, _, _, _, _, _)));
1208}
1209
1210TEST(AnyOfTest, HugeMatcher) {
1211 // Verify that using AnyOf with many arguments doesn't cause
1212 // the compiler to exceed template instantiation depth limit.
1213 EXPECT_THAT(0, testing::AnyOf(_, _, _, _, _, _, _, _, _,
1214 testing::AnyOf(_, _, _, _, _, _, _, _, _, _)));
1215}
1216
zhanyong.wan86d2eeb2011-03-16 17:10:39 +00001217namespace adl_test {
1218
1219// Verifies that the implementation of ::testing::AllOf and ::testing::AnyOf
1220// don't issue unqualified recursive calls. If they do, the argument dependent
1221// name lookup will cause AllOf/AnyOf in the 'adl_test' namespace to be found
1222// as a candidate and the compilation will break due to an ambiguous overload.
1223
1224// The matcher must be in the same namespace as AllOf/AnyOf to make argument
1225// dependent lookup find those.
1226MATCHER(M, "") { return true; }
1227
1228template <typename T1, typename T2>
1229bool AllOf(const T1& t1, const T2& t2) { return true; }
1230
1231TEST(AllOfTest, DoesNotCallAllOfUnqualified) {
1232 EXPECT_THAT(42, testing::AllOf(
1233 M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
1234}
1235
1236template <typename T1, typename T2> bool
1237AnyOf(const T1& t1, const T2& t2) { return true; }
1238
1239TEST(AnyOfTest, DoesNotCallAnyOfUnqualified) {
1240 EXPECT_THAT(42, testing::AnyOf(
1241 M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
1242}
1243
1244} // namespace adl_test
1245
zhanyong.wan82113312010-01-08 21:55:40 +00001246#ifdef _MSC_VER
zhanyong.wan658ac0b2011-02-24 07:29:13 +00001247# pragma warning(pop)
zhanyong.wan82113312010-01-08 21:55:40 +00001248#endif
1249
shiqiane35fdd92008-12-10 05:08:54 +00001250} // namespace