Skip to content

Expected calls matching several actual calls #1018

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
dae0892
Preparation of mock library in order to add support for expected call…
jgonzalezdr Jun 21, 2016
ed56f39
Basic implementation of "multi-matching" expectedCalls.
jgonzalezdr Jun 26, 2016
8ee564a
Added C language new calls for multi-matching expected calls.
jgonzalezdr Jun 26, 2016
0d2a162
Removed MockExpectedCallComposite.
jgonzalezdr Jun 26, 2016
e94cd93
Added test that checks that non-fulfilled calls have higher matching …
jgonzalezdr Jun 27, 2016
f42377e
When creating the list of potentially matching expected calls, non-fu…
jgonzalezdr Jun 27, 2016
162d117
Renamed MockExpectedCallsDidntHappenFailure to MockExpectedCallsNotFu…
jgonzalezdr Jun 27, 2016
94c5c69
Re-enabled strict ordering checks when the expected calls have no opt…
jgonzalezdr Jun 27, 2016
84593a9
Fixed declaration of MockExpectedCallsListForTest::addFunction to avo…
jgonzalezdr Jun 27, 2016
49f3b7d
Fixed declaration of call order related methods and attributes to use…
jgonzalezdr Jun 27, 2016
60828dc
Updated some unit tests and added a new one to improve code coverage.
jgonzalezdr Jun 28, 2016
a12e83b
Modified MockSupport::expectNoCall() method implementation to use a m…
jgonzalezdr Jun 28, 2016
6b5db95
Removed superfluous constructor and method in MockExpectedCall that w…
jgonzalezdr Jun 28, 2016
412ca5e
Fixed the mock scope not being properly reported when failing because…
jgonzalezdr Jun 30, 2016
a80efda
Added a class to store a queue of actual calls, needed to add a log o…
jgonzalezdr Jul 3, 2016
0f4623a
Modified MockCheckedActualCall to store a reference to its associated…
jgonzalezdr Jul 3, 2016
bc33181
Added functionality to MockCheckedActualCall and MockActualCallsQueue…
jgonzalezdr Jul 3, 2016
044a693
Added actual calls reporting to MockFailure, and preliminary non-func…
jgonzalezdr Jul 4, 2016
b3a20a4
Fixed compilation for Dos platform.
jgonzalezdr Jul 4, 2016
eb3aae1
Implemented a log of actual calls in MockSupport, such that mock fail…
jgonzalezdr Jul 5, 2016
fb5653f
Added functionality to MockSupport to limit the maximum size of the a…
jgonzalezdr Jul 5, 2016
3999548
Added C language new call for setMaxCallLogSize.
jgonzalezdr Jul 5, 2016
faf07f9
Added unit test for MockActualCallsList::hasFinalizedMatchingExpectat…
jgonzalezdr Jul 5, 2016
e912650
Improved unit test for MockSupport::setMaxCallLogSize().
jgonzalezdr Jul 7, 2016
111f0f0
Improved unit tests for MockExpectedCall.
jgonzalezdr Jul 7, 2016
ef47ef7
Removed old commented code that was left over.
jgonzalezdr Jul 7, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Basic implementation of "multi-matching" expectedCalls.
Now expectedCalls can match multiple actual calls, between a minimum and a maximum number of calls as defined by the user.

The method expectNCalls is not handled any more as just a collection of N expectedCall objects, but just a single expectedCall that requires exactly N calls to be considered fulfilled.

Other methods where also added to MockSupport to expect a range of calls (between a minimum and a maximum), at least a number of calls (i.e. minimum), at most a number of calls (i.e. maximum), and any number of calls.

Strict ordering checks are not working for the moment, except when all expected calls are for exactly 1 call (i.e. only using expectOneCall).

Actual calls order is not registered properly, so for the meantime failure messages just show the expectations that were fulfilled.

