Skip to content

Commit f3563d5

Browse files
authored
Merge pull request cpputest#1023 from jgonzalezdr/expected-calls-multimatch-7
Expected calls matching several actual calls - (3) Implementation of "multi-matching" expectedCalls.
2 parents ccd7cb4 + 5ac48ad commit f3563d5

17 files changed

+505
-713
lines changed

‎include/CppUTestExt/MockCheckedActualCall.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class MockCheckedActualCall : public MockActualCall
105105
virtual void failTest(const MockFailure& failure);
106106
virtual void checkInputParameter(const MockNamedValue& actualParameter);
107107
virtual void checkOutputParameter(const MockNamedValue& outputParameter);
108-
virtual void callIsInProgress();
108+
virtual void discardCurrentlyMatchingExpectations();
109109

110110
enum ActualCallState {
111111
CALL_IN_PROGRESS,
@@ -120,6 +120,7 @@ class MockCheckedActualCall : public MockActualCall
120120
MockFailureReporter* reporter_;
121121

122122
ActualCallState state_;
123+
bool expectationsChecked_;
123124
MockCheckedExpectedCall* matchingExpectation_;
124125

125126
MockExpectedCallsList potentiallyMatchingExpectations_;

‎include/CppUTestExt/MockCheckedExpectedCall.h‎

Lines changed: 11 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ class MockCheckedExpectedCall : public MockExpectedCall
3636

3737
public:
3838
MockCheckedExpectedCall();
39+
MockCheckedExpectedCall(unsigned int numCalls);
3940
virtual ~MockCheckedExpectedCall();
4041

4142
virtual MockExpectedCall& withName(const SimpleString& name) _override;
42-
virtual MockExpectedCall& withCallOrder(unsigned int callOrder) _override;
43+
virtual MockExpectedCall& withCallOrder(unsigned int callOrder) _override { return withCallOrder(callOrder, callOrder); }
44+
virtual MockExpectedCall& withCallOrder(unsigned int initialCallOrder, unsigned int finalCallOrder) _override;
4345
virtual MockExpectedCall& withBoolParameter(const SimpleString& name, bool value) _override;
4446
virtual MockExpectedCall& withIntParameter(const SimpleString& name, int value) _override;
4547
virtual MockExpectedCall& withUnsignedIntParameter(const SimpleString& name, unsigned int value) _override;
@@ -100,8 +102,9 @@ class MockCheckedExpectedCall : public MockExpectedCall
100102
virtual SimpleString callToString();
101103
virtual SimpleString missingParametersToString();
102104

103-
enum { NOT_CALLED_YET = 0, NO_EXPECTED_CALL_ORDER = 0 };
104-
virtual unsigned int getCallOrder() const;
105+
enum { NO_EXPECTED_CALL_ORDER = 0 };
106+
107+
virtual unsigned int getActualCallsFulfilled() const;
105108

106109
protected:
107110
void setName(const SimpleString& name);
@@ -125,58 +128,16 @@ class MockCheckedExpectedCall : public MockExpectedCall
125128

126129
bool ignoreOtherParameters_;
127130
bool isActualCallMatchFinalized_;
128-
unsigned int actualCallOrder_;
129-
unsigned int expectedCallOrder_;
131+
unsigned int initialExpectedCallOrder_;
132+
unsigned int finalExpectedCallOrder_;
130133
bool outOfOrder_;
131134
MockNamedValueList* inputParameters_;
132135
MockNamedValueList* outputParameters_;
133136
MockNamedValue returnValue_;
134137
void* objectPtr_;
135138
bool wasPassedToObject_;
136-
};
137-
138-
struct MockExpectedCallCompositeNode;
139-
class MockExpectedCallComposite : public MockExpectedCall
140-
{
141-
public:
142-
MockExpectedCallComposite();
143-
virtual ~MockExpectedCallComposite();
144-
145-
virtual MockExpectedCall& withName(const SimpleString& name) _override;
146-
virtual MockExpectedCall& withCallOrder(unsigned int callOrder) _override;
147-
virtual MockExpectedCall& withBoolParameter(const SimpleString& name, bool value) _override;
148-
virtual MockExpectedCall& withIntParameter(const SimpleString& name, int value) _override;
149-
virtual MockExpectedCall& withUnsignedIntParameter(const SimpleString& name, unsigned int value) _override;
150-
virtual MockExpectedCall& withLongIntParameter(const SimpleString& name, long int value) _override;
151-
virtual MockExpectedCall& withUnsignedLongIntParameter(const SimpleString& name, unsigned long int value) _override;
152-
virtual MockExpectedCall& withDoubleParameter(const SimpleString& name, double value) _override;
153-
virtual MockExpectedCall& withStringParameter(const SimpleString& name, const char* value) _override;
154-
virtual MockExpectedCall& withConstPointerParameter(const SimpleString& name, const void* value) _override;
155-
virtual MockExpectedCall& withPointerParameter(const SimpleString& name, void* value) _override;
156-
virtual MockExpectedCall& withFunctionPointerParameter(const SimpleString& name, void (*value)()) _override;
157-
virtual MockExpectedCall& withMemoryBufferParameter(const SimpleString& name, const unsigned char* value, size_t size) _override;
158-
virtual MockExpectedCall& withParameterOfType(const SimpleString& typeName, const SimpleString& name, const void* value) _override;
159-
virtual MockExpectedCall& withOutputParameterReturning(const SimpleString& name, const void* value, size_t size) _override;
160-
virtual MockExpectedCall& withOutputParameterOfTypeReturning(const SimpleString& typeName, const SimpleString& name, const void* value) _override;
161-
virtual MockExpectedCall& ignoreOtherParameters() _override;
162-
163-
virtual MockExpectedCall& andReturnValue(bool value) _override;
164-
virtual MockExpectedCall& andReturnValue(int value) _override;
165-
virtual MockExpectedCall& andReturnValue(unsigned int value) _override;
166-
virtual MockExpectedCall& andReturnValue(long int value) _override;
167-
virtual MockExpectedCall& andReturnValue(unsigned long int value) _override;
168-
virtual MockExpectedCall& andReturnValue(double value) _override;
169-
virtual MockExpectedCall& andReturnValue(const char* value) _override;
170-
virtual MockExpectedCall& andReturnValue(void* value) _override;
171-
virtual MockExpectedCall& andReturnValue(const void* value) _override;
172-
virtual MockExpectedCall& andReturnValue(void (*value)()) _override;
173-
174-
virtual MockExpectedCall& onObject(void* objectPtr) _override;
175-
176-
virtual void add(MockExpectedCall& call);
177-
virtual void clear();
178-
private:
179-
MockExpectedCallCompositeNode* head_;
139+
unsigned int actualCalls_;
140+
unsigned int expectedCalls_;
180141
};
181142

