test-parse-options: convert to OPT_BOOL()
Introduce OPT_BOOL() to test-parse-options and add some tests for these "true" boolean options. Rename OPT_BOOLEAN to OPT_COUNTUP and OPTION_BOOLEAN to OPTION_COUNTUP as well. Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
af543833d4
commit
b9e63ddddc
@ -10,7 +10,10 @@ test_description='our own option parser'
|
|||||||
cat > expect << EOF
|
cat > expect << EOF
|
||||||
usage: test-parse-options <options>
|
usage: test-parse-options <options>
|
||||||
|
|
||||||
-b, --boolean get a boolean
|
--yes get a boolean
|
||||||
|
-D, --no-doubt begins with 'no-'
|
||||||
|
-B, --no-fear be brave
|
||||||
|
-b, --boolean increment by one
|
||||||
-4, --or4 bitwise-or boolean with ...0100
|
-4, --or4 bitwise-or boolean with ...0100
|
||||||
--neg-or4 same as --no-or4
|
--neg-or4 same as --no-or4
|
||||||
|
|
||||||
@ -53,6 +56,59 @@ test_expect_success 'test help' '
|
|||||||
|
|
||||||
mv expect expect.err
|
mv expect expect.err
|
||||||
|
|
||||||
|
cat >expect.template <<EOF
|
||||||
|
boolean: 0
|
||||||
|
integer: 0
|
||||||
|
timestamp: 0
|
||||||
|
string: (not set)
|
||||||
|
abbrev: 7
|
||||||
|
verbose: 0
|
||||||
|
quiet: no
|
||||||
|
dry run: no
|
||||||
|
file: (not set)
|
||||||
|
EOF
|
||||||
|
|
||||||
|
check() {
|
||||||
|
what="$1" &&
|
||||||
|
shift &&
|
||||||
|
expect="$1" &&
|
||||||
|
shift &&
|
||||||
|
sed "s/^$what .*/$what $expect/" <expect.template >expect &&
|
||||||
|
test-parse-options $* >output 2>output.err &&
|
||||||
|
test ! -s output.err &&
|
||||||
|
test_cmp expect output
|
||||||
|
}
|
||||||
|
|
||||||
|
check_unknown() {
|
||||||
|
case "$1" in
|
||||||
|
--*)
|
||||||
|
echo error: unknown option \`${1#--}\' >expect ;;
|
||||||
|
-*)
|
||||||
|
echo error: unknown switch \`${1#-}\' >expect ;;
|
||||||
|
esac &&
|
||||||
|
cat expect.err >>expect &&
|
||||||
|
test_must_fail test-parse-options $* >output 2>output.err &&
|
||||||
|
test ! -s output &&
|
||||||
|
test_cmp expect output.err
|
||||||
|
}
|
||||||
|
|
||||||
|
test_expect_success 'OPT_BOOL() #1' 'check boolean: 1 --yes'
|
||||||
|
test_expect_success 'OPT_BOOL() #2' 'check boolean: 1 --no-doubt'
|
||||||
|
test_expect_success 'OPT_BOOL() #3' 'check boolean: 1 -D'
|
||||||
|
test_expect_success 'OPT_BOOL() #4' 'check boolean: 1 --no-fear'
|
||||||
|
test_expect_success 'OPT_BOOL() #5' 'check boolean: 1 -B'
|
||||||
|
|
||||||
|
test_expect_success 'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes'
|
||||||
|
test_expect_success 'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB'
|
||||||
|
|
||||||
|
test_expect_success 'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes'
|
||||||
|
test_expect_success 'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt'
|
||||||
|
|
||||||
|
test_expect_success 'OPT_BOOL() no negation #1' 'check_unknown --fear'
|
||||||
|
test_expect_success 'OPT_BOOL() no negation #2' 'check_unknown --no-no-fear'
|
||||||
|
|
||||||
|
test_expect_failure 'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt'
|
||||||
|
|
||||||
cat > expect << EOF
|
cat > expect << EOF
|
||||||
boolean: 2
|
boolean: 2
|
||||||
integer: 1729
|
integer: 1729
|
||||||
@ -296,7 +352,7 @@ test_expect_success 'OPT_NEGBIT() works' '
|
|||||||
test_cmp expect output
|
test_cmp expect output
|
||||||
'
|
'
|
||||||
|
|
||||||
test_expect_success 'OPT_BOOLEAN() with PARSE_OPT_NODASH works' '
|
test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
|
||||||
test-parse-options + + + + + + > output 2> output.err &&
|
test-parse-options + + + + + + > output 2> output.err &&
|
||||||
test ! -s output.err &&
|
test ! -s output.err &&
|
||||||
test_cmp expect output
|
test_cmp expect output
|
||||||
|
@ -37,7 +37,11 @@ int main(int argc, const char **argv)
|
|||||||
NULL
|
NULL
|
||||||
};
|
};
|
||||||
struct option options[] = {
|
struct option options[] = {
|
||||||
OPT_BOOLEAN('b', "boolean", &boolean, "get a boolean"),
|
OPT_BOOL(0, "yes", &boolean, "get a boolean"),
|
||||||
|
OPT_BOOL('D', "no-doubt", &boolean, "begins with 'no-'"),
|
||||||
|
{ OPTION_SET_INT, 'B', "no-fear", &boolean, NULL,
|
||||||
|
"be brave", PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },
|
||||||
|
OPT_COUNTUP('b', "boolean", &boolean, "increment by one"),
|
||||||
OPT_BIT('4', "or4", &boolean,
|
OPT_BIT('4', "or4", &boolean,
|
||||||
"bitwise-or boolean with ...0100", 4),
|
"bitwise-or boolean with ...0100", 4),
|
||||||
OPT_NEGBIT(0, "neg-or4", &boolean, "same as --no-or4", 4),
|
OPT_NEGBIT(0, "neg-or4", &boolean, "same as --no-or4", 4),
|
||||||
@ -62,11 +66,11 @@ int main(int argc, const char **argv)
|
|||||||
OPT_ARGUMENT("quux", "means --quux"),
|
OPT_ARGUMENT("quux", "means --quux"),
|
||||||
OPT_NUMBER_CALLBACK(&integer, "set integer to NUM",
|
OPT_NUMBER_CALLBACK(&integer, "set integer to NUM",
|
||||||
number_callback),
|
number_callback),
|
||||||
{ OPTION_BOOLEAN, '+', NULL, &boolean, NULL, "same as -b",
|
{ OPTION_COUNTUP, '+', NULL, &boolean, NULL, "same as -b",
|
||||||
PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH },
|
PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH },
|
||||||
{ OPTION_BOOLEAN, 0, "ambiguous", &ambiguous, NULL,
|
{ OPTION_COUNTUP, 0, "ambiguous", &ambiguous, NULL,
|
||||||
"positive ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
|
"positive ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
|
||||||
{ OPTION_BOOLEAN, 0, "no-ambiguous", &ambiguous, NULL,
|
{ OPTION_COUNTUP, 0, "no-ambiguous", &ambiguous, NULL,
|
||||||
"negative ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
|
"negative ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
|
||||||
OPT_GROUP("Standard options"),
|
OPT_GROUP("Standard options"),
|
||||||
OPT__ABBREV(&abbrev),
|
OPT__ABBREV(&abbrev),
|
||||||
|
Reference in New Issue
Block a user