File indexing completed on 2025-02-21 10:03:07
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063 #ifndef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_
0064 #define GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_
0065
0066 #include <cstdint>
0067 #include <type_traits>
0068
0069 #include "gmock/gmock-spec-builders.h"
0070 #include "gmock/internal/gmock-port.h"
0071
0072 namespace testing {
0073 template <class MockClass>
0074 class NiceMock;
0075 template <class MockClass>
0076 class NaggyMock;
0077 template <class MockClass>
0078 class StrictMock;
0079
0080 namespace internal {
0081 template <typename T>
0082 std::true_type StrictnessModifierProbe(const NiceMock<T>&);
0083 template <typename T>
0084 std::true_type StrictnessModifierProbe(const NaggyMock<T>&);
0085 template <typename T>
0086 std::true_type StrictnessModifierProbe(const StrictMock<T>&);
0087 std::false_type StrictnessModifierProbe(...);
0088
0089 template <typename T>
0090 constexpr bool HasStrictnessModifier() {
0091 return decltype(StrictnessModifierProbe(std::declval<const T&>()))::value;
0092 }
0093
0094
0095
0096
0097
0098
0099
0100
0101 #if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW && \
0102 (defined(_MSC_VER) || defined(__clang__))
0103
0104
0105 #define GTEST_INTERNAL_EMPTY_BASE_CLASS __declspec(empty_bases)
0106 #else
0107 #define GTEST_INTERNAL_EMPTY_BASE_CLASS
0108 #endif
0109
0110 template <typename Base>
0111 class NiceMockImpl {
0112 public:
0113 NiceMockImpl() {
0114 ::testing::Mock::AllowUninterestingCalls(reinterpret_cast<uintptr_t>(this));
0115 }
0116
0117 ~NiceMockImpl() {
0118 ::testing::Mock::UnregisterCallReaction(reinterpret_cast<uintptr_t>(this));
0119 }
0120 };
0121
0122 template <typename Base>
0123 class NaggyMockImpl {
0124 public:
0125 NaggyMockImpl() {
0126 ::testing::Mock::WarnUninterestingCalls(reinterpret_cast<uintptr_t>(this));
0127 }
0128
0129 ~NaggyMockImpl() {
0130 ::testing::Mock::UnregisterCallReaction(reinterpret_cast<uintptr_t>(this));
0131 }
0132 };
0133
0134 template <typename Base>
0135 class StrictMockImpl {
0136 public:
0137 StrictMockImpl() {
0138 ::testing::Mock::FailUninterestingCalls(reinterpret_cast<uintptr_t>(this));
0139 }
0140
0141 ~StrictMockImpl() {
0142 ::testing::Mock::UnregisterCallReaction(reinterpret_cast<uintptr_t>(this));
0143 }
0144 };
0145
0146 }
0147
0148 template <class MockClass>
0149 class GTEST_INTERNAL_EMPTY_BASE_CLASS NiceMock
0150 : private internal::NiceMockImpl<MockClass>,
0151 public MockClass {
0152 public:
0153 static_assert(!internal::HasStrictnessModifier<MockClass>(),
0154 "Can't apply NiceMock to a class hierarchy that already has a "
0155 "strictness modifier. See "
0156 "https://google.github.io/googletest/"
0157 "gmock_cook_book.html#NiceStrictNaggy");
0158 NiceMock() : MockClass() {
0159 static_assert(sizeof(*this) == sizeof(MockClass),
0160 "The impl subclass shouldn't introduce any padding");
0161 }
0162
0163
0164
0165
0166
0167
0168
0169
0170 template <typename A>
0171 explicit NiceMock(A&& arg) : MockClass(std::forward<A>(arg)) {
0172 static_assert(sizeof(*this) == sizeof(MockClass),
0173 "The impl subclass shouldn't introduce any padding");
0174 }
0175
0176 template <typename TArg1, typename TArg2, typename... An>
0177 NiceMock(TArg1&& arg1, TArg2&& arg2, An&&... args)
0178 : MockClass(std::forward<TArg1>(arg1), std::forward<TArg2>(arg2),
0179 std::forward<An>(args)...) {
0180 static_assert(sizeof(*this) == sizeof(MockClass),
0181 "The impl subclass shouldn't introduce any padding");
0182 }
0183
0184 private:
0185 NiceMock(const NiceMock&) = delete;
0186 NiceMock& operator=(const NiceMock&) = delete;
0187 };
0188
0189 template <class MockClass>
0190 class GTEST_INTERNAL_EMPTY_BASE_CLASS NaggyMock
0191 : private internal::NaggyMockImpl<MockClass>,
0192 public MockClass {
0193 static_assert(!internal::HasStrictnessModifier<MockClass>(),
0194 "Can't apply NaggyMock to a class hierarchy that already has a "
0195 "strictness modifier. See "
0196 "https://google.github.io/googletest/"
0197 "gmock_cook_book.html#NiceStrictNaggy");
0198
0199 public:
0200 NaggyMock() : MockClass() {
0201 static_assert(sizeof(*this) == sizeof(MockClass),
0202 "The impl subclass shouldn't introduce any padding");
0203 }
0204
0205
0206
0207
0208
0209
0210
0211
0212 template <typename A>
0213 explicit NaggyMock(A&& arg) : MockClass(std::forward<A>(arg)) {
0214 static_assert(sizeof(*this) == sizeof(MockClass),
0215 "The impl subclass shouldn't introduce any padding");
0216 }
0217
0218 template <typename TArg1, typename TArg2, typename... An>
0219 NaggyMock(TArg1&& arg1, TArg2&& arg2, An&&... args)
0220 : MockClass(std::forward<TArg1>(arg1), std::forward<TArg2>(arg2),
0221 std::forward<An>(args)...) {
0222 static_assert(sizeof(*this) == sizeof(MockClass),
0223 "The impl subclass shouldn't introduce any padding");
0224 }
0225
0226 private:
0227 NaggyMock(const NaggyMock&) = delete;
0228 NaggyMock& operator=(const NaggyMock&) = delete;
0229 };
0230
0231 template <class MockClass>
0232 class GTEST_INTERNAL_EMPTY_BASE_CLASS StrictMock
0233 : private internal::StrictMockImpl<MockClass>,
0234 public MockClass {
0235 public:
0236 static_assert(
0237 !internal::HasStrictnessModifier<MockClass>(),
0238 "Can't apply StrictMock to a class hierarchy that already has a "
0239 "strictness modifier. See "
0240 "https://google.github.io/googletest/"
0241 "gmock_cook_book.html#NiceStrictNaggy");
0242 StrictMock() : MockClass() {
0243 static_assert(sizeof(*this) == sizeof(MockClass),
0244 "The impl subclass shouldn't introduce any padding");
0245 }
0246
0247
0248
0249
0250
0251
0252
0253
0254 template <typename A>
0255 explicit StrictMock(A&& arg) : MockClass(std::forward<A>(arg)) {
0256 static_assert(sizeof(*this) == sizeof(MockClass),
0257 "The impl subclass shouldn't introduce any padding");
0258 }
0259
0260 template <typename TArg1, typename TArg2, typename... An>
0261 StrictMock(TArg1&& arg1, TArg2&& arg2, An&&... args)
0262 : MockClass(std::forward<TArg1>(arg1), std::forward<TArg2>(arg2),
0263 std::forward<An>(args)...) {
0264 static_assert(sizeof(*this) == sizeof(MockClass),
0265 "The impl subclass shouldn't introduce any padding");
0266 }
0267
0268 private:
0269 StrictMock(const StrictMock&) = delete;
0270 StrictMock& operator=(const StrictMock&) = delete;
0271 };
0272
0273 #undef GTEST_INTERNAL_EMPTY_BASE_CLASS
0274
0275 }
0276
0277 #endif