From 7231844e3725b436d8cadd19caddb5e491364e37 Mon Sep 17 00:00:00 2001 From: MA Xilai Date: Thu, 19 May 2016 22:32:06 +0800 Subject: [PATCH] ignoreAdditionalCalls (ignoreMultipleCalls?) --- include/CppUTestExt/MockSupport.h | 3 ++ include/CppUTestExt/MockSupport_c.h | 1 + src/CppUTestExt/MockSupport.cpp | 28 ++++++++++++++- src/CppUTestExt/MockSupport_c.cpp | 7 ++++ tests/CppUTestExt/MockCallTest.cpp | 45 ++++++++++++++++++++++++- tests/CppUTestExt/MockHierarchyTest.cpp | 21 ++++++++++++ tests/CppUTestExt/MockSupport_cTest.cpp | 10 ++++++ 7 files changed, 113 insertions(+), 2 deletions(-) diff --git a/include/CppUTestExt/MockSupport.h b/include/CppUTestExt/MockSupport.h index e58bf2a57..7e3a7a279 100755 --- a/include/CppUTestExt/MockSupport.h +++ b/include/CppUTestExt/MockSupport.h @@ -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(); @@ -133,6 +134,7 @@ class MockSupport MockExpectedCallsList expectations_; MockExpectedCallsList unExpectations_; bool ignoreOtherCalls_; + bool ignoreAdditionalCalls_; bool enabled_; MockCheckedActualCall *lastActualFunctionCall_; MockExpectedCallComposite compositeCalls_; @@ -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); diff --git a/include/CppUTestExt/MockSupport_c.h b/include/CppUTestExt/MockSupport_c.h index 0706d5472..cca2c7f04 100644 --- a/include/CppUTestExt/MockSupport_c.h +++ b/include/CppUTestExt/MockSupport_c.h @@ -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); diff --git a/src/CppUTestExt/MockSupport.cpp b/src/CppUTestExt/MockSupport.cpp index fb307652e..53f621569 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) + : callOrder_(0), expectedCallOrder_(0), strictOrdering_(false), standardReporter_(&defaultReporter_), ignoreOtherCalls_(false), ignoreAdditionalCalls_(false), enabled_(true), lastActualFunctionCall_(NULL), mockName_(mockName), tracing_(false) { setActiveReporter(NULL); } @@ -122,6 +122,7 @@ void MockSupport::clear() unExpectations_.deleteAllExpectationsAndClearList(); compositeCalls_.clear(); ignoreOtherCalls_ = false; + ignoreAdditionalCalls_ = false; enabled_ = true; callOrder_ = 0; expectedCallOrder_ = 0; @@ -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); @@ -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; @@ -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; @@ -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(); diff --git a/src/CppUTestExt/MockSupport_c.cpp b/src/CppUTestExt/MockSupport_c.cpp index 485b40fe9..c9b3015a5 100644 --- a/src/CppUTestExt/MockSupport_c.cpp +++ b/src/CppUTestExt/MockSupport_c.cpp @@ -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); @@ -339,6 +340,7 @@ static MockSupport_c gMockSupport = { disable_c, enable_c, ignoreOtherCalls_c, + ignoreAdditionalCalls_c, checkExpectations_c, expectedCallsLeft_c, clear_c, @@ -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)); diff --git a/tests/CppUTestExt/MockCallTest.cpp b/tests/CppUTestExt/MockCallTest.cpp index 44f3f7829..6b5da1022 100644 --- a/tests/CppUTestExt/MockCallTest.cpp +++ b/tests/CppUTestExt/MockCallTest.cpp @@ -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; @@ -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; @@ -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"); diff --git a/tests/CppUTestExt/MockHierarchyTest.cpp b/tests/CppUTestExt/MockHierarchyTest.cpp index 800ec74dc..6056bef7d 100644 --- a/tests/CppUTestExt/MockHierarchyTest.cpp +++ b/tests/CppUTestExt/MockHierarchyTest.cpp @@ -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; diff --git a/tests/CppUTestExt/MockSupport_cTest.cpp b/tests/CppUTestExt/MockSupport_cTest.cpp index 8fc55ec70..639a871ca 100644 --- a/tests/CppUTestExt/MockSupport_cTest.cpp +++ b/tests/CppUTestExt/MockSupport_cTest.cpp @@ -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(); +} +