Debian Codegraph @

tests/CppUTestExt/MockCallTest.cpp

raw symbols SCIP
1 /*
2 * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the <organization> nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "CppUTest/TestHarness.h"
29 #include "CppUTest/TestTestingFixture.h"
30 #include "MockFailureReporterForTest.h"
31
32 TEST_GROUP(MockCallTest)
33 {
34 void teardown()
35 {
36 mock().checkExpectations();
37 mock().clear();
38 }
39 };
40
41 TEST(MockCallTest, clear)
42 {
43 mock().expectOneCall("func");
44 mock().clear();
45 CHECK(! mock().expectedCallsLeft());
46 }
47
48 TEST(MockCallTest, checkExpectationsDoesntFail)
49 {
50 mock().checkExpectations();
51 }
52
53 TEST(MockCallTest, expectASingleCallThatHappens)
54 {
55 mock().expectOneCall("func");
56 MockCheckedActualCall& actualCall = (MockCheckedActualCall&) mock().actualCall("func");
57 actualCall.checkExpectations();
58 CHECK(! mock().expectedCallsLeft());
59 }
60
61 TEST(MockCallTest, expectASingleCallThatDoesntHappen)
62 {
63 mock().expectOneCall("func");
64 CHECK(mock().expectedCallsLeft());
65 mock().clear();
66 }
67
68 TEST(MockCallTest, expectAMultiCallThatHappensTheExpectedTimes)
69 {
70 mock().expectNCalls(2, "func");
71 mock().actualCall("func");
72 MockCheckedActualCall& actualCall = (MockCheckedActualCall&) mock().actualCall("func");
73 actualCall.checkExpectations();
74 CHECK(! mock().expectedCallsLeft());
75 }
76
77 TEST(MockCallTest, expectAMultiCallThatDoesntHappenTheExpectedTimes)
78 {
79 mock().expectNCalls(2, "func");
80 MockCheckedActualCall& actualCall = (MockCheckedActualCall&) mock().actualCall("func");
81 actualCall.checkExpectations();
82 CHECK(mock().expectedCallsLeft());
83 mock().clear();
84 }
85
86 TEST(MockCallTest, checkExpectationsClearsTheExpectations)
87 {
88 MockFailureReporterInstaller failureReporterInstaller;
89
90 MockExpectedCallsListForTest expectations;
91 expectations.addFunction("foobar");
92 MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations);
93
94 mock().expectOneCall("foobar");
95 mock().checkExpectations();
96
97 CHECK(! mock().expectedCallsLeft());
98 CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
99 }
100
101 TEST(MockCallTest, expectOneCallInScopeButNotHappen)
102 {
103
104 MockFailureReporterInstaller failureReporterInstaller;
105
106 MockExpectedCallsListForTest expectations;
107 expectations.addFunction("scope::foobar");
108 MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations);
109
110 mock("scope").expectOneCall("foobar");
111 mock().checkExpectations();
112
113 CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
114
115 }
116
117 TEST(MockCallTest, unexpectedCallHappened)
118 {
119 MockFailureReporterInstaller failureReporterInstaller;
120
121 MockExpectedCallsListForTest emptyExpectations;
122 MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "func", emptyExpectations);
123
124 mock().actualCall("func");
125
126 CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
127 }
128
129 TEST(MockCallTest, unexpectedScopeCallHappened)
130 {
131 MockFailureReporterInstaller failureReporterInstaller;
132
133 MockExpectedCallsListForTest emptyExpectations;
134 MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "scope::func", emptyExpectations);
135
136 mock("scope").actualCall("func");
137
138 CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
139 }
140
141 TEST(MockCallTest, expectOneCallInOneScopeButActualCallInAnotherScope)
142 {
143 MockFailureReporterInstaller failureReporterInstaller;
144
145 MockExpectedCallsListForTest emptyExpectations;
146 MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "class::foo", emptyExpectations);
147
148 mock("scope").expectOneCall("foo");
149 mock("class").actualCall("foo");
150
151 CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
152 mock().clear();
153 }
154
155 TEST(MockCallTest, expectOneCallInScopeButActualCallInGlobal)
156 {
157 MockFailureReporterInstaller failureReporterInstaller;
158
159 MockExpectedCallsListForTest emptyExpectations;
160 MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "foo", emptyExpectations);
161
162 mock("scope").expectOneCall("foo");
163 mock().actualCall("foo");
164
165 CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
166 mock().clear();
167 }
168
169
170 TEST(MockCallTest, expectMultipleSingleCallsThatHappen)
171 {
172 mock().expectOneCall("foo");
173 mock().expectOneCall("foo");
174 mock().actualCall("foo");
175 mock().actualCall("foo");
176 mock().checkExpectations();
177 }
178
179 TEST(MockCallTest, expectOneCallHoweverMultipleHappened)
180 {
181 MockFailureReporterInstaller failureReporterInstaller;
182
183 MockExpectedCallsListForTest expectations;
184 expectations.addFunction("foo")->callWasMade(1);
185 expectations.addFunction("foo")->callWasMade(2);
186 MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "foo", expectations);
187
188 mock().expectOneCall("foo");
189 mock().expectOneCall("foo");
190 mock().actualCall("foo");
191 mock().actualCall("foo");
192 mock().actualCall("foo");
193
194 CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
195 }
196
197 TEST(MockCallTest, expectNoCallThatHappened)
198 {
199 MockFailureReporterInstaller failureReporterInstaller;
200
201 MockExpectedCallsListForTest expectations;
202 expectations.addFunction(0, "lazy");
203 MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "lazy", expectations);
204
205 mock().expectNoCall("lazy");
206 mock().actualCall("lazy");
207
208 CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
209 }
210
211 TEST(MockCallTest, expectNoCallDoesntInfluenceExpectOneCall)
212 {
213 MockFailureReporterInstaller failureReporterInstaller;
214
215 MockExpectedCallsListForTest expectations;
216 expectations.addFunction(0, "lazy");
217 expectations.addFunction("influence")->callWasMade(1);
218 MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "lazy", expectations);
219
220 mock().expectNoCall("lazy");
221 mock().expectOneCall("influence");
222 mock().actualCall("influence");
223 mock().actualCall("lazy");
224
225 CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
226 }
227
228 TEST(MockCallTest, expectNoCallOnlyFailureOnceWhenMultipleHappened)
229 {
230 MockFailureReporterInstaller failureReporterInstaller;
231
232 MockExpectedCallsListForTest expectations;
233 expectations.addFunction(0, "lazy");
234 MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "lazy", expectations);
235
236 mock().expectNoCall("lazy");
237 mock().actualCall("lazy");
238 mock().actualCall("lazy");
239 CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
240 }
241
242 TEST(MockCallTest, ignoreOtherCallsExceptForTheUnExpectedOne)
243 {
244 MockFailureReporterInstaller failureReporterInstaller;
245
246 MockExpectedCallsListForTest expectations;
247 expectations.addFunction(0, "lazy");
248 MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "lazy", expectations);
249
250 mock().expectNoCall("lazy");
251 mock().ignoreOtherCalls();
252 mock().actualCall("bar").withParameter("foo", 1);
253 mock().actualCall("bar1").withParameter("foo", 1);
254 mock().actualCall("bar2").withParameter("foo", 1);
255 mock().actualCall("lazy");
256
257 CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
258 }
259
260
261 TEST(MockCallTest, expectNoCallInScopeThatHappened)
262 {
263 MockFailureReporterInstaller failureReporterInstaller;
264
265 MockExpectedCallsListForTest expectations;
266 expectations.addFunction(0, "scope::lazy");
267 MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "scope::lazy", expectations);
268
269 mock("scope").expectNoCall("lazy");
270 mock("scope").actualCall("lazy");
271
272 CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
273 }
274
275 TEST(MockCallTest, expectNoCallInScopeButActualCallInAnotherScope)
276 {
277 MockFailureReporterInstaller failureReporterInstaller;
278
279 MockExpectedCallsListForTest expectations;
280 MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "scope2::lazy", expectations);
281
282 mock("scope1").expectNoCall("lazy");
283 mock("scope2").actualCall("lazy");
284
285 CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
286 }
287
288 TEST(MockCallTest, expectNoCallInScopeButActualCallInGlobal)
289 {
290 MockFailureReporterInstaller failureReporterInstaller;
291
292 MockExpectedCallsListForTest expectations;
293 MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "lazy", expectations);
294
295 mock("scope1").expectNoCall("lazy");
296 mock().actualCall("lazy");
297
298 CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
299 }
300
301 TEST(MockCallTest, ignoreOtherCallsExceptForTheExpectedOne)
302 {
303 mock().expectOneCall("foo");
304 mock().ignoreOtherCalls();
305 mock().actualCall("bar").withParameter("foo", 1);
306
307 mock().clear();
308 }
309
310 TEST(MockCallTest, ignoreOtherCallsDoesntIgnoreMultipleCallsOfTheSameFunction)
311 {
312 MockFailureReporterInstaller failureReporterInstaller;
313
314 MockExpectedCallsListForTest expectations;
315 expectations.addFunction("foo")->callWasMade(1);
316 MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "foo", expectations);
317
318 mock().expectOneCall("foo");
319 mock().ignoreOtherCalls();
320 mock().actualCall("bar");
321 mock().actualCall("foo");
322 mock().actualCall("foo");
323
324 CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
325 }
326
327 TEST(MockCallTest, ignoreOtherStillFailsIfExpectedOneDidntHappen)
328 {
329 MockFailureReporterInstaller failureReporterInstaller;
330
331 MockExpectedCallsListForTest expectations;
332 expectations.addFunction("foo");
333 MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations);
334
335 mock().expectOneCall("foo");
336 mock().ignoreOtherCalls();
337 mock().checkExpectations();
338
339 CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
340 }
341
342 TEST(MockCallTest, threeExpectedAndActual)
343 {
344 mock().expectOneCall("function1");
345 mock().expectOneCall("function2");
346 mock().expectOneCall("function3");
347 mock().actualCall("function1");
348 mock().actualCall("function2");
349 mock().actualCall("function3");
350
351 mock().checkExpectations();
352 }
353
354 TEST(MockCallTest, disableEnable)
355 {
356 mock().disable();
357 mock().expectOneCall("function");
358 mock().actualCall("differenFunction");
359 CHECK(! mock().expectedCallsLeft());
360 mock().enable();
361 mock().expectOneCall("function");
362 CHECK(mock().expectedCallsLeft());
363 mock().actualCall("function");
364
365 mock().checkExpectations();
366 }
367
368 TEST(MockCallTest, OnObject)
369 {
370 void* objectPtr = (void*) 0x001;
371 mock().expectOneCall("boo").onObject(objectPtr);
372 mock().actualCall("boo").onObject(objectPtr);
373 }
374
375 TEST(MockCallTest, OnObjectIgnored_MatchingAlreadyWhenObjectPassed)
376 {
377 void* objectPtr = (void*) 0x001;
378 mock().expectOneCall("boo");
379 mock().actualCall("boo").onObject(objectPtr);
380 }
381
382 TEST(MockCallTest, OnObjectIgnored_NotMatchingYetWhenObjectPassed)
383 {
384 void* objectPtr = (void*) 0x001;
385 mock().expectOneCall("boo").withBoolParameter("p", true);
386 mock().actualCall("boo").onObject(objectPtr).withBoolParameter("p", true);
387 }
388
389 TEST(MockCallTest, OnObjectIgnored_InitialMatchDiscarded)
390 {
391 void* objectPtr1 = (void*) 0x001;
392 void* objectPtr2 = (void*) 0x002;
393
394 mock().expectOneCall("boo");
395 mock().expectOneCall("boo").withBoolParameter("p", true);
396 mock().actualCall("boo").onObject(objectPtr2).withBoolParameter("p", true);
397 mock().actualCall("boo").onObject(objectPtr1);
398 }
399
400 TEST(MockCallTest, OnObjectFails)
401 {
402 MockFailureReporterInstaller failureReporterInstaller;
403
404 void* objectPtr = (void*) 0x001;
405 void* objectPtr2 = (void*) 0x002;
406 MockExpectedCallsListForTest expectations;
407 expectations.addFunction("boo")->onObject(objectPtr);
408
409 mock().expectOneCall("boo").onObject(objectPtr);
410 mock().actualCall("boo").onObject(objectPtr2);
411
412 MockUnexpectedObjectFailure expectedFailure(mockFailureTest(), "boo", objectPtr2, expectations);
413 CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
414 }
415
416 TEST(MockCallTest, OnObjectExpectedButNotCalled)
417 {
418 MockFailureReporterInstaller failureReporterInstaller;
419
420 void* objectPtr = (void*) 0x001;
421 MockExpectedCallsListForTest expectations;
422 expectations.addFunction("boo")->onObject(objectPtr);
423 expectations.addFunction("boo")->onObject(objectPtr);
424
425 mock().expectOneCall("boo").onObject(objectPtr);
426 mock().expectOneCall("boo").onObject(objectPtr);
427 mock().actualCall("boo");
428 mock().actualCall("boo");
429
430 MockExpectedObjectDidntHappenFailure expectedFailure(mockFailureTest(), "boo", expectations);
431 CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
432 mock().checkExpectations();
433 CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
434 }
435
436 TEST(MockCallTest, expectNCalls_Fulfilled)
437 {
438 mock().expectNCalls(2, "boo");
439 mock().actualCall("boo");
440 mock().actualCall("boo");
441 mock().checkExpectations();
442 }
443
444 TEST(MockCallTest, expectNCalls_NotFulfilled)
445 {
446 MockFailureReporterInstaller failureReporterInstaller;
447
448 MockExpectedCallsListForTest expectations;
449 expectations.addFunction(2, "boo")->callWasMade(1);
450 MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations);
451
452 mock().expectNCalls(2, "boo");
453 mock().actualCall("boo");
454 mock().checkExpectations();
455
456 CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
457 }
458
459 TEST(MockCallTest, shouldntFailTwice)
460 {
461 MockFailureReporterInstaller failureReporterInstaller;
462
463 mock().strictOrder();
464 mock().expectOneCall("foo");
465 mock().expectOneCall("boo");
466 mock().actualCall("boo");
467 mock().actualCall("bar");
468 mock().checkExpectations();
469
470 CHECK(!MockFailureReporterForTest::getReporter()->mockFailureString.contains("bar"));
471 CHECK(MockFailureReporterForTest::getReporter()->mockFailureString.contains("boo"));
472 }
473
474 TEST(MockCallTest, shouldReturnDefaultWhenThereIsntAnythingToReturn)
475 {
476 CHECK(mock().returnValue().equals(MockNamedValue("")));
477 }
478
479 IGNORE_TEST(MockCallTest, testForPerformanceProfiling)
480 {
481 /* TO fix! */
482 mock().expectNCalls(2000, "SimpleFunction");
483 for (int i = 0; i < 2000; i++) {
484 mock().actualCall("SimpleFunction");
485 }
486 }
487
488 static void mocksAreCountedAsChecksTestFunction_()
489 {
490 mock().expectOneCall("foo");
491 mock().expectNCalls(3, "bar");
492 mock().expectNoCall("lazy");
493 mock().clear();
494 }
495
496 TEST(MockCallTest, mockExpectationShouldIncreaseNumberOfChecks)
497 {
498 TestTestingFixture fixture;
499 fixture.setTestFunction(mocksAreCountedAsChecksTestFunction_);
500 fixture.runAllTests();
501 LONGS_EQUAL(3, fixture.getCheckCount());
502 }
503