Composite calls are not needed any more, they have been disabled by pre-compiling them out (#if 0) along with their tests.
  • Loading branch information
jgonzalezdr committed Jun 28, 2016
commit ed56f3916328c7ce9cc6b3a96d25051fae5f4594
3 changes: 2 additions & 1 deletion include/CppUTestExt/MockCheckedActualCall.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class MockCheckedActualCall : public MockActualCall
virtual void failTest(const MockFailure& failure);
virtual void checkInputParameter(const MockNamedValue& actualParameter);
virtual void checkOutputParameter(const MockNamedValue& outputParameter);
virtual void callIsInProgress();
virtual void discardCurrentlyMatchingExpectations();

enum ActualCallState {
CALL_IN_PROGRESS,
Expand All @@ -120,6 +120,7 @@ class MockCheckedActualCall : public MockActualCall
MockFailureReporter* reporter_;

ActualCallState state_;
bool expectationsChecked_;
MockCheckedExpectedCall* matchingExpectation_;

MockExpectedCallsList potentiallyMatchingExpectations_;
Expand Down
8 changes: 8 additions & 0 deletions include/CppUTestExt/MockCheckedExpectedCall.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class MockCheckedExpectedCall : public MockExpectedCall

public:
MockCheckedExpectedCall();
MockCheckedExpectedCall(unsigned int minCalls, unsigned int maxCalls);
virtual ~MockCheckedExpectedCall();

virtual MockExpectedCall& withName(const SimpleString& name) _override;
Expand Down Expand Up @@ -103,6 +104,8 @@ class MockCheckedExpectedCall : public MockExpectedCall
enum { NOT_CALLED_YET = -1, NO_EXPECTED_CALL_ORDER = -1};
virtual int getCallOrder() const;

virtual unsigned int getMaxCalls() const;

protected:
void setName(const SimpleString& name);
SimpleString getName() const;
Expand Down Expand Up @@ -133,8 +136,12 @@ class MockCheckedExpectedCall : public MockExpectedCall
MockNamedValue returnValue_;
void* objectPtr_;
bool wasPassedToObject_;
unsigned int actualCalls_;
unsigned int minCalls_;
unsigned int maxCalls_;
};

#if 0
struct MockExpectedCallCompositeNode;
class MockExpectedCallComposite : public MockExpectedCall
{
Expand Down Expand Up @@ -178,6 +185,7 @@ class MockExpectedCallComposite : public MockExpectedCall
private:
MockExpectedCallCompositeNode* head_;
};
#endif

class MockIgnoredExpectedCall: public MockExpectedCall
{
Expand Down
8 changes: 3 additions & 5 deletions include/CppUTestExt/MockExpectedCallsList.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ class MockExpectedCallsList
virtual ~MockExpectedCallsList();
virtual void deleteAllExpectationsAndClearList();

virtual int size() const;
virtual int amountOfExpectationsFor(const SimpleString& name) const;
virtual int amountOfUnfulfilledExpectations() const;
virtual unsigned int size() const;
virtual unsigned int amountOfExpectationsFor(const SimpleString& name) const;
virtual unsigned int amountOfUnfulfilledExpectations() const;
virtual bool hasUnfulfilledExpectations() const;
virtual bool hasFinalizedMatchingExpectations() const;
virtual bool hasUnmatchingExpectationsBecauseOfMissingParameters() const;
Expand Down Expand Up @@ -69,7 +69,6 @@ class MockExpectedCallsList
virtual MockCheckedExpectedCall* getFirstMatchingExpectation();

virtual void resetActualCallMatchingState();
virtual void callWasMade(int callOrder);
virtual void wasPassedToObject();
virtual void parameterWasPassed(const SimpleString& parameterName);
virtual void outputParameterWasPassed(const SimpleString& parameterName);
Expand All @@ -91,7 +90,6 @@ class MockExpectedCallsList
: expectedCall_(expectedCall), next_(NULL) {}
};

virtual MockExpectedCallsListNode* findNodeWithCallOrderOf(int callOrder) const;
private:
MockExpectedCallsListNode* head_;

Expand Down
14 changes: 11 additions & 3 deletions include/CppUTestExt/MockSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ class MockSupport
virtual void strictOrder();
virtual MockExpectedCall& expectOneCall(const SimpleString& functionName);
virtual void expectNoCall(const SimpleString& functionName);
virtual MockExpectedCall& expectNCalls(int amount, const SimpleString& functionName);
virtual MockExpectedCall& expectNCalls(unsigned int amount, const SimpleString& functionName);
virtual MockExpectedCall& expectAtLeastOneCall(const SimpleString& functionName);
virtual MockExpectedCall& expectAtLeastNCalls(unsigned int amount, const SimpleString& functionName);
virtual MockExpectedCall& expectAtMostOneCall(const SimpleString& functionName);
virtual MockExpectedCall& expectAtMostNCalls(unsigned int amount, const SimpleString& functionName);
virtual MockExpectedCall& expectAnyCalls(const SimpleString& functionName);
virtual MockExpectedCall& expectRangeOfCalls(unsigned int minCalls, unsigned int maxCalls, const SimpleString& functionName);
virtual MockActualCall& actualCall(const SimpleString& functionName);
virtual bool hasReturnValue();
virtual MockNamedValue returnValue();
Expand Down Expand Up @@ -124,7 +130,7 @@ class MockSupport
void countCheck();

private:
int callOrder_;
int actualCallOrder_;
int expectedCallOrder_;
bool strictOrdering_;
MockFailureReporter *activeReporter_;
Expand All @@ -135,7 +141,9 @@ class MockSupport
bool ignoreOtherCalls_;
bool enabled_;
MockCheckedActualCall *lastActualFunctionCall_;
#if 0
MockExpectedCallComposite compositeCalls_;
#endif
MockNamedValueComparatorsAndCopiersRepository comparatorsAndCopiersRepository_;
MockNamedValueList data_;
const SimpleString mockName_;
Expand All @@ -144,7 +152,7 @@ class MockSupport

void checkExpectationsOfLastActualCall();
bool wasLastActualCallFulfilled();
void failTestWithUnexpectedCalls();
void failTestWithExpectedCallsNotFulfilled();
void failTestWithOutOfOrderCalls();

MockNamedValue* retrieveDataFromStore(const SimpleString& name);
Expand Down
29 changes: 21 additions & 8 deletions src/CppUTestExt/MockActualCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ SimpleString MockCheckedActualCall::getName() const
}

