Skip to content

Commit

Permalink
Merge pull request #893 from cuitoldfish/Option2ForAppendScopeName
Browse files Browse the repository at this point in the history
fix #884 Option2 for append scope name
  • Loading branch information
basvodde committed Mar 4, 2016
2 parents 5654970 + 41ecd64 commit 1d50e8f
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 25 deletions.
10 changes: 7 additions & 3 deletions include/CppUTestExt/MockSupport.h 100644 → 100755
Expand Up @@ -42,7 +42,7 @@ MockSupport& mock(const SimpleString& mockName = "", MockFailureReporter* failur
class MockSupport
{
public:
MockSupport();
MockSupport(const SimpleString& mockName = "");
virtual ~MockSupport();

virtual void strictOrder();
Expand Down Expand Up @@ -115,7 +115,7 @@ class MockSupport
virtual void removeAllComparatorsAndCopiers();

protected:
MockSupport* clone();
MockSupport* clone(const SimpleString& mockName);
virtual MockCheckedActualCall *createActualFunctionCall();
virtual void failTest(MockFailure& failure);
void countCheck();
Expand All @@ -135,7 +135,8 @@ class MockSupport
MockExpectedCallComposite compositeCalls_;
MockNamedValueComparatorsAndCopiersRepository comparatorsAndCopiersRepository_;
MockNamedValueList data_;

const SimpleString mockName_;

bool tracing_;

void checkExpectationsOfLastCall();
Expand All @@ -150,6 +151,9 @@ class MockSupport
bool hasntExpectationWithName(const SimpleString& functionName);
bool hasntUnexpectationWithName(const SimpleString& functionName);
bool hasCallsOutOfOrder();

SimpleString appendScopeToName(const SimpleString& functionName);

};

#endif
Expand Down
26 changes: 17 additions & 9 deletions src/CppUTestExt/MockSupport.cpp 100644 → 100755
Expand Up @@ -43,8 +43,8 @@ MockSupport& mock(const SimpleString& mockName, MockFailureReporter* failureRepo
return mock_support;
}

MockSupport::MockSupport()
: callOrder_(0), expectedCallOrder_(0), strictOrdering_(false), standardReporter_(&defaultReporter_), ignoreOtherCalls_(false), enabled_(true), lastActualFunctionCall_(NULL), tracing_(false)
MockSupport::MockSupport(const SimpleString& mockName)
: callOrder_(0), expectedCallOrder_(0), strictOrdering_(false), standardReporter_(&defaultReporter_), ignoreOtherCalls_(false), enabled_(true), lastActualFunctionCall_(NULL), mockName_(mockName), tracing_(false)
{
setActiveReporter(NULL);
}
Expand Down Expand Up @@ -142,14 +142,20 @@ void MockSupport::strictOrder()
strictOrdering_ = true;
}

SimpleString MockSupport::appendScopeToName(const SimpleString& functionName)
{
if (mockName_.isEmpty()) return functionName;
return mockName_ + "::" + functionName;
}

