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,7 +121,41 @@ 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 ) {
class Session {
static bool alreadyInstantiated;
public:
Session() {
if( alreadyInstantiated ) {
std::string msg = "Only one instance of Catch::Session can ever be used";
std::cerr << msg << std::endl;
throw std::logic_error( msg );
}
alreadyInstantiated = true;
}
~Session() {
Catch::cleanUp();
}
int run( Ptr<Config> const& config ) {
try
{
Runner runner( config );
// Handle list request
if( list( config ) )
return 0;
return static_cast<int>( runner.runTests().assertions.failed );
}
catch( std::exception& ex ) {
std::cerr << ex.what() << std::endl;
return (std::numeric_limits<int>::max)();
}
}
Ptr<Config> processConfig( int argc, char* const argv[], ConfigData& configData ) {
Clara::CommandLine<ConfigData> cli = makeCommandLineParser(); Clara::CommandLine<ConfigData> cli = makeCommandLineParser();
std::vector<Clara::Parser::Token> unused = cli.parseInto( argc, argv, configData ); std::vector<Clara::Parser::Token> unused = cli.parseInto( argc, argv, configData );
if( !unused.empty() ) { if( !unused.empty() ) {
@ -160,12 +170,11 @@ namespace Catch {
Ptr<Config> config = new Config( configData ); Ptr<Config> config = new Config( configData );
return config; return config;
} }
inline Ptr<Config> processConfig( int argc, char* const argv[] ) { Ptr<Config> processConfig( int argc, char* const argv[] ) {
ConfigData configData; ConfigData configData;
return processConfig( argc, argv, configData ); return processConfig( argc, argv, configData );
} }
int run( int argc, char* const argv[], ConfigData configData = ConfigData() ) {
inline int Main( int argc, char* const argv[], ConfigData configData = ConfigData() ) {
Ptr<Config> config; Ptr<Config> config;
@ -173,7 +182,6 @@ namespace Catch {
config = processConfig( argc, argv, configData ); config = processConfig( argc, argv, configData );
if( config->showHelp() ) { if( config->showHelp() ) {
showHelp( config->getProcessName() ); showHelp( config->getProcessName() );
Catch::cleanUp();
return 0; return 0;
} }
} }
@ -181,11 +189,12 @@ namespace Catch {
std::cerr << "\nError in input:\n" std::cerr << "\nError in input:\n"
<< " " << ex.what() << "\n\n"; << " " << ex.what() << "\n\n";
makeCommandLineParser().usage( std::cout, configData.processName ); makeCommandLineParser().usage( std::cout, configData.processName );
Catch::cleanUp();
return (std::numeric_limits<int>::max)(); return (std::numeric_limits<int>::max)();
} }
return Main( config ); 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];