catch2/include/internal/catch_commandline.hpp

670 lines
28 KiB
C++
Raw Normal View History

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"
2010-11-10 00:24:00 +01:00
2012-05-16 00:58:23 +02:00
namespace Catch {
class Command {
2010-11-10 00:24:00 +01:00
public:
Command(){}
2010-11-10 00:24:00 +01:00
explicit Command( const std::string& name ) : m_name( name ) {
}
Command& operator += ( const std::string& arg ) {
m_args.push_back( arg );
return *this;
}
Command& operator += ( const Command& other ) {
std::copy( other.m_args.begin(), other.m_args.end(), std::back_inserter( m_args ) );
if( m_name.empty() )
m_name = other.m_name;
return *this;
}
Command operator + ( const Command& other ) {
Command newCommand( *this );
newCommand += other;
return newCommand;
2010-11-10 00:24:00 +01:00
}
operator SafeBool::type() const {
return SafeBool::makeSafe( !m_name.empty() || !m_args.empty() );
2010-11-10 00:24:00 +01:00
}
std::string name() const { return m_name; }
std::string operator[]( std::size_t i ) const { return m_args[i]; }
std::size_t argsCount() const { return m_args.size(); }
CATCH_ATTRIBUTE_NORETURN
void raiseError( const std::string& message ) const {
std::ostringstream oss;
if( m_name.empty() )
oss << "Error while parsing " << m_name << ". " << message << ".";
else
oss << "Error while parsing arguments. " << message << ".";
if( m_args.size() > 0 )
oss << " Arguments were:";
for( std::size_t i = 0; i < m_args.size(); ++i )
oss << " " << m_args[i];
throw std::domain_error( oss.str() );
2010-11-10 00:24:00 +01:00
}
private:
std::string m_name;
2010-11-10 00:24:00 +01:00
std::vector<std::string> m_args;
};
class CommandParser {
public:
2012-06-03 00:08:07 +02:00
CommandParser( int argc, char const * const * argv ) : m_argc( static_cast<std::size_t>( argc ) ), m_argv( argv ) {}
std::string exeName() const {
return m_argv[0];
}
Command find( const std::string& arg1, const std::string& arg2, const std::string& arg3 ) const {
return find( arg1 ) + find( arg2 ) + find( arg3 );
}
Command find( const std::string& shortArg, const std::string& longArg ) const {
return find( shortArg ) + find( longArg );
}
Command find( const std::string& arg ) const {
if( arg.empty() )
return getArgs( "", 1 );
else
for( std::size_t i = 1; i < m_argc; ++i )
if( m_argv[i] == arg )
return getArgs( m_argv[i], i+1 );
return Command();
}
Command getDefaultArgs() const {
return getArgs( "", 1 );
}
private:
Command getArgs( const std::string& cmdName, std::size_t from ) const {
Command command( cmdName );
for( std::size_t i = from; i < m_argc && m_argv[i][0] != '-'; ++i )
command += m_argv[i];
return command;
}
2012-06-03 00:08:07 +02:00
std::size_t m_argc;
char const * const * m_argv;
};
2012-08-27 13:19:07 +02:00
class OptionParser : public SharedImpl<IShared> {
public:
OptionParser( int minArgs = 0, int maxArgs = 0 )
: m_minArgs( minArgs ), m_maxArgs( maxArgs )
{}
virtual ~OptionParser() {}
2012-08-27 13:19:07 +02:00
Command find( const CommandParser& parser ) const {
Command cmd;
for( std::vector<std::string>::const_iterator it = m_optionNames.begin();
it != m_optionNames.end();
++it )
cmd += parser.find( *it );
2012-08-27 13:19:07 +02:00
return cmd;
}
void validateArgs( const Command& args ) const {
if( tooFewArgs( args ) || tooManyArgs( args ) ) {
std::ostringstream oss;
if( m_maxArgs == -1 )
oss <<"Expected at least " << pluralise( static_cast<std::size_t>( m_minArgs ), "argument" );
else if( m_minArgs == m_maxArgs )
oss <<"Expected " << pluralise( static_cast<std::size_t>( m_minArgs ), "argument" );
else
oss <<"Expected between " << m_minArgs << " and " << m_maxArgs << " argument";
args.raiseError( oss.str() );
}
}
2012-08-27 13:19:07 +02:00
void parseIntoConfig( const CommandParser& parser, ConfigData& config ) {
if( Command cmd = find( parser ) ) {
validateArgs( cmd );
parseIntoConfig( cmd, config );
}
}
virtual void parseIntoConfig( const Command& cmd, ConfigData& config ) = 0;
virtual std::string argsSynopsis() const = 0;
virtual std::string optionSummary() const = 0;
virtual std::string optionDescription() const { return ""; }
2012-08-27 13:19:07 +02:00
std::string optionNames() const {
std::string names;
for( std::vector<std::string>::const_iterator it = m_optionNames.begin();
it != m_optionNames.end();
++it ) {
if( !it->empty() ) {
if( !names.empty() )
names += ", ";
names += *it;
}
else {
names = "[" + names;
}
}
if( names[0] == '[' )
names += "]";
return names;
}
2012-06-08 09:22:56 +02:00
protected:
bool tooFewArgs( const Command& args ) const {
return args.argsCount() < static_cast<std::size_t>( m_minArgs );
}
bool tooManyArgs( const Command& args ) const {
return m_maxArgs >= 0 && args.argsCount() > static_cast<std::size_t>( m_maxArgs );
}
std::vector<std::string> m_optionNames;
int m_minArgs;
int m_maxArgs;
};
namespace Options {
2012-08-27 13:19:07 +02:00
class HelpOptionParser : public OptionParser {
public:
HelpOptionParser() {
m_optionNames.push_back( "-?" );
m_optionNames.push_back( "-h" );
m_optionNames.push_back( "--help" );
}
virtual std::string argsSynopsis() const {
2012-09-25 08:43:37 +02:00
return "[<option for help on> ...]";
2012-08-27 13:19:07 +02:00
}
virtual std::string optionSummary() const {
2012-09-25 08:43:37 +02:00
return "Shows this usage summary, or help on a specific option, or options, if supplied";
}
virtual std::string optionDescription() const {
return "";
2012-08-27 13:19:07 +02:00
}
virtual void parseIntoConfig( const Command&, ConfigData& ) {
// Does not affect config
}
};
class TestCaseOptionParser : public OptionParser {
public:
TestCaseOptionParser() : OptionParser( 1, -1 ) {
m_optionNames.push_back( "-t" );
m_optionNames.push_back( "--test" );
m_optionNames.push_back( "" ); // default option
}
virtual std::string argsSynopsis() const {
return "<testspec> [<testspec>...]";
}
virtual std::string optionSummary() const {
return "Specifies which test case or cases to run";
}
// Lines are split at the nearest prior space char to the 80 char column.
// Tab chars are removed from the output but their positions are used to align
// subsequently wrapped lines
virtual std::string optionDescription() const {
2012-09-09 12:44:30 +02:00
return
"This option allows one ore more test specs to be supplied. Each spec either fully "
"specifies a test case or is a pattern containing wildcards to match a set of test "
"cases. If this option is not provided then all test cases, except those prefixed "
"by './' are run\n"
"\n"
"Specs must be enclosed in \"quotes\" if they contain spaces. If they do not "
"contain spaces the quotes are optional.\n"
"\n"
"Wildcards consist of the * character at the beginning, end, or both and can substitute for "
"any number of any characters (including none)\n"
"\n"
"If spec is prefixed with exclude: or the ~ character then the pattern matches an exclusion. "
"This means that tests matching the pattern are excluded from the set - even if a prior "
"inclusion spec included them. Subsequent inclusion specs will take precedence, however. "
2012-09-09 12:44:30 +02:00
"Inclusions and exclusions are evaluated in left-to-right order.\n"
"\n"
"Examples:\n"
"\n"
" -t thisTestOnly \tMatches the test case called, 'thisTestOnly'\n"
" -t \"this test only\" \tMatches the test case called, 'this test only'\n"
" -t these/* \tMatches all cases starting with 'these/'\n"
" -t exclude:notThis \tMatches all tests except, 'notThis'\n"
" -t ~notThis \tMatches all tests except, 'notThis'\n"
" -t ~*private* \tMatches all tests except those that contain 'private'\n"
" -t a/* ~a/b/* a/b/c \tMatches all tests that start with 'a/', except those "
"that start with 'a/b/', except 'a/b/c', which is included";
}
virtual void parseIntoConfig( const Command& cmd, ConfigData& config ) {
std::string groupName;
for( std::size_t i = 0; i < cmd.argsCount(); ++i ) {
if( i != 0 )
groupName += " ";
groupName += cmd[i];
}
TestCaseFilters filters( groupName );
for( std::size_t i = 0; i < cmd.argsCount(); ++i )
filters.addFilter( TestCaseFilter( cmd[i] ) );
config.filters.push_back( filters );
}
};
class TagOptionParser : public OptionParser {
public:
TagOptionParser() : OptionParser( 1, -1 ) {
m_optionNames.push_back( "-g" );
m_optionNames.push_back( "--tag" );
}
virtual std::string argsSynopsis() const {
return "<tagspec> [,<tagspec>...]";
}
virtual std::string optionSummary() const {
return "Matches test cases against tags or tag patterns";
}
// Lines are split at the nearest prior space char to the 80 char column.
// Tab chars are removed from the output but their positions are used to align
// subsequently wrapped lines
virtual std::string optionDescription() const {
return
Squashed commit of the following: commit 70c5ef9eede41e7061ff1af45b5babe0d7cde4ba Author: Phil Nash <github@philnash.me> Date: Tue Nov 13 21:46:01 2012 +0000 Regen single include commit 4ea535e5059a664cc393404b454f29e6dad0886e Author: Phil Nash <github@philnash.me> Date: Tue Nov 13 09:56:30 2012 +0000 Tidied up result enums commit 7717c29072013fd45e36d1bc48527927fa94cbed Author: Phil Nash <github@philnash.me> Date: Tue Nov 13 09:45:29 2012 +0000 Implemented CHECK_NOFAIL Previous commit missed some files commit d539da903032e5a6f2bf40d6d7d8e7874c860088 Author: Phil Nash <github@philnash.me> Date: Tue Nov 13 09:44:52 2012 +0000 Implemented CHECK_NO_FAIL commit af1a321860a8b63fb57fcade75ba7f33cad7400c Author: Phil Nash <github@philnash.me> Date: Sat Nov 10 18:46:39 2012 +0000 Regen single include commit f54ac5625e4702e1c032f7b5a7f4d9346c64f502 Author: Phil Nash <github@philnash.me> Date: Sat Nov 10 18:44:12 2012 +0000 New (combined) baselines commit defca58566653fd088f615c2979d7e7a681bf107 Author: Phil Nash <github@philnash.me> Date: Sat Nov 10 18:43:23 2012 +0000 negate() -> endExpression(), takes ResultDisposition commit b2ef99882590ccd3824ebca44cb9b4cfc42f4728 Author: Phil Nash <github@philnash.me> Date: Sat Nov 10 10:35:09 2012 +0000 Changed shouldNegate boolean to use part of ResultDisposition enum commit 1af13dba97474c2a0b33f1ca1919c80ea4f95ca3 Author: Phil Nash <github@philnash.me> Date: Sat Nov 10 10:20:08 2012 +0000 Changed StopOnFailure boolean to an enum commit a1dc7e312cffb0a2d241273adcae4a126d49ff15 Author: Phil Nash <github@philnash.me> Date: Tue Nov 6 19:34:35 2012 +0000 Regen single include commit 20e59ce9d133b469798c4ce2a8d681c0d9b18975 Author: Phil Nash <github@philnash.me> Date: Tue Nov 6 19:34:10 2012 +0000 Added tags docs commit b5b1b1e430c8c61dbd3162da0255a368ba9ad75f Author: Phil Nash <github@philnash.me> Date: Tue Nov 6 19:13:25 2012 +0000 Some expression/ evaluation clean-up commit ec5956f47114ff8b597be85fd7654d29da93aea0 Author: Phil Nash <github@philnash.me> Date: Tue Nov 6 07:52:28 2012 +0000 Fix for #134 The INTERNAL_CATCH_MSG macro, which is used by INFO, WARN and FAIL places its lines in a do…while block so it can be used after an if statement with no block commit 88b70828f24bbbeae636d663b14fd0117165108a Author: Phil Nash <github@philnash.me> Date: Sun Nov 4 21:39:38 2012 +0000 Regen single include commit b323fc7e6c57caa535d65cae31e6018822ed9e3c Author: Phil Nash <github@philnash.me> Date: Sun Nov 4 21:39:16 2012 +0000 Fixed line/no regression for SECTIONs Unexpected exceptions within a section now get the SECTION's line no. again (instead of TEST_CASE line/no) commit 78fba28c4b9f48ec25c9d234a29c36e4427ca6e0 Author: Phil Nash <github@philnash.me> Date: Sun Nov 4 21:11:59 2012 +0000 Added className to TestCaseInfo className is passed through from class based test methods and held in the TestCaseInfo. For free-function based test cases it is set to "global". The JUnit reporter uses the className value to populate he class attribute. commit 81cb69ef1831ed29bf770254c9112b8f0465e912 Author: Phil Nash <github@philnash.me> Date: Sun Nov 4 21:09:22 2012 +0000 AssertionInfo captures more info (for test cases and sections) commit a4e088c999439e9ec0fe3a4205b7520fbf258a5f Author: Phil Nash <github@philnash.me> Date: Fri Nov 2 08:29:03 2012 +0000 Removed __FUNCTION__ from SourceLineInfo commit 2a1e8bfc6ed3bcc1e3746d717726a9e525de6369 Author: Phil Nash <github@philnash.me> Date: Thu Nov 1 08:16:15 2012 +0000 Updated colour comments commit f0f407fc3ebdcbdad50fc1f9a45f15ab3cc54c63 Author: Phil Nash <github@philnash.me> Date: Wed Oct 31 18:28:21 2012 +0000 Manually applied merge #133 from Master commit 355b95fda1de28ba830541768aad32c7b1790290 Author: Phil Nash <github@philnash.me> Date: Wed Oct 31 18:04:22 2012 +0000 Cleaned up ANSI colour code impl a bit commit 778f9c4fc7fa66566679d9f1d8a721079d8589bf Author: Phil Nash <github@philnash.me> Date: Tue Oct 30 09:09:30 2012 +0000 Removed "no-" from Wno-global-constructors when disabling commit 5efa4bcb8a620efa13e8eb5e68d0899681919ca1 Author: Phil Nash <github@philnash.me> Date: Mon Oct 29 20:49:22 2012 +0000 Regenerated single_include commit 108f1937d8cbe04038ab8d2aaf9f6e958fe8168c Author: Phil Nash <github@philnash.me> Date: Mon Oct 29 20:46:45 2012 +0000 Added terminal colour codes for POSIX With thanks to Adam Strzelecki commit 8f4cc541d55cf55b2a67b5d6b77b5c4ceb4d99ee Author: Phil Nash <github@philnash.me> Date: Mon Oct 29 19:55:34 2012 +0000 Added regression test baselines commit 2e203a183474672e386f4cf840847cf29ccf90b9 Author: Phil Nash <github@philnash.me> Date: Mon Oct 29 19:55:13 2012 +0000 Fixed remaining reporting regressions commit 134e45b3ade922e5cbdb2df6b81830fbd93fe448 Author: Phil Nash <github@philnash.me> Date: Sun Oct 28 20:57:21 2012 +0000 Fixed #132 commit 2f92db9898978ca1ee27c964d8a51fa507391935 Author: Phil Nash <github@philnash.me> Date: Sun Oct 28 12:15:34 2012 +0000 Updated the readme specifically for the Integration branch commit 82acc2ca05459ce353de71469f141ba93e9b63cf Author: Phil Nash <github@philnash.me> Date: Sun Oct 28 12:07:17 2012 +0000 Regenerated single include commit fe1d7c1d08f06c0a83b727e9b2e00cc49e74175d Author: Phil Nash <github@philnash.me> Date: Sun Oct 28 10:27:44 2012 +0000 Small fixes and tweaks commit 355b5e546d2f64dd0abbc0134dd3579ce6c28cea Author: Phil Nash <github@philnash.me> Date: Fri Oct 26 09:05:36 2012 +0100 Some tidy-up commit f847186ebbf0e7eeabd99925574c8988add600fb Author: Phil Nash <github@philnash.me> Date: Fri Oct 26 08:45:23 2012 +0100 AssertionResultBuilder -> ExpressionResultBuilder commit 8cca2f13699aa6aa5cd4a60e293f49a1bae54629 Author: Phil Nash <github@philnash.me> Date: Wed Oct 24 22:09:01 2012 +0100 ExpressionBuilder ->ExpressionDecomposer Expression -> ExpressionLhs commit e04e74f896c714de1f03a9cc006bcd071318ea11 Author: Phil Nash <github@philnash.me> Date: Wed Oct 24 21:59:47 2012 +0100 More AssertionResult refactoring commit 1dd56d4d2b29520cdd62f5f449a28e67f954f5db Author: Phil Nash <github@philnash.me> Date: Fri Oct 19 08:01:34 2012 +0100 AssertionResultBuilder can be constructed from result type commit f2d5f1b3e444055ff02ed0536d5d0610b08ecb9f Author: Phil Nash <github@philnash.me> Date: Fri Oct 19 08:01:05 2012 +0100 Expression has its own result builder - not passed in from expression builder commit e3b111a39a4edd3bfe4d88290c99c723b8b74417 Author: Phil Nash <github@philnash.me> Date: Thu Oct 18 22:59:16 2012 +0100 streamlined acceptResult commit 3ad13256e1e50a6ee4a2697e7b6d0f669ae82ab5 Author: Phil Nash <github@philnash.me> Date: Thu Oct 18 08:39:44 2012 +0100 Refactored assertion builder stuff out of expression builder commit c96f9330a095f527673de4d8db4f434d38a7e813 Author: Phil Nash <github@philnash.me> Date: Wed Oct 17 08:14:22 2012 +0100 Collect assertion info up front commit a5fa78284d6d95770cd647a2ec10df09d378ba46 Author: Phil Nash <github@philnash.me> Date: Tue Oct 16 08:33:13 2012 +0100 ResultData -> AssertionResultData commit c597a893fa95462b8d2d5029547d4054eda1839f Author: Phil Nash <github@philnash.me> Date: Tue Oct 16 08:31:05 2012 +0100 ResultInfo -> AssertionResult filenames and variables commit d16955f63a4a128221da41602215b5592df39eb9 Author: Phil Nash <github@philnash.me> Date: Tue Oct 16 08:27:21 2012 +0100 Renamed ResultInfo -> AssertionResult commit 175da3ef6454b6fa0465d22c96d21d58dd9d2f90 Author: Phil Nash <github@philnash.me> Date: Fri Oct 12 18:39:22 2012 +0100 regen test 3
2012-11-13 22:59:50 +01:00
"This option allows one or more tags or tag patterns to be specified.\n"
"Each tag is enclosed in square brackets. A series of tags form an AND expression "
"wheras a comma seperated sequence forms an OR expression. e.g.:\n\n"
" -g [one][two],[three]\n\n"
"This matches all tests tagged [one] and [two], as well as all tests tagged [three].\n\n"
"Tags can be negated with the ~ character. This removes matching tests from the set. e.g.:\n\n"
" -g [one]~[two]\n\n"
"matches all tests tagged [one], except those also tagged [two]";
}
virtual void parseIntoConfig( const Command& cmd, ConfigData& config ) {
std::string groupName;
for( std::size_t i = 0; i < cmd.argsCount(); ++i ) {
if( i != 0 )
groupName += " ";
groupName += cmd[i];
}
TestCaseFilters filters( groupName );
for( std::size_t i = 0; i < cmd.argsCount(); ++i )
filters.addTags( cmd[i] );
config.filters.push_back( filters );
}
};
class ListOptionParser : public OptionParser {
public:
ListOptionParser() : OptionParser( 0, 2 ) {
m_optionNames.push_back( "-l" );
m_optionNames.push_back( "--list" );
}
virtual std::string argsSynopsis() const {
2012-08-27 13:19:07 +02:00
return "[all | tests | reporters [xml]]";
}
virtual std::string optionSummary() const {
return "Lists available tests or reporters";
}
virtual std::string optionDescription() const {
2012-09-09 12:44:30 +02:00
return
"With no arguments this option will list all registered tests - one per line.\n"
"Supplying the xml argument formats the list as an xml document (which may be useful for "
"consumption by other tools).\n"
"Supplying the tests or reporters lists tests or reporters respectively - with descriptions.\n"
"\n"
"Examples:\n"
"\n"
" -l\n"
" -l tests\n"
" -l reporters xml\n"
" -l xml";
}
virtual void parseIntoConfig( const Command& cmd, ConfigData& config ) {
config.listSpec = List::TestNames;
if( cmd.argsCount() >= 1 ) {
if( cmd[0] == "all" )
config.listSpec = List::All;
else if( cmd[0] == "tests" )
config.listSpec = List::Tests;
else if( cmd[0] == "reporters" )
config.listSpec = List::Reports;
else
cmd.raiseError( "Expected [tests] or [reporters]" );
}
if( cmd.argsCount() >= 2 ) {
if( cmd[1] == "xml" )
config.listSpec = static_cast<List::What>( config.listSpec | List::AsXml );
else if( cmd[1] == "text" )
config.listSpec = static_cast<List::What>( config.listSpec | List::AsText );
else
cmd.raiseError( "Expected [xml] or [text]" );
}
}
};
class ReporterOptionParser : public OptionParser {
public:
ReporterOptionParser() : OptionParser( 1, 1 ) {
m_optionNames.push_back( "-r" );
m_optionNames.push_back( "--reporter" );
}
virtual std::string argsSynopsis() const {
2012-08-27 13:19:07 +02:00
return "<reporter name>";
}
virtual std::string optionSummary() const {
return "Specifies type of reporter";
}
virtual std::string optionDescription() const {
2012-09-09 12:44:30 +02:00
return
"A reporter is an object that formats and structures the output of running "
"tests, and potentially summarises the results. By default a basic reporter "
"is used that writes IDE friendly results. CATCH comes bundled with some "
"alternative reporters, but more can be added in client code.\n"
"\n"
"The bundled reporters are:\n"
" -r basic\n"
" -r xml\n"
" -r junit\n"
"\n"
"The JUnit reporter is an xml format that follows the structure of the JUnit "
"XML Report ANT task, as consumed by a number of third-party tools, "
"including Continuous Integration servers such as Jenkins.\n"
"If not otherwise needed, the standard XML reporter is preferred as this is "
"a streaming reporter, whereas the Junit reporter needs to hold all its "
"results until the end so it can write the overall results into attributes "
"of the root node.";
}
virtual void parseIntoConfig( const Command& cmd, ConfigData& config ) {
config.reporter = cmd[0];
}
};
class OutputOptionParser : public OptionParser {
public:
OutputOptionParser() : OptionParser( 1, 1 ) {
m_optionNames.push_back( "-o" );
m_optionNames.push_back( "--out" );
}
virtual std::string argsSynopsis() const {
2012-08-27 13:19:07 +02:00
return "<file name>|<%stream name>";
}
virtual std::string optionSummary() const {
return "Sends output to a file or stream";
}
2012-09-09 12:44:30 +02:00
virtual std::string optionDescription() const {
return
"Use this option to send all output to a file or a stream. By default output is "
"sent to stdout (note that uses of stdout and stderr from within test cases are "
2012-09-09 12:44:30 +02:00
"redirected and included in the report - so even stderr will effectively end up "
"on stdout). If the name begins with % it is interpreted as a stream. "
2012-09-09 12:44:30 +02:00
"Otherwise it is treated as a filename.\n"
"\n"
"Examples are:\n"
"\n"
" -o filename.txt\n"
" -o \"long filename.txt\"\n"
" -o %stdout\n"
" -o %stderr\n"
" -o %debug \t(The IDE's debug output window - currently only Windows' "
"OutputDebugString is supported).";
}
virtual void parseIntoConfig( const Command& cmd, ConfigData& config ) {
if( cmd[0][0] == '%' )
config.stream = cmd[0].substr( 1 );
2012-08-23 21:08:50 +02:00
else
config.outputFilename = cmd[0];
}
};
2012-09-09 12:44:30 +02:00
class SuccessOptionParser : public OptionParser {
public:
2012-09-09 12:44:30 +02:00
SuccessOptionParser() {
m_optionNames.push_back( "-s" );
m_optionNames.push_back( "--success" );
}
virtual std::string argsSynopsis() const {
2012-08-27 13:19:07 +02:00
return "";
}
virtual std::string optionSummary() const {
return "Shows results for successful tests";
2012-08-23 21:08:50 +02:00
}
2012-09-09 12:44:30 +02:00
virtual std::string optionDescription() const {
return
"Usually you only want to see reporting for failed tests. Sometimes it's useful "
"to see all the output (especially when you don't trust that that test you just "
"added worked first time!). To see successful, as well as failing, test results "
2012-09-09 12:44:30 +02:00
"just pass this option.";
}
2012-08-27 22:48:15 +02:00
virtual void parseIntoConfig( const Command&, ConfigData& config ) {
config.includeWhichResults = Include::SuccessfulResults;
}
};
2012-06-08 09:22:56 +02:00
class DebugBreakOptionParser : public OptionParser {
public:
DebugBreakOptionParser() {
m_optionNames.push_back( "-b" );
m_optionNames.push_back( "--break" );
}
virtual std::string argsSynopsis() const {
2012-08-27 13:19:07 +02:00
return "";
}
virtual std::string optionSummary() const {
return "Breaks into the debugger on failure";
}
2012-09-09 12:44:30 +02:00
virtual std::string optionDescription() const {
return
"In some IDEs (currently XCode and Visual Studio) it is possible for CATCH to "
"break into the debugger on a test failure. This can be very helpful during "
"debug sessions - especially when there is more than one path through a "
"particular test. In addition to the command line option, ensure you have "
"built your code with the DEBUG preprocessor symbol";
}
2012-08-27 22:48:15 +02:00
virtual void parseIntoConfig( const Command&, ConfigData& config ) {
config.shouldDebugBreak = true;
}
};
2012-06-08 09:22:56 +02:00
class NameOptionParser : public OptionParser {
public:
NameOptionParser() : OptionParser( 1, 1 ) {
m_optionNames.push_back( "-n" );
m_optionNames.push_back( "--name" );
}
virtual std::string argsSynopsis() const {
2012-08-27 13:19:07 +02:00
return "<name>";
}
virtual std::string optionSummary() const {
return "Names a test run";
}
2012-09-09 12:44:30 +02:00
virtual std::string optionDescription() const {
return
"If a name is supplied it will be used by the reporter to provide an overall "
"name for the test run. This can be useful if you are sending to a file, for "
"example, and need to distinguish different test runs - either from different "
"Catch executables or runs of the same executable with different options.\n"
"\n"
"Examples:\n"
"\n"
" -n testRun\n"
" -n \"tests of the widget component\"";
}
virtual void parseIntoConfig( const Command& cmd, ConfigData& config ) {
config.name = cmd[0];
}
};
class AbortOptionParser : public OptionParser {
public:
AbortOptionParser() : OptionParser( 0, 1 ) {
m_optionNames.push_back( "-a" );
m_optionNames.push_back( "--abort" );
}
virtual std::string argsSynopsis() const {
2012-08-27 13:19:07 +02:00
return "[#]";
}
virtual std::string optionSummary() const {
return "Aborts after a certain number of failures";
}
2012-09-09 12:44:30 +02:00
virtual std::string optionDescription() const {
return
"If a REQUIRE assertion fails the test case aborts, but subsequent test cases "
"are still run. If a CHECK assertion fails even the current test case is not "
"aborted.\n"
"\n"
"Sometimes this results in a flood of failure messages and you'd rather just "
"see the first few. Specifying -a or --abort on its own will abort the whole "
"test run on the first failed assertion of any kind. Following it with a "
"number causes it to abort after that number of assertion failures.";
}
virtual void parseIntoConfig( const Command& cmd, ConfigData& config ) {
int threshold = 1;
if( cmd.argsCount() == 1 ) {
std::stringstream ss;
ss << cmd[0];
ss >> threshold;
if( ss.fail() || threshold <= 0 )
cmd.raiseError( "threshold must be a number greater than zero" );
}
config.cutoff = threshold;
}
};
class NoThrowOptionParser : public OptionParser {
public:
NoThrowOptionParser() {
m_optionNames.push_back( "-nt" );
m_optionNames.push_back( "--nothrow" );
}
virtual std::string argsSynopsis() const {
2012-08-27 13:19:07 +02:00
return "";
}
virtual std::string optionSummary() const {
return "Elides assertions expected to throw";
}
2012-09-09 12:44:30 +02:00
virtual std::string optionDescription() const {
return
"Skips all assertions that test that an exception is thrown, "
"e.g. REQUIRE_THROWS.\n"
"\n"
"These can be a nuisance in certain debugging environments that may break when "
"exceptions are thrown (while this is usually optional for handled exceptions, "
"it can be useful to have enabled if you are trying to track down something "
"unexpected).\n"
"\n"
"When running with this option the throw checking assertions are skipped so "
"as not to contribute additional noise.";
}
2012-08-27 22:48:15 +02:00
virtual void parseIntoConfig( const Command&, ConfigData& config ) {
config.allowThrows = false;
}
};
class WarningsOptionParser : public OptionParser {
public:
WarningsOptionParser() : OptionParser( 1, -1 ) {
m_optionNames.push_back( "-w" );
m_optionNames.push_back( "--warnings" );
}
virtual std::string argsSynopsis() const {
return "<warning>";
}
virtual std::string optionSummary() const {
return "Enable warnings";
}
2012-09-09 12:44:30 +02:00
virtual std::string optionDescription() const {
return
"Enables the named warnings. If the warnings are violated the test case is "
"failed.\n"
"\n"
"At present only one warning has been provided: NoAssertions. If this warning "
"is enabled then any test case that completes without an assertions (CHECK, "
"REQUIRE etc) being encountered violates the warning.\n"
"\n"
"e.g.:\n"
"\n"
" -w NoAssertions";
}
virtual void parseIntoConfig( const Command& cmd, ConfigData& config ) {
for( std::size_t i = 0; i < cmd.argsCount(); ++i ) {
if( cmd[i] == "NoAssertions" )
config.warnings = (ConfigData::WarnAbout::What)( config.warnings | ConfigData::WarnAbout::NoAssertions );
else
cmd.raiseError( "Unrecognised warning: " + cmd[i] );
}
}
};
}
class AllOptions
{
public:
typedef std::vector<Ptr<OptionParser> > Parsers;
typedef Parsers::const_iterator const_iterator;
typedef Parsers::const_iterator iterator;
AllOptions() {
add<Options::TestCaseOptionParser>(); // Keep this one first
add<Options::TagOptionParser>();
add<Options::ListOptionParser>();
add<Options::ReporterOptionParser>();
2012-08-27 13:19:07 +02:00
add<Options::OutputOptionParser>();
2012-09-09 12:44:30 +02:00
add<Options::SuccessOptionParser>();
2012-08-27 13:19:07 +02:00
add<Options::DebugBreakOptionParser>();
add<Options::NameOptionParser>();
add<Options::AbortOptionParser>();
add<Options::NoThrowOptionParser>();
add<Options::WarningsOptionParser>();
add<Options::HelpOptionParser>(); // Keep this one last
2012-06-08 09:22:56 +02:00
}
void parseIntoConfig( const CommandParser& parser, ConfigData& config ) {
for( const_iterator it = m_parsers.begin(); it != m_parsers.end(); ++it )
(*it)->parseIntoConfig( parser, config );
2012-06-08 09:22:56 +02:00
}
const_iterator begin() const {
return m_parsers.begin();
2012-06-08 09:22:56 +02:00
}
const_iterator end() const {
return m_parsers.end();
}
private:
template<typename T>
void add() {
2012-08-27 13:19:07 +02:00
m_parsers.push_back( new T() );
}
Parsers m_parsers;
2012-06-08 09:22:56 +02:00
};
2012-06-08 09:22:56 +02:00
2010-11-10 00:24:00 +01:00
} // end namespace Catch
#endif // TWOBLUECUBES_CATCH_COMMANDLINE_HPP_INCLUDED