182143
class MockIgnoredExpectedCall: public MockExpectedCall
@@ -185,6 +146,7 @@ class MockIgnoredExpectedCall: public MockExpectedCall
185146

186147
virtual MockExpectedCall& withName(const SimpleString&) _override { return *this;}
187148
virtual MockExpectedCall& withCallOrder(unsigned int) _override { return *this; }
149+
virtual MockExpectedCall& withCallOrder(unsigned int, unsigned int) _override { return *this; }
188150
virtual MockExpectedCall& withBoolParameter(const SimpleString&, bool) _override { return *this; }
189151
virtual MockExpectedCall& withIntParameter(const SimpleString&, int) _override { return *this; }
190152
virtual MockExpectedCall& withUnsignedIntParameter(const SimpleString&, unsigned int) _override{ return *this; }

‎include/CppUTestExt/MockExpectedCall.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class MockExpectedCall
4040

4141
virtual MockExpectedCall& withName(const SimpleString& name)=0;
4242
virtual MockExpectedCall& withCallOrder(unsigned int)=0;
43+
virtual MockExpectedCall& withCallOrder(unsigned int, unsigned int)=0;
4344
MockExpectedCall& withParameter(const SimpleString& name, bool value) { return withBoolParameter(name, value); }
4445
MockExpectedCall& withParameter(const SimpleString& name, int value) { return withIntParameter(name, value); }
4546
MockExpectedCall& withParameter(const SimpleString& name, unsigned int value) { return withUnsignedIntParameter(name, value); }

‎include/CppUTestExt/MockExpectedCallsList.h‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class MockExpectedCallsList
4040
virtual void deleteAllExpectationsAndClearList();
4141

4242
virtual unsigned int size() const;
43-
virtual unsigned int amountOfExpectationsFor(const SimpleString& name) const;
43+
virtual unsigned int amountOfActualCallsFulfilledFor(const SimpleString& name) const;
4444
virtual unsigned int amountOfUnfulfilledExpectations() const;
4545
virtual bool hasUnfulfilledExpectations() const;
4646
virtual bool hasFinalizedMatchingExpectations() const;
@@ -69,7 +69,6 @@ class MockExpectedCallsList
6969
virtual MockCheckedExpectedCall* getFirstMatchingExpectation();
7070

7171
virtual void resetActualCallMatchingState();
72-
virtual void callWasMade(unsigned int callOrder);
7372
virtual void wasPassedToObject();
7473
virtual void parameterWasPassed(const SimpleString& parameterName);
7574
virtual void outputParameterWasPassed(const SimpleString& parameterName);
@@ -91,7 +90,6 @@ class MockExpectedCallsList
9190
: expectedCall_(expectedCall), next_(NULL) {}
9291
};
9392

94-
virtual MockExpectedCallsListNode* findNodeWithCallOrderOf(unsigned int callOrder) const;
9593
private:
9694
MockExpectedCallsListNode* head_;
9795

‎include/CppUTestExt/MockSupport.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ class MockSupport
135135
bool ignoreOtherCalls_;
136136
bool enabled_;
137137
MockCheckedActualCall *lastActualFunctionCall_;
138-
MockExpectedCallComposite compositeCalls_;
139138
MockNamedValueComparatorsAndCopiersRepository comparatorsAndCopiersRepository_;
140139
MockNamedValueList data_;
141140
const SimpleString mockName_;
@@ -144,7 +143,7 @@ class MockSupport
144143

