|
From: <bl...@us...> - 2003-05-01 18:53:23
|
Update of /cvsroot/cpptool/rfta/include/rfta/test
In directory sc8-pr-cvs1:/tmp/cvs-serv24787/include/rfta/test
Added Files:
UnitTesting.h
Log Message:
* introduced RFTA_CUSTOM_ASSERT
* moved common UnitTesting.h parts to include/rfta/test/UnitTesting.h
--- NEW FILE: UnitTesting.h ---
#ifndef RFTATEST_UNITTESTING_H
#define RFTATEST_UNITTESTING_H
#include <cppunit/extensions/HelperMacros.h>
#include <sstream>
#include <typeinfo>
namespace Refactoring
{
namespace Testing
{
template<typename FirstType
,typename SecondType
>
void checkEquals( const FirstType &expected,
const SecondType &actual,
const CppUnit::AdditionalMessage &message,
const CppUnit::SourceLine &sourceLine )
{
if ( expected == actual )
return;
std::ostringstream sExpected;
sExpected << expected;
std::ostringstream sActual;
sActual << actual;
CppUnit::Message fixedMessage;
if ( !message.shortDescription().empty() )
fixedMessage.addDetail( message.shortDescription() );
for ( int index =0; index < message.detailCount(); ++index )
fixedMessage.addDetail( message.detailAt( index ) );
CppUnit::Asserter::failNotEqual( sExpected.str(),
sActual.str(),
sourceLine,
fixedMessage );
}
inline void checkType( const std::type_info &expectedType,
const std::type_info &actualType,
const std::string &expression,
const CppUnit::SourceLine &sourceLine )
{
if ( expectedType == actualType )
return;
CppUnit::Message message( "expression is not of the expected type" );
message.addDetail( std::string("Expected type: ") + expectedType.name() );
message.addDetail( std::string("Actual type: ") + actualType.name() );
if ( !expression.empty() )
message.addDetail( "Expression: " + expression );
CppUnit::Asserter::fail( message, sourceLine );
}
struct AsserterBase
{
AsserterBase &setAssertLocation( const CppUnit::SourceLine &location )
{
location_ = location;
return *this;
}
AsserterBase &chain( const AsserterBase ¤tAsserter )
{
location_ = currentAsserter.location_;
return *this;
}
void fail( const CppUnit::AdditionalMessage &message = "" ) const
{
CppUnit::Asserter::fail( message, location_ );
}
void failIf( bool condition,
const CppUnit::AdditionalMessage &message = "" ) const
{
CppUnit::Asserter::failIf( condition, message, location_ );
}
template<typename FirstType
,typename SecondType>
void checkEquals( const FirstType &expected,
const SecondType &actual,
const CppUnit::AdditionalMessage &message = "")
{
::Refactoring::Testing::checkEquals( expected, actual, message, location_ );
}
CppUnit::SourceLine location_;
};
template<typename DerivedType>
struct Asserter : public AsserterBase
{
DerivedType &setAssertLocation( const CppUnit::SourceLine &location )
{
return static_cast<DerivedType &>( AsserterBase::setAssertLocation( location ) );
}
DerivedType &chain( const AsserterBase ¤tAsserter )
{
return static_cast<DerivedType &>( AsserterBase::chain( currentAsserter ) );
}
};
}
}
#define RFTA_CUSTOM_ASSERT( AsserterType ) \
AsserterType().setAssertLocation( CPPUNIT_SOURCELINE() )
#define RFTA_ASSERT_EQUAL( x, y ) \
Refactoring::Testing::checkEquals( x, y, "", CPPUNIT_SOURCELINE() )
#define RFTA_ASSERTM_EQUAL( x, y, message ) \
Refactoring::Testing::checkEquals( x, y, message, CPPUNIT_SOURCELINE() )
#define RFTA_ASSERT_THROW( expression, ExceptionType ) \
try { \
expression; \
{ \
CPPUNIT_NS::Message message( "excepted exception of type " \
#ExceptionType " not thrown.", \
"Expression: " #expression ); \
CPPUNIT_NS::Asserter::fail( message, CPPUNIT_SOURCELINE() ); \
} \
} catch ( ExceptionType & ) { \
}
#define RFTA_ASSERT_TYPE_IS( expectedType, expression ) \
Refactoring::Testing::checkType( typeid(expectedType), \
typeid(expression), \
#expression, \
CPPUNIT_SOURCELINE() )
#endif // RFTATEST_UNITTESTING_H
|