Skip to content

Commit 7231844

Browse files
committed
ignoreAdditionalCalls (ignoreMultipleCalls?)
1 parent 7ea6f14 commit 7231844

File tree

7 files changed

+113
-2
lines changed

7 files changed

+113
-2
lines changed

include/CppUTestExt/MockSupport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ class MockSupport
9797
virtual void enable();
9898
virtual void tracing(bool enabled);
9999
virtual void ignoreOtherCalls();
100+
virtual void ignoreAdditionalCalls();
100101

101102
virtual void checkExpectations();
102103
virtual bool expectedCallsLeft();
@@ -133,6 +134,7 @@ class MockSupport
133134
MockExpectedCallsList expectations_;
134135
MockExpectedCallsList unExpectations_;
135136
bool ignoreOtherCalls_;
137+
bool ignoreAdditionalCalls_;
136138
bool enabled_;
137139
MockCheckedActualCall *lastActualFunctionCall_;
138140
MockExpectedCallComposite compositeCalls_;
@@ -153,6 +155,7 @@ class MockSupport
153155

154156
bool hasntExpectationWithName(const SimpleString& functionName);
155157
bool hasntUnexpectationWithName(const SimpleString& functionName);
158+
bool hasntUnFulfilledWithName(const SimpleString& functionName);
156159
bool hasCallsOutOfOrder();
157160

158161
SimpleString appendScopeToName(const SimpleString& functionName);

include/CppUTestExt/MockSupport_c.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ struct SMockSupport_c
189189
void (*disable)(void);
190190
void (*enable)(void);
191191
void (*ignoreOtherCalls)(void);
192+
void (*ignoreAdditionalCalls)(void);
192193

193194
void (*checkExpectations)(void);
194195
int (*expectedCallsLeft)(void);

src/CppUTestExt/MockSupport.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ MockSupport& mock(const SimpleString& mockName, MockFailureReporter* failureRepo
4444
}
4545

4646
MockSupport::MockSupport(const SimpleString& mockName)
47-
: callOrder_(0), expectedCallOrder_(0), strictOrdering_(false), standardReporter_(&defaultReporter_), ignoreOtherCalls_(false), enabled_(true), lastActualFunctionCall_(NULL), mockName_(mockName), tracing_(false)
47+
: callOrder_(0), expectedCallOrder_(0), strictOrdering_(false), standardReporter_(&defaultReporter_), ignoreOtherCalls_(false), ignoreAdditionalCalls_(false), enabled_(true), lastActualFunctionCall_(NULL), mockName_(mockName), tracing_(false)
4848
{
4949
setActiveReporter(NULL);
5050
}
@@ -122,6 +122,7 @@ void MockSupport::clear()
122122
unExpectations_.deleteAllExpectationsAndClearList();
123123
compositeCalls_.clear();
124124
ignoreOtherCalls_ = false;
125+
ignoreAdditionalCalls_ = false;
125126
enabled_ = true;
126127
callOrder_ = 0;
127128
expectedCallOrder_ = 0;
@@ -198,6 +199,18 @@ bool MockSupport::hasntUnexpectationWithName(const SimpleString& functionName)
198199
return !unExpectations_.hasExpectationWithName(functionName);
199200
}
200201

202+
bool MockSupport::hasntUnFulfilledWithName(const SimpleString& functionName)
203+
{
204+
if (expectations_.hasExpectationWithName(functionName)) {
205+
MockExpectedCallsList unfulfilledExpectations;
206+
unfulfilledExpectations.addUnfulfilledExpectations(expectations_);
207+
if (!unfulfilledExpectations.hasExpectationWithName(functionName) && ignoreAdditionalCalls_) {
208+
return true;
209+
}
210+
}
211+
return false;
212+
}
213+
201214
MockActualCall& MockSupport::actualCall(const SimpleString& functionName)
202215
{
203216
const SimpleString scopeFuntionName = appendScopeToName(functionName);
@@ -216,6 +229,10 @@ MockActualCall& MockSupport::actualCall(const SimpleString& functionName)
216229
return MockIgnoredActualCall::instance();
217230
}
218231

232+
if (hasntUnFulfilledWithName(scopeFuntionName)) {
233+
return MockIgnoredActualCall::instance();
234+
}
235+
219236
MockCheckedActualCall* call = createActualFunctionCall();
220237
call->withName(scopeFuntionName);
221238
return *call;
@@ -229,6 +246,14 @@ void MockSupport::ignoreOtherCalls()
229246
if (getMockSupport(p)) getMockSupport(p)->ignoreOtherCalls();
230247
}
231248

