1 // Copyright 2008, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
31 // Implements class templates NiceMock, NaggyMock, and StrictMock.
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.
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
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.
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
61 // GOOGLETEST_CM0002 DO NOT DELETE
63 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_
64 #define GMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_
66 #include "gmock/gmock-spec-builders.h"
67 #include "gmock/internal/gmock-port.h"
71 template <class MockClass
>
72 class NiceMock
: public MockClass
{
74 NiceMock() : MockClass() {
75 ::testing::Mock::AllowUninterestingCalls(
76 internal::ImplicitCast_
<MockClass
*>(this));
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.
84 // Single argument constructor is special-cased so that it can be
87 explicit NiceMock(A
&& arg
) : MockClass(std::forward
<A
>(arg
)) {
88 ::testing::Mock::AllowUninterestingCalls(
89 internal::ImplicitCast_
<MockClass
*>(this));
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));
100 ~NiceMock() { // NOLINT
101 ::testing::Mock::UnregisterCallReaction(
102 internal::ImplicitCast_
<MockClass
*>(this));
106 GTEST_DISALLOW_COPY_AND_ASSIGN_(NiceMock
);
109 template <class MockClass
>
110 class NaggyMock
: public MockClass
{
112 NaggyMock() : MockClass() {
113 ::testing::Mock::WarnUninterestingCalls(
114 internal::ImplicitCast_
<MockClass
*>(this));
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.
122 // Single argument constructor is special-cased so that it can be
124 template <typename A
>
125 explicit NaggyMock(A
&& arg
) : MockClass(std::forward
<A
>(arg
)) {
126 ::testing::Mock::WarnUninterestingCalls(
127 internal::ImplicitCast_
<MockClass
*>(this));
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));
138 ~NaggyMock() { // NOLINT
139 ::testing::Mock::UnregisterCallReaction(
140 internal::ImplicitCast_
<MockClass
*>(this));
144 GTEST_DISALLOW_COPY_AND_ASSIGN_(NaggyMock
);
147 template <class MockClass
>
148 class StrictMock
: public MockClass
{
150 StrictMock() : MockClass() {
151 ::testing::Mock::FailUninterestingCalls(
152 internal::ImplicitCast_
<MockClass
*>(this));
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.
160 // Single argument constructor is special-cased so that it can be
162 template <typename A
>
163 explicit StrictMock(A
&& arg
) : MockClass(std::forward
<A
>(arg
)) {
164 ::testing::Mock::FailUninterestingCalls(
165 internal::ImplicitCast_
<MockClass
*>(this));
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));
176 ~StrictMock() { // NOLINT
177 ::testing::Mock::UnregisterCallReaction(
178 internal::ImplicitCast_
<MockClass
*>(this));
182 GTEST_DISALLOW_COPY_AND_ASSIGN_(StrictMock
);
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.
189 // These specializations are declared but not defined, as NiceMock,
190 // NaggyMock, and StrictMock cannot be nested.
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
> >;
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
> >;
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
> >;
213 } // namespace testing
215 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_NICE_STRICT_H_