Skip to content

Commit

Permalink
Refactored the NamedValue for Mocking. This way it will be better to …
Browse files Browse the repository at this point in the history
…add new features
  • Loading branch information
basvodde committed Aug 10, 2010
1 parent 94f4086 commit 6a3b933
Show file tree
Hide file tree
Showing 19 changed files with 310 additions and 203 deletions.
2 changes: 1 addition & 1 deletion examples/ApplicationLib/EventDispatcherTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ObserverMock : public EventObserver
}
};

class EventComparator : public MockParameterComparator
class EventComparator : public MockNamedValueComparator
{
public:
virtual bool isEqual(void* object1, void* object2)
Expand Down
4 changes: 2 additions & 2 deletions include/CppUTestExt/MockActualFunctionCall.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

class MockFailureReporter;
class MockFailure;
class MockFunctionParameter;
class MockNamedValue;

class MockActualFunctionCall : public MockFunctionCall
{
Expand All @@ -57,7 +57,7 @@ class MockActualFunctionCall : public MockFunctionCall
virtual Utest* getTest() const;
virtual void callHasSucceeded();
virtual void failTest(const MockFailure& failure);
virtual void checkActualParameter(const MockFunctionParameter& actualParameter);
virtual void checkActualParameter(const MockNamedValue& actualParameter);

enum ActualCallState {
CALL_IN_PROGESS,
Expand Down
51 changes: 23 additions & 28 deletions include/CppUTestExt/MockExpectedFunctionCall.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,7 @@

#include "CppUTestExt/MockFunctionCall.h"

union MockParameterValue{
int intValue_;
double doubleValue_;
const char* stringValue_;
void* pointerValue_;
void* objectPointerValue_;
};

struct MockFunctionParameter
{
MockFunctionParameter(const SimpleString& name, const SimpleString& type)
: name_(name), type_(type), fulfilled_(false), comparator_(NULL), nextParameter(NULL){}

SimpleString name_;
SimpleString type_;
MockParameterValue value_;
bool fulfilled_;
MockParameterComparator* comparator_;
MockFunctionParameter* nextParameter;
};


extern SimpleString StringFrom(const MockFunctionParameter& parameter);
extern SimpleString StringFrom(const MockNamedValue& parameter);

class MockExpectedFunctionCall : public MockFunctionCall
{
Expand All @@ -73,7 +51,7 @@ class MockExpectedFunctionCall : public MockFunctionCall
virtual SimpleString getParameterValueString(const SimpleString& name);

virtual bool hasParameterWithName(const SimpleString& name);
virtual bool hasParameter(const MockFunctionParameter& parameter);
virtual bool hasParameter(const MockNamedValue& parameter);
virtual bool relatesTo(const SimpleString& functionName);

virtual bool isFulfilled();
Expand All @@ -87,12 +65,29 @@ class MockExpectedFunctionCall : public MockFunctionCall
virtual SimpleString missingParametersToString();

private:

class MockExpectedFunctionParameter : public MockNamedValue
{
public:
MockExpectedFunctionParameter(const SimpleString& name)
: MockNamedValue(name), fulfilled_(false) {}

bool fulfilled_;
};

struct MockFunctionParameterNode
{
MockFunctionParameterNode(MockExpectedFunctionParameter* data) : data_(data), next_(NULL) {}
MockExpectedFunctionParameter* data_;
MockFunctionParameterNode* next_;
};

bool wasCallMade_;
MockFunctionParameter* parameters_;
MockFunctionParameterNode* parameters_;

MockFunctionParameter* addNewParameter(const SimpleString& name, const SimpleString& type);
bool parametersEqual(const MockFunctionParameter& p1, const MockFunctionParameter& p2);
MockFunctionParameter* getParameterByName(const SimpleString& name);
MockNamedValue* addNewParameter(const SimpleString& name);
bool parametersEqual(const MockNamedValue& p1, const MockNamedValue& p2);
MockNamedValue* getParameterByName(const SimpleString& name);

};

Expand Down
6 changes: 3 additions & 3 deletions include/CppUTestExt/MockExpectedFunctionsList.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#define D_MockExpectedFunctionsList_h

class MockExpectedFunctionCall;
class MockFunctionParameter;
class MockNamedValue;

class MockExpectedFunctionsList
{
Expand All @@ -53,11 +53,11 @@ class MockExpectedFunctionsList
virtual void addUnfilfilledExpectations(const MockExpectedFunctionsList& list);

virtual void onlyKeepExpectationsRelatedTo(const SimpleString& name);
virtual void onlyKeepExpectationsWithParameter(const MockFunctionParameter& parameter);
virtual void onlyKeepExpectationsWithParameter(const MockNamedValue& parameter);
virtual void onlyKeepExpectationsWithParameterName(const SimpleString& name);
virtual void onlyKeepUnfulfilledExpectations();
virtual void onlyKeepUnfulfilledExpectationsRelatedTo(const SimpleString& name);
virtual void onlyKeepUnfulfilledExpectationsWithParameter(const MockFunctionParameter& parameter);
virtual void onlyKeepUnfulfilledExpectationsWithParameter(const MockNamedValue& parameter);

virtual void removeOneFulfilledExpectation();

Expand Down
4 changes: 2 additions & 2 deletions include/CppUTestExt/MockFailure.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

class MockExpectedFunctionsList;
class MockActualFunctionCall;
class MockFunctionParameter;
class MockNamedValue;
class MockFailure;

class MockFailureReporter
Expand Down Expand Up @@ -77,7 +77,7 @@ class MockUnexpectedCallHappenedFailure : public MockFailure
class MockUnexpectedParameterFailure : public MockFailure
{
public:
MockUnexpectedParameterFailure(Utest* test, const SimpleString& functionName, const MockFunctionParameter& parameter, const MockExpectedFunctionsList& expectations);
MockUnexpectedParameterFailure(Utest* test, const SimpleString& functionName, const MockNamedValue& parameter, const MockExpectedFunctionsList& expectations);
virtual ~MockUnexpectedParameterFailure(){};
};

Expand Down
74 changes: 58 additions & 16 deletions include/CppUTestExt/MockFunctionCall.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,68 @@

/*
* MockParameterComparator is an interface that needs to be used when creating Comparators.
* This is needed when comparing parameters of non-native type.
*
* This is needed when comparing values of non-native type.
*/

class MockParameterComparator
class MockNamedValueComparator
{
public:
MockParameterComparator() {};
virtual ~MockParameterComparator() {};
MockNamedValueComparator() {};
virtual ~MockNamedValueComparator() {};

virtual bool isEqual(void* object1, void* object2)=0;
virtual SimpleString valueToString(void* object)=0;
};


/*
* MockNamedValue is the generic value class used. It encapsulates basic types and can use them "as if one"
* Also it enables other types by putting object pointers. They can be compared with comparators.
*
* Basically this class ties together a Name, a Value, a Type, and a Comparator
*/

class MockNamedValue
{
public:
MockNamedValue(const SimpleString& name);
virtual ~MockNamedValue();

virtual void setValue(int value);
virtual void setValue(double value);
virtual void setValue(void* value);
virtual void setValue(const char* value);
virtual void setObjectPointer(const SimpleString& type, void* objectPtr);

virtual void setComparator(MockNamedValueComparator* comparator);

virtual bool equals(const MockNamedValue& p) const;

virtual SimpleString toString() const;

virtual SimpleString getName() const;
virtual SimpleString getType() const;

virtual MockParameterValue getValue();
private:
SimpleString name_;
SimpleString type_;
union {
int intValue_;
double doubleValue_;
const char* stringValue_;
void* pointerValue_;
void* objectPointerValue_;
} value_;
MockNamedValueComparator* comparator_;
};

/*
* MockFunctionCall is the main interface for recording and actualizing mock calls.
*
*/

class MockParameterComparatorRepository;
class MockNamedValueComparatorRepository;
class MockFunctionCall
{
public:
Expand All @@ -63,32 +105,32 @@ class MockFunctionCall
virtual MockFunctionCall& withParameter(const SimpleString& name, void* value)=0;
virtual MockFunctionCall& withParameterOfType(const SimpleString& typeName, const SimpleString& name, void* value)=0;

virtual void setComparatorRepository(MockParameterComparatorRepository* repository);
virtual void setComparatorRepository(MockNamedValueComparatorRepository* repository);

protected:
void setName(const SimpleString& name);
SimpleString getName() const;
MockParameterComparator* getComparatorForType(const SimpleString& type) const;
MockNamedValueComparator* getComparatorForType(const SimpleString& type) const;
private:
SimpleString functionName_;
MockParameterComparatorRepository* comparatorRepository_;
MockNamedValueComparatorRepository* comparatorRepository_;
};

/*
* MockParameterComparatorRepository is a class which stores comparators which can be used for comparing non-native types
*
*/

struct MockParameterComparatorRepositoryNode;
class MockParameterComparatorRepository
struct MockNamedValueComparatorRepositoryNode;
class MockNamedValueComparatorRepository
{
MockParameterComparatorRepositoryNode* head_;
MockNamedValueComparatorRepositoryNode* head_;
public:
MockParameterComparatorRepository();
virtual ~MockParameterComparatorRepository();
MockNamedValueComparatorRepository();
virtual ~MockNamedValueComparatorRepository();

virtual void installComparator(const SimpleString& name, MockParameterComparator& comparator);
virtual MockParameterComparator* getComparatorForType(const SimpleString& name);
virtual void installComparator(const SimpleString& name, MockNamedValueComparator& comparator);
virtual MockNamedValueComparator* getComparatorForType(const SimpleString& name);

void clear();
};
Expand Down
4 changes: 2 additions & 2 deletions include/CppUTestExt/MockSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class MockSupport
virtual void checkExpectations();

virtual void setMockFailureReporter(MockFailureReporter* reporter);
virtual void installComparator(const SimpleString& typeName, MockParameterComparator& comparator);
virtual void installComparator(const SimpleString& typeName, MockNamedValueComparator& comparator);
virtual void removeAllComparators();

virtual void crashOnFailure();
Expand All @@ -70,7 +70,7 @@ class MockSupport
bool ignoreOtherCalls_;
bool enabled_;
MockActualFunctionCall* lastActualFunctionCall_;
MockParameterComparatorRepository comparatorRepository_;
MockNamedValueComparatorRepository comparatorRepository_;
};

#endif
Expand Down
26 changes: 13 additions & 13 deletions src/CppUTestExt/MockActualFunctionCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ MockFunctionCall& MockActualFunctionCall::withName(const SimpleString& name)
return *this;
}

void MockActualFunctionCall::checkActualParameter(const MockFunctionParameter& actualParameter)
void MockActualFunctionCall::checkActualParameter(const MockNamedValue& actualParameter)
{
unfulfilledExpectations_.onlyKeepUnfulfilledExpectationsWithParameter(actualParameter);

Expand All @@ -89,39 +89,39 @@ void MockActualFunctionCall::checkActualParameter(const MockFunctionParameter& a
return;
}

unfulfilledExpectations_.parameterWasPassed(actualParameter.name_);
unfulfilledExpectations_.parameterWasPassed(actualParameter.getName());
if (unfulfilledExpectations_.hasFulfilledExpectations())
callHasSucceeded();
}

MockFunctionCall& MockActualFunctionCall::withParameter(const SimpleString& name, int value)
{
MockFunctionParameter actualParameter(name, "int");
actualParameter.value_.intValue_ = value;
MockNamedValue actualParameter(name);
actualParameter.setValue(value);
checkActualParameter(actualParameter);
return *this;
}

MockFunctionCall& MockActualFunctionCall::withParameter(const SimpleString& name, double value)
{
MockFunctionParameter actualParameter(name, "double");
actualParameter.value_.doubleValue_ = value;
MockNamedValue actualParameter(name);
actualParameter.setValue(value);
checkActualParameter(actualParameter);
return *this;
}

MockFunctionCall& MockActualFunctionCall::withParameter(const SimpleString& name, const char* value)
{
MockFunctionParameter actualParameter(name, "char*");
actualParameter.value_.stringValue_ = value;
MockNamedValue actualParameter(name);
actualParameter.setValue(value);
checkActualParameter(actualParameter);
return *this;
}

MockFunctionCall& MockActualFunctionCall::withParameter(const SimpleString& name, void* value)
{
MockFunctionParameter actualParameter(name, "void*");
actualParameter.value_.pointerValue_ = value;
MockNamedValue actualParameter(name);
actualParameter.setValue(value);
checkActualParameter(actualParameter);
return *this;
}
Expand All @@ -133,9 +133,9 @@ MockFunctionCall& MockActualFunctionCall::withParameterOfType(const SimpleString
failTest(failure);
return *this;
}
MockFunctionParameter actualParameter(name, type);
actualParameter.value_.objectPointerValue_ = value;
actualParameter.comparator_ = getComparatorForType(type);
MockNamedValue actualParameter(name);
actualParameter.setObjectPointer(type, value);
actualParameter.setComparator(getComparatorForType(type));
checkActualParameter(actualParameter);
return *this;
}
Expand Down
Loading

0 comments on commit 6a3b933

Please sign in to comment.