mirror of https://github.com/catchorg/Catch2.git
Improved exception reporting
Refactored exception handling (handle more in the translator) and report exceptions in CHECKs
This commit is contained in:
parent
a57b025c9c
commit
333e6e6254
|
@ -684,8 +684,20 @@ inline bool isTrue
|
|||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
#define INTERNAL_CATCH_TEST( expr, isNot, stopOnFailure, macroName ) \
|
||||
try \
|
||||
{ \
|
||||
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 ); }
|
||||
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 ) \
|
||||
|
@ -704,10 +716,6 @@ inline bool isTrue
|
|||
expr; \
|
||||
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( ... ) \
|
||||
{ \
|
||||
INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr ) << Catch::Hub::getExceptionTranslatorRegistry().translateActiveException() ).setResultType( Catch::ResultWas::ThrewException ), stopOnFailure ); \
|
||||
|
|
|
@ -31,9 +31,28 @@ namespace Catch
|
|||
virtual std::string translateActiveException
|
||||
()
|
||||
const
|
||||
{
|
||||
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() );
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
std::string tryTranslators
|
||||
|
|
|
@ -601,21 +601,6 @@ namespace Catch
|
|||
{
|
||||
// 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(...)
|
||||
{
|
||||
acceptMessage( Catch::Hub::getExceptionTranslatorRegistry().translateActiveException() );
|
||||
|
|
|
@ -208,6 +208,9 @@ namespace Catch
|
|||
virtual ~IReporter
|
||||
(){}
|
||||
|
||||
virtual bool shouldRedirectStdout
|
||||
() const = 0;
|
||||
|
||||
virtual void StartTesting
|
||||
() = 0;
|
||||
|
||||
|
@ -1895,8 +1898,20 @@ inline bool isTrue
|
|||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
#define INTERNAL_CATCH_TEST( expr, isNot, stopOnFailure, macroName ) \
|
||||
try \
|
||||
{ \
|
||||
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 ); }
|
||||
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 ) \
|
||||
|
@ -1915,10 +1930,6 @@ inline bool isTrue
|
|||
expr; \
|
||||
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( ... ) \
|
||||
{ \
|
||||
INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr ) << Catch::Hub::getExceptionTranslatorRegistry().translateActiveException() ).setResultType( Catch::ResultWas::ThrewException ), stopOnFailure ); \
|
||||
|
@ -3318,9 +3329,28 @@ namespace Catch
|
|||
virtual std::string translateActiveException
|
||||
()
|
||||
const
|
||||
{
|
||||
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() );
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
std::string tryTranslators
|
||||
|
@ -4220,30 +4250,22 @@ namespace Catch
|
|||
try
|
||||
{
|
||||
m_runningTest->reset();
|
||||
if( m_reporter->shouldRedirectStdout() )
|
||||
{
|
||||
StreamRedirect coutRedir( std::cout, redirectedCout );
|
||||
StreamRedirect cerrRedir( std::cerr, redirectedCerr );
|
||||
m_runningTest->getTestCaseInfo().invoke();
|
||||
}
|
||||
else
|
||||
{
|
||||
m_runningTest->getTestCaseInfo().invoke();
|
||||
}
|
||||
m_runningTest->ranToCompletion();
|
||||
}
|
||||
catch( TestFailureException& )
|
||||
{
|
||||
// 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(...)
|
||||
{
|
||||
acceptMessage( Catch::Hub::getExceptionTranslatorRegistry().translateActiveException() );
|
||||
|
@ -5033,6 +5055,14 @@ namespace Catch
|
|||
|
||||
private: // IReporter
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
virtual bool shouldRedirectStdout
|
||||
()
|
||||
const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
virtual void StartTesting
|
||||
()
|
||||
|
@ -5668,6 +5698,14 @@ namespace Catch
|
|||
|
||||
private: // IReporter
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
virtual bool shouldRedirectStdout
|
||||
()
|
||||
const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
virtual void StartTesting
|
||||
()
|
||||
|
@ -5892,6 +5930,14 @@ namespace Catch
|
|||
|
||||
private: // IReporter
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
virtual bool shouldRedirectStdout
|
||||
()
|
||||
const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
virtual void StartTesting()
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue