Moved all Catch::Main()s into new Session class

- renamed them run()
- moved cleanUp call into destructor
This commit is contained in:
Phil Nash 2013-06-05 18:48:18 +01:00
parent c57ebc84b2
commit c1196b6245
2 changed files with 71 additions and 62 deletions

View File

@ -108,30 +108,6 @@ namespace Catch {
std::set<TestCase> m_testsAlreadyRun; std::set<TestCase> m_testsAlreadyRun;
}; };
inline int Main( Ptr<Config> const& config ) {
int result = 0;
try
{
Runner runner( config );
// Handle list request
if( list( config ) ) {
Catch::cleanUp();
return 0;
}
result = static_cast<int>( runner.runTests().assertions.failed );
}
catch( std::exception& ex ) {
std::cerr << ex.what() << std::endl;
result = (std::numeric_limits<int>::max)();
}
Catch::cleanUp();
return result;
}
inline void showHelp( std::string const& processName ) { inline void showHelp( std::string const& processName ) {
Clara::CommandLine<ConfigData> cli = makeCommandLineParser(); Clara::CommandLine<ConfigData> cli = makeCommandLineParser();
@ -145,47 +121,80 @@ namespace Catch {
cli.usage( std::cout, processName ); cli.usage( std::cout, processName );
std::cout << "\nFor more detail usage please see: https://github.com/philsquared/Catch/wiki/Command-line\n" << std::endl; std::cout << "\nFor more detail usage please see: https://github.com/philsquared/Catch/wiki/Command-line\n" << std::endl;
} }
inline Ptr<Config> processConfig( int argc, char* const argv[], ConfigData& configData ) {
Clara::CommandLine<ConfigData> cli = makeCommandLineParser(); class Session {
std::vector<Clara::Parser::Token> unused = cli.parseInto( argc, argv, configData ); static bool alreadyInstantiated;
if( !unused.empty() ) {
std::vector<Clara::Parser::Token>::const_iterator public:
it = unused.begin(), Session() {
itEnd = unused.end(); if( alreadyInstantiated ) {
std::string msg; std::string msg = "Only one instance of Catch::Session can ever be used";
for(; it != itEnd; ++it ) std::cerr << msg << std::endl;
msg += " unrecognised option: " + it->data + "\n"; throw std::logic_error( msg );
throw std::runtime_error( msg.substr( 0, msg.size()-1 ) ); }
alreadyInstantiated = true;
}
~Session() {
Catch::cleanUp();
} }
Ptr<Config> config = new Config( configData );
return config;
}
inline Ptr<Config> processConfig( int argc, char* const argv[] ) {
ConfigData configData;
return processConfig( argc, argv, configData );
}
inline int Main( int argc, char* const argv[], ConfigData configData = ConfigData() ) { int run( Ptr<Config> const& config ) {
try
{
Runner runner( config );
Ptr<Config> config; // Handle list request
if( list( config ) )
return 0;
try { return static_cast<int>( runner.runTests().assertions.failed );
config = processConfig( argc, argv, configData ); }
if( config->showHelp() ) { catch( std::exception& ex ) {
showHelp( config->getProcessName() ); std::cerr << ex.what() << std::endl;
Catch::cleanUp(); return (std::numeric_limits<int>::max)();
return 0;
} }
} }
catch( std::exception& ex ) {
std::cerr << "\nError in input:\n" Ptr<Config> processConfig( int argc, char* const argv[], ConfigData& configData ) {
<< " " << ex.what() << "\n\n"; Clara::CommandLine<ConfigData> cli = makeCommandLineParser();
makeCommandLineParser().usage( std::cout, configData.processName ); std::vector<Clara::Parser::Token> unused = cli.parseInto( argc, argv, configData );
Catch::cleanUp(); if( !unused.empty() ) {
return (std::numeric_limits<int>::max)(); std::vector<Clara::Parser::Token>::const_iterator
it = unused.begin(),
itEnd = unused.end();
std::string msg;
for(; it != itEnd; ++it )
msg += " unrecognised option: " + it->data + "\n";
throw std::runtime_error( msg.substr( 0, msg.size()-1 ) );
}
Ptr<Config> config = new Config( configData );
return config;
} }
return Main( config ); Ptr<Config> processConfig( int argc, char* const argv[] ) {
} ConfigData configData;
return processConfig( argc, argv, configData );
}
int run( int argc, char* const argv[], ConfigData configData = ConfigData() ) {
Ptr<Config> config;
try {
config = processConfig( argc, argv, configData );
if( config->showHelp() ) {
showHelp( config->getProcessName() );
return 0;
}
}
catch( std::exception& ex ) {
std::cerr << "\nError in input:\n"
<< " " << ex.what() << "\n\n";
makeCommandLineParser().usage( std::cout, configData.processName );
return (std::numeric_limits<int>::max)();
}
return run( config );
}
};
bool Session::alreadyInstantiated = false;
} // end namespace Catch } // end namespace Catch

View File

@ -12,7 +12,7 @@
// Standard C/C++ main entry point // Standard C/C++ main entry point
int main (int argc, char * const argv[]) { int main (int argc, char * const argv[]) {
return Catch::Main( argc, argv ); return Catch::Session().run( argc, argv );
} }
#else // __OBJC__ #else // __OBJC__
@ -24,7 +24,7 @@ int main (int argc, char * const argv[]) {
#endif #endif
Catch::registerTestMethods(); Catch::registerTestMethods();
int result = Catch::Main( argc, (char* const*)argv ); int result = Catch::Session().run( argc, (char* const*)argv );
#if !CATCH_ARC_ENABLED #if !CATCH_ARC_ENABLED
[pool drain]; [pool drain];