From b60f3c14d1787addede455bdee9bd68dffa4f7f0 Mon Sep 17 00:00:00 2001 From: Daniel Aarno Date: Thu, 17 May 2012 22:12:08 +0200 Subject: [PATCH] Added tests for various Arg properties such as invalid flag/names and DocBookOutput --- examples/Makefile.am | 4 +- examples/test24.cpp | 67 ++++++++++++++++++++++++ examples/test25.cpp | 37 ++++++++++++++ tests/Makefile.am | 8 ++- tests/runtests.sh | 2 +- tests/test83.out | 7 +++ tests/test83.sh | 11 ++++ tests/test84.out | 119 +++++++++++++++++++++++++++++++++++++++++++ tests/test84.sh | 13 +++++ 9 files changed, 264 insertions(+), 4 deletions(-) create mode 100644 examples/test24.cpp create mode 100644 examples/test25.cpp create mode 100644 tests/test83.out create mode 100755 tests/test83.sh create mode 100644 tests/test84.out create mode 100755 tests/test84.sh diff --git a/examples/Makefile.am b/examples/Makefile.am index f75025f..2c10fc8 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -1,7 +1,7 @@ noinst_PROGRAMS = test1 test2 test3 test4 test5 test6 test7 test8 test9 \ test10 test11 test12 test13 test14 test15 test16 \ - test17 test18 test19 test20 test21 test22 test23 + test17 test18 test19 test20 test21 test22 test23 test24 test25 test1_SOURCES = test1.cpp test2_SOURCES = test2.cpp @@ -26,6 +26,8 @@ test20_SOURCES = test20.cpp test21_SOURCES = test21.cpp test22_SOURCES = test22.cpp test23_SOURCES = test23.cpp +test24_SOURCES = test24.cpp +test25_SOURCES = test25.cpp AM_CPPFLAGS = -I$(top_srcdir)/include diff --git a/examples/test24.cpp b/examples/test24.cpp new file mode 100644 index 0000000..5964694 --- /dev/null +++ b/examples/test24.cpp @@ -0,0 +1,67 @@ +// -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*- + +// Test various Arg properties such as invalid flag/names + +#include "tclap/CmdLine.h" + +using namespace TCLAP; +using namespace std; + +int main() { + CmdLine cmd("Command description message", ' ', "0.9"); + try { // Argument with two character 'flag' + ValueArg nameArg("nx","name","Name to print",true, + "homer","string"); + return EXIT_FAILURE; + } catch(SpecificationException e) { + cout << e.what() << std::endl; // Expected + } + + try { // space as flag + ValueArg nameArg(" ","name","Name to print",true, + "homer","string"); + return EXIT_FAILURE; + } catch(SpecificationException e) { + cout << e.what() << std::endl; // Expected + } + + try { // - as flag + ValueArg nameArg("-","name","Name to print",true, + "homer","string"); + return EXIT_FAILURE; + } catch(SpecificationException e) { + cout << e.what() << std::endl; // Expected + } + + try { // -- as flag + ValueArg nameArg("--","name","Name to print",true, + "homer","string"); + return EXIT_FAILURE; + } catch(SpecificationException e) { + cout << e.what() << std::endl; // Expected + } + + try { // space as name + ValueArg nameArg("n"," ","Name to print",true, + "homer","string"); + return EXIT_FAILURE; + } catch(SpecificationException e) { + cout << e.what() << std::endl; // Expected + } + + try { // - as flag + ValueArg nameArg("n","-","Name to print",true, + "homer","string"); + return EXIT_FAILURE; + } catch(SpecificationException e) { + cout << e.what() << std::endl; // Expected + } + + try { // -- as flag + ValueArg nameArg("n","--","Name to print",true, + "homer","string"); + return EXIT_FAILURE; + } catch(SpecificationException e) { + cout << e.what() << std::endl; // Expected + } +} diff --git a/examples/test25.cpp b/examples/test25.cpp new file mode 100644 index 0000000..a799347 --- /dev/null +++ b/examples/test25.cpp @@ -0,0 +1,37 @@ +// -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*- + +#include "tclap/CmdLine.h" +#include "tclap/DocBookOutput.h" +#include "tclap/ZshCompletionOutput.h" +#include +#include + +using namespace TCLAP; +using namespace std; + +int main(int argc, char** argv) +{ + CmdLine cmd("this is a message", ' ', "0.99" ); + DocBookOutput docoutput; + ZshCompletionOutput zshoutput; + CmdLineOutput *output = &zshoutput; + + if (argc > 2) + output = &docoutput; + + cmd.setOutput(output); + + SwitchArg btest("B","sB", "exist Test B", false); + MultiArg atest("A","sA", "exist Test A", false, "integer"); + + ValueArg stest("s", "Bs", "string test", true, "homer", + "string"); + + cmd.xorAdd(stest, btest); + cmd.add( atest ); + + cmd.parse(argc,argv); +} + + + diff --git a/tests/Makefile.am b/tests/Makefile.am index d3953d0..f354178 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -81,7 +81,9 @@ TESTS = test1.sh \ test79.sh \ test80.sh \ test81.sh \ - test82.sh + test82.sh \ + test83.sh \ + test84.sh EXTRA_DIST = $(TESTS) \ test1.out \ @@ -165,6 +167,8 @@ EXTRA_DIST = $(TESTS) \ test79.out \ test80.out \ test81.out \ - test82.out + test82.out \ + test83.out \ + test84.out CLEANFILES = tmp.out diff --git a/tests/runtests.sh b/tests/runtests.sh index 6fb2608..9c2e8b9 100755 --- a/tests/runtests.sh +++ b/tests/runtests.sh @@ -6,7 +6,7 @@ cd $DIR let "suc = 0" let "fail = 0" -NUMTEST=82 +NUMTEST=84 for (( tno = 1 ; $tno <= $NUMTEST ; tno = $tno + 1 )); do ./testCheck.sh $tno diff --git a/tests/test83.out b/tests/test83.out new file mode 100644 index 0000000..7198bc1 --- /dev/null +++ b/tests/test83.out @@ -0,0 +1,7 @@ +-nx (--name) -- Argument flag can only be one character long +- (--name) -- Argument flag cannot be either '-' or '--' or a space. +-- (--name) -- Argument flag cannot be either '-' or '--' or a space. +--- (--name) -- Argument flag can only be one character long +-n (-- ) -- Argument name begin with either '-' or '--' or space. +-n (---) -- Argument name begin with either '-' or '--' or space. +-n (----) -- Argument name begin with either '-' or '--' or space. diff --git a/tests/test83.sh b/tests/test83.sh new file mode 100755 index 0000000..5aab222 --- /dev/null +++ b/tests/test83.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +# success +../examples/test24 > tmp.out 2>&1 + +if cmp -s tmp.out $srcdir/test83.out; then + exit 0 +else + exit 1 +fi + diff --git a/tests/test84.out b/tests/test84.out new file mode 100644 index 0000000..92ed5ad --- /dev/null +++ b/tests/test84.out @@ -0,0 +1,119 @@ + + + + + +test25 +1 + + +test25 +this is a message + + + +test25 + +-s string +-B + +-A +-- +--version +-h + + + +Description + +this is a message + + + +Options + + + + + + + + + + +exist Test A + + + + + + + + + + + + +(OR required) exist Test B + + + + + + + + + + + + +(OR required) string test + + + + + + + + + + + + +Ignores the rest of the labeled arguments following this flag. + + + + + + + + + +Displays version information and exits. + + + + + + + + + + + + +Displays usage information and exits. + + + + + + +Version + +0.99 + + + diff --git a/tests/test84.sh b/tests/test84.sh new file mode 100755 index 0000000..4a35dd7 --- /dev/null +++ b/tests/test84.sh @@ -0,0 +1,13 @@ +#!/bin/sh + +# docbookoutput. The when this test fails due to e.g. formatting +# changes the results needs to be manually reviewed and the test81.out +# updated +../examples/test25 -h x > tmp.out 2>&1 + +if cmp -s tmp.out $srcdir/test84.out; then + exit 0 +else + exit 1 +fi +