Reduce number of places getCurrentRunContext() is called

This commit is contained in:
Phil Nash 2015-11-20 08:31:17 +00:00
parent 6789dfa2ba
commit c70170e904
9 changed files with 32 additions and 15 deletions

View File

@ -30,10 +30,12 @@ namespace Catch {
virtual ~IRunContext(); virtual ~IRunContext();
virtual void assertionEnded( AssertionResult const& result ) = 0; virtual void assertionEnded( AssertionResult const& result ) = 0;
virtual bool sectionStarted( SectionInfo const& sectionInfo, virtual bool sectionStarted( SectionInfo const& sectionInfo,
Counts& assertions ) = 0; Counts& assertions ) = 0;
virtual void sectionEnded( SectionEndInfo const& endInfo ) = 0; virtual void sectionEnded( SectionEndInfo const& endInfo ) = 0;
virtual void sectionEndedEarly( SectionEndInfo const& endInfo ) = 0; virtual void sectionEndedEarly( SectionEndInfo const& endInfo ) = 0;
virtual void pushScopedMessage( MessageInfo const& message ) = 0; virtual void pushScopedMessage( MessageInfo const& message ) = 0;
virtual void popScopedMessage( MessageInfo const& message ) = 0; virtual void popScopedMessage( MessageInfo const& message ) = 0;
@ -42,11 +44,13 @@ namespace Catch {
virtual std::string getCurrentTestName() const = 0; virtual std::string getCurrentTestName() const = 0;
virtual AssertionResult const* getLastResult() const = 0; virtual AssertionResult const* getLastResult() const = 0;
virtual bool isAborting() const = 0; virtual bool isAborting() const = 0;
virtual IConfig const& config() const = 0; virtual IConfig const& config() const = 0;
}; };
IRunContext* tryGetCurrentRunContext(); IRunContext* tryGetCurrentRunContext();
IRunContext& getCurrentRunContext(); IRunContext& getCurrentRunContext();
IConfig const* getCurrentConfig(); IConfig const* getCurrentConfig();
} }

View File

