| 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 | |
| 30 | |
| 31 | // Implements class templates NiceMock, NaggyMock, and StrictMock. |
| 32 | // |
| 33 | // Given a mock class MockFoo that is created using Google Mock, |
| 34 | // NiceMock<MockFoo> is a subclass of MockFoo that allows |
| 35 | // uninteresting calls (i.e. calls to mock methods that have no |
| 36 | // EXPECT_CALL specs), NaggyMock<MockFoo> is a subclass of MockFoo |
| 37 | // that prints a warning when an uninteresting call occurs, and |
| 38 | // StrictMock<MockFoo> is a subclass of MockFoo that treats all |
| 39 | // uninteresting calls as errors. |
| 40 | // |
| 41 | // Currently a mock is naggy by default, so MockFoo and |
| 42 | // NaggyMock<MockFoo> behave like the same. However, we will soon |
| 43 | // switch the default behavior of mocks to be nice, as that in general |
| 44 | // leads to more maintainable tests. When that happens, MockFoo will |
| 45 | // stop behaving like NaggyMock<MockFoo> and start behaving like |
| 46 | // NiceMock<MockFoo>. |
| 47 | // |
| 48 | // NiceMock, NaggyMock, and StrictMock "inherit" the constructors of |
| 49 | // their respective base class. Therefore you can write |
| 50 | // NiceMock<MockFoo>(5, "a") to construct a nice mock where MockFoo |
| 51 | // has a constructor that accepts (int, const char*), for example. |
| 52 | // |
| 53 | // A known limitation is that NiceMock<MockFoo>, NaggyMock<MockFoo>, |
| 54 | // and StrictMock<MockFoo> only works for mock methods defined using |
| 55 | // the MOCK_METHOD* family of macros DIRECTLY in the MockFoo class. |
| 56 | // If a mock method is defined in a base class of MockFoo, the "nice" |
| 57 | // or "strict" modifier may not affect it, depending on the compiler. |
| 58 | // In particular, nesting NiceMock, NaggyMock, and StrictMock is NOT |
| 59 | // supported. |
| 60 | |
| 61 | // GOOGLETEST_CM0002 DO NOT DELETE |
| 62 | |
| 63 | #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_ |
| 64 | #define GMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_ |
| 65 | |
| 66 | #include "gmock/gmock-spec-builders.h" |
| 67 | #include "gmock/internal/gmock-port.h" |
| 68 | |
| 69 | namespace testing { |
| 70 | |
| 71 | template <class MockClass> |
| 72 | class NiceMock : public MockClass { |
| 73 | public: |
| 74 | NiceMock() : MockClass() { |
| 75 | ::testing::Mock::AllowUninterestingCalls( |
| 76 | internal::ImplicitCast_<MockClass*>(this)); |
| 77 | } |
| 78 | |
| 79 | // Ideally, we would inherit base class's constructors through a using |
| 80 | // declaration, which would preserve their visibility. However, many existing |
| 81 | // tests rely on the fact that current implementation reexports protected |
| 82 | // constructors as public. These tests would need to be cleaned up first. |
| 83 | |
| 84 | // Single argument constructor is special-cased so that it can be |
| 85 | // made explicit. |
| 86 | template <typename A> |
| 87 | explicit NiceMock(A&& arg) : MockClass(std::forward<A>(arg)) { |
| 88 | ::testing::Mock::AllowUninterestingCalls( |
| 89 | internal::ImplicitCast_<MockClass*>(this)); |
| 90 | } |
| 91 | |
| 92 | template <typename A1, typename A2, typename... An> |
| 93 | NiceMock(A1&& arg1, A2&& arg2, An&&... args) |
| 94 | : MockClass(std::forward<A1>(arg1), std::forward<A2>(arg2), |
| 95 | std::forward<An>(args)...) { |
| 96 | ::testing::Mock::AllowUninterestingCalls( |
| 97 | internal::ImplicitCast_<MockClass*>(this)); |
| 98 | } |
| 99 | |
| 100 | ~NiceMock() { // NOLINT |
| 101 | ::testing::Mock::UnregisterCallReaction( |
| 102 | internal::ImplicitCast_<MockClass*>(this)); |
| 103 | } |
| 104 | |
| 105 | private: |
| 106 | GTEST_DISALLOW_COPY_AND_ASSIGN_(NiceMock); |
| 107 | }; |
| 108 | |
| 109 | template <class MockClass> |
| 110 | class NaggyMock : public MockClass { |
| 111 | public: |
| 112 | NaggyMock() : MockClass() { |
| 113 | ::testing::Mock::WarnUninterestingCalls( |
| 114 | internal::ImplicitCast_<MockClass*>(this)); |
| 115 | } |
| 116 | |
| 117 | // Ideally, we would inherit base class's constructors through a using |
| 118 | // declaration, which would preserve their visibility. However, many existing |
| 119 | // tests rely on the fact that current implementation reexports protected |
| 120 | // constructors as public. These tests would need to be cleaned up first. |
| 121 | |
| 122 | // Single argument constructor is special-cased so that it can be |
| 123 | // made explicit. |
| 124 | template <typename A> |
| 125 | explicit NaggyMock(A&& arg) : MockClass(std::forward<A>(arg)) { |
| 126 | ::testing::Mock::WarnUninterestingCalls( |
| 127 | internal::ImplicitCast_<MockClass*>(this)); |
| 128 | } |
| 129 | |
| 130 | template <typename A1, typename A2, typename... An> |
| 131 | NaggyMock(A1&& arg1, A2&& arg2, An&&... args) |
| 132 | : MockClass(std::forward<A1>(arg1), std::forward<A2>(arg2), |
| 133 | std::forward<An>(args)...) { |
| 134 | ::testing::Mock::WarnUninterestingCalls( |
| 135 | internal::ImplicitCast_<MockClass*>(this)); |
| 136 | } |
| 137 | |
| 138 | ~NaggyMock() { // NOLINT |
| 139 | ::testing::Mock::UnregisterCallReaction( |
| 140 | internal::ImplicitCast_<MockClass*>(this)); |
| 141 | } |
| 142 | |
| 143 | private: |
| 144 | GTEST_DISALLOW_COPY_AND_ASSIGN_(NaggyMock); |
| 145 | }; |
| 146 | |
| 147 | template <class MockClass> |
| 148 | class StrictMock : public MockClass { |
| 149 | public: |
| 150 | StrictMock() : MockClass() { |
| 151 | ::testing::Mock::FailUninterestingCalls( |
| 152 | internal::ImplicitCast_<MockClass*>(this)); |
| 153 | } |
| 154 | |
| 155 | // Ideally, we would inherit base class's constructors through a using |
| 156 | // declaration, which would preserve their visibility. However, many existing |
| 157 | // tests rely on the fact that current implementation reexports protected |
| 158 | // constructors as public. These tests would need to be cleaned up first. |
| 159 | |
| 160 | // Single argument constructor is special-cased so that it can be |
| 161 | // made explicit. |
| 162 | template <typename A> |
| 163 | explicit StrictMock(A&& arg) : MockClass(std::forward<A>(arg)) { |
| 164 | ::testing::Mock::FailUninterestingCalls( |
| 165 | internal::ImplicitCast_<MockClass*>(this)); |
| 166 | } |
| 167 | |
| 168 | template <typename A1, typename A2, typename... An> |
| 169 | StrictMock(A1&& arg1, A2&& arg2, An&&... args) |
| 170 | : MockClass(std::forward<A1>(arg1), std::forward<A2>(arg2), |
| 171 | std::forward<An>(args)...) { |
| 172 | ::testing::Mock::FailUninterestingCalls( |
| 173 | internal::ImplicitCast_<MockClass*>(this)); |
| 174 | } |
| 175 | |
| 176 | ~StrictMock() { // NOLINT |
| 177 | ::testing::Mock::UnregisterCallReaction( |
| 178 | internal::ImplicitCast_<MockClass*>(this)); |
| 179 | } |
| 180 | |
| 181 | private: |
| 182 | GTEST_DISALLOW_COPY_AND_ASSIGN_(StrictMock); |
| 183 | }; |
| 184 | |
| 185 | // The following specializations catch some (relatively more common) |
| 186 | // user errors of nesting nice and strict mocks. They do NOT catch |
| 187 | // all possible errors. |
| 188 | |
| 189 | // These specializations are declared but not defined, as NiceMock, |
| 190 | // NaggyMock, and StrictMock cannot be nested. |
| 191 | |
| 192 | template <typename MockClass> |
| 193 | class NiceMock<NiceMock<MockClass> >; |
| 194 | template <typename MockClass> |
| 195 | class NiceMock<NaggyMock<MockClass> >; |
| 196 | template <typename MockClass> |
| 197 | class NiceMock<StrictMock<MockClass> >; |
| 198 | |
| 199 | template <typename MockClass> |
| 200 | class NaggyMock<NiceMock<MockClass> >; |
| 201 | template <typename MockClass> |
| 202 | class NaggyMock<NaggyMock<MockClass> >; |
| 203 | template <typename MockClass> |
| 204 | class NaggyMock<StrictMock<MockClass> >; |
| 205 | |
| 206 | template <typename MockClass> |
| 207 | class StrictMock<NiceMock<MockClass> >; |
| 208 | template <typename MockClass> |
| 209 | class StrictMock<NaggyMock<MockClass> >; |
| 210 | template <typename MockClass> |
| 211 | class StrictMock<StrictMock<MockClass> >; |
| 212 | |
| 213 | } // namespace testing |
| 214 | |
| 215 | #endif // GMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_ |
| 216 | |