Skip to content

Commit 3e402b9

Browse files
author
Liu Nick
committed
another proposed fix of cpputest#829
1 parent 7a7a43e commit 3e402b9

File tree

6 files changed

+68
-7
lines changed

6 files changed

+68
-7
lines changed

include/CppUTestExt/MockExpectedCallsList.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class MockExpectedCallsList
5353
virtual void addExpectedCall(MockCheckedExpectedCall* call);
5454
virtual void addExpectations(const MockExpectedCallsList& list);
5555
virtual void addExpectationsRelatedTo(const SimpleString& name, const MockExpectedCallsList& list);
56+
57+
virtual void onlyKeepOutOfOrderExpectations();
5658
virtual void addUnfulfilledExpectations(const MockExpectedCallsList& list);
5759

5860
virtual void onlyKeepExpectationsRelatedTo(const SimpleString& name);

include/CppUTestExt/MockSupport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ class MockSupport
149149

150150
bool hasntExpectationWithName(const SimpleString& functionName);
151151
bool hasntUnexpectationWithName(const SimpleString& functionName);
152+
bool hasCallsOutOfOrder();
152153
};
153154

154155
#endif

src/CppUTestExt/MockExpectedCallsList.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ void MockExpectedCallsList::onlyKeepExpectationsRelatedTo(const SimpleString& na
149149
pruneEmptyNodeFromList();
150150
}
151151

152+
void MockExpectedCallsList::onlyKeepOutOfOrderExpectations()
153+
{
154+
for (MockExpectedCallsListNode* p = head_; p; p = p->next_)
155+
if (!p->expectedCall_->isOutOfOrder())
156+
p->expectedCall_ = NULL;
157+
pruneEmptyNodeFromList();
158+
}
159+
152160
void MockExpectedCallsList::onlyKeepUnfulfilledExpectations()
153161
{
154162
for (MockExpectedCallsListNode* p = head_; p; p = p->next_)

src/CppUTestExt/MockFailure.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,13 @@ MockUnexpectedCallHappenedFailure::MockUnexpectedCallHappenedFailure(UtestShell*
115115

116116
MockCallOrderFailure::MockCallOrderFailure(UtestShell* test, const MockExpectedCallsList& expectations) : MockFailure(test)
117117
{
118+
MockExpectedCallsList expectationsForOutOfOrder;
119+
expectationsForOutOfOrder.addExpectations(expectations);
120+
expectationsForOutOfOrder.onlyKeepOutOfOrderExpectations();
121+
118122
message_ = "Mock Failure: Out of order calls";
119123
message_ += "\n";
120-
addExpectationsAndCallHistory(expectations);
124+
addExpectationsAndCallHistory(expectationsForOutOfOrder);
121125
}
122126

123127
MockUnexpectedInputParameterFailure::MockUnexpectedInputParameterFailure(UtestShell* test, const SimpleString& functionName, const MockNamedValue& parameter, const MockExpectedCallsList& expectations) : MockFailure(test)

src/CppUTestExt/MockSupport.cpp

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -310,22 +310,42 @@ void MockSupport::countCheck()
310310
UtestShell::getCurrent()->countCheck();
311311
}
312312

313-
void MockSupport::checkExpectations()
313+
void MockSupport::checkExpectationsOfLastCall()
314314
{
315-
for(MockNamedValueListNode *p = data_.begin(); p; p = p->next())
316-
if(getMockSupport(p) && getMockSupport(p)->lastActualFunctionCall_)
317-
getMockSupport(p)->checkExpectations();
318-
319315
if(lastActualFunctionCall_)
320316
lastActualFunctionCall_->checkExpectations();
321317

318+
for(MockNamedValueListNode *p = data_.begin();p;p = p->next())
319+
if(getMockSupport(p) && getMockSupport(p)->lastActualFunctionCall_)
320+
getMockSupport(p)->lastActualFunctionCall_->checkExpectations();
321+
}
322+
323+
bool MockSupport::hasCallsOutOfOrder()
324+
{
325+
if (expectations_.hasCallsOutOfOrder())
326+
{
327+
return true;
328+
}
329+
for (MockNamedValueListNode* p = data_.begin(); p; p = p->next())
330+
if (getMockSupport(p) && getMockSupport(p)->hasCallsOutOfOrder())
331+
{
332+
return true;
333+
}
334+
return false;
335+
}
336+
337+
void MockSupport::checkExpectations()
338+
{
339+
checkExpectationsOfLastCall();
340+
322341
if (wasLastCallFulfilled() && expectedCallsLeft())
323342
failTestWithUnexpectedCalls();
324343

325-
if (expectations_.hasCallsOutOfOrder())
344+
if (hasCallsOutOfOrder())
326345
failTestWithOutOfOrderCalls();
327346
}
328347

348+
329349
bool MockSupport::hasData(const SimpleString& name)
330350
{
331351
return data_.getValueByName(name) != NULL;

tests/CppUTestExt/MockStrictOrderTest.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,32 @@ TEST(MockStrictOrderTest, orderViolatedWorksHierarchically)
105105
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
106106
}
107107

108+
TEST(MockStrictOrderTest, orderViolatedWorksWithExtraUnexpectedCall)
109+
{
110+
MockFailureReporterInstaller failureReporterInstaller;
111+
mock().strictOrder();
112+
mock("bla").strictOrder();
113+
mock().ignoreOtherCalls();
114+
115+
MockExpectedCallsListForTest expectations;
116+
expectations.addFunction("foo1", 1)->callWasMade(2);
117+
expectations.addFunction("foo2", 2)->callWasMade(1);
118+
MockCallOrderFailure expectedFailure(mockFailureTest(), expectations);
119+
120+
mock("bla").expectOneCall("foo1");
121+
mock("foo").expectOneCall("foo1");
122+
mock("foo").expectOneCall("foo2");
123+
124+
mock("bla").actualCall("foo1");
125+
mock("foo").actualCall("foo2");
126+
mock("foo").actualCall("unexpected1");
127+
mock("foo").actualCall("foo1");
128+
mock("foo").actualCall("unexpected2");
129+
130+
mock().checkExpectations();
131+
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
132+
}
133+
108134
TEST(MockStrictOrderTest, orderViolatedWithinAScope)
109135
{
110136
MockFailureReporterInstaller failureReporterInstaller;

0 commit comments

Comments
 (0)