From c96f9330a095f527673de4d8db4f434d38a7e813 Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Wed, 17 Oct 2012 08:14:22 +0100 Subject: [PATCH] Collect assertion info up front --- include/internal/catch_assertionresult.h | 14 +++++++ include/internal/catch_capture.hpp | 41 +++++++++++-------- include/internal/catch_expression_builder.hpp | 11 +---- include/internal/catch_interfaces_capture.h | 2 + .../internal/catch_interfaces_registry_hub.h | 2 + include/internal/catch_registry_hub.hpp | 4 ++ include/internal/catch_runner_impl.hpp | 10 +++++ 7 files changed, 59 insertions(+), 25 deletions(-) diff --git a/include/internal/catch_assertionresult.h b/include/internal/catch_assertionresult.h index f3256510..f1155ca1 100644 --- a/include/internal/catch_assertionresult.h +++ b/include/internal/catch_assertionresult.h @@ -13,6 +13,20 @@ 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 { AssertionResultData() : resultType( ResultWas::Unknown ) {} diff --git a/include/internal/catch_capture.hpp b/include/internal/catch_capture.hpp index 2c1552e8..d734dd0d 100644 --- a/include/internal/catch_capture.hpp +++ b/include/internal/catch_capture.hpp @@ -13,20 +13,25 @@ #include "catch_debugger.hpp" #include "catch_context.h" #include "catch_common.h" +#include "catch_interfaces_registry_hub.h" #include namespace Catch { + inline IResultCapture& getResultCapture() { + return getCurrentContext().getResultCapture(); + } + struct TestFailureException{}; class ScopedInfo { public: ScopedInfo() : m_oss() { - getCurrentContext().getResultCapture().pushScopedInfo( this ); + getResultCapture().pushScopedInfo( this ); } ~ScopedInfo() { - getCurrentContext().getResultCapture().popScopedInfo( this ); + getResultCapture().popScopedInfo( this ); } template @@ -49,12 +54,12 @@ private: // This is just here to avoid compiler warnings with macro constants inline bool isTrue( bool value ){ return value; } - + } // end namespace Catch /////////////////////////////////////////////////////////////////////////////// #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::Abort ) 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 ) \ 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& ) { \ throw; \ } 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; \ } } while( Catch::isTrue( false ) ) /////////////////////////////////////////////////////////////////////////////// #define INTERNAL_CATCH_IF( 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 ) \ 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 ) \ try { \ + Catch::getResultCapture().acceptAssertionInfo( Catch::AssertionInfo( macroName, CATCH_INTERNAL_LINEINFO, #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( ... ) { \ - 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 ) \ try { \ + Catch::getResultCapture().acceptAssertionInfo( Catch::AssertionInfo( macroName, CATCH_INTERNAL_LINEINFO, #expr ) ); \ if( Catch::getCurrentContext().getConfig()->allowThrows() ) { \ 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& ) { \ throw; \ } \ 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 ) \ INTERNAL_CATCH_THROWS( expr, exceptionType, stopOnFailure, macroName ) \ 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 ) \ - 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 ) \ @@ -126,11 +134,12 @@ inline bool isTrue( bool value ){ return value; } /////////////////////////////////////////////////////////////////////////////// #define INTERNAL_CHECK_THAT( arg, matcher, stopOnFailure, macroName ) \ 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& ) { \ throw; \ } 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; \ }}while( Catch::isTrue( false ) ) diff --git a/include/internal/catch_expression_builder.hpp b/include/internal/catch_expression_builder.hpp index b342ce20..4016f9f4 100644 --- a/include/internal/catch_expression_builder.hpp +++ b/include/internal/catch_expression_builder.hpp @@ -22,17 +22,10 @@ namespace Catch { class ExpressionBuilder { public: - ExpressionBuilder( const SourceLineInfo& lineInfo, - const char* macroName, - const char* expr = "", - bool isFalse = false ) + ExpressionBuilder( bool isFalse = false ) : m_messageStream() { - m_result - .setCapturedExpression( expr ) - .setIsFalse( isFalse ) - .setLineInfo( lineInfo ) - .setMacroName( macroName ); + m_result.setIsFalse( isFalse ); } template diff --git a/include/internal/catch_interfaces_capture.h b/include/internal/catch_interfaces_capture.h index 9477017b..4915132c 100644 --- a/include/internal/catch_interfaces_capture.h +++ b/include/internal/catch_interfaces_capture.h @@ -19,6 +19,7 @@ namespace Catch { class ScopedInfo; class AssertionResultBuilder; class AssertionResult; + struct AssertionInfo; struct IResultCapture { @@ -34,6 +35,7 @@ namespace Catch { virtual void popScopedInfo( ScopedInfo* scopedInfo ) = 0; virtual bool shouldDebugBreak() const = 0; + virtual void acceptAssertionInfo( const AssertionInfo& assertionInfo ) = 0; virtual ResultAction::Value acceptResult( bool result ) = 0; virtual ResultAction::Value acceptResult( ResultWas::OfType result ) = 0; virtual ResultAction::Value acceptExpression( const AssertionResultBuilder& assertionResult ) = 0; diff --git a/include/internal/catch_interfaces_registry_hub.h b/include/internal/catch_interfaces_registry_hub.h index 28aae7a0..da2d7fd4 100644 --- a/include/internal/catch_interfaces_registry_hub.h +++ b/include/internal/catch_interfaces_registry_hub.h @@ -38,6 +38,8 @@ namespace Catch { IRegistryHub& getRegistryHub(); IMutableRegistryHub& getMutableRegistryHub(); void cleanUp(); + std::string translateActiveException(); + } #endif // TWOBLUECUBES_CATCH_INTERFACES_REGISTRY_HUB_H_INCLUDED diff --git a/include/internal/catch_registry_hub.hpp b/include/internal/catch_registry_hub.hpp index b832d54a..d66205de 100644 --- a/include/internal/catch_registry_hub.hpp +++ b/include/internal/catch_registry_hub.hpp @@ -73,6 +73,10 @@ namespace Catch { getTheRegistryHub() = NULL; cleanUpContext(); } + std::string translateActiveException() { + return getRegistryHub().getExceptionTranslatorRegistry().translateActiveException(); + } + } // end namespace Catch diff --git a/include/internal/catch_runner_impl.hpp b/include/internal/catch_runner_impl.hpp index 449f2467..d23bb55b 100644 --- a/include/internal/catch_runner_impl.hpp +++ b/include/internal/catch_runner_impl.hpp @@ -131,6 +131,9 @@ namespace Catch { private: // IResultCapture + virtual void acceptAssertionInfo( const AssertionInfo& assertionInfo ) { + m_assertionInfo = assertionInfo; + } virtual ResultAction::Value acceptResult( bool result ) { return acceptResult( result ? ResultWas::Ok : ResultWas::ExpressionFailed ); } @@ -242,10 +245,16 @@ namespace Catch { private: ResultAction::Value actOnCurrentResult() { + m_currentResult + .setMacroName( m_assertionInfo.macroName ) + .setLineInfo( m_assertionInfo.lineInfo ) + .setCapturedExpression( m_assertionInfo.capturedExpression ); + m_lastResult = m_currentResult.build(); testEnded( m_lastResult ); m_currentResult = AssertionResultBuilder(); + m_assertionInfo = AssertionInfo(); ResultAction::Value action = ResultAction::None; @@ -304,6 +313,7 @@ namespace Catch { IRunner* m_prevRunner; IResultCapture* m_prevResultCapture; const IConfig* m_prevConfig; + AssertionInfo m_assertionInfo; }; } // end namespace Catch