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 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), and StrictMock<MockFoo> is a subclass of
     43 // MockFoo that treats all uninteresting calls as errors.
     44 //
     45 // NiceMock and StrictMock "inherits" the constructors of their
     46 // respective base class, with up-to $n arguments.  Therefore you can
     47 // write NiceMock<MockFoo>(5, "a") to construct a nice mock where
     48 // MockFoo has a constructor that accepts (int, const char*), for
     49 // example.
     50 //
     51 // A known limitation is that NiceMock<MockFoo> and
     52 // StrictMock<MockFoo> only works for mock methods defined using the
     53 // MOCK_METHOD* family of macros DIRECTLY in the MockFoo class.  If a
     54 // mock method is defined in a base class of MockFoo, the "nice" or
     55 // "strict" modifier may not affect it, depending on the compiler.  In
     56 // particular, nesting NiceMock and StrictMock is NOT supported.
     57 //
     58 // Another known limitation is that the constructors of the base mock
     59 // cannot have arguments passed by non-const reference, which are
     60 // banned by the Google C++ style guide anyway.
     61 
     62 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_NICE_STRICT_H_
     63 #define GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_NICE_STRICT_H_
     64 
     65 #include "gmock/gmock-spec-builders.h"
     66 #include "gmock/internal/gmock-port.h"
     67 
     68 namespace testing {
     69 
     70 template <class MockClass>
     71 class NiceMock : public MockClass {
     72  public:
     73   // We don't factor out the constructor body to a common method, as
     74   // we have to avoid a possible clash with members of MockClass.
     75   NiceMock() {
     76     ::testing::Mock::AllowUninterestingCalls(
     77         internal::ImplicitCast_<MockClass*>(this));
     78   }
     79 
     80   // C++ doesn't (yet) allow inheritance of constructors, so we have
     81   // to define it for each arity.
     82   template <typename A1>
     83   explicit NiceMock(const A1& a1) : MockClass(a1) {
     84     ::testing::Mock::AllowUninterestingCalls(
     85         internal::ImplicitCast_<MockClass*>(this));
     86   }
     87 
     88 $range i 2..n
     89 $for i [[
     90 $range j 1..i
     91   template <$for j, [[typename A$j]]>
     92   NiceMock($for j, [[const A$j& a$j]]) : MockClass($for j, [[a$j]]) {
     93     ::testing::Mock::AllowUninterestingCalls(
     94         internal::ImplicitCast_<MockClass*>(this));
     95   }
     96 
     97 
     98 ]]
     99   virtual ~NiceMock() {
    100     ::testing::Mock::UnregisterCallReaction(
    101         internal::ImplicitCast_<MockClass*>(this));
    102   }
    103 
    104  private:
    105   GTEST_DISALLOW_COPY_AND_ASSIGN_(NiceMock);
    106 };
    107 
    108 template <class MockClass>
    109 class StrictMock : public MockClass {
    110  public:
    111   // We don't factor out the constructor body to a common method, as
    112   // we have to avoid a possible clash with members of MockClass.
    113   StrictMock() {
    114     ::testing::Mock::FailUninterestingCalls(
    115         internal::ImplicitCast_<MockClass*>(this));
    116   }
    117 
    118   template <typename A1>
    119   explicit StrictMock(const A1& a1) : MockClass(a1) {
    120     ::testing::Mock::FailUninterestingCalls(
    121         internal::ImplicitCast_<MockClass*>(this));
    122   }
    123 
    124 $for i [[
    125 $range j 1..i
    126   template <$for j, [[typename A$j]]>
    127   StrictMock($for j, [[const A$j& a$j]]) : MockClass($for j, [[a$j]]) {
    128     ::testing::Mock::FailUninterestingCalls(
    129         internal::ImplicitCast_<MockClass*>(this));
    130   }
    131 
    132 
    133 ]]
    134   virtual ~StrictMock() {
    135     ::testing::Mock::UnregisterCallReaction(
    136         internal::ImplicitCast_<MockClass*>(this));
    137   }
    138 
    139  private:
    140   GTEST_DISALLOW_COPY_AND_ASSIGN_(StrictMock);
    141 };
    142 
    143 // The following specializations catch some (relatively more common)
    144 // user errors of nesting nice and strict mocks.  They do NOT catch
    145 // all possible errors.
    146 
    147 // These specializations are declared but not defined, as NiceMock and
    148 // StrictMock cannot be nested.
    149 template <typename MockClass>
    150 class NiceMock<NiceMock<MockClass> >;
    151 template <typename MockClass>
    152 class NiceMock<StrictMock<MockClass> >;
    153 template <typename MockClass>
    154 class StrictMock<NiceMock<MockClass> >;
    155 template <typename MockClass>
    156 class StrictMock<StrictMock<MockClass> >;
    157 
    158 }  // namespace testing
    159 
    160 #endif  // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_NICE_STRICT_H_
    161