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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. $$ -*- mode: c++; -*-
  2. $$ This is a Pump source file. Please use Pump to convert it to
  3. $$ 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. //
  35. // Author: wan@google.com (Zhanyong Wan)
  36. // Implements class templates NiceMock, NaggyMock, and StrictMock.
  37. //
  38. // Given a mock class MockFoo that is created using Google Mock,
  39. // NiceMock<MockFoo> is a subclass of MockFoo that allows
  40. // uninteresting calls (i.e. calls to mock methods that have no
  41. // EXPECT_CALL specs), NaggyMock<MockFoo> is a subclass of MockFoo
  42. // that prints a warning when an uninteresting call occurs, and
  43. // StrictMock<MockFoo> is a subclass of MockFoo that treats all
  44. // uninteresting calls as errors.
  45. //
  46. // Currently a mock is naggy by default, so MockFoo and
  47. // NaggyMock<MockFoo> behave like the same. However, we will soon
  48. // switch the default behavior of mocks to be nice, as that in general
  49. // leads to more maintainable tests. When that happens, MockFoo will
  50. // stop behaving like NaggyMock<MockFoo> and start behaving like
  51. // NiceMock<MockFoo>.
  52. //
  53. // NiceMock, NaggyMock, and StrictMock "inherit" the constructors of
  54. // their respective base class, with up-to $n arguments. Therefore
  55. // you can write NiceMock<MockFoo>(5, "a") to construct a nice mock
  56. // where MockFoo has a constructor that accepts (int, const char*),
  57. // for example.
  58. //
  59. // A known limitation is that NiceMock<MockFoo>, NaggyMock<MockFoo>,
  60. // and StrictMock<MockFoo> only works for mock methods defined using
  61. // the MOCK_METHOD* family of macros DIRECTLY in the MockFoo class.
  62. // If a mock method is defined in a base class of MockFoo, the "nice"
  63. // or "strict" modifier may not affect it, depending on the compiler.
  64. // In particular, nesting NiceMock, NaggyMock, and StrictMock is NOT
  65. // supported.
  66. //
  67. // Another known limitation is that the constructors of the base mock
  68. // cannot have arguments passed by non-const reference, which are
  69. // banned by the Google C++ style guide anyway.
  70. #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_NICE_STRICT_H_
  71. #define GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_NICE_STRICT_H_
  72. #include "gmock/gmock-spec-builders.h"
  73. #include "gmock/internal/gmock-port.h"
  74. namespace testing {
  75. $range kind 0..2
  76. $for kind [[
  77. $var clazz=[[$if kind==0 [[NiceMock]]
  78. $elif kind==1 [[NaggyMock]]
  79. $else [[StrictMock]]]]
  80. $var method=[[$if kind==0 [[AllowUninterestingCalls]]
  81. $elif kind==1 [[WarnUninterestingCalls]]
  82. $else [[FailUninterestingCalls]]]]
  83. template <class MockClass>
  84. class $clazz : public MockClass {
  85. public:
  86. // We don't factor out the constructor body to a common method, as
  87. // we have to avoid a possible clash with members of MockClass.
  88. $clazz() {
  89. ::testing::Mock::$method(
  90. internal::ImplicitCast_<MockClass*>(this));
  91. }
  92. // C++ doesn't (yet) allow inheritance of constructors, so we have
  93. // to define it for each arity.
  94. template <typename A1>
  95. explicit $clazz(const A1& a1) : MockClass(a1) {
  96. ::testing::Mock::$method(
  97. internal::ImplicitCast_<MockClass*>(this));
  98. }
  99. $range i 2..n
  100. $for i [[
  101. $range j 1..i
  102. template <$for j, [[typename A$j]]>
  103. $clazz($for j, [[const A$j& a$j]]) : MockClass($for j, [[a$j]]) {
  104. ::testing::Mock::$method(
  105. internal::ImplicitCast_<MockClass*>(this));
  106. }
  107. ]]
  108. virtual ~$clazz() {
  109. ::testing::Mock::UnregisterCallReaction(
  110. internal::ImplicitCast_<MockClass*>(this));
  111. }
  112. private:
  113. GTEST_DISALLOW_COPY_AND_ASSIGN_($clazz);
  114. };
  115. ]]
  116. // The following specializations catch some (relatively more common)
  117. // user errors of nesting nice and strict mocks. They do NOT catch
  118. // all possible errors.
  119. // These specializations are declared but not defined, as NiceMock,
  120. // NaggyMock, and StrictMock cannot be nested.
  121. template <typename MockClass>
  122. class NiceMock<NiceMock<MockClass> >;
  123. template <typename MockClass>
  124. class NiceMock<NaggyMock<MockClass> >;
  125. template <typename MockClass>
  126. class NiceMock<StrictMock<MockClass> >;
  127. template <typename MockClass>
  128. class NaggyMock<NiceMock<MockClass> >;
  129. template <typename MockClass>
  130. class NaggyMock<NaggyMock<MockClass> >;
  131. template <typename MockClass>
  132. class NaggyMock<StrictMock<MockClass> >;
  133. template <typename MockClass>
  134. class StrictMock<NiceMock<MockClass> >;
  135. template <typename MockClass>
  136. class StrictMock<NaggyMock<MockClass> >;
  137. template <typename MockClass>
  138. class StrictMock<StrictMock<MockClass> >;
  139. } // namespace testing
  140. #endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_NICE_STRICT_H_