Skip to content

Commit

Permalink
Added setting of Data functionality and hierarical MockSupport scope
Browse files Browse the repository at this point in the history
  • Loading branch information
basvodde committed Aug 11, 2010
1 parent 5fc1428 commit bcf6612
Show file tree
Hide file tree
Showing 6 changed files with 305 additions and 204 deletions.
26 changes: 12 additions & 14 deletions examples/ApplicationLib/EventDispatcherTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@
#include "CppUTestExt/MockSupport.h"
#include "EventDispatcher.h"

MockSupport mock;

class ObserverMock : public EventObserver
{
public:
virtual void notify(const Event& event, int timeOutInSeconds)
{
mock.actualCall("notify").withParameterOfType("Event", "event", (void*) &event).withParameter("timeOutInSeconds", timeOutInSeconds);
mock().actualCall("notify").withParameterOfType("Event", "event", (void*) &event).withParameter("timeOutInSeconds", timeOutInSeconds);
}
virtual void notifyRegistration(EventObserver* newObserver)
{
mock.actualCall("notifyRegistration").withParameter("newObserver", newObserver);
mock().actualCall("notifyRegistration").withParameter("newObserver", newObserver);
}
};

Expand Down Expand Up @@ -42,49 +40,49 @@ TEST_GROUP(EventDispatcher)
void setup()
{
dispatcher = new EventDispatcher;
mock.installComparator("Event", eventComparator);
mock().installComparator("Event", eventComparator);
}
void teardown()
{
delete dispatcher;
mock.removeAllComparators();
mock().removeAllComparators();
}
};


TEST(EventDispatcher, EventWithoutRegistrationsResultsIntoNoCalls)
{
dispatcher->dispatchEvent(event, 10);
mock.checkExpectations();
mock().checkExpectations();
}

TEST(EventDispatcher, EventWithRegistrationForEventResultsIntoCallback)
{
mock.expectOneCall("notify").withParameterOfType("Event", "event", &event).withParameter("timeOutInSeconds", 10);
mock().expectOneCall("notify").withParameterOfType("Event", "event", &event).withParameter("timeOutInSeconds", 10);
event.type = IMPORTANT_EVENT;

dispatcher->registerObserver(IMPORTANT_EVENT, &observer);
dispatcher->dispatchEvent(event, 10);
mock.checkExpectations();
mock().checkExpectations();
}

TEST(EventDispatcher, DifferentEventWithRegistrationDoesNotResultIntoCallback)
{
event.type = LESS_IMPORTANT_EVENT;
dispatcher->registerObserver(IMPORTANT_EVENT, &observer);
dispatcher->dispatchEvent(event, 10);
mock.checkExpectations();
mock().checkExpectations();
}

TEST(EventDispatcher, RegisterTwoObserversResultIntoTwoCallsAndARegistrationNotification)
{
mock.expectOneCall("notify").withParameterOfType("Event", "event", &event).withParameter("timeOutInSeconds", 10);
mock.expectOneCall("notify").withParameterOfType("Event", "event", &event).withParameter("timeOutInSeconds", 10);
mock.expectOneCall("notifyRegistration").withParameter("newObserver", &observer);
mock().expectOneCall("notify").withParameterOfType("Event", "event", &event).withParameter("timeOutInSeconds", 10);
mock().expectOneCall("notify").withParameterOfType("Event", "event", &event).withParameter("timeOutInSeconds", 10);
mock().expectOneCall("notifyRegistration").withParameter("newObserver", &observer);

event.type = IMPORTANT_EVENT;
dispatcher->registerObserver(IMPORTANT_EVENT, &observer);
dispatcher->registerObserver(IMPORTANT_EVENT, &observer);
dispatcher->dispatchEvent(event, 10);
mock.checkExpectations();
mock().checkExpectations();
}
20 changes: 17 additions & 3 deletions include/CppUTestExt/MockSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,27 @@
#include "CppUTestExt/MockExpectedFunctionsList.h"

class Utest;
class MockSupport;

/* This allows access to "the global" mocking support for easier testing */
MockSupport& mock(const SimpleString& mockName = "");

class MockSupport
{
public:
MockSupport();
virtual ~MockSupport();

virtual void clear();

virtual MockFunctionCall& expectOneCall(const SimpleString& functionName);
virtual MockFunctionCall& actualCall(const SimpleString& functionName);

virtual void disable();
virtual void enable();
virtual void ignoreOtherCalls();

virtual void clearExpectations();
virtual bool expectedCallsLeft();

virtual void checkExpectations();

virtual void setMockFailureReporter(MockFailureReporter* reporter);
Expand All @@ -61,7 +65,14 @@ class MockSupport

bool hasData(const SimpleString& name);
void setData(const SimpleString& name, int value);
void setData(const SimpleString& name, const char* value);
void setData(const SimpleString& name, double value);
void setData(const SimpleString& name, void* value);
void setDataObject(const SimpleString& name, const SimpleString& type, void* value);
MockNamedValue getData(const SimpleString& name);

MockSupport* getMockSupportScope(const SimpleString& name);

protected:
virtual MockActualFunctionCall* createActualFunctionCall();

Expand All @@ -75,7 +86,10 @@ class MockSupport
MockActualFunctionCall* lastActualFunctionCall_;
MockNamedValueComparatorRepository comparatorRepository_;

MockNamedValue* data_;
MockNamedValueList* data_;

MockNamedValue* createAndStoreData(const SimpleString& name);

};

