catch2/include/internal/catch_commandline.hpp
Phil Nash b9fea75109 New version of Clara.
- updated command line setup with new API
- updated STITCH macros
- force embedded Clara to use Catch’s console width (but restore it after)
- remove command line tests (as these have now moved into the Clara project)
2014-03-06 08:16:06 +00:00

154 lines
5.1 KiB
C++

/*
* 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
#include "catch_config.hpp"
#include "catch_common.h"
#include "catch_clara.h"
#include <fstream>
namespace Catch {
inline void abortAfterFirst( ConfigData& config ) { config.abortAfter = 1; }
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;
}
inline void addTestOrTags( ConfigData& config, std::string const& _testSpec ) { config.testsOrTags.push_back( _testSpec ); }
inline void addWarning( ConfigData& config, std::string const& _warning ) {
if( _warning == "NoAssertions" )
config.warnings = (WarnAbout::What)( config.warnings | WarnAbout::NoAssertions );
else
throw std::runtime_error( "Unrecognised warning: '" + _warning + "'" );
}
inline void setVerbosity( ConfigData& config, int level ) {
// !TBD: accept strings?
config.verbosity = (Verbosity::Level)level;
}
inline void setShowDurations( ConfigData& config, bool _showDurations ) {
config.showDurations = _showDurations
? ShowDurations::Always
: ShowDurations::Never;
}
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);
if( !line.empty() && !startsWith( line, "#" ) )
addTestOrTags( config, line );
}
}
inline Clara::CommandLine<ConfigData> makeCommandLineParser() {
using namespace Clara;
CommandLine<ConfigData> cli;
cli.bindProcessName( &ConfigData::processName );
cli["-?"]["-h"]["--help"]
.describe( "display usage information" )
.into( &ConfigData::showHelp );
cli["-l"]["--list-tests"]
.describe( "list all/matching test cases" )
.into( &ConfigData::listTests );
cli["-t"]["--list-tags"]
.describe( "list all/matching tags" )
.into( &ConfigData::listTags );
cli["-s"]["--success"]
.describe( "include successful tests in output" )
.into( &ConfigData::showSuccessfulTests );
cli["-b"]["--break"]
.describe( "break into debugger on failure" )
.into( &ConfigData::shouldDebugBreak );
cli["-e"]["--nothrow"]
.describe( "skip exception tests" )
.into( &ConfigData::noThrow );
cli["-o"]["--out"]
.placeholder( "filename" )
.describe( "output filename" )
.into( &ConfigData::outputFilename );
cli["-r"]["--reporter"]
// .placeholder( "name[:filename]" )
.placeholder( "name" )
.describe( "reporter to use (defaults to console)" )
.into( &ConfigData::reporterName );
cli["-n"]["--name"]
.placeholder( "name" )
.describe( "suite name" )
.into( &ConfigData::name );
cli["-a"]["--abort"]
.describe( "abort at first failure" )
.into( &abortAfterFirst );
cli["-x"]["--abortx"]
.placeholder( "number of failures" )
.describe( "abort after x failures" )
.into( &abortAfterX );
cli["-w"]["--warn"]
.placeholder( "warning name" )
.describe( "enable warnings" )
.into( &addWarning );
// - needs updating if reinstated
// cli.into( &setVerbosity )
// .describe( "level of verbosity (0=no output)" )
// .shortOpt( "v")
// .longOpt( "verbosity" )
// .placeholder( "level" );
cli[_]
.placeholder( "test name, pattern or tags" )
.describe( "which test or tests to use" )
.into( &addTestOrTags );
cli["-d"]["--durations"]
.placeholder( "yes/no" )
.describe( "show test durations" )
.into( &setShowDurations );
cli["-f"]["--input-file"]
.placeholder( "filename" )
.describe( "load test names to run from a file" )
.into( &loadTestNamesFromFile );
// Less common commands which don't have a short form
cli["--list-test-names-only"]
.describe( "list all/matching test cases names only" )
.into( &ConfigData::listTestNamesOnly );
cli["--list-reporters"]
.describe( "list all reporters" )
.into( &ConfigData::listReporters );
return cli;
}
} // end namespace Catch
#endif // TWOBLUECUBES_CATCH_COMMANDLINE_HPP_INCLUDED