Skip to content

Commit b9e63dd

Browse files
René Scharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent af54383 commit b9e63dd

File tree

2 files changed

+66
-6
lines changed

2 files changed

+66
-6
lines changed

t/t0040-parse-options.sh

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ test_description='our own option parser'
1010
cat > expect << EOF
1111
usage: test-parse-options <options>
1212
13-
-b, --boolean get a boolean
13+
--yes get a boolean
14+
-D, --no-doubt begins with 'no-'
15+
-B, --no-fear be brave
16+
-b, --boolean increment by one
1417
-4, --or4 bitwise-or boolean with ...0100
1518
--neg-or4 same as --no-or4
1619
@@ -53,6 +56,59 @@ test_expect_success 'test help' '
5356

5457
mv expect expect.err
5558

59+
cat >expect.template <<EOF
60+
boolean: 0
61+
integer: 0
62+
timestamp: 0
63+
string: (not set)
64+
abbrev: 7
65+
verbose: 0
66+
quiet: no
67+
dry run: no
68+
file: (not set)
69+
EOF
70+
71+
check() {
72+
what="$1" &&
73+
shift &&
74+
expect="$1" &&
75+
shift &&
76+
sed "s/^$what .*/$what $expect/" <expect.template >expect &&
77+
test-parse-options $* >output 2>output.err &&
78+
test ! -s output.err &&
79+
test_cmp expect output
80+
}
81+
82+
check_unknown() {
83+
case "$1" in
84+
--*)
85+
echo error: unknown option \`${1#--}\' >expect ;;
86+
-*)
87+
echo error: unknown switch \`${1#-}\' >expect ;;
88+
esac &&
89+
cat expect.err >>expect &&
90+
test_must_fail test-parse-options $* >output 2>output.err &&
91+
test ! -s output &&
92+
test_cmp expect output.err
93+
}
94+
95+
test_expect_success 'OPT_BOOL() #1' 'check boolean: 1 --yes'
96+
test_expect_success 'OPT_BOOL() #2' 'check boolean: 1 --no-doubt'
97+
test_expect_success 'OPT_BOOL() #3' 'check boolean: 1 -D'
98+
test_expect_success 'OPT_BOOL() #4' 'check boolean: 1 --no-fear'
99+
test_expect_success 'OPT_BOOL() #5' 'check boolean: 1 -B'
100+
101+
test_expect_success 'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes'
102+
test_expect_success 'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB'
103+
104+
test_expect_success 'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes'
105+
test_expect_success 'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt'
106+
107+
test_expect_success 'OPT_BOOL() no negation #1' 'check_unknown --fear'
108+
test_expect_success 'OPT_BOOL() no negation #2' 'check_unknown --no-no-fear'
109+
110+
test_expect_failure 'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt'
111+
56112
cat > expect << EOF
57113
boolean: 2
58114
integer: 1729
@@ -296,7 +352,7 @@ test_expect_success 'OPT_NEGBIT() works' '
296352
test_cmp expect output
297353
'
298354

299-
test_expect_success 'OPT_BOOLEAN() with PARSE_OPT_NODASH works' '
355+
test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
300356
test-parse-options + + + + + + > output 2> output.err &&
301357
test ! -s output.err &&
302358
test_cmp expect output

test-parse-options.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ int main(int argc, const char **argv)
3737
NULL
3838
};
3939
struct option options[] = {
40-
OPT_BOOLEAN('b', "boolean", &boolean, "get a boolean"),
40+
OPT_BOOL(0, "yes", &boolean, "get a boolean"),
41+
OPT_BOOL('D', "no-doubt", &boolean, "begins with 'no-'"),
42+
{ OPTION_SET_INT, 'B', "no-fear", &boolean, NULL,
43+
"be brave", PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },
44+
OPT_COUNTUP('b', "boolean", &boolean, "increment by one"),
4145
OPT_BIT('4', "or4", &boolean,
4246
"bitwise-or boolean with ...0100", 4),
4347
OPT_NEGBIT(0, "neg-or4", &boolean, "same as --no-or4", 4),
@@ -62,11 +66,11 @@ int main(int argc, const char **argv)
6266
OPT_ARGUMENT("quux", "means --quux"),
6367
OPT_NUMBER_CALLBACK(&integer, "set integer to NUM",
6468
number_callback),
65-
{ OPTION_BOOLEAN, '+', NULL, &boolean, NULL, "same as -b",
69+
{ OPTION_COUNTUP, '+', NULL, &boolean, NULL, "same as -b",
6670
PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH },
67-
{ OPTION_BOOLEAN, 0, "ambiguous", &ambiguous, NULL,
71+
{ OPTION_COUNTUP, 0, "ambiguous", &ambiguous, NULL,
6872
"positive ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
69-
{ OPTION_BOOLEAN, 0, "no-ambiguous", &ambiguous, NULL,
73+
{ OPTION_COUNTUP, 0, "no-ambiguous", &ambiguous, NULL,
7074
"negative ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
7175
OPT_GROUP("Standard options"),
7276
OPT__ABBREV(&abbrev),

0 commit comments

Comments
 (0)