#endif
Expand Down
95 changes: 84 additions & 11 deletions src/CppUTestExt/MockSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@
#include "CppUTestExt/MockExpectedFunctionCall.h"
#include "CppUTestExt/MockFailure.h"

#define MOCK_SUPPORT_SCOPE_PREFIX "!!!$$$MockingSupportScope$$$!!!"

static MockSupport global_mock;

MockSupport& mock(const SimpleString& mockName)
{
if (mockName != "")
return *global_mock.getMockSupportScope(mockName);
return global_mock;
}


MockSupport::MockSupport()
: reporter_(&defaultReporter_), ignoreOtherCalls_(false), enabled_(true), lastActualFunctionCall_(NULL), data_(NULL)
{
Expand Down Expand Up @@ -60,14 +72,24 @@ void MockSupport::removeAllComparators()
comparatorRepository_.clear();
}

void MockSupport::clearExpectations()
void MockSupport::clear()
{
delete lastActualFunctionCall_;
lastActualFunctionCall_ = NULL;
expectations_.deleteAllExpectationsAndClearList();
ignoreOtherCalls_ = false;

if (data_) {
for (MockNamedValueListNode* p = data_->begin(); p; p = p->next())
if (p->getType() == "MockSupport" && p->getName().contains(MOCK_SUPPORT_SCOPE_PREFIX)) {
MockSupport* support = (MockSupport*) p->item()->getObjectPointer();
support->clear();
delete support;
}
data_->clear();
}
delete data_;
data_ = NULL;
}

MockFunctionCall& MockSupport::expectOneCall(const SimpleString& functionName)
Expand Down Expand Up @@ -119,7 +141,6 @@ void MockSupport::enable()
enabled_ = true;
}


bool MockSupport::expectedCallsLeft()
{
return expectations_.hasUnfullfilledExpectations();
Expand All @@ -134,28 +155,80 @@ void MockSupport::checkExpectations()
}
else if (expectedCallsLeft()) {
MockExpectedCallsDidntHappenFailure failure(reporter_->getTestToFail(), expectations_);
clearExpectations();
clear();
reporter_->failTest(failure);
}
clearExpectations();
clear();
}

bool MockSupport::hasData(const SimpleString& name)
{
return data_ && data_->getName() == name;
if (data_ == NULL)
return false;
return data_->getValueByName(name) != NULL;
}


MockNamedValue* MockSupport::createAndStoreData(const SimpleString& name)
{
if (data_ == NULL)
data_ = new MockNamedValueList;

MockNamedValue* newData = new MockNamedValue(name);
data_->add(newData);
return newData;
}

void MockSupport::setData(const SimpleString& name, int value)
{
data_ = new MockNamedValue(name);
data_->setValue(value);
MockNamedValue* newData = createAndStoreData(name);
newData->setValue(value);
}

void MockSupport::setData(const SimpleString& name, const char* value)
{
MockNamedValue* newData = createAndStoreData(name);
newData->setValue(value);
}

void MockSupport::setData(const SimpleString& name, double value)
{
MockNamedValue* newData = createAndStoreData(name);
newData->setValue(value);
}

void MockSupport::setData(const SimpleString& name, void* value)
{
MockNamedValue* newData = createAndStoreData(name);
newData->setValue(value);
}

void MockSupport::setDataObject(const SimpleString& name, const SimpleString& type, void* value)
{
MockNamedValue* newData = createAndStoreData(name);
newData->setObjectPointer(type, value);
}

MockNamedValue MockSupport::getData(const SimpleString& name)
{
name.size();
if (data_)
return *data_;
return MockNamedValue("");
if (data_ == NULL)
return MockNamedValue("");

MockNamedValue* value = data_->getValueByName(name);
if (value == NULL)
return MockNamedValue("");
return *value;
}

MockSupport* MockSupport::getMockSupportScope(const SimpleString& name)
{
SimpleString mockingSupportName = MOCK_SUPPORT_SCOPE_PREFIX;
mockingSupportName += name;

if (!hasData(mockingSupportName))
setDataObject(mockingSupportName, "MockSupport", new MockSupport);

STRCMP_EQUAL("MockSupport", getData(mockingSupportName).getType().asCharString());
return (MockSupport*) getData(mockingSupportName).getObjectPointer();
}

Loading

0 comments on commit bcf6612

Please sign in to comment.