gmock-generated-matchers_test.cc
Go to the documentation of this file.
00001 // Copyright 2008, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 
00030 // Google Mock - a framework for writing C++ mock classes.
00031 //
00032 // This file tests the built-in matchers generated by a script.
00033 
00034 #include "gmock/gmock-generated-matchers.h"
00035 
00036 #include <list>
00037 #include <map>
00038 #include <set>
00039 #include <sstream>
00040 #include <string>
00041 #include <utility>
00042 #include <vector>
00043 
00044 #include "gmock/gmock.h"
00045 #include "gtest/gtest.h"
00046 #include "gtest/gtest-spi.h"
00047 
00048 namespace {
00049 
00050 using std::list;
00051 using std::map;
00052 using std::pair;
00053 using std::set;
00054 using std::stringstream;
00055 using std::vector;
00056 using std::tr1::get;
00057 using std::tr1::make_tuple;
00058 using std::tr1::tuple;
00059 using testing::_;
00060 using testing::Args;
00061 using testing::Contains;
00062 using testing::ElementsAre;
00063 using testing::ElementsAreArray;
00064 using testing::Eq;
00065 using testing::Ge;
00066 using testing::Gt;
00067 using testing::Le;
00068 using testing::Lt;
00069 using testing::MakeMatcher;
00070 using testing::Matcher;
00071 using testing::MatcherInterface;
00072 using testing::MatchResultListener;
00073 using testing::Ne;
00074 using testing::Not;
00075 using testing::Pointee;
00076 using testing::PrintToString;
00077 using testing::Ref;
00078 using testing::StaticAssertTypeEq;
00079 using testing::StrEq;
00080 using testing::Value;
00081 using testing::internal::ElementsAreArrayMatcher;
00082 using testing::internal::string;
00083 
00084 // Evaluates to the number of elements in 'array'.
00085 #define GMOCK_ARRAY_SIZE_(a) (sizeof(a) / sizeof(a[0]))
00086 
00087 // Returns the description of the given matcher.
00088 template <typename T>
00089 string Describe(const Matcher<T>& m) {
00090   stringstream ss;
00091   m.DescribeTo(&ss);
00092   return ss.str();
00093 }
00094 
00095 // Returns the description of the negation of the given matcher.
00096 template <typename T>
00097 string DescribeNegation(const Matcher<T>& m) {
00098   stringstream ss;
00099   m.DescribeNegationTo(&ss);
00100   return ss.str();
00101 }
00102 
00103 // Returns the reason why x matches, or doesn't match, m.
00104 template <typename MatcherType, typename Value>
00105 string Explain(const MatcherType& m, const Value& x) {
00106   stringstream ss;
00107   m.ExplainMatchResultTo(x, &ss);
00108   return ss.str();
00109 }
00110 
00111 // Tests Args<k0, ..., kn>(m).
00112 
00113 TEST(ArgsTest, AcceptsZeroTemplateArg) {
00114   const tuple<int, bool> t(5, true);
00115   EXPECT_THAT(t, Args<>(Eq(tuple<>())));
00116   EXPECT_THAT(t, Not(Args<>(Ne(tuple<>()))));
00117 }
00118 
00119 TEST(ArgsTest, AcceptsOneTemplateArg) {
00120   const tuple<int, bool> t(5, true);
00121   EXPECT_THAT(t, Args<0>(Eq(make_tuple(5))));
00122   EXPECT_THAT(t, Args<1>(Eq(make_tuple(true))));
00123   EXPECT_THAT(t, Not(Args<1>(Eq(make_tuple(false)))));
00124 }
00125 
00126 TEST(ArgsTest, AcceptsTwoTemplateArgs) {
00127   const tuple<short, int, long> t(4, 5, 6L);  // NOLINT
00128 
00129   EXPECT_THAT(t, (Args<0, 1>(Lt())));
00130   EXPECT_THAT(t, (Args<1, 2>(Lt())));
00131   EXPECT_THAT(t, Not(Args<0, 2>(Gt())));
00132 }
00133 
00134 TEST(ArgsTest, AcceptsRepeatedTemplateArgs) {
00135   const tuple<short, int, long> t(4, 5, 6L);  // NOLINT
00136   EXPECT_THAT(t, (Args<0, 0>(Eq())));
00137   EXPECT_THAT(t, Not(Args<1, 1>(Ne())));
00138 }
00139 
00140 TEST(ArgsTest, AcceptsDecreasingTemplateArgs) {
00141   const tuple<short, int, long> t(4, 5, 6L);  // NOLINT
00142   EXPECT_THAT(t, (Args<2, 0>(Gt())));
00143   EXPECT_THAT(t, Not(Args<2, 1>(Lt())));
00144 }
00145 
00146 // The MATCHER*() macros trigger warning C4100 (unreferenced formal
00147 // parameter) in MSVC with -W4.  Unfortunately they cannot be fixed in
00148 // the macro definition, as the warnings are generated when the macro
00149 // is expanded and macro expansion cannot contain #pragma.  Therefore
00150 // we suppress them here.
00151 #ifdef _MSC_VER
00152 # pragma warning(push)
00153 # pragma warning(disable:4100)
00154 #endif
00155 
00156 MATCHER(SumIsZero, "") {
00157   return get<0>(arg) + get<1>(arg) + get<2>(arg) == 0;
00158 }
00159 
00160 TEST(ArgsTest, AcceptsMoreTemplateArgsThanArityOfOriginalTuple) {
00161   EXPECT_THAT(make_tuple(-1, 2), (Args<0, 0, 1>(SumIsZero())));
00162   EXPECT_THAT(make_tuple(1, 2), Not(Args<0, 0, 1>(SumIsZero())));
00163 }
00164 
00165 TEST(ArgsTest, CanBeNested) {
00166   const tuple<short, int, long, int> t(4, 5, 6L, 6);  // NOLINT
00167   EXPECT_THAT(t, (Args<1, 2, 3>(Args<1, 2>(Eq()))));
00168   EXPECT_THAT(t, (Args<0, 1, 3>(Args<0, 2>(Lt()))));
00169 }
00170 
00171 TEST(ArgsTest, CanMatchTupleByValue) {
00172   typedef tuple<char, int, int> Tuple3;
00173   const Matcher<Tuple3> m = Args<1, 2>(Lt());
00174   EXPECT_TRUE(m.Matches(Tuple3('a', 1, 2)));
00175   EXPECT_FALSE(m.Matches(Tuple3('b', 2, 2)));
00176 }
00177 
00178 TEST(ArgsTest, CanMatchTupleByReference) {
00179   typedef tuple<char, char, int> Tuple3;
00180   const Matcher<const Tuple3&> m = Args<0, 1>(Lt());
00181   EXPECT_TRUE(m.Matches(Tuple3('a', 'b', 2)));
00182   EXPECT_FALSE(m.Matches(Tuple3('b', 'b', 2)));
00183 }
00184 
00185 // Validates that arg is printed as str.
00186 MATCHER_P(PrintsAs, str, "") {
00187   return testing::PrintToString(arg) == str;
00188 }
00189 
00190 TEST(ArgsTest, AcceptsTenTemplateArgs) {
00191   EXPECT_THAT(make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
00192               (Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
00193                   PrintsAs("(9, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
00194   EXPECT_THAT(make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
00195               Not(Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
00196                       PrintsAs("(0, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
00197 }
00198 
00199 TEST(ArgsTest, DescirbesSelfCorrectly) {
00200   const Matcher<tuple<int, bool, char> > m = Args<2, 0>(Lt());
00201   EXPECT_EQ("are a tuple whose fields (#2, #0) are a pair where "
00202             "the first < the second",
00203             Describe(m));
00204 }
00205 
00206 TEST(ArgsTest, DescirbesNestedArgsCorrectly) {
00207   const Matcher<const tuple<int, bool, char, int>&> m =
00208       Args<0, 2, 3>(Args<2, 0>(Lt()));
00209   EXPECT_EQ("are a tuple whose fields (#0, #2, #3) are a tuple "
00210             "whose fields (#2, #0) are a pair where the first < the second",
00211             Describe(m));
00212 }
00213 
00214 TEST(ArgsTest, DescribesNegationCorrectly) {
00215   const Matcher<tuple<int, char> > m = Args<1, 0>(Gt());
00216   EXPECT_EQ("are a tuple whose fields (#1, #0) aren't a pair "
00217             "where the first > the second",
00218             DescribeNegation(m));
00219 }
00220 
00221 TEST(ArgsTest, ExplainsMatchResultWithoutInnerExplanation) {
00222   const Matcher<tuple<bool, int, int> > m = Args<1, 2>(Eq());
00223   EXPECT_EQ("whose fields (#1, #2) are (42, 42)",
00224             Explain(m, make_tuple(false, 42, 42)));
00225   EXPECT_EQ("whose fields (#1, #2) are (42, 43)",
00226             Explain(m, make_tuple(false, 42, 43)));
00227 }
00228 
00229 // For testing Args<>'s explanation.
00230 class LessThanMatcher : public MatcherInterface<tuple<char, int> > {
00231  public:
00232   virtual void DescribeTo(::std::ostream* os) const {}
00233 
00234   virtual bool MatchAndExplain(tuple<char, int> value,
00235                                MatchResultListener* listener) const {
00236     const int diff = get<0>(value) - get<1>(value);
00237     if (diff > 0) {
00238       *listener << "where the first value is " << diff
00239                 << " more than the second";
00240     }
00241     return diff < 0;
00242   }
00243 };
00244 
00245 Matcher<tuple<char, int> > LessThan() {
00246   return MakeMatcher(new LessThanMatcher);
00247 }
00248 
00249 TEST(ArgsTest, ExplainsMatchResultWithInnerExplanation) {
00250   const Matcher<tuple<char, int, int> > m = Args<0, 2>(LessThan());
00251   EXPECT_EQ("whose fields (#0, #2) are ('a' (97, 0x61), 42), "
00252             "where the first value is 55 more than the second",
00253             Explain(m, make_tuple('a', 42, 42)));
00254   EXPECT_EQ("whose fields (#0, #2) are ('\\0', 43)",
00255             Explain(m, make_tuple('\0', 42, 43)));
00256 }
00257 
00258 // For testing ExplainMatchResultTo().
00259 class GreaterThanMatcher : public MatcherInterface<int> {
00260  public:
00261   explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
00262 
00263   virtual void DescribeTo(::std::ostream* os) const {
00264     *os << "is greater than " << rhs_;
00265   }
00266 
00267   virtual bool MatchAndExplain(int lhs,
00268                                MatchResultListener* listener) const {
00269     const int diff = lhs - rhs_;
00270     if (diff > 0) {
00271       *listener << "which is " << diff << " more than " << rhs_;
00272     } else if (diff == 0) {
00273       *listener << "which is the same as " << rhs_;
00274     } else {
00275       *listener << "which is " << -diff << " less than " << rhs_;
00276     }
00277 
00278     return lhs > rhs_;
00279   }
00280 
00281  private:
00282   int rhs_;
00283 };
00284 
00285 Matcher<int> GreaterThan(int n) {
00286   return MakeMatcher(new GreaterThanMatcher(n));
00287 }
00288 
00289 // Tests for ElementsAre().
00290 
00291 TEST(ElementsAreTest, CanDescribeExpectingNoElement) {
00292   Matcher<const vector<int>&> m = ElementsAre();
00293   EXPECT_EQ("is empty", Describe(m));
00294 }
00295 
00296 TEST(ElementsAreTest, CanDescribeExpectingOneElement) {
00297   Matcher<vector<int> > m = ElementsAre(Gt(5));
00298   EXPECT_EQ("has 1 element that is > 5", Describe(m));
00299 }
00300 
00301 TEST(ElementsAreTest, CanDescribeExpectingManyElements) {
00302   Matcher<list<string> > m = ElementsAre(StrEq("one"), "two");
00303   EXPECT_EQ("has 2 elements where\n"
00304             "element #0 is equal to \"one\",\n"
00305             "element #1 is equal to \"two\"", Describe(m));
00306 }
00307 
00308 TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) {
00309   Matcher<vector<int> > m = ElementsAre();
00310   EXPECT_EQ("isn't empty", DescribeNegation(m));
00311 }
00312 
00313 TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElment) {
00314   Matcher<const list<int>& > m = ElementsAre(Gt(5));
00315   EXPECT_EQ("doesn't have 1 element, or\n"
00316             "element #0 isn't > 5", DescribeNegation(m));
00317 }
00318 
00319 TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) {
00320   Matcher<const list<string>& > m = ElementsAre("one", "two");
00321   EXPECT_EQ("doesn't have 2 elements, or\n"
00322             "element #0 isn't equal to \"one\", or\n"
00323             "element #1 isn't equal to \"two\"", DescribeNegation(m));
00324 }
00325 
00326 TEST(ElementsAreTest, DoesNotExplainTrivialMatch) {
00327   Matcher<const list<int>& > m = ElementsAre(1, Ne(2));
00328 
00329   list<int> test_list;
00330   test_list.push_back(1);
00331   test_list.push_back(3);
00332   EXPECT_EQ("", Explain(m, test_list));  // No need to explain anything.
00333 }
00334 
00335 TEST(ElementsAreTest, ExplainsNonTrivialMatch) {
00336   Matcher<const vector<int>& > m =
00337       ElementsAre(GreaterThan(1), 0, GreaterThan(2));
00338 
00339   const int a[] = { 10, 0, 100 };
00340   vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
00341   EXPECT_EQ("whose element #0 matches, which is 9 more than 1,\n"
00342             "and whose element #2 matches, which is 98 more than 2",
00343             Explain(m, test_vector));
00344 }
00345 
00346 TEST(ElementsAreTest, CanExplainMismatchWrongSize) {
00347   Matcher<const list<int>& > m = ElementsAre(1, 3);
00348 
00349   list<int> test_list;
00350   // No need to explain when the container is empty.
00351   EXPECT_EQ("", Explain(m, test_list));
00352 
00353   test_list.push_back(1);
00354   EXPECT_EQ("which has 1 element", Explain(m, test_list));
00355 }
00356 
00357 TEST(ElementsAreTest, CanExplainMismatchRightSize) {
00358   Matcher<const vector<int>& > m = ElementsAre(1, GreaterThan(5));
00359 
00360   vector<int> v;
00361   v.push_back(2);
00362   v.push_back(1);
00363   EXPECT_EQ("whose element #0 doesn't match", Explain(m, v));
00364 
00365   v[0] = 1;
00366   EXPECT_EQ("whose element #1 doesn't match, which is 4 less than 5",
00367             Explain(m, v));
00368 }
00369 
00370 TEST(ElementsAreTest, MatchesOneElementVector) {
00371   vector<string> test_vector;
00372   test_vector.push_back("test string");
00373 
00374   EXPECT_THAT(test_vector, ElementsAre(StrEq("test string")));
00375 }
00376 
00377 TEST(ElementsAreTest, MatchesOneElementList) {
00378   list<string> test_list;
00379   test_list.push_back("test string");
00380 
00381   EXPECT_THAT(test_list, ElementsAre("test string"));
00382 }
00383 
00384 TEST(ElementsAreTest, MatchesThreeElementVector) {
00385   vector<string> test_vector;
00386   test_vector.push_back("one");
00387   test_vector.push_back("two");
00388   test_vector.push_back("three");
00389 
00390   EXPECT_THAT(test_vector, ElementsAre("one", StrEq("two"), _));
00391 }
00392 
00393 TEST(ElementsAreTest, MatchesOneElementEqMatcher) {
00394   vector<int> test_vector;
00395   test_vector.push_back(4);
00396 
00397   EXPECT_THAT(test_vector, ElementsAre(Eq(4)));
00398 }
00399 
00400 TEST(ElementsAreTest, MatchesOneElementAnyMatcher) {
00401   vector<int> test_vector;
00402   test_vector.push_back(4);
00403 
00404   EXPECT_THAT(test_vector, ElementsAre(_));
00405 }
00406 
00407 TEST(ElementsAreTest, MatchesOneElementValue) {
00408   vector<int> test_vector;
00409   test_vector.push_back(4);
00410 
00411   EXPECT_THAT(test_vector, ElementsAre(4));
00412 }
00413 
00414 TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) {
00415   vector<int> test_vector;
00416   test_vector.push_back(1);
00417   test_vector.push_back(2);
00418   test_vector.push_back(3);
00419 
00420   EXPECT_THAT(test_vector, ElementsAre(1, Eq(2), _));
00421 }
00422 
00423 TEST(ElementsAreTest, MatchesTenElementVector) {
00424   const int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
00425   vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
00426 
00427   EXPECT_THAT(test_vector,
00428               // The element list can contain values and/or matchers
00429               // of different types.
00430               ElementsAre(0, Ge(0), _, 3, 4, Ne(2), Eq(6), 7, 8, _));
00431 }
00432 
00433 TEST(ElementsAreTest, DoesNotMatchWrongSize) {
00434   vector<string> test_vector;
00435   test_vector.push_back("test string");
00436   test_vector.push_back("test string");
00437 
00438   Matcher<vector<string> > m = ElementsAre(StrEq("test string"));
00439   EXPECT_FALSE(m.Matches(test_vector));
00440 }
00441 
00442 TEST(ElementsAreTest, DoesNotMatchWrongValue) {
00443   vector<string> test_vector;
00444   test_vector.push_back("other string");
00445 
00446   Matcher<vector<string> > m = ElementsAre(StrEq("test string"));
00447   EXPECT_FALSE(m.Matches(test_vector));
00448 }
00449 
00450 TEST(ElementsAreTest, DoesNotMatchWrongOrder) {
00451   vector<string> test_vector;
00452   test_vector.push_back("one");
00453   test_vector.push_back("three");
00454   test_vector.push_back("two");
00455 
00456   Matcher<vector<string> > m = ElementsAre(
00457     StrEq("one"), StrEq("two"), StrEq("three"));
00458   EXPECT_FALSE(m.Matches(test_vector));
00459 }
00460 
00461 TEST(ElementsAreTest, WorksForNestedContainer) {
00462   const char* strings[] = {
00463     "Hi",
00464     "world"
00465   };
00466 
00467   vector<list<char> > nested;
00468   for (size_t i = 0; i < GMOCK_ARRAY_SIZE_(strings); i++) {
00469     nested.push_back(list<char>(strings[i], strings[i] + strlen(strings[i])));
00470   }
00471 
00472   EXPECT_THAT(nested, ElementsAre(ElementsAre('H', Ne('e')),
00473                                   ElementsAre('w', 'o', _, _, 'd')));
00474   EXPECT_THAT(nested, Not(ElementsAre(ElementsAre('H', 'e'),
00475                                       ElementsAre('w', 'o', _, _, 'd'))));
00476 }
00477 
00478 TEST(ElementsAreTest, WorksWithByRefElementMatchers) {
00479   int a[] = { 0, 1, 2 };
00480   vector<int> v(a, a + GMOCK_ARRAY_SIZE_(a));
00481 
00482   EXPECT_THAT(v, ElementsAre(Ref(v[0]), Ref(v[1]), Ref(v[2])));
00483   EXPECT_THAT(v, Not(ElementsAre(Ref(v[0]), Ref(v[1]), Ref(a[2]))));
00484 }
00485 
00486 TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) {
00487   int a[] = { 0, 1, 2 };
00488   vector<int> v(a, a + GMOCK_ARRAY_SIZE_(a));
00489 
00490   EXPECT_THAT(&v, Pointee(ElementsAre(0, 1, _)));
00491   EXPECT_THAT(&v, Not(Pointee(ElementsAre(0, _, 3))));
00492 }
00493 
00494 TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) {
00495   int array[] = { 0, 1, 2 };
00496   EXPECT_THAT(array, ElementsAre(0, 1, _));
00497   EXPECT_THAT(array, Not(ElementsAre(1, _, _)));
00498   EXPECT_THAT(array, Not(ElementsAre(0, _)));
00499 }
00500 
00501 class NativeArrayPassedAsPointerAndSize {
00502  public:
00503   NativeArrayPassedAsPointerAndSize() {}
00504 
00505   MOCK_METHOD2(Helper, void(int* array, int size));
00506 
00507  private:
00508   GTEST_DISALLOW_COPY_AND_ASSIGN_(NativeArrayPassedAsPointerAndSize);
00509 };
00510 
00511 TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) {
00512   int array[] = { 0, 1 };
00513   ::std::tr1::tuple<int*, size_t> array_as_tuple(array, 2);
00514   EXPECT_THAT(array_as_tuple, ElementsAre(0, 1));
00515   EXPECT_THAT(array_as_tuple, Not(ElementsAre(0)));
00516 
00517   NativeArrayPassedAsPointerAndSize helper;
00518   EXPECT_CALL(helper, Helper(_, _))
00519       .With(ElementsAre(0, 1));
00520   helper.Helper(array, 2);
00521 }
00522 
00523 TEST(ElementsAreTest, WorksWithTwoDimensionalNativeArray) {
00524   const char a2[][3] = { "hi", "lo" };
00525   EXPECT_THAT(a2, ElementsAre(ElementsAre('h', 'i', '\0'),
00526                               ElementsAre('l', 'o', '\0')));
00527   EXPECT_THAT(a2, ElementsAre(StrEq("hi"), StrEq("lo")));
00528   EXPECT_THAT(a2, ElementsAre(Not(ElementsAre('h', 'o', '\0')),
00529                               ElementsAre('l', 'o', '\0')));
00530 }
00531 
00532 TEST(ElementsAreTest, AcceptsStringLiteral) {
00533   string array[] = { "hi", "one", "two" };
00534   EXPECT_THAT(array, ElementsAre("hi", "one", "two"));
00535   EXPECT_THAT(array, Not(ElementsAre("hi", "one", "too")));
00536 }
00537 
00538 #ifndef _MSC_VER
00539 
00540 // The following test passes a value of type const char[] to a
00541 // function template that expects const T&.  Some versions of MSVC
00542 // generates a compiler error C2665 for that.  We believe it's a bug
00543 // in MSVC.  Therefore this test is #if-ed out for MSVC.
00544 
00545 // Declared here with the size unknown.  Defined AFTER the following test.
00546 extern const char kHi[];
00547 
00548 TEST(ElementsAreTest, AcceptsArrayWithUnknownSize) {
00549   // The size of kHi is not known in this test, but ElementsAre() should
00550   // still accept it.
00551 
00552   string array1[] = { "hi" };
00553   EXPECT_THAT(array1, ElementsAre(kHi));
00554 
00555   string array2[] = { "ho" };
00556   EXPECT_THAT(array2, Not(ElementsAre(kHi)));
00557 }
00558 
00559 const char kHi[] = "hi";
00560 
00561 #endif  // _MSC_VER
00562 
00563 TEST(ElementsAreTest, MakesCopyOfArguments) {
00564   int x = 1;
00565   int y = 2;
00566   // This should make a copy of x and y.
00567   ::testing::internal::ElementsAreMatcher<std::tr1::tuple<int, int> >
00568           polymorphic_matcher = ElementsAre(x, y);
00569   // Changing x and y now shouldn't affect the meaning of the above matcher.
00570   x = y = 0;
00571   const int array1[] = { 1, 2 };
00572   EXPECT_THAT(array1, polymorphic_matcher);
00573   const int array2[] = { 0, 0 };
00574   EXPECT_THAT(array2, Not(polymorphic_matcher));
00575 }
00576 
00577 
00578 // Tests for ElementsAreArray().  Since ElementsAreArray() shares most
00579 // of the implementation with ElementsAre(), we don't test it as
00580 // thoroughly here.
00581 
00582 TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) {
00583   const int a[] = { 1, 2, 3 };
00584 
00585   vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
00586   EXPECT_THAT(test_vector, ElementsAreArray(a));
00587 
00588   test_vector[2] = 0;
00589   EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
00590 }
00591 
00592 TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) {
00593   const char* a[] = { "one", "two", "three" };
00594 
00595   vector<string> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
00596   EXPECT_THAT(test_vector, ElementsAreArray(a, GMOCK_ARRAY_SIZE_(a)));
00597 
00598   const char** p = a;
00599   test_vector[0] = "1";
00600   EXPECT_THAT(test_vector, Not(ElementsAreArray(p, GMOCK_ARRAY_SIZE_(a))));
00601 }
00602 
00603 TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) {
00604   const char* a[] = { "one", "two", "three" };
00605 
00606   vector<string> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
00607   EXPECT_THAT(test_vector, ElementsAreArray(a));
00608 
00609   test_vector[0] = "1";
00610   EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
00611 }
00612 
00613 TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) {
00614   const Matcher<string> kMatcherArray[] =
00615     { StrEq("one"), StrEq("two"), StrEq("three") };
00616 
00617   vector<string> test_vector;
00618   test_vector.push_back("one");
00619   test_vector.push_back("two");
00620   test_vector.push_back("three");
00621   EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray));
00622 
00623   test_vector.push_back("three");
00624   EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray)));
00625 }
00626 
00627 TEST(ElementsAreArrayTest, CanBeCreatedWithVector) {
00628   const int a[] = { 1, 2, 3 };
00629   vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
00630   const vector<int> expected(a, a + GMOCK_ARRAY_SIZE_(a));
00631   EXPECT_THAT(test_vector, ElementsAreArray(expected));
00632   test_vector.push_back(4);
00633   EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));
00634 }
00635 
00636 #if GTEST_LANG_CXX11
00637 
00638 TEST(ElementsAreArrayTest, TakesInitializerList) {
00639   const int a[5] = { 1, 2, 3, 4, 5 };
00640   EXPECT_THAT(a, ElementsAreArray({ 1, 2, 3, 4, 5 }));
00641   EXPECT_THAT(a, Not(ElementsAreArray({ 1, 2, 3, 5, 4 })));
00642   EXPECT_THAT(a, Not(ElementsAreArray({ 1, 2, 3, 4, 6 })));
00643 }
00644 
00645 TEST(ElementsAreArrayTest, TakesInitializerListOfCStrings) {
00646   const string a[5] = { "a", "b", "c", "d", "e" };
00647   EXPECT_THAT(a, ElementsAreArray({ "a", "b", "c", "d", "e" }));
00648   EXPECT_THAT(a, Not(ElementsAreArray({ "a", "b", "c", "e", "d" })));
00649   EXPECT_THAT(a, Not(ElementsAreArray({ "a", "b", "c", "d", "ef" })));
00650 }
00651 
00652 TEST(ElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {
00653   const int a[5] = { 1, 2, 3, 4, 5 };
00654   EXPECT_THAT(a, ElementsAreArray(
00655       { Eq(1), Eq(2), Eq(3), Eq(4), Eq(5) }));
00656   EXPECT_THAT(a, Not(ElementsAreArray(
00657       { Eq(1), Eq(2), Eq(3), Eq(4), Eq(6) })));
00658 }
00659 
00660 TEST(ElementsAreArrayTest,
00661      TakesInitializerListOfDifferentTypedMatchers) {
00662   const int a[5] = { 1, 2, 3, 4, 5 };
00663   // The compiler cannot infer the type of the initializer list if its
00664   // elements have different types.  We must explicitly specify the
00665   // unified element type in this case.
00666   EXPECT_THAT(a, ElementsAreArray<Matcher<int> >(
00667       { Eq(1), Ne(-2), Ge(3), Le(4), Eq(5) }));
00668   EXPECT_THAT(a, Not(ElementsAreArray<Matcher<int> >(
00669       { Eq(1), Ne(-2), Ge(3), Le(4), Eq(6) })));
00670 }
00671 
00672 #endif  // GTEST_LANG_CXX11
00673 
00674 TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherVector) {
00675   const int a[] = { 1, 2, 3 };
00676   const Matcher<int> kMatchers[] = { Eq(1), Eq(2), Eq(3) };
00677   vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
00678   const vector<Matcher<int> > expected(
00679       kMatchers, kMatchers + GMOCK_ARRAY_SIZE_(kMatchers));
00680   EXPECT_THAT(test_vector, ElementsAreArray(expected));
00681   test_vector.push_back(4);
00682   EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));
00683 }
00684 
00685 TEST(ElementsAreArrayTest, CanBeCreatedWithIteratorRange) {
00686   const int a[] = { 1, 2, 3 };
00687   const vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
00688   const vector<int> expected(a, a + GMOCK_ARRAY_SIZE_(a));
00689   EXPECT_THAT(test_vector, ElementsAreArray(expected.begin(), expected.end()));
00690   // Pointers are iterators, too.
00691   EXPECT_THAT(test_vector, ElementsAreArray(a, a + GMOCK_ARRAY_SIZE_(a)));
00692   // The empty range of NULL pointers should also be okay.
00693   int* const null_int = NULL;
00694   EXPECT_THAT(test_vector, Not(ElementsAreArray(null_int, null_int)));
00695   EXPECT_THAT((vector<int>()), ElementsAreArray(null_int, null_int));
00696 }
00697 
00698 // Since ElementsAre() and ElementsAreArray() share much of the
00699 // implementation, we only do a sanity test for native arrays here.
00700 TEST(ElementsAreArrayTest, WorksWithNativeArray) {
00701   ::std::string a[] = { "hi", "ho" };
00702   ::std::string b[] = { "hi", "ho" };
00703 
00704   EXPECT_THAT(a, ElementsAreArray(b));
00705   EXPECT_THAT(a, ElementsAreArray(b, 2));
00706   EXPECT_THAT(a, Not(ElementsAreArray(b, 1)));
00707 }
00708 
00709 TEST(ElementsAreArrayTest, SourceLifeSpan) {
00710   const int a[] = { 1, 2, 3 };
00711   vector<int> test_vector(a, a + GMOCK_ARRAY_SIZE_(a));
00712   vector<int> expect(a, a + GMOCK_ARRAY_SIZE_(a));
00713   ElementsAreArrayMatcher<int> matcher_maker =
00714       ElementsAreArray(expect.begin(), expect.end());
00715   EXPECT_THAT(test_vector, matcher_maker);
00716   // Changing in place the values that initialized matcher_maker should not
00717   // affect matcher_maker anymore. It should have made its own copy of them.
00718   typedef vector<int>::iterator Iter;
00719   for (Iter it = expect.begin(); it != expect.end(); ++it) { *it += 10; }
00720   EXPECT_THAT(test_vector, matcher_maker);
00721   test_vector.push_back(3);
00722   EXPECT_THAT(test_vector, Not(matcher_maker));
00723 }
00724 
00725 // Tests for the MATCHER*() macro family.
00726 
00727 // Tests that a simple MATCHER() definition works.
00728 
00729 MATCHER(IsEven, "") { return (arg % 2) == 0; }
00730 
00731 TEST(MatcherMacroTest, Works) {
00732   const Matcher<int> m = IsEven();
00733   EXPECT_TRUE(m.Matches(6));
00734   EXPECT_FALSE(m.Matches(7));
00735 
00736   EXPECT_EQ("is even", Describe(m));
00737   EXPECT_EQ("not (is even)", DescribeNegation(m));
00738   EXPECT_EQ("", Explain(m, 6));
00739   EXPECT_EQ("", Explain(m, 7));
00740 }
00741 
00742 // This also tests that the description string can reference 'negation'.
00743 MATCHER(IsEven2, negation ? "is odd" : "is even") {
00744   if ((arg % 2) == 0) {
00745     // Verifies that we can stream to result_listener, a listener
00746     // supplied by the MATCHER macro implicitly.
00747     *result_listener << "OK";
00748     return true;
00749   } else {
00750     *result_listener << "% 2 == " << (arg % 2);
00751     return false;
00752   }
00753 }
00754 
00755 // This also tests that the description string can reference matcher
00756 // parameters.
00757 MATCHER_P2(EqSumOf, x, y,
00758            string(negation ? "doesn't equal" : "equals") + " the sum of " +
00759            PrintToString(x) + " and " + PrintToString(y)) {
00760   if (arg == (x + y)) {
00761     *result_listener << "OK";
00762     return true;
00763   } else {
00764     // Verifies that we can stream to the underlying stream of
00765     // result_listener.
00766     if (result_listener->stream() != NULL) {
00767       *result_listener->stream() << "diff == " << (x + y - arg);
00768     }
00769     return false;
00770   }
00771 }
00772 
00773 // Tests that the matcher description can reference 'negation' and the
00774 // matcher parameters.
00775 TEST(MatcherMacroTest, DescriptionCanReferenceNegationAndParameters) {
00776   const Matcher<int> m1 = IsEven2();
00777   EXPECT_EQ("is even", Describe(m1));
00778   EXPECT_EQ("is odd", DescribeNegation(m1));
00779 
00780   const Matcher<int> m2 = EqSumOf(5, 9);
00781   EXPECT_EQ("equals the sum of 5 and 9", Describe(m2));
00782   EXPECT_EQ("doesn't equal the sum of 5 and 9", DescribeNegation(m2));
00783 }
00784 
00785 // Tests explaining match result in a MATCHER* macro.
00786 TEST(MatcherMacroTest, CanExplainMatchResult) {
00787   const Matcher<int> m1 = IsEven2();
00788   EXPECT_EQ("OK", Explain(m1, 4));
00789   EXPECT_EQ("% 2 == 1", Explain(m1, 5));
00790 
00791   const Matcher<int> m2 = EqSumOf(1, 2);
00792   EXPECT_EQ("OK", Explain(m2, 3));
00793   EXPECT_EQ("diff == -1", Explain(m2, 4));
00794 }
00795 
00796 // Tests that the body of MATCHER() can reference the type of the
00797 // value being matched.
00798 
00799 MATCHER(IsEmptyString, "") {
00800   StaticAssertTypeEq< ::std::string, arg_type>();
00801   return arg == "";
00802 }
00803 
00804 MATCHER(IsEmptyStringByRef, "") {
00805   StaticAssertTypeEq<const ::std::string&, arg_type>();
00806   return arg == "";
00807 }
00808 
00809 TEST(MatcherMacroTest, CanReferenceArgType) {
00810   const Matcher< ::std::string> m1 = IsEmptyString();
00811   EXPECT_TRUE(m1.Matches(""));
00812 
00813   const Matcher<const ::std::string&> m2 = IsEmptyStringByRef();
00814   EXPECT_TRUE(m2.Matches(""));
00815 }
00816 
00817 // Tests that MATCHER() can be used in a namespace.
00818 
00819 namespace matcher_test {
00820 MATCHER(IsOdd, "") { return (arg % 2) != 0; }
00821 }  // namespace matcher_test
00822 
00823 TEST(MatcherMacroTest, WorksInNamespace) {
00824   Matcher<int> m = matcher_test::IsOdd();
00825   EXPECT_FALSE(m.Matches(4));
00826   EXPECT_TRUE(m.Matches(5));
00827 }
00828 
00829 // Tests that Value() can be used to compose matchers.
00830 MATCHER(IsPositiveOdd, "") {
00831   return Value(arg, matcher_test::IsOdd()) && arg > 0;
00832 }
00833 
00834 TEST(MatcherMacroTest, CanBeComposedUsingValue) {
00835   EXPECT_THAT(3, IsPositiveOdd());
00836   EXPECT_THAT(4, Not(IsPositiveOdd()));
00837   EXPECT_THAT(-1, Not(IsPositiveOdd()));
00838 }
00839 
00840 // Tests that a simple MATCHER_P() definition works.
00841 
00842 MATCHER_P(IsGreaterThan32And, n, "") { return arg > 32 && arg > n; }
00843 
00844 TEST(MatcherPMacroTest, Works) {
00845   const Matcher<int> m = IsGreaterThan32And(5);
00846   EXPECT_TRUE(m.Matches(36));
00847   EXPECT_FALSE(m.Matches(5));
00848 
00849   EXPECT_EQ("is greater than 32 and 5", Describe(m));
00850   EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m));
00851   EXPECT_EQ("", Explain(m, 36));
00852   EXPECT_EQ("", Explain(m, 5));
00853 }
00854 
00855 // Tests that the description is calculated correctly from the matcher name.
00856 MATCHER_P(_is_Greater_Than32and_, n, "") { return arg > 32 && arg > n; }
00857 
00858 TEST(MatcherPMacroTest, GeneratesCorrectDescription) {
00859   const Matcher<int> m = _is_Greater_Than32and_(5);
00860 
00861   EXPECT_EQ("is greater than 32 and 5", Describe(m));
00862   EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m));
00863   EXPECT_EQ("", Explain(m, 36));
00864   EXPECT_EQ("", Explain(m, 5));
00865 }
00866 
00867 // Tests that a MATCHER_P matcher can be explicitly instantiated with
00868 // a reference parameter type.
00869 
00870 class UncopyableFoo {
00871  public:
00872   explicit UncopyableFoo(char value) : value_(value) {}
00873  private:
00874   UncopyableFoo(const UncopyableFoo&);
00875   void operator=(const UncopyableFoo&);
00876 
00877   char value_;
00878 };
00879 
00880 MATCHER_P(ReferencesUncopyable, variable, "") { return &arg == &variable; }
00881 
00882 TEST(MatcherPMacroTest, WorksWhenExplicitlyInstantiatedWithReference) {
00883   UncopyableFoo foo1('1'), foo2('2');
00884   const Matcher<const UncopyableFoo&> m =
00885       ReferencesUncopyable<const UncopyableFoo&>(foo1);
00886 
00887   EXPECT_TRUE(m.Matches(foo1));
00888   EXPECT_FALSE(m.Matches(foo2));
00889 
00890   // We don't want the address of the parameter printed, as most
00891   // likely it will just annoy the user.  If the address is
00892   // interesting, the user should consider passing the parameter by
00893   // pointer instead.
00894   EXPECT_EQ("references uncopyable 1-byte object <31>", Describe(m));
00895 }
00896 
00897 
00898 // Tests that the body of MATCHER_Pn() can reference the parameter
00899 // types.
00900 
00901 MATCHER_P3(ParamTypesAreIntLongAndChar, foo, bar, baz, "") {
00902   StaticAssertTypeEq<int, foo_type>();
00903   StaticAssertTypeEq<long, bar_type>();  // NOLINT
00904   StaticAssertTypeEq<char, baz_type>();
00905   return arg == 0;
00906 }
00907 
00908 TEST(MatcherPnMacroTest, CanReferenceParamTypes) {
00909   EXPECT_THAT(0, ParamTypesAreIntLongAndChar(10, 20L, 'a'));
00910 }
00911 
00912 // Tests that a MATCHER_Pn matcher can be explicitly instantiated with
00913 // reference parameter types.
00914 
00915 MATCHER_P2(ReferencesAnyOf, variable1, variable2, "") {
00916   return &arg == &variable1 || &arg == &variable2;
00917 }
00918 
00919 TEST(MatcherPnMacroTest, WorksWhenExplicitlyInstantiatedWithReferences) {
00920   UncopyableFoo foo1('1'), foo2('2'), foo3('3');
00921   const Matcher<const UncopyableFoo&> m =
00922       ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
00923 
00924   EXPECT_TRUE(m.Matches(foo1));
00925   EXPECT_TRUE(m.Matches(foo2));
00926   EXPECT_FALSE(m.Matches(foo3));
00927 }
00928 
00929 TEST(MatcherPnMacroTest,
00930      GeneratesCorretDescriptionWhenExplicitlyInstantiatedWithReferences) {
00931   UncopyableFoo foo1('1'), foo2('2');
00932   const Matcher<const UncopyableFoo&> m =
00933       ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
00934 
00935   // We don't want the addresses of the parameters printed, as most
00936   // likely they will just annoy the user.  If the addresses are
00937   // interesting, the user should consider passing the parameters by
00938   // pointers instead.
00939   EXPECT_EQ("references any of (1-byte object <31>, 1-byte object <32>)",
00940             Describe(m));
00941 }
00942 
00943 // Tests that a simple MATCHER_P2() definition works.
00944 
00945 MATCHER_P2(IsNotInClosedRange, low, hi, "") { return arg < low || arg > hi; }
00946 
00947 TEST(MatcherPnMacroTest, Works) {
00948   const Matcher<const long&> m = IsNotInClosedRange(10, 20);  // NOLINT
00949   EXPECT_TRUE(m.Matches(36L));
00950   EXPECT_FALSE(m.Matches(15L));
00951 
00952   EXPECT_EQ("is not in closed range (10, 20)", Describe(m));
00953   EXPECT_EQ("not (is not in closed range (10, 20))", DescribeNegation(m));
00954   EXPECT_EQ("", Explain(m, 36L));
00955   EXPECT_EQ("", Explain(m, 15L));
00956 }
00957 
00958 // Tests that MATCHER*() definitions can be overloaded on the number
00959 // of parameters; also tests MATCHER_Pn() where n >= 3.
00960 
00961 MATCHER(EqualsSumOf, "") { return arg == 0; }
00962 MATCHER_P(EqualsSumOf, a, "") { return arg == a; }
00963 MATCHER_P2(EqualsSumOf, a, b, "") { return arg == a + b; }
00964 MATCHER_P3(EqualsSumOf, a, b, c, "") { return arg == a + b + c; }
00965 MATCHER_P4(EqualsSumOf, a, b, c, d, "") { return arg == a + b + c + d; }
00966 MATCHER_P5(EqualsSumOf, a, b, c, d, e, "") { return arg == a + b + c + d + e; }
00967 MATCHER_P6(EqualsSumOf, a, b, c, d, e, f, "") {
00968   return arg == a + b + c + d + e + f;
00969 }
00970 MATCHER_P7(EqualsSumOf, a, b, c, d, e, f, g, "") {
00971   return arg == a + b + c + d + e + f + g;
00972 }
00973 MATCHER_P8(EqualsSumOf, a, b, c, d, e, f, g, h, "") {
00974   return arg == a + b + c + d + e + f + g + h;
00975 }
00976 MATCHER_P9(EqualsSumOf, a, b, c, d, e, f, g, h, i, "") {
00977   return arg == a + b + c + d + e + f + g + h + i;
00978 }
00979 MATCHER_P10(EqualsSumOf, a, b, c, d, e, f, g, h, i, j, "") {
00980   return arg == a + b + c + d + e + f + g + h + i + j;
00981 }
00982 
00983 TEST(MatcherPnMacroTest, CanBeOverloadedOnNumberOfParameters) {
00984   EXPECT_THAT(0, EqualsSumOf());
00985   EXPECT_THAT(1, EqualsSumOf(1));
00986   EXPECT_THAT(12, EqualsSumOf(10, 2));
00987   EXPECT_THAT(123, EqualsSumOf(100, 20, 3));
00988   EXPECT_THAT(1234, EqualsSumOf(1000, 200, 30, 4));
00989   EXPECT_THAT(12345, EqualsSumOf(10000, 2000, 300, 40, 5));
00990   EXPECT_THAT("abcdef",
00991               EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f'));
00992   EXPECT_THAT("abcdefg",
00993               EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g'));
00994   EXPECT_THAT("abcdefgh",
00995               EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
00996                           "h"));
00997   EXPECT_THAT("abcdefghi",
00998               EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
00999                           "h", 'i'));
01000   EXPECT_THAT("abcdefghij",
01001               EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
01002                           "h", 'i', ::std::string("j")));
01003 
01004   EXPECT_THAT(1, Not(EqualsSumOf()));
01005   EXPECT_THAT(-1, Not(EqualsSumOf(1)));
01006   EXPECT_THAT(-12, Not(EqualsSumOf(10, 2)));
01007   EXPECT_THAT(-123, Not(EqualsSumOf(100, 20, 3)));
01008   EXPECT_THAT(-1234, Not(EqualsSumOf(1000, 200, 30, 4)));
01009   EXPECT_THAT(-12345, Not(EqualsSumOf(10000, 2000, 300, 40, 5)));
01010   EXPECT_THAT("abcdef ",
01011               Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f')));
01012   EXPECT_THAT("abcdefg ",
01013               Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f',
01014                               'g')));
01015   EXPECT_THAT("abcdefgh ",
01016               Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
01017                               "h")));
01018   EXPECT_THAT("abcdefghi ",
01019               Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
01020                               "h", 'i')));
01021   EXPECT_THAT("abcdefghij ",
01022               Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
01023                               "h", 'i', ::std::string("j"))));
01024 }
01025 
01026 // Tests that a MATCHER_Pn() definition can be instantiated with any
01027 // compatible parameter types.
01028 TEST(MatcherPnMacroTest, WorksForDifferentParameterTypes) {
01029   EXPECT_THAT(123, EqualsSumOf(100L, 20, static_cast<char>(3)));
01030   EXPECT_THAT("abcd", EqualsSumOf(::std::string("a"), "b", 'c', "d"));
01031 
01032   EXPECT_THAT(124, Not(EqualsSumOf(100L, 20, static_cast<char>(3))));
01033   EXPECT_THAT("abcde", Not(EqualsSumOf(::std::string("a"), "b", 'c', "d")));
01034 }
01035 
01036 // Tests that the matcher body can promote the parameter types.
01037 
01038 MATCHER_P2(EqConcat, prefix, suffix, "") {
01039   // The following lines promote the two parameters to desired types.
01040   std::string prefix_str(prefix);
01041   char suffix_char = static_cast<char>(suffix);
01042   return arg == prefix_str + suffix_char;
01043 }
01044 
01045 TEST(MatcherPnMacroTest, SimpleTypePromotion) {
01046   Matcher<std::string> no_promo =
01047       EqConcat(std::string("foo"), 't');
01048   Matcher<const std::string&> promo =
01049       EqConcat("foo", static_cast<int>('t'));
01050   EXPECT_FALSE(no_promo.Matches("fool"));
01051   EXPECT_FALSE(promo.Matches("fool"));
01052   EXPECT_TRUE(no_promo.Matches("foot"));
01053   EXPECT_TRUE(promo.Matches("foot"));
01054 }
01055 
01056 // Verifies the type of a MATCHER*.
01057 
01058 TEST(MatcherPnMacroTest, TypesAreCorrect) {
01059   // EqualsSumOf() must be assignable to a EqualsSumOfMatcher variable.
01060   EqualsSumOfMatcher a0 = EqualsSumOf();
01061 
01062   // EqualsSumOf(1) must be assignable to a EqualsSumOfMatcherP variable.
01063   EqualsSumOfMatcherP<int> a1 = EqualsSumOf(1);
01064 
01065   // EqualsSumOf(p1, ..., pk) must be assignable to a EqualsSumOfMatcherPk
01066   // variable, and so on.
01067   EqualsSumOfMatcherP2<int, char> a2 = EqualsSumOf(1, '2');
01068   EqualsSumOfMatcherP3<int, int, char> a3 = EqualsSumOf(1, 2, '3');
01069   EqualsSumOfMatcherP4<int, int, int, char> a4 = EqualsSumOf(1, 2, 3, '4');
01070   EqualsSumOfMatcherP5<int, int, int, int, char> a5 =
01071       EqualsSumOf(1, 2, 3, 4, '5');
01072   EqualsSumOfMatcherP6<int, int, int, int, int, char> a6 =
01073       EqualsSumOf(1, 2, 3, 4, 5, '6');
01074   EqualsSumOfMatcherP7<int, int, int, int, int, int, char> a7 =
01075       EqualsSumOf(1, 2, 3, 4, 5, 6, '7');
01076   EqualsSumOfMatcherP8<int, int, int, int, int, int, int, char> a8 =
01077       EqualsSumOf(1, 2, 3, 4, 5, 6, 7, '8');
01078   EqualsSumOfMatcherP9<int, int, int, int, int, int, int, int, char> a9 =
01079       EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, '9');
01080   EqualsSumOfMatcherP10<int, int, int, int, int, int, int, int, int, char> a10 =
01081       EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, 9, '0');
01082 
01083   // Avoid "unused variable" warnings.
01084   (void)a0;
01085   (void)a1;
01086   (void)a2;
01087   (void)a3;
01088   (void)a4;
01089   (void)a5;
01090   (void)a6;
01091   (void)a7;
01092   (void)a8;
01093   (void)a9;
01094   (void)a10;
01095 }
01096 
01097 // Tests that matcher-typed parameters can be used in Value() inside a
01098 // MATCHER_Pn definition.
01099 
01100 // Succeeds if arg matches exactly 2 of the 3 matchers.
01101 MATCHER_P3(TwoOf, m1, m2, m3, "") {
01102   const int count = static_cast<int>(Value(arg, m1))
01103       + static_cast<int>(Value(arg, m2)) + static_cast<int>(Value(arg, m3));
01104   return count == 2;
01105 }
01106 
01107 TEST(MatcherPnMacroTest, CanUseMatcherTypedParameterInValue) {
01108   EXPECT_THAT(42, TwoOf(Gt(0), Lt(50), Eq(10)));
01109   EXPECT_THAT(0, Not(TwoOf(Gt(-1), Lt(1), Eq(0))));
01110 }
01111 
01112 // Tests Contains().
01113 
01114 TEST(ContainsTest, ListMatchesWhenElementIsInContainer) {
01115   list<int> some_list;
01116   some_list.push_back(3);
01117   some_list.push_back(1);
01118   some_list.push_back(2);
01119   EXPECT_THAT(some_list, Contains(1));
01120   EXPECT_THAT(some_list, Contains(Gt(2.5)));
01121   EXPECT_THAT(some_list, Contains(Eq(2.0f)));
01122 
01123   list<string> another_list;
01124   another_list.push_back("fee");
01125   another_list.push_back("fie");
01126   another_list.push_back("foe");
01127   another_list.push_back("fum");
01128   EXPECT_THAT(another_list, Contains(string("fee")));
01129 }
01130 
01131 TEST(ContainsTest, ListDoesNotMatchWhenElementIsNotInContainer) {
01132   list<int> some_list;
01133   some_list.push_back(3);
01134   some_list.push_back(1);
01135   EXPECT_THAT(some_list, Not(Contains(4)));
01136 }
01137 
01138 TEST(ContainsTest, SetMatchesWhenElementIsInContainer) {
01139   set<int> some_set;
01140   some_set.insert(3);
01141   some_set.insert(1);
01142   some_set.insert(2);
01143   EXPECT_THAT(some_set, Contains(Eq(1.0)));
01144   EXPECT_THAT(some_set, Contains(Eq(3.0f)));
01145   EXPECT_THAT(some_set, Contains(2));
01146 
01147   set<const char*> another_set;
01148   another_set.insert("fee");
01149   another_set.insert("fie");
01150   another_set.insert("foe");
01151   another_set.insert("fum");
01152   EXPECT_THAT(another_set, Contains(Eq(string("fum"))));
01153 }
01154 
01155 TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) {
01156   set<int> some_set;
01157   some_set.insert(3);
01158   some_set.insert(1);
01159   EXPECT_THAT(some_set, Not(Contains(4)));
01160 
01161   set<const char*> c_string_set;
01162   c_string_set.insert("hello");
01163   EXPECT_THAT(c_string_set, Not(Contains(string("hello").c_str())));
01164 }
01165 
01166 TEST(ContainsTest, ExplainsMatchResultCorrectly) {
01167   const int a[2] = { 1, 2 };
01168   Matcher<const int (&)[2]> m = Contains(2);
01169   EXPECT_EQ("whose element #1 matches", Explain(m, a));
01170 
01171   m = Contains(3);
01172   EXPECT_EQ("", Explain(m, a));
01173 
01174   m = Contains(GreaterThan(0));
01175   EXPECT_EQ("whose element #0 matches, which is 1 more than 0", Explain(m, a));
01176 
01177   m = Contains(GreaterThan(10));
01178   EXPECT_EQ("", Explain(m, a));
01179 }
01180 
01181 TEST(ContainsTest, DescribesItselfCorrectly) {
01182   Matcher<vector<int> > m = Contains(1);
01183   EXPECT_EQ("contains at least one element that is equal to 1", Describe(m));
01184 
01185   Matcher<vector<int> > m2 = Not(m);
01186   EXPECT_EQ("doesn't contain any element that is equal to 1", Describe(m2));
01187 }
01188 
01189 TEST(ContainsTest, MapMatchesWhenElementIsInContainer) {
01190   map<const char*, int> my_map;
01191   const char* bar = "a string";
01192   my_map[bar] = 2;
01193   EXPECT_THAT(my_map, Contains(pair<const char* const, int>(bar, 2)));
01194 
01195   map<string, int> another_map;
01196   another_map["fee"] = 1;
01197   another_map["fie"] = 2;
01198   another_map["foe"] = 3;
01199   another_map["fum"] = 4;
01200   EXPECT_THAT(another_map, Contains(pair<const string, int>(string("fee"), 1)));
01201   EXPECT_THAT(another_map, Contains(pair<const string, int>("fie", 2)));
01202 }
01203 
01204 TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) {
01205   map<int, int> some_map;
01206   some_map[1] = 11;
01207   some_map[2] = 22;
01208   EXPECT_THAT(some_map, Not(Contains(pair<const int, int>(2, 23))));
01209 }
01210 
01211 TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) {
01212   const char* string_array[] = { "fee", "fie", "foe", "fum" };
01213   EXPECT_THAT(string_array, Contains(Eq(string("fum"))));
01214 }
01215 
01216 TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) {
01217   int int_array[] = { 1, 2, 3, 4 };
01218   EXPECT_THAT(int_array, Not(Contains(5)));
01219 }
01220 
01221 TEST(ContainsTest, AcceptsMatcher) {
01222   const int a[] = { 1, 2, 3 };
01223   EXPECT_THAT(a, Contains(Gt(2)));
01224   EXPECT_THAT(a, Not(Contains(Gt(4))));
01225 }
01226 
01227 TEST(ContainsTest, WorksForNativeArrayAsTuple) {
01228   const int a[] = { 1, 2 };
01229   const int* const pointer = a;
01230   EXPECT_THAT(make_tuple(pointer, 2), Contains(1));
01231   EXPECT_THAT(make_tuple(pointer, 2), Not(Contains(Gt(3))));
01232 }
01233 
01234 TEST(ContainsTest, WorksForTwoDimensionalNativeArray) {
01235   int a[][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
01236   EXPECT_THAT(a, Contains(ElementsAre(4, 5, 6)));
01237   EXPECT_THAT(a, Contains(Contains(5)));
01238   EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5))));
01239   EXPECT_THAT(a, Contains(Not(Contains(5))));
01240 }
01241 
01242 TEST(AllOfTest, HugeMatcher) {
01243   // Verify that using AllOf with many arguments doesn't cause
01244   // the compiler to exceed template instantiation depth limit.
01245   EXPECT_THAT(0, testing::AllOf(_, _, _, _, _, _, _, _, _,
01246                                 testing::AllOf(_, _, _, _, _, _, _, _, _, _)));
01247 }
01248 
01249 TEST(AnyOfTest, HugeMatcher) {
01250   // Verify that using AnyOf with many arguments doesn't cause
01251   // the compiler to exceed template instantiation depth limit.
01252   EXPECT_THAT(0, testing::AnyOf(_, _, _, _, _, _, _, _, _,
01253                                 testing::AnyOf(_, _, _, _, _, _, _, _, _, _)));
01254 }
01255 
01256 namespace adl_test {
01257 
01258 // Verifies that the implementation of ::testing::AllOf and ::testing::AnyOf
01259 // don't issue unqualified recursive calls.  If they do, the argument dependent
01260 // name lookup will cause AllOf/AnyOf in the 'adl_test' namespace to be found
01261 // as a candidate and the compilation will break due to an ambiguous overload.
01262 
01263 // The matcher must be in the same namespace as AllOf/AnyOf to make argument
01264 // dependent lookup find those.
01265 MATCHER(M, "") { return true; }
01266 
01267 template <typename T1, typename T2>
01268 bool AllOf(const T1& t1, const T2& t2) { return true; }
01269 
01270 TEST(AllOfTest, DoesNotCallAllOfUnqualified) {
01271   EXPECT_THAT(42, testing::AllOf(
01272       M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
01273 }
01274 
01275 template <typename T1, typename T2> bool
01276 AnyOf(const T1& t1, const T2& t2) { return true; }
01277 
01278 TEST(AnyOfTest, DoesNotCallAnyOfUnqualified) {
01279   EXPECT_THAT(42, testing::AnyOf(
01280       M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
01281 }
01282 
01283 }  // namespace adl_test
01284 
01285 #ifdef _MSC_VER
01286 # pragma warning(pop)
01287 #endif
01288 
01289 }  // namespace


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:41