Added ReporterPreferences and started some SectionInfo refactoring

This commit is contained in:
Phil Nash 2012-11-29 09:05:51 +00:00
parent 994e64c217
commit c4ba6757d9
4 changed files with 93 additions and 58 deletions

View File

@ -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;
@ -161,6 +172,11 @@ namespace Catch
{}
virtual ~LegacyReporterAdapter();
virtual ReporterPreferences getPreferences() const {
ReporterPreferences prefs;
prefs.shouldRedirectStdOut = m_legacyReporter->shouldRedirectStdout();
return prefs;
}
virtual void testRunStarting( const std::string& ) {
m_legacyReporter->StartTesting();

View File

@ -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<std::string>(), 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();

View File

@ -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,11 +72,7 @@ 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;
@ -98,7 +95,7 @@ namespace Catch {
}
const TestCase& getTestCase() const {
return *m_info;
return m_info;
}
bool hasUntestedSections() const {
@ -107,7 +104,7 @@ namespace Catch {
}
private:
const TestCase* m_info;
const TestCase& m_info;
RunStatus m_runStatus;
SectionInfo m_rootSection;
SectionInfo* m_currentSection;

View File

@ -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 );
}
bool shouldRun() const {
return m_status < TestedBranch;
virtual std::string getName() const {
return m_name;
}
bool ran() {
if( m_status < Branch ) {
m_status = TestedLeaf;
return true;
}
return false;
bool shouldRun() const {
return m_state < TestedBranch;
}
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<std::string, SectionInfo*>::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<std::string, SectionInfo*>::const_iterator it = m_subSections.begin();
@ -92,10 +78,41 @@ namespace Catch {
return false;
}
// Mutable methods:
SectionInfo* getParent() {
return m_parent;
}
SectionInfo* findOrAddSubSection( const std::string& name, bool& changed ) {
std::map<std::string, SectionInfo*>::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<std::string, SectionInfo*> m_subSections;
State m_state;
SectionInfo* m_parent;
std::string m_name;
std::map<std::string, SectionInfo*> m_subSections;
};
}