gmock-generated-nice-strict.h.pump 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. $$ -*- mode: c++; -*-
  2. $$ This is a Pump source file. Please use Pump to convert
  3. $$ it to gmock-generated-nice-strict.h.
  4. $$
  5. $var n = 10 $$ The maximum arity we support.
  6. // Copyright 2008, Google Inc.
  7. // All rights reserved.
  8. //
  9. // Redistribution and use in source and binary forms, with or without
  10. // modification, are permitted provided that the following conditions are
  11. // met:
  12. //
  13. // * Redistributions of source code must retain the above copyright
  14. // notice, this list of conditions and the following disclaimer.
  15. // * Redistributions in binary form must reproduce the above
  16. // copyright notice, this list of conditions and the following disclaimer
  17. // in the documentation and/or other materials provided with the
  18. // distribution.
  19. // * Neither the name of Google Inc. nor the names of its
  20. // contributors may be used to endorse or promote products derived from
  21. // this software without specific prior written permission.
  22. //
  23. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. // Implements class templates NiceMock, NaggyMock, and StrictMock.
  35. //
  36. // Given a mock class MockFoo that is created using Google Mock,
  37. // NiceMock<MockFoo> is a subclass of MockFoo that allows
  38. // uninteresting calls (i.e. calls to mock methods that have no
  39. // EXPECT_CALL specs), NaggyMock<MockFoo> is a subclass of MockFoo
  40. // that prints a warning when an uninteresting call occurs, and
  41. // StrictMock<MockFoo> is a subclass of MockFoo that treats all
  42. // uninteresting calls as errors.
  43. //
  44. // Currently a mock is naggy by default, so MockFoo and
  45. // NaggyMock<MockFoo> behave like the same. However, we will soon
  46. // switch the default behavior of mocks to be nice, as that in general
  47. // leads to more maintainable tests. When that happens, MockFoo will
  48. // stop behaving like NaggyMock<MockFoo> and start behaving like
  49. // NiceMock<MockFoo>.
  50. //
  51. // NiceMock, NaggyMock, and StrictMock "inherit" the constructors of
  52. // their respective base class. Therefore you can write
  53. // NiceMock<MockFoo>(5, "a") to construct a nice mock where MockFoo
  54. // has a constructor that accepts (int, const char*), for example.
  55. //
  56. // A known limitation is that NiceMock<MockFoo>, NaggyMock<MockFoo>,
  57. // and StrictMock<MockFoo> only works for mock methods defined using
  58. // the MOCK_METHOD* family of macros DIRECTLY in the MockFoo class.
  59. // If a mock method is defined in a base class of MockFoo, the "nice"
  60. // or "strict" modifier may not affect it, depending on the compiler.
  61. // In particular, nesting NiceMock, NaggyMock, and StrictMock is NOT
  62. // supported.
  63. // GOOGLETEST_CM0002 DO NOT DELETE
  64. #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_NICE_STRICT_H_
  65. #define GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_NICE_STRICT_H_
  66. #include "gmock/gmock-spec-builders.h"
  67. #include "gmock/internal/gmock-port.h"
  68. namespace testing {
  69. $range kind 0..2
  70. $for kind [[
  71. $var clazz=[[$if kind==0 [[NiceMock]]
  72. $elif kind==1 [[NaggyMock]]
  73. $else [[StrictMock]]]]
  74. $var method=[[$if kind==0 [[AllowUninterestingCalls]]
  75. $elif kind==1 [[WarnUninterestingCalls]]
  76. $else [[FailUninterestingCalls]]]]
  77. template <class MockClass>
  78. class $clazz : public MockClass {
  79. public:
  80. $clazz() : MockClass() {
  81. ::testing::Mock::$method(
  82. internal::ImplicitCast_<MockClass*>(this));
  83. }
  84. #if GTEST_LANG_CXX11
  85. // Ideally, we would inherit base class's constructors through a using
  86. // declaration, which would preserve their visibility. However, many existing
  87. // tests rely on the fact that current implementation reexports protected
  88. // constructors as public. These tests would need to be cleaned up first.
  89. // Single argument constructor is special-cased so that it can be
  90. // made explicit.
  91. template <typename A>
  92. explicit $clazz(A&& arg) : MockClass(std::forward<A>(arg)) {
  93. ::testing::Mock::$method(
  94. internal::ImplicitCast_<MockClass*>(this));
  95. }
  96. template <typename A1, typename A2, typename... An>
  97. $clazz(A1&& arg1, A2&& arg2, An&&... args)
  98. : MockClass(std::forward<A1>(arg1), std::forward<A2>(arg2),
  99. std::forward<An>(args)...) {
  100. ::testing::Mock::$method(
  101. internal::ImplicitCast_<MockClass*>(this));
  102. }
  103. #else
  104. // C++98 doesn't have variadic templates, so we have to define one
  105. // for each arity.
  106. template <typename A1>
  107. explicit $clazz(const A1& a1) : MockClass(a1) {
  108. ::testing::Mock::$method(
  109. internal::ImplicitCast_<MockClass*>(this));
  110. }
  111. $range i 2..n
  112. $for i [[
  113. $range j 1..i
  114. template <$for j, [[typename A$j]]>
  115. $clazz($for j, [[const A$j& a$j]]) : MockClass($for j, [[a$j]]) {
  116. ::testing::Mock::$method(
  117. internal::ImplicitCast_<MockClass*>(this));
  118. }
  119. ]]
  120. #endif // GTEST_LANG_CXX11
  121. ~$clazz() {
  122. ::testing::Mock::UnregisterCallReaction(
  123. internal::ImplicitCast_<MockClass*>(this));
  124. }
  125. private:
  126. GTEST_DISALLOW_COPY_AND_ASSIGN_($clazz);
  127. };
  128. ]]
  129. // The following specializations catch some (relatively more common)
  130. // user errors of nesting nice and strict mocks. They do NOT catch
  131. // all possible errors.
  132. // These specializations are declared but not defined, as NiceMock,
  133. // NaggyMock, and StrictMock cannot be nested.
  134. template <typename MockClass>
  135. class NiceMock<NiceMock<MockClass> >;
  136. template <typename MockClass>
  137. class NiceMock<NaggyMock<MockClass> >;
  138. template <typename MockClass>
  139. class NiceMock<StrictMock<MockClass> >;
  140. template <typename MockClass>
  141. class NaggyMock<NiceMock<MockClass> >;
  142. template <typename MockClass>
  143. class NaggyMock<NaggyMock<MockClass> >;
  144. template <typename MockClass>
  145. class NaggyMock<StrictMock<MockClass> >;
  146. template <typename MockClass>
  147. class StrictMock<NiceMock<MockClass> >;
  148. template <typename MockClass>
  149. class StrictMock<NaggyMock<MockClass> >;
  150. template <typename MockClass>
  151. class StrictMock<StrictMock<MockClass> >;
  152. } // namespace testing
  153. #endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_NICE_STRICT_H_