Skip to content

ignoreAdditionalCalls (ignoreMultipleCalls?) #973

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 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/CppUTestExt/MockSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class MockSupport
virtual void enable();
virtual void tracing(bool enabled);
virtual void ignoreOtherCalls();
virtual void ignoreAdditionalCalls();

virtual void checkExpectations();
virtual bool expectedCallsLeft();
Expand Down Expand Up @@ -133,6 +134,7 @@ class MockSupport
MockExpectedCallsList expectations_;
MockExpectedCallsList unExpectations_;
bool ignoreOtherCalls_;
bool ignoreAdditionalCalls_;
bool enabled_;
MockCheckedActualCall *lastActualFunctionCall_;
MockExpectedCallComposite compositeCalls_;
Expand All @@ -153,6 +155,7 @@ class MockSupport

bool hasntExpectationWithName(const SimpleString& functionName);
bool hasntUnexpectationWithName(const SimpleString& functionName);
bool hasntUnFulfilledWithName(const SimpleString& functionName);
bool hasCallsOutOfOrder();

SimpleString appendScopeToName(const SimpleString& functionName);
Expand Down
1 change: 1 addition & 0 deletions include/CppUTestExt/MockSupport_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ struct SMockSupport_c
void (*disable)(void);
void (*enable)(void);
void (*ignoreOtherCalls)(void);
void (*ignoreAdditionalCalls)(void);

void (*checkExpectations)(void);
int (*expectedCallsLeft)(void);
Expand Down
28 changes: 27 additions & 1 deletion src/CppUTestExt/MockSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
: callOrder_(0), expectedCallOrder_(0), strictOrdering_(false), standardReporter_(&defaultReporter_), ignoreOtherCalls_(false), ignoreAdditionalCalls_(false), enabled_(true), lastActualFunctionCall_(NULL), mockName_(mockName), tracing_(false)
{
setActiveReporter(NULL);
}
Expand Down Expand Up @@ -122,6 +122,7 @@ void MockSupport::clear()
unExpectations_.deleteAllExpectationsAndClearList();
compositeCalls_.clear();
ignoreOtherCalls_ = false;
ignoreAdditionalCalls_ = false;
enabled_ = true;
callOrder_ = 0;
expectedCallOrder_ = 0;
Expand Down Expand Up @@ -198,6 +199,18 @@ bool MockSupport::hasntUnexpectationWithName(const SimpleString& functionName)
return !unExpectations_.hasExpectationWithName(functionName);
}

bool MockSupport::hasntUnFulfilledWithName(const SimpleString& functionName)
{
if (expectations_.hasExpectationWithName(functionName)) {
MockExpectedCallsList unfulfilledExpectations;
unfulfilledExpectations.addUnfulfilledExpectations(expectations_);
if (!unfulfilledExpectations.hasExpectationWithName(functionName) && ignoreAdditionalCalls_) {
return true;
}
}
return false;
}

MockActualCall& MockSupport::actualCall(const SimpleString& functionName)
{
const SimpleString scopeFuntionName = appendScopeToName(functionName);
Expand All @@ -216,6 +229,10 @@ MockActualCall& MockSupport::actualCall(const SimpleString& functionName)
return MockIgnoredActualCall::instance();
}

if (hasntUnFulfilledWithName(scopeFuntionName)) {
return MockIgnoredActualCall::instance();
}

MockCheckedActualCall* call = createActualFunctionCall();
call->withName(scopeFuntionName);
return *call;
Expand All @@ -229,6 +246,14 @@ void MockSupport::ignoreOtherCalls()
if (getMockSupport(p)) getMockSupport(p)->ignoreOtherCalls();
}

void MockSupport::ignoreAdditionalCalls()
{
ignoreAdditionalCalls_ = true;

for (MockNamedValueListNode* p = data_.begin(); p; p = p->next())
if (getMockSupport(p)) getMockSupport(p)->ignoreAdditionalCalls();
}

void MockSupport::disable()
{
enabled_ = false;
Expand Down Expand Up @@ -436,6 +461,7 @@ MockSupport* MockSupport::clone(const SimpleString& mockName)
MockSupport* newMock = new MockSupport(mockName);
newMock->setMockFailureStandardReporter(standardReporter_);
if (ignoreOtherCalls_) newMock->ignoreOtherCalls();
if (ignoreAdditionalCalls_) newMock->ignoreAdditionalCalls();

if (!enabled_) newMock->disable();

Expand Down
7 changes: 7 additions & 0 deletions src/CppUTestExt/MockSupport_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ MockActualCall_c* actualCall_c(const char* name);
void disable_c();
void enable_c();
void ignoreOtherCalls_c();
void ignoreAdditionalCalls_c();
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);
Expand Down Expand Up @@ -339,6 +340,7 @@ static MockSupport_c gMockSupport = {
disable_c,
enable_c,
ignoreOtherCalls_c,
ignoreAdditionalCalls_c,
checkExpectations_c,
expectedCallsLeft_c,
clear_c,
Expand Down Expand Up @@ -814,6 +816,11 @@ void ignoreOtherCalls_c()
currentMockSupport->ignoreOtherCalls();
}

