Update of /cvsroot/cppunit/cppunit2/include/cpput
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27352/include/cpput
Added Files:
exceptionguard.h
Removed Files:
testfailureguard.h
Log Message:
* renamed testfailureguard.h/cpp to exceptionguard.h/cpp
--- testfailureguard.h DELETED ---
--- NEW FILE: exceptionguard.h ---
#ifndef CPPUT_TESTFAILUREGUARD_H_INCLUDED
# define CPPUT_TESTFAILUREGUARD_H_INCLUDED
# include <cpput/forwards.h>
# include <cpput/functor.h>
# include <deque>
/* Tests setUp(), tearDown(), run() call are protected by a ExceptionGuard.
*
* This guard chain ensures that any exceptions thrown by the call is caught and properly handled.
* The default guard chain handles AssertionException (thrown when an assertion fails),
* std::exception, and any other exception.
*
* A ExceptionGuardElement can be added to the guard chain to handle exception type unknown
* to the test framework which are not derived from std::exception (MFC CException * for example).
*/
namespace CppUT {
class CPPUT_API ExceptionGuardElement
{
public:
struct Context;
virtual ~ExceptionGuardElement()
{
}
void setNextInChain( const ExceptionGuardElementPtr &deleguate );
/// Returns \c false if a failure or a fault occurred, \c true otherwise.
virtual bool protect( Context &context ) = 0;
protected:
bool callNextInChain( Context &context );
private:
ExceptionGuardElementPtr deleguate_;
};
/** Helpers to provide simple exception translation.
*/
template<class ExceptionType
,class Translator>
class ExceptionTranslatorGuard : public ExceptionGuardElement
{
public:
ExceptionTranslatorGuard( Translator translator )
: translator_( translator )
{
}
// overridden from ExceptionGuardElement
bool protect( Context &context )
{
try
{
return callNextInChain( context );
}
catch ( const ExceptionType &e )
{
translator_( e );
return false;
}
}
private:
Translator translator_;
};
/**
* @see registerExceptionTranslation() to easily register custom exception translator.
*/
class CPPUT_API ExceptionGuard
{
public:
ExceptionGuard();
void append( const ExceptionGuardElementPtr &guard );
void removeLast();
bool protect( Functor0 test ) const;
private:
typedef std::deque<ExceptionGuardElementPtr> Guards;
Guards guards_;
};
/** Register an exception translator.
* @todo provides some helper to generate faul result...
* \code
* static void translateMFCException( CException *e ) {
* TCHAR szCause[255];
* ex.GetErrorMessage(szCause, 255);
* CString str( szCause );
*
* OpenTest::Properties fault;
* fault["type"] = "unexpected exception";
* fault["exception_type"] = "CException";
* fault["message"].append( (const char *)str );
* TestInfo::appendFaultToResult( fault );
* }
* \endcode
*/
template<class Translator
,class ExceptionType>
void registerExceptionTranslation( ExceptionGuard &guard,
Translator translator,
CppTL::Type<ExceptionType> )
{
typedef ExceptionTranslatorGuard<ExceptionType,Translator> GuardType;
guard.append( ExceptionGuardElementPtr( new GuardType( translator ) ) );
}
} // namespace CppUT
#endif // CPPUT_TESTFAILUREGUARD_H_INCLUDED
|