From 4e12e12c1f60f20d5231229aba5c617348903547 Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Fri, 30 Nov 2012 19:15:23 +0000 Subject: [PATCH] Streaming reporter interface is now used natively. Legacy reporters are adapted by their factories. --- include/catch_runner.hpp | 2 +- include/internal/catch_interfaces_reporter.h | 4 +-- .../internal/catch_reporter_registrars.hpp | 29 ++++++++++++++++-- include/internal/catch_reporter_registry.hpp | 2 +- include/internal/catch_runner_impl.hpp | 30 +++++++++---------- projects/SelfTest/catch_self_test.cpp | 2 +- 6 files changed, 46 insertions(+), 23 deletions(-) diff --git a/include/catch_runner.hpp b/include/catch_runner.hpp index 74f28aa1..cac00af0 100644 --- a/include/catch_runner.hpp +++ b/include/catch_runner.hpp @@ -112,7 +112,7 @@ namespace Catch { Config& m_configWrapper; const ConfigData& m_config; std::ofstream m_ofs; - Ptr m_reporter; + Ptr m_reporter; std::set m_testsAlreadyRun; }; diff --git a/include/internal/catch_interfaces_reporter.h b/include/internal/catch_interfaces_reporter.h index fec09e77..7c30d96c 100644 --- a/include/internal/catch_interfaces_reporter.h +++ b/include/internal/catch_interfaces_reporter.h @@ -249,7 +249,7 @@ namespace Catch struct IReporterFactory { virtual ~IReporterFactory(); - virtual IReporter* create( ReporterConfig const& config ) const = 0; + virtual IStreamingReporter* create( ReporterConfig const& config ) const = 0; virtual std::string getDescription() const = 0; }; @@ -257,7 +257,7 @@ namespace Catch typedef std::map FactoryMap; virtual ~IReporterRegistry(); - virtual IReporter* create( std::string const& name, ReporterConfig const& config ) const = 0; + virtual IStreamingReporter* create( std::string const& name, ReporterConfig const& config ) const = 0; virtual const FactoryMap& getFactories() const = 0; }; diff --git a/include/internal/catch_reporter_registrars.hpp b/include/internal/catch_reporter_registrars.hpp index 33af47da..77ed5374 100644 --- a/include/internal/catch_reporter_registrars.hpp +++ b/include/internal/catch_reporter_registrars.hpp @@ -13,12 +13,12 @@ namespace Catch { template - class ReporterRegistrar { + class LegacyReporterRegistrar { class ReporterFactory : public IReporterFactory { - virtual IReporter* create( const ReporterConfig& config ) const { - return new T( config ); + virtual IStreamingReporter* create( const ReporterConfig& config ) const { + return new LegacyReporterAdapter( new T( config ), config ); } virtual std::string getDescription() const { @@ -26,6 +26,27 @@ namespace Catch { } }; + public: + + LegacyReporterRegistrar( const std::string& name ) { + getMutableRegistryHub().registerReporter( name, new ReporterFactory() ); + } + }; + + template + class ReporterRegistrar { + + class ReporterFactory : public IReporterFactory { + + virtual IStreamingReporter* create( const ReporterConfig& config ) const { + return new T( config ); + } + + virtual std::string getDescription() const { + return T::getDescription(); + } + }; + public: ReporterRegistrar( const std::string& name ) { @@ -35,6 +56,8 @@ namespace Catch { } #define INTERNAL_CATCH_REGISTER_REPORTER( name, reporterType ) \ + Catch::LegacyReporterRegistrar catch_internal_RegistrarFor##reporterType( name ); +#define INTERNAL_CATCH_REGISTER_REPORTER2( name, reporterType ) \ Catch::ReporterRegistrar catch_internal_RegistrarFor##reporterType( name ); #endif // TWOBLUECUBES_CATCH_REPORTER_REGISTRARS_HPP_INCLUDED diff --git a/include/internal/catch_reporter_registry.hpp b/include/internal/catch_reporter_registry.hpp index 21a48962..78219805 100644 --- a/include/internal/catch_reporter_registry.hpp +++ b/include/internal/catch_reporter_registry.hpp @@ -22,7 +22,7 @@ namespace Catch { deleteAllValues( m_factories ); } - virtual IReporter* create( const std::string& name, const ReporterConfig& config ) const { + virtual IStreamingReporter* create( const std::string& name, const ReporterConfig& config ) const { FactoryMap::const_iterator it = m_factories.find( name ); if( it == m_factories.end() ) return NULL; diff --git a/include/internal/catch_runner_impl.hpp b/include/internal/catch_runner_impl.hpp index 69d2158e..850fa56f 100644 --- a/include/internal/catch_runner_impl.hpp +++ b/include/internal/catch_runner_impl.hpp @@ -56,11 +56,11 @@ namespace Catch { public: - explicit Runner( const Config& config, const Ptr& reporter ) + explicit Runner( const Config& config, const Ptr& reporter ) : m_context( getCurrentMutableContext() ), m_runningTest( NULL ), m_config( config ), - m_reporter( reporter, ReporterConfig( m_config.stream(), m_config.data() ) ), + m_reporter( reporter ), m_prevRunner( &m_context.getRunner() ), m_prevResultCapture( &m_context.getResultCapture() ), m_prevConfig( m_context.getConfig() ) @@ -68,11 +68,11 @@ namespace Catch { m_context.setRunner( this ); m_context.setConfig( &m_config ); m_context.setResultCapture( this ); - m_reporter.testRunStarting( "" ); // !TBD - name + m_reporter->testRunStarting( "" ); // !TBD - name } virtual ~Runner() { - m_reporter.testRunEnded( new TestRunStats( "", m_totals, aborting() ) ); // !TBD - name + m_reporter->testRunEnded( new TestRunStats( "", m_totals, aborting() ) ); // !TBD - name m_context.setRunner( m_prevRunner ); m_context.setConfig( NULL ); m_context.setResultCapture( m_prevResultCapture ); @@ -80,10 +80,10 @@ namespace Catch { } void testGroupStarting( std::string const& testSpec ) { - m_reporter.testGroupStarting( testSpec ); + m_reporter->testGroupStarting( testSpec ); } void testGroupEnded( std::string const& testSpec, Totals const& totals ) { - m_reporter.testGroupEnded( new TestGroupStats( testSpec, totals, aborting() ) ); + m_reporter->testGroupEnded( new TestGroupStats( testSpec, totals, aborting() ) ); } Totals runMatching( const std::string& testSpec ) { @@ -111,7 +111,7 @@ namespace Catch { TestCaseInfo testInfo = testCase.getTestCaseInfo(); - m_reporter.testCaseStarting( testInfo ); + m_reporter->testCaseStarting( testInfo ); m_runningTest = new RunningTest( testCase ); @@ -134,7 +134,7 @@ namespace Catch { m_totals.testCases += deltaTotals.testCases; - m_reporter.testCaseEnded( new TestCaseStats( testInfo, + m_reporter->testCaseEnded( new TestCaseStats( testInfo, deltaTotals, redirectedCout, redirectedCerr, @@ -170,13 +170,13 @@ namespace Catch { std::vector::const_iterator it = m_scopedInfos.begin(); std::vector::const_iterator itEnd = m_scopedInfos.end(); for(; it != itEnd; ++it ) - m_reporter.assertionEnded( new AssertionStats( (*it)->buildResult( m_lastAssertionInfo ), m_totals ) ); + m_reporter->assertionEnded( new AssertionStats( (*it)->buildResult( m_lastAssertionInfo ), m_totals ) ); } { std::vector::const_iterator it = m_assertionResults.begin(); std::vector::const_iterator itEnd = m_assertionResults.end(); for(; it != itEnd; ++it ) - m_reporter.assertionEnded( new AssertionStats( *it, m_totals ) ); + m_reporter->assertionEnded( new AssertionStats( *it, m_totals ) ); } m_assertionResults.clear(); } @@ -187,7 +187,7 @@ namespace Catch { m_totals.assertions.info++; } else - m_reporter.assertionEnded( new AssertionStats( result, m_totals ) ); + m_reporter->assertionEnded( new AssertionStats( result, m_totals ) ); // Reset AssertionInfo m_lastAssertionInfo = AssertionInfo( "", m_lastAssertionInfo.lineInfo, "{Unknown expression after this line}" , m_lastAssertionInfo.resultDisposition ); @@ -207,7 +207,7 @@ namespace Catch { m_lastAssertionInfo.lineInfo = sectionInfo.lineInfo; - m_reporter.sectionStarting( sectionInfo ); + m_reporter->sectionStarting( sectionInfo ); assertions = m_totals.assertions; @@ -227,7 +227,7 @@ namespace Catch { } m_runningTest->endSection( info.name ); - m_reporter.sectionEnded( new SectionStats( info, assertions, missingAssertions ) ); + m_reporter->sectionEnded( new SectionStats( info, assertions, missingAssertions ) ); } virtual void pushScopedInfo( ScopedInfo* scopedInfo ) { @@ -281,7 +281,7 @@ namespace Catch { try { m_lastAssertionInfo = AssertionInfo( "TEST_CASE", m_runningTest->getTestCase().getTestCaseInfo().lineInfo, "", ResultDisposition::Normal ); m_runningTest->reset(); - if( m_reporter.getPreferences().shouldRedirectStdOut ) { + if( m_reporter->getPreferences().shouldRedirectStdOut ) { StreamRedirect coutRedir( std::cout, redirectedCout ); StreamRedirect cerrRedir( std::cerr, redirectedCerr ); m_runningTest->getTestCase().invoke(); @@ -309,7 +309,7 @@ namespace Catch { const Config& m_config; Totals m_totals; - LegacyReporterAdapter m_reporter; + Ptr m_reporter; std::vector m_scopedInfos; std::vector m_assertionResults; IRunner* m_prevRunner; diff --git a/projects/SelfTest/catch_self_test.cpp b/projects/SelfTest/catch_self_test.cpp index 244cd822..3182b884 100644 --- a/projects/SelfTest/catch_self_test.cpp +++ b/projects/SelfTest/catch_self_test.cpp @@ -24,7 +24,7 @@ namespace Catch{ // Scoped because Runner doesn't report EndTesting until its destructor { - Runner runner( config, m_reporter.get() ); + Runner runner( config, new LegacyReporterAdapter( m_reporter.get(), ReporterConfig( config.stream(), config.data() ) ) ); totals = runner.runMatching( rawTestSpec ); } m_output = oss.str();