145144
void checkExpectationsOfLastActualCall();
146145
bool wasLastActualCallFulfilled();
147-
void failTestWithUnexpectedCalls();
146+
void failTestWithExpectedCallsNotFulfilled();
148147
void failTestWithOutOfOrderCalls();
149148

150149
MockNamedValue* retrieveDataFromStore(const SimpleString& name);

‎src/CppUTestExt/MockActualCall.cpp‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ SimpleString MockCheckedActualCall::getName() const
5050
}
5151

5252
MockCheckedActualCall::MockCheckedActualCall(unsigned int callOrder, MockFailureReporter* reporter, const MockExpectedCallsList& allExpectations)
53-
: callOrder_(callOrder), reporter_(reporter), state_(CALL_SUCCEED), matchingExpectation_(NULL), allExpectations_(allExpectations), outputParameterExpectations_(NULL)
53+
: callOrder_(callOrder), reporter_(reporter), state_(CALL_SUCCEED), expectationsChecked_(false), matchingExpectation_(NULL),
54+
allExpectations_(allExpectations), outputParameterExpectations_(NULL)
5455
{
5556
potentiallyMatchingExpectations_.addPotentiallyMatchingExpectations(allExpectations);
5657
}
@@ -124,9 +125,8 @@ void MockCheckedActualCall::callHasSucceeded()
124125
setState(CALL_SUCCEED);
125126
}
126127

127-
void MockCheckedActualCall::callIsInProgress()
128+
void MockCheckedActualCall::discardCurrentlyMatchingExpectations()
128129
{
129-
setState(CALL_IN_PROGRESS);
130130
if (matchingExpectation_)
131131
{
132132
matchingExpectation_->resetActualCallMatchingState();
@@ -138,7 +138,7 @@ void MockCheckedActualCall::callIsInProgress()
138138
MockActualCall& MockCheckedActualCall::withName(const SimpleString& name)
139139
{
140140
setName(name);
141-
callIsInProgress();
141+
setState(CALL_IN_PROGRESS);
142142

143143
potentiallyMatchingExpectations_.onlyKeepExpectationsRelatedTo(name);
144144
if (potentiallyMatchingExpectations_.isEmpty()) {
@@ -147,7 +147,6 @@ MockActualCall& MockCheckedActualCall::withName(const SimpleString& name)
147147
return *this;
148148
}
149149

150-
potentiallyMatchingExpectations_.callWasMade(callOrder_);
151150
completeCallWhenMatchIsFound();
152151

153152
return *this;
@@ -165,7 +164,8 @@ void MockCheckedActualCall::checkInputParameter(const MockNamedValue& actualPara
165164
return;
166165
}
167166

168-
callIsInProgress();
167+
setState(CALL_IN_PROGRESS);
168+
discardCurrentlyMatchingExpectations();
169169

170170
potentiallyMatchingExpectations_.onlyKeepExpectationsWithInputParameter(actualParameter);
171171

@@ -186,7 +186,8 @@ void MockCheckedActualCall::checkOutputParameter(const MockNamedValue& outputPar
186186
return;
187187
}
188188

189-
callIsInProgress();
189+
setState(CALL_IN_PROGRESS);
190+
discardCurrentlyMatchingExpectations();
190191

191192
potentiallyMatchingExpectations_.onlyKeepExpectationsWithOutputParameter(outputParameter);
192193

@@ -336,8 +337,16 @@ bool MockCheckedActualCall::hasFailed() const
336337

337338
void MockCheckedActualCall::checkExpectations()
338339
{
339-
if (state_ != CALL_IN_PROGRESS)
340-
{
340+
if(expectationsChecked_) {
341+
return;
342+
}
343+
344+
expectationsChecked_ = true;
345+
346+
if (state_ != CALL_IN_PROGRESS) {
347+
if(state_ == CALL_SUCCEED) {
348+
matchingExpectation_->callWasMade(callOrder_);
349+
}
341350
potentiallyMatchingExpectations_.resetActualCallMatchingState();
342351
return;
343352
}
@@ -349,6 +358,7 @@ void MockCheckedActualCall::checkExpectations()
349358
if (matchingExpectation_) {
350359
matchingExpectation_->finalizeActualCallMatch();
351360
callHasSucceeded();
361+
matchingExpectation_->callWasMade(callOrder_);
352362
potentiallyMatchingExpectations_.resetActualCallMatchingState();
353363
return;
354364
}
@@ -517,7 +527,8 @@ MockActualCall& MockCheckedActualCall::onObject(const void* objectPtr)
517527
return *this;
518528
}
519529

520-
callIsInProgress();
530+
setState(CALL_IN_PROGRESS);
531+
discardCurrentlyMatchingExpectations();
521532

522533
potentiallyMatchingExpectations_.onlyKeepExpectationsOnObject(objectPtr);
523534

0 commit comments

Comments
 (0)