MockExpectedCall& MockSupport::expectOneCall(const SimpleString& functionName)
{
if (!enabled_) return MockIgnoredExpectedCall::instance();

countCheck();

MockCheckedExpectedCall* call = new MockCheckedExpectedCall;
call->withName(functionName);
call->withName(appendScopeToName(functionName));
if (strictOrdering_)
call->withCallOrder(++expectedCallOrder_);
expectations_.addExpectedCall(call);
Expand Down Expand Up @@ -194,22 +200,24 @@ bool MockSupport::hasntUnexpectationWithName(const SimpleString& functionName)

MockActualCall& MockSupport::actualCall(const SimpleString& functionName)
{
const SimpleString scopeFuntionName = appendScopeToName(functionName);

if (lastActualFunctionCall_) {
lastActualFunctionCall_->checkExpectations();
delete lastActualFunctionCall_;
lastActualFunctionCall_ = NULL;
}

if (!enabled_) return MockIgnoredActualCall::instance();
if (tracing_) return MockActualCallTrace::instance().withName(functionName);
if (tracing_) return MockActualCallTrace::instance().withName(scopeFuntionName);


if (hasntUnexpectationWithName(functionName) && hasntExpectationWithName(functionName)) {
if (hasntUnexpectationWithName(scopeFuntionName) && hasntExpectationWithName(scopeFuntionName)) {
return MockIgnoredActualCall::instance();
}

MockCheckedActualCall* call = createActualFunctionCall();
call->withName(functionName);
call->withName(scopeFuntionName);
return *call;
}

Expand Down Expand Up @@ -417,9 +425,9 @@ MockNamedValue MockSupport::getData(const SimpleString& name)
return *value;
}

MockSupport* MockSupport::clone()
MockSupport* MockSupport::clone(const SimpleString& mockName)
{
MockSupport* newMock = new MockSupport;
MockSupport* newMock = new MockSupport(mockName);
newMock->setMockFailureStandardReporter(standardReporter_);
if (ignoreOtherCalls_) newMock->ignoreOtherCalls();

Expand All @@ -442,7 +450,7 @@ MockSupport* MockSupport::getMockSupportScope(const SimpleString& name)
return (MockSupport*) getData(mockingSupportName).getObjectPointer();
}

MockSupport *newMock = clone();
MockSupport *newMock = clone(name);

setDataObject(mockingSupportName, "MockSupport", newMock);
return newMock;
Expand Down
57 changes: 57 additions & 0 deletions tests/CppUTestExt/MockCallTest.cpp
Expand Up @@ -78,6 +78,22 @@ TEST(MockCallTest, checkExpectationsClearsTheExpectations)
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}

TEST(MockCallTest, expectOneCallInScopeButNotHappen)
{

MockFailureReporterInstaller failureReporterInstaller;

MockExpectedCallsListForTest expectations;
expectations.addFunction("scope::foobar");
MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations);

mock("scope").expectOneCall("foobar");
mock().checkExpectations();

CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);

}

TEST(MockCallTest, unexpectedCallHappened)
{
MockFailureReporterInstaller failureReporterInstaller;
Expand All @@ -90,6 +106,47 @@ TEST(MockCallTest, unexpectedCallHappened)
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}

TEST(MockCallTest, unexpectedScopeCallHappened)
{
MockFailureReporterInstaller failureReporterInstaller;

MockExpectedCallsListForTest emptyExpectations;
MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "scope::func", emptyExpectations);

mock("scope").actualCall("func");

CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}

TEST(MockCallTest, expectOneCallInOneScopeButActualCallInAnotherScope)
{
MockFailureReporterInstaller failureReporterInstaller;

MockExpectedCallsListForTest emptyExpectations;
MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "class::foo", emptyExpectations);

mock("scope").expectOneCall("foo");
mock("class").actualCall("foo");

CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
mock().clear();
}

TEST(MockCallTest, expectOneCallInScopeButActualCallInGlobal)
{
MockFailureReporterInstaller failureReporterInstaller;

MockExpectedCallsListForTest emptyExpectations;
MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "foo", emptyExpectations);

mock("scope").expectOneCall("foo");
mock().actualCall("foo");

CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
mock().clear();
}


