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
Next Next commit
Preparation of mock library in order to add support for expected call…
…s 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).
  • Loading branch information
jgonzalezdr committed Jun 28, 2016
commit dae0892ce56f30a723d51b1cfa3a58dfe1f771b0
6 changes: 3 additions & 3 deletions include/CppUTestExt/MockCheckedActualCall.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class MockCheckedActualCall : public MockActualCall
virtual UtestShell* getTest() const;
virtual void callHasSucceeded();
virtual void finalizeOutputParameters(MockCheckedExpectedCall* call);
virtual void finalizeCallWhenFulfilled();
virtual void finalizeCallWhenMatchIsFound();
virtual void failTest(const MockFailure& failure);
virtual void checkInputParameter(const MockNamedValue& actualParameter);
virtual void checkOutputParameter(const MockNamedValue& outputParameter);
Expand All @@ -120,9 +120,9 @@ class MockCheckedActualCall : public MockActualCall
MockFailureReporter* reporter_;

ActualCallState state_;
MockCheckedExpectedCall* fulfilledExpectation_;
MockCheckedExpectedCall* matchingExpectation_;

MockExpectedCallsList unfulfilledExpectations_;
MockExpectedCallsList potentiallyMatchingExpectations_;
const MockExpectedCallsList& allExpectations_;

class MockOutputParametersListNode
Expand Down
21 changes: 11 additions & 10 deletions include/CppUTestExt/MockCheckedExpectedCall.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,18 @@ class MockCheckedExpectedCall : public MockExpectedCall
virtual bool relatesToObject(const void* objectPtr) const;

virtual bool isFulfilled();
virtual bool isFulfilledWithoutIgnoredParameters();
virtual bool areParametersFulfilled();
virtual bool areIgnoredParametersFulfilled();
virtual bool canMatchActualCalls();
virtual bool isMatchingActualCallAndFinalized();
virtual bool isMatchingActualCall();
virtual bool areParametersMatchingActualCall();
virtual bool isOutOfOrder() const;

virtual void callWasMade(int callOrder);
virtual void inputParameterWasPassed(const SimpleString& name);
virtual void outputParameterWasPassed(const SimpleString& name);
virtual void parametersWereIgnored();
virtual void finalizeActualCallMatch();
virtual void wasPassedToObject();
virtual void resetExpectation();
virtual void resetActualCallMatchingState();

virtual SimpleString callToString();
virtual SimpleString missingParametersToString();
Expand All @@ -113,18 +114,18 @@ class MockCheckedExpectedCall : public MockExpectedCall
{
public:
MockExpectedFunctionParameter(const SimpleString& name);
void setFulfilled(bool b);
bool isFulfilled() const;
void setMatchesActualCall(bool b);
bool isMatchingActualCall() const;

private:
bool fulfilled_;
bool matchesActualCall_;
};

MockExpectedFunctionParameter* item(MockNamedValueListNode* node);

bool ignoreOtherParameters_;
bool parametersWereIgnored_;
int callOrder_;
bool isActualCallMatchFinalized_;
int actualCallOrder_;
int expectedCallOrder_;
bool outOfOrder_;
MockNamedValueList* inputParameters_;
Expand Down
15 changes: 8 additions & 7 deletions include/CppUTestExt/MockExpectedCallsList.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class MockExpectedCallsList
virtual int amountOfExpectationsFor(const SimpleString& name) const;
virtual int amountOfUnfulfilledExpectations() const;
virtual bool hasUnfulfilledExpectations() const;
virtual bool hasUnfulfilledExpectationsBecauseOfMissingParameters() const;
virtual bool hasFinalizedMatchingExpectations() const;
virtual bool hasUnmatchingExpectationsBecauseOfMissingParameters() const;
virtual bool hasExpectationWithName(const SimpleString& name) const;
virtual bool hasCallsOutOfOrder() const;
virtual bool isEmpty() const;
Expand All @@ -53,21 +54,21 @@ class MockExpectedCallsList
virtual void addExpectationsRelatedTo(const SimpleString& name, const MockExpectedCallsList& list);

virtual void onlyKeepOutOfOrderExpectations();
virtual void addUnfulfilledExpectations(const MockExpectedCallsList& list);
virtual void addPotentiallyMatchingExpectations(const MockExpectedCallsList& list);

virtual void onlyKeepExpectationsRelatedTo(const SimpleString& name);
virtual void onlyKeepExpectationsWithInputParameter(const MockNamedValue& parameter);
virtual void onlyKeepExpectationsWithInputParameterName(const SimpleString& name);
virtual void onlyKeepExpectationsWithOutputParameter(const MockNamedValue& parameter);
virtual void onlyKeepExpectationsWithOutputParameterName(const SimpleString& name);
virtual void onlyKeepExpectationsOnObject(const void* objectPtr);
virtual void onlyKeepUnfulfilledExpectations();
virtual void onlyKeepUnmatchingExpectations();

