From ebb4677089bbd239bf3b129e4b4d7fce580b23d1 Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Mon, 23 Nov 2015 18:43:16 +0000 Subject: [PATCH] Funnel most calls to getCurrentRunContext() through C_A_T_C_H_Context() --- include/internal/catch_capture.hpp | 23 ++++++++----------- include/internal/catch_config.hpp | 2 +- include/internal/catch_interfaces_capture.h | 14 +++++++++-- include/internal/catch_interfaces_reporter.h | 9 +++----- .../catch_legacy_reporter_adapter.hpp | 2 +- include/internal/catch_message.h | 7 ++++-- include/internal/catch_message.hpp | 2 +- include/internal/catch_result_builder.h | 12 ++++++---- include/internal/catch_result_builder.hpp | 14 ++++++----- include/internal/catch_run_context.hpp | 20 ++++++++-------- include/internal/catch_section.h | 4 ++-- include/internal/catch_section.hpp | 8 ++++--- include/internal/catch_section_info.h | 5 +++- projects/SelfTest/PartTrackerTests.cpp | 23 ------------------- 14 files changed, 69 insertions(+), 76 deletions(-) diff --git a/include/internal/catch_capture.hpp b/include/internal/catch_capture.hpp index 2cd08582..5ee44065 100644 --- a/include/internal/catch_capture.hpp +++ b/include/internal/catch_capture.hpp @@ -15,9 +15,6 @@ #include "catch_tostring.h" #include "catch_compiler_capabilities.h" -namespace Catch { - AssertionResult const* getLastResult(); -} /////////////////////////////////////////////////////////////////////////////// // In the event of a failure works out if the debugger needs to be invoked @@ -32,7 +29,7 @@ namespace Catch { /////////////////////////////////////////////////////////////////////////////// #define INTERNAL_CATCH_TEST( expr, resultDisposition, macroName ) \ do { \ - Catch::ResultBuilder __catchResult( macroName, CATCH_INTERNAL_LINEINFO, #expr, resultDisposition ); \ + Catch::ResultBuilder __catchResult( C_A_T_C_H_Context(), macroName, CATCH_INTERNAL_LINEINFO, #expr, resultDisposition ); \ try { \ ( __catchResult <= expr ).endExpression(); \ } \ @@ -45,17 +42,17 @@ namespace Catch { /////////////////////////////////////////////////////////////////////////////// #define INTERNAL_CATCH_IF( expr, resultDisposition, macroName ) \ INTERNAL_CATCH_TEST( expr, resultDisposition, macroName ); \ - if( Catch::getLastResult()->succeeded() ) + if( C_A_T_C_H_Context().getLastResult()->succeeded() ) /////////////////////////////////////////////////////////////////////////////// #define INTERNAL_CATCH_ELSE( expr, resultDisposition, macroName ) \ INTERNAL_CATCH_TEST( expr, resultDisposition, macroName ); \ - if( !Catch::getLastResult()->succeeded() ) + if( !C_A_T_C_H_Context().getLastResult()->succeeded() ) /////////////////////////////////////////////////////////////////////////////// #define INTERNAL_CATCH_NO_THROW( expr, resultDisposition, macroName ) \ do { \ - Catch::ResultBuilder __catchResult( macroName, CATCH_INTERNAL_LINEINFO, #expr, resultDisposition ); \ + Catch::ResultBuilder __catchResult( C_A_T_C_H_Context(), macroName, CATCH_INTERNAL_LINEINFO, #expr, resultDisposition ); \ try { \ expr; \ __catchResult.captureResult( Catch::ResultWas::Ok ); \ @@ -69,7 +66,7 @@ namespace Catch { /////////////////////////////////////////////////////////////////////////////// #define INTERNAL_CATCH_THROWS( expr, resultDisposition, matcher, macroName ) \ do { \ - Catch::ResultBuilder __catchResult( macroName, CATCH_INTERNAL_LINEINFO, #expr, resultDisposition, #matcher ); \ + Catch::ResultBuilder __catchResult( C_A_T_C_H_Context(), macroName, CATCH_INTERNAL_LINEINFO, #expr, resultDisposition, #matcher ); \ if( __catchResult.allowThrows() ) \ try { \ expr; \ @@ -86,7 +83,7 @@ namespace Catch { /////////////////////////////////////////////////////////////////////////////// #define INTERNAL_CATCH_THROWS_AS( expr, exceptionType, resultDisposition, macroName ) \ do { \ - Catch::ResultBuilder __catchResult( macroName, CATCH_INTERNAL_LINEINFO, #expr, resultDisposition ); \ + Catch::ResultBuilder __catchResult( C_A_T_C_H_Context(), macroName, CATCH_INTERNAL_LINEINFO, #expr, resultDisposition ); \ if( __catchResult.allowThrows() ) \ try { \ expr; \ @@ -108,7 +105,7 @@ namespace Catch { #ifdef CATCH_CONFIG_VARIADIC_MACROS #define INTERNAL_CATCH_MSG( messageType, resultDisposition, macroName, ... ) \ do { \ - Catch::ResultBuilder __catchResult( macroName, CATCH_INTERNAL_LINEINFO, "", resultDisposition ); \ + Catch::ResultBuilder __catchResult( C_A_T_C_H_Context(), macroName, CATCH_INTERNAL_LINEINFO, "", resultDisposition ); \ __catchResult << __VA_ARGS__ + ::Catch::StreamEndStop(); \ __catchResult.captureResult( messageType ); \ INTERNAL_CATCH_REACT( __catchResult ) \ @@ -116,7 +113,7 @@ namespace Catch { #else #define INTERNAL_CATCH_MSG( messageType, resultDisposition, macroName, log ) \ do { \ - Catch::ResultBuilder __catchResult( macroName, CATCH_INTERNAL_LINEINFO, "", resultDisposition ); \ + Catch::ResultBuilder __catchResult( C_A_T_C_H_Context(), macroName, CATCH_INTERNAL_LINEINFO, "", resultDisposition ); \ __catchResult << log + ::Catch::StreamEndStop(); \ __catchResult.captureResult( messageType ); \ INTERNAL_CATCH_REACT( __catchResult ) \ @@ -125,12 +122,12 @@ namespace Catch { /////////////////////////////////////////////////////////////////////////////// #define INTERNAL_CATCH_INFO( log, macroName ) \ - Catch::ScopedMessage INTERNAL_CATCH_UNIQUE_NAME( scopedMessage ) = Catch::MessageBuilder( macroName, CATCH_INTERNAL_LINEINFO, Catch::ResultWas::Info ) << log; + Catch::ScopedMessage INTERNAL_CATCH_UNIQUE_NAME( scopedMessage ) = Catch::MessageBuilder( C_A_T_C_H_Context(), macroName, CATCH_INTERNAL_LINEINFO, Catch::ResultWas::Info ) << log; /////////////////////////////////////////////////////////////////////////////// #define INTERNAL_CHECK_THAT( arg, matcher, resultDisposition, macroName ) \ do { \ - Catch::ResultBuilder __catchResult( macroName, CATCH_INTERNAL_LINEINFO, #arg ", " #matcher, resultDisposition ); \ + Catch::ResultBuilder __catchResult( C_A_T_C_H_Context(), macroName, CATCH_INTERNAL_LINEINFO, #arg ", " #matcher, resultDisposition ); \ try { \ std::string matcherAsString = (matcher).toString(); \ __catchResult \ diff --git a/include/internal/catch_config.hpp b/include/internal/catch_config.hpp index ce7b3b47..41db6555 100644 --- a/include/internal/catch_config.hpp +++ b/include/internal/catch_config.hpp @@ -150,7 +150,7 @@ namespace Catch { } ConfigData m_data; - std::auto_ptr m_stream; + CATCH_AUTO_PTR( IStream const ) m_stream; TestSpec m_testSpec; }; diff --git a/include/internal/catch_interfaces_capture.h b/include/internal/catch_interfaces_capture.h index e7b6c0e5..4ed9bf2d 100644 --- a/include/internal/catch_interfaces_capture.h +++ b/include/internal/catch_interfaces_capture.h @@ -16,7 +16,6 @@ namespace Catch { class TestCase; class AssertionResult; - class ScopedMessageBuilder; struct AssertionInfo; struct SectionInfo; @@ -48,10 +47,21 @@ namespace Catch { virtual IConfig const& config() const = 0; }; - IRunContext* tryGetCurrentRunContext(); IRunContext& getCurrentRunContext(); IConfig const* getCurrentConfig(); + + class LocalContext { + + public: + IRunContext& operator()() const { + return getCurrentRunContext(); // !TBD + } + }; +} + +inline Catch::IRunContext& C_A_T_C_H_Context() { + return Catch::getCurrentRunContext(); } #endif // TWOBLUECUBES_CATCH_INTERFACES_CAPTURE_H_INCLUDED diff --git a/include/internal/catch_interfaces_reporter.h b/include/internal/catch_interfaces_reporter.h index d1c6e704..e8a75066 100644 --- a/include/internal/catch_interfaces_reporter.h +++ b/include/internal/catch_interfaces_reporter.h @@ -91,12 +91,9 @@ namespace Catch { if( assertionResult.hasMessage() ) { // Copy message into messages list. - // !TBD This should have been done earlier, somewhere - MessageBuilder builder( assertionResult.getTestMacroName(), assertionResult.getSourceInfo(), assertionResult.getResultType() ); - builder << assertionResult.getMessage(); - builder.m_info.message = builder.m_stream.str(); - - infoMessages.push_back( builder.m_info ); + MessageInfo info( assertionResult.getTestMacroName(), assertionResult.getSourceInfo(), assertionResult.getResultType() ); + info.message = assertionResult.getMessage(); + infoMessages.push_back( info ); } } virtual ~AssertionStats(); diff --git a/include/internal/catch_legacy_reporter_adapter.hpp b/include/internal/catch_legacy_reporter_adapter.hpp index 6034581e..5a973bd4 100644 --- a/include/internal/catch_legacy_reporter_adapter.hpp +++ b/include/internal/catch_legacy_reporter_adapter.hpp @@ -46,7 +46,7 @@ namespace Catch it != itEnd; ++it ) { if( it->type == ResultWas::Info ) { - ResultBuilder rb( it->macroName.c_str(), it->lineInfo, "", ResultDisposition::Normal ); + ResultBuilder rb( getCurrentRunContext(), it->macroName.c_str(), it->lineInfo, "", ResultDisposition::Normal ); rb << it->message; rb.setResultType( ResultWas::Info ); AssertionResult result = rb.build(); diff --git a/include/internal/catch_message.h b/include/internal/catch_message.h index d2ec5257..1ff98351 100644 --- a/include/internal/catch_message.h +++ b/include/internal/catch_message.h @@ -38,10 +38,12 @@ namespace Catch { }; struct MessageBuilder { - MessageBuilder( std::string const& macroName, + MessageBuilder( IRunContext& runContext, + std::string const& macroName, SourceLineInfo const& lineInfo, ResultWas::OfType type ) - : m_info( macroName, lineInfo, type ) + : m_runContext( runContext ), + m_info( macroName, lineInfo, type ) {} template @@ -50,6 +52,7 @@ namespace Catch { return *this; } + IRunContext& m_runContext; MessageInfo m_info; std::ostringstream m_stream; }; diff --git a/include/internal/catch_message.hpp b/include/internal/catch_message.hpp index 934f75da..d9ac2c01 100644 --- a/include/internal/catch_message.hpp +++ b/include/internal/catch_message.hpp @@ -29,7 +29,7 @@ namespace Catch { ScopedMessage::ScopedMessage( MessageBuilder const& builder ) : m_info( builder.m_info ), - m_runContext( getCurrentRunContext() ) + m_runContext( builder.m_runContext ) { m_info.message = builder.m_stream.str(); m_runContext.pushScopedMessage( m_info ); diff --git a/include/internal/catch_result_builder.h b/include/internal/catch_result_builder.h index df412246..6ba9c325 100644 --- a/include/internal/catch_result_builder.h +++ b/include/internal/catch_result_builder.h @@ -37,11 +37,13 @@ namespace Catch { class ResultBuilder { public: - ResultBuilder( char const* macroName, - SourceLineInfo const& lineInfo, - char const* capturedExpression, - ResultDisposition::Flags resultDisposition, - char const* secondArg = "" ); + ResultBuilder + ( IRunContext& runContext, + char const* macroName, + SourceLineInfo const& lineInfo, + char const* capturedExpression, + ResultDisposition::Flags resultDisposition, + char const* secondArg = "" ); template ExpressionLhs operator <= ( T const& operand ); diff --git a/include/internal/catch_result_builder.hpp b/include/internal/catch_result_builder.hpp index 959a7e4c..37c80318 100644 --- a/include/internal/catch_result_builder.hpp +++ b/include/internal/catch_result_builder.hpp @@ -21,12 +21,14 @@ namespace Catch { ? capturedExpression : capturedExpression + ", " + secondArg; } - ResultBuilder::ResultBuilder( char const* macroName, - SourceLineInfo const& lineInfo, - char const* capturedExpression, - ResultDisposition::Flags resultDisposition, - char const* secondArg ) - : m_runContext( getCurrentRunContext() ), + ResultBuilder::ResultBuilder + ( IRunContext& runContext, + char const* macroName, + SourceLineInfo const& lineInfo, + char const* capturedExpression, + ResultDisposition::Flags resultDisposition, + char const* secondArg ) + : m_runContext( runContext ), m_assertionInfo( macroName, lineInfo, capturedExpressionWithSecondArgument( capturedExpression, secondArg ), resultDisposition ), m_shouldDebugBreak( false ), m_shouldThrow( false ) diff --git a/include/internal/catch_run_context.hpp b/include/internal/catch_run_context.hpp index ef2656cd..cb926823 100644 --- a/include/internal/catch_run_context.hpp +++ b/include/internal/catch_run_context.hpp @@ -75,9 +75,6 @@ namespace Catch { else return CATCH_NULL; } - AssertionResult const* getLastResult() { - return getCurrentRunContext().getLastResult(); - } class RunContext : public IRunContext { @@ -243,7 +240,8 @@ namespace Catch { // Recreate section for test case (as we will lose the one that was in scope) SectionInfo testCaseSection - ( m_activeTestCaseInfo->lineInfo, + ( C_A_T_C_H_Context(), + m_activeTestCaseInfo->lineInfo, m_activeTestCaseInfo->name, m_activeTestCaseInfo->description ); @@ -273,7 +271,7 @@ namespace Catch { private: void runTest( TestCase const& testCase, std::string& redirectedCout, std::string& redirectedCerr ) { - SectionInfo testCaseSection( testCase.lineInfo, testCase.name, testCase.description ); + SectionInfo testCaseSection( *this, testCase.lineInfo, testCase.name, testCase.description ); m_reporter->sectionStarting( testCaseSection ); Counts prevAssertions = m_totals.assertions; double duration = 0; @@ -325,11 +323,13 @@ namespace Catch { private: - ResultBuilder makeUnexpectedResultBuilder() const { - return ResultBuilder( m_lastAssertionInfo.macroName.c_str(), - m_lastAssertionInfo.lineInfo, - m_lastAssertionInfo.capturedExpression.c_str(), - m_lastAssertionInfo.resultDisposition ); + ResultBuilder makeUnexpectedResultBuilder() { + return ResultBuilder + ( *this, + m_lastAssertionInfo.macroName.c_str(), + m_lastAssertionInfo.lineInfo, + m_lastAssertionInfo.capturedExpression.c_str(), + m_lastAssertionInfo.resultDisposition ); } void handleUnfinishedSections() { diff --git a/include/internal/catch_section.h b/include/internal/catch_section.h index 04fd5bc8..d788eb97 100644 --- a/include/internal/catch_section.h +++ b/include/internal/catch_section.h @@ -40,10 +40,10 @@ namespace Catch { #ifdef CATCH_CONFIG_VARIADIC_MACROS #define INTERNAL_CATCH_SECTION( ... ) \ - if( Catch::Section const& INTERNAL_CATCH_UNIQUE_NAME( catch_internal_Section ) = Catch::SectionInfo( CATCH_INTERNAL_LINEINFO, __VA_ARGS__ ) ) + if( Catch::Section const& INTERNAL_CATCH_UNIQUE_NAME( catch_internal_Section ) = Catch::SectionInfo( C_A_T_C_H_Context(), CATCH_INTERNAL_LINEINFO, __VA_ARGS__ ) ) #else #define INTERNAL_CATCH_SECTION( name, desc ) \ - if( Catch::Section const& INTERNAL_CATCH_UNIQUE_NAME( catch_internal_Section ) = Catch::SectionInfo( CATCH_INTERNAL_LINEINFO, name, desc ) ) + if( Catch::Section const& INTERNAL_CATCH_UNIQUE_NAME( catch_internal_Section ) = Catch::SectionInfo( C_A_T_C_H_Context(), CATCH_INTERNAL_LINEINFO, name, desc ) ) #endif #endif // TWOBLUECUBES_CATCH_SECTION_H_INCLUDED diff --git a/include/internal/catch_section.hpp b/include/internal/catch_section.hpp index 9568c914..935c302f 100644 --- a/include/internal/catch_section.hpp +++ b/include/internal/catch_section.hpp @@ -16,17 +16,19 @@ namespace Catch { SectionInfo::SectionInfo - ( SourceLineInfo const& _lineInfo, + ( IRunContext& _runContext, + SourceLineInfo const& _lineInfo, std::string const& _name, std::string const& _description ) - : name( _name ), + : runContext( &_runContext ), + name( _name ), description( _description ), lineInfo( _lineInfo ) {} Section::Section( SectionInfo const& info ) : m_info( info ), - m_runContext( getCurrentRunContext() ), + m_runContext( *info.runContext ), m_sectionIncluded( m_runContext.sectionStarted( m_info, m_assertions ) ) { m_timer.start(); diff --git a/include/internal/catch_section_info.h b/include/internal/catch_section_info.h index 00b65600..f54a8cd0 100644 --- a/include/internal/catch_section_info.h +++ b/include/internal/catch_section_info.h @@ -10,15 +10,18 @@ #include "catch_common.h" #include "catch_totals.hpp" +#include "catch_interfaces_capture.h" namespace Catch { struct SectionInfo { SectionInfo - ( SourceLineInfo const& _lineInfo, + ( IRunContext& _runContext, + SourceLineInfo const& _lineInfo, std::string const& _name, std::string const& _description = std::string() ); + IRunContext* runContext; std::string name; std::string description; SourceLineInfo lineInfo; diff --git a/projects/SelfTest/PartTrackerTests.cpp b/projects/SelfTest/PartTrackerTests.cpp index 29256351..ff01b466 100644 --- a/projects/SelfTest/PartTrackerTests.cpp +++ b/projects/SelfTest/PartTrackerTests.cpp @@ -9,33 +9,10 @@ #include "internal/catch_test_case_tracker.hpp" -namespace Catch -{ - class LocalContext { - - public: - TrackerContext& operator()() const { - return TrackerContext::instance(); - } - }; - -} // namespace Catch - -inline Catch::TrackerContext& C_A_T_C_H_Context() { - return Catch::TrackerContext::instance(); -} - -// ------------------- - #include "catch.hpp" using namespace Catch; -//inline void testCase( Catch::LocalContext const& C_A_T_C_H_Context ) { -// -// REQUIRE( C_A_T_C_H_Context().i() == 42 ); -//} - TEST_CASE( "Tracker", "" ) { TrackerContext ctx;