gmock-generated-matchers_test.cc
Go to the documentation of this file.
1 // 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 
35 
36 #include <list>
37 #include <map>
38 #include <set>
39 #include <sstream>
40 #include <string>
41 #include <utility>
42 #include <vector>
43 
44 #include "gmock/gmock.h"
45 #include "gtest/gtest.h"
46 #include "gtest/gtest-spi.h"
47 
48 namespace {
49 
50 using std::list;
51 using std::map;
52 using std::pair;
53 using std::set;
54 using std::stringstream;
55 using std::vector;
56 using std::tr1::get;
58 using std::tr1::tuple;
59 using testing::_;
60 using testing::Args;
61 using testing::Contains;
64 using testing::Eq;
65 using testing::Ge;
66 using testing::Gt;
67 using testing::Le;
68 using testing::Lt;
70 using testing::Matcher;
73 using testing::Ne;
74 using testing::Not;
75 using testing::Pointee;
77 using testing::Ref;
79 using testing::StrEq;
80 using testing::Value;
83 
84 // Evaluates to the number of elements in 'array'.
85 #define GMOCK_ARRAY_SIZE_(a) (sizeof(a) / sizeof(a[0]))
86 
87 // Returns the description of the given matcher.
88 template <typename T>
89 string Describe(const Matcher<T>& m) {
90  stringstream ss;
91  m.DescribeTo(&ss);
92  return ss.str();
93 }
94 
95 // Returns the description of the negation of the given matcher.
96 template <typename T>
97 string DescribeNegation(const Matcher<T>& m) {
98  stringstream ss;
99  m.DescribeNegationTo(&ss);
100  return ss.str();
101 }
102 
103 // Returns the reason why x matches, or doesn't match, m.
104 template <typename MatcherType, typename Value>
105 string Explain(const MatcherType& m, const Value& x) {
106  stringstream ss;
107  m.ExplainMatchResultTo(x, &ss);
108  return ss.str();
109 }
110 
111 // Tests Args<k0, ..., kn>(m).
112 
113 TEST(ArgsTest, AcceptsZeroTemplateArg) {
114  const tuple<int, bool> t(5, true);
115  EXPECT_THAT(t, Args<>(Eq(tuple<>())));
116  EXPECT_THAT(t, Not(Args<>(Ne(tuple<>()))));
117 }
118 
119 TEST(ArgsTest, AcceptsOneTemplateArg) {
120  const tuple<int, bool> t(5, true);
121  EXPECT_THAT(t, Args<0>(Eq(make_tuple(5))));
122  EXPECT_THAT(t, Args<1>(Eq(make_tuple(true))));
123  EXPECT_THAT(t, Not(Args<1>(Eq(make_tuple(false)))));
124 }
125 
126 TEST(ArgsTest, AcceptsTwoTemplateArgs) {
127  const tuple<short, int, long> t(4, 5, 6L); // NOLINT
128 
129  EXPECT_THAT(t, (Args<0, 1>(Lt())));
130  EXPECT_THAT(t, (Args<1, 2>(Lt())));
131  EXPECT_THAT(t, Not(Args<0, 2>(Gt())));
132 }
133 
134 TEST(ArgsTest, AcceptsRepeatedTemplateArgs) {
135  const tuple<short, int, long> t(4, 5, 6L); // NOLINT
136  EXPECT_THAT(t, (Args<0, 0>(Eq())));
137  EXPECT_THAT(t, Not(Args<1, 1>(Ne())));
138 }
139 
140 TEST(ArgsTest, AcceptsDecreasingTemplateArgs) {
141  const tuple<short, int, long> t(4, 5, 6L); // NOLINT
142  EXPECT_THAT(t, (Args<2, 0>(Gt())));
143  EXPECT_THAT(t, Not(Args<2, 1>(Lt())));
144 }
145 
146 // The MATCHER*() macros trigger warning C4100 (unreferenced formal
147 // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
148 // the macro definition, as the warnings are generated when the macro
149 // is expanded and macro expansion cannot contain #pragma. Therefore
150 // we suppress them here.
151 #ifdef _MSC_VER
152 # pragma warning(push)
153 # pragma warning(disable:4100)
154 #endif
155 
156 MATCHER(SumIsZero, "") {
157  return get<0>(arg) + get<1>(arg) + get<2>(arg) == 0;
158 }
159 
160 TEST(ArgsTest, AcceptsMoreTemplateArgsThanArityOfOriginalTuple) {
161  EXPECT_THAT(make_tuple(-1, 2), (Args<0, 0, 1>(SumIsZero())));
162  EXPECT_THAT(make_tuple(1, 2), Not(Args<0, 0, 1>(SumIsZero())));
163 }
164 
165 TEST(ArgsTest, CanBeNested) {
166  const tuple<short, int, long, int> t(4, 5, 6L, 6); // NOLINT
167  EXPECT_THAT(t, (Args<1, 2, 3>(Args<1, 2>(Eq()))));
168  EXPECT_THAT(t, (Args<0, 1, 3>(Args<0, 2>(Lt()))));
169 }
170 
171 TEST(ArgsTest, CanMatchTupleByValue) {
172  typedef tuple<char, int, int> Tuple3;
173  const Matcher<Tuple3> m = Args<1, 2>(Lt());
174  EXPECT_TRUE(m.Matches(Tuple3('a', 1, 2)));
175  EXPECT_FALSE(m.Matches(Tuple3('b', 2, 2)));
176 }
177 
178 TEST(ArgsTest, CanMatchTupleByReference) {
179  typedef tuple<char, char, int> Tuple3;
180  const Matcher<const Tuple3&> m = Args<0, 1>(Lt());
181  EXPECT_TRUE(m.Matches(Tuple3('a', 'b', 2)));
182  EXPECT_FALSE(m.Matches(Tuple3('b', 'b', 2)));
183 }
184 
185 // Validates that arg is printed as str.
186 MATCHER_P(PrintsAs, str, "") {
187  return testing::PrintToString(arg) == str;
188 }
189 
190 TEST(ArgsTest, AcceptsTenTemplateArgs) {
191  EXPECT_THAT(make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
192  (Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
193  PrintsAs("(9, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
194  EXPECT_THAT(make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
195  Not(Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
196  PrintsAs("(0, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
197 }
198 
199 TEST(ArgsTest, DescirbesSelfCorrectly) {
200  const Matcher<tuple<int, bool, char> > m = Args<2, 0>(Lt());
201  EXPECT_EQ("are a tuple whose fields (#2, #0) are a pair where "
202  "the first < the second",
203  Describe(m));
204 }
205 
206 TEST(ArgsTest, DescirbesNestedArgsCorrectly) {
207  const Matcher<const tuple<int, bool, char, int>&> m =
208  Args<0, 2, 3>(Args<2, 0>(Lt()));
209  EXPECT_EQ("are a tuple whose fields (#0, #2, #3) are a tuple "
210  "whose fields (#2, #0) are a pair where the first < the second",
211  Describe(m));
212 }
213 
214 TEST(ArgsTest, DescribesNegationCorrectly) {
215  const Matcher<tuple<int, char> > m = Args<1, 0>(Gt());
216  EXPECT_EQ("are a tuple whose fields (#1, #0) aren't a pair "
217  "where the first > the second",
218  DescribeNegation(m));
219 }
220 
221 TEST(ArgsTest, ExplainsMatchResultWithoutInnerExplanation) {
222  const Matcher<tuple<bool, int, int> > m = Args<1, 2>(Eq());
223  EXPECT_EQ("whose fields (#1, #2) are (42, 42)",
224  Explain(m, make_tuple(false, 42, 42)));
225  EXPECT_EQ("whose fields (#1, #2) are (42, 43)",
226  Explain(m, make_tuple(false, 42, 43)));
227 }
228 
229 // For testing Args<>'s explanation.
230 class LessThanMatcher : public MatcherInterface<tuple<char, int> > {
231  public:
232  virtual void DescribeTo(::std::ostream* os) const {}
233 
234  virtual bool MatchAndExplain(tuple<char, int> value,
235  MatchResultListener* listener) const {
236  const int diff = get<0>(value) - get<1>(value);
237  if (diff > 0) {
238  *listener << "where the first value is " << diff
239  << " more than the second";
240  }
241  return diff < 0;
242  }
243 };
244 
245 Matcher<tuple<char, int> > LessThan() {
246  return MakeMatcher(new LessThanMatcher);
247 }
248 
249 TEST(ArgsTest, ExplainsMatchResultWithInnerExplanation) {
250  const Matcher<tuple<char, int, int> > m = Args<0, 2>(LessThan());
251  EXPECT_EQ("whose fields (#0, #2) are ('a' (97, 0x61), 42), "
252  "where the first value is 55 more than the second",
253  Explain(m, make_tuple('a', 42, 42)));
254  EXPECT_EQ("whose fields (#0, #2) are ('\\0', 43)",
255  Explain(m, make_tuple('\0', 42, 43)));
256 }
257 
258 // For testing ExplainMatchResultTo().
259 class GreaterThanMatcher : public MatcherInterface<int> {
260  public:
261  explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
262 
263  virtual void DescribeTo(::std::ostream* os) const {
264  *os << "is greater than " << rhs_;
265  }
266 
267  virtual bool MatchAndExplain(int lhs,
268  MatchResultListener* listener) const {
269  const int diff = lhs - rhs_;
270  if (diff > 0) {
271  *listener << "which is " << diff << " more than " << rhs_;
272  } else if (diff == 0) {
273  *listener << "which is the same as " << rhs_;
274  } else {
275  *listener << "which is " << -diff << " less than " << rhs_;
276  }
277 
278  return lhs > rhs_;
279  }
280 
281  private:
282  int rhs_;
283 };
284 
285 Matcher<int> GreaterThan(int n) {
286  return MakeMatcher(new GreaterThanMatcher(n));
287 }
288 
289 // Tests for ElementsAre().
290 
291 TEST(ElementsAreTest, CanDescribeExpectingNoElement) {
292  Matcher<const vector<int>&> m = ElementsAre();
293  EXPECT_EQ("is empty", Describe(m));
294 }
295 
296 TEST(ElementsAreTest, CanDescribeExpectingOneElement) {
297  Matcher<vector<int> > m = ElementsAre(Gt(5));
298  EXPECT_EQ("has 1 element that is > 5", Describe(m));
299 }
300 
301 TEST(ElementsAreTest, CanDescribeExpectingManyElements) {
302  Matcher<list<string> > m = ElementsAre(StrEq("one"), "two");
303  EXPECT_EQ("has 2 elements where\n"
304  "element #0 is equal to \"one\",\n"
305  "element #1 is equal to \"two\"", Describe(m));
306 }
307 
308 TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) {
309  Matcher<vector<int> > m = ElementsAre();
310  EXPECT_EQ("isn't empty", DescribeNegation(m));
311 }
312 
313 TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElment) {
314  Matcher<const list<int>& > m = ElementsAre(Gt(5));
315  EXPECT_EQ("doesn't have 1 element, or\n"
316  "element #0 isn't > 5", DescribeNegation(m));
317 }
318 
319 TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) {
320  Matcher<const list<string>& > m = ElementsAre("one", "two");
321  EXPECT_EQ("doesn't have 2 elements, or\n"
322  "element #0 isn't equal to \"one\", or\n"
323  "element #1 isn't equal to \"two\"", DescribeNegation(m));
324 }
325 
326 TEST(ElementsAreTest, DoesNotExplainTrivialMatch) {
327  Matcher<const list<int>& > m = ElementsAre(1, Ne(2));
328 
329  list<int> test_list;
330  test_list.push_back(1);
331  test_list.push_back(3);
332  EXPECT_EQ("", Explain(m, test_list)); // No need to explain anything.
333 }
334 
335 TEST(ElementsAreTest, ExplainsNonTrivialMatch) {
336  Matcher<const vector<int>& > m =
338 
339  const int a[] = { 10, 0, 100 };
340  vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
341  EXPECT_EQ("whose element #0 matches, which is 9 more than 1,\n"
342  "and whose element #2 matches, which is 98 more than 2",
343  Explain(m, test_vector));
344 }
345 
346 TEST(ElementsAreTest, CanExplainMismatchWrongSize) {
347  Matcher<const list<int>& > m = ElementsAre(1, 3);
348 
349  list<int> test_list;
350  // No need to explain when the container is empty.
351  EXPECT_EQ("", Explain(m, test_list));
352 
353  test_list.push_back(1);
354  EXPECT_EQ("which has 1 element", Explain(m, test_list));
355 }
356 
357 TEST(ElementsAreTest, CanExplainMismatchRightSize) {
358  Matcher<const vector<int>& > m = ElementsAre(1, GreaterThan(5));
359 
360  vector<int> v;
361  v.push_back(2);
362  v.push_back(1);
363  EXPECT_EQ("whose element #0 doesn't match", Explain(m, v));
364 
365  v[0] = 1;
366  EXPECT_EQ("whose element #1 doesn't match, which is 4 less than 5",
367  Explain(m, v));
368 }
369 
370 TEST(ElementsAreTest, MatchesOneElementVector) {
371  vector<string> test_vector;
372  test_vector.push_back("test string");
373 
374  EXPECT_THAT(test_vector, ElementsAre(StrEq("test string")));
375 }
376 
377 TEST(ElementsAreTest, MatchesOneElementList) {
378  list<string> test_list;
379  test_list.push_back("test string");
380 
381  EXPECT_THAT(test_list, ElementsAre("test string"));
382 }
383 
384 TEST(ElementsAreTest, MatchesThreeElementVector) {
385  vector<string> test_vector;
386  test_vector.push_back("one");
387  test_vector.push_back("two");
388  test_vector.push_back("three");
389 
390  EXPECT_THAT(test_vector, ElementsAre("one", StrEq("two"), _));
391 }
392 
393 TEST(ElementsAreTest, MatchesOneElementEqMatcher) {
394  vector<int> test_vector;
395  test_vector.push_back(4);
396 
397  EXPECT_THAT(test_vector, ElementsAre(Eq(4)));
398 }
399 
400 TEST(ElementsAreTest, MatchesOneElementAnyMatcher) {
401  vector<int> test_vector;
402  test_vector.push_back(4);
403 
404  EXPECT_THAT(test_vector, ElementsAre(_));
405 }
406 
407 TEST(ElementsAreTest, MatchesOneElementValue) {
408  vector<int> test_vector;
409  test_vector.push_back(4);
410 
411  EXPECT_THAT(test_vector, ElementsAre(4));
412 }
413 
414 TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) {
415  vector<int> test_vector;
416  test_vector.push_back(1);
417  test_vector.push_back(2);
418  test_vector.push_back(3);
419 
420  EXPECT_THAT(test_vector, ElementsAre(1, Eq(2), _));
421 }
422 
423 TEST(ElementsAreTest, MatchesTenElementVector) {
424  const int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
425  vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
426 
427  EXPECT_THAT(test_vector,
428  // The element list can contain values and/or matchers
429  // of different types.
430  ElementsAre(0, Ge(0), _, 3, 4, Ne(2), Eq(6), 7, 8, _));
431 }
432 
433 TEST(ElementsAreTest, DoesNotMatchWrongSize) {
434  vector<string> test_vector;
435  test_vector.push_back("test string");
436  test_vector.push_back("test string");
437 
438  Matcher<vector<string> > m = ElementsAre(StrEq("test string"));
439  EXPECT_FALSE(m.Matches(test_vector));
440 }
441 
442 TEST(ElementsAreTest, DoesNotMatchWrongValue) {
443  vector<string> test_vector;
444  test_vector.push_back("other string");
445 
446  Matcher<vector<string> > m = ElementsAre(StrEq("test string"));
447  EXPECT_FALSE(m.Matches(test_vector));
448 }
449 
450 TEST(ElementsAreTest, DoesNotMatchWrongOrder) {
451  vector<string> test_vector;
452  test_vector.push_back("one");
453  test_vector.push_back("three");
454  test_vector.push_back("two");
455 
456  Matcher<vector<string> > m = ElementsAre(
457  StrEq("one"), StrEq("two"), StrEq("three"));
458  EXPECT_FALSE(m.Matches(test_vector));
459 }
460 
461 TEST(ElementsAreTest, WorksForNestedContainer) {
462  const char* strings[] = {
463  "Hi",
464  "world"
465  };
466 
467  vector<list<char> > nested;
468  for (size_t i = 0; i < GMOCK_ARRAY_SIZE_(strings); i++) {
469  nested.push_back(list<char>(strings[i], strings[i] + strlen(strings[i])));
470  }
471 
472  EXPECT_THAT(nested, ElementsAre(ElementsAre('H', Ne('e')),
473  ElementsAre('w', 'o', _, _, 'd')));
474  EXPECT_THAT(nested, Not(ElementsAre(ElementsAre('H', 'e'),
475  ElementsAre('w', 'o', _, _, 'd'))));
476 }
477 
478 TEST(ElementsAreTest, WorksWithByRefElementMatchers) {
479  int a[] = { 0, 1, 2 };
480  vector<int> v(a, a + GMOCK_ARRAY_SIZE_(a));
481 
482  EXPECT_THAT(v, ElementsAre(Ref(v[0]), Ref(v[1]), Ref(v[2])));
483  EXPECT_THAT(v, Not(ElementsAre(Ref(v[0]), Ref(v[1]), Ref(a[2]))));
484 }
485 
486 TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) {
487  int a[] = { 0, 1, 2 };
488  vector<int> v(a, a + GMOCK_ARRAY_SIZE_(a));
489 
490  EXPECT_THAT(&v, Pointee(ElementsAre(0, 1, _)));
491  EXPECT_THAT(&v, Not(Pointee(ElementsAre(0, _, 3))));
492 }
493 
494 TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) {
495  int array[] = { 0, 1, 2 };
496  EXPECT_THAT(array, ElementsAre(0, 1, _));
497  EXPECT_THAT(array, Not(ElementsAre(1, _, _)));
498  EXPECT_THAT(array, Not(ElementsAre(0, _)));
499 }
500 
501 class NativeArrayPassedAsPointerAndSize {
502  public:
503  NativeArrayPassedAsPointerAndSize() {}
504 
505  MOCK_METHOD2(Helper, void(int* array, int size));
506 
507  private:
508  GTEST_DISALLOW_COPY_AND_ASSIGN_(NativeArrayPassedAsPointerAndSize);
509 };
510 
511 TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) {
512  int array[] = { 0, 1 };
513  ::std::tr1::tuple<int*, size_t> array_as_tuple(array, 2);
514  EXPECT_THAT(array_as_tuple, ElementsAre(0, 1));
515  EXPECT_THAT(array_as_tuple, Not(ElementsAre(0)));
516 
517  NativeArrayPassedAsPointerAndSize helper;
518  EXPECT_CALL(helper, Helper(_, _))
519  .With(ElementsAre(0, 1));
520  helper.Helper(array, 2);
521 }
522 
523 TEST(ElementsAreTest, WorksWithTwoDimensionalNativeArray) {
524  const char a2[][3] = { "hi", "lo" };
525  EXPECT_THAT(a2, ElementsAre(ElementsAre('h', 'i', '\0'),
526  ElementsAre('l', 'o', '\0')));
527  EXPECT_THAT(a2, ElementsAre(StrEq("hi"), StrEq("lo")));
528  EXPECT_THAT(a2, ElementsAre(Not(ElementsAre('h', 'o', '\0')),
529  ElementsAre('l', 'o', '\0')));
530 }
531 
532 TEST(ElementsAreTest, AcceptsStringLiteral) {
533  string array[] = { "hi", "one", "two" };
534  EXPECT_THAT(array, ElementsAre("hi", "one", "two"));
535  EXPECT_THAT(array, Not(ElementsAre("hi", "one", "too")));
536 }
537 
538 #ifndef _MSC_VER
539 
540 // The following test passes a value of type const char[] to a
541 // function template that expects const T&. Some versions of MSVC
542 // generates a compiler error C2665 for that. We believe it's a bug
543 // in MSVC. Therefore this test is #if-ed out for MSVC.
544 
545 // Declared here with the size unknown. Defined AFTER the following test.
546 extern const char kHi[];
547 
548 TEST(ElementsAreTest, AcceptsArrayWithUnknownSize) {
549  // The size of kHi is not known in this test, but ElementsAre() should
550  // still accept it.
551 
552  string array1[] = { "hi" };
553  EXPECT_THAT(array1, ElementsAre(kHi));
554 
555  string array2[] = { "ho" };
556  EXPECT_THAT(array2, Not(ElementsAre(kHi)));
557 }
558 
559 const char kHi[] = "hi";
560 
561 #endif // _MSC_VER
562 
563 TEST(ElementsAreTest, MakesCopyOfArguments) {
564  int x = 1;
565  int y = 2;
566  // This should make a copy of x and y.
568  polymorphic_matcher = ElementsAre(x, y);
569  // Changing x and y now shouldn't affect the meaning of the above matcher.
570  x = y = 0;
571  const int array1[] = { 1, 2 };
572  EXPECT_THAT(array1, polymorphic_matcher);
573  const int array2[] = { 0, 0 };
574  EXPECT_THAT(array2, Not(polymorphic_matcher));
575 }
576 
577 
578 // Tests for ElementsAreArray(). Since ElementsAreArray() shares most
579 // of the implementation with ElementsAre(), we don't test it as
580 // thoroughly here.
581 
582 TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) {
583  const int a[] = { 1, 2, 3 };
584 
585  vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
586  EXPECT_THAT(test_vector, ElementsAreArray(a));
587 
588  test_vector[2] = 0;
589  EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
590 }
591 
592 TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) {
593  const char* a[] = { "one", "two", "three" };
594 
595  vector<string> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
596  EXPECT_THAT(test_vector, ElementsAreArray(a, GMOCK_ARRAY_SIZE_(a)));
597 
598  const char** p = a;
599  test_vector[0] = "1";
600  EXPECT_THAT(test_vector, Not(ElementsAreArray(p, GMOCK_ARRAY_SIZE_(a))));
601 }
602 
603 TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) {
604  const char* a[] = { "one", "two", "three" };
605 
606  vector<string> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
607  EXPECT_THAT(test_vector, ElementsAreArray(a));
608 
609  test_vector[0] = "1";
610  EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
611 }
612 
613 TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) {
614  const Matcher<string> kMatcherArray[] =
615  { StrEq("one"), StrEq("two"), StrEq("three") };
616 
617  vector<string> test_vector;
618  test_vector.push_back("one");
619  test_vector.push_back("two");
620  test_vector.push_back("three");
621  EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray));
622 
623  test_vector.push_back("three");
624  EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray)));
625 }
626 
627 TEST(ElementsAreArrayTest, CanBeCreatedWithVector) {
628  const int a[] = { 1, 2, 3 };
629  vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
630  const vector<int> expected(a, a + GMOCK_ARRAY_SIZE_(a));
631  EXPECT_THAT(test_vector, ElementsAreArray(expected));
632  test_vector.push_back(4);
633  EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));
634 }
635 
636 #if GTEST_LANG_CXX11
637 
638 TEST(ElementsAreArrayTest, TakesInitializerList) {
639  const int a[5] = { 1, 2, 3, 4, 5 };
640  EXPECT_THAT(a, ElementsAreArray({ 1, 2, 3, 4, 5 }));
641  EXPECT_THAT(a, Not(ElementsAreArray({ 1, 2, 3, 5, 4 })));
642  EXPECT_THAT(a, Not(ElementsAreArray({ 1, 2, 3, 4, 6 })));
643 }
644 
645 TEST(ElementsAreArrayTest, TakesInitializerListOfCStrings) {
646  const string a[5] = { "a", "b", "c", "d", "e" };
647  EXPECT_THAT(a, ElementsAreArray({ "a", "b", "c", "d", "e" }));
648  EXPECT_THAT(a, Not(ElementsAreArray({ "a", "b", "c", "e", "d" })));
649  EXPECT_THAT(a, Not(ElementsAreArray({ "a", "b", "c", "d", "ef" })));
650 }
651 
652 TEST(ElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {
653  const int a[5] = { 1, 2, 3, 4, 5 };
655  { Eq(1), Eq(2), Eq(3), Eq(4), Eq(5) }));
657  { Eq(1), Eq(2), Eq(3), Eq(4), Eq(6) })));
658 }
659 
660 TEST(ElementsAreArrayTest,
661  TakesInitializerListOfDifferentTypedMatchers) {
662  const int a[5] = { 1, 2, 3, 4, 5 };
663  // The compiler cannot infer the type of the initializer list if its
664  // elements have different types. We must explicitly specify the
665  // unified element type in this case.
666  EXPECT_THAT(a, ElementsAreArray<Matcher<int> >(
667  { Eq(1), Ne(-2), Ge(3), Le(4), Eq(5) }));
668  EXPECT_THAT(a, Not(ElementsAreArray<Matcher<int> >(
669  { Eq(1), Ne(-2), Ge(3), Le(4), Eq(6) })));
670 }
671 
672 #endif // GTEST_LANG_CXX11
673 
674 TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherVector) {
675  const int a[] = { 1, 2, 3 };
676  const Matcher<int> kMatchers[] = { Eq(1), Eq(2), Eq(3) };
677  vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
678  const vector<Matcher<int> > expected(
679  kMatchers, kMatchers + GMOCK_ARRAY_SIZE_(kMatchers));
680  EXPECT_THAT(test_vector, ElementsAreArray(expected));
681  test_vector.push_back(4);
682  EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));
683 }
684 
685 TEST(ElementsAreArrayTest, CanBeCreatedWithIteratorRange) {
686  const int a[] = { 1, 2, 3 };
687  const vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
688  const vector<int> expected(a, a + GMOCK_ARRAY_SIZE_(a));
689  EXPECT_THAT(test_vector, ElementsAreArray(expected.begin(), expected.end()));
690  // Pointers are iterators, too.
691  EXPECT_THAT(test_vector, ElementsAreArray(a, a + GMOCK_ARRAY_SIZE_(a)));
692  // The empty range of NULL pointers should also be okay.
693  int* const null_int = NULL;
694  EXPECT_THAT(test_vector, Not(ElementsAreArray(null_int, null_int)));
695  EXPECT_THAT((vector<int>()), ElementsAreArray(null_int, null_int));
696 }
697 
698 // Since ElementsAre() and ElementsAreArray() share much of the
699 // implementation, we only do a sanity test for native arrays here.
700 TEST(ElementsAreArrayTest, WorksWithNativeArray) {
701  ::std::string a[] = { "hi", "ho" };
702  ::std::string b[] = { "hi", "ho" };
703 
705  EXPECT_THAT(a, ElementsAreArray(b, 2));
706  EXPECT_THAT(a, Not(ElementsAreArray(b, 1)));
707 }
708 
709 TEST(ElementsAreArrayTest, SourceLifeSpan) {
710  const int a[] = { 1, 2, 3 };
711  vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
712  vector<int> expect(a, a + GMOCK_ARRAY_SIZE_(a));
713  ElementsAreArrayMatcher<int> matcher_maker =
714  ElementsAreArray(expect.begin(), expect.end());
715  EXPECT_THAT(test_vector, matcher_maker);
716  // Changing in place the values that initialized matcher_maker should not
717  // affect matcher_maker anymore. It should have made its own copy of them.
718  typedef vector<int>::iterator Iter;
719  for (Iter it = expect.begin(); it != expect.end(); ++it) { *it += 10; }
720  EXPECT_THAT(test_vector, matcher_maker);
721  test_vector.push_back(3);
722  EXPECT_THAT(test_vector, Not(matcher_maker));
723 }
724 
725 // Tests for the MATCHER*() macro family.
726 
727 // Tests that a simple MATCHER() definition works.
728 
729 MATCHER(IsEven, "") { return (arg % 2) == 0; }
730 
731 TEST(MatcherMacroTest, Works) {
732  const Matcher<int> m = IsEven();
733  EXPECT_TRUE(m.Matches(6));
734  EXPECT_FALSE(m.Matches(7));
735 
736  EXPECT_EQ("is even", Describe(m));
737  EXPECT_EQ("not (is even)", DescribeNegation(m));
738  EXPECT_EQ("", Explain(m, 6));
739  EXPECT_EQ("", Explain(m, 7));
740 }
741 
742 // This also tests that the description string can reference 'negation'.
743 MATCHER(IsEven2, negation ? "is odd" : "is even") {
744  if ((arg % 2) == 0) {
745  // Verifies that we can stream to result_listener, a listener
746  // supplied by the MATCHER macro implicitly.
747  *result_listener << "OK";
748  return true;
749  } else {
750  *result_listener << "% 2 == " << (arg % 2);
751  return false;
752  }
753 }
754 
755 // This also tests that the description string can reference matcher
756 // parameters.
757 MATCHER_P2(EqSumOf, x, y,
758  string(negation ? "doesn't equal" : "equals") + " the sum of " +
759  PrintToString(x) + " and " + PrintToString(y)) {
760  if (arg == (x + y)) {
761  *result_listener << "OK";
762  return true;
763  } else {
764  // Verifies that we can stream to the underlying stream of
765  // result_listener.
766  if (result_listener->stream() != NULL) {
767  *result_listener->stream() << "diff == " << (x + y - arg);
768  }
769  return false;
770  }
771 }
772 
773 // Tests that the matcher description can reference 'negation' and the
774 // matcher parameters.
775 TEST(MatcherMacroTest, DescriptionCanReferenceNegationAndParameters) {
776  const Matcher<int> m1 = IsEven2();
777  EXPECT_EQ("is even", Describe(m1));
778  EXPECT_EQ("is odd", DescribeNegation(m1));
779 
780  const Matcher<int> m2 = EqSumOf(5, 9);
781  EXPECT_EQ("equals the sum of 5 and 9", Describe(m2));
782  EXPECT_EQ("doesn't equal the sum of 5 and 9", DescribeNegation(m2));
783 }
784 
785 // Tests explaining match result in a MATCHER* macro.
786 TEST(MatcherMacroTest, CanExplainMatchResult) {
787  const Matcher<int> m1 = IsEven2();
788  EXPECT_EQ("OK", Explain(m1, 4));
789  EXPECT_EQ("% 2 == 1", Explain(m1, 5));
790 
791  const Matcher<int> m2 = EqSumOf(1, 2);
792  EXPECT_EQ("OK", Explain(m2, 3));
793  EXPECT_EQ("diff == -1", Explain(m2, 4));
794 }
795 
796 // Tests that the body of MATCHER() can reference the type of the
797 // value being matched.
798 
799 MATCHER(IsEmptyString, "") {
800  StaticAssertTypeEq< ::std::string, arg_type>();
801  return arg == "";
802 }
803 
804 MATCHER(IsEmptyStringByRef, "") {
805  StaticAssertTypeEq<const ::std::string&, arg_type>();
806  return arg == "";
807 }
808 
809 TEST(MatcherMacroTest, CanReferenceArgType) {
810  const Matcher< ::std::string> m1 = IsEmptyString();
811  EXPECT_TRUE(m1.Matches(""));
812 
813  const Matcher<const ::std::string&> m2 = IsEmptyStringByRef();
814  EXPECT_TRUE(m2.Matches(""));
815 }
816 
817 // Tests that MATCHER() can be used in a namespace.
818 
819 namespace matcher_test {
820 MATCHER(IsOdd, "") { return (arg % 2) != 0; }
821 } // namespace matcher_test
822 
823 TEST(MatcherMacroTest, WorksInNamespace) {
824  Matcher<int> m = matcher_test::IsOdd();
825  EXPECT_FALSE(m.Matches(4));
826  EXPECT_TRUE(m.Matches(5));
827 }
828 
829 // Tests that Value() can be used to compose matchers.
830 MATCHER(IsPositiveOdd, "") {
831  return Value(arg, matcher_test::IsOdd()) && arg > 0;
832 }
833 
834 TEST(MatcherMacroTest, CanBeComposedUsingValue) {
835  EXPECT_THAT(3, IsPositiveOdd());
836  EXPECT_THAT(4, Not(IsPositiveOdd()));
837  EXPECT_THAT(-1, Not(IsPositiveOdd()));
838 }
839 
840 // Tests that a simple MATCHER_P() definition works.
841 
842 MATCHER_P(IsGreaterThan32And, n, "") { return arg > 32 && arg > n; }
843 
844 TEST(MatcherPMacroTest, Works) {
845  const Matcher<int> m = IsGreaterThan32And(5);
846  EXPECT_TRUE(m.Matches(36));
847  EXPECT_FALSE(m.Matches(5));
848 
849  EXPECT_EQ("is greater than 32 and 5", Describe(m));
850  EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m));
851  EXPECT_EQ("", Explain(m, 36));
852  EXPECT_EQ("", Explain(m, 5));
853 }
854 
855 // Tests that the description is calculated correctly from the matcher name.
856 MATCHER_P(_is_Greater_Than32and_, n, "") { return arg > 32 && arg > n; }
857 
858 TEST(MatcherPMacroTest, GeneratesCorrectDescription) {
859  const Matcher<int> m = _is_Greater_Than32and_(5);
860 
861  EXPECT_EQ("is greater than 32 and 5", Describe(m));
862  EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m));
863  EXPECT_EQ("", Explain(m, 36));
864  EXPECT_EQ("", Explain(m, 5));
865 }
866 
867 // Tests that a MATCHER_P matcher can be explicitly instantiated with
868 // a reference parameter type.
869 
870 class UncopyableFoo {
871  public:
872  explicit UncopyableFoo(char value) : value_(value) {}
873  private:
874  UncopyableFoo(const UncopyableFoo&);
875  void operator=(const UncopyableFoo&);
876 
877  char value_;
878 };
879 
880 MATCHER_P(ReferencesUncopyable, variable, "") { return &arg == &variable; }
881 
882 TEST(MatcherPMacroTest, WorksWhenExplicitlyInstantiatedWithReference) {
883  UncopyableFoo foo1('1'), foo2('2');
884  const Matcher<const UncopyableFoo&> m =
885  ReferencesUncopyable<const UncopyableFoo&>(foo1);
886 
887  EXPECT_TRUE(m.Matches(foo1));
888  EXPECT_FALSE(m.Matches(foo2));
889 
890  // We don't want the address of the parameter printed, as most
891  // likely it will just annoy the user. If the address is
892  // interesting, the user should consider passing the parameter by
893  // pointer instead.
894  EXPECT_EQ("references uncopyable 1-byte object <31>", Describe(m));
895 }
896 
897 
898 // Tests that the body of MATCHER_Pn() can reference the parameter
899 // types.
900 
901 MATCHER_P3(ParamTypesAreIntLongAndChar, foo, bar, baz, "") {
902  StaticAssertTypeEq<int, foo_type>();
903  StaticAssertTypeEq<long, bar_type>(); // NOLINT
904  StaticAssertTypeEq<char, baz_type>();
905  return arg == 0;
906 }
907 
908 TEST(MatcherPnMacroTest, CanReferenceParamTypes) {
909  EXPECT_THAT(0, ParamTypesAreIntLongAndChar(10, 20L, 'a'));
910 }
911 
912 // Tests that a MATCHER_Pn matcher can be explicitly instantiated with
913 // reference parameter types.
914 
915 MATCHER_P2(ReferencesAnyOf, variable1, variable2, "") {
916  return &arg == &variable1 || &arg == &variable2;
917 }
918 
919 TEST(MatcherPnMacroTest, WorksWhenExplicitlyInstantiatedWithReferences) {
920  UncopyableFoo foo1('1'), foo2('2'), foo3('3');
921  const Matcher<const UncopyableFoo&> m =
922  ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
923 
924  EXPECT_TRUE(m.Matches(foo1));
925  EXPECT_TRUE(m.Matches(foo2));
926  EXPECT_FALSE(m.Matches(foo3));
927 }
928 
929 TEST(MatcherPnMacroTest,
930  GeneratesCorretDescriptionWhenExplicitlyInstantiatedWithReferences) {
931  UncopyableFoo foo1('1'), foo2('2');
932  const Matcher<const UncopyableFoo&> m =
933  ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
934 
935  // We don't want the addresses of the parameters printed, as most
936  // likely they will just annoy the user. If the addresses are
937  // interesting, the user should consider passing the parameters by
938  // pointers instead.
939  EXPECT_EQ("references any of (1-byte object <31>, 1-byte object <32>)",
940  Describe(m));
941 }
942 
943 // Tests that a simple MATCHER_P2() definition works.
944 
945 MATCHER_P2(IsNotInClosedRange, low, hi, "") { return arg < low || arg > hi; }
946 
947 TEST(MatcherPnMacroTest, Works) {
948  const Matcher<const long&> m = IsNotInClosedRange(10, 20); // NOLINT
949  EXPECT_TRUE(m.Matches(36L));
950  EXPECT_FALSE(m.Matches(15L));
951 
952  EXPECT_EQ("is not in closed range (10, 20)", Describe(m));
953  EXPECT_EQ("not (is not in closed range (10, 20))", DescribeNegation(m));
954  EXPECT_EQ("", Explain(m, 36L));
955  EXPECT_EQ("", Explain(m, 15L));
956 }
957 
958 // Tests that MATCHER*() definitions can be overloaded on the number
959 // of parameters; also tests MATCHER_Pn() where n >= 3.
960 
961 MATCHER(EqualsSumOf, "") { return arg == 0; }
962 MATCHER_P(EqualsSumOf, a, "") { return arg == a; }
963 MATCHER_P2(EqualsSumOf, a, b, "") { return arg == a + b; }
964 MATCHER_P3(EqualsSumOf, a, b, c, "") { return arg == a + b + c; }
965 MATCHER_P4(EqualsSumOf, a, b, c, d, "") { return arg == a + b + c + d; }
966 MATCHER_P5(EqualsSumOf, a, b, c, d, e, "") { return arg == a + b + c + d + e; }
967 MATCHER_P6(EqualsSumOf, a, b, c, d, e, f, "") {
968  return arg == a + b + c + d + e + f;
969 }
970 MATCHER_P7(EqualsSumOf, a, b, c, d, e, f, g, "") {
971  return arg == a + b + c + d + e + f + g;
972 }
973 MATCHER_P8(EqualsSumOf, a, b, c, d, e, f, g, h, "") {
974  return arg == a + b + c + d + e + f + g + h;
975 }
976 MATCHER_P9(EqualsSumOf, a, b, c, d, e, f, g, h, i, "") {
977  return arg == a + b + c + d + e + f + g + h + i;
978 }
979 MATCHER_P10(EqualsSumOf, a, b, c, d, e, f, g, h, i, j, "") {
980  return arg == a + b + c + d + e + f + g + h + i + j;
981 }
982 
983 TEST(MatcherPnMacroTest, CanBeOverloadedOnNumberOfParameters) {
984  EXPECT_THAT(0, EqualsSumOf());
985  EXPECT_THAT(1, EqualsSumOf(1));
986  EXPECT_THAT(12, EqualsSumOf(10, 2));
987  EXPECT_THAT(123, EqualsSumOf(100, 20, 3));
988  EXPECT_THAT(1234, EqualsSumOf(1000, 200, 30, 4));
989  EXPECT_THAT(12345, EqualsSumOf(10000, 2000, 300, 40, 5));
990  EXPECT_THAT("abcdef",
991  EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f'));
992  EXPECT_THAT("abcdefg",
993  EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g'));
994  EXPECT_THAT("abcdefgh",
995  EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
996  "h"));
997  EXPECT_THAT("abcdefghi",
998  EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
999  "h", 'i'));
1000  EXPECT_THAT("abcdefghij",
1001  EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
1002  "h", 'i', ::std::string("j")));
1003 
1004  EXPECT_THAT(1, Not(EqualsSumOf()));
1005  EXPECT_THAT(-1, Not(EqualsSumOf(1)));
1006  EXPECT_THAT(-12, Not(EqualsSumOf(10, 2)));
1007  EXPECT_THAT(-123, Not(EqualsSumOf(100, 20, 3)));
1008  EXPECT_THAT(-1234, Not(EqualsSumOf(1000, 200, 30, 4)));
1009  EXPECT_THAT(-12345, Not(EqualsSumOf(10000, 2000, 300, 40, 5)));
1010  EXPECT_THAT("abcdef ",
1011  Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f')));
1012  EXPECT_THAT("abcdefg ",
1013  Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f',
1014  'g')));
1015  EXPECT_THAT("abcdefgh ",
1016  Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
1017  "h")));
1018  EXPECT_THAT("abcdefghi ",
1019  Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
1020  "h", 'i')));
1021  EXPECT_THAT("abcdefghij ",
1022  Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
1023  "h", 'i', ::std::string("j"))));
1024 }
1025 
1026 // Tests that a MATCHER_Pn() definition can be instantiated with any
1027 // compatible parameter types.
1028 TEST(MatcherPnMacroTest, WorksForDifferentParameterTypes) {
1029  EXPECT_THAT(123, EqualsSumOf(100L, 20, static_cast<char>(3)));
1030  EXPECT_THAT("abcd", EqualsSumOf(::std::string("a"), "b", 'c', "d"));
1031 
1032  EXPECT_THAT(124, Not(EqualsSumOf(100L, 20, static_cast<char>(3))));
1033  EXPECT_THAT("abcde", Not(EqualsSumOf(::std::string("a"), "b", 'c', "d")));
1034 }
1035 
1036 // Tests that the matcher body can promote the parameter types.
1037 
1038 MATCHER_P2(EqConcat, prefix, suffix, "") {
1039  // The following lines promote the two parameters to desired types.
1040  std::string prefix_str(prefix);
1041  char suffix_char = static_cast<char>(suffix);
1042  return arg == prefix_str + suffix_char;
1043 }
1044 
1045 TEST(MatcherPnMacroTest, SimpleTypePromotion) {
1046  Matcher<std::string> no_promo =
1047  EqConcat(std::string("foo"), 't');
1048  Matcher<const std::string&> promo =
1049  EqConcat("foo", static_cast<int>('t'));
1050  EXPECT_FALSE(no_promo.Matches("fool"));
1051  EXPECT_FALSE(promo.Matches("fool"));
1052  EXPECT_TRUE(no_promo.Matches("foot"));
1053  EXPECT_TRUE(promo.Matches("foot"));
1054 }
1055 
1056 // Verifies the type of a MATCHER*.
1057 
1058 TEST(MatcherPnMacroTest, TypesAreCorrect) {
1059  // EqualsSumOf() must be assignable to a EqualsSumOfMatcher variable.
1060  EqualsSumOfMatcher a0 = EqualsSumOf();
1061 
1062  // EqualsSumOf(1) must be assignable to a EqualsSumOfMatcherP variable.
1063  EqualsSumOfMatcherP<int> a1 = EqualsSumOf(1);
1064 
1065  // EqualsSumOf(p1, ..., pk) must be assignable to a EqualsSumOfMatcherPk
1066  // variable, and so on.
1067  EqualsSumOfMatcherP2<int, char> a2 = EqualsSumOf(1, '2');
1068  EqualsSumOfMatcherP3<int, int, char> a3 = EqualsSumOf(1, 2, '3');
1069  EqualsSumOfMatcherP4<int, int, int, char> a4 = EqualsSumOf(1, 2, 3, '4');
1070  EqualsSumOfMatcherP5<int, int, int, int, char> a5 =
1071  EqualsSumOf(1, 2, 3, 4, '5');
1072  EqualsSumOfMatcherP6<int, int, int, int, int, char> a6 =
1073  EqualsSumOf(1, 2, 3, 4, 5, '6');
1074  EqualsSumOfMatcherP7<int, int, int, int, int, int, char> a7 =
1075  EqualsSumOf(1, 2, 3, 4, 5, 6, '7');
1076  EqualsSumOfMatcherP8<int, int, int, int, int, int, int, char> a8 =
1077  EqualsSumOf(1, 2, 3, 4, 5, 6, 7, '8');
1078  EqualsSumOfMatcherP9<int, int, int, int, int, int, int, int, char> a9 =
1079  EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, '9');
1080  EqualsSumOfMatcherP10<int, int, int, int, int, int, int, int, int, char> a10 =
1081  EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, 9, '0');
1082 
1083  // Avoid "unused variable" warnings.
1084  (void)a0;
1085  (void)a1;
1086  (void)a2;
1087  (void)a3;
1088  (void)a4;
1089  (void)a5;
1090  (void)a6;
1091  (void)a7;
1092  (void)a8;
1093  (void)a9;
1094  (void)a10;
1095 }
1096 
1097 // Tests that matcher-typed parameters can be used in Value() inside a
1098 // MATCHER_Pn definition.
1099 
1100 // Succeeds if arg matches exactly 2 of the 3 matchers.
1101 MATCHER_P3(TwoOf, m1, m2, m3, "") {
1102  const int count = static_cast<int>(Value(arg, m1))
1103  + static_cast<int>(Value(arg, m2)) + static_cast<int>(Value(arg, m3));
1104  return count == 2;
1105 }
1106 
1107 TEST(MatcherPnMacroTest, CanUseMatcherTypedParameterInValue) {
1108  EXPECT_THAT(42, TwoOf(Gt(0), Lt(50), Eq(10)));
1109  EXPECT_THAT(0, Not(TwoOf(Gt(-1), Lt(1), Eq(0))));
1110 }
1111 
1112 // Tests Contains().
1113 
1114 TEST(ContainsTest, ListMatchesWhenElementIsInContainer) {
1115  list<int> some_list;
1116  some_list.push_back(3);
1117  some_list.push_back(1);
1118  some_list.push_back(2);
1119  EXPECT_THAT(some_list, Contains(1));
1120  EXPECT_THAT(some_list, Contains(Gt(2.5)));
1121  EXPECT_THAT(some_list, Contains(Eq(2.0f)));
1122 
1123  list<string> another_list;
1124  another_list.push_back("fee");
1125  another_list.push_back("fie");
1126  another_list.push_back("foe");
1127  another_list.push_back("fum");
1128  EXPECT_THAT(another_list, Contains(string("fee")));
1129 }
1130 
1131 TEST(ContainsTest, ListDoesNotMatchWhenElementIsNotInContainer) {
1132  list<int> some_list;
1133  some_list.push_back(3);
1134  some_list.push_back(1);
1135  EXPECT_THAT(some_list, Not(Contains(4)));
1136 }
1137 
1138 TEST(ContainsTest, SetMatchesWhenElementIsInContainer) {
1139  set<int> some_set;
1140  some_set.insert(3);
1141  some_set.insert(1);
1142  some_set.insert(2);
1143  EXPECT_THAT(some_set, Contains(Eq(1.0)));
1144  EXPECT_THAT(some_set, Contains(Eq(3.0f)));
1145  EXPECT_THAT(some_set, Contains(2));
1146 
1147  set<const char*> another_set;
1148  another_set.insert("fee");
1149  another_set.insert("fie");
1150  another_set.insert("foe");
1151  another_set.insert("fum");
1152  EXPECT_THAT(another_set, Contains(Eq(string("fum"))));
1153 }
1154 
1155 TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) {
1156  set<int> some_set;
1157  some_set.insert(3);
1158  some_set.insert(1);
1159  EXPECT_THAT(some_set, Not(Contains(4)));
1160 
1161  set<const char*> c_string_set;
1162  c_string_set.insert("hello");
1163  EXPECT_THAT(c_string_set, Not(Contains(string("hello").c_str())));
1164 }
1165 
1166 TEST(ContainsTest, ExplainsMatchResultCorrectly) {
1167  const int a[2] = { 1, 2 };
1168  Matcher<const int (&)[2]> m = Contains(2);
1169  EXPECT_EQ("whose element #1 matches", Explain(m, a));
1170 
1171  m = Contains(3);
1172  EXPECT_EQ("", Explain(m, a));
1173 
1174  m = Contains(GreaterThan(0));
1175  EXPECT_EQ("whose element #0 matches, which is 1 more than 0", Explain(m, a));
1176 
1177  m = Contains(GreaterThan(10));
1178  EXPECT_EQ("", Explain(m, a));
1179 }
1180 
1181 TEST(ContainsTest, DescribesItselfCorrectly) {
1182  Matcher<vector<int> > m = Contains(1);
1183  EXPECT_EQ("contains at least one element that is equal to 1", Describe(m));
1184 
1185  Matcher<vector<int> > m2 = Not(m);
1186  EXPECT_EQ("doesn't contain any element that is equal to 1", Describe(m2));
1187 }
1188 
1189 TEST(ContainsTest, MapMatchesWhenElementIsInContainer) {
1190  map<const char*, int> my_map;
1191  const char* bar = "a string";
1192  my_map[bar] = 2;
1193  EXPECT_THAT(my_map, Contains(pair<const char* const, int>(bar, 2)));
1194 
1195  map<string, int> another_map;
1196  another_map["fee"] = 1;
1197  another_map["fie"] = 2;
1198  another_map["foe"] = 3;
1199  another_map["fum"] = 4;
1200  EXPECT_THAT(another_map, Contains(pair<const string, int>(string("fee"), 1)));
1201  EXPECT_THAT(another_map, Contains(pair<const string, int>("fie", 2)));
1202 }
1203 
1204 TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) {
1205  map<int, int> some_map;
1206  some_map[1] = 11;
1207  some_map[2] = 22;
1208  EXPECT_THAT(some_map, Not(Contains(pair<const int, int>(2, 23))));
1209 }
1210 
1211 TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) {
1212  const char* string_array[] = { "fee", "fie", "foe", "fum" };
1213  EXPECT_THAT(string_array, Contains(Eq(string("fum"))));
1214 }
1215 
1216 TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) {
1217  int int_array[] = { 1, 2, 3, 4 };
1218  EXPECT_THAT(int_array, Not(Contains(5)));
1219 }
1220 
1221 TEST(ContainsTest, AcceptsMatcher) {
1222  const int a[] = { 1, 2, 3 };
1223  EXPECT_THAT(a, Contains(Gt(2)));
1224  EXPECT_THAT(a, Not(Contains(Gt(4))));
1225 }
1226 
1227 TEST(ContainsTest, WorksForNativeArrayAsTuple) {
1228  const int a[] = { 1, 2 };
1229  const int* const pointer = a;
1230  EXPECT_THAT(make_tuple(pointer, 2), Contains(1));
1231  EXPECT_THAT(make_tuple(pointer, 2), Not(Contains(Gt(3))));
1232 }
1233 
1234 TEST(ContainsTest, WorksForTwoDimensionalNativeArray) {
1235  int a[][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
1236  EXPECT_THAT(a, Contains(ElementsAre(4, 5, 6)));
1237  EXPECT_THAT(a, Contains(Contains(5)));
1238  EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5))));
1239  EXPECT_THAT(a, Contains(Not(Contains(5))));
1240 }
1241 
1242 TEST(AllOfTest, HugeMatcher) {
1243  // Verify that using AllOf with many arguments doesn't cause
1244  // the compiler to exceed template instantiation depth limit.
1245  EXPECT_THAT(0, testing::AllOf(_, _, _, _, _, _, _, _, _,
1246  testing::AllOf(_, _, _, _, _, _, _, _, _, _)));
1247 }
1248 
1249 TEST(AnyOfTest, HugeMatcher) {
1250  // Verify that using AnyOf with many arguments doesn't cause
1251  // the compiler to exceed template instantiation depth limit.
1252  EXPECT_THAT(0, testing::AnyOf(_, _, _, _, _, _, _, _, _,
1253  testing::AnyOf(_, _, _, _, _, _, _, _, _, _)));
1254 }
1255 
1256 namespace adl_test {
1257 
1258 // Verifies that the implementation of ::testing::AllOf and ::testing::AnyOf
1259 // don't issue unqualified recursive calls. If they do, the argument dependent
1260 // name lookup will cause AllOf/AnyOf in the 'adl_test' namespace to be found
1261 // as a candidate and the compilation will break due to an ambiguous overload.
1262 
1263 // The matcher must be in the same namespace as AllOf/AnyOf to make argument
1264 // dependent lookup find those.
1265 MATCHER(M, "") { return true; }
1266 
1267 template <typename T1, typename T2>
1268 bool AllOf(const T1& t1, const T2& t2) { return true; }
1269 
1270 TEST(AllOfTest, DoesNotCallAllOfUnqualified) {
1272  M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
1273 }
1274 
1275 template <typename T1, typename T2> bool
1276 AnyOf(const T1& t1, const T2& t2) { return true; }
1277 
1278 TEST(AnyOfTest, DoesNotCallAnyOfUnqualified) {
1280  M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
1281 }
1282 
1283 } // namespace adl_test
1284 
1285 #ifdef _MSC_VER
1286 # pragma warning(pop)
1287 #endif
1288 
1289 } // namespace
d
internal::GtMatcher< Rhs > Gt(Rhs x)
#define MATCHER(name, description)
<< DiffStrings(str, arg);
internal::NeMatcher< Rhs > Ne(Rhs x)
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
#define MATCHER_P7(name, p0, p1, p2, p3, p4, p5, p6, description)
int * count
f
internal::EqMatcher< T > Eq(T x)
::std::string PrintToString(const T &value)
#define EXPECT_THAT(value, matcher)
Matcher< int > GreaterThan(int n)
#define MATCHER_P4(name, p0, p1, p2, p3, description)
string Explain(const MatcherType &m, const Value &x)
internal::ArgsMatcher< InnerMatcher > Args(const InnerMatcher &matcher)
#define EXPECT_TRUE(condition)
string DescribeNegation(const Matcher< T > &m)
internal::AllOfResult2< M1, M2 >::type AllOf(M1 m1, M2 m2)
internal::RefMatcher< T & > Ref(T &x)
PolymorphicMatcher< internal::StrEqualityMatcher< internal::string > > StrEq(const internal::string &str)
internal::LtMatcher< Rhs > Lt(Rhs x)
#define MOCK_METHOD2(m,...)
internal::LeMatcher< Rhs > Le(Rhs x)
internal::ElementsAreMatcher< std::tr1::tuple<> > ElementsAre()
internal::ElementsAreArrayMatcher< typename::std::iterator_traits< Iter >::value_type > ElementsAreArray(Iter first, Iter last)
std::string Describe(const T &object, const Tabs &tabs)
#define EXPECT_FALSE(condition)
bool Value(const T &value, M matcher)
internal::NotMatcher< InnerMatcher > Not(InnerMatcher m)
internal::AnyOfResult2< M1, M2 >::type AnyOf(M1 m1, M2 m2)
Matcher< T > MakeMatcher(const MatcherInterface< T > *impl)
#define MATCHER_P2(name, p0, p1, description)
internal::PointeeMatcher< InnerMatcher > Pointee(const InnerMatcher &inner_matcher)
#define TEST(test_case_name, test_name)
#define MATCHER_P3(name, p0, p1, p2, description)
#define MATCHER_P10(name, p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, description)
#define MATCHER_P5(name, p0, p1, p2, p3, p4, description)
#define MATCHER_P6(name, p0, p1, p2, p3, p4, p5, description)
#define MATCHER_P8(name, p0, p1, p2, p3, p4, p5, p6, p7, description)
internal::NamedArg< char > arg(StringRef name, const T &arg)
Definition: format.h:3918
#define MATCHER_P(name, p0, description)
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
internal::GeMatcher< Rhs > Ge(Rhs x)
#define MATCHER_P9(name, p0, p1, p2, p3, p4, p5, p6, p7, p8, description)
internal::ContainsMatcher< M > Contains(M matcher)
#define GMOCK_ARRAY_SIZE_(a)
#define EXPECT_EQ(expected, actual)


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:06:05