virtual MockCheckedExpectedCall* removeOneFulfilledExpectation();
virtual MockCheckedExpectedCall* removeOneFulfilledExpectationWithIgnoredParameters();
virtual MockCheckedExpectedCall* getOneFulfilledExpectationWithIgnoredParameters();
virtual MockCheckedExpectedCall* removeFirstFinalizedMatchingExpectation();
virtual MockCheckedExpectedCall* removeFirstMatchingExpectation();
virtual MockCheckedExpectedCall* getFirstMatchingExpectation();

virtual void resetExpectations();
virtual void resetActualCallMatchingState();
virtual void callWasMade(int callOrder);
virtual void wasPassedToObject();
virtual void parameterWasPassed(const SimpleString& parameterName);
Expand Down
4 changes: 2 additions & 2 deletions include/CppUTestExt/MockSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ class MockSupport

bool tracing_;

void checkExpectationsOfLastCall();
bool wasLastCallFulfilled();
void checkExpectationsOfLastActualCall();
bool wasLastActualCallFulfilled();
void failTestWithUnexpectedCalls();
void failTestWithOutOfOrderCalls();

Expand Down
87 changes: 46 additions & 41 deletions src/CppUTestExt/MockActualCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ SimpleString MockCheckedActualCall::getName() const
}

MockCheckedActualCall::MockCheckedActualCall(int callOrder, MockFailureReporter* reporter, const MockExpectedCallsList& allExpectations)
: callOrder_(callOrder), reporter_(reporter), state_(CALL_SUCCEED), fulfilledExpectation_(NULL), allExpectations_(allExpectations), outputParameterExpectations_(NULL)
: callOrder_(callOrder), reporter_(reporter), state_(CALL_SUCCEED), matchingExpectation_(NULL), allExpectations_(allExpectations), outputParameterExpectations_(NULL)
{
unfulfilledExpectations_.addUnfulfilledExpectations(allExpectations);
potentiallyMatchingExpectations_.addPotentiallyMatchingExpectations(allExpectations);
}

MockCheckedActualCall::~MockCheckedActualCall()
Expand Down Expand Up @@ -103,19 +103,19 @@ void MockCheckedActualCall::finalizeOutputParameters(MockCheckedExpectedCall* ex
}
}

