]> git.proxmox.com Git - ceph.git/blob - ceph/src/s3select/rapidjson/thirdparty/gtest/googlemock/test/gmock_output_test_.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / s3select / rapidjson / thirdparty / gtest / googlemock / test / gmock_output_test_.cc
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 // Author: wan@google.com (Zhanyong Wan)
31
32 // Tests Google Mock's output in various scenarios. This ensures that
33 // Google Mock's messages are readable and useful.
34
35 #include "gmock/gmock.h"
36
37 #include <stdio.h>
38 #include <string>
39
40 #include "gtest/gtest.h"
41
42 // Silence C4100 (unreferenced formal parameter)
43 #ifdef _MSC_VER
44 # pragma warning(push)
45 # pragma warning(disable:4100)
46 #endif
47
48 using testing::_;
49 using testing::AnyNumber;
50 using testing::Ge;
51 using testing::InSequence;
52 using testing::NaggyMock;
53 using testing::Ref;
54 using testing::Return;
55 using testing::Sequence;
56 using testing::Value;
57
58 class MockFoo {
59 public:
60 MockFoo() {}
61
62 MOCK_METHOD3(Bar, char(const std::string& s, int i, double x));
63 MOCK_METHOD2(Bar2, bool(int x, int y));
64 MOCK_METHOD2(Bar3, void(int x, int y));
65
66 private:
67 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
68 };
69
70 class GMockOutputTest : public testing::Test {
71 protected:
72 NaggyMock<MockFoo> foo_;
73 };
74
75 TEST_F(GMockOutputTest, ExpectedCall) {
76 testing::GMOCK_FLAG(verbose) = "info";
77
78 EXPECT_CALL(foo_, Bar2(0, _));
79 foo_.Bar2(0, 0); // Expected call
80
81 testing::GMOCK_FLAG(verbose) = "warning";
82 }
83
84 TEST_F(GMockOutputTest, ExpectedCallToVoidFunction) {
85 testing::GMOCK_FLAG(verbose) = "info";
86
87 EXPECT_CALL(foo_, Bar3(0, _));
88 foo_.Bar3(0, 0); // Expected call
89
90 testing::GMOCK_FLAG(verbose) = "warning";
91 }
92
93 TEST_F(GMockOutputTest, ExplicitActionsRunOut) {
94 EXPECT_CALL(foo_, Bar2(_, _))
95 .Times(2)
96 .WillOnce(Return(false));
97 foo_.Bar2(2, 2);
98 foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out.
99 }
100
101 TEST_F(GMockOutputTest, UnexpectedCall) {
102 EXPECT_CALL(foo_, Bar2(0, _));
103
104 foo_.Bar2(1, 0); // Unexpected call
105 foo_.Bar2(0, 0); // Expected call
106 }
107
108 TEST_F(GMockOutputTest, UnexpectedCallToVoidFunction) {
109 EXPECT_CALL(foo_, Bar3(0, _));
110
111 foo_.Bar3(1, 0); // Unexpected call
112 foo_.Bar3(0, 0); // Expected call
113 }
114
115 TEST_F(GMockOutputTest, ExcessiveCall) {
116 EXPECT_CALL(foo_, Bar2(0, _));
117
118 foo_.Bar2(0, 0); // Expected call
119 foo_.Bar2(0, 1); // Excessive call
120 }
121
122 TEST_F(GMockOutputTest, ExcessiveCallToVoidFunction) {
123 EXPECT_CALL(foo_, Bar3(0, _));
124
125 foo_.Bar3(0, 0); // Expected call
126 foo_.Bar3(0, 1); // Excessive call
127 }
128
129 TEST_F(GMockOutputTest, UninterestingCall) {
130 foo_.Bar2(0, 1); // Uninteresting call
131 }
132
133 TEST_F(GMockOutputTest, UninterestingCallToVoidFunction) {
134 foo_.Bar3(0, 1); // Uninteresting call
135 }
136
137 TEST_F(GMockOutputTest, RetiredExpectation) {
138 EXPECT_CALL(foo_, Bar2(_, _))
139 .RetiresOnSaturation();
140 EXPECT_CALL(foo_, Bar2(0, 0));
141
142 foo_.Bar2(1, 1);
143 foo_.Bar2(1, 1); // Matches a retired expectation
144 foo_.Bar2(0, 0);
145 }
146
147 TEST_F(GMockOutputTest, UnsatisfiedPrerequisite) {
148 {
149 InSequence s;
150 EXPECT_CALL(foo_, Bar(_, 0, _));
151 EXPECT_CALL(foo_, Bar2(0, 0));
152 EXPECT_CALL(foo_, Bar2(1, _));
153 }
154
155 foo_.Bar2(1, 0); // Has one immediate unsatisfied pre-requisite
156 foo_.Bar("Hi", 0, 0);
157 foo_.Bar2(0, 0);
158 foo_.Bar2(1, 0);
159 }
160
161 TEST_F(GMockOutputTest, UnsatisfiedPrerequisites) {
162 Sequence s1, s2;
163
164 EXPECT_CALL(foo_, Bar(_, 0, _))
165 .InSequence(s1);
166 EXPECT_CALL(foo_, Bar2(0, 0))
167 .InSequence(s2);
168 EXPECT_CALL(foo_, Bar2(1, _))
169 .InSequence(s1, s2);
170
171 foo_.Bar2(1, 0); // Has two immediate unsatisfied pre-requisites
172 foo_.Bar("Hi", 0, 0);
173 foo_.Bar2(0, 0);
174 foo_.Bar2(1, 0);
175 }
176
177 TEST_F(GMockOutputTest, UnsatisfiedWith) {
178 EXPECT_CALL(foo_, Bar2(_, _)).With(Ge());
179 }
180
181 TEST_F(GMockOutputTest, UnsatisfiedExpectation) {
182 EXPECT_CALL(foo_, Bar(_, _, _));
183 EXPECT_CALL(foo_, Bar2(0, _))
184 .Times(2);
185
186 foo_.Bar2(0, 1);
187 }
188
189 TEST_F(GMockOutputTest, MismatchArguments) {
190 const std::string s = "Hi";
191 EXPECT_CALL(foo_, Bar(Ref(s), _, Ge(0)));
192
193 foo_.Bar("Ho", 0, -0.1); // Mismatch arguments
194 foo_.Bar(s, 0, 0);
195 }
196
197 TEST_F(GMockOutputTest, MismatchWith) {
198 EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1)))
199 .With(Ge());
200
201 foo_.Bar2(2, 3); // Mismatch With()
202 foo_.Bar2(2, 1);
203 }
204
205 TEST_F(GMockOutputTest, MismatchArgumentsAndWith) {
206 EXPECT_CALL(foo_, Bar2(Ge(2), Ge(1)))
207 .With(Ge());
208
209 foo_.Bar2(1, 3); // Mismatch arguments and mismatch With()
210 foo_.Bar2(2, 1);
211 }
212
213 TEST_F(GMockOutputTest, UnexpectedCallWithDefaultAction) {
214 ON_CALL(foo_, Bar2(_, _))
215 .WillByDefault(Return(true)); // Default action #1
216 ON_CALL(foo_, Bar2(1, _))
217 .WillByDefault(Return(false)); // Default action #2
218
219 EXPECT_CALL(foo_, Bar2(2, 2));
220 foo_.Bar2(1, 0); // Unexpected call, takes default action #2.
221 foo_.Bar2(0, 0); // Unexpected call, takes default action #1.
222 foo_.Bar2(2, 2); // Expected call.
223 }
224
225 TEST_F(GMockOutputTest, ExcessiveCallWithDefaultAction) {
226 ON_CALL(foo_, Bar2(_, _))
227 .WillByDefault(Return(true)); // Default action #1
228 ON_CALL(foo_, Bar2(1, _))
229 .WillByDefault(Return(false)); // Default action #2
230
231 EXPECT_CALL(foo_, Bar2(2, 2));
232 EXPECT_CALL(foo_, Bar2(1, 1));
233
234 foo_.Bar2(2, 2); // Expected call.
235 foo_.Bar2(2, 2); // Excessive call, takes default action #1.
236 foo_.Bar2(1, 1); // Expected call.
237 foo_.Bar2(1, 1); // Excessive call, takes default action #2.
238 }
239
240 TEST_F(GMockOutputTest, UninterestingCallWithDefaultAction) {
241 ON_CALL(foo_, Bar2(_, _))
242 .WillByDefault(Return(true)); // Default action #1
243 ON_CALL(foo_, Bar2(1, _))
244 .WillByDefault(Return(false)); // Default action #2
245
246 foo_.Bar2(2, 2); // Uninteresting call, takes default action #1.
247 foo_.Bar2(1, 1); // Uninteresting call, takes default action #2.
248 }
249
250 TEST_F(GMockOutputTest, ExplicitActionsRunOutWithDefaultAction) {
251 ON_CALL(foo_, Bar2(_, _))
252 .WillByDefault(Return(true)); // Default action #1
253
254 EXPECT_CALL(foo_, Bar2(_, _))
255 .Times(2)
256 .WillOnce(Return(false));
257 foo_.Bar2(2, 2);
258 foo_.Bar2(1, 1); // Explicit actions in EXPECT_CALL run out.
259 }
260
261 TEST_F(GMockOutputTest, CatchesLeakedMocks) {
262 MockFoo* foo1 = new MockFoo;
263 MockFoo* foo2 = new MockFoo;
264
265 // Invokes ON_CALL on foo1.
266 ON_CALL(*foo1, Bar(_, _, _)).WillByDefault(Return('a'));
267
268 // Invokes EXPECT_CALL on foo2.
269 EXPECT_CALL(*foo2, Bar2(_, _));
270 EXPECT_CALL(*foo2, Bar2(1, _));
271 EXPECT_CALL(*foo2, Bar3(_, _)).Times(AnyNumber());
272 foo2->Bar2(2, 1);
273 foo2->Bar2(1, 1);
274
275 // Both foo1 and foo2 are deliberately leaked.
276 }
277
278 MATCHER_P2(IsPair, first, second, "") {
279 return Value(arg.first, first) && Value(arg.second, second);
280 }
281
282 TEST_F(GMockOutputTest, PrintsMatcher) {
283 const testing::Matcher<int> m1 = Ge(48);
284 EXPECT_THAT((std::pair<int, bool>(42, true)), IsPair(m1, true));
285 }
286
287 void TestCatchesLeakedMocksInAdHocTests() {
288 MockFoo* foo = new MockFoo;
289
290 // Invokes EXPECT_CALL on foo.
291 EXPECT_CALL(*foo, Bar2(_, _));
292 foo->Bar2(2, 1);
293
294 // foo is deliberately leaked.
295 }
296
297 int main(int argc, char **argv) {
298 testing::InitGoogleMock(&argc, argv);
299 // Ensures that the tests pass no matter what value of
300 // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies.
301 testing::GMOCK_FLAG(catch_leaked_mocks) = true;
302 testing::GMOCK_FLAG(verbose) = "warning";
303
304 TestCatchesLeakedMocksInAdHocTests();
305 return RUN_ALL_TESTS();
306 }
307
308 #ifdef _MSC_VER
309 # pragma warning(pop)
310 #endif