Catch/include/catch_runner.hpp

145 lines
5.1 KiB
C++
Raw Normal View History

2010-11-09 18:24:00 -05: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
#include "internal/catch_context_impl.hpp"
2011-01-07 05:22:24 -05:00
2010-11-09 18:24:00 -05:00
#include "internal/catch_commandline.hpp"
#include "internal/catch_list.hpp"
2011-04-26 03:32:40 -04:00
#include "reporters/catch_reporter_basic.hpp"
#include "reporters/catch_reporter_xml.hpp"
#include "reporters/catch_reporter_junit.hpp"
2010-11-09 18:24:00 -05:00
#include <fstream>
#include <stdlib.h>
2011-03-15 18:41:27 -04:00
#include <limits>
2012-05-16 10:02:51 -04:00
namespace Catch {
INTERNAL_CATCH_REGISTER_REPORTER( "basic", BasicReporter )
INTERNAL_CATCH_REGISTER_REPORTER( "xml", XmlReporter )
INTERNAL_CATCH_REGISTER_REPORTER( "junit", JunitReporter )
2012-05-16 10:02:51 -04:00
inline int Main( Config& config ) {
std::string reporterName = config.data().reporter.empty()
? "basic"
: config.data().reporter;
Ptr<IReporter> reporter = getCurrentContext().getReporterRegistry().create( reporterName, config );
if( !config.data().stream.empty() ) {
if( config.data().stream[0] == '%' )
config.useStream( config.data().stream.substr( 1 ) );
else
config.setFilename( config.data().stream );
}
2010-11-09 18:24:00 -05:00
// Handle list request
if( config.listWhat() != List::None )
2010-11-09 18:24:00 -05:00
return List( config );
// Open output file, if specified
std::ofstream ofs;
2012-05-16 10:02:51 -04:00
if( !config.getFilename().empty() ) {
2010-11-09 18:24:00 -05:00
ofs.open( config.getFilename().c_str() );
2012-05-16 10:02:51 -04:00
if( ofs.fail() ) {
2010-11-09 18:24:00 -05:00
std::cerr << "Unable to open file: '" << config.getFilename() << "'" << std::endl;
return (std::numeric_limits<int>::max)();
2010-11-09 18:24:00 -05:00
}
config.setStreamBuf( ofs.rdbuf() );
2010-11-09 18:24:00 -05:00
}
int result = 0;
// Scope here for the Runner so it can use the context before it is cleaned-up
{
Runner runner( config, reporter );
2010-11-09 18:24:00 -05:00
// Run test specs specified on the command line - or default to all
if( !config.testsSpecified() ) {
runner.runAll();
}
else {
// !TBD We should get all the testcases upfront, report any missing,
// then just run them
std::vector<std::string>::const_iterator it = config.getTestSpecs().begin();
std::vector<std::string>::const_iterator itEnd = config.getTestSpecs().end();
for(; it != itEnd; ++it ) {
if( runner.runMatching( *it ) == 0 ) {
std::cerr << "\n[No test cases matched with: " << *it << "]" << std::endl;
}
2010-11-09 18:24:00 -05:00
}
}
result = static_cast<int>( runner.getTotals().assertions.failed );
2010-11-09 18:24:00 -05:00
}
Catch::Context::cleanUp();
return result;
2010-11-09 18:24:00 -05:00
}
2011-03-21 08:36:21 -04:00
inline void showUsage( std::ostream& os ) {
2012-07-05 13:37:58 -04:00
os << "\t-?, -h, --help\n"
<< "\t-l, --list <tests | reporters> [xml]\n"
<< "\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-02 18:08:07 -04:00
<< "\t-n, --name <name>\n"
<< "\t-a, --abort [#]\n"
2012-06-08 03:22:56 -04:00
<< "\t-nt, --nothrow\n\n"
<< "For more detail usage please see: https://github.com/philsquared/Catch/wiki/Command-line" << std::endl;
}
2012-05-16 10:02:51 -04:00
inline void showHelp( std::string exeName ) {
2011-03-21 08:36:21 -04:00
std::string::size_type pos = exeName.find_last_of( "/\\" );
2012-05-16 10:02:51 -04:00
if( pos != std::string::npos ) {
2011-03-21 08:36:21 -04:00
exeName = exeName.substr( pos+1 );
}
std::cout << exeName << " is a CATCH host application. Options are as follows:\n\n";
showUsage( std::cout );
2011-03-21 08:36:21 -04:00
}
2010-11-09 18:24:00 -05:00
2012-05-16 10:02:51 -04:00
inline int Main( int argc, char* const argv[], Config& config ) {
2012-06-08 03:22:56 -04: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] );
Catch::Context::cleanUp();
return 0;
}
parseIntoConfig( parser, config.data() );
2012-06-08 03:22:56 -04:00
}
catch( std::exception& ex ) {
std::cerr << ex.what() << + "\n\nUsage: ...\n\n";
showUsage( std::cerr );
Catch::Context::cleanUp();
return (std::numeric_limits<int>::max)();
}
2012-06-08 03:22:56 -04:00
return Main( config );
}
2012-05-16 10:02:51 -04:00
inline int Main( int argc, char* const argv[] ) {
Config config;
// !TBD: This doesn't always work, for some reason
2011-03-29 03:05:47 -04:00
// if( isDebuggerActive() )
// config.useStream( "debug" );
return Main( argc, argv, config );
}
2010-11-09 18:24:00 -05:00
} // end namespace Catch
#endif // TWOBLUECUBES_CATCH_RUNNER_HPP_INCLUDED