Skip to content

Commit ade8bbf

Browse files
committed
Preparation of mock library in order to add support for expected calls that can match many actual calls (i.e. range of calls).
The changes are for now just semantic: The nomenclature of "expectation fulfillment"-related methods has been changed, and also some methods have been duplicated and renamed, because expected calls will soon be able to fulfill (match) more than one single actual call, and therefore some of the internal state of expected calls will be devoted to check if the expected call matches the actual call that is being processed, and other state will check if the expected call is fulfilled as a whole (i.e. it has been called/matched a minimum number of times).
1 parent cfef99e commit ade8bbf

File tree

10 files changed

+165
-141
lines changed

10 files changed

+165
-141
lines changed

‎include/CppUTestExt/MockCheckedActualCall.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ class MockCheckedActualCall : public MockActualCall
100100
SimpleString getName() const;
101101
virtual UtestShell* getTest() const;
102102
virtual void callHasSucceeded();
103-
virtual void finalizeOutputParameters(MockCheckedExpectedCall* call);
104-
virtual void finalizeCallWhenFulfilled();
103+
virtual void copyOutputParameters(MockCheckedExpectedCall* call);
104+
virtual void completeCallWhenMatchIsFound();
105105
virtual void failTest(const MockFailure& failure);
106106
virtual void checkInputParameter(const MockNamedValue& actualParameter);
107107
virtual void checkOutputParameter(const MockNamedValue& outputParameter);
@@ -120,9 +120,9 @@ class MockCheckedActualCall : public MockActualCall
120120
MockFailureReporter* reporter_;
121121

122122
ActualCallState state_;
123-
MockCheckedExpectedCall* fulfilledExpectation_;
123+
MockCheckedExpectedCall* matchingExpectation_;
124124

125-
MockExpectedCallsList unfulfilledExpectations_;
125+
MockExpectedCallsList potentiallyMatchingExpectations_;
126126
const MockExpectedCallsList& allExpectations_;
127127

128128
class MockOutputParametersListNode

‎include/CppUTestExt/MockCheckedExpectedCall.h‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,18 @@ class MockCheckedExpectedCall : public MockExpectedCall
8484
virtual bool relatesToObject(const void* objectPtr) const;
8585

8686
virtual bool isFulfilled();
87-
virtual bool isFulfilledWithoutIgnoredParameters();
88-
virtual bool areParametersFulfilled();
89-
virtual bool areIgnoredParametersFulfilled();
87+
virtual bool canMatchActualCalls();
88+
virtual bool isMatchingActualCallAndFinalized();
89+
virtual bool isMatchingActualCall();
90+
virtual bool areParametersMatchingActualCall();
9091
virtual bool isOutOfOrder() const;
9192

9293
virtual void callWasMade(int callOrder);
9394
virtual void inputParameterWasPassed(const SimpleString& name);
9495
virtual void outputParameterWasPassed(const SimpleString& name);
95-
virtual void parametersWereIgnored();
96+
virtual void finalizeActualCallMatch();
9697
virtual void wasPassedToObject();
97-
virtual void resetExpectation();
98+
virtual void resetActualCallMatchingState();
9899

99100
virtual SimpleString callToString();
100101
virtual SimpleString missingParametersToString();
@@ -113,18 +114,18 @@ class MockCheckedExpectedCall : public MockExpectedCall
113114
{
114115
public:
115116
MockExpectedFunctionParameter(const SimpleString& name);
116-
void setFulfilled(bool b);
117-
bool isFulfilled() const;
117+
void setMatchesActualCall(bool b);
118+
bool isMatchingActualCall() const;
118119

119120
private:
120-
bool fulfilled_;
121+
bool matchesActualCall_;
121122
};
122123

123124
MockExpectedFunctionParameter* item(MockNamedValueListNode* node);
124125

125126
bool ignoreOtherParameters_;
126-
bool parametersWereIgnored_;
127-
int callOrder_;
127+
bool isActualCallMatchFinalized_;
128+
int actualCallOrder_;
128129
int expectedCallOrder_;
129130
bool outOfOrder_;
130131
MockNamedValueList* inputParameters_;

