Improved exception reporting

Refactored exception handling (handle more in the translator)
and report exceptions in CHECKs
This commit is contained in:
Phil Nash 2012-02-17 19:50:59 +00:00
parent a57b025c9c
commit 333e6e6254
4 changed files with 106 additions and 48 deletions

View File

@ -684,8 +684,20 @@ inline bool isTrue
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_TEST( expr, isNot, stopOnFailure, macroName ) \ #define INTERNAL_CATCH_TEST( expr, isNot, stopOnFailure, macroName ) \
INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr, isNot )->*expr ), stopOnFailure ); \ try \
if( Catch::isTrue( false ) ){ bool internal_catch_dummyResult = ( expr ); Catch::isTrue( internal_catch_dummyResult ); } { \
INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr, isNot )->*expr ), stopOnFailure ); \
if( Catch::isTrue( false ) ){ bool internal_catch_dummyResult = ( expr ); Catch::isTrue( internal_catch_dummyResult ); } \
} \
catch( Catch::TestFailureException& ) \
{ \
throw; \
} \
catch( ... ) \
{ \
INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr ) << Catch::Hub::getExceptionTranslatorRegistry().translateActiveException() ).setResultType( Catch::ResultWas::ThrewException ), false ); \
throw; \
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_IF( expr, isNot, stopOnFailure, macroName ) \ #define INTERNAL_CATCH_IF( expr, isNot, stopOnFailure, macroName ) \
@ -704,10 +716,6 @@ inline bool isTrue
expr; \ expr; \
INTERNAL_CATCH_ACCEPT_EXPR( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr ).setResultType( Catch::ResultWas::Ok ), stopOnFailure ); \ INTERNAL_CATCH_ACCEPT_EXPR( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr ).setResultType( Catch::ResultWas::Ok ), stopOnFailure ); \
} \ } \
catch( std::exception& internal_catch_exception ) \
{ \
INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr ) << internal_catch_exception.what() ).setResultType( Catch::ResultWas::ThrewException ), stopOnFailure ); \
} \
catch( ... ) \ catch( ... ) \
{ \ { \
INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr ) << Catch::Hub::getExceptionTranslatorRegistry().translateActiveException() ).setResultType( Catch::ResultWas::ThrewException ), stopOnFailure ); \ INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr ) << Catch::Hub::getExceptionTranslatorRegistry().translateActiveException() ).setResultType( Catch::ResultWas::ThrewException ), stopOnFailure ); \

View File

@ -32,7 +32,26 @@ namespace Catch
() ()
const const
{ {
return tryTranslators( m_translators.begin() ); try
{
throw;
}
catch( std::exception& ex )
{
return ex.what();
}
catch( std::string& msg )
{
return msg;
}
catch( const char* msg )
{
return msg;
}
catch(...)
{
return tryTranslators( m_translators.begin() );
}
} }
/////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////

View File

