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