From c4ba6757d91776a3d5da91499688b6ff29f4316d Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Thu, 29 Nov 2012 09:05:51 +0000 Subject: [PATCH] Added ReporterPreferences and started some SectionInfo refactoring --- include/internal/catch_interfaces_reporter.h | 20 +++- include/internal/catch_runner_impl.hpp | 9 +- include/internal/catch_running_test.hpp | 17 ++- include/internal/catch_section_info.hpp | 105 +++++++++++-------- 4 files changed, 93 insertions(+), 58 deletions(-) diff --git a/include/internal/catch_interfaces_reporter.h b/include/internal/catch_interfaces_reporter.h index 0ea59ef9..a0b274ab 100644 --- a/include/internal/catch_interfaces_reporter.h +++ b/include/internal/catch_interfaces_reporter.h @@ -36,6 +36,15 @@ namespace Catch ConfigData m_fullConfig; }; + struct ReporterPreferences + { + ReporterPreferences() + : shouldRedirectStdOut( false ) + {} + + bool shouldRedirectStdOut; + }; + struct AssertionStats { AssertionStats( const AssertionResult& _assertionResult, const Totals& _totals ) @@ -102,6 +111,8 @@ namespace Catch // !Work In progress struct IStreamingReporter : IShared { virtual ~IStreamingReporter(); + virtual ReporterPreferences getPreferences() const = 0; + virtual void testRunStarting( const std::string& runName ) = 0; virtual void testGroupStarting( const std::string& groupName ) = 0; @@ -160,8 +171,13 @@ namespace Catch m_config( config ) {} virtual ~LegacyReporterAdapter(); - - + + virtual ReporterPreferences getPreferences() const { + ReporterPreferences prefs; + prefs.shouldRedirectStdOut = m_legacyReporter->shouldRedirectStdout(); + return prefs; + } + virtual void testRunStarting( const std::string& ) { m_legacyReporter->StartTesting(); } diff --git a/include/internal/catch_runner_impl.hpp b/include/internal/catch_runner_impl.hpp index 039a3e38..e307a164 100644 --- a/include/internal/catch_runner_impl.hpp +++ b/include/internal/catch_runner_impl.hpp @@ -109,7 +109,7 @@ namespace Catch { LegacyReporterAdapter reporter( m_reporter, ReporterConfig( m_config.stream(), m_config.data() ) ); reporter.testCaseStarting( testInfo ); - m_runningTest = new RunningTest( &testCase ); + m_runningTest = new RunningTest( testCase ); do { do { @@ -202,6 +202,10 @@ namespace Catch { m_lastAssertionInfo.lineInfo = lineInfo; + // !TBD: ------------ + std::string className = m_runningTest->getTestCase().getTestCaseInfo().className; + TestCaseInfo sectionInfo( name, className, description, std::set(), false, lineInfo ); + m_reporter->StartSection( name, description ); assertions = m_totals.assertions; @@ -272,7 +276,8 @@ namespace Catch { try { m_lastAssertionInfo = AssertionInfo( "TEST_CASE", m_runningTest->getTestCase().getTestCaseInfo().lineInfo, "", ResultDisposition::Normal ); m_runningTest->reset(); - if( m_reporter->shouldRedirectStdout() ) { + LegacyReporterAdapter reporter( m_reporter, ReporterConfig( m_config.stream(), m_config.data() ) ); + if( reporter.getPreferences().shouldRedirectStdOut ) { StreamRedirect coutRedir( std::cout, redirectedCout ); StreamRedirect cerrRedir( std::cerr, redirectedCerr ); m_runningTest->getTestCase().invoke(); diff --git a/include/internal/catch_running_test.hpp b/include/internal/catch_running_test.hpp index 2bda148c..6f0ec7bf 100644 --- a/include/internal/catch_running_test.hpp +++ b/include/internal/catch_running_test.hpp @@ -24,9 +24,10 @@ namespace Catch { }; public: - explicit RunningTest( const TestCase* info = NULL ) + explicit RunningTest( const TestCase& info ) : m_info( info ), m_runStatus( RanAtLeastOneSection ), + m_rootSection( info.getTestCaseInfo().name ), m_currentSection( &m_rootSection ), m_changed( false ) {} @@ -71,12 +72,8 @@ namespace Catch { if( m_runStatus == NothingRun ) m_runStatus = EncounteredASection; - SectionInfo* thisSection = m_currentSection->findSubSection( name ); - if( !thisSection ) { - thisSection = m_currentSection->addSubSection( name ); - m_changed = true; - } - + SectionInfo* thisSection = m_currentSection->findOrAddSubSection( name, m_changed ); + if( !wasSectionSeen() && thisSection->shouldRun() ) { m_currentSection = thisSection; m_lastSectionToRun = NULL; @@ -98,16 +95,16 @@ namespace Catch { } const TestCase& getTestCase() const { - return *m_info; + return m_info; } - + bool hasUntestedSections() const { return m_runStatus == RanAtLeastOneSection || ( m_rootSection.hasUntestedSections() && m_changed ); } private: - const TestCase* m_info; + const TestCase& m_info; RunStatus m_runStatus; SectionInfo m_rootSection; SectionInfo* m_currentSection; diff --git a/include/internal/catch_section_info.hpp b/include/internal/catch_section_info.hpp index 2d4d0edc..9cdcd679 100644 --- a/include/internal/catch_section_info.hpp +++ b/include/internal/catch_section_info.hpp @@ -15,10 +15,17 @@ namespace Catch { - class SectionInfo { + struct ISectionInfo { + virtual ~ISectionInfo() {} + + virtual std::string getName() const = 0; + virtual const ISectionInfo* getParent() const = 0; + }; + + class SectionInfo : ISectionInfo { public: - enum Status { + enum State { Root, Unknown, Branch, @@ -26,61 +33,40 @@ namespace Catch { TestedLeaf }; - SectionInfo( SectionInfo* parent ) - : m_status( Unknown ), - m_parent( parent ) + SectionInfo( SectionInfo* parent, const std::string& name ) + : m_state( Unknown ), + m_parent( parent ), + m_name( name ) {} - SectionInfo() - : m_status( Root ), - m_parent( NULL ) + SectionInfo( const std::string& name ) + : m_state( Root ), + m_parent( NULL ), + m_name( name ) {} ~SectionInfo() { deleteAllValues( m_subSections ); } - + + virtual std::string getName() const { + return m_name; + } + bool shouldRun() const { - return m_status < TestedBranch; + return m_state < TestedBranch; } - bool ran() { - if( m_status < Branch ) { - m_status = TestedLeaf; - return true; - } - return false; - } - bool isBranch() const { - return m_status == Branch; + return m_state == Branch; } - void ranToCompletion() { - if( m_status == Branch && !hasUntestedSections() ) - m_status = TestedBranch; - } - - SectionInfo* findSubSection( const std::string& name ) { - std::map::const_iterator it = m_subSections.find( name ); - return it != m_subSections.end() - ? it->second - : NULL; - } - - SectionInfo* addSubSection( const std::string& name ) { - SectionInfo* subSection = new SectionInfo( this ); - m_subSections.insert( std::make_pair( name, subSection ) ); - m_status = Branch; - return subSection; - } - - SectionInfo* getParent() { + const SectionInfo* getParent() const { return m_parent; } - + bool hasUntestedSections() const { - if( m_status == Unknown ) + if( m_state == Unknown ) return true; std::map::const_iterator it = m_subSections.begin(); @@ -91,11 +77,42 @@ namespace Catch { } return false; } - + + // Mutable methods: + + SectionInfo* getParent() { + return m_parent; + } + + SectionInfo* findOrAddSubSection( const std::string& name, bool& changed ) { + std::map::const_iterator it = m_subSections.find( name ); + if( it != m_subSections.end() ) + return it->second; + SectionInfo* subSection = new SectionInfo( this, name ); + m_subSections.insert( std::make_pair( name, subSection ) ); + m_state = Branch; + changed = true; + return subSection; + } + + bool ran() { + if( m_state < Branch ) { + m_state = TestedLeaf; + return true; + } + return false; + } + + void ranToCompletion() { + if( m_state == Branch && !hasUntestedSections() ) + m_state = TestedBranch; + } + private: - Status m_status; - std::map m_subSections; + State m_state; SectionInfo* m_parent; + std::string m_name; + std::map m_subSections; }; }