gmock-nice-strict.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 <type_traits>
  63. #include "gmock/gmock-spec-builders.h"
  64. #include "gmock/internal/gmock-port.h"
  65. namespace testing {
  66. template <class MockClass>
  67. class NiceMock;
  68. template <class MockClass>
  69. class NaggyMock;
  70. template <class MockClass>
  71. class StrictMock;
  72. namespace internal {
  73. template <typename T>
  74. std::true_type StrictnessModifierProbe(const NiceMock<T>&);
  75. template <typename T>
  76. std::true_type StrictnessModifierProbe(const NaggyMock<T>&);
  77. template <typename T>
  78. std::true_type StrictnessModifierProbe(const StrictMock<T>&);
  79. std::false_type StrictnessModifierProbe(...);
  80. template <typename T>
  81. constexpr bool HasStrictnessModifier() {
  82. return decltype(StrictnessModifierProbe(std::declval<const T&>()))::value;
  83. }
  84. // Base classes that register and deregister with testing::Mock to alter the
  85. // default behavior around uninteresting calls. Inheriting from one of these
  86. // classes first and then MockClass ensures the MockClass constructor is run
  87. // after registration, and that the MockClass destructor runs before
  88. // deregistration. This guarantees that MockClass's constructor and destructor
  89. // run with the same level of strictness as its instance methods.
  90. #if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW && \
  91. (defined(_MSC_VER) || defined(__clang__))
  92. // We need to mark these classes with this declspec to ensure that
  93. // the empty base class optimization is performed.
  94. #define GTEST_INTERNAL_EMPTY_BASE_CLASS __declspec(empty_bases)
  95. #else
  96. #define GTEST_INTERNAL_EMPTY_BASE_CLASS
  97. #endif
  98. template <typename Base>
  99. class NiceMockImpl {
  100. public:
  101. NiceMockImpl() { ::testing::Mock::AllowUninterestingCalls(this); }
  102. ~NiceMockImpl() { ::testing::Mock::UnregisterCallReaction(this); }
  103. };
  104. template <typename Base>
  105. class NaggyMockImpl {
  106. public:
  107. NaggyMockImpl() { ::testing::Mock::WarnUninterestingCalls(this); }
  108. ~NaggyMockImpl() { ::testing::Mock::UnregisterCallReaction(this); }
  109. };
  110. template <typename Base>
  111. class StrictMockImpl {
  112. public:
  113. StrictMockImpl() { ::testing::Mock::FailUninterestingCalls(this); }
  114. ~StrictMockImpl() { ::testing::Mock::UnregisterCallReaction(this); }
  115. };
  116. } // namespace internal
  117. template <class MockClass>
  118. class GTEST_INTERNAL_EMPTY_BASE_CLASS NiceMock
  119. : private internal::NiceMockImpl<MockClass>,
  120. public MockClass {
  121. public:
  122. static_assert(!internal::HasStrictnessModifier<MockClass>(),
  123. "Can't apply NiceMock to a class hierarchy that already has a "
  124. "strictness modifier. See "
  125. "https://google.github.io/googletest/"
  126. "gmock_cook_book.html#NiceStrictNaggy");
  127. NiceMock() : MockClass() {
  128. static_assert(sizeof(*this) == sizeof(MockClass),
  129. "The impl subclass shouldn't introduce any padding");
  130. }
  131. // Ideally, we would inherit base class's constructors through a using
  132. // declaration, which would preserve their visibility. However, many existing
  133. // tests rely on the fact that current implementation reexports protected
  134. // constructors as public. These tests would need to be cleaned up first.
  135. // Single argument constructor is special-cased so that it can be
  136. // made explicit.
  137. template <typename A>
  138. explicit NiceMock(A&& arg) : MockClass(std::forward<A>(arg)) {
  139. static_assert(sizeof(*this) == sizeof(MockClass),
  140. "The impl subclass shouldn't introduce any padding");
  141. }
  142. template <typename TArg1, typename TArg2, typename... An>
  143. NiceMock(TArg1&& arg1, TArg2&& arg2, An&&... args)
  144. : MockClass(std::forward<TArg1>(arg1), std::forward<TArg2>(arg2),
  145. std::forward<An>(args)...) {
  146. static_assert(sizeof(*this) == sizeof(MockClass),
  147. "The impl subclass shouldn't introduce any padding");
  148. }
  149. private:
  150. GTEST_DISALLOW_COPY_AND_ASSIGN_(NiceMock);
  151. };
  152. template <class MockClass>
  153. class GTEST_INTERNAL_EMPTY_BASE_CLASS NaggyMock
  154. : private internal::NaggyMockImpl<MockClass>,
  155. public MockClass {
  156. static_assert(!internal::HasStrictnessModifier<MockClass>(),
  157. "Can't apply NaggyMock to a class hierarchy that already has a "
  158. "strictness modifier. See "
  159. "https://google.github.io/googletest/"
  160. "gmock_cook_book.html#NiceStrictNaggy");
  161. public:
  162. NaggyMock() : MockClass() {
  163. static_assert(sizeof(*this) == sizeof(MockClass),
  164. "The impl subclass shouldn't introduce any padding");
  165. }
  166. // Ideally, we would inherit base class's constructors through a using
  167. // declaration, which would preserve their visibility. However, many existing
  168. // tests rely on the fact that current implementation reexports protected
  169. // constructors as public. These tests would need to be cleaned up first.
  170. // Single argument constructor is special-cased so that it can be
  171. // made explicit.
  172. template <typename A>
  173. explicit NaggyMock(A&& arg) : MockClass(std::forward<A>(arg)) {
  174. static_assert(sizeof(*this) == sizeof(MockClass),
  175. "The impl subclass shouldn't introduce any padding");
  176. }
  177. template <typename TArg1, typename TArg2, typename... An>
  178. NaggyMock(TArg1&& arg1, TArg2&& arg2, An&&... args)
  179. : MockClass(std::forward<TArg1>(arg1), std::forward<TArg2>(arg2),
  180. std::forward<An>(args)...) {
  181. static_assert(sizeof(*this) == sizeof(MockClass),
  182. "The impl subclass shouldn't introduce any padding");
  183. }
  184. private:
  185. GTEST_DISALLOW_COPY_AND_ASSIGN_(NaggyMock);
  186. };
  187. template <class MockClass>
  188. class GTEST_INTERNAL_EMPTY_BASE_CLASS StrictMock
  189. : private internal::StrictMockImpl<MockClass>,
  190. public MockClass {
  191. public:
  192. static_assert(
  193. !internal::HasStrictnessModifier<MockClass>(),
  194. "Can't apply StrictMock to a class hierarchy that already has a "
  195. "strictness modifier. See "
  196. "https://google.github.io/googletest/"
  197. "gmock_cook_book.html#NiceStrictNaggy");
  198. StrictMock() : MockClass() {
  199. static_assert(sizeof(*this) == sizeof(MockClass),
  200. "The impl subclass shouldn't introduce any padding");
  201. }
  202. // Ideally, we would inherit base class's constructors through a using
  203. // declaration, which would preserve their visibility. However, many existing
  204. // tests rely on the fact that current implementation reexports protected
  205. // constructors as public. These tests would need to be cleaned up first.
  206. // Single argument constructor is special-cased so that it can be
  207. // made explicit.
  208. template <typename A>
  209. explicit StrictMock(A&& arg) : MockClass(std::forward<A>(arg)) {
  210. static_assert(sizeof(*this) == sizeof(MockClass),
  211. "The impl subclass shouldn't introduce any padding");
  212. }
  213. template <typename TArg1, typename TArg2, typename... An>
  214. StrictMock(TArg1&& arg1, TArg2&& arg2, An&&... args)
  215. : MockClass(std::forward<TArg1>(arg1), std::forward<TArg2>(arg2),
  216. std::forward<An>(args)...) {
  217. static_assert(sizeof(*this) == sizeof(MockClass),
  218. "The impl subclass shouldn't introduce any padding");
  219. }
  220. private:
  221. GTEST_DISALLOW_COPY_AND_ASSIGN_(StrictMock);
  222. };
  223. #undef GTEST_INTERNAL_EMPTY_BASE_CLASS
  224. } // namespace testing
  225. #endif // GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_