2010-11-10 00:24:00 +01:00
|
|
|
/*
|
|
|
|
* Created by Phil on 31/10/2010.
|
|
|
|
* Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
|
|
|
|
*
|
|
|
|
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
*/
|
|
|
|
#ifndef TWOBLUECUBES_CATCH_RUNNER_HPP_INCLUDED
|
|
|
|
#define TWOBLUECUBES_CATCH_RUNNER_HPP_INCLUDED
|
|
|
|
|
2011-04-26 09:32:40 +02:00
|
|
|
#include "reporters/catch_reporter_basic.hpp"
|
|
|
|
#include "reporters/catch_reporter_xml.hpp"
|
|
|
|
#include "reporters/catch_reporter_junit.hpp"
|
2010-11-10 00:24:00 +01:00
|
|
|
|
2012-08-13 08:46:10 +02:00
|
|
|
#include "internal/catch_commandline.hpp"
|
|
|
|
#include "internal/catch_list.hpp"
|
|
|
|
#include "internal/catch_runner_impl.hpp"
|
|
|
|
|
2010-12-31 23:07:47 +01:00
|
|
|
#include <fstream>
|
2011-02-04 10:30:36 +01:00
|
|
|
#include <stdlib.h>
|
2011-03-15 23:41:27 +01:00
|
|
|
#include <limits>
|
2010-12-31 23:07:47 +01:00
|
|
|
|
2012-05-16 16:02:51 +02:00
|
|
|
namespace Catch {
|
|
|
|
|
2012-05-31 20:40:26 +02:00
|
|
|
INTERNAL_CATCH_REGISTER_REPORTER( "basic", BasicReporter )
|
|
|
|
INTERNAL_CATCH_REGISTER_REPORTER( "xml", XmlReporter )
|
|
|
|
INTERNAL_CATCH_REGISTER_REPORTER( "junit", JunitReporter )
|
2012-07-17 09:04:19 +02:00
|
|
|
|
2012-08-13 20:27:03 +02:00
|
|
|
inline int resolveStream( Config& configWrapper ) {
|
|
|
|
const ConfigData& config = configWrapper.data();
|
2012-07-28 21:37:07 +02:00
|
|
|
|
2012-08-13 20:27:03 +02:00
|
|
|
if( !config.stream.empty() ) {
|
|
|
|
if( config.stream[0] == '%' )
|
|
|
|
configWrapper.useStream( config.stream.substr( 1 ) );
|
2012-07-17 09:04:19 +02:00
|
|
|
else
|
2012-08-13 20:27:03 +02:00
|
|
|
configWrapper.setFilename( config.stream );
|
2012-07-17 09:04:19 +02:00
|
|
|
}
|
2010-11-10 00:24:00 +01:00
|
|
|
// Open output file, if specified
|
|
|
|
std::ofstream ofs;
|
2012-08-13 20:27:03 +02:00
|
|
|
if( !config.outputFilename.empty() ) {
|
|
|
|
ofs.open( config.outputFilename.c_str() );
|
2012-05-16 16:02:51 +02:00
|
|
|
if( ofs.fail() ) {
|
2012-08-13 20:27:03 +02:00
|
|
|
std::cerr << "Unable to open file: '" << config.outputFilename << "'" << std::endl;
|
2011-02-16 20:02:09 +01:00
|
|
|
return (std::numeric_limits<int>::max)();
|
2010-11-10 00:24:00 +01:00
|
|
|
}
|
2012-08-13 20:27:03 +02:00
|
|
|
configWrapper.setStreamBuf( ofs.rdbuf() );
|
2010-11-10 00:24:00 +01:00
|
|
|
}
|
2012-08-13 20:27:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Ptr<IReporter> makeReporter( Config& configWrapper ) {
|
|
|
|
const ConfigData& config = configWrapper.data();
|
|
|
|
|
|
|
|
std::string reporterName = config.reporter.empty()
|
|
|
|
? "basic"
|
|
|
|
: config.reporter;
|
|
|
|
|
|
|
|
ReporterConfig reporterConfig( config.name, configWrapper.stream(), config.includeWhichResults == Include::SuccessfulResults );
|
|
|
|
|
|
|
|
Ptr<IReporter> reporter = getRegistryHub().getReporterRegistry().create( reporterName, reporterConfig );
|
|
|
|
if( !reporter )
|
|
|
|
std::cerr << "No reporter registered with name: '" << reporterName << "'" << std::endl;
|
|
|
|
return reporter;
|
|
|
|
}
|
2010-11-10 00:24:00 +01:00
|
|
|
|
2012-08-13 20:27:03 +02:00
|
|
|
inline int Main( Config& configWrapper ) {
|
|
|
|
|
|
|
|
int result = resolveStream( configWrapper );
|
|
|
|
if( result != 0 )
|
|
|
|
return result;
|
|
|
|
|
|
|
|
Ptr<IReporter> reporter = makeReporter( configWrapper );
|
|
|
|
if( !reporter )
|
|
|
|
return (std::numeric_limits<int>::max)();
|
|
|
|
|
|
|
|
const ConfigData& config = configWrapper.data();
|
|
|
|
|
|
|
|
// Handle list request
|
|
|
|
if( config.listSpec != List::None )
|
|
|
|
return List( config );
|
2012-05-25 09:52:05 +02:00
|
|
|
|
|
|
|
// Scope here for the Runner so it can use the context before it is cleaned-up
|
|
|
|
{
|
2012-08-13 20:27:03 +02:00
|
|
|
Runner runner( configWrapper, reporter );
|
2010-11-10 00:24:00 +01:00
|
|
|
|
2012-05-25 09:52:05 +02:00
|
|
|
// Run test specs specified on the command line - or default to all
|
2012-08-13 20:27:03 +02:00
|
|
|
if( config.testSpecs.empty() ) {
|
2012-05-25 09:52:05 +02:00
|
|
|
runner.runAll();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// !TBD We should get all the testcases upfront, report any missing,
|
|
|
|
// then just run them
|
2012-08-13 20:27:03 +02:00
|
|
|
std::vector<std::string>::const_iterator it = config.testSpecs.begin();
|
|
|
|
std::vector<std::string>::const_iterator itEnd = config.testSpecs.end();
|
2012-05-25 09:52:05 +02:00
|
|
|
for(; it != itEnd; ++it ) {
|
|
|
|
if( runner.runMatching( *it ) == 0 ) {
|
2012-07-16 09:58:28 +02:00
|
|
|
std::cerr << "\n[No test cases matched with: " << *it << "]" << std::endl;
|
2012-05-25 09:52:05 +02:00
|
|
|
}
|
2010-11-10 00:24:00 +01:00
|
|
|
}
|
|
|
|
}
|
2012-05-25 09:52:05 +02:00
|
|
|
result = static_cast<int>( runner.getTotals().assertions.failed );
|
2010-11-10 00:24:00 +01:00
|
|
|
}
|
2012-08-06 21:16:53 +02:00
|
|
|
Catch::cleanUp();
|
2012-05-21 19:52:09 +02:00
|
|
|
return result;
|
2010-11-10 00:24:00 +01:00
|
|
|
}
|
2011-03-21 13:36:21 +01:00
|
|
|
|
2012-05-31 20:40:26 +02:00
|
|
|
inline void showUsage( std::ostream& os ) {
|
2012-07-05 19:37:58 +02:00
|
|
|
os << "\t-?, -h, --help\n"
|
|
|
|
<< "\t-l, --list <tests | reporters> [xml]\n"
|
2012-05-31 20:40:26 +02:00
|
|
|
<< "\t-t, --test <testspec> [<testspec>...]\n"
|
|
|
|
<< "\t-r, --reporter <reporter name>\n"
|
|
|
|
<< "\t-o, --out <file name>|<%stream name>\n"
|
|
|
|
<< "\t-s, --success\n"
|
|
|
|
<< "\t-b, --break\n"
|
2012-06-03 00:08:07 +02:00
|
|
|
<< "\t-n, --name <name>\n"
|
2012-07-16 09:58:28 +02:00
|
|
|
<< "\t-a, --abort [#]\n"
|
2012-06-08 09:22:56 +02:00
|
|
|
<< "\t-nt, --nothrow\n\n"
|
2012-05-31 20:40:26 +02:00
|
|
|
<< "For more detail usage please see: https://github.com/philsquared/Catch/wiki/Command-line" << std::endl;
|
|
|
|
}
|
2012-05-16 16:02:51 +02:00
|
|
|
inline void showHelp( std::string exeName ) {
|
2011-03-21 13:36:21 +01:00
|
|
|
std::string::size_type pos = exeName.find_last_of( "/\\" );
|
2012-05-16 16:02:51 +02:00
|
|
|
if( pos != std::string::npos ) {
|
2011-03-21 13:36:21 +01:00
|
|
|
exeName = exeName.substr( pos+1 );
|
|
|
|
}
|
2012-05-31 20:40:26 +02:00
|
|
|
|
|
|
|
std::cout << exeName << " is a CATCH host application. Options are as follows:\n\n";
|
|
|
|
showUsage( std::cout );
|
2011-03-21 13:36:21 +01:00
|
|
|
}
|
2010-11-10 00:24:00 +01:00
|
|
|
|
2012-05-16 16:02:51 +02:00
|
|
|
inline int Main( int argc, char* const argv[], Config& config ) {
|
2012-06-08 09:22:56 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
CommandParser parser( argc, argv );
|
|
|
|
|
|
|
|
if( Command cmd = parser.find( "-h", "-?", "--help" ) ) {
|
|
|
|
if( cmd.argsCount() != 0 )
|
|
|
|
cmd.raiseError( "Does not accept arguments" );
|
|
|
|
|
|
|
|
showHelp( argv[0] );
|
2012-08-06 21:16:53 +02:00
|
|
|
Catch::cleanUp();
|
2012-06-08 09:22:56 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2011-03-21 13:21:25 +01:00
|
|
|
|
2012-07-17 09:04:19 +02:00
|
|
|
parseIntoConfig( parser, config.data() );
|
2012-06-08 09:22:56 +02:00
|
|
|
}
|
|
|
|
catch( std::exception& ex ) {
|
2012-07-23 09:24:52 +02:00
|
|
|
std::cerr << ex.what() << "\n\nUsage: ...\n\n";
|
2012-05-31 20:40:26 +02:00
|
|
|
showUsage( std::cerr );
|
2012-08-06 21:16:53 +02:00
|
|
|
Catch::cleanUp();
|
2011-03-21 13:21:25 +01:00
|
|
|
return (std::numeric_limits<int>::max)();
|
|
|
|
}
|
2012-06-08 09:22:56 +02:00
|
|
|
|
2011-03-21 13:21:25 +01:00
|
|
|
return Main( config );
|
|
|
|
}
|
|
|
|
|
2012-05-16 16:02:51 +02:00
|
|
|
inline int Main( int argc, char* const argv[] ) {
|
2011-03-21 13:21:25 +01:00
|
|
|
Config config;
|
2012-05-21 19:52:09 +02:00
|
|
|
// !TBD: This doesn't always work, for some reason
|
2011-03-29 09:05:47 +02:00
|
|
|
// if( isDebuggerActive() )
|
|
|
|
// config.useStream( "debug" );
|
2012-05-21 19:52:09 +02:00
|
|
|
return Main( argc, argv, config );
|
2011-03-21 13:21:25 +01:00
|
|
|
}
|
|
|
|
|
2010-11-10 00:24:00 +01:00
|
|
|
} // end namespace Catch
|
|
|
|
|
2011-02-16 20:02:09 +01:00
|
|
|
#endif // TWOBLUECUBES_CATCH_RUNNER_HPP_INCLUDED
|