TEST(MockCallTest, expectMultipleCallsThatHappen)
{
mock().expectOneCall("foo");
Expand Down
10 changes: 5 additions & 5 deletions tests/CppUTestExt/MockHierarchyTest.cpp
Expand Up @@ -94,8 +94,8 @@ TEST(MockHierarchyTest, checkExpectationsWorksHierarchically)
MockFailureReporterInstaller failureReporterInstaller;

MockExpectedCallsListForTest expectations;
expectations.addFunction("foobar");
expectations.addFunction("helloworld");
expectations.addFunction("first::foobar");
expectations.addFunction("second::helloworld");
MockExpectedCallsDidntHappenFailure expectedFailure(mockFailureTest(), expectations);

mock("first").expectOneCall("foobar");
Expand Down Expand Up @@ -127,8 +127,8 @@ TEST(MockHierarchyTest, checkExpectationsWorksHierarchicallyForLastCallNotFinish
MockFailureReporterInstaller failureReporterInstaller;

MockExpectedCallsListForTest expectations;
expectations.addFunction("foobar")->withParameter("boo", 1);
MockExpectedParameterDidntHappenFailure expectedFailure(mockFailureTest(), "foobar", expectations);
expectations.addFunction("first::foobar")->withParameter("boo", 1);
MockExpectedParameterDidntHappenFailure expectedFailure(mockFailureTest(), "first::foobar", expectations);

mock("first").expectOneCall("foobar").withParameter("boo", 1);
mock("first").actualCall("foobar");
Expand All @@ -144,7 +144,7 @@ TEST(MockHierarchyTest, reporterIsInheritedInHierarchicalMocks)

mock("differentScope").actualCall("foobar");

MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "foobar", expectations);
MockUnexpectedCallHappenedFailure expectedFailure(mockFailureTest(), "differentScope::foobar", expectations);
CHECK_EXPECTED_MOCK_FAILURE(expectedFailure);
}

4 changes: 2 additions & 2 deletions tests/CppUTestExt/MockPluginTest.cpp
Expand Up @@ -75,8 +75,8 @@ TEST(MockPlugin, checkExpectationsWorksAlsoWithHierachicalObjects)
MockFailureReporterInstaller failureReporterInstaller;

MockExpectedCallsListForTest expectations;
expectations.addFunction("foobar")->onObject((void*) 1);
MockExpectedObjectDidntHappenFailure expectedFailure(test, "foobar", expectations);
expectations.addFunction("differentScope::foobar")->onObject((void*) 1);
MockExpectedObjectDidntHappenFailure expectedFailure(test, "differentScope::foobar", expectations);

mock("differentScope").expectOneCall("foobar").onObject((void*) 1);
mock("differentScope").actualCall("foobar");
Expand Down
12 changes: 6 additions & 6 deletions tests/CppUTestExt/MockStrictOrderTest.cpp
Expand Up @@ -89,8 +89,8 @@ TEST(MockStrictOrderTest, orderViolatedWorksHierarchically)
mock("bla").strictOrder();

MockExpectedCallsListForTest expectations;
expectations.addFunction("foo1", 1)->callWasMade(2);
expectations.addFunction("foo2", 2)->callWasMade(1);
expectations.addFunction("foo::foo1", 1)->callWasMade(2);
expectations.addFunction("foo::foo2", 2)->callWasMade(1);
MockCallOrderFailure expectedFailure(mockFailureTest(), expectations);

mock("bla").expectOneCall("foo1");
Expand All @@ -113,8 +113,8 @@ TEST(MockStrictOrderTest, orderViolatedWorksWithExtraUnexpectedCall)
mock().ignoreOtherCalls();

MockExpectedCallsListForTest expectations;
expectations.addFunction("foo1", 1)->callWasMade(2);
expectations.addFunction("foo2", 2)->callWasMade(1);
expectations.addFunction("foo::foo1", 1)->callWasMade(2);
expectations.addFunction("foo::foo2", 2)->callWasMade(1);
MockCallOrderFailure expectedFailure(mockFailureTest(), expectations);

mock("bla").expectOneCall("foo1");
Expand All @@ -137,8 +137,8 @@ TEST(MockStrictOrderTest, orderViolatedWithinAScope)
mock().strictOrder();

MockExpectedCallsListForTest expectations;
expectations.addFunction("foo1", 1)->callWasMade(2);
expectations.addFunction("foo2", 2)->callWasMade(1);
expectations.addFunction("scope::foo1", 1)->callWasMade(2);
expectations.addFunction("scope::foo2", 2)->callWasMade(1);
MockCallOrderFailure expectedFailure(mockFailureTest(), expectations);

mock("scope").expectOneCall("foo1");
Expand Down

0 comments on commit 1d50e8f

Please sign in to comment.