test-parse-options: --expect=<string> option to simplify tests
[git.git] / t / t0040-parse-options.sh
blobd678fbf1b781c2d9920a54ef45013e321883dc97
1 #!/bin/sh
3 # Copyright (c) 2007 Johannes Schindelin
6 test_description='our own option parser'
8 . ./test-lib.sh
10 cat >expect <<\EOF
11 usage: test-parse-options <options>
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
20 -i, --integer <n> get a integer
21 -j <n> get a integer, too
22 -m, --magnitude <n> get a magnitude
23 --set23 set integer to 23
24 -t <time> get timestamp of <time>
25 -L, --length <str> get length of <str>
26 -F, --file <file> set file to <file>
28 String options
29 -s, --string <string>
30 get a string
31 --string2 <str> get another string
32 --st <st> get another string (pervert ordering)
33 -o <str> get another string
34 --list <str> add str to list
36 Magic arguments
37 --quux means --quux
38 -NUM set integer to NUM
39 + same as -b
40 --ambiguous positive ambiguity
41 --no-ambiguous negative ambiguity
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
48 --expect <string> expected output in the variable dump
50 EOF
52 test_expect_success 'test help' '
53 test_must_fail test-parse-options -h >output 2>output.err &&
54 test_must_be_empty output.err &&
55 test_i18ncmp expect output
58 mv expect expect.err
60 cat >expect.template <<\EOF
61 boolean: 0
62 integer: 0
63 magnitude: 0
64 timestamp: 0
65 string: (not set)
66 abbrev: 7
67 verbose: -1
68 quiet: 0
69 dry run: no
70 file: (not set)
71 EOF
73 check() {
74 what="$1" &&
75 shift &&
76 expect="$1" &&
77 shift &&
78 sed "s/^$what .*/$what $expect/" <expect.template >expect &&
79 test-parse-options $* >output 2>output.err &&
80 test_must_be_empty output.err &&
81 test_cmp expect output
84 check_i18n() {
85 what="$1" &&
86 shift &&
87 expect="$1" &&
88 shift &&
89 sed "s/^$what .*/$what $expect/" <expect.template >expect &&
90 test-parse-options $* >output 2>output.err &&
91 test_must_be_empty output.err &&
92 test_i18ncmp expect output
95 check_unknown() {
96 case "$1" in
97 --*)
98 echo error: unknown option \`${1#--}\' >expect ;;
99 -*)
100 echo error: unknown switch \`${1#-}\' >expect ;;
101 esac &&
102 cat expect.err >>expect &&
103 test_must_fail test-parse-options $* >output 2>output.err &&
104 test_must_be_empty output &&
105 test_cmp expect output.err
108 check_unknown_i18n() {
109 case "$1" in
110 --*)
111 echo error: unknown option \`${1#--}\' >expect ;;
113 echo error: unknown switch \`${1#-}\' >expect ;;
114 esac &&
115 cat expect.err >>expect &&
116 test_must_fail test-parse-options $* >output 2>output.err &&
117 test_must_be_empty output &&
118 test_i18ncmp expect output.err
121 test_expect_success 'OPT_BOOL() #1' 'check boolean: 1 --yes'
122 test_expect_success 'OPT_BOOL() #2' 'check boolean: 1 --no-doubt'
123 test_expect_success 'OPT_BOOL() #3' 'check boolean: 1 -D'
124 test_expect_success 'OPT_BOOL() #4' 'check boolean: 1 --no-fear'
125 test_expect_success 'OPT_BOOL() #5' 'check boolean: 1 -B'
127 test_expect_success 'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes'
128 test_expect_success 'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB'
130 test_expect_success 'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes'
131 test_expect_success 'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt'
133 test_expect_success 'OPT_BOOL() no negation #1' 'check_unknown_i18n --fear'
134 test_expect_success 'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear'
136 test_expect_success 'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt'
138 test_expect_success 'OPT_INT() negative' 'check integer: -2345 -i -2345'
140 test_expect_success 'OPT_MAGNITUDE() simple' '
141 check magnitude: 2345678 -m 2345678
144 test_expect_success 'OPT_MAGNITUDE() kilo' '
145 check magnitude: 239616 -m 234k
148 test_expect_success 'OPT_MAGNITUDE() mega' '
149 check magnitude: 104857600 -m 100m
152 test_expect_success 'OPT_MAGNITUDE() giga' '
153 check magnitude: 1073741824 -m 1g
156 test_expect_success 'OPT_MAGNITUDE() 3giga' '
157 check magnitude: 3221225472 -m 3g
160 cat >expect <<\EOF
161 boolean: 2
162 integer: 1729
163 magnitude: 16384
164 timestamp: 0
165 string: 123
166 abbrev: 7
167 verbose: 2
168 quiet: 0
169 dry run: yes
170 file: prefix/my.file
173 test_expect_success 'short options' '
174 test-parse-options -s123 -b -i 1729 -m 16k -b -vv -n -F my.file \
175 >output 2>output.err &&
176 test_cmp expect output &&
177 test_must_be_empty output.err
180 cat >expect <<\EOF
181 boolean: 2
182 integer: 1729
183 magnitude: 16384
184 timestamp: 0
185 string: 321
186 abbrev: 10
187 verbose: 2
188 quiet: 0
189 dry run: no
190 file: prefix/fi.le
193 test_expect_success 'long options' '
194 test-parse-options --boolean --integer 1729 --magnitude 16k \
195 --boolean --string2=321 --verbose --verbose --no-dry-run \
196 --abbrev=10 --file fi.le --obsolete \
197 >output 2>output.err &&
198 test_must_be_empty output.err &&
199 test_cmp expect output
202 test_expect_success 'missing required value' '
203 test_expect_code 129 test-parse-options -s &&
204 test_expect_code 129 test-parse-options --string &&
205 test_expect_code 129 test-parse-options --file
208 cat >expect <<\EOF
209 boolean: 1
210 integer: 13
211 magnitude: 0
212 timestamp: 0
213 string: 123
214 abbrev: 7
215 verbose: -1
216 quiet: 0
217 dry run: no
218 file: (not set)
219 arg 00: a1
220 arg 01: b1
221 arg 02: --boolean
224 test_expect_success 'intermingled arguments' '
225 test-parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
226 >output 2>output.err &&
227 test_must_be_empty output.err &&
228 test_cmp expect output
231 cat >expect <<\EOF
232 boolean: 0
233 integer: 2
234 magnitude: 0
235 timestamp: 0
236 string: (not set)
237 abbrev: 7
238 verbose: -1
239 quiet: 0
240 dry run: no
241 file: (not set)
244 test_expect_success 'unambiguously abbreviated option' '
245 test-parse-options --int 2 --boolean --no-bo >output 2>output.err &&
246 test_must_be_empty output.err &&
247 test_cmp expect output
250 test_expect_success 'unambiguously abbreviated option with "="' '
251 test-parse-options --int=2 >output 2>output.err &&
252 test_must_be_empty output.err &&
253 test_cmp expect output
256 test_expect_success 'ambiguously abbreviated option' '
257 test_expect_code 129 test-parse-options --strin 123
260 cat >expect <<\EOF
261 boolean: 0
262 integer: 0
263 magnitude: 0
264 timestamp: 0
265 string: 123
266 abbrev: 7
267 verbose: -1
268 quiet: 0
269 dry run: no
270 file: (not set)
273 test_expect_success 'non ambiguous option (after two options it abbreviates)' '
274 test-parse-options --st 123 >output 2>output.err &&
275 test_must_be_empty output.err &&
276 test_cmp expect output
279 cat >typo.err <<\EOF
280 error: did you mean `--boolean` (with two dashes ?)
283 test_expect_success 'detect possible typos' '
284 test_must_fail test-parse-options -boolean >output 2>output.err &&
285 test_must_be_empty output &&
286 test_cmp typo.err output.err
289 cat >typo.err <<\EOF
290 error: did you mean `--ambiguous` (with two dashes ?)
293 test_expect_success 'detect possible typos' '
294 test_must_fail test-parse-options -ambiguous >output 2>output.err &&
295 test_must_be_empty output &&
296 test_cmp typo.err output.err
299 cat >expect <<\EOF
300 boolean: 0
301 integer: 0
302 magnitude: 0
303 timestamp: 0
304 string: (not set)
305 abbrev: 7
306 verbose: -1
307 quiet: 0
308 dry run: no
309 file: (not set)
310 arg 00: --quux
313 test_expect_success 'keep some options as arguments' '
314 test-parse-options --quux >output 2>output.err &&
315 test_must_be_empty output.err &&
316 test_cmp expect output
319 cat >expect <<\EOF
320 boolean: 0
321 integer: 0
322 magnitude: 0
323 timestamp: 1
324 string: (not set)
325 abbrev: 7
326 verbose: -1
327 quiet: 1
328 dry run: no
329 file: (not set)
330 arg 00: foo
333 test_expect_success 'OPT_DATE() works' '
334 test-parse-options -t "1970-01-01 00:00:01 +0000" \
335 foo -q >output 2>output.err &&
336 test_must_be_empty output.err &&
337 test_cmp expect output
340 cat >expect <<\EOF
341 Callback: "four", 0
342 boolean: 5
343 integer: 4
344 magnitude: 0
345 timestamp: 0
346 string: (not set)
347 abbrev: 7
348 verbose: -1
349 quiet: 0
350 dry run: no
351 file: (not set)
354 test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
355 test-parse-options --length=four -b -4 >output 2>output.err &&
356 test_must_be_empty output.err &&
357 test_cmp expect output
360 >expect
362 test_expect_success 'OPT_CALLBACK() and callback errors work' '
363 test_must_fail test-parse-options --no-length >output 2>output.err &&
364 test_i18ncmp expect output &&
365 test_i18ncmp expect.err output.err
368 cat >expect <<\EOF
369 boolean: 1
370 integer: 23
371 magnitude: 0
372 timestamp: 0
373 string: (not set)
374 abbrev: 7
375 verbose: -1
376 quiet: 0
377 dry run: no
378 file: (not set)
381 test_expect_success 'OPT_BIT() and OPT_SET_INT() work' '
382 test-parse-options --set23 -bbbbb --no-or4 >output 2>output.err &&
383 test_must_be_empty output.err &&
384 test_cmp expect output
387 test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' '
388 test-parse-options --set23 -bbbbb --neg-or4 >output 2>output.err &&
389 test_must_be_empty output.err &&
390 test_cmp expect output
393 cat >expect <<\EOF
394 boolean: 6
395 integer: 0
396 magnitude: 0
397 timestamp: 0
398 string: (not set)
399 abbrev: 7
400 verbose: -1
401 quiet: 0
402 dry run: no
403 file: (not set)
406 test_expect_success 'OPT_BIT() works' '
407 test-parse-options -bb --or4 >output 2>output.err &&
408 test_must_be_empty output.err &&
409 test_cmp expect output
412 test_expect_success 'OPT_NEGBIT() works' '
413 test-parse-options -bb --no-neg-or4 >output 2>output.err &&
414 test_must_be_empty output.err &&
415 test_cmp expect output
418 test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
419 test-parse-options + + + + + + >output 2>output.err &&
420 test_must_be_empty output.err &&
421 test_cmp expect output
424 cat >expect <<\EOF
425 boolean: 0
426 integer: 12345
427 magnitude: 0
428 timestamp: 0
429 string: (not set)
430 abbrev: 7
431 verbose: -1
432 quiet: 0
433 dry run: no
434 file: (not set)
437 test_expect_success 'OPT_NUMBER_CALLBACK() works' '
438 test-parse-options -12345 >output 2>output.err &&
439 test_must_be_empty output.err &&
440 test_cmp expect output
443 cat >expect <<\EOF
444 boolean: 0
445 integer: 0
446 magnitude: 0
447 timestamp: 0
448 string: (not set)
449 abbrev: 7
450 verbose: -1
451 quiet: 0
452 dry run: no
453 file: (not set)
456 test_expect_success 'negation of OPT_NONEG flags is not ambiguous' '
457 test-parse-options --no-ambig >output 2>output.err &&
458 test_must_be_empty output.err &&
459 test_cmp expect output
462 cat >>expect <<\EOF
463 list: foo
464 list: bar
465 list: baz
467 test_expect_success '--list keeps list of strings' '
468 test-parse-options --list foo --list=bar --list=baz >output &&
469 test_cmp expect output
472 test_expect_success '--no-list resets list' '
473 test-parse-options --list=other --list=irrelevant --list=options \
474 --no-list --list=foo --list=bar --list=baz >output &&
475 test_cmp expect output
478 cat >expect <<\EOF
479 boolean: 0
480 integer: 0
481 magnitude: 0
482 timestamp: 0
483 string: (not set)
484 abbrev: 7
485 verbose: -1
486 quiet: 3
487 dry run: no
488 file: (not set)
491 test_expect_success 'multiple quiet levels' '
492 test-parse-options -q -q -q >output 2>output.err &&
493 test_must_be_empty output.err &&
494 test_cmp expect output
497 cat >expect <<\EOF
498 boolean: 0
499 integer: 0
500 magnitude: 0
501 timestamp: 0
502 string: (not set)
503 abbrev: 7
504 verbose: 3
505 quiet: 0
506 dry run: no
507 file: (not set)
510 test_expect_success 'multiple verbose levels' '
511 test-parse-options -v -v -v >output 2>output.err &&
512 test_must_be_empty output.err &&
513 test_cmp expect output
516 cat >expect <<\EOF
517 boolean: 0
518 integer: 0
519 magnitude: 0
520 timestamp: 0
521 string: (not set)
522 abbrev: 7
523 verbose: -1
524 quiet: 0
525 dry run: no
526 file: (not set)
529 test_expect_success '--no-quiet sets --quiet to 0' '
530 test-parse-options --no-quiet >output 2>output.err &&
531 test_must_be_empty output.err &&
532 test_cmp expect output
535 cat >expect <<\EOF
536 boolean: 0
537 integer: 0
538 magnitude: 0
539 timestamp: 0
540 string: (not set)
541 abbrev: 7
542 verbose: -1
543 quiet: 0
544 dry run: no
545 file: (not set)
548 test_expect_success '--no-quiet resets multiple -q to 0' '
549 test-parse-options -q -q -q --no-quiet >output 2>output.err &&
550 test_must_be_empty output.err &&
551 test_cmp expect output
554 cat >expect <<\EOF
555 boolean: 0
556 integer: 0
557 magnitude: 0
558 timestamp: 0
559 string: (not set)
560 abbrev: 7
561 verbose: 0
562 quiet: 0
563 dry run: no
564 file: (not set)
567 test_expect_success '--no-verbose sets verbose to 0' '
568 test-parse-options --no-verbose >output 2>output.err &&
569 test_must_be_empty output.err &&
570 test_cmp expect output
573 cat >expect <<\EOF
574 boolean: 0
575 integer: 0
576 magnitude: 0
577 timestamp: 0
578 string: (not set)
579 abbrev: 7
580 verbose: 0
581 quiet: 0
582 dry run: no
583 file: (not set)
586 test_expect_success '--no-verbose resets multiple verbose to 0' '
587 test-parse-options -v -v -v --no-verbose >output 2>output.err &&
588 test_must_be_empty output.err &&
589 test_cmp expect output
592 test_done