void ignoreAdditionalCalls_c()
{
currentMockSupport->ignoreAdditionalCalls();
}

void setBoolData_c(const char* name, int value)
{
currentMockSupport->setData(name, (value != 0));
Expand Down
45 changes: 44 additions & 1 deletion tests/CppUTestExt/MockCallTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,22 @@ TEST(MockCallTest, ignoreOtherCallsExceptForTheExpectedOne)
{
mock().expectOneCall("foo");
mock().ignoreOtherCalls();
mock().actualCall("bar").withParameter("foo", 1);;
mock().actualCall("bar").withParameter("foo", 1);

mock().clear();
}

TEST(MockCallTest, ignoreAdditionalCallsExceptForTheExpectedOne)
{
mock().expectOneCall("foo");
mock().ignoreAdditionalCalls();
mock().actualCall("foo");
mock().actualCall("foo");
mock().actualCall("foo").withParameter("foo", 1);

mock().clear();
}

TEST(MockCallTest, ignoreOtherCallsDoesntIgnoreMultipleCallsOfTheSameFunction)
{
MockFailureReporterInstaller failureReporterInstaller;
Expand All @@ -299,6 +310,23 @@ TEST(MockCallTest, ignoreOtherCallsDoesntIgnoreMultipleCallsOfTheSameFunction)
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}

TEST(MockCallTest, ignoreAdditionalCallsOfTheSameFunctionDoesntIgnoreOtherCalls)
{
MockFailureReporterInstaller failureReporterInstaller;

MockExpectedCallsListForTest expectations;
expectations.addFunction("foo")->callWasMade(1);
MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "bar", expectations);

mock().expectOneCall("foo");
mock().ignoreAdditionalCalls();
mock().actualCall("foo");
mock().actualCall("foo");
mock().actualCall("bar");

CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}

TEST(MockCallTest, ignoreOtherStillFailsIfExpectedOneDidntHappen)
{
MockFailureReporterInstaller failureReporterInstaller;
Expand All @@ -314,6 +342,21 @@ TEST(MockCallTest, ignoreOtherStillFailsIfExpectedOneDidntHappen)
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}

TEST(MockCallTest, ignoreAdditionalStillFailsIfExpectedOneDidntHappen)
{
MockFailureReporterInstaller failureReporterInstaller;

MockExpectedCallsListForTest expectations;
expectations.addFunction("foo");
MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations);

mock().expectOneCall("foo");
mock().ignoreAdditionalCalls();
mock().checkExpectations();

CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}

TEST(MockCallTest, threeExpectedAndActual)
{
mock().expectOneCall("function1");
Expand Down
21 changes: 21 additions & 0 deletions tests/CppUTestExt/MockHierarchyTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,27 @@ TEST(MockHierarchyTest, ignoreOtherCallsWorksHierarchicallyWhenDynamicallyCreate
mock().checkExpectations();
}

TEST(MockHierarchyTest, ignoreAdditionalCallsWorksHierarchically)
{
mock("first").expectOneCall("boo");
mock().ignoreAdditionalCalls();
mock("first").actualCall("boo");
mock("first").actualCall("boo");

mock().checkExpectations();
}

TEST(MockHierarchyTest, ignoreAdditionalCallsWorksHierarchicallyWhenDynamicallyCreated)
{
mock().ignoreAdditionalCalls();
mock("first").expectOneCall("boo");
mock("first").actualCall("boo");
mock("first").actualCall("boo");

mock().checkExpectations();
}


TEST(MockHierarchyTest, checkExpectationsWorksHierarchicallyForLastCallNotFinished)
{
MockFailureReporterInstaller failureReporterInstaller;
Expand Down
10 changes: 10 additions & 0 deletions tests/CppUTestExt/MockSupport_cTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -650,3 +650,13 @@ TEST(MockSupport_c, ignoreOtherCalls)
mock_c()->actualCall("bar");
mock_c()->checkExpectations();
}

TEST(MockSupport_c, ignoreAdditionalCalls)
{
mock_c()->expectOneCall("foo");
mock_c()->ignoreAdditionalCalls();
mock_c()->actualCall("foo");
mock_c()->actualCall("foo");
mock_c()->checkExpectations();
}