Catch/projects/SelfTest/CmdLineTests.cpp

193 lines
5.3 KiB
C++
Raw Normal View History

2013-04-27 07:26:13 -04:00
/*
* Created by Phil on 22/10/2010.
* Copyright 2010 Two Blue Cubes Ltd
*
* 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)
*/
#ifdef __clang__
#pragma clang diagnostic ignored "-Wpadded"
#endif
2013-05-27 05:52:58 -04:00
#include "internal/clara.h"
2013-05-27 05:52:58 -04:00
#include "catch.hpp"
2013-04-27 07:26:13 -04:00
// 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 );
}
2013-04-27 07:26:13 -04:00
struct TestOpt {
TestOpt() : number( 0 ), index( 0 ), flag( false ) {}
2013-04-27 07:26:13 -04:00
std::string processName;
2013-04-27 07:26:13 -04:00
std::string fileName;
int number;
int index;
bool flag;
std::string firstPos;
std::string secondPos;
std::string unpositional;
void setValidIndex( int i ) {
if( i < 0 || i > 10 )
throw std::domain_error( "index must be between 0 and 10" );
index = i;
}
2013-04-27 07:26:13 -04:00
};
struct TestOpt2 {
std::string description;
};
#ifdef CATCH_CONFIG_VARIADIC_MACROS
TEST_CASE( "cmdline" ) {
2013-04-27 07:26:13 -04:00
TestOpt config;
Clara::CommandLine<TestOpt> cli;
cli.bindProcessName( &TestOpt::processName );
cli.bind( &TestOpt::fileName )
2013-05-15 14:25:34 -04:00
.describe( "specifies output file" )
.shortOpt( "o" )
.longOpt( "output" )
.hint( "filename" );
2013-04-27 07:26:13 -04:00
SECTION( "process name" ) {
char const * argv[] = { "test", "-o filename.ext" };
parseInto( cli, argv, config );
CHECK( config.processName == "test" );
}
2013-05-23 14:01:10 -04:00
SECTION( "arg separated by spaces" ) {
char const * argv[] = { "test", "-o filename.ext" };
parseInto( cli, argv, config );
CHECK( config.fileName == "filename.ext" );
}
2013-05-23 14:01:10 -04:00
SECTION( "arg separated by colon" ) {
2013-04-27 07:26:13 -04:00
const char* argv[] = { "test", "-o:filename.ext" };
parseInto( cli, argv, config );
2013-04-27 07:26:13 -04:00
CHECK( config.fileName == "filename.ext" );
}
2013-05-23 14:01:10 -04:00
SECTION( "arg separated by =" ) {
const char* argv[] = { "test", "-o=filename.ext" };
parseInto( cli, argv, config );
CHECK( config.fileName == "filename.ext" );
2013-04-27 07:26:13 -04:00
}
SECTION( "long opt" ) {
const char* argv[] = { "test", "--output %stdout" };
parseInto( cli, argv, config );
2013-04-27 07:26:13 -04:00
CHECK( config.fileName == "%stdout" );
2013-04-27 07:26:13 -04:00
}
cli.bind( &TestOpt::number )
2013-05-15 14:25:34 -04:00
.shortOpt( "n" )
.hint( "an integral value" );
2013-04-27 07:26:13 -04:00
SECTION( "a number" ) {
const char* argv[] = { "test", "-n 42" };
parseInto( cli, argv, config );
2013-04-27 07:26:13 -04:00
CHECK( config.number == 42 );
}
SECTION( "not a number" ) {
const char* argv[] = { "test", "-n forty-two" };
CHECK_THROWS( parseInto( cli, argv, config ) );
2013-04-27 07:26:13 -04:00
CHECK( config.number == 0 );
}
SECTION( "two parsers" ) {
TestOpt config1;
TestOpt2 config2;
Clara::CommandLine<TestOpt2> cli2;
cli2.bind( &TestOpt2::description )
2013-05-15 14:25:34 -04:00
.describe( "description" )
.shortOpt( "d" )
.longOpt( "description" )
.hint( "some text" );
const char* argv[] = { "test", "-n 42", "-d some text" };
std::vector<Clara::Parser::Token> unusedTokens = parseInto( cli, argv, config1 );
CHECK( config1.number == 42 );
2013-05-15 14:25:34 -04:00
REQUIRE_FALSE( unusedTokens.empty() );
cli2.populate( unusedTokens, config2 );
CHECK( config2.description == "some text" );
}
SECTION( "methods" ) {
cli.bind( &TestOpt::setValidIndex )
2013-05-15 14:25:34 -04:00
.describe( "An index, which is an integer between 0 and 10, inclusive" )
.shortOpt( "i" )
.hint( "index" );
SECTION( "in range" ) {
const char* argv[] = { "test", "-i 3" };
parseInto( cli, argv, config );
REQUIRE( config.index == 3 );
}
SECTION( "out of range" ) {
const char* argv[] = { "test", "-i 42" };
REQUIRE_THROWS( parseInto( cli, argv, config ) );
}
}
SECTION( "flags" ) {
cli.bind( &TestOpt::flag )
2013-05-15 14:25:34 -04:00
.describe( "A flag" )
.shortOpt( "f" );
SECTION( "set" ) {
const char* argv[] = { "test", "-f" };
parseInto( cli, argv, config );
REQUIRE( config.flag );
}
SECTION( "not set" ) {
const char* argv[] = { "test" };
parseInto( cli, argv, config );
REQUIRE( config.flag == false );
}
}
SECTION( "positional" ) {
cli.bind( &TestOpt::secondPos )
.describe( "Second position" )
.hint( "second arg" )
.position( 2 );
cli.bind( &TestOpt::unpositional )
.hint( "any arg" )
.describe( "Unpositional" );
cli.bind( &TestOpt::firstPos )
.describe( "First position" )
.hint( "first arg" )
.position( 1 );
// std::cout << cli.usage( "testApp" ) << std::endl;
2013-05-21 14:21:24 -04:00
const char* argv[] = { "test", "-f", "1st", "-o", "filename", "2nd", "3rd" };
parseInto( cli, argv, config );
REQUIRE( config.firstPos == "1st" );
REQUIRE( config.secondPos == "2nd" );
REQUIRE( config.unpositional == "3rd" );
}
}
#endif