re-encapsulated session vars.

- construct Config object lazily
This commit is contained in:
Phil Nash 2013-06-07 18:41:22 +01:00
parent 8fde14313b
commit 80557647d6
2 changed files with 49 additions and 34 deletions

View File

@ -116,7 +116,7 @@ namespace Catch {
struct OnUnusedOptions { enum DoWhat { Ignore, Fail }; }; struct OnUnusedOptions { enum DoWhat { Ignore, Fail }; };
Session() Session()
: cli( makeCommandLineParser() ) { : m_cli( makeCommandLineParser() ) {
if( alreadyInstantiated ) { if( alreadyInstantiated ) {
std::string msg = "Only one instance of Catch::Session can ever be used"; std::string msg = "Only one instance of Catch::Session can ever be used";
std::cerr << msg << std::endl; std::cerr << msg << std::endl;
@ -136,38 +136,38 @@ namespace Catch {
std::cout << " (" << libraryVersion.branchName << " branch)"; std::cout << " (" << libraryVersion.branchName << " branch)";
std::cout << "\n"; std::cout << "\n";
cli.usage( std::cout, processName ); m_cli.usage( std::cout, processName );
std::cout << "For more detail usage please see the project docs\n" << std::endl; std::cout << "For more detail usage please see the project docs\n" << std::endl;
} }
int applyCommandLine( int argc, char* const argv[], OnUnusedOptions::DoWhat unusedOptionBehaviour = OnUnusedOptions::Fail ) { int applyCommandLine( int argc, char* const argv[], OnUnusedOptions::DoWhat unusedOptionBehaviour = OnUnusedOptions::Fail ) {
try { try {
unusedTokens = cli.parseInto( argc, argv, configData ); m_unusedTokens = m_cli.parseInto( argc, argv, m_configData );
if( unusedOptionBehaviour == OnUnusedOptions::Fail ) if( unusedOptionBehaviour == OnUnusedOptions::Fail )
enforceNoUsedTokens(); enforceNoUsedTokens();
if( configData.showHelp ) if( m_configData.showHelp )
showHelp( configData.processName ); showHelp( m_configData.processName );
config.reset(); m_config.reset();
} }
catch( std::exception& ex ) { catch( std::exception& ex ) {
std::cerr << "\nError in input:\n" std::cerr << "\nError in input:\n"
<< " " << ex.what() << "\n\n"; << " " << ex.what() << "\n\n";
cli.usage( std::cout, configData.processName ); m_cli.usage( std::cout, m_configData.processName );
return (std::numeric_limits<int>::max)(); return (std::numeric_limits<int>::max)();
} }
return 0; return 0;
} }
void useConfigData( ConfigData const& _configData ) { void useConfigData( ConfigData const& _configData ) {
configData = _configData; m_configData = _configData;
config.reset(); m_config.reset();
} }
void enforceNoUsedTokens() const { void enforceNoUsedTokens() const {
if( !unusedTokens.empty() ) { if( !m_unusedTokens.empty() ) {
std::vector<Clara::Parser::Token>::const_iterator std::vector<Clara::Parser::Token>::const_iterator
it = unusedTokens.begin(), it = m_unusedTokens.begin(),
itEnd = unusedTokens.end(); itEnd = m_unusedTokens.end();
std::string msg; std::string msg;
for(; it != itEnd; ++it ) for(; it != itEnd; ++it )
msg += " unrecognised option: " + it->data + "\n"; msg += " unrecognised option: " + it->data + "\n";
@ -184,17 +184,16 @@ namespace Catch {
} }
int run() { int run() {
if( configData.showHelp ) if( m_configData.showHelp )
return 0; return 0;
try try
{ {
if( !config ) config(); // Force config to be constructed
config = new Config( configData ); Runner runner( m_config );
Runner runner( config );
// Handle list request // Handle list request
if( Option<std::size_t> listed = list( config ) ) if( Option<std::size_t> listed = list( config() ) )
return static_cast<int>( *listed ); return static_cast<int>( *listed );
return static_cast<int>( runner.runTests().assertions.failed ); return static_cast<int>( runner.runTests().assertions.failed );
@ -205,10 +204,26 @@ namespace Catch {
} }
} }
Clara::CommandLine<ConfigData> cli; Clara::CommandLine<ConfigData> const& cli() const {
std::vector<Clara::Parser::Token> unusedTokens; return m_cli;
ConfigData configData; }
Ptr<Config> config; std::vector<Clara::Parser::Token> const& unusedTokens() const {
return m_unusedTokens;
}
ConfigData& configData() {
return m_configData;
}
Config& config() {
if( !m_config )
m_config = new Config( m_configData );
return *m_config;
}
private:
Clara::CommandLine<ConfigData> m_cli;
std::vector<Clara::Parser::Token> m_unusedTokens;
ConfigData m_configData;
Ptr<Config> m_config;
}; };
bool Session::alreadyInstantiated = false; bool Session::alreadyInstantiated = false;

View File

@ -25,8 +25,8 @@ namespace Catch {
return true; return true;
} }
inline std::size_t listTests( Ptr<Config> const& config ) { inline std::size_t listTests( Config const& config ) {
if( config->filters().empty() ) if( config.filters().empty() )
std::cout << "All available test cases:\n"; std::cout << "All available test cases:\n";
else else
std::cout << "Matching test cases:\n"; std::cout << "Matching test cases:\n";
@ -37,7 +37,7 @@ namespace Catch {
std::size_t maxTagLen = 0; std::size_t maxTagLen = 0;
std::size_t maxNameLen = 0; std::size_t maxNameLen = 0;
for(; it != itEnd; ++it ) { for(; it != itEnd; ++it ) {
if( matchesFilters( config->filters(), *it ) ) { if( matchesFilters( config.filters(), *it ) ) {
maxTagLen = (std::max)( it->getTestCaseInfo().tagsAsString.size(), maxTagLen ); maxTagLen = (std::max)( it->getTestCaseInfo().tagsAsString.size(), maxTagLen );
maxNameLen = (std::max)( it->getTestCaseInfo().name.size(), maxNameLen ); maxNameLen = (std::max)( it->getTestCaseInfo().name.size(), maxNameLen );
} }
@ -54,7 +54,7 @@ namespace Catch {
std::size_t matchedTests = 0; std::size_t matchedTests = 0;
for( it = allTests.begin(); it != itEnd; ++it ) { for( it = allTests.begin(); it != itEnd; ++it ) {
if( matchesFilters( config->filters(), *it ) ) { if( matchesFilters( config.filters(), *it ) ) {
matchedTests++; matchedTests++;
// !TBD: consider listAs() // !TBD: consider listAs()
Text nameWrapper( it->getTestCaseInfo().name, Text nameWrapper( it->getTestCaseInfo().name,
@ -94,15 +94,15 @@ namespace Catch {
} }
} }
} }
if( config->filters().empty() ) if( config.filters().empty() )
std::cout << pluralise( matchedTests, "test case" ) << std::endl; std::cout << pluralise( matchedTests, "test case" ) << std::endl;
else else
std::cout << pluralise( matchedTests, "matching test case" ) << std::endl; std::cout << pluralise( matchedTests, "matching test case" ) << std::endl;
return matchedTests; return matchedTests;
} }
inline std::size_t listTags( Ptr<Config> const& config ) { inline std::size_t listTags( Config const& config ) {
if( config->filters().empty() ) if( config.filters().empty() )
std::cout << "All available tags:\n"; std::cout << "All available tags:\n";
else else
std::cout << "Matching tags:\n"; std::cout << "Matching tags:\n";
@ -114,7 +114,7 @@ namespace Catch {
std::size_t maxTagLen = 0; std::size_t maxTagLen = 0;
for(; it != itEnd; ++it ) { for(; it != itEnd; ++it ) {
if( matchesFilters( config->filters(), *it ) ) { if( matchesFilters( config.filters(), *it ) ) {
for( std::set<std::string>::const_iterator tagIt = it->getTestCaseInfo().tags.begin(), for( std::set<std::string>::const_iterator tagIt = it->getTestCaseInfo().tags.begin(),
tagItEnd = it->getTestCaseInfo().tags.end(); tagItEnd = it->getTestCaseInfo().tags.end();
tagIt != tagItEnd; tagIt != tagItEnd;
@ -154,7 +154,7 @@ namespace Catch {
return tagCounts.size(); return tagCounts.size();
} }
inline std::size_t listReporters( Ptr<Config> const& /*config*/ ) { inline std::size_t listReporters( Config const& /*config*/ ) {
std::cout << "Available reports:\n"; std::cout << "Available reports:\n";
IReporterRegistry::FactoryMap const& factories = getRegistryHub().getReporterRegistry().getFactories(); IReporterRegistry::FactoryMap const& factories = getRegistryHub().getReporterRegistry().getFactories();
IReporterRegistry::FactoryMap::const_iterator it = factories.begin(), itEnd = factories.end(); IReporterRegistry::FactoryMap::const_iterator it = factories.begin(), itEnd = factories.end();
@ -164,13 +164,13 @@ namespace Catch {
return factories.size(); return factories.size();
} }
inline Option<std::size_t> list( Ptr<Config> const& config ) { inline Option<std::size_t> list( Config const& config ) {
Option<std::size_t> listedCount; Option<std::size_t> listedCount;
if( config->listTests() ) if( config.listTests() )
listedCount = listedCount.valueOr(0) + listTests( config ); listedCount = listedCount.valueOr(0) + listTests( config );
if( config->listTags() ) if( config.listTags() )
listedCount = listedCount.valueOr(0) + listTags( config ); listedCount = listedCount.valueOr(0) + listTags( config );
if( config->listReporters() ) if( config.listReporters() )
listedCount = listedCount.valueOr(0) + listReporters( config ); listedCount = listedCount.valueOr(0) + listReporters( config );
return listedCount; return listedCount;
} }