‎include/CppUTestExt/MockExpectedCallsList.h‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ class MockExpectedCallsList
4343
virtual int amountOfExpectationsFor(const SimpleString& name) const;
4444
virtual int amountOfUnfulfilledExpectations() const;
4545
virtual bool hasUnfulfilledExpectations() const;
46-
virtual bool hasUnfulfilledExpectationsBecauseOfMissingParameters() const;
46+
virtual bool hasFinalizedMatchingExpectations() const;
47+
virtual bool hasUnmatchingExpectationsBecauseOfMissingParameters() const;
4748
virtual bool hasExpectationWithName(const SimpleString& name) const;
4849
virtual bool hasCallsOutOfOrder() const;
4950
virtual bool isEmpty() const;
@@ -53,21 +54,21 @@ class MockExpectedCallsList
5354
virtual void addExpectationsRelatedTo(const SimpleString& name, const MockExpectedCallsList& list);
5455

5556
virtual void onlyKeepOutOfOrderExpectations();
56-
virtual void addUnfulfilledExpectations(const MockExpectedCallsList& list);
57+
virtual void addPotentiallyMatchingExpectations(const MockExpectedCallsList& list);
5758

5859
virtual void onlyKeepExpectationsRelatedTo(const SimpleString& name);
5960
virtual void onlyKeepExpectationsWithInputParameter(const MockNamedValue& parameter);
6061
virtual void onlyKeepExpectationsWithInputParameterName(const SimpleString& name);
6162
virtual void onlyKeepExpectationsWithOutputParameter(const MockNamedValue& parameter);
6263
virtual void onlyKeepExpectationsWithOutputParameterName(const SimpleString& name);
6364
virtual void onlyKeepExpectationsOnObject(const void* objectPtr);
64-
virtual void onlyKeepUnfulfilledExpectations();
65+
virtual void onlyKeepUnmatchingExpectations();
6566

66-
virtual MockCheckedExpectedCall* removeOneFulfilledExpectation();
67-
virtual MockCheckedExpectedCall* removeOneFulfilledExpectationWithIgnoredParameters();
68-
virtual MockCheckedExpectedCall* getOneFulfilledExpectationWithIgnoredParameters();
67+
virtual MockCheckedExpectedCall* removeFirstFinalizedMatchingExpectation();
68+
virtual MockCheckedExpectedCall* removeFirstMatchingExpectation();
69+
virtual MockCheckedExpectedCall* getFirstMatchingExpectation();
6970

70-
virtual void resetExpectations();
71+
virtual void resetActualCallMatchingState();
7172
virtual void callWasMade(int callOrder);
7273
virtual void wasPassedToObject();
7374
virtual void parameterWasPassed(const SimpleString& parameterName);

‎include/CppUTestExt/MockSupport.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class MockSupport
124124
void countCheck();
125125

126126
private:
127-
int callOrder_;
127+
int actualCallOrder_;
128128
int expectedCallOrder_;
129129
bool strictOrdering_;
130130
MockFailureReporter *activeReporter_;
@@ -142,8 +142,8 @@ class MockSupport
142142

143143
bool tracing_;
144144

145-
void checkExpectationsOfLastCall();
146-
bool wasLastCallFulfilled();
145+
void checkExpectationsOfLastActualCall();
146+
bool wasLastActualCallFulfilled();
147147
void failTestWithUnexpectedCalls();
148148
void failTestWithOutOfOrderCalls();
149149

‎src/CppUTestExt/MockActualCall.cpp‎

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ SimpleString MockCheckedActualCall::getName() const
5050
}
5151

5252
MockCheckedActualCall::MockCheckedActualCall(int callOrder, MockFailureReporter* reporter, const MockExpectedCallsList& allExpectations)
53-
: callOrder_(callOrder), reporter_(reporter), state_(CALL_SUCCEED), fulfilledExpectation_(NULL), allExpectations_(allExpectations), outputParameterExpectations_(NULL)
53+
: callOrder_(callOrder), reporter_(reporter), state_(CALL_SUCCEED), matchingExpectation_(NULL), allExpectations_(allExpectations), outputParameterExpectations_(NULL)
5454
{
55-
unfulfilledExpectations_.addUnfulfilledExpectations(allExpectations);
55+
potentiallyMatchingExpectations_.addPotentiallyMatchingExpectations(allExpectations);
5656
}
5757

5858
MockCheckedActualCall::~MockCheckedActualCall()
@@ -78,7 +78,7 @@ void MockCheckedActualCall::failTest(const MockFailure& failure)
7878
}
7979
}
8080

