From c70170e90437a224a22176a8b01b11dd5f2c3d15 Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Fri, 20 Nov 2015 08:31:17 +0000 Subject: [PATCH] Reduce number of places getCurrentRunContext() is called --- include/internal/catch_interfaces_capture.h | 4 ++++ include/internal/catch_message.h | 3 +++ include/internal/catch_message.hpp | 10 ++++++---- include/internal/catch_result_builder.h | 2 ++ include/internal/catch_result_builder.hpp | 12 ++++++------ include/internal/catch_run_context.hpp | 1 - include/internal/catch_section.h | 3 +++ include/internal/catch_section.hpp | 7 ++++--- include/internal/catch_tostring.hpp | 5 ++++- 9 files changed, 32 insertions(+), 15 deletions(-) diff --git a/include/internal/catch_interfaces_capture.h b/include/internal/catch_interfaces_capture.h index 39a5ae0a..e7b6c0e5 100644 --- a/include/internal/catch_interfaces_capture.h +++ b/include/internal/catch_interfaces_capture.h @@ -30,10 +30,12 @@ namespace Catch { virtual ~IRunContext(); virtual void assertionEnded( AssertionResult const& result ) = 0; + virtual bool sectionStarted( SectionInfo const& sectionInfo, Counts& assertions ) = 0; virtual void sectionEnded( SectionEndInfo const& endInfo ) = 0; virtual void sectionEndedEarly( SectionEndInfo const& endInfo ) = 0; + virtual void pushScopedMessage( MessageInfo const& message ) = 0; virtual void popScopedMessage( MessageInfo const& message ) = 0; @@ -42,11 +44,13 @@ namespace Catch { virtual std::string getCurrentTestName() const = 0; virtual AssertionResult const* getLastResult() const = 0; virtual bool isAborting() const = 0; + virtual IConfig const& config() const = 0; }; IRunContext* tryGetCurrentRunContext(); IRunContext& getCurrentRunContext(); + IConfig const* getCurrentConfig(); } diff --git a/include/internal/catch_message.h b/include/internal/catch_message.h index 84ff95ea..d2ec5257 100644 --- a/include/internal/catch_message.h +++ b/include/internal/catch_message.h @@ -13,6 +13,8 @@ #include "catch_common.h" namespace Catch { + + struct IRunContext; struct MessageInfo { MessageInfo( std::string const& _macroName, @@ -58,6 +60,7 @@ namespace Catch { ScopedMessage( ScopedMessage const& other ); ~ScopedMessage(); + IRunContext& m_runContext; MessageInfo m_info; }; diff --git a/include/internal/catch_message.hpp b/include/internal/catch_message.hpp index 633fa5b3..934f75da 100644 --- a/include/internal/catch_message.hpp +++ b/include/internal/catch_message.hpp @@ -28,17 +28,19 @@ namespace Catch { //////////////////////////////////////////////////////////////////////////// ScopedMessage::ScopedMessage( MessageBuilder const& builder ) - : m_info( builder.m_info ) + : m_info( builder.m_info ), + m_runContext( getCurrentRunContext() ) { m_info.message = builder.m_stream.str(); - getCurrentRunContext().pushScopedMessage( m_info ); + m_runContext.pushScopedMessage( m_info ); } ScopedMessage::ScopedMessage( ScopedMessage const& other ) - : m_info( other.m_info ) + : m_info( other.m_info ), + m_runContext( other.m_runContext ) {} ScopedMessage::~ScopedMessage() { - getCurrentRunContext().popScopedMessage( m_info ); + m_runContext.popScopedMessage( m_info ); } diff --git a/include/internal/catch_result_builder.h b/include/internal/catch_result_builder.h index 89002660..df412246 100644 --- a/include/internal/catch_result_builder.h +++ b/include/internal/catch_result_builder.h @@ -15,6 +15,7 @@ namespace Catch { + struct IRunContext; struct TestFailureException{}; template class ExpressionLhs; @@ -77,6 +78,7 @@ namespace Catch { bool allowThrows() const; private: + IRunContext& m_runContext; AssertionInfo m_assertionInfo; AssertionResultData m_data; struct ExprComponents { diff --git a/include/internal/catch_result_builder.hpp b/include/internal/catch_result_builder.hpp index bd8e5b00..959a7e4c 100644 --- a/include/internal/catch_result_builder.hpp +++ b/include/internal/catch_result_builder.hpp @@ -26,7 +26,8 @@ namespace Catch { char const* capturedExpression, ResultDisposition::Flags resultDisposition, char const* secondArg ) - : m_assertionInfo( macroName, lineInfo, capturedExpressionWithSecondArgument( capturedExpression, secondArg ), resultDisposition ), + : m_runContext( getCurrentRunContext() ), + m_assertionInfo( macroName, lineInfo, capturedExpressionWithSecondArgument( capturedExpression, secondArg ), resultDisposition ), m_shouldDebugBreak( false ), m_shouldThrow( false ) {} @@ -96,13 +97,12 @@ namespace Catch { } void ResultBuilder::handleResult( AssertionResult const& result ) { - IRunContext& context = getCurrentRunContext(); - context.assertionEnded( result ); + m_runContext.assertionEnded( result ); if( !result.isOk() ) { - if( context.config().shouldDebugBreak() ) + if( m_runContext.config().shouldDebugBreak() ) m_shouldDebugBreak = true; - if( context.isAborting() || (m_assertionInfo.resultDisposition & ResultDisposition::Normal) ) + if( m_runContext.isAborting() || (m_assertionInfo.resultDisposition & ResultDisposition::Normal) ) m_shouldThrow = true; } } @@ -112,7 +112,7 @@ namespace Catch { } bool ResultBuilder::shouldDebugBreak() const { return m_shouldDebugBreak; } - bool ResultBuilder::allowThrows() const { return getCurrentConfig()->allowThrows(); } + bool ResultBuilder::allowThrows() const { return m_runContext.config().allowThrows(); } AssertionResult ResultBuilder::build() const { diff --git a/include/internal/catch_run_context.hpp b/include/internal/catch_run_context.hpp index 80fc8313..ef2656cd 100644 --- a/include/internal/catch_run_context.hpp +++ b/include/internal/catch_run_context.hpp @@ -63,7 +63,6 @@ namespace Catch { IRunContext* tryGetCurrentRunContext() { return s_currentRunContext; } - IRunContext& getCurrentRunContext() { if( IRunContext* capture = tryGetCurrentRunContext() ) return *capture; diff --git a/include/internal/catch_section.h b/include/internal/catch_section.h index d8b3ae42..04fd5bc8 100644 --- a/include/internal/catch_section.h +++ b/include/internal/catch_section.h @@ -16,6 +16,8 @@ namespace Catch { + struct IRunContext; + class Section : NonCopyable { public: Section( SectionInfo const& info ); @@ -29,6 +31,7 @@ namespace Catch { std::string m_name; Counts m_assertions; + IRunContext& m_runContext; bool m_sectionIncluded; Timer m_timer; }; diff --git a/include/internal/catch_section.hpp b/include/internal/catch_section.hpp index f921b6a2..9568c914 100644 --- a/include/internal/catch_section.hpp +++ b/include/internal/catch_section.hpp @@ -26,7 +26,8 @@ namespace Catch { Section::Section( SectionInfo const& info ) : m_info( info ), - m_sectionIncluded( getCurrentRunContext().sectionStarted( m_info, m_assertions ) ) + m_runContext( getCurrentRunContext() ), + m_sectionIncluded( m_runContext.sectionStarted( m_info, m_assertions ) ) { m_timer.start(); } @@ -35,9 +36,9 @@ namespace Catch { if( m_sectionIncluded ) { SectionEndInfo endInfo( m_info, m_assertions, m_timer.getElapsedSeconds() ); if( std::uncaught_exception() ) - getCurrentRunContext().sectionEndedEarly( endInfo ); + m_runContext.sectionEndedEarly( endInfo ); else - getCurrentRunContext().sectionEnded( endInfo ); + m_runContext.sectionEnded( endInfo ); } } diff --git a/include/internal/catch_tostring.hpp b/include/internal/catch_tostring.hpp index ad16e25e..687a9117 100644 --- a/include/internal/catch_tostring.hpp +++ b/include/internal/catch_tostring.hpp @@ -55,10 +55,13 @@ namespace Detail { std::string toString( std::string const& value ) { std::string s = value; - if( getCurrentConfig() && getCurrentConfig()->showInvisibles() ) { + IConfig const* config = getCurrentConfig(); + if( config && config->showInvisibles() ) { for(size_t i = 0; i < s.size(); ++i ) { std::string subs; switch( s[i] ) { + case '\r': subs = "\\r"; break; + case '\l': subs = "\\l"; break; case '\n': subs = "\\n"; break; case '\t': subs = "\\t"; break; default: break;