Skip to content

Expected calls matching several actual calls #1018

New issue

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

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

Already on GitHub? Sign in to your account

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added functionality to MockCheckedActualCall and MockActualCallsQueue…
… to generate a representation of their contents as a string, in preparation implementing an actual calls log.
  • Loading branch information
jgonzalezdr committed Jul 3, 2016
commit bc331818188cbb2a5f49ec21ecb0c5156132fc60
5 changes: 3 additions & 2 deletions include/CppUTest/SimpleString.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 18 additions & 0 deletions include/CppUTestExt/MockActualCallsQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
10 changes: 9 additions & 1 deletion include/CppUTestExt/MockCheckedActualCall.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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_;
Expand Down
7 changes: 4 additions & 3 deletions include/CppUTestExt/MockCheckedExpectedCall.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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_;
Expand Down
2 changes: 1 addition & 1 deletion include/CppUTestExt/MockExpectedCallsList.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions include/CppUTestExt/MockSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "CppUTestExt/MockCheckedActualCall.h"
#include "CppUTestExt/MockCheckedExpectedCall.h"
#include "CppUTestExt/MockExpectedCallsList.h"
#include "CppUTestExt/MockActualCallsQueue.h"

class UtestShell;
class MockSupport;
Expand Down Expand Up @@ -125,6 +126,8 @@ class MockSupport

virtual const MockExpectedCallsList& getExpectedCalls() const;

const SimpleString& getName() const;

protected:
MockSupport* clone(const SimpleString& mockName);
virtual MockCheckedActualCall *createActualFunctionCall();
Expand Down
19 changes: 19 additions & 0 deletions src/CppUTest/SimpleString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 += "<none>";
}
return str;
}

SimpleStringCollection::SimpleStringCollection()
{
collection_ = 0;
Expand Down
32 changes: 31 additions & 1 deletion src/CppUTestExt/MockActualCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void MockCheckedActualCall::setName(const SimpleString& name)
functionName_ = name;
}

SimpleString MockCheckedActualCall::getName() const
const SimpleString& MockCheckedActualCall::getName() const
{
return functionName_;
}
Expand Down Expand Up @@ -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()
{
Expand Down
21 changes: 21 additions & 0 deletions src/CppUTestExt/MockActualCallsQueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

31 changes: 17 additions & 14 deletions src/CppUTestExt/MockExpectedCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void MockCheckedExpectedCall::setName(const SimpleString& name)
functionName_ = name;
}

SimpleString MockCheckedExpectedCall::getName() const
const SimpleString& MockCheckedExpectedCall::getName() const
{
return functionName_;
}
Expand Down Expand Up @@ -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";
Expand All @@ -326,14 +326,15 @@ 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_)
str = StringFromFormat("(object address: %p)::", objectPtr_);

str += getName();
str += " -> ";

if (initialExpectedCallOrder_ != NO_EXPECTED_CALL_ORDER) {
if (initialExpectedCallOrder_ == finalExpectedCallOrder_) {
str += StringFromFormat("expected call order: <%u> -> ", initialExpectedCallOrder_);
Expand Down Expand Up @@ -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;
}
Expand Down
36 changes: 9 additions & 27 deletions src/CppUTestExt/MockExpectedCallsList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 += "<none>";
}
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
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/CppUTestExt/MockSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -643,3 +643,8 @@ const MockExpectedCallsList& MockSupport::getExpectedCalls() const
return expectations_;
}

const SimpleString& MockSupport::getName() const
{
return mockName_;
}

Loading