googletest/googlemock/include/gmock/gmock-nice-strict.h
Go to the documentation of this file.
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 // Implements class templates NiceMock, NaggyMock, and StrictMock.
31 //
32 // Given a mock class MockFoo that is created using Google Mock,
33 // NiceMock<MockFoo> is a subclass of MockFoo that allows
34 // uninteresting calls (i.e. calls to mock methods that have no
35 // EXPECT_CALL specs), NaggyMock<MockFoo> is a subclass of MockFoo
36 // that prints a warning when an uninteresting call occurs, and
37 // StrictMock<MockFoo> is a subclass of MockFoo that treats all
38 // uninteresting calls as errors.
39 //
40 // Currently a mock is naggy by default, so MockFoo and
41 // NaggyMock<MockFoo> behave like the same. However, we will soon
42 // switch the default behavior of mocks to be nice, as that in general
43 // leads to more maintainable tests. When that happens, MockFoo will
44 // stop behaving like NaggyMock<MockFoo> and start behaving like
45 // NiceMock<MockFoo>.
46 //
47 // NiceMock, NaggyMock, and StrictMock "inherit" the constructors of
48 // their respective base class. Therefore you can write
49 // NiceMock<MockFoo>(5, "a") to construct a nice mock where MockFoo
50 // has a constructor that accepts (int, const char*), for example.
51 //
52 // A known limitation is that NiceMock<MockFoo>, NaggyMock<MockFoo>,
53 // and StrictMock<MockFoo> only works for mock methods defined using
54 // the MOCK_METHOD* family of macros DIRECTLY in the MockFoo class.
55 // If a mock method is defined in a base class of MockFoo, the "nice"
56 // or "strict" modifier may not affect it, depending on the compiler.
57 // In particular, nesting NiceMock, NaggyMock, and StrictMock is NOT
58 // supported.
59 
60 // IWYU pragma: private, include "gmock/gmock.h"
61 // IWYU pragma: friend gmock/.*
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>&);
87 
88 template <typename T>
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>
111  public:
112  NiceMockImpl() { ::testing::Mock::AllowUninterestingCalls(this); }
113 
114  ~NiceMockImpl() { ::testing::Mock::UnregisterCallReaction(this); }
115 };
116 
117 template <typename Base>
119  public:
120  NaggyMockImpl() { ::testing::Mock::WarnUninterestingCalls(this); }
121 
122  ~NaggyMockImpl() { ::testing::Mock::UnregisterCallReaction(this); }
123 };
124 
125 template <typename Base>
127  public:
128  StrictMockImpl() { ::testing::Mock::FailUninterestingCalls(this); }
129 
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");
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>
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>
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:
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:
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>
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>
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:
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");
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>
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>
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:
255 };
256 
257 #undef GTEST_INTERNAL_EMPTY_BASE_CLASS
258 
259 } // namespace testing
260 
261 #endif // GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_
testing::StrictMock
Definition: bloaty/third_party/googletest/googlemock/include/gmock/gmock-nice-strict.h:148
testing
Definition: aws_request_signer_test.cc:25
testing::internal::NiceMockImpl::NiceMockImpl
NiceMockImpl()
Definition: googletest/googlemock/include/gmock/gmock-nice-strict.h:112
testing::internal::NaggyMockImpl
Definition: googletest/googlemock/include/gmock/gmock-nice-strict.h:118
testing::internal::HasStrictnessModifier
constexpr bool HasStrictnessModifier()
Definition: googletest/googlemock/include/gmock/gmock-nice-strict.h:89
google::protobuf.internal::true_type
integral_constant< bool, true > true_type
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/template_util.h:89
google::protobuf.internal::false_type
integral_constant< bool, false > false_type
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/template_util.h:90
testing::NiceMock::NiceMock
NiceMock(A &&arg)
Definition: googletest/googlemock/include/gmock/gmock-nice-strict.h:158
testing::internal::NaggyMockImpl::~NaggyMockImpl
~NaggyMockImpl()
Definition: googletest/googlemock/include/gmock/gmock-nice-strict.h:122
testing::StrictMock::StrictMock
StrictMock(A &&arg)
Definition: googletest/googlemock/include/gmock/gmock-nice-strict.h:240
GTEST_INTERNAL_EMPTY_BASE_CLASS
#define GTEST_INTERNAL_EMPTY_BASE_CLASS
Definition: googletest/googlemock/include/gmock/gmock-nice-strict.h:106
testing::internal::StrictMockImpl::~StrictMockImpl
~StrictMockImpl()
Definition: googletest/googlemock/include/gmock/gmock-nice-strict.h:130
testing::An
Matcher< T > An()
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8555
testing::NiceMock
Definition: bloaty/third_party/googletest/googlemock/include/gmock/gmock-nice-strict.h:72
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
testing::NaggyMock::NaggyMock
NaggyMock(A &&arg)
Definition: googletest/googlemock/include/gmock/gmock-nice-strict.h:199
testing::NaggyMock::NaggyMock
NaggyMock(TArg1 &&arg1, TArg2 &&arg2, An &&... args)
Definition: googletest/googlemock/include/gmock/gmock-nice-strict.h:205
testing::internal::StrictMockImpl
Definition: googletest/googlemock/include/gmock/gmock-nice-strict.h:126
testing::internal::NiceMockImpl
Definition: googletest/googlemock/include/gmock/gmock-nice-strict.h:110
arg
Definition: cmdline.cc:40
testing::NiceMock::NiceMock
NiceMock(TArg1 &&arg1, TArg2 &&arg2, An &&... args)
Definition: googletest/googlemock/include/gmock/gmock-nice-strict.h:164
testing::StrictMock::StrictMock
StrictMock()
Definition: googletest/googlemock/include/gmock/gmock-nice-strict.h:227
testing::internal::NiceMockImpl::~NiceMockImpl
~NiceMockImpl()
Definition: googletest/googlemock/include/gmock/gmock-nice-strict.h:114
GTEST_DISALLOW_COPY_AND_ASSIGN_
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
Definition: bloaty/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:683
value
const char * value
Definition: hpack_parser_table.cc:165
testing::NaggyMock
Definition: bloaty/third_party/googletest/googlemock/include/gmock/gmock-nice-strict.h:110
testing::NaggyMock::NaggyMock
NaggyMock()
Definition: googletest/googlemock/include/gmock/gmock-nice-strict.h:186
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
A
Definition: miscompile_with_no_unique_address_test.cc:23
testing::StrictMock::StrictMock
StrictMock(TArg1 &&arg1, TArg2 &&arg2, An &&... args)
Definition: googletest/googlemock/include/gmock/gmock-nice-strict.h:246
testing::internal::StrictMockImpl::StrictMockImpl
StrictMockImpl()
Definition: googletest/googlemock/include/gmock/gmock-nice-strict.h:128
internal
Definition: benchmark/test/output_test_helper.cc:20
testing::internal::StrictnessModifierProbe
std::true_type StrictnessModifierProbe(const NiceMock< T > &)
absl::forward
constexpr T && forward(absl::remove_reference_t< T > &t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:230
testing::NiceMock::NiceMock
NiceMock()
Definition: googletest/googlemock/include/gmock/gmock-nice-strict.h:145
testing::internal::NaggyMockImpl::NaggyMockImpl
NaggyMockImpl()
Definition: googletest/googlemock/include/gmock/gmock-nice-strict.h:120


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:36