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