Collect assertion info up front

This commit is contained in:
Phil Nash 2012-10-17 08:14:22 +01:00
parent a5fa78284d
commit c96f9330a0
7 changed files with 59 additions and 25 deletions

View File

@ -13,6 +13,20 @@
namespace Catch { namespace Catch {
struct AssertionInfo
{
AssertionInfo() {}
AssertionInfo( const std::string& _macroName, const SourceLineInfo& _lineInfo, const std::string& _capturedExpression )
: macroName( _macroName ),
lineInfo( _lineInfo ),
capturedExpression( _capturedExpression )
{}
std::string macroName;
SourceLineInfo lineInfo;
std::string capturedExpression;
};
struct AssertionResultData struct AssertionResultData
{ {
AssertionResultData() : resultType( ResultWas::Unknown ) {} AssertionResultData() : resultType( ResultWas::Unknown ) {}

View File

@ -13,20 +13,25 @@
#include "catch_debugger.hpp" #include "catch_debugger.hpp"
#include "catch_context.h" #include "catch_context.h"
#include "catch_common.h" #include "catch_common.h"
#include "catch_interfaces_registry_hub.h"
#include <ostream> #include <ostream>
namespace Catch { namespace Catch {
inline IResultCapture& getResultCapture() {
return getCurrentContext().getResultCapture();
}
struct TestFailureException{}; struct TestFailureException{};
class ScopedInfo { class ScopedInfo {
public: public:
ScopedInfo() : m_oss() { ScopedInfo() : m_oss() {
getCurrentContext().getResultCapture().pushScopedInfo( this ); getResultCapture().pushScopedInfo( this );
} }
~ScopedInfo() { ~ScopedInfo() {
getCurrentContext().getResultCapture().popScopedInfo( this ); getResultCapture().popScopedInfo( this );
} }
template<typename T> template<typename T>
@ -49,12 +54,12 @@ private:
// This is just here to avoid compiler warnings with macro constants // This is just here to avoid compiler warnings with macro constants
inline bool isTrue( bool value ){ return value; } inline bool isTrue( bool value ){ return value; }
} // end namespace Catch } // end namespace Catch
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_ACCEPT_EXPR( expr, stopOnFailure, originalExpr ) \ #define INTERNAL_CATCH_ACCEPT_EXPR( expr, stopOnFailure, originalExpr ) \
if( Catch::ResultAction::Value internal_catch_action = Catch::getCurrentContext().getResultCapture().acceptExpression( expr ) ) { \ if( Catch::ResultAction::Value internal_catch_action = Catch::getResultCapture().acceptExpression( expr ) ) { \
if( internal_catch_action & Catch::ResultAction::Debug ) BreakIntoDebugger(); \ if( internal_catch_action & Catch::ResultAction::Debug ) BreakIntoDebugger(); \
if( internal_catch_action & Catch::ResultAction::Abort ) throw Catch::TestFailureException(); \ if( internal_catch_action & Catch::ResultAction::Abort ) throw Catch::TestFailureException(); \
if( Catch::isTrue( stopOnFailure ) ) throw Catch::TestFailureException(); \ if( Catch::isTrue( stopOnFailure ) ) throw Catch::TestFailureException(); \
@ -64,59 +69,62 @@ inline bool isTrue( bool value ){ return value; }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_TEST( expr, isFalse, stopOnFailure, macroName ) \ #define INTERNAL_CATCH_TEST( expr, isFalse, stopOnFailure, macroName ) \
do { try { \ do { try { \
INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ExpressionBuilder( CATCH_INTERNAL_LINEINFO, macroName, #expr, isFalse )->*expr ), stopOnFailure, expr ); \ Catch::getResultCapture().acceptAssertionInfo( Catch::AssertionInfo( macroName, CATCH_INTERNAL_LINEINFO, #expr ) ); \
INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ExpressionBuilder( isFalse )->*expr ), stopOnFailure, expr ); \
} catch( Catch::TestFailureException& ) { \ } catch( Catch::TestFailureException& ) { \
throw; \ throw; \
} catch( ... ) { \ } catch( ... ) { \
INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ExpressionBuilder( CATCH_INTERNAL_LINEINFO, macroName, #expr ) << Catch::getRegistryHub().getExceptionTranslatorRegistry().translateActiveException() ).setResultType( Catch::ResultWas::ThrewException ), false, expr ); \ INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ExpressionBuilder() << Catch::translateActiveException() ).setResultType( Catch::ResultWas::ThrewException ), false, expr ); \
throw; \ throw; \
} } while( Catch::isTrue( false ) ) } } while( Catch::isTrue( false ) )
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_IF( expr, isFalse, stopOnFailure, macroName ) \ #define INTERNAL_CATCH_IF( expr, isFalse, stopOnFailure, macroName ) \
INTERNAL_CATCH_TEST( expr, isFalse, stopOnFailure, macroName ); \ INTERNAL_CATCH_TEST( expr, isFalse, stopOnFailure, macroName ); \
if( Catch::getCurrentContext().getResultCapture().getLastResult()->ok() ) if( Catch::getResultCapture().getLastResult()->ok() )
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_ELSE( expr, isFalse, stopOnFailure, macroName ) \ #define INTERNAL_CATCH_ELSE( expr, isFalse, stopOnFailure, macroName ) \
INTERNAL_CATCH_TEST( expr, isFalse, stopOnFailure, macroName ); \ INTERNAL_CATCH_TEST( expr, isFalse, stopOnFailure, macroName ); \
if( !Catch::getCurrentContext().getResultCapture().getLastResult()->ok() ) if( !Catch::getResultCapture().getLastResult()->ok() )
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_NO_THROW( expr, stopOnFailure, macroName ) \ #define INTERNAL_CATCH_NO_THROW( expr, stopOnFailure, macroName ) \
try { \ try { \
Catch::getResultCapture().acceptAssertionInfo( Catch::AssertionInfo( macroName, CATCH_INTERNAL_LINEINFO, #expr ) ); \
expr; \ expr; \
INTERNAL_CATCH_ACCEPT_EXPR( Catch::ExpressionBuilder( CATCH_INTERNAL_LINEINFO, macroName, #expr ).setResultType( Catch::ResultWas::Ok ), stopOnFailure, false ); \ INTERNAL_CATCH_ACCEPT_EXPR( Catch::ExpressionBuilder().setResultType( Catch::ResultWas::Ok ), stopOnFailure, false ); \
} \ } \
catch( ... ) { \ catch( ... ) { \
INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ExpressionBuilder( CATCH_INTERNAL_LINEINFO, macroName, #expr ) << Catch::getRegistryHub().getExceptionTranslatorRegistry().translateActiveException() ).setResultType( Catch::ResultWas::ThrewException ), stopOnFailure, false ); \ INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ExpressionBuilder() << Catch::translateActiveException() ).setResultType( Catch::ResultWas::ThrewException ), stopOnFailure, false ); \
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_THROWS( expr, exceptionType, stopOnFailure, macroName ) \ #define INTERNAL_CATCH_THROWS( expr, exceptionType, stopOnFailure, macroName ) \
try { \ try { \
Catch::getResultCapture().acceptAssertionInfo( Catch::AssertionInfo( macroName, CATCH_INTERNAL_LINEINFO, #expr ) ); \
if( Catch::getCurrentContext().getConfig()->allowThrows() ) { \ if( Catch::getCurrentContext().getConfig()->allowThrows() ) { \
expr; \ expr; \
INTERNAL_CATCH_ACCEPT_EXPR( Catch::ExpressionBuilder( CATCH_INTERNAL_LINEINFO, macroName, #expr ).setResultType( Catch::ResultWas::DidntThrowException ), stopOnFailure, false ); \ INTERNAL_CATCH_ACCEPT_EXPR( Catch::ExpressionBuilder().setResultType( Catch::ResultWas::DidntThrowException ), stopOnFailure, false ); \
} \ } \
} \ } \
catch( Catch::TestFailureException& ) { \ catch( Catch::TestFailureException& ) { \
throw; \ throw; \
} \ } \
catch( exceptionType ) { \ catch( exceptionType ) { \
INTERNAL_CATCH_ACCEPT_EXPR( Catch::ExpressionBuilder( CATCH_INTERNAL_LINEINFO, macroName, #expr ).setResultType( Catch::ResultWas::Ok ), stopOnFailure, false ); \ INTERNAL_CATCH_ACCEPT_EXPR( Catch::ExpressionBuilder().setResultType( Catch::ResultWas::Ok ), stopOnFailure, false ); \
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_THROWS_AS( expr, exceptionType, stopOnFailure, macroName ) \ #define INTERNAL_CATCH_THROWS_AS( expr, exceptionType, stopOnFailure, macroName ) \
INTERNAL_CATCH_THROWS( expr, exceptionType, stopOnFailure, macroName ) \ INTERNAL_CATCH_THROWS( expr, exceptionType, stopOnFailure, macroName ) \
catch( ... ) { \ catch( ... ) { \
INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ExpressionBuilder( CATCH_INTERNAL_LINEINFO, macroName, #expr ) << Catch::getRegistryHub().getExceptionTranslatorRegistry() ).setResultType( Catch::ResultWas::ThrewException ), stopOnFailure, false ); \ INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ExpressionBuilder() << Catch::getRegistryHub().getExceptionTranslatorRegistry() ).setResultType( Catch::ResultWas::ThrewException ), stopOnFailure, false ); \
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_MSG( reason, resultType, stopOnFailure, macroName ) \ #define INTERNAL_CATCH_MSG( reason, resultType, stopOnFailure, macroName ) \
Catch::getCurrentContext().getResultCapture().acceptExpression( ( Catch::ExpressionBuilder( CATCH_INTERNAL_LINEINFO, macroName ) << reason ).setResultType( resultType ) ); Catch::getResultCapture().acceptExpression( ( Catch::ExpressionBuilder() << reason ).setResultType( resultType ) );
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_SCOPED_INFO( log ) \ #define INTERNAL_CATCH_SCOPED_INFO( log ) \
@ -126,11 +134,12 @@ inline bool isTrue( bool value ){ return value; }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CHECK_THAT( arg, matcher, stopOnFailure, macroName ) \ #define INTERNAL_CHECK_THAT( arg, matcher, stopOnFailure, macroName ) \
do { try { \ do { try { \
INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ExpressionBuilder( CATCH_INTERNAL_LINEINFO, macroName, #arg " " #matcher, false ).acceptMatcher( ::Catch::Matchers::matcher, arg, #matcher ) ), stopOnFailure, false ); \ Catch::getResultCapture().acceptAssertionInfo( Catch::AssertionInfo( macroName, CATCH_INTERNAL_LINEINFO, #arg " " #matcher ) ); \
INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ExpressionBuilder().acceptMatcher( ::Catch::Matchers::matcher, arg, #matcher ) ), stopOnFailure, false ); \
} catch( Catch::TestFailureException& ) { \ } catch( Catch::TestFailureException& ) { \
throw; \ throw; \
} catch( ... ) { \ } catch( ... ) { \
INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ExpressionBuilder( CATCH_INTERNAL_LINEINFO, macroName, #arg " " #matcher ) << Catch::getRegistryHub().getExceptionTranslatorRegistry().translateActiveException() ).setResultType( Catch::ResultWas::ThrewException ), false, false ); \ INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ExpressionBuilder() << Catch::translateActiveException() ).setResultType( Catch::ResultWas::ThrewException ), false, false ); \
throw; \ throw; \
}}while( Catch::isTrue( false ) ) }}while( Catch::isTrue( false ) )

View File

@ -22,17 +22,10 @@ namespace Catch {
class ExpressionBuilder { class ExpressionBuilder {
public: public:
ExpressionBuilder( const SourceLineInfo& lineInfo, ExpressionBuilder( bool isFalse = false )
const char* macroName,
const char* expr = "",
bool isFalse = false )
: m_messageStream() : m_messageStream()
{ {
m_result m_result.setIsFalse( isFalse );
.setCapturedExpression( expr )
.setIsFalse( isFalse )
.setLineInfo( lineInfo )
.setMacroName( macroName );
} }
template<typename T> template<typename T>

View File

@ -19,6 +19,7 @@ namespace Catch {
class ScopedInfo; class ScopedInfo;
class AssertionResultBuilder; class AssertionResultBuilder;
class AssertionResult; class AssertionResult;
struct AssertionInfo;
struct IResultCapture { struct IResultCapture {
@ -34,6 +35,7 @@ namespace Catch {
virtual void popScopedInfo( ScopedInfo* scopedInfo ) = 0; virtual void popScopedInfo( ScopedInfo* scopedInfo ) = 0;
virtual bool shouldDebugBreak() const = 0; virtual bool shouldDebugBreak() const = 0;
virtual void acceptAssertionInfo( const AssertionInfo& assertionInfo ) = 0;
virtual ResultAction::Value acceptResult( bool result ) = 0; virtual ResultAction::Value acceptResult( bool result ) = 0;
virtual ResultAction::Value acceptResult( ResultWas::OfType result ) = 0; virtual ResultAction::Value acceptResult( ResultWas::OfType result ) = 0;
virtual ResultAction::Value acceptExpression( const AssertionResultBuilder& assertionResult ) = 0; virtual ResultAction::Value acceptExpression( const AssertionResultBuilder& assertionResult ) = 0;

View File

@ -38,6 +38,8 @@ namespace Catch {
IRegistryHub& getRegistryHub(); IRegistryHub& getRegistryHub();
IMutableRegistryHub& getMutableRegistryHub(); IMutableRegistryHub& getMutableRegistryHub();
void cleanUp(); void cleanUp();
std::string translateActiveException();
} }
#endif // TWOBLUECUBES_CATCH_INTERFACES_REGISTRY_HUB_H_INCLUDED #endif // TWOBLUECUBES_CATCH_INTERFACES_REGISTRY_HUB_H_INCLUDED

View File

@ -73,6 +73,10 @@ namespace Catch {
getTheRegistryHub() = NULL; getTheRegistryHub() = NULL;
cleanUpContext(); cleanUpContext();
} }
std::string translateActiveException() {
return getRegistryHub().getExceptionTranslatorRegistry().translateActiveException();
}
} // end namespace Catch } // end namespace Catch

View File

@ -131,6 +131,9 @@ namespace Catch {
private: // IResultCapture private: // IResultCapture
virtual void acceptAssertionInfo( const AssertionInfo& assertionInfo ) {
m_assertionInfo = assertionInfo;
}
virtual ResultAction::Value acceptResult( bool result ) { virtual ResultAction::Value acceptResult( bool result ) {
return acceptResult( result ? ResultWas::Ok : ResultWas::ExpressionFailed ); return acceptResult( result ? ResultWas::Ok : ResultWas::ExpressionFailed );
} }
@ -242,10 +245,16 @@ namespace Catch {
private: private:
ResultAction::Value actOnCurrentResult() { ResultAction::Value actOnCurrentResult() {
m_currentResult
.setMacroName( m_assertionInfo.macroName )
.setLineInfo( m_assertionInfo.lineInfo )
.setCapturedExpression( m_assertionInfo.capturedExpression );
m_lastResult = m_currentResult.build(); m_lastResult = m_currentResult.build();
testEnded( m_lastResult ); testEnded( m_lastResult );
m_currentResult = AssertionResultBuilder(); m_currentResult = AssertionResultBuilder();
m_assertionInfo = AssertionInfo();
ResultAction::Value action = ResultAction::None; ResultAction::Value action = ResultAction::None;
@ -304,6 +313,7 @@ namespace Catch {
IRunner* m_prevRunner; IRunner* m_prevRunner;
IResultCapture* m_prevResultCapture; IResultCapture* m_prevResultCapture;
const IConfig* m_prevConfig; const IConfig* m_prevConfig;
AssertionInfo m_assertionInfo;
}; };
} // end namespace Catch } // end namespace Catch