?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

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