Refactored CLI tests and converted main one into a scenario

This commit is contained in:
Phil Nash 2013-05-17 08:00:57 +01:00
parent 8d1100daa6
commit 6574f639a3

View File

@ -375,6 +375,13 @@ namespace Clara {
} // end namespace Clara } // end namespace Clara
// Helper to deduce size from array literals and pass on to parser
template<size_t size, typename ConfigT>
std::vector<Clara::Parser::Token> parseInto( Clara::CommandLine<ConfigT>& cli, char const* (&argv)[size], ConfigT& config ) {
return cli.parseInto( size, argv, config );
}
struct TestOpt { struct TestOpt {
TestOpt() : number( 0 ), index( 0 ), flag( false ) {} TestOpt() : number( 0 ), index( 0 ), flag( false ) {}
@ -408,26 +415,26 @@ TEST_CASE( "cmdline" ) {
SECTION( "plain filename" ) { SECTION( "plain filename" ) {
const char* argv[] = { "test", "-o filename.ext" }; const char* argv[] = { "test", "-o filename.ext" };
parseInto( cli, argv, config );
cli.parseInto( sizeof(argv)/sizeof(char*), argv, config );
CHECK( config.fileName == "filename.ext" ); CHECK( config.fileName == "filename.ext" );
} }
SECTION( "plain filename with colon" ) { SECTION( "plain filename with colon" ) {
const char* argv[] = { "test", "-o:filename.ext" }; const char* argv[] = { "test", "-o:filename.ext" };
parseInto( cli, argv, config );
cli.parseInto( sizeof(argv)/sizeof(char*), argv, config );
CHECK( config.fileName == "filename.ext" ); CHECK( config.fileName == "filename.ext" );
} }
SECTION( "plain filename with =" ) { SECTION( "plain filename with =" ) {
const char* argv[] = { "test", "-o=filename.ext" }; const char* argv[] = { "test", "-o=filename.ext" };
parseInto( cli, argv, config );
cli.parseInto( sizeof(argv)/sizeof(char*), argv, config );
CHECK( config.fileName == "filename.ext" ); CHECK( config.fileName == "filename.ext" );
} }
SECTION( "long opt" ) { SECTION( "long opt" ) {
const char* argv[] = { "test", "--output %stdout" }; const char* argv[] = { "test", "--output %stdout" };
parseInto( cli, argv, config );
cli.parseInto( sizeof(argv)/sizeof(char*), argv, config );
CHECK( config.fileName == "%stdout" ); CHECK( config.fileName == "%stdout" );
} }
@ -437,14 +444,14 @@ TEST_CASE( "cmdline" ) {
SECTION( "a number" ) { SECTION( "a number" ) {
const char* argv[] = { "test", "-n 42" }; const char* argv[] = { "test", "-n 42" };
parseInto( cli, argv, config );
cli.parseInto( sizeof(argv)/sizeof(char*), argv, config );
CHECK( config.number == 42 ); CHECK( config.number == 42 );
} }
SECTION( "not a number" ) { SECTION( "not a number" ) {
const char* argv[] = { "test", "-n forty-two" }; const char* argv[] = { "test", "-n forty-two" };
CHECK_THROWS( parseInto( cli, argv, config ) );
CHECK_THROWS( cli.parseInto( sizeof(argv)/sizeof(char*), argv, config ) );
CHECK( config.number == 0 ); CHECK( config.number == 0 );
} }
@ -461,8 +468,8 @@ TEST_CASE( "cmdline" ) {
.argName( "<some text>" ); .argName( "<some text>" );
const char* argv[] = { "test", "-n 42", "-d some text" }; const char* argv[] = { "test", "-n 42", "-d some text" };
std::vector<Clara::Parser::Token> unusedTokens = parseInto( cli, argv, config1 );
std::vector<Clara::Parser::Token> unusedTokens = cli.parseInto( sizeof(argv)/sizeof(char*), argv, config1 );
CHECK( config1.number == 42 ); CHECK( config1.number == 42 );
REQUIRE_FALSE( unusedTokens.empty() ); REQUIRE_FALSE( unusedTokens.empty() );
@ -478,14 +485,14 @@ TEST_CASE( "cmdline" ) {
SECTION( "in range" ) { SECTION( "in range" ) {
const char* argv[] = { "test", "-i 3" }; const char* argv[] = { "test", "-i 3" };
parseInto( cli, argv, config );
cli.parseInto( sizeof(argv)/sizeof(char*), argv, config );
REQUIRE( config.index == 3 ); REQUIRE( config.index == 3 );
} }
SECTION( "out of range" ) { SECTION( "out of range" ) {
const char* argv[] = { "test", "-i 42" }; const char* argv[] = { "test", "-i 42" };
REQUIRE_THROWS( cli.parseInto( sizeof(argv)/sizeof(char*), argv, config ) ); REQUIRE_THROWS( parseInto( cli, argv, config ) );
} }
} }
@ -496,14 +503,14 @@ TEST_CASE( "cmdline" ) {
SECTION( "set" ) { SECTION( "set" ) {
const char* argv[] = { "test", "-f" }; const char* argv[] = { "test", "-f" };
parseInto( cli, argv, config );
cli.parseInto( sizeof(argv)/sizeof(char*), argv, config );
REQUIRE( config.flag ); REQUIRE( config.flag );
} }
SECTION( "not set" ) { SECTION( "not set" ) {
const char* argv[] = { "test" }; const char* argv[] = { "test" };
parseInto( cli, argv, config );
cli.parseInto( sizeof(argv)/sizeof(char*), argv, config );
REQUIRE( config.flag == false ); REQUIRE( config.flag == false );
} }
} }
@ -542,95 +549,104 @@ struct Config {
}; };
TEST_CASE( "growing new Catch cli" ) { SCENARIO( "New Catch commandline interface", "[cli]" ) {
Clara::CommandLine<Config> cli;
cli.bind( &Config::showHelp ) GIVEN( "A built cli parser for Catch" ) {
.describe( "display usage information" ) Clara::CommandLine<Config> cli;
.shortOpt( "?")
.shortOpt( "h")
.longOpt( "help" );
cli.bind( &Config::listTests ) cli.bind( &Config::showHelp )
.describe( "list all (or matching) test cases" ) .describe( "display usage information" )
.shortOpt( "l") .shortOpt( "?")
.longOpt( "list" ); .shortOpt( "h")
.longOpt( "help" );
cli.bind( &Config::listTags ) cli.bind( &Config::listTests )
.describe( "list all (or matching) tags" ) .describe( "list all (or matching) test cases" )
.shortOpt( "t") .shortOpt( "l")
.longOpt( "tags" ); .longOpt( "list" );
cli.bind( &Config::showPassingTests ) cli.bind( &Config::listTags )
.describe( "show passing test output" ) .describe( "list all (or matching) tags" )
.shortOpt( "p") .shortOpt( "t")
.longOpt( "passing" ); .longOpt( "tags" );
cli.bind( &Config::breakIntoDebugger ) cli.bind( &Config::showPassingTests )
.describe( "break into debugger on failure" ) .describe( "show passing test output" )
.shortOpt( "b") .shortOpt( "p")
.longOpt( "break" ); .longOpt( "passing" );
cli.bind( &Config::noThrow ) cli.bind( &Config::breakIntoDebugger )
.describe( "Skip exception tests" ) .describe( "break into debugger on failure" )
.shortOpt( "e") .shortOpt( "b")
.longOpt( "nothrow" ); .longOpt( "break" );
cli.bind( &Config::fileName ) cli.bind( &Config::noThrow )
.describe( "output filename" ) .describe( "Skip exception tests" )
.shortOpt( "o") .shortOpt( "e")
.longOpt( "out" ) .longOpt( "nothrow" );
.argName( "file name" );
cli.bind( &Config::reporter ) cli.bind( &Config::fileName )
.describe( "e.g. console | xml | junit" ) .describe( "output filename" )
.shortOpt( "r") .shortOpt( "o")
.longOpt( "reporter" ) .longOpt( "out" )
.argName( "reporter name[:filename]" ); .argName( "file name" );
cli.bind( &Config::suiteName ) cli.bind( &Config::reporter )
.describe( "suite name" ) .describe( "e.g. console | xml | junit" )
.shortOpt( "n") .shortOpt( "r")
.longOpt( "name" ) .longOpt( "reporter" )
.argName( "name" ); .argName( "reporter name[:filename]" );
cli.bind( &Config::abortAfterFirst ) cli.bind( &Config::suiteName )
.describe( "abort at first failure" ) .describe( "suite name" )
.shortOpt( "a") .shortOpt( "n")
.longOpt( "abort" ); .longOpt( "name" )
.argName( "name" );
cli.bind( &Config::abortAfterX ) cli.bind( &Config::abortAfterFirst )
.describe( "abort after x failures" ) .describe( "abort at first failure" )
.shortOpt( "x") .shortOpt( "a")
.longOpt( "abortx" ) .longOpt( "abort" );
.argName( "number of failures" );
cli.bind( &Config::addWarning ) cli.bind( &Config::abortAfterX )
.describe( "enables warnings" ) .describe( "abort after x failures" )
.shortOpt( "w") .shortOpt( "x")
.longOpt( "warn" ) .longOpt( "abortx" )
.argName( "warning name" ); .argName( "number of failures" );
cli.bind( &Config::addTestOrTags ) cli.bind( &Config::addWarning )
.describe( "which test or tests to use" ) .describe( "enables warnings" )
.argName( "test name, pattern or tags" ); .shortOpt( "w")
.longOpt( "warn" )
.argName( "warning name" );
std::cout << cli << std::endl; cli.bind( &Config::addTestOrTags )
.describe( "which test or tests to use" )
Config config; .argName( "test name, pattern or tags" );
const char* argv[] = { "test", "-peb" }; WHEN( "It is streamed" )
int argc = sizeof(argv)/sizeof(char*); THEN( "It prints the usage strings" )
std::cout << cli << std::endl;
cli.parseInto( argc, argv, config );
Config config;
CHECK( config.showPassingTests );
CHECK( config.noThrow ); WHEN( "Multiple flags are combined" ) {
CHECK( config.breakIntoDebugger );
CHECK_FALSE( config.showPassingTests );
// CHECK_FALSE( config.noThrow );
// REQUIRE_THROWS( cli.parseInto( sizeof(argv)/sizeof(char*), argv, config ) ); CHECK_FALSE( config.breakIntoDebugger );
const char* argv[] = { "test", "-peb" };
parseInto( cli, argv, config );
THEN( "All the flags are set" ) {
CHECK( config.showPassingTests );
CHECK( config.noThrow );
CHECK( config.breakIntoDebugger );
}
}
}
} }
// !TBD still support this? // !TBD still support this?