]>
Commit | Line | Data |
---|---|---|
beb47437 JS |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2007 Johannes Schindelin | |
4 | # | |
5 | ||
6 | test_description='our own option parser' | |
7 | ||
8 | . ./test-lib.sh | |
9 | ||
8425b7ea | 10 | cat >expect <<\EOF |
beb47437 JS |
11 | usage: test-parse-options <options> |
12 | ||
b9e63ddd RS |
13 | --yes get a boolean |
14 | -D, --no-doubt begins with 'no-' | |
15 | -B, --no-fear be brave | |
16 | -b, --boolean increment by one | |
010a2dac | 17 | -4, --or4 bitwise-or boolean with ...0100 |
2f4b97f9 | 18 | --neg-or4 same as --no-or4 |
010a2dac | 19 | |
beb47437 JS |
20 | -i, --integer <n> get a integer |
21 | -j <n> get a integer, too | |
2a514ed8 | 22 | -m, --magnitude <n> get a magnitude |
010a2dac SB |
23 | --set23 set integer to 23 |
24 | -t <time> get timestamp of <time> | |
25 | -L, --length <str> get length of <str> | |
41dbcd45 | 26 | -F, --file <file> set file to <file> |
beb47437 | 27 | |
010a2dac | 28 | String options |
beb47437 JS |
29 | -s, --string <string> |
30 | get a string | |
31 | --string2 <str> get another string | |
243e0614 | 32 | --st <st> get another string (pervert ordering) |
3a9f0f41 | 33 | -o <str> get another string |
c8ba1639 | 34 | --list <str> add str to list |
beb47437 | 35 | |
010a2dac | 36 | Magic arguments |
580d5bff | 37 | --quux means --quux |
e0319ff5 | 38 | -NUM set integer to NUM |
51a9949e | 39 | + same as -b |
6bbfd1fa AS |
40 | --ambiguous positive ambiguity |
41 | --no-ambiguous negative ambiguity | |
580d5bff | 42 | |
010a2dac SB |
43 | Standard options |
44 | --abbrev[=<n>] use <n> digits to display SHA-1s | |
45 | -v, --verbose be verbose | |
46 | -n, --dry-run dry run | |
47 | -q, --quiet be quiet | |
ab6b28b0 | 48 | --expect <string> expected output in the variable dump |
010a2dac | 49 | |
beb47437 JS |
50 | EOF |
51 | ||
52 | test_expect_success 'test help' ' | |
8425b7ea | 53 | test_must_fail test-parse-options -h >output 2>output.err && |
ca8d148d | 54 | test_must_be_empty output.err && |
9a001381 | 55 | test_i18ncmp expect output |
beb47437 JS |
56 | ' |
57 | ||
9c7304e3 GS |
58 | mv expect expect.err |
59 | ||
8ca65aeb | 60 | check () { |
b9e63ddd RS |
61 | what="$1" && |
62 | shift && | |
63 | expect="$1" && | |
64 | shift && | |
8ca65aeb | 65 | test-parse-options --expect="$what $expect" "$@" |
b9e63ddd RS |
66 | } |
67 | ||
9a001381 JX |
68 | check_unknown_i18n() { |
69 | case "$1" in | |
70 | --*) | |
71 | echo error: unknown option \`${1#--}\' >expect ;; | |
72 | -*) | |
73 | echo error: unknown switch \`${1#-}\' >expect ;; | |
74 | esac && | |
75 | cat expect.err >>expect && | |
76 | test_must_fail test-parse-options $* >output 2>output.err && | |
ca8d148d | 77 | test_must_be_empty output && |
9a001381 JX |
78 | test_i18ncmp expect output.err |
79 | } | |
80 | ||
b9e63ddd RS |
81 | test_expect_success 'OPT_BOOL() #1' 'check boolean: 1 --yes' |
82 | test_expect_success 'OPT_BOOL() #2' 'check boolean: 1 --no-doubt' | |
83 | test_expect_success 'OPT_BOOL() #3' 'check boolean: 1 -D' | |
84 | test_expect_success 'OPT_BOOL() #4' 'check boolean: 1 --no-fear' | |
85 | test_expect_success 'OPT_BOOL() #5' 'check boolean: 1 -B' | |
86 | ||
87 | test_expect_success 'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes' | |
88 | test_expect_success 'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB' | |
89 | ||
90 | test_expect_success 'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes' | |
91 | test_expect_success 'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt' | |
92 | ||
9a001381 JX |
93 | test_expect_success 'OPT_BOOL() no negation #1' 'check_unknown_i18n --fear' |
94 | test_expect_success 'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear' | |
b9e63ddd | 95 | |
0f1930c5 | 96 | test_expect_success 'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt' |
b9e63ddd | 97 | |
81a48cc0 CB |
98 | test_expect_success 'OPT_INT() negative' 'check integer: -2345 -i -2345' |
99 | ||
2a514ed8 CB |
100 | test_expect_success 'OPT_MAGNITUDE() simple' ' |
101 | check magnitude: 2345678 -m 2345678 | |
102 | ' | |
103 | ||
104 | test_expect_success 'OPT_MAGNITUDE() kilo' ' | |
105 | check magnitude: 239616 -m 234k | |
106 | ' | |
107 | ||
108 | test_expect_success 'OPT_MAGNITUDE() mega' ' | |
109 | check magnitude: 104857600 -m 100m | |
110 | ' | |
111 | ||
112 | test_expect_success 'OPT_MAGNITUDE() giga' ' | |
113 | check magnitude: 1073741824 -m 1g | |
114 | ' | |
115 | ||
116 | test_expect_success 'OPT_MAGNITUDE() 3giga' ' | |
117 | check magnitude: 3221225472 -m 3g | |
118 | ' | |
119 | ||
8425b7ea | 120 | cat >expect <<\EOF |
beb47437 JS |
121 | boolean: 2 |
122 | integer: 1729 | |
2a514ed8 | 123 | magnitude: 16384 |
c4aca9cc | 124 | timestamp: 0 |
beb47437 | 125 | string: 123 |
010a2dac SB |
126 | abbrev: 7 |
127 | verbose: 2 | |
36e6a5ba | 128 | quiet: 0 |
010a2dac | 129 | dry run: yes |
df217ed6 | 130 | file: prefix/my.file |
beb47437 JS |
131 | EOF |
132 | ||
133 | test_expect_success 'short options' ' | |
2a514ed8 CB |
134 | test-parse-options -s123 -b -i 1729 -m 16k -b -vv -n -F my.file \ |
135 | >output 2>output.err && | |
3af82863 | 136 | test_cmp expect output && |
ca8d148d | 137 | test_must_be_empty output.err |
beb47437 | 138 | ' |
010a2dac | 139 | |
8425b7ea | 140 | cat >expect <<\EOF |
beb47437 JS |
141 | boolean: 2 |
142 | integer: 1729 | |
2a514ed8 | 143 | magnitude: 16384 |
c4aca9cc | 144 | timestamp: 0 |
beb47437 | 145 | string: 321 |
010a2dac SB |
146 | abbrev: 10 |
147 | verbose: 2 | |
36e6a5ba | 148 | quiet: 0 |
010a2dac | 149 | dry run: no |
df217ed6 | 150 | file: prefix/fi.le |
beb47437 JS |
151 | EOF |
152 | ||
153 | test_expect_success 'long options' ' | |
2a514ed8 CB |
154 | test-parse-options --boolean --integer 1729 --magnitude 16k \ |
155 | --boolean --string2=321 --verbose --verbose --no-dry-run \ | |
156 | --abbrev=10 --file fi.le --obsolete \ | |
157 | >output 2>output.err && | |
ca8d148d | 158 | test_must_be_empty output.err && |
3af82863 | 159 | test_cmp expect output |
beb47437 JS |
160 | ' |
161 | ||
d5d745f9 | 162 | test_expect_success 'missing required value' ' |
c21fc9d0 JK |
163 | test_expect_code 129 test-parse-options -s && |
164 | test_expect_code 129 test-parse-options --string && | |
165 | test_expect_code 129 test-parse-options --file | |
d5d745f9 OM |
166 | ' |
167 | ||
8425b7ea | 168 | cat >expect <<\EOF |
beb47437 JS |
169 | boolean: 1 |
170 | integer: 13 | |
2a514ed8 | 171 | magnitude: 0 |
c4aca9cc | 172 | timestamp: 0 |
beb47437 | 173 | string: 123 |
010a2dac | 174 | abbrev: 7 |
e0070e8b | 175 | verbose: -1 |
36e6a5ba | 176 | quiet: 0 |
010a2dac | 177 | dry run: no |
df217ed6 | 178 | file: (not set) |
beb47437 JS |
179 | arg 00: a1 |
180 | arg 01: b1 | |
181 | arg 02: --boolean | |
182 | EOF | |
183 | ||
184 | test_expect_success 'intermingled arguments' ' | |
185 | test-parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \ | |
8425b7ea | 186 | >output 2>output.err && |
ca8d148d | 187 | test_must_be_empty output.err && |
3af82863 | 188 | test_cmp expect output |
beb47437 JS |
189 | ' |
190 | ||
8425b7ea | 191 | cat >expect <<\EOF |
7f275b91 JS |
192 | boolean: 0 |
193 | integer: 2 | |
2a514ed8 | 194 | magnitude: 0 |
c4aca9cc | 195 | timestamp: 0 |
7f275b91 | 196 | string: (not set) |
010a2dac | 197 | abbrev: 7 |
e0070e8b | 198 | verbose: -1 |
36e6a5ba | 199 | quiet: 0 |
010a2dac | 200 | dry run: no |
df217ed6 | 201 | file: (not set) |
7f275b91 JS |
202 | EOF |
203 | ||
204 | test_expect_success 'unambiguously abbreviated option' ' | |
8425b7ea | 205 | test-parse-options --int 2 --boolean --no-bo >output 2>output.err && |
ca8d148d | 206 | test_must_be_empty output.err && |
3af82863 | 207 | test_cmp expect output |
7f275b91 JS |
208 | ' |
209 | ||
210 | test_expect_success 'unambiguously abbreviated option with "="' ' | |
86009f32 | 211 | test-parse-options --expect="integer: 2" --int=2 |
7f275b91 JS |
212 | ' |
213 | ||
41ac414e | 214 | test_expect_success 'ambiguously abbreviated option' ' |
c21fc9d0 | 215 | test_expect_code 129 test-parse-options --strin 123 |
7f275b91 JS |
216 | ' |
217 | ||
243e0614 | 218 | test_expect_success 'non ambiguous option (after two options it abbreviates)' ' |
86009f32 | 219 | test-parse-options --expect="string: 123" --st 123 |
243e0614 JS |
220 | ' |
221 | ||
8425b7ea PB |
222 | cat >typo.err <<\EOF |
223 | error: did you mean `--boolean` (with two dashes ?) | |
3a9f0f41 PH |
224 | EOF |
225 | ||
226 | test_expect_success 'detect possible typos' ' | |
8425b7ea | 227 | test_must_fail test-parse-options -boolean >output 2>output.err && |
ca8d148d | 228 | test_must_be_empty output && |
010a2dac | 229 | test_cmp typo.err output.err |
3a9f0f41 PH |
230 | ' |
231 | ||
8425b7ea PB |
232 | cat >typo.err <<\EOF |
233 | error: did you mean `--ambiguous` (with two dashes ?) | |
38916c5b RS |
234 | EOF |
235 | ||
236 | test_expect_success 'detect possible typos' ' | |
8425b7ea | 237 | test_must_fail test-parse-options -ambiguous >output 2>output.err && |
ca8d148d | 238 | test_must_be_empty output && |
38916c5b RS |
239 | test_cmp typo.err output.err |
240 | ' | |
241 | ||
580d5bff | 242 | test_expect_success 'keep some options as arguments' ' |
86009f32 | 243 | test-parse-options --expect="arg 00: --quux" --quux |
580d5bff PH |
244 | ' |
245 | ||
8425b7ea | 246 | cat >expect <<\EOF |
010a2dac | 247 | boolean: 0 |
c4aca9cc | 248 | integer: 0 |
2a514ed8 | 249 | magnitude: 0 |
c4aca9cc | 250 | timestamp: 1 |
20d1c652 | 251 | string: (not set) |
010a2dac | 252 | abbrev: 7 |
e0070e8b | 253 | verbose: -1 |
36e6a5ba | 254 | quiet: 1 |
010a2dac | 255 | dry run: no |
df217ed6 | 256 | file: (not set) |
010a2dac SB |
257 | arg 00: foo |
258 | EOF | |
259 | ||
20d1c652 MR |
260 | test_expect_success 'OPT_DATE() works' ' |
261 | test-parse-options -t "1970-01-01 00:00:01 +0000" \ | |
8425b7ea | 262 | foo -q >output 2>output.err && |
ca8d148d | 263 | test_must_be_empty output.err && |
010a2dac SB |
264 | test_cmp expect output |
265 | ' | |
266 | ||
8425b7ea | 267 | cat >expect <<\EOF |
010a2dac SB |
268 | Callback: "four", 0 |
269 | boolean: 5 | |
270 | integer: 4 | |
2a514ed8 | 271 | magnitude: 0 |
c4aca9cc | 272 | timestamp: 0 |
010a2dac SB |
273 | string: (not set) |
274 | abbrev: 7 | |
e0070e8b | 275 | verbose: -1 |
36e6a5ba | 276 | quiet: 0 |
010a2dac | 277 | dry run: no |
df217ed6 | 278 | file: (not set) |
010a2dac SB |
279 | EOF |
280 | ||
281 | test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' ' | |
8425b7ea | 282 | test-parse-options --length=four -b -4 >output 2>output.err && |
ca8d148d | 283 | test_must_be_empty output.err && |
010a2dac SB |
284 | test_cmp expect output |
285 | ' | |
286 | ||
accac419 | 287 | >expect |
010a2dac SB |
288 | |
289 | test_expect_success 'OPT_CALLBACK() and callback errors work' ' | |
8425b7ea | 290 | test_must_fail test-parse-options --no-length >output 2>output.err && |
9a001381 JX |
291 | test_i18ncmp expect output && |
292 | test_i18ncmp expect.err output.err | |
010a2dac SB |
293 | ' |
294 | ||
8425b7ea | 295 | cat >expect <<\EOF |
010a2dac SB |
296 | boolean: 1 |
297 | integer: 23 | |
2a514ed8 | 298 | magnitude: 0 |
c4aca9cc | 299 | timestamp: 0 |
010a2dac SB |
300 | string: (not set) |
301 | abbrev: 7 | |
e0070e8b | 302 | verbose: -1 |
36e6a5ba | 303 | quiet: 0 |
010a2dac | 304 | dry run: no |
df217ed6 | 305 | file: (not set) |
010a2dac SB |
306 | EOF |
307 | ||
308 | test_expect_success 'OPT_BIT() and OPT_SET_INT() work' ' | |
8425b7ea | 309 | test-parse-options --set23 -bbbbb --no-or4 >output 2>output.err && |
ca8d148d | 310 | test_must_be_empty output.err && |
010a2dac SB |
311 | test_cmp expect output |
312 | ' | |
313 | ||
2f4b97f9 | 314 | test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' ' |
8425b7ea | 315 | test-parse-options --set23 -bbbbb --neg-or4 >output 2>output.err && |
ca8d148d | 316 | test_must_be_empty output.err && |
2f4b97f9 RS |
317 | test_cmp expect output |
318 | ' | |
319 | ||
2f4b97f9 | 320 | test_expect_success 'OPT_BIT() works' ' |
86009f32 | 321 | test-parse-options --expect="boolean: 6" -bb --or4 |
2f4b97f9 RS |
322 | ' |
323 | ||
324 | test_expect_success 'OPT_NEGBIT() works' ' | |
86009f32 | 325 | test-parse-options --expect="boolean: 6" -bb --no-neg-or4 |
2f4b97f9 | 326 | ' |
010a2dac | 327 | |
b9e63ddd | 328 | test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' ' |
86009f32 | 329 | test-parse-options --expect="boolean: 6" + + + + + + |
51a9949e RS |
330 | ' |
331 | ||
e0319ff5 | 332 | test_expect_success 'OPT_NUMBER_CALLBACK() works' ' |
86009f32 | 333 | test-parse-options --expect="integer: 12345" -12345 |
e0319ff5 RS |
334 | ' |
335 | ||
8425b7ea | 336 | cat >expect <<\EOF |
6bbfd1fa AS |
337 | boolean: 0 |
338 | integer: 0 | |
2a514ed8 | 339 | magnitude: 0 |
6bbfd1fa AS |
340 | timestamp: 0 |
341 | string: (not set) | |
342 | abbrev: 7 | |
e0070e8b | 343 | verbose: -1 |
36e6a5ba | 344 | quiet: 0 |
6bbfd1fa AS |
345 | dry run: no |
346 | file: (not set) | |
347 | EOF | |
348 | ||
349 | test_expect_success 'negation of OPT_NONEG flags is not ambiguous' ' | |
350 | test-parse-options --no-ambig >output 2>output.err && | |
ca8d148d | 351 | test_must_be_empty output.err && |
6bbfd1fa AS |
352 | test_cmp expect output |
353 | ' | |
354 | ||
8425b7ea | 355 | cat >>expect <<\EOF |
c8ba1639 JK |
356 | list: foo |
357 | list: bar | |
358 | list: baz | |
359 | EOF | |
360 | test_expect_success '--list keeps list of strings' ' | |
361 | test-parse-options --list foo --list=bar --list=baz >output && | |
362 | test_cmp expect output | |
363 | ' | |
364 | ||
365 | test_expect_success '--no-list resets list' ' | |
366 | test-parse-options --list=other --list=irrelevant --list=options \ | |
367 | --no-list --list=foo --list=bar --list=baz >output && | |
368 | test_cmp expect output | |
369 | ' | |
370 | ||
7d177152 | 371 | test_expect_success 'multiple quiet levels' ' |
86009f32 | 372 | test-parse-options --expect="quiet: 3" -q -q -q |
7d177152 PB |
373 | ' |
374 | ||
7d177152 | 375 | test_expect_success 'multiple verbose levels' ' |
86009f32 | 376 | test-parse-options --expect="verbose: 3" -v -v -v |
7d177152 PB |
377 | ' |
378 | ||
7d177152 | 379 | test_expect_success '--no-quiet sets --quiet to 0' ' |
86009f32 | 380 | test-parse-options --expect="quiet: 0" --no-quiet |
7d177152 PB |
381 | ' |
382 | ||
7d177152 | 383 | test_expect_success '--no-quiet resets multiple -q to 0' ' |
86009f32 | 384 | test-parse-options --expect="quiet: 0" -q -q -q --no-quiet |
7d177152 PB |
385 | ' |
386 | ||
7d177152 | 387 | test_expect_success '--no-verbose sets verbose to 0' ' |
86009f32 | 388 | test-parse-options --expect="verbose: 0" --no-verbose |
7d177152 PB |
389 | ' |
390 | ||
7d177152 | 391 | test_expect_success '--no-verbose resets multiple verbose to 0' ' |
86009f32 | 392 | test-parse-options --expect="verbose: 0" -v -v -v --no-verbose |
7d177152 PB |
393 | ' |
394 | ||
beb47437 | 395 | test_done |