blob: eebca8a04ec77cae6f4f0397a0c3909522a25525 [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.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;
75using testing::Ref;
zhanyong.wance198ff2009-02-12 01:34:27 +000076using testing::StaticAssertTypeEq;
shiqiane35fdd92008-12-10 05:08:54 +000077using testing::StrEq;
zhanyong.wanb8243162009-06-04 05:48:20 +000078using testing::Value;
shiqiane35fdd92008-12-10 05:08:54 +000079using testing::internal::string;
80
81// Returns the description of the given matcher.
82template <typename T>
83string Describe(const Matcher<T>& m) {
84 stringstream ss;
85 m.DescribeTo(&ss);
86 return ss.str();
87}
88
89// Returns the description of the negation of the given matcher.
90template <typename T>
91string DescribeNegation(const Matcher<T>& m) {
92 stringstream ss;
93 m.DescribeNegationTo(&ss);
94 return ss.str();
95}
96
97// Returns the reason why x matches, or doesn't match, m.
98template <typename MatcherType, typename Value>
99string Explain(const MatcherType& m, const Value& x) {
100 stringstream ss;
101 m.ExplainMatchResultTo(x, &ss);
102 return ss.str();
103}
104
zhanyong.wan2661c682009-06-09 05:42:12 +0000105// Tests Args<k0, ..., kn>(m).
106
107TEST(ArgsTest, AcceptsZeroTemplateArg) {
108 const tuple<int, bool> t(5, true);
109 EXPECT_THAT(t, Args<>(Eq(tuple<>())));
110 EXPECT_THAT(t, Not(Args<>(Ne(tuple<>()))));
111}
112
113TEST(ArgsTest, AcceptsOneTemplateArg) {
114 const tuple<int, bool> t(5, true);
115 EXPECT_THAT(t, Args<0>(Eq(make_tuple(5))));
116 EXPECT_THAT(t, Args<1>(Eq(make_tuple(true))));
117 EXPECT_THAT(t, Not(Args<1>(Eq(make_tuple(false)))));
118}
119
120TEST(ArgsTest, AcceptsTwoTemplateArgs) {
121 const tuple<short, int, long> t(4, 5, 6L); // NOLINT
122
123 EXPECT_THAT(t, (Args<0, 1>(Lt())));
124 EXPECT_THAT(t, (Args<1, 2>(Lt())));
125 EXPECT_THAT(t, Not(Args<0, 2>(Gt())));
126}
127
128TEST(ArgsTest, AcceptsRepeatedTemplateArgs) {
129 const tuple<short, int, long> t(4, 5, 6L); // NOLINT
130 EXPECT_THAT(t, (Args<0, 0>(Eq())));
131 EXPECT_THAT(t, Not(Args<1, 1>(Ne())));
132}
133
134TEST(ArgsTest, AcceptsDecreasingTemplateArgs) {
135 const tuple<short, int, long> t(4, 5, 6L); // NOLINT
136 EXPECT_THAT(t, (Args<2, 0>(Gt())));
137 EXPECT_THAT(t, Not(Args<2, 1>(Lt())));
138}
139
zhanyong.wan82113312010-01-08 21:55:40 +0000140// The MATCHER*() macros trigger warning C4100 (unreferenced formal
141// parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
142// the macro definition, as the warnings are generated when the macro
143// is expanded and macro expansion cannot contain #pragma. Therefore
144// we suppress them here.
145#ifdef _MSC_VER
146#pragma warning(push)
147#pragma warning(disable:4100)
148#endif
149
zhanyong.wan2661c682009-06-09 05:42:12 +0000150MATCHER(SumIsZero, "") {
151 return get<0>(arg) + get<1>(arg) + get<2>(arg) == 0;
152}
153
154TEST(ArgsTest, AcceptsMoreTemplateArgsThanArityOfOriginalTuple) {
155 EXPECT_THAT(make_tuple(-1, 2), (Args<0, 0, 1>(SumIsZero())));
156 EXPECT_THAT(make_tuple(1, 2), Not(Args<0, 0, 1>(SumIsZero())));
157}
158
159TEST(ArgsTest, CanBeNested) {
160 const tuple<short, int, long, int> t(4, 5, 6L, 6); // NOLINT
161 EXPECT_THAT(t, (Args<1, 2, 3>(Args<1, 2>(Eq()))));
162 EXPECT_THAT(t, (Args<0, 1, 3>(Args<0, 2>(Lt()))));
163}
164
165TEST(ArgsTest, CanMatchTupleByValue) {
166 typedef tuple<char, int, int> Tuple3;
167 const Matcher<Tuple3> m = Args<1, 2>(Lt());
168 EXPECT_TRUE(m.Matches(Tuple3('a', 1, 2)));
169 EXPECT_FALSE(m.Matches(Tuple3('b', 2, 2)));
170}
171
172TEST(ArgsTest, CanMatchTupleByReference) {
173 typedef tuple<char, char, int> Tuple3;
174 const Matcher<const Tuple3&> m = Args<0, 1>(Lt());
175 EXPECT_TRUE(m.Matches(Tuple3('a', 'b', 2)));
176 EXPECT_FALSE(m.Matches(Tuple3('b', 'b', 2)));
177}
178
179// Validates that arg is printed as str.
180MATCHER_P(PrintsAs, str, "") {
zhanyong.wan676e8cc2010-03-16 20:01:51 +0000181 return testing::PrintToString(arg) == str;
zhanyong.wan2661c682009-06-09 05:42:12 +0000182}
183
184TEST(ArgsTest, AcceptsTenTemplateArgs) {
185 EXPECT_THAT(make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
186 (Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
187 PrintsAs("(9, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
188 EXPECT_THAT(make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
189 Not(Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
190 PrintsAs("(0, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
191}
192
193TEST(ArgsTest, DescirbesSelfCorrectly) {
194 const Matcher<tuple<int, bool, char> > m = Args<2, 0>(Lt());
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000195 EXPECT_EQ("are a tuple whose fields (#2, #0) are a pair where "
196 "the first < the second",
zhanyong.wan2661c682009-06-09 05:42:12 +0000197 Describe(m));
198}
199
200TEST(ArgsTest, DescirbesNestedArgsCorrectly) {
201 const Matcher<const tuple<int, bool, char, int>&> m =
202 Args<0, 2, 3>(Args<2, 0>(Lt()));
203 EXPECT_EQ("are a tuple whose fields (#0, #2, #3) are a tuple "
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000204 "whose fields (#2, #0) are a pair where the first < the second",
zhanyong.wan2661c682009-06-09 05:42:12 +0000205 Describe(m));
206}
207
208TEST(ArgsTest, DescribesNegationCorrectly) {
209 const Matcher<tuple<int, char> > m = Args<1, 0>(Gt());
zhanyong.wanab5b77c2010-05-17 19:32:48 +0000210 EXPECT_EQ("are a tuple whose fields (#1, #0) aren't a pair "
211 "where the first > the second",
zhanyong.wan2661c682009-06-09 05:42:12 +0000212 DescribeNegation(m));
213}
214
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000215TEST(ArgsTest, ExplainsMatchResultWithoutInnerExplanation) {
216 const Matcher<tuple<bool, int, int> > m = Args<1, 2>(Eq());
217 EXPECT_EQ("whose fields (#1, #2) are (42, 42)",
218 Explain(m, make_tuple(false, 42, 42)));
219 EXPECT_EQ("whose fields (#1, #2) are (42, 43)",
220 Explain(m, make_tuple(false, 42, 43)));
221}
222
223// For testing Args<>'s explanation.
224class LessThanMatcher : public MatcherInterface<tuple<char, int> > {
225 public:
226 virtual void DescribeTo(::std::ostream* os) const {}
227
228 virtual bool MatchAndExplain(tuple<char, int> value,
229 MatchResultListener* listener) const {
230 const int diff = get<0>(value) - get<1>(value);
231 if (diff > 0) {
232 *listener << "where the first value is " << diff
233 << " more than the second";
234 }
235 return diff < 0;
236 }
237};
238
239Matcher<tuple<char, int> > LessThan() {
240 return MakeMatcher(new LessThanMatcher);
241}
242
243TEST(ArgsTest, ExplainsMatchResultWithInnerExplanation) {
244 const Matcher<tuple<char, int, int> > m = Args<0, 2>(LessThan());
245 EXPECT_EQ("whose fields (#0, #2) are ('a' (97), 42), "
246 "where the first value is 55 more than the second",
247 Explain(m, make_tuple('a', 42, 42)));
248 EXPECT_EQ("whose fields (#0, #2) are ('\\0', 43)",
249 Explain(m, make_tuple('\0', 42, 43)));
250}
251
shiqiane35fdd92008-12-10 05:08:54 +0000252// For testing ExplainMatchResultTo().
253class GreaterThanMatcher : public MatcherInterface<int> {
254 public:
255 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
256
shiqiane35fdd92008-12-10 05:08:54 +0000257 virtual void DescribeTo(::std::ostream* os) const {
258 *os << "is greater than " << rhs_;
259 }
260
zhanyong.wandb22c222010-01-28 21:52:29 +0000261 virtual bool MatchAndExplain(int lhs,
262 MatchResultListener* listener) const {
shiqiane35fdd92008-12-10 05:08:54 +0000263 const int diff = lhs - rhs_;
264 if (diff > 0) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000265 *listener << "which is " << diff << " more than " << rhs_;
shiqiane35fdd92008-12-10 05:08:54 +0000266 } else if (diff == 0) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000267 *listener << "which is the same as " << rhs_;
shiqiane35fdd92008-12-10 05:08:54 +0000268 } else {
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000269 *listener << "which is " << -diff << " less than " << rhs_;
shiqiane35fdd92008-12-10 05:08:54 +0000270 }
zhanyong.wandb22c222010-01-28 21:52:29 +0000271
272 return lhs > rhs_;
shiqiane35fdd92008-12-10 05:08:54 +0000273 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000274
shiqiane35fdd92008-12-10 05:08:54 +0000275 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000276 int rhs_;
shiqiane35fdd92008-12-10 05:08:54 +0000277};
278
279Matcher<int> GreaterThan(int n) {
280 return MakeMatcher(new GreaterThanMatcher(n));
281}
282
283// Tests for ElementsAre().
284
285// Evaluates to the number of elements in 'array'.
286#define GMOCK_ARRAY_SIZE_(array) (sizeof(array)/sizeof(array[0]))
287
288TEST(ElementsAreTest, CanDescribeExpectingNoElement) {
289 Matcher<const vector<int>&> m = ElementsAre();
290 EXPECT_EQ("is empty", Describe(m));
291}
292
293TEST(ElementsAreTest, CanDescribeExpectingOneElement) {
294 Matcher<vector<int> > m = ElementsAre(Gt(5));
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000295 EXPECT_EQ("has 1 element that is > 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000296}
297
298TEST(ElementsAreTest, CanDescribeExpectingManyElements) {
299 Matcher<list<string> > m = ElementsAre(StrEq("one"), "two");
300 EXPECT_EQ("has 2 elements where\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000301 "element #0 is equal to \"one\",\n"
302 "element #1 is equal to \"two\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000303}
304
305TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) {
306 Matcher<vector<int> > m = ElementsAre();
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000307 EXPECT_EQ("isn't empty", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +0000308}
309
310TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElment) {
311 Matcher<const list<int>& > m = ElementsAre(Gt(5));
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000312 EXPECT_EQ("doesn't have 1 element, or\n"
313 "element #0 isn't > 5", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +0000314}
315
316TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) {
317 Matcher<const list<string>& > m = ElementsAre("one", "two");
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000318 EXPECT_EQ("doesn't have 2 elements, or\n"
319 "element #0 isn't equal to \"one\", or\n"
320 "element #1 isn't equal to \"two\"", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +0000321}
322
323TEST(ElementsAreTest, DoesNotExplainTrivialMatch) {
324 Matcher<const list<int>& > m = ElementsAre(1, Ne(2));
325
326 list<int> test_list;
327 test_list.push_back(1);
328 test_list.push_back(3);
329 EXPECT_EQ("", Explain(m, test_list)); // No need to explain anything.
330}
331
332TEST(ElementsAreTest, ExplainsNonTrivialMatch) {
333 Matcher<const vector<int>& > m =
334 ElementsAre(GreaterThan(1), 0, GreaterThan(2));
335
336 const int a[] = { 10, 0, 100 };
337 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000338 EXPECT_EQ("whose element #0 matches, which is 9 more than 1,\n"
339 "and whose element #2 matches, which is 98 more than 2",
340 Explain(m, test_vector));
shiqiane35fdd92008-12-10 05:08:54 +0000341}
342
343TEST(ElementsAreTest, CanExplainMismatchWrongSize) {
344 Matcher<const list<int>& > m = ElementsAre(1, 3);
345
346 list<int> test_list;
347 // No need to explain when the container is empty.
348 EXPECT_EQ("", Explain(m, test_list));
349
350 test_list.push_back(1);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000351 EXPECT_EQ("which has 1 element", Explain(m, test_list));
shiqiane35fdd92008-12-10 05:08:54 +0000352}
353
354TEST(ElementsAreTest, CanExplainMismatchRightSize) {
355 Matcher<const vector<int>& > m = ElementsAre(1, GreaterThan(5));
356
357 vector<int> v;
358 v.push_back(2);
359 v.push_back(1);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000360 EXPECT_EQ("whose element #0 doesn't match", Explain(m, v));
shiqiane35fdd92008-12-10 05:08:54 +0000361
362 v[0] = 1;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000363 EXPECT_EQ("whose element #1 doesn't match, which is 4 less than 5",
364 Explain(m, v));
shiqiane35fdd92008-12-10 05:08:54 +0000365}
366
367TEST(ElementsAreTest, MatchesOneElementVector) {
368 vector<string> test_vector;
369 test_vector.push_back("test string");
370
371 EXPECT_THAT(test_vector, ElementsAre(StrEq("test string")));
372}
373
374TEST(ElementsAreTest, MatchesOneElementList) {
375 list<string> test_list;
376 test_list.push_back("test string");
377
378 EXPECT_THAT(test_list, ElementsAre("test string"));
379}
380
381TEST(ElementsAreTest, MatchesThreeElementVector) {
382 vector<string> test_vector;
383 test_vector.push_back("one");
384 test_vector.push_back("two");
385 test_vector.push_back("three");
386
387 EXPECT_THAT(test_vector, ElementsAre("one", StrEq("two"), _));
388}
389
390TEST(ElementsAreTest, MatchesOneElementEqMatcher) {
391 vector<int> test_vector;
392 test_vector.push_back(4);
393
394 EXPECT_THAT(test_vector, ElementsAre(Eq(4)));
395}
396
397TEST(ElementsAreTest, MatchesOneElementAnyMatcher) {
398 vector<int> test_vector;
399 test_vector.push_back(4);
400
401 EXPECT_THAT(test_vector, ElementsAre(_));
402}
403
404TEST(ElementsAreTest, MatchesOneElementValue) {
405 vector<int> test_vector;
406 test_vector.push_back(4);
407
408 EXPECT_THAT(test_vector, ElementsAre(4));
409}
410
411TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) {
412 vector<int> test_vector;
413 test_vector.push_back(1);
414 test_vector.push_back(2);
415 test_vector.push_back(3);
416
417 EXPECT_THAT(test_vector, ElementsAre(1, Eq(2), _));
418}
419
420TEST(ElementsAreTest, MatchesTenElementVector) {
421 const int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
422 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
423
424 EXPECT_THAT(test_vector,
425 // The element list can contain values and/or matchers
426 // of different types.
427 ElementsAre(0, Ge(0), _, 3, 4, Ne(2), Eq(6), 7, 8, _));
428}
429
430TEST(ElementsAreTest, DoesNotMatchWrongSize) {
431 vector<string> test_vector;
432 test_vector.push_back("test string");
433 test_vector.push_back("test string");
434
435 Matcher<vector<string> > m = ElementsAre(StrEq("test string"));
436 EXPECT_FALSE(m.Matches(test_vector));
437}
438
439TEST(ElementsAreTest, DoesNotMatchWrongValue) {
440 vector<string> test_vector;
441 test_vector.push_back("other string");
442
443 Matcher<vector<string> > m = ElementsAre(StrEq("test string"));
444 EXPECT_FALSE(m.Matches(test_vector));
445}
446
447TEST(ElementsAreTest, DoesNotMatchWrongOrder) {
448 vector<string> test_vector;
449 test_vector.push_back("one");
450 test_vector.push_back("three");
451 test_vector.push_back("two");
452
453 Matcher<vector<string> > m = ElementsAre(
454 StrEq("one"), StrEq("two"), StrEq("three"));
455 EXPECT_FALSE(m.Matches(test_vector));
456}
457
458TEST(ElementsAreTest, WorksForNestedContainer) {
459 const char* strings[] = {
460 "Hi",
461 "world"
462 };
463
464 vector<list<char> > nested;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000465 for (size_t i = 0; i < GMOCK_ARRAY_SIZE_(strings); i++) {
shiqiane35fdd92008-12-10 05:08:54 +0000466 nested.push_back(list<char>(strings[i], strings[i] + strlen(strings[i])));
467 }
468
469 EXPECT_THAT(nested, ElementsAre(ElementsAre('H', Ne('e')),
470 ElementsAre('w', 'o', _, _, 'd')));
471 EXPECT_THAT(nested, Not(ElementsAre(ElementsAre('H', 'e'),
472 ElementsAre('w', 'o', _, _, 'd'))));
473}
474
475TEST(ElementsAreTest, WorksWithByRefElementMatchers) {
476 int a[] = { 0, 1, 2 };
477 vector<int> v(a, a + GMOCK_ARRAY_SIZE_(a));
478
479 EXPECT_THAT(v, ElementsAre(Ref(v[0]), Ref(v[1]), Ref(v[2])));
480 EXPECT_THAT(v, Not(ElementsAre(Ref(v[0]), Ref(v[1]), Ref(a[2]))));
481}
482
483TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) {
484 int a[] = { 0, 1, 2 };
485 vector<int> v(a, a + GMOCK_ARRAY_SIZE_(a));
486
487 EXPECT_THAT(&v, Pointee(ElementsAre(0, 1, _)));
488 EXPECT_THAT(&v, Not(Pointee(ElementsAre(0, _, 3))));
489}
490
zhanyong.wanb8243162009-06-04 05:48:20 +0000491TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) {
492 int array[] = { 0, 1, 2 };
493 EXPECT_THAT(array, ElementsAre(0, 1, _));
494 EXPECT_THAT(array, Not(ElementsAre(1, _, _)));
495 EXPECT_THAT(array, Not(ElementsAre(0, _)));
496}
497
498class NativeArrayPassedAsPointerAndSize {
499 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000500 NativeArrayPassedAsPointerAndSize() {}
501
zhanyong.wanb8243162009-06-04 05:48:20 +0000502 MOCK_METHOD2(Helper, void(int* array, int size));
zhanyong.wan32de5f52009-12-23 00:13:23 +0000503
504 private:
505 GTEST_DISALLOW_COPY_AND_ASSIGN_(NativeArrayPassedAsPointerAndSize);
zhanyong.wanb8243162009-06-04 05:48:20 +0000506};
507
508TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) {
509 int array[] = { 0, 1 };
510 ::std::tr1::tuple<int*, size_t> array_as_tuple(array, 2);
511 EXPECT_THAT(array_as_tuple, ElementsAre(0, 1));
512 EXPECT_THAT(array_as_tuple, Not(ElementsAre(0)));
513
514 NativeArrayPassedAsPointerAndSize helper;
515 EXPECT_CALL(helper, Helper(_, _))
zhanyong.wanbf550852009-06-09 06:09:53 +0000516 .With(ElementsAre(0, 1));
zhanyong.wanb8243162009-06-04 05:48:20 +0000517 helper.Helper(array, 2);
518}
519
520TEST(ElementsAreTest, WorksWithTwoDimensionalNativeArray) {
521 const char a2[][3] = { "hi", "lo" };
522 EXPECT_THAT(a2, ElementsAre(ElementsAre('h', 'i', '\0'),
523 ElementsAre('l', 'o', '\0')));
524 EXPECT_THAT(a2, ElementsAre(StrEq("hi"), StrEq("lo")));
525 EXPECT_THAT(a2, ElementsAre(Not(ElementsAre('h', 'o', '\0')),
526 ElementsAre('l', 'o', '\0')));
527}
528
shiqiane35fdd92008-12-10 05:08:54 +0000529// Tests for ElementsAreArray(). Since ElementsAreArray() shares most
530// of the implementation with ElementsAre(), we don't test it as
531// thoroughly here.
532
533TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) {
534 const int a[] = { 1, 2, 3 };
535
536 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
537 EXPECT_THAT(test_vector, ElementsAreArray(a));
538
539 test_vector[2] = 0;
540 EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
541}
542
543TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) {
544 const char* a[] = { "one", "two", "three" };
545
546 vector<string> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
547 EXPECT_THAT(test_vector, ElementsAreArray(a, GMOCK_ARRAY_SIZE_(a)));
548
549 const char** p = a;
550 test_vector[0] = "1";
551 EXPECT_THAT(test_vector, Not(ElementsAreArray(p, GMOCK_ARRAY_SIZE_(a))));
552}
553
554TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) {
555 const char* a[] = { "one", "two", "three" };
556
557 vector<string> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
558 EXPECT_THAT(test_vector, ElementsAreArray(a));
559
560 test_vector[0] = "1";
561 EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
562}
563
564TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) {
565 const Matcher<string> kMatcherArray[] =
566 { StrEq("one"), StrEq("two"), StrEq("three") };
567
568 vector<string> test_vector;
569 test_vector.push_back("one");
570 test_vector.push_back("two");
571 test_vector.push_back("three");
572 EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray));
573
574 test_vector.push_back("three");
575 EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray)));
576}
577
zhanyong.wanb8243162009-06-04 05:48:20 +0000578// Since ElementsAre() and ElementsAreArray() share much of the
579// implementation, we only do a sanity test for native arrays here.
580TEST(ElementsAreArrayTest, WorksWithNativeArray) {
581 ::std::string a[] = { "hi", "ho" };
582 ::std::string b[] = { "hi", "ho" };
583
584 EXPECT_THAT(a, ElementsAreArray(b));
585 EXPECT_THAT(a, ElementsAreArray(b, 2));
586 EXPECT_THAT(a, Not(ElementsAreArray(b, 1)));
587}
588
zhanyong.wance198ff2009-02-12 01:34:27 +0000589// Tests for the MATCHER*() macro family.
590
591// Tests that a simple MATCHER() definition works.
592
593MATCHER(IsEven, "") { return (arg % 2) == 0; }
594
595TEST(MatcherMacroTest, Works) {
596 const Matcher<int> m = IsEven();
597 EXPECT_TRUE(m.Matches(6));
598 EXPECT_FALSE(m.Matches(7));
599
600 EXPECT_EQ("is even", Describe(m));
601 EXPECT_EQ("not (is even)", DescribeNegation(m));
602 EXPECT_EQ("", Explain(m, 6));
603 EXPECT_EQ("", Explain(m, 7));
604}
605
zhanyong.wan82113312010-01-08 21:55:40 +0000606// Tests explaining match result in a MATCHER* macro.
607
608MATCHER(IsEven2, "is even") {
609 if ((arg % 2) == 0) {
610 // Verifies that we can stream to result_listener, a listener
611 // supplied by the MATCHER macro implicitly.
612 *result_listener << "OK";
613 return true;
614 } else {
615 *result_listener << "% 2 == " << (arg % 2);
616 return false;
617 }
618}
619
620MATCHER_P2(EqSumOf, x, y, "") {
621 if (arg == (x + y)) {
622 *result_listener << "OK";
623 return true;
624 } else {
625 // Verifies that we can stream to the underlying stream of
626 // result_listener.
627 if (result_listener->stream() != NULL) {
628 *result_listener->stream() << "diff == " << (x + y - arg);
629 }
630 return false;
631 }
632}
633
634TEST(MatcherMacroTest, CanExplainMatchResult) {
635 const Matcher<int> m1 = IsEven2();
636 EXPECT_EQ("OK", Explain(m1, 4));
637 EXPECT_EQ("% 2 == 1", Explain(m1, 5));
638
639 const Matcher<int> m2 = EqSumOf(1, 2);
640 EXPECT_EQ("OK", Explain(m2, 3));
641 EXPECT_EQ("diff == -1", Explain(m2, 4));
642}
643
zhanyong.wance198ff2009-02-12 01:34:27 +0000644// Tests that the description string supplied to MATCHER() must be
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000645// valid.
zhanyong.wance198ff2009-02-12 01:34:27 +0000646
zhanyong.wan32de5f52009-12-23 00:13:23 +0000647MATCHER(HasBadDescription, "Invalid%") {
648 // Uses arg to suppress "unused parameter" warning.
649 return arg==arg;
650}
zhanyong.wance198ff2009-02-12 01:34:27 +0000651
652TEST(MatcherMacroTest,
653 CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) {
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000654 EXPECT_NONFATAL_FAILURE(
655 HasBadDescription(),
656 "Syntax error at index 7 in matcher description \"Invalid%\": "
657 "use \"%%\" instead of \"%\" to print \"%\".");
658}
659
zhanyong.wan32de5f52009-12-23 00:13:23 +0000660MATCHER(HasGoodDescription, "good") { return arg==arg; }
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000661
662TEST(MatcherMacroTest, AcceptsValidDescription) {
663 const Matcher<int> m = HasGoodDescription();
664 EXPECT_EQ("good", Describe(m));
zhanyong.wance198ff2009-02-12 01:34:27 +0000665}
666
667// Tests that the body of MATCHER() can reference the type of the
668// value being matched.
669
670MATCHER(IsEmptyString, "") {
671 StaticAssertTypeEq< ::std::string, arg_type>();
672 return arg == "";
673}
674
675MATCHER(IsEmptyStringByRef, "") {
676 StaticAssertTypeEq<const ::std::string&, arg_type>();
677 return arg == "";
678}
679
680TEST(MatcherMacroTest, CanReferenceArgType) {
681 const Matcher< ::std::string> m1 = IsEmptyString();
682 EXPECT_TRUE(m1.Matches(""));
683
684 const Matcher<const ::std::string&> m2 = IsEmptyStringByRef();
685 EXPECT_TRUE(m2.Matches(""));
686}
687
688// Tests that MATCHER() can be used in a namespace.
689
690namespace matcher_test {
691MATCHER(IsOdd, "") { return (arg % 2) != 0; }
692} // namespace matcher_test
693
zhanyong.wanb8243162009-06-04 05:48:20 +0000694TEST(MatcherMacroTest, WorksInNamespace) {
zhanyong.wance198ff2009-02-12 01:34:27 +0000695 Matcher<int> m = matcher_test::IsOdd();
696 EXPECT_FALSE(m.Matches(4));
697 EXPECT_TRUE(m.Matches(5));
698}
699
zhanyong.wanb8243162009-06-04 05:48:20 +0000700// Tests that Value() can be used to compose matchers.
701MATCHER(IsPositiveOdd, "") {
702 return Value(arg, matcher_test::IsOdd()) && arg > 0;
703}
704
705TEST(MatcherMacroTest, CanBeComposedUsingValue) {
706 EXPECT_THAT(3, IsPositiveOdd());
707 EXPECT_THAT(4, Not(IsPositiveOdd()));
708 EXPECT_THAT(-1, Not(IsPositiveOdd()));
709}
710
zhanyong.wance198ff2009-02-12 01:34:27 +0000711// Tests that a simple MATCHER_P() definition works.
712
713MATCHER_P(IsGreaterThan32And, n, "") { return arg > 32 && arg > n; }
714
715TEST(MatcherPMacroTest, Works) {
716 const Matcher<int> m = IsGreaterThan32And(5);
717 EXPECT_TRUE(m.Matches(36));
718 EXPECT_FALSE(m.Matches(5));
719
720 EXPECT_EQ("is greater than 32 and 5", Describe(m));
721 EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m));
722 EXPECT_EQ("", Explain(m, 36));
723 EXPECT_EQ("", Explain(m, 5));
724}
725
726// Tests that the description string supplied to MATCHER_P() must be
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000727// valid.
zhanyong.wance198ff2009-02-12 01:34:27 +0000728
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000729MATCHER_P(HasBadDescription1, n, "not %(m)s good") {
zhanyong.wance198ff2009-02-12 01:34:27 +0000730 return arg > n;
731}
732
733TEST(MatcherPMacroTest,
734 CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) {
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000735 EXPECT_NONFATAL_FAILURE(
736 HasBadDescription1(2),
737 "Syntax error at index 6 in matcher description \"not %(m)s good\": "
738 "\"m\" is an invalid parameter name.");
739}
740
741
zhanyong.wan32de5f52009-12-23 00:13:23 +0000742MATCHER_P(HasGoodDescription1, n, "good %(n)s") { return arg==arg; }
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000743
744TEST(MatcherPMacroTest, AcceptsValidDescription) {
745 const Matcher<int> m = HasGoodDescription1(5);
746 EXPECT_EQ("good 5", Describe(m));
zhanyong.wance198ff2009-02-12 01:34:27 +0000747}
748
749// Tests that the description is calculated correctly from the matcher name.
750MATCHER_P(_is_Greater_Than32and_, n, "") { return arg > 32 && arg > n; }
751
752TEST(MatcherPMacroTest, GeneratesCorrectDescription) {
753 const Matcher<int> m = _is_Greater_Than32and_(5);
754
755 EXPECT_EQ("is greater than 32 and 5", Describe(m));
756 EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m));
757 EXPECT_EQ("", Explain(m, 36));
758 EXPECT_EQ("", Explain(m, 5));
759}
760
761// Tests that a MATCHER_P matcher can be explicitly instantiated with
762// a reference parameter type.
763
764class UncopyableFoo {
765 public:
766 explicit UncopyableFoo(char value) : value_(value) {}
767 private:
768 UncopyableFoo(const UncopyableFoo&);
769 void operator=(const UncopyableFoo&);
770
771 char value_;
772};
773
774MATCHER_P(ReferencesUncopyable, variable, "") { return &arg == &variable; }
775
776TEST(MatcherPMacroTest, WorksWhenExplicitlyInstantiatedWithReference) {
777 UncopyableFoo foo1('1'), foo2('2');
778 const Matcher<const UncopyableFoo&> m =
779 ReferencesUncopyable<const UncopyableFoo&>(foo1);
780
781 EXPECT_TRUE(m.Matches(foo1));
782 EXPECT_FALSE(m.Matches(foo2));
783
784 // We don't want the address of the parameter printed, as most
785 // likely it will just annoy the user. If the address is
786 // interesting, the user should consider passing the parameter by
787 // pointer instead.
788 EXPECT_EQ("references uncopyable 1-byte object <31>", Describe(m));
789}
790
791
792// Tests that the description string supplied to MATCHER_Pn() must be
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000793// valid.
zhanyong.wance198ff2009-02-12 01:34:27 +0000794
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000795MATCHER_P2(HasBadDescription2, m, n, "not %(good") {
zhanyong.wance198ff2009-02-12 01:34:27 +0000796 return arg > m + n;
797}
798
799TEST(MatcherPnMacroTest,
800 CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) {
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000801 EXPECT_NONFATAL_FAILURE(
802 HasBadDescription2(3, 4),
803 "Syntax error at index 4 in matcher description \"not %(good\": "
804 "an interpolation must end with \")s\", but \"%(good\" does not.");
805}
806
807MATCHER_P2(HasComplexDescription, foo, bar,
808 "is as complex as %(foo)s %(bar)s (i.e. %(*)s or %%%(foo)s!)") {
zhanyong.wan32de5f52009-12-23 00:13:23 +0000809 return arg==arg;
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000810}
811
812TEST(MatcherPnMacroTest, AcceptsValidDescription) {
813 Matcher<int> m = HasComplexDescription(100, "ducks");
814 EXPECT_EQ("is as complex as 100 \"ducks\" (i.e. (100, \"ducks\") or %100!)",
815 Describe(m));
zhanyong.wance198ff2009-02-12 01:34:27 +0000816}
817
818// Tests that the body of MATCHER_Pn() can reference the parameter
819// types.
820
821MATCHER_P3(ParamTypesAreIntLongAndChar, foo, bar, baz, "") {
822 StaticAssertTypeEq<int, foo_type>();
823 StaticAssertTypeEq<long, bar_type>(); // NOLINT
824 StaticAssertTypeEq<char, baz_type>();
825 return arg == 0;
826}
827
828TEST(MatcherPnMacroTest, CanReferenceParamTypes) {
829 EXPECT_THAT(0, ParamTypesAreIntLongAndChar(10, 20L, 'a'));
830}
831
832// Tests that a MATCHER_Pn matcher can be explicitly instantiated with
833// reference parameter types.
834
835MATCHER_P2(ReferencesAnyOf, variable1, variable2, "") {
836 return &arg == &variable1 || &arg == &variable2;
837}
838
839TEST(MatcherPnMacroTest, WorksWhenExplicitlyInstantiatedWithReferences) {
840 UncopyableFoo foo1('1'), foo2('2'), foo3('3');
841 const Matcher<const UncopyableFoo&> m =
842 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
843
844 EXPECT_TRUE(m.Matches(foo1));
845 EXPECT_TRUE(m.Matches(foo2));
846 EXPECT_FALSE(m.Matches(foo3));
847}
848
849TEST(MatcherPnMacroTest,
850 GeneratesCorretDescriptionWhenExplicitlyInstantiatedWithReferences) {
851 UncopyableFoo foo1('1'), foo2('2');
852 const Matcher<const UncopyableFoo&> m =
853 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
854
855 // We don't want the addresses of the parameters printed, as most
856 // likely they will just annoy the user. If the addresses are
857 // interesting, the user should consider passing the parameters by
858 // pointers instead.
859 EXPECT_EQ("references any of (1-byte object <31>, 1-byte object <32>)",
860 Describe(m));
861}
862
863// Tests that a simple MATCHER_P2() definition works.
864
865MATCHER_P2(IsNotInClosedRange, low, hi, "") { return arg < low || arg > hi; }
866
867TEST(MatcherPnMacroTest, Works) {
868 const Matcher<const long&> m = IsNotInClosedRange(10, 20); // NOLINT
869 EXPECT_TRUE(m.Matches(36L));
870 EXPECT_FALSE(m.Matches(15L));
871
872 EXPECT_EQ("is not in closed range (10, 20)", Describe(m));
873 EXPECT_EQ("not (is not in closed range (10, 20))", DescribeNegation(m));
874 EXPECT_EQ("", Explain(m, 36L));
875 EXPECT_EQ("", Explain(m, 15L));
876}
877
878// Tests that MATCHER*() definitions can be overloaded on the number
879// of parameters; also tests MATCHER_Pn() where n >= 3.
880
881MATCHER(EqualsSumOf, "") { return arg == 0; }
882MATCHER_P(EqualsSumOf, a, "") { return arg == a; }
883MATCHER_P2(EqualsSumOf, a, b, "") { return arg == a + b; }
884MATCHER_P3(EqualsSumOf, a, b, c, "") { return arg == a + b + c; }
885MATCHER_P4(EqualsSumOf, a, b, c, d, "") { return arg == a + b + c + d; }
886MATCHER_P5(EqualsSumOf, a, b, c, d, e, "") { return arg == a + b + c + d + e; }
887MATCHER_P6(EqualsSumOf, a, b, c, d, e, f, "") {
888 return arg == a + b + c + d + e + f;
889}
890MATCHER_P7(EqualsSumOf, a, b, c, d, e, f, g, "") {
891 return arg == a + b + c + d + e + f + g;
892}
893MATCHER_P8(EqualsSumOf, a, b, c, d, e, f, g, h, "") {
894 return arg == a + b + c + d + e + f + g + h;
895}
896MATCHER_P9(EqualsSumOf, a, b, c, d, e, f, g, h, i, "") {
897 return arg == a + b + c + d + e + f + g + h + i;
898}
899MATCHER_P10(EqualsSumOf, a, b, c, d, e, f, g, h, i, j, "") {
900 return arg == a + b + c + d + e + f + g + h + i + j;
901}
902
903TEST(MatcherPnMacroTest, CanBeOverloadedOnNumberOfParameters) {
904 EXPECT_THAT(0, EqualsSumOf());
905 EXPECT_THAT(1, EqualsSumOf(1));
906 EXPECT_THAT(12, EqualsSumOf(10, 2));
907 EXPECT_THAT(123, EqualsSumOf(100, 20, 3));
908 EXPECT_THAT(1234, EqualsSumOf(1000, 200, 30, 4));
909 EXPECT_THAT(12345, EqualsSumOf(10000, 2000, 300, 40, 5));
910 EXPECT_THAT("abcdef",
911 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f'));
912 EXPECT_THAT("abcdefg",
913 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g'));
914 EXPECT_THAT("abcdefgh",
915 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
916 "h"));
917 EXPECT_THAT("abcdefghi",
918 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
919 "h", 'i'));
920 EXPECT_THAT("abcdefghij",
921 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
922 "h", 'i', ::std::string("j")));
923
924 EXPECT_THAT(1, Not(EqualsSumOf()));
925 EXPECT_THAT(-1, Not(EqualsSumOf(1)));
926 EXPECT_THAT(-12, Not(EqualsSumOf(10, 2)));
927 EXPECT_THAT(-123, Not(EqualsSumOf(100, 20, 3)));
928 EXPECT_THAT(-1234, Not(EqualsSumOf(1000, 200, 30, 4)));
929 EXPECT_THAT(-12345, Not(EqualsSumOf(10000, 2000, 300, 40, 5)));
930 EXPECT_THAT("abcdef ",
931 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f')));
932 EXPECT_THAT("abcdefg ",
933 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f',
934 'g')));
935 EXPECT_THAT("abcdefgh ",
936 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
937 "h")));
938 EXPECT_THAT("abcdefghi ",
939 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
940 "h", 'i')));
941 EXPECT_THAT("abcdefghij ",
942 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
943 "h", 'i', ::std::string("j"))));
944}
945
946// Tests that a MATCHER_Pn() definition can be instantiated with any
947// compatible parameter types.
948TEST(MatcherPnMacroTest, WorksForDifferentParameterTypes) {
949 EXPECT_THAT(123, EqualsSumOf(100L, 20, static_cast<char>(3)));
950 EXPECT_THAT("abcd", EqualsSumOf(::std::string("a"), "b", 'c', "d"));
951
952 EXPECT_THAT(124, Not(EqualsSumOf(100L, 20, static_cast<char>(3))));
953 EXPECT_THAT("abcde", Not(EqualsSumOf(::std::string("a"), "b", 'c', "d")));
954}
955
956// Tests that the matcher body can promote the parameter types.
957
958MATCHER_P2(EqConcat, prefix, suffix, "") {
959 // The following lines promote the two parameters to desired types.
960 std::string prefix_str(prefix);
zhanyong.wan32de5f52009-12-23 00:13:23 +0000961 char suffix_char = static_cast<char>(suffix);
zhanyong.wance198ff2009-02-12 01:34:27 +0000962 return arg == prefix_str + suffix_char;
963}
964
965TEST(MatcherPnMacroTest, SimpleTypePromotion) {
966 Matcher<std::string> no_promo =
967 EqConcat(std::string("foo"), 't');
968 Matcher<const std::string&> promo =
969 EqConcat("foo", static_cast<int>('t'));
970 EXPECT_FALSE(no_promo.Matches("fool"));
971 EXPECT_FALSE(promo.Matches("fool"));
972 EXPECT_TRUE(no_promo.Matches("foot"));
973 EXPECT_TRUE(promo.Matches("foot"));
974}
975
976// Verifies the type of a MATCHER*.
977
978TEST(MatcherPnMacroTest, TypesAreCorrect) {
979 // EqualsSumOf() must be assignable to a EqualsSumOfMatcher variable.
980 EqualsSumOfMatcher a0 = EqualsSumOf();
981
982 // EqualsSumOf(1) must be assignable to a EqualsSumOfMatcherP variable.
983 EqualsSumOfMatcherP<int> a1 = EqualsSumOf(1);
984
985 // EqualsSumOf(p1, ..., pk) must be assignable to a EqualsSumOfMatcherPk
986 // variable, and so on.
987 EqualsSumOfMatcherP2<int, char> a2 = EqualsSumOf(1, '2');
988 EqualsSumOfMatcherP3<int, int, char> a3 = EqualsSumOf(1, 2, '3');
989 EqualsSumOfMatcherP4<int, int, int, char> a4 = EqualsSumOf(1, 2, 3, '4');
990 EqualsSumOfMatcherP5<int, int, int, int, char> a5 =
991 EqualsSumOf(1, 2, 3, 4, '5');
992 EqualsSumOfMatcherP6<int, int, int, int, int, char> a6 =
993 EqualsSumOf(1, 2, 3, 4, 5, '6');
994 EqualsSumOfMatcherP7<int, int, int, int, int, int, char> a7 =
995 EqualsSumOf(1, 2, 3, 4, 5, 6, '7');
996 EqualsSumOfMatcherP8<int, int, int, int, int, int, int, char> a8 =
997 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, '8');
998 EqualsSumOfMatcherP9<int, int, int, int, int, int, int, int, char> a9 =
999 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, '9');
1000 EqualsSumOfMatcherP10<int, int, int, int, int, int, int, int, int, char> a10 =
1001 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, 9, '0');
1002}
1003
zhanyong.wanb8243162009-06-04 05:48:20 +00001004// Tests that matcher-typed parameters can be used in Value() inside a
1005// MATCHER_Pn definition.
1006
1007// Succeeds if arg matches exactly 2 of the 3 matchers.
1008MATCHER_P3(TwoOf, m1, m2, m3, "") {
1009 const int count = static_cast<int>(Value(arg, m1))
1010 + static_cast<int>(Value(arg, m2)) + static_cast<int>(Value(arg, m3));
1011 return count == 2;
1012}
1013
1014TEST(MatcherPnMacroTest, CanUseMatcherTypedParameterInValue) {
1015 EXPECT_THAT(42, TwoOf(Gt(0), Lt(50), Eq(10)));
1016 EXPECT_THAT(0, Not(TwoOf(Gt(-1), Lt(1), Eq(0))));
1017}
1018
1019// Tests Contains().
1020
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001021TEST(ContainsTest, ListMatchesWhenElementIsInContainer) {
1022 list<int> some_list;
1023 some_list.push_back(3);
1024 some_list.push_back(1);
1025 some_list.push_back(2);
1026 EXPECT_THAT(some_list, Contains(1));
zhanyong.wanb8243162009-06-04 05:48:20 +00001027 EXPECT_THAT(some_list, Contains(Gt(2.5)));
1028 EXPECT_THAT(some_list, Contains(Eq(2.0f)));
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001029
1030 list<string> another_list;
1031 another_list.push_back("fee");
1032 another_list.push_back("fie");
1033 another_list.push_back("foe");
1034 another_list.push_back("fum");
1035 EXPECT_THAT(another_list, Contains(string("fee")));
1036}
1037
1038TEST(ContainsTest, ListDoesNotMatchWhenElementIsNotInContainer) {
1039 list<int> some_list;
1040 some_list.push_back(3);
1041 some_list.push_back(1);
1042 EXPECT_THAT(some_list, Not(Contains(4)));
1043}
1044
1045TEST(ContainsTest, SetMatchesWhenElementIsInContainer) {
1046 set<int> some_set;
1047 some_set.insert(3);
1048 some_set.insert(1);
1049 some_set.insert(2);
zhanyong.wanb8243162009-06-04 05:48:20 +00001050 EXPECT_THAT(some_set, Contains(Eq(1.0)));
1051 EXPECT_THAT(some_set, Contains(Eq(3.0f)));
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001052 EXPECT_THAT(some_set, Contains(2));
1053
1054 set<const char*> another_set;
1055 another_set.insert("fee");
1056 another_set.insert("fie");
1057 another_set.insert("foe");
1058 another_set.insert("fum");
zhanyong.wanb8243162009-06-04 05:48:20 +00001059 EXPECT_THAT(another_set, Contains(Eq(string("fum"))));
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001060}
1061
1062TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) {
1063 set<int> some_set;
1064 some_set.insert(3);
1065 some_set.insert(1);
1066 EXPECT_THAT(some_set, Not(Contains(4)));
1067
1068 set<const char*> c_string_set;
1069 c_string_set.insert("hello");
1070 EXPECT_THAT(c_string_set, Not(Contains(string("hello").c_str())));
1071}
1072
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001073TEST(ContainsTest, ExplainsMatchResultCorrectly) {
zhanyong.wanb8243162009-06-04 05:48:20 +00001074 const int a[2] = { 1, 2 };
1075 Matcher<const int(&)[2]> m = Contains(2);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001076 EXPECT_EQ("whose element #1 matches", Explain(m, a));
zhanyong.wanb8243162009-06-04 05:48:20 +00001077
1078 m = Contains(3);
1079 EXPECT_EQ("", Explain(m, a));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001080
1081 m = Contains(GreaterThan(0));
1082 EXPECT_EQ("whose element #0 matches, which is 1 more than 0", Explain(m, a));
1083
1084 m = Contains(GreaterThan(10));
1085 EXPECT_EQ("", Explain(m, a));
zhanyong.wanb8243162009-06-04 05:48:20 +00001086}
1087
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001088TEST(ContainsTest, DescribesItselfCorrectly) {
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001089 Matcher<vector<int> > m = Contains(1);
zhanyong.wanb8243162009-06-04 05:48:20 +00001090 EXPECT_EQ("contains at least one element that is equal to 1", Describe(m));
1091
1092 Matcher<vector<int> > m2 = Not(m);
1093 EXPECT_EQ("doesn't contain any element that is equal to 1", Describe(m2));
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001094}
1095
1096TEST(ContainsTest, MapMatchesWhenElementIsInContainer) {
1097 map<const char*, int> my_map;
1098 const char* bar = "a string";
1099 my_map[bar] = 2;
1100 EXPECT_THAT(my_map, Contains(pair<const char* const, int>(bar, 2)));
1101
1102 map<string, int> another_map;
1103 another_map["fee"] = 1;
1104 another_map["fie"] = 2;
1105 another_map["foe"] = 3;
1106 another_map["fum"] = 4;
1107 EXPECT_THAT(another_map, Contains(pair<const string, int>(string("fee"), 1)));
1108 EXPECT_THAT(another_map, Contains(pair<const string, int>("fie", 2)));
1109}
1110
1111TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) {
1112 map<int, int> some_map;
1113 some_map[1] = 11;
1114 some_map[2] = 22;
1115 EXPECT_THAT(some_map, Not(Contains(pair<const int, int>(2, 23))));
1116}
1117
1118TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) {
1119 const char* string_array[] = { "fee", "fie", "foe", "fum" };
zhanyong.wanb8243162009-06-04 05:48:20 +00001120 EXPECT_THAT(string_array, Contains(Eq(string("fum"))));
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001121}
1122
1123TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) {
1124 int int_array[] = { 1, 2, 3, 4 };
1125 EXPECT_THAT(int_array, Not(Contains(5)));
1126}
1127
zhanyong.wanb8243162009-06-04 05:48:20 +00001128TEST(ContainsTest, AcceptsMatcher) {
1129 const int a[] = { 1, 2, 3 };
1130 EXPECT_THAT(a, Contains(Gt(2)));
1131 EXPECT_THAT(a, Not(Contains(Gt(4))));
1132}
1133
1134TEST(ContainsTest, WorksForNativeArrayAsTuple) {
1135 const int a[] = { 1, 2 };
zhanyong.wan2661c682009-06-09 05:42:12 +00001136 const int* const pointer = a;
1137 EXPECT_THAT(make_tuple(pointer, 2), Contains(1));
1138 EXPECT_THAT(make_tuple(pointer, 2), Not(Contains(Gt(3))));
zhanyong.wanb8243162009-06-04 05:48:20 +00001139}
1140
1141TEST(ContainsTest, WorksForTwoDimensionalNativeArray) {
1142 int a[][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
1143 EXPECT_THAT(a, Contains(ElementsAre(4, 5, 6)));
1144 EXPECT_THAT(a, Contains(Contains(5)));
1145 EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5))));
1146 EXPECT_THAT(a, Contains(Not(Contains(5))));
1147}
1148
zhanyong.wan82113312010-01-08 21:55:40 +00001149#ifdef _MSC_VER
1150#pragma warning(pop)
1151#endif
1152
shiqiane35fdd92008-12-10 05:08:54 +00001153} // namespace