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 }; };
Session()
: cli( makeCommandLineParser() ) {
: m_cli( makeCommandLineParser() ) {
if( alreadyInstantiated ) {
std::string msg = "Only one instance of Catch::Session can ever be used";
std::cerr << msg << std::endl;
@ -136,38 +136,38 @@ namespace Catch {
std::cout << " (" << libraryVersion.branchName << " branch)";
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;
}
int applyCommandLine( int argc, char* const argv[], OnUnusedOptions::DoWhat unusedOptionBehaviour = OnUnusedOptions::Fail ) {
try {
unusedTokens = cli.parseInto( argc, argv, configData );
m_unusedTokens = m_cli.parseInto( argc, argv, m_configData );
if( unusedOptionBehaviour == OnUnusedOptions::Fail )
enforceNoUsedTokens();
if( configData.showHelp )
showHelp( configData.processName );
config.reset();
if( m_configData.showHelp )
showHelp( m_configData.processName );
m_config.reset();
}
catch( std::exception& ex ) {
std::cerr << "\nError in input:\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 0;
}
void useConfigData( ConfigData const& _configData ) {
configData = _configData;
config.reset();
m_configData = _configData;
m_config.reset();
}
void enforceNoUsedTokens() const {
if( !unusedTokens.empty() ) {
if( !m_unusedTokens.empty() ) {
std::vector<Clara::Parser::Token>::const_iterator
it = unusedTokens.begin(),
itEnd = unusedTokens.end();
it = m_unusedTokens.begin(),
itEnd = m_unusedTokens.end();
std::string msg;
for(; it != itEnd; ++it )
msg += " unrecognised option: " + it->data + "\n";
@ -184,17 +184,16 @@ namespace Catch {
}
int run() {
if( configData.showHelp )
if( m_configData.showHelp )
return 0;
try
{
if( !config )
config = new Config( configData );
Runner runner( config );
config(); // Force config to be constructed
Runner runner( m_config );
// 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>( runner.runTests().assertions.failed );
@ -205,10 +204,26 @@ namespace Catch {
}
}
Clara::CommandLine<ConfigData> cli;
std::vector<Clara::Parser::Token> unusedTokens;
ConfigData configData;
Ptr<Config> config;
Clara::CommandLine<ConfigData> const& cli() const {
return m_cli;
}
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;

View File

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