You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
8.0 KiB

  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 GMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_
  60. #define GMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_
  61. #include "gmock/gmock-spec-builders.h"
  62. #include "gmock/internal/gmock-port.h"
  63. namespace testing {
  64. template <class MockClass>
  65. class NiceMock : public MockClass {
  66. public:
  67. NiceMock() : MockClass() {
  68. ::testing::Mock::AllowUninterestingCalls(
  69. internal::ImplicitCast_<MockClass*>(this));
  70. }
  71. // Ideally, we would inherit base class's constructors through a using
  72. // declaration, which would preserve their visibility. However, many existing
  73. // tests rely on the fact that current implementation reexports protected
  74. // constructors as public. These tests would need to be cleaned up first.
  75. // Single argument constructor is special-cased so that it can be
  76. // made explicit.
  77. template <typename A>
  78. explicit NiceMock(A&& arg) : MockClass(std::forward<A>(arg)) {
  79. ::testing::Mock::AllowUninterestingCalls(
  80. internal::ImplicitCast_<MockClass*>(this));
  81. }
  82. template <typename A1, typename A2, typename... An>
  83. NiceMock(A1&& arg1, A2&& arg2, An&&... args)
  84. : MockClass(std::forward<A1>(arg1), std::forward<A2>(arg2),
  85. std::forward<An>(args)...) {
  86. ::testing::Mock::AllowUninterestingCalls(
  87. internal::ImplicitCast_<MockClass*>(this));
  88. }
  89. ~NiceMock() { // NOLINT
  90. ::testing::Mock::UnregisterCallReaction(
  91. internal::ImplicitCast_<MockClass*>(this));
  92. }
  93. private:
  94. GTEST_DISALLOW_COPY_AND_ASSIGN_(NiceMock);
  95. };
  96. template <class MockClass>
  97. class NaggyMock : public MockClass {
  98. public:
  99. NaggyMock() : MockClass() {
  100. ::testing::Mock::WarnUninterestingCalls(
  101. internal::ImplicitCast_<MockClass*>(this));
  102. }
  103. // Ideally, we would inherit base class's constructors through a using
  104. // declaration, which would preserve their visibility. However, many existing
  105. // tests rely on the fact that current implementation reexports protected
  106. // constructors as public. These tests would need to be cleaned up first.
  107. // Single argument constructor is special-cased so that it can be
  108. // made explicit.
  109. template <typename A>
  110. explicit NaggyMock(A&& arg) : MockClass(std::forward<A>(arg)) {
  111. ::testing::Mock::WarnUninterestingCalls(
  112. internal::ImplicitCast_<MockClass*>(this));
  113. }
  114. template <typename A1, typename A2, typename... An>
  115. NaggyMock(A1&& arg1, A2&& arg2, An&&... args)
  116. : MockClass(std::forward<A1>(arg1), std::forward<A2>(arg2),
  117. std::forward<An>(args)...) {
  118. ::testing::Mock::WarnUninterestingCalls(
  119. internal::ImplicitCast_<MockClass*>(this));
  120. }
  121. ~NaggyMock() { // NOLINT
  122. ::testing::Mock::UnregisterCallReaction(
  123. internal::ImplicitCast_<MockClass*>(this));
  124. }
  125. private:
  126. GTEST_DISALLOW_COPY_AND_ASSIGN_(NaggyMock);
  127. };
  128. template <class MockClass>
  129. class StrictMock : public MockClass {
  130. public:
  131. StrictMock() : MockClass() {
  132. ::testing::Mock::FailUninterestingCalls(
  133. internal::ImplicitCast_<MockClass*>(this));
  134. }
  135. // Ideally, we would inherit base class's constructors through a using
  136. // declaration, which would preserve their visibility. However, many existing
  137. // tests rely on the fact that current implementation reexports protected
  138. // constructors as public. These tests would need to be cleaned up first.
  139. // Single argument constructor is special-cased so that it can be
  140. // made explicit.
  141. template <typename A>
  142. explicit StrictMock(A&& arg) : MockClass(std::forward<A>(arg)) {
  143. ::testing::Mock::FailUninterestingCalls(
  144. internal::ImplicitCast_<MockClass*>(this));
  145. }
  146. template <typename A1, typename A2, typename... An>
  147. StrictMock(A1&& arg1, A2&& arg2, An&&... args)
  148. : MockClass(std::forward<A1>(arg1), std::forward<A2>(arg2),
  149. std::forward<An>(args)...) {
  150. ::testing::Mock::FailUninterestingCalls(
  151. internal::ImplicitCast_<MockClass*>(this));
  152. }
  153. ~StrictMock() { // NOLINT
  154. ::testing::Mock::UnregisterCallReaction(
  155. internal::ImplicitCast_<MockClass*>(this));
  156. }
  157. private:
  158. GTEST_DISALLOW_COPY_AND_ASSIGN_(StrictMock);
  159. };
  160. // The following specializations catch some (relatively more common)
  161. // user errors of nesting nice and strict mocks. They do NOT catch
  162. // all possible errors.
  163. // These specializations are declared but not defined, as NiceMock,
  164. // NaggyMock, and StrictMock cannot be nested.
  165. template <typename MockClass>
  166. class NiceMock<NiceMock<MockClass> >;
  167. template <typename MockClass>
  168. class NiceMock<NaggyMock<MockClass> >;
  169. template <typename MockClass>
  170. class NiceMock<StrictMock<MockClass> >;
  171. template <typename MockClass>
  172. class NaggyMock<NiceMock<MockClass> >;
  173. template <typename MockClass>
  174. class NaggyMock<NaggyMock<MockClass> >;
  175. template <typename MockClass>
  176. class NaggyMock<StrictMock<MockClass> >;
  177. template <typename MockClass>
  178. class StrictMock<NiceMock<MockClass> >;
  179. template <typename MockClass>
  180. class StrictMock<NaggyMock<MockClass> >;
  181. template <typename MockClass>
  182. class StrictMock<StrictMock<MockClass> >;
  183. } // namespace testing
  184. #endif // GMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_