Wizard
Software Engineering Project - Wizard
Loading...
Searching...
No Matches
gmock-nice-strict.h
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
71namespace testing {
72template <class MockClass>
73class NiceMock;
74template <class MockClass>
75class NaggyMock;
76template <class MockClass>
77class StrictMock;
78
79namespace internal {
80template <typename T>
81std::true_type StrictnessModifierProbe(const NiceMock<T>&);
82template <typename T>
83std::true_type StrictnessModifierProbe(const NaggyMock<T>&);
84template <typename T>
85std::true_type StrictnessModifierProbe(const StrictMock<T>&);
86std::false_type StrictnessModifierProbe(...);
87
88template <typename T>
89constexpr 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
109template <typename Base>
111 public:
112 NiceMockImpl() { ::testing::Mock::AllowUninterestingCalls(this); }
113
114 ~NiceMockImpl() { ::testing::Mock::UnregisterCallReaction(this); }
115};
116
117template <typename Base>
119 public:
120 NaggyMockImpl() { ::testing::Mock::WarnUninterestingCalls(this); }
121
122 ~NaggyMockImpl() { ::testing::Mock::UnregisterCallReaction(this); }
123};
124
125template <typename Base>
127 public:
128 StrictMockImpl() { ::testing::Mock::FailUninterestingCalls(this); }
129
130 ~StrictMockImpl() { ::testing::Mock::UnregisterCallReaction(this); }
131};
132
133} // namespace internal
134
135template <class MockClass>
136class GTEST_INTERNAL_EMPTY_BASE_CLASS NiceMock
137 : private internal::NiceMockImpl<MockClass>,
138 public MockClass {
139 public:
140 static_assert(
141 !internal::HasStrictnessModifier<MockClass>(),
142 "Can't apply NiceMock to a class hierarchy that already has a "
143 "strictness modifier. See "
144 "https://github.com/google/googletest/blob/master/googlemock/docs/"
145 "cook_book.md#the-nice-the-strict-and-the-naggy-nicestrictnaggy");
146 NiceMock() : MockClass() {
147 static_assert(sizeof(*this) == sizeof(MockClass),
148 "The impl subclass shouldn't introduce any padding");
149 }
150
151 // Ideally, we would inherit base class's constructors through a using
152 // declaration, which would preserve their visibility. However, many existing
153 // tests rely on the fact that current implementation reexports protected
154 // constructors as public. These tests would need to be cleaned up first.
155
156 // Single argument constructor is special-cased so that it can be
157 // made explicit.
158 template <typename A>
159 explicit NiceMock(A&& arg) : MockClass(std::forward<A>(arg)) {
160 static_assert(sizeof(*this) == sizeof(MockClass),
161 "The impl subclass shouldn't introduce any padding");
162 }
163
164 template <typename TArg1, typename TArg2, typename... An>
165 NiceMock(TArg1&& arg1, TArg2&& arg2, An&&... args)
166 : MockClass(std::forward<TArg1>(arg1), std::forward<TArg2>(arg2),
167 std::forward<An>(args)...) {
168 static_assert(sizeof(*this) == sizeof(MockClass),
169 "The impl subclass shouldn't introduce any padding");
170 }
171
172 private:
173 GTEST_DISALLOW_COPY_AND_ASSIGN_(NiceMock);
174};
175
176template <class MockClass>
177class GTEST_INTERNAL_EMPTY_BASE_CLASS NaggyMock
178 : private internal::NaggyMockImpl<MockClass>,
179 public MockClass {
180 static_assert(
181 !internal::HasStrictnessModifier<MockClass>(),
182 "Can't apply NaggyMock to a class hierarchy that already has a "
183 "strictness modifier. See "
184 "https://github.com/google/googletest/blob/master/googlemock/docs/"
185 "cook_book.md#the-nice-the-strict-and-the-naggy-nicestrictnaggy");
186
187 public:
188 NaggyMock() : MockClass() {
189 static_assert(sizeof(*this) == sizeof(MockClass),
190 "The impl subclass shouldn't introduce any padding");
191 }
192
193 // Ideally, we would inherit base class's constructors through a using
194 // declaration, which would preserve their visibility. However, many existing
195 // tests rely on the fact that current implementation reexports protected
196 // constructors as public. These tests would need to be cleaned up first.
197
198 // Single argument constructor is special-cased so that it can be
199 // made explicit.
200 template <typename A>
201 explicit NaggyMock(A&& arg) : MockClass(std::forward<A>(arg)) {
202 static_assert(sizeof(*this) == sizeof(MockClass),
203 "The impl subclass shouldn't introduce any padding");
204 }
205
206 template <typename TArg1, typename TArg2, typename... An>
207 NaggyMock(TArg1&& arg1, TArg2&& arg2, An&&... args)
208 : MockClass(std::forward<TArg1>(arg1), std::forward<TArg2>(arg2),
209 std::forward<An>(args)...) {
210 static_assert(sizeof(*this) == sizeof(MockClass),
211 "The impl subclass shouldn't introduce any padding");
212 }
213
214 private:
215 GTEST_DISALLOW_COPY_AND_ASSIGN_(NaggyMock);
216};
217
218template <class MockClass>
219class GTEST_INTERNAL_EMPTY_BASE_CLASS StrictMock
220 : private internal::StrictMockImpl<MockClass>,
221 public MockClass {
222 public:
223 static_assert(
224 !internal::HasStrictnessModifier<MockClass>(),
225 "Can't apply StrictMock to a class hierarchy that already has a "
226 "strictness modifier. See "
227 "https://github.com/google/googletest/blob/master/googlemock/docs/"
228 "cook_book.md#the-nice-the-strict-and-the-naggy-nicestrictnaggy");
229 StrictMock() : MockClass() {
230 static_assert(sizeof(*this) == sizeof(MockClass),
231 "The impl subclass shouldn't introduce any padding");
232 }
233
234 // Ideally, we would inherit base class's constructors through a using
235 // declaration, which would preserve their visibility. However, many existing
236 // tests rely on the fact that current implementation reexports protected
237 // constructors as public. These tests would need to be cleaned up first.
238
239 // Single argument constructor is special-cased so that it can be
240 // made explicit.
241 template <typename A>
242 explicit StrictMock(A&& arg) : MockClass(std::forward<A>(arg)) {
243 static_assert(sizeof(*this) == sizeof(MockClass),
244 "The impl subclass shouldn't introduce any padding");
245 }
246
247 template <typename TArg1, typename TArg2, typename... An>
248 StrictMock(TArg1&& arg1, TArg2&& arg2, An&&... args)
249 : MockClass(std::forward<TArg1>(arg1), std::forward<TArg2>(arg2),
250 std::forward<An>(args)...) {
251 static_assert(sizeof(*this) == sizeof(MockClass),
252 "The impl subclass shouldn't introduce any padding");
253 }
254
255 private:
256 GTEST_DISALLOW_COPY_AND_ASSIGN_(StrictMock);
257};
258
259#undef GTEST_INTERNAL_EMPTY_BASE_CLASS
260
261} // namespace testing
262
263#endif // GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_
Definition gmock-nice-strict.h:179
Definition gmock-nice-strict.h:138
Definition gmock-nice-strict.h:221
Definition gmock-nice-strict.h:118
Definition gmock-nice-strict.h:110
Definition gmock-nice-strict.h:126