 |
 |
|
Rajinder Yadav - Windows C++ Development Tools & Resources :Design, Code, Test, Deploy |
// Source: StreamLogger.cpp
// Author: Rajinder Yadav
// Date: July 4, 2004
//
// Copyright (c) Rajinder Yadav 2004, 2007
//
// web: http://devmentor.org
// email: rajinder@devmentor.org
//
#include "stdafx.h"
#include "UnitTestAssembly.h"
#include "IUnitTestRunner.h"
#include "IUnitTestObserver.h"
#include "UnitTestRunner.h"
#include "StreamLogger.h"
using std::endl;
StreamLogger::StreamLogger() :
m_nTestRun( 0 ),
m_nFailed( 0 ),
m_nExceptions( 0 )
{
}
StreamLogger::StreamLogger( std::wstring strFilename ) :
m_strFilename( strFilename ),
m_nTestRun( 0 ),
m_nFailed( 0 ),
m_nExceptions( 0 )
{
Open( strFilename );
}
StreamLogger::~StreamLogger()
{
if( m_LogFile.is_open() ) {
Close();
}
}
void StreamLogger::Open( std::wstring strFilename )
{
m_nTestRun = 0;
m_nFailed = 0;
m_nExceptions = 0;
m_strFilename = strFilename;
m_LogFile.open( m_strFilename.c_str() );
}
void StreamLogger::Close()
{
m_LogFile
<< "----------------" << endl
<< "Overall Summary" << endl
<< "----------------" << endl
<< "Total Run: " << m_nTestRun << endl
<< "Total Failed: " << m_nFailed << endl
<< "Total Exceptions: " << m_nExceptions << endl;
if ( m_LogFile.is_open() )
{
m_LogFile.flush();
m_LogFile.close();
}
}
void StreamLogger::UnitTestStart(IUnitTestRunner& subject)
{
try
{
m_LogFile
<< "-----------------------------------------" << endl
<< "Test Runner: " << dynamic_cast<UnitTestRunner&>(subject).ClassNameIs() << endl
<< "-----------------------------------------" << endl
<< endl
<< "Unit Test Started" << endl;
}
catch(std::bad_cast e)
{
_ASSERT(0); // something is wrong!
}
}
void StreamLogger::Failed(IUnitTestRunner& subject)
{
try
{
m_LogFile
<< dynamic_cast<UnitTestRunner&>(subject).TestMsgIs()
<< endl;
}
catch(std::bad_cast e)
{
_ASSERT(0); // something is wrong!
}
}
void StreamLogger::Passed(IUnitTestRunner& subject)
{
try
{
m_LogFile
<< dynamic_cast<UnitTestRunner&>(subject).TestMsgIs()
<< endl;
}
catch(std::bad_cast e)
{
_ASSERT(0); // something is wrong!
}
}
void StreamLogger::Exception(IUnitTestRunner& subject)
{
try
{
m_LogFile
<< dynamic_cast<UnitTestRunner&>(subject).TestMsgIs()
<< endl;
}
catch(std::bad_cast e)
{
_ASSERT(0); // something is wrong!
}
}
void StreamLogger::UnitTestEnd(IUnitTestRunner& subject)
{
try
{
m_LogFile
<< "Unit Test complete. "
<< endl << endl
<< "Test Summary: Tests("
<< dynamic_cast<UnitTestRunner&>(subject).TestCountIs() << ")"
<< " Fails("
<< dynamic_cast<UnitTestRunner&>(subject).FailedCountIs() << ")"
<< " Exceptions("
<< dynamic_cast<UnitTestRunner&>(subject).ExceptionCountIs() << ")"
<< endl << endl;
m_nTestRun += dynamic_cast<UnitTestRunner&>(subject).TestCountIs();
m_nFailed += dynamic_cast<UnitTestRunner&>(subject).FailedCountIs();
m_nExceptions += dynamic_cast<UnitTestRunner&>(subject).ExceptionCountIs();
}
catch(std::bad_cast e)
{
_ASSERT(0); // something is wrong!
}
}
Back
Copyright © 2007 Rajinder Yadav, All rights reserved