1 // Copyright 2008, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 
31 // Implements class templates NiceMock, NaggyMock, and StrictMock.
32 //
33 // Given a mock class MockFoo that is created using Google Mock,
34 // NiceMock<MockFoo> is a subclass of MockFoo that allows
35 // uninteresting calls (i.e. calls to mock methods that have no
36 // EXPECT_CALL specs), NaggyMock<MockFoo> is a subclass of MockFoo
37 // that prints a warning when an uninteresting call occurs, and
38 // StrictMock<MockFoo> is a subclass of MockFoo that treats all
39 // uninteresting calls as errors.
40 //
41 // Currently a mock is naggy by default, so MockFoo and
42 // NaggyMock<MockFoo> behave like the same.  However, we will soon
43 // switch the default behavior of mocks to be nice, as that in general
44 // leads to more maintainable tests.  When that happens, MockFoo will
45 // stop behaving like NaggyMock<MockFoo> and start behaving like
46 // NiceMock<MockFoo>.
47 //
48 // NiceMock, NaggyMock, and StrictMock "inherit" the constructors of
49 // their respective base class.  Therefore you can write
50 // NiceMock<MockFoo>(5, "a") to construct a nice mock where MockFoo
51 // has a constructor that accepts (int, const char*), for example.
52 //
53 // A known limitation is that NiceMock<MockFoo>, NaggyMock<MockFoo>,
54 // and StrictMock<MockFoo> only works for mock methods defined using
55 // the MOCK_METHOD* family of macros DIRECTLY in the MockFoo class.
56 // If a mock method is defined in a base class of MockFoo, the "nice"
57 // or "strict" modifier may not affect it, depending on the compiler.
58 // In particular, nesting NiceMock, NaggyMock, and StrictMock is NOT
59 // supported.
60 
61 // GOOGLETEST_CM0002 DO NOT DELETE
62 
63 #ifndef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_
64 #define GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_
65 
66 #include <type_traits>
67 
68 #include "gmock/gmock-spec-builders.h"
69 #include "gmock/internal/gmock-port.h"
70 
71 namespace testing {
72 template <class MockClass>
73 class NiceMock;
74 template <class MockClass>
75 class NaggyMock;
76 template <class MockClass>
77 class StrictMock;
78 
79 namespace internal {
80 template <typename T>
81 std::true_type StrictnessModifierProbe(const NiceMock<T>&);
82 template <typename T>
83 std::true_type StrictnessModifierProbe(const NaggyMock<T>&);
84 template <typename T>
85 std::true_type StrictnessModifierProbe(const StrictMock<T>&);
86 std::false_type StrictnessModifierProbe(...);
87 
88 template <typename T>
HasStrictnessModifier()89 constexpr bool HasStrictnessModifier() {
90   return decltype(StrictnessModifierProbe(std::declval<const T&>()))::value;
91 }
92 
93 // Base classes that register and deregister with testing::Mock to alter the
94 // default behavior around uninteresting calls. Inheriting from one of these
95 // classes first and then MockClass ensures the MockClass constructor is run
96 // after registration, and that the MockClass destructor runs before
97 // deregistration. This guarantees that MockClass's constructor and destructor
98 // run with the same level of strictness as its instance methods.
99 
100 #if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW && \
101     (defined(_MSC_VER) || defined(__clang__))
102 // We need to mark these classes with this declspec to ensure that
103 // the empty base class optimization is performed.
104 #define GTEST_INTERNAL_EMPTY_BASE_CLASS __declspec(empty_bases)
105 #else
106 #define GTEST_INTERNAL_EMPTY_BASE_CLASS
107 #endif
108 
109 template <typename Base>
110 class NiceMockImpl {
111  public:
NiceMockImpl()112   NiceMockImpl() { ::testing::Mock::AllowUninterestingCalls(this); }
113 
~NiceMockImpl()114   ~NiceMockImpl() { ::testing::Mock::UnregisterCallReaction(this); }
115 };
116 
117 template <typename Base>
118 class NaggyMockImpl {
119  public:
NaggyMockImpl()120   NaggyMockImpl() { ::testing::Mock::WarnUninterestingCalls(this); }
121 
~NaggyMockImpl()122   ~NaggyMockImpl() { ::testing::Mock::UnregisterCallReaction(this); }
123 };
124 
125 template <typename Base>
126 class StrictMockImpl {
127  public:
StrictMockImpl()128   StrictMockImpl() { ::testing::Mock::FailUninterestingCalls(this); }
129 
~StrictMockImpl()130   ~StrictMockImpl() { ::testing::Mock::UnregisterCallReaction(this); }
131 };
132 
133 }  // namespace internal
134 
135 template <class MockClass>
136 class GTEST_INTERNAL_EMPTY_BASE_CLASS NiceMock
137     : private internal::NiceMockImpl<MockClass>,
138       public MockClass {
139  public:
140   static_assert(!internal::HasStrictnessModifier<MockClass>(),
141                 "Can't apply NiceMock to a class hierarchy that already has a "
142                 "strictness modifier. See "
143                 "https://google.github.io/googletest/"
144                 "gmock_cook_book.html#NiceStrictNaggy");
NiceMock()145   NiceMock() : MockClass() {
146     static_assert(sizeof(*this) == sizeof(MockClass),
147                   "The impl subclass shouldn't introduce any padding");
148   }
149 
150   // Ideally, we would inherit base class's constructors through a using
151   // declaration, which would preserve their visibility. However, many existing
152   // tests rely on the fact that current implementation reexports protected
153   // constructors as public. These tests would need to be cleaned up first.
154 
155   // Single argument constructor is special-cased so that it can be
156   // made explicit.
157   template <typename A>
NiceMock(A && arg)158   explicit NiceMock(A&& arg) : MockClass(std::forward<A>(arg)) {
159     static_assert(sizeof(*this) == sizeof(MockClass),
160                   "The impl subclass shouldn't introduce any padding");
161   }
162 
163   template <typename TArg1, typename TArg2, typename... An>
NiceMock(TArg1 && arg1,TArg2 && arg2,An &&...args)164   NiceMock(TArg1&& arg1, TArg2&& arg2, An&&... args)
165       : MockClass(std::forward<TArg1>(arg1), std::forward<TArg2>(arg2),
166                   std::forward<An>(args)...) {
167     static_assert(sizeof(*this) == sizeof(MockClass),
168                   "The impl subclass shouldn't introduce any padding");
169   }
170 
171  private:
172   GTEST_DISALLOW_COPY_AND_ASSIGN_(NiceMock);
173 };
174 
175 template <class MockClass>
176 class GTEST_INTERNAL_EMPTY_BASE_CLASS NaggyMock
177     : private internal::NaggyMockImpl<MockClass>,
178       public MockClass {
179   static_assert(!internal::HasStrictnessModifier<MockClass>(),
180                 "Can't apply NaggyMock to a class hierarchy that already has a "
181                 "strictness modifier. See "
182                 "https://google.github.io/googletest/"
183                 "gmock_cook_book.html#NiceStrictNaggy");
184 
185  public:
NaggyMock()186   NaggyMock() : MockClass() {
187     static_assert(sizeof(*this) == sizeof(MockClass),
188                   "The impl subclass shouldn't introduce any padding");
189   }
190 
191   // Ideally, we would inherit base class's constructors through a using
192   // declaration, which would preserve their visibility. However, many existing
193   // tests rely on the fact that current implementation reexports protected
194   // constructors as public. These tests would need to be cleaned up first.
195 
196   // Single argument constructor is special-cased so that it can be
197   // made explicit.
198   template <typename A>
NaggyMock(A && arg)199   explicit NaggyMock(A&& arg) : MockClass(std::forward<A>(arg)) {
200     static_assert(sizeof(*this) == sizeof(MockClass),
201                   "The impl subclass shouldn't introduce any padding");
202   }
203 
204   template <typename TArg1, typename TArg2, typename... An>
NaggyMock(TArg1 && arg1,TArg2 && arg2,An &&...args)205   NaggyMock(TArg1&& arg1, TArg2&& arg2, An&&... args)
206       : MockClass(std::forward<TArg1>(arg1), std::forward<TArg2>(arg2),
207                   std::forward<An>(args)...) {
208     static_assert(sizeof(*this) == sizeof(MockClass),
209                   "The impl subclass shouldn't introduce any padding");
210   }
211 
212  private:
213   GTEST_DISALLOW_COPY_AND_ASSIGN_(NaggyMock);
214 };
215 
216 template <class MockClass>
217 class GTEST_INTERNAL_EMPTY_BASE_CLASS StrictMock
218     : private internal::StrictMockImpl<MockClass>,
219       public MockClass {
220  public:
221   static_assert(
222       !internal::HasStrictnessModifier<MockClass>(),
223       "Can't apply StrictMock to a class hierarchy that already has a "
224       "strictness modifier. See "
225       "https://google.github.io/googletest/"
226       "gmock_cook_book.html#NiceStrictNaggy");
StrictMock()227   StrictMock() : MockClass() {
228     static_assert(sizeof(*this) == sizeof(MockClass),
229                   "The impl subclass shouldn't introduce any padding");
230   }
231 
232   // Ideally, we would inherit base class's constructors through a using
233   // declaration, which would preserve their visibility. However, many existing
234   // tests rely on the fact that current implementation reexports protected
235   // constructors as public. These tests would need to be cleaned up first.
236 
237   // Single argument constructor is special-cased so that it can be
238   // made explicit.
239   template <typename A>
StrictMock(A && arg)240   explicit StrictMock(A&& arg) : MockClass(std::forward<A>(arg)) {
241     static_assert(sizeof(*this) == sizeof(MockClass),
242                   "The impl subclass shouldn't introduce any padding");
243   }
244 
245   template <typename TArg1, typename TArg2, typename... An>
StrictMock(TArg1 && arg1,TArg2 && arg2,An &&...args)246   StrictMock(TArg1&& arg1, TArg2&& arg2, An&&... args)
247       : MockClass(std::forward<TArg1>(arg1), std::forward<TArg2>(arg2),
248                   std::forward<An>(args)...) {
249     static_assert(sizeof(*this) == sizeof(MockClass),
250                   "The impl subclass shouldn't introduce any padding");
251   }
252 
253  private:
254   GTEST_DISALLOW_COPY_AND_ASSIGN_(StrictMock);
255 };
256 
257 #undef GTEST_INTERNAL_EMPTY_BASE_CLASS
258 
259 }  // namespace testing
260 
261 #endif  // GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_
262