blob: 12479af4c421739ee5712dce586a5c399ef0890c [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());
195 EXPECT_EQ("are a tuple whose fields (#2, #0) are a pair (x, y) where x < y",
196 Describe(m));
197}
198
199TEST(ArgsTest, DescirbesNestedArgsCorrectly) {
200 const Matcher<const tuple<int, bool, char, int>&> m =
201 Args<0, 2, 3>(Args<2, 0>(Lt()));
202 EXPECT_EQ("are a tuple whose fields (#0, #2, #3) are a tuple "
203 "whose fields (#2, #0) are a pair (x, y) where x < y",
204 Describe(m));
205}
206
207TEST(ArgsTest, DescribesNegationCorrectly) {
208 const Matcher<tuple<int, char> > m = Args<1, 0>(Gt());
209 EXPECT_EQ("are a tuple whose fields (#1, #0) are a pair (x, y) "
210 "where x > y is false",
211 DescribeNegation(m));
212}
213
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000214TEST(ArgsTest, ExplainsMatchResultWithoutInnerExplanation) {
215 const Matcher<tuple<bool, int, int> > m = Args<1, 2>(Eq());
216 EXPECT_EQ("whose fields (#1, #2) are (42, 42)",
217 Explain(m, make_tuple(false, 42, 42)));
218 EXPECT_EQ("whose fields (#1, #2) are (42, 43)",
219 Explain(m, make_tuple(false, 42, 43)));
220}
221
222// For testing Args<>'s explanation.
223class LessThanMatcher : public MatcherInterface<tuple<char, int> > {
224 public:
225 virtual void DescribeTo(::std::ostream* os) const {}
226
227 virtual bool MatchAndExplain(tuple<char, int> value,
228 MatchResultListener* listener) const {
229 const int diff = get<0>(value) - get<1>(value);
230 if (diff > 0) {
231 *listener << "where the first value is " << diff
232 << " more than the second";
233 }
234 return diff < 0;
235 }
236};
237
238Matcher<tuple<char, int> > LessThan() {
239 return MakeMatcher(new LessThanMatcher);
240}
241
242TEST(ArgsTest, ExplainsMatchResultWithInnerExplanation) {
243 const Matcher<tuple<char, int, int> > m = Args<0, 2>(LessThan());
244 EXPECT_EQ("whose fields (#0, #2) are ('a' (97), 42), "
245 "where the first value is 55 more than the second",
246 Explain(m, make_tuple('a', 42, 42)));
247 EXPECT_EQ("whose fields (#0, #2) are ('\\0', 43)",
248 Explain(m, make_tuple('\0', 42, 43)));
249}
250
shiqiane35fdd92008-12-10 05:08:54 +0000251// For testing ExplainMatchResultTo().
252class GreaterThanMatcher : public MatcherInterface<int> {
253 public:
254 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
255
shiqiane35fdd92008-12-10 05:08:54 +0000256 virtual void DescribeTo(::std::ostream* os) const {
257 *os << "is greater than " << rhs_;
258 }
259
zhanyong.wandb22c222010-01-28 21:52:29 +0000260 virtual bool MatchAndExplain(int lhs,
261 MatchResultListener* listener) const {
shiqiane35fdd92008-12-10 05:08:54 +0000262 const int diff = lhs - rhs_;
263 if (diff > 0) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000264 *listener << "which is " << diff << " more than " << rhs_;
shiqiane35fdd92008-12-10 05:08:54 +0000265 } else if (diff == 0) {
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000266 *listener << "which is the same as " << rhs_;
shiqiane35fdd92008-12-10 05:08:54 +0000267 } else {
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000268 *listener << "which is " << -diff << " less than " << rhs_;
shiqiane35fdd92008-12-10 05:08:54 +0000269 }
zhanyong.wandb22c222010-01-28 21:52:29 +0000270
271 return lhs > rhs_;
shiqiane35fdd92008-12-10 05:08:54 +0000272 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000273
shiqiane35fdd92008-12-10 05:08:54 +0000274 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000275 int rhs_;
shiqiane35fdd92008-12-10 05:08:54 +0000276};
277
278Matcher<int> GreaterThan(int n) {
279 return MakeMatcher(new GreaterThanMatcher(n));
280}
281
282// Tests for ElementsAre().
283
284// Evaluates to the number of elements in 'array'.
285#define GMOCK_ARRAY_SIZE_(array) (sizeof(array)/sizeof(array[0]))
286
287TEST(ElementsAreTest, CanDescribeExpectingNoElement) {
288 Matcher<const vector<int>&> m = ElementsAre();
289 EXPECT_EQ("is empty", Describe(m));
290}
291
292TEST(ElementsAreTest, CanDescribeExpectingOneElement) {
293 Matcher<vector<int> > m = ElementsAre(Gt(5));
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000294 EXPECT_EQ("has 1 element that is > 5", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000295}
296
297TEST(ElementsAreTest, CanDescribeExpectingManyElements) {
298 Matcher<list<string> > m = ElementsAre(StrEq("one"), "two");
299 EXPECT_EQ("has 2 elements where\n"
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000300 "element #0 is equal to \"one\",\n"
301 "element #1 is equal to \"two\"", Describe(m));
shiqiane35fdd92008-12-10 05:08:54 +0000302}
303
304TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) {
305 Matcher<vector<int> > m = ElementsAre();
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000306 EXPECT_EQ("isn't empty", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +0000307}
308
309TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElment) {
310 Matcher<const list<int>& > m = ElementsAre(Gt(5));
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000311 EXPECT_EQ("doesn't have 1 element, or\n"
312 "element #0 isn't > 5", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +0000313}
314
315TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) {
316 Matcher<const list<string>& > m = ElementsAre("one", "two");
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000317 EXPECT_EQ("doesn't have 2 elements, or\n"
318 "element #0 isn't equal to \"one\", or\n"
319 "element #1 isn't equal to \"two\"", DescribeNegation(m));
shiqiane35fdd92008-12-10 05:08:54 +0000320}
321
322TEST(ElementsAreTest, DoesNotExplainTrivialMatch) {
323 Matcher<const list<int>& > m = ElementsAre(1, Ne(2));
324
325 list<int> test_list;
326 test_list.push_back(1);
327 test_list.push_back(3);
328 EXPECT_EQ("", Explain(m, test_list)); // No need to explain anything.
329}
330
331TEST(ElementsAreTest, ExplainsNonTrivialMatch) {
332 Matcher<const vector<int>& > m =
333 ElementsAre(GreaterThan(1), 0, GreaterThan(2));
334
335 const int a[] = { 10, 0, 100 };
336 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000337 EXPECT_EQ("whose element #0 matches, which is 9 more than 1,\n"
338 "and whose element #2 matches, which is 98 more than 2",
339 Explain(m, test_vector));
shiqiane35fdd92008-12-10 05:08:54 +0000340}
341
342TEST(ElementsAreTest, CanExplainMismatchWrongSize) {
343 Matcher<const list<int>& > m = ElementsAre(1, 3);
344
345 list<int> test_list;
346 // No need to explain when the container is empty.
347 EXPECT_EQ("", Explain(m, test_list));
348
349 test_list.push_back(1);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000350 EXPECT_EQ("which has 1 element", Explain(m, test_list));
shiqiane35fdd92008-12-10 05:08:54 +0000351}
352
353TEST(ElementsAreTest, CanExplainMismatchRightSize) {
354 Matcher<const vector<int>& > m = ElementsAre(1, GreaterThan(5));
355
356 vector<int> v;
357 v.push_back(2);
358 v.push_back(1);
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000359 EXPECT_EQ("whose element #0 doesn't match", Explain(m, v));
shiqiane35fdd92008-12-10 05:08:54 +0000360
361 v[0] = 1;
zhanyong.wanb1c7f932010-03-24 17:35:11 +0000362 EXPECT_EQ("whose element #1 doesn't match, which is 4 less than 5",
363 Explain(m, v));
shiqiane35fdd92008-12-10 05:08:54 +0000364}
365
366TEST(ElementsAreTest, MatchesOneElementVector) {
367 vector<string> test_vector;
368 test_vector.push_back("test string");
369
370 EXPECT_THAT(test_vector, ElementsAre(StrEq("test string")));
371}
372
373TEST(ElementsAreTest, MatchesOneElementList) {
374 list<string> test_list;
375 test_list.push_back("test string");
376
377 EXPECT_THAT(test_list, ElementsAre("test string"));
378}
379
380TEST(ElementsAreTest, MatchesThreeElementVector) {
381 vector<string> test_vector;
382 test_vector.push_back("one");
383 test_vector.push_back("two");
384 test_vector.push_back("three");
385
386 EXPECT_THAT(test_vector, ElementsAre("one", StrEq("two"), _));
387}
388
389TEST(ElementsAreTest, MatchesOneElementEqMatcher) {
390 vector<int> test_vector;
391 test_vector.push_back(4);
392
393 EXPECT_THAT(test_vector, ElementsAre(Eq(4)));
394}
395
396TEST(ElementsAreTest, MatchesOneElementAnyMatcher) {
397 vector<int> test_vector;
398 test_vector.push_back(4);
399
400 EXPECT_THAT(test_vector, ElementsAre(_));
401}
402
403TEST(ElementsAreTest, MatchesOneElementValue) {
404 vector<int> test_vector;
405 test_vector.push_back(4);
406
407 EXPECT_THAT(test_vector, ElementsAre(4));
408}
409
410TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) {
411 vector<int> test_vector;
412 test_vector.push_back(1);
413 test_vector.push_back(2);
414 test_vector.push_back(3);
415
416 EXPECT_THAT(test_vector, ElementsAre(1, Eq(2), _));
417}
418
419TEST(ElementsAreTest, MatchesTenElementVector) {
420 const int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
421 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
422
423 EXPECT_THAT(test_vector,
424 // The element list can contain values and/or matchers
425 // of different types.
426 ElementsAre(0, Ge(0), _, 3, 4, Ne(2), Eq(6), 7, 8, _));
427}
428
429TEST(ElementsAreTest, DoesNotMatchWrongSize) {
430 vector<string> test_vector;
431 test_vector.push_back("test string");
432 test_vector.push_back("test string");
433
434 Matcher<vector<string> > m = ElementsAre(StrEq("test string"));
435 EXPECT_FALSE(m.Matches(test_vector));
436}
437
438TEST(ElementsAreTest, DoesNotMatchWrongValue) {
439 vector<string> test_vector;
440 test_vector.push_back("other string");
441
442 Matcher<vector<string> > m = ElementsAre(StrEq("test string"));
443 EXPECT_FALSE(m.Matches(test_vector));
444}
445
446TEST(ElementsAreTest, DoesNotMatchWrongOrder) {
447 vector<string> test_vector;
448 test_vector.push_back("one");
449 test_vector.push_back("three");
450 test_vector.push_back("two");
451
452 Matcher<vector<string> > m = ElementsAre(
453 StrEq("one"), StrEq("two"), StrEq("three"));
454 EXPECT_FALSE(m.Matches(test_vector));
455}
456
457TEST(ElementsAreTest, WorksForNestedContainer) {
458 const char* strings[] = {
459 "Hi",
460 "world"
461 };
462
463 vector<list<char> > nested;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000464 for (size_t i = 0; i < GMOCK_ARRAY_SIZE_(strings); i++) {
shiqiane35fdd92008-12-10 05:08:54 +0000465 nested.push_back(list<char>(strings[i], strings[i] + strlen(strings[i])));
466 }
467
468 EXPECT_THAT(nested, ElementsAre(ElementsAre('H', Ne('e')),
469 ElementsAre('w', 'o', _, _, 'd')));
470 EXPECT_THAT(nested, Not(ElementsAre(ElementsAre('H', 'e'),
471 ElementsAre('w', 'o', _, _, 'd'))));
472}
473
474TEST(ElementsAreTest, WorksWithByRefElementMatchers) {
475 int a[] = { 0, 1, 2 };
476 vector<int> v(a, a + GMOCK_ARRAY_SIZE_(a));
477
478 EXPECT_THAT(v, ElementsAre(Ref(v[0]), Ref(v[1]), Ref(v[2])));
479 EXPECT_THAT(v, Not(ElementsAre(Ref(v[0]), Ref(v[1]), Ref(a[2]))));
480}
481
482TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) {
483 int a[] = { 0, 1, 2 };
484 vector<int> v(a, a + GMOCK_ARRAY_SIZE_(a));
485
486 EXPECT_THAT(&v, Pointee(ElementsAre(0, 1, _)));
487 EXPECT_THAT(&v, Not(Pointee(ElementsAre(0, _, 3))));
488}
489
zhanyong.wanb8243162009-06-04 05:48:20 +0000490TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) {
491 int array[] = { 0, 1, 2 };
492 EXPECT_THAT(array, ElementsAre(0, 1, _));
493 EXPECT_THAT(array, Not(ElementsAre(1, _, _)));
494 EXPECT_THAT(array, Not(ElementsAre(0, _)));
495}
496
497class NativeArrayPassedAsPointerAndSize {
498 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000499 NativeArrayPassedAsPointerAndSize() {}
500
zhanyong.wanb8243162009-06-04 05:48:20 +0000501 MOCK_METHOD2(Helper, void(int* array, int size));
zhanyong.wan32de5f52009-12-23 00:13:23 +0000502
503 private:
504 GTEST_DISALLOW_COPY_AND_ASSIGN_(NativeArrayPassedAsPointerAndSize);
zhanyong.wanb8243162009-06-04 05:48:20 +0000505};
506
507TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) {
508 int array[] = { 0, 1 };
509 ::std::tr1::tuple<int*, size_t> array_as_tuple(array, 2);
510 EXPECT_THAT(array_as_tuple, ElementsAre(0, 1));
511 EXPECT_THAT(array_as_tuple, Not(ElementsAre(0)));
512
513 NativeArrayPassedAsPointerAndSize helper;
514 EXPECT_CALL(helper, Helper(_, _))
zhanyong.wanbf550852009-06-09 06:09:53 +0000515 .With(ElementsAre(0, 1));
zhanyong.wanb8243162009-06-04 05:48:20 +0000516 helper.Helper(array, 2);
517}
518
519TEST(ElementsAreTest, WorksWithTwoDimensionalNativeArray) {
520 const char a2[][3] = { "hi", "lo" };
521 EXPECT_THAT(a2, ElementsAre(ElementsAre('h', 'i', '\0'),
522 ElementsAre('l', 'o', '\0')));
523 EXPECT_THAT(a2, ElementsAre(StrEq("hi"), StrEq("lo")));
524 EXPECT_THAT(a2, ElementsAre(Not(ElementsAre('h', 'o', '\0')),
525 ElementsAre('l', 'o', '\0')));
526}
527
shiqiane35fdd92008-12-10 05:08:54 +0000528// Tests for ElementsAreArray(). Since ElementsAreArray() shares most
529// of the implementation with ElementsAre(), we don't test it as
530// thoroughly here.
531
532TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) {
533 const int a[] = { 1, 2, 3 };
534
535 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
536 EXPECT_THAT(test_vector, ElementsAreArray(a));
537
538 test_vector[2] = 0;
539 EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
540}
541
542TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) {
543 const char* a[] = { "one", "two", "three" };
544
545 vector<string> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
546 EXPECT_THAT(test_vector, ElementsAreArray(a, GMOCK_ARRAY_SIZE_(a)));
547
548 const char** p = a;
549 test_vector[0] = "1";
550 EXPECT_THAT(test_vector, Not(ElementsAreArray(p, GMOCK_ARRAY_SIZE_(a))));
551}
552
553TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) {
554 const char* a[] = { "one", "two", "three" };
555
556 vector<string> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
557 EXPECT_THAT(test_vector, ElementsAreArray(a));
558
559 test_vector[0] = "1";
560 EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
561}
562
563TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) {
564 const Matcher<string> kMatcherArray[] =
565 { StrEq("one"), StrEq("two"), StrEq("three") };
566
567 vector<string> test_vector;
568 test_vector.push_back("one");
569 test_vector.push_back("two");
570 test_vector.push_back("three");
571 EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray));
572
573 test_vector.push_back("three");
574 EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray)));
575}
576
zhanyong.wanb8243162009-06-04 05:48:20 +0000577// Since ElementsAre() and ElementsAreArray() share much of the
578// implementation, we only do a sanity test for native arrays here.
579TEST(ElementsAreArrayTest, WorksWithNativeArray) {
580 ::std::string a[] = { "hi", "ho" };
581 ::std::string b[] = { "hi", "ho" };
582
583 EXPECT_THAT(a, ElementsAreArray(b));
584 EXPECT_THAT(a, ElementsAreArray(b, 2));
585 EXPECT_THAT(a, Not(ElementsAreArray(b, 1)));
586}
587
zhanyong.wance198ff2009-02-12 01:34:27 +0000588// Tests for the MATCHER*() macro family.
589
590// Tests that a simple MATCHER() definition works.
591
592MATCHER(IsEven, "") { return (arg % 2) == 0; }
593
594TEST(MatcherMacroTest, Works) {
595 const Matcher<int> m = IsEven();
596 EXPECT_TRUE(m.Matches(6));
597 EXPECT_FALSE(m.Matches(7));
598
599 EXPECT_EQ("is even", Describe(m));
600 EXPECT_EQ("not (is even)", DescribeNegation(m));
601 EXPECT_EQ("", Explain(m, 6));
602 EXPECT_EQ("", Explain(m, 7));
603}
604
zhanyong.wan82113312010-01-08 21:55:40 +0000605// Tests explaining match result in a MATCHER* macro.
606
607MATCHER(IsEven2, "is even") {
608 if ((arg % 2) == 0) {
609 // Verifies that we can stream to result_listener, a listener
610 // supplied by the MATCHER macro implicitly.
611 *result_listener << "OK";
612 return true;
613 } else {
614 *result_listener << "% 2 == " << (arg % 2);
615 return false;
616 }
617}
618
619MATCHER_P2(EqSumOf, x, y, "") {
620 if (arg == (x + y)) {
621 *result_listener << "OK";
622 return true;
623 } else {
624 // Verifies that we can stream to the underlying stream of
625 // result_listener.
626 if (result_listener->stream() != NULL) {
627 *result_listener->stream() << "diff == " << (x + y - arg);
628 }
629 return false;
630 }
631}
632
633TEST(MatcherMacroTest, CanExplainMatchResult) {
634 const Matcher<int> m1 = IsEven2();
635 EXPECT_EQ("OK", Explain(m1, 4));
636 EXPECT_EQ("% 2 == 1", Explain(m1, 5));
637
638 const Matcher<int> m2 = EqSumOf(1, 2);
639 EXPECT_EQ("OK", Explain(m2, 3));
640 EXPECT_EQ("diff == -1", Explain(m2, 4));
641}
642
zhanyong.wance198ff2009-02-12 01:34:27 +0000643// Tests that the description string supplied to MATCHER() must be
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000644// valid.
zhanyong.wance198ff2009-02-12 01:34:27 +0000645
zhanyong.wan32de5f52009-12-23 00:13:23 +0000646MATCHER(HasBadDescription, "Invalid%") {
647 // Uses arg to suppress "unused parameter" warning.
648 return arg==arg;
649}
zhanyong.wance198ff2009-02-12 01:34:27 +0000650
651TEST(MatcherMacroTest,
652 CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) {
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000653 EXPECT_NONFATAL_FAILURE(
654 HasBadDescription(),
655 "Syntax error at index 7 in matcher description \"Invalid%\": "
656 "use \"%%\" instead of \"%\" to print \"%\".");
657}
658
zhanyong.wan32de5f52009-12-23 00:13:23 +0000659MATCHER(HasGoodDescription, "good") { return arg==arg; }
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000660
661TEST(MatcherMacroTest, AcceptsValidDescription) {
662 const Matcher<int> m = HasGoodDescription();
663 EXPECT_EQ("good", Describe(m));
zhanyong.wance198ff2009-02-12 01:34:27 +0000664}
665
666// Tests that the body of MATCHER() can reference the type of the
667// value being matched.
668
669MATCHER(IsEmptyString, "") {
670 StaticAssertTypeEq< ::std::string, arg_type>();
671 return arg == "";
672}
673
674MATCHER(IsEmptyStringByRef, "") {
675 StaticAssertTypeEq<const ::std::string&, arg_type>();
676 return arg == "";
677}
678
679TEST(MatcherMacroTest, CanReferenceArgType) {
680 const Matcher< ::std::string> m1 = IsEmptyString();
681 EXPECT_TRUE(m1.Matches(""));
682
683 const Matcher<const ::std::string&> m2 = IsEmptyStringByRef();
684 EXPECT_TRUE(m2.Matches(""));
685}
686
687// Tests that MATCHER() can be used in a namespace.
688
689namespace matcher_test {
690MATCHER(IsOdd, "") { return (arg % 2) != 0; }
691} // namespace matcher_test
692
zhanyong.wanb8243162009-06-04 05:48:20 +0000693TEST(MatcherMacroTest, WorksInNamespace) {
zhanyong.wance198ff2009-02-12 01:34:27 +0000694 Matcher<int> m = matcher_test::IsOdd();
695 EXPECT_FALSE(m.Matches(4));
696 EXPECT_TRUE(m.Matches(5));
697}
698
zhanyong.wanb8243162009-06-04 05:48:20 +0000699// Tests that Value() can be used to compose matchers.
700MATCHER(IsPositiveOdd, "") {
701 return Value(arg, matcher_test::IsOdd()) && arg > 0;
702}
703
704TEST(MatcherMacroTest, CanBeComposedUsingValue) {
705 EXPECT_THAT(3, IsPositiveOdd());
706 EXPECT_THAT(4, Not(IsPositiveOdd()));
707 EXPECT_THAT(-1, Not(IsPositiveOdd()));
708}
709
zhanyong.wance198ff2009-02-12 01:34:27 +0000710// Tests that a simple MATCHER_P() definition works.
711
712MATCHER_P(IsGreaterThan32And, n, "") { return arg > 32 && arg > n; }
713
714TEST(MatcherPMacroTest, Works) {
715 const Matcher<int> m = IsGreaterThan32And(5);
716 EXPECT_TRUE(m.Matches(36));
717 EXPECT_FALSE(m.Matches(5));
718
719 EXPECT_EQ("is greater than 32 and 5", Describe(m));
720 EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m));
721 EXPECT_EQ("", Explain(m, 36));
722 EXPECT_EQ("", Explain(m, 5));
723}
724
725// Tests that the description string supplied to MATCHER_P() must be
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000726// valid.
zhanyong.wance198ff2009-02-12 01:34:27 +0000727
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000728MATCHER_P(HasBadDescription1, n, "not %(m)s good") {
zhanyong.wance198ff2009-02-12 01:34:27 +0000729 return arg > n;
730}
731
732TEST(MatcherPMacroTest,
733 CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) {
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000734 EXPECT_NONFATAL_FAILURE(
735 HasBadDescription1(2),
736 "Syntax error at index 6 in matcher description \"not %(m)s good\": "
737 "\"m\" is an invalid parameter name.");
738}
739
740
zhanyong.wan32de5f52009-12-23 00:13:23 +0000741MATCHER_P(HasGoodDescription1, n, "good %(n)s") { return arg==arg; }
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000742
743TEST(MatcherPMacroTest, AcceptsValidDescription) {
744 const Matcher<int> m = HasGoodDescription1(5);
745 EXPECT_EQ("good 5", Describe(m));
zhanyong.wance198ff2009-02-12 01:34:27 +0000746}
747
748// Tests that the description is calculated correctly from the matcher name.
749MATCHER_P(_is_Greater_Than32and_, n, "") { return arg > 32 && arg > n; }
750
751TEST(MatcherPMacroTest, GeneratesCorrectDescription) {
752 const Matcher<int> m = _is_Greater_Than32and_(5);
753
754 EXPECT_EQ("is greater than 32 and 5", Describe(m));
755 EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m));
756 EXPECT_EQ("", Explain(m, 36));
757 EXPECT_EQ("", Explain(m, 5));
758}
759
760// Tests that a MATCHER_P matcher can be explicitly instantiated with
761// a reference parameter type.
762
763class UncopyableFoo {
764 public:
765 explicit UncopyableFoo(char value) : value_(value) {}
766 private:
767 UncopyableFoo(const UncopyableFoo&);
768 void operator=(const UncopyableFoo&);
769
770 char value_;
771};
772
773MATCHER_P(ReferencesUncopyable, variable, "") { return &arg == &variable; }
774
775TEST(MatcherPMacroTest, WorksWhenExplicitlyInstantiatedWithReference) {
776 UncopyableFoo foo1('1'), foo2('2');
777 const Matcher<const UncopyableFoo&> m =
778 ReferencesUncopyable<const UncopyableFoo&>(foo1);
779
780 EXPECT_TRUE(m.Matches(foo1));
781 EXPECT_FALSE(m.Matches(foo2));
782
783 // We don't want the address of the parameter printed, as most
784 // likely it will just annoy the user. If the address is
785 // interesting, the user should consider passing the parameter by
786 // pointer instead.
787 EXPECT_EQ("references uncopyable 1-byte object <31>", Describe(m));
788}
789
790
791// Tests that the description string supplied to MATCHER_Pn() must be
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000792// valid.
zhanyong.wance198ff2009-02-12 01:34:27 +0000793
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000794MATCHER_P2(HasBadDescription2, m, n, "not %(good") {
zhanyong.wance198ff2009-02-12 01:34:27 +0000795 return arg > m + n;
796}
797
798TEST(MatcherPnMacroTest,
799 CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) {
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000800 EXPECT_NONFATAL_FAILURE(
801 HasBadDescription2(3, 4),
802 "Syntax error at index 4 in matcher description \"not %(good\": "
803 "an interpolation must end with \")s\", but \"%(good\" does not.");
804}
805
806MATCHER_P2(HasComplexDescription, foo, bar,
807 "is as complex as %(foo)s %(bar)s (i.e. %(*)s or %%%(foo)s!)") {
zhanyong.wan32de5f52009-12-23 00:13:23 +0000808 return arg==arg;
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000809}
810
811TEST(MatcherPnMacroTest, AcceptsValidDescription) {
812 Matcher<int> m = HasComplexDescription(100, "ducks");
813 EXPECT_EQ("is as complex as 100 \"ducks\" (i.e. (100, \"ducks\") or %100!)",
814 Describe(m));
zhanyong.wance198ff2009-02-12 01:34:27 +0000815}
816
817// Tests that the body of MATCHER_Pn() can reference the parameter
818// types.
819
820MATCHER_P3(ParamTypesAreIntLongAndChar, foo, bar, baz, "") {
821 StaticAssertTypeEq<int, foo_type>();
822 StaticAssertTypeEq<long, bar_type>(); // NOLINT
823 StaticAssertTypeEq<char, baz_type>();
824 return arg == 0;
825}
826
827TEST(MatcherPnMacroTest, CanReferenceParamTypes) {
828 EXPECT_THAT(0, ParamTypesAreIntLongAndChar(10, 20L, 'a'));
829}
830
831// Tests that a MATCHER_Pn matcher can be explicitly instantiated with
832// reference parameter types.
833
834MATCHER_P2(ReferencesAnyOf, variable1, variable2, "") {
835 return &arg == &variable1 || &arg == &variable2;
836}
837
838TEST(MatcherPnMacroTest, WorksWhenExplicitlyInstantiatedWithReferences) {
839 UncopyableFoo foo1('1'), foo2('2'), foo3('3');
840 const Matcher<const UncopyableFoo&> m =
841 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
842
843 EXPECT_TRUE(m.Matches(foo1));
844 EXPECT_TRUE(m.Matches(foo2));
845 EXPECT_FALSE(m.Matches(foo3));
846}
847
848TEST(MatcherPnMacroTest,
849 GeneratesCorretDescriptionWhenExplicitlyInstantiatedWithReferences) {
850 UncopyableFoo foo1('1'), foo2('2');
851 const Matcher<const UncopyableFoo&> m =
852 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
853
854 // We don't want the addresses of the parameters printed, as most
855 // likely they will just annoy the user. If the addresses are
856 // interesting, the user should consider passing the parameters by
857 // pointers instead.
858 EXPECT_EQ("references any of (1-byte object <31>, 1-byte object <32>)",
859 Describe(m));
860}
861
862// Tests that a simple MATCHER_P2() definition works.
863
864MATCHER_P2(IsNotInClosedRange, low, hi, "") { return arg < low || arg > hi; }
865
866TEST(MatcherPnMacroTest, Works) {
867 const Matcher<const long&> m = IsNotInClosedRange(10, 20); // NOLINT
868 EXPECT_TRUE(m.Matches(36L));
869 EXPECT_FALSE(m.Matches(15L));
870
871 EXPECT_EQ("is not in closed range (10, 20)", Describe(m));
872 EXPECT_EQ("not (is not in closed range (10, 20))", DescribeNegation(m));
873 EXPECT_EQ("", Explain(m, 36L));
874 EXPECT_EQ("", Explain(m, 15L));
875}
876
877// Tests that MATCHER*() definitions can be overloaded on the number
878// of parameters; also tests MATCHER_Pn() where n >= 3.
879
880MATCHER(EqualsSumOf, "") { return arg == 0; }
881MATCHER_P(EqualsSumOf, a, "") { return arg == a; }
882MATCHER_P2(EqualsSumOf, a, b, "") { return arg == a + b; }
883MATCHER_P3(EqualsSumOf, a, b, c, "") { return arg == a + b + c; }
884MATCHER_P4(EqualsSumOf, a, b, c, d, "") { return arg == a + b + c + d; }
885MATCHER_P5(EqualsSumOf, a, b, c, d, e, "") { return arg == a + b + c + d + e; }
886MATCHER_P6(EqualsSumOf, a, b, c, d, e, f, "") {
887 return arg == a + b + c + d + e + f;
888}
889MATCHER_P7(EqualsSumOf, a, b, c, d, e, f, g, "") {
890 return arg == a + b + c + d + e + f + g;
891}
892MATCHER_P8(EqualsSumOf, a, b, c, d, e, f, g, h, "") {
893 return arg == a + b + c + d + e + f + g + h;
894}
895MATCHER_P9(EqualsSumOf, a, b, c, d, e, f, g, h, i, "") {
896 return arg == a + b + c + d + e + f + g + h + i;
897}
898MATCHER_P10(EqualsSumOf, a, b, c, d, e, f, g, h, i, j, "") {
899 return arg == a + b + c + d + e + f + g + h + i + j;
900}
901
902TEST(MatcherPnMacroTest, CanBeOverloadedOnNumberOfParameters) {
903 EXPECT_THAT(0, EqualsSumOf());
904 EXPECT_THAT(1, EqualsSumOf(1));
905 EXPECT_THAT(12, EqualsSumOf(10, 2));
906 EXPECT_THAT(123, EqualsSumOf(100, 20, 3));
907 EXPECT_THAT(1234, EqualsSumOf(1000, 200, 30, 4));
908 EXPECT_THAT(12345, EqualsSumOf(10000, 2000, 300, 40, 5));
909 EXPECT_THAT("abcdef",
910 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f'));
911 EXPECT_THAT("abcdefg",
912 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g'));
913 EXPECT_THAT("abcdefgh",
914 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
915 "h"));
916 EXPECT_THAT("abcdefghi",
917 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
918 "h", 'i'));
919 EXPECT_THAT("abcdefghij",
920 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
921 "h", 'i', ::std::string("j")));
922
923 EXPECT_THAT(1, Not(EqualsSumOf()));
924 EXPECT_THAT(-1, Not(EqualsSumOf(1)));
925 EXPECT_THAT(-12, Not(EqualsSumOf(10, 2)));
926 EXPECT_THAT(-123, Not(EqualsSumOf(100, 20, 3)));
927 EXPECT_THAT(-1234, Not(EqualsSumOf(1000, 200, 30, 4)));
928 EXPECT_THAT(-12345, Not(EqualsSumOf(10000, 2000, 300, 40, 5)));
929 EXPECT_THAT("abcdef ",
930 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f')));
931 EXPECT_THAT("abcdefg ",
932 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f',
933 'g')));
934 EXPECT_THAT("abcdefgh ",
935 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
936 "h")));
937 EXPECT_THAT("abcdefghi ",
938 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
939 "h", 'i')));
940 EXPECT_THAT("abcdefghij ",
941 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
942 "h", 'i', ::std::string("j"))));
943}
944
945// Tests that a MATCHER_Pn() definition can be instantiated with any
946// compatible parameter types.
947TEST(MatcherPnMacroTest, WorksForDifferentParameterTypes) {
948 EXPECT_THAT(123, EqualsSumOf(100L, 20, static_cast<char>(3)));
949 EXPECT_THAT("abcd", EqualsSumOf(::std::string("a"), "b", 'c', "d"));
950
951 EXPECT_THAT(124, Not(EqualsSumOf(100L, 20, static_cast<char>(3))));
952 EXPECT_THAT("abcde", Not(EqualsSumOf(::std::string("a"), "b", 'c', "d")));
953}
954
955// Tests that the matcher body can promote the parameter types.
956
957MATCHER_P2(EqConcat, prefix, suffix, "") {
958 // The following lines promote the two parameters to desired types.
959 std::string prefix_str(prefix);
zhanyong.wan32de5f52009-12-23 00:13:23 +0000960 char suffix_char = static_cast<char>(suffix);
zhanyong.wance198ff2009-02-12 01:34:27 +0000961 return arg == prefix_str + suffix_char;
962}
963
964TEST(MatcherPnMacroTest, SimpleTypePromotion) {
965 Matcher<std::string> no_promo =
966 EqConcat(std::string("foo"), 't');
967 Matcher<const std::string&> promo =
968 EqConcat("foo", static_cast<int>('t'));
969 EXPECT_FALSE(no_promo.Matches("fool"));
970 EXPECT_FALSE(promo.Matches("fool"));
971 EXPECT_TRUE(no_promo.Matches("foot"));
972 EXPECT_TRUE(promo.Matches("foot"));
973}
974
975// Verifies the type of a MATCHER*.
976
977TEST(MatcherPnMacroTest, TypesAreCorrect) {
978 // EqualsSumOf() must be assignable to a EqualsSumOfMatcher variable.
979 EqualsSumOfMatcher a0 = EqualsSumOf();
980
981 // EqualsSumOf(1) must be assignable to a EqualsSumOfMatcherP variable.
982 EqualsSumOfMatcherP<int> a1 = EqualsSumOf(1);
983
984 // EqualsSumOf(p1, ..., pk) must be assignable to a EqualsSumOfMatcherPk
985 // variable, and so on.
986 EqualsSumOfMatcherP2<int, char> a2 = EqualsSumOf(1, '2');
987 EqualsSumOfMatcherP3<int, int, char> a3 = EqualsSumOf(1, 2, '3');
988 EqualsSumOfMatcherP4<int, int, int, char> a4 = EqualsSumOf(1, 2, 3, '4');
989 EqualsSumOfMatcherP5<int, int, int, int, char> a5 =
990 EqualsSumOf(1, 2, 3, 4, '5');
991 EqualsSumOfMatcherP6<int, int, int, int, int, char> a6 =
992 EqualsSumOf(1, 2, 3, 4, 5, '6');
993 EqualsSumOfMatcherP7<int, int, int, int, int, int, char> a7 =
994 EqualsSumOf(1, 2, 3, 4, 5, 6, '7');
995 EqualsSumOfMatcherP8<int, int, int, int, int, int, int, char> a8 =
996 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, '8');
997 EqualsSumOfMatcherP9<int, int, int, int, int, int, int, int, char> a9 =
998 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, '9');
999 EqualsSumOfMatcherP10<int, int, int, int, int, int, int, int, int, char> a10 =
1000 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, 9, '0');
1001}
1002
zhanyong.wanb8243162009-06-04 05:48:20 +00001003// Tests that matcher-typed parameters can be used in Value() inside a
1004// MATCHER_Pn definition.
1005
1006// Succeeds if arg matches exactly 2 of the 3 matchers.
1007MATCHER_P3(TwoOf, m1, m2, m3, "") {
1008 const int count = static_cast<int>(Value(arg, m1))
1009 + static_cast<int>(Value(arg, m2)) + static_cast<int>(Value(arg, m3));
1010 return count == 2;
1011}
1012
1013TEST(MatcherPnMacroTest, CanUseMatcherTypedParameterInValue) {
1014 EXPECT_THAT(42, TwoOf(Gt(0), Lt(50), Eq(10)));
1015 EXPECT_THAT(0, Not(TwoOf(Gt(-1), Lt(1), Eq(0))));
1016}
1017
1018// Tests Contains().
1019
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001020TEST(ContainsTest, ListMatchesWhenElementIsInContainer) {
1021 list<int> some_list;
1022 some_list.push_back(3);
1023 some_list.push_back(1);
1024 some_list.push_back(2);
1025 EXPECT_THAT(some_list, Contains(1));
zhanyong.wanb8243162009-06-04 05:48:20 +00001026 EXPECT_THAT(some_list, Contains(Gt(2.5)));
1027 EXPECT_THAT(some_list, Contains(Eq(2.0f)));
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001028
1029 list<string> another_list;
1030 another_list.push_back("fee");
1031 another_list.push_back("fie");
1032 another_list.push_back("foe");
1033 another_list.push_back("fum");
1034 EXPECT_THAT(another_list, Contains(string("fee")));
1035}
1036
1037TEST(ContainsTest, ListDoesNotMatchWhenElementIsNotInContainer) {
1038 list<int> some_list;
1039 some_list.push_back(3);
1040 some_list.push_back(1);
1041 EXPECT_THAT(some_list, Not(Contains(4)));
1042}
1043
1044TEST(ContainsTest, SetMatchesWhenElementIsInContainer) {
1045 set<int> some_set;
1046 some_set.insert(3);
1047 some_set.insert(1);
1048 some_set.insert(2);
zhanyong.wanb8243162009-06-04 05:48:20 +00001049 EXPECT_THAT(some_set, Contains(Eq(1.0)));
1050 EXPECT_THAT(some_set, Contains(Eq(3.0f)));
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001051 EXPECT_THAT(some_set, Contains(2));
1052
1053 set<const char*> another_set;
1054 another_set.insert("fee");
1055 another_set.insert("fie");
1056 another_set.insert("foe");
1057 another_set.insert("fum");
zhanyong.wanb8243162009-06-04 05:48:20 +00001058 EXPECT_THAT(another_set, Contains(Eq(string("fum"))));
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001059}
1060
1061TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) {
1062 set<int> some_set;
1063 some_set.insert(3);
1064 some_set.insert(1);
1065 EXPECT_THAT(some_set, Not(Contains(4)));
1066
1067 set<const char*> c_string_set;
1068 c_string_set.insert("hello");
1069 EXPECT_THAT(c_string_set, Not(Contains(string("hello").c_str())));
1070}
1071
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001072TEST(ContainsTest, ExplainsMatchResultCorrectly) {
zhanyong.wanb8243162009-06-04 05:48:20 +00001073 const int a[2] = { 1, 2 };
1074 Matcher<const int(&)[2]> m = Contains(2);
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001075 EXPECT_EQ("whose element #1 matches", Explain(m, a));
zhanyong.wanb8243162009-06-04 05:48:20 +00001076
1077 m = Contains(3);
1078 EXPECT_EQ("", Explain(m, a));
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001079
1080 m = Contains(GreaterThan(0));
1081 EXPECT_EQ("whose element #0 matches, which is 1 more than 0", Explain(m, a));
1082
1083 m = Contains(GreaterThan(10));
1084 EXPECT_EQ("", Explain(m, a));
zhanyong.wanb8243162009-06-04 05:48:20 +00001085}
1086
zhanyong.wanb1c7f932010-03-24 17:35:11 +00001087TEST(ContainsTest, DescribesItselfCorrectly) {
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001088 Matcher<vector<int> > m = Contains(1);
zhanyong.wanb8243162009-06-04 05:48:20 +00001089 EXPECT_EQ("contains at least one element that is equal to 1", Describe(m));
1090
1091 Matcher<vector<int> > m2 = Not(m);
1092 EXPECT_EQ("doesn't contain any element that is equal to 1", Describe(m2));
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001093}
1094
1095TEST(ContainsTest, MapMatchesWhenElementIsInContainer) {
1096 map<const char*, int> my_map;
1097 const char* bar = "a string";
1098 my_map[bar] = 2;
1099 EXPECT_THAT(my_map, Contains(pair<const char* const, int>(bar, 2)));
1100
1101 map<string, int> another_map;
1102 another_map["fee"] = 1;
1103 another_map["fie"] = 2;
1104 another_map["foe"] = 3;
1105 another_map["fum"] = 4;
1106 EXPECT_THAT(another_map, Contains(pair<const string, int>(string("fee"), 1)));
1107 EXPECT_THAT(another_map, Contains(pair<const string, int>("fie", 2)));
1108}
1109
1110TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) {
1111 map<int, int> some_map;
1112 some_map[1] = 11;
1113 some_map[2] = 22;
1114 EXPECT_THAT(some_map, Not(Contains(pair<const int, int>(2, 23))));
1115}
1116
1117TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) {
1118 const char* string_array[] = { "fee", "fie", "foe", "fum" };
zhanyong.wanb8243162009-06-04 05:48:20 +00001119 EXPECT_THAT(string_array, Contains(Eq(string("fum"))));
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001120}
1121
1122TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) {
1123 int int_array[] = { 1, 2, 3, 4 };
1124 EXPECT_THAT(int_array, Not(Contains(5)));
1125}
1126
zhanyong.wanb8243162009-06-04 05:48:20 +00001127TEST(ContainsTest, AcceptsMatcher) {
1128 const int a[] = { 1, 2, 3 };
1129 EXPECT_THAT(a, Contains(Gt(2)));
1130 EXPECT_THAT(a, Not(Contains(Gt(4))));
1131}
1132
1133TEST(ContainsTest, WorksForNativeArrayAsTuple) {
1134 const int a[] = { 1, 2 };
zhanyong.wan2661c682009-06-09 05:42:12 +00001135 const int* const pointer = a;
1136 EXPECT_THAT(make_tuple(pointer, 2), Contains(1));
1137 EXPECT_THAT(make_tuple(pointer, 2), Not(Contains(Gt(3))));
zhanyong.wanb8243162009-06-04 05:48:20 +00001138}
1139
1140TEST(ContainsTest, WorksForTwoDimensionalNativeArray) {
1141 int a[][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
1142 EXPECT_THAT(a, Contains(ElementsAre(4, 5, 6)));
1143 EXPECT_THAT(a, Contains(Contains(5)));
1144 EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5))));
1145 EXPECT_THAT(a, Contains(Not(Contains(5))));
1146}
1147
zhanyong.wan82113312010-01-08 21:55:40 +00001148#ifdef _MSC_VER
1149#pragma warning(pop)
1150#endif
1151
shiqiane35fdd92008-12-10 05:08:54 +00001152} // namespace