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;
};
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 ) {
Clara::CommandLine<ConfigData> cli = makeCommandLineParser();
@ -145,47 +121,80 @@ namespace Catch {
cli.usage( std::cout, processName );
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();
std::vector<Clara::Parser::Token> unused = cli.parseInto( argc, argv, configData );
if( !unused.empty() ) {
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 ) );
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();
}
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 {
config = processConfig( argc, argv, configData );
if( config->showHelp() ) {
showHelp( config->getProcessName() );
Catch::cleanUp();
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)();
}
}
catch( std::exception& ex ) {
std::cerr << "\nError in input:\n"
<< " " << ex.what() << "\n\n";
makeCommandLineParser().usage( std::cout, configData.processName );
Catch::cleanUp();
return (std::numeric_limits<int>::max)();
Ptr<Config> processConfig( int argc, char* const argv[], ConfigData& configData ) {
Clara::CommandLine<ConfigData> cli = makeCommandLineParser();
std::vector<Clara::Parser::Token> unused = cli.parseInto( argc, argv, configData );
if( !unused.empty() ) {
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

View File

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