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 actual calls reporting to MockFailure, and preliminary non-func…
…tional addition of actual calls registry to MockSupport.
  • Loading branch information
jgonzalezdr committed Jul 4, 2016
commit 044a69359b4ccb172f140d5b1286b6fa97eda050
29 changes: 19 additions & 10 deletions include/CppUTestExt/MockFailure.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "CppUTest/TestFailure.h"

class MockExpectedCallsList;
class MockActualCallsQueue;
class MockCheckedActualCall;
class MockNamedValue;
class MockFailure;
Expand All @@ -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
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion include/CppUTestExt/MockSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class MockSupport
virtual void removeAllComparatorsAndCopiers();

virtual const MockExpectedCallsList& getExpectedCalls() const;
virtual const MockActualCallsQueue& getActualCalls() const;

const SimpleString& getName() const;

Expand All @@ -148,7 +149,7 @@ class MockSupport
MockNamedValueComparatorsAndCopiersRepository comparatorsAndCopiersRepository_;
MockNamedValueList data_;
const SimpleString mockName_;

MockActualCallsQueue actualCalls_;
bool tracing_;

void checkExpectationsOfLastActualCall();
Expand Down
14 changes: 8 additions & 6 deletions src/CppUTestExt/MockActualCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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;
}
Expand Down
69 changes: 47 additions & 22 deletions src/CppUTestExt/MockFailure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -93,40 +98,53 @@ 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);
expectationsForOutOfOrder.onlyKeepOutOfOrderExpectations();

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);
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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)
Expand All @@ -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)
Expand Down
Loading