2010-11-10 00:24:00 +01:00
|
|
|
/*
|
|
|
|
* Created by Phil on 02/11/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_COMMANDLINE_HPP_INCLUDED
|
|
|
|
#define TWOBLUECUBES_CATCH_COMMANDLINE_HPP_INCLUDED
|
|
|
|
|
2011-01-01 01:29:58 +01:00
|
|
|
#include "catch_config.hpp"
|
2012-08-23 21:08:50 +02:00
|
|
|
#include "catch_common.h"
|
2014-02-10 18:20:30 +01:00
|
|
|
#include "catch_clara.h"
|
2010-11-10 00:24:00 +01:00
|
|
|
|
2013-11-26 21:57:34 +01:00
|
|
|
#include <fstream>
|
|
|
|
|
2012-05-16 00:58:23 +02:00
|
|
|
namespace Catch {
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2013-05-31 19:48:31 +02:00
|
|
|
inline void abortAfterFirst( ConfigData& config ) { config.abortAfter = 1; }
|
2013-06-04 09:37:28 +02:00
|
|
|
inline void abortAfterX( ConfigData& config, int x ) {
|
|
|
|
if( x < 1 )
|
|
|
|
throw std::runtime_error( "Value after -x or --abortAfter must be greater than zero" );
|
|
|
|
config.abortAfter = x;
|
|
|
|
}
|
2013-05-31 19:48:31 +02:00
|
|
|
inline void addTestOrTags( ConfigData& config, std::string const& _testSpec ) { config.testsOrTags.push_back( _testSpec ); }
|
2015-08-05 20:02:17 +02:00
|
|
|
inline void addReporterName( ConfigData& config, std::string const& _reporterName ) { config.reporterNames.push_back( _reporterName ); }
|
2013-05-31 19:48:31 +02:00
|
|
|
|
|
|
|
inline void addWarning( ConfigData& config, std::string const& _warning ) {
|
|
|
|
if( _warning == "NoAssertions" )
|
2014-07-09 08:35:34 +02:00
|
|
|
config.warnings = static_cast<WarnAbout::What>( config.warnings | WarnAbout::NoAssertions );
|
2013-05-31 19:48:31 +02:00
|
|
|
else
|
|
|
|
throw std::runtime_error( "Unrecognised warning: '" + _warning + "'" );
|
2014-09-15 19:39:31 +02:00
|
|
|
}
|
|
|
|
inline void setOrder( ConfigData& config, std::string const& order ) {
|
|
|
|
if( startsWith( "declared", order ) )
|
|
|
|
config.runOrder = RunTests::InDeclarationOrder;
|
|
|
|
else if( startsWith( "lexical", order ) )
|
|
|
|
config.runOrder = RunTests::InLexicographicalOrder;
|
|
|
|
else if( startsWith( "random", order ) )
|
|
|
|
config.runOrder = RunTests::InRandomOrder;
|
|
|
|
else
|
|
|
|
throw std::runtime_error( "Unrecognised ordering: '" + order + "'" );
|
|
|
|
}
|
|
|
|
inline void setRngSeed( ConfigData& config, std::string const& seed ) {
|
|
|
|
if( seed == "time" ) {
|
|
|
|
config.rngSeed = static_cast<unsigned int>( std::time(0) );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << seed;
|
|
|
|
ss >> config.rngSeed;
|
|
|
|
if( ss.fail() )
|
|
|
|
throw std::runtime_error( "Argment to --rng-seed should be the word 'time' or a number" );
|
|
|
|
}
|
2013-05-31 19:48:31 +02:00
|
|
|
}
|
|
|
|
inline void setVerbosity( ConfigData& config, int level ) {
|
|
|
|
// !TBD: accept strings?
|
2014-07-09 08:35:34 +02:00
|
|
|
config.verbosity = static_cast<Verbosity::Level>( level );
|
2013-05-31 19:48:31 +02:00
|
|
|
}
|
2013-08-07 19:56:35 +02:00
|
|
|
inline void setShowDurations( ConfigData& config, bool _showDurations ) {
|
|
|
|
config.showDurations = _showDurations
|
|
|
|
? ShowDurations::Always
|
|
|
|
: ShowDurations::Never;
|
|
|
|
}
|
2016-02-24 19:51:01 +01:00
|
|
|
inline void setUseColour( ConfigData& config, std::string const& value ) {
|
|
|
|
std::string mode = toLower( value );
|
|
|
|
|
|
|
|
if( mode == "yes" )
|
|
|
|
config.useColour = UseColour::Yes;
|
|
|
|
else if( mode == "no" )
|
|
|
|
config.useColour = UseColour::No;
|
|
|
|
else if( mode == "auto" )
|
|
|
|
config.useColour = UseColour::Auto;
|
|
|
|
else
|
|
|
|
throw std::runtime_error( "colour mode must be one of: auto, yes or no" );
|
|
|
|
}
|
|
|
|
inline void forceColour( ConfigData& config ) {
|
|
|
|
config.useColour = UseColour::Yes;
|
|
|
|
}
|
2013-11-26 21:57:34 +01:00
|
|
|
inline void loadTestNamesFromFile( ConfigData& config, std::string const& _filename ) {
|
|
|
|
std::ifstream f( _filename.c_str() );
|
|
|
|
if( !f.is_open() )
|
|
|
|
throw std::domain_error( "Unable to load input file: " + _filename );
|
|
|
|
|
|
|
|
std::string line;
|
|
|
|
while( std::getline( f, line ) ) {
|
|
|
|
line = trim(line);
|
2016-09-27 11:27:28 +02:00
|
|
|
if( !line.empty() && !startsWith( line, "#" ) ) {
|
|
|
|
if( !startsWith( line, "\"" ) )
|
|
|
|
line = "\"" + line + "\"";
|
|
|
|
addTestOrTags( config, line + "," );
|
|
|
|
}
|
2013-11-26 21:57:34 +01:00
|
|
|
}
|
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2013-06-04 09:37:28 +02:00
|
|
|
inline Clara::CommandLine<ConfigData> makeCommandLineParser() {
|
2013-05-31 19:48:31 +02:00
|
|
|
|
2014-03-06 09:16:06 +01:00
|
|
|
using namespace Clara;
|
|
|
|
CommandLine<ConfigData> cli;
|
2013-05-31 19:48:31 +02:00
|
|
|
|
2013-06-04 09:37:28 +02:00
|
|
|
cli.bindProcessName( &ConfigData::processName );
|
|
|
|
|
2014-03-06 09:16:06 +01:00
|
|
|
cli["-?"]["-h"]["--help"]
|
2013-05-31 19:48:31 +02:00
|
|
|
.describe( "display usage information" )
|
2014-03-17 19:40:58 +01:00
|
|
|
.bind( &ConfigData::showHelp );
|
2013-05-31 19:48:31 +02:00
|
|
|
|
2014-03-06 09:16:06 +01:00
|
|
|
cli["-l"]["--list-tests"]
|
2013-11-26 21:57:34 +01:00
|
|
|
.describe( "list all/matching test cases" )
|
2014-03-17 19:40:58 +01:00
|
|
|
.bind( &ConfigData::listTests );
|
2013-05-31 19:48:31 +02:00
|
|
|
|
2014-03-06 09:16:06 +01:00
|
|
|
cli["-t"]["--list-tags"]
|
2013-11-26 21:57:34 +01:00
|
|
|
.describe( "list all/matching tags" )
|
2014-03-17 19:40:58 +01:00
|
|
|
.bind( &ConfigData::listTags );
|
2013-05-31 19:48:31 +02:00
|
|
|
|
2014-03-06 09:16:06 +01:00
|
|
|
cli["-s"]["--success"]
|
2013-05-31 19:48:31 +02:00
|
|
|
.describe( "include successful tests in output" )
|
2014-03-17 19:40:58 +01:00
|
|
|
.bind( &ConfigData::showSuccessfulTests );
|
2013-05-31 19:48:31 +02:00
|
|
|
|
2014-03-06 09:16:06 +01:00
|
|
|
cli["-b"]["--break"]
|
2013-05-31 19:48:31 +02:00
|
|
|
.describe( "break into debugger on failure" )
|
2014-03-17 19:40:58 +01:00
|
|
|
.bind( &ConfigData::shouldDebugBreak );
|
2013-05-31 19:48:31 +02:00
|
|
|
|
2014-03-06 09:16:06 +01:00
|
|
|
cli["-e"]["--nothrow"]
|
2013-06-07 22:15:25 +02:00
|
|
|
.describe( "skip exception tests" )
|
2014-03-17 19:40:58 +01:00
|
|
|
.bind( &ConfigData::noThrow );
|
2013-05-31 19:48:31 +02:00
|
|
|
|
2014-04-22 19:23:42 +02:00
|
|
|
cli["-i"]["--invisibles"]
|
|
|
|
.describe( "show invisibles (tabs, newlines)" )
|
|
|
|
.bind( &ConfigData::showInvisibles );
|
|
|
|
|
2014-03-06 09:16:06 +01:00
|
|
|
cli["-o"]["--out"]
|
2013-05-31 19:48:31 +02:00
|
|
|
.describe( "output filename" )
|
2014-03-17 19:40:58 +01:00
|
|
|
.bind( &ConfigData::outputFilename, "filename" );
|
2013-05-31 19:48:31 +02:00
|
|
|
|
2014-03-06 09:16:06 +01:00
|
|
|
cli["-r"]["--reporter"]
|
|
|
|
// .placeholder( "name[:filename]" )
|
2013-11-26 21:57:34 +01:00
|
|
|
.describe( "reporter to use (defaults to console)" )
|
2015-08-05 20:02:17 +02:00
|
|
|
.bind( &addReporterName, "name" );
|
2013-05-31 19:48:31 +02:00
|
|
|
|
2014-03-06 09:16:06 +01:00
|
|
|
cli["-n"]["--name"]
|
2013-05-31 19:48:31 +02:00
|
|
|
.describe( "suite name" )
|
2014-03-17 19:40:58 +01:00
|
|
|
.bind( &ConfigData::name, "name" );
|
2013-05-31 19:48:31 +02:00
|
|
|
|
2014-03-06 09:16:06 +01:00
|
|
|
cli["-a"]["--abort"]
|
2013-05-31 19:48:31 +02:00
|
|
|
.describe( "abort at first failure" )
|
2014-03-17 19:40:58 +01:00
|
|
|
.bind( &abortAfterFirst );
|
2013-05-31 19:48:31 +02:00
|
|
|
|
2014-03-06 09:16:06 +01:00
|
|
|
cli["-x"]["--abortx"]
|
2013-05-31 19:48:31 +02:00
|
|
|
.describe( "abort after x failures" )
|
2014-03-17 19:40:58 +01:00
|
|
|
.bind( &abortAfterX, "no. failures" );
|
2013-05-31 19:48:31 +02:00
|
|
|
|
2014-03-06 09:16:06 +01:00
|
|
|
cli["-w"]["--warn"]
|
2013-05-31 19:48:31 +02:00
|
|
|
.describe( "enable warnings" )
|
2014-03-17 19:40:58 +01:00
|
|
|
.bind( &addWarning, "warning name" );
|
2013-05-31 19:48:31 +02:00
|
|
|
|
2014-03-06 09:16:06 +01:00
|
|
|
// - needs updating if reinstated
|
|
|
|
// cli.into( &setVerbosity )
|
2013-06-06 19:56:43 +02:00
|
|
|
// .describe( "level of verbosity (0=no output)" )
|
|
|
|
// .shortOpt( "v")
|
|
|
|
// .longOpt( "verbosity" )
|
2014-03-06 09:16:06 +01:00
|
|
|
// .placeholder( "level" );
|
2013-05-31 19:48:31 +02:00
|
|
|
|
2014-03-06 09:16:06 +01:00
|
|
|
cli[_]
|
2013-05-31 19:48:31 +02:00
|
|
|
.describe( "which test or tests to use" )
|
2014-03-17 19:40:58 +01:00
|
|
|
.bind( &addTestOrTags, "test name, pattern or tags" );
|
2013-05-31 19:48:31 +02:00
|
|
|
|
2014-03-06 09:16:06 +01:00
|
|
|
cli["-d"]["--durations"]
|
2013-08-07 19:56:35 +02:00
|
|
|
.describe( "show test durations" )
|
2016-02-24 19:51:01 +01:00
|
|
|
.bind( &setShowDurations, "yes|no" );
|
2013-08-07 19:56:35 +02:00
|
|
|
|
2014-03-06 09:16:06 +01:00
|
|
|
cli["-f"]["--input-file"]
|
2013-11-26 21:57:34 +01:00
|
|
|
.describe( "load test names to run from a file" )
|
2014-03-17 19:40:58 +01:00
|
|
|
.bind( &loadTestNamesFromFile, "filename" );
|
2013-11-26 21:57:34 +01:00
|
|
|
|
2015-07-06 19:46:50 +02:00
|
|
|
cli["-#"]["--filenames-as-tags"]
|
|
|
|
.describe( "adds a tag for the filename" )
|
|
|
|
.bind( &ConfigData::filenamesAsTags );
|
|
|
|
|
2013-11-26 21:57:34 +01:00
|
|
|
// Less common commands which don't have a short form
|
2014-03-06 09:16:06 +01:00
|
|
|
cli["--list-test-names-only"]
|
2013-11-26 21:57:34 +01:00
|
|
|
.describe( "list all/matching test cases names only" )
|
2014-03-17 19:40:58 +01:00
|
|
|
.bind( &ConfigData::listTestNamesOnly );
|
2013-11-26 21:57:34 +01:00
|
|
|
|
2015-11-04 19:01:28 +01:00
|
|
|
cli["--list-reporters"]
|
2013-11-26 21:57:34 +01:00
|
|
|
.describe( "list all reporters" )
|
2014-03-17 19:40:58 +01:00
|
|
|
.bind( &ConfigData::listReporters );
|
2013-11-26 21:57:34 +01:00
|
|
|
|
2014-09-15 19:39:31 +02:00
|
|
|
cli["--order"]
|
|
|
|
.describe( "test case order (defaults to decl)" )
|
|
|
|
.bind( &setOrder, "decl|lex|rand" );
|
|
|
|
|
|
|
|
cli["--rng-seed"]
|
|
|
|
.describe( "set a specific seed for random numbers" )
|
|
|
|
.bind( &setRngSeed, "'time'|number" );
|
|
|
|
|
2015-02-11 21:40:20 +01:00
|
|
|
cli["--force-colour"]
|
2016-02-24 19:51:01 +01:00
|
|
|
.describe( "force colourised output (deprecated)" )
|
|
|
|
.bind( &forceColour );
|
|
|
|
|
|
|
|
cli["--use-colour"]
|
|
|
|
.describe( "should output be colourised" )
|
|
|
|
.bind( &setUseColour, "yes|no" );
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2013-06-04 09:37:28 +02:00
|
|
|
return cli;
|
2013-05-31 19:48:31 +02:00
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2010-11-10 00:24:00 +01:00
|
|
|
} // end namespace Catch
|
|
|
|
|
2010-12-28 15:42:46 +01:00
|
|
|
#endif // TWOBLUECUBES_CATCH_COMMANDLINE_HPP_INCLUDED
|