mirror of
https://github.com/catchorg/Catch2.git
synced 2025-08-01 12:55:40 +02:00
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)
This commit is contained in:
@@ -1,192 +0,0 @@
|
||||
/*
|
||||
* 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
|
||||
|
||||
#include "internal/clara.h" // This will does not declare Clara within the Catch namespace
|
||||
|
||||
#include "catch.hpp"
|
||||
|
||||
|
||||
// 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 {
|
||||
TestOpt() : number( 0 ), index( 0 ), flag( false ) {}
|
||||
|
||||
std::string processName;
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
struct TestOpt2 {
|
||||
std::string description;
|
||||
};
|
||||
|
||||
#ifdef CATCH_CONFIG_VARIADIC_MACROS
|
||||
|
||||
TEST_CASE( "cmdline" ) {
|
||||
|
||||
TestOpt config;
|
||||
Clara::CommandLine<TestOpt> cli;
|
||||
cli.bindProcessName( &TestOpt::processName );
|
||||
cli.bind( &TestOpt::fileName )
|
||||
.describe( "specifies output file" )
|
||||
.shortOpt( "o" )
|
||||
.longOpt( "output" )
|
||||
.hint( "filename" );
|
||||
|
||||
SECTION( "process name" ) {
|
||||
char const * argv[] = { "test", "-o filename.ext" };
|
||||
parseInto( cli, argv, config );
|
||||
|
||||
CHECK( config.processName == "test" );
|
||||
}
|
||||
SECTION( "arg separated by spaces" ) {
|
||||
char const * argv[] = { "test", "-o filename.ext" };
|
||||
parseInto( cli, argv, config );
|
||||
|
||||
CHECK( config.fileName == "filename.ext" );
|
||||
}
|
||||
SECTION( "arg separated by colon" ) {
|
||||
const char* argv[] = { "test", "-o:filename.ext" };
|
||||
parseInto( cli, argv, config );
|
||||
|
||||
CHECK( config.fileName == "filename.ext" );
|
||||
}
|
||||
SECTION( "arg separated by =" ) {
|
||||
const char* argv[] = { "test", "-o=filename.ext" };
|
||||
parseInto( cli, argv, config );
|
||||
|
||||
CHECK( config.fileName == "filename.ext" );
|
||||
}
|
||||
SECTION( "long opt" ) {
|
||||
const char* argv[] = { "test", "--output %stdout" };
|
||||
parseInto( cli, argv, config );
|
||||
|
||||
CHECK( config.fileName == "%stdout" );
|
||||
}
|
||||
|
||||
cli.bind( &TestOpt::number )
|
||||
.shortOpt( "n" )
|
||||
.hint( "an integral value" );
|
||||
|
||||
SECTION( "a number" ) {
|
||||
const char* argv[] = { "test", "-n 42" };
|
||||
parseInto( cli, argv, config );
|
||||
|
||||
CHECK( config.number == 42 );
|
||||
}
|
||||
SECTION( "not a number" ) {
|
||||
const char* argv[] = { "test", "-n forty-two" };
|
||||
CHECK_THROWS( parseInto( cli, argv, config ) );
|
||||
|
||||
CHECK( config.number == 0 );
|
||||
}
|
||||
|
||||
SECTION( "two parsers" ) {
|
||||
|
||||
TestOpt config1;
|
||||
TestOpt2 config2;
|
||||
Clara::CommandLine<TestOpt2> cli2;
|
||||
|
||||
cli2.bind( &TestOpt2::description )
|
||||
.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 );
|
||||
|
||||
REQUIRE_FALSE( unusedTokens.empty() );
|
||||
cli2.populate( unusedTokens, config2 );
|
||||
CHECK( config2.description == "some text" );
|
||||
}
|
||||
|
||||
SECTION( "methods" ) {
|
||||
cli.bind( &TestOpt::setValidIndex )
|
||||
.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 )
|
||||
.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;
|
||||
|
||||
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
|
@@ -8,7 +8,6 @@
|
||||
|
||||
/* Begin PBXBuildFile section */
|
||||
266B06B816F3A60A004ED264 /* VariadicMacrosTests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 266B06B616F3A60A004ED264 /* VariadicMacrosTests.cpp */; };
|
||||
266E9AD617290E8E0061DAB2 /* CmdLineTests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 266E9AD417290E8E0061DAB2 /* CmdLineTests.cpp */; };
|
||||
266ECD74170F3C620030D735 /* BDDTests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 266ECD73170F3C620030D735 /* BDDTests.cpp */; };
|
||||
26847E5F16BBADB40043B9C1 /* catch_message.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 26847E5D16BBADB40043B9C1 /* catch_message.cpp */; };
|
||||
26948286179A9AB900ED166E /* SectionTrackerTests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 26948284179A9AB900ED166E /* SectionTrackerTests.cpp */; };
|
||||
@@ -68,7 +67,6 @@
|
||||
263FD06117AF8DF200988A20 /* catch_timer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = catch_timer.h; sourceTree = "<group>"; };
|
||||
266B06B616F3A60A004ED264 /* VariadicMacrosTests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = VariadicMacrosTests.cpp; path = ../../../SelfTest/VariadicMacrosTests.cpp; sourceTree = "<group>"; };
|
||||
266E9AD117230ACF0061DAB2 /* catch_text.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = catch_text.hpp; sourceTree = "<group>"; };
|
||||
266E9AD417290E8E0061DAB2 /* CmdLineTests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CmdLineTests.cpp; path = ../../../SelfTest/CmdLineTests.cpp; sourceTree = "<group>"; };
|
||||
266ECD73170F3C620030D735 /* BDDTests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = BDDTests.cpp; path = ../../../SelfTest/BDDTests.cpp; sourceTree = "<group>"; };
|
||||
266ECD8C1713614B0030D735 /* catch_legacy_reporter_adapter.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = catch_legacy_reporter_adapter.hpp; sourceTree = "<group>"; };
|
||||
266ECD8D1713614B0030D735 /* catch_legacy_reporter_adapter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = catch_legacy_reporter_adapter.h; sourceTree = "<group>"; };
|
||||
@@ -193,7 +191,6 @@
|
||||
266E9AD317290E710061DAB2 /* Introspective Tests */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
266E9AD417290E8E0061DAB2 /* CmdLineTests.cpp */,
|
||||
26948284179A9AB900ED166E /* SectionTrackerTests.cpp */,
|
||||
);
|
||||
name = "Introspective Tests";
|
||||
@@ -535,7 +532,6 @@
|
||||
26847E5F16BBADB40043B9C1 /* catch_message.cpp in Sources */,
|
||||
266B06B816F3A60A004ED264 /* VariadicMacrosTests.cpp in Sources */,
|
||||
266ECD74170F3C620030D735 /* BDDTests.cpp in Sources */,
|
||||
266E9AD617290E8E0061DAB2 /* CmdLineTests.cpp in Sources */,
|
||||
26948286179A9AB900ED166E /* SectionTrackerTests.cpp in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
|
Reference in New Issue
Block a user