blob: db2ffb2f62fcc14b7af22e6748e96327d7c6bef6 [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
shiqiane35fdd92008-12-10 05:08:54 +0000214// For testing ExplainMatchResultTo().
215class GreaterThanMatcher : public MatcherInterface<int> {
216 public:
217 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
218
shiqiane35fdd92008-12-10 05:08:54 +0000219 virtual void DescribeTo(::std::ostream* os) const {
220 *os << "is greater than " << rhs_;
221 }
222
zhanyong.wandb22c222010-01-28 21:52:29 +0000223 virtual bool MatchAndExplain(int lhs,
224 MatchResultListener* listener) const {
shiqiane35fdd92008-12-10 05:08:54 +0000225 const int diff = lhs - rhs_;
226 if (diff > 0) {
zhanyong.wandb22c222010-01-28 21:52:29 +0000227 *listener << "is " << diff << " more than " << rhs_;
shiqiane35fdd92008-12-10 05:08:54 +0000228 } else if (diff == 0) {
zhanyong.wandb22c222010-01-28 21:52:29 +0000229 *listener << "is the same as " << rhs_;
shiqiane35fdd92008-12-10 05:08:54 +0000230 } else {
zhanyong.wandb22c222010-01-28 21:52:29 +0000231 *listener << "is " << -diff << " less than " << rhs_;
shiqiane35fdd92008-12-10 05:08:54 +0000232 }
zhanyong.wandb22c222010-01-28 21:52:29 +0000233
234 return lhs > rhs_;
shiqiane35fdd92008-12-10 05:08:54 +0000235 }
zhanyong.wan32de5f52009-12-23 00:13:23 +0000236
shiqiane35fdd92008-12-10 05:08:54 +0000237 private:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000238 int rhs_;
shiqiane35fdd92008-12-10 05:08:54 +0000239};
240
241Matcher<int> GreaterThan(int n) {
242 return MakeMatcher(new GreaterThanMatcher(n));
243}
244
245// Tests for ElementsAre().
246
247// Evaluates to the number of elements in 'array'.
248#define GMOCK_ARRAY_SIZE_(array) (sizeof(array)/sizeof(array[0]))
249
250TEST(ElementsAreTest, CanDescribeExpectingNoElement) {
251 Matcher<const vector<int>&> m = ElementsAre();
252 EXPECT_EQ("is empty", Describe(m));
253}
254
255TEST(ElementsAreTest, CanDescribeExpectingOneElement) {
256 Matcher<vector<int> > m = ElementsAre(Gt(5));
257 EXPECT_EQ("has 1 element that is greater than 5", Describe(m));
258}
259
260TEST(ElementsAreTest, CanDescribeExpectingManyElements) {
261 Matcher<list<string> > m = ElementsAre(StrEq("one"), "two");
262 EXPECT_EQ("has 2 elements where\n"
263 "element 0 is equal to \"one\",\n"
264 "element 1 is equal to \"two\"", Describe(m));
265}
266
267TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) {
268 Matcher<vector<int> > m = ElementsAre();
269 EXPECT_EQ("is not empty", DescribeNegation(m));
270}
271
272TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElment) {
273 Matcher<const list<int>& > m = ElementsAre(Gt(5));
274 EXPECT_EQ("does not have 1 element, or\n"
275 "element 0 is not greater than 5", DescribeNegation(m));
276}
277
278TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) {
279 Matcher<const list<string>& > m = ElementsAre("one", "two");
280 EXPECT_EQ("does not have 2 elements, or\n"
281 "element 0 is not equal to \"one\", or\n"
282 "element 1 is not equal to \"two\"", DescribeNegation(m));
283}
284
285TEST(ElementsAreTest, DoesNotExplainTrivialMatch) {
286 Matcher<const list<int>& > m = ElementsAre(1, Ne(2));
287
288 list<int> test_list;
289 test_list.push_back(1);
290 test_list.push_back(3);
291 EXPECT_EQ("", Explain(m, test_list)); // No need to explain anything.
292}
293
294TEST(ElementsAreTest, ExplainsNonTrivialMatch) {
295 Matcher<const vector<int>& > m =
296 ElementsAre(GreaterThan(1), 0, GreaterThan(2));
297
298 const int a[] = { 10, 0, 100 };
299 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
300 EXPECT_EQ("element 0 is 9 more than 1,\n"
301 "element 2 is 98 more than 2", Explain(m, test_vector));
302}
303
304TEST(ElementsAreTest, CanExplainMismatchWrongSize) {
305 Matcher<const list<int>& > m = ElementsAre(1, 3);
306
307 list<int> test_list;
308 // No need to explain when the container is empty.
309 EXPECT_EQ("", Explain(m, test_list));
310
311 test_list.push_back(1);
312 EXPECT_EQ("has 1 element", Explain(m, test_list));
313}
314
315TEST(ElementsAreTest, CanExplainMismatchRightSize) {
316 Matcher<const vector<int>& > m = ElementsAre(1, GreaterThan(5));
317
318 vector<int> v;
319 v.push_back(2);
320 v.push_back(1);
321 EXPECT_EQ("element 0 doesn't match", Explain(m, v));
322
323 v[0] = 1;
324 EXPECT_EQ("element 1 doesn't match (is 4 less than 5)", Explain(m, v));
325}
326
327TEST(ElementsAreTest, MatchesOneElementVector) {
328 vector<string> test_vector;
329 test_vector.push_back("test string");
330
331 EXPECT_THAT(test_vector, ElementsAre(StrEq("test string")));
332}
333
334TEST(ElementsAreTest, MatchesOneElementList) {
335 list<string> test_list;
336 test_list.push_back("test string");
337
338 EXPECT_THAT(test_list, ElementsAre("test string"));
339}
340
341TEST(ElementsAreTest, MatchesThreeElementVector) {
342 vector<string> test_vector;
343 test_vector.push_back("one");
344 test_vector.push_back("two");
345 test_vector.push_back("three");
346
347 EXPECT_THAT(test_vector, ElementsAre("one", StrEq("two"), _));
348}
349
350TEST(ElementsAreTest, MatchesOneElementEqMatcher) {
351 vector<int> test_vector;
352 test_vector.push_back(4);
353
354 EXPECT_THAT(test_vector, ElementsAre(Eq(4)));
355}
356
357TEST(ElementsAreTest, MatchesOneElementAnyMatcher) {
358 vector<int> test_vector;
359 test_vector.push_back(4);
360
361 EXPECT_THAT(test_vector, ElementsAre(_));
362}
363
364TEST(ElementsAreTest, MatchesOneElementValue) {
365 vector<int> test_vector;
366 test_vector.push_back(4);
367
368 EXPECT_THAT(test_vector, ElementsAre(4));
369}
370
371TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) {
372 vector<int> test_vector;
373 test_vector.push_back(1);
374 test_vector.push_back(2);
375 test_vector.push_back(3);
376
377 EXPECT_THAT(test_vector, ElementsAre(1, Eq(2), _));
378}
379
380TEST(ElementsAreTest, MatchesTenElementVector) {
381 const int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
382 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
383
384 EXPECT_THAT(test_vector,
385 // The element list can contain values and/or matchers
386 // of different types.
387 ElementsAre(0, Ge(0), _, 3, 4, Ne(2), Eq(6), 7, 8, _));
388}
389
390TEST(ElementsAreTest, DoesNotMatchWrongSize) {
391 vector<string> test_vector;
392 test_vector.push_back("test string");
393 test_vector.push_back("test string");
394
395 Matcher<vector<string> > m = ElementsAre(StrEq("test string"));
396 EXPECT_FALSE(m.Matches(test_vector));
397}
398
399TEST(ElementsAreTest, DoesNotMatchWrongValue) {
400 vector<string> test_vector;
401 test_vector.push_back("other string");
402
403 Matcher<vector<string> > m = ElementsAre(StrEq("test string"));
404 EXPECT_FALSE(m.Matches(test_vector));
405}
406
407TEST(ElementsAreTest, DoesNotMatchWrongOrder) {
408 vector<string> test_vector;
409 test_vector.push_back("one");
410 test_vector.push_back("three");
411 test_vector.push_back("two");
412
413 Matcher<vector<string> > m = ElementsAre(
414 StrEq("one"), StrEq("two"), StrEq("three"));
415 EXPECT_FALSE(m.Matches(test_vector));
416}
417
418TEST(ElementsAreTest, WorksForNestedContainer) {
419 const char* strings[] = {
420 "Hi",
421 "world"
422 };
423
424 vector<list<char> > nested;
zhanyong.wan32de5f52009-12-23 00:13:23 +0000425 for (size_t i = 0; i < GMOCK_ARRAY_SIZE_(strings); i++) {
shiqiane35fdd92008-12-10 05:08:54 +0000426 nested.push_back(list<char>(strings[i], strings[i] + strlen(strings[i])));
427 }
428
429 EXPECT_THAT(nested, ElementsAre(ElementsAre('H', Ne('e')),
430 ElementsAre('w', 'o', _, _, 'd')));
431 EXPECT_THAT(nested, Not(ElementsAre(ElementsAre('H', 'e'),
432 ElementsAre('w', 'o', _, _, 'd'))));
433}
434
435TEST(ElementsAreTest, WorksWithByRefElementMatchers) {
436 int a[] = { 0, 1, 2 };
437 vector<int> v(a, a + GMOCK_ARRAY_SIZE_(a));
438
439 EXPECT_THAT(v, ElementsAre(Ref(v[0]), Ref(v[1]), Ref(v[2])));
440 EXPECT_THAT(v, Not(ElementsAre(Ref(v[0]), Ref(v[1]), Ref(a[2]))));
441}
442
443TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) {
444 int a[] = { 0, 1, 2 };
445 vector<int> v(a, a + GMOCK_ARRAY_SIZE_(a));
446
447 EXPECT_THAT(&v, Pointee(ElementsAre(0, 1, _)));
448 EXPECT_THAT(&v, Not(Pointee(ElementsAre(0, _, 3))));
449}
450
zhanyong.wanb8243162009-06-04 05:48:20 +0000451TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) {
452 int array[] = { 0, 1, 2 };
453 EXPECT_THAT(array, ElementsAre(0, 1, _));
454 EXPECT_THAT(array, Not(ElementsAre(1, _, _)));
455 EXPECT_THAT(array, Not(ElementsAre(0, _)));
456}
457
458class NativeArrayPassedAsPointerAndSize {
459 public:
zhanyong.wan32de5f52009-12-23 00:13:23 +0000460 NativeArrayPassedAsPointerAndSize() {}
461
zhanyong.wanb8243162009-06-04 05:48:20 +0000462 MOCK_METHOD2(Helper, void(int* array, int size));
zhanyong.wan32de5f52009-12-23 00:13:23 +0000463
464 private:
465 GTEST_DISALLOW_COPY_AND_ASSIGN_(NativeArrayPassedAsPointerAndSize);
zhanyong.wanb8243162009-06-04 05:48:20 +0000466};
467
468TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) {
469 int array[] = { 0, 1 };
470 ::std::tr1::tuple<int*, size_t> array_as_tuple(array, 2);
471 EXPECT_THAT(array_as_tuple, ElementsAre(0, 1));
472 EXPECT_THAT(array_as_tuple, Not(ElementsAre(0)));
473
474 NativeArrayPassedAsPointerAndSize helper;
475 EXPECT_CALL(helper, Helper(_, _))
zhanyong.wanbf550852009-06-09 06:09:53 +0000476 .With(ElementsAre(0, 1));
zhanyong.wanb8243162009-06-04 05:48:20 +0000477 helper.Helper(array, 2);
478}
479
480TEST(ElementsAreTest, WorksWithTwoDimensionalNativeArray) {
481 const char a2[][3] = { "hi", "lo" };
482 EXPECT_THAT(a2, ElementsAre(ElementsAre('h', 'i', '\0'),
483 ElementsAre('l', 'o', '\0')));
484 EXPECT_THAT(a2, ElementsAre(StrEq("hi"), StrEq("lo")));
485 EXPECT_THAT(a2, ElementsAre(Not(ElementsAre('h', 'o', '\0')),
486 ElementsAre('l', 'o', '\0')));
487}
488
shiqiane35fdd92008-12-10 05:08:54 +0000489// Tests for ElementsAreArray(). Since ElementsAreArray() shares most
490// of the implementation with ElementsAre(), we don't test it as
491// thoroughly here.
492
493TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) {
494 const int a[] = { 1, 2, 3 };
495
496 vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
497 EXPECT_THAT(test_vector, ElementsAreArray(a));
498
499 test_vector[2] = 0;
500 EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
501}
502
503TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) {
504 const char* a[] = { "one", "two", "three" };
505
506 vector<string> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
507 EXPECT_THAT(test_vector, ElementsAreArray(a, GMOCK_ARRAY_SIZE_(a)));
508
509 const char** p = a;
510 test_vector[0] = "1";
511 EXPECT_THAT(test_vector, Not(ElementsAreArray(p, GMOCK_ARRAY_SIZE_(a))));
512}
513
514TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) {
515 const char* a[] = { "one", "two", "three" };
516
517 vector<string> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
518 EXPECT_THAT(test_vector, ElementsAreArray(a));
519
520 test_vector[0] = "1";
521 EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
522}
523
524TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) {
525 const Matcher<string> kMatcherArray[] =
526 { StrEq("one"), StrEq("two"), StrEq("three") };
527
528 vector<string> test_vector;
529 test_vector.push_back("one");
530 test_vector.push_back("two");
531 test_vector.push_back("three");
532 EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray));
533
534 test_vector.push_back("three");
535 EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray)));
536}
537
zhanyong.wanb8243162009-06-04 05:48:20 +0000538// Since ElementsAre() and ElementsAreArray() share much of the
539// implementation, we only do a sanity test for native arrays here.
540TEST(ElementsAreArrayTest, WorksWithNativeArray) {
541 ::std::string a[] = { "hi", "ho" };
542 ::std::string b[] = { "hi", "ho" };
543
544 EXPECT_THAT(a, ElementsAreArray(b));
545 EXPECT_THAT(a, ElementsAreArray(b, 2));
546 EXPECT_THAT(a, Not(ElementsAreArray(b, 1)));
547}
548
zhanyong.wance198ff2009-02-12 01:34:27 +0000549// Tests for the MATCHER*() macro family.
550
551// Tests that a simple MATCHER() definition works.
552
553MATCHER(IsEven, "") { return (arg % 2) == 0; }
554
555TEST(MatcherMacroTest, Works) {
556 const Matcher<int> m = IsEven();
557 EXPECT_TRUE(m.Matches(6));
558 EXPECT_FALSE(m.Matches(7));
559
560 EXPECT_EQ("is even", Describe(m));
561 EXPECT_EQ("not (is even)", DescribeNegation(m));
562 EXPECT_EQ("", Explain(m, 6));
563 EXPECT_EQ("", Explain(m, 7));
564}
565
zhanyong.wan82113312010-01-08 21:55:40 +0000566// Tests explaining match result in a MATCHER* macro.
567
568MATCHER(IsEven2, "is even") {
569 if ((arg % 2) == 0) {
570 // Verifies that we can stream to result_listener, a listener
571 // supplied by the MATCHER macro implicitly.
572 *result_listener << "OK";
573 return true;
574 } else {
575 *result_listener << "% 2 == " << (arg % 2);
576 return false;
577 }
578}
579
580MATCHER_P2(EqSumOf, x, y, "") {
581 if (arg == (x + y)) {
582 *result_listener << "OK";
583 return true;
584 } else {
585 // Verifies that we can stream to the underlying stream of
586 // result_listener.
587 if (result_listener->stream() != NULL) {
588 *result_listener->stream() << "diff == " << (x + y - arg);
589 }
590 return false;
591 }
592}
593
594TEST(MatcherMacroTest, CanExplainMatchResult) {
595 const Matcher<int> m1 = IsEven2();
596 EXPECT_EQ("OK", Explain(m1, 4));
597 EXPECT_EQ("% 2 == 1", Explain(m1, 5));
598
599 const Matcher<int> m2 = EqSumOf(1, 2);
600 EXPECT_EQ("OK", Explain(m2, 3));
601 EXPECT_EQ("diff == -1", Explain(m2, 4));
602}
603
zhanyong.wance198ff2009-02-12 01:34:27 +0000604// Tests that the description string supplied to MATCHER() must be
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000605// valid.
zhanyong.wance198ff2009-02-12 01:34:27 +0000606
zhanyong.wan32de5f52009-12-23 00:13:23 +0000607MATCHER(HasBadDescription, "Invalid%") {
608 // Uses arg to suppress "unused parameter" warning.
609 return arg==arg;
610}
zhanyong.wance198ff2009-02-12 01:34:27 +0000611
612TEST(MatcherMacroTest,
613 CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) {
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000614 EXPECT_NONFATAL_FAILURE(
615 HasBadDescription(),
616 "Syntax error at index 7 in matcher description \"Invalid%\": "
617 "use \"%%\" instead of \"%\" to print \"%\".");
618}
619
zhanyong.wan32de5f52009-12-23 00:13:23 +0000620MATCHER(HasGoodDescription, "good") { return arg==arg; }
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000621
622TEST(MatcherMacroTest, AcceptsValidDescription) {
623 const Matcher<int> m = HasGoodDescription();
624 EXPECT_EQ("good", Describe(m));
zhanyong.wance198ff2009-02-12 01:34:27 +0000625}
626
627// Tests that the body of MATCHER() can reference the type of the
628// value being matched.
629
630MATCHER(IsEmptyString, "") {
631 StaticAssertTypeEq< ::std::string, arg_type>();
632 return arg == "";
633}
634
635MATCHER(IsEmptyStringByRef, "") {
636 StaticAssertTypeEq<const ::std::string&, arg_type>();
637 return arg == "";
638}
639
640TEST(MatcherMacroTest, CanReferenceArgType) {
641 const Matcher< ::std::string> m1 = IsEmptyString();
642 EXPECT_TRUE(m1.Matches(""));
643
644 const Matcher<const ::std::string&> m2 = IsEmptyStringByRef();
645 EXPECT_TRUE(m2.Matches(""));
646}
647
648// Tests that MATCHER() can be used in a namespace.
649
650namespace matcher_test {
651MATCHER(IsOdd, "") { return (arg % 2) != 0; }
652} // namespace matcher_test
653
zhanyong.wanb8243162009-06-04 05:48:20 +0000654TEST(MatcherMacroTest, WorksInNamespace) {
zhanyong.wance198ff2009-02-12 01:34:27 +0000655 Matcher<int> m = matcher_test::IsOdd();
656 EXPECT_FALSE(m.Matches(4));
657 EXPECT_TRUE(m.Matches(5));
658}
659
zhanyong.wanb8243162009-06-04 05:48:20 +0000660// Tests that Value() can be used to compose matchers.
661MATCHER(IsPositiveOdd, "") {
662 return Value(arg, matcher_test::IsOdd()) && arg > 0;
663}
664
665TEST(MatcherMacroTest, CanBeComposedUsingValue) {
666 EXPECT_THAT(3, IsPositiveOdd());
667 EXPECT_THAT(4, Not(IsPositiveOdd()));
668 EXPECT_THAT(-1, Not(IsPositiveOdd()));
669}
670
zhanyong.wance198ff2009-02-12 01:34:27 +0000671// Tests that a simple MATCHER_P() definition works.
672
673MATCHER_P(IsGreaterThan32And, n, "") { return arg > 32 && arg > n; }
674
675TEST(MatcherPMacroTest, Works) {
676 const Matcher<int> m = IsGreaterThan32And(5);
677 EXPECT_TRUE(m.Matches(36));
678 EXPECT_FALSE(m.Matches(5));
679
680 EXPECT_EQ("is greater than 32 and 5", Describe(m));
681 EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m));
682 EXPECT_EQ("", Explain(m, 36));
683 EXPECT_EQ("", Explain(m, 5));
684}
685
686// Tests that the description string supplied to MATCHER_P() must be
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000687// valid.
zhanyong.wance198ff2009-02-12 01:34:27 +0000688
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000689MATCHER_P(HasBadDescription1, n, "not %(m)s good") {
zhanyong.wance198ff2009-02-12 01:34:27 +0000690 return arg > n;
691}
692
693TEST(MatcherPMacroTest,
694 CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) {
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000695 EXPECT_NONFATAL_FAILURE(
696 HasBadDescription1(2),
697 "Syntax error at index 6 in matcher description \"not %(m)s good\": "
698 "\"m\" is an invalid parameter name.");
699}
700
701
zhanyong.wan32de5f52009-12-23 00:13:23 +0000702MATCHER_P(HasGoodDescription1, n, "good %(n)s") { return arg==arg; }
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000703
704TEST(MatcherPMacroTest, AcceptsValidDescription) {
705 const Matcher<int> m = HasGoodDescription1(5);
706 EXPECT_EQ("good 5", Describe(m));
zhanyong.wance198ff2009-02-12 01:34:27 +0000707}
708
709// Tests that the description is calculated correctly from the matcher name.
710MATCHER_P(_is_Greater_Than32and_, n, "") { return arg > 32 && arg > n; }
711
712TEST(MatcherPMacroTest, GeneratesCorrectDescription) {
713 const Matcher<int> m = _is_Greater_Than32and_(5);
714
715 EXPECT_EQ("is greater than 32 and 5", Describe(m));
716 EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m));
717 EXPECT_EQ("", Explain(m, 36));
718 EXPECT_EQ("", Explain(m, 5));
719}
720
721// Tests that a MATCHER_P matcher can be explicitly instantiated with
722// a reference parameter type.
723
724class UncopyableFoo {
725 public:
726 explicit UncopyableFoo(char value) : value_(value) {}
727 private:
728 UncopyableFoo(const UncopyableFoo&);
729 void operator=(const UncopyableFoo&);
730
731 char value_;
732};
733
734MATCHER_P(ReferencesUncopyable, variable, "") { return &arg == &variable; }
735
736TEST(MatcherPMacroTest, WorksWhenExplicitlyInstantiatedWithReference) {
737 UncopyableFoo foo1('1'), foo2('2');
738 const Matcher<const UncopyableFoo&> m =
739 ReferencesUncopyable<const UncopyableFoo&>(foo1);
740
741 EXPECT_TRUE(m.Matches(foo1));
742 EXPECT_FALSE(m.Matches(foo2));
743
744 // We don't want the address of the parameter printed, as most
745 // likely it will just annoy the user. If the address is
746 // interesting, the user should consider passing the parameter by
747 // pointer instead.
748 EXPECT_EQ("references uncopyable 1-byte object <31>", Describe(m));
749}
750
751
752// Tests that the description string supplied to MATCHER_Pn() must be
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000753// valid.
zhanyong.wance198ff2009-02-12 01:34:27 +0000754
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000755MATCHER_P2(HasBadDescription2, m, n, "not %(good") {
zhanyong.wance198ff2009-02-12 01:34:27 +0000756 return arg > m + n;
757}
758
759TEST(MatcherPnMacroTest,
760 CreatingMatcherWithBadDescriptionGeneratesNonfatalFailure) {
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000761 EXPECT_NONFATAL_FAILURE(
762 HasBadDescription2(3, 4),
763 "Syntax error at index 4 in matcher description \"not %(good\": "
764 "an interpolation must end with \")s\", but \"%(good\" does not.");
765}
766
767MATCHER_P2(HasComplexDescription, foo, bar,
768 "is as complex as %(foo)s %(bar)s (i.e. %(*)s or %%%(foo)s!)") {
zhanyong.wan32de5f52009-12-23 00:13:23 +0000769 return arg==arg;
zhanyong.wan4a5330d2009-02-19 00:36:44 +0000770}
771
772TEST(MatcherPnMacroTest, AcceptsValidDescription) {
773 Matcher<int> m = HasComplexDescription(100, "ducks");
774 EXPECT_EQ("is as complex as 100 \"ducks\" (i.e. (100, \"ducks\") or %100!)",
775 Describe(m));
zhanyong.wance198ff2009-02-12 01:34:27 +0000776}
777
778// Tests that the body of MATCHER_Pn() can reference the parameter
779// types.
780
781MATCHER_P3(ParamTypesAreIntLongAndChar, foo, bar, baz, "") {
782 StaticAssertTypeEq<int, foo_type>();
783 StaticAssertTypeEq<long, bar_type>(); // NOLINT
784 StaticAssertTypeEq<char, baz_type>();
785 return arg == 0;
786}
787
788TEST(MatcherPnMacroTest, CanReferenceParamTypes) {
789 EXPECT_THAT(0, ParamTypesAreIntLongAndChar(10, 20L, 'a'));
790}
791
792// Tests that a MATCHER_Pn matcher can be explicitly instantiated with
793// reference parameter types.
794
795MATCHER_P2(ReferencesAnyOf, variable1, variable2, "") {
796 return &arg == &variable1 || &arg == &variable2;
797}
798
799TEST(MatcherPnMacroTest, WorksWhenExplicitlyInstantiatedWithReferences) {
800 UncopyableFoo foo1('1'), foo2('2'), foo3('3');
801 const Matcher<const UncopyableFoo&> m =
802 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
803
804 EXPECT_TRUE(m.Matches(foo1));
805 EXPECT_TRUE(m.Matches(foo2));
806 EXPECT_FALSE(m.Matches(foo3));
807}
808
809TEST(MatcherPnMacroTest,
810 GeneratesCorretDescriptionWhenExplicitlyInstantiatedWithReferences) {
811 UncopyableFoo foo1('1'), foo2('2');
812 const Matcher<const UncopyableFoo&> m =
813 ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
814
815 // We don't want the addresses of the parameters printed, as most
816 // likely they will just annoy the user. If the addresses are
817 // interesting, the user should consider passing the parameters by
818 // pointers instead.
819 EXPECT_EQ("references any of (1-byte object <31>, 1-byte object <32>)",
820 Describe(m));
821}
822
823// Tests that a simple MATCHER_P2() definition works.
824
825MATCHER_P2(IsNotInClosedRange, low, hi, "") { return arg < low || arg > hi; }
826
827TEST(MatcherPnMacroTest, Works) {
828 const Matcher<const long&> m = IsNotInClosedRange(10, 20); // NOLINT
829 EXPECT_TRUE(m.Matches(36L));
830 EXPECT_FALSE(m.Matches(15L));
831
832 EXPECT_EQ("is not in closed range (10, 20)", Describe(m));
833 EXPECT_EQ("not (is not in closed range (10, 20))", DescribeNegation(m));
834 EXPECT_EQ("", Explain(m, 36L));
835 EXPECT_EQ("", Explain(m, 15L));
836}
837
838// Tests that MATCHER*() definitions can be overloaded on the number
839// of parameters; also tests MATCHER_Pn() where n >= 3.
840
841MATCHER(EqualsSumOf, "") { return arg == 0; }
842MATCHER_P(EqualsSumOf, a, "") { return arg == a; }
843MATCHER_P2(EqualsSumOf, a, b, "") { return arg == a + b; }
844MATCHER_P3(EqualsSumOf, a, b, c, "") { return arg == a + b + c; }
845MATCHER_P4(EqualsSumOf, a, b, c, d, "") { return arg == a + b + c + d; }
846MATCHER_P5(EqualsSumOf, a, b, c, d, e, "") { return arg == a + b + c + d + e; }
847MATCHER_P6(EqualsSumOf, a, b, c, d, e, f, "") {
848 return arg == a + b + c + d + e + f;
849}
850MATCHER_P7(EqualsSumOf, a, b, c, d, e, f, g, "") {
851 return arg == a + b + c + d + e + f + g;
852}
853MATCHER_P8(EqualsSumOf, a, b, c, d, e, f, g, h, "") {
854 return arg == a + b + c + d + e + f + g + h;
855}
856MATCHER_P9(EqualsSumOf, a, b, c, d, e, f, g, h, i, "") {
857 return arg == a + b + c + d + e + f + g + h + i;
858}
859MATCHER_P10(EqualsSumOf, a, b, c, d, e, f, g, h, i, j, "") {
860 return arg == a + b + c + d + e + f + g + h + i + j;
861}
862
863TEST(MatcherPnMacroTest, CanBeOverloadedOnNumberOfParameters) {
864 EXPECT_THAT(0, EqualsSumOf());
865 EXPECT_THAT(1, EqualsSumOf(1));
866 EXPECT_THAT(12, EqualsSumOf(10, 2));
867 EXPECT_THAT(123, EqualsSumOf(100, 20, 3));
868 EXPECT_THAT(1234, EqualsSumOf(1000, 200, 30, 4));
869 EXPECT_THAT(12345, EqualsSumOf(10000, 2000, 300, 40, 5));
870 EXPECT_THAT("abcdef",
871 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f'));
872 EXPECT_THAT("abcdefg",
873 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g'));
874 EXPECT_THAT("abcdefgh",
875 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
876 "h"));
877 EXPECT_THAT("abcdefghi",
878 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
879 "h", 'i'));
880 EXPECT_THAT("abcdefghij",
881 EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
882 "h", 'i', ::std::string("j")));
883
884 EXPECT_THAT(1, Not(EqualsSumOf()));
885 EXPECT_THAT(-1, Not(EqualsSumOf(1)));
886 EXPECT_THAT(-12, Not(EqualsSumOf(10, 2)));
887 EXPECT_THAT(-123, Not(EqualsSumOf(100, 20, 3)));
888 EXPECT_THAT(-1234, Not(EqualsSumOf(1000, 200, 30, 4)));
889 EXPECT_THAT(-12345, Not(EqualsSumOf(10000, 2000, 300, 40, 5)));
890 EXPECT_THAT("abcdef ",
891 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f')));
892 EXPECT_THAT("abcdefg ",
893 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f',
894 'g')));
895 EXPECT_THAT("abcdefgh ",
896 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
897 "h")));
898 EXPECT_THAT("abcdefghi ",
899 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
900 "h", 'i')));
901 EXPECT_THAT("abcdefghij ",
902 Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
903 "h", 'i', ::std::string("j"))));
904}
905
906// Tests that a MATCHER_Pn() definition can be instantiated with any
907// compatible parameter types.
908TEST(MatcherPnMacroTest, WorksForDifferentParameterTypes) {
909 EXPECT_THAT(123, EqualsSumOf(100L, 20, static_cast<char>(3)));
910 EXPECT_THAT("abcd", EqualsSumOf(::std::string("a"), "b", 'c', "d"));
911
912 EXPECT_THAT(124, Not(EqualsSumOf(100L, 20, static_cast<char>(3))));
913 EXPECT_THAT("abcde", Not(EqualsSumOf(::std::string("a"), "b", 'c', "d")));
914}
915
916// Tests that the matcher body can promote the parameter types.
917
918MATCHER_P2(EqConcat, prefix, suffix, "") {
919 // The following lines promote the two parameters to desired types.
920 std::string prefix_str(prefix);
zhanyong.wan32de5f52009-12-23 00:13:23 +0000921 char suffix_char = static_cast<char>(suffix);
zhanyong.wance198ff2009-02-12 01:34:27 +0000922 return arg == prefix_str + suffix_char;
923}
924
925TEST(MatcherPnMacroTest, SimpleTypePromotion) {
926 Matcher<std::string> no_promo =
927 EqConcat(std::string("foo"), 't');
928 Matcher<const std::string&> promo =
929 EqConcat("foo", static_cast<int>('t'));
930 EXPECT_FALSE(no_promo.Matches("fool"));
931 EXPECT_FALSE(promo.Matches("fool"));
932 EXPECT_TRUE(no_promo.Matches("foot"));
933 EXPECT_TRUE(promo.Matches("foot"));
934}
935
936// Verifies the type of a MATCHER*.
937
938TEST(MatcherPnMacroTest, TypesAreCorrect) {
939 // EqualsSumOf() must be assignable to a EqualsSumOfMatcher variable.
940 EqualsSumOfMatcher a0 = EqualsSumOf();
941
942 // EqualsSumOf(1) must be assignable to a EqualsSumOfMatcherP variable.
943 EqualsSumOfMatcherP<int> a1 = EqualsSumOf(1);
944
945 // EqualsSumOf(p1, ..., pk) must be assignable to a EqualsSumOfMatcherPk
946 // variable, and so on.
947 EqualsSumOfMatcherP2<int, char> a2 = EqualsSumOf(1, '2');
948 EqualsSumOfMatcherP3<int, int, char> a3 = EqualsSumOf(1, 2, '3');
949 EqualsSumOfMatcherP4<int, int, int, char> a4 = EqualsSumOf(1, 2, 3, '4');
950 EqualsSumOfMatcherP5<int, int, int, int, char> a5 =
951 EqualsSumOf(1, 2, 3, 4, '5');
952 EqualsSumOfMatcherP6<int, int, int, int, int, char> a6 =
953 EqualsSumOf(1, 2, 3, 4, 5, '6');
954 EqualsSumOfMatcherP7<int, int, int, int, int, int, char> a7 =
955 EqualsSumOf(1, 2, 3, 4, 5, 6, '7');
956 EqualsSumOfMatcherP8<int, int, int, int, int, int, int, char> a8 =
957 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, '8');
958 EqualsSumOfMatcherP9<int, int, int, int, int, int, int, int, char> a9 =
959 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, '9');
960 EqualsSumOfMatcherP10<int, int, int, int, int, int, int, int, int, char> a10 =
961 EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, 9, '0');
962}
963
zhanyong.wanb8243162009-06-04 05:48:20 +0000964// Tests that matcher-typed parameters can be used in Value() inside a
965// MATCHER_Pn definition.
966
967// Succeeds if arg matches exactly 2 of the 3 matchers.
968MATCHER_P3(TwoOf, m1, m2, m3, "") {
969 const int count = static_cast<int>(Value(arg, m1))
970 + static_cast<int>(Value(arg, m2)) + static_cast<int>(Value(arg, m3));
971 return count == 2;
972}
973
974TEST(MatcherPnMacroTest, CanUseMatcherTypedParameterInValue) {
975 EXPECT_THAT(42, TwoOf(Gt(0), Lt(50), Eq(10)));
976 EXPECT_THAT(0, Not(TwoOf(Gt(-1), Lt(1), Eq(0))));
977}
978
979// Tests Contains().
980
zhanyong.wan1bee7b22009-02-20 18:31:04 +0000981TEST(ContainsTest, ListMatchesWhenElementIsInContainer) {
982 list<int> some_list;
983 some_list.push_back(3);
984 some_list.push_back(1);
985 some_list.push_back(2);
986 EXPECT_THAT(some_list, Contains(1));
zhanyong.wanb8243162009-06-04 05:48:20 +0000987 EXPECT_THAT(some_list, Contains(Gt(2.5)));
988 EXPECT_THAT(some_list, Contains(Eq(2.0f)));
zhanyong.wan1bee7b22009-02-20 18:31:04 +0000989
990 list<string> another_list;
991 another_list.push_back("fee");
992 another_list.push_back("fie");
993 another_list.push_back("foe");
994 another_list.push_back("fum");
995 EXPECT_THAT(another_list, Contains(string("fee")));
996}
997
998TEST(ContainsTest, ListDoesNotMatchWhenElementIsNotInContainer) {
999 list<int> some_list;
1000 some_list.push_back(3);
1001 some_list.push_back(1);
1002 EXPECT_THAT(some_list, Not(Contains(4)));
1003}
1004
1005TEST(ContainsTest, SetMatchesWhenElementIsInContainer) {
1006 set<int> some_set;
1007 some_set.insert(3);
1008 some_set.insert(1);
1009 some_set.insert(2);
zhanyong.wanb8243162009-06-04 05:48:20 +00001010 EXPECT_THAT(some_set, Contains(Eq(1.0)));
1011 EXPECT_THAT(some_set, Contains(Eq(3.0f)));
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001012 EXPECT_THAT(some_set, Contains(2));
1013
1014 set<const char*> another_set;
1015 another_set.insert("fee");
1016 another_set.insert("fie");
1017 another_set.insert("foe");
1018 another_set.insert("fum");
zhanyong.wanb8243162009-06-04 05:48:20 +00001019 EXPECT_THAT(another_set, Contains(Eq(string("fum"))));
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001020}
1021
1022TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) {
1023 set<int> some_set;
1024 some_set.insert(3);
1025 some_set.insert(1);
1026 EXPECT_THAT(some_set, Not(Contains(4)));
1027
1028 set<const char*> c_string_set;
1029 c_string_set.insert("hello");
1030 EXPECT_THAT(c_string_set, Not(Contains(string("hello").c_str())));
1031}
1032
1033TEST(ContainsTest, DescribesItselfCorrectly) {
zhanyong.wanb8243162009-06-04 05:48:20 +00001034 const int a[2] = { 1, 2 };
1035 Matcher<const int(&)[2]> m = Contains(2);
1036 EXPECT_EQ("element 1 matches", Explain(m, a));
1037
1038 m = Contains(3);
1039 EXPECT_EQ("", Explain(m, a));
1040}
1041
1042TEST(ContainsTest, ExplainsMatchResultCorrectly) {
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001043 Matcher<vector<int> > m = Contains(1);
zhanyong.wanb8243162009-06-04 05:48:20 +00001044 EXPECT_EQ("contains at least one element that is equal to 1", Describe(m));
1045
1046 Matcher<vector<int> > m2 = Not(m);
1047 EXPECT_EQ("doesn't contain any element that is equal to 1", Describe(m2));
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001048}
1049
1050TEST(ContainsTest, MapMatchesWhenElementIsInContainer) {
1051 map<const char*, int> my_map;
1052 const char* bar = "a string";
1053 my_map[bar] = 2;
1054 EXPECT_THAT(my_map, Contains(pair<const char* const, int>(bar, 2)));
1055
1056 map<string, int> another_map;
1057 another_map["fee"] = 1;
1058 another_map["fie"] = 2;
1059 another_map["foe"] = 3;
1060 another_map["fum"] = 4;
1061 EXPECT_THAT(another_map, Contains(pair<const string, int>(string("fee"), 1)));
1062 EXPECT_THAT(another_map, Contains(pair<const string, int>("fie", 2)));
1063}
1064
1065TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) {
1066 map<int, int> some_map;
1067 some_map[1] = 11;
1068 some_map[2] = 22;
1069 EXPECT_THAT(some_map, Not(Contains(pair<const int, int>(2, 23))));
1070}
1071
1072TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) {
1073 const char* string_array[] = { "fee", "fie", "foe", "fum" };
zhanyong.wanb8243162009-06-04 05:48:20 +00001074 EXPECT_THAT(string_array, Contains(Eq(string("fum"))));
zhanyong.wan1bee7b22009-02-20 18:31:04 +00001075}
1076
1077TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) {
1078 int int_array[] = { 1, 2, 3, 4 };
1079 EXPECT_THAT(int_array, Not(Contains(5)));
1080}
1081
zhanyong.wanb8243162009-06-04 05:48:20 +00001082TEST(ContainsTest, AcceptsMatcher) {
1083 const int a[] = { 1, 2, 3 };
1084 EXPECT_THAT(a, Contains(Gt(2)));
1085 EXPECT_THAT(a, Not(Contains(Gt(4))));
1086}
1087
1088TEST(ContainsTest, WorksForNativeArrayAsTuple) {
1089 const int a[] = { 1, 2 };
zhanyong.wan2661c682009-06-09 05:42:12 +00001090 const int* const pointer = a;
1091 EXPECT_THAT(make_tuple(pointer, 2), Contains(1));
1092 EXPECT_THAT(make_tuple(pointer, 2), Not(Contains(Gt(3))));
zhanyong.wanb8243162009-06-04 05:48:20 +00001093}
1094
1095TEST(ContainsTest, WorksForTwoDimensionalNativeArray) {
1096 int a[][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
1097 EXPECT_THAT(a, Contains(ElementsAre(4, 5, 6)));
1098 EXPECT_THAT(a, Contains(Contains(5)));
1099 EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5))));
1100 EXPECT_THAT(a, Contains(Not(Contains(5))));
1101}
1102
zhanyong.wan82113312010-01-08 21:55:40 +00001103#ifdef _MSC_VER
1104#pragma warning(pop)
1105#endif
1106
shiqiane35fdd92008-12-10 05:08:54 +00001107} // namespace