@ -391,7 +391,7 @@ namespace Catch
m_reporter->EndTestCase( testInfo, m_successes - prevSuccessCount, m_failures - prevFailureCount, redirectedCout, redirectedCerr ); m_reporter->EndTestCase( testInfo, m_successes - prevSuccessCount, m_failures - prevFailureCount, redirectedCout, redirectedCerr );
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
virtual std::size_t getSuccessCount virtual std::size_t getSuccessCount
() ()
@ -601,21 +601,6 @@ namespace Catch
{ {
// This just means the test was aborted due to failure // This just means the test was aborted due to failure
} }
catch( std::exception& ex )
{
acceptMessage( ex.what() );
acceptResult( ResultWas::ThrewException );
}
catch( std::string& msg )
{
acceptMessage( msg );
acceptResult( ResultWas::ThrewException );
}
catch( const char* msg )
{
acceptMessage( msg );
acceptResult( ResultWas::ThrewException );
}
catch(...) catch(...)
{ {
acceptMessage( Catch::Hub::getExceptionTranslatorRegistry().translateActiveException() ); acceptMessage( Catch::Hub::getExceptionTranslatorRegistry().translateActiveException() );

View File

@ -208,6 +208,9 @@ namespace Catch
virtual ~IReporter virtual ~IReporter
(){} (){}
virtual bool shouldRedirectStdout
() const = 0;
virtual void StartTesting virtual void StartTesting
() = 0; () = 0;
@ -1895,8 +1898,20 @@ inline bool isTrue
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_TEST( expr, isNot, stopOnFailure, macroName ) \ #define INTERNAL_CATCH_TEST( expr, isNot, stopOnFailure, macroName ) \
INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr, isNot )->*expr ), stopOnFailure ); \ try \
if( Catch::isTrue( false ) ){ bool internal_catch_dummyResult = ( expr ); Catch::isTrue( internal_catch_dummyResult ); } { \
INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr, isNot )->*expr ), stopOnFailure ); \
if( Catch::isTrue( false ) ){ bool internal_catch_dummyResult = ( expr ); Catch::isTrue( internal_catch_dummyResult ); } \
} \
catch( Catch::TestFailureException& ) \
{ \
throw; \
} \
catch( ... ) \
{ \
INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr ) << Catch::Hub::getExceptionTranslatorRegistry().translateActiveException() ).setResultType( Catch::ResultWas::ThrewException ), false ); \
throw; \
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_IF( expr, isNot, stopOnFailure, macroName ) \ #define INTERNAL_CATCH_IF( expr, isNot, stopOnFailure, macroName ) \
@ -1915,10 +1930,6 @@ inline bool isTrue
expr; \ expr; \
INTERNAL_CATCH_ACCEPT_EXPR( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr ).setResultType( Catch::ResultWas::Ok ), stopOnFailure ); \ INTERNAL_CATCH_ACCEPT_EXPR( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr ).setResultType( Catch::ResultWas::Ok ), stopOnFailure ); \
} \ } \
catch( std::exception& internal_catch_exception ) \
{ \
INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr ) << internal_catch_exception.what() ).setResultType( Catch::ResultWas::ThrewException ), stopOnFailure ); \
} \
catch( ... ) \ catch( ... ) \
{ \ { \
INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr ) << Catch::Hub::getExceptionTranslatorRegistry().translateActiveException() ).setResultType( Catch::ResultWas::ThrewException ), stopOnFailure ); \ INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr ) << Catch::Hub::getExceptionTranslatorRegistry().translateActiveException() ).setResultType( Catch::ResultWas::ThrewException ), stopOnFailure ); \
@ -3319,7 +3330,26 @@ namespace Catch
() ()
const const
{ {
return tryTranslators( m_translators.begin() ); try
{
throw;
}
catch( std::exception& ex )
{
return ex.what();
}
catch( std::string& msg )
{
return msg;
}
catch( const char* msg )
{
return msg;
}
catch(...)
{
return tryTranslators( m_translators.begin() );
}
} }
/////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////
@ -4220,30 +4250,22 @@ namespace Catch
try try
{ {
m_runningTest->reset(); m_runningTest->reset();
StreamRedirect coutRedir( std::cout, redirectedCout ); if( m_reporter->shouldRedirectStdout() )
StreamRedirect cerrRedir( std::cerr, redirectedCerr ); {
m_runningTest->getTestCaseInfo().invoke(); StreamRedirect coutRedir( std::cout, redirectedCout );
StreamRedirect cerrRedir( std::cerr, redirectedCerr );
m_runningTest->getTestCaseInfo().invoke();
}
else
{
m_runningTest->getTestCaseInfo().invoke();
}
m_runningTest->ranToCompletion(); m_runningTest->ranToCompletion();
} }
catch( TestFailureException& ) catch( TestFailureException& )
{ {
// This just means the test was aborted due to failure // This just means the test was aborted due to failure
} }
catch( std::exception& ex )
{
acceptMessage( ex.what() );
acceptResult( ResultWas::ThrewException );
}
catch( std::string& msg )
{
acceptMessage( msg );
acceptResult( ResultWas::ThrewException );
}
catch( const char* msg )
{
acceptMessage( msg );
acceptResult( ResultWas::ThrewException );
}
catch(...) catch(...)
{ {
acceptMessage( Catch::Hub::getExceptionTranslatorRegistry().translateActiveException() ); acceptMessage( Catch::Hub::getExceptionTranslatorRegistry().translateActiveException() );
@ -5033,6 +5055,14 @@ namespace Catch
private: // IReporter private: // IReporter
///////////////////////////////////////////////////////////////////////////
virtual bool shouldRedirectStdout
()
const
{
return false;
}
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
virtual void StartTesting virtual void StartTesting
() ()
@ -5668,6 +5698,14 @@ namespace Catch
private: // IReporter private: // IReporter
///////////////////////////////////////////////////////////////////////////
virtual bool shouldRedirectStdout
()
const
{
return true;
}
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
virtual void StartTesting virtual void StartTesting
() ()
@ -5892,6 +5930,14 @@ namespace Catch
private: // IReporter private: // IReporter
///////////////////////////////////////////////////////////////////////////
virtual bool shouldRedirectStdout
()
const
{
return true;
}
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
virtual void StartTesting() virtual void StartTesting()
{ {