Refactoring towards interface based config

This commit is contained in:
Phil Nash 2013-05-28 18:39:32 +01:00
parent 10fa0593db
commit e1459955f1
17 changed files with 754 additions and 245 deletions

View File

@ -24,9 +24,8 @@ namespace Catch {
class Runner2 { // This will become Runner when Runner becomes Context
public:
Runner2( Config& configWrapper )
: m_configWrapper( configWrapper ),
m_config( configWrapper.data() )
Runner2( Ptr<Config> const& config )
: m_config( config )
{
openStream();
makeReporter();
@ -34,13 +33,13 @@ namespace Catch {
Totals runTests() {
std::vector<TestCaseFilters> filterGroups = m_config.filters;
std::vector<TestCaseFilters> filterGroups = m_config->data().filters;
if( filterGroups.empty() ) {
TestCaseFilters filterGroup( "" );
filterGroups.push_back( filterGroup );
}
Runner context( m_configWrapper, m_reporter ); // This Runner will be renamed Context
Runner context( m_config, m_reporter ); // This Runner will be renamed Context
Totals totals;
for( std::size_t i=0; i < filterGroups.size() && !context.aborting(); ++i ) {
@ -77,28 +76,26 @@ namespace Catch {
private:
void openStream() {
if( !m_config.stream.empty() )
m_configWrapper.useStream( m_config.stream );
if( !m_config->data().stream.empty() )
m_config->useStream( m_config->data().stream );
// Open output file, if specified
if( !m_config.outputFilename.empty() ) {
m_ofs.open( m_config.outputFilename.c_str() );
if( !m_config->getFilename().empty() ) {
m_ofs.open( m_config->getFilename().c_str() );
if( m_ofs.fail() ) {
std::ostringstream oss;
oss << "Unable to open file: '" << m_config.outputFilename << "'";
oss << "Unable to open file: '" << m_config->getFilename() << "'";
throw std::domain_error( oss.str() );
}
m_configWrapper.setStreamBuf( m_ofs.rdbuf() );
m_config->setStreamBuf( m_ofs.rdbuf() );
}
}
void makeReporter() {
std::string reporterName = m_config.reporter.empty()
std::string reporterName = m_config->data().reporter.empty()
? "console"
: m_config.reporter;
: m_config->data().reporter;
ReporterConfig reporterConfig( m_configWrapper.stream(), m_config );
m_reporter = getRegistryHub().getReporterRegistry().create( reporterName, reporterConfig );
m_reporter = getRegistryHub().getReporterRegistry().create( reporterName, m_config.get() );
if( !m_reporter ) {
std::ostringstream oss;
oss << "No reporter registered with name: '" << reporterName << "'";
@ -107,24 +104,23 @@ namespace Catch {
}
private:
Config& m_configWrapper;
const ConfigData& m_config;
Ptr<Config> m_config;
std::ofstream m_ofs;
Ptr<IStreamingReporter> m_reporter;
std::set<TestCase> m_testsAlreadyRun;
};
inline int Main( Config& configWrapper ) {
inline int Main( Ptr<Config> const& config ) {
int result = 0;
try
{
Runner2 runner( configWrapper );
Runner2 runner( config );
const ConfigData& config = configWrapper.data();
const ConfigData& configData = config->data();
// Handle list request
if( config.listSpec != List::None ) {
list( config );
if( configData.listSpec != List::None ) {
list( configData );
Catch::cleanUp();
return 0;
}
@ -177,7 +173,7 @@ namespace Catch {
}
}
inline int Main( int argc, char* const argv[], Config& config ) {
inline int Main( int argc, char* const argv[], Ptr<Config> const& config ) {
try {
CommandParser parser( argc, argv );
@ -193,7 +189,7 @@ namespace Catch {
AllOptions options;
options.parseIntoConfig( parser, config.data() );
options.parseIntoConfig( parser, config->data() );
}
catch( std::exception& ex ) {
std::cerr << ex.what() << "\n\nUsage: ...\n\n";
@ -206,7 +202,7 @@ namespace Catch {
}
inline int Main( int argc, char* const argv[] ) {
Config config;
Ptr<Config> config = new Config();
// !TBD: This doesn't always work, for some reason
// if( isDebuggerActive() )
// config.useStream( "debug" );

View File

@ -75,7 +75,7 @@ namespace Catch {
};
class Config : public IConfig {
class Config : public SharedImpl<IConfig> {
private:
Config( Config const& other );
Config& operator = ( Config const& other );
@ -124,10 +124,6 @@ namespace Catch {
return m_data.shouldDebugBreak;
}
virtual std::ostream& stream() const {
return m_os;
}
void setStreamBuf( std::streambuf* buf ) {
m_os.rdbuf( buf ? buf : std::cout.rdbuf() );
}
@ -145,18 +141,10 @@ namespace Catch {
m_data.filters.push_back( filters );
}
virtual bool includeSuccessfulResults() const {
return m_data.includeWhichResults == Include::SuccessfulResults;
}
int getCutoff() const {
return m_data.cutoff;
}
virtual bool allowThrows() const {
return m_data.allowThrows;
}
ConfigData const& data() const {
return m_data;
}
@ -164,6 +152,13 @@ namespace Catch {
return m_data;
}
// IConfig interface
virtual bool allowThrows() const { return m_data.allowThrows; }
virtual std::ostream& stream() const { return m_os; }
virtual std::string name() const { return m_data.name; }
virtual bool includeSuccessfulResults() const { return m_data.includeWhichResults == Include::SuccessfulResults; }
virtual bool warnAboutMissingAssertions() const { return m_data.warnings & ConfigData::WarnAbout::NoAssertions; }
private:
ConfigData m_data;

View File

@ -9,6 +9,7 @@
#define TWOBLUECUBES_CATCH_CONTEXT_H_INCLUDED
#include "catch_interfaces_generators.h"
#include "catch_ptr.hpp"
#include <memory>
#include <vector>
@ -31,7 +32,7 @@ namespace Catch {
virtual IRunner& getRunner() = 0;
virtual size_t getGeneratorIndex( std::string const& fileInfo, size_t totalSize ) = 0;
virtual bool advanceGeneratorsForCurrentTest() = 0;
virtual const IConfig* getConfig() const = 0;
virtual Ptr<IConfig> getConfig() const = 0;
};
struct IMutableContext : IContext
@ -39,7 +40,7 @@ namespace Catch {
virtual ~IMutableContext();
virtual void setResultCapture( IResultCapture* resultCapture ) = 0;
virtual void setRunner( IRunner* runner ) = 0;
virtual void setConfig( const IConfig* config ) = 0;
virtual void setConfig( Ptr<IConfig> const& config ) = 0;
};
IContext& getCurrentContext();

View File

@ -38,7 +38,7 @@ namespace Catch {
return generators && generators->moveNext();
}
virtual const IConfig* getConfig() const {
virtual Ptr<IConfig> getConfig() const {
return m_config;
}
@ -49,7 +49,7 @@ namespace Catch {
virtual void setRunner( IRunner* runner ) {
m_runner = runner;
}
virtual void setConfig( const IConfig* config ) {
virtual void setConfig( Ptr<IConfig> const& config ) {
m_config = config;
}
@ -79,7 +79,7 @@ namespace Catch {
private:
IRunner* m_runner;
IResultCapture* m_resultCapture;
const IConfig* m_config;
Ptr<IConfig> m_config;
std::map<std::string, IGeneratorsForTest*> m_generatorsByTestName;
};

View File

@ -8,13 +8,22 @@
#ifndef TWOBLUECUBES_CATCH_INTERFACES_CONFIG_H_INCLUDED
#define TWOBLUECUBES_CATCH_INTERFACES_CONFIG_H_INCLUDED
#include <iostream>
#include <string>
#include "catch_ptr.hpp"
namespace Catch {
struct IConfig {
struct IConfig : IShared {
virtual ~IConfig();
virtual bool allowThrows() const = 0;
virtual std::ostream& stream() const = 0;
virtual std::string name() const = 0;
virtual bool includeSuccessfulResults() const = 0;
virtual bool warnAboutMissingAssertions() const = 0;
};
}

View File

@ -24,17 +24,18 @@
namespace Catch
{
struct ReporterConfig {
ReporterConfig( std::ostream& _stream, ConfigData const& _fullConfig )
explicit ReporterConfig( Ptr<IConfig> const& _fullConfig )
: m_stream( &_fullConfig->stream() ), m_fullConfig( _fullConfig ) {}
ReporterConfig( Ptr<IConfig> const& _fullConfig, std::ostream& _stream )
: m_stream( &_stream ), m_fullConfig( _fullConfig ) {}
std::ostream& stream() const { return *m_stream; }
std::string name() const { return m_fullConfig.name; }
bool includeSuccessfulResults() const { return m_fullConfig.includeWhichResults == Include::SuccessfulResults; }
bool warnAboutMissingAssertions() const { return m_fullConfig.warnings & ConfigData::WarnAbout::NoAssertions; }
Ptr<IConfig> fullConfig() const { return m_fullConfig; }
private:
std::ostream* m_stream;
ConfigData m_fullConfig;
Ptr<IConfig> m_fullConfig;
};
struct ReporterPreferences {
@ -217,7 +218,7 @@ namespace Catch
struct StreamingReporterBase : SharedImpl<IStreamingReporter> {
StreamingReporterBase( ReporterConfig const& _config )
: m_config( _config ),
: m_config( _config.fullConfig() ),
stream( _config.stream() )
{}
@ -264,7 +265,7 @@ namespace Catch
testRunInfo.reset();
}
ReporterConfig m_config;
Ptr<IConfig> m_config;
Option<TestRunInfo> testRunInfo;
Option<GroupInfo> unusedGroupInfo;
Option<TestCaseInfo> unusedTestCaseInfo;
@ -321,7 +322,7 @@ namespace Catch
typedef std::map<std::string, IReporterFactory*> FactoryMap;
virtual ~IReporterRegistry();
virtual IStreamingReporter* create( std::string const& name, ReporterConfig const& config ) const = 0;
virtual IStreamingReporter* create( std::string const& name, Ptr<IConfig> const& config ) const = 0;
virtual FactoryMap const& getFactories() const = 0;
};

View File

@ -15,7 +15,7 @@ namespace Catch
class LegacyReporterAdapter : public SharedImpl<IStreamingReporter>
{
public:
LegacyReporterAdapter( Ptr<IReporter> const& legacyReporter, ReporterConfig const& config );
LegacyReporterAdapter( Ptr<IReporter> const& legacyReporter );
virtual ~LegacyReporterAdapter();
virtual ReporterPreferences getPreferences() const;
@ -33,7 +33,6 @@ namespace Catch
private:
Ptr<IReporter> m_legacyReporter;
ReporterConfig m_config;
};
}

View File

@ -12,9 +12,8 @@
namespace Catch
{
LegacyReporterAdapter::LegacyReporterAdapter( Ptr<IReporter> const& legacyReporter, ReporterConfig const& config )
: m_legacyReporter( legacyReporter ),
m_config( config )
LegacyReporterAdapter::LegacyReporterAdapter( Ptr<IReporter> const& legacyReporter )
: m_legacyReporter( legacyReporter )
{}
LegacyReporterAdapter::~LegacyReporterAdapter() {}

View File

@ -19,7 +19,7 @@ namespace Catch {
class ReporterFactory : public IReporterFactory {
virtual IStreamingReporter* create( ReporterConfig const& config ) const {
return new LegacyReporterAdapter( new T( config ), config );
return new LegacyReporterAdapter( new T( config ) );
}
virtual std::string getDescription() const {

View File

@ -22,11 +22,11 @@ namespace Catch {
deleteAllValues( m_factories );
}
virtual IStreamingReporter* create( std::string const& name, ReporterConfig const& config ) const {
virtual IStreamingReporter* create( std::string const& name, Ptr<IConfig> const& config ) const {
FactoryMap::const_iterator it = m_factories.find( name );
if( it == m_factories.end() )
return NULL;
return it->second->create( config );
return it->second->create( ReporterConfig( config ) );
}
void registerReporter( std::string const& name, IReporterFactory* factory ) {

View File

@ -56,8 +56,8 @@ namespace Catch {
public:
explicit Runner( Config const& config, Ptr<IStreamingReporter> const& reporter )
: m_runInfo( config.data().name ),
explicit Runner( Ptr<Config> const& config, Ptr<IStreamingReporter> const& reporter )
: m_runInfo( config->name() ),
m_context( getCurrentMutableContext() ),
m_runningTest( NULL ),
m_config( config ),
@ -67,7 +67,7 @@ namespace Catch {
m_prevConfig( m_context.getConfig() )
{
m_context.setRunner( this );
m_context.setConfig( &m_config );
m_context.setConfig( m_config.get() );
m_context.setResultCapture( this );
m_reporter->testRunStarting( m_runInfo );
}
@ -127,7 +127,7 @@ namespace Catch {
Totals deltaTotals = m_totals.delta( prevTotals );
bool missingAssertions = false;
if( deltaTotals.assertions.total() == 0 &&
( m_config.data().warnings & ConfigData::WarnAbout::NoAssertions ) ) {
( m_config->data().warnings & ConfigData::WarnAbout::NoAssertions ) ) {
m_totals.assertions.failed++;
deltaTotals = m_totals.delta( prevTotals );
missingAssertions = true;
@ -149,7 +149,7 @@ namespace Catch {
return deltaTotals;
}
Config const& config() const {
Ptr<Config> config() const {
return m_config;
}
@ -209,7 +209,7 @@ namespace Catch {
Counts assertions = m_totals.assertions - prevAssertions;
bool missingAssertions = false;
if( assertions.total() == 0 &&
( m_config.data().warnings & ConfigData::WarnAbout::NoAssertions ) &&
( m_config->data().warnings & ConfigData::WarnAbout::NoAssertions ) &&
!m_runningTest->isBranchSection() ) {
m_totals.assertions.failed++;
assertions.failed++;
@ -231,7 +231,7 @@ namespace Catch {
}
virtual bool shouldDebugBreak() const {
return m_config.shouldDebugBreak();
return m_config->shouldDebugBreak();
}
virtual std::string getCurrentTestName() const {
@ -247,7 +247,7 @@ namespace Catch {
public:
// !TBD We need to do this another way!
bool aborting() const {
return m_totals.assertions.failed == static_cast<std::size_t>( m_config.getCutoff() );
return m_totals.assertions.failed == static_cast<std::size_t>( m_config->getCutoff() );
}
private:
@ -315,13 +315,13 @@ namespace Catch {
RunningTest* m_runningTest;
AssertionResult m_lastResult;
Config const& m_config;
Ptr<Config> m_config;
Totals m_totals;
Ptr<IStreamingReporter> m_reporter;
std::vector<MessageInfo> m_messages;
IRunner* m_prevRunner;
IResultCapture* m_prevResultCapture;
const IConfig* m_prevConfig;
Ptr<IConfig> m_prevConfig;
AssertionInfo m_lastAssertionInfo;
std::vector<UnfinishedSections> m_unfinishedSections;
};

View File

@ -160,7 +160,7 @@ namespace Catch {
}
virtual void Result( const AssertionResult& assertionResult ) {
if( !m_config.includeSuccessfulResults() && assertionResult.getResultType() == ResultWas::Ok )
if( !m_config.fullConfig()->includeSuccessfulResults() && assertionResult.getResultType() == ResultWas::Ok )
return;
startSpansLazily();
@ -289,10 +289,10 @@ namespace Catch {
void startSpansLazily() {
if( !m_testingSpan.emitted ) {
if( m_config.name().empty() )
if( m_config.fullConfig()->name().empty() )
m_config.stream() << "[Started testing]" << std::endl;
else
m_config.stream() << "[Started testing: " << m_config.name() << "]" << std::endl;
m_config.stream() << "[Started testing: " << m_config.fullConfig()->name() << "]" << std::endl;
m_testingSpan.emitted = true;
}

View File

@ -43,7 +43,7 @@ namespace Catch {
AssertionResult const& result = _assertionStats.assertionResult;
// Drop out if result was successful and we're not printing those
if( !m_config.includeSuccessfulResults() && result.isOk() )
if( !m_config->includeSuccessfulResults() && result.isOk() )
return;
lazyPrint();

View File

@ -65,7 +65,7 @@ namespace Catch {
};
public:
JunitReporter( const ReporterConfig& config )
JunitReporter( ReporterConfig const& config )
: m_config( config ),
m_testSuiteStats( "AllTests" ),
m_currentStats( &m_testSuiteStats )
@ -86,7 +86,7 @@ namespace Catch {
virtual void StartGroup( const std::string& groupName ) {
if( groupName.empty() )
m_statsForSuites.push_back( Stats( m_config.name() ) );
m_statsForSuites.push_back( Stats( m_config.fullConfig()->name() ) );
else
m_statsForSuites.push_back( Stats( groupName ) );
m_currentStats = &m_statsForSuites.back();
@ -110,7 +110,7 @@ namespace Catch {
}
virtual void Result( const Catch::AssertionResult& assertionResult ) {
if( assertionResult.getResultType() != ResultWas::Ok || m_config.includeSuccessfulResults() ) {
if( assertionResult.getResultType() != ResultWas::Ok || m_config.fullConfig()->includeSuccessfulResults() ) {
TestCaseStats& testCaseStats = m_currentStats->m_testCaseStats.back();
TestStats stats;
std::ostringstream oss;
@ -238,7 +238,6 @@ namespace Catch {
private:
ReporterConfig m_config;
// bool m_currentTestSuccess;
Stats m_testSuiteStats;
Stats* m_currentStats;

View File

@ -16,7 +16,7 @@
namespace Catch {
class XmlReporter : public SharedImpl<IReporter> {
public:
XmlReporter( const ReporterConfig& config ) : m_config( config ) {}
XmlReporter( ReporterConfig const& config ) : m_config( config ) {}
static std::string getDescription() {
return "Reports test results as an XML document";
@ -32,8 +32,8 @@ namespace Catch {
virtual void StartTesting() {
m_xml = XmlWriter( m_config.stream() );
m_xml.startElement( "Catch" );
if( !m_config.name().empty() )
m_xml.writeAttribute( "name", m_config.name() );
if( !m_config.fullConfig()->name().empty() )
m_xml.writeAttribute( "name", m_config.fullConfig()->name() );
}
virtual void EndTesting( const Totals& totals ) {
@ -76,7 +76,7 @@ namespace Catch {
}
virtual void Result( const Catch::AssertionResult& assertionResult ) {
if( !m_config.includeSuccessfulResults() && assertionResult.getResultType() == ResultWas::Ok )
if( !m_config.fullConfig()->includeSuccessfulResults() && assertionResult.getResultType() == ResultWas::Ok )
return;
if( assertionResult.hasExpression() ) {

File diff suppressed because it is too large Load Diff

View File

@ -15,8 +15,8 @@ namespace Catch{
Totals EmbeddedRunner::runMatching( const std::string& rawTestSpec, std::size_t groupIndex, std::size_t groupsCount, const std::string& ) {
std::ostringstream oss;
Config config;
config.setStreamBuf( oss.rdbuf() );
Ptr<Config> config = new Config();
config->setStreamBuf( oss.rdbuf() );
Totals totals;