Debian Codegraph @

llvm-project/third-party/unittest/googlemock/include/gmock/gmock-nice-strict.h

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