Update bundled GoogleTest to current HEAD
[alexxy/gromacs.git] / src / external / googletest / googlemock / include / gmock / 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 #ifndef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_
62 #define GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_
63
64 #include <type_traits>
65
66 #include "gmock/gmock-spec-builders.h"
67 #include "gmock/internal/gmock-port.h"
68
69 namespace testing {
70 template <class MockClass>
71 class NiceMock;
72 template <class MockClass>
73 class NaggyMock;
74 template <class MockClass>
75 class StrictMock;
76
77 namespace internal {
78 template <typename T>
79 std::true_type StrictnessModifierProbe(const NiceMock<T>&);
80 template <typename T>
81 std::true_type StrictnessModifierProbe(const NaggyMock<T>&);
82 template <typename T>
83 std::true_type StrictnessModifierProbe(const StrictMock<T>&);
84 std::false_type StrictnessModifierProbe(...);
85
86 template <typename T>
87 constexpr bool HasStrictnessModifier() {
88   return decltype(StrictnessModifierProbe(std::declval<const T&>()))::value;
89 }
90
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.
97
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)
103 #else
104 #define GTEST_INTERNAL_EMPTY_BASE_CLASS
105 #endif
106
107 template <typename Base>
108 class NiceMockImpl {
109  public:
110   NiceMockImpl() { ::testing::Mock::AllowUninterestingCalls(this); }
111
112   ~NiceMockImpl() { ::testing::Mock::UnregisterCallReaction(this); }
113 };
114
115 template <typename Base>
116 class NaggyMockImpl {
117  public:
118   NaggyMockImpl() { ::testing::Mock::WarnUninterestingCalls(this); }
119
120   ~NaggyMockImpl() { ::testing::Mock::UnregisterCallReaction(this); }
121 };
122
123 template <typename Base>
124 class StrictMockImpl {
125  public:
126   StrictMockImpl() { ::testing::Mock::FailUninterestingCalls(this); }
127
128   ~StrictMockImpl() { ::testing::Mock::UnregisterCallReaction(this); }
129 };
130
131 }  // namespace internal
132
133 template <class MockClass>
134 class GTEST_INTERNAL_EMPTY_BASE_CLASS NiceMock
135     : private internal::NiceMockImpl<MockClass>,
136       public MockClass {
137  public:
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");
146   }
147
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.
152
153   // Single argument constructor is special-cased so that it can be
154   // made explicit.
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");
159   }
160
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");
167   }
168
169  private:
170   GTEST_DISALLOW_COPY_AND_ASSIGN_(NiceMock);
171 };
172
173 template <class MockClass>
174 class GTEST_INTERNAL_EMPTY_BASE_CLASS NaggyMock
175     : private internal::NaggyMockImpl<MockClass>,
176       public 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");
182
183  public:
184   NaggyMock() : MockClass() {
185     static_assert(sizeof(*this) == sizeof(MockClass),
186                   "The impl subclass shouldn't introduce any padding");
187   }
188
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.
193
194   // Single argument constructor is special-cased so that it can be
195   // made explicit.
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");
200   }
201
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");
208   }
209
210  private:
211   GTEST_DISALLOW_COPY_AND_ASSIGN_(NaggyMock);
212 };
213
214 template <class MockClass>
215 class GTEST_INTERNAL_EMPTY_BASE_CLASS StrictMock
216     : private internal::StrictMockImpl<MockClass>,
217       public MockClass {
218  public:
219   static_assert(
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");
228   }
229
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.
234
235   // Single argument constructor is special-cased so that it can be
236   // made explicit.
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");
241   }
242
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");
249   }
250
251  private:
252   GTEST_DISALLOW_COPY_AND_ASSIGN_(StrictMock);
253 };
254
255 #undef GTEST_INTERNAL_EMPTY_BASE_CLASS
256
257 }  // namespace testing
258
259 #endif  // GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_