blob: 2681446355d7d4529f2d2cab632d56304e4d5e7f [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
34#include <gmock/gmock-generated-matchers.h>
35
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
44#include <gmock/gmock.h>
45#include <gtest/gtest.h>
zhanyong.wance198ff2009-02-12 01:34:27 +000046#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.wanb8243162009-06-04 05:48:20 +000056using std::tr1::make_tuple;
shiqiane35fdd92008-12-10 05:08:54 +000057using testing::_;
zhanyong.wan1bee7b22009-02-20 18:31:04 +000058using testing::Contains;
shiqiane35fdd92008-12-10 05:08:54 +000059using testing::ElementsAre;
60using testing::ElementsAreArray;
61using testing::Eq;
62using testing::Ge;
63using testing::Gt;
zhanyong.wanb8243162009-06-04 05:48:20 +000064using testing::Lt;
shiqiane35fdd92008-12-10 05:08:54 +000065using testing::MakeMatcher;
66using testing::Matcher;
67using testing::MatcherInterface;
68using testing::Ne;
69using testing::Not;
70using testing::Pointee;
71using testing::Ref;
zhanyong.wance198ff2009-02-12 01:34:27 +000072using testing::StaticAssertTypeEq;
shiqiane35fdd92008-12-10 05:08:54 +000073using testing::StrEq;
zhanyong.wanb8243162009-06-04 05:48:20 +000074using testing::Value;
shiqiane35fdd92008-12-10 05:08:54 +000075using testing::internal::string;
76
77// Returns the description of the given matcher.
78template <typename T>
79string Describe(const Matcher<T>& m) {
80 stringstream ss;
81 m.DescribeTo(&ss);
82 return ss.str();
83}
84
85// Returns the description of the negation of the given matcher.
86template <typename T>
87string DescribeNegation(const Matcher<T>& m) {
88 stringstream ss;
89 m.DescribeNegationTo(&ss);
90 return ss.str();
91}
92
93// Returns the reason why x matches, or doesn't match, m.
94template <typename MatcherType, typename Value>
95string Explain(const MatcherType& m, const Value& x) {
96 stringstream ss;
97 m.ExplainMatchResultTo(x, &ss);
98 return ss.str();
99}
100
101// For testing ExplainMatchResultTo().
102class GreaterThanMatcher : public MatcherInterface<int> {
103 public:
104 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
105
106 virtual bool Matches(int lhs) const { return lhs > rhs_; }
107
108 virtual void DescribeTo(::std::ostream* os) const {
109 *os << "is greater than " << rhs_;
110 }
111
112 virtual void ExplainMatchResultTo(int lhs, ::std::ostream* os) const {
113 const int diff = lhs - rhs_;
114 if (diff > 0) {
115 *os << "is " << diff << " more than " << rhs_;
116 } else if (diff == 0) {
117 *os << "is the same as " << rhs_;
118 } else {
119 *os << "is " << -diff << " less than " << rhs_;
120 }
121 }
122 private:
123 const int rhs_;
124};
125
126Matcher<int> GreaterThan(int n) {
127 return MakeMatcher(new GreaterThanMatcher(n));
128}
129
130// Tests for ElementsAre().
131
132// Evaluates to the number of elements in 'array'.
133#define GMOCK_ARRAY_SIZE_(array) (sizeof(array)/sizeof(array[0]))
134
135TEST(ElementsAreTest, CanDescribeExpectingNoElement) {
136 Matcher<const vector<int>&> m = ElementsAre();
137 EXPECT_EQ("is empty", Describe(m));
138}
139
140TEST(ElementsAreTest, CanDescribeExpectingOneElement) {
141 Matcher<vector<int> > m = ElementsAre(Gt(5));
142 EXPECT_EQ("has 1 element that is greater than 5", Describe(m));
143}
144
145TEST(ElementsAreTest, CanDescribeExpectingManyElements) {
146 Matcher<list<string> > m = ElementsAre(StrEq("one"), "two");
147 EXPECT_EQ("has 2 elements where\n"
148 "element 0 is equal to \"one\",\n"
149 "element 1 is equal to \"two\"", Describe(m));
150}
151
152TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) {
153 Matcher<vector<int> > m = ElementsAre();
154 EXPECT_EQ("is not empty", DescribeNegation(m));
155}
156
157TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElment) {
158 Matcher<const list<int>& > m = ElementsAre(Gt(5));
159 EXPECT_EQ("does not have 1 element, or\n"
160 "element 0 is not greater than 5", DescribeNegation(m));
161}
162
163TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) {
164 Matcher<const list<string>& > m = ElementsAre("one", "two");
165 EXPECT_EQ("does not have 2 elements, or\n"
166 "element 0 is not equal to \"one\", or\n"
167 "element 1 is not equal to \"two\"", DescribeNegation(m));
168}
169
170TEST(ElementsAreTest, DoesNotExplainTrivialMatch) {
171 Matcher<const list<int>& > m = ElementsAre(1, Ne(2));
172
173 list<int> test_list;
174 test_list.push_back(1);
175 test_list.push_back(3);
176 EXPECT_EQ("", Explain(m, test_list)); // No need to explain anything.
177}
178
179TEST(ElementsAreTest, ExplainsNonTrivialMatch) {
180 Matcher<const vector<int>& > m =
181 ElementsAre(GreaterThan(1), 0, GreaterThan(2));
182
183 const int a[] = { 10, 0, 100 };
184 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
185 EXPECT_EQ("element 0 is 9 more than 1,\n"
186 "element 2 is 98 more than 2", Explain(m, test_vector));
187}
188
189TEST(ElementsAreTest, CanExplainMismatchWrongSize) {
190 Matcher<const list<int>& > m = ElementsAre(1, 3);
191
192 list<int> test_list;
193 // No need to explain when the container is empty.
194 EXPECT_EQ("", Explain(m, test_list));
195
196 test_list.push_back(1);
197 EXPECT_EQ("has 1 element", Explain(m, test_list));
198}
199
200TEST(ElementsAreTest, CanExplainMismatchRightSize) {
201 Matcher<const vector<int>& > m = ElementsAre(1, GreaterThan(5));
202
203 vector<int> v;
204 v.push_back(2);
205 v.push_back(1);
206 EXPECT_EQ("element 0 doesn't match", Explain(m, v));
207
208 v[0] = 1;
209 EXPECT_EQ("element 1 doesn't match (is 4 less than 5)", Explain(m, v));
210}
211
212TEST(ElementsAreTest, MatchesOneElementVector) {
213 vector<string> test_vector;
214 test_vector.push_back("test string");
215
216 EXPECT_THAT(test_vector, ElementsAre(StrEq("test string")));
217}
218
219TEST(ElementsAreTest, MatchesOneElementList) {
220 list<string> test_list;
221 test_list.push_back("test string");
222
223 EXPECT_THAT(test_list, ElementsAre("test string"));
224}
225
226TEST(ElementsAreTest, MatchesThreeElementVector) {
227 vector<string> test_vector;
228 test_vector.push_back("one");
229 test_vector.push_back("two");
230 test_vector.push_back("three");
231
232 EXPECT_THAT(test_vector, ElementsAre("one", StrEq("two"), _));
233}
234
235TEST(ElementsAreTest, MatchesOneElementEqMatcher) {
236 vector<int> test_vector;
237 test_vector.push_back(4);
238
239 EXPECT_THAT(test_vector, ElementsAre(Eq(4)));
240}
241
242TEST(ElementsAreTest, MatchesOneElementAnyMatcher) {
243 vector<int> test_vector;
244 test_vector.push_back(4);
245
246 EXPECT_THAT(test_vector, ElementsAre(_));
247}
248
249TEST(ElementsAreTest, MatchesOneElementValue) {
250 vector<int> test_vector;
251 test_vector.push_back(4);
252
253 EXPECT_THAT(test_vector, ElementsAre(4));
254}
255
256TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) {
257 vector<int> test_vector;
258 test_vector.push_back(1);
259 test_vector.push_back(2);
260 test_vector.push_back(3);
261
262 EXPECT_THAT(test_vector, ElementsAre(1, Eq(2), _));
263}
264
265TEST(ElementsAreTest, MatchesTenElementVector) {
266 const int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
267 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
268
269 EXPECT_THAT(test_vector,
270 // The element list can contain values and/or matchers
271 // of different types.
272 ElementsAre(0, Ge(0), _, 3, 4, Ne(2), Eq(6), 7, 8, _));
273}
274
275TEST(ElementsAreTest, DoesNotMatchWrongSize) {
276 vector<string> test_vector;
277 test_vector.push_back("test string");
278 test_vector.push_back("test string");
279
280 Matcher<vector<string> > m = ElementsAre(StrEq("test string"));
281 EXPECT_FALSE(m.Matches(test_vector));
282}
283
284TEST(ElementsAreTest, DoesNotMatchWrongValue) {
285 vector<string> test_vector;
286 test_vector.push_back("other string");
287
288 Matcher<vector<string> > m = ElementsAre(StrEq("test string"));
289 EXPECT_FALSE(m.Matches(test_vector));
290}
291
292TEST(ElementsAreTest, DoesNotMatchWrongOrder) {
293 vector<string> test_vector;
294 test_vector.push_back("one");
295 test_vector.push_back("three");
296 test_vector.push_back("two");
297
298 Matcher<vector<string> > m = ElementsAre(
299 StrEq("one"), StrEq("two"), StrEq("three"));
300 EXPECT_FALSE(m.Matches(test_vector));
301}
302
303TEST(ElementsAreTest, WorksForNestedContainer) {
304 const char* strings[] = {
305 "Hi",
306 "world"
307 };
308
309 vector<list<char> > nested;
310 for (int i = 0; i < GMOCK_ARRAY_SIZE_(strings); i++) {
311 nested.push_back(list<char>(strings[i], strings[i] + strlen(strings[i])));
312 }
313
314 EXPECT_THAT(nested, ElementsAre(ElementsAre('H', Ne('e')),
315 ElementsAre('w', 'o', _, _, 'd')));
316 EXPECT_THAT(nested, Not(ElementsAre(ElementsAre('H', 'e'),
317 ElementsAre('w', 'o', _, _, 'd'))));
318}
319
320TEST(ElementsAreTest, WorksWithByRefElementMatchers) {
321 int a[] = { 0, 1, 2 };
322 vector<int> v(a, a + GMOCK_ARRAY_SIZE_(a));
323
324 EXPECT_THAT(v, ElementsAre(Ref(v[0]), Ref(v[1]), Ref(v[2])));
325 EXPECT_THAT(v, Not(ElementsAre(Ref(v[0]), Ref(v[1]), Ref(a[2]))));
326}
327
328TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) {
329 int a[] = { 0, 1, 2 };
330 vector<int> v(a, a + GMOCK_ARRAY_SIZE_(a));
331
332 EXPECT_THAT(&v, Pointee(ElementsAre(0, 1, _)));
333 EXPECT_THAT(&v, Not(Pointee(ElementsAre(0, _, 3))));
334}
335
zhanyong.wanb8243162009-06-04 05:48:20 +0000336TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) {
337 int array[] = { 0, 1, 2 };
338 EXPECT_THAT(array, ElementsAre(0, 1, _));
339 EXPECT_THAT(array, Not(ElementsAre(1, _, _)));
340 EXPECT_THAT(array, Not(ElementsAre(0, _)));
341}
342
343class NativeArrayPassedAsPointerAndSize {
344 public:
345 MOCK_METHOD2(Helper, void(int* array, int size));
346};
347
348TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) {
349 int array[] = { 0, 1 };
350 ::std::tr1::tuple<int*, size_t> array_as_tuple(array, 2);
351 EXPECT_THAT(array_as_tuple, ElementsAre(0, 1));
352 EXPECT_THAT(array_as_tuple, Not(ElementsAre(0)));
353
354 NativeArrayPassedAsPointerAndSize helper;
355 EXPECT_CALL(helper, Helper(_, _))
356 .WithArguments(ElementsAre(0, 1));
357 helper.Helper(array, 2);
358}
359
360TEST(ElementsAreTest, WorksWithTwoDimensionalNativeArray) {
361 const char a2[][3] = { "hi", "lo" };
362 EXPECT_THAT(a2, ElementsAre(ElementsAre('h', 'i', '\0'),
363 ElementsAre('l', 'o', '\0')));
364 EXPECT_THAT(a2, ElementsAre(StrEq("hi"), StrEq("lo")));
365 EXPECT_THAT(a2, ElementsAre(Not(ElementsAre('h', 'o', '\0')),
366 ElementsAre('l', 'o', '\0')));
367}
368
shiqiane35fdd92008-12-10 05:08:54 +0000369// Tests for ElementsAreArray(). Since ElementsAreArray() shares most
370// of the implementation with ElementsAre(), we don't test it as
371// thoroughly here.
372
373TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) {
374 const int a[] = { 1, 2, 3 };
375
376 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
377 EXPECT_THAT(test_vector, ElementsAreArray(a));
378
379 test_vector[2] = 0;
380 EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
381}
382
383TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) {
384 const char* a[] = { "one", "two", "three" };
385
386 vector<string> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
387 EXPECT_THAT(test_vector, ElementsAreArray(a, GMOCK_ARRAY_SIZE_(a)));
388
389 const char** p = a;
390 test_vector[0] = "1";
391 EXPECT_THAT(test_vector, Not(ElementsAreArray(p, GMOCK_ARRAY_SIZE_(a))));
392}
393
394TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) {
395 const char* a[] = { "one", "two", "three" };
396
397 vector<string> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
398 EXPECT_THAT(test_vector, ElementsAreArray(a));
399
400 test_vector[0] = "1";
401 EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
402}
403
404TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) {
405 const Matcher<string> kMatcherArray[] =
406 { StrEq("one"), StrEq("two"), StrEq("three") };
407
408 vector<string> test_vector;
409 test_vector.push_back("one");
410 test_vector.push_back("two");
411 test_vector.push_back("three");
412 EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray));
413
414 test_vector.push_back("three");
415 EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray)));
416}
417
zhanyong.wanb8243162009-06-04 05:48:20 +0000418// Since ElementsAre() and ElementsAreArray() share much of the
419// implementation, we only do a sanity test for native arrays here.
420TEST(ElementsAreArrayTest, WorksWithNativeArray) {
421 ::std::string a[] = { "hi", "ho" };
422 ::std::string b[] = { "hi", "ho" };
423
424 EXPECT_THAT(a, ElementsAreArray(b));
425 EXPECT_THAT(a, ElementsAreArray(b, 2));
426 EXPECT_THAT(a, Not(ElementsAreArray(b, 1)));
427}
428
zhanyong.wance198ff2009-02-12 01:34:27 +0000429// Tests for the MATCHER*() macro family.
430
431// Tests that a simple MATCHER() definition works.
432
433MATCHER(IsEven, "") { return (arg % 2) == 0; }
434
435TEST(MatcherMacroTest, Works) {
436 const Matcher<int> m = IsEven();
437 EXPECT_TRUE(m.Matches(6));
438 EXPECT_FALSE(m.Matches(7));
439
440 EXPECT_EQ("is even", Describe(m));
441 EXPECT_EQ("not (is even)", DescribeNegation(m));
442 EXPECT_EQ("", Explain(m, 6));
443 EXPECT_EQ("", Explain(m, 7));
444}
445
446// Tests that the description string supplied to MATCHER() must be
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000447// valid.
zhanyong.wance198ff2009-02-12 01:34:27 +0000448
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000449MATCHER(HasBadDescription, "Invalid%") { return true; }
zhanyong.wance198ff2009-02-12 01:34:27 +0000450
451TEST(MatcherMacroTest,
452 CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) {
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000453 EXPECT_NONFATAL_FAILURE(
454 HasBadDescription(),
455 "Syntax error at index 7 in matcher description \"Invalid%\": "
456 "use \"%%\" instead of \"%\" to print \"%\".");
457}
458
459MATCHER(HasGoodDescription, "good") { return true; }
460
461TEST(MatcherMacroTest, AcceptsValidDescription) {
462 const Matcher<int> m = HasGoodDescription();
463 EXPECT_EQ("good", Describe(m));
zhanyong.wance198ff2009-02-12 01:34:27 +0000464}
465
466// Tests that the body of MATCHER() can reference the type of the
467// value being matched.
468
469MATCHER(IsEmptyString, "") {
470 StaticAssertTypeEq< ::std::string, arg_type>();
471 return arg == "";
472}
473
474MATCHER(IsEmptyStringByRef, "") {
475 StaticAssertTypeEq<const ::std::string&, arg_type>();
476 return arg == "";
477}
478
479TEST(MatcherMacroTest, CanReferenceArgType) {
480 const Matcher< ::std::string> m1 = IsEmptyString();
481 EXPECT_TRUE(m1.Matches(""));
482
483 const Matcher<const ::std::string&> m2 = IsEmptyStringByRef();
484 EXPECT_TRUE(m2.Matches(""));
485}
486
487// Tests that MATCHER() can be used in a namespace.
488
489namespace matcher_test {
490MATCHER(IsOdd, "") { return (arg % 2) != 0; }
491} // namespace matcher_test
492
zhanyong.wanb8243162009-06-04 05:48:20 +0000493TEST(MatcherMacroTest, WorksInNamespace) {
zhanyong.wance198ff2009-02-12 01:34:27 +0000494 Matcher<int> m = matcher_test::IsOdd();
495 EXPECT_FALSE(m.Matches(4));
496 EXPECT_TRUE(m.Matches(5));
497}
498
zhanyong.wanb8243162009-06-04 05:48:20 +0000499// Tests that Value() can be used to compose matchers.
500MATCHER(IsPositiveOdd, "") {
501 return Value(arg, matcher_test::IsOdd()) && arg > 0;
502}
503
504TEST(MatcherMacroTest, CanBeComposedUsingValue) {
505 EXPECT_THAT(3, IsPositiveOdd());
506 EXPECT_THAT(4, Not(IsPositiveOdd()));
507 EXPECT_THAT(-1, Not(IsPositiveOdd()));
508}
509
zhanyong.wance198ff2009-02-12 01:34:27 +0000510// Tests that a simple MATCHER_P() definition works.
511
512MATCHER_P(IsGreaterThan32And, n, "") { return arg > 32 && arg > n; }
513
514TEST(MatcherPMacroTest, Works) {
515 const Matcher<int> m = IsGreaterThan32And(5);
516 EXPECT_TRUE(m.Matches(36));
517 EXPECT_FALSE(m.Matches(5));
518
519 EXPECT_EQ("is greater than 32 and 5", Describe(m));
520 EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m));
521 EXPECT_EQ("", Explain(m, 36));
522 EXPECT_EQ("", Explain(m, 5));
523}
524
525// Tests that the description string supplied to MATCHER_P() must be
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000526// valid.
zhanyong.wance198ff2009-02-12 01:34:27 +0000527
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000528MATCHER_P(HasBadDescription1, n, "not %(m)s good") {
zhanyong.wance198ff2009-02-12 01:34:27 +0000529 return arg > n;
530}
531
532TEST(MatcherPMacroTest,
533 CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) {
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000534 EXPECT_NONFATAL_FAILURE(
535 HasBadDescription1(2),
536 "Syntax error at index 6 in matcher description \"not %(m)s good\": "
537 "\"m\" is an invalid parameter name.");
538}
539
540
541MATCHER_P(HasGoodDescription1, n, "good %(n)s") { return true; }
542
543TEST(MatcherPMacroTest, AcceptsValidDescription) {
544 const Matcher<int> m = HasGoodDescription1(5);
545 EXPECT_EQ("good 5", Describe(m));
zhanyong.wance198ff2009-02-12 01:34:27 +0000546}
547
548// Tests that the description is calculated correctly from the matcher name.
549MATCHER_P(_is_Greater_Than32and_, n, "") { return arg > 32 && arg > n; }
550
551TEST(MatcherPMacroTest, GeneratesCorrectDescription) {
552 const Matcher<int> m = _is_Greater_Than32and_(5);
553
554 EXPECT_EQ("is greater than 32 and 5", Describe(m));
555 EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m));
556 EXPECT_EQ("", Explain(m, 36));
557 EXPECT_EQ("", Explain(m, 5));
558}
559
560// Tests that a MATCHER_P matcher can be explicitly instantiated with
561// a reference parameter type.
562
563class UncopyableFoo {
564 public:
565 explicit UncopyableFoo(char value) : value_(value) {}
566 private:
567 UncopyableFoo(const UncopyableFoo&);
568 void operator=(const UncopyableFoo&);
569
570 char value_;
571};
572
573MATCHER_P(ReferencesUncopyable, variable, "") { return &arg == &variable; }
574
575TEST(MatcherPMacroTest, WorksWhenExplicitlyInstantiatedWithReference) {
576 UncopyableFoo foo1('1'), foo2('2');
577 const Matcher<const UncopyableFoo&> m =
578 ReferencesUncopyable<const UncopyableFoo&>(foo1);
579
580 EXPECT_TRUE(m.Matches(foo1));
581 EXPECT_FALSE(m.Matches(foo2));
582
583 // We don't want the address of the parameter printed, as most
584 // likely it will just annoy the user. If the address is
585 // interesting, the user should consider passing the parameter by
586 // pointer instead.
587 EXPECT_EQ("references uncopyable 1-byte object <31>", Describe(m));
588}
589
590
591// Tests that the description string supplied to MATCHER_Pn() must be
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000592// valid.
zhanyong.wance198ff2009-02-12 01:34:27 +0000593
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000594MATCHER_P2(HasBadDescription2, m, n, "not %(good") {
zhanyong.wance198ff2009-02-12 01:34:27 +0000595 return arg > m + n;
596}
597
598TEST(MatcherPnMacroTest,
599 CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) {
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000600 EXPECT_NONFATAL_FAILURE(
601 HasBadDescription2(3, 4),
602 "Syntax error at index 4 in matcher description \"not %(good\": "
603 "an interpolation must end with \")s\", but \"%(good\" does not.");
604}
605
606MATCHER_P2(HasComplexDescription, foo, bar,
607 "is as complex as %(foo)s %(bar)s (i.e. %(*)s or %%%(foo)s!)") {
608 return true;
609}
610
611TEST(MatcherPnMacroTest, AcceptsValidDescription) {
612 Matcher<int> m = HasComplexDescription(100, "ducks");
613 EXPECT_EQ("is as complex as 100 \"ducks\" (i.e. (100, \"ducks\") or %100!)",
614 Describe(m));
zhanyong.wance198ff2009-02-12 01:34:27 +0000615}
616
617// Tests that the body of MATCHER_Pn() can reference the parameter
618// types.
619
620MATCHER_P3(ParamTypesAreIntLongAndChar, foo, bar, baz, "") {
621 StaticAssertTypeEq<int, foo_type>();
622 StaticAssertTypeEq<long, bar_type>(); // NOLINT
623 StaticAssertTypeEq<char, baz_type>();
624 return arg == 0;
625}
626
627TEST(MatcherPnMacroTest, CanReferenceParamTypes) {
628 EXPECT_THAT(0, ParamTypesAreIntLongAndChar(10, 20L, 'a'));
629}
630
631// Tests that a MATCHER_Pn matcher can be explicitly instantiated with
632// reference parameter types.
633
634MATCHER_P2(ReferencesAnyOf, variable1, variable2, "") {
635 return &arg == &variable1 || &arg == &variable2;
636}
637
638TEST(MatcherPnMacroTest, WorksWhenExplicitlyInstantiatedWithReferences) {
639 UncopyableFoo foo1('1'), foo2('2'), foo3('3');
640 const Matcher<const UncopyableFoo&> m =
641 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
642
643 EXPECT_TRUE(m.Matches(foo1));
644 EXPECT_TRUE(m.Matches(foo2));
645 EXPECT_FALSE(m.Matches(foo3));
646}
647
648TEST(MatcherPnMacroTest,
649 GeneratesCorretDescriptionWhenExplicitlyInstantiatedWithReferences) {
650 UncopyableFoo foo1('1'), foo2('2');
651 const Matcher<const UncopyableFoo&> m =
652 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
653
654 // We don't want the addresses of the parameters printed, as most
655 // likely they will just annoy the user. If the addresses are
656 // interesting, the user should consider passing the parameters by
657 // pointers instead.
658 EXPECT_EQ("references any of (1-byte object <31>, 1-byte object <32>)",
659 Describe(m));
660}
661
662// Tests that a simple MATCHER_P2() definition works.
663
664MATCHER_P2(IsNotInClosedRange, low, hi, "") { return arg < low || arg > hi; }
665
666TEST(MatcherPnMacroTest, Works) {
667 const Matcher<const long&> m = IsNotInClosedRange(10, 20); // NOLINT
668 EXPECT_TRUE(m.Matches(36L));
669 EXPECT_FALSE(m.Matches(15L));
670
671 EXPECT_EQ("is not in closed range (10, 20)", Describe(m));
672 EXPECT_EQ("not (is not in closed range (10, 20))", DescribeNegation(m));
673 EXPECT_EQ("", Explain(m, 36L));
674 EXPECT_EQ("", Explain(m, 15L));
675}
676
677// Tests that MATCHER*() definitions can be overloaded on the number
678// of parameters; also tests MATCHER_Pn() where n >= 3.
679
680MATCHER(EqualsSumOf, "") { return arg == 0; }
681MATCHER_P(EqualsSumOf, a, "") { return arg == a; }
682MATCHER_P2(EqualsSumOf, a, b, "") { return arg == a + b; }
683MATCHER_P3(EqualsSumOf, a, b, c, "") { return arg == a + b + c; }
684MATCHER_P4(EqualsSumOf, a, b, c, d, "") { return arg == a + b + c + d; }
685MATCHER_P5(EqualsSumOf, a, b, c, d, e, "") { return arg == a + b + c + d + e; }
686MATCHER_P6(EqualsSumOf, a, b, c, d, e, f, "") {
687 return arg == a + b + c + d + e + f;
688}
689MATCHER_P7(EqualsSumOf, a, b, c, d, e, f, g, "") {
690 return arg == a + b + c + d + e + f + g;
691}
692MATCHER_P8(EqualsSumOf, a, b, c, d, e, f, g, h, "") {
693 return arg == a + b + c + d + e + f + g + h;
694}
695MATCHER_P9(EqualsSumOf, a, b, c, d, e, f, g, h, i, "") {
696 return arg == a + b + c + d + e + f + g + h + i;
697}
698MATCHER_P10(EqualsSumOf, a, b, c, d, e, f, g, h, i, j, "") {
699 return arg == a + b + c + d + e + f + g + h + i + j;
700}
701
702TEST(MatcherPnMacroTest, CanBeOverloadedOnNumberOfParameters) {
703 EXPECT_THAT(0, EqualsSumOf());
704 EXPECT_THAT(1, EqualsSumOf(1));
705 EXPECT_THAT(12, EqualsSumOf(10, 2));
706 EXPECT_THAT(123, EqualsSumOf(100, 20, 3));
707 EXPECT_THAT(1234, EqualsSumOf(1000, 200, 30, 4));
708 EXPECT_THAT(12345, EqualsSumOf(10000, 2000, 300, 40, 5));
709 EXPECT_THAT("abcdef",
710 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f'));
711 EXPECT_THAT("abcdefg",
712 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g'));
713 EXPECT_THAT("abcdefgh",
714 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
715 "h"));
716 EXPECT_THAT("abcdefghi",
717 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
718 "h", 'i'));
719 EXPECT_THAT("abcdefghij",
720 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
721 "h", 'i', ::std::string("j")));
722
723 EXPECT_THAT(1, Not(EqualsSumOf()));
724 EXPECT_THAT(-1, Not(EqualsSumOf(1)));
725 EXPECT_THAT(-12, Not(EqualsSumOf(10, 2)));
726 EXPECT_THAT(-123, Not(EqualsSumOf(100, 20, 3)));
727 EXPECT_THAT(-1234, Not(EqualsSumOf(1000, 200, 30, 4)));
728 EXPECT_THAT(-12345, Not(EqualsSumOf(10000, 2000, 300, 40, 5)));
729 EXPECT_THAT("abcdef ",
730 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f')));
731 EXPECT_THAT("abcdefg ",
732 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f',
733 'g')));
734 EXPECT_THAT("abcdefgh ",
735 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
736 "h")));
737 EXPECT_THAT("abcdefghi ",
738 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
739 "h", 'i')));
740 EXPECT_THAT("abcdefghij ",
741 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
742 "h", 'i', ::std::string("j"))));
743}
744
745// Tests that a MATCHER_Pn() definition can be instantiated with any
746// compatible parameter types.
747TEST(MatcherPnMacroTest, WorksForDifferentParameterTypes) {
748 EXPECT_THAT(123, EqualsSumOf(100L, 20, static_cast<char>(3)));
749 EXPECT_THAT("abcd", EqualsSumOf(::std::string("a"), "b", 'c', "d"));
750
751 EXPECT_THAT(124, Not(EqualsSumOf(100L, 20, static_cast<char>(3))));
752 EXPECT_THAT("abcde", Not(EqualsSumOf(::std::string("a"), "b", 'c', "d")));
753}
754
755// Tests that the matcher body can promote the parameter types.
756
757MATCHER_P2(EqConcat, prefix, suffix, "") {
758 // The following lines promote the two parameters to desired types.
759 std::string prefix_str(prefix);
760 char suffix_char(suffix);
761 return arg == prefix_str + suffix_char;
762}
763
764TEST(MatcherPnMacroTest, SimpleTypePromotion) {
765 Matcher<std::string> no_promo =
766 EqConcat(std::string("foo"), 't');
767 Matcher<const std::string&> promo =
768 EqConcat("foo", static_cast<int>('t'));
769 EXPECT_FALSE(no_promo.Matches("fool"));
770 EXPECT_FALSE(promo.Matches("fool"));
771 EXPECT_TRUE(no_promo.Matches("foot"));
772 EXPECT_TRUE(promo.Matches("foot"));
773}
774
775// Verifies the type of a MATCHER*.
776
777TEST(MatcherPnMacroTest, TypesAreCorrect) {
778 // EqualsSumOf() must be assignable to a EqualsSumOfMatcher variable.
779 EqualsSumOfMatcher a0 = EqualsSumOf();
780
781 // EqualsSumOf(1) must be assignable to a EqualsSumOfMatcherP variable.
782 EqualsSumOfMatcherP<int> a1 = EqualsSumOf(1);
783
784 // EqualsSumOf(p1, ..., pk) must be assignable to a EqualsSumOfMatcherPk
785 // variable, and so on.
786 EqualsSumOfMatcherP2<int, char> a2 = EqualsSumOf(1, '2');
787 EqualsSumOfMatcherP3<int, int, char> a3 = EqualsSumOf(1, 2, '3');
788 EqualsSumOfMatcherP4<int, int, int, char> a4 = EqualsSumOf(1, 2, 3, '4');
789 EqualsSumOfMatcherP5<int, int, int, int, char> a5 =
790 EqualsSumOf(1, 2, 3, 4, '5');
791 EqualsSumOfMatcherP6<int, int, int, int, int, char> a6 =
792 EqualsSumOf(1, 2, 3, 4, 5, '6');
793 EqualsSumOfMatcherP7<int, int, int, int, int, int, char> a7 =
794 EqualsSumOf(1, 2, 3, 4, 5, 6, '7');
795 EqualsSumOfMatcherP8<int, int, int, int, int, int, int, char> a8 =
796 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, '8');
797 EqualsSumOfMatcherP9<int, int, int, int, int, int, int, int, char> a9 =
798 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, '9');
799 EqualsSumOfMatcherP10<int, int, int, int, int, int, int, int, int, char> a10 =
800 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, 9, '0');
801}
802
zhanyong.wanb8243162009-06-04 05:48:20 +0000803// Tests that matcher-typed parameters can be used in Value() inside a
804// MATCHER_Pn definition.
805
806// Succeeds if arg matches exactly 2 of the 3 matchers.
807MATCHER_P3(TwoOf, m1, m2, m3, "") {
808 const int count = static_cast<int>(Value(arg, m1))
809 + static_cast<int>(Value(arg, m2)) + static_cast<int>(Value(arg, m3));
810 return count == 2;
811}
812
813TEST(MatcherPnMacroTest, CanUseMatcherTypedParameterInValue) {
814 EXPECT_THAT(42, TwoOf(Gt(0), Lt(50), Eq(10)));
815 EXPECT_THAT(0, Not(TwoOf(Gt(-1), Lt(1), Eq(0))));
816}
817
818// Tests Contains().
819
zhanyong.wan1bee7b22009-02-20 18:31:04 +0000820TEST(ContainsTest, ListMatchesWhenElementIsInContainer) {
821 list<int> some_list;
822 some_list.push_back(3);
823 some_list.push_back(1);
824 some_list.push_back(2);
825 EXPECT_THAT(some_list, Contains(1));
zhanyong.wanb8243162009-06-04 05:48:20 +0000826 EXPECT_THAT(some_list, Contains(Gt(2.5)));
827 EXPECT_THAT(some_list, Contains(Eq(2.0f)));
zhanyong.wan1bee7b22009-02-20 18:31:04 +0000828
829 list<string> another_list;
830 another_list.push_back("fee");
831 another_list.push_back("fie");
832 another_list.push_back("foe");
833 another_list.push_back("fum");
834 EXPECT_THAT(another_list, Contains(string("fee")));
835}
836
837TEST(ContainsTest, ListDoesNotMatchWhenElementIsNotInContainer) {
838 list<int> some_list;
839 some_list.push_back(3);
840 some_list.push_back(1);
841 EXPECT_THAT(some_list, Not(Contains(4)));
842}
843
844TEST(ContainsTest, SetMatchesWhenElementIsInContainer) {
845 set<int> some_set;
846 some_set.insert(3);
847 some_set.insert(1);
848 some_set.insert(2);
zhanyong.wanb8243162009-06-04 05:48:20 +0000849 EXPECT_THAT(some_set, Contains(Eq(1.0)));
850 EXPECT_THAT(some_set, Contains(Eq(3.0f)));
zhanyong.wan1bee7b22009-02-20 18:31:04 +0000851 EXPECT_THAT(some_set, Contains(2));
852
853 set<const char*> another_set;
854 another_set.insert("fee");
855 another_set.insert("fie");
856 another_set.insert("foe");
857 another_set.insert("fum");
zhanyong.wanb8243162009-06-04 05:48:20 +0000858 EXPECT_THAT(another_set, Contains(Eq(string("fum"))));
zhanyong.wan1bee7b22009-02-20 18:31:04 +0000859}
860
861TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) {
862 set<int> some_set;
863 some_set.insert(3);
864 some_set.insert(1);
865 EXPECT_THAT(some_set, Not(Contains(4)));
866
867 set<const char*> c_string_set;
868 c_string_set.insert("hello");
869 EXPECT_THAT(c_string_set, Not(Contains(string("hello").c_str())));
870}
871
872TEST(ContainsTest, DescribesItselfCorrectly) {
zhanyong.wanb8243162009-06-04 05:48:20 +0000873 const int a[2] = { 1, 2 };
874 Matcher<const int(&)[2]> m = Contains(2);
875 EXPECT_EQ("element 1 matches", Explain(m, a));
876
877 m = Contains(3);
878 EXPECT_EQ("", Explain(m, a));
879}
880
881TEST(ContainsTest, ExplainsMatchResultCorrectly) {
zhanyong.wan1bee7b22009-02-20 18:31:04 +0000882 Matcher<vector<int> > m = Contains(1);
zhanyong.wanb8243162009-06-04 05:48:20 +0000883 EXPECT_EQ("contains at least one element that is equal to 1", Describe(m));
884
885 Matcher<vector<int> > m2 = Not(m);
886 EXPECT_EQ("doesn't contain any element that is equal to 1", Describe(m2));
zhanyong.wan1bee7b22009-02-20 18:31:04 +0000887}
888
889TEST(ContainsTest, MapMatchesWhenElementIsInContainer) {
890 map<const char*, int> my_map;
891 const char* bar = "a string";
892 my_map[bar] = 2;
893 EXPECT_THAT(my_map, Contains(pair<const char* const, int>(bar, 2)));
894
895 map<string, int> another_map;
896 another_map["fee"] = 1;
897 another_map["fie"] = 2;
898 another_map["foe"] = 3;
899 another_map["fum"] = 4;
900 EXPECT_THAT(another_map, Contains(pair<const string, int>(string("fee"), 1)));
901 EXPECT_THAT(another_map, Contains(pair<const string, int>("fie", 2)));
902}
903
904TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) {
905 map<int, int> some_map;
906 some_map[1] = 11;
907 some_map[2] = 22;
908 EXPECT_THAT(some_map, Not(Contains(pair<const int, int>(2, 23))));
909}
910
911TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) {
912 const char* string_array[] = { "fee", "fie", "foe", "fum" };
zhanyong.wanb8243162009-06-04 05:48:20 +0000913 EXPECT_THAT(string_array, Contains(Eq(string("fum"))));
zhanyong.wan1bee7b22009-02-20 18:31:04 +0000914}
915
916TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) {
917 int int_array[] = { 1, 2, 3, 4 };
918 EXPECT_THAT(int_array, Not(Contains(5)));
919}
920
zhanyong.wanb8243162009-06-04 05:48:20 +0000921TEST(ContainsTest, AcceptsMatcher) {
922 const int a[] = { 1, 2, 3 };
923 EXPECT_THAT(a, Contains(Gt(2)));
924 EXPECT_THAT(a, Not(Contains(Gt(4))));
925}
926
927TEST(ContainsTest, WorksForNativeArrayAsTuple) {
928 const int a[] = { 1, 2 };
929 EXPECT_THAT(make_tuple(a, 2), Contains(1));
930 EXPECT_THAT(make_tuple(a, 2), Not(Contains(Gt(3))));
931}
932
933TEST(ContainsTest, WorksForTwoDimensionalNativeArray) {
934 int a[][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
935 EXPECT_THAT(a, Contains(ElementsAre(4, 5, 6)));
936 EXPECT_THAT(a, Contains(Contains(5)));
937 EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5))));
938 EXPECT_THAT(a, Contains(Not(Contains(5))));
939}
940
shiqiane35fdd92008-12-10 05:08:54 +0000941} // namespace