Skip to content

Expected calls matching several actual calls - (1) Preparation #1020

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

Merged
merged 4 commits into from
Jul 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions include/CppUTestExt/MockCheckedActualCall.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ class MockCheckedActualCall : public MockActualCall
SimpleString getName() const;
virtual UtestShell* getTest() const;
virtual void callHasSucceeded();
virtual void finalizeOutputParameters(MockCheckedExpectedCall* call);
virtual void finalizeCallWhenFulfilled();
virtual void copyOutputParameters(MockCheckedExpectedCall* call);
virtual void completeCallWhenMatchIsFound();
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
6 changes: 3 additions & 3 deletions include/CppUTestExt/MockSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class MockSupport
void countCheck();

private:
int callOrder_;
int actualCallOrder_;
int expectedCallOrder_;
bool strictOrdering_;
MockFailureReporter *activeReporter_;
Expand All @@ -142,8 +142,8 @@ class MockSupport

bool tracing_;

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

Expand Down
88 changes: 46 additions & 42 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 All @@ -78,7 +78,7 @@ void MockCheckedActualCall::failTest(const MockFailure& failure)
}
}

void MockCheckedActualCall::finalizeOutputParameters(MockCheckedExpectedCall* expectedCall)
void MockCheckedActualCall::copyOutputParameters(MockCheckedExpectedCall* expectedCall)
{
for (MockOutputParametersListNode* p = outputParameterExpectations_; p; p = p->next_)
{
Expand All @@ -103,19 +103,19 @@ void MockCheckedActualCall::finalizeOutputParameters(MockCheckedExpectedCall* ex
}
}

void MockCheckedActualCall::finalizeCallWhenFulfilled()
void MockCheckedActualCall::completeCallWhenMatchIsFound()
{
// 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_) {
copyOutputParameters(matchingExpectation_);
callHasSucceeded();
} else {
MockCheckedExpectedCall* fulfilledExpectationWithIgnoredParameters = unfulfilledExpectations_.getOneFulfilledExpectationWithIgnoredParameters();
if (fulfilledExpectationWithIgnoredParameters) {
finalizeOutputParameters(fulfilledExpectationWithIgnoredParameters);
}
MockCheckedExpectedCall* matchingExpectationWithIgnoredParameters = potentiallyMatchingExpectations_.getFirstMatchingExpectation();
if (matchingExpectationWithIgnoredParameters) {
copyOutputParameters(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_);
completeCallWhenMatchIsFound();

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());
completeCallWhenMatchIsFound();
}

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());
completeCallWhenMatchIsFound();
}

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,23 @@ 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();
completeCallWhenMatchIsFound();

finalizeCallWhenFulfilled();
return *this;
}

Expand Down
Loading