Skip to content

Commit 010a2da

Browse files
sbeyergitster
authored andcommitted
Extend parse-options test suite
This patch serves two purposes: 1. test-parse-option.c should be a more complete example for the parse-options API, and 2. there have been no tests for OPT_CALLBACK, OPT_DATE, OPT_BIT, OPT_SET_INT and OPT_SET_PTR before. Signed-off-by: Stephan Beyer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 224712e commit 010a2da

File tree

2 files changed

+144
-11
lines changed

2 files changed

+144
-11
lines changed

t/t0040-parse-options.sh

Lines changed: 109 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,35 @@ cat > expect.err << EOF
1111
usage: test-parse-options <options>
1212
1313
-b, --boolean get a boolean
14+
-4, --or4 bitwise-or boolean with ...0100
15+
1416
-i, --integer <n> get a integer
1517
-j <n> get a integer, too
18+
--set23 set integer to 23
19+
-t <time> get timestamp of <time>
20+
-L, --length <str> get length of <str>
1621
17-
string options
22+
String options
1823
-s, --string <string>
1924
get a string
2025
--string2 <str> get another string
2126
--st <st> get another string (pervert ordering)
2227
-o <str> get another string
28+
--default-string set string to default
2329
24-
magic arguments
30+
Magic arguments
2531
--quux means --quux
2632
33+
Standard options
34+
--abbrev[=<n>] use <n> digits to display SHA-1s
35+
-v, --verbose be verbose
36+
-n, --dry-run dry run
37+
-q, --quiet be quiet
38+
2739
EOF
2840

2941
test_expect_success 'test help' '
30-
! test-parse-options -h > output 2> output.err &&
42+
test_must_fail test-parse-options -h > output 2> output.err &&
3143
test ! -s output &&
3244
test_cmp expect.err output.err
3345
'
@@ -36,21 +48,31 @@ cat > expect << EOF
3648
boolean: 2
3749
integer: 1729
3850
string: 123
51+
abbrev: 7
52+
verbose: 2
53+
quiet: no
54+
dry run: yes
3955
EOF
4056

4157
test_expect_success 'short options' '
42-
test-parse-options -s123 -b -i 1729 -b > output 2> output.err &&
58+
test-parse-options -s123 -b -i 1729 -b -vv -n > output 2> output.err &&
4359
test_cmp expect output &&
4460
test ! -s output.err
4561
'
62+
4663
cat > expect << EOF
4764
boolean: 2
4865
integer: 1729
4966
string: 321
67+
abbrev: 10
68+
verbose: 2
69+
quiet: no
70+
dry run: no
5071
EOF
5172

5273
test_expect_success 'long options' '
5374
test-parse-options --boolean --integer 1729 --boolean --string2=321 \
75+
--verbose --verbose --no-dry-run --abbrev=10 \
5476
> output 2> output.err &&
5577
test ! -s output.err &&
5678
test_cmp expect output
@@ -60,6 +82,10 @@ cat > expect << EOF
6082
boolean: 1
6183
integer: 13
6284
string: 123
85+
abbrev: 7
86+
verbose: 0
87+
quiet: no
88+
dry run: no
6389
arg 00: a1
6490
arg 01: b1
6591
arg 02: --boolean
@@ -76,6 +102,10 @@ cat > expect << EOF
76102
boolean: 0
77103
integer: 2
78104
string: (not set)
105+
abbrev: 7
106+
verbose: 0
107+
quiet: no
108+
dry run: no
79109
EOF
80110

81111
test_expect_success 'unambiguously abbreviated option' '
@@ -99,6 +129,10 @@ cat > expect << EOF
99129
boolean: 0
100130
integer: 0
101131
string: 123
132+
abbrev: 7
133+
verbose: 0
134+
quiet: no
135+
dry run: no
102136
EOF
103137

104138
test_expect_success 'non ambiguous option (after two options it abbreviates)' '
@@ -107,20 +141,24 @@ test_expect_success 'non ambiguous option (after two options it abbreviates)' '
107141
test_cmp expect output
108142
'
109143

