3 # Copyright (c) 2007 Johannes Schindelin
6 test_description
='our own option parser'
10 cat > expect.err
<< EOF
11 usage: test-parse-options <options>
13 -b, --boolean get a boolean
14 -4, --or4 bitwise-or boolean with ...0100
15 --neg-or4 same as --no-or4
17 -i, --integer <n> get a integer
18 -j <n> get a integer, too
19 --set23 set integer to 23
20 -t <time> get timestamp of <time>
21 -L, --length <str> get length of <str>
26 --string2 <str> get another string
27 --st <st> get another string (pervert ordering)
28 -o <str> get another string
29 --default-string set string to default
35 --abbrev[=<n>] use <n> digits to display SHA-1s
36 -v, --verbose be verbose
42 test_expect_success
'test help' '
43 test_must_fail test-parse-options -h > output 2> output.err &&
45 test_cmp expect.err output.err
59 test_expect_success
'short options' '
60 test-parse-options -s123 -b -i 1729 -b -vv -n > output 2> output.err &&
61 test_cmp expect output &&
76 test_expect_success
'long options' '
77 test-parse-options --boolean --integer 1729 --boolean --string2=321 \
78 --verbose --verbose --no-dry-run --abbrev=10 \
79 > output 2> output.err &&
80 test ! -s output.err &&
81 test_cmp expect output
84 test_expect_success
'missing required value' '
85 test-parse-options -s;
87 test-parse-options --string;
105 test_expect_success
'intermingled arguments' '
106 test-parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
107 > output 2> output.err &&
108 test ! -s output.err &&
109 test_cmp expect output
123 test_expect_success
'unambiguously abbreviated option' '
124 test-parse-options --int 2 --boolean --no-bo > output 2> output.err &&
125 test ! -s output.err &&
126 test_cmp expect output
129 test_expect_success
'unambiguously abbreviated option with "="' '
130 test-parse-options --int=2 > output 2> output.err &&
131 test ! -s output.err &&
132 test_cmp expect output
135 test_expect_success
'ambiguously abbreviated option' '
136 test-parse-options --strin 123;
151 test_expect_success
'non ambiguous option (after two options it abbreviates)' '
152 test-parse-options --st 123 > output 2> output.err &&
153 test ! -s output.err &&
154 test_cmp expect output
157 cat > typo.err
<< EOF
158 error: did you mean \`--boolean\` (with two dashes ?)
161 test_expect_success
'detect possible typos' '
162 test_must_fail test-parse-options -boolean > output 2> output.err &&
164 test_cmp typo.err output.err
179 test_expect_success
'keep some options as arguments' '
180 test-parse-options --quux > output 2> output.err &&
181 test ! -s output.err &&
182 test_cmp expect output
197 test_expect_success
'OPT_DATE() and OPT_SET_PTR() work' '
198 test-parse-options -t "1970-01-01 00:00:01 +0000" --default-string \
199 foo -q > output 2> output.err &&
200 test ! -s output.err &&
201 test_cmp expect output
216 test_expect_success
'OPT_CALLBACK() and OPT_BIT() work' '
217 test-parse-options --length=four -b -4 > output 2> output.err &&
218 test ! -s output.err &&
219 test_cmp expect output
223 Callback: "not set", 1
226 test_expect_success
'OPT_CALLBACK() and callback errors work' '
227 test_must_fail test-parse-options --no-length > output 2> output.err &&
228 test_cmp expect output &&
229 test_cmp expect.err output.err
243 test_expect_success
'OPT_BIT() and OPT_SET_INT() work' '
244 test-parse-options --set23 -bbbbb --no-or4 > output 2> output.err &&
245 test ! -s output.err &&
246 test_cmp expect output
249 test_expect_success
'OPT_NEGBIT() and OPT_SET_INT() work' '
250 test-parse-options --set23 -bbbbb --neg-or4 > output 2> output.err &&
251 test ! -s output.err &&
252 test_cmp expect output
266 test_expect_success
'OPT_BIT() works' '
267 test-parse-options -bb --or4 > output 2> output.err &&
268 test ! -s output.err &&
269 test_cmp expect output
272 test_expect_success
'OPT_NEGBIT() works' '
273 test-parse-options -bb --no-neg-or4 > output 2> output.err &&
274 test ! -s output.err &&
275 test_cmp expect output