From dae0892ce56f30a723d51b1cfa3a58dfe1f771b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Tue, 21 Jun 2016 23:30:10 +0200 Subject: [PATCH 01/26] 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). --- include/CppUTestExt/MockCheckedActualCall.h | 6 +- include/CppUTestExt/MockCheckedExpectedCall.h | 21 ++--- include/CppUTestExt/MockExpectedCallsList.h | 15 ++-- include/CppUTestExt/MockSupport.h | 4 +- src/CppUTestExt/MockActualCall.cpp | 87 ++++++++++--------- src/CppUTestExt/MockExpectedCall.cpp | 70 ++++++++------- src/CppUTestExt/MockExpectedCallsList.cpp | 56 +++++++----- src/CppUTestExt/MockSupport.cpp | 10 +-- .../CppUTestExt/ExpectedFunctionsListTest.cpp | 24 ++--- tests/CppUTestExt/MockParameterTest.cpp | 2 +- 10 files changed, 160 insertions(+), 135 deletions(-) diff --git a/include/CppUTestExt/MockCheckedActualCall.h b/include/CppUTestExt/MockCheckedActualCall.h index f4606cf17..cd592881c 100644 --- a/include/CppUTestExt/MockCheckedActualCall.h +++ b/include/CppUTestExt/MockCheckedActualCall.h @@ -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); @@ -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 diff --git a/include/CppUTestExt/MockCheckedExpectedCall.h b/include/CppUTestExt/MockCheckedExpectedCall.h index e3f3725af..b183d896d 100644 --- a/include/CppUTestExt/MockCheckedExpectedCall.h +++ b/include/CppUTestExt/MockCheckedExpectedCall.h @@ -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(); @@ -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_; diff --git a/include/CppUTestExt/MockExpectedCallsList.h b/include/CppUTestExt/MockExpectedCallsList.h index 115192dd8..479f31fdd 100644 --- a/include/CppUTestExt/MockExpectedCallsList.h +++ b/include/CppUTestExt/MockExpectedCallsList.h @@ -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; @@ -53,7 +54,7 @@ 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); @@ -61,13 +62,13 @@ class MockExpectedCallsList 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); diff --git a/include/CppUTestExt/MockSupport.h b/include/CppUTestExt/MockSupport.h index e58bf2a57..575bc4fd8 100755 --- a/include/CppUTestExt/MockSupport.h +++ b/include/CppUTestExt/MockSupport.h @@ -142,8 +142,8 @@ class MockSupport bool tracing_; - void checkExpectationsOfLastCall(); - bool wasLastCallFulfilled(); + void checkExpectationsOfLastActualCall(); + bool wasLastActualCallFulfilled(); void failTestWithUnexpectedCalls(); void failTestWithOutOfOrderCalls(); diff --git a/src/CppUTestExt/MockActualCall.cpp b/src/CppUTestExt/MockActualCall.cpp index a7644dc53..a4ec864a0 100644 --- a/src/CppUTestExt/MockActualCall.cpp +++ b/src/CppUTestExt/MockActualCall.cpp @@ -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() @@ -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); + } } } @@ -127,12 +127,12 @@ 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) @@ -140,16 +140,15 @@ 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; } @@ -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) @@ -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) @@ -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); } @@ -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"); } @@ -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; } diff --git a/src/CppUTestExt/MockExpectedCall.cpp b/src/CppUTestExt/MockExpectedCall.cpp index e9f3c96d0..ac987c105 100644 --- a/src/CppUTestExt/MockExpectedCall.cpp +++ b/src/CppUTestExt/MockExpectedCall.cpp @@ -52,7 +52,7 @@ SimpleString MockCheckedExpectedCall::getName() const } MockCheckedExpectedCall::MockCheckedExpectedCall() - : ignoreOtherParameters_(false), parametersWereIgnored_(false), callOrder_(0), expectedCallOrder_(NO_EXPECTED_CALL_ORDER), outOfOrder_(true), returnValue_(""), objectPtr_(NULL), wasPassedToObject_(true) + : ignoreOtherParameters_(false), isActualCallMatchFinalized_(false), actualCallOrder_(0), expectedCallOrder_(NO_EXPECTED_CALL_ORDER), outOfOrder_(true), returnValue_(""), objectPtr_(NULL), wasPassedToObject_(true) { inputParameters_ = new MockNamedValueList(); outputParameters_ = new MockNamedValueList(); @@ -69,7 +69,7 @@ MockCheckedExpectedCall::~MockCheckedExpectedCall() MockExpectedCall& MockCheckedExpectedCall::withName(const SimpleString& name) { setName(name); - callOrder_ = NOT_CALLED_YET; + actualCallOrder_ = NOT_CALLED_YET; return *this; } @@ -216,25 +216,18 @@ MockNamedValue MockCheckedExpectedCall::getOutputParameter(const SimpleString& n return (p) ? *p : MockNamedValue(""); } -bool MockCheckedExpectedCall::areParametersFulfilled() +bool MockCheckedExpectedCall::areParametersMatchingActualCall() { MockNamedValueListNode* p; for (p = inputParameters_->begin(); p; p = p->next()) - if (! item(p)->isFulfilled()) + if (! item(p)->isMatchingActualCall()) return false; for (p = outputParameters_->begin(); p; p = p->next()) - if (! item(p)->isFulfilled()) + if (! item(p)->isMatchingActualCall()) return false; return true; } -bool MockCheckedExpectedCall::areIgnoredParametersFulfilled() -{ - if (ignoreOtherParameters_) - return parametersWereIgnored_; - return true; -} - MockExpectedCall& MockCheckedExpectedCall::ignoreOtherParameters() { ignoreOtherParameters_ = true; @@ -243,29 +236,38 @@ MockExpectedCall& MockCheckedExpectedCall::ignoreOtherParameters() bool MockCheckedExpectedCall::isFulfilled() { - return isFulfilledWithoutIgnoredParameters() && areIgnoredParametersFulfilled(); + return isMatchingActualCallAndFinalized(); } -bool MockCheckedExpectedCall::isFulfilledWithoutIgnoredParameters() +bool MockCheckedExpectedCall::canMatchActualCalls() { - return callOrder_ != NOT_CALLED_YET && areParametersFulfilled() && wasPassedToObject_; + return !isFulfilled(); } +bool MockCheckedExpectedCall::isMatchingActualCallAndFinalized() +{ + return isMatchingActualCall() && (!ignoreOtherParameters_ || isActualCallMatchFinalized_); +} + +bool MockCheckedExpectedCall::isMatchingActualCall() +{ + return (actualCallOrder_ != NOT_CALLED_YET) && areParametersMatchingActualCall() && wasPassedToObject_; +} void MockCheckedExpectedCall::callWasMade(int callOrder) { - callOrder_ = callOrder; + actualCallOrder_ = callOrder; if (expectedCallOrder_ == NO_EXPECTED_CALL_ORDER) outOfOrder_ = false; - else if (callOrder_ == expectedCallOrder_) + else if (actualCallOrder_ == expectedCallOrder_) outOfOrder_ = false; else outOfOrder_ = true; } -void MockCheckedExpectedCall::parametersWereIgnored() +void MockCheckedExpectedCall::finalizeActualCallMatch() { - parametersWereIgnored_ = true; + isActualCallMatchFinalized_ = true; } @@ -274,23 +276,25 @@ void MockCheckedExpectedCall::wasPassedToObject() wasPassedToObject_ = true; } -void MockCheckedExpectedCall::resetExpectation() +void MockCheckedExpectedCall::resetActualCallMatchingState() { - callOrder_ = NOT_CALLED_YET; + actualCallOrder_ = NOT_CALLED_YET; wasPassedToObject_ = (objectPtr_ == NULL); + isActualCallMatchFinalized_ = false; + MockNamedValueListNode* p; for (p = inputParameters_->begin(); p; p = p->next()) - item(p)->setFulfilled(false); + item(p)->setMatchesActualCall(false); for (p = outputParameters_->begin(); p; p = p->next()) - item(p)->setFulfilled(false); + item(p)->setMatchesActualCall(false); } void MockCheckedExpectedCall::inputParameterWasPassed(const SimpleString& name) { for (MockNamedValueListNode* p = inputParameters_->begin(); p; p = p->next()) { if (p->getName() == name) - item(p)->setFulfilled(true); + item(p)->setMatchesActualCall(true); } } @@ -298,7 +302,7 @@ void MockCheckedExpectedCall::outputParameterWasPassed(const SimpleString& name) { for (MockNamedValueListNode* p = outputParameters_->begin(); p; p = p->next()) { if (p->getName() == name) - item(p)->setFulfilled(true); + item(p)->setMatchesActualCall(true); } } @@ -365,13 +369,13 @@ SimpleString MockCheckedExpectedCall::missingParametersToString() MockNamedValueListNode* p; for (p = inputParameters_->begin(); p; p = p->next()) { - if (! item(p)->isFulfilled()) { + if (! item(p)->isMatchingActualCall()) { if (str != "") str += ", "; str += StringFromFormat("%s %s", p->getType().asCharString(), p->getName().asCharString()); } } for (p = outputParameters_->begin(); p; p = p->next()) { - if (! item(p)->isFulfilled()) { + if (! item(p)->isMatchingActualCall()) { if (str != "") str += ", "; str += StringFromFormat("%s %s", p->getType().asCharString(), p->getName().asCharString()); } @@ -395,18 +399,18 @@ MockCheckedExpectedCall::MockExpectedFunctionParameter* MockCheckedExpectedCall: } MockCheckedExpectedCall::MockExpectedFunctionParameter::MockExpectedFunctionParameter(const SimpleString& name) - : MockNamedValue(name), fulfilled_(false) + : MockNamedValue(name), matchesActualCall_(false) { } -void MockCheckedExpectedCall::MockExpectedFunctionParameter::setFulfilled(bool b) +void MockCheckedExpectedCall::MockExpectedFunctionParameter::setMatchesActualCall(bool b) { - fulfilled_ = b; + matchesActualCall_ = b; } -bool MockCheckedExpectedCall::MockExpectedFunctionParameter::isFulfilled() const +bool MockCheckedExpectedCall::MockExpectedFunctionParameter::isMatchingActualCall() const { - return fulfilled_; + return matchesActualCall_; } MockExpectedCall& MockCheckedExpectedCall::andReturnValue(bool value) @@ -493,7 +497,7 @@ MockNamedValue MockCheckedExpectedCall::returnValue() int MockCheckedExpectedCall::getCallOrder() const { - return callOrder_; + return actualCallOrder_; } MockExpectedCall& MockCheckedExpectedCall::withCallOrder(int callOrder) diff --git a/src/CppUTestExt/MockExpectedCallsList.cpp b/src/CppUTestExt/MockExpectedCallsList.cpp index e34ca4125..ba2407c68 100644 --- a/src/CppUTestExt/MockExpectedCallsList.cpp +++ b/src/CppUTestExt/MockExpectedCallsList.cpp @@ -81,9 +81,24 @@ int MockExpectedCallsList::amountOfUnfulfilledExpectations() const return count; } +bool MockExpectedCallsList::hasFinalizedMatchingExpectations() const +{ + for (MockExpectedCallsListNode* p = head_; p; p = p->next_) + { + if (p->expectedCall_->isMatchingActualCallAndFinalized()) + { + return true; + } + } + return false; +} + bool MockExpectedCallsList::hasUnfulfilledExpectations() const { - return amountOfUnfulfilledExpectations() != 0; + for (MockExpectedCallsListNode* p = head_; p; p = p->next_) + if (!p->expectedCall_->isFulfilled()) + return true; + return false; } bool MockExpectedCallsList::hasExpectationWithName(const SimpleString& name) const @@ -107,10 +122,10 @@ void MockExpectedCallsList::addExpectedCall(MockCheckedExpectedCall* call) } } -void MockExpectedCallsList::addUnfulfilledExpectations(const MockExpectedCallsList& list) +void MockExpectedCallsList::addPotentiallyMatchingExpectations(const MockExpectedCallsList& list) { for (MockExpectedCallsListNode* p = list.head_; p; p = p->next_) - if (! p->expectedCall_->isFulfilled()) + if (p->expectedCall_->canMatchActualCalls()) addExpectedCall(p->expectedCall_); } @@ -144,12 +159,12 @@ void MockExpectedCallsList::onlyKeepOutOfOrderExpectations() pruneEmptyNodeFromList(); } -void MockExpectedCallsList::onlyKeepUnfulfilledExpectations() +void MockExpectedCallsList::onlyKeepUnmatchingExpectations() { for (MockExpectedCallsListNode* p = head_; p; p = p->next_) - if (p->expectedCall_->isFulfilled()) + if (p->expectedCall_->isMatchingActualCallAndFinalized()) { - p->expectedCall_->resetExpectation(); + p->expectedCall_->resetActualCallMatchingState(); p->expectedCall_ = NULL; } @@ -196,38 +211,37 @@ void MockExpectedCallsList::onlyKeepExpectationsOnObject(const void* objectPtr) pruneEmptyNodeFromList(); } -MockCheckedExpectedCall* MockExpectedCallsList::removeOneFulfilledExpectation() +MockCheckedExpectedCall* MockExpectedCallsList::removeFirstFinalizedMatchingExpectation() { for (MockExpectedCallsListNode* p = head_; p; p = p->next_) { - if (p->expectedCall_->isFulfilled()) { - MockCheckedExpectedCall* fulfilledCall = p->expectedCall_; + if (p->expectedCall_->isMatchingActualCallAndFinalized()) { + MockCheckedExpectedCall* matchingCall = p->expectedCall_; p->expectedCall_ = NULL; pruneEmptyNodeFromList(); - return fulfilledCall; + return matchingCall; } } return NULL; } -MockCheckedExpectedCall* MockExpectedCallsList::getOneFulfilledExpectationWithIgnoredParameters() +MockCheckedExpectedCall* MockExpectedCallsList::getFirstMatchingExpectation() { for (MockExpectedCallsListNode* p = head_; p; p = p->next_) { - if (p->expectedCall_->isFulfilledWithoutIgnoredParameters()) { + if (p->expectedCall_->isMatchingActualCall()) { return p->expectedCall_; } } return NULL; } -MockCheckedExpectedCall* MockExpectedCallsList::removeOneFulfilledExpectationWithIgnoredParameters() +MockCheckedExpectedCall* MockExpectedCallsList::removeFirstMatchingExpectation() { for (MockExpectedCallsListNode* p = head_; p; p = p->next_) { - if (p->expectedCall_->isFulfilledWithoutIgnoredParameters()) { - MockCheckedExpectedCall* fulfilledCall = p->expectedCall_; - p->expectedCall_->parametersWereIgnored(); + if (p->expectedCall_->isMatchingActualCall()) { + MockCheckedExpectedCall* matchingCall = p->expectedCall_; p->expectedCall_ = NULL; pruneEmptyNodeFromList(); - return fulfilledCall; + return matchingCall; } } return NULL; @@ -265,10 +279,10 @@ void MockExpectedCallsList::deleteAllExpectationsAndClearList() } } -void MockExpectedCallsList::resetExpectations() +void MockExpectedCallsList::resetActualCallMatchingState() { for (MockExpectedCallsListNode* p = head_; p; p = p->next_) - p->expectedCall_->resetExpectation(); + p->expectedCall_->resetActualCallMatchingState(); } void MockExpectedCallsList::callWasMade(int callOrder) @@ -354,10 +368,10 @@ SimpleString MockExpectedCallsList::missingParametersToString() const return stringOrNoneTextWhenEmpty(str, ""); } -bool MockExpectedCallsList::hasUnfulfilledExpectationsBecauseOfMissingParameters() const +bool MockExpectedCallsList::hasUnmatchingExpectationsBecauseOfMissingParameters() const { for (MockExpectedCallsListNode* p = head_; p; p = p->next_) - if (! p->expectedCall_->areParametersFulfilled()) + if (! p->expectedCall_->areParametersMatchingActualCall()) return true; return false; } diff --git a/src/CppUTestExt/MockSupport.cpp b/src/CppUTestExt/MockSupport.cpp index fb307652e..2c8266834 100755 --- a/src/CppUTestExt/MockSupport.cpp +++ b/src/CppUTestExt/MockSupport.cpp @@ -268,13 +268,13 @@ bool MockSupport::expectedCallsLeft() return callsLeft != 0; } -bool MockSupport::wasLastCallFulfilled() +bool MockSupport::wasLastActualCallFulfilled() { if (lastActualFunctionCall_ && !lastActualFunctionCall_->isFulfilled()) return false; for (MockNamedValueListNode* p = data_.begin(); p; p = p->next()) - if (getMockSupport(p) && !getMockSupport(p)->wasLastCallFulfilled()) + if (getMockSupport(p) && !getMockSupport(p)->wasLastActualCallFulfilled()) return false; return true; @@ -318,7 +318,7 @@ void MockSupport::countCheck() UtestShell::getCurrent()->countCheck(); } -void MockSupport::checkExpectationsOfLastCall() +void MockSupport::checkExpectationsOfLastActualCall() { if(lastActualFunctionCall_) lastActualFunctionCall_->checkExpectations(); @@ -344,9 +344,9 @@ bool MockSupport::hasCallsOutOfOrder() void MockSupport::checkExpectations() { - checkExpectationsOfLastCall(); + checkExpectationsOfLastActualCall(); - if (wasLastCallFulfilled() && expectedCallsLeft()) + if (wasLastActualCallFulfilled() && expectedCallsLeft()) failTestWithUnexpectedCalls(); if (hasCallsOutOfOrder()) diff --git a/tests/CppUTestExt/ExpectedFunctionsListTest.cpp b/tests/CppUTestExt/ExpectedFunctionsListTest.cpp index da0ec90aa..1b8e55da3 100644 --- a/tests/CppUTestExt/ExpectedFunctionsListTest.cpp +++ b/tests/CppUTestExt/ExpectedFunctionsListTest.cpp @@ -63,7 +63,7 @@ TEST_GROUP(MockExpectedCallsList) TEST(MockExpectedCallsList, emptyList) { CHECK(! list->hasUnfulfilledExpectations()); - CHECK(list->size() == list->amountOfUnfulfilledExpectations()); + CHECK(! list->hasFinalizedMatchingExpectations()); LONGS_EQUAL(0, list->size()); } @@ -74,7 +74,7 @@ TEST(MockExpectedCallsList, addingCalls) LONGS_EQUAL(2, list->size()); } -TEST(MockExpectedCallsList, listWithFulfilledExpectationHasNoUnfillfilledOnes) +TEST(MockExpectedCallsList, listWithFulfilledExpectationHasNoUnfulfilledOnes) { call1->callWasMade(1); call2->callWasMade(2); @@ -112,7 +112,7 @@ TEST(MockExpectedCallsList, deleteAllExpectationsAndClearList) list->deleteAllExpectationsAndClearList(); } -TEST(MockExpectedCallsList, onlyKeepUnfulfilledExpectations) +TEST(MockExpectedCallsList, onlyKeepUnmatchingExpectations) { call1->withName("relate"); call2->withName("unrelate"); @@ -122,7 +122,7 @@ TEST(MockExpectedCallsList, onlyKeepUnfulfilledExpectations) list->addExpectedCall(call1); list->addExpectedCall(call2); list->addExpectedCall(call3); - list->onlyKeepUnfulfilledExpectations(); + list->onlyKeepUnmatchingExpectations(); LONGS_EQUAL(1, list->size()); } @@ -200,21 +200,21 @@ TEST(MockExpectedCallsList, onlyKeepExpectationsWithInputParameter) LONGS_EQUAL(2, list->size()); } -TEST(MockExpectedCallsList, addUnfilfilledExpectationsWithEmptyList) +TEST(MockExpectedCallsList, addPotentiallyMatchingExpectationsWithEmptyList) { MockExpectedCallsList newList; - newList.addUnfulfilledExpectations(*list); + newList.addPotentiallyMatchingExpectations(*list); LONGS_EQUAL(0, newList.size()); } -TEST(MockExpectedCallsList, addUnfilfilledExpectationsMultipleUnfulfilledExpectations) +TEST(MockExpectedCallsList, addPotentiallyMatchingExpectationsMultipleUnmatchedExpectations) { call2->callWasMade(1); list->addExpectedCall(call1); list->addExpectedCall(call2); list->addExpectedCall(call3); MockExpectedCallsList newList; - newList.addUnfulfilledExpectations(*list); + newList.addPotentiallyMatchingExpectations(*list); LONGS_EQUAL(2, newList.size()); } @@ -266,14 +266,14 @@ TEST(MockExpectedCallsList, callToStringForFulfilledFunctions) STRCMP_EQUAL(expectedString.asCharString(), list->fulfilledCallsToString().asCharString()); } -TEST(MockExpectedCallsList, removeOneFulfilledExpectationFromEmptyList) +TEST(MockExpectedCallsList, removeOneFinalizedMatchingExpectationFromEmptyList) { - POINTERS_EQUAL(NULL, list->removeOneFulfilledExpectation()); + POINTERS_EQUAL(NULL, list->removeFirstFinalizedMatchingExpectation()); } -TEST(MockExpectedCallsList, getOneFulfilledExpectationWithIgnoredParametersFromEmptyList) +TEST(MockExpectedCallsList, getOneMatchingExpectationFromEmptyList) { - POINTERS_EQUAL(NULL, list->getOneFulfilledExpectationWithIgnoredParameters()); + POINTERS_EQUAL(NULL, list->getFirstMatchingExpectation()); } TEST(MockExpectedCallsList, toStringOnEmptyList) diff --git a/tests/CppUTestExt/MockParameterTest.cpp b/tests/CppUTestExt/MockParameterTest.cpp index bd9cf0062..74b608d2f 100644 --- a/tests/CppUTestExt/MockParameterTest.cpp +++ b/tests/CppUTestExt/MockParameterTest.cpp @@ -442,7 +442,7 @@ TEST(MockParameterTest, ignoreOtherParametersMultipleCallsButOneDidntHappen) MockCheckedExpectedCall* call = expectations.addFunction("boo"); call->ignoreOtherParameters(); call->callWasMade(1); - call->parametersWereIgnored(); + call->finalizeActualCallMatch(); call->ignoreOtherParameters(); expectations.addFunction("boo")->ignoreOtherParameters(); MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations); From ed56f3916328c7ce9cc6b3a96d25051fae5f4594 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Sun, 26 Jun 2016 16:32:47 +0200 Subject: [PATCH 02/26] 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. --- include/CppUTestExt/MockCheckedActualCall.h | 3 +- include/CppUTestExt/MockCheckedExpectedCall.h | 8 + include/CppUTestExt/MockExpectedCallsList.h | 8 +- include/CppUTestExt/MockSupport.h | 14 +- src/CppUTestExt/MockActualCall.cpp | 29 +- src/CppUTestExt/MockExpectedCall.cpp | 77 +++-- src/CppUTestExt/MockExpectedCallsList.cpp | 39 +-- src/CppUTestExt/MockFailure.cpp | 15 +- src/CppUTestExt/MockSupport.cpp | 66 +++-- .../CppUTestExt/ExpectedFunctionsListTest.cpp | 6 +- tests/CppUTestExt/MockCallTest.cpp | 267 +++++++++++++++++- tests/CppUTestExt/MockExpectedCallTest.cpp | 188 ++++++++---- .../MockFailureReporterForTest.cpp | 8 + .../CppUTestExt/MockFailureReporterForTest.h | 1 + tests/CppUTestExt/MockFailureTest.cpp | 64 ++--- tests/CppUTestExt/MockStrictOrderTest.cpp | 2 +- 16 files changed, 612 insertions(+), 183 deletions(-) diff --git a/include/CppUTestExt/MockCheckedActualCall.h b/include/CppUTestExt/MockCheckedActualCall.h index cd592881c..470c15631 100644 --- a/include/CppUTestExt/MockCheckedActualCall.h +++ b/include/CppUTestExt/MockCheckedActualCall.h @@ -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, @@ -120,6 +120,7 @@ class MockCheckedActualCall : public MockActualCall MockFailureReporter* reporter_; ActualCallState state_; + bool expectationsChecked_; MockCheckedExpectedCall* matchingExpectation_; MockExpectedCallsList potentiallyMatchingExpectations_; diff --git a/include/CppUTestExt/MockCheckedExpectedCall.h b/include/CppUTestExt/MockCheckedExpectedCall.h index b183d896d..50df9a2ef 100644 --- a/include/CppUTestExt/MockCheckedExpectedCall.h +++ b/include/CppUTestExt/MockCheckedExpectedCall.h @@ -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; @@ -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; @@ -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 { @@ -178,6 +185,7 @@ class MockExpectedCallComposite : public MockExpectedCall private: MockExpectedCallCompositeNode* head_; }; +#endif class MockIgnoredExpectedCall: public MockExpectedCall { diff --git a/include/CppUTestExt/MockExpectedCallsList.h b/include/CppUTestExt/MockExpectedCallsList.h index 479f31fdd..7154ad954 100644 --- a/include/CppUTestExt/MockExpectedCallsList.h +++ b/include/CppUTestExt/MockExpectedCallsList.h @@ -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; @@ -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); @@ -91,7 +90,6 @@ class MockExpectedCallsList : expectedCall_(expectedCall), next_(NULL) {} }; - virtual MockExpectedCallsListNode* findNodeWithCallOrderOf(int callOrder) const; private: MockExpectedCallsListNode* head_; diff --git a/include/CppUTestExt/MockSupport.h b/include/CppUTestExt/MockSupport.h index 575bc4fd8..39c4921f7 100755 --- a/include/CppUTestExt/MockSupport.h +++ b/include/CppUTestExt/MockSupport.h @@ -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(); @@ -124,7 +130,7 @@ class MockSupport void countCheck(); private: - int callOrder_; + int actualCallOrder_; int expectedCallOrder_; bool strictOrdering_; MockFailureReporter *activeReporter_; @@ -135,7 +141,9 @@ class MockSupport bool ignoreOtherCalls_; bool enabled_; MockCheckedActualCall *lastActualFunctionCall_; +#if 0 MockExpectedCallComposite compositeCalls_; +#endif MockNamedValueComparatorsAndCopiersRepository comparatorsAndCopiersRepository_; MockNamedValueList data_; const SimpleString mockName_; @@ -144,7 +152,7 @@ class MockSupport void checkExpectationsOfLastActualCall(); bool wasLastActualCallFulfilled(); - void failTestWithUnexpectedCalls(); + void failTestWithExpectedCallsNotFulfilled(); void failTestWithOutOfOrderCalls(); MockNamedValue* retrieveDataFromStore(const SimpleString& name); diff --git a/src/CppUTestExt/MockActualCall.cpp b/src/CppUTestExt/MockActualCall.cpp index a4ec864a0..feb97faf0 100644 --- a/src/CppUTestExt/MockActualCall.cpp +++ b/src/CppUTestExt/MockActualCall.cpp @@ -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); } @@ -124,9 +124,8 @@ void MockCheckedActualCall::callHasSucceeded() setState(CALL_SUCCEED); } -void MockCheckedActualCall::callIsInProgress() +void MockCheckedActualCall::discardCurrentlyMatchingExpectations() { - setState(CALL_IN_PROGRESS); if (matchingExpectation_) { matchingExpectation_->resetActualCallMatchingState(); @@ -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()) { @@ -147,7 +146,6 @@ MockActualCall& MockCheckedActualCall::withName(const SimpleString& name) return *this; } - potentiallyMatchingExpectations_.callWasMade(callOrder_); finalizeCallWhenMatchIsFound(); return *this; @@ -165,7 +163,8 @@ void MockCheckedActualCall::checkInputParameter(const MockNamedValue& actualPara return; } - callIsInProgress(); + setState(CALL_IN_PROGRESS); + discardCurrentlyMatchingExpectations(); potentiallyMatchingExpectations_.onlyKeepExpectationsWithInputParameter(actualParameter); @@ -186,7 +185,8 @@ void MockCheckedActualCall::checkOutputParameter(const MockNamedValue& outputPar return; } - callIsInProgress(); + setState(CALL_IN_PROGRESS); + discardCurrentlyMatchingExpectations(); potentiallyMatchingExpectations_.onlyKeepExpectationsWithOutputParameter(outputParameter); @@ -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; } @@ -349,6 +360,7 @@ void MockCheckedActualCall::checkExpectations() if (matchingExpectation_) { matchingExpectation_->finalizeActualCallMatch(); callHasSucceeded(); + matchingExpectation_->callWasMade(callOrder_); potentiallyMatchingExpectations_.resetActualCallMatchingState(); return; } @@ -518,7 +530,8 @@ MockActualCall& MockCheckedActualCall::onObject(const void* objectPtr) return *this; } - callIsInProgress(); + setState(CALL_IN_PROGRESS); + discardCurrentlyMatchingExpectations(); potentiallyMatchingExpectations_.onlyKeepExpectationsOnObject(objectPtr); diff --git a/src/CppUTestExt/MockExpectedCall.cpp b/src/CppUTestExt/MockExpectedCall.cpp index ac987c105..590872b83 100644 --- a/src/CppUTestExt/MockExpectedCall.cpp +++ b/src/CppUTestExt/MockExpectedCall.cpp @@ -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(); @@ -69,7 +80,6 @@ MockCheckedExpectedCall::~MockCheckedExpectedCall() MockExpectedCall& MockCheckedExpectedCall::withName(const SimpleString& name) { setName(name); - actualCallOrder_ = NOT_CALLED_YET; return *this; } @@ -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() @@ -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() @@ -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: ", 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: ", 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; } @@ -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){} @@ -737,6 +777,7 @@ MockExpectedCall& MockExpectedCallComposite::onObject(void* object) node->call_.onObject(object); return *this; } +#endif MockExpectedCall& MockIgnoredExpectedCall::instance() { diff --git a/src/CppUTestExt/MockExpectedCallsList.cpp b/src/CppUTestExt/MockExpectedCallsList.cpp index ba2407c68..5b4f9496a 100644 --- a/src/CppUTestExt/MockExpectedCallsList.cpp +++ b/src/CppUTestExt/MockExpectedCallsList.cpp @@ -50,9 +50,9 @@ bool MockExpectedCallsList::hasCallsOutOfOrder() const return false; } -int MockExpectedCallsList::size() const +unsigned int MockExpectedCallsList::size() const { - int count = 0; + unsigned int count = 0; for (MockExpectedCallsListNode* p = head_; p; p = p->next_) count++; return count; @@ -63,19 +63,17 @@ bool MockExpectedCallsList::isEmpty() const return size() == 0; } - -int MockExpectedCallsList::amountOfExpectationsFor(const SimpleString& name) const +unsigned int MockExpectedCallsList::amountOfExpectationsFor(const SimpleString& name) const { - int count = 0; + unsigned int count = 0; for (MockExpectedCallsListNode* p = head_; p; p = p->next_) - if (p->expectedCall_->relatesTo(name)) count++; + if (p->expectedCall_->relatesTo(name)) count += p->expectedCall_->getMaxCalls(); return count; - } -int MockExpectedCallsList::amountOfUnfulfilledExpectations() const +unsigned int MockExpectedCallsList::amountOfUnfulfilledExpectations() const { - int count = 0; + unsigned int count = 0; for (MockExpectedCallsListNode* p = head_; p; p = p->next_) if (! p->expectedCall_->isFulfilled()) count++; return count; @@ -285,12 +283,6 @@ void MockExpectedCallsList::resetActualCallMatchingState() p->expectedCall_->resetActualCallMatchingState(); } -void MockExpectedCallsList::callWasMade(int callOrder) -{ - for (MockExpectedCallsListNode* p = head_; p; p = p->next_) - p->expectedCall_->callWasMade(callOrder); -} - void MockExpectedCallsList::wasPassedToObject() { for (MockExpectedCallsListNode* p = head_; p; p = p->next_) @@ -310,14 +302,6 @@ void MockExpectedCallsList::outputParameterWasPassed(const SimpleString& paramet p->expectedCall_->outputParameterWasPassed(parameterName); } -MockExpectedCallsList::MockExpectedCallsListNode* MockExpectedCallsList::findNodeWithCallOrderOf(int callOrder) const -{ - for (MockExpectedCallsListNode* p = head_; p; p = p->next_) - if (p->expectedCall_->getCallOrder() == callOrder && p->expectedCall_->isFulfilled()) - return p; - return NULL; -} - static SimpleString stringOrNoneTextWhenEmpty(const SimpleString& inputString, const SimpleString& linePrefix) { SimpleString str = inputString; @@ -350,10 +334,9 @@ SimpleString MockExpectedCallsList::fulfilledCallsToString(const SimpleString& l { SimpleString str; - MockExpectedCallsListNode* nextNodeInOrder; - for (int callOrder = 1; (nextNodeInOrder = findNodeWithCallOrderOf(callOrder)); callOrder++) - if (nextNodeInOrder) - str = appendStringOnANewLine(str, linePrefix, nextNodeInOrder->expectedCall_->callToString()); + for (MockExpectedCallsListNode* p = head_; p; p = p->next_) + if (p->expectedCall_->isFulfilled()) + str = appendStringOnANewLine(str, linePrefix, p->expectedCall_->callToString()); return stringOrNoneTextWhenEmpty(str, linePrefix); } @@ -362,7 +345,7 @@ SimpleString MockExpectedCallsList::missingParametersToString() const { SimpleString str; for (MockExpectedCallsListNode* p = head_; p; p = p->next_) - if (! p->expectedCall_->isFulfilled()) + if (! p->expectedCall_->isMatchingActualCall()) str = appendStringOnANewLine(str, "", p->expectedCall_->missingParametersToString()); return stringOrNoneTextWhenEmpty(str, ""); diff --git a/src/CppUTestExt/MockFailure.cpp b/src/CppUTestExt/MockFailure.cpp index bb93ea503..cca41e6b6 100644 --- a/src/CppUTestExt/MockFailure.cpp +++ b/src/CppUTestExt/MockFailure.cpp @@ -71,9 +71,9 @@ MockFailure::MockFailure(UtestShell* test) : TestFailure(test, "Test failed with void MockFailure::addExpectationsAndCallHistory(const MockExpectedCallsList& expectations) { - message_ += "\tEXPECTED calls that did NOT happen:\n"; + message_ += "\tEXPECTED calls that WERE NOT fulfilled:\n"; message_ += expectations.unfulfilledCallsToString("\t\t"); - message_ += "\n\tACTUAL calls that did happen (in call order):\n"; + message_ += "\n\tEXPECTED calls that WERE fulfilled:\n"; message_ += expectations.fulfilledCallsToString("\t\t"); } @@ -82,13 +82,13 @@ void MockFailure::addExpectationsAndCallHistoryRelatedTo(const SimpleString& nam MockExpectedCallsList expectationsForFunction; expectationsForFunction.addExpectationsRelatedTo(name, expectations); - message_ += "\tEXPECTED calls that DID NOT happen related to function: "; + message_ += "\tEXPECTED calls that WERE NOT fulfilled related to function: "; message_ += name; message_ += "\n"; message_ += expectationsForFunction.unfulfilledCallsToString("\t\t"); - message_ += "\n\tACTUAL calls that DID happen related to function: "; + message_ += "\n\tEXPECTED calls that WERE fulfilled related to function: "; message_ += name; message_ += "\n"; @@ -97,14 +97,15 @@ void MockFailure::addExpectationsAndCallHistoryRelatedTo(const SimpleString& nam MockExpectedCallsDidntHappenFailure::MockExpectedCallsDidntHappenFailure(UtestShell* test, const MockExpectedCallsList& expectations) : MockFailure(test) { - message_ = "Mock Failure: Expected call did not happen.\n"; + message_ = "Mock Failure: Expected call WAS NOT fulfilled.\n"; addExpectationsAndCallHistory(expectations); } MockUnexpectedCallHappenedFailure::MockUnexpectedCallHappenedFailure(UtestShell* test, const SimpleString& name, const MockExpectedCallsList& expectations) : MockFailure(test) { - if (expectations.amountOfExpectationsFor(name)) { - SimpleString ordinalNumber = StringFromOrdinalNumber((unsigned)(expectations.amountOfExpectationsFor(name) + 1)); + unsigned int amountOfExpectations = expectations.amountOfExpectationsFor(name); + if (amountOfExpectations) { + SimpleString ordinalNumber = StringFromOrdinalNumber(amountOfExpectations + 1); message_ = StringFromFormat("Mock Failure: Unexpected additional (%s) call to function: ", ordinalNumber.asCharString()); } else { message_ = "Mock Failure: Unexpected call to function: "; diff --git a/src/CppUTestExt/MockSupport.cpp b/src/CppUTestExt/MockSupport.cpp index 2c8266834..316482f17 100755 --- a/src/CppUTestExt/MockSupport.cpp +++ b/src/CppUTestExt/MockSupport.cpp @@ -44,7 +44,7 @@ MockSupport& mock(const SimpleString& mockName, MockFailureReporter* failureRepo } MockSupport::MockSupport(const SimpleString& mockName) - : callOrder_(0), expectedCallOrder_(0), strictOrdering_(false), standardReporter_(&defaultReporter_), ignoreOtherCalls_(false), enabled_(true), lastActualFunctionCall_(NULL), mockName_(mockName), tracing_(false) + : actualCallOrder_(0), expectedCallOrder_(0), strictOrdering_(false), standardReporter_(&defaultReporter_), ignoreOtherCalls_(false), enabled_(true), lastActualFunctionCall_(NULL), mockName_(mockName), tracing_(false) { setActiveReporter(NULL); } @@ -120,10 +120,12 @@ void MockSupport::clear() expectations_.deleteAllExpectationsAndClearList(); unExpectations_.deleteAllExpectationsAndClearList(); +#if 0 compositeCalls_.clear(); +#endif ignoreOtherCalls_ = false; enabled_ = true; - callOrder_ = 0; + actualCallOrder_ = 0; expectedCallOrder_ = 0; strictOrdering_ = false; @@ -150,16 +152,12 @@ SimpleString MockSupport::appendScopeToName(const SimpleString& functionName) MockExpectedCall& MockSupport::expectOneCall(const SimpleString& functionName) { - if (!enabled_) return MockIgnoredExpectedCall::instance(); - - countCheck(); + return expectRangeOfCalls(1, 1, functionName); +} - MockCheckedExpectedCall* call = new MockCheckedExpectedCall; - call->withName(appendScopeToName(functionName)); - if (strictOrdering_) - call->withCallOrder(++expectedCallOrder_); - expectations_.addExpectedCall(call); - return *call; +MockExpectedCall& MockSupport::expectNCalls(unsigned int amount, const SimpleString& functionName) +{ + return expectRangeOfCalls(amount, amount, functionName); } void MockSupport::expectNoCall(const SimpleString& functionName) @@ -173,18 +171,48 @@ void MockSupport::expectNoCall(const SimpleString& functionName) unExpectations_.addExpectedCall(call); } -MockExpectedCall& MockSupport::expectNCalls(int amount, const SimpleString& functionName) +MockExpectedCall& MockSupport::expectAtLeastOneCall(const SimpleString& functionName) { - compositeCalls_.clear(); + return expectRangeOfCalls(1, (unsigned int)-1, functionName); +} - for (int i = 0; i < amount; i++) - compositeCalls_.add(expectOneCall(functionName)); - return compositeCalls_; +MockExpectedCall& MockSupport::expectAtLeastNCalls(unsigned int amount, const SimpleString& functionName) +{ + return expectRangeOfCalls(amount, (unsigned int)-1, functionName); +} + +MockExpectedCall& MockSupport::expectAtMostOneCall(const SimpleString& functionName) +{ + return expectRangeOfCalls(0, 1, functionName); +} + +MockExpectedCall& MockSupport::expectAtMostNCalls(unsigned int amount, const SimpleString& functionName) +{ + return expectRangeOfCalls(0, amount, functionName); +} + +MockExpectedCall& MockSupport::expectAnyCalls(const SimpleString& functionName) +{ + return expectRangeOfCalls(0, (unsigned int)-1, functionName); +} + +MockExpectedCall& MockSupport::expectRangeOfCalls(unsigned int minCalls, unsigned int maxCalls, const SimpleString& functionName) +{ + if (!enabled_) return MockIgnoredExpectedCall::instance(); + + countCheck(); + + MockCheckedExpectedCall* call = new MockCheckedExpectedCall(minCalls, maxCalls); + call->withName(appendScopeToName(functionName)); + if (strictOrdering_) + call->withCallOrder(++expectedCallOrder_); + expectations_.addExpectedCall(call); + return *call; } MockCheckedActualCall* MockSupport::createActualFunctionCall() { - lastActualFunctionCall_ = new MockCheckedActualCall(++callOrder_, activeReporter_, expectations_); + lastActualFunctionCall_ = new MockCheckedActualCall(++actualCallOrder_, activeReporter_, expectations_); return lastActualFunctionCall_; } @@ -280,7 +308,7 @@ bool MockSupport::wasLastActualCallFulfilled() return true; } -void MockSupport::failTestWithUnexpectedCalls() +void MockSupport::failTestWithExpectedCallsNotFulfilled() { MockExpectedCallsList expectationsList; expectationsList.addExpectations(expectations_); @@ -347,7 +375,7 @@ void MockSupport::checkExpectations() checkExpectationsOfLastActualCall(); if (wasLastActualCallFulfilled() && expectedCallsLeft()) - failTestWithUnexpectedCalls(); + failTestWithExpectedCallsNotFulfilled(); if (hasCallsOutOfOrder()) failTestWithOutOfOrderCalls(); diff --git a/tests/CppUTestExt/ExpectedFunctionsListTest.cpp b/tests/CppUTestExt/ExpectedFunctionsListTest.cpp index 1b8e55da3..cf313d4bf 100644 --- a/tests/CppUTestExt/ExpectedFunctionsListTest.cpp +++ b/tests/CppUTestExt/ExpectedFunctionsListTest.cpp @@ -116,9 +116,7 @@ TEST(MockExpectedCallsList, onlyKeepUnmatchingExpectations) { call1->withName("relate"); call2->withName("unrelate"); - call3->withName("relate"); - call2->callWasMade(1); - call3->callWasMade(2); + call3->withName("relate").withParameter("param",1); list->addExpectedCall(call1); list->addExpectedCall(call2); list->addExpectedCall(call3); @@ -262,7 +260,7 @@ TEST(MockExpectedCallsList, callToStringForFulfilledFunctions) list->addExpectedCall(call2); SimpleString expectedString; - expectedString = StringFromFormat("%s\n%s", call2->callToString().asCharString(), call1->callToString().asCharString()); + expectedString = StringFromFormat("%s\n%s", call1->callToString().asCharString(), call2->callToString().asCharString()); STRCMP_EQUAL(expectedString.asCharString(), list->fulfilledCallsToString().asCharString()); } diff --git a/tests/CppUTestExt/MockCallTest.cpp b/tests/CppUTestExt/MockCallTest.cpp index 44f3f7829..ca6e30762 100644 --- a/tests/CppUTestExt/MockCallTest.cpp +++ b/tests/CppUTestExt/MockCallTest.cpp @@ -49,20 +49,39 @@ TEST(MockCallTest, checkExpectationsDoesntFail) mock().checkExpectations(); } -TEST(MockCallTest, exceptACallThatHappens) +TEST(MockCallTest, expectASingleCallThatHappens) { mock().expectOneCall("func"); - mock().actualCall("func"); + MockCheckedActualCall& actualCall = (MockCheckedActualCall&) mock().actualCall("func"); + actualCall.checkExpectations(); CHECK(! mock().expectedCallsLeft()); } -TEST(MockCallTest, exceptACallInceasesExpectedCallsLeft) +TEST(MockCallTest, expectASingleCallThatDoesntHappen) { mock().expectOneCall("func"); CHECK(mock().expectedCallsLeft()); mock().clear(); } +TEST(MockCallTest, expectAMultiCallThatHappensTheMinimumTimes) +{ + mock().expectRangeOfCalls(2, 4, "func"); + mock().actualCall("func"); + MockCheckedActualCall& actualCall = (MockCheckedActualCall&) mock().actualCall("func"); + actualCall.checkExpectations(); + CHECK(! mock().expectedCallsLeft()); +} + +TEST(MockCallTest, expectAMultiCallThatDoesntHappenTheMinimumTimes) +{ + mock().expectRangeOfCalls(2, 4, "func"); + MockCheckedActualCall& actualCall = (MockCheckedActualCall&) mock().actualCall("func"); + actualCall.checkExpectations(); + CHECK(mock().expectedCallsLeft()); + mock().clear(); +} + TEST(MockCallTest, checkExpectationsClearsTheExpectations) { MockFailureReporterInstaller failureReporterInstaller; @@ -147,7 +166,7 @@ TEST(MockCallTest, expectOneCallInScopeButActualCallInGlobal) } -TEST(MockCallTest, expectMultipleCallsThatHappen) +TEST(MockCallTest, expectMultipleSingleCallsThatHappen) { mock().expectOneCall("foo"); mock().expectOneCall("foo"); @@ -383,7 +402,7 @@ TEST(MockCallTest, OnObjectExpectedButNotCalled) CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); } -TEST(MockCallTest, expectMultipleCalls) +TEST(MockCallTest, expectNCalls_Fulfilled) { mock().expectNCalls(2, "boo"); mock().actualCall("boo"); @@ -391,15 +410,251 @@ TEST(MockCallTest, expectMultipleCalls) mock().checkExpectations(); } +TEST(MockCallTest, expectAtLeastOneCall_Fulfilled_Minimum) +{ + mock().expectAtLeastOneCall("boo"); + mock().actualCall("boo"); + mock().checkExpectations(); +} + +TEST(MockCallTest, expectAtLeastOneCall_Fulfilled_MoreThanMinimum) +{ + mock().expectAtLeastOneCall("boo"); + mock().actualCall("boo"); + mock().actualCall("boo"); + mock().checkExpectations(); +} + +TEST(MockCallTest, expectAtLeastNCalls_Fulfilled_Minimum) +{ + mock().expectAtLeastNCalls(3, "boo"); + mock().actualCall("boo"); + mock().actualCall("boo"); + mock().actualCall("boo"); + mock().checkExpectations(); +} + +TEST(MockCallTest, expectAtLeastNCalls_Fulfilled_MoreThanMinimum) +{ + mock().expectAtLeastNCalls(3, "boo"); + mock().actualCall("boo"); + mock().actualCall("boo"); + mock().actualCall("boo"); + mock().actualCall("boo"); + mock().checkExpectations(); +} + +TEST(MockCallTest, expectAtMostOneCall_Fulfilled_Minimum) +{ + mock().expectAtMostOneCall("boo"); + mock().checkExpectations(); +} + +TEST(MockCallTest, expectAtMostOneCall_Fulfilled_Maximum) +{ + mock().expectAtMostOneCall("boo"); + mock().actualCall("boo"); + mock().checkExpectations(); +} + +TEST(MockCallTest, expectAtMostNCalls_Fulfilled_Minimum) +{ + mock().expectAtMostNCalls(2, "boo"); + mock().checkExpectations(); +} + +TEST(MockCallTest, expectAtMostNCalls_Fulfilled_BetweenMinAndMax) +{ + mock().expectAtMostNCalls(2, "boo"); + mock().actualCall("boo"); + mock().checkExpectations(); +} + +TEST(MockCallTest, expectAtMostNCalls_Fulfilled_Maximum) +{ + mock().expectAtMostNCalls(2, "boo"); + mock().actualCall("boo"); + mock().actualCall("boo"); + mock().checkExpectations(); +} + +TEST(MockCallTest, expectAnyCalls_Fulfilled_Minimum) +{ + mock().expectAnyCalls("boo"); + mock().checkExpectations(); +} + +TEST(MockCallTest, expectAnyCalls_Fulfilled_MoreThanMinimum) +{ + mock().expectAnyCalls("boo"); + mock().actualCall("boo"); + mock().checkExpectations(); +} + +TEST(MockCallTest, expectRangeOfCalls_Fulfilled_Minimum) +{ + mock().expectRangeOfCalls(2, 6, "boo"); + mock().actualCall("boo"); + mock().actualCall("boo"); + mock().checkExpectations(); +} + +TEST(MockCallTest, expectRangeOfCalls_Fulfilled_BetweenMinAndMax) +{ + mock().expectRangeOfCalls(2, 6, "boo"); + mock().actualCall("boo"); + mock().actualCall("boo"); + mock().actualCall("boo"); + mock().actualCall("boo"); + mock().checkExpectations(); +} + +TEST(MockCallTest, expectRangeOfCalls_Fulfilled_Maximum) +{ + mock().expectRangeOfCalls(2, 6, "boo"); + mock().actualCall("boo"); + mock().actualCall("boo"); + mock().actualCall("boo"); + mock().actualCall("boo"); + mock().actualCall("boo"); + mock().actualCall("boo"); + mock().checkExpectations(); +} + +TEST(MockCallTest, expectNCalls_NotFulfilled) +{ + MockFailureReporterInstaller failureReporterInstaller; + + MockExpectedCallsListForTest expectations; + expectations.addFunction(2, 2, "boo")->callWasMade(1); + MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations); + + mock().expectNCalls(2, "boo"); + mock().actualCall("boo"); + mock().checkExpectations(); + + CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); +} + +TEST(MockCallTest, expectAtLeastOneCall_NotFulfilled) +{ + MockFailureReporterInstaller failureReporterInstaller; + + MockExpectedCallsListForTest expectations; + expectations.addFunction(1, (unsigned int)-1, "boo"); + MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations); + + mock().expectAtLeastOneCall("boo"); + mock().checkExpectations(); + + CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); +} + +TEST(MockCallTest, expectAtLeastNCalls_NotFulfilled) +{ + MockFailureReporterInstaller failureReporterInstaller; + + MockExpectedCallsListForTest expectations; + MockCheckedExpectedCall* expectedCall = expectations.addFunction(3, (unsigned int)-1, "boo"); + expectedCall->callWasMade(1); + expectedCall->callWasMade(2); + MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations); + + mock().expectAtLeastNCalls(3, "boo"); + mock().actualCall("boo"); + mock().actualCall("boo"); + mock().checkExpectations(); + + CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); +} + +TEST(MockCallTest, expectAtMostOneCall_NotFulfilled) +{ + MockFailureReporterInstaller failureReporterInstaller; + + MockExpectedCallsListForTest expectations; + expectations.addFunction(0, 1, "boo")->callWasMade(1); + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "boo", expectations); + + mock().expectAtMostOneCall("boo"); + mock().actualCall("boo"); + mock().actualCall("boo"); + mock().checkExpectations(); + + CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); +} + +TEST(MockCallTest, expectAtMostNCalls_NotFulfilled) +{ + MockFailureReporterInstaller failureReporterInstaller; + + MockExpectedCallsListForTest expectations; + MockCheckedExpectedCall* expectedCall = expectations.addFunction(0, 3, "boo"); + expectedCall->callWasMade(1); + expectedCall->callWasMade(2); + expectedCall->callWasMade(3); + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "boo", expectations); + + mock().expectAtMostNCalls(3, "boo"); + mock().actualCall("boo"); + mock().actualCall("boo"); + mock().actualCall("boo"); + mock().actualCall("boo"); + mock().checkExpectations(); + + CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); +} + +TEST(MockCallTest, expectRangeOfCalls_NotFulfilled_BelowMinimum) +{ + MockFailureReporterInstaller failureReporterInstaller; + + MockExpectedCallsListForTest expectations; + MockCheckedExpectedCall* expectedCall = expectations.addFunction(2, 3, "boo"); + expectedCall->callWasMade(1); + MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations); + + mock().expectRangeOfCalls(2, 3, "boo"); + mock().actualCall("boo"); + mock().checkExpectations(); + + CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); +} + +TEST(MockCallTest, expectRangeOfCalls_NotFulfilled_AboveMaximum) +{ + MockFailureReporterInstaller failureReporterInstaller; + + MockExpectedCallsListForTest expectations; + MockCheckedExpectedCall* expectedCall = expectations.addFunction(2, 3, "boo"); + expectedCall->callWasMade(1); + expectedCall->callWasMade(2); + expectedCall->callWasMade(3); + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "boo", expectations); + + mock().expectRangeOfCalls(2, 3, "boo"); + mock().actualCall("boo"); + mock().actualCall("boo"); + mock().actualCall("boo"); + mock().actualCall("boo"); + mock().checkExpectations(); + + CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); +} + TEST(MockCallTest, shouldntFailTwice) { MockFailureReporterInstaller failureReporterInstaller; + mock().strictOrder(); mock().expectOneCall("foo"); + mock().expectOneCall("boo"); + mock().actualCall("boo"); mock().actualCall("bar"); mock().checkExpectations(); CHECK(!MockFailureReporterForTest::getReporter()->mockFailureString.contains("bar")); + CHECK(MockFailureReporterForTest::getReporter()->mockFailureString.contains("boo")); } TEST(MockCallTest, shouldReturnDefaultWhenThereIsntAnythingToReturn) @@ -429,6 +684,6 @@ TEST(MockCallTest, mockExpectationShouldIncreaseNumberOfChecks) TestTestingFixture fixture; fixture.setTestFunction(mocksAreCountedAsChecksTestFunction_); fixture.runAllTests(); - LONGS_EQUAL(5, fixture.getCheckCount()); + LONGS_EQUAL(3, fixture.getCheckCount()); } diff --git a/tests/CppUTestExt/MockExpectedCallTest.cpp b/tests/CppUTestExt/MockExpectedCallTest.cpp index 7c1a73670..e7fd933df 100644 --- a/tests/CppUTestExt/MockExpectedCallTest.cpp +++ b/tests/CppUTestExt/MockExpectedCallTest.cpp @@ -385,58 +385,131 @@ TEST(MockExpectedCall, callWithThreeDifferentParameter) DOUBLES_EQUAL(0.12, call->getInputParameter("double").getDoubleValue(), 0.05); } -TEST(MockExpectedCall, withoutANameItsFulfilled) +TEST(MockExpectedCall, singleCallNotMadeIsNotFulfilledButCanMatchActualCalls) { - CHECK(call->isFulfilled()); + MockCheckedExpectedCall expectedCall(1, 1); + expectedCall.withName("name"); + CHECK(!expectedCall.isFulfilled()); + CHECK(expectedCall.canMatchActualCalls()); } -TEST(MockExpectedCall, withANameItsNotFulfilled) +TEST(MockExpectedCall, singleCallMadeIsFulFilledAndCannotMatchActualCalls) { - call->withName("name"); - CHECK(!call->isFulfilled()); + MockCheckedExpectedCall expectedCall(1, 1); + expectedCall.callWasMade(1); + CHECK(expectedCall.isFulfilled()); + CHECK(!expectedCall.canMatchActualCalls()); } -TEST(MockExpectedCall, afterSettingCallFulfilledItsFulFilled) +TEST(MockExpectedCall, multiCallNotMadeIsNotFulfilledButCanMatchActualCalls) { - call->withName("name"); - call->callWasMade(1); - CHECK(call->isFulfilled()); + MockCheckedExpectedCall expectedCall(2, 3); + expectedCall.withName("name"); + CHECK(!expectedCall.isFulfilled()); + CHECK(expectedCall.canMatchActualCalls()); } -TEST(MockExpectedCall, calledButNotWithParameterIsNotFulFilled) +TEST(MockExpectedCall, multiCallNotMadeMinimumTimesIsNotFulfilledButCanMatchActualCalls) { - call->withName("name").withParameter("para", 1); - call->callWasMade(1); - CHECK(!call->isFulfilled()); + MockCheckedExpectedCall expectedCall(2, 3); + expectedCall.withName("name"); + expectedCall.callWasMade(1); + CHECK(!expectedCall.isFulfilled()); + CHECK(expectedCall.canMatchActualCalls()); } -TEST(MockExpectedCall, calledAndParametersAreFulfilled) +TEST(MockExpectedCall, multiCallMadeMinimumTimesIsFulfilledAndCanMatchActualCalls) { - call->withName("name").withParameter("para", 1); - call->callWasMade(1); - call->inputParameterWasPassed("para"); - CHECK(call->isFulfilled()); + MockCheckedExpectedCall expectedCall(2, 3); + expectedCall.withName("name"); + expectedCall.callWasMade(1); + expectedCall.callWasMade(2); + CHECK(expectedCall.isFulfilled()); + CHECK(expectedCall.canMatchActualCalls()); } -TEST(MockExpectedCall, calledButNotAllParametersAreFulfilled) +TEST(MockExpectedCall, multiCallsMadeMaximumTimesIsFulfilledAndCannotMatchActualCalls) { - call->withName("name").withParameter("para", 1).withParameter("two", 2); - call->callWasMade(1); - call->inputParameterWasPassed("para"); - CHECK(!call->isFulfilled()); + MockCheckedExpectedCall expectedCall(2, 3); + expectedCall.withName("name"); + expectedCall.callWasMade(1); + expectedCall.callWasMade(2); + expectedCall.callWasMade(3); + CHECK(expectedCall.isFulfilled()); + CHECK(!expectedCall.canMatchActualCalls()); } -TEST(MockExpectedCall, toStringForNoParameters) +TEST(MockExpectedCall, multiCallsMadeMoreThanMaximumTimesIsNotFulfilledAndCannotMatchActualCalls) { - call->withName("name"); - STRCMP_EQUAL("name -> no parameters", call->callToString().asCharString()); + MockCheckedExpectedCall expectedCall(2, 3); + expectedCall.withName("name"); + expectedCall.callWasMade(1); + expectedCall.callWasMade(2); + expectedCall.callWasMade(3); + expectedCall.callWasMade(4); + CHECK(!expectedCall.isFulfilled()); + CHECK(!expectedCall.canMatchActualCalls()); +} + +TEST(MockExpectedCall, callsWithoutParameterAlwaysMatch) +{ + MockCheckedExpectedCall expectedCall(1, 1); + CHECK(expectedCall.isMatchingActualCall()); +} + +TEST(MockExpectedCall, callsWithParameterNotFulfilledDontMatch) +{ + MockCheckedExpectedCall expectedCall(1, 1); + expectedCall.withParameter("para", 1); + CHECK(!expectedCall.isMatchingActualCall()); +} + +TEST(MockExpectedCall, callsWithParameterFulfilledDoMatch) +{ + MockCheckedExpectedCall expectedCall(1, 1); + expectedCall.withParameter("para", 1); + expectedCall.inputParameterWasPassed("para"); + CHECK(expectedCall.isMatchingActualCall()); +} + +TEST(MockExpectedCall, callsWithSomeParametersNotFulfilledDontMatch) +{ + MockCheckedExpectedCall expectedCall(1, 1); + expectedCall.withParameter("para", 1).withParameter("two", 2); + expectedCall.inputParameterWasPassed("para"); + CHECK(!expectedCall.isMatchingActualCall()); +} + +TEST(MockExpectedCall, toStringForNoParametersSingleCallNotCalled) +{ + MockCheckedExpectedCall expectedCall(1, 1); + expectedCall.withName("name"); + STRCMP_EQUAL("name -> no parameters (expected 1 call, but was called 0 times)", expectedCall.callToString().asCharString()); +} + +TEST(MockExpectedCall, toStringForNoParametersMultiCallCalledLessThanMinimum) +{ + MockCheckedExpectedCall expectedCall(2, 2); + expectedCall.withName("name"); + expectedCall.callWasMade(1); + STRCMP_EQUAL("name -> no parameters (expected 2 calls, but was called 1 time)", expectedCall.callToString().asCharString()); +} + +TEST(MockExpectedCall, toStringForNoParametersMultiCallCalledMinimum) +{ + MockCheckedExpectedCall expectedCall(2, 3); + expectedCall.withName("name"); + expectedCall.callWasMade(1); + expectedCall.callWasMade(2); + STRCMP_EQUAL("name -> no parameters (called 2 times)", expectedCall.callToString().asCharString()); } TEST(MockExpectedCall, toStringForIgnoredParameters) { - call->withName("name"); - call->ignoreOtherParameters(); - STRCMP_EQUAL("name -> all parameters ignored", call->callToString().asCharString()); + MockCheckedExpectedCall expectedCall(1, 1); + expectedCall.withName("name"); + expectedCall.ignoreOtherParameters(); + STRCMP_EQUAL("name -> all parameters ignored (expected 1 call, but was called 0 times)", expectedCall.callToString().asCharString()); } TEST(MockExpectedCall, toStringForMultipleInputParameters) @@ -444,11 +517,14 @@ TEST(MockExpectedCall, toStringForMultipleInputParameters) int int_value = 10; unsigned int uint_value = 7; - call->withName("name"); - call->withParameter("string", "value"); - call->withParameter("integer", int_value); - call->withParameter("unsigned-integer", uint_value); - STRCMP_EQUAL("name -> const char* string: , int integer: <10 (0xa)>, unsigned int unsigned-integer: <7 (0x7)>", call->callToString().asCharString()); + MockCheckedExpectedCall expectedCall(1, 1); + expectedCall.withName("name"); + expectedCall.withParameter("string", "value"); + expectedCall.withParameter("integer", int_value); + expectedCall.withParameter("unsigned-integer", uint_value); + expectedCall.callWasMade(1); + STRCMP_EQUAL("name -> const char* string: , int integer: <10 (0xa)>, unsigned int unsigned-integer: <7 (0x7)> " + "(called 1 time)", expectedCall.callToString().asCharString()); } TEST(MockExpectedCall, toStringForMultipleInputAndOutputParameters) @@ -457,38 +533,46 @@ TEST(MockExpectedCall, toStringForMultipleInputAndOutputParameters) unsigned int uint_value = 7; unsigned char buffer_value[3]; - call->withName("name"); - call->withParameter("string", "value"); - call->withParameter("integer", int_value); - call->withParameter("unsigned-integer", uint_value); - call->withOutputParameterReturning("buffer", buffer_value, sizeof(buffer_value)); + MockCheckedExpectedCall expectedCall(1, 1); + expectedCall.withName("name"); + expectedCall.withParameter("string", "value"); + expectedCall.withParameter("integer", int_value); + expectedCall.withParameter("unsigned-integer", uint_value); + expectedCall.withOutputParameterReturning("buffer", buffer_value, sizeof(buffer_value)); + expectedCall.callWasMade(1); STRCMP_EQUAL("name -> const char* string: , int integer: <10 (0xa)>, unsigned int unsigned-integer: <7 (0x7)>, " - "const void* buffer: ", call->callToString().asCharString()); + "const void* buffer: (called 1 time)", expectedCall.callToString().asCharString()); } TEST(MockExpectedCall, toStringForMultipleOutputParameters) { unsigned char buffer_value[3]; - call->withName("name"); - call->withOutputParameterReturning("buffer1", buffer_value, sizeof(buffer_value)); - call->withOutputParameterReturning("buffer2", buffer_value, sizeof(buffer_value)); - STRCMP_EQUAL("name -> const void* buffer1: , const void* buffer2: ", call->callToString().asCharString()); + MockCheckedExpectedCall expectedCall(1, 1); + expectedCall.withName("name"); + expectedCall.withOutputParameterReturning("buffer1", buffer_value, sizeof(buffer_value)); + expectedCall.withOutputParameterReturning("buffer2", buffer_value, sizeof(buffer_value)); + expectedCall.callWasMade(1); + STRCMP_EQUAL("name -> const void* buffer1: , const void* buffer2: (called 1 time)", expectedCall.callToString().asCharString()); } TEST(MockExpectedCall, toStringForParameterAndIgnored) { - call->withName("name"); - call->withParameter("string", "value"); - call->ignoreOtherParameters(); - STRCMP_EQUAL("name -> const char* string: , other parameters are ignored", call->callToString().asCharString()); + MockCheckedExpectedCall expectedCall(1, 1); + expectedCall.withName("name"); + expectedCall.withParameter("string", "value"); + expectedCall.ignoreOtherParameters(); + expectedCall.callWasMade(1); + STRCMP_EQUAL("name -> const char* string: , other parameters are ignored (called 1 time)", expectedCall.callToString().asCharString()); } TEST(MockExpectedCall, toStringForCallOrder) { - call->withName("name"); - call->withCallOrder(2); - STRCMP_EQUAL("name -> expected call order: <2> -> no parameters", call->callToString().asCharString()); + MockCheckedExpectedCall expectedCall(1, 1); + expectedCall.withName("name"); + expectedCall.withCallOrder(2); + expectedCall.callWasMade(1); + STRCMP_EQUAL("name -> expected call order: <2> -> no parameters (called 1 time)", expectedCall.callToString().asCharString()); } TEST(MockExpectedCall, callOrderIsNotFulfilledWithWrongOrder) @@ -553,6 +637,7 @@ TEST(MockExpectedCall, hasNoOutputParameterOfTypeDifferentType) CHECK_FALSE(call->hasOutputParameter(foo)); } +#if 0 static MockExpectedCallComposite composite; TEST_GROUP(MockExpectedCallComposite) @@ -726,6 +811,7 @@ TEST(MockExpectedCallComposite, doesNotSupportCallOrder) fixture.runAllTests(); fixture.assertPrintContains("withCallOrder not supported for CompositeCalls"); } +#endif TEST_GROUP(MockIgnoredExpectedCall) { diff --git a/tests/CppUTestExt/MockFailureReporterForTest.cpp b/tests/CppUTestExt/MockFailureReporterForTest.cpp index 4e0b0cfb6..0974b5567 100644 --- a/tests/CppUTestExt/MockFailureReporterForTest.cpp +++ b/tests/CppUTestExt/MockFailureReporterForTest.cpp @@ -104,6 +104,14 @@ MockCheckedExpectedCall* MockExpectedCallsListForTest::addFunction(const SimpleS return newCall; } +MockCheckedExpectedCall* MockExpectedCallsListForTest::addFunction(unsigned int minCalls, unsigned int maxCalls, const SimpleString& name) +{ + MockCheckedExpectedCall* newCall = new MockCheckedExpectedCall(minCalls, maxCalls); + newCall->withName(name); + addExpectedCall(newCall); + return newCall; +} + MockCheckedExpectedCall* MockExpectedCallsListForTest::addFunction(const SimpleString& name, int order) { MockCheckedExpectedCall* newCall = addFunction(name); diff --git a/tests/CppUTestExt/MockFailureReporterForTest.h b/tests/CppUTestExt/MockFailureReporterForTest.h index bbc58e793..c5386c7c6 100644 --- a/tests/CppUTestExt/MockFailureReporterForTest.h +++ b/tests/CppUTestExt/MockFailureReporterForTest.h @@ -60,6 +60,7 @@ class MockExpectedCallsListForTest : public MockExpectedCallsList public: ~MockExpectedCallsListForTest(); MockCheckedExpectedCall* addFunction(const SimpleString& name); + MockCheckedExpectedCall* addFunction(unsigned int minCalls, unsigned int maxCalls, const SimpleString& name); MockCheckedExpectedCall* addFunction(const SimpleString& name, int order); }; diff --git a/tests/CppUTestExt/MockFailureTest.cpp b/tests/CppUTestExt/MockFailureTest.cpp index d1356515f..014d35153 100644 --- a/tests/CppUTestExt/MockFailureTest.cpp +++ b/tests/CppUTestExt/MockFailureTest.cpp @@ -90,9 +90,9 @@ TEST(MockFailureTest, unexpectedCallHappened) { MockUnexpectedCallHappenedFailure failure(UtestShell::getCurrent(), "foobar", *list); STRCMP_EQUAL("Mock Failure: Unexpected call to function: foobar\n" - "\tEXPECTED calls that did NOT happen:\n" + "\tEXPECTED calls that WERE NOT fulfilled:\n" "\t\t\n" - "\tACTUAL calls that did happen (in call order):\n" + "\tEXPECTED calls that WERE fulfilled:\n" "\t\t", failure.getMessage().asCharString()); } @@ -105,12 +105,12 @@ TEST(MockFailureTest, expectedCallDidNotHappen) addAllToList(); MockExpectedCallsDidntHappenFailure failure(UtestShell::getCurrent(), *list); - STRCMP_EQUAL("Mock Failure: Expected call did not happen.\n" - "\tEXPECTED calls that did NOT happen:\n" - "\t\tfoobar -> no parameters\n" - "\t\tworld -> int boo: <2 (0x2)>, const char* hello: \n" - "\tACTUAL calls that did happen (in call order):\n" - "\t\thaphaphap -> no parameters", failure.getMessage().asCharString()); + STRCMP_EQUAL("Mock Failure: Expected call WAS NOT fulfilled.\n" + "\tEXPECTED calls that WERE NOT fulfilled:\n" + "\t\tfoobar -> no parameters (expected 1 call, but was called 0 times)\n" + "\t\tworld -> int boo: <2 (0x2)>, const char* hello: (expected 1 call, but was called 0 times)\n" + "\tEXPECTED calls that WERE fulfilled:\n" + "\t\thaphaphap -> no parameters (called 1 time)", failure.getMessage().asCharString()); } TEST(MockFailureTest, MockUnexpectedNthAdditionalCallFailure) @@ -139,10 +139,10 @@ TEST(MockFailureTest, MockUnexpectedInputParameterFailure) MockUnexpectedInputParameterFailure failure(UtestShell::getCurrent(), "foo", actualParameter, *list); STRCMP_EQUAL("Mock Failure: Unexpected parameter name to function \"foo\": bar\n" - "\tEXPECTED calls that DID NOT happen related to function: foo\n" - "\t\tfoo -> int boo: <2 (0x2)>\n" - "\t\tfoo -> double boo: <3.3>\n" - "\tACTUAL calls that DID happen related to function: foo\n" + "\tEXPECTED calls that WERE NOT fulfilled related to function: foo\n" + "\t\tfoo -> int boo: <2 (0x2)> (expected 1 call, but was called 0 times)\n" + "\t\tfoo -> double boo: <3.3> (expected 1 call, but was called 0 times)\n" + "\tEXPECTED calls that WERE fulfilled related to function: foo\n" "\t\t\n" "\tACTUAL unexpected parameter passed to function: foo\n" "\t\tint bar: <2 (0x2)>", failure.getMessage().asCharString()); @@ -162,10 +162,10 @@ TEST(MockFailureTest, MockUnexpectedOutputParameterFailure) MockUnexpectedOutputParameterFailure failure(UtestShell::getCurrent(), "foo", actualParameter, *list); STRCMP_EQUAL("Mock Failure: Unexpected output parameter name to function \"foo\": bar\n" - "\tEXPECTED calls that DID NOT happen related to function: foo\n" - "\t\tfoo -> const void* boo: \n" - "\t\tfoo -> const void* boo: \n" - "\tACTUAL calls that DID happen related to function: foo\n" + "\tEXPECTED calls that WERE NOT fulfilled related to function: foo\n" + "\t\tfoo -> const void* boo: (expected 1 call, but was called 0 times)\n" + "\t\tfoo -> const void* boo: (expected 1 call, but was called 0 times)\n" + "\tEXPECTED calls that WERE fulfilled related to function: foo\n" "\t\t\n" "\tACTUAL unexpected output parameter passed to function: foo\n" "\t\tvoid* bar", failure.getMessage().asCharString()); @@ -183,10 +183,10 @@ TEST(MockFailureTest, MockUnexpectedParameterValueFailure) MockUnexpectedInputParameterFailure failure(UtestShell::getCurrent(), "foo", actualParameter, *list); STRCMP_EQUAL("Mock Failure: Unexpected parameter value to parameter \"boo\" to function \"foo\": <20 (0x14)>\n" - "\tEXPECTED calls that DID NOT happen related to function: foo\n" - "\t\tfoo -> int boo: <2 (0x2)>\n" - "\t\tfoo -> int boo: <10 (0xa)>\n" - "\tACTUAL calls that DID happen related to function: foo\n" + "\tEXPECTED calls that WERE NOT fulfilled related to function: foo\n" + "\t\tfoo -> int boo: <2 (0x2)> (expected 1 call, but was called 0 times)\n" + "\t\tfoo -> int boo: <10 (0xa)> (expected 1 call, but was called 0 times)\n" + "\tEXPECTED calls that WERE fulfilled related to function: foo\n" "\t\t\n" "\tACTUAL unexpected parameter passed to function: foo\n" "\t\tint boo: <20 (0x14)>", failure.getMessage().asCharString()); @@ -204,10 +204,10 @@ TEST(MockFailureTest, MockExpectedParameterDidntHappenFailure) MockExpectedParameterDidntHappenFailure failure(UtestShell::getCurrent(), "foo", *list); STRCMP_EQUAL("Mock Failure: Expected parameter for function \"foo\" did not happen.\n" - "\tEXPECTED calls that DID NOT happen related to function: foo\n" - "\t\tfoo -> int bar: <2 (0x2)>, const char* boo: \n" - "\tACTUAL calls that DID happen related to function: foo\n" - "\t\tfoo -> int bar: <10 (0xa)>, const char* boo: \n" + "\tEXPECTED calls that WERE NOT fulfilled related to function: foo\n" + "\t\tfoo -> int bar: <2 (0x2)>, const char* boo: (expected 1 call, but was called 0 times)\n" + "\tEXPECTED calls that WERE fulfilled related to function: foo\n" + "\t\tfoo -> int bar: <10 (0xa)>, const char* boo: (called 1 time)\n" "\tMISSING parameters that didn't happen:\n" "\t\tint bar, const char* boo", failure.getMessage().asCharString()); } @@ -231,10 +231,10 @@ TEST(MockFailureTest, MockUnexpectedObjectFailure) STRCMP_EQUAL(StringFromFormat ( "MockFailure: Function called on an unexpected object: foo\n" "\tActual object for call has address: <%p>\n" - "\tEXPECTED calls that DID NOT happen related to function: foo\n" - "\t\t(object address: %p)::foo -> no parameters\n" - "\tACTUAL calls that DID happen related to function: foo\n" - "\t\t(object address: %p)::foo -> no parameters", + "\tEXPECTED calls that WERE NOT fulfilled related to function: foo\n" + "\t\t(object address: %p)::foo -> no parameters (expected 1 call, but was called 0 times)\n" + "\tEXPECTED calls that WERE fulfilled related to function: foo\n" + "\t\t(object address: %p)::foo -> no parameters (called 1 time)", (void*) 0x01, (void*) 0x02, (void*) 0x03).asCharString(), failure.getMessage().asCharString()); } @@ -250,9 +250,9 @@ TEST(MockFailureTest, MockExpectedObjectDidntHappenFailure) MockExpectedObjectDidntHappenFailure failure(UtestShell::getCurrent(), "foo", *list); STRCMP_EQUAL(StringFromFormat( "Mock Failure: Expected call on object for function \"foo\" but it did not happen.\n" - "\tEXPECTED calls that DID NOT happen related to function: foo\n" - "\t\t(object address: %p)::foo -> no parameters\n" - "\tACTUAL calls that DID happen related to function: foo\n" - "\t\t(object address: %p)::foo -> no parameters", + "\tEXPECTED calls that WERE NOT fulfilled related to function: foo\n" + "\t\t(object address: %p)::foo -> no parameters (expected 1 call, but was called 0 times)\n" + "\tEXPECTED calls that WERE fulfilled related to function: foo\n" + "\t\t(object address: %p)::foo -> no parameters (called 1 time)", (void*) 0x2, (void*) 0x3).asCharString(), failure.getMessage().asCharString()); } diff --git a/tests/CppUTestExt/MockStrictOrderTest.cpp b/tests/CppUTestExt/MockStrictOrderTest.cpp index 41fe96d9d..4c3d1f494 100644 --- a/tests/CppUTestExt/MockStrictOrderTest.cpp +++ b/tests/CppUTestExt/MockStrictOrderTest.cpp @@ -178,7 +178,7 @@ TEST(MockStrictOrderTest, orderViolatedAcrossScopes) mock("mock2").checkExpectations(); } -TEST(MockStrictOrderTest, orderUsingNCalls) +IGNORE_TEST(MockStrictOrderTest, orderUsingNCalls) { mock().strictOrder(); From 8ee564aab45a024a2e24dba991947b2f29ad2c2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Sun, 26 Jun 2016 18:36:14 +0200 Subject: [PATCH 03/26] Added C language new calls for multi-matching expected calls. --- include/CppUTestExt/MockSupport_c.h | 8 +++- src/CppUTestExt/MockSupport_c.cpp | 52 ++++++++++++++++++++++++- tests/CppUTestExt/MockSupport_cTest.cpp | 46 ++++++++++++++++++++++ 3 files changed, 103 insertions(+), 3 deletions(-) diff --git a/include/CppUTestExt/MockSupport_c.h b/include/CppUTestExt/MockSupport_c.h index 0706d5472..18cedaa77 100644 --- a/include/CppUTestExt/MockSupport_c.h +++ b/include/CppUTestExt/MockSupport_c.h @@ -150,7 +150,13 @@ struct SMockSupport_c void (*strictOrder)(void); MockExpectedCall_c* (*expectOneCall)(const char* name); void (*expectNoCall)(const char* name); - MockExpectedCall_c* (*expectNCalls)(int number, const char* name); + MockExpectedCall_c* (*expectNCalls)(unsigned int number, const char* name); + MockExpectedCall_c* (*expectAtLeastOneCall)(const char* name); + MockExpectedCall_c* (*expectAtLeastNCalls)(unsigned int amount, const char* name); + MockExpectedCall_c* (*expectAtMostOneCall)(const char* name); + MockExpectedCall_c* (*expectAtMostNCalls)(unsigned int amount, const char* name); + MockExpectedCall_c* (*expectAnyCalls)(const char* name); + MockExpectedCall_c* (*expectRangeOfCalls)(unsigned int minCalls, unsigned int maxCalls, const char* name); MockActualCall_c* (*actualCall)(const char* name); int (*hasReturnValue)(void); MockValue_c (*returnValue)(void); diff --git a/src/CppUTestExt/MockSupport_c.cpp b/src/CppUTestExt/MockSupport_c.cpp index 69145b7de..ab3cb102f 100644 --- a/src/CppUTestExt/MockSupport_c.cpp +++ b/src/CppUTestExt/MockSupport_c.cpp @@ -120,7 +120,13 @@ extern "C" { void strictOrder_c(); MockExpectedCall_c* expectOneCall_c(const char* name); void expectNoCall_c(const char* name); -MockExpectedCall_c* expectNCalls_c(const int number, const char* name); +MockExpectedCall_c* expectNCalls_c(const unsigned int number, const char* name); +MockExpectedCall_c* expectAtLeastOneCall_c(const char* name); +MockExpectedCall_c* expectAtLeastNCalls_c(const unsigned int number, const char* name); +MockExpectedCall_c* expectAtMostOneCall_c(const char* name); +MockExpectedCall_c* expectAtMostNCalls_c(const unsigned int number, const char* name); +MockExpectedCall_c* expectAnyCalls_c(const char* name); +MockExpectedCall_c* expectRangeOfCalls_c(const unsigned int minCalls, const unsigned int maxCalls, const char* name); MockActualCall_c* actualCall_c(const char* name); void disable_c(); void enable_c(); @@ -303,6 +309,12 @@ static MockSupport_c gMockSupport = { expectOneCall_c, expectNoCall_c, expectNCalls_c, + expectAtLeastOneCall_c, + expectAtLeastNCalls_c, + expectAtMostOneCall_c, + expectAtMostNCalls_c, + expectAnyCalls_c, + expectRangeOfCalls_c, actualCall_c, hasReturnValue_c, returnValue_c, @@ -568,12 +580,48 @@ void expectNoCall_c(const char* name) currentMockSupport->expectNoCall(name); } -MockExpectedCall_c* expectNCalls_c(const int number, const char* name) +MockExpectedCall_c* expectNCalls_c(const unsigned int number, const char* name) { expectedCall = ¤tMockSupport->expectNCalls(number, name); return &gExpectedCall; } +MockExpectedCall_c* expectAtLeastOneCall_c(const char* name) +{ + expectedCall = ¤tMockSupport->expectAtLeastOneCall(name); + return &gExpectedCall; +} + +MockExpectedCall_c* expectAtLeastNCalls_c(const unsigned int number, const char* name) +{ + expectedCall = ¤tMockSupport->expectAtLeastNCalls(number, name); + return &gExpectedCall; +} + +MockExpectedCall_c* expectAtMostOneCall_c(const char* name) +{ + expectedCall = ¤tMockSupport->expectAtMostOneCall(name); + return &gExpectedCall; +} + +MockExpectedCall_c* expectAtMostNCalls_c(const unsigned int number, const char* name) +{ + expectedCall = ¤tMockSupport->expectAtMostNCalls(number, name); + return &gExpectedCall; +} + +MockExpectedCall_c* expectAnyCalls_c(const char* name) +{ + expectedCall = ¤tMockSupport->expectAnyCalls(name); + return &gExpectedCall; +} + +MockExpectedCall_c* expectRangeOfCalls_c(const unsigned int minCalls, const unsigned int maxCalls, const char* name) +{ + expectedCall = ¤tMockSupport->expectRangeOfCalls(minCalls, maxCalls, name); + return &gExpectedCall; +} + MockActualCall_c* actualCall_c(const char* name) { actualCall = ¤tMockSupport->actualCall(name); diff --git a/tests/CppUTestExt/MockSupport_cTest.cpp b/tests/CppUTestExt/MockSupport_cTest.cpp index 5188857f3..869302071 100644 --- a/tests/CppUTestExt/MockSupport_cTest.cpp +++ b/tests/CppUTestExt/MockSupport_cTest.cpp @@ -92,6 +92,52 @@ TEST(MockSupport_c, expectNoCall) mock_c()->checkExpectations(); } +TEST(MockSupport_c, expectAtLeastOneCallFulfilled) +{ + mock_c()->expectAtLeastOneCall("boo"); + mock_c()->actualCall("boo"); + mock_c()->checkExpectations(); +} + +TEST(MockSupport_c, expectAtLeastNCallsFulfilled) +{ + mock_c()->expectAtLeastNCalls(2, "boo"); + mock_c()->actualCall("boo"); + mock_c()->actualCall("boo"); + mock_c()->checkExpectations(); +} + +TEST(MockSupport_c, expectAtMostOneCallFulfilled) +{ + mock_c()->expectAtMostOneCall("boo"); + mock_c()->checkExpectations(); +} + +TEST(MockSupport_c, expectAtMostNCallsFulfilled) +{ + mock_c()->expectAtMostNCalls(4, "boo"); + mock_c()->actualCall("boo"); + mock_c()->actualCall("boo"); + mock_c()->actualCall("boo"); + mock_c()->actualCall("boo"); + mock_c()->checkExpectations(); +} + +TEST(MockSupport_c, expectAnyCallsFulfilled) +{ + mock_c()->expectAnyCalls("boo"); + mock_c()->checkExpectations(); +} + +TEST(MockSupport_c, expectRangeOfCallsFulfilled) +{ + mock_c()->expectRangeOfCalls(2, 4, "boo"); + mock_c()->actualCall("boo"); + mock_c()->actualCall("boo"); + mock_c()->actualCall("boo"); + mock_c()->checkExpectations(); +} + TEST(MockSupport_c, expectAndActualParameters) { mock_c()->expectOneCall("boo")->withIntParameters("integer", 1)->withDoubleParameters("double", 1.0)-> From 0d2a162b88b64f813aca8546a673872c1f11215b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Sun, 26 Jun 2016 18:58:23 +0200 Subject: [PATCH 04/26] Removed MockExpectedCallComposite. --- include/CppUTestExt/MockCheckedExpectedCall.h | 46 ---- include/CppUTestExt/MockSupport.h | 3 - src/CppUTestExt/MockExpectedCall.cpp | 229 ------------------ src/CppUTestExt/MockSupport.cpp | 3 - tests/CppUTestExt/MockExpectedCallTest.cpp | 176 -------------- 5 files changed, 457 deletions(-) diff --git a/include/CppUTestExt/MockCheckedExpectedCall.h b/include/CppUTestExt/MockCheckedExpectedCall.h index 50df9a2ef..5c5bec0bc 100644 --- a/include/CppUTestExt/MockCheckedExpectedCall.h +++ b/include/CppUTestExt/MockCheckedExpectedCall.h @@ -141,52 +141,6 @@ class MockCheckedExpectedCall : public MockExpectedCall unsigned int maxCalls_; }; -#if 0 -struct MockExpectedCallCompositeNode; -class MockExpectedCallComposite : public MockExpectedCall -{ -public: - MockExpectedCallComposite(); - virtual ~MockExpectedCallComposite(); - - virtual MockExpectedCall& withName(const SimpleString& name) _override; - virtual MockExpectedCall& withCallOrder(int callOrder) _override; - virtual MockExpectedCall& withBoolParameter(const SimpleString& name, bool value) _override; - virtual MockExpectedCall& withIntParameter(const SimpleString& name, int value) _override; - virtual MockExpectedCall& withUnsignedIntParameter(const SimpleString& name, unsigned int value) _override; - virtual MockExpectedCall& withLongIntParameter(const SimpleString& name, long int value) _override; - virtual MockExpectedCall& withUnsignedLongIntParameter(const SimpleString& name, unsigned long int value) _override; - virtual MockExpectedCall& withDoubleParameter(const SimpleString& name, double value) _override; - virtual MockExpectedCall& withStringParameter(const SimpleString& name, const char* value) _override; - virtual MockExpectedCall& withConstPointerParameter(const SimpleString& name, const void* value) _override; - virtual MockExpectedCall& withPointerParameter(const SimpleString& name, void* value) _override; - virtual MockExpectedCall& withFunctionPointerParameter(const SimpleString& name, void (*value)()) _override; - virtual MockExpectedCall& withMemoryBufferParameter(const SimpleString& name, const unsigned char* value, size_t size) _override; - virtual MockExpectedCall& withParameterOfType(const SimpleString& typeName, const SimpleString& name, const void* value) _override; - virtual MockExpectedCall& withOutputParameterReturning(const SimpleString& name, const void* value, size_t size) _override; - virtual MockExpectedCall& withOutputParameterOfTypeReturning(const SimpleString& typeName, const SimpleString& name, const void* value) _override; - virtual MockExpectedCall& ignoreOtherParameters() _override; - - virtual MockExpectedCall& andReturnValue(bool value) _override; - virtual MockExpectedCall& andReturnValue(int value) _override; - virtual MockExpectedCall& andReturnValue(unsigned int value) _override; - virtual MockExpectedCall& andReturnValue(long int value) _override; - virtual MockExpectedCall& andReturnValue(unsigned long int value) _override; - virtual MockExpectedCall& andReturnValue(double value) _override; - virtual MockExpectedCall& andReturnValue(const char* value) _override; - virtual MockExpectedCall& andReturnValue(void* value) _override; - virtual MockExpectedCall& andReturnValue(const void* value) _override; - virtual MockExpectedCall& andReturnValue(void (*value)()) _override; - - virtual MockExpectedCall& onObject(void* objectPtr) _override; - - virtual void add(MockExpectedCall& call); - virtual void clear(); -private: - MockExpectedCallCompositeNode* head_; -}; -#endif - class MockIgnoredExpectedCall: public MockExpectedCall { public: diff --git a/include/CppUTestExt/MockSupport.h b/include/CppUTestExt/MockSupport.h index 39c4921f7..c39856826 100755 --- a/include/CppUTestExt/MockSupport.h +++ b/include/CppUTestExt/MockSupport.h @@ -141,9 +141,6 @@ class MockSupport bool ignoreOtherCalls_; bool enabled_; MockCheckedActualCall *lastActualFunctionCall_; -#if 0 - MockExpectedCallComposite compositeCalls_; -#endif MockNamedValueComparatorsAndCopiersRepository comparatorsAndCopiersRepository_; MockNamedValueList data_; const SimpleString mockName_; diff --git a/src/CppUTestExt/MockExpectedCall.cpp b/src/CppUTestExt/MockExpectedCall.cpp index 590872b83..d339daeb9 100644 --- a/src/CppUTestExt/MockExpectedCall.cpp +++ b/src/CppUTestExt/MockExpectedCall.cpp @@ -550,235 +550,6 @@ unsigned int MockCheckedExpectedCall::getMaxCalls() const return maxCalls_; } -#if 0 -struct MockExpectedCallCompositeNode -{ - MockExpectedCallCompositeNode(MockExpectedCall& functionCall, MockExpectedCallCompositeNode* next) : next_(next), call_(functionCall){} - - MockExpectedCallCompositeNode* next_; - MockExpectedCall& call_; -}; - -MockExpectedCallComposite::MockExpectedCallComposite() : head_(NULL) -{ -} - -MockExpectedCallComposite::~MockExpectedCallComposite() -{ - -} - -void MockExpectedCallComposite::add(MockExpectedCall& call) -{ - head_ = new MockExpectedCallCompositeNode(call, head_); -} - -void MockExpectedCallComposite::clear() -{ - while (head_) { - MockExpectedCallCompositeNode* next = head_->next_; - delete head_; - head_ = next; - } -} - -MockExpectedCall& MockExpectedCallComposite::withName(const SimpleString& name) -{ - for (MockExpectedCallCompositeNode* node = head_; node != NULL; node = node->next_) - node->call_.withName(name); - return *this; -} - -MockExpectedCall& MockExpectedCallComposite::withCallOrder(int) -{ - FAIL("withCallOrder not supported for CompositeCalls"); - return *this; // LCOV_EXCL_LINE -} - -MockExpectedCall& MockExpectedCallComposite::withBoolParameter(const SimpleString& name, bool value) -{ - for (MockExpectedCallCompositeNode* node = head_; node != NULL; node = node->next_) - node->call_.withParameter(name, value); - return *this; -} - -MockExpectedCall& MockExpectedCallComposite::withUnsignedIntParameter(const SimpleString& name, unsigned int value) -{ - for (MockExpectedCallCompositeNode* node = head_; node != NULL; node = node->next_) - node->call_.withParameter(name, value); - return *this; -} - -MockExpectedCall& MockExpectedCallComposite::withIntParameter(const SimpleString& name, int value) -{ - for (MockExpectedCallCompositeNode* node = head_; node != NULL; node = node->next_) - node->call_.withParameter(name, value); - return *this; -} - -MockExpectedCall& MockExpectedCallComposite::withLongIntParameter(const SimpleString& name, long int value) -{ - for (MockExpectedCallCompositeNode* node = head_; node != NULL; node = node->next_) - node->call_.withParameter(name, value); - return *this; -} - -MockExpectedCall& MockExpectedCallComposite::withUnsignedLongIntParameter(const SimpleString& name, unsigned long int value) -{ - for (MockExpectedCallCompositeNode* node = head_; node != NULL; node = node->next_) - node->call_.withParameter(name, value); - return *this; -} - -MockExpectedCall& MockExpectedCallComposite::withDoubleParameter(const SimpleString& name, double value) -{ - for (MockExpectedCallCompositeNode* node = head_; node != NULL; node = node->next_) - node->call_.withParameter(name, value); - return *this; - -} - -MockExpectedCall& MockExpectedCallComposite::withStringParameter(const SimpleString& name, const char* value) -{ - for (MockExpectedCallCompositeNode* node = head_; node != NULL; node = node->next_) - node->call_.withParameter(name, value); - return *this; -} - -MockExpectedCall& MockExpectedCallComposite::withPointerParameter(const SimpleString& name, void* value) -{ - for (MockExpectedCallCompositeNode* node = head_; node != NULL; node = node->next_) - node->call_.withParameter(name, value); - return *this; -} - -MockExpectedCall& MockExpectedCallComposite::withConstPointerParameter(const SimpleString& name, const void* value) -{ - for (MockExpectedCallCompositeNode* node = head_; node != NULL; node = node->next_) - node->call_.withParameter(name, value); - return *this; -} - -MockExpectedCall& MockExpectedCallComposite::withFunctionPointerParameter(const SimpleString& name, void (*value)()) -{ - for (MockExpectedCallCompositeNode* node = head_; node != NULL; node = node->next_) - node->call_.withParameter(name, value); - return *this; -} - -MockExpectedCall& MockExpectedCallComposite::withMemoryBufferParameter(const SimpleString& name, const unsigned char* value, size_t size) -{ - for (MockExpectedCallCompositeNode* node = head_; node != NULL; node = node->next_) - node->call_.withParameter(name, value, size); - return *this; -} - -MockExpectedCall& MockExpectedCallComposite::withParameterOfType(const SimpleString& typeName, const SimpleString& name, const void* value) -{ - for (MockExpectedCallCompositeNode* node = head_; node != NULL; node = node->next_) - node->call_.withParameterOfType(typeName, name, value); - return *this; -} - -MockExpectedCall& MockExpectedCallComposite::withOutputParameterReturning(const SimpleString& name, const void* value, size_t size) -{ - for (MockExpectedCallCompositeNode* node = head_; node != NULL; node = node->next_) - node->call_.withOutputParameterReturning(name, value, size); - return *this; -} - -MockExpectedCall& MockExpectedCallComposite::withOutputParameterOfTypeReturning(const SimpleString& typeName, const SimpleString& name, const void* value) -{ - for (MockExpectedCallCompositeNode* node = head_; node != NULL; node = node->next_) - node->call_.withOutputParameterOfTypeReturning(typeName, name, value); - return *this; -} - -MockExpectedCall& MockExpectedCallComposite::ignoreOtherParameters() -{ - for (MockExpectedCallCompositeNode* node = head_; node != NULL; node = node->next_) - node->call_.ignoreOtherParameters(); - return *this; -} - -MockExpectedCall& MockExpectedCallComposite::andReturnValue(bool value) -{ - for (MockExpectedCallCompositeNode* node = head_; node != NULL; node = node->next_) - node->call_.andReturnValue(value); - return *this; -} - -MockExpectedCall& MockExpectedCallComposite::andReturnValue(unsigned int value) -{ - for (MockExpectedCallCompositeNode* node = head_; node != NULL; node = node->next_) - node->call_.andReturnValue(value); - return *this; -} - -MockExpectedCall& MockExpectedCallComposite::andReturnValue(int value) -{ - for (MockExpectedCallCompositeNode* node = head_; node != NULL; node = node->next_) - node->call_.andReturnValue(value); - return *this; -} - -MockExpectedCall& MockExpectedCallComposite::andReturnValue(long int value) -{ - for (MockExpectedCallCompositeNode* node = head_; node != NULL; node = node->next_) - node->call_.andReturnValue(value); - return *this; -} - -MockExpectedCall& MockExpectedCallComposite::andReturnValue(unsigned long int value) -{ - for (MockExpectedCallCompositeNode* node = head_; node != NULL; node = node->next_) - node->call_.andReturnValue(value); - return *this; -} - -MockExpectedCall& MockExpectedCallComposite::andReturnValue(double value) -{ - for (MockExpectedCallCompositeNode* node = head_; node != NULL; node = node->next_) - node->call_.andReturnValue(value); - return *this; -} - -MockExpectedCall& MockExpectedCallComposite::andReturnValue(const char* value) -{ - for (MockExpectedCallCompositeNode* node = head_; node != NULL; node = node->next_) - node->call_.andReturnValue(value); - return *this; -} - -MockExpectedCall& MockExpectedCallComposite::andReturnValue(void* value) -{ - for (MockExpectedCallCompositeNode* node = head_; node != NULL; node = node->next_) - node->call_.andReturnValue(value); - return *this; -} - -MockExpectedCall& MockExpectedCallComposite::andReturnValue(const void* value) -{ - for (MockExpectedCallCompositeNode* node = head_; node != NULL; node = node->next_) - node->call_.andReturnValue(value); - return *this; -} - -MockExpectedCall& MockExpectedCallComposite::andReturnValue(void (*value)()) -{ - for (MockExpectedCallCompositeNode* node = head_; node != NULL; node = node->next_) - node->call_.andReturnValue(value); - return *this; -} - -MockExpectedCall& MockExpectedCallComposite::onObject(void* object) -{ - for (MockExpectedCallCompositeNode* node = head_; node != NULL; node = node->next_) - node->call_.onObject(object); - return *this; -} -#endif - MockExpectedCall& MockIgnoredExpectedCall::instance() { static MockIgnoredExpectedCall call; diff --git a/src/CppUTestExt/MockSupport.cpp b/src/CppUTestExt/MockSupport.cpp index 316482f17..0f684e734 100755 --- a/src/CppUTestExt/MockSupport.cpp +++ b/src/CppUTestExt/MockSupport.cpp @@ -120,9 +120,6 @@ void MockSupport::clear() expectations_.deleteAllExpectationsAndClearList(); unExpectations_.deleteAllExpectationsAndClearList(); -#if 0 - compositeCalls_.clear(); -#endif ignoreOtherCalls_ = false; enabled_ = true; actualCallOrder_ = 0; diff --git a/tests/CppUTestExt/MockExpectedCallTest.cpp b/tests/CppUTestExt/MockExpectedCallTest.cpp index e7fd933df..6e99fc0e3 100644 --- a/tests/CppUTestExt/MockExpectedCallTest.cpp +++ b/tests/CppUTestExt/MockExpectedCallTest.cpp @@ -637,182 +637,6 @@ TEST(MockExpectedCall, hasNoOutputParameterOfTypeDifferentType) CHECK_FALSE(call->hasOutputParameter(foo)); } -#if 0 -static MockExpectedCallComposite composite; - -TEST_GROUP(MockExpectedCallComposite) -{ - MockCheckedExpectedCall call; - - void setup() _override - { - composite.add(call); - composite.withName("name"); - } - - void teardown() _override - { - CHECK_NO_MOCK_FAILURE(); - composite.clear(); - } -}; - -TEST(MockExpectedCallComposite, hasBoolParameter) -{ - composite.withParameter("param", true); - STRCMP_EQUAL("name -> bool param: ", call.callToString().asCharString()); -} - -TEST(MockExpectedCallComposite, hasLongIntParameter) -{ - composite.withParameter("param", (long int) 1); - STRCMP_EQUAL("name -> long int param: <1 (0x1)>", call.callToString().asCharString()); -} - -TEST(MockExpectedCallComposite, hasUnsignedLongIntParameter) -{ - composite.withParameter("param", (unsigned long int) 5); - STRCMP_EQUAL("name -> unsigned long int param: <5 (0x5)>", call.callToString().asCharString()); -} - -TEST(MockExpectedCallComposite, hasPointerParameter) -{ - composite.withParameter("param", (void*) 0); - STRCMP_EQUAL("name -> void* param: <0x0>", call.callToString().asCharString()); -} - -TEST(MockExpectedCallComposite, hasConstPointerParameter) -{ - composite.withParameter("param", (const void*) 0); - STRCMP_EQUAL("name -> const void* param: <0x0>", call.callToString().asCharString()); -} - -TEST(MockExpectedCallComposite, hasFunctionPointerParameter) -{ - composite.withParameter("param", (void (*)()) 0); - STRCMP_EQUAL("name -> void (*)() param: <0x0>", call.callToString().asCharString()); -} - -TEST(MockExpectedCallComposite, hasMemoryBufferParameter) -{ - const unsigned char mem_buffer[] = { 0x89, 0xFE, 0x15 }; - composite.withParameter("param", mem_buffer, sizeof(mem_buffer)); - STRCMP_EQUAL("name -> const unsigned char* param: ", call.callToString().asCharString()); -} - -TEST(MockExpectedCallComposite, hasParameterOfType) -{ - composite.withParameterOfType("type", "param", (const void*) 0); - STRCMP_EQUAL("name -> type param: ", call.callToString().asCharString()); -} - -TEST(MockExpectedCallComposite, hasOutputParameterReturning) -{ - composite.withOutputParameterReturning("out", (const void*) 0, 1); - STRCMP_EQUAL("name -> const void* out: ", call.callToString().asCharString()); -} - -TEST(MockExpectedCallComposite, hasOutputParameterOfTypeReturning) -{ - composite.withOutputParameterOfTypeReturning("type", "out", (const void*) 0); - STRCMP_EQUAL("name -> type out: ", call.callToString().asCharString()); -} - -TEST(MockExpectedCallComposite, hasBoolReturnValue) -{ - composite.andReturnValue(true); - STRCMP_EQUAL("bool", call.returnValue().getType().asCharString()); - CHECK_EQUAL(true, call.returnValue().getBoolValue()); -} - -TEST(MockExpectedCallComposite, hasUnsignedIntReturnValue) -{ - composite.andReturnValue((unsigned int) 2); - STRCMP_EQUAL("unsigned int", call.returnValue().getType().asCharString()); - LONGS_EQUAL(2, call.returnValue().getUnsignedIntValue()); -} - -TEST(MockExpectedCallComposite, hasIntReturnValue) -{ - composite.andReturnValue((int) -5); - STRCMP_EQUAL("int", call.returnValue().getType().asCharString()); - LONGS_EQUAL(-5, call.returnValue().getIntValue()); -} - -TEST(MockExpectedCallComposite, hasLongIntReturnValue) -{ - composite.andReturnValue((long int) -17); - STRCMP_EQUAL("long int", call.returnValue().getType().asCharString()); - LONGS_EQUAL(-17, call.returnValue().getLongIntValue()); -} - -TEST(MockExpectedCallComposite, hasUnsignedLongIntReturnValue) -{ - composite.andReturnValue((unsigned long int) 6); - STRCMP_EQUAL("unsigned long int", call.returnValue().getType().asCharString()); - LONGS_EQUAL(6, call.returnValue().getUnsignedLongIntValue()); -} - -TEST(MockExpectedCallComposite, hasDoubleReturnValue) -{ - composite.andReturnValue((double) 3.005); - STRCMP_EQUAL("double", call.returnValue().getType().asCharString()); - DOUBLES_EQUAL(3.005, call.returnValue().getDoubleValue(), 0.0001); -} - -TEST(MockExpectedCallComposite, hasStringReturnValue) -{ - composite.andReturnValue("hello"); - STRCMP_EQUAL("const char*", call.returnValue().getType().asCharString()); - STRCMP_EQUAL("hello", call.returnValue().getStringValue()); -} - -TEST(MockExpectedCallComposite, hasPointerReturnValue) -{ - composite.andReturnValue((void*) 0); - STRCMP_EQUAL("void*", call.returnValue().getType().asCharString()); - POINTERS_EQUAL((void*) 0, call.returnValue().getPointerValue()); -} - -TEST(MockExpectedCallComposite, hasConstPointerReturnValue) -{ - composite.andReturnValue((const void*) 0); - STRCMP_EQUAL("const void*", call.returnValue().getType().asCharString()); - POINTERS_EQUAL((const void*) 0, call.returnValue().getConstPointerValue()); -} - -TEST(MockExpectedCallComposite, hasFunctionPointerReturnValue) -{ - composite.andReturnValue((void(*)()) 0); - STRCMP_EQUAL("void (*)()", call.returnValue().getType().asCharString()); - FUNCTIONPOINTERS_EQUAL((void(*)()) 0, call.returnValue().getFunctionPointerValue()); -} - -TEST(MockExpectedCallComposite, isOnObject) -{ - composite.onObject(&composite); - SimpleString info("(object address: "); - info += StringFromFormat("%p", (void*) &composite); - info += ")::name -> no parameters"; - STRCMP_EQUAL(info.asCharString(), call.callToString().asCharString()); -} - -#include "CppUTest/TestTestingFixture.h" - -static void withCallOrderNotSupportedFailMethod_(void) -{ - composite.withCallOrder(5); -} // LCOV_EXCL_LINE - -TEST(MockExpectedCallComposite, doesNotSupportCallOrder) -{ - TestTestingFixture fixture; - fixture.setTestFunction(&withCallOrderNotSupportedFailMethod_); - fixture.runAllTests(); - fixture.assertPrintContains("withCallOrder not supported for CompositeCalls"); -} -#endif - TEST_GROUP(MockIgnoredExpectedCall) { MockIgnoredExpectedCall ignored; From e94cd9377cd01ba5b3f0a9bf27b18eb56c31fd7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Mon, 27 Jun 2016 21:37:01 +0200 Subject: [PATCH 05/26] Added test that checks that non-fulfilled calls have higher matching preference than fulfilled ones. --- tests/CppUTestExt/MockCallTest.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/CppUTestExt/MockCallTest.cpp b/tests/CppUTestExt/MockCallTest.cpp index ca6e30762..fa99cdd4d 100644 --- a/tests/CppUTestExt/MockCallTest.cpp +++ b/tests/CppUTestExt/MockCallTest.cpp @@ -642,6 +642,18 @@ TEST(MockCallTest, expectRangeOfCalls_NotFulfilled_AboveMaximum) CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); } +TEST(MockCallTest, expectedCallsNotFulfilledMatchBeforeFulfilledOnes) +{ + mock().expectAtLeastOneCall("foo"); + mock().expectAtLeastNCalls(2, "foo"); + + mock().actualCall("foo"); + mock().actualCall("foo"); + mock().actualCall("foo"); + + mock().checkExpectations(); +} + TEST(MockCallTest, shouldntFailTwice) { MockFailureReporterInstaller failureReporterInstaller; From f42377ef2147fc0709c8ae83af24d77557e86cab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Mon, 27 Jun 2016 21:42:45 +0200 Subject: [PATCH 06/26] When creating the list of potentially matching expected calls, non-fulfilled ones have higher preference than fulfilled ones. --- src/CppUTestExt/MockExpectedCallsList.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/CppUTestExt/MockExpectedCallsList.cpp b/src/CppUTestExt/MockExpectedCallsList.cpp index 5b4f9496a..43c6297cb 100644 --- a/src/CppUTestExt/MockExpectedCallsList.cpp +++ b/src/CppUTestExt/MockExpectedCallsList.cpp @@ -122,9 +122,21 @@ void MockExpectedCallsList::addExpectedCall(MockCheckedExpectedCall* call) void MockExpectedCallsList::addPotentiallyMatchingExpectations(const MockExpectedCallsList& list) { - for (MockExpectedCallsListNode* p = list.head_; p; p = p->next_) - if (p->expectedCall_->canMatchActualCalls()) - addExpectedCall(p->expectedCall_); + MockExpectedCallsList alreadyFulfilled; + + for (MockExpectedCallsListNode* p = list.head_; p; p = p->next_) { + if (p->expectedCall_->canMatchActualCalls()) { + // Calls that can match actual calls but are already fulfilled (i.e. have been called at least the minimum number of times) + // have lower preference, so they will be added _after_ non-fulfilled ones. + if (p->expectedCall_->isFulfilled()) { + alreadyFulfilled.addExpectedCall(p->expectedCall_); + } else { + addExpectedCall(p->expectedCall_); + } + } + } + + addExpectations(alreadyFulfilled); } void MockExpectedCallsList::addExpectationsRelatedTo(const SimpleString& name, const MockExpectedCallsList& list) From 162d117013d12855af7914b9143fd3fccf12bd68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Mon, 27 Jun 2016 22:27:50 +0200 Subject: [PATCH 07/26] Renamed MockExpectedCallsDidntHappenFailure to MockExpectedCallsNotFulfilledFailure. --- include/CppUTestExt/MockFailure.h | 4 ++-- src/CppUTestExt/MockFailure.cpp | 2 +- src/CppUTestExt/MockSupport.cpp | 2 +- tests/CppUTestExt/MockCallTest.cpp | 14 +++++++------- tests/CppUTestExt/MockComparatorCopierTest.cpp | 4 ++-- tests/CppUTestExt/MockFailureTest.cpp | 2 +- tests/CppUTestExt/MockHierarchyTest.cpp | 2 +- tests/CppUTestExt/MockParameterTest.cpp | 4 ++-- tests/CppUTestExt/MockPluginTest.cpp | 2 +- 9 files changed, 18 insertions(+), 18 deletions(-) diff --git a/include/CppUTestExt/MockFailure.h b/include/CppUTestExt/MockFailure.h index 8cfdced74..b9c5f3c10 100644 --- a/include/CppUTestExt/MockFailure.h +++ b/include/CppUTestExt/MockFailure.h @@ -60,10 +60,10 @@ class MockFailure : public TestFailure void addExpectationsAndCallHistoryRelatedTo(const SimpleString& function, const MockExpectedCallsList& expectations); }; -class MockExpectedCallsDidntHappenFailure : public MockFailure +class MockExpectedCallsNotFulfilledFailure : public MockFailure { public: - MockExpectedCallsDidntHappenFailure(UtestShell* test, const MockExpectedCallsList& expectations); + MockExpectedCallsNotFulfilledFailure(UtestShell* test, const MockExpectedCallsList& expectations); }; class MockUnexpectedCallHappenedFailure : public MockFailure diff --git a/src/CppUTestExt/MockFailure.cpp b/src/CppUTestExt/MockFailure.cpp index cca41e6b6..a25eeff75 100644 --- a/src/CppUTestExt/MockFailure.cpp +++ b/src/CppUTestExt/MockFailure.cpp @@ -95,7 +95,7 @@ void MockFailure::addExpectationsAndCallHistoryRelatedTo(const SimpleString& nam message_ += expectationsForFunction.fulfilledCallsToString("\t\t"); } -MockExpectedCallsDidntHappenFailure::MockExpectedCallsDidntHappenFailure(UtestShell* test, const MockExpectedCallsList& expectations) : MockFailure(test) +MockExpectedCallsNotFulfilledFailure::MockExpectedCallsNotFulfilledFailure(UtestShell* test, const MockExpectedCallsList& expectations) : MockFailure(test) { message_ = "Mock Failure: Expected call WAS NOT fulfilled.\n"; addExpectationsAndCallHistory(expectations); diff --git a/src/CppUTestExt/MockSupport.cpp b/src/CppUTestExt/MockSupport.cpp index 0f684e734..768847fa8 100755 --- a/src/CppUTestExt/MockSupport.cpp +++ b/src/CppUTestExt/MockSupport.cpp @@ -314,7 +314,7 @@ void MockSupport::failTestWithExpectedCallsNotFulfilled() if(getMockSupport(p)) expectationsList.addExpectations(getMockSupport(p)->expectations_); - MockExpectedCallsDidntHappenFailure failure(activeReporter_->getTestToFail(), expectationsList); + MockExpectedCallsNotFulfilledFailure failure(activeReporter_->getTestToFail(), expectationsList); clear(); failTest(failure); } diff --git a/tests/CppUTestExt/MockCallTest.cpp b/tests/CppUTestExt/MockCallTest.cpp index fa99cdd4d..2b6c227ec 100644 --- a/tests/CppUTestExt/MockCallTest.cpp +++ b/tests/CppUTestExt/MockCallTest.cpp @@ -88,7 +88,7 @@ TEST(MockCallTest, checkExpectationsClearsTheExpectations) MockExpectedCallsListForTest expectations; expectations.addFunction("foobar"); - MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations); + MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations); mock().expectOneCall("foobar"); mock().checkExpectations(); @@ -104,7 +104,7 @@ TEST(MockCallTest, expectOneCallInScopeButNotHappen) MockExpectedCallsListForTest expectations; expectations.addFunction("scope::foobar"); - MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations); + MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations); mock("scope").expectOneCall("foobar"); mock().checkExpectations(); @@ -324,7 +324,7 @@ TEST(MockCallTest, ignoreOtherStillFailsIfExpectedOneDidntHappen) MockExpectedCallsListForTest expectations; expectations.addFunction("foo"); - MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations); + MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations); mock().expectOneCall("foo"); mock().ignoreOtherCalls(); @@ -527,7 +527,7 @@ TEST(MockCallTest, expectNCalls_NotFulfilled) MockExpectedCallsListForTest expectations; expectations.addFunction(2, 2, "boo")->callWasMade(1); - MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations); + MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations); mock().expectNCalls(2, "boo"); mock().actualCall("boo"); @@ -542,7 +542,7 @@ TEST(MockCallTest, expectAtLeastOneCall_NotFulfilled) MockExpectedCallsListForTest expectations; expectations.addFunction(1, (unsigned int)-1, "boo"); - MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations); + MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations); mock().expectAtLeastOneCall("boo"); mock().checkExpectations(); @@ -558,7 +558,7 @@ TEST(MockCallTest, expectAtLeastNCalls_NotFulfilled) MockCheckedExpectedCall* expectedCall = expectations.addFunction(3, (unsigned int)-1, "boo"); expectedCall->callWasMade(1); expectedCall->callWasMade(2); - MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations); + MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations); mock().expectAtLeastNCalls(3, "boo"); mock().actualCall("boo"); @@ -612,7 +612,7 @@ TEST(MockCallTest, expectRangeOfCalls_NotFulfilled_BelowMinimum) MockExpectedCallsListForTest expectations; MockCheckedExpectedCall* expectedCall = expectations.addFunction(2, 3, "boo"); expectedCall->callWasMade(1); - MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations); + MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations); mock().expectRangeOfCalls(2, 3, "boo"); mock().actualCall("boo"); diff --git a/tests/CppUTestExt/MockComparatorCopierTest.cpp b/tests/CppUTestExt/MockComparatorCopierTest.cpp index 0b83a6811..65d0237f1 100644 --- a/tests/CppUTestExt/MockComparatorCopierTest.cpp +++ b/tests/CppUTestExt/MockComparatorCopierTest.cpp @@ -149,7 +149,7 @@ TEST(MockComparatorCopierTest, customObjectWithFunctionComparatorThatFailsCovers MockExpectedCallsListForTest expectations; expectations.addFunction("function")->withParameterOfType("MyTypeForTesting", "parameterName", &object); - MockExpectedCallsDidntHappenFailure failure(UtestShell::getCurrent(), expectations); + MockExpectedCallsNotFulfilledFailure failure(UtestShell::getCurrent(), expectations); mock().expectOneCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object); mock().checkExpectations(); @@ -184,7 +184,7 @@ TEST(MockComparatorCopierTest, noActualCallForCustomTypeOutputParameter) MockExpectedCallsListForTest expectations; expectations.addFunction("foo")->withOutputParameterOfTypeReturning("MyTypeForTesting", "output", &expectedObject); - MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations); + MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations); mock().expectOneCall("foo").withOutputParameterOfTypeReturning("MyTypeForTesting", "output", &expectedObject); mock().checkExpectations(); diff --git a/tests/CppUTestExt/MockFailureTest.cpp b/tests/CppUTestExt/MockFailureTest.cpp index 014d35153..d9f04dc8d 100644 --- a/tests/CppUTestExt/MockFailureTest.cpp +++ b/tests/CppUTestExt/MockFailureTest.cpp @@ -104,7 +104,7 @@ TEST(MockFailureTest, expectedCallDidNotHappen) call3->callWasMade(1); addAllToList(); - MockExpectedCallsDidntHappenFailure failure(UtestShell::getCurrent(), *list); + MockExpectedCallsNotFulfilledFailure failure(UtestShell::getCurrent(), *list); STRCMP_EQUAL("Mock Failure: Expected call WAS NOT fulfilled.\n" "\tEXPECTED calls that WERE NOT fulfilled:\n" "\t\tfoobar -> no parameters (expected 1 call, but was called 0 times)\n" diff --git a/tests/CppUTestExt/MockHierarchyTest.cpp b/tests/CppUTestExt/MockHierarchyTest.cpp index 800ec74dc..cc771c8e1 100644 --- a/tests/CppUTestExt/MockHierarchyTest.cpp +++ b/tests/CppUTestExt/MockHierarchyTest.cpp @@ -96,7 +96,7 @@ TEST(MockHierarchyTest, checkExpectationsWorksHierarchically) MockExpectedCallsListForTest expectations; expectations.addFunction("first::foobar"); expectations.addFunction("second::helloworld"); - MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations); + MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations); mock("first").expectOneCall("foobar"); mock("second").expectOneCall("helloworld"); diff --git a/tests/CppUTestExt/MockParameterTest.cpp b/tests/CppUTestExt/MockParameterTest.cpp index 74b608d2f..9d4d728e2 100644 --- a/tests/CppUTestExt/MockParameterTest.cpp +++ b/tests/CppUTestExt/MockParameterTest.cpp @@ -445,7 +445,7 @@ TEST(MockParameterTest, ignoreOtherParametersMultipleCallsButOneDidntHappen) call->finalizeActualCallMatch(); call->ignoreOtherParameters(); expectations.addFunction("boo")->ignoreOtherParameters(); - MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations); + MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations); mock().expectOneCall("boo").ignoreOtherParameters(); mock().expectOneCall("boo").ignoreOtherParameters(); @@ -492,7 +492,7 @@ TEST(MockParameterTest, noActualCallForOutputParameter) mock().expectOneCall("foo").withOutputParameterReturning("output", &output, sizeof(output)); expectations.addFunction("foo")->withOutputParameterReturning("output", &output, sizeof(output)); - MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations); + MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations); mock().checkExpectations(); CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); diff --git a/tests/CppUTestExt/MockPluginTest.cpp b/tests/CppUTestExt/MockPluginTest.cpp index ae1739b86..da0c628ec 100644 --- a/tests/CppUTestExt/MockPluginTest.cpp +++ b/tests/CppUTestExt/MockPluginTest.cpp @@ -59,7 +59,7 @@ TEST(MockPlugin, checkExpectationsAndClearAtEnd) MockExpectedCallsListForTest expectations; expectations.addFunction("foobar"); - MockExpectedCallsDidntHappenFailure expectedFailure(test, expectations); + MockExpectedCallsNotFulfilledFailure expectedFailure(test, expectations); mock().expectOneCall("foobar"); From 94c5c69a72188f667df0fb00fde100e4af1a28ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Tue, 28 Jun 2016 00:47:15 +0200 Subject: [PATCH 08/26] Re-enabled strict ordering checks when the expected calls have no optional calls (i.e. expectOneCall and expectNCalls are allowed, but not calls where the minimum and maximum number of calls are not equal). --- include/CppUTestExt/MockCheckedExpectedCall.h | 13 +++-- include/CppUTestExt/MockExpectedCall.h | 3 +- include/CppUTestExt/MockFailure.h | 7 +++ include/CppUTestExt/MockSupport.h | 5 +- src/CppUTestExt/MockExpectedCall.cpp | 34 ++++++----- src/CppUTestExt/MockFailure.cpp | 6 +- src/CppUTestExt/MockSupport.cpp | 14 +++-- tests/CppUTestExt/MockExpectedCallTest.cpp | 56 ++++++++++++++++++- tests/CppUTestExt/MockStrictOrderTest.cpp | 17 +++++- 9 files changed, 119 insertions(+), 36 deletions(-) diff --git a/include/CppUTestExt/MockCheckedExpectedCall.h b/include/CppUTestExt/MockCheckedExpectedCall.h index 5c5bec0bc..82864897f 100644 --- a/include/CppUTestExt/MockCheckedExpectedCall.h +++ b/include/CppUTestExt/MockCheckedExpectedCall.h @@ -40,7 +40,8 @@ class MockCheckedExpectedCall : public MockExpectedCall virtual ~MockCheckedExpectedCall(); virtual MockExpectedCall& withName(const SimpleString& name) _override; - virtual MockExpectedCall& withCallOrder(int callOrder) _override; + virtual MockExpectedCall& withCallOrder(unsigned int callOrder) _override { return withCallOrder(callOrder, callOrder); } + virtual MockExpectedCall& withCallOrder(unsigned int initialCallOrder, unsigned int finalCallOrder) _override; virtual MockExpectedCall& withBoolParameter(const SimpleString& name, bool value) _override; virtual MockExpectedCall& withIntParameter(const SimpleString& name, int value) _override; virtual MockExpectedCall& withUnsignedIntParameter(const SimpleString& name, unsigned int value) _override; @@ -101,8 +102,7 @@ class MockCheckedExpectedCall : public MockExpectedCall virtual SimpleString callToString(); virtual SimpleString missingParametersToString(); - enum { NOT_CALLED_YET = -1, NO_EXPECTED_CALL_ORDER = -1}; - virtual int getCallOrder() const; + enum { NO_EXPECTED_CALL_ORDER = 0 }; virtual unsigned int getMaxCalls() const; @@ -128,8 +128,8 @@ class MockCheckedExpectedCall : public MockExpectedCall bool ignoreOtherParameters_; bool isActualCallMatchFinalized_; - int actualCallOrder_; - int expectedCallOrder_; + unsigned int initialExpectedCallOrder_; + unsigned int finalExpectedCallOrder_; bool outOfOrder_; MockNamedValueList* inputParameters_; MockNamedValueList* outputParameters_; @@ -146,7 +146,8 @@ class MockIgnoredExpectedCall: public MockExpectedCall public: virtual MockExpectedCall& withName(const SimpleString&) _override { return *this;} - virtual MockExpectedCall& withCallOrder(int) _override { return *this; } + virtual MockExpectedCall& withCallOrder(unsigned int) _override { return *this; } + virtual MockExpectedCall& withCallOrder(unsigned int, unsigned int) _override { return *this; } virtual MockExpectedCall& withBoolParameter(const SimpleString&, bool) _override { return *this; } virtual MockExpectedCall& withIntParameter(const SimpleString&, int) _override { return *this; } virtual MockExpectedCall& withUnsignedIntParameter(const SimpleString&, unsigned int) _override{ return *this; } diff --git a/include/CppUTestExt/MockExpectedCall.h b/include/CppUTestExt/MockExpectedCall.h index 73120e7f1..2bd11aa7e 100644 --- a/include/CppUTestExt/MockExpectedCall.h +++ b/include/CppUTestExt/MockExpectedCall.h @@ -39,7 +39,8 @@ class MockExpectedCall virtual ~MockExpectedCall(); virtual MockExpectedCall& withName(const SimpleString& name)=0; - virtual MockExpectedCall& withCallOrder(int)=0; + virtual MockExpectedCall& withCallOrder(unsigned int)=0; + virtual MockExpectedCall& withCallOrder(unsigned int, unsigned int)=0; MockExpectedCall& withParameter(const SimpleString& name, bool value) { return withBoolParameter(name, value); } MockExpectedCall& withParameter(const SimpleString& name, int value) { return withIntParameter(name, value); } MockExpectedCall& withParameter(const SimpleString& name, unsigned int value) { return withUnsignedIntParameter(name, value); } diff --git a/include/CppUTestExt/MockFailure.h b/include/CppUTestExt/MockFailure.h index b9c5f3c10..fe17cbba2 100644 --- a/include/CppUTestExt/MockFailure.h +++ b/include/CppUTestExt/MockFailure.h @@ -120,4 +120,11 @@ class MockExpectedObjectDidntHappenFailure : public MockFailure MockExpectedObjectDidntHappenFailure(UtestShell* test, const SimpleString& functionName, const MockExpectedCallsList& expectations); }; +class MockStrictOrderingIncompatibleWithOptionalCallsFailure : public MockFailure +{ +public: + MockStrictOrderingIncompatibleWithOptionalCallsFailure(UtestShell* test, const SimpleString& functionName, unsigned int minCalls, unsigned int maxCalls); + virtual ~MockStrictOrderingIncompatibleWithOptionalCallsFailure(){} +}; + #endif diff --git a/include/CppUTestExt/MockSupport.h b/include/CppUTestExt/MockSupport.h index c39856826..b181fc73b 100755 --- a/include/CppUTestExt/MockSupport.h +++ b/include/CppUTestExt/MockSupport.h @@ -130,8 +130,8 @@ class MockSupport void countCheck(); private: - int actualCallOrder_; - int expectedCallOrder_; + unsigned int actualCallOrder_; + unsigned int expectedCallOrder_; bool strictOrdering_; MockFailureReporter *activeReporter_; MockFailureReporter *standardReporter_; @@ -151,6 +151,7 @@ class MockSupport bool wasLastActualCallFulfilled(); void failTestWithExpectedCallsNotFulfilled(); void failTestWithOutOfOrderCalls(); + void failTestWithStrictOrderingIncompatibleWithOptionalCalls(); MockNamedValue* retrieveDataFromStore(const SimpleString& name); diff --git a/src/CppUTestExt/MockExpectedCall.cpp b/src/CppUTestExt/MockExpectedCall.cpp index d339daeb9..32764dd34 100644 --- a/src/CppUTestExt/MockExpectedCall.cpp +++ b/src/CppUTestExt/MockExpectedCall.cpp @@ -52,7 +52,8 @@ SimpleString MockCheckedExpectedCall::getName() const } MockCheckedExpectedCall::MockCheckedExpectedCall() - : ignoreOtherParameters_(false), isActualCallMatchFinalized_(false), actualCallOrder_(NOT_CALLED_YET), expectedCallOrder_(NO_EXPECTED_CALL_ORDER), + : ignoreOtherParameters_(false), isActualCallMatchFinalized_(false), + initialExpectedCallOrder_(NO_EXPECTED_CALL_ORDER), finalExpectedCallOrder_(NO_EXPECTED_CALL_ORDER), outOfOrder_(false), returnValue_(""), objectPtr_(NULL), wasPassedToObject_(true), actualCalls_(0), minCalls_(1), maxCalls_(1) { @@ -61,7 +62,8 @@ MockCheckedExpectedCall::MockCheckedExpectedCall() } MockCheckedExpectedCall::MockCheckedExpectedCall(unsigned int minCalls, unsigned int maxCalls) - : ignoreOtherParameters_(false), isActualCallMatchFinalized_(false), actualCallOrder_(NOT_CALLED_YET), expectedCallOrder_(NO_EXPECTED_CALL_ORDER), + : ignoreOtherParameters_(false), isActualCallMatchFinalized_(false), + initialExpectedCallOrder_(NO_EXPECTED_CALL_ORDER), finalExpectedCallOrder_(NO_EXPECTED_CALL_ORDER), outOfOrder_(false), returnValue_(""), objectPtr_(NULL), wasPassedToObject_(true), actualCalls_(0), minCalls_(minCalls), maxCalls_(maxCalls) { @@ -267,14 +269,11 @@ bool MockCheckedExpectedCall::isMatchingActualCall() void MockCheckedExpectedCall::callWasMade(int callOrder) { actualCalls_++; - actualCallOrder_ = callOrder; - if (expectedCallOrder_ == NO_EXPECTED_CALL_ORDER) - outOfOrder_ = false; - else if (actualCallOrder_ == expectedCallOrder_) - outOfOrder_ = false; - else + if ( (initialExpectedCallOrder_ != NO_EXPECTED_CALL_ORDER) && + ((callOrder < initialExpectedCallOrder_) || (callOrder > finalExpectedCallOrder_)) ) { outOfOrder_ = true; + } resetActualCallMatchingState(); } @@ -292,7 +291,6 @@ void MockCheckedExpectedCall::wasPassedToObject() void MockCheckedExpectedCall::resetActualCallMatchingState() { - actualCallOrder_ = NOT_CALLED_YET; wasPassedToObject_ = (objectPtr_ == NULL); isActualCallMatchFinalized_ = false; @@ -346,8 +344,12 @@ SimpleString MockCheckedExpectedCall::callToString() str += getName(); str += " -> "; - if (expectedCallOrder_ != NO_EXPECTED_CALL_ORDER) { - str += StringFromFormat("expected call order: <%d> -> ", expectedCallOrder_); + if (initialExpectedCallOrder_ != NO_EXPECTED_CALL_ORDER) { + if (initialExpectedCallOrder_ == finalExpectedCallOrder_) { + str += StringFromFormat("expected call order: <%u> -> ", initialExpectedCallOrder_); + } else { + str += StringFromFormat("expected calls order: <%u..%u> -> ", initialExpectedCallOrder_, finalExpectedCallOrder_); + } } if (inputParameters_->begin() == NULL && outputParameters_->begin() == NULL) { @@ -529,14 +531,10 @@ MockNamedValue MockCheckedExpectedCall::returnValue() return returnValue_; } -int MockCheckedExpectedCall::getCallOrder() const -{ - return actualCallOrder_; -} - -MockExpectedCall& MockCheckedExpectedCall::withCallOrder(int callOrder) +MockExpectedCall& MockCheckedExpectedCall::withCallOrder(unsigned int initialCallOrder, unsigned int finalCallOrder) { - expectedCallOrder_ = callOrder; + initialExpectedCallOrder_ = initialCallOrder; + finalExpectedCallOrder_ = finalCallOrder; return *this; } diff --git a/src/CppUTestExt/MockFailure.cpp b/src/CppUTestExt/MockFailure.cpp index a25eeff75..cd7828c12 100644 --- a/src/CppUTestExt/MockFailure.cpp +++ b/src/CppUTestExt/MockFailure.cpp @@ -238,4 +238,8 @@ MockExpectedObjectDidntHappenFailure::MockExpectedObjectDidntHappenFailure(Utest addExpectationsAndCallHistoryRelatedTo(functionName, expectations); } - +MockStrictOrderingIncompatibleWithOptionalCallsFailure::MockStrictOrderingIncompatibleWithOptionalCallsFailure(UtestShell* test, const SimpleString& functionName, unsigned int minCalls, unsigned int maxCalls) : MockFailure(test) +{ + message_ = StringFromFormat("MockFailure: Expected optional calls are not supported when strict ordering is enabled," + " but function \"%s\" expected calls range was %u..%u.", functionName.asCharString(), minCalls, maxCalls); +} diff --git a/src/CppUTestExt/MockSupport.cpp b/src/CppUTestExt/MockSupport.cpp index 768847fa8..51a4ba0da 100755 --- a/src/CppUTestExt/MockSupport.cpp +++ b/src/CppUTestExt/MockSupport.cpp @@ -201,8 +201,15 @@ MockExpectedCall& MockSupport::expectRangeOfCalls(unsigned int minCalls, unsigne MockCheckedExpectedCall* call = new MockCheckedExpectedCall(minCalls, maxCalls); call->withName(appendScopeToName(functionName)); - if (strictOrdering_) - call->withCallOrder(++expectedCallOrder_); + if (strictOrdering_) { + if (minCalls == maxCalls) { + call->withCallOrder(expectedCallOrder_ + 1, expectedCallOrder_ + minCalls); + expectedCallOrder_ += minCalls; + } else { + MockStrictOrderingIncompatibleWithOptionalCallsFailure failure(activeReporter_->getTestToFail(), functionName, minCalls, maxCalls); + failTest(failure); + } + } expectations_.addExpectedCall(call); return *call; } @@ -315,7 +322,6 @@ void MockSupport::failTestWithExpectedCallsNotFulfilled() expectationsList.addExpectations(getMockSupport(p)->expectations_); MockExpectedCallsNotFulfilledFailure failure(activeReporter_->getTestToFail(), expectationsList); - clear(); failTest(failure); } @@ -329,12 +335,12 @@ void MockSupport::failTestWithOutOfOrderCalls() expectationsList.addExpectations(getMockSupport(p)->expectations_); MockCallOrderFailure failure(activeReporter_->getTestToFail(), expectationsList); - clear(); failTest(failure); } void MockSupport::failTest(MockFailure& failure) { + clear(); activeReporter_->failTest(failure); } diff --git a/tests/CppUTestExt/MockExpectedCallTest.cpp b/tests/CppUTestExt/MockExpectedCallTest.cpp index 6e99fc0e3..20f407d79 100644 --- a/tests/CppUTestExt/MockExpectedCallTest.cpp +++ b/tests/CppUTestExt/MockExpectedCallTest.cpp @@ -566,7 +566,7 @@ TEST(MockExpectedCall, toStringForParameterAndIgnored) STRCMP_EQUAL("name -> const char* string: , other parameters are ignored (called 1 time)", expectedCall.callToString().asCharString()); } -TEST(MockExpectedCall, toStringForCallOrder) +TEST(MockExpectedCall, toStringForCallOrderSingle) { MockCheckedExpectedCall expectedCall(1, 1); expectedCall.withName("name"); @@ -575,7 +575,20 @@ TEST(MockExpectedCall, toStringForCallOrder) STRCMP_EQUAL("name -> expected call order: <2> -> no parameters (called 1 time)", expectedCall.callToString().asCharString()); } -TEST(MockExpectedCall, callOrderIsNotFulfilledWithWrongOrder) +TEST(MockExpectedCall, toStringForCallOrderMultiple) +{ + MockCheckedExpectedCall expectedCall(5, 5); + expectedCall.withName("name"); + expectedCall.withCallOrder(5, 9); + expectedCall.callWasMade(5); + expectedCall.callWasMade(6); + expectedCall.callWasMade(7); + expectedCall.callWasMade(8); + expectedCall.callWasMade(9); + STRCMP_EQUAL("name -> expected calls order: <5..9> -> no parameters (called 5 times)", expectedCall.callToString().asCharString()); +} + +TEST(MockExpectedCall, callOrderIsFulfilledButWithWrongOrderSingle) { call->withName("name"); call->withCallOrder(2); @@ -584,7 +597,31 @@ TEST(MockExpectedCall, callOrderIsNotFulfilledWithWrongOrder) CHECK(call->isOutOfOrder()); } -TEST(MockExpectedCall, callOrderIsFulfilled) +TEST(MockExpectedCall, callOrderIsFulfilledButWithWrongOrderMultipleTooEarly) +{ + MockCheckedExpectedCall expectedCall(3, 3); + expectedCall.withName("name"); + expectedCall.withCallOrder(10, 12); + expectedCall.callWasMade(9); + expectedCall.callWasMade(10); + expectedCall.callWasMade(11); + CHECK(expectedCall.isFulfilled()); + CHECK(expectedCall.isOutOfOrder()); +} + +TEST(MockExpectedCall, callOrderIsFulfilledButWithWrongOrderMultipleTooLate) +{ + MockCheckedExpectedCall expectedCall(3, 3); + expectedCall.withName("name"); + expectedCall.withCallOrder(10, 12); + expectedCall.callWasMade(11); + expectedCall.callWasMade(12); + expectedCall.callWasMade(13); + CHECK(expectedCall.isFulfilled()); + CHECK(expectedCall.isOutOfOrder()); +} + +TEST(MockExpectedCall, callOrderIsFulfilledSingle) { call->withName("name"); call->withCallOrder(1); @@ -593,6 +630,19 @@ TEST(MockExpectedCall, callOrderIsFulfilled) CHECK_FALSE(call->isOutOfOrder()); } +TEST(MockExpectedCall, callOrderIsFulfilledMultiple) +{ + MockCheckedExpectedCall expectedCall(4, 4); + expectedCall.withName("name"); + expectedCall.withCallOrder(150, 153); + expectedCall.callWasMade(150); + expectedCall.callWasMade(151); + expectedCall.callWasMade(152); + expectedCall.callWasMade(153); + CHECK(expectedCall.isFulfilled()); + CHECK_FALSE(expectedCall.isOutOfOrder()); +} + TEST(MockExpectedCall, hasOutputParameter) { const int value = 1; diff --git a/tests/CppUTestExt/MockStrictOrderTest.cpp b/tests/CppUTestExt/MockStrictOrderTest.cpp index 4c3d1f494..2a18ace01 100644 --- a/tests/CppUTestExt/MockStrictOrderTest.cpp +++ b/tests/CppUTestExt/MockStrictOrderTest.cpp @@ -178,18 +178,33 @@ TEST(MockStrictOrderTest, orderViolatedAcrossScopes) mock("mock2").checkExpectations(); } -IGNORE_TEST(MockStrictOrderTest, orderUsingNCalls) +TEST(MockStrictOrderTest, orderUsingNCalls) { mock().strictOrder(); mock().expectOneCall("foo1"); mock().expectNCalls(2, "foo2"); mock().expectOneCall("foo1"); + mock().expectNCalls(3, "foo2"); + mock().actualCall("foo1"); mock().actualCall("foo2"); mock().actualCall("foo2"); mock().actualCall("foo1"); + mock().actualCall("foo2"); + mock().actualCall("foo2"); + mock().actualCall("foo2"); mock().checkExpectations(); } +TEST(MockStrictOrderTest, orderUsingOptionalCalls) +{ + MockFailureReporterInstaller failureReporterInstaller; + + mock().strictOrder(); + mock().expectRangeOfCalls(0, 1, "foo"); + + MockStrictOrderingIncompatibleWithOptionalCallsFailure expectedFailure(mockFailureTest(), "foo", 0, 1); + CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); +} From 84593a91c276cb4000f9d1904159951909867e24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Tue, 28 Jun 2016 01:07:26 +0200 Subject: [PATCH 09/26] Fixed declaration of MockExpectedCallsListForTest::addFunction to avoid sign mismatch errors when compiling with sign strictness. --- tests/CppUTestExt/MockFailureReporterForTest.cpp | 4 +--- tests/CppUTestExt/MockFailureReporterForTest.h | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/CppUTestExt/MockFailureReporterForTest.cpp b/tests/CppUTestExt/MockFailureReporterForTest.cpp index 0974b5567..4953fd4e2 100644 --- a/tests/CppUTestExt/MockFailureReporterForTest.cpp +++ b/tests/CppUTestExt/MockFailureReporterForTest.cpp @@ -112,11 +112,9 @@ MockCheckedExpectedCall* MockExpectedCallsListForTest::addFunction(unsigned int return newCall; } -MockCheckedExpectedCall* MockExpectedCallsListForTest::addFunction(const SimpleString& name, int order) +MockCheckedExpectedCall* MockExpectedCallsListForTest::addFunction(const SimpleString& name, unsigned int order) { MockCheckedExpectedCall* newCall = addFunction(name); newCall->withCallOrder(order); return newCall; } - - diff --git a/tests/CppUTestExt/MockFailureReporterForTest.h b/tests/CppUTestExt/MockFailureReporterForTest.h index c5386c7c6..9783cf358 100644 --- a/tests/CppUTestExt/MockFailureReporterForTest.h +++ b/tests/CppUTestExt/MockFailureReporterForTest.h @@ -61,7 +61,7 @@ class MockExpectedCallsListForTest : public MockExpectedCallsList ~MockExpectedCallsListForTest(); MockCheckedExpectedCall* addFunction(const SimpleString& name); MockCheckedExpectedCall* addFunction(unsigned int minCalls, unsigned int maxCalls, const SimpleString& name); - MockCheckedExpectedCall* addFunction(const SimpleString& name, int order); + MockCheckedExpectedCall* addFunction(const SimpleString& name, unsigned int order); }; #endif From 49f3b7dc9692fd9537bbda1096a8671c6061e7b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Tue, 28 Jun 2016 01:51:35 +0200 Subject: [PATCH 10/26] Fixed declaration of call order related methods and attributes to use proper unsigned integers, and also removed unused method MockActualCall::withCallOrder (and its overridden counterparts), except in MockActualCallTrace, where it's been redefined and re-enabled. --- include/CppUTestExt/MockActualCall.h | 1 - include/CppUTestExt/MockCheckedActualCall.h | 9 ++++--- include/CppUTestExt/MockCheckedExpectedCall.h | 2 +- src/CppUTestExt/MockActualCall.cpp | 24 ++++++++----------- src/CppUTestExt/MockExpectedCall.cpp | 2 +- src/CppUTestExt/MockSupport.cpp | 2 +- tests/CppUTestExt/MockActualCallTest.cpp | 8 +++---- 7 files changed, 20 insertions(+), 28 deletions(-) diff --git a/include/CppUTestExt/MockActualCall.h b/include/CppUTestExt/MockActualCall.h index 496a499a0..badd7b5e1 100644 --- a/include/CppUTestExt/MockActualCall.h +++ b/include/CppUTestExt/MockActualCall.h @@ -42,7 +42,6 @@ class MockActualCall virtual ~MockActualCall(); virtual MockActualCall& withName(const SimpleString& name)=0; - virtual MockActualCall& withCallOrder(int callOrder)=0; MockActualCall& withParameter(const SimpleString& name, bool value) { return withBoolParameter(name, value); } MockActualCall& withParameter(const SimpleString& name, int value) { return withIntParameter(name, value); } MockActualCall& withParameter(const SimpleString& name, unsigned int value) { return withUnsignedIntParameter(name, value); } diff --git a/include/CppUTestExt/MockCheckedActualCall.h b/include/CppUTestExt/MockCheckedActualCall.h index 470c15631..d61422e0c 100644 --- a/include/CppUTestExt/MockCheckedActualCall.h +++ b/include/CppUTestExt/MockCheckedActualCall.h @@ -34,11 +34,10 @@ class MockCheckedActualCall : public MockActualCall { public: - MockCheckedActualCall(int callOrder, MockFailureReporter* reporter, const MockExpectedCallsList& expectations); + MockCheckedActualCall(unsigned int callOrder, MockFailureReporter* reporter, const MockExpectedCallsList& expectations); virtual ~MockCheckedActualCall(); virtual MockActualCall& withName(const SimpleString& name) _override; - virtual MockActualCall& withCallOrder(int) _override; virtual MockActualCall& withBoolParameter(const SimpleString& name, bool value) _override; virtual MockActualCall& withIntParameter(const SimpleString& name, int value) _override; virtual MockActualCall& withUnsignedIntParameter(const SimpleString& name, unsigned int value) _override; @@ -116,7 +115,7 @@ class MockCheckedActualCall : public MockActualCall private: SimpleString functionName_; - int callOrder_; + unsigned int callOrder_; MockFailureReporter* reporter_; ActualCallState state_; @@ -151,7 +150,6 @@ class MockActualCallTrace : public MockActualCall virtual ~MockActualCallTrace(); virtual MockActualCall& withName(const SimpleString& name) _override; - virtual MockActualCall& withCallOrder(int) _override; virtual MockActualCall& withBoolParameter(const SimpleString& name, bool value) _override; virtual MockActualCall& withIntParameter(const SimpleString& name, int value) _override; virtual MockActualCall& withUnsignedIntParameter(const SimpleString& name, unsigned int value) _override; @@ -202,6 +200,8 @@ class MockActualCallTrace : public MockActualCall virtual MockActualCall& onObject(const void* objectPtr) _override; + MockActualCall& withCallOrder(unsigned int callOrder); + const char* getTraceOutput(); void clear(); static MockActualCallTrace& instance(); @@ -216,7 +216,6 @@ class MockIgnoredActualCall: public MockActualCall { public: virtual MockActualCall& withName(const SimpleString&) _override { return *this;} - virtual MockActualCall& withCallOrder(int) _override { return *this; } virtual MockActualCall& withBoolParameter(const SimpleString&, bool) _override { return *this; } virtual MockActualCall& withIntParameter(const SimpleString&, int) _override { return *this; } virtual MockActualCall& withUnsignedIntParameter(const SimpleString&, unsigned int) _override { return *this; } diff --git a/include/CppUTestExt/MockCheckedExpectedCall.h b/include/CppUTestExt/MockCheckedExpectedCall.h index 82864897f..99e112a79 100644 --- a/include/CppUTestExt/MockCheckedExpectedCall.h +++ b/include/CppUTestExt/MockCheckedExpectedCall.h @@ -92,7 +92,7 @@ class MockCheckedExpectedCall : public MockExpectedCall virtual bool areParametersMatchingActualCall(); virtual bool isOutOfOrder() const; - virtual void callWasMade(int callOrder); + virtual void callWasMade(unsigned int callOrder); virtual void inputParameterWasPassed(const SimpleString& name); virtual void outputParameterWasPassed(const SimpleString& name); virtual void finalizeActualCallMatch(); diff --git a/src/CppUTestExt/MockActualCall.cpp b/src/CppUTestExt/MockActualCall.cpp index feb97faf0..d9cfdf8c0 100644 --- a/src/CppUTestExt/MockActualCall.cpp +++ b/src/CppUTestExt/MockActualCall.cpp @@ -49,7 +49,7 @@ SimpleString MockCheckedActualCall::getName() const return functionName_; } -MockCheckedActualCall::MockCheckedActualCall(int callOrder, MockFailureReporter* reporter, const MockExpectedCallsList& allExpectations) +MockCheckedActualCall::MockCheckedActualCall(unsigned int callOrder, MockFailureReporter* reporter, const MockExpectedCallsList& allExpectations) : callOrder_(callOrder), reporter_(reporter), state_(CALL_SUCCEED), expectationsChecked_(false), matchingExpectation_(NULL), allExpectations_(allExpectations), outputParameterExpectations_(NULL) { potentiallyMatchingExpectations_.addPotentiallyMatchingExpectations(allExpectations); @@ -151,11 +151,6 @@ MockActualCall& MockCheckedActualCall::withName(const SimpleString& name) return *this; } -MockActualCall& MockCheckedActualCall::withCallOrder(int) -{ - return *this; -} - void MockCheckedActualCall::checkInputParameter(const MockNamedValue& actualParameter) { if(hasFailed()) @@ -583,18 +578,11 @@ MockActualCallTrace::~MockActualCallTrace() MockActualCall& MockActualCallTrace::withName(const SimpleString& name) { - traceBuffer_ += "\nFunction name:"; + traceBuffer_ += " Function name:"; traceBuffer_ += name; return *this; } -MockActualCall& MockActualCallTrace::withCallOrder(int callOrder) -{ - traceBuffer_ += " withCallOrder:"; - traceBuffer_ += StringFrom(callOrder); - return *this; -} - void MockActualCallTrace::addParameterName(const SimpleString& name) { traceBuffer_ += " "; @@ -602,6 +590,14 @@ void MockActualCallTrace::addParameterName(const SimpleString& name) traceBuffer_ += ":"; } +MockActualCall& MockActualCallTrace::withCallOrder(unsigned int callOrder) +{ + traceBuffer_ += "\n"; + traceBuffer_ += StringFrom(callOrder); + traceBuffer_ += " >"; + return *this; +} + MockActualCall& MockActualCallTrace::withBoolParameter(const SimpleString& name, bool value) { addParameterName(name); diff --git a/src/CppUTestExt/MockExpectedCall.cpp b/src/CppUTestExt/MockExpectedCall.cpp index 32764dd34..31289b003 100644 --- a/src/CppUTestExt/MockExpectedCall.cpp +++ b/src/CppUTestExt/MockExpectedCall.cpp @@ -266,7 +266,7 @@ bool MockCheckedExpectedCall::isMatchingActualCall() return areParametersMatchingActualCall() && wasPassedToObject_; } -void MockCheckedExpectedCall::callWasMade(int callOrder) +void MockCheckedExpectedCall::callWasMade(unsigned int callOrder) { actualCalls_++; diff --git a/src/CppUTestExt/MockSupport.cpp b/src/CppUTestExt/MockSupport.cpp index 51a4ba0da..5a14576fe 100755 --- a/src/CppUTestExt/MockSupport.cpp +++ b/src/CppUTestExt/MockSupport.cpp @@ -241,7 +241,7 @@ MockActualCall& MockSupport::actualCall(const SimpleString& functionName) } if (!enabled_) return MockIgnoredActualCall::instance(); - if (tracing_) return MockActualCallTrace::instance().withName(scopeFuntionName); + if (tracing_) return MockActualCallTrace::instance().withCallOrder(++actualCallOrder_).withName(scopeFuntionName); if (hasntUnexpectationWithName(scopeFuntionName) && hasntExpectationWithName(scopeFuntionName)) { diff --git a/tests/CppUTestExt/MockActualCallTest.cpp b/tests/CppUTestExt/MockActualCallTest.cpp index b60c099a3..2ab33187d 100644 --- a/tests/CppUTestExt/MockActualCallTest.cpp +++ b/tests/CppUTestExt/MockActualCallTest.cpp @@ -83,7 +83,7 @@ TEST(MockCheckedActualCall, unExpectedCallWithAnOutputParameter) TEST(MockCheckedActualCall, actualCallWithNoReturnValueAndMeaninglessCallOrderForCoverage) { MockCheckedActualCall actualCall(1, reporter, *emptyList); - actualCall.withName("noreturn").withCallOrder(0).returnValue(); + actualCall.withName("noreturn").returnValue(); MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "noreturn", *list); CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); @@ -135,7 +135,6 @@ TEST(MockCheckedActualCall, MockIgnoredActualCallWorksAsItShould) { MockIgnoredActualCall actual; actual.withName("func"); - actual.withCallOrder(1); CHECK(false == actual.returnBoolValue()); CHECK(false == actual.returnBoolValueOrDefault(true)); @@ -168,8 +167,8 @@ TEST(MockCheckedActualCall, remainderOfMockActualCallTraceWorksAsItShould) const unsigned char mem_buffer[] = { 0xFE, 0x15 }; void (*function_value)() = (void (*)())0xDEAD; MockActualCallTrace actual; - actual.withName("func"); actual.withCallOrder(1); + actual.withName("func"); actual.onObject(&value); actual.withBoolParameter("bool", true); @@ -182,8 +181,7 @@ TEST(MockCheckedActualCall, remainderOfMockActualCallTraceWorksAsItShould) actual.withMemoryBufferParameter("mem_buffer", mem_buffer, sizeof(mem_buffer)); actual.withParameterOfType("int", "named_type", &const_value); - SimpleString expectedString("\nFunction name:func"); - expectedString += " withCallOrder:1"; + SimpleString expectedString("\n1 > Function name:func"); expectedString += " onObject:0x"; expectedString += HexStringFrom(&value); expectedString += " bool:true"; From 60828dcfb08376921b9f7761bc170b67b0239f47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Tue, 28 Jun 2016 20:31:54 +0200 Subject: [PATCH 11/26] Updated some unit tests and added a new one to improve code coverage. --- tests/CppUTestExt/MockActualCallTest.cpp | 19 ++++++++++++++++++- tests/CppUTestExt/MockExpectedCallTest.cpp | 1 + 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/tests/CppUTestExt/MockActualCallTest.cpp b/tests/CppUTestExt/MockActualCallTest.cpp index 2ab33187d..9474941b6 100644 --- a/tests/CppUTestExt/MockActualCallTest.cpp +++ b/tests/CppUTestExt/MockActualCallTest.cpp @@ -62,13 +62,15 @@ TEST(MockCheckedActualCall, unExpectedCall) CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); } -TEST(MockCheckedActualCall, unExpectedCallWithAParameter) +TEST(MockCheckedActualCall, unExpectedCallWithAnInputParameter) { MockCheckedActualCall actualCall(1, reporter, *emptyList); actualCall.withName("unexpected").withParameter("bar", 0); MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "unexpected", *list); CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); + + CHECK(actualCall.hasFailed()); // Checks that withParameter() doesn't "reset" call state } TEST(MockCheckedActualCall, unExpectedCallWithAnOutputParameter) @@ -78,6 +80,21 @@ TEST(MockCheckedActualCall, unExpectedCallWithAnOutputParameter) MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "unexpected", *list); CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); + + CHECK(actualCall.hasFailed()); // Checks that withOutputParameter() doesn't "reset" call state +} + +TEST(MockCheckedActualCall, unExpectedCallOnObject) +{ + int object; + + MockCheckedActualCall actualCall(1, reporter, *emptyList); + actualCall.withName("unexpected").onObject(&object); + + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "unexpected", *list); + CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); + + CHECK(actualCall.hasFailed()); // Checks that onObject() doesn't "reset" call state } TEST(MockCheckedActualCall, actualCallWithNoReturnValueAndMeaninglessCallOrderForCoverage) diff --git a/tests/CppUTestExt/MockExpectedCallTest.cpp b/tests/CppUTestExt/MockExpectedCallTest.cpp index 20f407d79..7980af16a 100644 --- a/tests/CppUTestExt/MockExpectedCallTest.cpp +++ b/tests/CppUTestExt/MockExpectedCallTest.cpp @@ -696,6 +696,7 @@ TEST(MockIgnoredExpectedCall, worksAsItShould) { ignored.withName("func"); ignored.withCallOrder(1); + ignored.withCallOrder(1, 1); ignored.onObject((void*) 0); ignored.withBoolParameter("umm", true); ignored.withIntParameter("bla", (int) 1); From a12e83b19250182091e1ea15ac9d70ae611da374 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Tue, 28 Jun 2016 22:29:07 +0200 Subject: [PATCH 12/26] Modified MockSupport::expectNoCall() method implementation to use a multi-matching expected call with zero expected calls, thus avoiding the usage of a dedicated list to keep track of "unexpectations" and therefore simplifying the architecture of the MockSupport class. Additionally, MockUnexpectedCallHappenedFailure now reports, when expectations exists for a function, the additional call number of the unexpected call according to previous actual calls to the same function (instead of the max number of calls expected for that function, which could be misleading when optional calls are used). --- include/CppUTestExt/MockCheckedExpectedCall.h | 2 +- include/CppUTestExt/MockExpectedCallsList.h | 2 +- include/CppUTestExt/MockSupport.h | 4 +-- src/CppUTestExt/MockExpectedCall.cpp | 4 +-- src/CppUTestExt/MockExpectedCallsList.cpp | 4 +-- src/CppUTestExt/MockFailure.cpp | 6 ++--- src/CppUTestExt/MockSupport.cpp | 27 ++++++++----------- .../CppUTestExt/ExpectedFunctionsListTest.cpp | 13 ++++++--- tests/CppUTestExt/MockCallTest.cpp | 5 ++++ tests/CppUTestExt/MockFailureTest.cpp | 4 +-- 10 files changed, 37 insertions(+), 34 deletions(-) diff --git a/include/CppUTestExt/MockCheckedExpectedCall.h b/include/CppUTestExt/MockCheckedExpectedCall.h index 99e112a79..3088cf7af 100644 --- a/include/CppUTestExt/MockCheckedExpectedCall.h +++ b/include/CppUTestExt/MockCheckedExpectedCall.h @@ -104,7 +104,7 @@ class MockCheckedExpectedCall : public MockExpectedCall enum { NO_EXPECTED_CALL_ORDER = 0 }; - virtual unsigned int getMaxCalls() const; + virtual unsigned int getActualCallsFulfilled() const; protected: void setName(const SimpleString& name); diff --git a/include/CppUTestExt/MockExpectedCallsList.h b/include/CppUTestExt/MockExpectedCallsList.h index 7154ad954..94723890d 100644 --- a/include/CppUTestExt/MockExpectedCallsList.h +++ b/include/CppUTestExt/MockExpectedCallsList.h @@ -40,7 +40,7 @@ class MockExpectedCallsList virtual void deleteAllExpectationsAndClearList(); virtual unsigned int size() const; - virtual unsigned int amountOfExpectationsFor(const SimpleString& name) const; + virtual unsigned int amountOfActualCallsFulfilledFor(const SimpleString& name) const; virtual unsigned int amountOfUnfulfilledExpectations() const; virtual bool hasUnfulfilledExpectations() const; virtual bool hasFinalizedMatchingExpectations() const; diff --git a/include/CppUTestExt/MockSupport.h b/include/CppUTestExt/MockSupport.h index b181fc73b..313a2b49e 100755 --- a/include/CppUTestExt/MockSupport.h +++ b/include/CppUTestExt/MockSupport.h @@ -137,7 +137,6 @@ class MockSupport MockFailureReporter *standardReporter_; MockFailureReporter defaultReporter_; MockExpectedCallsList expectations_; - MockExpectedCallsList unExpectations_; bool ignoreOtherCalls_; bool enabled_; MockCheckedActualCall *lastActualFunctionCall_; @@ -157,8 +156,7 @@ class MockSupport MockSupport* getMockSupport(MockNamedValueListNode* node); - bool hasntExpectationWithName(const SimpleString& functionName); - bool hasntUnexpectationWithName(const SimpleString& functionName); + bool callIsIgnored(const SimpleString& functionName); bool hasCallsOutOfOrder(); SimpleString appendScopeToName(const SimpleString& functionName); diff --git a/src/CppUTestExt/MockExpectedCall.cpp b/src/CppUTestExt/MockExpectedCall.cpp index 31289b003..c5d2b7416 100644 --- a/src/CppUTestExt/MockExpectedCall.cpp +++ b/src/CppUTestExt/MockExpectedCall.cpp @@ -543,9 +543,9 @@ bool MockCheckedExpectedCall::isOutOfOrder() const return outOfOrder_; } -unsigned int MockCheckedExpectedCall::getMaxCalls() const +unsigned int MockCheckedExpectedCall::getActualCallsFulfilled() const { - return maxCalls_; + return actualCalls_; } MockExpectedCall& MockIgnoredExpectedCall::instance() diff --git a/src/CppUTestExt/MockExpectedCallsList.cpp b/src/CppUTestExt/MockExpectedCallsList.cpp index 43c6297cb..43da1edcf 100644 --- a/src/CppUTestExt/MockExpectedCallsList.cpp +++ b/src/CppUTestExt/MockExpectedCallsList.cpp @@ -63,11 +63,11 @@ bool MockExpectedCallsList::isEmpty() const return size() == 0; } -unsigned int MockExpectedCallsList::amountOfExpectationsFor(const SimpleString& name) const +unsigned int MockExpectedCallsList::amountOfActualCallsFulfilledFor(const SimpleString& name) const { unsigned int count = 0; for (MockExpectedCallsListNode* p = head_; p; p = p->next_) - if (p->expectedCall_->relatesTo(name)) count += p->expectedCall_->getMaxCalls(); + if (p->expectedCall_->relatesTo(name)) count += p->expectedCall_->getActualCallsFulfilled(); return count; } diff --git a/src/CppUTestExt/MockFailure.cpp b/src/CppUTestExt/MockFailure.cpp index cd7828c12..9ac420fe2 100644 --- a/src/CppUTestExt/MockFailure.cpp +++ b/src/CppUTestExt/MockFailure.cpp @@ -103,9 +103,9 @@ MockExpectedCallsNotFulfilledFailure::MockExpectedCallsNotFulfilledFailure(Utest MockUnexpectedCallHappenedFailure::MockUnexpectedCallHappenedFailure(UtestShell* test, const SimpleString& name, const MockExpectedCallsList& expectations) : MockFailure(test) { - unsigned int amountOfExpectations = expectations.amountOfExpectationsFor(name); - if (amountOfExpectations) { - SimpleString ordinalNumber = StringFromOrdinalNumber(amountOfExpectations + 1); + if (expectations.hasExpectationWithName(name)) { + unsigned int actualCalls = expectations.amountOfActualCallsFulfilledFor(name); + SimpleString ordinalNumber = StringFromOrdinalNumber(actualCalls + 1); message_ = StringFromFormat("Mock Failure: Unexpected additional (%s) call to function: ", ordinalNumber.asCharString()); } else { message_ = "Mock Failure: Unexpected call to function: "; diff --git a/src/CppUTestExt/MockSupport.cpp b/src/CppUTestExt/MockSupport.cpp index 5a14576fe..a56f0d828 100755 --- a/src/CppUTestExt/MockSupport.cpp +++ b/src/CppUTestExt/MockSupport.cpp @@ -119,7 +119,6 @@ void MockSupport::clear() MockActualCallTrace::instance().clear(); expectations_.deleteAllExpectationsAndClearList(); - unExpectations_.deleteAllExpectationsAndClearList(); ignoreOtherCalls_ = false; enabled_ = true; actualCallOrder_ = 0; @@ -159,13 +158,14 @@ MockExpectedCall& MockSupport::expectNCalls(unsigned int amount, const SimpleStr void MockSupport::expectNoCall(const SimpleString& functionName) { - if (!enabled_) return; - - countCheck(); - - MockCheckedExpectedCall* call = new MockCheckedExpectedCall; - call->withName(appendScopeToName(functionName)); - unExpectations_.addExpectedCall(call); + expectRangeOfCalls(0, 0, functionName); +// if (!enabled_) return; +// +// countCheck(); +// +// MockCheckedExpectedCall* call = new MockCheckedExpectedCall; +// call->withName(appendScopeToName(functionName)); +// unExpectations_.addExpectedCall(call); } MockExpectedCall& MockSupport::expectAtLeastOneCall(const SimpleString& functionName) @@ -220,14 +220,9 @@ MockCheckedActualCall* MockSupport::createActualFunctionCall() return lastActualFunctionCall_; } -bool MockSupport::hasntExpectationWithName(const SimpleString& functionName) -{ - return !expectations_.hasExpectationWithName(functionName) && ignoreOtherCalls_; -} - -bool MockSupport::hasntUnexpectationWithName(const SimpleString& functionName) +bool MockSupport::callIsIgnored(const SimpleString& functionName) { - return !unExpectations_.hasExpectationWithName(functionName); + return ignoreOtherCalls_ && !expectations_.hasExpectationWithName(functionName); } MockActualCall& MockSupport::actualCall(const SimpleString& functionName) @@ -244,7 +239,7 @@ MockActualCall& MockSupport::actualCall(const SimpleString& functionName) if (tracing_) return MockActualCallTrace::instance().withCallOrder(++actualCallOrder_).withName(scopeFuntionName); - if (hasntUnexpectationWithName(scopeFuntionName) && hasntExpectationWithName(scopeFuntionName)) { + if (callIsIgnored(scopeFuntionName)) { return MockIgnoredActualCall::instance(); } diff --git a/tests/CppUTestExt/ExpectedFunctionsListTest.cpp b/tests/CppUTestExt/ExpectedFunctionsListTest.cpp index cf313d4bf..f0f37e651 100644 --- a/tests/CppUTestExt/ExpectedFunctionsListTest.cpp +++ b/tests/CppUTestExt/ExpectedFunctionsListTest.cpp @@ -216,20 +216,25 @@ TEST(MockExpectedCallsList, addPotentiallyMatchingExpectationsMultipleUnmatchedE LONGS_EQUAL(2, newList.size()); } -TEST(MockExpectedCallsList, amountOfExpectationsFor) +TEST(MockExpectedCallsList, amountOfActualCallsFulfilledFor) { call1->withName("foo"); call2->withName("bar"); + call1->callWasMade(1); + call2->callWasMade(2); list->addExpectedCall(call1); list->addExpectedCall(call2); - LONGS_EQUAL(1, list->amountOfExpectationsFor("bar")); + LONGS_EQUAL(1, list->amountOfActualCallsFulfilledFor("bar")); } -TEST(MockExpectedCallsList, amountOfExpectationsForHasNone) +TEST(MockExpectedCallsList, amountOfActualCallsFulfilledForHasNone) { call1->withName("foo"); + call2->withName("bar"); + call1->callWasMade(1); list->addExpectedCall(call1); - LONGS_EQUAL(0, list->amountOfExpectationsFor("bar")); + list->addExpectedCall(call2); + LONGS_EQUAL(0, list->amountOfActualCallsFulfilledFor("bar")); } TEST(MockExpectedCallsList, callToStringForUnfulfilledFunctions) diff --git a/tests/CppUTestExt/MockCallTest.cpp b/tests/CppUTestExt/MockCallTest.cpp index 2b6c227ec..2242e6616 100644 --- a/tests/CppUTestExt/MockCallTest.cpp +++ b/tests/CppUTestExt/MockCallTest.cpp @@ -198,6 +198,7 @@ TEST(MockCallTest, expectNoCallThatHappened) MockFailureReporterInstaller failureReporterInstaller; MockExpectedCallsListForTest expectations; + expectations.addFunction(0, 0, "lazy"); MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "lazy", expectations); mock().expectNoCall("lazy"); @@ -211,6 +212,7 @@ TEST(MockCallTest, expectNoCallDoesntInfluenceExpectOneCall) MockFailureReporterInstaller failureReporterInstaller; MockExpectedCallsListForTest expectations; + expectations.addFunction(0, 0, "lazy"); expectations.addFunction("influence", MockCheckedExpectedCall::NO_EXPECTED_CALL_ORDER)->callWasMade(1); MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "lazy", expectations); @@ -227,6 +229,7 @@ TEST(MockCallTest, expectNoCallOnlyFailureOnceWhenMultipleHappened) MockFailureReporterInstaller failureReporterInstaller; MockExpectedCallsListForTest expectations; + expectations.addFunction(0, 0, "lazy"); MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "lazy", expectations); mock().expectNoCall("lazy"); @@ -240,6 +243,7 @@ TEST(MockCallTest, ignoreOtherCallsExceptForTheUnExpectedOne) MockFailureReporterInstaller failureReporterInstaller; MockExpectedCallsListForTest expectations; + expectations.addFunction(0, 0, "lazy"); MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "lazy", expectations); mock().expectNoCall("lazy"); @@ -258,6 +262,7 @@ TEST(MockCallTest, expectNoCallInScopeThatHappened) MockFailureReporterInstaller failureReporterInstaller; MockExpectedCallsListForTest expectations; + expectations.addFunction(0, 0, "scope::lazy"); MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "scope::lazy", expectations); mock("scope").expectNoCall("lazy"); diff --git a/tests/CppUTestExt/MockFailureTest.cpp b/tests/CppUTestExt/MockFailureTest.cpp index d9f04dc8d..459615e4c 100644 --- a/tests/CppUTestExt/MockFailureTest.cpp +++ b/tests/CppUTestExt/MockFailureTest.cpp @@ -65,12 +65,12 @@ TEST_GROUP(MockFailureTest) void checkUnexpectedNthCallMessage(unsigned int count, const char* expectedOrdinal) { MockExpectedCallsList callList; - MockCheckedExpectedCall expCall; + MockCheckedExpectedCall expCall(count-1, count-1); + callList.addExpectedCall(&expCall); expCall.withName("bar"); for (unsigned int i = 0; i < (count - 1); i++) { expCall.callWasMade(1); - callList.addExpectedCall(&expCall); } MockUnexpectedCallHappenedFailure failure(UtestShell::getCurrent(), "bar", callList); From 6b5db9522a22d63e899c9cc9cf375d76df25d7ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Wed, 29 Jun 2016 01:11:56 +0200 Subject: [PATCH 13/26] Removed superfluous constructor and method in MockExpectedCall that where not being used in production code. --- include/CppUTestExt/MockCheckedExpectedCall.h | 3 --- include/CppUTestExt/MockExpectedCall.h | 1 - src/CppUTestExt/MockExpectedCall.cpp | 10 ---------- tests/CppUTestExt/ExpectedFunctionsListTest.cpp | 16 ++++++++-------- tests/CppUTestExt/MockActualCallTest.cpp | 6 +++--- tests/CppUTestExt/MockExpectedCallTest.cpp | 9 ++++----- tests/CppUTestExt/MockFailureReporterForTest.cpp | 4 ++-- tests/CppUTestExt/MockFailureTest.cpp | 6 +++--- tests/CppUTestExt/MockStrictOrderTest.cpp | 2 +- 9 files changed, 21 insertions(+), 36 deletions(-) diff --git a/include/CppUTestExt/MockCheckedExpectedCall.h b/include/CppUTestExt/MockCheckedExpectedCall.h index 3088cf7af..d7cb1fc49 100644 --- a/include/CppUTestExt/MockCheckedExpectedCall.h +++ b/include/CppUTestExt/MockCheckedExpectedCall.h @@ -35,12 +35,10 @@ class MockCheckedExpectedCall : public MockExpectedCall { public: - MockCheckedExpectedCall(); MockCheckedExpectedCall(unsigned int minCalls, unsigned int maxCalls); virtual ~MockCheckedExpectedCall(); virtual MockExpectedCall& withName(const SimpleString& name) _override; - virtual MockExpectedCall& withCallOrder(unsigned int callOrder) _override { return withCallOrder(callOrder, callOrder); } virtual MockExpectedCall& withCallOrder(unsigned int initialCallOrder, unsigned int finalCallOrder) _override; virtual MockExpectedCall& withBoolParameter(const SimpleString& name, bool value) _override; virtual MockExpectedCall& withIntParameter(const SimpleString& name, int value) _override; @@ -146,7 +144,6 @@ class MockIgnoredExpectedCall: public MockExpectedCall public: virtual MockExpectedCall& withName(const SimpleString&) _override { return *this;} - virtual MockExpectedCall& withCallOrder(unsigned int) _override { return *this; } virtual MockExpectedCall& withCallOrder(unsigned int, unsigned int) _override { return *this; } virtual MockExpectedCall& withBoolParameter(const SimpleString&, bool) _override { return *this; } virtual MockExpectedCall& withIntParameter(const SimpleString&, int) _override { return *this; } diff --git a/include/CppUTestExt/MockExpectedCall.h b/include/CppUTestExt/MockExpectedCall.h index 2bd11aa7e..5efd6ed8b 100644 --- a/include/CppUTestExt/MockExpectedCall.h +++ b/include/CppUTestExt/MockExpectedCall.h @@ -39,7 +39,6 @@ class MockExpectedCall virtual ~MockExpectedCall(); virtual MockExpectedCall& withName(const SimpleString& name)=0; - virtual MockExpectedCall& withCallOrder(unsigned int)=0; virtual MockExpectedCall& withCallOrder(unsigned int, unsigned int)=0; MockExpectedCall& withParameter(const SimpleString& name, bool value) { return withBoolParameter(name, value); } MockExpectedCall& withParameter(const SimpleString& name, int value) { return withIntParameter(name, value); } diff --git a/src/CppUTestExt/MockExpectedCall.cpp b/src/CppUTestExt/MockExpectedCall.cpp index c5d2b7416..25ac2f075 100644 --- a/src/CppUTestExt/MockExpectedCall.cpp +++ b/src/CppUTestExt/MockExpectedCall.cpp @@ -51,16 +51,6 @@ SimpleString MockCheckedExpectedCall::getName() const return functionName_; } -MockCheckedExpectedCall::MockCheckedExpectedCall() - : ignoreOtherParameters_(false), isActualCallMatchFinalized_(false), - initialExpectedCallOrder_(NO_EXPECTED_CALL_ORDER), finalExpectedCallOrder_(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), initialExpectedCallOrder_(NO_EXPECTED_CALL_ORDER), finalExpectedCallOrder_(NO_EXPECTED_CALL_ORDER), diff --git a/tests/CppUTestExt/ExpectedFunctionsListTest.cpp b/tests/CppUTestExt/ExpectedFunctionsListTest.cpp index f0f37e651..12846ee91 100644 --- a/tests/CppUTestExt/ExpectedFunctionsListTest.cpp +++ b/tests/CppUTestExt/ExpectedFunctionsListTest.cpp @@ -41,10 +41,10 @@ TEST_GROUP(MockExpectedCallsList) void setup() { list = new MockExpectedCallsList; - call1 = new MockCheckedExpectedCall; - call2 = new MockCheckedExpectedCall; - call3 = new MockCheckedExpectedCall; - call4 = new MockCheckedExpectedCall; + call1 = new MockCheckedExpectedCall(1, 1); + call2 = new MockCheckedExpectedCall(1, 1); + call3 = new MockCheckedExpectedCall(1, 1); + call4 = new MockCheckedExpectedCall(1, 1); call1->withName("foo"); call2->withName("bar"); call3->withName("boo"); @@ -85,8 +85,8 @@ TEST(MockExpectedCallsList, listWithFulfilledExpectationHasNoUnfulfilledOnes) TEST(MockExpectedCallsList, listWithFulfilledExpectationButOutOfOrder) { - call1->withCallOrder(1); - call2->withCallOrder(2); + call1->withCallOrder(1, 1); + call2->withCallOrder(2, 2); list->addExpectedCall(call1); list->addExpectedCall(call2); call2->callWasMade(1); @@ -107,8 +107,8 @@ TEST(MockExpectedCallsList, listWithUnFulfilledExpectationHasNoUnfillfilledOnes) TEST(MockExpectedCallsList, deleteAllExpectationsAndClearList) { - list->addExpectedCall(new MockCheckedExpectedCall); - list->addExpectedCall(new MockCheckedExpectedCall); + list->addExpectedCall(new MockCheckedExpectedCall(1, 1)); + list->addExpectedCall(new MockCheckedExpectedCall(1, 1)); list->deleteAllExpectationsAndClearList(); } diff --git a/tests/CppUTestExt/MockActualCallTest.cpp b/tests/CppUTestExt/MockActualCallTest.cpp index 9474941b6..bf15bcedb 100644 --- a/tests/CppUTestExt/MockActualCallTest.cpp +++ b/tests/CppUTestExt/MockActualCallTest.cpp @@ -108,7 +108,7 @@ TEST(MockCheckedActualCall, actualCallWithNoReturnValueAndMeaninglessCallOrderFo TEST(MockCheckedActualCall, unExpectedParameterName) { - MockCheckedExpectedCall call1; + MockCheckedExpectedCall call1(1, 1); call1.withName("func"); list->addExpectedCall(&call1); @@ -124,8 +124,8 @@ TEST(MockCheckedActualCall, unExpectedParameterName) TEST(MockCheckedActualCall, multipleSameFunctionsExpectingAndHappenGradually) { - MockCheckedExpectedCall* call1 = new MockCheckedExpectedCall(); - MockCheckedExpectedCall* call2 = new MockCheckedExpectedCall(); + MockCheckedExpectedCall* call1 = new MockCheckedExpectedCall(1, 1); + MockCheckedExpectedCall* call2 = new MockCheckedExpectedCall(1, 1); call1->withName("func"); call2->withName("func"); list->addExpectedCall(call1); diff --git a/tests/CppUTestExt/MockExpectedCallTest.cpp b/tests/CppUTestExt/MockExpectedCallTest.cpp index 7980af16a..cb137dc42 100644 --- a/tests/CppUTestExt/MockExpectedCallTest.cpp +++ b/tests/CppUTestExt/MockExpectedCallTest.cpp @@ -155,7 +155,7 @@ TEST_GROUP(MockExpectedCall) MockCheckedExpectedCall* call; void setup() { - call = new MockCheckedExpectedCall; + call = new MockCheckedExpectedCall(1, 1); } void teardown() { @@ -570,7 +570,7 @@ TEST(MockExpectedCall, toStringForCallOrderSingle) { MockCheckedExpectedCall expectedCall(1, 1); expectedCall.withName("name"); - expectedCall.withCallOrder(2); + expectedCall.withCallOrder(2, 2); expectedCall.callWasMade(1); STRCMP_EQUAL("name -> expected call order: <2> -> no parameters (called 1 time)", expectedCall.callToString().asCharString()); } @@ -591,7 +591,7 @@ TEST(MockExpectedCall, toStringForCallOrderMultiple) TEST(MockExpectedCall, callOrderIsFulfilledButWithWrongOrderSingle) { call->withName("name"); - call->withCallOrder(2); + call->withCallOrder(2, 2); call->callWasMade(1); CHECK(call->isFulfilled()); CHECK(call->isOutOfOrder()); @@ -624,7 +624,7 @@ TEST(MockExpectedCall, callOrderIsFulfilledButWithWrongOrderMultipleTooLate) TEST(MockExpectedCall, callOrderIsFulfilledSingle) { call->withName("name"); - call->withCallOrder(1); + call->withCallOrder(1, 1); call->callWasMade(1); CHECK(call->isFulfilled()); CHECK_FALSE(call->isOutOfOrder()); @@ -695,7 +695,6 @@ TEST_GROUP(MockIgnoredExpectedCall) TEST(MockIgnoredExpectedCall, worksAsItShould) { ignored.withName("func"); - ignored.withCallOrder(1); ignored.withCallOrder(1, 1); ignored.onObject((void*) 0); ignored.withBoolParameter("umm", true); diff --git a/tests/CppUTestExt/MockFailureReporterForTest.cpp b/tests/CppUTestExt/MockFailureReporterForTest.cpp index 4953fd4e2..1acab2671 100644 --- a/tests/CppUTestExt/MockFailureReporterForTest.cpp +++ b/tests/CppUTestExt/MockFailureReporterForTest.cpp @@ -98,7 +98,7 @@ MockExpectedCallsListForTest::~MockExpectedCallsListForTest() MockCheckedExpectedCall* MockExpectedCallsListForTest::addFunction(const SimpleString& name) { - MockCheckedExpectedCall* newCall = new MockCheckedExpectedCall; + MockCheckedExpectedCall* newCall = new MockCheckedExpectedCall(1, 1); newCall->withName(name); addExpectedCall(newCall); return newCall; @@ -115,6 +115,6 @@ MockCheckedExpectedCall* MockExpectedCallsListForTest::addFunction(unsigned int MockCheckedExpectedCall* MockExpectedCallsListForTest::addFunction(const SimpleString& name, unsigned int order) { MockCheckedExpectedCall* newCall = addFunction(name); - newCall->withCallOrder(order); + newCall->withCallOrder(order, order); return newCall; } diff --git a/tests/CppUTestExt/MockFailureTest.cpp b/tests/CppUTestExt/MockFailureTest.cpp index 459615e4c..1921dc129 100644 --- a/tests/CppUTestExt/MockFailureTest.cpp +++ b/tests/CppUTestExt/MockFailureTest.cpp @@ -43,9 +43,9 @@ TEST_GROUP(MockFailureTest) void setup () { list = new MockExpectedCallsList; - call1 = new MockCheckedExpectedCall; - call2 = new MockCheckedExpectedCall; - call3 = new MockCheckedExpectedCall; + call1 = new MockCheckedExpectedCall(1, 1); + call2 = new MockCheckedExpectedCall(1, 1); + call3 = new MockCheckedExpectedCall(1, 1); } void teardown () { diff --git a/tests/CppUTestExt/MockStrictOrderTest.cpp b/tests/CppUTestExt/MockStrictOrderTest.cpp index 2a18ace01..181c02ff0 100644 --- a/tests/CppUTestExt/MockStrictOrderTest.cpp +++ b/tests/CppUTestExt/MockStrictOrderTest.cpp @@ -50,7 +50,7 @@ TEST(MockStrictOrderTest, OrderObserved) TEST(MockStrictOrderTest, someOrderObserved) { - mock().expectOneCall("foo3").withCallOrder(3); + mock().expectOneCall("foo3").withCallOrder(3, 3); mock().expectOneCall("foo1"); mock().expectOneCall("foo2"); mock().actualCall("foo2"); From 412ca5e5024fd7787a729820aa531da2419f2dc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Thu, 30 Jun 2016 21:43:25 +0200 Subject: [PATCH 14/26] Fixed the mock scope not being properly reported when failing because of using optional calls with strict ordering. --- src/CppUTestExt/MockSupport.cpp | 13 +++++++------ tests/CppUTestExt/MockStrictOrderTest.cpp | 11 +++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/CppUTestExt/MockSupport.cpp b/src/CppUTestExt/MockSupport.cpp index a56f0d828..6252b05d4 100755 --- a/src/CppUTestExt/MockSupport.cpp +++ b/src/CppUTestExt/MockSupport.cpp @@ -199,14 +199,15 @@ MockExpectedCall& MockSupport::expectRangeOfCalls(unsigned int minCalls, unsigne countCheck(); + const SimpleString scopeFunctionName = appendScopeToName(functionName); MockCheckedExpectedCall* call = new MockCheckedExpectedCall(minCalls, maxCalls); - call->withName(appendScopeToName(functionName)); + call->withName(scopeFunctionName); if (strictOrdering_) { if (minCalls == maxCalls) { call->withCallOrder(expectedCallOrder_ + 1, expectedCallOrder_ + minCalls); expectedCallOrder_ += minCalls; } else { - MockStrictOrderingIncompatibleWithOptionalCallsFailure failure(activeReporter_->getTestToFail(), functionName, minCalls, maxCalls); + MockStrictOrderingIncompatibleWithOptionalCallsFailure failure(activeReporter_->getTestToFail(), scopeFunctionName, minCalls, maxCalls); failTest(failure); } } @@ -227,7 +228,7 @@ bool MockSupport::callIsIgnored(const SimpleString& functionName) MockActualCall& MockSupport::actualCall(const SimpleString& functionName) { - const SimpleString scopeFuntionName = appendScopeToName(functionName); + const SimpleString scopeFunctionName = appendScopeToName(functionName); if (lastActualFunctionCall_) { lastActualFunctionCall_->checkExpectations(); @@ -236,15 +237,15 @@ MockActualCall& MockSupport::actualCall(const SimpleString& functionName) } if (!enabled_) return MockIgnoredActualCall::instance(); - if (tracing_) return MockActualCallTrace::instance().withCallOrder(++actualCallOrder_).withName(scopeFuntionName); + if (tracing_) return MockActualCallTrace::instance().withCallOrder(++actualCallOrder_).withName(scopeFunctionName); - if (callIsIgnored(scopeFuntionName)) { + if (callIsIgnored(scopeFunctionName)) { return MockIgnoredActualCall::instance(); } MockCheckedActualCall* call = createActualFunctionCall(); - call->withName(scopeFuntionName); + call->withName(scopeFunctionName); return *call; } diff --git a/tests/CppUTestExt/MockStrictOrderTest.cpp b/tests/CppUTestExt/MockStrictOrderTest.cpp index 181c02ff0..8c05f7856 100644 --- a/tests/CppUTestExt/MockStrictOrderTest.cpp +++ b/tests/CppUTestExt/MockStrictOrderTest.cpp @@ -208,3 +208,14 @@ TEST(MockStrictOrderTest, orderUsingOptionalCalls) MockStrictOrderingIncompatibleWithOptionalCallsFailure expectedFailure(mockFailureTest(), "foo", 0, 1); CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); } + +TEST(MockStrictOrderTest, orderUsingOptionalCallsOnScope) +{ + MockFailureReporterInstaller failureReporterInstaller; + + mock("bar").strictOrder(); + mock("bar").expectRangeOfCalls(5, 11, "foo"); + + MockStrictOrderingIncompatibleWithOptionalCallsFailure expectedFailure(mockFailureTest(), "bar::foo", 5, 11); + CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); +} From a80efda249c7a54e55d649b702adde2701045cc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Sun, 3 Jul 2016 19:05:54 +0200 Subject: [PATCH 15/26] Added a class to store a queue of actual calls, needed to add a log of actual calls. The implementation has been chosen as a queue instead of a simple list to allow limiting the number of registered calls to the last ones, which is convenient when performing tests that involve a large number of calls in a target platform with limited memory resources. --- CppUTest.dsp | 8 + CppUTest.vcproj | 26 ++ CppUTest.vcxproj | 2 + Makefile.am | 3 + include/CppUTestExt/MockActualCallsQueue.h | 152 +++++++++++ src/CppUTestExt/CMakeLists.txt | 2 + src/CppUTestExt/MockActualCallsQueue.cpp | 128 +++++++++ tests/AllTests.dsp | 4 + tests/AllTests.vcproj | 23 ++ tests/AllTests.vcxproj | 1 + tests/CppUTestExt/ActualCallsQueueTest.cpp | 297 +++++++++++++++++++++ tests/CppUTestExt/CMakeLists.txt | 1 + 12 files changed, 647 insertions(+) create mode 100644 include/CppUTestExt/MockActualCallsQueue.h create mode 100644 src/CppUTestExt/MockActualCallsQueue.cpp create mode 100644 tests/CppUTestExt/ActualCallsQueueTest.cpp diff --git a/CppUTest.dsp b/CppUTest.dsp index f13f8216e..b20538dd0 100644 --- a/CppUTest.dsp +++ b/CppUTest.dsp @@ -106,6 +106,10 @@ SOURCE=.\src\CppUTestExt\MockActualCall.cpp" # End Source File # Begin Source File +SOURCE=.\src\CppUTestExt\MockActualCallsQueue.cpp" +# End Source File +# Begin Source File + SOURCE=.\src\CppUTestExt\MockExpectedCall.cpp" # End Source File # Begin Source File @@ -242,6 +246,10 @@ SOURCE=.\include\CppUTestExt\MockCheckedActualCall.h # End Source File # Begin Source File +SOURCE=.\include\CppUTestExt\MockActualCallsQueue.h +# End Source File +# Begin Source File + SOURCE=.\include\CppUTestExt\MockCheckedExpectedCall.h # End Source File # Begin Source File diff --git a/CppUTest.vcproj b/CppUTest.vcproj index c0edc3813..b2202f31d 100644 --- a/CppUTest.vcproj +++ b/CppUTest.vcproj @@ -384,6 +384,28 @@ /> + + + + + + + + @@ -927,6 +949,10 @@ RelativePath=".\include\CppUTestExt\MockActualCall.h" > + + diff --git a/CppUTest.vcxproj b/CppUTest.vcxproj index 16e718e85..54e8a2db2 100644 --- a/CppUTest.vcxproj +++ b/CppUTest.vcxproj @@ -117,6 +117,7 @@ + @@ -159,6 +160,7 @@ + diff --git a/Makefile.am b/Makefile.am index 30803d2ea..666a2ca5a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -95,6 +95,7 @@ lib_libCppUTestExt_a_SOURCES = \ src/CppUTestExt/MemoryReporterPlugin.cpp \ src/CppUTestExt/MemoryReportFormatter.cpp \ src/CppUTestExt/MockActualCall.cpp \ + src/CppUTestExt/MockActualCallsQueue.cpp \ src/CppUTestExt/MockExpectedCall.cpp \ src/CppUTestExt/MockExpectedCallsList.cpp \ src/CppUTestExt/MockFailure.cpp \ @@ -118,6 +119,7 @@ include_cpputestext_HEADERS = \ include/CppUTestExt/MockActualCall.h \ include/CppUTestExt/MockCheckedActualCall.h \ include/CppUTestExt/MockCheckedExpectedCall.h \ + include/CppUTestExt/MockActualCallsQueue.h \ include/CppUTestExt/MockExpectedCall.h \ include/CppUTestExt/MockExpectedCallsList.h \ include/CppUTestExt/MockFailure.h \ @@ -192,6 +194,7 @@ CppUTestExtTests_SOURCES = \ tests/CppUTestExt/MockComparatorCopierTest.cpp \ tests/CppUTestExt/MockExpectedCallTest.cpp \ tests/CppUTestExt/ExpectedFunctionsListTest.cpp \ + tests/CppUTestExt/ActualCallsQueueTest.cpp \ tests/CppUTestExt/MockFailureReporterForTest.cpp \ tests/CppUTestExt/MockFailureTest.cpp \ tests/CppUTestExt/MockHierarchyTest.cpp \ diff --git a/include/CppUTestExt/MockActualCallsQueue.h b/include/CppUTestExt/MockActualCallsQueue.h new file mode 100644 index 000000000..ce5a4389c --- /dev/null +++ b/include/CppUTestExt/MockActualCallsQueue.h @@ -0,0 +1,152 @@ +/* + * Copyright (c) 2016, Jesus Gonzalez + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef D_MockActualCallsQueue_h +#define D_MockActualCallsQueue_h + +class MockCheckedActualCall; +class SimpleString; + +/** + * This class implements a FIFO queue of MockCheckedActualCall calls. + * + * @note The queue can be created as owner of its stored calls or not. If the queue is the owner, then the ownership of the calls added + * to the queue is transferred to the queue, and therefore it's responsible for managing their life cycle (i.e. deleting them). + * + * @note The queue has a size limit. When the queue reaches its maximum size, adding new calls on the back will remove calls from the front. + */ +class MockActualCallsQueue +{ + +public: + /** + * Constructor. + * + * Creates an empty queue. + * + * @param owner [in] Indicates if the ownership of the calls added to the queue shall be transferred to the queue + * @param maxSize [in] Maximum size for the queue + */ + MockActualCallsQueue(bool owner, unsigned int maxSize = (unsigned int) -1); + + /** + * Destructor. + * + * Discards all items, and if the queue is the owner of the calls, then deletes their associated calls. + */ + virtual ~MockActualCallsQueue(); + + /** + * Returns the number of calls stored in the queue. + * + * @return Size of the queue + */ + virtual unsigned int size() const; + + /** + * Checks if the queue is empty. + * + * @return @c true if the queue is empty, @c false otherwise + */ + virtual bool isEmpty() const; + + /** + * Clears the queue, discarding all items, and if the queue is the owner of its calls, then also deleting their associated calls. + */ + virtual void clear(); + + /** + * Appends the call to the back of the queue. + * + * @note If the queue is the owner of its calls, then it becomes the owner of the passed object. + * + * @note If the queue is at its size limit, then the first item is discarded, and if the queue is the owner of its calls, then the + * associated call is also deleted. + * + * @param call [in] Pointer to the call to be appended + */ + virtual void pushBack(MockCheckedActualCall* call); + + /** + * Removes a call from the front of the queue, and returns it. + * + * @note If the queue is the owner of its calls, then the caller becomes the owner of the returned object. + * + * @return Pointer to the call that was in front of the queue, or NULL if the queue was empty + */ + virtual MockCheckedActualCall* popFront(); + + /** + * Appends all the calls from @c otherQueue to the back of this queue. + * + * @note If this queue is the owner of calls, then it becomes the owner all the calls from @c otherQueue. This means that either + * this queue shall not be owner of calls, or that it's owner of calls and the calls from @c otherQueue do not have an owner + * yet (i.e. @c otherQueue is not owner of calls). + * + * @param otherQueue [in] Another queue + */ + virtual void pushBackAll(const MockActualCallsQueue& otherQueue); + + /** + * Gets the maximum size for the queue. + * @return Maximum size + */ + virtual unsigned int getMaxSize(); + + /** + * Sets the maximum size for the queue. + * + * @note If the current size is greater than @c maxSize, then calls are removed in order from the front of the queue until the + * current size is equal to the maximum size. + * + * @param maxSize [in] Maximum size + */ + virtual void setMaxSize(unsigned int maxSize); + +protected: + class MockActualCallsQueueNode + { + public: + MockCheckedActualCall* call_; + + MockActualCallsQueueNode* next_; + MockActualCallsQueueNode(MockCheckedActualCall* call) : call_(call), next_(NULL) {} + }; + +private: + MockActualCallsQueueNode* head_; + MockActualCallsQueueNode* tail_; + + unsigned int currentSize_; + unsigned int maxSize_; + + bool owner_; + + virtual void deleteFromFront(); +}; + +#endif diff --git a/src/CppUTestExt/CMakeLists.txt b/src/CppUTestExt/CMakeLists.txt index 749be7a9e..c29b59bb0 100644 --- a/src/CppUTestExt/CMakeLists.txt +++ b/src/CppUTestExt/CMakeLists.txt @@ -12,6 +12,7 @@ set(CppUTestExt_src OrderedTest.cpp MemoryReportFormatter.cpp MockExpectedCallsList.cpp + MockActualCallsQueue.cpp MockSupport.cpp ) @@ -34,6 +35,7 @@ set(CppUTestExt_headers ${CppUTestRootDirectory}/include/CppUTestExt/GTestConvertor.h ${CppUTestRootDirectory}/include/CppUTestExt/MockActualCall.h ${CppUTestRootDirectory}/include/CppUTestExt/MockCheckedActualCall.h + ${CppUTestRootDirectory}/include/CppUTestExt/MockActualCallsQueue.h ${CppUTestRootDirectory}/include/CppUTestExt/MockNamedValue.h ${CppUTestRootDirectory}/include/CppUTestExt/MockSupport.h ) diff --git a/src/CppUTestExt/MockActualCallsQueue.cpp b/src/CppUTestExt/MockActualCallsQueue.cpp new file mode 100644 index 000000000..34045b70d --- /dev/null +++ b/src/CppUTestExt/MockActualCallsQueue.cpp @@ -0,0 +1,128 @@ +/* + * Copyright (c) 2016, Jesus Gonzalez + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "CppUTest/TestHarness.h" +#include "CppUTestExt/MockActualCallsQueue.h" +#include "CppUTestExt/MockCheckedActualCall.h" +#include "CppUTest/SimpleString.h" + +MockActualCallsQueue::MockActualCallsQueue(bool owner, unsigned int maxSize) +: head_(NULL), tail_(NULL), currentSize_(0), maxSize_(maxSize), owner_(owner) +{ +} + +MockActualCallsQueue::~MockActualCallsQueue() +{ + clear(); +} + +unsigned int MockActualCallsQueue::size() const +{ + return currentSize_; +} + +bool MockActualCallsQueue::isEmpty() const +{ + return currentSize_ == 0; +} + +void MockActualCallsQueue::pushBack(MockCheckedActualCall* call) +{ + MockActualCallsQueueNode* newCall = new MockActualCallsQueueNode(call); + + if (currentSize_ >= maxSize_) { + deleteFromFront(); + } + + if (tail_ == NULL) { + head_ = newCall; + } else { + tail_->next_ = newCall; + } + tail_ = newCall; + currentSize_++; +} + +MockCheckedActualCall* MockActualCallsQueue::popFront() +{ + MockCheckedActualCall* actualCall = NULL; + + if (head_) { + MockActualCallsQueueNode* next = head_->next_; + actualCall = head_->call_; + delete head_; + currentSize_--; + head_ = next; + if (!head_) { + tail_ = NULL; + } + } + + return actualCall; +} + +void MockActualCallsQueue::clear() +{ + while (head_) { + MockActualCallsQueueNode* next = head_->next_; + if (owner_) { + delete head_->call_; + } + delete head_; + head_ = next; + } + tail_ = NULL; + currentSize_ = 0; +} + +void MockActualCallsQueue::pushBackAll(const MockActualCallsQueue& otherQueue) +{ + for (MockActualCallsQueueNode* p = otherQueue.head_; p; p = p->next_) { + pushBack(p->call_); + } +} + +unsigned int MockActualCallsQueue::getMaxSize() +{ + return maxSize_; +} + +void MockActualCallsQueue::setMaxSize(unsigned int maxSize) +{ + maxSize_ = maxSize; + while(currentSize_ > maxSize_) { + deleteFromFront(); + } +} + +void MockActualCallsQueue::deleteFromFront() +{ + MockCheckedActualCall* frontCall = popFront(); + if (owner_) { + delete frontCall; + } +} diff --git a/tests/AllTests.dsp b/tests/AllTests.dsp index da3578ac2..da793ce14 100644 --- a/tests/AllTests.dsp +++ b/tests/AllTests.dsp @@ -134,6 +134,10 @@ SOURCE=.\CommandLineTestRunnerTest.cpp # End Source File # Begin Source File +SOURCE=.\CppUTestExt\ActualCallsQueueTest.cpp +# End Source File +# Begin Source File + SOURCE=.\CppUTestExt\ExpectedFunctionsListTest.cpp # End Source File # Begin Source File diff --git a/tests/AllTests.vcproj b/tests/AllTests.vcproj index 0225cba2e..f2ae110a9 100644 --- a/tests/AllTests.vcproj +++ b/tests/AllTests.vcproj @@ -423,6 +423,29 @@ /> + + + + + + + + diff --git a/tests/AllTests.vcxproj b/tests/AllTests.vcxproj index d48f39e46..2ed38d2c3 100644 --- a/tests/AllTests.vcxproj +++ b/tests/AllTests.vcxproj @@ -140,6 +140,7 @@ + diff --git a/tests/CppUTestExt/ActualCallsQueueTest.cpp b/tests/CppUTestExt/ActualCallsQueueTest.cpp new file mode 100644 index 000000000..e96e0ceeb --- /dev/null +++ b/tests/CppUTestExt/ActualCallsQueueTest.cpp @@ -0,0 +1,297 @@ +/* + * Copyright (c) 2016, Jesus Gonzalez + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "CppUTest/TestHarness.h" +#include "CppUTestExt/MockActualCallsQueue.h" +#include "CppUTestExt/MockCheckedActualCall.h" +#include "CppUTestExt/MockFailure.h" +#include "MockFailureReporterForTest.h" + +TEST_GROUP(MockActualCallsQueue) +{ + MockExpectedCallsList emptyList; + + void teardown() + { + CHECK_NO_MOCK_FAILURE(); + } +}; + +TEST(MockActualCallsQueue, emptyQueue) +{ + MockActualCallsQueue* actualCalls = new MockActualCallsQueue(true); + CHECK_TRUE(actualCalls->isEmpty()); + LONGS_EQUAL(0, actualCalls->size()); + LONGS_EQUAL((unsigned int)-1, actualCalls->getMaxSize()); + delete actualCalls; +} + +TEST(MockActualCallsQueue, pushBack_Owner) +{ + MockActualCallsQueue* actualCalls = new MockActualCallsQueue(true); + MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, emptyList); + MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, emptyList); + + actualCalls->pushBack(call1); + CHECK_FALSE(actualCalls->isEmpty()); + LONGS_EQUAL(1, actualCalls->size()); + + actualCalls->pushBack(call2); + LONGS_EQUAL(2, actualCalls->size()); + + delete actualCalls; +} + +TEST(MockActualCallsQueue, pushBack_NotOwner) +{ + MockActualCallsQueue* actualCalls = new MockActualCallsQueue(false); + MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, emptyList); + MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, emptyList); + + actualCalls->pushBack(call1); + CHECK_FALSE(actualCalls->isEmpty()); + LONGS_EQUAL(1, actualCalls->size()); + + actualCalls->pushBack(call2); + LONGS_EQUAL(2, actualCalls->size()); + + delete actualCalls; + delete call1; + delete call2; +} + +TEST(MockActualCallsQueue, pushBack_MaxSizeReached_Owner) +{ + MockActualCallsQueue* actualCalls = new MockActualCallsQueue(true, 2); + MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, emptyList); + MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, emptyList); + MockCheckedActualCall* call3 = new MockCheckedActualCall(3, NULL, emptyList); + + actualCalls->pushBack(call1); + CHECK_FALSE(actualCalls->isEmpty()); + LONGS_EQUAL(1, actualCalls->size()); + + actualCalls->pushBack(call2); + LONGS_EQUAL(2, actualCalls->size()); + + actualCalls->pushBack(call3); + LONGS_EQUAL(2, actualCalls->size()); + + delete actualCalls; +} + +TEST(MockActualCallsQueue, pushBack_MaxSizeReached_NotOwner) +{ + MockActualCallsQueue* actualCalls = new MockActualCallsQueue(false, 2); + MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, emptyList); + MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, emptyList); + MockCheckedActualCall* call3 = new MockCheckedActualCall(3, NULL, emptyList); + + actualCalls->pushBack(call1); + CHECK_FALSE(actualCalls->isEmpty()); + LONGS_EQUAL(1, actualCalls->size()); + + actualCalls->pushBack(call2); + LONGS_EQUAL(2, actualCalls->size()); + + delete actualCalls; + delete call1; + delete call2; + delete call3; +} + +TEST(MockActualCallsQueue, clear_Owner) +{ + MockActualCallsQueue* actualCalls = new MockActualCallsQueue(true); + MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, emptyList); + MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, emptyList); + actualCalls->pushBack(call1); + actualCalls->pushBack(call2); + + actualCalls->clear(); + CHECK_TRUE(actualCalls->isEmpty()); + LONGS_EQUAL(0, actualCalls->size()); + + delete actualCalls; +} + +TEST(MockActualCallsQueue, clear_NotOwner) +{ + MockActualCallsQueue* actualCalls = new MockActualCallsQueue(false); + MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, emptyList); + MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, emptyList); + actualCalls->pushBack(call1); + actualCalls->pushBack(call2); + + actualCalls->clear(); + CHECK_TRUE(actualCalls->isEmpty()); + LONGS_EQUAL(0, actualCalls->size()); + + delete actualCalls; + delete call1; + delete call2; +} + +TEST(MockActualCallsQueue, popFront_Owner) +{ + MockActualCallsQueue* actualCalls = new MockActualCallsQueue(true); + MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, emptyList); + MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, emptyList); + actualCalls->pushBack(call1); + actualCalls->pushBack(call2); + + MockCheckedActualCall* returnedCall = actualCalls->popFront(); + LONGS_EQUAL(1, actualCalls->size()); + POINTERS_EQUAL(call1, returnedCall); + + delete actualCalls; + delete call1; +} + +TEST(MockActualCallsQueue, popFront_NotOwner) +{ + MockActualCallsQueue* actualCalls = new MockActualCallsQueue(false); + MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, emptyList); + MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, emptyList); + actualCalls->pushBack(call1); + actualCalls->pushBack(call2); + + MockCheckedActualCall* returnedCall = actualCalls->popFront(); + LONGS_EQUAL(1, actualCalls->size()); + POINTERS_EQUAL(call1, returnedCall); + + returnedCall = actualCalls->popFront(); + LONGS_EQUAL(0, actualCalls->size()); + CHECK_TRUE(actualCalls->isEmpty()); + POINTERS_EQUAL(call2, returnedCall); + + delete actualCalls; + delete call1; + delete call2; +} + +TEST(MockActualCallsQueue, popFront_OnEmptyList) +{ + MockActualCallsQueue* actualCalls = new MockActualCallsQueue(false); + + MockCheckedActualCall* returnedCall = actualCalls->popFront(); + LONGS_EQUAL(0, actualCalls->size()); + CHECK_TRUE(actualCalls->isEmpty()); + POINTERS_EQUAL(NULL, returnedCall); + + delete actualCalls; +} + +TEST(MockActualCallsQueue, pushBackAll_Empty) +{ + MockActualCallsQueue* actualCalls1 = new MockActualCallsQueue(false); + MockActualCallsQueue* actualCalls2 = new MockActualCallsQueue(false); + + actualCalls2->pushBackAll(actualCalls1); + CHECK_TRUE(actualCalls2->isEmpty()); + LONGS_EQUAL(0, actualCalls2->size()); + + delete actualCalls1; + delete actualCalls2; +} + +TEST(MockActualCallsQueue, pushBackAll_NotEmpty_FromNotOwner) +{ + MockActualCallsQueue* actualCalls1 = new MockActualCallsQueue(false); + MockActualCallsQueue* actualCalls2 = new MockActualCallsQueue(false); + MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, emptyList); + MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, emptyList); + actualCalls1->pushBack(call1); + actualCalls1->pushBack(call2); + + actualCalls2->pushBackAll(*actualCalls1); + CHECK_FALSE(actualCalls2->isEmpty()); + LONGS_EQUAL(2, actualCalls2->size()); + + delete actualCalls1; + delete actualCalls2; + delete call1; + delete call2; +} + +TEST(MockActualCallsQueue, pushBackAll_NotEmpty_FromOwner) +{ + MockActualCallsQueue* actualCalls1 = new MockActualCallsQueue(true); + MockActualCallsQueue* actualCalls2 = new MockActualCallsQueue(false); + MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, emptyList); + MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, emptyList); + actualCalls1->pushBack(call1); + actualCalls1->pushBack(call2); + + actualCalls2->pushBackAll(*actualCalls1); + CHECK_FALSE(actualCalls2->isEmpty()); + LONGS_EQUAL(2, actualCalls2->size()); + + delete actualCalls2; + delete actualCalls1; +} + +TEST(MockActualCallsQueue, setMaxSize_GreaterThanCurrent) +{ + MockActualCallsQueue* actualCalls = new MockActualCallsQueue(false, 2); + MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, emptyList); + MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, emptyList); + actualCalls->pushBack(call1); + actualCalls->pushBack(call2); + + LONGS_EQUAL(2, actualCalls->size()); + actualCalls->setMaxSize(5); + LONGS_EQUAL(2, actualCalls->size()); + + delete actualCalls; + delete call1; + delete call2; +} + +TEST(MockActualCallsQueue, setMaxSize_LowerThanCurrent) +{ + MockActualCallsQueue* actualCalls = new MockActualCallsQueue(false); + MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, emptyList); + MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, emptyList); + MockCheckedActualCall* call3 = new MockCheckedActualCall(3, NULL, emptyList); + actualCalls->pushBack(call1); + actualCalls->pushBack(call2); + actualCalls->pushBack(call3); + + LONGS_EQUAL(3, actualCalls->size()); + actualCalls->setMaxSize(2); + LONGS_EQUAL(2, actualCalls->size()); + + MockCheckedActualCall* returnedCall = actualCalls->popFront(); + LONGS_EQUAL(1, actualCalls->size()); + POINTERS_EQUAL(call2, returnedCall); + + delete actualCalls; + delete call1; + delete call2; + delete call3; +} diff --git a/tests/CppUTestExt/CMakeLists.txt b/tests/CppUTestExt/CMakeLists.txt index 8039a3b52..63dae8f6c 100644 --- a/tests/CppUTestExt/CMakeLists.txt +++ b/tests/CppUTestExt/CMakeLists.txt @@ -14,6 +14,7 @@ set(CppUTestExtTests_src MockComparatorCopierTest.cpp MockExpectedCallTest.cpp ExpectedFunctionsListTest.cpp + ActualCallsQueueTest.cpp MockFailureReporterForTest.cpp MockFailureTest.cpp MockHierarchyTest.cpp From 0f4623a70065e11e8c9e1615ec73655823947b97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Sun, 3 Jul 2016 19:32:18 +0200 Subject: [PATCH 16/26] Modified MockCheckedActualCall to store a reference to its associated MockSupport instead of a reference to the list of expected calls, in preparation for the implementation of a registry of actual calls that will be stored in the MockSupport objects. --- include/CppUTestExt/MockCheckedActualCall.h | 6 ++- include/CppUTestExt/MockSupport.h | 2 + src/CppUTestExt/MockActualCall.cpp | 19 +++---- src/CppUTestExt/MockSupport.cpp | 8 ++- tests/CppUTestExt/ActualCallsQueueTest.cpp | 58 +++++++++++---------- tests/CppUTestExt/MockActualCallTest.cpp | 58 +++++++++------------ 6 files changed, 77 insertions(+), 74 deletions(-) diff --git a/include/CppUTestExt/MockCheckedActualCall.h b/include/CppUTestExt/MockCheckedActualCall.h index d61422e0c..9c23063c3 100644 --- a/include/CppUTestExt/MockCheckedActualCall.h +++ b/include/CppUTestExt/MockCheckedActualCall.h @@ -31,10 +31,12 @@ #include "CppUTestExt/MockActualCall.h" #include "CppUTestExt/MockExpectedCallsList.h" +class MockSupport; + class MockCheckedActualCall : public MockActualCall { public: - MockCheckedActualCall(unsigned int callOrder, MockFailureReporter* reporter, const MockExpectedCallsList& expectations); + MockCheckedActualCall(unsigned int callOrder, MockFailureReporter* reporter, const MockSupport& mockSupport); virtual ~MockCheckedActualCall(); virtual MockActualCall& withName(const SimpleString& name) _override; @@ -123,7 +125,7 @@ class MockCheckedActualCall : public MockActualCall MockCheckedExpectedCall* matchingExpectation_; MockExpectedCallsList potentiallyMatchingExpectations_; - const MockExpectedCallsList& allExpectations_; + const MockSupport& mockSupport_; class MockOutputParametersListNode { diff --git a/include/CppUTestExt/MockSupport.h b/include/CppUTestExt/MockSupport.h index 313a2b49e..66a5f7d09 100755 --- a/include/CppUTestExt/MockSupport.h +++ b/include/CppUTestExt/MockSupport.h @@ -123,6 +123,8 @@ class MockSupport virtual void installComparatorsAndCopiers(const MockNamedValueComparatorsAndCopiersRepository& repository); virtual void removeAllComparatorsAndCopiers(); + virtual const MockExpectedCallsList& getExpectedCalls() const; + protected: MockSupport* clone(const SimpleString& mockName); virtual MockCheckedActualCall *createActualFunctionCall(); diff --git a/src/CppUTestExt/MockActualCall.cpp b/src/CppUTestExt/MockActualCall.cpp index d9cfdf8c0..a9f1d3dde 100644 --- a/src/CppUTestExt/MockActualCall.cpp +++ b/src/CppUTestExt/MockActualCall.cpp @@ -30,6 +30,7 @@ #include "CppUTestExt/MockCheckedExpectedCall.h" #include "CppUTestExt/MockFailure.h" #include "CppUTest/PlatformSpecificFunctions.h" +#include "CppUTestExt/MockSupport.h" MockActualCall::MockActualCall() { @@ -49,10 +50,10 @@ SimpleString MockCheckedActualCall::getName() const return functionName_; } -MockCheckedActualCall::MockCheckedActualCall(unsigned int callOrder, MockFailureReporter* reporter, const MockExpectedCallsList& allExpectations) - : callOrder_(callOrder), reporter_(reporter), state_(CALL_SUCCEED), expectationsChecked_(false), matchingExpectation_(NULL), allExpectations_(allExpectations), outputParameterExpectations_(NULL) +MockCheckedActualCall::MockCheckedActualCall(unsigned int callOrder, MockFailureReporter* reporter, const MockSupport& mockSupport) + : callOrder_(callOrder), reporter_(reporter), state_(CALL_SUCCEED), expectationsChecked_(false), matchingExpectation_(NULL), mockSupport_(mockSupport), outputParameterExpectations_(NULL) { - potentiallyMatchingExpectations_.addPotentiallyMatchingExpectations(allExpectations); + potentiallyMatchingExpectations_.addPotentiallyMatchingExpectations(mockSupport.getExpectedCalls()); } MockCheckedActualCall::~MockCheckedActualCall() @@ -141,7 +142,7 @@ MockActualCall& MockCheckedActualCall::withName(const SimpleString& name) potentiallyMatchingExpectations_.onlyKeepExpectationsRelatedTo(name); if (potentiallyMatchingExpectations_.isEmpty()) { - MockUnexpectedCallHappenedFailure failure(getTest(), name, allExpectations_); + MockUnexpectedCallHappenedFailure failure(getTest(), name, mockSupport_.getExpectedCalls()); failTest(failure); return *this; } @@ -164,7 +165,7 @@ void MockCheckedActualCall::checkInputParameter(const MockNamedValue& actualPara potentiallyMatchingExpectations_.onlyKeepExpectationsWithInputParameter(actualParameter); if (potentiallyMatchingExpectations_.isEmpty()) { - MockUnexpectedInputParameterFailure failure(getTest(), getName(), actualParameter, allExpectations_); + MockUnexpectedInputParameterFailure failure(getTest(), getName(), actualParameter, mockSupport_.getExpectedCalls()); failTest(failure); return; } @@ -186,7 +187,7 @@ void MockCheckedActualCall::checkOutputParameter(const MockNamedValue& outputPar potentiallyMatchingExpectations_.onlyKeepExpectationsWithOutputParameter(outputParameter); if (potentiallyMatchingExpectations_.isEmpty()) { - MockUnexpectedOutputParameterFailure failure(getTest(), getName(), outputParameter, allExpectations_); + MockUnexpectedOutputParameterFailure failure(getTest(), getName(), outputParameter, mockSupport_.getExpectedCalls()); failTest(failure); return; } @@ -361,11 +362,11 @@ void MockCheckedActualCall::checkExpectations() } if (potentiallyMatchingExpectations_.hasUnmatchingExpectationsBecauseOfMissingParameters()) { - MockExpectedParameterDidntHappenFailure failure(getTest(), getName(), allExpectations_); + MockExpectedParameterDidntHappenFailure failure(getTest(), getName(), mockSupport_.getExpectedCalls()); failTest(failure); } else { - MockExpectedObjectDidntHappenFailure failure(getTest(), getName(), allExpectations_); + MockExpectedObjectDidntHappenFailure failure(getTest(), getName(), mockSupport_.getExpectedCalls()); failTest(failure); } } @@ -531,7 +532,7 @@ MockActualCall& MockCheckedActualCall::onObject(const void* objectPtr) potentiallyMatchingExpectations_.onlyKeepExpectationsOnObject(objectPtr); if (potentiallyMatchingExpectations_.isEmpty()) { - MockUnexpectedObjectFailure failure(getTest(), getName(), objectPtr, allExpectations_); + MockUnexpectedObjectFailure failure(getTest(), getName(), objectPtr, mockSupport_.getExpectedCalls()); failTest(failure); return *this; } diff --git a/src/CppUTestExt/MockSupport.cpp b/src/CppUTestExt/MockSupport.cpp index 6252b05d4..f68557359 100755 --- a/src/CppUTestExt/MockSupport.cpp +++ b/src/CppUTestExt/MockSupport.cpp @@ -217,7 +217,7 @@ MockExpectedCall& MockSupport::expectRangeOfCalls(unsigned int minCalls, unsigne MockCheckedActualCall* MockSupport::createActualFunctionCall() { - lastActualFunctionCall_ = new MockCheckedActualCall(++actualCallOrder_, activeReporter_, expectations_); + lastActualFunctionCall_ = new MockCheckedActualCall(++actualCallOrder_, activeReporter_, *this); return lastActualFunctionCall_; } @@ -637,3 +637,9 @@ bool MockSupport::hasReturnValue() if (lastActualFunctionCall_) return lastActualFunctionCall_->hasReturnValue(); return false; } + +const MockExpectedCallsList& MockSupport::getExpectedCalls() const +{ + return expectations_; +} + diff --git a/tests/CppUTestExt/ActualCallsQueueTest.cpp b/tests/CppUTestExt/ActualCallsQueueTest.cpp index e96e0ceeb..e8da873ee 100644 --- a/tests/CppUTestExt/ActualCallsQueueTest.cpp +++ b/tests/CppUTestExt/ActualCallsQueueTest.cpp @@ -33,11 +33,12 @@ TEST_GROUP(MockActualCallsQueue) { - MockExpectedCallsList emptyList; + MockSupport testMock; void teardown() { CHECK_NO_MOCK_FAILURE(); + testMock.clear(); } }; @@ -53,8 +54,8 @@ TEST(MockActualCallsQueue, emptyQueue) TEST(MockActualCallsQueue, pushBack_Owner) { MockActualCallsQueue* actualCalls = new MockActualCallsQueue(true); - MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, emptyList); - MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, emptyList); + MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, testMock); + MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, testMock); actualCalls->pushBack(call1); CHECK_FALSE(actualCalls->isEmpty()); @@ -69,8 +70,8 @@ TEST(MockActualCallsQueue, pushBack_Owner) TEST(MockActualCallsQueue, pushBack_NotOwner) { MockActualCallsQueue* actualCalls = new MockActualCallsQueue(false); - MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, emptyList); - MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, emptyList); + MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, testMock); + MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, testMock); actualCalls->pushBack(call1); CHECK_FALSE(actualCalls->isEmpty()); @@ -87,9 +88,9 @@ TEST(MockActualCallsQueue, pushBack_NotOwner) TEST(MockActualCallsQueue, pushBack_MaxSizeReached_Owner) { MockActualCallsQueue* actualCalls = new MockActualCallsQueue(true, 2); - MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, emptyList); - MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, emptyList); - MockCheckedActualCall* call3 = new MockCheckedActualCall(3, NULL, emptyList); + MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, testMock); + MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, testMock); + MockCheckedActualCall* call3 = new MockCheckedActualCall(3, NULL, testMock); actualCalls->pushBack(call1); CHECK_FALSE(actualCalls->isEmpty()); @@ -107,9 +108,9 @@ TEST(MockActualCallsQueue, pushBack_MaxSizeReached_Owner) TEST(MockActualCallsQueue, pushBack_MaxSizeReached_NotOwner) { MockActualCallsQueue* actualCalls = new MockActualCallsQueue(false, 2); - MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, emptyList); - MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, emptyList); - MockCheckedActualCall* call3 = new MockCheckedActualCall(3, NULL, emptyList); + MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, testMock); + MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, testMock); + MockCheckedActualCall* call3 = new MockCheckedActualCall(3, NULL, testMock); actualCalls->pushBack(call1); CHECK_FALSE(actualCalls->isEmpty()); @@ -127,8 +128,8 @@ TEST(MockActualCallsQueue, pushBack_MaxSizeReached_NotOwner) TEST(MockActualCallsQueue, clear_Owner) { MockActualCallsQueue* actualCalls = new MockActualCallsQueue(true); - MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, emptyList); - MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, emptyList); + MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, testMock); + MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, testMock); actualCalls->pushBack(call1); actualCalls->pushBack(call2); @@ -142,8 +143,8 @@ TEST(MockActualCallsQueue, clear_Owner) TEST(MockActualCallsQueue, clear_NotOwner) { MockActualCallsQueue* actualCalls = new MockActualCallsQueue(false); - MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, emptyList); - MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, emptyList); + MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, testMock); + MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, testMock); actualCalls->pushBack(call1); actualCalls->pushBack(call2); @@ -159,8 +160,8 @@ TEST(MockActualCallsQueue, clear_NotOwner) TEST(MockActualCallsQueue, popFront_Owner) { MockActualCallsQueue* actualCalls = new MockActualCallsQueue(true); - MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, emptyList); - MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, emptyList); + MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, testMock); + MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, testMock); actualCalls->pushBack(call1); actualCalls->pushBack(call2); @@ -175,8 +176,8 @@ TEST(MockActualCallsQueue, popFront_Owner) TEST(MockActualCallsQueue, popFront_NotOwner) { MockActualCallsQueue* actualCalls = new MockActualCallsQueue(false); - MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, emptyList); - MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, emptyList); + MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, testMock); + MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, testMock); actualCalls->pushBack(call1); actualCalls->pushBack(call2); @@ -223,8 +224,8 @@ TEST(MockActualCallsQueue, pushBackAll_NotEmpty_FromNotOwner) { MockActualCallsQueue* actualCalls1 = new MockActualCallsQueue(false); MockActualCallsQueue* actualCalls2 = new MockActualCallsQueue(false); - MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, emptyList); - MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, emptyList); + MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, testMock); + MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, testMock); actualCalls1->pushBack(call1); actualCalls1->pushBack(call2); @@ -242,8 +243,8 @@ TEST(MockActualCallsQueue, pushBackAll_NotEmpty_FromOwner) { MockActualCallsQueue* actualCalls1 = new MockActualCallsQueue(true); MockActualCallsQueue* actualCalls2 = new MockActualCallsQueue(false); - MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, emptyList); - MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, emptyList); + MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, testMock); + MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, testMock); actualCalls1->pushBack(call1); actualCalls1->pushBack(call2); @@ -258,8 +259,8 @@ TEST(MockActualCallsQueue, pushBackAll_NotEmpty_FromOwner) TEST(MockActualCallsQueue, setMaxSize_GreaterThanCurrent) { MockActualCallsQueue* actualCalls = new MockActualCallsQueue(false, 2); - MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, emptyList); - MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, emptyList); + MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, testMock); + MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, testMock); actualCalls->pushBack(call1); actualCalls->pushBack(call2); @@ -275,9 +276,9 @@ TEST(MockActualCallsQueue, setMaxSize_GreaterThanCurrent) TEST(MockActualCallsQueue, setMaxSize_LowerThanCurrent) { MockActualCallsQueue* actualCalls = new MockActualCallsQueue(false); - MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, emptyList); - MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, emptyList); - MockCheckedActualCall* call3 = new MockCheckedActualCall(3, NULL, emptyList); + MockCheckedActualCall* call1 = new MockCheckedActualCall(1, NULL, testMock); + MockCheckedActualCall* call2 = new MockCheckedActualCall(2, NULL, testMock); + MockCheckedActualCall* call3 = new MockCheckedActualCall(3, NULL, testMock); actualCalls->pushBack(call1); actualCalls->pushBack(call2); actualCalls->pushBack(call3); @@ -295,3 +296,4 @@ TEST(MockActualCallsQueue, setMaxSize_LowerThanCurrent) delete call2; delete call3; } + diff --git a/tests/CppUTestExt/MockActualCallTest.cpp b/tests/CppUTestExt/MockActualCallTest.cpp index bf15bcedb..c4bb7d858 100644 --- a/tests/CppUTestExt/MockActualCallTest.cpp +++ b/tests/CppUTestExt/MockActualCallTest.cpp @@ -34,40 +34,38 @@ TEST_GROUP(MockCheckedActualCall) { - MockExpectedCallsList* emptyList; - MockExpectedCallsList* list; MockFailureReporter* reporter; + MockSupport* testMock; void setup() { - emptyList = new MockExpectedCallsList; - list = new MockExpectedCallsList; + testMock = new MockSupport(); reporter = MockFailureReporterForTest::getReporter(); } void teardown() { CHECK_NO_MOCK_FAILURE(); - delete emptyList; - delete list; + testMock->clear(); + delete testMock; } }; TEST(MockCheckedActualCall, unExpectedCall) { - MockCheckedActualCall actualCall(1, reporter, *emptyList); + MockCheckedActualCall actualCall(1, reporter, *testMock); actualCall.withName("unexpected"); - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "unexpected", *list); + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "unexpected", testMock->getExpectedCalls()); CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); } TEST(MockCheckedActualCall, unExpectedCallWithAnInputParameter) { - MockCheckedActualCall actualCall(1, reporter, *emptyList); + MockCheckedActualCall actualCall(1, reporter, *testMock); actualCall.withName("unexpected").withParameter("bar", 0); - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "unexpected", *list); + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "unexpected", testMock->getExpectedCalls()); CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); CHECK(actualCall.hasFailed()); // Checks that withParameter() doesn't "reset" call state @@ -75,10 +73,10 @@ TEST(MockCheckedActualCall, unExpectedCallWithAnInputParameter) TEST(MockCheckedActualCall, unExpectedCallWithAnOutputParameter) { - MockCheckedActualCall actualCall(1, reporter, *emptyList); + MockCheckedActualCall actualCall(1, reporter, *testMock); actualCall.withName("unexpected").withOutputParameter("bar", (void*)0); - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "unexpected", *list); + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "unexpected", testMock->getExpectedCalls()); CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); CHECK(actualCall.hasFailed()); // Checks that withOutputParameter() doesn't "reset" call state @@ -88,10 +86,10 @@ TEST(MockCheckedActualCall, unExpectedCallOnObject) { int object; - MockCheckedActualCall actualCall(1, reporter, *emptyList); + MockCheckedActualCall actualCall(1, reporter, *testMock); actualCall.withName("unexpected").onObject(&object); - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "unexpected", *list); + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "unexpected", testMock->getExpectedCalls()); CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); CHECK(actualCall.hasFailed()); // Checks that onObject() doesn't "reset" call state @@ -99,53 +97,45 @@ TEST(MockCheckedActualCall, unExpectedCallOnObject) TEST(MockCheckedActualCall, actualCallWithNoReturnValueAndMeaninglessCallOrderForCoverage) { - MockCheckedActualCall actualCall(1, reporter, *emptyList); + MockCheckedActualCall actualCall(1, reporter, *testMock); actualCall.withName("noreturn").returnValue(); - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "noreturn", *list); + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "noreturn", testMock->getExpectedCalls()); CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); } TEST(MockCheckedActualCall, unExpectedParameterName) { - MockCheckedExpectedCall call1(1, 1); - call1.withName("func"); - list->addExpectedCall(&call1); + testMock->expectOneCall("func"); - MockCheckedActualCall actualCall(1, reporter, *list); + MockCheckedActualCall actualCall(1, reporter, *testMock); actualCall.withName("func").withParameter("integer", 1); MockNamedValue parameter("integer"); parameter.setValue(1); - MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "func", parameter, *list); + MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "func", parameter, testMock->getExpectedCalls()); CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); } TEST(MockCheckedActualCall, multipleSameFunctionsExpectingAndHappenGradually) { - MockCheckedExpectedCall* call1 = new MockCheckedExpectedCall(1, 1); - MockCheckedExpectedCall* call2 = new MockCheckedExpectedCall(1, 1); - call1->withName("func"); - call2->withName("func"); - list->addExpectedCall(call1); - list->addExpectedCall(call2); + testMock->expectOneCall("func"); + testMock->expectOneCall("func"); - LONGS_EQUAL(2, list->amountOfUnfulfilledExpectations()); + LONGS_EQUAL(2, testMock->getExpectedCalls().amountOfUnfulfilledExpectations()); - MockCheckedActualCall actualCall1(1, reporter, *list); + MockCheckedActualCall actualCall1(1, reporter, *testMock); actualCall1.withName("func"); actualCall1.checkExpectations(); - LONGS_EQUAL(1, list->amountOfUnfulfilledExpectations()); + LONGS_EQUAL(1, testMock->getExpectedCalls().amountOfUnfulfilledExpectations()); - MockCheckedActualCall actualCall2(2, reporter, *list); + MockCheckedActualCall actualCall2(2, reporter, *testMock); actualCall2.withName("func"); actualCall2.checkExpectations(); - LONGS_EQUAL(0, list->amountOfUnfulfilledExpectations()); - - list->deleteAllExpectationsAndClearList(); + LONGS_EQUAL(0, testMock->getExpectedCalls().amountOfUnfulfilledExpectations()); } TEST(MockCheckedActualCall, MockIgnoredActualCallWorksAsItShould) From bc331818188cbb2a5f49ec21ecb0c5156132fc60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Sun, 3 Jul 2016 20:16:02 +0200 Subject: [PATCH 17/26] Added functionality to MockCheckedActualCall and MockActualCallsQueue to generate a representation of their contents as a string, in preparation implementing an actual calls log. --- include/CppUTest/SimpleString.h | 5 +- include/CppUTestExt/MockActualCallsQueue.h | 18 ++++++ include/CppUTestExt/MockCheckedActualCall.h | 10 ++- include/CppUTestExt/MockCheckedExpectedCall.h | 7 +- include/CppUTestExt/MockExpectedCallsList.h | 2 +- include/CppUTestExt/MockSupport.h | 3 + src/CppUTest/SimpleString.cpp | 19 ++++++ src/CppUTestExt/MockActualCall.cpp | 32 +++++++++- src/CppUTestExt/MockActualCallsQueue.cpp | 21 ++++++ src/CppUTestExt/MockExpectedCall.cpp | 31 +++++---- src/CppUTestExt/MockExpectedCallsList.cpp | 36 +++-------- src/CppUTestExt/MockSupport.cpp | 5 ++ tests/CppUTestExt/ActualCallsQueueTest.cpp | 64 +++++++++++++++++++ .../CppUTestExt/ExpectedFunctionsListTest.cpp | 4 +- tests/CppUTestExt/MockActualCallTest.cpp | 40 ++++++++++++ tests/CppUTestExt/MockExpectedCallTest.cpp | 20 +++--- .../MockFailureReporterForTest.cpp | 33 ++++++++++ .../CppUTestExt/MockFailureReporterForTest.h | 18 ++++++ 18 files changed, 307 insertions(+), 61 deletions(-) diff --git a/include/CppUTest/SimpleString.h b/include/CppUTest/SimpleString.h index c8bbcb5eb..1c64f81b2 100644 --- a/include/CppUTest/SimpleString.h +++ b/include/CppUTest/SimpleString.h @@ -173,8 +173,9 @@ SimpleString BracketsFormattedHexStringFrom(cpputest_longlong value); SimpleString BracketsFormattedHexStringFrom(cpputest_ulonglong value); SimpleString BracketsFormattedHexStringFrom(signed char value); SimpleString BracketsFormattedHexString(SimpleString hexString); - - +SimpleString AppendStringOnANewLine(const SimpleString& inputString, const SimpleString& stringToAppend, + const SimpleString& linePrefix = ""); +SimpleString ReplaceWithTextNoneWhenEmpty(const SimpleString& inputString, const SimpleString& linePrefix); #if CPPUTEST_USE_STD_CPP_LIB diff --git a/include/CppUTestExt/MockActualCallsQueue.h b/include/CppUTestExt/MockActualCallsQueue.h index ce5a4389c..16568b8af 100644 --- a/include/CppUTestExt/MockActualCallsQueue.h +++ b/include/CppUTestExt/MockActualCallsQueue.h @@ -127,6 +127,24 @@ class MockActualCallsQueue */ virtual void setMaxSize(unsigned int maxSize); + /** + * Returns a string representation of the queue as a list with the description of all its stored calls, each on a new line. + * + * @param linePrefix String to be added before each individual call description + * @return String representation of the queue + */ + virtual SimpleString toString(const SimpleString& linePrefix) const; + + /** + * Returns a string representation of the queue as a list with the description of its stored calls for function @c functionName, + * each on a new line. + * + * @param functionName Name of the function to be filtered through + * @param linePrefix String to be added before each individual call description + * @return String representation of the queue + */ + virtual SimpleString toStringFilterByFunction(const SimpleString& functionName, const SimpleString& linePrefix) const; + protected: class MockActualCallsQueueNode { diff --git a/include/CppUTestExt/MockCheckedActualCall.h b/include/CppUTestExt/MockCheckedActualCall.h index 9c23063c3..e66fc76c5 100644 --- a/include/CppUTestExt/MockCheckedActualCall.h +++ b/include/CppUTestExt/MockCheckedActualCall.h @@ -96,9 +96,13 @@ class MockCheckedActualCall : public MockActualCall virtual void checkExpectations(); virtual void setMockFailureReporter(MockFailureReporter* reporter); + + virtual SimpleString toString() const; + + const SimpleString& getName() const; + protected: void setName(const SimpleString& name); - SimpleString getName() const; virtual UtestShell* getTest() const; virtual void callHasSucceeded(); virtual void finalizeOutputParameters(MockCheckedExpectedCall* call); @@ -115,6 +119,10 @@ class MockCheckedActualCall : public MockActualCall }; virtual void setState(ActualCallState state); + virtual const SimpleString& getScopeName() const; + + void setMatchingExpectedCall(MockCheckedExpectedCall* call); + private: SimpleString functionName_; unsigned int callOrder_; diff --git a/include/CppUTestExt/MockCheckedExpectedCall.h b/include/CppUTestExt/MockCheckedExpectedCall.h index d7cb1fc49..e0f19435e 100644 --- a/include/CppUTestExt/MockCheckedExpectedCall.h +++ b/include/CppUTestExt/MockCheckedExpectedCall.h @@ -74,7 +74,7 @@ class MockCheckedExpectedCall : public MockExpectedCall virtual MockNamedValue getInputParameter(const SimpleString& name); virtual MockNamedValue getOutputParameter(const SimpleString& name); virtual SimpleString getInputParameterType(const SimpleString& name); - virtual SimpleString getInputParameterValueString(const SimpleString& name); + virtual SimpleString getInputParameterValueString(const SimpleString& name) const; virtual bool hasInputParameterWithName(const SimpleString& name); virtual bool hasInputParameter(const MockNamedValue& parameter); @@ -97,16 +97,17 @@ class MockCheckedExpectedCall : public MockExpectedCall virtual void wasPassedToObject(); virtual void resetActualCallMatchingState(); - virtual SimpleString callToString(); + virtual SimpleString callToString(bool asActualCall) const; virtual SimpleString missingParametersToString(); enum { NO_EXPECTED_CALL_ORDER = 0 }; virtual unsigned int getActualCallsFulfilled() const; + const SimpleString& getName() const; + protected: void setName(const SimpleString& name); - SimpleString getName() const; private: SimpleString functionName_; diff --git a/include/CppUTestExt/MockExpectedCallsList.h b/include/CppUTestExt/MockExpectedCallsList.h index 94723890d..01b8792ce 100644 --- a/include/CppUTestExt/MockExpectedCallsList.h +++ b/include/CppUTestExt/MockExpectedCallsList.h @@ -75,7 +75,7 @@ class MockExpectedCallsList virtual SimpleString unfulfilledCallsToString(const SimpleString& linePrefix = "") const; virtual SimpleString fulfilledCallsToString(const SimpleString& linePrefix = "") const; - virtual SimpleString missingParametersToString() const; + virtual SimpleString missingParametersToString(const SimpleString& linePrefix = "") const; protected: virtual void pruneEmptyNodeFromList(); diff --git a/include/CppUTestExt/MockSupport.h b/include/CppUTestExt/MockSupport.h index 66a5f7d09..b80341a6b 100755 --- a/include/CppUTestExt/MockSupport.h +++ b/include/CppUTestExt/MockSupport.h @@ -32,6 +32,7 @@ #include "CppUTestExt/MockCheckedActualCall.h" #include "CppUTestExt/MockCheckedExpectedCall.h" #include "CppUTestExt/MockExpectedCallsList.h" +#include "CppUTestExt/MockActualCallsQueue.h" class UtestShell; class MockSupport; @@ -125,6 +126,8 @@ class MockSupport virtual const MockExpectedCallsList& getExpectedCalls() const; + const SimpleString& getName() const; + protected: MockSupport* clone(const SimpleString& mockName); virtual MockCheckedActualCall *createActualFunctionCall(); diff --git a/src/CppUTest/SimpleString.cpp b/src/CppUTest/SimpleString.cpp index 6932d0b54..9853fd98d 100644 --- a/src/CppUTest/SimpleString.cpp +++ b/src/CppUTest/SimpleString.cpp @@ -808,6 +808,25 @@ SimpleString StringFromOrdinalNumber(unsigned int number) return StringFromFormat("%u%s", number, suffix); } +SimpleString AppendStringOnANewLine(const SimpleString& inputString, const SimpleString& stringToAppend, const SimpleString& linePrefix) +{ + SimpleString str = inputString; + if (!str.isEmpty()) str += "\n"; + str += linePrefix; + str += stringToAppend; + return str; +} + +SimpleString ReplaceWithTextNoneWhenEmpty(const SimpleString& inputString, const SimpleString& linePrefix) +{ + SimpleString str = inputString; + if (str == "") { + str += linePrefix; + str += ""; + } + return str; +} + SimpleStringCollection::SimpleStringCollection() { collection_ = 0; diff --git a/src/CppUTestExt/MockActualCall.cpp b/src/CppUTestExt/MockActualCall.cpp index a9f1d3dde..b9e6cec7f 100644 --- a/src/CppUTestExt/MockActualCall.cpp +++ b/src/CppUTestExt/MockActualCall.cpp @@ -45,7 +45,7 @@ void MockCheckedActualCall::setName(const SimpleString& name) functionName_ = name; } -SimpleString MockCheckedActualCall::getName() const +const SimpleString& MockCheckedActualCall::getName() const { return functionName_; } @@ -568,6 +568,36 @@ void MockCheckedActualCall::cleanUpOutputParameterList() } } +const SimpleString& MockCheckedActualCall::getScopeName() const +{ + return mockSupport_.getName(); +} + +SimpleString MockCheckedActualCall::toString() const +{ + SimpleString str; + const SimpleString& scopeName = getScopeName(); + + if (scopeName.isEmpty()) { + str += StringFromFormat("(%u) ", callOrder_); + } else { + str += StringFromFormat("(%s::%u) ", scopeName.asCharString(), callOrder_); + } + + if (matchingExpectation_) { + str += matchingExpectation_->callToString(true); + } else { + str += functionName_; + str += " (NOT matching any expected call)"; + } + + return str; +} + +void MockCheckedActualCall::setMatchingExpectedCall(MockCheckedExpectedCall* call) +{ + matchingExpectation_ = call; +} MockActualCallTrace::MockActualCallTrace() { diff --git a/src/CppUTestExt/MockActualCallsQueue.cpp b/src/CppUTestExt/MockActualCallsQueue.cpp index 34045b70d..372bb45c9 100644 --- a/src/CppUTestExt/MockActualCallsQueue.cpp +++ b/src/CppUTestExt/MockActualCallsQueue.cpp @@ -126,3 +126,24 @@ void MockActualCallsQueue::deleteFromFront() delete frontCall; } } + +SimpleString MockActualCallsQueue::toString(const SimpleString& linePrefix) const +{ + SimpleString str; + for (MockActualCallsQueueNode* p = head_; p; p = p->next_) { + str = AppendStringOnANewLine(str, p->call_->toString(), linePrefix); + } + return ReplaceWithTextNoneWhenEmpty(str, linePrefix); +} + +SimpleString MockActualCallsQueue::toStringFilterByFunction(const SimpleString& functionName, const SimpleString& linePrefix) const +{ + SimpleString str; + for (MockActualCallsQueueNode* p = head_; p; p = p->next_) { + if (p->call_->getName() == functionName) { + str = AppendStringOnANewLine(str, p->call_->toString(), linePrefix); + } + } + return ReplaceWithTextNoneWhenEmpty(str, linePrefix); +} + diff --git a/src/CppUTestExt/MockExpectedCall.cpp b/src/CppUTestExt/MockExpectedCall.cpp index 25ac2f075..65c4b4b5a 100644 --- a/src/CppUTestExt/MockExpectedCall.cpp +++ b/src/CppUTestExt/MockExpectedCall.cpp @@ -46,7 +46,7 @@ void MockCheckedExpectedCall::setName(const SimpleString& name) functionName_ = name; } -SimpleString MockCheckedExpectedCall::getName() const +const SimpleString& MockCheckedExpectedCall::getName() const { return functionName_; } @@ -308,7 +308,7 @@ void MockCheckedExpectedCall::outputParameterWasPassed(const SimpleString& name) } } -SimpleString MockCheckedExpectedCall::getInputParameterValueString(const SimpleString& name) +SimpleString MockCheckedExpectedCall::getInputParameterValueString(const SimpleString& name) const { MockNamedValue * p = inputParameters_->getValueByName(name); return (p) ? StringFrom(*p) : "failed"; @@ -326,7 +326,7 @@ bool MockCheckedExpectedCall::hasOutputParameter(const MockNamedValue& parameter return (p) ? p->compatibleForCopying(parameter) : ignoreOtherParameters_; } -SimpleString MockCheckedExpectedCall::callToString() +SimpleString MockCheckedExpectedCall::callToString(bool asActualCall) const { SimpleString str; if (objectPtr_) @@ -334,6 +334,7 @@ SimpleString MockCheckedExpectedCall::callToString() str += getName(); str += " -> "; + if (initialExpectedCallOrder_ != NO_EXPECTED_CALL_ORDER) { if (initialExpectedCallOrder_ == finalExpectedCallOrder_) { str += StringFromFormat("expected call order: <%u> -> ", initialExpectedCallOrder_); @@ -368,23 +369,25 @@ SimpleString MockCheckedExpectedCall::callToString() str += ", other parameters are ignored"; } - if (actualCalls_ < minCalls_) - { - if (minCalls_ == maxCalls_) + if (!asActualCall) { + if (actualCalls_ < minCalls_) { - str += StringFromFormat(" (expected %d call%s, but was called %d time%s)", - minCalls_, (minCalls_ == 1) ? "" : "s", actualCalls_, (actualCalls_ == 1) ? "" : "s" ); + 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" ); + } } else { - str += StringFromFormat(" (expected at least %d call%s, but was called %d time%s)", - minCalls_, (minCalls_ == 1) ? "" : "s", actualCalls_, (actualCalls_ == 1) ? "" : "s" ); + str += StringFromFormat(" (called %d time%s)", actualCalls_, (actualCalls_ == 1) ? "" : "s"); } } - else - { - str += StringFromFormat(" (called %d time%s)", actualCalls_, (actualCalls_ == 1) ? "" : "s"); - } return str; } diff --git a/src/CppUTestExt/MockExpectedCallsList.cpp b/src/CppUTestExt/MockExpectedCallsList.cpp index 43da1edcf..45caf89fc 100644 --- a/src/CppUTestExt/MockExpectedCallsList.cpp +++ b/src/CppUTestExt/MockExpectedCallsList.cpp @@ -314,32 +314,14 @@ void MockExpectedCallsList::outputParameterWasPassed(const SimpleString& paramet p->expectedCall_->outputParameterWasPassed(parameterName); } -static SimpleString stringOrNoneTextWhenEmpty(const SimpleString& inputString, const SimpleString& linePrefix) -{ - SimpleString str = inputString; - if (str == "") { - str += linePrefix; - str += ""; - } - return str; -} - -static SimpleString appendStringOnANewLine(const SimpleString& inputString, const SimpleString& linePrefix, const SimpleString& stringToAppend) -{ - SimpleString str = inputString; - if (str != "") str += "\n"; - str += linePrefix; - str += stringToAppend; - return str; -} - SimpleString MockExpectedCallsList::unfulfilledCallsToString(const SimpleString& linePrefix) const { SimpleString str; for (MockExpectedCallsListNode* p = head_; p; p = p->next_) if (!p->expectedCall_->isFulfilled()) - str = appendStringOnANewLine(str, linePrefix, p->expectedCall_->callToString()); - return stringOrNoneTextWhenEmpty(str, linePrefix); + str = AppendStringOnANewLine(str, p->expectedCall_->callToString(false), linePrefix); + + return ReplaceWithTextNoneWhenEmpty(str, linePrefix); } SimpleString MockExpectedCallsList::fulfilledCallsToString(const SimpleString& linePrefix) const @@ -348,19 +330,19 @@ SimpleString MockExpectedCallsList::fulfilledCallsToString(const SimpleString& l for (MockExpectedCallsListNode* p = head_; p; p = p->next_) if (p->expectedCall_->isFulfilled()) - str = appendStringOnANewLine(str, linePrefix, p->expectedCall_->callToString()); + str = AppendStringOnANewLine(str, p->expectedCall_->callToString(false), linePrefix); - return stringOrNoneTextWhenEmpty(str, linePrefix); + return ReplaceWithTextNoneWhenEmpty(str, linePrefix); } -SimpleString MockExpectedCallsList::missingParametersToString() const +SimpleString MockExpectedCallsList::missingParametersToString(const SimpleString& linePrefix) const { SimpleString str; for (MockExpectedCallsListNode* p = head_; p; p = p->next_) - if (! p->expectedCall_->isMatchingActualCall()) - str = appendStringOnANewLine(str, "", p->expectedCall_->missingParametersToString()); + if (p->expectedCall_->canMatchActualCalls()) + str = AppendStringOnANewLine(str, p->expectedCall_->missingParametersToString(), linePrefix); - return stringOrNoneTextWhenEmpty(str, ""); + return ReplaceWithTextNoneWhenEmpty(str, linePrefix); } bool MockExpectedCallsList::hasUnmatchingExpectationsBecauseOfMissingParameters() const diff --git a/src/CppUTestExt/MockSupport.cpp b/src/CppUTestExt/MockSupport.cpp index f68557359..b0190b536 100755 --- a/src/CppUTestExt/MockSupport.cpp +++ b/src/CppUTestExt/MockSupport.cpp @@ -643,3 +643,8 @@ const MockExpectedCallsList& MockSupport::getExpectedCalls() const return expectations_; } +const SimpleString& MockSupport::getName() const +{ + return mockName_; +} + diff --git a/tests/CppUTestExt/ActualCallsQueueTest.cpp b/tests/CppUTestExt/ActualCallsQueueTest.cpp index e8da873ee..e7ac6a9aa 100644 --- a/tests/CppUTestExt/ActualCallsQueueTest.cpp +++ b/tests/CppUTestExt/ActualCallsQueueTest.cpp @@ -297,3 +297,67 @@ TEST(MockActualCallsQueue, setMaxSize_LowerThanCurrent) delete call3; } +TEST(MockActualCallsQueue, callToString_Empty) +{ + MockActualCallsQueue* actualCalls = new MockActualCallsQueue(true); + + STRCMP_EQUAL("###", actualCalls->toString("###").asCharString()); + + delete actualCalls; +} + +TEST(MockActualCallsQueue, callToString_NotEmpty) +{ + MockCheckedExpectedCall expectedCall1(1, 1); + expectedCall1.withName("foo"); + MockCheckedExpectedCall expectedCall2(1, 1); + expectedCall2.withName("bar"); + + MockActualCallsQueue* actualCalls = new MockActualCallsQueue(true); + MockCheckedActualCall* actualCall1 = new MockCheckedActualCallForTest(1, "", &expectedCall1); + MockCheckedActualCall* actualCall2 = new MockCheckedActualCallForTest(2, "", &expectedCall2); + actualCalls->pushBack(actualCall1); + actualCalls->pushBack(actualCall2); + + SimpleString expectedString = StringFromFormat("$\t%s\n$\t%s", actualCall1->toString().asCharString(), actualCall2->toString().asCharString()); + STRCMP_EQUAL(expectedString.asCharString(), actualCalls->toString("$\t").asCharString()); + + delete actualCalls; +} + +TEST(MockActualCallsQueue, toStringFilterByFunction_NoMatches) +{ + MockCheckedExpectedCall expectedCall1(1, 1); + expectedCall1.withName("foo"); + MockCheckedExpectedCall expectedCall2(1, 1); + expectedCall2.withName("bar"); + + MockActualCallsQueue* actualCalls = new MockActualCallsQueue(true); + MockCheckedActualCall* actualCall1 = new MockCheckedActualCallForTest(1, "", &expectedCall1); + MockCheckedActualCall* actualCall2 = new MockCheckedActualCallForTest(2, "", &expectedCall2); + actualCalls->pushBack(actualCall1); + actualCalls->pushBack(actualCall2); + + STRCMP_EQUAL("[@]", actualCalls->toStringFilterByFunction("foobar", "[@]").asCharString()); + + delete actualCalls; +} + +TEST(MockActualCallsQueue, toStringFilterByFunction_WithMatches) +{ + MockCheckedExpectedCall expectedCall1(1, 1); + expectedCall1.withName("foo"); + MockCheckedExpectedCall expectedCall2(1, 1); + expectedCall2.withName("bar"); + + MockActualCallsQueue* actualCalls = new MockActualCallsQueue(true); + MockCheckedActualCall* actualCall1 = new MockCheckedActualCallForTest(1, "", &expectedCall1); + MockCheckedActualCall* actualCall2 = new MockCheckedActualCallForTest(2, "", &expectedCall2); + actualCalls->pushBack(actualCall1); + actualCalls->pushBack(actualCall2); + + SimpleString expectedString = StringFromFormat("\t~ %s", actualCall1->toString().asCharString()); + STRCMP_EQUAL(expectedString.asCharString(), actualCalls->toStringFilterByFunction("foo", "\t~ ").asCharString()); + + delete actualCalls; +} diff --git a/tests/CppUTestExt/ExpectedFunctionsListTest.cpp b/tests/CppUTestExt/ExpectedFunctionsListTest.cpp index 12846ee91..a8c815238 100644 --- a/tests/CppUTestExt/ExpectedFunctionsListTest.cpp +++ b/tests/CppUTestExt/ExpectedFunctionsListTest.cpp @@ -249,7 +249,7 @@ TEST(MockExpectedCallsList, callToStringForUnfulfilledFunctions) list->addExpectedCall(call3); SimpleString expectedString; - expectedString = StringFromFormat("%s\n%s", call1->callToString().asCharString(), call2->callToString().asCharString()); + expectedString = StringFromFormat("%s\n%s", call1->callToString(false).asCharString(), call2->callToString(false).asCharString()); STRCMP_EQUAL(expectedString.asCharString(), list->unfulfilledCallsToString().asCharString()); } @@ -265,7 +265,7 @@ TEST(MockExpectedCallsList, callToStringForFulfilledFunctions) list->addExpectedCall(call2); SimpleString expectedString; - expectedString = StringFromFormat("%s\n%s", call1->callToString().asCharString(), call2->callToString().asCharString()); + expectedString = StringFromFormat("%s\n%s", call1->callToString(false).asCharString(), call2->callToString(false).asCharString()); STRCMP_EQUAL(expectedString.asCharString(), list->fulfilledCallsToString().asCharString()); } diff --git a/tests/CppUTestExt/MockActualCallTest.cpp b/tests/CppUTestExt/MockActualCallTest.cpp index c4bb7d858..d90f2d1c7 100644 --- a/tests/CppUTestExt/MockActualCallTest.cpp +++ b/tests/CppUTestExt/MockActualCallTest.cpp @@ -138,6 +138,46 @@ TEST(MockCheckedActualCall, multipleSameFunctionsExpectingAndHappenGradually) LONGS_EQUAL(0, testMock->getExpectedCalls().amountOfUnfulfilledExpectations()); } +TEST(MockCheckedActualCall, toString_NotMatchingExpectedCall) +{ + mock().expectOneCall("foo").withParameter("bar", 1); + + MockCheckedActualCall actualCall(10, reporter, mock()); + actualCall.withName("foo"); + + STRCMP_EQUAL("(10) foo (NOT matching any expected call)", actualCall.toString().asCharString()); + + mock().clear(); +} + +TEST(MockCheckedActualCall, toString_NoScope) +{ + void* object = (void*) 0xAA; + MockCheckedExpectedCall& expectedCall = (MockCheckedExpectedCall&) mock().expectOneCall("foo"); + expectedCall.onObject(object).withParameter("bar", 5); + + MockCheckedActualCall actualCall(99, reporter, mock()); + actualCall.withName("foo").onObject(object).withParameter("bar", 5); + + SimpleString expectedString = StringFromFormat("(99) %s", expectedCall.callToString(true).asCharString()); + STRCMP_EQUAL(expectedString.asCharString(), actualCall.toString().asCharString()); + + mock().clear(); +} + +TEST(MockCheckedActualCall, toString_WithScope) +{ + MockCheckedExpectedCall& expectedCall = (MockCheckedExpectedCall&) mock("bar").expectOneCall("foo"); + + MockCheckedActualCall actualCall(7765, reporter, mock("bar")); + actualCall.withName("bar::foo"); + + SimpleString expectedString = StringFromFormat("(bar::7765) %s", expectedCall.callToString(true).asCharString()); + STRCMP_EQUAL(expectedString.asCharString(), actualCall.toString().asCharString()); + + mock().clear(); +} + TEST(MockCheckedActualCall, MockIgnoredActualCallWorksAsItShould) { MockIgnoredActualCall actual; diff --git a/tests/CppUTestExt/MockExpectedCallTest.cpp b/tests/CppUTestExt/MockExpectedCallTest.cpp index cb137dc42..d825bbdbe 100644 --- a/tests/CppUTestExt/MockExpectedCallTest.cpp +++ b/tests/CppUTestExt/MockExpectedCallTest.cpp @@ -484,7 +484,7 @@ TEST(MockExpectedCall, toStringForNoParametersSingleCallNotCalled) { MockCheckedExpectedCall expectedCall(1, 1); expectedCall.withName("name"); - STRCMP_EQUAL("name -> no parameters (expected 1 call, but was called 0 times)", expectedCall.callToString().asCharString()); + STRCMP_EQUAL("name -> no parameters (expected 1 call, but was called 0 times)", expectedCall.callToString(false).asCharString()); } TEST(MockExpectedCall, toStringForNoParametersMultiCallCalledLessThanMinimum) @@ -492,7 +492,7 @@ TEST(MockExpectedCall, toStringForNoParametersMultiCallCalledLessThanMinimum) MockCheckedExpectedCall expectedCall(2, 2); expectedCall.withName("name"); expectedCall.callWasMade(1); - STRCMP_EQUAL("name -> no parameters (expected 2 calls, but was called 1 time)", expectedCall.callToString().asCharString()); + STRCMP_EQUAL("name -> no parameters (expected 2 calls, but was called 1 time)", expectedCall.callToString(false).asCharString()); } TEST(MockExpectedCall, toStringForNoParametersMultiCallCalledMinimum) @@ -501,7 +501,7 @@ TEST(MockExpectedCall, toStringForNoParametersMultiCallCalledMinimum) expectedCall.withName("name"); expectedCall.callWasMade(1); expectedCall.callWasMade(2); - STRCMP_EQUAL("name -> no parameters (called 2 times)", expectedCall.callToString().asCharString()); + STRCMP_EQUAL("name -> no parameters (called 2 times)", expectedCall.callToString(false).asCharString()); } TEST(MockExpectedCall, toStringForIgnoredParameters) @@ -509,7 +509,7 @@ TEST(MockExpectedCall, toStringForIgnoredParameters) MockCheckedExpectedCall expectedCall(1, 1); expectedCall.withName("name"); expectedCall.ignoreOtherParameters(); - STRCMP_EQUAL("name -> all parameters ignored (expected 1 call, but was called 0 times)", expectedCall.callToString().asCharString()); + STRCMP_EQUAL("name -> all parameters ignored (expected 1 call, but was called 0 times)", expectedCall.callToString(false).asCharString()); } TEST(MockExpectedCall, toStringForMultipleInputParameters) @@ -524,7 +524,7 @@ TEST(MockExpectedCall, toStringForMultipleInputParameters) expectedCall.withParameter("unsigned-integer", uint_value); expectedCall.callWasMade(1); STRCMP_EQUAL("name -> const char* string: , int integer: <10 (0xa)>, unsigned int unsigned-integer: <7 (0x7)> " - "(called 1 time)", expectedCall.callToString().asCharString()); + "(called 1 time)", expectedCall.callToString(false).asCharString()); } TEST(MockExpectedCall, toStringForMultipleInputAndOutputParameters) @@ -541,7 +541,7 @@ TEST(MockExpectedCall, toStringForMultipleInputAndOutputParameters) expectedCall.withOutputParameterReturning("buffer", buffer_value, sizeof(buffer_value)); expectedCall.callWasMade(1); STRCMP_EQUAL("name -> const char* string: , int integer: <10 (0xa)>, unsigned int unsigned-integer: <7 (0x7)>, " - "const void* buffer: (called 1 time)", expectedCall.callToString().asCharString()); + "const void* buffer: (called 1 time)", expectedCall.callToString(false).asCharString()); } TEST(MockExpectedCall, toStringForMultipleOutputParameters) @@ -553,7 +553,7 @@ TEST(MockExpectedCall, toStringForMultipleOutputParameters) expectedCall.withOutputParameterReturning("buffer1", buffer_value, sizeof(buffer_value)); expectedCall.withOutputParameterReturning("buffer2", buffer_value, sizeof(buffer_value)); expectedCall.callWasMade(1); - STRCMP_EQUAL("name -> const void* buffer1: , const void* buffer2: (called 1 time)", expectedCall.callToString().asCharString()); + STRCMP_EQUAL("name -> const void* buffer1: , const void* buffer2: (called 1 time)", expectedCall.callToString(false).asCharString()); } TEST(MockExpectedCall, toStringForParameterAndIgnored) @@ -563,7 +563,7 @@ TEST(MockExpectedCall, toStringForParameterAndIgnored) expectedCall.withParameter("string", "value"); expectedCall.ignoreOtherParameters(); expectedCall.callWasMade(1); - STRCMP_EQUAL("name -> const char* string: , other parameters are ignored (called 1 time)", expectedCall.callToString().asCharString()); + STRCMP_EQUAL("name -> const char* string: , other parameters are ignored (called 1 time)", expectedCall.callToString(false).asCharString()); } TEST(MockExpectedCall, toStringForCallOrderSingle) @@ -572,7 +572,7 @@ TEST(MockExpectedCall, toStringForCallOrderSingle) expectedCall.withName("name"); expectedCall.withCallOrder(2, 2); expectedCall.callWasMade(1); - STRCMP_EQUAL("name -> expected call order: <2> -> no parameters (called 1 time)", expectedCall.callToString().asCharString()); + STRCMP_EQUAL("name -> expected call order: <2> -> no parameters (called 1 time)", expectedCall.callToString(false).asCharString()); } TEST(MockExpectedCall, toStringForCallOrderMultiple) @@ -585,7 +585,7 @@ TEST(MockExpectedCall, toStringForCallOrderMultiple) expectedCall.callWasMade(7); expectedCall.callWasMade(8); expectedCall.callWasMade(9); - STRCMP_EQUAL("name -> expected calls order: <5..9> -> no parameters (called 5 times)", expectedCall.callToString().asCharString()); + STRCMP_EQUAL("name -> expected calls order: <5..9> -> no parameters (called 5 times)", expectedCall.callToString(false).asCharString()); } TEST(MockExpectedCall, callOrderIsFulfilledButWithWrongOrderSingle) diff --git a/tests/CppUTestExt/MockFailureReporterForTest.cpp b/tests/CppUTestExt/MockFailureReporterForTest.cpp index 1acab2671..154cdebc5 100644 --- a/tests/CppUTestExt/MockFailureReporterForTest.cpp +++ b/tests/CppUTestExt/MockFailureReporterForTest.cpp @@ -118,3 +118,36 @@ MockCheckedExpectedCall* MockExpectedCallsListForTest::addFunction(const SimpleS newCall->withCallOrder(order, order); return newCall; } + +static MockSupport testMockSupport; + +MockCheckedActualCallForTest::MockCheckedActualCallForTest(unsigned int callOrder, const SimpleString& scopeName, MockCheckedExpectedCall* matchingCall) +: MockCheckedActualCall(callOrder, NULL, testMockSupport), scopeName_(scopeName) +{ + setName(matchingCall->getName()); + setMatchingExpectedCall(matchingCall); +} + +const SimpleString& MockCheckedActualCallForTest::getScopeName() const +{ + return scopeName_; +} + +MockActualCallsQueueForTest::MockActualCallsQueueForTest() : MockActualCallsQueue(true) +{ +} + +MockCheckedActualCall* MockActualCallsQueueForTest::addCall(unsigned int callOrder, MockCheckedExpectedCall* matchingCall) +{ + return addCall(callOrder, "", matchingCall); +} + +MockCheckedActualCall* MockActualCallsQueueForTest::addCall(unsigned int callOrder, const SimpleString& mockScope, MockCheckedExpectedCall* matchingCall) +{ + MockCheckedActualCallForTest* newCall = new MockCheckedActualCallForTest(callOrder, mockScope, matchingCall); + matchingCall->finalizeActualCallMatch(); + matchingCall->callWasMade(callOrder); + pushBack(newCall); + return newCall; +} + diff --git a/tests/CppUTestExt/MockFailureReporterForTest.h b/tests/CppUTestExt/MockFailureReporterForTest.h index 9783cf358..27b2313ca 100644 --- a/tests/CppUTestExt/MockFailureReporterForTest.h +++ b/tests/CppUTestExt/MockFailureReporterForTest.h @@ -64,5 +64,23 @@ class MockExpectedCallsListForTest : public MockExpectedCallsList MockCheckedExpectedCall* addFunction(const SimpleString& name, unsigned int order); }; +class MockCheckedActualCallForTest : public MockCheckedActualCall +{ +public: + MockCheckedActualCallForTest(unsigned int callOrder, const SimpleString& scopeName, MockCheckedExpectedCall* matchingCall); + virtual const SimpleString& getScopeName() const _override; + +private: + SimpleString scopeName_; +}; + +class MockActualCallsQueueForTest : public MockActualCallsQueue +{ +public: + MockActualCallsQueueForTest(); + MockCheckedActualCall* addCall(unsigned int callOrder, MockCheckedExpectedCall* matchingCall); + MockCheckedActualCall* addCall(unsigned int callOrder, const SimpleString& mockScope, MockCheckedExpectedCall* matchingCall); +}; + #endif From 044a69359b4ccb172f140d5b1286b6fa97eda050 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Mon, 4 Jul 2016 21:15:15 +0200 Subject: [PATCH 18/26] Added actual calls reporting to MockFailure, and preliminary non-functional addition of actual calls registry to MockSupport. --- include/CppUTestExt/MockFailure.h | 29 +- include/CppUTestExt/MockSupport.h | 3 +- src/CppUTestExt/MockActualCall.cpp | 14 +- src/CppUTestExt/MockFailure.cpp | 69 +++-- src/CppUTestExt/MockSupport.cpp | 12 +- tests/CppUTestExt/MockActualCallTest.cpp | 12 +- tests/CppUTestExt/MockCallTest.cpp | 75 +++-- .../CppUTestExt/MockComparatorCopierTest.cpp | 15 +- tests/CppUTestExt/MockFailureTest.cpp | 271 ++++++++++++------ tests/CppUTestExt/MockHierarchyTest.cpp | 9 +- tests/CppUTestExt/MockParameterTest.cpp | 51 ++-- tests/CppUTestExt/MockPluginTest.cpp | 6 +- tests/CppUTestExt/MockStrictOrderTest.cpp | 12 +- tests/CppUTestExt/MockSupportTest.cpp | 5 +- 14 files changed, 392 insertions(+), 191 deletions(-) diff --git a/include/CppUTestExt/MockFailure.h b/include/CppUTestExt/MockFailure.h index fe17cbba2..ee625cd92 100644 --- a/include/CppUTestExt/MockFailure.h +++ b/include/CppUTestExt/MockFailure.h @@ -32,6 +32,7 @@ #include "CppUTest/TestFailure.h" class MockExpectedCallsList; +class MockActualCallsQueue; class MockCheckedActualCall; class MockNamedValue; class MockFailure; @@ -56,44 +57,50 @@ class MockFailure : public TestFailure MockFailure(UtestShell* test); virtual ~MockFailure(){} protected: - void addExpectationsAndCallHistory(const MockExpectedCallsList& expectations); - void addExpectationsAndCallHistoryRelatedTo(const SimpleString& function, const MockExpectedCallsList& expectations); + void addExpectationsAndCallHistory(const MockExpectedCallsList& expectedCalls, const MockActualCallsQueue& actualCalls); + void addExpectationsAndCallHistoryRelatedTo(const SimpleString& function, const MockExpectedCallsList& expectedCalls, + const MockActualCallsQueue& actualCalls); }; class MockExpectedCallsNotFulfilledFailure : public MockFailure { public: - MockExpectedCallsNotFulfilledFailure(UtestShell* test, const MockExpectedCallsList& expectations); + MockExpectedCallsNotFulfilledFailure(UtestShell* test, const MockExpectedCallsList& expectations, + const MockActualCallsQueue& actualCalls); }; class MockUnexpectedCallHappenedFailure : public MockFailure { public: - MockUnexpectedCallHappenedFailure(UtestShell* test, const SimpleString& name, const MockExpectedCallsList& expectations); + MockUnexpectedCallHappenedFailure(UtestShell* test, const SimpleString& name, const MockExpectedCallsList& expectations, + const MockActualCallsQueue& actualCalls); }; class MockCallOrderFailure : public MockFailure { public: - MockCallOrderFailure(UtestShell* test, const MockExpectedCallsList& expectations); + MockCallOrderFailure(UtestShell* test, const MockExpectedCallsList& expectations, const MockActualCallsQueue& actualCalls); }; class MockUnexpectedInputParameterFailure : public MockFailure { public: - MockUnexpectedInputParameterFailure(UtestShell* test, const SimpleString& functionName, const MockNamedValue& parameter, const MockExpectedCallsList& expectations); + MockUnexpectedInputParameterFailure(UtestShell* test, const SimpleString& functionName, const MockNamedValue& parameter, + const MockExpectedCallsList& expectations, const MockActualCallsQueue& actualCalls); }; class MockUnexpectedOutputParameterFailure : public MockFailure { public: - MockUnexpectedOutputParameterFailure(UtestShell* test, const SimpleString& functionName, const MockNamedValue& parameter, const MockExpectedCallsList& expectations); + MockUnexpectedOutputParameterFailure(UtestShell* test, const SimpleString& functionName, const MockNamedValue& parameter, + const MockExpectedCallsList& expectations, const MockActualCallsQueue& actualCalls); }; class MockExpectedParameterDidntHappenFailure : public MockFailure { public: - MockExpectedParameterDidntHappenFailure(UtestShell* test, const SimpleString& functionName, const MockExpectedCallsList& expectations); + MockExpectedParameterDidntHappenFailure(UtestShell* test, const SimpleString& functionName, const MockExpectedCallsList& expectations, + const MockActualCallsQueue& actualCalls); }; class MockNoWayToCompareCustomTypeFailure : public MockFailure @@ -111,13 +118,15 @@ class MockNoWayToCopyCustomTypeFailure : public MockFailure class MockUnexpectedObjectFailure : public MockFailure { public: - MockUnexpectedObjectFailure(UtestShell* test, const SimpleString& functionName, const void* expected, const MockExpectedCallsList& expectations); + MockUnexpectedObjectFailure(UtestShell* test, const SimpleString& functionName, const void* expected, + const MockExpectedCallsList& expectations, const MockActualCallsQueue& actualCalls); }; class MockExpectedObjectDidntHappenFailure : public MockFailure { public: - MockExpectedObjectDidntHappenFailure(UtestShell* test, const SimpleString& functionName, const MockExpectedCallsList& expectations); + MockExpectedObjectDidntHappenFailure(UtestShell* test, const SimpleString& functionName, const MockExpectedCallsList& expectations, + const MockActualCallsQueue& actualCalls); }; class MockStrictOrderingIncompatibleWithOptionalCallsFailure : public MockFailure diff --git a/include/CppUTestExt/MockSupport.h b/include/CppUTestExt/MockSupport.h index b80341a6b..cf3b73f41 100755 --- a/include/CppUTestExt/MockSupport.h +++ b/include/CppUTestExt/MockSupport.h @@ -125,6 +125,7 @@ class MockSupport virtual void removeAllComparatorsAndCopiers(); virtual const MockExpectedCallsList& getExpectedCalls() const; + virtual const MockActualCallsQueue& getActualCalls() const; const SimpleString& getName() const; @@ -148,7 +149,7 @@ class MockSupport MockNamedValueComparatorsAndCopiersRepository comparatorsAndCopiersRepository_; MockNamedValueList data_; const SimpleString mockName_; - + MockActualCallsQueue actualCalls_; bool tracing_; void checkExpectationsOfLastActualCall(); diff --git a/src/CppUTestExt/MockActualCall.cpp b/src/CppUTestExt/MockActualCall.cpp index b9e6cec7f..d97974128 100644 --- a/src/CppUTestExt/MockActualCall.cpp +++ b/src/CppUTestExt/MockActualCall.cpp @@ -142,7 +142,7 @@ MockActualCall& MockCheckedActualCall::withName(const SimpleString& name) potentiallyMatchingExpectations_.onlyKeepExpectationsRelatedTo(name); if (potentiallyMatchingExpectations_.isEmpty()) { - MockUnexpectedCallHappenedFailure failure(getTest(), name, mockSupport_.getExpectedCalls()); + MockUnexpectedCallHappenedFailure failure(getTest(), name, mockSupport_.getExpectedCalls(), mockSupport_.getActualCalls()); failTest(failure); return *this; } @@ -165,7 +165,8 @@ void MockCheckedActualCall::checkInputParameter(const MockNamedValue& actualPara potentiallyMatchingExpectations_.onlyKeepExpectationsWithInputParameter(actualParameter); if (potentiallyMatchingExpectations_.isEmpty()) { - MockUnexpectedInputParameterFailure failure(getTest(), getName(), actualParameter, mockSupport_.getExpectedCalls()); + MockUnexpectedInputParameterFailure failure(getTest(), getName(), actualParameter, mockSupport_.getExpectedCalls(), + mockSupport_.getActualCalls()); failTest(failure); return; } @@ -187,7 +188,8 @@ void MockCheckedActualCall::checkOutputParameter(const MockNamedValue& outputPar potentiallyMatchingExpectations_.onlyKeepExpectationsWithOutputParameter(outputParameter); if (potentiallyMatchingExpectations_.isEmpty()) { - MockUnexpectedOutputParameterFailure failure(getTest(), getName(), outputParameter, mockSupport_.getExpectedCalls()); + MockUnexpectedOutputParameterFailure failure(getTest(), getName(), outputParameter, mockSupport_.getExpectedCalls(), + mockSupport_.getActualCalls()); failTest(failure); return; } @@ -362,11 +364,11 @@ void MockCheckedActualCall::checkExpectations() } if (potentiallyMatchingExpectations_.hasUnmatchingExpectationsBecauseOfMissingParameters()) { - MockExpectedParameterDidntHappenFailure failure(getTest(), getName(), mockSupport_.getExpectedCalls()); + MockExpectedParameterDidntHappenFailure failure(getTest(), getName(), mockSupport_.getExpectedCalls(), mockSupport_.getActualCalls()); failTest(failure); } else { - MockExpectedObjectDidntHappenFailure failure(getTest(), getName(), mockSupport_.getExpectedCalls()); + MockExpectedObjectDidntHappenFailure failure(getTest(), getName(), mockSupport_.getExpectedCalls(), mockSupport_.getActualCalls()); failTest(failure); } } @@ -532,7 +534,7 @@ MockActualCall& MockCheckedActualCall::onObject(const void* objectPtr) potentiallyMatchingExpectations_.onlyKeepExpectationsOnObject(objectPtr); if (potentiallyMatchingExpectations_.isEmpty()) { - MockUnexpectedObjectFailure failure(getTest(), getName(), objectPtr, mockSupport_.getExpectedCalls()); + MockUnexpectedObjectFailure failure(getTest(), getName(), objectPtr, mockSupport_.getExpectedCalls(), mockSupport_.getActualCalls()); failTest(failure); return *this; } diff --git a/src/CppUTestExt/MockFailure.cpp b/src/CppUTestExt/MockFailure.cpp index 9ac420fe2..bff24669a 100644 --- a/src/CppUTestExt/MockFailure.cpp +++ b/src/CppUTestExt/MockFailure.cpp @@ -29,6 +29,8 @@ #include "CppUTestExt/MockFailure.h" #include "CppUTestExt/MockExpectedCall.h" #include "CppUTestExt/MockExpectedCallsList.h" +#include "CppUTestExt/MockActualCallsQueue.h" +#include "CppUTestExt/MockCheckedActualCall.h" #include "CppUTestExt/MockNamedValue.h" class MockFailureReporterTestTerminator : public NormalTestTerminator @@ -69,15 +71,18 @@ MockFailure::MockFailure(UtestShell* test) : TestFailure(test, "Test failed with { } -void MockFailure::addExpectationsAndCallHistory(const MockExpectedCallsList& expectations) +void MockFailure::addExpectationsAndCallHistory(const MockExpectedCallsList& expectations, const MockActualCallsQueue& actualCalls) { message_ += "\tEXPECTED calls that WERE NOT fulfilled:\n"; message_ += expectations.unfulfilledCallsToString("\t\t"); message_ += "\n\tEXPECTED calls that WERE fulfilled:\n"; message_ += expectations.fulfilledCallsToString("\t\t"); + message_ += "\n\tACTUAL calls that were expected (in call order):\n"; + message_ += actualCalls.toString("\t\t"); } -void MockFailure::addExpectationsAndCallHistoryRelatedTo(const SimpleString& name, const MockExpectedCallsList& expectations) +void MockFailure::addExpectationsAndCallHistoryRelatedTo(const SimpleString& name, const MockExpectedCallsList& expectations, + const MockActualCallsQueue& actualCalls) { MockExpectedCallsList expectationsForFunction; expectationsForFunction.addExpectationsRelatedTo(name, expectations); @@ -93,29 +98,40 @@ void MockFailure::addExpectationsAndCallHistoryRelatedTo(const SimpleString& nam message_ += "\n"; message_ += expectationsForFunction.fulfilledCallsToString("\t\t"); + + message_ += "\n\tACTUAL calls that were expected (in call order) for function: "; + message_ += name; + message_ += "\n"; + + message_ += actualCalls.toStringFilterByFunction(name, "\t\t"); } -MockExpectedCallsNotFulfilledFailure::MockExpectedCallsNotFulfilledFailure(UtestShell* test, const MockExpectedCallsList& expectations) : MockFailure(test) +MockExpectedCallsNotFulfilledFailure::MockExpectedCallsNotFulfilledFailure(UtestShell* test, const MockExpectedCallsList& expectations, + const MockActualCallsQueue& actualCalls) +: MockFailure(test) { message_ = "Mock Failure: Expected call WAS NOT fulfilled.\n"; - addExpectationsAndCallHistory(expectations); + addExpectationsAndCallHistory(expectations, actualCalls); } -MockUnexpectedCallHappenedFailure::MockUnexpectedCallHappenedFailure(UtestShell* test, const SimpleString& name, const MockExpectedCallsList& expectations) : MockFailure(test) +MockUnexpectedCallHappenedFailure::MockUnexpectedCallHappenedFailure(UtestShell* test, const SimpleString& name, const MockExpectedCallsList& expectations, + const MockActualCallsQueue& actualCalls) +: MockFailure(test) { if (expectations.hasExpectationWithName(name)) { - unsigned int actualCalls = expectations.amountOfActualCallsFulfilledFor(name); - SimpleString ordinalNumber = StringFromOrdinalNumber(actualCalls + 1); + unsigned int actualCallsForFunction = expectations.amountOfActualCallsFulfilledFor(name); + SimpleString ordinalNumber = StringFromOrdinalNumber(actualCallsForFunction + 1); message_ = StringFromFormat("Mock Failure: Unexpected additional (%s) call to function: ", ordinalNumber.asCharString()); } else { message_ = "Mock Failure: Unexpected call to function: "; } message_ += name; message_ += "\n"; - addExpectationsAndCallHistory(expectations); + addExpectationsAndCallHistory(expectations, actualCalls); } -MockCallOrderFailure::MockCallOrderFailure(UtestShell* test, const MockExpectedCallsList& expectations) : MockFailure(test) +MockCallOrderFailure::MockCallOrderFailure(UtestShell* test, const MockExpectedCallsList& expectations, const MockActualCallsQueue& actualCalls) +: MockFailure(test) { MockExpectedCallsList expectationsForOutOfOrder; expectationsForOutOfOrder.addExpectations(expectations); @@ -123,10 +139,12 @@ MockCallOrderFailure::MockCallOrderFailure(UtestShell* test, const MockExpectedC message_ = "Mock Failure: Out of order calls"; message_ += "\n"; - addExpectationsAndCallHistory(expectationsForOutOfOrder); + addExpectationsAndCallHistory(expectationsForOutOfOrder, actualCalls); } -MockUnexpectedInputParameterFailure::MockUnexpectedInputParameterFailure(UtestShell* test, const SimpleString& functionName, const MockNamedValue& parameter, const MockExpectedCallsList& expectations) : MockFailure(test) +MockUnexpectedInputParameterFailure::MockUnexpectedInputParameterFailure(UtestShell* test, const SimpleString& functionName, const MockNamedValue& parameter, + const MockExpectedCallsList& expectations, const MockActualCallsQueue& actualCalls) +: MockFailure(test) { MockExpectedCallsList expectationsForFunctionWithParameterName; expectationsForFunctionWithParameterName.addExpectationsRelatedTo(functionName, expectations); @@ -149,7 +167,7 @@ MockUnexpectedInputParameterFailure::MockUnexpectedInputParameterFailure(UtestSh } message_ += "\n"; - addExpectationsAndCallHistoryRelatedTo(functionName, expectations); + addExpectationsAndCallHistoryRelatedTo(functionName, expectations, actualCalls); message_ += "\n\tACTUAL unexpected parameter passed to function: "; message_ += functionName; @@ -164,7 +182,9 @@ MockUnexpectedInputParameterFailure::MockUnexpectedInputParameterFailure(UtestSh message_ += ">"; } -MockUnexpectedOutputParameterFailure::MockUnexpectedOutputParameterFailure(UtestShell* test, const SimpleString& functionName, const MockNamedValue& parameter, const MockExpectedCallsList& expectations) : MockFailure(test) +MockUnexpectedOutputParameterFailure::MockUnexpectedOutputParameterFailure(UtestShell* test, const SimpleString& functionName, const MockNamedValue& parameter, + const MockExpectedCallsList& expectations, const MockActualCallsQueue& actualCalls) +: MockFailure(test) { MockExpectedCallsList expectationsForFunctionWithParameterName; expectationsForFunctionWithParameterName.addExpectationsRelatedTo(functionName, expectations); @@ -187,7 +207,7 @@ MockUnexpectedOutputParameterFailure::MockUnexpectedOutputParameterFailure(Utest } message_ += "\n"; - addExpectationsAndCallHistoryRelatedTo(functionName, expectations); + addExpectationsAndCallHistoryRelatedTo(functionName, expectations, actualCalls); message_ += "\n\tACTUAL unexpected output parameter passed to function: "; message_ += functionName; @@ -199,7 +219,9 @@ MockUnexpectedOutputParameterFailure::MockUnexpectedOutputParameterFailure(Utest message_ += parameter.getName(); } -MockExpectedParameterDidntHappenFailure::MockExpectedParameterDidntHappenFailure(UtestShell* test, const SimpleString& functionName, const MockExpectedCallsList& expectations) : MockFailure(test) +MockExpectedParameterDidntHappenFailure::MockExpectedParameterDidntHappenFailure(UtestShell* test, const SimpleString& functionName, + const MockExpectedCallsList& expectations, const MockActualCallsQueue& actualCalls) +: MockFailure(test) { MockExpectedCallsList expectationsForFunction; expectationsForFunction.addExpectationsRelatedTo(functionName, expectations); @@ -208,11 +230,10 @@ MockExpectedParameterDidntHappenFailure::MockExpectedParameterDidntHappenFailure message_ += functionName; message_ += "\" did not happen.\n"; - addExpectationsAndCallHistoryRelatedTo(functionName, expectations); + addExpectationsAndCallHistoryRelatedTo(functionName, expectations, actualCalls); message_ += "\n\tMISSING parameters that didn't happen:\n"; - message_ += "\t\t"; - message_ += expectationsForFunction.missingParametersToString(); + message_ += expectationsForFunction.missingParametersToString("\t\t"); } MockNoWayToCompareCustomTypeFailure::MockNoWayToCompareCustomTypeFailure(UtestShell* test, const SimpleString& typeName) : MockFailure(test) @@ -225,17 +246,21 @@ MockNoWayToCopyCustomTypeFailure::MockNoWayToCopyCustomTypeFailure(UtestShell* t message_ = StringFromFormat("MockFailure: No way to copy type <%s>. Please install a MockNamedValueCopier.", typeName.asCharString()); } -MockUnexpectedObjectFailure::MockUnexpectedObjectFailure(UtestShell* test, const SimpleString& functionName, const void* actual, const MockExpectedCallsList& expectations) : MockFailure(test) +MockUnexpectedObjectFailure::MockUnexpectedObjectFailure(UtestShell* test, const SimpleString& functionName, const void* actual, + const MockExpectedCallsList& expectations, const MockActualCallsQueue& actualCalls) +: MockFailure(test) { message_ = StringFromFormat ("MockFailure: Function called on an unexpected object: %s\n" "\tActual object for call has address: <%p>\n", functionName.asCharString(),actual); - addExpectationsAndCallHistoryRelatedTo(functionName, expectations); + addExpectationsAndCallHistoryRelatedTo(functionName, expectations, actualCalls); } -MockExpectedObjectDidntHappenFailure::MockExpectedObjectDidntHappenFailure(UtestShell* test, const SimpleString& functionName, const MockExpectedCallsList& expectations) : MockFailure(test) +MockExpectedObjectDidntHappenFailure::MockExpectedObjectDidntHappenFailure(UtestShell* test, const SimpleString& functionName, + const MockExpectedCallsList& expectations, const MockActualCallsQueue& actualCalls) +: MockFailure(test) { message_ = StringFromFormat("Mock Failure: Expected call on object for function \"%s\" but it did not happen.\n", functionName.asCharString()); - addExpectationsAndCallHistoryRelatedTo(functionName, expectations); + addExpectationsAndCallHistoryRelatedTo(functionName, expectations, actualCalls); } MockStrictOrderingIncompatibleWithOptionalCallsFailure::MockStrictOrderingIncompatibleWithOptionalCallsFailure(UtestShell* test, const SimpleString& functionName, unsigned int minCalls, unsigned int maxCalls) : MockFailure(test) diff --git a/src/CppUTestExt/MockSupport.cpp b/src/CppUTestExt/MockSupport.cpp index b0190b536..d1fbd0940 100755 --- a/src/CppUTestExt/MockSupport.cpp +++ b/src/CppUTestExt/MockSupport.cpp @@ -44,7 +44,8 @@ MockSupport& mock(const SimpleString& mockName, MockFailureReporter* failureRepo } MockSupport::MockSupport(const SimpleString& mockName) - : actualCallOrder_(0), expectedCallOrder_(0), strictOrdering_(false), standardReporter_(&defaultReporter_), ignoreOtherCalls_(false), enabled_(true), lastActualFunctionCall_(NULL), mockName_(mockName), tracing_(false) + : actualCallOrder_(0), expectedCallOrder_(0), strictOrdering_(false), standardReporter_(&defaultReporter_), ignoreOtherCalls_(false), + enabled_(true), lastActualFunctionCall_(NULL), mockName_(mockName), actualCalls_(true), tracing_(false) { setActiveReporter(NULL); } @@ -317,7 +318,7 @@ void MockSupport::failTestWithExpectedCallsNotFulfilled() if(getMockSupport(p)) expectationsList.addExpectations(getMockSupport(p)->expectations_); - MockExpectedCallsNotFulfilledFailure failure(activeReporter_->getTestToFail(), expectationsList); + MockExpectedCallsNotFulfilledFailure failure(activeReporter_->getTestToFail(), expectationsList, actualCalls_); failTest(failure); } @@ -330,7 +331,7 @@ void MockSupport::failTestWithOutOfOrderCalls() if(getMockSupport(p)) expectationsList.addExpectations(getMockSupport(p)->expectations_); - MockCallOrderFailure failure(activeReporter_->getTestToFail(), expectationsList); + MockCallOrderFailure failure(activeReporter_->getTestToFail(), expectationsList, actualCalls_); failTest(failure); } @@ -643,6 +644,11 @@ const MockExpectedCallsList& MockSupport::getExpectedCalls() const return expectations_; } +const MockActualCallsQueue& MockSupport::getActualCalls() const +{ + return actualCalls_; +} + const SimpleString& MockSupport::getName() const { return mockName_; diff --git a/tests/CppUTestExt/MockActualCallTest.cpp b/tests/CppUTestExt/MockActualCallTest.cpp index d90f2d1c7..783ee1c01 100644 --- a/tests/CppUTestExt/MockActualCallTest.cpp +++ b/tests/CppUTestExt/MockActualCallTest.cpp @@ -56,7 +56,7 @@ TEST(MockCheckedActualCall, unExpectedCall) MockCheckedActualCall actualCall(1, reporter, *testMock); actualCall.withName("unexpected"); - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "unexpected", testMock->getExpectedCalls()); + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "unexpected", testMock->getExpectedCalls(), testMock->getActualCalls()); CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); } @@ -65,7 +65,7 @@ TEST(MockCheckedActualCall, unExpectedCallWithAnInputParameter) MockCheckedActualCall actualCall(1, reporter, *testMock); actualCall.withName("unexpected").withParameter("bar", 0); - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "unexpected", testMock->getExpectedCalls()); + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "unexpected", testMock->getExpectedCalls(), testMock->getActualCalls()); CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); CHECK(actualCall.hasFailed()); // Checks that withParameter() doesn't "reset" call state @@ -76,7 +76,7 @@ TEST(MockCheckedActualCall, unExpectedCallWithAnOutputParameter) MockCheckedActualCall actualCall(1, reporter, *testMock); actualCall.withName("unexpected").withOutputParameter("bar", (void*)0); - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "unexpected", testMock->getExpectedCalls()); + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "unexpected", testMock->getExpectedCalls(), testMock->getActualCalls()); CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); CHECK(actualCall.hasFailed()); // Checks that withOutputParameter() doesn't "reset" call state @@ -89,7 +89,7 @@ TEST(MockCheckedActualCall, unExpectedCallOnObject) MockCheckedActualCall actualCall(1, reporter, *testMock); actualCall.withName("unexpected").onObject(&object); - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "unexpected", testMock->getExpectedCalls()); + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "unexpected", testMock->getExpectedCalls(), testMock->getActualCalls()); CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); CHECK(actualCall.hasFailed()); // Checks that onObject() doesn't "reset" call state @@ -100,7 +100,7 @@ TEST(MockCheckedActualCall, actualCallWithNoReturnValueAndMeaninglessCallOrderFo MockCheckedActualCall actualCall(1, reporter, *testMock); actualCall.withName("noreturn").returnValue(); - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "noreturn", testMock->getExpectedCalls()); + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "noreturn", testMock->getExpectedCalls(), testMock->getActualCalls()); CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); } @@ -114,7 +114,7 @@ TEST(MockCheckedActualCall, unExpectedParameterName) MockNamedValue parameter("integer"); parameter.setValue(1); - MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "func", parameter, testMock->getExpectedCalls()); + MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "func", parameter, testMock->getExpectedCalls(), testMock->getActualCalls()); CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); } diff --git a/tests/CppUTestExt/MockCallTest.cpp b/tests/CppUTestExt/MockCallTest.cpp index 2242e6616..ad87a15a6 100644 --- a/tests/CppUTestExt/MockCallTest.cpp +++ b/tests/CppUTestExt/MockCallTest.cpp @@ -88,7 +88,8 @@ TEST(MockCallTest, checkExpectationsClearsTheExpectations) MockExpectedCallsListForTest expectations; expectations.addFunction("foobar"); - MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations); + MockActualCallsQueueForTest actualCalls; + MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations, actualCalls); mock().expectOneCall("foobar"); mock().checkExpectations(); @@ -104,7 +105,8 @@ TEST(MockCallTest, expectOneCallInScopeButNotHappen) MockExpectedCallsListForTest expectations; expectations.addFunction("scope::foobar"); - MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations); + MockActualCallsQueueForTest actualCalls; + MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations, actualCalls); mock("scope").expectOneCall("foobar"); mock().checkExpectations(); @@ -118,7 +120,8 @@ TEST(MockCallTest, unexpectedCallHappened) MockFailureReporterInstaller failureReporterInstaller; MockExpectedCallsListForTest emptyExpectations; - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "func", emptyExpectations); + MockActualCallsQueueForTest actualCalls; + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "func", emptyExpectations, actualCalls); mock().actualCall("func"); @@ -130,7 +133,8 @@ TEST(MockCallTest, unexpectedScopeCallHappened) MockFailureReporterInstaller failureReporterInstaller; MockExpectedCallsListForTest emptyExpectations; - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "scope::func", emptyExpectations); + MockActualCallsQueueForTest actualCalls; + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "scope::func", emptyExpectations, actualCalls); mock("scope").actualCall("func"); @@ -142,7 +146,8 @@ TEST(MockCallTest, expectOneCallInOneScopeButActualCallInAnotherScope) MockFailureReporterInstaller failureReporterInstaller; MockExpectedCallsListForTest emptyExpectations; - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "class::foo", emptyExpectations); + MockActualCallsQueueForTest actualCalls; + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "class::foo", emptyExpectations, actualCalls); mock("scope").expectOneCall("foo"); mock("class").actualCall("foo"); @@ -156,7 +161,8 @@ TEST(MockCallTest, expectOneCallInScopeButActualCallInGlobal) MockFailureReporterInstaller failureReporterInstaller; MockExpectedCallsListForTest emptyExpectations; - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "foo", emptyExpectations); + MockActualCallsQueueForTest actualCalls; + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "foo", emptyExpectations, actualCalls); mock("scope").expectOneCall("foo"); mock().actualCall("foo"); @@ -182,7 +188,8 @@ TEST(MockCallTest, expectOneCallHoweverMultipleHappened) MockExpectedCallsListForTest expectations; expectations.addFunction("foo")->callWasMade(1); expectations.addFunction("foo")->callWasMade(2); - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "foo", expectations); + MockActualCallsQueueForTest actualCalls; + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "foo", expectations, actualCalls); mock().expectOneCall("foo"); mock().expectOneCall("foo"); @@ -199,7 +206,8 @@ TEST(MockCallTest, expectNoCallThatHappened) MockExpectedCallsListForTest expectations; expectations.addFunction(0, 0, "lazy"); - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "lazy", expectations); + MockActualCallsQueueForTest actualCalls; + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "lazy", expectations, actualCalls); mock().expectNoCall("lazy"); mock().actualCall("lazy"); @@ -214,7 +222,8 @@ TEST(MockCallTest, expectNoCallDoesntInfluenceExpectOneCall) MockExpectedCallsListForTest expectations; expectations.addFunction(0, 0, "lazy"); expectations.addFunction("influence", MockCheckedExpectedCall::NO_EXPECTED_CALL_ORDER)->callWasMade(1); - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "lazy", expectations); + MockActualCallsQueueForTest actualCalls; + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "lazy", expectations, actualCalls); mock().expectNoCall("lazy"); mock().expectOneCall("influence"); @@ -230,7 +239,8 @@ TEST(MockCallTest, expectNoCallOnlyFailureOnceWhenMultipleHappened) MockExpectedCallsListForTest expectations; expectations.addFunction(0, 0, "lazy"); - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "lazy", expectations); + MockActualCallsQueueForTest actualCalls; + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "lazy", expectations, actualCalls); mock().expectNoCall("lazy"); mock().actualCall("lazy"); @@ -244,7 +254,8 @@ TEST(MockCallTest, ignoreOtherCallsExceptForTheUnExpectedOne) MockExpectedCallsListForTest expectations; expectations.addFunction(0, 0, "lazy"); - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "lazy", expectations); + MockActualCallsQueueForTest actualCalls; + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "lazy", expectations, actualCalls); mock().expectNoCall("lazy"); mock().ignoreOtherCalls(); @@ -263,7 +274,8 @@ TEST(MockCallTest, expectNoCallInScopeThatHappened) MockExpectedCallsListForTest expectations; expectations.addFunction(0, 0, "scope::lazy"); - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "scope::lazy", expectations); + MockActualCallsQueueForTest actualCalls; + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "scope::lazy", expectations, actualCalls); mock("scope").expectNoCall("lazy"); mock("scope").actualCall("lazy"); @@ -276,7 +288,8 @@ TEST(MockCallTest, expectNoCallInScopeButActualCallInAnotherScope) MockFailureReporterInstaller failureReporterInstaller; MockExpectedCallsListForTest expectations; - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "scope2::lazy", expectations); + MockActualCallsQueueForTest actualCalls; + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "scope2::lazy", expectations, actualCalls); mock("scope1").expectNoCall("lazy"); mock("scope2").actualCall("lazy"); @@ -289,7 +302,8 @@ TEST(MockCallTest, expectNoCallInScopeButActualCallInGlobal) MockFailureReporterInstaller failureReporterInstaller; MockExpectedCallsListForTest expectations; - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "lazy", expectations); + MockActualCallsQueueForTest actualCalls; + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "lazy", expectations, actualCalls); mock("scope1").expectNoCall("lazy"); mock().actualCall("lazy"); @@ -312,7 +326,8 @@ TEST(MockCallTest, ignoreOtherCallsDoesntIgnoreMultipleCallsOfTheSameFunction) MockExpectedCallsListForTest expectations; expectations.addFunction("foo")->callWasMade(1); - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "foo", expectations); + MockActualCallsQueueForTest actualCalls; + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "foo", expectations, actualCalls); mock().expectOneCall("foo"); mock().ignoreOtherCalls(); @@ -329,7 +344,8 @@ TEST(MockCallTest, ignoreOtherStillFailsIfExpectedOneDidntHappen) MockExpectedCallsListForTest expectations; expectations.addFunction("foo"); - MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations); + MockActualCallsQueueForTest actualCalls; + MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations, actualCalls); mock().expectOneCall("foo"); mock().ignoreOtherCalls(); @@ -379,11 +395,12 @@ TEST(MockCallTest, OnObjectFails) void* objectPtr2 = (void*) 0x002; MockExpectedCallsListForTest expectations; expectations.addFunction("boo")->onObject(objectPtr); + MockActualCallsQueueForTest actualCalls; mock().expectOneCall("boo").onObject(objectPtr); mock().actualCall("boo").onObject(objectPtr2); - MockUnexpectedObjectFailure expectedFailure(mockFailureTest(), "boo", objectPtr2, expectations); + MockUnexpectedObjectFailure expectedFailure(mockFailureTest(), "boo", objectPtr2, expectations, actualCalls); CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); } @@ -395,13 +412,14 @@ TEST(MockCallTest, OnObjectExpectedButNotCalled) MockExpectedCallsListForTest expectations; expectations.addFunction("boo")->onObject(objectPtr); expectations.addFunction("boo")->onObject(objectPtr); + MockActualCallsQueueForTest actualCalls; mock().expectOneCall("boo").onObject(objectPtr); mock().expectOneCall("boo").onObject(objectPtr); mock().actualCall("boo"); mock().actualCall("boo"); - MockExpectedObjectDidntHappenFailure expectedFailure(mockFailureTest(), "boo", expectations); + MockExpectedObjectDidntHappenFailure expectedFailure(mockFailureTest(), "boo", expectations, actualCalls); CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); mock().checkExpectations(); CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); @@ -532,7 +550,8 @@ TEST(MockCallTest, expectNCalls_NotFulfilled) MockExpectedCallsListForTest expectations; expectations.addFunction(2, 2, "boo")->callWasMade(1); - MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations); + MockActualCallsQueueForTest actualCalls; + MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations, actualCalls); mock().expectNCalls(2, "boo"); mock().actualCall("boo"); @@ -547,7 +566,8 @@ TEST(MockCallTest, expectAtLeastOneCall_NotFulfilled) MockExpectedCallsListForTest expectations; expectations.addFunction(1, (unsigned int)-1, "boo"); - MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations); + MockActualCallsQueueForTest actualCalls; + MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations, actualCalls); mock().expectAtLeastOneCall("boo"); mock().checkExpectations(); @@ -563,7 +583,8 @@ TEST(MockCallTest, expectAtLeastNCalls_NotFulfilled) MockCheckedExpectedCall* expectedCall = expectations.addFunction(3, (unsigned int)-1, "boo"); expectedCall->callWasMade(1); expectedCall->callWasMade(2); - MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations); + MockActualCallsQueueForTest actualCalls; + MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations, actualCalls); mock().expectAtLeastNCalls(3, "boo"); mock().actualCall("boo"); @@ -579,7 +600,8 @@ TEST(MockCallTest, expectAtMostOneCall_NotFulfilled) MockExpectedCallsListForTest expectations; expectations.addFunction(0, 1, "boo")->callWasMade(1); - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "boo", expectations); + MockActualCallsQueueForTest actualCalls; + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "boo", expectations, actualCalls); mock().expectAtMostOneCall("boo"); mock().actualCall("boo"); @@ -598,7 +620,8 @@ TEST(MockCallTest, expectAtMostNCalls_NotFulfilled) expectedCall->callWasMade(1); expectedCall->callWasMade(2); expectedCall->callWasMade(3); - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "boo", expectations); + MockActualCallsQueueForTest actualCalls; + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "boo", expectations, actualCalls); mock().expectAtMostNCalls(3, "boo"); mock().actualCall("boo"); @@ -617,7 +640,8 @@ TEST(MockCallTest, expectRangeOfCalls_NotFulfilled_BelowMinimum) MockExpectedCallsListForTest expectations; MockCheckedExpectedCall* expectedCall = expectations.addFunction(2, 3, "boo"); expectedCall->callWasMade(1); - MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations); + MockActualCallsQueueForTest actualCalls; + MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations, actualCalls); mock().expectRangeOfCalls(2, 3, "boo"); mock().actualCall("boo"); @@ -635,7 +659,8 @@ TEST(MockCallTest, expectRangeOfCalls_NotFulfilled_AboveMaximum) expectedCall->callWasMade(1); expectedCall->callWasMade(2); expectedCall->callWasMade(3); - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "boo", expectations); + MockActualCallsQueueForTest actualCalls; + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "boo", expectations, actualCalls); mock().expectRangeOfCalls(2, 3, "boo"); mock().actualCall("boo"); diff --git a/tests/CppUTestExt/MockComparatorCopierTest.cpp b/tests/CppUTestExt/MockComparatorCopierTest.cpp index 65d0237f1..877adf89b 100644 --- a/tests/CppUTestExt/MockComparatorCopierTest.cpp +++ b/tests/CppUTestExt/MockComparatorCopierTest.cpp @@ -149,7 +149,8 @@ TEST(MockComparatorCopierTest, customObjectWithFunctionComparatorThatFailsCovers MockExpectedCallsListForTest expectations; expectations.addFunction("function")->withParameterOfType("MyTypeForTesting", "parameterName", &object); - MockExpectedCallsNotFulfilledFailure failure(UtestShell::getCurrent(), expectations); + MockActualCallsQueueForTest actualCalls; + MockExpectedCallsNotFulfilledFailure failure(UtestShell::getCurrent(), expectations, actualCalls); mock().expectOneCall("function").withParameterOfType("MyTypeForTesting", "parameterName", &object); mock().checkExpectations(); @@ -184,7 +185,8 @@ TEST(MockComparatorCopierTest, noActualCallForCustomTypeOutputParameter) MockExpectedCallsListForTest expectations; expectations.addFunction("foo")->withOutputParameterOfTypeReturning("MyTypeForTesting", "output", &expectedObject); - MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations); + MockActualCallsQueueForTest actualCalls; + MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations, actualCalls); mock().expectOneCall("foo").withOutputParameterOfTypeReturning("MyTypeForTesting", "output", &expectedObject); mock().checkExpectations(); @@ -206,7 +208,8 @@ TEST(MockComparatorCopierTest, unexpectedCustomTypeOutputParameter) expectations.addFunction("foo"); MockNamedValue parameter("parameterName"); parameter.setObjectPointer("MyTypeForTesting", &actualObject); - MockUnexpectedOutputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations); + MockActualCallsQueueForTest actualCalls; + MockUnexpectedOutputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations, actualCalls); mock().expectOneCall("foo"); mock().actualCall("foo").withOutputParameterOfType("MyTypeForTesting", "parameterName", &actualObject); @@ -227,7 +230,8 @@ TEST(MockComparatorCopierTest, customTypeOutputParameterMissing) MockExpectedCallsListForTest expectations; expectations.addFunction("foo")->withOutputParameterOfTypeReturning("MyTypeForTesting", "output", &expectedObject); - MockExpectedParameterDidntHappenFailure expectedFailure(mockFailureTest(), "foo", expectations); + MockActualCallsQueueForTest actualCalls; + MockExpectedParameterDidntHappenFailure expectedFailure(mockFailureTest(), "foo", expectations, actualCalls); mock().expectOneCall("foo").withOutputParameterOfTypeReturning("MyTypeForTesting", "output", &expectedObject); mock().actualCall("foo"); @@ -251,7 +255,8 @@ TEST(MockComparatorCopierTest, customTypeOutputParameterOfWrongType) expectations.addFunction("foo")->withOutputParameterOfTypeReturning("MyTypeForTesting", "output", &expectedObject); MockNamedValue parameter("output"); parameter.setObjectPointer("OtherTypeForTesting", &actualObject); - MockUnexpectedOutputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations); + MockActualCallsQueueForTest actualCalls; + MockUnexpectedOutputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations, actualCalls); mock().expectOneCall("foo").withOutputParameterOfTypeReturning("MyTypeForTesting", "output", &expectedObject); mock().actualCall("foo").withOutputParameterOfType("OtherTypeForTesting", "output", &actualObject); diff --git a/tests/CppUTestExt/MockFailureTest.cpp b/tests/CppUTestExt/MockFailureTest.cpp index 1921dc129..dae206d03 100644 --- a/tests/CppUTestExt/MockFailureTest.cpp +++ b/tests/CppUTestExt/MockFailureTest.cpp @@ -33,47 +33,22 @@ TEST_GROUP(MockFailureTest) { - MockFailureReporter reporter; - - MockExpectedCallsList *list; - MockCheckedExpectedCall* call1; - MockCheckedExpectedCall* call2; - MockCheckedExpectedCall* call3; - - void setup () - { - list = new MockExpectedCallsList; - call1 = new MockCheckedExpectedCall(1, 1); - call2 = new MockCheckedExpectedCall(1, 1); - call3 = new MockCheckedExpectedCall(1, 1); - } void teardown () { - delete list; - delete call1; - delete call2; - delete call3; CHECK_NO_MOCK_FAILURE(); } - void addAllToList() - { - list->addExpectedCall(call1); - list->addExpectedCall(call2); - list->addExpectedCall(call3); - } void checkUnexpectedNthCallMessage(unsigned int count, const char* expectedOrdinal) { - MockExpectedCallsList callList; - MockCheckedExpectedCall expCall(count-1, count-1); + MockExpectedCallsListForTest expectedCalls; + MockCheckedExpectedCall* expectedCall = expectedCalls.addFunction(count-1, count-1, "bar"); - callList.addExpectedCall(&expCall); - expCall.withName("bar"); + MockActualCallsQueueForTest actualCalls; for (unsigned int i = 0; i < (count - 1); i++) { - expCall.callWasMade(1); + actualCalls.addCall(i, expectedCall); } - MockUnexpectedCallHappenedFailure failure(UtestShell::getCurrent(), "bar", callList); + MockUnexpectedCallHappenedFailure failure(UtestShell::getCurrent(), "bar", expectedCalls, actualCalls); SimpleString expectedMessage = StringFromFormat("Mock Failure: Unexpected additional (%s) call to function: bar\n\tEXPECTED", expectedOrdinal); STRCMP_CONTAINS(expectedMessage.asCharString(), failure.getMessage().asCharString()); @@ -88,29 +63,38 @@ TEST(MockFailureTest, noErrorFailureSomethingGoneWrong) TEST(MockFailureTest, unexpectedCallHappened) { - MockUnexpectedCallHappenedFailure failure(UtestShell::getCurrent(), "foobar", *list); + MockExpectedCallsListForTest expectedCalls; + MockActualCallsQueueForTest actualCalls; + MockUnexpectedCallHappenedFailure failure(UtestShell::getCurrent(), "foobar", expectedCalls, actualCalls); STRCMP_EQUAL("Mock Failure: Unexpected call to function: foobar\n" "\tEXPECTED calls that WERE NOT fulfilled:\n" "\t\t\n" "\tEXPECTED calls that WERE fulfilled:\n" + "\t\t\n" + "\tACTUAL calls that were expected (in call order):\n" "\t\t", failure.getMessage().asCharString()); } TEST(MockFailureTest, expectedCallDidNotHappen) { - call1->withName("foobar"); - call2->withName("world").withParameter("boo", 2).withParameter("hello", "world"); - call3->withName("haphaphap"); - call3->callWasMade(1); - addAllToList(); + MockExpectedCallsListForTest expectedCalls; + expectedCalls.addFunction("foobar"); + MockCheckedExpectedCall* expectedCall2 = expectedCalls.addFunction("world"); + expectedCall2->withParameter("boo", 2).withParameter("hello", "world"); + MockCheckedExpectedCall* expectedCall3 = expectedCalls.addFunction("haphaphap"); + + MockActualCallsQueueForTest actualCalls; + actualCalls.addCall(1, expectedCall3); - MockExpectedCallsNotFulfilledFailure failure(UtestShell::getCurrent(), *list); + MockExpectedCallsNotFulfilledFailure failure(UtestShell::getCurrent(), expectedCalls, actualCalls); STRCMP_EQUAL("Mock Failure: Expected call WAS NOT fulfilled.\n" "\tEXPECTED calls that WERE NOT fulfilled:\n" "\t\tfoobar -> no parameters (expected 1 call, but was called 0 times)\n" "\t\tworld -> int boo: <2 (0x2)>, const char* hello: (expected 1 call, but was called 0 times)\n" "\tEXPECTED calls that WERE fulfilled:\n" - "\t\thaphaphap -> no parameters (called 1 time)", failure.getMessage().asCharString()); + "\t\thaphaphap -> no parameters (called 1 time)\n" + "\tACTUAL calls that were expected (in call order):\n" + "\t\t(1) haphaphap -> no parameters", failure.getMessage().asCharString()); } TEST(MockFailureTest, MockUnexpectedNthAdditionalCallFailure) @@ -129,21 +113,28 @@ TEST(MockFailureTest, MockUnexpectedNthAdditionalCallFailure) TEST(MockFailureTest, MockUnexpectedInputParameterFailure) { - call1->withName("foo").withParameter("boo", 2); - call2->withName("foo").withParameter("boo", 3.3); - call3->withName("unrelated"); - addAllToList(); + MockExpectedCallsListForTest expectedCalls; + MockCheckedExpectedCall* expectedCall1 = expectedCalls.addFunction("foo"); + expectedCall1->withParameter("boo", 2); + MockCheckedExpectedCall* expectedCall2 = expectedCalls.addFunction("foo"); + expectedCall2->withParameter("boo", 3.3); + MockCheckedExpectedCall* expectedCall3 = expectedCalls.addFunction("unrelated"); + + MockActualCallsQueueForTest actualCalls; + actualCalls.addCall(1, expectedCall3); MockNamedValue actualParameter("bar"); actualParameter.setValue(2); - MockUnexpectedInputParameterFailure failure(UtestShell::getCurrent(), "foo", actualParameter, *list); + MockUnexpectedInputParameterFailure failure(UtestShell::getCurrent(), "foo", actualParameter, expectedCalls, actualCalls); STRCMP_EQUAL("Mock Failure: Unexpected parameter name to function \"foo\": bar\n" "\tEXPECTED calls that WERE NOT fulfilled related to function: foo\n" "\t\tfoo -> int boo: <2 (0x2)> (expected 1 call, but was called 0 times)\n" "\t\tfoo -> double boo: <3.3> (expected 1 call, but was called 0 times)\n" "\tEXPECTED calls that WERE fulfilled related to function: foo\n" "\t\t\n" + "\tACTUAL calls that were expected (in call order) for function: foo\n" + "\t\t\n" "\tACTUAL unexpected parameter passed to function: foo\n" "\t\tint bar: <2 (0x2)>", failure.getMessage().asCharString()); } @@ -152,66 +143,166 @@ TEST(MockFailureTest, MockUnexpectedOutputParameterFailure) { int out1; int out2; - call1->withName("foo").withOutputParameterReturning("boo", &out1, sizeof(out1)); - call2->withName("foo").withOutputParameterReturning("boo", &out2, sizeof(out2)); - call3->withName("unrelated"); - addAllToList(); + MockExpectedCallsListForTest expectedCalls; + MockCheckedExpectedCall* expectedCall1 = expectedCalls.addFunction("foo"); + expectedCall1->withOutputParameterReturning("boo", &out1, sizeof(out1)); + MockCheckedExpectedCall* expectedCall2 = expectedCalls.addFunction("foo"); + expectedCall2->withOutputParameterReturning("boo", &out2, sizeof(out2)); + expectedCalls.addFunction("unrelated"); + + MockActualCallsQueueForTest actualCalls; MockNamedValue actualParameter("bar"); actualParameter.setValue((void *)0x123); - MockUnexpectedOutputParameterFailure failure(UtestShell::getCurrent(), "foo", actualParameter, *list); + MockUnexpectedOutputParameterFailure failure(UtestShell::getCurrent(), "foo", actualParameter, expectedCalls, actualCalls); STRCMP_EQUAL("Mock Failure: Unexpected output parameter name to function \"foo\": bar\n" "\tEXPECTED calls that WERE NOT fulfilled related to function: foo\n" "\t\tfoo -> const void* boo: (expected 1 call, but was called 0 times)\n" "\t\tfoo -> const void* boo: (expected 1 call, but was called 0 times)\n" "\tEXPECTED calls that WERE fulfilled related to function: foo\n" "\t\t\n" + "\tACTUAL calls that were expected (in call order) for function: foo\n" + "\t\t\n" "\tACTUAL unexpected output parameter passed to function: foo\n" "\t\tvoid* bar", failure.getMessage().asCharString()); } TEST(MockFailureTest, MockUnexpectedParameterValueFailure) { - call1->withName("foo").withParameter("boo", 2); - call2->withName("foo").withParameter("boo", 10); - call3->withName("unrelated"); - addAllToList(); + MockExpectedCallsListForTest expectedCalls; + MockCheckedExpectedCall* expectedCall1 = expectedCalls.addFunction("foo"); + expectedCall1->withParameter("boo", 2); + MockCheckedExpectedCall* expectedCall2 = expectedCalls.addFunction("foo"); + expectedCall2->withParameter("boo", 10); + expectedCalls.addFunction("unrelated"); + + MockActualCallsQueueForTest actualCalls; + actualCalls.addCall(1, expectedCall1); MockNamedValue actualParameter("boo"); actualParameter.setValue(20); - MockUnexpectedInputParameterFailure failure(UtestShell::getCurrent(), "foo", actualParameter, *list); + MockUnexpectedInputParameterFailure failure(UtestShell::getCurrent(), "foo", actualParameter, expectedCalls, actualCalls); STRCMP_EQUAL("Mock Failure: Unexpected parameter value to parameter \"boo\" to function \"foo\": <20 (0x14)>\n" "\tEXPECTED calls that WERE NOT fulfilled related to function: foo\n" - "\t\tfoo -> int boo: <2 (0x2)> (expected 1 call, but was called 0 times)\n" "\t\tfoo -> int boo: <10 (0xa)> (expected 1 call, but was called 0 times)\n" "\tEXPECTED calls that WERE fulfilled related to function: foo\n" - "\t\t\n" + "\t\tfoo -> int boo: <2 (0x2)> (called 1 time)\n" + "\tACTUAL calls that were expected (in call order) for function: foo\n" + "\t\t(1) foo -> int boo: <2 (0x2)>\n" "\tACTUAL unexpected parameter passed to function: foo\n" "\t\tint boo: <20 (0x14)>", failure.getMessage().asCharString()); } -TEST(MockFailureTest, MockExpectedParameterDidntHappenFailure) +TEST(MockFailureTest, MockExpectedParameterDidntHappenFailure_AllParametersMissing) { - call1->withName("foo").withParameter("bar", 2).withParameter("boo", "str"); - call2->withName("foo").withParameter("bar", 10).withParameter("boo", "bleh"); - call2->callWasMade(1); - call2->inputParameterWasPassed("bar"); - call2->inputParameterWasPassed("boo"); - call3->withName("unrelated"); - addAllToList(); - - MockExpectedParameterDidntHappenFailure failure(UtestShell::getCurrent(), "foo", *list); + MockExpectedCallsListForTest expectedCalls; + MockCheckedExpectedCall* expectedCall1 = expectedCalls.addFunction("foo"); + expectedCall1->withParameter("bar", 2).withParameter("boo", "str"); + MockCheckedExpectedCall* expectedCall2 = expectedCalls.addFunction("foo"); + expectedCall2->withParameter("bar", 10).withParameter("boo", "bleh"); + MockCheckedExpectedCall* expectedCall3 = expectedCalls.addFunction("unrelated"); + MockCheckedExpectedCall* expectedCall4 = expectedCalls.addFunction("foo"); + expectedCall4->withParameter("bar", 10).withParameter("boo", "blih"); + + MockActualCallsQueueForTest actualCalls; + actualCalls.addCall(1, expectedCall4); + actualCalls.addCall(2, expectedCall3); + actualCalls.addCall(3, expectedCall2); + + MockExpectedParameterDidntHappenFailure failure(UtestShell::getCurrent(), "foo", expectedCalls, actualCalls); STRCMP_EQUAL("Mock Failure: Expected parameter for function \"foo\" did not happen.\n" "\tEXPECTED calls that WERE NOT fulfilled related to function: foo\n" "\t\tfoo -> int bar: <2 (0x2)>, const char* boo: (expected 1 call, but was called 0 times)\n" "\tEXPECTED calls that WERE fulfilled related to function: foo\n" "\t\tfoo -> int bar: <10 (0xa)>, const char* boo: (called 1 time)\n" + "\t\tfoo -> int bar: <10 (0xa)>, const char* boo: (called 1 time)\n" + "\tACTUAL calls that were expected (in call order) for function: foo\n" + "\t\t(1) foo -> int bar: <10 (0xa)>, const char* boo: \n" + "\t\t(3) foo -> int bar: <10 (0xa)>, const char* boo: \n" "\tMISSING parameters that didn't happen:\n" "\t\tint bar, const char* boo", failure.getMessage().asCharString()); } +TEST(MockFailureTest, MockExpectedParameterDidntHappenFailure_SomeParametersMissing) +{ + MockExpectedCallsListForTest expectedCalls; + MockCheckedExpectedCall* expectedCall1 = expectedCalls.addFunction("foo"); + expectedCall1->withParameter("bar", 2).withParameter("boo", "str1"); + MockCheckedExpectedCall* expectedCall2 = expectedCalls.addFunction("foo"); + expectedCall2->withParameter("bar", 10).withParameter("boo", "bleh"); + MockCheckedExpectedCall* expectedCall3 = expectedCalls.addFunction("unrelated"); + MockCheckedExpectedCall* expectedCall4 = expectedCalls.addFunction("foo"); + expectedCall4->withParameter("bar", 10).withParameter("boo", "blih"); + MockCheckedExpectedCall* expectedCall5 = expectedCalls.addFunction("foo"); + expectedCall5->withParameter("bar", 2).withParameter("baa", "str2"); + + MockActualCallsQueueForTest actualCalls; + actualCalls.addCall(1, expectedCall4); + actualCalls.addCall(2, expectedCall3); + actualCalls.addCall(3, expectedCall2); + + expectedCall1->inputParameterWasPassed("bar"); + expectedCall5->inputParameterWasPassed("bar"); + + MockExpectedParameterDidntHappenFailure failure(UtestShell::getCurrent(), "foo", expectedCalls, actualCalls); + STRCMP_EQUAL("Mock Failure: Expected parameter for function \"foo\" did not happen.\n" + "\tEXPECTED calls that WERE NOT fulfilled related to function: foo\n" + "\t\tfoo -> int bar: <2 (0x2)>, const char* boo: (expected 1 call, but was called 0 times)\n" + "\t\tfoo -> int bar: <2 (0x2)>, const char* baa: (expected 1 call, but was called 0 times)\n" + "\tEXPECTED calls that WERE fulfilled related to function: foo\n" + "\t\tfoo -> int bar: <10 (0xa)>, const char* boo: (called 1 time)\n" + "\t\tfoo -> int bar: <10 (0xa)>, const char* boo: (called 1 time)\n" + "\tACTUAL calls that were expected (in call order) for function: foo\n" + "\t\t(1) foo -> int bar: <10 (0xa)>, const char* boo: \n" + "\t\t(3) foo -> int bar: <10 (0xa)>, const char* boo: \n" + "\tMISSING parameters that didn't happen:\n" + "\t\tconst char* boo\n" + "\t\tconst char* baa", failure.getMessage().asCharString()); +} + +TEST(MockFailureTest, MockExpectedParameterDidntHappenFailure_WithOptionalCalls) +{ + MockExpectedCallsListForTest expectedCalls; + MockCheckedExpectedCall* expectedCall1 = expectedCalls.addFunction(2, 3, "foo"); + expectedCall1->withParameter("bar", 2).withParameter("boo", "str1"); + MockCheckedExpectedCall* expectedCall2 = expectedCalls.addFunction("foo"); + expectedCall2->withParameter("bar", 10).withParameter("boo", "bleh"); + MockCheckedExpectedCall* expectedCall3 = expectedCalls.addFunction("unrelated"); + MockCheckedExpectedCall* expectedCall4 = expectedCalls.addFunction("foo"); + expectedCall4->withParameter("bar", 10).withParameter("boo", "blih"); + MockCheckedExpectedCall* expectedCall5 = expectedCalls.addFunction("foo"); + expectedCall5->withParameter("bar", 2).withParameter("baa", "str2"); + + MockActualCallsQueueForTest actualCalls; + actualCalls.addCall(1, expectedCall1); + actualCalls.addCall(2, expectedCall4); + actualCalls.addCall(3, expectedCall3); + actualCalls.addCall(4, expectedCall2); + actualCalls.addCall(5, expectedCall1); + + expectedCall1->inputParameterWasPassed("bar"); + expectedCall5->inputParameterWasPassed("bar"); + + MockExpectedParameterDidntHappenFailure failure(UtestShell::getCurrent(), "foo", expectedCalls, actualCalls); + STRCMP_EQUAL("Mock Failure: Expected parameter for function \"foo\" did not happen.\n" + "\tEXPECTED calls that WERE NOT fulfilled related to function: foo\n" + "\t\tfoo -> int bar: <2 (0x2)>, const char* baa: (expected 1 call, but was called 0 times)\n" + "\tEXPECTED calls that WERE fulfilled related to function: foo\n" + "\t\tfoo -> int bar: <2 (0x2)>, const char* boo: (called 2 times)\n" + "\t\tfoo -> int bar: <10 (0xa)>, const char* boo: (called 1 time)\n" + "\t\tfoo -> int bar: <10 (0xa)>, const char* boo: (called 1 time)\n" + "\tACTUAL calls that were expected (in call order) for function: foo\n" + "\t\t(1) foo -> int bar: <2 (0x2)>, const char* boo: \n" + "\t\t(2) foo -> int bar: <10 (0xa)>, const char* boo: \n" + "\t\t(4) foo -> int bar: <10 (0xa)>, const char* boo: \n" + "\t\t(5) foo -> int bar: <2 (0x2)>, const char* boo: \n" + "\tMISSING parameters that didn't happen:\n" + "\t\tconst char* boo\n" + "\t\tconst char* baa", failure.getMessage().asCharString()); +} + TEST(MockFailureTest, MockNoWayToCompareCustomTypeFailure) { MockNoWayToCompareCustomTypeFailure failure(UtestShell::getCurrent(), "myType"); @@ -220,39 +311,49 @@ TEST(MockFailureTest, MockNoWayToCompareCustomTypeFailure) TEST(MockFailureTest, MockUnexpectedObjectFailure) { - call1->withName("foo").onObject((void*) 0x02); - call2->withName("foo").onObject((void*) 0x03); - call2->callWasMade(1); - call2->wasPassedToObject(); - call3->withName("unrelated"); - addAllToList(); - - MockUnexpectedObjectFailure failure(UtestShell::getCurrent(), "foo", (void*)0x1, *list); + MockExpectedCallsListForTest expectedCalls; + MockCheckedExpectedCall* expectedCall1 = expectedCalls.addFunction("foo"); + expectedCall1->onObject((void*) 0x02); + MockCheckedExpectedCall* expectedCall2 = expectedCalls.addFunction("foo"); + expectedCall2->onObject((void*) 0x03); + expectedCalls.addFunction("unrelated"); + + MockActualCallsQueueForTest actualCalls; + actualCalls.addCall(1, expectedCall2); + + MockUnexpectedObjectFailure failure(UtestShell::getCurrent(), "foo", (void*)0x1, expectedCalls, actualCalls); STRCMP_EQUAL(StringFromFormat ( "MockFailure: Function called on an unexpected object: foo\n" "\tActual object for call has address: <%p>\n" "\tEXPECTED calls that WERE NOT fulfilled related to function: foo\n" "\t\t(object address: %p)::foo -> no parameters (expected 1 call, but was called 0 times)\n" "\tEXPECTED calls that WERE fulfilled related to function: foo\n" - "\t\t(object address: %p)::foo -> no parameters (called 1 time)", - (void*) 0x01, (void*) 0x02, (void*) 0x03).asCharString(), failure.getMessage().asCharString()); + "\t\t(object address: %p)::foo -> no parameters (called 1 time)\n" + "\tACTUAL calls that were expected (in call order) for function: foo\n" + "\t\t(1) (object address: %p)::foo -> no parameters", + (void*) 0x01, (void*) 0x02, (void*) 0x03, (void*) 0x03).asCharString(), failure.getMessage().asCharString()); } TEST(MockFailureTest, MockExpectedObjectDidntHappenFailure) { - call1->withName("foo").onObject((void*) 0x02); - call2->withName("foo").onObject((void*) 0x03); - call2->callWasMade(1); - call2->wasPassedToObject(); - call3->withName("unrelated"); - addAllToList(); - - MockExpectedObjectDidntHappenFailure failure(UtestShell::getCurrent(), "foo", *list); + MockExpectedCallsListForTest expectedCalls; + MockCheckedExpectedCall* expectedCall1 = expectedCalls.addFunction("foo"); + expectedCall1->onObject((void*) 0x02); + MockCheckedExpectedCall* expectedCall2 = expectedCalls.addFunction("foo"); + expectedCall2->onObject((void*) 0x03); + expectedCalls.addFunction("unrelated"); + + MockActualCallsQueueForTest actualCalls; + actualCalls.addCall(1, expectedCall2); + + MockExpectedObjectDidntHappenFailure failure(UtestShell::getCurrent(), "foo", expectedCalls, actualCalls); STRCMP_EQUAL(StringFromFormat( "Mock Failure: Expected call on object for function \"foo\" but it did not happen.\n" "\tEXPECTED calls that WERE NOT fulfilled related to function: foo\n" "\t\t(object address: %p)::foo -> no parameters (expected 1 call, but was called 0 times)\n" "\tEXPECTED calls that WERE fulfilled related to function: foo\n" - "\t\t(object address: %p)::foo -> no parameters (called 1 time)", - (void*) 0x2, (void*) 0x3).asCharString(), failure.getMessage().asCharString()); + "\t\t(object address: %p)::foo -> no parameters (called 1 time)\n" + "\tACTUAL calls that were expected (in call order) for function: foo\n" + "\t\t(1) (object address: %p)::foo -> no parameters", + (void*) 0x2, (void*) 0x3, (void*) 0x3).asCharString(), failure.getMessage().asCharString()); } diff --git a/tests/CppUTestExt/MockHierarchyTest.cpp b/tests/CppUTestExt/MockHierarchyTest.cpp index cc771c8e1..620ea6479 100644 --- a/tests/CppUTestExt/MockHierarchyTest.cpp +++ b/tests/CppUTestExt/MockHierarchyTest.cpp @@ -96,7 +96,8 @@ TEST(MockHierarchyTest, checkExpectationsWorksHierarchically) MockExpectedCallsListForTest expectations; expectations.addFunction("first::foobar"); expectations.addFunction("second::helloworld"); - MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations); + MockActualCallsQueueForTest actualCalls; + MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations, actualCalls); mock("first").expectOneCall("foobar"); mock("second").expectOneCall("helloworld"); @@ -128,7 +129,8 @@ TEST(MockHierarchyTest, checkExpectationsWorksHierarchicallyForLastCallNotFinish MockExpectedCallsListForTest expectations; expectations.addFunction("first::foobar")->withParameter("boo", 1); - MockExpectedParameterDidntHappenFailure expectedFailure(mockFailureTest(), "first::foobar", expectations); + MockActualCallsQueueForTest actualCalls; + MockExpectedParameterDidntHappenFailure expectedFailure(mockFailureTest(), "first::foobar", expectations, actualCalls); mock("first").expectOneCall("foobar").withParameter("boo", 1); mock("first").actualCall("foobar"); @@ -144,7 +146,8 @@ TEST(MockHierarchyTest, reporterIsInheritedInHierarchicalMocks) mock("differentScope").actualCall("foobar"); - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "differentScope::foobar", expectations); + MockActualCallsQueueForTest actualCalls; + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "differentScope::foobar", expectations, actualCalls); CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); } diff --git a/tests/CppUTestExt/MockParameterTest.cpp b/tests/CppUTestExt/MockParameterTest.cpp index 9d4d728e2..dbeca85b3 100644 --- a/tests/CppUTestExt/MockParameterTest.cpp +++ b/tests/CppUTestExt/MockParameterTest.cpp @@ -136,7 +136,8 @@ TEST(MockParameterTest, longAndUnsignedLongWithSameBitRepresentationShouldNotBeT expectations.addFunction("foo")->withParameter("parameter", (long)-1); MockNamedValue parameter("parameter"); parameter.setValue((unsigned long)-1); - MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations); + MockActualCallsQueueForTest actualCalls; + MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations, actualCalls); mock().expectOneCall("foo").withParameter("parameter", (long)-1); mock().actualCall("foo").withParameter("parameter", (unsigned long)-1); @@ -152,7 +153,8 @@ TEST(MockParameterTest, unsignedLongAndLongWithSameBitRepresentationShouldnotBeT expectations.addFunction("foo")->withParameter("parameter", (unsigned long)-1); MockNamedValue parameter("parameter"); parameter.setValue((long)-1); - MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations); + MockActualCallsQueueForTest actualCalls; + MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations, actualCalls); mock().expectOneCall("foo").withParameter("parameter", (unsigned long)-1); mock().actualCall("foo").withParameter("parameter", (long)-1); @@ -220,7 +222,8 @@ TEST(MockParameterTest, expectOneMemBufferParameterAndValueFailsDueToContents) expectations.addFunction("foo")->withParameter("parameter", memBuffer1, sizeof(memBuffer1)); MockNamedValue parameter("parameter"); parameter.setMemoryBuffer( memBuffer2, sizeof(memBuffer2) ); - MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations); + MockActualCallsQueueForTest actualCalls; + MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations, actualCalls); mock().expectOneCall("foo").withParameter("parameter", memBuffer1, sizeof(memBuffer1)); mock().actualCall("foo").withParameter("parameter", memBuffer2, sizeof(memBuffer2)); @@ -239,7 +242,8 @@ TEST(MockParameterTest, expectOneMemBufferParameterAndValueFailsDueToSize) expectations.addFunction("foo")->withParameter("parameter", memBuffer1, sizeof(memBuffer1)); MockNamedValue parameter("parameter"); parameter.setMemoryBuffer( memBuffer2, sizeof(memBuffer2) ); - MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations); + MockActualCallsQueueForTest actualCalls; + MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations, actualCalls); mock().expectOneCall("foo").withParameter("parameter", memBuffer1, sizeof(memBuffer1)); mock().actualCall("foo").withParameter("parameter", memBuffer2, sizeof(memBuffer2)); @@ -255,7 +259,8 @@ TEST(MockParameterTest, expectOneStringParameterAndValueFails) expectations.addFunction("foo")->withParameter("parameter", "string"); MockNamedValue parameter("parameter"); parameter.setValue("different"); - MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations); + MockActualCallsQueueForTest actualCalls; + MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations, actualCalls); mock().expectOneCall("foo").withParameter("parameter", "string"); mock().actualCall("foo").withParameter("parameter", "different"); @@ -272,7 +277,8 @@ TEST(MockParameterTest, expectOneUnsignedIntegerParameterAndFailsDueToParameterN expectations.addFunction("foo")->withParameter("parameter", value); MockNamedValue parameter("different"); parameter.setValue(value); - MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations); + MockActualCallsQueueForTest actualCalls; + MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations, actualCalls); mock().expectOneCall("foo").withParameter("parameter", value); mock().actualCall("foo").withParameter("different", value); @@ -288,7 +294,8 @@ TEST(MockParameterTest, expectOneIntegerParameterAndFailsDueToParameterName) expectations.addFunction("foo")->withParameter("parameter", 10); MockNamedValue parameter("different"); parameter.setValue(10); - MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations); + MockActualCallsQueueForTest actualCalls; + MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations, actualCalls); mock().expectOneCall("foo").withParameter("parameter", 10); mock().actualCall("foo").withParameter("different", 10); @@ -306,7 +313,8 @@ TEST(MockParameterTest, expectOneUnsignedIntegerParameterAndFailsDueToValue) expectations.addFunction("foo")->withParameter("parameter", expected_value); MockNamedValue parameter("parameter"); parameter.setValue(actual_value); - MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations); + MockActualCallsQueueForTest actualCalls; + MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations, actualCalls); mock().expectOneCall("foo").withParameter("parameter", expected_value); mock().actualCall("foo").withParameter("parameter", actual_value); @@ -322,7 +330,8 @@ TEST(MockParameterTest, expectOneIntegerParameterAndFailsDueToValue) expectations.addFunction("foo")->withParameter("parameter", 10); MockNamedValue parameter("parameter"); parameter.setValue(8); - MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations); + MockActualCallsQueueForTest actualCalls; + MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations, actualCalls); mock().expectOneCall("foo").withParameter("parameter", 10); mock().actualCall("foo").withParameter("parameter", 8); @@ -338,7 +347,8 @@ TEST(MockParameterTest, expectOneIntegerParameterAndFailsDueToTypes) expectations.addFunction("foo")->withParameter("parameter", 10); MockNamedValue parameter("parameter"); parameter.setValue("heh"); - MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations); + MockActualCallsQueueForTest actualCalls; + MockUnexpectedInputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations, actualCalls); mock().expectOneCall("foo").withParameter("parameter", 10); mock().actualCall("foo").withParameter("parameter", "heh"); @@ -383,7 +393,8 @@ TEST(MockParameterTest, calledWithoutParameters) MockExpectedCallsListForTest expectations; expectations.addFunction("foo")->withParameter("p1", 1); - MockExpectedParameterDidntHappenFailure expectedFailure(mockFailureTest(), "foo", expectations); + MockActualCallsQueueForTest actualCalls; + MockExpectedParameterDidntHappenFailure expectedFailure(mockFailureTest(), "foo", expectations, actualCalls); mock().expectOneCall("foo").withParameter("p1", 1); mock().actualCall("foo"); @@ -414,7 +425,8 @@ TEST(MockParameterTest, ignoreOtherParametersButExpectedParameterDidntHappen) MockExpectedCallsListForTest expectations; expectations.addFunction("foo")->withParameter("p1", 1).ignoreOtherParameters(); - MockExpectedParameterDidntHappenFailure expectedFailure(mockFailureTest(), "foo", expectations); + MockActualCallsQueueForTest actualCalls; + MockExpectedParameterDidntHappenFailure expectedFailure(mockFailureTest(), "foo", expectations, actualCalls); mock().expectOneCall("foo").withParameter("p1", 1).ignoreOtherParameters(); mock().actualCall("foo").withParameter("p2", 2).withParameter("p3", 3).withParameter("p4", 4); @@ -445,7 +457,8 @@ TEST(MockParameterTest, ignoreOtherParametersMultipleCallsButOneDidntHappen) call->finalizeActualCallMatch(); call->ignoreOtherParameters(); expectations.addFunction("boo")->ignoreOtherParameters(); - MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations); + MockActualCallsQueueForTest actualCalls; + MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations, actualCalls); mock().expectOneCall("boo").ignoreOtherParameters(); mock().expectOneCall("boo").ignoreOtherParameters(); @@ -461,7 +474,8 @@ TEST(MockParameterTest, newCallStartsWhileNotAllParametersWerePassed) MockExpectedCallsListForTest expectations; expectations.addFunction("foo")->withParameter("p1", 1); - MockExpectedParameterDidntHappenFailure expectedFailure(mockFailureTest(), "foo", expectations); + MockActualCallsQueueForTest actualCalls; + MockExpectedParameterDidntHappenFailure expectedFailure(mockFailureTest(), "foo", expectations, actualCalls); mock().expectOneCall("foo").withParameter("p1", 1); mock().actualCall("foo"); @@ -492,7 +506,8 @@ TEST(MockParameterTest, noActualCallForOutputParameter) mock().expectOneCall("foo").withOutputParameterReturning("output", &output, sizeof(output)); expectations.addFunction("foo")->withOutputParameterReturning("output", &output, sizeof(output)); - MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations); + MockActualCallsQueueForTest actualCalls; + MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations, actualCalls); mock().checkExpectations(); CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); @@ -510,7 +525,8 @@ TEST(MockParameterTest, unexpectedOutputParameter) expectations.addFunction("foo"); MockNamedValue parameter("parameterName"); parameter.setValue(¶m); - MockUnexpectedOutputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations); + MockActualCallsQueueForTest actualCalls; + MockUnexpectedOutputParameterFailure expectedFailure(mockFailureTest(), "foo", parameter, expectations, actualCalls); mock().checkExpectations(); CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); @@ -526,7 +542,8 @@ TEST(MockParameterTest, outputParameterMissing) mock().actualCall("foo"); expectations.addFunction("foo")->withOutputParameterReturning("output", &output, sizeof(output)); - MockExpectedParameterDidntHappenFailure expectedFailure(mockFailureTest(), "foo", expectations); + MockActualCallsQueueForTest actualCalls; + MockExpectedParameterDidntHappenFailure expectedFailure(mockFailureTest(), "foo", expectations, actualCalls); mock().checkExpectations(); CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); diff --git a/tests/CppUTestExt/MockPluginTest.cpp b/tests/CppUTestExt/MockPluginTest.cpp index da0c628ec..0ded4a75e 100644 --- a/tests/CppUTestExt/MockPluginTest.cpp +++ b/tests/CppUTestExt/MockPluginTest.cpp @@ -59,7 +59,8 @@ TEST(MockPlugin, checkExpectationsAndClearAtEnd) MockExpectedCallsListForTest expectations; expectations.addFunction("foobar"); - MockExpectedCallsNotFulfilledFailure expectedFailure(test, expectations); + MockActualCallsQueueForTest actualCalls; + MockExpectedCallsNotFulfilledFailure expectedFailure(test, expectations, actualCalls); mock().expectOneCall("foobar"); @@ -76,7 +77,8 @@ TEST(MockPlugin, checkExpectationsWorksAlsoWithHierachicalObjects) MockExpectedCallsListForTest expectations; expectations.addFunction("differentScope::foobar")->onObject((void*) 1); - MockExpectedObjectDidntHappenFailure expectedFailure(test, "differentScope::foobar", expectations); + MockActualCallsQueueForTest actualCalls; + MockExpectedObjectDidntHappenFailure expectedFailure(test, "differentScope::foobar", expectations, actualCalls); mock("differentScope").expectOneCall("foobar").onObject((void*) 1); mock("differentScope").actualCall("foobar"); diff --git a/tests/CppUTestExt/MockStrictOrderTest.cpp b/tests/CppUTestExt/MockStrictOrderTest.cpp index 8c05f7856..1e6a57e82 100644 --- a/tests/CppUTestExt/MockStrictOrderTest.cpp +++ b/tests/CppUTestExt/MockStrictOrderTest.cpp @@ -69,7 +69,8 @@ TEST(MockStrictOrderTest, orderViolated) expectations.addFunction("foo1", 1)->callWasMade(1); expectations.addFunction("foo1", 2)->callWasMade(3); expectations.addFunction("foo2", 3)->callWasMade(2); - MockCallOrderFailure expectedFailure(mockFailureTest(), expectations); + MockActualCallsQueueForTest actualCalls; + MockCallOrderFailure expectedFailure(mockFailureTest(), expectations, actualCalls); mock().expectOneCall("foo1"); mock().expectOneCall("foo1"); @@ -91,7 +92,8 @@ TEST(MockStrictOrderTest, orderViolatedWorksHierarchically) MockExpectedCallsListForTest expectations; expectations.addFunction("foo::foo1", 1)->callWasMade(2); expectations.addFunction("foo::foo2", 2)->callWasMade(1); - MockCallOrderFailure expectedFailure(mockFailureTest(), expectations); + MockActualCallsQueueForTest actualCalls; + MockCallOrderFailure expectedFailure(mockFailureTest(), expectations, actualCalls); mock("bla").expectOneCall("foo1"); mock("foo").expectOneCall("foo1"); @@ -115,7 +117,8 @@ TEST(MockStrictOrderTest, orderViolatedWorksWithExtraUnexpectedCall) MockExpectedCallsListForTest expectations; expectations.addFunction("foo::foo1", 1)->callWasMade(2); expectations.addFunction("foo::foo2", 2)->callWasMade(1); - MockCallOrderFailure expectedFailure(mockFailureTest(), expectations); + MockActualCallsQueueForTest actualCalls; + MockCallOrderFailure expectedFailure(mockFailureTest(), expectations, actualCalls); mock("bla").expectOneCall("foo1"); mock("foo").expectOneCall("foo1"); @@ -139,7 +142,8 @@ TEST(MockStrictOrderTest, orderViolatedWithinAScope) MockExpectedCallsListForTest expectations; expectations.addFunction("scope::foo1", 1)->callWasMade(2); expectations.addFunction("scope::foo2", 2)->callWasMade(1); - MockCallOrderFailure expectedFailure(mockFailureTest(), expectations); + MockActualCallsQueueForTest actualCalls; + MockCallOrderFailure expectedFailure(mockFailureTest(), expectations, actualCalls); mock("scope").expectOneCall("foo1"); mock("scope").expectOneCall("foo2"); diff --git a/tests/CppUTestExt/MockSupportTest.cpp b/tests/CppUTestExt/MockSupportTest.cpp index 4c616dd5d..e655e6479 100644 --- a/tests/CppUTestExt/MockSupportTest.cpp +++ b/tests/CppUTestExt/MockSupportTest.cpp @@ -166,8 +166,9 @@ TEST_GROUP(MockSupportTestWithFixture) static void CHECK_EXPECTED_MOCK_FAILURE_LOCATION_failedTestMethod_() { - MockExpectedCallsList list; - MockUnexpectedCallHappenedFailure expectedFailure(UtestShell::getCurrent(), "unexpected", list); + MockExpectedCallsList expectedCalls; + MockActualCallsQueue actualCalls(false); + MockUnexpectedCallHappenedFailure expectedFailure(UtestShell::getCurrent(), "unexpected", expectedCalls, actualCalls); mock().actualCall("boo"); CHECK_EXPECTED_MOCK_FAILURE_LOCATION(expectedFailure, "file", 1); } From b3a20a45aeda63320790ec849508d51219ecdfbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Mon, 4 Jul 2016 22:05:39 +0200 Subject: [PATCH 19/26] Fixed compilation for Dos platform. --- platforms/Dos/sources.mk | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/platforms/Dos/sources.mk b/platforms/Dos/sources.mk index 0c44762e0..62730c1a8 100644 --- a/platforms/Dos/sources.mk +++ b/platforms/Dos/sources.mk @@ -34,6 +34,7 @@ CPPUX_OBJECTS := \ $(CPPUTEST_HOME)/src/CppUTestExt/OrderedTest.o \ $(CPPUTEST_HOME)/src/CppUTestExt/MemoryReportFormatter.o \ $(CPPUTEST_HOME)/src/CppUTestExt/MockExpectedCallsList.o \ + $(CPPUTEST_HOME)/src/CppUTestExt/MockActualCallsQueue.o \ $(CPPUTEST_HOME)/src/CppUTestExt/MockSupport.o \ CPPU1_OBJECTS := \ @@ -107,13 +108,14 @@ CPPU6_OBJECTS := \ $(CPPUTEST_HOME)/tests/CppUTestExt/AllTests.o \ $(CPPUTEST_HOME)/tests/CppUTestExt/MockFailureReporterForTest.o \ $(CPPUTEST_HOME)/tests/CppUTestExt/ExpectedFunctionsListTest.o \ + $(CPPUTEST_HOME)/tests/CppUTestExt/ActualCallsQueueTest.o \ $(CPPUTEST_HOME)/tests/CppUTestExt/MockCallTest.o \ $(CPPUTEST_HOME)/tests/CppUTestExt/MockComparatorCopierTest.o \ $(CPPUTEST_HOME)/tests/CppUTestExt/MockHierarchyTest.o \ $(CPPUTEST_HOME)/tests/CppUTestExt/MockParameterTest.o \ $(CPPUTEST_HOME)/tests/TestUTestStringMacro.o \ - CPPU7_OBJECTS := \ +CPPU7_OBJECTS := \ $(CPPUTEST_HOME)/tests/CppUTestExt/AllTests.o \ $(CPPUTEST_HOME)/tests/CppUTestExt/MockFailureReporterForTest.o \ $(CPPUTEST_HOME)/tests/CppUTestExt/IEEE754PluginTest.o \ From eb3aae13db1d5e473d4bf0babc6cbab8e2c623f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Tue, 5 Jul 2016 21:13:43 +0200 Subject: [PATCH 20/26] Implemented a log of actual calls in MockSupport, such that mock failures can now report again the actual calls that actually happened in call order. --- include/CppUTestExt/MockSupport.h | 13 +- src/CppUTestExt/MockSupport.cpp | 163 ++++++++++++++-------- tests/CppUTestExt/MockCallTest.cpp | 112 +++++++++------ tests/CppUTestExt/MockParameterTest.cpp | 17 +-- tests/CppUTestExt/MockStrictOrderTest.cpp | 75 ++++++---- 5 files changed, 248 insertions(+), 132 deletions(-) diff --git a/include/CppUTestExt/MockSupport.h b/include/CppUTestExt/MockSupport.h index cf3b73f41..e962163d4 100755 --- a/include/CppUTestExt/MockSupport.h +++ b/include/CppUTestExt/MockSupport.h @@ -131,7 +131,7 @@ class MockSupport protected: MockSupport* clone(const SimpleString& mockName); - virtual MockCheckedActualCall *createActualFunctionCall(); + virtual MockCheckedActualCall *createActualCall(); virtual void failTest(MockFailure& failure); void countCheck(); @@ -145,7 +145,7 @@ class MockSupport MockExpectedCallsList expectations_; bool ignoreOtherCalls_; bool enabled_; - MockCheckedActualCall *lastActualFunctionCall_; + MockCheckedActualCall *currentActualCall_; MockNamedValueComparatorsAndCopiersRepository comparatorsAndCopiersRepository_; MockNamedValueList data_; const SimpleString mockName_; @@ -153,7 +153,10 @@ class MockSupport bool tracing_; void checkExpectationsOfLastActualCall(); - bool wasLastActualCallFulfilled(); + void checkExpectationsOfLastActualCallsInAllScopes(); + bool isLastActualCallFulfilled(); + bool shallFailWithExpectedCallsNotFulfilled(); + void failTestWithExpectedCallsNotFulfilled(); void failTestWithOutOfOrderCalls(); void failTestWithStrictOrderingIncompatibleWithOptionalCalls(); @@ -163,10 +166,12 @@ class MockSupport MockSupport* getMockSupport(MockNamedValueListNode* node); bool callIsIgnored(const SimpleString& functionName); - bool hasCallsOutOfOrder(); + bool hasCallsOutOfOrderInAnyScope(); SimpleString appendScopeToName(const SimpleString& functionName); + void addExpectedCallsForAllScopes(MockExpectedCallsList& expectedCalls); + void addActualCallsForAllScopes(MockActualCallsQueue& expectedCalls); }; #endif diff --git a/src/CppUTestExt/MockSupport.cpp b/src/CppUTestExt/MockSupport.cpp index d1fbd0940..de1213570 100755 --- a/src/CppUTestExt/MockSupport.cpp +++ b/src/CppUTestExt/MockSupport.cpp @@ -44,8 +44,8 @@ MockSupport& mock(const SimpleString& mockName, MockFailureReporter* failureRepo } MockSupport::MockSupport(const SimpleString& mockName) - : actualCallOrder_(0), expectedCallOrder_(0), strictOrdering_(false), standardReporter_(&defaultReporter_), ignoreOtherCalls_(false), - enabled_(true), lastActualFunctionCall_(NULL), mockName_(mockName), actualCalls_(true), tracing_(false) + : actualCallOrder_(0), expectedCallOrder_(0), strictOrdering_(false), standardReporter_(&defaultReporter_), ignoreOtherCalls_(false), + enabled_(true), currentActualCall_(NULL), mockName_(mockName), actualCalls_(true), tracing_(false) { setActiveReporter(NULL); } @@ -63,8 +63,8 @@ void MockSupport::setMockFailureStandardReporter(MockFailureReporter* reporter) { standardReporter_ = (reporter != NULL) ? reporter : &defaultReporter_; - if (lastActualFunctionCall_) - lastActualFunctionCall_->setMockFailureReporter(standardReporter_); + if (currentActualCall_) + currentActualCall_->setMockFailureReporter(standardReporter_); for (MockNamedValueListNode* p = data_.begin(); p; p = p->next()) if (getMockSupport(p)) getMockSupport(p)->setMockFailureStandardReporter(standardReporter_); @@ -113,13 +113,16 @@ void MockSupport::removeAllComparatorsAndCopiers() void MockSupport::clear() { - delete lastActualFunctionCall_; - lastActualFunctionCall_ = NULL; + if (currentActualCall_) { + delete currentActualCall_; + currentActualCall_ = NULL; + } tracing_ = false; MockActualCallTrace::instance().clear(); expectations_.deleteAllExpectationsAndClearList(); + actualCalls_.clear(); ignoreOtherCalls_ = false; enabled_ = true; actualCallOrder_ = 0; @@ -216,10 +219,10 @@ MockExpectedCall& MockSupport::expectRangeOfCalls(unsigned int minCalls, unsigne return *call; } -MockCheckedActualCall* MockSupport::createActualFunctionCall() +MockCheckedActualCall* MockSupport::createActualCall() { - lastActualFunctionCall_ = new MockCheckedActualCall(++actualCallOrder_, activeReporter_, *this); - return lastActualFunctionCall_; + currentActualCall_ = new MockCheckedActualCall(++actualCallOrder_, activeReporter_, *this); + return currentActualCall_; } bool MockSupport::callIsIgnored(const SimpleString& functionName) @@ -231,21 +234,24 @@ MockActualCall& MockSupport::actualCall(const SimpleString& functionName) { const SimpleString scopeFunctionName = appendScopeToName(functionName); - if (lastActualFunctionCall_) { - lastActualFunctionCall_->checkExpectations(); - delete lastActualFunctionCall_; - lastActualFunctionCall_ = NULL; + if (currentActualCall_) { + currentActualCall_->checkExpectations(); + if (currentActualCall_->isFulfilled()) { + actualCalls_.pushBack(currentActualCall_); + } else { + delete currentActualCall_; + } + currentActualCall_ = NULL; } if (!enabled_) return MockIgnoredActualCall::instance(); if (tracing_) return MockActualCallTrace::instance().withCallOrder(++actualCallOrder_).withName(scopeFunctionName); - if (callIsIgnored(scopeFunctionName)) { return MockIgnoredActualCall::instance(); } - MockCheckedActualCall* call = createActualFunctionCall(); + MockCheckedActualCall* call = createActualCall(); call->withName(scopeFunctionName); return *call; } @@ -289,49 +295,82 @@ const char* MockSupport::getTraceOutput() bool MockSupport::expectedCallsLeft() { - int callsLeft = expectations_.hasUnfulfilledExpectations(); + if (expectations_.hasUnfulfilledExpectations()) { + return true; + } - for (MockNamedValueListNode* p = data_.begin(); p; p = p->next()) - if (getMockSupport(p)) callsLeft += getMockSupport(p)->expectedCallsLeft(); + for (MockNamedValueListNode* p = data_.begin(); p; p = p->next()) { + if (getMockSupport(p) && getMockSupport(p)->expectedCallsLeft()) { + return true; + } + } + + return false; +} - return callsLeft != 0; +bool MockSupport::isLastActualCallFulfilled() +{ + return !currentActualCall_ || currentActualCall_->isFulfilled(); } -bool MockSupport::wasLastActualCallFulfilled() +bool MockSupport::shallFailWithExpectedCallsNotFulfilled() { - if (lastActualFunctionCall_ && !lastActualFunctionCall_->isFulfilled()) - return false; + if (isLastActualCallFulfilled() && expectations_.hasUnfulfilledExpectations()) { + return true; + } - for (MockNamedValueListNode* p = data_.begin(); p; p = p->next()) - if (getMockSupport(p) && !getMockSupport(p)->wasLastActualCallFulfilled()) - return false; + for (MockNamedValueListNode* p = data_.begin(); p; p = p->next()) { + if (getMockSupport(p) && getMockSupport(p)->shallFailWithExpectedCallsNotFulfilled()) { + return true; + } + } - return true; + return false; +} + +void MockSupport::addExpectedCallsForAllScopes(MockExpectedCallsList& expectedCalls) +{ + expectedCalls.addExpectations(expectations_); + + for (MockNamedValueListNode *p = data_.begin();p;p = p->next()) { + if (getMockSupport(p)) { + expectedCalls.addExpectations(getMockSupport(p)->expectations_); + } + } +} + +void MockSupport::addActualCallsForAllScopes(MockActualCallsQueue& actualCalls) +{ + actualCalls.pushBackAll(actualCalls_); + + for (MockNamedValueListNode *p = data_.begin();p;p = p->next()) { + if (getMockSupport(p)) { + actualCalls.pushBackAll(getMockSupport(p)->actualCalls_); + } + } } void MockSupport::failTestWithExpectedCallsNotFulfilled() { - MockExpectedCallsList expectationsList; - expectationsList.addExpectations(expectations_); + MockExpectedCallsList expectedCallsForAllScopes; + addExpectedCallsForAllScopes(expectedCallsForAllScopes); - for(MockNamedValueListNode *p = data_.begin();p;p = p->next()) - if(getMockSupport(p)) - expectationsList.addExpectations(getMockSupport(p)->expectations_); + MockActualCallsQueue actualCallsForAllScopes(false); + addActualCallsForAllScopes(actualCallsForAllScopes); - MockExpectedCallsNotFulfilledFailure failure(activeReporter_->getTestToFail(), expectationsList, actualCalls_); + MockExpectedCallsNotFulfilledFailure failure(activeReporter_->getTestToFail(), expectedCallsForAllScopes, actualCallsForAllScopes); failTest(failure); } void MockSupport::failTestWithOutOfOrderCalls() { - MockExpectedCallsList expectationsList; - expectationsList.addExpectations(expectations_); + MockExpectedCallsList expectedCallsForAllScopes; + addExpectedCallsForAllScopes(expectedCallsForAllScopes); - for(MockNamedValueListNode *p = data_.begin();p;p = p->next()) - if(getMockSupport(p)) - expectationsList.addExpectations(getMockSupport(p)->expectations_); + MockActualCallsQueue actualCallsForAllScopes(false); + addActualCallsForAllScopes(actualCallsForAllScopes); - MockCallOrderFailure failure(activeReporter_->getTestToFail(), expectationsList, actualCalls_); + MockCallOrderFailure failure(activeReporter_->getTestToFail(), expectedCallsForAllScopes, actualCallsForAllScopes); failTest(failure); } @@ -348,40 +387,54 @@ void MockSupport::countCheck() void MockSupport::checkExpectationsOfLastActualCall() { - if(lastActualFunctionCall_) - lastActualFunctionCall_->checkExpectations(); + if (currentActualCall_) { + currentActualCall_->checkExpectations(); + if (currentActualCall_->isFulfilled()) { + actualCalls_.pushBack(currentActualCall_); + currentActualCall_ = NULL; + } + } +} - for(MockNamedValueListNode *p = data_.begin();p;p = p->next()) - if(getMockSupport(p) && getMockSupport(p)->lastActualFunctionCall_) - getMockSupport(p)->lastActualFunctionCall_->checkExpectations(); +void MockSupport::checkExpectationsOfLastActualCallsInAllScopes() +{ + checkExpectationsOfLastActualCall(); + + for (MockNamedValueListNode *p = data_.begin();p;p = p->next()) { + if (getMockSupport(p)) { + getMockSupport(p)->checkExpectationsOfLastActualCallsInAllScopes(); + } + } } -bool MockSupport::hasCallsOutOfOrder() +bool MockSupport::hasCallsOutOfOrderInAnyScope() { - if (expectations_.hasCallsOutOfOrder()) - { + if (expectations_.hasCallsOutOfOrder()) { return true; } - for (MockNamedValueListNode* p = data_.begin(); p; p = p->next()) - if (getMockSupport(p) && getMockSupport(p)->hasCallsOutOfOrder()) - { + + for (MockNamedValueListNode* p = data_.begin(); p; p = p->next()) { + if (getMockSupport(p) && getMockSupport(p)->hasCallsOutOfOrderInAnyScope()) { return true; } + } + return false; } void MockSupport::checkExpectations() { - checkExpectationsOfLastActualCall(); + checkExpectationsOfLastActualCallsInAllScopes(); - if (wasLastActualCallFulfilled() && expectedCallsLeft()) + if (shallFailWithExpectedCallsNotFulfilled()) { failTestWithExpectedCallsNotFulfilled(); + } - if (hasCallsOutOfOrder()) + if (hasCallsOutOfOrderInAnyScope()) { failTestWithOutOfOrderCalls(); + } } - bool MockSupport::hasData(const SimpleString& name) { return data_.getValueByName(name) != NULL; @@ -499,7 +552,7 @@ MockSupport* MockSupport::getMockSupport(MockNamedValueListNode* node) MockNamedValue MockSupport::returnValue() { - if (lastActualFunctionCall_) return lastActualFunctionCall_->returnValue(); + if (currentActualCall_) return currentActualCall_->returnValue(); return MockNamedValue(""); } @@ -635,7 +688,7 @@ void (*MockSupport::functionPointerReturnValue())() bool MockSupport::hasReturnValue() { - if (lastActualFunctionCall_) return lastActualFunctionCall_->hasReturnValue(); + if (currentActualCall_) return currentActualCall_->hasReturnValue(); return false; } diff --git a/tests/CppUTestExt/MockCallTest.cpp b/tests/CppUTestExt/MockCallTest.cpp index ad87a15a6..3b583ed4b 100644 --- a/tests/CppUTestExt/MockCallTest.cpp +++ b/tests/CppUTestExt/MockCallTest.cpp @@ -185,11 +185,15 @@ TEST(MockCallTest, expectOneCallHoweverMultipleHappened) { MockFailureReporterInstaller failureReporterInstaller; - MockExpectedCallsListForTest expectations; - expectations.addFunction("foo")->callWasMade(1); - expectations.addFunction("foo")->callWasMade(2); + MockExpectedCallsListForTest expectedCalls; + MockCheckedExpectedCall* expectedCall1 = expectedCalls.addFunction("foo"); + MockCheckedExpectedCall* expectedCall2 = expectedCalls.addFunction("foo"); + MockActualCallsQueueForTest actualCalls; - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "foo", expectations, actualCalls); + actualCalls.addCall(1, expectedCall1); + actualCalls.addCall(2, expectedCall2); + + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "foo", expectedCalls, actualCalls); mock().expectOneCall("foo"); mock().expectOneCall("foo"); @@ -219,11 +223,14 @@ TEST(MockCallTest, expectNoCallDoesntInfluenceExpectOneCall) { MockFailureReporterInstaller failureReporterInstaller; - MockExpectedCallsListForTest expectations; - expectations.addFunction(0, 0, "lazy"); - expectations.addFunction("influence", MockCheckedExpectedCall::NO_EXPECTED_CALL_ORDER)->callWasMade(1); + MockExpectedCallsListForTest expectedCalls; + expectedCalls.addFunction(0, 0, "lazy"); + MockCheckedExpectedCall* expectedCall2 = expectedCalls.addFunction("influence"); + MockActualCallsQueueForTest actualCalls; - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "lazy", expectations, actualCalls); + actualCalls.addCall(1, expectedCall2); + + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "lazy", expectedCalls, actualCalls); mock().expectNoCall("lazy"); mock().expectOneCall("influence"); @@ -313,21 +320,32 @@ TEST(MockCallTest, expectNoCallInScopeButActualCallInGlobal) TEST(MockCallTest, ignoreOtherCallsExceptForTheExpectedOne) { + MockFailureReporterInstaller failureReporterInstaller; + + MockExpectedCallsListForTest expectedCalls; + expectedCalls.addFunction("foo"); + MockActualCallsQueueForTest actualCalls; + MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectedCalls, actualCalls); + mock().expectOneCall("foo"); mock().ignoreOtherCalls(); - mock().actualCall("bar").withParameter("foo", 1);; + mock().actualCall("bar").withParameter("foo", 1); + mock().checkExpectations(); - mock().clear(); + CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); } TEST(MockCallTest, ignoreOtherCallsDoesntIgnoreMultipleCallsOfTheSameFunction) { MockFailureReporterInstaller failureReporterInstaller; - MockExpectedCallsListForTest expectations; - expectations.addFunction("foo")->callWasMade(1); + MockExpectedCallsListForTest expectedCalls; + MockCheckedExpectedCall* expectedCall1 = expectedCalls.addFunction("foo"); + MockActualCallsQueueForTest actualCalls; - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "foo", expectations, actualCalls); + actualCalls.addCall(1, expectedCall1); + + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "foo", expectedCalls, actualCalls); mock().expectOneCall("foo"); mock().ignoreOtherCalls(); @@ -548,10 +566,13 @@ TEST(MockCallTest, expectNCalls_NotFulfilled) { MockFailureReporterInstaller failureReporterInstaller; - MockExpectedCallsListForTest expectations; - expectations.addFunction(2, 2, "boo")->callWasMade(1); + MockExpectedCallsListForTest expectedCalls; + MockCheckedExpectedCall* expectedCall1 = expectedCalls.addFunction(2, 2, "boo"); + MockActualCallsQueueForTest actualCalls; - MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations, actualCalls); + actualCalls.addCall(1, expectedCall1); + + MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectedCalls, actualCalls); mock().expectNCalls(2, "boo"); mock().actualCall("boo"); @@ -579,12 +600,14 @@ TEST(MockCallTest, expectAtLeastNCalls_NotFulfilled) { MockFailureReporterInstaller failureReporterInstaller; - MockExpectedCallsListForTest expectations; - MockCheckedExpectedCall* expectedCall = expectations.addFunction(3, (unsigned int)-1, "boo"); - expectedCall->callWasMade(1); - expectedCall->callWasMade(2); + MockExpectedCallsListForTest expectedCalls; + MockCheckedExpectedCall* expectedCall1 = expectedCalls.addFunction(3, (unsigned int)-1, "boo"); + MockActualCallsQueueForTest actualCalls; - MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations, actualCalls); + actualCalls.addCall(1, expectedCall1); + actualCalls.addCall(2, expectedCall1); + + MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectedCalls, actualCalls); mock().expectAtLeastNCalls(3, "boo"); mock().actualCall("boo"); @@ -598,10 +621,13 @@ TEST(MockCallTest, expectAtMostOneCall_NotFulfilled) { MockFailureReporterInstaller failureReporterInstaller; - MockExpectedCallsListForTest expectations; - expectations.addFunction(0, 1, "boo")->callWasMade(1); + MockExpectedCallsListForTest expectedCalls; + MockCheckedExpectedCall* expectedCall1 = expectedCalls.addFunction(0, 1, "boo"); + MockActualCallsQueueForTest actualCalls; - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "boo", expectations, actualCalls); + actualCalls.addCall(1, expectedCall1); + + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "boo", expectedCalls, actualCalls); mock().expectAtMostOneCall("boo"); mock().actualCall("boo"); @@ -615,13 +641,15 @@ TEST(MockCallTest, expectAtMostNCalls_NotFulfilled) { MockFailureReporterInstaller failureReporterInstaller; - MockExpectedCallsListForTest expectations; - MockCheckedExpectedCall* expectedCall = expectations.addFunction(0, 3, "boo"); - expectedCall->callWasMade(1); - expectedCall->callWasMade(2); - expectedCall->callWasMade(3); + MockExpectedCallsListForTest expectedCalls; + MockCheckedExpectedCall* expectedCall1 = expectedCalls.addFunction(0, 3, "boo"); + MockActualCallsQueueForTest actualCalls; - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "boo", expectations, actualCalls); + actualCalls.addCall(1, expectedCall1); + actualCalls.addCall(2, expectedCall1); + actualCalls.addCall(3, expectedCall1); + + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "boo", expectedCalls, actualCalls); mock().expectAtMostNCalls(3, "boo"); mock().actualCall("boo"); @@ -637,11 +665,13 @@ TEST(MockCallTest, expectRangeOfCalls_NotFulfilled_BelowMinimum) { MockFailureReporterInstaller failureReporterInstaller; - MockExpectedCallsListForTest expectations; - MockCheckedExpectedCall* expectedCall = expectations.addFunction(2, 3, "boo"); - expectedCall->callWasMade(1); + MockExpectedCallsListForTest expectedCalls; + MockCheckedExpectedCall* expectedCall1 = expectedCalls.addFunction(2, 3, "boo"); + MockActualCallsQueueForTest actualCalls; - MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations, actualCalls); + actualCalls.addCall(1, expectedCall1); + + MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectedCalls, actualCalls); mock().expectRangeOfCalls(2, 3, "boo"); mock().actualCall("boo"); @@ -654,13 +684,15 @@ TEST(MockCallTest, expectRangeOfCalls_NotFulfilled_AboveMaximum) { MockFailureReporterInstaller failureReporterInstaller; - MockExpectedCallsListForTest expectations; - MockCheckedExpectedCall* expectedCall = expectations.addFunction(2, 3, "boo"); - expectedCall->callWasMade(1); - expectedCall->callWasMade(2); - expectedCall->callWasMade(3); + MockExpectedCallsListForTest expectedCalls; + MockCheckedExpectedCall* expectedCall1 = expectedCalls.addFunction(2, 3, "boo"); + MockActualCallsQueueForTest actualCalls; - MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "boo", expectations, actualCalls); + actualCalls.addCall(1, expectedCall1); + actualCalls.addCall(2, expectedCall1); + actualCalls.addCall(3, expectedCall1); + + MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "boo", expectedCalls, actualCalls); mock().expectRangeOfCalls(2, 3, "boo"); mock().actualCall("boo"); diff --git a/tests/CppUTestExt/MockParameterTest.cpp b/tests/CppUTestExt/MockParameterTest.cpp index dbeca85b3..aa192f045 100644 --- a/tests/CppUTestExt/MockParameterTest.cpp +++ b/tests/CppUTestExt/MockParameterTest.cpp @@ -450,15 +450,16 @@ TEST(MockParameterTest, ignoreOtherParametersMultipleCallsButOneDidntHappen) { MockFailureReporterInstaller failureReporterInstaller; - MockExpectedCallsListForTest expectations; - MockCheckedExpectedCall* call = expectations.addFunction("boo"); - call->ignoreOtherParameters(); - call->callWasMade(1); - call->finalizeActualCallMatch(); - call->ignoreOtherParameters(); - expectations.addFunction("boo")->ignoreOtherParameters(); + MockExpectedCallsListForTest expectedCalls; + MockCheckedExpectedCall* expectedCall1 = expectedCalls.addFunction("boo"); + expectedCall1->ignoreOtherParameters(); + MockCheckedExpectedCall* expectedCall2 = expectedCalls.addFunction("boo"); + expectedCall2->ignoreOtherParameters(); + MockActualCallsQueueForTest actualCalls; - MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectations, actualCalls); + actualCalls.addCall(1, expectedCall1); + + MockExpectedCallsNotFulfilledFailure expectedFailure(mockFailureTest(), expectedCalls, actualCalls); mock().expectOneCall("boo").ignoreOtherParameters(); mock().expectOneCall("boo").ignoreOtherParameters(); diff --git a/tests/CppUTestExt/MockStrictOrderTest.cpp b/tests/CppUTestExt/MockStrictOrderTest.cpp index 1e6a57e82..cf4e79f61 100644 --- a/tests/CppUTestExt/MockStrictOrderTest.cpp +++ b/tests/CppUTestExt/MockStrictOrderTest.cpp @@ -63,15 +63,20 @@ TEST(MockStrictOrderTest, someOrderObserved) TEST(MockStrictOrderTest, orderViolated) { MockFailureReporterInstaller failureReporterInstaller; - mock().strictOrder(); - MockExpectedCallsListForTest expectations; - expectations.addFunction("foo1", 1)->callWasMade(1); - expectations.addFunction("foo1", 2)->callWasMade(3); - expectations.addFunction("foo2", 3)->callWasMade(2); + MockExpectedCallsListForTest expectedCalls; + MockCheckedExpectedCall* expectedCall1 = expectedCalls.addFunction("foo1", 1); + MockCheckedExpectedCall* expectedCall2 = expectedCalls.addFunction("foo1", 2); + MockCheckedExpectedCall* expectedCall3 = expectedCalls.addFunction("foo2", 3); + MockActualCallsQueueForTest actualCalls; - MockCallOrderFailure expectedFailure(mockFailureTest(), expectations, actualCalls); + actualCalls.addCall(1, expectedCall1); + actualCalls.addCall(2, expectedCall3); + actualCalls.addCall(3, expectedCall2); + + MockCallOrderFailure expectedFailure(mockFailureTest(), expectedCalls, actualCalls); + mock().strictOrder(); mock().expectOneCall("foo1"); mock().expectOneCall("foo1"); mock().expectOneCall("foo2"); @@ -86,14 +91,22 @@ TEST(MockStrictOrderTest, orderViolated) TEST(MockStrictOrderTest, orderViolatedWorksHierarchically) { MockFailureReporterInstaller failureReporterInstaller; - mock().strictOrder(); - mock("bla").strictOrder(); - MockExpectedCallsListForTest expectations; - expectations.addFunction("foo::foo1", 1)->callWasMade(2); - expectations.addFunction("foo::foo2", 2)->callWasMade(1); + MockExpectedCallsListForTest expectedCallsReported; + MockExpectedCallsListForTest expectedCallsNotReported; + MockCheckedExpectedCall* expectedCall1 = expectedCallsNotReported.addFunction("bla::foo1", 1); + MockCheckedExpectedCall* expectedCall2 = expectedCallsReported.addFunction("foo::foo1", 1); + MockCheckedExpectedCall* expectedCall3 = expectedCallsReported.addFunction("foo::foo2", 2); + MockActualCallsQueueForTest actualCalls; - MockCallOrderFailure expectedFailure(mockFailureTest(), expectations, actualCalls); + actualCalls.addCall(1, "bla", expectedCall1); + actualCalls.addCall(1, "foo", expectedCall3); + actualCalls.addCall(2, "foo", expectedCall2); + + MockCallOrderFailure expectedFailure(mockFailureTest(), expectedCallsReported, actualCalls); + + mock().strictOrder(); + mock("bla").strictOrder(); mock("bla").expectOneCall("foo1"); mock("foo").expectOneCall("foo1"); @@ -110,15 +123,23 @@ TEST(MockStrictOrderTest, orderViolatedWorksHierarchically) TEST(MockStrictOrderTest, orderViolatedWorksWithExtraUnexpectedCall) { MockFailureReporterInstaller failureReporterInstaller; - mock().strictOrder(); - mock("bla").strictOrder(); - mock().ignoreOtherCalls(); - MockExpectedCallsListForTest expectations; - expectations.addFunction("foo::foo1", 1)->callWasMade(2); - expectations.addFunction("foo::foo2", 2)->callWasMade(1); + MockExpectedCallsListForTest expectedCallsReported; + MockExpectedCallsListForTest expectedCallsNotReported; + MockCheckedExpectedCall* expectedCall1 = expectedCallsNotReported.addFunction("bla::foo1", 1); + MockCheckedExpectedCall* expectedCall2 = expectedCallsReported.addFunction("foo::foo1", 1); + MockCheckedExpectedCall* expectedCall3 = expectedCallsReported.addFunction("foo::foo2", 2); + MockActualCallsQueueForTest actualCalls; - MockCallOrderFailure expectedFailure(mockFailureTest(), expectations, actualCalls); + actualCalls.addCall(1, "bla", expectedCall1); + actualCalls.addCall(1, "foo", expectedCall3); + actualCalls.addCall(2, "foo", expectedCall2); + + MockCallOrderFailure expectedFailure(mockFailureTest(), expectedCallsReported, actualCalls); + + mock().strictOrder(); + mock("bla").strictOrder(); + mock().ignoreOtherCalls(); mock("bla").expectOneCall("foo1"); mock("foo").expectOneCall("foo1"); @@ -126,9 +147,9 @@ TEST(MockStrictOrderTest, orderViolatedWorksWithExtraUnexpectedCall) mock("bla").actualCall("foo1"); mock("foo").actualCall("foo2"); - mock("foo").actualCall("unexpected1"); + mock("foo").actualCall("unexpected1"); mock("foo").actualCall("foo1"); - mock("foo").actualCall("unexpected2"); + mock("foo").actualCall("unexpected2"); mock().checkExpectations(); CHECK_EXPECTED_MOCK_FAILURE(expectedFailure); @@ -139,11 +160,15 @@ TEST(MockStrictOrderTest, orderViolatedWithinAScope) MockFailureReporterInstaller failureReporterInstaller; mock().strictOrder(); - MockExpectedCallsListForTest expectations; - expectations.addFunction("scope::foo1", 1)->callWasMade(2); - expectations.addFunction("scope::foo2", 2)->callWasMade(1); + MockExpectedCallsListForTest expectedCalls; + MockCheckedExpectedCall* expectedCall1 = expectedCalls.addFunction("scope::foo1", 1); + MockCheckedExpectedCall* expectedCall2 = expectedCalls.addFunction("scope::foo2", 2); + MockActualCallsQueueForTest actualCalls; - MockCallOrderFailure expectedFailure(mockFailureTest(), expectations, actualCalls); + actualCalls.addCall(1, "scope", expectedCall2); + actualCalls.addCall(2, "scope", expectedCall1); + + MockCallOrderFailure expectedFailure(mockFailureTest(), expectedCalls, actualCalls); mock("scope").expectOneCall("foo1"); mock("scope").expectOneCall("foo2"); From fb5653fbfe4c7c5c290fa1f40d1c2cddd238d670 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Tue, 5 Jul 2016 21:51:09 +0200 Subject: [PATCH 21/26] Added functionality to MockSupport to limit the maximum size of the actual calls log. The actual calls log limit is inherited hierarchically between mock scopes. --- include/CppUTestExt/MockSupport.h | 1 + src/CppUTestExt/MockSupport.cpp | 13 ++++++++++++ tests/CppUTestExt/MockSupportTest.cpp | 30 +++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/include/CppUTestExt/MockSupport.h b/include/CppUTestExt/MockSupport.h index e962163d4..c5b49716c 100755 --- a/include/CppUTestExt/MockSupport.h +++ b/include/CppUTestExt/MockSupport.h @@ -104,6 +104,7 @@ class MockSupport virtual void enable(); virtual void tracing(bool enabled); virtual void ignoreOtherCalls(); + virtual void setMaxCallLogSize(unsigned int maxSize); virtual void checkExpectations(); virtual bool expectedCallsLeft(); diff --git a/src/CppUTestExt/MockSupport.cpp b/src/CppUTestExt/MockSupport.cpp index de1213570..a4071a4bd 100755 --- a/src/CppUTestExt/MockSupport.cpp +++ b/src/CppUTestExt/MockSupport.cpp @@ -123,6 +123,7 @@ void MockSupport::clear() expectations_.deleteAllExpectationsAndClearList(); actualCalls_.clear(); + actualCalls_.setMaxSize((unsigned int) -1); ignoreOtherCalls_ = false; enabled_ = true; actualCallOrder_ = 0; @@ -288,6 +289,17 @@ void MockSupport::tracing(bool enabled) if (getMockSupport(p)) getMockSupport(p)->tracing(enabled); } +void MockSupport::setMaxCallLogSize(unsigned int maxSize) +{ + actualCalls_.setMaxSize(maxSize); + + for (MockNamedValueListNode* p = data_.begin(); p; p = p->next()) { + if (getMockSupport(p)) { + getMockSupport(p)->setMaxCallLogSize(maxSize); + } + } +} + const char* MockSupport::getTraceOutput() { return MockActualCallTrace::instance().getTraceOutput(); @@ -523,6 +535,7 @@ MockSupport* MockSupport::clone(const SimpleString& mockName) if (strictOrdering_) newMock->strictOrder(); newMock->tracing(tracing_); + newMock->setMaxCallLogSize(actualCalls_.getMaxSize()); newMock->installComparatorsAndCopiers(comparatorsAndCopiersRepository_); return newMock; } diff --git a/tests/CppUTestExt/MockSupportTest.cpp b/tests/CppUTestExt/MockSupportTest.cpp index e655e6479..76d9005e4 100644 --- a/tests/CppUTestExt/MockSupportTest.cpp +++ b/tests/CppUTestExt/MockSupportTest.cpp @@ -146,6 +146,36 @@ TEST(MockSupportTest, tracing) STRCMP_CONTAINS("foo", mock().getTraceOutput()); } +TEST(MockSupportTest, setMaxCallLogSize) +{ + mock().setMaxCallLogSize(2); + mock("foo").setMaxCallLogSize(3); + + mock().expectAnyCalls("boo"); + mock("foo").expectAnyCalls("baa"); + mock("bar").expectAnyCalls("buu"); + + mock().actualCall("boo"); + mock().actualCall("boo"); + mock().actualCall("boo"); + mock("foo").actualCall("baa"); + mock("foo").actualCall("baa"); + mock("foo").actualCall("baa"); + mock("foo").actualCall("baa"); + mock("foo").actualCall("baa"); + mock("foo").actualCall("baa"); + mock("foo").actualCall("baa"); + mock("bar").actualCall("buu"); + mock("bar").actualCall("buu"); + mock("bar").actualCall("buu"); + mock("bar").actualCall("buu"); + mock("bar").actualCall("buu"); + + LONGS_EQUAL(2, mock().getActualCalls().size()) + LONGS_EQUAL(3, mock("foo").getActualCalls().size()) + LONGS_EQUAL(2, mock("bar").getActualCalls().size()) +} + TEST(MockSupportTest, tracingWorksHierarchically) { mock("scope").tracing(true); From 3999548cfc66781083983cce68dbb8c2a21b8fc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Tue, 5 Jul 2016 22:11:59 +0200 Subject: [PATCH 22/26] Added C language new call for setMaxCallLogSize. --- include/CppUTestExt/MockSupport_c.h | 1 + src/CppUTestExt/MockSupport_c.cpp | 7 +++++++ tests/CppUTestExt/MockSupport_cTest.cpp | 20 ++++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/include/CppUTestExt/MockSupport_c.h b/include/CppUTestExt/MockSupport_c.h index 18cedaa77..902ce5f47 100644 --- a/include/CppUTestExt/MockSupport_c.h +++ b/include/CppUTestExt/MockSupport_c.h @@ -195,6 +195,7 @@ struct SMockSupport_c void (*disable)(void); void (*enable)(void); void (*ignoreOtherCalls)(void); + void (*setMaxCallLogSize)(unsigned int maxSize); void (*checkExpectations)(void); int (*expectedCallsLeft)(void); diff --git a/src/CppUTestExt/MockSupport_c.cpp b/src/CppUTestExt/MockSupport_c.cpp index ab3cb102f..e8c319b0c 100644 --- a/src/CppUTestExt/MockSupport_c.cpp +++ b/src/CppUTestExt/MockSupport_c.cpp @@ -131,6 +131,7 @@ MockActualCall_c* actualCall_c(const char* name); void disable_c(); void enable_c(); void ignoreOtherCalls_c(); +void setMaxCallLogSize_c(unsigned int maxSize); void setBoolData_c(const char* name, int value); void setIntData_c(const char* name, int value); void setUnsignedIntData_c(const char* name, unsigned int value); @@ -351,6 +352,7 @@ static MockSupport_c gMockSupport = { disable_c, enable_c, ignoreOtherCalls_c, + setMaxCallLogSize_c, checkExpectations_c, expectedCallsLeft_c, clear_c, @@ -862,6 +864,11 @@ void ignoreOtherCalls_c() currentMockSupport->ignoreOtherCalls(); } +void setMaxCallLogSize_c(unsigned int maxSize) +{ + currentMockSupport->setMaxCallLogSize(maxSize); +} + void setBoolData_c(const char* name, int value) { currentMockSupport->setData(name, (value != 0)); diff --git a/tests/CppUTestExt/MockSupport_cTest.cpp b/tests/CppUTestExt/MockSupport_cTest.cpp index 869302071..e22a7855b 100644 --- a/tests/CppUTestExt/MockSupport_cTest.cpp +++ b/tests/CppUTestExt/MockSupport_cTest.cpp @@ -696,3 +696,23 @@ TEST(MockSupport_c, ignoreOtherCalls) mock_c()->actualCall("bar"); mock_c()->checkExpectations(); } + +static void failingUnexpectedCallWithActualCallLogLimited_() +{ + mock_c()->setMaxCallLogSize(2); + mock_c()->expectAnyCalls("foo"); + mock_c()->actualCall("foo"); + mock_c()->actualCall("foo"); + mock_c()->actualCall("foo"); + mock_c()->actualCall("bar"); +} // LCOV_EXCL_LINE + +TEST(MockSupport_c, setMaxCallLogSize) +{ + TestTestingFixture fixture; + fixture.runTestWithMethod(failingUnexpectedCallWithActualCallLogLimited_); + fixture.assertPrintContains("ACTUAL calls that were expected (in call order):\n" + "\t\t(2) foo -> no parameters\n" + "\t\t(3) foo -> no parameters"); +} + From faf07f9a2305c1e9bdafcbc29fd7c909d610251f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Tue, 5 Jul 2016 22:44:18 +0200 Subject: [PATCH 23/26] Added unit test for MockActualCallsList::hasFinalizedMatchingExpectations(). --- tests/CppUTestExt/ExpectedFunctionsListTest.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/CppUTestExt/ExpectedFunctionsListTest.cpp b/tests/CppUTestExt/ExpectedFunctionsListTest.cpp index a8c815238..89382cac9 100644 --- a/tests/CppUTestExt/ExpectedFunctionsListTest.cpp +++ b/tests/CppUTestExt/ExpectedFunctionsListTest.cpp @@ -283,3 +283,13 @@ TEST(MockExpectedCallsList, toStringOnEmptyList) { STRCMP_EQUAL("", list->unfulfilledCallsToString().asCharString()); } + +TEST(MockExpectedCallsList, hasFinalizedMatchingExpectations) +{ + call1->ignoreOtherParameters(); + list->addExpectedCall(call1); + CHECK(! list->hasFinalizedMatchingExpectations()); + + call1->finalizeActualCallMatch(); + CHECK(list->hasFinalizedMatchingExpectations()); +} From e91265075f9ff10f8d6e6e9ad2d2d53c86c13723 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Thu, 7 Jul 2016 21:59:43 +0200 Subject: [PATCH 24/26] Improved unit test for MockSupport::setMaxCallLogSize(). --- tests/CppUTestExt/MockSupportTest.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/CppUTestExt/MockSupportTest.cpp b/tests/CppUTestExt/MockSupportTest.cpp index 76d9005e4..e99a19319 100644 --- a/tests/CppUTestExt/MockSupportTest.cpp +++ b/tests/CppUTestExt/MockSupportTest.cpp @@ -148,12 +148,14 @@ TEST(MockSupportTest, tracing) TEST(MockSupportTest, setMaxCallLogSize) { + mock("bii").setMaxCallLogSize(10); mock().setMaxCallLogSize(2); mock("foo").setMaxCallLogSize(3); mock().expectAnyCalls("boo"); mock("foo").expectAnyCalls("baa"); mock("bar").expectAnyCalls("buu"); + mock("bii").expectAnyCalls("bee"); mock().actualCall("boo"); mock().actualCall("boo"); @@ -170,10 +172,15 @@ TEST(MockSupportTest, setMaxCallLogSize) mock("bar").actualCall("buu"); mock("bar").actualCall("buu"); mock("bar").actualCall("buu"); + mock("bii").actualCall("bee"); + mock("bii").actualCall("bee"); + mock("bii").actualCall("bee"); + mock("bii").actualCall("bee"); LONGS_EQUAL(2, mock().getActualCalls().size()) LONGS_EQUAL(3, mock("foo").getActualCalls().size()) LONGS_EQUAL(2, mock("bar").getActualCalls().size()) + LONGS_EQUAL(2, mock("bii").getActualCalls().size()) } TEST(MockSupportTest, tracingWorksHierarchically) From 111f0f0ee9d09e20ffcc6fee4fb6bf1748c45020 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Fri, 8 Jul 2016 00:52:14 +0200 Subject: [PATCH 25/26] Improved unit tests for MockExpectedCall. --- tests/CppUTestExt/MockExpectedCallTest.cpp | 130 ++++++++++++--------- 1 file changed, 78 insertions(+), 52 deletions(-) diff --git a/tests/CppUTestExt/MockExpectedCallTest.cpp b/tests/CppUTestExt/MockExpectedCallTest.cpp index d825bbdbe..379e35d4b 100644 --- a/tests/CppUTestExt/MockExpectedCallTest.cpp +++ b/tests/CppUTestExt/MockExpectedCallTest.cpp @@ -156,6 +156,7 @@ TEST_GROUP(MockExpectedCall) void setup() { call = new MockCheckedExpectedCall(1, 1); + call->withName("funcName"); } void teardown() { @@ -173,103 +174,128 @@ TEST(MockExpectedCall, callWithoutParameterSetOrNotFound) TEST(MockExpectedCall, callWithUnsignedIntegerParameter) { - const SimpleString name = "unsigned integer"; - unsigned int value = 777; - call->withParameter(name, value); - STRCMP_EQUAL("unsigned int", call->getInputParameterType(name).asCharString()); - LONGS_EQUAL(value, call->getInputParameter(name).getUnsignedIntValue()); - CHECK(call->hasInputParameterWithName(name)); + const SimpleString paramName = "paramName"; + unsigned int value = 356; + call->withParameter(paramName, value); + STRCMP_EQUAL("unsigned int", call->getInputParameterType(paramName).asCharString()); + LONGS_EQUAL(value, call->getInputParameter(paramName).getUnsignedIntValue()); + CHECK(call->hasInputParameterWithName(paramName)); + STRCMP_EQUAL("funcName -> unsigned int paramName: <356 (0x164)>", call->callToString(true).asCharString()); } TEST(MockExpectedCall, callWithIntegerParameter) { - call->withParameter("integer", 1); - STRCMP_EQUAL("int", call->getInputParameterType("integer").asCharString()); - LONGS_EQUAL(1, call->getInputParameter("integer").getIntValue()); - CHECK(call->hasInputParameterWithName("integer")); + const SimpleString paramName = "paramName"; + int value = 2; + call->withParameter(paramName, value); + STRCMP_EQUAL("int", call->getInputParameterType(paramName).asCharString()); + LONGS_EQUAL(value, call->getInputParameter(paramName).getIntValue()); + CHECK(call->hasInputParameterWithName(paramName)); + STRCMP_EQUAL("funcName -> int paramName: <2 (0x2)>", call->callToString(true).asCharString()); } TEST(MockExpectedCall, callWithBooleanParameter) { - call->withParameter("boolean", true); - STRCMP_EQUAL("bool", call->getInputParameterType("boolean").asCharString()); - CHECK_EQUAL(true, call->getInputParameter("boolean").getBoolValue()); - CHECK(call->hasInputParameterWithName("boolean")); + const SimpleString paramName = "paramName"; + bool value = true; + call->withParameter(paramName, value); + STRCMP_EQUAL("bool", call->getInputParameterType(paramName).asCharString()); + CHECK_EQUAL(value, call->getInputParameter(paramName).getBoolValue()); + CHECK(call->hasInputParameterWithName(paramName)); + STRCMP_EQUAL("funcName -> bool paramName: ", call->callToString(true).asCharString()); } TEST(MockExpectedCall, callWithUnsignedLongIntegerParameter) { - const SimpleString name = "unsigned long integer"; - unsigned long value = 777; - call->withParameter(name, value); - STRCMP_EQUAL("unsigned long int", call->getInputParameterType(name).asCharString()); - LONGS_EQUAL(value, call->getInputParameter(name).getUnsignedLongIntValue()); - CHECK(call->hasInputParameterWithName(name)); + const SimpleString paramName = "paramName"; + unsigned long value = 888; + call->withParameter(paramName, value); + STRCMP_EQUAL("unsigned long int", call->getInputParameterType(paramName).asCharString()); + LONGS_EQUAL(value, call->getInputParameter(paramName).getUnsignedLongIntValue()); + CHECK(call->hasInputParameterWithName(paramName)); + STRCMP_EQUAL("funcName -> unsigned long int paramName: <888 (0x378)>", call->callToString(true).asCharString()); } TEST(MockExpectedCall, callWithLongIntegerParameter) { - const SimpleString name = "long integer"; + const SimpleString paramName = "paramName"; long value = 777; - call->withParameter(name, value); - STRCMP_EQUAL("long int", call->getInputParameterType(name).asCharString()); - LONGS_EQUAL(value, call->getInputParameter(name).getLongIntValue()); - CHECK(call->hasInputParameterWithName(name)); + call->withParameter(paramName, value); + STRCMP_EQUAL("long int", call->getInputParameterType(paramName).asCharString()); + LONGS_EQUAL(value, call->getInputParameter(paramName).getLongIntValue()); + CHECK(call->hasInputParameterWithName(paramName)); + STRCMP_EQUAL("funcName -> long int paramName: <777 (0x309)>", call->callToString(true).asCharString()); } TEST(MockExpectedCall, callWithDoubleParameter) { - call->withParameter("double", 1.2); - STRCMP_EQUAL("double", call->getInputParameterType("double").asCharString()); - DOUBLES_EQUAL(1.2, call->getInputParameter("double").getDoubleValue(), 0.05); + const SimpleString paramName = "paramName"; + double value = 1.2; + call->withParameter(paramName, value); + STRCMP_EQUAL("double", call->getInputParameterType(paramName).asCharString()); + DOUBLES_EQUAL(value, call->getInputParameter(paramName).getDoubleValue(), 0.05); + STRCMP_EQUAL("funcName -> double paramName: <1.2>", call->callToString(true).asCharString()); } TEST(MockExpectedCall, callWithStringParameter) { - call->withParameter("string", "hello world"); - STRCMP_EQUAL("const char*", call->getInputParameterType("string").asCharString()); - STRCMP_EQUAL("hello world", call->getInputParameter("string").getStringValue()); + const SimpleString paramName = "paramName"; + const char* value = "hello world"; + call->withParameter(paramName, value); + STRCMP_EQUAL("const char*", call->getInputParameterType(paramName).asCharString()); + STRCMP_EQUAL(value, call->getInputParameter(paramName).getStringValue()); + STRCMP_EQUAL("funcName -> const char* paramName: ", call->callToString(true).asCharString()); } TEST(MockExpectedCall, callWithPointerParameter) { - void* ptr = (void*) 0x123; - call->withParameter("pointer", ptr); - STRCMP_EQUAL("void*", call->getInputParameterType("pointer").asCharString()); - POINTERS_EQUAL(ptr, call->getInputParameter("pointer").getPointerValue()); + const SimpleString paramName = "paramName"; + void* value = (void*) 0x123; + call->withParameter(paramName, value); + STRCMP_EQUAL("void*", call->getInputParameterType(paramName).asCharString()); + POINTERS_EQUAL(value, call->getInputParameter(paramName).getPointerValue()); + STRCMP_EQUAL("funcName -> void* paramName: <0x123>", call->callToString(true).asCharString()); } TEST(MockExpectedCall, callWithConstPointerParameter) { - const void* ptr = (const void*) 0x345; - call->withParameter("constPointer", ptr); - STRCMP_EQUAL("const void*", call->getInputParameterType("constPointer").asCharString()); - POINTERS_EQUAL(ptr, call->getInputParameter("constPointer").getConstPointerValue()); + const SimpleString paramName = "paramName"; + const void* value = (const void*) 0x345; + call->withParameter(paramName, value); + STRCMP_EQUAL("const void*", call->getInputParameterType(paramName).asCharString()); + POINTERS_EQUAL(value, call->getInputParameter(paramName).getConstPointerValue()); + STRCMP_EQUAL("funcName -> const void* paramName: <0x345>", call->callToString(true).asCharString()); } TEST(MockExpectedCall, callWithFunctionPointerParameter) { - void (*ptr)() = (void (*)()) 0x123; - call->withParameter("functionPointer", ptr); - STRCMP_EQUAL("void (*)()", call->getInputParameterType("functionPointer").asCharString()); - FUNCTIONPOINTERS_EQUAL(ptr, call->getInputParameter("functionPointer").getFunctionPointerValue()); + const SimpleString paramName = "paramName"; + void (*value)() = (void (*)()) 0xdead; + call->withParameter(paramName, value); + STRCMP_EQUAL("void (*)()", call->getInputParameterType(paramName).asCharString()); + FUNCTIONPOINTERS_EQUAL(value, call->getInputParameter(paramName).getFunctionPointerValue()); + STRCMP_EQUAL("funcName -> void (*)() paramName: <0xdead>", call->callToString(true).asCharString()); } TEST(MockExpectedCall, callWithMemoryBuffer) { - const unsigned char mem_buffer[] = { 0x12, 0xFE, 0xA1 }; - call->withParameter("memoryBuffer", mem_buffer, sizeof(mem_buffer)); - STRCMP_EQUAL("const unsigned char*", call->getInputParameterType("memoryBuffer").asCharString()); - POINTERS_EQUAL( (void*) mem_buffer, (void*) call->getInputParameter("memoryBuffer").getMemoryBuffer() ); - LONGS_EQUAL(sizeof(mem_buffer), call->getInputParameter("memoryBuffer").getSize()); + const SimpleString paramName = "paramName"; + const unsigned char value[] = { 0x12, 0xFE, 0xA1 }; + call->withParameter(paramName, value, sizeof(value)); + STRCMP_EQUAL("const unsigned char*", call->getInputParameterType(paramName).asCharString()); + POINTERS_EQUAL( (void*) value, (void*) call->getInputParameter(paramName).getMemoryBuffer() ); + LONGS_EQUAL(sizeof(value), call->getInputParameter(paramName).getSize()); + STRCMP_EQUAL("funcName -> const unsigned char* paramName: ", call->callToString(true).asCharString()); } TEST(MockExpectedCall, callWithObjectParameter) { - void* ptr = (void*) 0x123; - call->withParameterOfType("class", "object", ptr); - POINTERS_EQUAL(ptr, call->getInputParameter("object").getObjectPointer()); - STRCMP_EQUAL("class", call->getInputParameterType("object").asCharString()); + const SimpleString paramName = "paramName"; + void* value = (void*) 0x123; + call->withParameterOfType("ClassName", paramName, value); + POINTERS_EQUAL(value, call->getInputParameter(paramName).getObjectPointer()); + STRCMP_EQUAL("ClassName", call->getInputParameterType(paramName).asCharString()); + STRCMP_EQUAL("funcName -> ClassName paramName: ", call->callToString(true).asCharString()); } TEST(MockExpectedCall, callWithObjectParameterUnequalComparison) From ef47ef7fb3e5b1c152347b40a312f8255be4ddb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Gonz=C3=A1lez?= Date: Fri, 8 Jul 2016 01:03:45 +0200 Subject: [PATCH 26/26] Removed old commented code that was left over. --- src/CppUTestExt/MockSupport.cpp | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/CppUTestExt/MockSupport.cpp b/src/CppUTestExt/MockSupport.cpp index a4071a4bd..4fbba1b22 100755 --- a/src/CppUTestExt/MockSupport.cpp +++ b/src/CppUTestExt/MockSupport.cpp @@ -164,13 +164,6 @@ MockExpectedCall& MockSupport::expectNCalls(unsigned int amount, const SimpleStr void MockSupport::expectNoCall(const SimpleString& functionName) { expectRangeOfCalls(0, 0, functionName); -// if (!enabled_) return; -// -// countCheck(); -// -// MockCheckedExpectedCall* call = new MockCheckedExpectedCall; -// call->withName(appendScopeToName(functionName)); -// unExpectations_.addExpectedCall(call); } MockExpectedCall& MockSupport::expectAtLeastOneCall(const SimpleString& functionName)