@ -13,6 +13,8 @@
#include "catch_common.h" #include "catch_common.h"
namespace Catch { namespace Catch {
struct IRunContext;
struct MessageInfo { struct MessageInfo {
MessageInfo( std::string const& _macroName, MessageInfo( std::string const& _macroName,
@ -58,6 +60,7 @@ namespace Catch {
ScopedMessage( ScopedMessage const& other ); ScopedMessage( ScopedMessage const& other );
~ScopedMessage(); ~ScopedMessage();
IRunContext& m_runContext;
MessageInfo m_info; MessageInfo m_info;
}; };

View File

@ -28,17 +28,19 @@ namespace Catch {
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
ScopedMessage::ScopedMessage( MessageBuilder const& builder ) 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(); m_info.message = builder.m_stream.str();
getCurrentRunContext().pushScopedMessage( m_info ); m_runContext.pushScopedMessage( m_info );
} }
ScopedMessage::ScopedMessage( ScopedMessage const& other ) ScopedMessage::ScopedMessage( ScopedMessage const& other )
: m_info( other.m_info ) : m_info( other.m_info ),
m_runContext( other.m_runContext )
{} {}
ScopedMessage::~ScopedMessage() { ScopedMessage::~ScopedMessage() {
getCurrentRunContext().popScopedMessage( m_info ); m_runContext.popScopedMessage( m_info );
} }

View File

@ -15,6 +15,7 @@
namespace Catch { namespace Catch {
struct IRunContext;
struct TestFailureException{}; struct TestFailureException{};
template<typename T> class ExpressionLhs; template<typename T> class ExpressionLhs;
@ -77,6 +78,7 @@ namespace Catch {
bool allowThrows() const; bool allowThrows() const;
private: private:
IRunContext& m_runContext;
AssertionInfo m_assertionInfo; AssertionInfo m_assertionInfo;
AssertionResultData m_data; AssertionResultData m_data;
struct ExprComponents { struct ExprComponents {

View File

@ -26,7 +26,8 @@ namespace Catch {
char const* capturedExpression, char const* capturedExpression,
ResultDisposition::Flags resultDisposition, ResultDisposition::Flags resultDisposition,
char const* secondArg ) 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_shouldDebugBreak( false ),
m_shouldThrow( false ) m_shouldThrow( false )
{} {}
@ -96,13 +97,12 @@ namespace Catch {
} }
void ResultBuilder::handleResult( AssertionResult const& result ) void ResultBuilder::handleResult( AssertionResult const& result )
{ {
IRunContext& context = getCurrentRunContext(); m_runContext.assertionEnded( result );
context.assertionEnded( result );
if( !result.isOk() ) { if( !result.isOk() ) {
if( context.config().shouldDebugBreak() ) if( m_runContext.config().shouldDebugBreak() )
m_shouldDebugBreak = true; m_shouldDebugBreak = true;
if( context.isAborting() || (m_assertionInfo.resultDisposition & ResultDisposition::Normal) ) if( m_runContext.isAborting() || (m_assertionInfo.resultDisposition & ResultDisposition::Normal) )
m_shouldThrow = true; m_shouldThrow = true;
} }
} }
@ -112,7 +112,7 @@ namespace Catch {
} }
bool ResultBuilder::shouldDebugBreak() const { return m_shouldDebugBreak; } 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 AssertionResult ResultBuilder::build() const
{ {

View File

@ -63,7 +63,6 @@ namespace Catch {
IRunContext* tryGetCurrentRunContext() { IRunContext* tryGetCurrentRunContext() {
return s_currentRunContext; return s_currentRunContext;
} }
IRunContext& getCurrentRunContext() { IRunContext& getCurrentRunContext() {
if( IRunContext* capture = tryGetCurrentRunContext() ) if( IRunContext* capture = tryGetCurrentRunContext() )
return *capture; return *capture;

View File

@ -16,6 +16,8 @@
namespace Catch { namespace Catch {
struct IRunContext;
class Section : NonCopyable { class Section : NonCopyable {
public: public:
Section( SectionInfo const& info ); Section( SectionInfo const& info );
@ -29,6 +31,7 @@ namespace Catch {
std::string m_name; std::string m_name;
Counts m_assertions; Counts m_assertions;
IRunContext& m_runContext;
bool m_sectionIncluded; bool m_sectionIncluded;
Timer m_timer; Timer m_timer;
}; };

View File

@ -26,7 +26,8 @@ namespace Catch {
Section::Section( SectionInfo const& info ) Section::Section( SectionInfo const& info )
: m_info( 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(); m_timer.start();
} }
@ -35,9 +36,9 @@ namespace Catch {
if( m_sectionIncluded ) { if( m_sectionIncluded ) {
SectionEndInfo endInfo( m_info, m_assertions, m_timer.getElapsedSeconds() ); SectionEndInfo endInfo( m_info, m_assertions, m_timer.getElapsedSeconds() );
if( std::uncaught_exception() ) if( std::uncaught_exception() )
getCurrentRunContext().sectionEndedEarly( endInfo ); m_runContext.sectionEndedEarly( endInfo );
else else
getCurrentRunContext().sectionEnded( endInfo ); m_runContext.sectionEnded( endInfo );
} }
} }

View File

@ -55,10 +55,13 @@ namespace Detail {
std::string toString( std::string const& value ) { std::string toString( std::string const& value ) {
std::string s = 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 ) { for(size_t i = 0; i < s.size(); ++i ) {
std::string subs; std::string subs;
switch( s[i] ) { switch( s[i] ) {
case '\r': subs = "\\r"; break;
case '\l': subs = "\\l"; break;
case '\n': subs = "\\n"; break; case '\n': subs = "\\n"; break;
case '\t': subs = "\\t"; break; case '\t': subs = "\\t"; break;
default: break; default: break;