Click here to Skip to main content
15,880,956 members
Articles / Desktop Programming / MFC

Unit Test's Reporter

Rate me:
Please Sign up or sign in to vote.
4.86/5 (28 votes)
19 Feb 2006CPOL11 min read 64.6K   2.8K   58  
The article describes an application built to visualise testing processes for the unit test framework of CppUnitLite.
#ifndef	_INTERFACE_CPP
#define	_INTERFACE_CPP

#define	WITHOUT_WINDOWS_DEFINITIONS
#include "TransportInterface.h"

#include "Logger.h"
#include "TestObject.h"
#include "Composite.h"
#include "MockComposite.h"
#include "MockEnvironment.h"
#include "MockEnvironmentObjectCreator.h"
#include "MockEnvironmentObjectCreatorExt.h"

Logger	__logger;

typedef	struct	_session_
{
	IEnvironmentObjectCreator<TestObject>*	pEnvironmentObjectCreator;
	MockEnvironment<TestObject>*			pEnvironment;
}
TestSession;

TEST_SESSION	StartSession()
{
	TestSession*	result	= null;

	result	= new TestSession;
	if (!result
	)	return	result
	;

	result->pEnvironmentObjectCreator	=
		new MockEnvironmentObjectCreatorExt<TestObject>(&__logger);
	if (!result->pEnvironmentObjectCreator)
	{
		FinalSession(reinterpret_cast<void**>(&result));
		result	= null;	
		return	result;
	}

	result->pEnvironment	=
		new MockEnvironment<TestObject>(result->pEnvironmentObjectCreator,&__logger);
	if (!result->pEnvironment)
	{
		FinalSession(reinterpret_cast<void**>(&result));
		result	= null;	
		return	result;
	}

//	Clear tree...
	return	result;
}

bool	FinalSession(TEST_SESSION* session)
{
	bool	result	= false;
	if (!session
	)	return	result
	;
	if (!*session
	)	return	result
	;
	TestSession
		*pSession	= static_cast<TestSession*>(*session);
	if (pSession->pEnvironment)
		delete	pSession->pEnvironment;
	if (pSession->pEnvironmentObjectCreator)
		delete	pSession->pEnvironmentObjectCreator;
	delete	pSession;

	result	= true;

	return	result;
}

TEST_OBJECT	InsertObject(
			TEST_SESSION session,
			TEST_OBJECT parent,
			char*		name,
			char*		image,
			ObjectType	type)
{
	TEST_OBJECT				result		= null;
	IComponent<TestObject>*	pComponent	= null
	;
	if (!session
	)	return	result
	;
	TestSession	*pSession	= static_cast<TestSession*>(session);
	if (!pSession->pEnvironment
	)	return	result
	;

	MockEnvironment<TestObject>*
		mockEnvironment = static_cast<MockEnvironment<TestObject>*>(pSession->pEnvironment);
	
	TestObject	testObject(std::string(name),std::string(image),std::string(""),std::string(""));

	if (type == Macro)
	{
		pComponent	= new Leaf<TestObject>(testObject,pSession->pEnvironment);	
		if (!pComponent
		)	return	result
		;
		pComponent->Object().SetOwner(pComponent);
		IEnvironmentObject<TestObject>*
		pEnvironmentObjectComposite	= pSession->pEnvironmentObjectCreator->Create(pComponent->Object());
		if (!pEnvironmentObjectComposite)
		{
			delete	pComponent;
			return	result;
		}
	}
	else
	{
		pComponent = new Composite<TestObject>(testObject,pSession->pEnvironment);	
		if (!pComponent
		)	return	result
		;
		pComponent->Object().SetOwner(pComponent);
		
		IEnvironmentObject<TestObject>*
		pEnvironmentObjectComposite	= pSession->pEnvironmentObjectCreator->Create(pComponent->Object());
		if (!pEnvironmentObjectComposite)
		{
			delete	pComponent;
			return	result;
		}

		if (type == Application)
		{
			MockEnvironmentObjectExt<TestObject>*
				pObject	= static_cast<MockEnvironmentObjectExt<TestObject>*>(pComponent->LinkedObject());
			IComponent<int>*	entityObject = pObject->Entity();
			mockEnvironment->TreeControl()->Add(entityObject);
		}
	}

	if (parent)
	{
		IComponent<TestObject>*	pParent	= static_cast<IComponent<TestObject>*>(parent);
		pParent->Add(pComponent);
	}

	result	= pComponent;

	return	result;
}

bool	UpdateObject(
		TEST_OBJECT object,
		char*		name,
		char*		image,
		char*		entity)
{
	bool	result	= false;
	IComponent<TestObject>*	pComponent	= null
	;

	if (!object)
	{
		return	result;
	}

	pComponent	= static_cast<IComponent<TestObject>*>(object);

	pComponent->Object().SetImage	(std::string(name));
	pComponent->Object().SetIdent	(std::string(image));
	pComponent->Object().SetEntity	(std::string(entity));

	result	= true;

	return	result;
}

bool	DestroyObject(
		TEST_OBJECT object)
{
	bool	result	= false;
	if (!object
	)	return	result
	;
	IComponent<TestObject>*
		pComponent	= static_cast<IComponent<TestObject>*>(object)
	;
	delete pComponent
	;
	result	= true
	;
	return	result;
}

#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Israel Israel
MSc in System Engineering from Tallinn Technical University, Estonia. Currently, I work in a hitech enterprise in Israel.

Comments and Discussions