81-
void MockCheckedActualCall::finalizeOutputParameters(MockCheckedExpectedCall* expectedCall)
81+
void MockCheckedActualCall::copyOutputParameters(MockCheckedExpectedCall* expectedCall)
8282
{
8383
for (MockOutputParametersListNode* p = outputParameterExpectations_; p; p = p->next_)
8484
{
@@ -103,19 +103,19 @@ void MockCheckedActualCall::finalizeOutputParameters(MockCheckedExpectedCall* ex
103103
}
104104
}
105105

106-
void MockCheckedActualCall::finalizeCallWhenFulfilled()
106+
void MockCheckedActualCall::completeCallWhenMatchIsFound()
107107
{
108-
// Expectations that don't ignore parameters have higher fulfillment preference than those that ignore parameters
108+
// Expectations that don't ignore parameters have higher fulfillment preference than those that ignore parameters
109109

110-
fulfilledExpectation_ = unfulfilledExpectations_.removeOneFulfilledExpectation();
111-
if (fulfilledExpectation_) {
112-
finalizeOutputParameters(fulfilledExpectation_);
110+
matchingExpectation_ = potentiallyMatchingExpectations_.removeFirstFinalizedMatchingExpectation();
111+
if (matchingExpectation_) {
112+
copyOutputParameters(matchingExpectation_);
113113
callHasSucceeded();
114114
} else {
115-
MockCheckedExpectedCall* fulfilledExpectationWithIgnoredParameters = unfulfilledExpectations_.getOneFulfilledExpectationWithIgnoredParameters();
116-
if (fulfilledExpectationWithIgnoredParameters) {
117-
finalizeOutputParameters(fulfilledExpectationWithIgnoredParameters);
118-
}
115+
MockCheckedExpectedCall* matchingExpectationWithIgnoredParameters = potentiallyMatchingExpectations_.getFirstMatchingExpectation();
116+
if (matchingExpectationWithIgnoredParameters) {
117+
copyOutputParameters(matchingExpectationWithIgnoredParameters);
118+
}
119119
}
120120
}
121121

@@ -127,29 +127,28 @@ void MockCheckedActualCall::callHasSucceeded()
127127
void MockCheckedActualCall::callIsInProgress()
128128
{
129129
setState(CALL_IN_PROGRESS);
130-
if (fulfilledExpectation_)
130+
if (matchingExpectation_)
131131
{
132-
fulfilledExpectation_->resetExpectation();
133-
fulfilledExpectation_ = NULL;
132+
matchingExpectation_->resetActualCallMatchingState();
133+
matchingExpectation_ = NULL;
134134
}
135-
unfulfilledExpectations_.onlyKeepUnfulfilledExpectations();
135+
potentiallyMatchingExpectations_.onlyKeepUnmatchingExpectations();
136136
}
137137

138138
MockActualCall& MockCheckedActualCall::withName(const SimpleString& name)
139139
{
140140
setName(name);
141141
callIsInProgress();
142142

143-
unfulfilledExpectations_.onlyKeepExpectationsRelatedTo(name);
144-
if (unfulfilledExpectations_.isEmpty()) {
143+
potentiallyMatchingExpectations_.onlyKeepExpectationsRelatedTo(name);
144+
if (potentiallyMatchingExpectations_.isEmpty()) {
145145
MockUnexpectedCallHappenedFailure failure(getTest(), name, allExpectations_);
146146
failTest(failure);
147147
return *this;
148148
}
149149

150-
unfulfilledExpectations_.callWasMade(callOrder_);
151-
152-
finalizeCallWhenFulfilled();
150+
potentiallyMatchingExpectations_.callWasMade(callOrder_);
151+
completeCallWhenMatchIsFound();
153152

154153
return *this;
155154
}
@@ -168,16 +167,16 @@ void MockCheckedActualCall::checkInputParameter(const MockNamedValue& actualPara
168167

169168
callIsInProgress();
170169

171-
unfulfilledExpectations_.onlyKeepExpectationsWithInputParameter(actualParameter);
170+
potentiallyMatchingExpectations_.onlyKeepExpectationsWithInputParameter(actualParameter);
172171

173-
if (unfulfilledExpectations_.isEmpty()) {
172+
if (potentiallyMatchingExpectations_.isEmpty()) {
174173
MockUnexpectedInputParameterFailure failure(getTest(), getName(), actualParameter, allExpectations_);
175174
failTest(failure);
176175
return;
177176
}
178177

179-
unfulfilledExpectations_.parameterWasPassed(actualParameter.getName());
180-
finalizeCallWhenFulfilled();
178+
potentiallyMatchingExpectations_.parameterWasPassed(actualParameter.getName());
179+
completeCallWhenMatchIsFound();
181180
}
182181

183182
void MockCheckedActualCall::checkOutputParameter(const MockNamedValue& outputParameter)
@@ -189,16 +188,16 @@ void MockCheckedActualCall::checkOutputParameter(const MockNamedValue& outputPar
189188

190189
callIsInProgress();
191190

192-
unfulfilledExpectations_.onlyKeepExpectationsWithOutputParameter(outputParameter);
191+
potentiallyMatchingExpectations_.onlyKeepExpectationsWithOutputParameter(outputParameter);
193192

194-
if (unfulfilledExpectations_.isEmpty()) {
193+
if (potentiallyMatchingExpectations_.isEmpty()) {
195194
MockUnexpectedOutputParameterFailure failure(getTest(), getName(), outputParameter, allExpectations_);
196195
failTest(failure);
197196
return;
198197
}
199198

200-
unfulfilledExpectations_.outputParameterWasPassed(outputParameter.getName());
201-
finalizeCallWhenFulfilled();
199+
potentiallyMatchingExpectations_.outputParameterWasPassed(outputParameter.getName());
200+
completeCallWhenMatchIsFound();
202201
}
203202

204203
MockActualCall& MockCheckedActualCall::withBoolParameter(const SimpleString& name, bool value)
@@ -339,21 +338,22 @@ void MockCheckedActualCall::checkExpectations()
339338
{
340339
if (state_ != CALL_IN_PROGRESS)
341340
{
342-
unfulfilledExpectations_.resetExpectations();
341+
potentiallyMatchingExpectations_.resetActualCallMatchingState();
343342
return;
344343
}
345344

346-
if (! unfulfilledExpectations_.hasUnfulfilledExpectations())
347-
FAIL("Actual call is in progress. Checking expectations. But no unfulfilled expectations. Cannot happen.") // LCOV_EXCL_LINE
345+
if (potentiallyMatchingExpectations_.hasFinalizedMatchingExpectations())
346+
FAIL("Actual call is in progress, but there are finalized matching expectations when checking expectations. This cannot happen.") // LCOV_EXCL_LINE
348347

349-
fulfilledExpectation_ = unfulfilledExpectations_.removeOneFulfilledExpectationWithIgnoredParameters();
350-
if (fulfilledExpectation_) {
348+
matchingExpectation_ = potentiallyMatchingExpectations_.removeFirstMatchingExpectation();
349+
if (matchingExpectation_) {
350+
matchingExpectation_->finalizeActualCallMatch();
351351
callHasSucceeded();
352-
unfulfilledExpectations_.resetExpectations();
352+
potentiallyMatchingExpectations_.resetActualCallMatchingState();
353353
return;
354354
}
355355

356-
if (unfulfilledExpectations_.hasUnfulfilledExpectationsBecauseOfMissingParameters()) {
356+
if (potentiallyMatchingExpectations_.hasUnmatchingExpectationsBecauseOfMissingParameters()) {
357357
MockExpectedParameterDidntHappenFailure failure(getTest(), getName(), allExpectations_);
358358
failTest(failure);
359359
}
@@ -371,8 +371,8 @@ void MockCheckedActualCall::setState(ActualCallState state)
371371
MockNamedValue MockCheckedActualCall::returnValue()
372372
{
373373
checkExpectations();
374-
if (fulfilledExpectation_)
375-
return fulfilledExpectation_->returnValue();
374+
if (matchingExpectation_)
375+
return matchingExpectation_->returnValue();
376376
return MockNamedValue("no return value");
377377
}
378378

@@ -513,19 +513,23 @@ bool MockCheckedActualCall::hasReturnValue()
513513

514514
MockActualCall& MockCheckedActualCall::onObject(const void* objectPtr)
515515
{
516+
if(hasFailed()) {
517+
return *this;
518+
}
519+
516520
callIsInProgress();
517521

518-
unfulfilledExpectations_.onlyKeepExpectationsOnObject(objectPtr);
522+
potentiallyMatchingExpectations_.onlyKeepExpectationsOnObject(objectPtr);
519523

520-
if (unfulfilledExpectations_.isEmpty()) {
524+
if (potentiallyMatchingExpectations_.isEmpty()) {
521525
MockUnexpectedObjectFailure failure(getTest(), getName(), objectPtr, allExpectations_);
522526
failTest(failure);
523527
return *this;
524528
}
525529

526-
unfulfilledExpectations_.wasPassedToObject();
530+
potentiallyMatchingExpectations_.wasPassedToObject();
531+
completeCallWhenMatchIsFound();
527532

528-
finalizeCallWhenFulfilled();
529533
return *this;
530534
}
531535

0 commit comments

Comments
 (0)