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 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();
}

View File

@ -14,6 +14,8 @@
namespace Catch {
struct IRunContext;
struct MessageInfo {
MessageInfo( std::string const& _macroName,
SourceLineInfo const& _lineInfo,
@ -58,6 +60,7 @@ namespace Catch {
ScopedMessage( ScopedMessage const& other );
~ScopedMessage();
IRunContext& m_runContext;
MessageInfo m_info;
};

View File

@ -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 );
}

View File

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

View File

@ -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
{

View File

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

View File

@ -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;
};

View File

@ -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 );
}
}

View File

@ -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;