110-
cat > expect.err << EOF
144+
cat > typo.err << EOF
111145
error: did you mean \`--boolean\` (with two dashes ?)
112146
EOF
113147

114148
test_expect_success 'detect possible typos' '
115-
! test-parse-options -boolean > output 2> output.err &&
149+
test_must_fail test-parse-options -boolean > output 2> output.err &&
116150
test ! -s output &&
117-
test_cmp expect.err output.err
151+
test_cmp typo.err output.err
118152
'
119153

120154
cat > expect <<EOF
121155
boolean: 0
122156
integer: 0
123157
string: (not set)
158+
abbrev: 7
159+
verbose: 0
160+
quiet: no
161+
dry run: no
124162
arg 00: --quux
125163
EOF
126164

@@ -130,4 +168,68 @@ test_expect_success 'keep some options as arguments' '
130168
test_cmp expect output
131169
'
132170

171+
cat > expect <<EOF
172+
boolean: 0
173+
integer: 1
174+
string: default
175+
abbrev: 7
176+
verbose: 0
177+
quiet: yes
178+
dry run: no
179+
arg 00: foo
180+
EOF
181+
182+
test_expect_success 'OPT_DATE() and OPT_SET_PTR() work' '
183+
test-parse-options -t "1970-01-01 00:00:01 +0000" --default-string \
184+
foo -q > output 2> output.err &&
185+
test ! -s output.err &&
186+
test_cmp expect output
187+
'
188+
189+
cat > expect <<EOF
190+
Callback: "four", 0
191+
boolean: 5
192+
integer: 4
193+
string: (not set)
194+
abbrev: 7
195+
verbose: 0
196+
quiet: no
197+
dry run: no
198+
EOF
199+
200+
test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
201+
test-parse-options --length=four -b -4 > output 2> output.err &&
202+
test ! -s output.err &&
203+
test_cmp expect output
204+
'
205+
206+
cat > expect <<EOF
207+
Callback: "not set", 1
208+
EOF
209+
210+
test_expect_success 'OPT_CALLBACK() and callback errors work' '
211+
test_must_fail test-parse-options --no-length > output 2> output.err &&
212+
test_cmp expect output &&
213+
test_cmp expect.err output.err
214+
'
215+
216+
cat > expect <<EOF
217+
boolean: 1
218+
integer: 23
219+
string: (not set)
220+
abbrev: 7
221+
verbose: 0
222+
quiet: no
223+
dry run: no
224+
EOF
225+
226+
test_expect_success 'OPT_BIT() and OPT_SET_INT() work' '
227+
test-parse-options --set23 -bbbbb --no-or4 > output 2> output.err &&
228+
test ! -s output.err &&
229+
test_cmp expect output
230+
'
231+
232+
# --or4
233+
# --no-or4
234+
133235
test_done

test-parse-options.c

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,22 @@
22
#include "parse-options.h"
33

44
static int boolean = 0;
5-
static int integer = 0;
5+
static unsigned long integer = 0;
6+
static int abbrev = 7;
7+
static int verbose = 0, dry_run = 0, quiet = 0;
68
static char *string = NULL;
79

10+
int length_callback(const struct option *opt, const char *arg, int unset)
11+
{
12+
printf("Callback: \"%s\", %d\n",
13+
(arg ? arg : "not set"), unset);
14+
if (unset)
15+
return 1; /* do not support unset */
16+
17+
*(unsigned long *)opt->value = strlen(arg);
18+
return 0;
19+
}
20+
821
int main(int argc, const char **argv)
922
{
1023
const char *usage[] = {
@@ -13,24 +26,42 @@ int main(int argc, const char **argv)
1326
};
1427
struct option options[] = {
1528
OPT_BOOLEAN('b', "boolean", &boolean, "get a boolean"),
29+
OPT_BIT('4', "or4", &boolean,
30+
"bitwise-or boolean with ...0100", 4),
31+
OPT_GROUP(""),
1632
OPT_INTEGER('i', "integer", &integer, "get a integer"),
1733
OPT_INTEGER('j', NULL, &integer, "get a integer, too"),
18-
OPT_GROUP("string options"),
34+
OPT_SET_INT(0, "set23", &integer, "set integer to 23", 23),
35+
OPT_DATE('t', NULL, &integer, "get timestamp of <time>"),
36+
OPT_CALLBACK('L', "length", &integer, "str",
37+
"get length of <str>", length_callback),
38+
OPT_GROUP("String options"),
1939
OPT_STRING('s', "string", &string, "string", "get a string"),
2040
OPT_STRING(0, "string2", &string, "str", "get another string"),
2141
OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"),
2242
OPT_STRING('o', NULL, &string, "str", "get another string"),
23-
OPT_GROUP("magic arguments"),
43+
OPT_SET_PTR(0, "default-string", &string,
44+
"set string to default", (unsigned long)"default"),
45+
OPT_GROUP("Magic arguments"),
2446
OPT_ARGUMENT("quux", "means --quux"),
47+
OPT_GROUP("Standard options"),
48+
OPT__ABBREV(&abbrev),
49+
OPT__VERBOSE(&verbose),
50+
OPT__DRY_RUN(&dry_run),
51+
OPT__QUIET(&quiet),
2552
OPT_END(),
2653
};
2754
int i;
2855

2956
argc = parse_options(argc, argv, options, usage, 0);
3057

3158
printf("boolean: %d\n", boolean);
32-
printf("integer: %d\n", integer);
59+
printf("integer: %lu\n", integer);
3360
printf("string: %s\n", string ? string : "(not set)");
61+
printf("abbrev: %d\n", abbrev);
62+
printf("verbose: %d\n", verbose);
63+
printf("quiet: %s\n", quiet ? "yes" : "no");
64+
printf("dry run: %s\n", dry_run ? "yes" : "no");
3465

3566
for (i = 0; i < argc; i++)
3667
printf("arg %02d: %s\n", i, argv[i]);

0 commit comments

Comments
 (0)