1 // Copyright 2008, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
31 // Implements class templates NiceMock, NaggyMock, and StrictMock.
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.
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
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.
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
61 #ifndef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_
62 #define GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_
64 #include <type_traits>
66 #include "gmock/gmock-spec-builders.h"
67 #include "gmock/internal/gmock-port.h"
70 template <class MockClass>
72 template <class MockClass>
74 template <class MockClass>
79 std::true_type StrictnessModifierProbe(const NiceMock<T>&);
81 std::true_type StrictnessModifierProbe(const NaggyMock<T>&);
83 std::true_type StrictnessModifierProbe(const StrictMock<T>&);
84 std::false_type StrictnessModifierProbe(...);
87 constexpr bool HasStrictnessModifier() {
88 return decltype(StrictnessModifierProbe(std::declval<const T&>()))::value;
91 // Base classes that register and deregister with testing::Mock to alter the
92 // default behavior around uninteresting calls. Inheriting from one of these
93 // classes first and then MockClass ensures the MockClass constructor is run
94 // after registration, and that the MockClass destructor runs before
95 // deregistration. This guarantees that MockClass's constructor and destructor
96 // run with the same level of strictness as its instance methods.
98 #if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW && \
99 (defined(_MSC_VER) || defined(__clang__))
100 // We need to mark these classes with this declspec to ensure that
101 // the empty base class optimization is performed.
102 #define GTEST_INTERNAL_EMPTY_BASE_CLASS __declspec(empty_bases)
104 #define GTEST_INTERNAL_EMPTY_BASE_CLASS
107 template <typename Base>
110 NiceMockImpl() { ::testing::Mock::AllowUninterestingCalls(this); }
112 ~NiceMockImpl() { ::testing::Mock::UnregisterCallReaction(this); }
115 template <typename Base>
116 class NaggyMockImpl {
118 NaggyMockImpl() { ::testing::Mock::WarnUninterestingCalls(this); }
120 ~NaggyMockImpl() { ::testing::Mock::UnregisterCallReaction(this); }
123 template <typename Base>
124 class StrictMockImpl {
126 StrictMockImpl() { ::testing::Mock::FailUninterestingCalls(this); }
128 ~StrictMockImpl() { ::testing::Mock::UnregisterCallReaction(this); }
131 } // namespace internal
133 template <class MockClass>
134 class GTEST_INTERNAL_EMPTY_BASE_CLASS NiceMock
135 : private internal::NiceMockImpl<MockClass>,
138 static_assert(!internal::HasStrictnessModifier<MockClass>(),
139 "Can't apply NiceMock to a class hierarchy that already has a "
140 "strictness modifier. See "
141 "https://google.github.io/googletest/"
142 "gmock_cook_book.html#NiceStrictNaggy");
143 NiceMock() : MockClass() {
144 static_assert(sizeof(*this) == sizeof(MockClass),
145 "The impl subclass shouldn't introduce any padding");
148 // Ideally, we would inherit base class's constructors through a using
149 // declaration, which would preserve their visibility. However, many existing
150 // tests rely on the fact that current implementation reexports protected
151 // constructors as public. These tests would need to be cleaned up first.
153 // Single argument constructor is special-cased so that it can be
155 template <typename A>
156 explicit NiceMock(A&& arg) : MockClass(std::forward<A>(arg)) {
157 static_assert(sizeof(*this) == sizeof(MockClass),
158 "The impl subclass shouldn't introduce any padding");
161 template <typename TArg1, typename TArg2, typename... An>
162 NiceMock(TArg1&& arg1, TArg2&& arg2, An&&... args)
163 : MockClass(std::forward<TArg1>(arg1), std::forward<TArg2>(arg2),
164 std::forward<An>(args)...) {
165 static_assert(sizeof(*this) == sizeof(MockClass),
166 "The impl subclass shouldn't introduce any padding");
170 GTEST_DISALLOW_COPY_AND_ASSIGN_(NiceMock);
173 template <class MockClass>
174 class GTEST_INTERNAL_EMPTY_BASE_CLASS NaggyMock
175 : private internal::NaggyMockImpl<MockClass>,
177 static_assert(!internal::HasStrictnessModifier<MockClass>(),
178 "Can't apply NaggyMock to a class hierarchy that already has a "
179 "strictness modifier. See "
180 "https://google.github.io/googletest/"
181 "gmock_cook_book.html#NiceStrictNaggy");
184 NaggyMock() : MockClass() {
185 static_assert(sizeof(*this) == sizeof(MockClass),
186 "The impl subclass shouldn't introduce any padding");
189 // Ideally, we would inherit base class's constructors through a using
190 // declaration, which would preserve their visibility. However, many existing
191 // tests rely on the fact that current implementation reexports protected
192 // constructors as public. These tests would need to be cleaned up first.
194 // Single argument constructor is special-cased so that it can be
196 template <typename A>
197 explicit NaggyMock(A&& arg) : MockClass(std::forward<A>(arg)) {
198 static_assert(sizeof(*this) == sizeof(MockClass),
199 "The impl subclass shouldn't introduce any padding");
202 template <typename TArg1, typename TArg2, typename... An>
203 NaggyMock(TArg1&& arg1, TArg2&& arg2, An&&... args)
204 : MockClass(std::forward<TArg1>(arg1), std::forward<TArg2>(arg2),
205 std::forward<An>(args)...) {
206 static_assert(sizeof(*this) == sizeof(MockClass),
207 "The impl subclass shouldn't introduce any padding");
211 GTEST_DISALLOW_COPY_AND_ASSIGN_(NaggyMock);
214 template <class MockClass>
215 class GTEST_INTERNAL_EMPTY_BASE_CLASS StrictMock
216 : private internal::StrictMockImpl<MockClass>,
220 !internal::HasStrictnessModifier<MockClass>(),
221 "Can't apply StrictMock to a class hierarchy that already has a "
222 "strictness modifier. See "
223 "https://google.github.io/googletest/"
224 "gmock_cook_book.html#NiceStrictNaggy");
225 StrictMock() : MockClass() {
226 static_assert(sizeof(*this) == sizeof(MockClass),
227 "The impl subclass shouldn't introduce any padding");
230 // Ideally, we would inherit base class's constructors through a using
231 // declaration, which would preserve their visibility. However, many existing
232 // tests rely on the fact that current implementation reexports protected
233 // constructors as public. These tests would need to be cleaned up first.
235 // Single argument constructor is special-cased so that it can be
237 template <typename A>
238 explicit StrictMock(A&& arg) : MockClass(std::forward<A>(arg)) {
239 static_assert(sizeof(*this) == sizeof(MockClass),
240 "The impl subclass shouldn't introduce any padding");
243 template <typename TArg1, typename TArg2, typename... An>
244 StrictMock(TArg1&& arg1, TArg2&& arg2, An&&... args)
245 : MockClass(std::forward<TArg1>(arg1), std::forward<TArg2>(arg2),
246 std::forward<An>(args)...) {
247 static_assert(sizeof(*this) == sizeof(MockClass),
248 "The impl subclass shouldn't introduce any padding");
252 GTEST_DISALLOW_COPY_AND_ASSIGN_(StrictMock);
255 #undef GTEST_INTERNAL_EMPTY_BASE_CLASS
257 } // namespace testing
259 #endif // GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_