void MockCheckedActualCall::finalizeCallWhenFulfilled()
void MockCheckedActualCall::finalizeCallWhenMatchIsFound()
{
// Expectations that don't ignore parameters have higher fulfillment preference than those that ignore parameters
// Expectations that don't ignore parameters have higher fulfillment preference than those that ignore parameters

fulfilledExpectation_ = unfulfilledExpectations_.removeOneFulfilledExpectation();
if (fulfilledExpectation_) {
finalizeOutputParameters(fulfilledExpectation_);
matchingExpectation_ = potentiallyMatchingExpectations_.removeFirstFinalizedMatchingExpectation();
if (matchingExpectation_) {
finalizeOutputParameters(matchingExpectation_);
callHasSucceeded();
} else {
MockCheckedExpectedCall* fulfilledExpectationWithIgnoredParameters = unfulfilledExpectations_.getOneFulfilledExpectationWithIgnoredParameters();
if (fulfilledExpectationWithIgnoredParameters) {
finalizeOutputParameters(fulfilledExpectationWithIgnoredParameters);
}
MockCheckedExpectedCall* matchingExpectationWithIgnoredParameters = potentiallyMatchingExpectations_.getFirstMatchingExpectation();
if (matchingExpectationWithIgnoredParameters) {
finalizeOutputParameters(matchingExpectationWithIgnoredParameters);
}
}
}

Expand All @@ -127,29 +127,28 @@ void MockCheckedActualCall::callHasSucceeded()
void MockCheckedActualCall::callIsInProgress()
{
setState(CALL_IN_PROGRESS);
if (fulfilledExpectation_)
if (matchingExpectation_)
{
fulfilledExpectation_->resetExpectation();
fulfilledExpectation_ = NULL;
matchingExpectation_->resetActualCallMatchingState();
matchingExpectation_ = NULL;
}
unfulfilledExpectations_.onlyKeepUnfulfilledExpectations();
potentiallyMatchingExpectations_.onlyKeepUnmatchingExpectations();
}

MockActualCall& MockCheckedActualCall::withName(const SimpleString& name)
{
setName(name);
callIsInProgress();

unfulfilledExpectations_.onlyKeepExpectationsRelatedTo(name);
if (unfulfilledExpectations_.isEmpty()) {
potentiallyMatchingExpectations_.onlyKeepExpectationsRelatedTo(name);
if (potentiallyMatchingExpectations_.isEmpty()) {
MockUnexpectedCallHappenedFailure failure(getTest(), name, allExpectations_);
failTest(failure);
return *this;
}

unfulfilledExpectations_.callWasMade(callOrder_);

finalizeCallWhenFulfilled();
potentiallyMatchingExpectations_.callWasMade(callOrder_);
finalizeCallWhenMatchIsFound();

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

callIsInProgress();

unfulfilledExpectations_.onlyKeepExpectationsWithInputParameter(actualParameter);
potentiallyMatchingExpectations_.onlyKeepExpectationsWithInputParameter(actualParameter);

if (unfulfilledExpectations_.isEmpty()) {
if (potentiallyMatchingExpectations_.isEmpty()) {
MockUnexpectedInputParameterFailure failure(getTest(), getName(), actualParameter, allExpectations_);
failTest(failure);
return;
}

unfulfilledExpectations_.parameterWasPassed(actualParameter.getName());
finalizeCallWhenFulfilled();
potentiallyMatchingExpectations_.parameterWasPassed(actualParameter.getName());
finalizeCallWhenMatchIsFound();
}

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

callIsInProgress();

unfulfilledExpectations_.onlyKeepExpectationsWithOutputParameter(outputParameter);
potentiallyMatchingExpectations_.onlyKeepExpectationsWithOutputParameter(outputParameter);

if (unfulfilledExpectations_.isEmpty()) {
if (potentiallyMatchingExpectations_.isEmpty()) {
MockUnexpectedOutputParameterFailure failure(getTest(), getName(), outputParameter, allExpectations_);
failTest(failure);
return;
}

unfulfilledExpectations_.outputParameterWasPassed(outputParameter.getName());
finalizeCallWhenFulfilled();
potentiallyMatchingExpectations_.outputParameterWasPassed(outputParameter.getName());
finalizeCallWhenMatchIsFound();
}

MockActualCall& MockCheckedActualCall::withBoolParameter(const SimpleString& name, bool value)
Expand Down Expand Up @@ -339,21 +338,22 @@ void MockCheckedActualCall::checkExpectations()
{
if (state_ != CALL_IN_PROGRESS)
{
unfulfilledExpectations_.resetExpectations();
potentiallyMatchingExpectations_.resetActualCallMatchingState();
return;
}

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

fulfilledExpectation_ = unfulfilledExpectations_.removeOneFulfilledExpectationWithIgnoredParameters();
if (fulfilledExpectation_) {
matchingExpectation_ = potentiallyMatchingExpectations_.removeFirstMatchingExpectation();
if (matchingExpectation_) {
matchingExpectation_->finalizeActualCallMatch();
callHasSucceeded();
unfulfilledExpectations_.resetExpectations();
potentiallyMatchingExpectations_.resetActualCallMatchingState();
return;
}

if (unfulfilledExpectations_.hasUnfulfilledExpectationsBecauseOfMissingParameters()) {
if (potentiallyMatchingExpectations_.hasUnmatchingExpectationsBecauseOfMissingParameters()) {
MockExpectedParameterDidntHappenFailure failure(getTest(), getName(), allExpectations_);
failTest(failure);
}
Expand All @@ -371,8 +371,8 @@ void MockCheckedActualCall::setState(ActualCallState state)
MockNamedValue MockCheckedActualCall::returnValue()
{
checkExpectations();
if (fulfilledExpectation_)
return fulfilledExpectation_->returnValue();
if (matchingExpectation_)
return matchingExpectation_->returnValue();
return MockNamedValue("no return value");
}

Expand Down Expand Up @@ -513,19 +513,24 @@ bool MockCheckedActualCall::hasReturnValue()

MockActualCall& MockCheckedActualCall::onObject(const void* objectPtr)
{
if(hasFailed())
{
return *this;
}

callIsInProgress();

unfulfilledExpectations_.onlyKeepExpectationsOnObject(objectPtr);
potentiallyMatchingExpectations_.onlyKeepExpectationsOnObject(objectPtr);

if (unfulfilledExpectations_.isEmpty()) {
if (potentiallyMatchingExpectations_.isEmpty()) {
MockUnexpectedObjectFailure failure(getTest(), getName(), objectPtr, allExpectations_);
failTest(failure);
return *this;
}

unfulfilledExpectations_.wasPassedToObject();
potentiallyMatchingExpectations_.wasPassedToObject();
finalizeCallWhenMatchIsFound();

finalizeCallWhenFulfilled();
return *this;
}

Expand Down
Loading