MockCheckedActualCall::MockCheckedActualCall(int callOrder, MockFailureReporter* reporter, const MockExpectedCallsList& allExpectations)
: callOrder_(callOrder), reporter_(reporter), state_(CALL_SUCCEED), matchingExpectation_(NULL), allExpectations_(allExpectations), outputParameterExpectations_(NULL)
: callOrder_(callOrder), reporter_(reporter), state_(CALL_SUCCEED), expectationsChecked_(false), matchingExpectation_(NULL), allExpectations_(allExpectations), outputParameterExpectations_(NULL)
{
potentiallyMatchingExpectations_.addPotentiallyMatchingExpectations(allExpectations);
}
Expand Down Expand Up @@ -124,9 +124,8 @@ void MockCheckedActualCall::callHasSucceeded()
setState(CALL_SUCCEED);
}

void MockCheckedActualCall::callIsInProgress()
void MockCheckedActualCall::discardCurrentlyMatchingExpectations()
{
setState(CALL_IN_PROGRESS);
if (matchingExpectation_)
{
matchingExpectation_->resetActualCallMatchingState();
Expand All @@ -138,7 +137,7 @@ void MockCheckedActualCall::callIsInProgress()
MockActualCall& MockCheckedActualCall::withName(const SimpleString& name)
{
setName(name);
callIsInProgress();
setState(CALL_IN_PROGRESS);

potentiallyMatchingExpectations_.onlyKeepExpectationsRelatedTo(name);
if (potentiallyMatchingExpectations_.isEmpty()) {
Expand All @@ -147,7 +146,6 @@ MockActualCall& MockCheckedActualCall::withName(const SimpleString& name)
return *this;
}

potentiallyMatchingExpectations_.callWasMade(callOrder_);
finalizeCallWhenMatchIsFound();

return *this;
Expand All @@ -165,7 +163,8 @@ void MockCheckedActualCall::checkInputParameter(const MockNamedValue& actualPara
return;
}

callIsInProgress();
setState(CALL_IN_PROGRESS);
discardCurrentlyMatchingExpectations();

potentiallyMatchingExpectations_.onlyKeepExpectationsWithInputParameter(actualParameter);

Expand All @@ -186,7 +185,8 @@ void MockCheckedActualCall::checkOutputParameter(const MockNamedValue& outputPar
return;
}

callIsInProgress();
setState(CALL_IN_PROGRESS);
discardCurrentlyMatchingExpectations();

potentiallyMatchingExpectations_.onlyKeepExpectationsWithOutputParameter(outputParameter);

Expand Down Expand Up @@ -336,8 +336,19 @@ bool MockCheckedActualCall::hasFailed() const

void MockCheckedActualCall::checkExpectations()
{
if(expectationsChecked_)
{
return;
}

expectationsChecked_ = true;

if (state_ != CALL_IN_PROGRESS)
{
if(state_ == CALL_SUCCEED)
{
matchingExpectation_->callWasMade(callOrder_);
}
potentiallyMatchingExpectations_.resetActualCallMatchingState();
return;
}
Expand All @@ -349,6 +360,7 @@ void MockCheckedActualCall::checkExpectations()
if (matchingExpectation_) {
matchingExpectation_->finalizeActualCallMatch();
callHasSucceeded();
matchingExpectation_->callWasMade(callOrder_);
potentiallyMatchingExpectations_.resetActualCallMatchingState();
return;
}
Expand Down Expand Up @@ -518,7 +530,8 @@ MockActualCall& MockCheckedActualCall::onObject(const void* objectPtr)
return *this;
}

callIsInProgress();
setState(CALL_IN_PROGRESS);
discardCurrentlyMatchingExpectations();

potentiallyMatchingExpectations_.onlyKeepExpectationsOnObject(objectPtr);

Expand Down
77 changes: 59 additions & 18 deletions src/CppUTestExt/MockExpectedCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,18 @@ SimpleString MockCheckedExpectedCall::getName() const
}

MockCheckedExpectedCall::MockCheckedExpectedCall()
: ignoreOtherParameters_(false), isActualCallMatchFinalized_(false), actualCallOrder_(0), expectedCallOrder_(NO_EXPECTED_CALL_ORDER), outOfOrder_(true), returnValue_(""), objectPtr_(NULL), wasPassedToObject_(true)
: ignoreOtherParameters_(false), isActualCallMatchFinalized_(false), actualCallOrder_(NOT_CALLED_YET), expectedCallOrder_(NO_EXPECTED_CALL_ORDER),
outOfOrder_(false), returnValue_(""), objectPtr_(NULL), wasPassedToObject_(true),
actualCalls_(0), minCalls_(1), maxCalls_(1)
{
inputParameters_ = new MockNamedValueList();
outputParameters_ = new MockNamedValueList();
}

MockCheckedExpectedCall::MockCheckedExpectedCall(unsigned int minCalls, unsigned int maxCalls)
: ignoreOtherParameters_(false), isActualCallMatchFinalized_(false), actualCallOrder_(NOT_CALLED_YET), expectedCallOrder_(NO_EXPECTED_CALL_ORDER),
outOfOrder_(false), returnValue_(""), objectPtr_(NULL), wasPassedToObject_(true),
actualCalls_(0), minCalls_(minCalls), maxCalls_(maxCalls)
{
inputParameters_ = new MockNamedValueList();
outputParameters_ = new MockNamedValueList();
Expand All @@ -69,7 +80,6 @@ MockCheckedExpectedCall::~MockCheckedExpectedCall()
MockExpectedCall& MockCheckedExpectedCall::withName(const SimpleString& name)
{
setName(name);
actualCallOrder_ = NOT_CALLED_YET;
return *this;
}

Expand Down Expand Up @@ -236,12 +246,12 @@ MockExpectedCall& MockCheckedExpectedCall::ignoreOtherParameters()

bool MockCheckedExpectedCall::isFulfilled()
{
return isMatchingActualCallAndFinalized();
return (actualCalls_ >= minCalls_) && (actualCalls_ <= maxCalls_);
}

bool MockCheckedExpectedCall::canMatchActualCalls()
{
return !isFulfilled();
return (actualCalls_ < maxCalls_);
}

bool MockCheckedExpectedCall::isMatchingActualCallAndFinalized()
Expand All @@ -251,18 +261,22 @@ bool MockCheckedExpectedCall::isMatchingActualCallAndFinalized()

bool MockCheckedExpectedCall::isMatchingActualCall()
{
return (actualCallOrder_ != NOT_CALLED_YET) && areParametersMatchingActualCall() && wasPassedToObject_;
return areParametersMatchingActualCall() && wasPassedToObject_;
}

void MockCheckedExpectedCall::callWasMade(int callOrder)
{
actualCalls_++;
actualCallOrder_ = callOrder;

if (expectedCallOrder_ == NO_EXPECTED_CALL_ORDER)
outOfOrder_ = false;
else if (actualCallOrder_ == expectedCallOrder_)
outOfOrder_ = false;
else
outOfOrder_ = true;

resetActualCallMatchingState();
}

void MockCheckedExpectedCall::finalizeActualCallMatch()
Expand Down Expand Up @@ -338,28 +352,48 @@ SimpleString MockCheckedExpectedCall::callToString()

if (inputParameters_->begin() == NULL && outputParameters_->begin() == NULL) {
str += (ignoreOtherParameters_) ? "all parameters ignored" : "no parameters";
return str;
}
else
{
MockNamedValueListNode* p;

MockNamedValueListNode* p;
for (p = inputParameters_->begin(); p; p = p->next()) {
str += StringFromFormat("%s %s: <%s>", p->getType().asCharString(), p->getName().asCharString(), getInputParameterValueString(p->getName()).asCharString());
if (p->next()) str += ", ";
}

for (p = inputParameters_->begin(); p; p = p->next()) {
str += StringFromFormat("%s %s: <%s>", p->getType().asCharString(), p->getName().asCharString(), getInputParameterValueString(p->getName()).asCharString());
if (p->next()) str += ", ";
if (inputParameters_->begin() && outputParameters_->begin())
{
str += ", ";
}

for (p = outputParameters_->begin(); p; p = p->next()) {
str += StringFromFormat("%s %s: <output>", p->getType().asCharString(), p->getName().asCharString());
if (p->next()) str += ", ";
}

if (ignoreOtherParameters_)
str += ", other parameters are ignored";
}

if (inputParameters_->begin() && outputParameters_->begin())
if (actualCalls_ < minCalls_)
{
str += ", ";
if (minCalls_ == maxCalls_)
{
str += StringFromFormat(" (expected %d call%s, but was called %d time%s)",
minCalls_, (minCalls_ == 1) ? "" : "s", actualCalls_, (actualCalls_ == 1) ? "" : "s" );
}
else
{
str += StringFromFormat(" (expected at least %d call%s, but was called %d time%s)",
minCalls_, (minCalls_ == 1) ? "" : "s", actualCalls_, (actualCalls_ == 1) ? "" : "s" );
}
}

for (p = outputParameters_->begin(); p; p = p->next()) {
str += StringFromFormat("%s %s: <output>", p->getType().asCharString(), p->getName().asCharString());
if (p->next()) str += ", ";
else
{
str += StringFromFormat(" (called %d time%s)", actualCalls_, (actualCalls_ == 1) ? "" : "s");
}

if (ignoreOtherParameters_)
str += ", other parameters are ignored";
return str;
}

Expand Down Expand Up @@ -511,6 +545,12 @@ bool MockCheckedExpectedCall::isOutOfOrder() const
return outOfOrder_;
}

unsigned int MockCheckedExpectedCall::getMaxCalls() const
{
return maxCalls_;
}

#if 0
struct MockExpectedCallCompositeNode
{
MockExpectedCallCompositeNode(MockExpectedCall& functionCall, MockExpectedCallCompositeNode* next) : next_(next), call_(functionCall){}
Expand Down Expand Up @@ -737,6 +777,7 @@ MockExpectedCall& MockExpectedCallComposite::onObject(void* object)
node->call_.onObject(object);
return *this;
}
#endif

MockExpectedCall& MockIgnoredExpectedCall::instance()
{
Expand Down
Loading