From 7f04b56738ba74bebb675572abc1aaec4bffbb69 Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Fri, 30 Nov 2012 18:54:06 +0000 Subject: [PATCH] Reporter "stats" objects passed in by Ptr --- include/internal/catch_impl.hpp | 7 +- include/internal/catch_interfaces_reporter.h | 79 ++++++++++---------- include/internal/catch_ptr.hpp | 44 ++++------- include/internal/catch_runner_impl.hpp | 21 +++--- 4 files changed, 70 insertions(+), 81 deletions(-) diff --git a/include/internal/catch_impl.hpp b/include/internal/catch_impl.hpp index 15bdd3b6..295bf773 100644 --- a/include/internal/catch_impl.hpp +++ b/include/internal/catch_impl.hpp @@ -49,7 +49,12 @@ namespace Catch { IReporterRegistry::~IReporterRegistry() {} IStreamingReporter::~IStreamingReporter() {} LegacyReporterAdapter::~LegacyReporterAdapter() {} - + AssertionStats::~AssertionStats() {} + SectionStats::~SectionStats() {} + TestCaseStats::~TestCaseStats() {} + TestGroupStats::~TestGroupStats() {} + TestRunStats::~TestRunStats() {} + BasicReporter::~BasicReporter() {} IRunner::~IRunner() {} IMutableContext::~IMutableContext() {} diff --git a/include/internal/catch_interfaces_reporter.h b/include/internal/catch_interfaces_reporter.h index da7b67ce..fec09e77 100644 --- a/include/internal/catch_interfaces_reporter.h +++ b/include/internal/catch_interfaces_reporter.h @@ -57,19 +57,19 @@ namespace Catch SourceLineInfo lineInfo; }; - struct AssertionStats { + struct AssertionStats : SharedImpl<> { AssertionStats( AssertionResult const& _assertionResult, Totals const& _totals ) : assertionResult( _assertionResult ), totals( _totals ) {} + virtual ~AssertionStats(); -// AssertionInfo assertionInfo; // !TBD: needed? It's in the result AssertionResult assertionResult; Totals totals; }; - struct SectionStats { + struct SectionStats : SharedImpl<> { SectionStats( SectionInfo const& _sectionInfo, Counts const& _assertions, bool _missingAssertions ) @@ -77,13 +77,14 @@ namespace Catch assertions( _assertions ), missingAssertions( _missingAssertions ) {} - + virtual ~SectionStats(); + SectionInfo sectionInfo; Counts assertions; bool missingAssertions; }; - struct TestCaseStats { + struct TestCaseStats : SharedImpl<> { TestCaseStats( TestCaseInfo const& _testInfo, Totals const& _totals, std::string const& _stdOut, @@ -97,6 +98,7 @@ namespace Catch missingAssertions( _missingAssertions ), aborting( _aborting ) {} + virtual ~TestCaseStats(); TestCaseInfo testInfo; Totals totals; @@ -106,7 +108,7 @@ namespace Catch bool aborting; }; - struct TestGroupStats { + struct TestGroupStats : SharedImpl<> { TestGroupStats( std::string const& _groupName, Totals const& _totals, bool _aborting ) @@ -114,13 +116,14 @@ namespace Catch totals( _totals ), aborting( _aborting ) {} + virtual ~TestGroupStats(); std::string groupName; Totals totals; bool aborting; }; - struct TestRunStats { + struct TestRunStats : SharedImpl<> { TestRunStats( std::string const& _runName, Totals const& _totals, bool _aborting ) @@ -128,6 +131,7 @@ namespace Catch totals( _totals ), aborting( _aborting ) {} + virtual ~TestRunStats(); std::string runName; Totals totals; @@ -147,11 +151,11 @@ namespace Catch virtual void assertionStarting( AssertionInfo const& assertionInfo ) = 0; - virtual void assertionEnded( AssertionStats const& assertionStats ) = 0; - virtual void sectionEnded( SectionStats const& sectionStats ) = 0; - virtual void testCaseEnded( TestCaseStats const& testCaseStats ) = 0; - virtual void testGroupEnded( TestGroupStats const& testGroupStats ) = 0; - virtual void testRunEnded( TestRunStats const& testRunStats ) = 0; + virtual void assertionEnded( Ptr const& assertionStats ) = 0; + virtual void sectionEnded( Ptr const& sectionStats ) = 0; + virtual void testCaseEnded( Ptr const& testCaseStats ) = 0; + virtual void testGroupEnded( Ptr const& testGroupStats ) = 0; + virtual void testRunEnded( Ptr const& testRunStats ) = 0; }; // !TBD: Derived helper that implements the streaming interface but holds the stats // - declares a new interface where methods are called at the end of each event @@ -160,34 +164,23 @@ namespace Catch // as it goes + // Deprecated struct IReporter : IShared { virtual ~IReporter(); virtual bool shouldRedirectStdout() const = 0; virtual void StartTesting() = 0; - virtual void EndTesting( Totals const& totals ) = 0; - + virtual void EndTesting( Totals const& totals ) = 0; virtual void StartGroup( std::string const& groupName ) = 0; - virtual void EndGroup( std::string const& groupName, Totals const& totals ) = 0; - + virtual void EndGroup( std::string const& groupName, Totals const& totals ) = 0; virtual void StartTestCase( TestCaseInfo const& testInfo ) = 0; - // TestCaseResult virtual void EndTestCase( TestCaseInfo const& testInfo, Totals const& totals, std::string const& stdOut, std::string const& stdErr ) = 0; - - // SectionInfo virtual void StartSection( std::string const& sectionName, std::string const& description ) = 0; - // Section Result virtual void EndSection( std::string const& sectionName, const Counts& assertions ) = 0; - - // - merge into SectionResult ? virtual void NoAssertionsInSection( std::string const& sectionName ) = 0; virtual void NoAssertionsInTestCase( std::string const& testName ) = 0; - - // - merge into SectionResult, TestCaseResult, GroupResult & TestRunResult virtual void Aborted() = 0; - - // AssertionReslt virtual void Result( const AssertionResult& result ) = 0; }; @@ -222,26 +215,30 @@ namespace Catch // Not on legacy interface } - virtual void assertionEnded( AssertionStats const& assertionStats ) { - m_legacyReporter->Result( assertionStats.assertionResult ); + virtual void assertionEnded( Ptr const& assertionStats ) { + m_legacyReporter->Result( assertionStats->assertionResult ); } - virtual void sectionEnded( SectionStats const& sectionStats ) { - if( sectionStats.missingAssertions ) - m_legacyReporter->NoAssertionsInSection( sectionStats.sectionInfo.name ); - m_legacyReporter->EndSection( sectionStats.sectionInfo.name, sectionStats.assertions ); + virtual void sectionEnded( Ptr const& sectionStats ) { + if( sectionStats->missingAssertions ) + m_legacyReporter->NoAssertionsInSection( sectionStats->sectionInfo.name ); + m_legacyReporter->EndSection( sectionStats->sectionInfo.name, sectionStats->assertions ); } - virtual void testCaseEnded( TestCaseStats const& testCaseStats ) { - if( testCaseStats.missingAssertions ) - m_legacyReporter->NoAssertionsInTestCase( testCaseStats.testInfo.name ); - m_legacyReporter->EndTestCase( testCaseStats.testInfo, testCaseStats.totals, testCaseStats.stdOut, testCaseStats.stdErr ); + virtual void testCaseEnded( Ptr const& testCaseStats ) { + if( testCaseStats->missingAssertions ) + m_legacyReporter->NoAssertionsInTestCase( testCaseStats->testInfo.name ); + m_legacyReporter->EndTestCase + ( testCaseStats->testInfo, + testCaseStats->totals, + testCaseStats->stdOut, + testCaseStats->stdErr ); } - virtual void testGroupEnded( TestGroupStats const& testGroupStats ) { - if( testGroupStats.aborting ) + virtual void testGroupEnded( Ptr const& testGroupStats ) { + if( testGroupStats->aborting ) m_legacyReporter->Aborted(); - m_legacyReporter->EndGroup( testGroupStats.groupName, testGroupStats.totals ); + m_legacyReporter->EndGroup( testGroupStats->groupName, testGroupStats->totals ); } - virtual void testRunEnded( TestRunStats const& testRunStats ) { - m_legacyReporter->EndTesting( testRunStats.totals ); + virtual void testRunEnded( Ptr const& testRunStats ) { + m_legacyReporter->EndTesting( testRunStats->totals ); } private: diff --git a/include/internal/catch_ptr.hpp b/include/internal/catch_ptr.hpp index 19e92a8d..60000aaa 100644 --- a/include/internal/catch_ptr.hpp +++ b/include/internal/catch_ptr.hpp @@ -23,7 +23,7 @@ namespace Catch { if( m_p ) m_p->addRef(); } - Ptr( const Ptr& other ) : m_p( other.m_p ){ + Ptr( Ptr const& other ) : m_p( other.m_p ){ if( m_p ) m_p->addRef(); } @@ -36,33 +36,17 @@ namespace Catch { swap( temp ); return *this; } - Ptr& operator = ( const Ptr& other ){ + Ptr& operator = ( Ptr const& other ){ Ptr temp( other ); swap( temp ); return *this; } - void swap( Ptr& other ){ - std::swap( m_p, other.m_p ); - } - - T* get(){ - return m_p; - } - const T* get() const{ - return m_p; - } - - T& operator*() const { - return *m_p; - } - - T* operator->() const { - return m_p; - } - - bool operator !() const { - return m_p == NULL; - } + void swap( Ptr& other ) { std::swap( m_p, other.m_p ); } + T* get() { return m_p; } + const T* get() const{ return m_p; } + T& operator*() const { return *m_p; } + T* operator->() const { return m_p; } + bool operator !() const { return m_p == NULL; } private: T* m_p; @@ -70,24 +54,24 @@ namespace Catch { struct IShared : NonCopyable { virtual ~IShared(); - virtual void addRef() = 0; - virtual void release() = 0; + virtual void addRef() const = 0; + virtual void release() const = 0; }; - template + template struct SharedImpl : T { SharedImpl() : m_rc( 0 ){} - virtual void addRef(){ + virtual void addRef() const { ++m_rc; } - virtual void release(){ + virtual void release() const { if( --m_rc == 0 ) delete this; } - int m_rc; + mutable unsigned int m_rc; }; } // end namespace Catch diff --git a/include/internal/catch_runner_impl.hpp b/include/internal/catch_runner_impl.hpp index 66fb52d6..69d2158e 100644 --- a/include/internal/catch_runner_impl.hpp +++ b/include/internal/catch_runner_impl.hpp @@ -72,7 +72,7 @@ namespace Catch { } virtual ~Runner() { - m_reporter.testRunEnded( 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 ); @@ -83,7 +83,7 @@ namespace Catch { m_reporter.testGroupStarting( testSpec ); } void testGroupEnded( std::string const& testSpec, Totals const& totals ) { - m_reporter.testGroupEnded( TestGroupStats( testSpec, totals, aborting() ) ); + m_reporter.testGroupEnded( new TestGroupStats( testSpec, totals, aborting() ) ); } Totals runMatching( const std::string& testSpec ) { @@ -134,8 +134,12 @@ namespace Catch { m_totals.testCases += deltaTotals.testCases; - TestCaseStats stats( testInfo, deltaTotals, redirectedCout, redirectedCerr, missingAssertions, aborting() ); - m_reporter.testCaseEnded( stats ); + m_reporter.testCaseEnded( new TestCaseStats( testInfo, + deltaTotals, + redirectedCout, + redirectedCerr, + missingAssertions, + aborting() ) ); delete m_runningTest; @@ -166,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( 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( AssertionStats( *it, m_totals ) ); + m_reporter.assertionEnded( new AssertionStats( *it, m_totals ) ); } m_assertionResults.clear(); } @@ -183,7 +187,7 @@ namespace Catch { m_totals.assertions.info++; } else - m_reporter.assertionEnded( 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 ); @@ -223,8 +227,7 @@ namespace Catch { } m_runningTest->endSection( info.name ); - SectionStats stats( info, assertions, missingAssertions ); - m_reporter.sectionEnded( stats ); + m_reporter.sectionEnded( new SectionStats( info, assertions, missingAssertions ) ); } virtual void pushScopedInfo( ScopedInfo* scopedInfo ) {