blob: 1f88626fcb5cf6dd405b95b1d4884000329c9f61 [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>
37#include <sstream>
38#include <string>
39#include <vector>
40
41#include <gmock/gmock.h>
42#include <gtest/gtest.h>
zhanyong.wance198ff2009-02-12 01:34:27 +000043#include <gtest/gtest-spi.h>
shiqiane35fdd92008-12-10 05:08:54 +000044
45namespace {
46
47using std::list;
48using std::stringstream;
49using std::vector;
50using testing::_;
51using testing::ElementsAre;
52using testing::ElementsAreArray;
53using testing::Eq;
54using testing::Ge;
55using testing::Gt;
56using testing::MakeMatcher;
57using testing::Matcher;
58using testing::MatcherInterface;
59using testing::Ne;
60using testing::Not;
61using testing::Pointee;
62using testing::Ref;
zhanyong.wance198ff2009-02-12 01:34:27 +000063using testing::StaticAssertTypeEq;
shiqiane35fdd92008-12-10 05:08:54 +000064using testing::StrEq;
65using testing::internal::string;
66
67// Returns the description of the given matcher.
68template <typename T>
69string Describe(const Matcher<T>& m) {
70 stringstream ss;
71 m.DescribeTo(&ss);
72 return ss.str();
73}
74
75// Returns the description of the negation of the given matcher.
76template <typename T>
77string DescribeNegation(const Matcher<T>& m) {
78 stringstream ss;
79 m.DescribeNegationTo(&ss);
80 return ss.str();
81}
82
83// Returns the reason why x matches, or doesn't match, m.
84template <typename MatcherType, typename Value>
85string Explain(const MatcherType& m, const Value& x) {
86 stringstream ss;
87 m.ExplainMatchResultTo(x, &ss);
88 return ss.str();
89}
90
91// For testing ExplainMatchResultTo().
92class GreaterThanMatcher : public MatcherInterface<int> {
93 public:
94 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
95
96 virtual bool Matches(int lhs) const { return lhs > rhs_; }
97
98 virtual void DescribeTo(::std::ostream* os) const {
99 *os << "is greater than " << rhs_;
100 }
101
102 virtual void ExplainMatchResultTo(int lhs, ::std::ostream* os) const {
103 const int diff = lhs - rhs_;
104 if (diff > 0) {
105 *os << "is " << diff << " more than " << rhs_;
106 } else if (diff == 0) {
107 *os << "is the same as " << rhs_;
108 } else {
109 *os << "is " << -diff << " less than " << rhs_;
110 }
111 }
112 private:
113 const int rhs_;
114};
115
116Matcher<int> GreaterThan(int n) {
117 return MakeMatcher(new GreaterThanMatcher(n));
118}
119
120// Tests for ElementsAre().
121
122// Evaluates to the number of elements in 'array'.
123#define GMOCK_ARRAY_SIZE_(array) (sizeof(array)/sizeof(array[0]))
124
125TEST(ElementsAreTest, CanDescribeExpectingNoElement) {
126 Matcher<const vector<int>&> m = ElementsAre();
127 EXPECT_EQ("is empty", Describe(m));
128}
129
130TEST(ElementsAreTest, CanDescribeExpectingOneElement) {
131 Matcher<vector<int> > m = ElementsAre(Gt(5));
132 EXPECT_EQ("has 1 element that is greater than 5", Describe(m));
133}
134
135TEST(ElementsAreTest, CanDescribeExpectingManyElements) {
136 Matcher<list<string> > m = ElementsAre(StrEq("one"), "two");
137 EXPECT_EQ("has 2 elements where\n"
138 "element 0 is equal to \"one\",\n"
139 "element 1 is equal to \"two\"", Describe(m));
140}
141
142TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) {
143 Matcher<vector<int> > m = ElementsAre();
144 EXPECT_EQ("is not empty", DescribeNegation(m));
145}
146
147TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElment) {
148 Matcher<const list<int>& > m = ElementsAre(Gt(5));
149 EXPECT_EQ("does not have 1 element, or\n"
150 "element 0 is not greater than 5", DescribeNegation(m));
151}
152
153TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) {
154 Matcher<const list<string>& > m = ElementsAre("one", "two");
155 EXPECT_EQ("does not have 2 elements, or\n"
156 "element 0 is not equal to \"one\", or\n"
157 "element 1 is not equal to \"two\"", DescribeNegation(m));
158}
159
160TEST(ElementsAreTest, DoesNotExplainTrivialMatch) {
161 Matcher<const list<int>& > m = ElementsAre(1, Ne(2));
162
163 list<int> test_list;
164 test_list.push_back(1);
165 test_list.push_back(3);
166 EXPECT_EQ("", Explain(m, test_list)); // No need to explain anything.
167}
168
169TEST(ElementsAreTest, ExplainsNonTrivialMatch) {
170 Matcher<const vector<int>& > m =
171 ElementsAre(GreaterThan(1), 0, GreaterThan(2));
172
173 const int a[] = { 10, 0, 100 };
174 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
175 EXPECT_EQ("element 0 is 9 more than 1,\n"
176 "element 2 is 98 more than 2", Explain(m, test_vector));
177}
178
179TEST(ElementsAreTest, CanExplainMismatchWrongSize) {
180 Matcher<const list<int>& > m = ElementsAre(1, 3);
181
182 list<int> test_list;
183 // No need to explain when the container is empty.
184 EXPECT_EQ("", Explain(m, test_list));
185
186 test_list.push_back(1);
187 EXPECT_EQ("has 1 element", Explain(m, test_list));
188}
189
190TEST(ElementsAreTest, CanExplainMismatchRightSize) {
191 Matcher<const vector<int>& > m = ElementsAre(1, GreaterThan(5));
192
193 vector<int> v;
194 v.push_back(2);
195 v.push_back(1);
196 EXPECT_EQ("element 0 doesn't match", Explain(m, v));
197
198 v[0] = 1;
199 EXPECT_EQ("element 1 doesn't match (is 4 less than 5)", Explain(m, v));
200}
201
202TEST(ElementsAreTest, MatchesOneElementVector) {
203 vector<string> test_vector;
204 test_vector.push_back("test string");
205
206 EXPECT_THAT(test_vector, ElementsAre(StrEq("test string")));
207}
208
209TEST(ElementsAreTest, MatchesOneElementList) {
210 list<string> test_list;
211 test_list.push_back("test string");
212
213 EXPECT_THAT(test_list, ElementsAre("test string"));
214}
215
216TEST(ElementsAreTest, MatchesThreeElementVector) {
217 vector<string> test_vector;
218 test_vector.push_back("one");
219 test_vector.push_back("two");
220 test_vector.push_back("three");
221
222 EXPECT_THAT(test_vector, ElementsAre("one", StrEq("two"), _));
223}
224
225TEST(ElementsAreTest, MatchesOneElementEqMatcher) {
226 vector<int> test_vector;
227 test_vector.push_back(4);
228
229 EXPECT_THAT(test_vector, ElementsAre(Eq(4)));
230}
231
232TEST(ElementsAreTest, MatchesOneElementAnyMatcher) {
233 vector<int> test_vector;
234 test_vector.push_back(4);
235
236 EXPECT_THAT(test_vector, ElementsAre(_));
237}
238
239TEST(ElementsAreTest, MatchesOneElementValue) {
240 vector<int> test_vector;
241 test_vector.push_back(4);
242
243 EXPECT_THAT(test_vector, ElementsAre(4));
244}
245
246TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) {
247 vector<int> test_vector;
248 test_vector.push_back(1);
249 test_vector.push_back(2);
250 test_vector.push_back(3);
251
252 EXPECT_THAT(test_vector, ElementsAre(1, Eq(2), _));
253}
254
255TEST(ElementsAreTest, MatchesTenElementVector) {
256 const int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
257 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
258
259 EXPECT_THAT(test_vector,
260 // The element list can contain values and/or matchers
261 // of different types.
262 ElementsAre(0, Ge(0), _, 3, 4, Ne(2), Eq(6), 7, 8, _));
263}
264
265TEST(ElementsAreTest, DoesNotMatchWrongSize) {
266 vector<string> test_vector;
267 test_vector.push_back("test string");
268 test_vector.push_back("test string");
269
270 Matcher<vector<string> > m = ElementsAre(StrEq("test string"));
271 EXPECT_FALSE(m.Matches(test_vector));
272}
273
274TEST(ElementsAreTest, DoesNotMatchWrongValue) {
275 vector<string> test_vector;
276 test_vector.push_back("other string");
277
278 Matcher<vector<string> > m = ElementsAre(StrEq("test string"));
279 EXPECT_FALSE(m.Matches(test_vector));
280}
281
282TEST(ElementsAreTest, DoesNotMatchWrongOrder) {
283 vector<string> test_vector;
284 test_vector.push_back("one");
285 test_vector.push_back("three");
286 test_vector.push_back("two");
287
288 Matcher<vector<string> > m = ElementsAre(
289 StrEq("one"), StrEq("two"), StrEq("three"));
290 EXPECT_FALSE(m.Matches(test_vector));
291}
292
293TEST(ElementsAreTest, WorksForNestedContainer) {
294 const char* strings[] = {
295 "Hi",
296 "world"
297 };
298
299 vector<list<char> > nested;
300 for (int i = 0; i < GMOCK_ARRAY_SIZE_(strings); i++) {
301 nested.push_back(list<char>(strings[i], strings[i] + strlen(strings[i])));
302 }
303
304 EXPECT_THAT(nested, ElementsAre(ElementsAre('H', Ne('e')),
305 ElementsAre('w', 'o', _, _, 'd')));
306 EXPECT_THAT(nested, Not(ElementsAre(ElementsAre('H', 'e'),
307 ElementsAre('w', 'o', _, _, 'd'))));
308}
309
310TEST(ElementsAreTest, WorksWithByRefElementMatchers) {
311 int a[] = { 0, 1, 2 };
312 vector<int> v(a, a + GMOCK_ARRAY_SIZE_(a));
313
314 EXPECT_THAT(v, ElementsAre(Ref(v[0]), Ref(v[1]), Ref(v[2])));
315 EXPECT_THAT(v, Not(ElementsAre(Ref(v[0]), Ref(v[1]), Ref(a[2]))));
316}
317
318TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) {
319 int a[] = { 0, 1, 2 };
320 vector<int> v(a, a + GMOCK_ARRAY_SIZE_(a));
321
322 EXPECT_THAT(&v, Pointee(ElementsAre(0, 1, _)));
323 EXPECT_THAT(&v, Not(Pointee(ElementsAre(0, _, 3))));
324}
325
326// Tests for ElementsAreArray(). Since ElementsAreArray() shares most
327// of the implementation with ElementsAre(), we don't test it as
328// thoroughly here.
329
330TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) {
331 const int a[] = { 1, 2, 3 };
332
333 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
334 EXPECT_THAT(test_vector, ElementsAreArray(a));
335
336 test_vector[2] = 0;
337 EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
338}
339
340TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) {
341 const char* a[] = { "one", "two", "three" };
342
343 vector<string> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
344 EXPECT_THAT(test_vector, ElementsAreArray(a, GMOCK_ARRAY_SIZE_(a)));
345
346 const char** p = a;
347 test_vector[0] = "1";
348 EXPECT_THAT(test_vector, Not(ElementsAreArray(p, GMOCK_ARRAY_SIZE_(a))));
349}
350
351TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) {
352 const char* a[] = { "one", "two", "three" };
353
354 vector<string> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
355 EXPECT_THAT(test_vector, ElementsAreArray(a));
356
357 test_vector[0] = "1";
358 EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
359}
360
361TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) {
362 const Matcher<string> kMatcherArray[] =
363 { StrEq("one"), StrEq("two"), StrEq("three") };
364
365 vector<string> test_vector;
366 test_vector.push_back("one");
367 test_vector.push_back("two");
368 test_vector.push_back("three");
369 EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray));
370
371 test_vector.push_back("three");
372 EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray)));
373}
374
zhanyong.wance198ff2009-02-12 01:34:27 +0000375// Tests for the MATCHER*() macro family.
376
377// Tests that a simple MATCHER() definition works.
378
379MATCHER(IsEven, "") { return (arg % 2) == 0; }
380
381TEST(MatcherMacroTest, Works) {
382 const Matcher<int> m = IsEven();
383 EXPECT_TRUE(m.Matches(6));
384 EXPECT_FALSE(m.Matches(7));
385
386 EXPECT_EQ("is even", Describe(m));
387 EXPECT_EQ("not (is even)", DescribeNegation(m));
388 EXPECT_EQ("", Explain(m, 6));
389 EXPECT_EQ("", Explain(m, 7));
390}
391
392// Tests that the description string supplied to MATCHER() must be
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000393// valid.
zhanyong.wance198ff2009-02-12 01:34:27 +0000394
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000395MATCHER(HasBadDescription, "Invalid%") { return true; }
zhanyong.wance198ff2009-02-12 01:34:27 +0000396
397TEST(MatcherMacroTest,
398 CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) {
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000399 EXPECT_NONFATAL_FAILURE(
400 HasBadDescription(),
401 "Syntax error at index 7 in matcher description \"Invalid%\": "
402 "use \"%%\" instead of \"%\" to print \"%\".");
403}
404
405MATCHER(HasGoodDescription, "good") { return true; }
406
407TEST(MatcherMacroTest, AcceptsValidDescription) {
408 const Matcher<int> m = HasGoodDescription();
409 EXPECT_EQ("good", Describe(m));
zhanyong.wance198ff2009-02-12 01:34:27 +0000410}
411
412// Tests that the body of MATCHER() can reference the type of the
413// value being matched.
414
415MATCHER(IsEmptyString, "") {
416 StaticAssertTypeEq< ::std::string, arg_type>();
417 return arg == "";
418}
419
420MATCHER(IsEmptyStringByRef, "") {
421 StaticAssertTypeEq<const ::std::string&, arg_type>();
422 return arg == "";
423}
424
425TEST(MatcherMacroTest, CanReferenceArgType) {
426 const Matcher< ::std::string> m1 = IsEmptyString();
427 EXPECT_TRUE(m1.Matches(""));
428
429 const Matcher<const ::std::string&> m2 = IsEmptyStringByRef();
430 EXPECT_TRUE(m2.Matches(""));
431}
432
433// Tests that MATCHER() can be used in a namespace.
434
435namespace matcher_test {
436MATCHER(IsOdd, "") { return (arg % 2) != 0; }
437} // namespace matcher_test
438
439TEST(MatcherTest, WorksInNamespace) {
440 Matcher<int> m = matcher_test::IsOdd();
441 EXPECT_FALSE(m.Matches(4));
442 EXPECT_TRUE(m.Matches(5));
443}
444
445// Tests that a simple MATCHER_P() definition works.
446
447MATCHER_P(IsGreaterThan32And, n, "") { return arg > 32 && arg > n; }
448
449TEST(MatcherPMacroTest, Works) {
450 const Matcher<int> m = IsGreaterThan32And(5);
451 EXPECT_TRUE(m.Matches(36));
452 EXPECT_FALSE(m.Matches(5));
453
454 EXPECT_EQ("is greater than 32 and 5", Describe(m));
455 EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m));
456 EXPECT_EQ("", Explain(m, 36));
457 EXPECT_EQ("", Explain(m, 5));
458}
459
460// Tests that the description string supplied to MATCHER_P() must be
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000461// valid.
zhanyong.wance198ff2009-02-12 01:34:27 +0000462
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000463MATCHER_P(HasBadDescription1, n, "not %(m)s good") {
zhanyong.wance198ff2009-02-12 01:34:27 +0000464 return arg > n;
465}
466
467TEST(MatcherPMacroTest,
468 CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) {
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000469 EXPECT_NONFATAL_FAILURE(
470 HasBadDescription1(2),
471 "Syntax error at index 6 in matcher description \"not %(m)s good\": "
472 "\"m\" is an invalid parameter name.");
473}
474
475
476MATCHER_P(HasGoodDescription1, n, "good %(n)s") { return true; }
477
478TEST(MatcherPMacroTest, AcceptsValidDescription) {
479 const Matcher<int> m = HasGoodDescription1(5);
480 EXPECT_EQ("good 5", Describe(m));
zhanyong.wance198ff2009-02-12 01:34:27 +0000481}
482
483// Tests that the description is calculated correctly from the matcher name.
484MATCHER_P(_is_Greater_Than32and_, n, "") { return arg > 32 && arg > n; }
485
486TEST(MatcherPMacroTest, GeneratesCorrectDescription) {
487 const Matcher<int> m = _is_Greater_Than32and_(5);
488
489 EXPECT_EQ("is greater than 32 and 5", Describe(m));
490 EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m));
491 EXPECT_EQ("", Explain(m, 36));
492 EXPECT_EQ("", Explain(m, 5));
493}
494
495// Tests that a MATCHER_P matcher can be explicitly instantiated with
496// a reference parameter type.
497
498class UncopyableFoo {
499 public:
500 explicit UncopyableFoo(char value) : value_(value) {}
501 private:
502 UncopyableFoo(const UncopyableFoo&);
503 void operator=(const UncopyableFoo&);
504
505 char value_;
506};
507
508MATCHER_P(ReferencesUncopyable, variable, "") { return &arg == &variable; }
509
510TEST(MatcherPMacroTest, WorksWhenExplicitlyInstantiatedWithReference) {
511 UncopyableFoo foo1('1'), foo2('2');
512 const Matcher<const UncopyableFoo&> m =
513 ReferencesUncopyable<const UncopyableFoo&>(foo1);
514
515 EXPECT_TRUE(m.Matches(foo1));
516 EXPECT_FALSE(m.Matches(foo2));
517
518 // We don't want the address of the parameter printed, as most
519 // likely it will just annoy the user. If the address is
520 // interesting, the user should consider passing the parameter by
521 // pointer instead.
522 EXPECT_EQ("references uncopyable 1-byte object <31>", Describe(m));
523}
524
525
526// Tests that the description string supplied to MATCHER_Pn() must be
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000527// valid.
zhanyong.wance198ff2009-02-12 01:34:27 +0000528
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000529MATCHER_P2(HasBadDescription2, m, n, "not %(good") {
zhanyong.wance198ff2009-02-12 01:34:27 +0000530 return arg > m + n;
531}
532
533TEST(MatcherPnMacroTest,
534 CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) {
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000535 EXPECT_NONFATAL_FAILURE(
536 HasBadDescription2(3, 4),
537 "Syntax error at index 4 in matcher description \"not %(good\": "
538 "an interpolation must end with \")s\", but \"%(good\" does not.");
539}
540
541MATCHER_P2(HasComplexDescription, foo, bar,
542 "is as complex as %(foo)s %(bar)s (i.e. %(*)s or %%%(foo)s!)") {
543 return true;
544}
545
546TEST(MatcherPnMacroTest, AcceptsValidDescription) {
547 Matcher<int> m = HasComplexDescription(100, "ducks");
548 EXPECT_EQ("is as complex as 100 \"ducks\" (i.e. (100, \"ducks\") or %100!)",
549 Describe(m));
zhanyong.wance198ff2009-02-12 01:34:27 +0000550}
551
552// Tests that the body of MATCHER_Pn() can reference the parameter
553// types.
554
555MATCHER_P3(ParamTypesAreIntLongAndChar, foo, bar, baz, "") {
556 StaticAssertTypeEq<int, foo_type>();
557 StaticAssertTypeEq<long, bar_type>(); // NOLINT
558 StaticAssertTypeEq<char, baz_type>();
559 return arg == 0;
560}
561
562TEST(MatcherPnMacroTest, CanReferenceParamTypes) {
563 EXPECT_THAT(0, ParamTypesAreIntLongAndChar(10, 20L, 'a'));
564}
565
566// Tests that a MATCHER_Pn matcher can be explicitly instantiated with
567// reference parameter types.
568
569MATCHER_P2(ReferencesAnyOf, variable1, variable2, "") {
570 return &arg == &variable1 || &arg == &variable2;
571}
572
573TEST(MatcherPnMacroTest, WorksWhenExplicitlyInstantiatedWithReferences) {
574 UncopyableFoo foo1('1'), foo2('2'), foo3('3');
575 const Matcher<const UncopyableFoo&> m =
576 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
577
578 EXPECT_TRUE(m.Matches(foo1));
579 EXPECT_TRUE(m.Matches(foo2));
580 EXPECT_FALSE(m.Matches(foo3));
581}
582
583TEST(MatcherPnMacroTest,
584 GeneratesCorretDescriptionWhenExplicitlyInstantiatedWithReferences) {
585 UncopyableFoo foo1('1'), foo2('2');
586 const Matcher<const UncopyableFoo&> m =
587 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
588
589 // We don't want the addresses of the parameters printed, as most
590 // likely they will just annoy the user. If the addresses are
591 // interesting, the user should consider passing the parameters by
592 // pointers instead.
593 EXPECT_EQ("references any of (1-byte object <31>, 1-byte object <32>)",
594 Describe(m));
595}
596
597// Tests that a simple MATCHER_P2() definition works.
598
599MATCHER_P2(IsNotInClosedRange, low, hi, "") { return arg < low || arg > hi; }
600
601TEST(MatcherPnMacroTest, Works) {
602 const Matcher<const long&> m = IsNotInClosedRange(10, 20); // NOLINT
603 EXPECT_TRUE(m.Matches(36L));
604 EXPECT_FALSE(m.Matches(15L));
605
606 EXPECT_EQ("is not in closed range (10, 20)", Describe(m));
607 EXPECT_EQ("not (is not in closed range (10, 20))", DescribeNegation(m));
608 EXPECT_EQ("", Explain(m, 36L));
609 EXPECT_EQ("", Explain(m, 15L));
610}
611
612// Tests that MATCHER*() definitions can be overloaded on the number
613// of parameters; also tests MATCHER_Pn() where n >= 3.
614
615MATCHER(EqualsSumOf, "") { return arg == 0; }
616MATCHER_P(EqualsSumOf, a, "") { return arg == a; }
617MATCHER_P2(EqualsSumOf, a, b, "") { return arg == a + b; }
618MATCHER_P3(EqualsSumOf, a, b, c, "") { return arg == a + b + c; }
619MATCHER_P4(EqualsSumOf, a, b, c, d, "") { return arg == a + b + c + d; }
620MATCHER_P5(EqualsSumOf, a, b, c, d, e, "") { return arg == a + b + c + d + e; }
621MATCHER_P6(EqualsSumOf, a, b, c, d, e, f, "") {
622 return arg == a + b + c + d + e + f;
623}
624MATCHER_P7(EqualsSumOf, a, b, c, d, e, f, g, "") {
625 return arg == a + b + c + d + e + f + g;
626}
627MATCHER_P8(EqualsSumOf, a, b, c, d, e, f, g, h, "") {
628 return arg == a + b + c + d + e + f + g + h;
629}
630MATCHER_P9(EqualsSumOf, a, b, c, d, e, f, g, h, i, "") {
631 return arg == a + b + c + d + e + f + g + h + i;
632}
633MATCHER_P10(EqualsSumOf, a, b, c, d, e, f, g, h, i, j, "") {
634 return arg == a + b + c + d + e + f + g + h + i + j;
635}
636
637TEST(MatcherPnMacroTest, CanBeOverloadedOnNumberOfParameters) {
638 EXPECT_THAT(0, EqualsSumOf());
639 EXPECT_THAT(1, EqualsSumOf(1));
640 EXPECT_THAT(12, EqualsSumOf(10, 2));
641 EXPECT_THAT(123, EqualsSumOf(100, 20, 3));
642 EXPECT_THAT(1234, EqualsSumOf(1000, 200, 30, 4));
643 EXPECT_THAT(12345, EqualsSumOf(10000, 2000, 300, 40, 5));
644 EXPECT_THAT("abcdef",
645 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f'));
646 EXPECT_THAT("abcdefg",
647 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g'));
648 EXPECT_THAT("abcdefgh",
649 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
650 "h"));
651 EXPECT_THAT("abcdefghi",
652 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
653 "h", 'i'));
654 EXPECT_THAT("abcdefghij",
655 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
656 "h", 'i', ::std::string("j")));
657
658 EXPECT_THAT(1, Not(EqualsSumOf()));
659 EXPECT_THAT(-1, Not(EqualsSumOf(1)));
660 EXPECT_THAT(-12, Not(EqualsSumOf(10, 2)));
661 EXPECT_THAT(-123, Not(EqualsSumOf(100, 20, 3)));
662 EXPECT_THAT(-1234, Not(EqualsSumOf(1000, 200, 30, 4)));
663 EXPECT_THAT(-12345, Not(EqualsSumOf(10000, 2000, 300, 40, 5)));
664 EXPECT_THAT("abcdef ",
665 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f')));
666 EXPECT_THAT("abcdefg ",
667 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f',
668 'g')));
669 EXPECT_THAT("abcdefgh ",
670 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
671 "h")));
672 EXPECT_THAT("abcdefghi ",
673 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
674 "h", 'i')));
675 EXPECT_THAT("abcdefghij ",
676 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
677 "h", 'i', ::std::string("j"))));
678}
679
680// Tests that a MATCHER_Pn() definition can be instantiated with any
681// compatible parameter types.
682TEST(MatcherPnMacroTest, WorksForDifferentParameterTypes) {
683 EXPECT_THAT(123, EqualsSumOf(100L, 20, static_cast<char>(3)));
684 EXPECT_THAT("abcd", EqualsSumOf(::std::string("a"), "b", 'c', "d"));
685
686 EXPECT_THAT(124, Not(EqualsSumOf(100L, 20, static_cast<char>(3))));
687 EXPECT_THAT("abcde", Not(EqualsSumOf(::std::string("a"), "b", 'c', "d")));
688}
689
690// Tests that the matcher body can promote the parameter types.
691
692MATCHER_P2(EqConcat, prefix, suffix, "") {
693 // The following lines promote the two parameters to desired types.
694 std::string prefix_str(prefix);
695 char suffix_char(suffix);
696 return arg == prefix_str + suffix_char;
697}
698
699TEST(MatcherPnMacroTest, SimpleTypePromotion) {
700 Matcher<std::string> no_promo =
701 EqConcat(std::string("foo"), 't');
702 Matcher<const std::string&> promo =
703 EqConcat("foo", static_cast<int>('t'));
704 EXPECT_FALSE(no_promo.Matches("fool"));
705 EXPECT_FALSE(promo.Matches("fool"));
706 EXPECT_TRUE(no_promo.Matches("foot"));
707 EXPECT_TRUE(promo.Matches("foot"));
708}
709
710// Verifies the type of a MATCHER*.
711
712TEST(MatcherPnMacroTest, TypesAreCorrect) {
713 // EqualsSumOf() must be assignable to a EqualsSumOfMatcher variable.
714 EqualsSumOfMatcher a0 = EqualsSumOf();
715
716 // EqualsSumOf(1) must be assignable to a EqualsSumOfMatcherP variable.
717 EqualsSumOfMatcherP<int> a1 = EqualsSumOf(1);
718
719 // EqualsSumOf(p1, ..., pk) must be assignable to a EqualsSumOfMatcherPk
720 // variable, and so on.
721 EqualsSumOfMatcherP2<int, char> a2 = EqualsSumOf(1, '2');
722 EqualsSumOfMatcherP3<int, int, char> a3 = EqualsSumOf(1, 2, '3');
723 EqualsSumOfMatcherP4<int, int, int, char> a4 = EqualsSumOf(1, 2, 3, '4');
724 EqualsSumOfMatcherP5<int, int, int, int, char> a5 =
725 EqualsSumOf(1, 2, 3, 4, '5');
726 EqualsSumOfMatcherP6<int, int, int, int, int, char> a6 =
727 EqualsSumOf(1, 2, 3, 4, 5, '6');
728 EqualsSumOfMatcherP7<int, int, int, int, int, int, char> a7 =
729 EqualsSumOf(1, 2, 3, 4, 5, 6, '7');
730 EqualsSumOfMatcherP8<int, int, int, int, int, int, int, char> a8 =
731 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, '8');
732 EqualsSumOfMatcherP9<int, int, int, int, int, int, int, int, char> a9 =
733 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, '9');
734 EqualsSumOfMatcherP10<int, int, int, int, int, int, int, int, int, char> a10 =
735 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, 9, '0');
736}
737
shiqiane35fdd92008-12-10 05:08:54 +0000738} // namespace