gmock-nice-strict.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. // Implements class templates NiceMock, NaggyMock, and StrictMock.
  30. //
  31. // Given a mock class MockFoo that is created using Google Mock,
  32. // NiceMock<MockFoo> is a subclass of MockFoo that allows
  33. // uninteresting calls (i.e. calls to mock methods that have no
  34. // EXPECT_CALL specs), NaggyMock<MockFoo> is a subclass of MockFoo
  35. // that prints a warning when an uninteresting call occurs, and
  36. // StrictMock<MockFoo> is a subclass of MockFoo that treats all
  37. // uninteresting calls as errors.
  38. //
  39. // Currently a mock is naggy by default, so MockFoo and
  40. // NaggyMock<MockFoo> behave like the same. However, we will soon
  41. // switch the default behavior of mocks to be nice, as that in general
  42. // leads to more maintainable tests. When that happens, MockFoo will
  43. // stop behaving like NaggyMock<MockFoo> and start behaving like
  44. // NiceMock<MockFoo>.
  45. //
  46. // NiceMock, NaggyMock, and StrictMock "inherit" the constructors of
  47. // their respective base class. Therefore you can write
  48. // NiceMock<MockFoo>(5, "a") to construct a nice mock where MockFoo
  49. // has a constructor that accepts (int, const char*), for example.
  50. //
  51. // A known limitation is that NiceMock<MockFoo>, NaggyMock<MockFoo>,
  52. // and StrictMock<MockFoo> only works for mock methods defined using
  53. // the MOCK_METHOD* family of macros DIRECTLY in the MockFoo class.
  54. // If a mock method is defined in a base class of MockFoo, the "nice"
  55. // or "strict" modifier may not affect it, depending on the compiler.
  56. // In particular, nesting NiceMock, NaggyMock, and StrictMock is NOT
  57. // supported.
  58. // IWYU pragma: private, include "gmock/gmock.h"
  59. // IWYU pragma: friend gmock/.*
  60. #ifndef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_
  61. #define GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_
  62. #include <cstdint>
  63. #include <type_traits>
  64. #include "gmock/gmock-spec-builders.h"
  65. #include "gmock/internal/gmock-port.h"
  66. namespace testing {
  67. template <class MockClass>
  68. class NiceMock;
  69. template <class MockClass>
  70. class NaggyMock;
  71. template <class MockClass>
  72. class StrictMock;
  73. namespace internal {
  74. template <typename T>
  75. std::true_type StrictnessModifierProbe(const NiceMock<T>&);
  76. template <typename T>
  77. std::true_type StrictnessModifierProbe(const NaggyMock<T>&);
  78. template <typename T>
  79. std::true_type StrictnessModifierProbe(const StrictMock<T>&);
  80. std::false_type StrictnessModifierProbe(...);
  81. template <typename T>
  82. constexpr bool HasStrictnessModifier() {
  83. return decltype(StrictnessModifierProbe(std::declval<const T&>()))::value;
  84. }
  85. // Base classes that register and deregister with testing::Mock to alter the
  86. // default behavior around uninteresting calls. Inheriting from one of these
  87. // classes first and then MockClass ensures the MockClass constructor is run
  88. // after registration, and that the MockClass destructor runs before
  89. // deregistration. This guarantees that MockClass's constructor and destructor
  90. // run with the same level of strictness as its instance methods.
  91. #if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW && \
  92. (defined(_MSC_VER) || defined(__clang__))
  93. // We need to mark these classes with this declspec to ensure that
  94. // the empty base class optimization is performed.
  95. #define GTEST_INTERNAL_EMPTY_BASE_CLASS __declspec(empty_bases)
  96. #else
  97. #define GTEST_INTERNAL_EMPTY_BASE_CLASS
  98. #endif
  99. template <typename Base>
  100. class NiceMockImpl {
  101. public:
  102. NiceMockImpl() {
  103. ::testing::Mock::AllowUninterestingCalls(reinterpret_cast<uintptr_t>(this));
  104. }
  105. ~NiceMockImpl() {
  106. ::testing::Mock::UnregisterCallReaction(reinterpret_cast<uintptr_t>(this));
  107. }
  108. };
  109. template <typename Base>
  110. class NaggyMockImpl {
  111. public:
  112. NaggyMockImpl() {
  113. ::testing::Mock::WarnUninterestingCalls(reinterpret_cast<uintptr_t>(this));
  114. }
  115. ~NaggyMockImpl() {
  116. ::testing::Mock::UnregisterCallReaction(reinterpret_cast<uintptr_t>(this));
  117. }
  118. };
  119. template <typename Base>
  120. class StrictMockImpl {
  121. public:
  122. StrictMockImpl() {
  123. ::testing::Mock::FailUninterestingCalls(reinterpret_cast<uintptr_t>(this));
  124. }
  125. ~StrictMockImpl() {
  126. ::testing::Mock::UnregisterCallReaction(reinterpret_cast<uintptr_t>(this));
  127. }
  128. };
  129. } // namespace internal
  130. template <class MockClass>
  131. class GTEST_INTERNAL_EMPTY_BASE_CLASS NiceMock
  132. : private internal::NiceMockImpl<MockClass>,
  133. public MockClass {
  134. public:
  135. static_assert(!internal::HasStrictnessModifier<MockClass>(),
  136. "Can't apply NiceMock to a class hierarchy that already has a "
  137. "strictness modifier. See "
  138. "https://google.github.io/googletest/"
  139. "gmock_cook_book.html#NiceStrictNaggy");
  140. NiceMock() : MockClass() {
  141. static_assert(sizeof(*this) == sizeof(MockClass),
  142. "The impl subclass shouldn't introduce any padding");
  143. }
  144. // Ideally, we would inherit base class's constructors through a using
  145. // declaration, which would preserve their visibility. However, many existing
  146. // tests rely on the fact that current implementation reexports protected
  147. // constructors as public. These tests would need to be cleaned up first.
  148. // Single argument constructor is special-cased so that it can be
  149. // made explicit.
  150. template <typename A>
  151. explicit NiceMock(A&& arg) : MockClass(std::forward<A>(arg)) {
  152. static_assert(sizeof(*this) == sizeof(MockClass),
  153. "The impl subclass shouldn't introduce any padding");
  154. }
  155. template <typename TArg1, typename TArg2, typename... An>
  156. NiceMock(TArg1&& arg1, TArg2&& arg2, An&&... args)
  157. : MockClass(std::forward<TArg1>(arg1), std::forward<TArg2>(arg2),
  158. std::forward<An>(args)...) {
  159. static_assert(sizeof(*this) == sizeof(MockClass),
  160. "The impl subclass shouldn't introduce any padding");
  161. }
  162. private:
  163. NiceMock(const NiceMock&) = delete;
  164. NiceMock& operator=(const NiceMock&) = delete;
  165. };
  166. template <class MockClass>
  167. class GTEST_INTERNAL_EMPTY_BASE_CLASS NaggyMock
  168. : private internal::NaggyMockImpl<MockClass>,
  169. public MockClass {
  170. static_assert(!internal::HasStrictnessModifier<MockClass>(),
  171. "Can't apply NaggyMock to a class hierarchy that already has a "
  172. "strictness modifier. See "
  173. "https://google.github.io/googletest/"
  174. "gmock_cook_book.html#NiceStrictNaggy");
  175. public:
  176. NaggyMock() : MockClass() {
  177. static_assert(sizeof(*this) == sizeof(MockClass),
  178. "The impl subclass shouldn't introduce any padding");
  179. }
  180. // Ideally, we would inherit base class's constructors through a using
  181. // declaration, which would preserve their visibility. However, many existing
  182. // tests rely on the fact that current implementation reexports protected
  183. // constructors as public. These tests would need to be cleaned up first.
  184. // Single argument constructor is special-cased so that it can be
  185. // made explicit.
  186. template <typename A>
  187. explicit NaggyMock(A&& arg) : MockClass(std::forward<A>(arg)) {
  188. static_assert(sizeof(*this) == sizeof(MockClass),
  189. "The impl subclass shouldn't introduce any padding");
  190. }
  191. template <typename TArg1, typename TArg2, typename... An>
  192. NaggyMock(TArg1&& arg1, TArg2&& arg2, An&&... args)
  193. : MockClass(std::forward<TArg1>(arg1), std::forward<TArg2>(arg2),
  194. std::forward<An>(args)...) {
  195. static_assert(sizeof(*this) == sizeof(MockClass),
  196. "The impl subclass shouldn't introduce any padding");
  197. }
  198. private:
  199. NaggyMock(const NaggyMock&) = delete;
  200. NaggyMock& operator=(const NaggyMock&) = delete;
  201. };
  202. template <class MockClass>
  203. class GTEST_INTERNAL_EMPTY_BASE_CLASS StrictMock
  204. : private internal::StrictMockImpl<MockClass>,
  205. public MockClass {
  206. public:
  207. static_assert(
  208. !internal::HasStrictnessModifier<MockClass>(),
  209. "Can't apply StrictMock to a class hierarchy that already has a "
  210. "strictness modifier. See "
  211. "https://google.github.io/googletest/"
  212. "gmock_cook_book.html#NiceStrictNaggy");
  213. StrictMock() : MockClass() {
  214. static_assert(sizeof(*this) == sizeof(MockClass),
  215. "The impl subclass shouldn't introduce any padding");
  216. }
  217. // Ideally, we would inherit base class's constructors through a using
  218. // declaration, which would preserve their visibility. However, many existing
  219. // tests rely on the fact that current implementation reexports protected
  220. // constructors as public. These tests would need to be cleaned up first.
  221. // Single argument constructor is special-cased so that it can be
  222. // made explicit.
  223. template <typename A>
  224. explicit StrictMock(A&& arg) : MockClass(std::forward<A>(arg)) {
  225. static_assert(sizeof(*this) == sizeof(MockClass),
  226. "The impl subclass shouldn't introduce any padding");
  227. }
  228. template <typename TArg1, typename TArg2, typename... An>
  229. StrictMock(TArg1&& arg1, TArg2&& arg2, An&&... args)
  230. : MockClass(std::forward<TArg1>(arg1), std::forward<TArg2>(arg2),
  231. std::forward<An>(args)...) {
  232. static_assert(sizeof(*this) == sizeof(MockClass),
  233. "The impl subclass shouldn't introduce any padding");
  234. }
  235. private:
  236. StrictMock(const StrictMock&) = delete;
  237. StrictMock& operator=(const StrictMock&) = delete;
  238. };
  239. #undef GTEST_INTERNAL_EMPTY_BASE_CLASS
  240. } // namespace testing
  241. #endif // GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_