Made ReportConfig a value type

This commit is contained in:
Phil Nash 2012-07-20 19:07:42 +01:00
parent f606517376
commit d2553cdc64
10 changed files with 156 additions and 134 deletions

View File

@ -25,13 +25,16 @@ namespace Catch {
INTERNAL_CATCH_REGISTER_REPORTER( "basic", BasicReporter ) INTERNAL_CATCH_REGISTER_REPORTER( "basic", BasicReporter )
INTERNAL_CATCH_REGISTER_REPORTER( "xml", XmlReporter ) INTERNAL_CATCH_REGISTER_REPORTER( "xml", XmlReporter )
INTERNAL_CATCH_REGISTER_REPORTER( "junit", JunitReporter ) INTERNAL_CATCH_REGISTER_REPORTER( "junit", JunitReporter )
inline int Main( Config& config ) { inline int Main( Config& config ) {
std::string reporterName = config.data().reporter.empty() std::string reporterName = config.data().reporter.empty()
? "basic" ? "basic"
: config.data().reporter; : config.data().reporter;
Ptr<IReporter> reporter = getCurrentContext().getReporterRegistry().create( reporterName, config );
ReporterConfig reporterConfig( config.getName(), config.stream(), config.includeSuccessfulResults() );
Ptr<IReporter> reporter = getCurrentContext().getReporterRegistry().create( reporterName, reporterConfig );
if( !config.data().stream.empty() ) { if( !config.data().stream.empty() ) {
if( config.data().stream[0] == '%' ) if( config.data().stream[0] == '%' )

View File

@ -60,7 +60,7 @@ namespace Catch {
}; };
class Config : public IReporterConfig, public IConfig { class Config : public IConfig {
private: private:
Config( const Config& other ); Config( const Config& other );
Config& operator = ( const Config& other ); Config& operator = ( const Config& other );

View File

@ -18,12 +18,20 @@
namespace Catch namespace Catch
{ {
struct IReporterConfig { struct ReporterConfig
virtual ~IReporterConfig() {} {
virtual std::ostream& stream () const = 0; ReporterConfig( const std::string& _name,
virtual bool includeSuccessfulResults () const = 0; std::ostream& _stream,
virtual std::string getName () const = 0; bool _includeSuccessfulResults = false )
}; : name( _name ),
stream( _stream ),
includeSuccessfulResults( _includeSuccessfulResults )
{}
std::string name;
std::ostream& stream;
bool includeSuccessfulResults;
};
class TestCaseInfo; class TestCaseInfo;
class ResultInfo; class ResultInfo;
@ -45,7 +53,7 @@ namespace Catch
struct IReporterFactory { struct IReporterFactory {
virtual ~IReporterFactory() {} virtual ~IReporterFactory() {}
virtual IReporter* create( const IReporterConfig& config ) const = 0; virtual IReporter* create( const ReporterConfig& config ) const = 0;
virtual std::string getDescription() const = 0; virtual std::string getDescription() const = 0;
}; };
@ -53,7 +61,7 @@ namespace Catch
typedef std::map<std::string, IReporterFactory*> FactoryMap; typedef std::map<std::string, IReporterFactory*> FactoryMap;
virtual ~IReporterRegistry() {} virtual ~IReporterRegistry() {}
virtual IReporter* create( const std::string& name, const IReporterConfig& config ) const = 0; virtual IReporter* create( const std::string& name, const ReporterConfig& config ) const = 0;
virtual void registerReporter( const std::string& name, IReporterFactory* factory ) = 0; virtual void registerReporter( const std::string& name, IReporterFactory* factory ) = 0;
virtual const FactoryMap& getFactories() const = 0; virtual const FactoryMap& getFactories() const = 0;
}; };

View File

@ -17,7 +17,7 @@ namespace Catch {
class ReporterFactory : public IReporterFactory { class ReporterFactory : public IReporterFactory {
virtual IReporter* create( const IReporterConfig& config ) const { virtual IReporter* create( const ReporterConfig& config ) const {
return new T( config ); return new T( config );
} }

View File

@ -22,7 +22,7 @@ namespace Catch {
deleteAllValues( m_factories ); deleteAllValues( m_factories );
} }
virtual IReporter* create( const std::string& name, const IReporterConfig& config ) const { virtual IReporter* create( const std::string& name, const ReporterConfig& config ) const {
FactoryMap::const_iterator it = m_factories.find( name ); FactoryMap::const_iterator it = m_factories.find( name );
if( it == m_factories.end() ) if( it == m_factories.end() )
return NULL; return NULL;

View File

@ -55,7 +55,7 @@ namespace Catch {
}; };
public: public:
BasicReporter( const IReporterConfig& config ) BasicReporter( const ReporterConfig& config )
: m_config( config ), : m_config( config ),
m_firstSectionInTestCase( true ), m_firstSectionInTestCase( true ),
m_aborted( false ) m_aborted( false )
@ -69,27 +69,27 @@ namespace Catch {
void ReportCounts( const std::string& label, const Counts& counts, const std::string& allPrefix = "All " ) { void ReportCounts( const std::string& label, const Counts& counts, const std::string& allPrefix = "All " ) {
if( counts.passed ) if( counts.passed )
m_config.stream() << counts.failed << " of " << counts.total() << " " << label << "s failed"; m_config.stream << counts.failed << " of " << counts.total() << " " << label << "s failed";
else else
m_config.stream() << ( counts.failed > 1 ? allPrefix : "" ) << pluralise( counts.failed, label ) << " failed"; m_config.stream << ( counts.failed > 1 ? allPrefix : "" ) << pluralise( counts.failed, label ) << " failed";
} }
void ReportCounts( const Totals& totals, const std::string& allPrefix = "All " ) { void ReportCounts( const Totals& totals, const std::string& allPrefix = "All " ) {
if( totals.assertions.total() == 0 ) { if( totals.assertions.total() == 0 ) {
m_config.stream() << "No tests ran"; m_config.stream << "No tests ran";
} }
else if( totals.assertions.failed ) { else if( totals.assertions.failed ) {
TextColour colour( TextColour::ResultError ); TextColour colour( TextColour::ResultError );
ReportCounts( "test case", totals.testCases, allPrefix ); ReportCounts( "test case", totals.testCases, allPrefix );
if( totals.testCases.failed > 0 ) { if( totals.testCases.failed > 0 ) {
m_config.stream() << " ("; m_config.stream << " (";
ReportCounts( "assertion", totals.assertions, allPrefix ); ReportCounts( "assertion", totals.assertions, allPrefix );
m_config.stream() << ")"; m_config.stream << ")";
} }
} }
else { else {
TextColour colour( TextColour::ResultSuccess ); TextColour colour( TextColour::ResultSuccess );
m_config.stream() << allPrefix << "tests passed (" m_config.stream << allPrefix << "tests passed ("
<< pluralise( totals.assertions.passed, "assertion" ) << " in " << pluralise( totals.assertions.passed, "assertion" ) << " in "
<< pluralise( totals.testCases.passed, "test case" ) << ")"; << pluralise( totals.testCases.passed, "test case" ) << ")";
} }
@ -112,14 +112,14 @@ namespace Catch {
virtual void EndTesting( const Totals& totals ) { virtual void EndTesting( const Totals& totals ) {
// Output the overall test results even if "Started Testing" was not emitted // Output the overall test results even if "Started Testing" was not emitted
if( m_aborted ) { if( m_aborted ) {
m_config.stream() << "\n[Testing aborted. "; m_config.stream << "\n[Testing aborted. ";
ReportCounts( totals, "The first " ); ReportCounts( totals, "The first " );
} }
else { else {
m_config.stream() << "\n[Testing completed. "; m_config.stream << "\n[Testing completed. ";
ReportCounts( totals ); ReportCounts( totals );
} }
m_config.stream() << "]\n" << std::endl; m_config.stream << "]\n" << std::endl;
} }
virtual void StartGroup( const std::string& groupName ) { virtual void StartGroup( const std::string& groupName ) {
@ -128,9 +128,9 @@ namespace Catch {
virtual void EndGroup( const std::string& groupName, const Totals& totals ) { virtual void EndGroup( const std::string& groupName, const Totals& totals ) {
if( m_groupSpan.emitted && !groupName.empty() ) { if( m_groupSpan.emitted && !groupName.empty() ) {
m_config.stream() << "[End of group: '" << groupName << "'. "; m_config.stream << "[End of group: '" << groupName << "'. ";
ReportCounts( totals ); ReportCounts( totals );
m_config.stream() << "]\n" << std::endl; m_config.stream << "]\n" << std::endl;
m_groupSpan = SpanInfo(); m_groupSpan = SpanInfo();
} }
} }
@ -146,7 +146,7 @@ namespace Catch {
virtual void EndSection( const std::string& sectionName, const Counts& assertions ) { virtual void EndSection( const std::string& sectionName, const Counts& assertions ) {
SpanInfo& sectionSpan = m_sectionSpans.back(); SpanInfo& sectionSpan = m_sectionSpans.back();
if( sectionSpan.emitted && !sectionSpan.name.empty() ) { if( sectionSpan.emitted && !sectionSpan.name.empty() ) {
m_config.stream() << "[End of section: '" << sectionName << "' "; m_config.stream << "[End of section: '" << sectionName << "' ";
if( assertions.failed ) { if( assertions.failed ) {
TextColour colour( TextColour::ResultError ); TextColour colour( TextColour::ResultError );
@ -154,35 +154,35 @@ namespace Catch {
} }
else { else {
TextColour colour( TextColour::ResultSuccess ); TextColour colour( TextColour::ResultSuccess );
m_config.stream() << ( assertions.passed > 1 ? "All " : "" ) m_config.stream << ( assertions.passed > 1 ? "All " : "" )
<< pluralise( assertions.passed, "assertion" ) << " passed" ; << pluralise( assertions.passed, "assertion" ) << " passed" ;
} }
m_config.stream() << "]\n" << std::endl; m_config.stream << "]\n" << std::endl;
} }
m_sectionSpans.pop_back(); m_sectionSpans.pop_back();
} }
virtual void Result( const ResultInfo& resultInfo ) { virtual void Result( const ResultInfo& resultInfo ) {
if( !m_config.includeSuccessfulResults() && resultInfo.getResultType() == ResultWas::Ok ) if( !m_config.includeSuccessfulResults && resultInfo.getResultType() == ResultWas::Ok )
return; return;
StartSpansLazily(); StartSpansLazily();
if( !resultInfo.getFilename().empty() ) { if( !resultInfo.getFilename().empty() ) {
TextColour colour( TextColour::FileName ); TextColour colour( TextColour::FileName );
m_config.stream() << SourceLineInfo( resultInfo.getFilename(), resultInfo.getLine() ); m_config.stream << SourceLineInfo( resultInfo.getFilename(), resultInfo.getLine() );
} }
if( resultInfo.hasExpression() ) { if( resultInfo.hasExpression() ) {
TextColour colour( TextColour::OriginalExpression ); TextColour colour( TextColour::OriginalExpression );
m_config.stream() << resultInfo.getExpression(); m_config.stream << resultInfo.getExpression();
if( resultInfo.ok() ) { if( resultInfo.ok() ) {
TextColour successColour( TextColour::Success ); TextColour successColour( TextColour::Success );
m_config.stream() << " succeeded"; m_config.stream << " succeeded";
} }
else { else {
TextColour errorColour( TextColour::Error ); TextColour errorColour( TextColour::Error );
m_config.stream() << " failed"; m_config.stream << " failed";
} }
} }
switch( resultInfo.getResultType() ) { switch( resultInfo.getResultType() ) {
@ -190,31 +190,31 @@ namespace Catch {
{ {
TextColour colour( TextColour::Error ); TextColour colour( TextColour::Error );
if( resultInfo.hasExpression() ) if( resultInfo.hasExpression() )
m_config.stream() << " with unexpected"; m_config.stream << " with unexpected";
else else
m_config.stream() << "Unexpected"; m_config.stream << "Unexpected";
m_config.stream() << " exception with message: '" << resultInfo.getMessage() << "'"; m_config.stream << " exception with message: '" << resultInfo.getMessage() << "'";
} }
break; break;
case ResultWas::DidntThrowException: case ResultWas::DidntThrowException:
{ {
TextColour colour( TextColour::Error ); TextColour colour( TextColour::Error );
if( resultInfo.hasExpression() ) if( resultInfo.hasExpression() )
m_config.stream() << " because no exception was thrown where one was expected"; m_config.stream << " because no exception was thrown where one was expected";
else else
m_config.stream() << "No exception thrown where one was expected"; m_config.stream << "No exception thrown where one was expected";
} }
break; break;
case ResultWas::Info: case ResultWas::Info:
streamVariableLengthText( "info", resultInfo.getMessage() ); streamVariableLengthText( "info", resultInfo.getMessage() );
break; break;
case ResultWas::Warning: case ResultWas::Warning:
m_config.stream() << "warning:\n'" << resultInfo.getMessage() << "'"; m_config.stream << "warning:\n'" << resultInfo.getMessage() << "'";
break; break;
case ResultWas::ExplicitFailure: case ResultWas::ExplicitFailure:
{ {
TextColour colour( TextColour::Error ); TextColour colour( TextColour::Error );
m_config.stream() << "failed with message: '" << resultInfo.getMessage() << "'"; m_config.stream << "failed with message: '" << resultInfo.getMessage() << "'";
} }
break; break;
case ResultWas::Unknown: // These cases are here to prevent compiler warnings case ResultWas::Unknown: // These cases are here to prevent compiler warnings
@ -226,22 +226,22 @@ namespace Catch {
if( !resultInfo.hasExpression() ) { if( !resultInfo.hasExpression() ) {
if( resultInfo.ok() ) { if( resultInfo.ok() ) {
TextColour colour( TextColour::Success ); TextColour colour( TextColour::Success );
m_config.stream() << " succeeded"; m_config.stream << " succeeded";
} }
else { else {
TextColour colour( TextColour::Error ); TextColour colour( TextColour::Error );
m_config.stream() << " failed"; m_config.stream << " failed";
} }
} }
break; break;
} }
if( resultInfo.hasExpandedExpression() ) { if( resultInfo.hasExpandedExpression() ) {
m_config.stream() << " for: "; m_config.stream << " for: ";
TextColour colour( TextColour::ReconstructedExpression ); TextColour colour( TextColour::ReconstructedExpression );
m_config.stream() << resultInfo.getExpandedExpression(); m_config.stream << resultInfo.getExpandedExpression();
} }
m_config.stream() << std::endl; m_config.stream << std::endl;
} }
virtual void EndTestCase( const TestCaseInfo& testInfo, virtual void EndTestCase( const TestCaseInfo& testInfo,
@ -259,9 +259,9 @@ namespace Catch {
} }
if( m_testSpan.emitted ) { if( m_testSpan.emitted ) {
m_config.stream() << "[Finished: '" << testInfo.getName() << "' "; m_config.stream << "[Finished: '" << testInfo.getName() << "' ";
ReportCounts( totals ); ReportCounts( totals );
m_config.stream() << "]" << std::endl; m_config.stream << "]" << std::endl;
} }
} }
@ -269,20 +269,20 @@ namespace Catch {
void StartSpansLazily() { void StartSpansLazily() {
if( !m_testingSpan.emitted ) { if( !m_testingSpan.emitted ) {
if( m_config.getName().empty() ) if( m_config.name.empty() )
m_config.stream() << "[Started testing]" << std::endl; m_config.stream << "[Started testing]" << std::endl;
else else
m_config.stream() << "[Started testing: " << m_config.getName() << "]" << std::endl; m_config.stream << "[Started testing: " << m_config.name << "]" << std::endl;
m_testingSpan.emitted = true; m_testingSpan.emitted = true;
} }
if( !m_groupSpan.emitted && !m_groupSpan.name.empty() ) { if( !m_groupSpan.emitted && !m_groupSpan.name.empty() ) {
m_config.stream() << "[Started group: '" << m_groupSpan.name << "']" << std::endl; m_config.stream << "[Started group: '" << m_groupSpan.name << "']" << std::endl;
m_groupSpan.emitted = true; m_groupSpan.emitted = true;
} }
if( !m_testSpan.emitted ) { if( !m_testSpan.emitted ) {
m_config.stream() << std::endl << "[Running: " << m_testSpan.name << "]" << std::endl; m_config.stream << std::endl << "[Running: " << m_testSpan.name << "]" << std::endl;
m_testSpan.emitted = true; m_testSpan.emitted = true;
} }
@ -290,7 +290,7 @@ namespace Catch {
SpanInfo& sectionSpan = m_sectionSpans.back(); SpanInfo& sectionSpan = m_sectionSpans.back();
if( !sectionSpan.emitted && !sectionSpan.name.empty() ) { if( !sectionSpan.emitted && !sectionSpan.name.empty() ) {
if( m_firstSectionInTestCase ) { if( m_firstSectionInTestCase ) {
m_config.stream() << "\n"; m_config.stream << "\n";
m_firstSectionInTestCase = false; m_firstSectionInTestCase = false;
} }
std::vector<SpanInfo>::iterator it = m_sectionSpans.begin(); std::vector<SpanInfo>::iterator it = m_sectionSpans.begin();
@ -298,7 +298,7 @@ namespace Catch {
for(; it != itEnd; ++it ) { for(; it != itEnd; ++it ) {
SpanInfo& prevSpan = *it; SpanInfo& prevSpan = *it;
if( !prevSpan.emitted && !prevSpan.name.empty() ) { if( !prevSpan.emitted && !prevSpan.name.empty() ) {
m_config.stream() << "[Started section: '" << prevSpan.name << "']" << std::endl; m_config.stream << "[Started section: '" << prevSpan.name << "']" << std::endl;
prevSpan.emitted = true; prevSpan.emitted = true;
} }
} }
@ -309,16 +309,16 @@ namespace Catch {
void streamVariableLengthText( const std::string& prefix, const std::string& text ) { void streamVariableLengthText( const std::string& prefix, const std::string& text ) {
std::string trimmed = trim( text ); std::string trimmed = trim( text );
if( trimmed.find_first_of( "\r\n" ) == std::string::npos ) { if( trimmed.find_first_of( "\r\n" ) == std::string::npos ) {
m_config.stream() << "[" << prefix << ": " << trimmed << "]\n"; m_config.stream << "[" << prefix << ": " << trimmed << "]\n";
} }
else { else {
m_config.stream() << "\n[" << prefix << "] >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n" << trimmed m_config.stream << "\n[" << prefix << "] >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n" << trimmed
<< "\n[end of " << prefix << "] <<<<<<<<<<<<<<<<<<<<<<<<\n"; << "\n[end of " << prefix << "] <<<<<<<<<<<<<<<<<<<<<<<<\n";
} }
} }
private: private:
const IReporterConfig& m_config; ReporterConfig m_config;
bool m_firstSectionInTestCase; bool m_firstSectionInTestCase;
SpanInfo m_testingSpan; SpanInfo m_testingSpan;

View File

@ -57,7 +57,7 @@ namespace Catch {
}; };
public: public:
JunitReporter( const IReporterConfig& config ) JunitReporter( const ReporterConfig& config )
: m_config( config ), : m_config( config ),
m_testSuiteStats( "AllTests" ), m_testSuiteStats( "AllTests" ),
m_currentStats( &m_testSuiteStats ) m_currentStats( &m_testSuiteStats )
@ -94,7 +94,7 @@ namespace Catch {
} }
virtual void Result( const Catch::ResultInfo& resultInfo ) { virtual void Result( const Catch::ResultInfo& resultInfo ) {
if( resultInfo.getResultType() != ResultWas::Ok || m_config.includeSuccessfulResults() ) { if( resultInfo.getResultType() != ResultWas::Ok || m_config.includeSuccessfulResults ) {
TestCaseStats& testCaseStats = m_currentStats->m_testCaseStats.back(); TestCaseStats& testCaseStats = m_currentStats->m_testCaseStats.back();
TestStats stats; TestStats stats;
std::ostringstream oss; std::ostringstream oss;
@ -151,7 +151,7 @@ namespace Catch {
} }
virtual void EndTesting( const Totals& ) { virtual void EndTesting( const Totals& ) {
std::ostream& str = m_config.stream(); std::ostream& str = m_config.stream;
{ {
XmlWriter xml( str ); XmlWriter xml( str );
@ -212,7 +212,7 @@ namespace Catch {
} }
private: private:
const IReporterConfig& m_config; ReporterConfig m_config;
bool m_currentTestSuccess; bool m_currentTestSuccess;
Stats m_testSuiteStats; Stats m_testSuiteStats;

View File

@ -16,7 +16,7 @@
namespace Catch { namespace Catch {
class XmlReporter : public SharedImpl<IReporter> { class XmlReporter : public SharedImpl<IReporter> {
public: public:
XmlReporter( const IReporterConfig& config ) : m_config( config ) {} XmlReporter( const ReporterConfig& config ) : m_config( config ) {}
static std::string getDescription() { static std::string getDescription() {
return "Reports test results as an XML document"; return "Reports test results as an XML document";
@ -29,10 +29,10 @@ namespace Catch {
} }
virtual void StartTesting() { virtual void StartTesting() {
m_xml = XmlWriter( m_config.stream() ); m_xml = XmlWriter( m_config.stream );
m_xml.startElement( "Catch" ); m_xml.startElement( "Catch" );
if( !m_config.getName().empty() ) if( !m_config.name.empty() )
m_xml.writeAttribute( "name", m_config.getName() ); m_xml.writeAttribute( "name", m_config.name );
} }
virtual void EndTesting( const Totals& totals ) { virtual void EndTesting( const Totals& totals ) {
@ -73,7 +73,7 @@ namespace Catch {
} }
virtual void Result( const Catch::ResultInfo& resultInfo ) { virtual void Result( const Catch::ResultInfo& resultInfo ) {
if( !m_config.includeSuccessfulResults() && resultInfo.getResultType() == ResultWas::Ok ) if( !m_config.includeSuccessfulResults && resultInfo.getResultType() == ResultWas::Ok )
return; return;
if( resultInfo.hasExpression() ) { if( resultInfo.hasExpression() ) {
@ -133,7 +133,7 @@ namespace Catch {
} }
private: private:
const IReporterConfig& m_config; ReporterConfig m_config;
bool m_currentTestSuccess; bool m_currentTestSuccess;
XmlWriter m_xml; XmlWriter m_xml;
}; };

View File

@ -27,7 +27,7 @@ namespace Catch {
addRecorder( recordSections ); addRecorder( recordSections );
} }
MockReporter( const IReporterConfig& ) { MockReporter( const ReporterConfig& ) {
recordAll(); recordAll();
} }

View File

@ -1,5 +1,5 @@
/* /*
* Generated: 2012-07-20 18:46:50.092936 * Generated: 2012-07-20 19:07:25.539622
* ---------------------------------------------------------- * ----------------------------------------------------------
* This file has been merged from multiple headers. Please don't edit it directly * This file has been merged from multiple headers. Please don't edit it directly
* Copyright (c) 2012 Two Blue Cubes Ltd. All rights reserved. * Copyright (c) 2012 Two Blue Cubes Ltd. All rights reserved.
@ -274,11 +274,19 @@ namespace Catch {
namespace Catch namespace Catch
{ {
struct IReporterConfig { struct ReporterConfig
virtual ~IReporterConfig() {} {
virtual std::ostream& stream () const = 0; ReporterConfig( const std::string& _name,
virtual bool includeSuccessfulResults () const = 0; std::ostream& _stream,
virtual std::string getName () const = 0; bool _includeSuccessfulResults = false )
: name( _name ),
stream( _stream ),
includeSuccessfulResults( _includeSuccessfulResults )
{}
std::string name;
std::ostream& stream;
bool includeSuccessfulResults;
}; };
class TestCaseInfo; class TestCaseInfo;
@ -301,7 +309,7 @@ namespace Catch
struct IReporterFactory { struct IReporterFactory {
virtual ~IReporterFactory() {} virtual ~IReporterFactory() {}
virtual IReporter* create( const IReporterConfig& config ) const = 0; virtual IReporter* create( const ReporterConfig& config ) const = 0;
virtual std::string getDescription() const = 0; virtual std::string getDescription() const = 0;
}; };
@ -309,7 +317,7 @@ namespace Catch
typedef std::map<std::string, IReporterFactory*> FactoryMap; typedef std::map<std::string, IReporterFactory*> FactoryMap;
virtual ~IReporterRegistry() {} virtual ~IReporterRegistry() {}
virtual IReporter* create( const std::string& name, const IReporterConfig& config ) const = 0; virtual IReporter* create( const std::string& name, const ReporterConfig& config ) const = 0;
virtual void registerReporter( const std::string& name, IReporterFactory* factory ) = 0; virtual void registerReporter( const std::string& name, IReporterFactory* factory ) = 0;
virtual const FactoryMap& getFactories() const = 0; virtual const FactoryMap& getFactories() const = 0;
}; };
@ -2478,7 +2486,7 @@ namespace Catch {
bool allowThrows; bool allowThrows;
}; };
class Config : public IReporterConfig, public IConfig { class Config : public IConfig {
private: private:
Config( const Config& other ); Config( const Config& other );
Config& operator = ( const Config& other ); Config& operator = ( const Config& other );
@ -3316,7 +3324,7 @@ namespace Catch {
deleteAllValues( m_factories ); deleteAllValues( m_factories );
} }
virtual IReporter* create( const std::string& name, const IReporterConfig& config ) const { virtual IReporter* create( const std::string& name, const ReporterConfig& config ) const {
FactoryMap::const_iterator it = m_factories.find( name ); FactoryMap::const_iterator it = m_factories.find( name );
if( it == m_factories.end() ) if( it == m_factories.end() )
return NULL; return NULL;
@ -3707,7 +3715,7 @@ namespace Catch {
class ReporterFactory : public IReporterFactory { class ReporterFactory : public IReporterFactory {
virtual IReporter* create( const IReporterConfig& config ) const { virtual IReporter* create( const ReporterConfig& config ) const {
return new T( config ); return new T( config );
} }
@ -3769,7 +3777,7 @@ namespace Catch {
}; };
public: public:
BasicReporter( const IReporterConfig& config ) BasicReporter( const ReporterConfig& config )
: m_config( config ), : m_config( config ),
m_firstSectionInTestCase( true ), m_firstSectionInTestCase( true ),
m_aborted( false ) m_aborted( false )
@ -3783,27 +3791,27 @@ namespace Catch {
void ReportCounts( const std::string& label, const Counts& counts, const std::string& allPrefix = "All " ) { void ReportCounts( const std::string& label, const Counts& counts, const std::string& allPrefix = "All " ) {
if( counts.passed ) if( counts.passed )
m_config.stream() << counts.failed << " of " << counts.total() << " " << label << "s failed"; m_config.stream << counts.failed << " of " << counts.total() << " " << label << "s failed";
else else
m_config.stream() << ( counts.failed > 1 ? allPrefix : "" ) << pluralise( counts.failed, label ) << " failed"; m_config.stream << ( counts.failed > 1 ? allPrefix : "" ) << pluralise( counts.failed, label ) << " failed";
} }
void ReportCounts( const Totals& totals, const std::string& allPrefix = "All " ) { void ReportCounts( const Totals& totals, const std::string& allPrefix = "All " ) {
if( totals.assertions.total() == 0 ) { if( totals.assertions.total() == 0 ) {
m_config.stream() << "No tests ran"; m_config.stream << "No tests ran";
} }
else if( totals.assertions.failed ) { else if( totals.assertions.failed ) {
TextColour colour( TextColour::ResultError ); TextColour colour( TextColour::ResultError );
ReportCounts( "test case", totals.testCases, allPrefix ); ReportCounts( "test case", totals.testCases, allPrefix );
if( totals.testCases.failed > 0 ) { if( totals.testCases.failed > 0 ) {
m_config.stream() << " ("; m_config.stream << " (";
ReportCounts( "assertion", totals.assertions, allPrefix ); ReportCounts( "assertion", totals.assertions, allPrefix );
m_config.stream() << ")"; m_config.stream << ")";
} }
} }
else { else {
TextColour colour( TextColour::ResultSuccess ); TextColour colour( TextColour::ResultSuccess );
m_config.stream() << allPrefix << "tests passed (" m_config.stream << allPrefix << "tests passed ("
<< pluralise( totals.assertions.passed, "assertion" ) << " in " << pluralise( totals.assertions.passed, "assertion" ) << " in "
<< pluralise( totals.testCases.passed, "test case" ) << ")"; << pluralise( totals.testCases.passed, "test case" ) << ")";
} }
@ -3826,14 +3834,14 @@ namespace Catch {
virtual void EndTesting( const Totals& totals ) { virtual void EndTesting( const Totals& totals ) {
// Output the overall test results even if "Started Testing" was not emitted // Output the overall test results even if "Started Testing" was not emitted
if( m_aborted ) { if( m_aborted ) {
m_config.stream() << "\n[Testing aborted. "; m_config.stream << "\n[Testing aborted. ";
ReportCounts( totals, "The first " ); ReportCounts( totals, "The first " );
} }
else { else {
m_config.stream() << "\n[Testing completed. "; m_config.stream << "\n[Testing completed. ";
ReportCounts( totals ); ReportCounts( totals );
} }
m_config.stream() << "]\n" << std::endl; m_config.stream << "]\n" << std::endl;
} }
virtual void StartGroup( const std::string& groupName ) { virtual void StartGroup( const std::string& groupName ) {
@ -3842,9 +3850,9 @@ namespace Catch {
virtual void EndGroup( const std::string& groupName, const Totals& totals ) { virtual void EndGroup( const std::string& groupName, const Totals& totals ) {
if( m_groupSpan.emitted && !groupName.empty() ) { if( m_groupSpan.emitted && !groupName.empty() ) {
m_config.stream() << "[End of group: '" << groupName << "'. "; m_config.stream << "[End of group: '" << groupName << "'. ";
ReportCounts( totals ); ReportCounts( totals );
m_config.stream() << "]\n" << std::endl; m_config.stream << "]\n" << std::endl;
m_groupSpan = SpanInfo(); m_groupSpan = SpanInfo();
} }
} }
@ -3860,7 +3868,7 @@ namespace Catch {
virtual void EndSection( const std::string& sectionName, const Counts& assertions ) { virtual void EndSection( const std::string& sectionName, const Counts& assertions ) {
SpanInfo& sectionSpan = m_sectionSpans.back(); SpanInfo& sectionSpan = m_sectionSpans.back();
if( sectionSpan.emitted && !sectionSpan.name.empty() ) { if( sectionSpan.emitted && !sectionSpan.name.empty() ) {
m_config.stream() << "[End of section: '" << sectionName << "' "; m_config.stream << "[End of section: '" << sectionName << "' ";
if( assertions.failed ) { if( assertions.failed ) {
TextColour colour( TextColour::ResultError ); TextColour colour( TextColour::ResultError );
@ -3868,35 +3876,35 @@ namespace Catch {
} }
else { else {
TextColour colour( TextColour::ResultSuccess ); TextColour colour( TextColour::ResultSuccess );
m_config.stream() << ( assertions.passed > 1 ? "All " : "" ) m_config.stream << ( assertions.passed > 1 ? "All " : "" )
<< pluralise( assertions.passed, "assertion" ) << " passed" ; << pluralise( assertions.passed, "assertion" ) << " passed" ;
} }
m_config.stream() << "]\n" << std::endl; m_config.stream << "]\n" << std::endl;
} }
m_sectionSpans.pop_back(); m_sectionSpans.pop_back();
} }
virtual void Result( const ResultInfo& resultInfo ) { virtual void Result( const ResultInfo& resultInfo ) {
if( !m_config.includeSuccessfulResults() && resultInfo.getResultType() == ResultWas::Ok ) if( !m_config.includeSuccessfulResults && resultInfo.getResultType() == ResultWas::Ok )
return; return;
StartSpansLazily(); StartSpansLazily();
if( !resultInfo.getFilename().empty() ) { if( !resultInfo.getFilename().empty() ) {
TextColour colour( TextColour::FileName ); TextColour colour( TextColour::FileName );
m_config.stream() << SourceLineInfo( resultInfo.getFilename(), resultInfo.getLine() ); m_config.stream << SourceLineInfo( resultInfo.getFilename(), resultInfo.getLine() );
} }
if( resultInfo.hasExpression() ) { if( resultInfo.hasExpression() ) {
TextColour colour( TextColour::OriginalExpression ); TextColour colour( TextColour::OriginalExpression );
m_config.stream() << resultInfo.getExpression(); m_config.stream << resultInfo.getExpression();
if( resultInfo.ok() ) { if( resultInfo.ok() ) {
TextColour successColour( TextColour::Success ); TextColour successColour( TextColour::Success );
m_config.stream() << " succeeded"; m_config.stream << " succeeded";
} }
else { else {
TextColour errorColour( TextColour::Error ); TextColour errorColour( TextColour::Error );
m_config.stream() << " failed"; m_config.stream << " failed";
} }
} }
switch( resultInfo.getResultType() ) { switch( resultInfo.getResultType() ) {
@ -3904,31 +3912,31 @@ namespace Catch {
{ {
TextColour colour( TextColour::Error ); TextColour colour( TextColour::Error );
if( resultInfo.hasExpression() ) if( resultInfo.hasExpression() )
m_config.stream() << " with unexpected"; m_config.stream << " with unexpected";
else else
m_config.stream() << "Unexpected"; m_config.stream << "Unexpected";
m_config.stream() << " exception with message: '" << resultInfo.getMessage() << "'"; m_config.stream << " exception with message: '" << resultInfo.getMessage() << "'";
} }
break; break;
case ResultWas::DidntThrowException: case ResultWas::DidntThrowException:
{ {
TextColour colour( TextColour::Error ); TextColour colour( TextColour::Error );
if( resultInfo.hasExpression() ) if( resultInfo.hasExpression() )
m_config.stream() << " because no exception was thrown where one was expected"; m_config.stream << " because no exception was thrown where one was expected";
else else
m_config.stream() << "No exception thrown where one was expected"; m_config.stream << "No exception thrown where one was expected";
} }
break; break;
case ResultWas::Info: case ResultWas::Info:
streamVariableLengthText( "info", resultInfo.getMessage() ); streamVariableLengthText( "info", resultInfo.getMessage() );
break; break;
case ResultWas::Warning: case ResultWas::Warning:
m_config.stream() << "warning:\n'" << resultInfo.getMessage() << "'"; m_config.stream << "warning:\n'" << resultInfo.getMessage() << "'";
break; break;
case ResultWas::ExplicitFailure: case ResultWas::ExplicitFailure:
{ {
TextColour colour( TextColour::Error ); TextColour colour( TextColour::Error );
m_config.stream() << "failed with message: '" << resultInfo.getMessage() << "'"; m_config.stream << "failed with message: '" << resultInfo.getMessage() << "'";
} }
break; break;
case ResultWas::Unknown: // These cases are here to prevent compiler warnings case ResultWas::Unknown: // These cases are here to prevent compiler warnings
@ -3940,22 +3948,22 @@ namespace Catch {
if( !resultInfo.hasExpression() ) { if( !resultInfo.hasExpression() ) {
if( resultInfo.ok() ) { if( resultInfo.ok() ) {
TextColour colour( TextColour::Success ); TextColour colour( TextColour::Success );
m_config.stream() << " succeeded"; m_config.stream << " succeeded";
} }
else { else {
TextColour colour( TextColour::Error ); TextColour colour( TextColour::Error );
m_config.stream() << " failed"; m_config.stream << " failed";
} }
} }
break; break;
} }
if( resultInfo.hasExpandedExpression() ) { if( resultInfo.hasExpandedExpression() ) {
m_config.stream() << " for: "; m_config.stream << " for: ";
TextColour colour( TextColour::ReconstructedExpression ); TextColour colour( TextColour::ReconstructedExpression );
m_config.stream() << resultInfo.getExpandedExpression(); m_config.stream << resultInfo.getExpandedExpression();
} }
m_config.stream() << std::endl; m_config.stream << std::endl;
} }
virtual void EndTestCase( const TestCaseInfo& testInfo, virtual void EndTestCase( const TestCaseInfo& testInfo,
@ -3973,9 +3981,9 @@ namespace Catch {
} }
if( m_testSpan.emitted ) { if( m_testSpan.emitted ) {
m_config.stream() << "[Finished: '" << testInfo.getName() << "' "; m_config.stream << "[Finished: '" << testInfo.getName() << "' ";
ReportCounts( totals ); ReportCounts( totals );
m_config.stream() << "]" << std::endl; m_config.stream << "]" << std::endl;
} }
} }
@ -3983,20 +3991,20 @@ namespace Catch {
void StartSpansLazily() { void StartSpansLazily() {
if( !m_testingSpan.emitted ) { if( !m_testingSpan.emitted ) {
if( m_config.getName().empty() ) if( m_config.name.empty() )
m_config.stream() << "[Started testing]" << std::endl; m_config.stream << "[Started testing]" << std::endl;
else else
m_config.stream() << "[Started testing: " << m_config.getName() << "]" << std::endl; m_config.stream << "[Started testing: " << m_config.name << "]" << std::endl;
m_testingSpan.emitted = true; m_testingSpan.emitted = true;
} }
if( !m_groupSpan.emitted && !m_groupSpan.name.empty() ) { if( !m_groupSpan.emitted && !m_groupSpan.name.empty() ) {
m_config.stream() << "[Started group: '" << m_groupSpan.name << "']" << std::endl; m_config.stream << "[Started group: '" << m_groupSpan.name << "']" << std::endl;
m_groupSpan.emitted = true; m_groupSpan.emitted = true;
} }
if( !m_testSpan.emitted ) { if( !m_testSpan.emitted ) {
m_config.stream() << std::endl << "[Running: " << m_testSpan.name << "]" << std::endl; m_config.stream << std::endl << "[Running: " << m_testSpan.name << "]" << std::endl;
m_testSpan.emitted = true; m_testSpan.emitted = true;
} }
@ -4004,7 +4012,7 @@ namespace Catch {
SpanInfo& sectionSpan = m_sectionSpans.back(); SpanInfo& sectionSpan = m_sectionSpans.back();
if( !sectionSpan.emitted && !sectionSpan.name.empty() ) { if( !sectionSpan.emitted && !sectionSpan.name.empty() ) {
if( m_firstSectionInTestCase ) { if( m_firstSectionInTestCase ) {
m_config.stream() << "\n"; m_config.stream << "\n";
m_firstSectionInTestCase = false; m_firstSectionInTestCase = false;
} }
std::vector<SpanInfo>::iterator it = m_sectionSpans.begin(); std::vector<SpanInfo>::iterator it = m_sectionSpans.begin();
@ -4012,7 +4020,7 @@ namespace Catch {
for(; it != itEnd; ++it ) { for(; it != itEnd; ++it ) {
SpanInfo& prevSpan = *it; SpanInfo& prevSpan = *it;
if( !prevSpan.emitted && !prevSpan.name.empty() ) { if( !prevSpan.emitted && !prevSpan.name.empty() ) {
m_config.stream() << "[Started section: '" << prevSpan.name << "']" << std::endl; m_config.stream << "[Started section: '" << prevSpan.name << "']" << std::endl;
prevSpan.emitted = true; prevSpan.emitted = true;
} }
} }
@ -4023,16 +4031,16 @@ namespace Catch {
void streamVariableLengthText( const std::string& prefix, const std::string& text ) { void streamVariableLengthText( const std::string& prefix, const std::string& text ) {
std::string trimmed = trim( text ); std::string trimmed = trim( text );
if( trimmed.find_first_of( "\r\n" ) == std::string::npos ) { if( trimmed.find_first_of( "\r\n" ) == std::string::npos ) {
m_config.stream() << "[" << prefix << ": " << trimmed << "]\n"; m_config.stream << "[" << prefix << ": " << trimmed << "]\n";
} }
else { else {
m_config.stream() << "\n[" << prefix << "] >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n" << trimmed m_config.stream << "\n[" << prefix << "] >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n" << trimmed
<< "\n[end of " << prefix << "] <<<<<<<<<<<<<<<<<<<<<<<<\n"; << "\n[end of " << prefix << "] <<<<<<<<<<<<<<<<<<<<<<<<\n";
} }
} }
private: private:
const IReporterConfig& m_config; ReporterConfig m_config;
bool m_firstSectionInTestCase; bool m_firstSectionInTestCase;
SpanInfo m_testingSpan; SpanInfo m_testingSpan;
@ -4250,7 +4258,7 @@ namespace Catch {
namespace Catch { namespace Catch {
class XmlReporter : public SharedImpl<IReporter> { class XmlReporter : public SharedImpl<IReporter> {
public: public:
XmlReporter( const IReporterConfig& config ) : m_config( config ) {} XmlReporter( const ReporterConfig& config ) : m_config( config ) {}
static std::string getDescription() { static std::string getDescription() {
return "Reports test results as an XML document"; return "Reports test results as an XML document";
@ -4263,10 +4271,10 @@ namespace Catch {
} }
virtual void StartTesting() { virtual void StartTesting() {
m_xml = XmlWriter( m_config.stream() ); m_xml = XmlWriter( m_config.stream );
m_xml.startElement( "Catch" ); m_xml.startElement( "Catch" );
if( !m_config.getName().empty() ) if( !m_config.name.empty() )
m_xml.writeAttribute( "name", m_config.getName() ); m_xml.writeAttribute( "name", m_config.name );
} }
virtual void EndTesting( const Totals& totals ) { virtual void EndTesting( const Totals& totals ) {
@ -4307,7 +4315,7 @@ namespace Catch {
} }
virtual void Result( const Catch::ResultInfo& resultInfo ) { virtual void Result( const Catch::ResultInfo& resultInfo ) {
if( !m_config.includeSuccessfulResults() && resultInfo.getResultType() == ResultWas::Ok ) if( !m_config.includeSuccessfulResults && resultInfo.getResultType() == ResultWas::Ok )
return; return;
if( resultInfo.hasExpression() ) { if( resultInfo.hasExpression() ) {
@ -4367,7 +4375,7 @@ namespace Catch {
} }
private: private:
const IReporterConfig& m_config; ReporterConfig m_config;
bool m_currentTestSuccess; bool m_currentTestSuccess;
XmlWriter m_xml; XmlWriter m_xml;
}; };
@ -4420,7 +4428,7 @@ namespace Catch {
}; };
public: public:
JunitReporter( const IReporterConfig& config ) JunitReporter( const ReporterConfig& config )
: m_config( config ), : m_config( config ),
m_testSuiteStats( "AllTests" ), m_testSuiteStats( "AllTests" ),
m_currentStats( &m_testSuiteStats ) m_currentStats( &m_testSuiteStats )
@ -4457,7 +4465,7 @@ namespace Catch {
} }
virtual void Result( const Catch::ResultInfo& resultInfo ) { virtual void Result( const Catch::ResultInfo& resultInfo ) {
if( resultInfo.getResultType() != ResultWas::Ok || m_config.includeSuccessfulResults() ) { if( resultInfo.getResultType() != ResultWas::Ok || m_config.includeSuccessfulResults ) {
TestCaseStats& testCaseStats = m_currentStats->m_testCaseStats.back(); TestCaseStats& testCaseStats = m_currentStats->m_testCaseStats.back();
TestStats stats; TestStats stats;
std::ostringstream oss; std::ostringstream oss;
@ -4514,7 +4522,7 @@ namespace Catch {
} }
virtual void EndTesting( const Totals& ) { virtual void EndTesting( const Totals& ) {
std::ostream& str = m_config.stream(); std::ostream& str = m_config.stream;
{ {
XmlWriter xml( str ); XmlWriter xml( str );
@ -4574,7 +4582,7 @@ namespace Catch {
} }
private: private:
const IReporterConfig& m_config; ReporterConfig m_config;
bool m_currentTestSuccess; bool m_currentTestSuccess;
Stats m_testSuiteStats; Stats m_testSuiteStats;
@ -4601,7 +4609,10 @@ namespace Catch {
std::string reporterName = config.data().reporter.empty() std::string reporterName = config.data().reporter.empty()
? "basic" ? "basic"
: config.data().reporter; : config.data().reporter;
Ptr<IReporter> reporter = getCurrentContext().getReporterRegistry().create( reporterName, config );
ReporterConfig reporterConfig( config.getName(), config.stream(), config.includeSuccessfulResults() );
Ptr<IReporter> reporter = getCurrentContext().getReporterRegistry().create( reporterName, reporterConfig );
if( !config.data().stream.empty() ) { if( !config.data().stream.empty() ) {
if( config.data().stream[0] == '%' ) if( config.data().stream[0] == '%' )