diff options
| author | Martin Hořeňovský | 2017-07-10 14:25:38 +0200 |
|---|---|---|
| committer | Martin Hořeňovský | 2017-07-10 14:25:38 +0200 |
| commit | 1d3bfa035344299f3fa1404ed0d138cf97d59fd5 (patch) | |
| tree | 73a5bdab120234d77bfa01b174a864696c43c745 /include/internal/catch_commandline.cpp | |
| parent | 5f3f19de0845c9bc4358dbef51af6827b13c0889 (diff) | |
| download | catch3-1d3bfa035344299f3fa1404ed0d138cf97d59fd5.tar.gz | |
More include and impl refactoring
Diffstat (limited to 'include/internal/catch_commandline.cpp')
| -rw-r--r-- | include/internal/catch_commandline.cpp | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/include/internal/catch_commandline.cpp b/include/internal/catch_commandline.cpp new file mode 100644 index 00000000..d5f2ab4e --- /dev/null +++ b/include/internal/catch_commandline.cpp @@ -0,0 +1,163 @@ +/* + * 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) + */ + +#include "catch_commandline.hpp" + +#include "catch_common.h" + +#include <fstream> +#include <ctime> + +namespace Catch { + + clara::Parser makeCommandLineParser( ConfigData& config ) { + + using namespace clara; + + auto const setWarning = [&]( std::string const& warning ) { + if( warning != "NoAssertions" ) + return ParserResult::runtimeError( "Unrecognised warning: '" + warning + "'" ); + config.warnings = static_cast<WarnAbout::What>( config.warnings | WarnAbout::NoAssertions ); + return ParserResult::ok( ParseResultType::Matched ); + }; + auto const loadTestNamesFromFile = [&]( std::string const& filename ) { + std::ifstream f( filename.c_str() ); + if( !f.is_open() ) + return ParserResult::runtimeError( "Unable to load input file: '" + filename + "'" ); + + std::string line; + while( std::getline( f, line ) ) { + line = trim(line); + if( !line.empty() && !startsWith( line, '#' ) ) { + if( !startsWith( line, '"' ) ) + line = '"' + line + '"'; + config.testsOrTags.push_back( line + ',' ); + } + } + return ParserResult::ok( ParseResultType::Matched ); + }; + auto const setTestOrder = [&]( 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 + return clara::ParserResult::runtimeError( "Unrecognised ordering: '" + order + "'" ); + return ParserResult::ok( ParseResultType::Matched ); + }; + auto const setRngSeed = [&]( std::string const& seed ) { + if( seed != "time" ) + return clara::detail::convertInto( seed, config.rngSeed ); + config.rngSeed = static_cast<unsigned int>( std::time(0) ); + return ParserResult::ok( ParseResultType::Matched ); + }; + auto const setColourUsage = [&]( std::string const& useColour ) { + auto mode = toLower( useColour ); + + if( mode == "yes" ) + config.useColour = UseColour::Yes; + else if( mode == "no" ) + config.useColour = UseColour::No; + else if( mode == "auto" ) + config.useColour = UseColour::Auto; + else + return ParserResult::runtimeError( "colour mode must be one of: auto, yes or no. '" + useColour + "' not recognised" ); + return ParserResult::ok( ParseResultType::Matched ); + }; + auto const setVerbosity = [&]( std::string const& verbosity ) { + auto lcVerbosity = toLower( verbosity ); + if( lcVerbosity == "quiet" ) + config.verbosity = Verbosity::Quiet; + else if( lcVerbosity == "normal" ) + config.verbosity = Verbosity::Normal; + else if( lcVerbosity == "high" ) + config.verbosity = Verbosity::High; + else + return ParserResult::runtimeError( "Unrecognised verbosity, '" + verbosity + "'" ); + return ParserResult::ok( ParseResultType::Matched ); + }; + + auto cli + = ExeName( config.processName ) + + Help( config.showHelp ) + + Opt( config.listTests ) + ["-l"]["--list-tests"] + ( "list all/matching test cases" ) + + Opt( config.listTags ) + ["-t"]["--list-tags"] + ( "list all/matching tags" ) + + Opt( config.showSuccessfulTests ) + ["-s"]["--success"] + ( "include successful tests in output" ) + + Opt( config.shouldDebugBreak ) + ["-b"]["--break"] + ( "break into debugger on failure" ) + + Opt( config.noThrow ) + ["-e"]["--nothrow"] + ( "skip exception tests" ) + + Opt( config.showInvisibles ) + ["-i"]["--invisibles"] + ( "show invisibles (tabs, newlines)" ) + + Opt( config.outputFilename, "filename" ) + ["-o"]["--out"] + ( "output filename" ) + + Opt( config.reporterNames, "name" ) + ["-r"]["--reporter"] + ( "reporter to use (defaults to console)" ) + + Opt( config.name, "name" ) + ["-n"]["--name"] + ( "suite name" ) + + Opt( [&]( bool ){ config.abortAfter = 1; } ) + ["-a"]["--abort"] + ( "abort at first failure" ) + + Opt( [&]( int x ){ config.abortAfter = x; }, "no. failures" ) + ["-x"]["--abortx"] + ( "abort after x failures" ) + + Opt( setWarning, "warning name" ) + ["-w"]["--warn"] + ( "enable warnings" ) + + Opt( [&]( bool ) { config.showDurations = ShowDurations::Always; } ) + ["-d"]["--durations"] + ( "show test durations" ) + + Opt( loadTestNamesFromFile, "filename" ) + ["-f"]["--input-file"] + ( "load test names to run from a file" ) + + Opt( config.filenamesAsTags ) + ["-#"]["--filenames-as-tags"] + ( "adds a tag for the filename" ) + + Opt( config.sectionsToRun, "section name" ) + ["-c"]["--section"] + ( "specify section to run" ) + + Opt( setVerbosity, "quiet|normal|high" ) + ["-v"]["--verbosity"] + ( "set output verbosity" ) + + Opt( config.listTestNamesOnly ) + ["--list-test-names-only"] + ( "list all/matching test cases names only" ) + + Opt( config.listReporters ) + ["--list-reporters"] + ( "list all reporters" ) + + Opt( setTestOrder, "decl|lex|rand" ) + ["--order"] + ( "test case order (defaults to decl)" ) + + Opt( setRngSeed, "'time'|number" ) + ["--rng-seed"] + ( "set a specific seed for random numbers" ) + + Opt( setColourUsage, "yes|no" ) + ["--use-colour"] + ( "should output be colourised" ) + + + Arg( config.testsOrTags, "test name|pattern|tags" ) + ( "which test or tests to use" ); + + return cli; + } + +} // end namespace Catch |