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-nice-strict.h.
      4 $$
      5 $var n = 10  $$ The maximum arity we support.
      6 // Copyright 2008, Google Inc.
      7 // All rights reserved.
      8 //
      9 // Redistribution and use in source and binary forms, with or without
     10 // modification, are permitted provided that the following conditions are
     11 // met:
     12 //
     13 //     * Redistributions of source code must retain the above copyright
     14 // notice, this list of conditions and the following disclaimer.
     15 //     * Redistributions in binary form must reproduce the above
     16 // copyright notice, this list of conditions and the following disclaimer
     17 // in the documentation and/or other materials provided with the
     18 // distribution.
     19 //     * Neither the name of Google Inc. nor the names of its
     20 // contributors may be used to endorse or promote products derived from
     21 // this software without specific prior written permission.
     22 //
     23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     24 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     25 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     26 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     27 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     28 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     29 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     30 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     31 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     33 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34 //
     35 // Author: wan (a] google.com (Zhanyong Wan)
     36 
     37 // Implements class templates NiceMock, NaggyMock, and StrictMock.
     38 //
     39 // Given a mock class MockFoo that is created using Google Mock,
     40 // NiceMock<MockFoo> is a subclass of MockFoo that allows
     41 // uninteresting calls (i.e. calls to mock methods that have no
     42 // EXPECT_CALL specs), NaggyMock<MockFoo> is a subclass of MockFoo
     43 // that prints a warning when an uninteresting call occurs, and
     44 // StrictMock<MockFoo> is a subclass of MockFoo that treats all
     45 // uninteresting calls as errors.
     46 //
     47 // Currently a mock is naggy by default, so MockFoo and
     48 // NaggyMock<MockFoo> behave like the same.  However, we will soon
     49 // switch the default behavior of mocks to be nice, as that in general
     50 // leads to more maintainable tests.  When that happens, MockFoo will
     51 // stop behaving like NaggyMock<MockFoo> and start behaving like
     52 // NiceMock<MockFoo>.
     53 //
     54 // NiceMock, NaggyMock, and StrictMock "inherit" the constructors of
     55 // their respective base class, with up-to $n arguments.  Therefore
     56 // you can write NiceMock<MockFoo>(5, "a") to construct a nice mock
     57 // where MockFoo has a constructor that accepts (int, const char*),
     58 // for example.
     59 //
     60 // A known limitation is that NiceMock<MockFoo>, NaggyMock<MockFoo>,
     61 // and StrictMock<MockFoo> only works for mock methods defined using
     62 // the MOCK_METHOD* family of macros DIRECTLY in the MockFoo class.
     63 // If a mock method is defined in a base class of MockFoo, the "nice"
     64 // or "strict" modifier may not affect it, depending on the compiler.
     65 // In particular, nesting NiceMock, NaggyMock, and StrictMock is NOT
     66 // supported.
     67 //
     68 // Another known limitation is that the constructors of the base mock
     69 // cannot have arguments passed by non-const reference, which are
     70 // banned by the Google C++ style guide anyway.
     71 
     72 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_NICE_STRICT_H_
     73 #define GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_NICE_STRICT_H_
     74 
     75 #include "gmock/gmock-spec-builders.h"
     76 #include "gmock/internal/gmock-port.h"
     77 
     78 namespace testing {
     79 
     80 $range kind 0..2
     81 $for kind [[
     82 
     83 $var clazz=[[$if kind==0 [[NiceMock]]
     84              $elif kind==1 [[NaggyMock]]
     85              $else [[StrictMock]]]]
     86 
     87 $var method=[[$if kind==0 [[AllowUninterestingCalls]]
     88              $elif kind==1 [[WarnUninterestingCalls]]
     89              $else [[FailUninterestingCalls]]]]
     90 
     91 template <class MockClass>
     92 class $clazz : public MockClass {
     93  public:
     94   // We don't factor out the constructor body to a common method, as
     95   // we have to avoid a possible clash with members of MockClass.
     96   $clazz() {
     97     ::testing::Mock::$method(
     98         internal::ImplicitCast_<MockClass*>(this));
     99   }
    100 
    101   // C++ doesn't (yet) allow inheritance of constructors, so we have
    102   // to define it for each arity.
    103   template <typename A1>
    104   explicit $clazz(const A1& a1) : MockClass(a1) {
    105     ::testing::Mock::$method(
    106         internal::ImplicitCast_<MockClass*>(this));
    107   }
    108 
    109 $range i 2..n
    110 $for i [[
    111 $range j 1..i
    112   template <$for j, [[typename A$j]]>
    113   $clazz($for j, [[const A$j& a$j]]) : MockClass($for j, [[a$j]]) {
    114     ::testing::Mock::$method(
    115         internal::ImplicitCast_<MockClass*>(this));
    116   }
    117 
    118 
    119 ]]
    120   virtual ~$clazz() {
    121     ::testing::Mock::UnregisterCallReaction(
    122         internal::ImplicitCast_<MockClass*>(this));
    123   }
    124 
    125  private:
    126   GTEST_DISALLOW_COPY_AND_ASSIGN_($clazz);
    127 };
    128 
    129 ]]
    130 
    131 // The following specializations catch some (relatively more common)
    132 // user errors of nesting nice and strict mocks.  They do NOT catch
    133 // all possible errors.
    134 
    135 // These specializations are declared but not defined, as NiceMock,
    136 // NaggyMock, and StrictMock cannot be nested.
    137 
    138 template <typename MockClass>
    139 class NiceMock<NiceMock<MockClass> >;
    140 template <typename MockClass>
    141 class NiceMock<NaggyMock<MockClass> >;
    142 template <typename MockClass>
    143 class NiceMock<StrictMock<MockClass> >;
    144 
    145 template <typename MockClass>
    146 class NaggyMock<NiceMock<MockClass> >;
    147 template <typename MockClass>
    148 class NaggyMock<NaggyMock<MockClass> >;
    149 template <typename MockClass>
    150 class NaggyMock<StrictMock<MockClass> >;
    151 
    152 template <typename MockClass>
    153 class StrictMock<NiceMock<MockClass> >;
    154 template <typename MockClass>
    155 class StrictMock<NaggyMock<MockClass> >;
    156 template <typename MockClass>
    157 class StrictMock<StrictMock<MockClass> >;
    158 
    159 }  // namespace testing
    160 
    161 #endif  // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_NICE_STRICT_H_
    162