249+
void MockSupport::ignoreAdditionalCalls()
250+
{
251+
ignoreAdditionalCalls_ = true;
252+
253+
for (MockNamedValueListNode* p = data_.begin(); p; p = p->next())
254+
if (getMockSupport(p)) getMockSupport(p)->ignoreAdditionalCalls();
255+
}
256+
232257
void MockSupport::disable()
233258
{
234259
enabled_ = false;
@@ -436,6 +461,7 @@ MockSupport* MockSupport::clone(const SimpleString& mockName)
436461
MockSupport* newMock = new MockSupport(mockName);
437462
newMock->setMockFailureStandardReporter(standardReporter_);
438463
if (ignoreOtherCalls_) newMock->ignoreOtherCalls();
464+
if (ignoreAdditionalCalls_) newMock->ignoreAdditionalCalls();
439465

440466
if (!enabled_) newMock->disable();
441467

src/CppUTestExt/MockSupport_c.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ MockActualCall_c* actualCall_c(const char* name);
125125
void disable_c();
126126
void enable_c();
127127
void ignoreOtherCalls_c();
128+
void ignoreAdditionalCalls_c();
128129
void setBoolData_c(const char* name, int value);
129130
void setIntData_c(const char* name, int value);
130131
void setUnsignedIntData_c(const char* name, unsigned int value);
@@ -339,6 +340,7 @@ static MockSupport_c gMockSupport = {
339340
disable_c,
340341
enable_c,
341342
ignoreOtherCalls_c,
343+
ignoreAdditionalCalls_c,
342344
checkExpectations_c,
343345
expectedCallsLeft_c,
344346
clear_c,
@@ -814,6 +816,11 @@ void ignoreOtherCalls_c()
814816
currentMockSupport->ignoreOtherCalls();
815817
}
816818

819+
void ignoreAdditionalCalls_c()
820+
{
821+
currentMockSupport->ignoreAdditionalCalls();
822+
}
823+
817824
void setBoolData_c(const char* name, int value)
818825
{
819826
currentMockSupport->setData(name, (value != 0));

tests/CppUTestExt/MockCallTest.cpp

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,22 @@ TEST(MockCallTest, ignoreOtherCallsExceptForTheExpectedOne)
277277
{
278278
mock().expectOneCall("foo");
279279
mock().ignoreOtherCalls();
280-
mock().actualCall("bar").withParameter("foo", 1);;
280+
mock().actualCall("bar").withParameter("foo", 1);
281281

282282
mock().clear();
283283
}
284284

285+
TEST(MockCallTest, ignoreAdditionalCallsExceptForTheExpectedOne)
286+
{
287+
mock().expectOneCall("foo");
288+
mock().ignoreAdditionalCalls();
289+
mock().actualCall("foo");
290+
mock().actualCall("foo");
291+
mock().actualCall("foo").withParameter("foo", 1);
292+
293+
mock().clear();
294+
}
295+
285296
TEST(MockCallTest, ignoreOtherCallsDoesntIgnoreMultipleCallsOfTheSameFunction)
286297
{
287298
MockFailureReporterInstaller failureReporterInstaller;
@@ -299,6 +310,23 @@ TEST(MockCallTest, ignoreOtherCallsDoesntIgnoreMultipleCallsOfTheSameFunction)
299310
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
300311
}
301312

313+
TEST(MockCallTest, ignoreAdditionalCallsOfTheSameFunctionDoesntIgnoreOtherCalls)
314+
{
315+
MockFailureReporterInstaller failureReporterInstaller;
316+
317+
MockExpectedCallsListForTest expectations;
318+
expectations.addFunction("foo")->callWasMade(1);
319+
MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "bar", expectations);
320+
321+
mock().expectOneCall("foo");
322+
mock().ignoreAdditionalCalls();
323+
mock().actualCall("foo");
324+
mock().actualCall("foo");
325+
mock().actualCall("bar");
326+
327+
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
328+
}
329+
302330
TEST(MockCallTest, ignoreOtherStillFailsIfExpectedOneDidntHappen)
303331
{
304332
MockFailureReporterInstaller failureReporterInstaller;
@@ -314,6 +342,21 @@ TEST(MockCallTest, ignoreOtherStillFailsIfExpectedOneDidntHappen)
314342
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
315343
}
316344

345+
TEST(MockCallTest, ignoreAdditionalStillFailsIfExpectedOneDidntHappen)
346+
{
347+
MockFailureReporterInstaller failureReporterInstaller;
348+
349+
MockExpectedCallsListForTest expectations;
350+
expectations.addFunction("foo");
351+
MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations);
352+
353+
mock().expectOneCall("foo");
354+
mock().ignoreAdditionalCalls();
355+
mock().checkExpectations();
356+
357+
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
358+
}
359+
317360
TEST(MockCallTest, threeExpectedAndActual)
318361
{
319362
mock().expectOneCall("function1");

tests/CppUTestExt/MockHierarchyTest.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,27 @@ TEST(MockHierarchyTest, ignoreOtherCallsWorksHierarchicallyWhenDynamicallyCreate
122122
mock().checkExpectations();
123123
}
124124

125+
TEST(MockHierarchyTest, ignoreAdditionalCallsWorksHierarchically)
126+
{
127+
mock("first").expectOneCall("boo");
128+
mock().ignoreAdditionalCalls();
129+
mock("first").actualCall("boo");
130+
mock("first").actualCall("boo");
131+
132+
mock().checkExpectations();
133+
}
134+
135+
TEST(MockHierarchyTest, ignoreAdditionalCallsWorksHierarchicallyWhenDynamicallyCreated)
136+
{
137+
mock().ignoreAdditionalCalls();
138+
mock("first").expectOneCall("boo");
139+
mock("first").actualCall("boo");
140+
mock("first").actualCall("boo");
141+
142+
mock().checkExpectations();
143+
}
144+
145+
125146
TEST(MockHierarchyTest, checkExpectationsWorksHierarchicallyForLastCallNotFinished)
126147
{
127148
MockFailureReporterInstaller failureReporterInstaller;

tests/CppUTestExt/MockSupport_cTest.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,3 +650,13 @@ TEST(MockSupport_c, ignoreOtherCalls)
650650
mock_c()->actualCall("bar");
651651
mock_c()->checkExpectations();
652652
}
653+
654+
TEST(MockSupport_c, ignoreAdditionalCalls)
655+
{
656+
mock_c()->expectOneCall("foo");
657+
mock_c()->ignoreAdditionalCalls();
658+
mock_c()->actualCall("foo");
659+
mock_c()->actualCall("foo");
660+
mock_c()->checkExpectations();
661+
}
662+

0 commit comments

Comments
 (0)