Home | History | Annotate | Download | only in gmock
      1 $$ -*- mode: c++; -*-
      2 $$ This is a Pump source file.  Please use Pump to convert it to
      3 $$ gmock-generated-actions.h.
      4 $$
      5 $var n = 10  $$ The maximum arity we support.
      6 $$ }} This line fixes auto-indentation of the following code in Emacs.
      7 // Copyright 2008, Google Inc.
      8 // All rights reserved.
      9 //
     10 // Redistribution and use in source and binary forms, with or without
     11 // modification, are permitted provided that the following conditions are
     12 // met:
     13 //
     14 //     * Redistributions of source code must retain the above copyright
     15 // notice, this list of conditions and the following disclaimer.
     16 //     * Redistributions in binary form must reproduce the above
     17 // copyright notice, this list of conditions and the following disclaimer
     18 // in the documentation and/or other materials provided with the
     19 // distribution.
     20 //     * Neither the name of Google Inc. nor the names of its
     21 // contributors may be used to endorse or promote products derived from
     22 // this software without specific prior written permission.
     23 //
     24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     27 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     28 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     29 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     30 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     31 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     32 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     33 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     34 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     35 
     36 // Google Mock - a framework for writing C++ mock classes.
     37 //
     38 // This file implements some commonly used variadic matchers.
     39 
     40 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_MATCHERS_H_
     41 #define GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_MATCHERS_H_
     42 
     43 #include <sstream>
     44 #include <string>
     45 #include <vector>
     46 #include "gmock/gmock-matchers.h"
     47 
     48 namespace testing {
     49 namespace internal {
     50 
     51 $range i 0..n-1
     52 
     53 // The type of the i-th (0-based) field of Tuple.
     54 #define GMOCK_FIELD_TYPE_(Tuple, i) \
     55     typename ::std::tr1::tuple_element<i, Tuple>::type
     56 
     57 // TupleFields<Tuple, k0, ..., kn> is for selecting fields from a
     58 // tuple of type Tuple.  It has two members:
     59 //
     60 //   type: a tuple type whose i-th field is the ki-th field of Tuple.
     61 //   GetSelectedFields(t): returns fields k0, ..., and kn of t as a tuple.
     62 //
     63 // For example, in class TupleFields<tuple<bool, char, int>, 2, 0>, we have:
     64 //
     65 //   type is tuple<int, bool>, and
     66 //   GetSelectedFields(make_tuple(true, 'a', 42)) is (42, true).
     67 
     68 template <class Tuple$for i [[, int k$i = -1]]>
     69 class TupleFields;
     70 
     71 // This generic version is used when there are $n selectors.
     72 template <class Tuple$for i [[, int k$i]]>
     73 class TupleFields {
     74  public:
     75   typedef ::std::tr1::tuple<$for i, [[GMOCK_FIELD_TYPE_(Tuple, k$i)]]> type;
     76   static type GetSelectedFields(const Tuple& t) {
     77     using ::std::tr1::get;
     78     return type($for i, [[get<k$i>(t)]]);
     79   }
     80 };
     81 
     82 // The following specialization is used for 0 ~ $(n-1) selectors.
     83 
     84 $for i [[
     85 $$ }}}
     86 $range j 0..i-1
     87 $range k 0..n-1
     88 
     89 template <class Tuple$for j [[, int k$j]]>
     90 class TupleFields<Tuple, $for k, [[$if k < i [[k$k]] $else [[-1]]]]> {
     91  public:
     92   typedef ::std::tr1::tuple<$for j, [[GMOCK_FIELD_TYPE_(Tuple, k$j)]]> type;
     93   static type GetSelectedFields(const Tuple& $if i==0 [[/* t */]] $else [[t]]) {
     94     using ::std::tr1::get;
     95     return type($for j, [[get<k$j>(t)]]);
     96   }
     97 };
     98 
     99 ]]
    100 
    101 #undef GMOCK_FIELD_TYPE_
    102 
    103 // Implements the Args() matcher.
    104 
    105 $var ks = [[$for i, [[k$i]]]]
    106 template <class ArgsTuple$for i [[, int k$i = -1]]>
    107 class ArgsMatcherImpl : public MatcherInterface<ArgsTuple> {
    108  public:
    109   // ArgsTuple may have top-level const or reference modifiers.
    110   typedef GTEST_REMOVE_REFERENCE_AND_CONST_(ArgsTuple) RawArgsTuple;
    111   typedef typename internal::TupleFields<RawArgsTuple, $ks>::type SelectedArgs;
    112   typedef Matcher<const SelectedArgs&> MonomorphicInnerMatcher;
    113 
    114   template <typename InnerMatcher>
    115   explicit ArgsMatcherImpl(const InnerMatcher& inner_matcher)
    116       : inner_matcher_(SafeMatcherCast<const SelectedArgs&>(inner_matcher)) {}
    117 
    118   virtual bool MatchAndExplain(ArgsTuple args,
    119                                MatchResultListener* listener) const {
    120     const SelectedArgs& selected_args = GetSelectedArgs(args);
    121     if (!listener->IsInterested())
    122       return inner_matcher_.Matches(selected_args);
    123 
    124     PrintIndices(listener->stream());
    125     *listener << "are " << PrintToString(selected_args);
    126 
    127     StringMatchResultListener inner_listener;
    128     const bool match = inner_matcher_.MatchAndExplain(selected_args,
    129                                                       &inner_listener);
    130     PrintIfNotEmpty(inner_listener.str(), listener->stream());
    131     return match;
    132   }
    133 
    134   virtual void DescribeTo(::std::ostream* os) const {
    135     *os << "are a tuple ";
    136     PrintIndices(os);
    137     inner_matcher_.DescribeTo(os);
    138   }
    139 
    140   virtual void DescribeNegationTo(::std::ostream* os) const {
    141     *os << "are a tuple ";
    142     PrintIndices(os);
    143     inner_matcher_.DescribeNegationTo(os);
    144   }
    145 
    146  private:
    147   static SelectedArgs GetSelectedArgs(ArgsTuple args) {
    148     return TupleFields<RawArgsTuple, $ks>::GetSelectedFields(args);
    149   }
    150 
    151   // Prints the indices of the selected fields.
    152   static void PrintIndices(::std::ostream* os) {
    153     *os << "whose fields (";
    154     const int indices[$n] = { $ks };
    155     for (int i = 0; i < $n; i++) {
    156       if (indices[i] < 0)
    157         break;
    158 
    159       if (i >= 1)
    160         *os << ", ";
    161 
    162       *os << "#" << indices[i];
    163     }
    164     *os << ") ";
    165   }
    166 
    167   const MonomorphicInnerMatcher inner_matcher_;
    168 
    169   GTEST_DISALLOW_ASSIGN_(ArgsMatcherImpl);
    170 };
    171 
    172 template <class InnerMatcher$for i [[, int k$i = -1]]>
    173 class ArgsMatcher {
    174  public:
    175   explicit ArgsMatcher(const InnerMatcher& inner_matcher)
    176       : inner_matcher_(inner_matcher) {}
    177 
    178   template <typename ArgsTuple>
    179   operator Matcher<ArgsTuple>() const {
    180     return MakeMatcher(new ArgsMatcherImpl<ArgsTuple, $ks>(inner_matcher_));
    181   }
    182 
    183  private:
    184   const InnerMatcher inner_matcher_;
    185 
    186   GTEST_DISALLOW_ASSIGN_(ArgsMatcher);
    187 };
    188 
    189 // Implements ElementsAre() of 1-$n arguments.
    190 
    191 
    192 $range i 1..n
    193 $for i [[
    194 $range j 1..i
    195 template <$for j, [[typename T$j]]>
    196 class ElementsAreMatcher$i {
    197  public:
    198   $if i==1 [[explicit ]]ElementsAreMatcher$i($for j, [[const T$j& e$j]])$if i > 0 [[ : ]]
    199       $for j, [[e$j[[]]_(e$j)]] {}
    200 
    201   template <typename Container>
    202   operator Matcher<Container>() const {
    203     typedef GTEST_REMOVE_REFERENCE_AND_CONST_(Container) RawContainer;
    204     typedef typename internal::StlContainerView<RawContainer>::type::value_type
    205         Element;
    206 
    207 $if i==1 [[
    208 
    209     // Nokia's Symbian Compiler has a nasty bug where the object put
    210     // in a one-element local array is not destructed when the array
    211     // goes out of scope.  This leads to obvious badness as we've
    212     // added the linked_ptr in it to our other linked_ptrs list.
    213     // Hence we implement ElementsAreMatcher1 specially to avoid using
    214     // a local array.
    215     const Matcher<const Element&> matcher =
    216         MatcherCast<const Element&>(e1_);
    217     return MakeMatcher(new ElementsAreMatcherImpl<Container>(&matcher, 1));
    218 ]] $else [[
    219 
    220     const Matcher<const Element&> matchers[] = {
    221 
    222 $for j [[
    223       MatcherCast<const Element&>(e$j[[]]_),
    224 
    225 ]]
    226     };
    227 
    228     return MakeMatcher(new ElementsAreMatcherImpl<Container>(matchers, $i));
    229 ]]
    230 
    231   }
    232 
    233  private:
    234 
    235 $for j [[
    236   const T$j& e$j[[]]_;
    237 
    238 ]]
    239 
    240   GTEST_DISALLOW_ASSIGN_(ElementsAreMatcher$i);
    241 };
    242 
    243 
    244 ]]
    245 // A set of metafunctions for computing the result type of AllOf.
    246 // AllOf(m1, ..., mN) returns
    247 // AllOfResultN<decltype(m1), ..., decltype(mN)>::type.
    248 
    249 // Although AllOf isn't defined for one argument, AllOfResult1 is defined
    250 // to simplify the implementation.
    251 template <typename M1>
    252 struct AllOfResult1 {
    253   typedef M1 type;
    254 };
    255 
    256 $range i 1..n
    257 
    258 $range i 2..n
    259 $for i [[
    260 $range j 2..i
    261 $var m = i/2
    262 $range k 1..m
    263 $range t m+1..i
    264 
    265 template <typename M1$for j [[, typename M$j]]>
    266 struct AllOfResult$i {
    267   typedef BothOfMatcher<
    268       typename AllOfResult$m<$for k, [[M$k]]>::type,
    269       typename AllOfResult$(i-m)<$for t, [[M$t]]>::type
    270   > type;
    271 };
    272 
    273 ]]
    274 
    275 // A set of metafunctions for computing the result type of AnyOf.
    276 // AnyOf(m1, ..., mN) returns
    277 // AnyOfResultN<decltype(m1), ..., decltype(mN)>::type.
    278 
    279 // Although AnyOf isn't defined for one argument, AnyOfResult1 is defined
    280 // to simplify the implementation.
    281 template <typename M1>
    282 struct AnyOfResult1 {
    283   typedef M1 type;
    284 };
    285 
    286 $range i 1..n
    287 
    288 $range i 2..n
    289 $for i [[
    290 $range j 2..i
    291 $var m = i/2
    292 $range k 1..m
    293 $range t m+1..i
    294 
    295 template <typename M1$for j [[, typename M$j]]>
    296 struct AnyOfResult$i {
    297   typedef EitherOfMatcher<
    298       typename AnyOfResult$m<$for k, [[M$k]]>::type,
    299       typename AnyOfResult$(i-m)<$for t, [[M$t]]>::type
    300   > type;
    301 };
    302 
    303 ]]
    304 
    305 }  // namespace internal
    306 
    307 // Args<N1, N2, ..., Nk>(a_matcher) matches a tuple if the selected
    308 // fields of it matches a_matcher.  C++ doesn't support default
    309 // arguments for function templates, so we have to overload it.
    310 
    311 $range i 0..n
    312 $for i [[
    313 $range j 1..i
    314 template <$for j [[int k$j, ]]typename InnerMatcher>
    315 inline internal::ArgsMatcher<InnerMatcher$for j [[, k$j]]>
    316 Args(const InnerMatcher& matcher) {
    317   return internal::ArgsMatcher<InnerMatcher$for j [[, k$j]]>(matcher);
    318 }
    319 
    320 
    321 ]]
    322 // ElementsAre(e0, e1, ..., e_n) matches an STL-style container with
    323 // (n + 1) elements, where the i-th element in the container must
    324 // match the i-th argument in the list.  Each argument of
    325 // ElementsAre() can be either a value or a matcher.  We support up to
    326 // $n arguments.
    327 //
    328 // NOTE: Since ElementsAre() cares about the order of the elements, it
    329 // must not be used with containers whose elements's order is
    330 // undefined (e.g. hash_map).
    331 
    332 inline internal::ElementsAreMatcher0 ElementsAre() {
    333   return internal::ElementsAreMatcher0();
    334 }
    335 
    336 $range i 1..n
    337 $for i [[
    338 $range j 1..i
    339 
    340 template <$for j, [[typename T$j]]>
    341 inline internal::ElementsAreMatcher$i<$for j, [[T$j]]> ElementsAre($for j, [[const T$j& e$j]]) {
    342   return internal::ElementsAreMatcher$i<$for j, [[T$j]]>($for j, [[e$j]]);
    343 }
    344 
    345 ]]
    346 
    347 // ElementsAreArray(array) and ElementAreArray(array, count) are like
    348 // ElementsAre(), except that they take an array of values or
    349 // matchers.  The former form infers the size of 'array', which must
    350 // be a static C-style array.  In the latter form, 'array' can either
    351 // be a static array or a pointer to a dynamically created array.
    352 
    353 template <typename T>
    354 inline internal::ElementsAreArrayMatcher<T> ElementsAreArray(
    355     const T* first, size_t count) {
    356   return internal::ElementsAreArrayMatcher<T>(first, count);
    357 }
    358 
    359 template <typename T, size_t N>
    360 inline internal::ElementsAreArrayMatcher<T>
    361 ElementsAreArray(const T (&array)[N]) {
    362   return internal::ElementsAreArrayMatcher<T>(array, N);
    363 }
    364 
    365 // AllOf(m1, m2, ..., mk) matches any value that matches all of the given
    366 // sub-matchers.  AllOf is called fully qualified to prevent ADL from firing.
    367 
    368 $range i 2..n
    369 $for i [[
    370 $range j 1..i
    371 $var m = i/2
    372 $range k 1..m
    373 $range t m+1..i
    374 
    375 template <$for j, [[typename M$j]]>
    376 inline typename internal::AllOfResult$i<$for j, [[M$j]]>::type
    377 AllOf($for j, [[M$j m$j]]) {
    378   return typename internal::AllOfResult$i<$for j, [[M$j]]>::type(
    379       $if m == 1 [[m1]] $else [[::testing::AllOf($for k, [[m$k]])]],
    380       $if m+1 == i [[m$i]] $else [[::testing::AllOf($for t, [[m$t]])]]);
    381 }
    382 
    383 ]]
    384 
    385 // AnyOf(m1, m2, ..., mk) matches any value that matches any of the given
    386 // sub-matchers.  AnyOf is called fully qualified to prevent ADL from firing.
    387 
    388 $range i 2..n
    389 $for i [[
    390 $range j 1..i
    391 $var m = i/2
    392 $range k 1..m
    393 $range t m+1..i
    394 
    395 template <$for j, [[typename M$j]]>
    396 inline typename internal::AnyOfResult$i<$for j, [[M$j]]>::type
    397 AnyOf($for j, [[M$j m$j]]) {
    398   return typename internal::AnyOfResult$i<$for j, [[M$j]]>::type(
    399       $if m == 1 [[m1]] $else [[::testing::AnyOf($for k, [[m$k]])]],
    400       $if m+1 == i [[m$i]] $else [[::testing::AnyOf($for t, [[m$t]])]]);
    401 }
    402 
    403 ]]
    404 
    405 }  // namespace testing
    406 $$ } // This Pump meta comment fixes auto-indentation in Emacs. It will not
    407 $$   // show up in the generated code.
    408 
    409 
    410 // The MATCHER* family of macros can be used in a namespace scope to
    411 // define custom matchers easily.
    412 //
    413 // Basic Usage
    414 // ===========
    415 //
    416 // The syntax
    417 //
    418 //   MATCHER(name, description_string) { statements; }
    419 //
    420 // defines a matcher with the given name that executes the statements,
    421 // which must return a bool to indicate if the match succeeds.  Inside
    422 // the statements, you can refer to the value being matched by 'arg',
    423 // and refer to its type by 'arg_type'.
    424 //
    425 // The description string documents what the matcher does, and is used
    426 // to generate the failure message when the match fails.  Since a
    427 // MATCHER() is usually defined in a header file shared by multiple
    428 // C++ source files, we require the description to be a C-string
    429 // literal to avoid possible side effects.  It can be empty, in which
    430 // case we'll use the sequence of words in the matcher name as the
    431 // description.
    432 //
    433 // For example:
    434 //
    435 //   MATCHER(IsEven, "") { return (arg % 2) == 0; }
    436 //
    437 // allows you to write
    438 //
    439 //   // Expects mock_foo.Bar(n) to be called where n is even.
    440 //   EXPECT_CALL(mock_foo, Bar(IsEven()));
    441 //
    442 // or,
    443 //
    444 //   // Verifies that the value of some_expression is even.
    445 //   EXPECT_THAT(some_expression, IsEven());
    446 //
    447 // If the above assertion fails, it will print something like:
    448 //
    449 //   Value of: some_expression
    450 //   Expected: is even
    451 //     Actual: 7
    452 //
    453 // where the description "is even" is automatically calculated from the
    454 // matcher name IsEven.
    455 //
    456 // Argument Type
    457 // =============
    458 //
    459 // Note that the type of the value being matched (arg_type) is
    460 // determined by the context in which you use the matcher and is
    461 // supplied to you by the compiler, so you don't need to worry about
    462 // declaring it (nor can you).  This allows the matcher to be
    463 // polymorphic.  For example, IsEven() can be used to match any type
    464 // where the value of "(arg % 2) == 0" can be implicitly converted to
    465 // a bool.  In the "Bar(IsEven())" example above, if method Bar()
    466 // takes an int, 'arg_type' will be int; if it takes an unsigned long,
    467 // 'arg_type' will be unsigned long; and so on.
    468 //
    469 // Parameterizing Matchers
    470 // =======================
    471 //
    472 // Sometimes you'll want to parameterize the matcher.  For that you
    473 // can use another macro:
    474 //
    475 //   MATCHER_P(name, param_name, description_string) { statements; }
    476 //
    477 // For example:
    478 //
    479 //   MATCHER_P(HasAbsoluteValue, value, "") { return abs(arg) == value; }
    480 //
    481 // will allow you to write:
    482 //
    483 //   EXPECT_THAT(Blah("a"), HasAbsoluteValue(n));
    484 //
    485 // which may lead to this message (assuming n is 10):
    486 //
    487 //   Value of: Blah("a")
    488 //   Expected: has absolute value 10
    489 //     Actual: -9
    490 //
    491 // Note that both the matcher description and its parameter are
    492 // printed, making the message human-friendly.
    493 //
    494 // In the matcher definition body, you can write 'foo_type' to
    495 // reference the type of a parameter named 'foo'.  For example, in the
    496 // body of MATCHER_P(HasAbsoluteValue, value) above, you can write
    497 // 'value_type' to refer to the type of 'value'.
    498 //
    499 // We also provide MATCHER_P2, MATCHER_P3, ..., up to MATCHER_P$n to
    500 // support multi-parameter matchers.
    501 //
    502 // Describing Parameterized Matchers
    503 // =================================
    504 //
    505 // The last argument to MATCHER*() is a string-typed expression.  The
    506 // expression can reference all of the matcher's parameters and a
    507 // special bool-typed variable named 'negation'.  When 'negation' is
    508 // false, the expression should evaluate to the matcher's description;
    509 // otherwise it should evaluate to the description of the negation of
    510 // the matcher.  For example,
    511 //
    512 //   using testing::PrintToString;
    513 //
    514 //   MATCHER_P2(InClosedRange, low, hi,
    515 //       string(negation ? "is not" : "is") + " in range [" +
    516 //       PrintToString(low) + ", " + PrintToString(hi) + "]") {
    517 //     return low <= arg && arg <= hi;
    518 //   }
    519 //   ...
    520 //   EXPECT_THAT(3, InClosedRange(4, 6));
    521 //   EXPECT_THAT(3, Not(InClosedRange(2, 4)));
    522 //
    523 // would generate two failures that contain the text:
    524 //
    525 //   Expected: is in range [4, 6]
    526 //   ...
    527 //   Expected: is not in range [2, 4]
    528 //
    529 // If you specify "" as the description, the failure message will
    530 // contain the sequence of words in the matcher name followed by the
    531 // parameter values printed as a tuple.  For example,
    532 //
    533 //   MATCHER_P2(InClosedRange, low, hi, "") { ... }
    534 //   ...
    535 //   EXPECT_THAT(3, InClosedRange(4, 6));
    536 //   EXPECT_THAT(3, Not(InClosedRange(2, 4)));
    537 //
    538 // would generate two failures that contain the text:
    539 //
    540 //   Expected: in closed range (4, 6)
    541 //   ...
    542 //   Expected: not (in closed range (2, 4))
    543 //
    544 // Types of Matcher Parameters
    545 // ===========================
    546 //
    547 // For the purpose of typing, you can view
    548 //
    549 //   MATCHER_Pk(Foo, p1, ..., pk, description_string) { ... }
    550 //
    551 // as shorthand for
    552 //
    553 //   template <typename p1_type, ..., typename pk_type>
    554 //   FooMatcherPk<p1_type, ..., pk_type>
    555 //   Foo(p1_type p1, ..., pk_type pk) { ... }
    556 //
    557 // When you write Foo(v1, ..., vk), the compiler infers the types of
    558 // the parameters v1, ..., and vk for you.  If you are not happy with
    559 // the result of the type inference, you can specify the types by
    560 // explicitly instantiating the template, as in Foo<long, bool>(5,
    561 // false).  As said earlier, you don't get to (or need to) specify
    562 // 'arg_type' as that's determined by the context in which the matcher
    563 // is used.  You can assign the result of expression Foo(p1, ..., pk)
    564 // to a variable of type FooMatcherPk<p1_type, ..., pk_type>.  This
    565 // can be useful when composing matchers.
    566 //
    567 // While you can instantiate a matcher template with reference types,
    568 // passing the parameters by pointer usually makes your code more
    569 // readable.  If, however, you still want to pass a parameter by
    570 // reference, be aware that in the failure message generated by the
    571 // matcher you will see the value of the referenced object but not its
    572 // address.
    573 //
    574 // Explaining Match Results
    575 // ========================
    576 //
    577 // Sometimes the matcher description alone isn't enough to explain why
    578 // the match has failed or succeeded.  For example, when expecting a
    579 // long string, it can be very helpful to also print the diff between
    580 // the expected string and the actual one.  To achieve that, you can
    581 // optionally stream additional information to a special variable
    582 // named result_listener, whose type is a pointer to class
    583 // MatchResultListener:
    584 //
    585 //   MATCHER_P(EqualsLongString, str, "") {
    586 //     if (arg == str) return true;
    587 //
    588 //     *result_listener << "the difference: "
    589 ///                     << DiffStrings(str, arg);
    590 //     return false;
    591 //   }
    592 //
    593 // Overloading Matchers
    594 // ====================
    595 //
    596 // You can overload matchers with different numbers of parameters:
    597 //
    598 //   MATCHER_P(Blah, a, description_string1) { ... }
    599 //   MATCHER_P2(Blah, a, b, description_string2) { ... }
    600 //
    601 // Caveats
    602 // =======
    603 //
    604 // When defining a new matcher, you should also consider implementing
    605 // MatcherInterface or using MakePolymorphicMatcher().  These
    606 // approaches require more work than the MATCHER* macros, but also
    607 // give you more control on the types of the value being matched and
    608 // the matcher parameters, which may leads to better compiler error
    609 // messages when the matcher is used wrong.  They also allow
    610 // overloading matchers based on parameter types (as opposed to just
    611 // based on the number of parameters).
    612 //
    613 // MATCHER*() can only be used in a namespace scope.  The reason is
    614 // that C++ doesn't yet allow function-local types to be used to
    615 // instantiate templates.  The up-coming C++0x standard will fix this.
    616 // Once that's done, we'll consider supporting using MATCHER*() inside
    617 // a function.
    618 //
    619 // More Information
    620 // ================
    621 //
    622 // To learn more about using these macros, please search for 'MATCHER'
    623 // on http://code.google.com/p/googlemock/wiki/CookBook.
    624 
    625 $range i 0..n
    626 $for i
    627 
    628 [[
    629 $var macro_name = [[$if i==0 [[MATCHER]] $elif i==1 [[MATCHER_P]]
    630                                          $else [[MATCHER_P$i]]]]
    631 $var class_name = [[name##Matcher[[$if i==0 [[]] $elif i==1 [[P]]
    632                                                  $else [[P$i]]]]]]
    633 $range j 0..i-1
    634 $var template = [[$if i==0 [[]] $else [[
    635 
    636   template <$for j, [[typename p$j##_type]]>\
    637 ]]]]
    638 $var ctor_param_list = [[$for j, [[p$j##_type gmock_p$j]]]]
    639 $var impl_ctor_param_list = [[$for j, [[p$j##_type gmock_p$j]]]]
    640 $var impl_inits = [[$if i==0 [[]] $else [[ : $for j, [[p$j(gmock_p$j)]]]]]]
    641 $var inits = [[$if i==0 [[]] $else [[ : $for j, [[p$j(gmock_p$j)]]]]]]
    642 $var params = [[$for j, [[p$j]]]]
    643 $var param_types = [[$if i==0 [[]] $else [[<$for j, [[p$j##_type]]>]]]]
    644 $var param_types_and_names = [[$for j, [[p$j##_type p$j]]]]
    645 $var param_field_decls = [[$for j
    646 [[
    647 
    648       p$j##_type p$j;\
    649 ]]]]
    650 $var param_field_decls2 = [[$for j
    651 [[
    652 
    653     p$j##_type p$j;\
    654 ]]]]
    655 
    656 #define $macro_name(name$for j [[, p$j]], description)\$template
    657   class $class_name {\
    658    public:\
    659     template <typename arg_type>\
    660     class gmock_Impl : public ::testing::MatcherInterface<arg_type> {\
    661      public:\
    662       [[$if i==1 [[explicit ]]]]gmock_Impl($impl_ctor_param_list)\
    663           $impl_inits {}\
    664       virtual bool MatchAndExplain(\
    665           arg_type arg, ::testing::MatchResultListener* result_listener) const;\
    666       virtual void DescribeTo(::std::ostream* gmock_os) const {\
    667         *gmock_os << FormatDescription(false);\
    668       }\
    669       virtual void DescribeNegationTo(::std::ostream* gmock_os) const {\
    670         *gmock_os << FormatDescription(true);\
    671       }\$param_field_decls
    672      private:\
    673       ::testing::internal::string FormatDescription(bool negation) const {\
    674         const ::testing::internal::string gmock_description = (description);\
    675         if (!gmock_description.empty())\
    676           return gmock_description;\
    677         return ::testing::internal::FormatMatcherDescription(\
    678             negation, #name, \
    679             ::testing::internal::UniversalTersePrintTupleFieldsToStrings(\
    680                 ::std::tr1::tuple<$for j, [[p$j##_type]]>($for j, [[p$j]])));\
    681       }\
    682       GTEST_DISALLOW_ASSIGN_(gmock_Impl);\
    683     };\
    684     template <typename arg_type>\
    685     operator ::testing::Matcher<arg_type>() const {\
    686       return ::testing::Matcher<arg_type>(\
    687           new gmock_Impl<arg_type>($params));\
    688     }\
    689     $class_name($ctor_param_list)$inits {\
    690     }\$param_field_decls2
    691    private:\
    692     GTEST_DISALLOW_ASSIGN_($class_name);\
    693   };\$template
    694   inline $class_name$param_types name($param_types_and_names) {\
    695     return $class_name$param_types($params);\
    696   }\$template
    697   template <typename arg_type>\
    698   bool $class_name$param_types::gmock_Impl<arg_type>::MatchAndExplain(\
    699       arg_type arg, \
    700       ::testing::MatchResultListener* result_listener GTEST_ATTRIBUTE_UNUSED_)\
    701           const
    702 ]]
    703 
    704 
    705 #endif  // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_MATCHERS_H_
    706