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>
René Scharfe committed
Feb 25, 2012 at 20:11 UTC
b9e63ddddc8928f39db6658bf6428a9b646e5ea5
2 files changed
+66
-6
t/t0040-parse-options.sh
+58
-2
@@ -10,7 +10,10 @@ test_description='our own option parser'
10
cat > expect << EOF
11
usage: test-parse-options <options>
12
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
17
-4, --or4 bitwise-or boolean with ...0100
18
--neg-or4 same as --no-or4
19
@@ -53,6 +56,59 @@ test_expect_success 'test help' '
56
57
mv expect expect.err
58
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
+
112
cat > expect << EOF
113
boolean: 2
114
integer: 1729
@@ -296,7 +352,7 @@ test_expect_success 'OPT_NEGBIT() works' '
352
test_cmp expect output
353
'
354
299
-test_expect_success 'OPT_BOOLEAN() with PARSE_OPT_NODASH works' '
355
+test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
356
test-parse-options + + + + + + > output 2> output.err &&
357
test ! -s output.err &&
358
test_cmp expect output
test-parse-options.c
+8
-4
@@ -37,7 +37,11 @@ int main(int argc, const char **argv)
37
NULL
38
};
39
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"),
45
OPT_BIT('4', "or4", &boolean,
46
"bitwise-or boolean with ...0100", 4),
47
OPT_NEGBIT(0, "neg-or4", &boolean, "same as --no-or4", 4),
@@ -62,11 +66,11 @@ int main(int argc, const char **argv)
66
OPT_ARGUMENT("quux", "means --quux"),
67
OPT_NUMBER_CALLBACK(&integer, "set integer to NUM",
68
number_callback),
65
- { OPTION_BOOLEAN, '+', NULL, &boolean, NULL, "same as -b",
69
+ { OPTION_COUNTUP, '+', NULL, &boolean, NULL, "same as -b",
70
PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH },
67
- { OPTION_BOOLEAN, 0, "ambiguous", &ambiguous, NULL,
71
+ { OPTION_COUNTUP, 0, "ambiguous", &ambiguous, NULL,
72
"positive ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
69
- { OPTION_BOOLEAN, 0, "no-ambiguous", &ambiguous, NULL,
73
+ { OPTION_COUNTUP, 0, "no-ambiguous", &ambiguous, NULL,
74
"negative ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
75
OPT_GROUP("Standard options"),
76
OPT__ABBREV(&abbrev),