t0040: remove unused test helpers
[git/git-svn.git] / t / t0040-parse-options.sh
blob5c8c72aa050087f8e118e910a1c1d6c52276ade8
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_unknown_i18n() {
85 case "$1" in
86 --*)
87 echo error: unknown option \`${1#--}\' >expect ;;
88 -*)
89 echo error: unknown switch \`${1#-}\' >expect ;;
90 esac &&
91 cat expect.err >>expect &&
92 test_must_fail test-parse-options $* >output 2>output.err &&
93 test_must_be_empty output &&
94 test_i18ncmp expect output.err
97 test_expect_success 'OPT_BOOL() #1' 'check boolean: 1 --yes'
98 test_expect_success 'OPT_BOOL() #2' 'check boolean: 1 --no-doubt'
99 test_expect_success 'OPT_BOOL() #3' 'check boolean: 1 -D'
100 test_expect_success 'OPT_BOOL() #4' 'check boolean: 1 --no-fear'
101 test_expect_success 'OPT_BOOL() #5' 'check boolean: 1 -B'
103 test_expect_success 'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes'
104 test_expect_success 'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB'
106 test_expect_success 'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes'
107 test_expect_success 'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt'
109 test_expect_success 'OPT_BOOL() no negation #1' 'check_unknown_i18n --fear'
110 test_expect_success 'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear'
112 test_expect_success 'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt'
114 test_expect_success 'OPT_INT() negative' 'check integer: -2345 -i -2345'
116 test_expect_success 'OPT_MAGNITUDE() simple' '
117 check magnitude: 2345678 -m 2345678
120 test_expect_success 'OPT_MAGNITUDE() kilo' '
121 check magnitude: 239616 -m 234k
124 test_expect_success 'OPT_MAGNITUDE() mega' '
125 check magnitude: 104857600 -m 100m
128 test_expect_success 'OPT_MAGNITUDE() giga' '
129 check magnitude: 1073741824 -m 1g
132 test_expect_success 'OPT_MAGNITUDE() 3giga' '
133 check magnitude: 3221225472 -m 3g
136 cat >expect <<\EOF
137 boolean: 2
138 integer: 1729
139 magnitude: 16384
140 timestamp: 0
141 string: 123
142 abbrev: 7
143 verbose: 2
144 quiet: 0
145 dry run: yes
146 file: prefix/my.file
149 test_expect_success 'short options' '
150 test-parse-options -s123 -b -i 1729 -m 16k -b -vv -n -F my.file \
151 >output 2>output.err &&
152 test_cmp expect output &&
153 test_must_be_empty output.err
156 cat >expect <<\EOF
157 boolean: 2
158 integer: 1729
159 magnitude: 16384
160 timestamp: 0
161 string: 321
162 abbrev: 10
163 verbose: 2
164 quiet: 0
165 dry run: no
166 file: prefix/fi.le
169 test_expect_success 'long options' '
170 test-parse-options --boolean --integer 1729 --magnitude 16k \
171 --boolean --string2=321 --verbose --verbose --no-dry-run \
172 --abbrev=10 --file fi.le --obsolete \
173 >output 2>output.err &&
174 test_must_be_empty output.err &&
175 test_cmp expect output
178 test_expect_success 'missing required value' '
179 test_expect_code 129 test-parse-options -s &&
180 test_expect_code 129 test-parse-options --string &&
181 test_expect_code 129 test-parse-options --file
184 cat >expect <<\EOF
185 boolean: 1
186 integer: 13
187 magnitude: 0
188 timestamp: 0
189 string: 123
190 abbrev: 7
191 verbose: -1
192 quiet: 0
193 dry run: no
194 file: (not set)
195 arg 00: a1
196 arg 01: b1
197 arg 02: --boolean
200 test_expect_success 'intermingled arguments' '
201 test-parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
202 >output 2>output.err &&
203 test_must_be_empty output.err &&
204 test_cmp expect output
207 cat >expect <<\EOF
208 boolean: 0
209 integer: 2
210 magnitude: 0
211 timestamp: 0
212 string: (not set)
213 abbrev: 7
214 verbose: -1
215 quiet: 0
216 dry run: no
217 file: (not set)
220 test_expect_success 'unambiguously abbreviated option' '
221 test-parse-options --int 2 --boolean --no-bo >output 2>output.err &&
222 test_must_be_empty output.err &&
223 test_cmp expect output
226 test_expect_success 'unambiguously abbreviated option with "="' '
227 test-parse-options --int=2 >output 2>output.err &&
228 test_must_be_empty output.err &&
229 test_cmp expect output
232 test_expect_success 'ambiguously abbreviated option' '
233 test_expect_code 129 test-parse-options --strin 123
236 cat >expect <<\EOF
237 boolean: 0
238 integer: 0
239 magnitude: 0
240 timestamp: 0
241 string: 123
242 abbrev: 7
243 verbose: -1
244 quiet: 0
245 dry run: no
246 file: (not set)
249 test_expect_success 'non ambiguous option (after two options it abbreviates)' '
250 test-parse-options --st 123 >output 2>output.err &&
251 test_must_be_empty output.err &&
252 test_cmp expect output
255 cat >typo.err <<\EOF
256 error: did you mean `--boolean` (with two dashes ?)
259 test_expect_success 'detect possible typos' '
260 test_must_fail test-parse-options -boolean >output 2>output.err &&
261 test_must_be_empty output &&
262 test_cmp typo.err output.err
265 cat >typo.err <<\EOF
266 error: did you mean `--ambiguous` (with two dashes ?)
269 test_expect_success 'detect possible typos' '
270 test_must_fail test-parse-options -ambiguous >output 2>output.err &&
271 test_must_be_empty output &&
272 test_cmp typo.err output.err
275 cat >expect <<\EOF
276 boolean: 0
277 integer: 0
278 magnitude: 0
279 timestamp: 0
280 string: (not set)
281 abbrev: 7
282 verbose: -1
283 quiet: 0
284 dry run: no
285 file: (not set)
286 arg 00: --quux
289 test_expect_success 'keep some options as arguments' '
290 test-parse-options --quux >output 2>output.err &&
291 test_must_be_empty output.err &&
292 test_cmp expect output
295 cat >expect <<\EOF
296 boolean: 0
297 integer: 0
298 magnitude: 0
299 timestamp: 1
300 string: (not set)
301 abbrev: 7
302 verbose: -1
303 quiet: 1
304 dry run: no
305 file: (not set)
306 arg 00: foo
309 test_expect_success 'OPT_DATE() works' '
310 test-parse-options -t "1970-01-01 00:00:01 +0000" \
311 foo -q >output 2>output.err &&
312 test_must_be_empty output.err &&
313 test_cmp expect output
316 cat >expect <<\EOF
317 Callback: "four", 0
318 boolean: 5
319 integer: 4
320 magnitude: 0
321 timestamp: 0
322 string: (not set)
323 abbrev: 7
324 verbose: -1
325 quiet: 0
326 dry run: no
327 file: (not set)
330 test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
331 test-parse-options --length=four -b -4 >output 2>output.err &&
332 test_must_be_empty output.err &&
333 test_cmp expect output
336 >expect
338 test_expect_success 'OPT_CALLBACK() and callback errors work' '
339 test_must_fail test-parse-options --no-length >output 2>output.err &&
340 test_i18ncmp expect output &&
341 test_i18ncmp expect.err output.err
344 cat >expect <<\EOF
345 boolean: 1
346 integer: 23
347 magnitude: 0
348 timestamp: 0
349 string: (not set)
350 abbrev: 7
351 verbose: -1
352 quiet: 0
353 dry run: no
354 file: (not set)
357 test_expect_success 'OPT_BIT() and OPT_SET_INT() work' '
358 test-parse-options --set23 -bbbbb --no-or4 >output 2>output.err &&
359 test_must_be_empty output.err &&
360 test_cmp expect output
363 test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' '
364 test-parse-options --set23 -bbbbb --neg-or4 >output 2>output.err &&
365 test_must_be_empty output.err &&
366 test_cmp expect output
369 cat >expect <<\EOF
370 boolean: 6
371 integer: 0
372 magnitude: 0
373 timestamp: 0
374 string: (not set)
375 abbrev: 7
376 verbose: -1
377 quiet: 0
378 dry run: no
379 file: (not set)
382 test_expect_success 'OPT_BIT() works' '
383 test-parse-options -bb --or4 >output 2>output.err &&
384 test_must_be_empty output.err &&
385 test_cmp expect output
388 test_expect_success 'OPT_NEGBIT() works' '
389 test-parse-options -bb --no-neg-or4 >output 2>output.err &&
390 test_must_be_empty output.err &&
391 test_cmp expect output
394 test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
395 test-parse-options + + + + + + >output 2>output.err &&
396 test_must_be_empty output.err &&
397 test_cmp expect output
400 cat >expect <<\EOF
401 boolean: 0
402 integer: 12345
403 magnitude: 0
404 timestamp: 0
405 string: (not set)
406 abbrev: 7
407 verbose: -1
408 quiet: 0
409 dry run: no
410 file: (not set)
413 test_expect_success 'OPT_NUMBER_CALLBACK() works' '
414 test-parse-options -12345 >output 2>output.err &&
415 test_must_be_empty output.err &&
416 test_cmp expect output
419 cat >expect <<\EOF
420 boolean: 0
421 integer: 0
422 magnitude: 0
423 timestamp: 0
424 string: (not set)
425 abbrev: 7
426 verbose: -1
427 quiet: 0
428 dry run: no
429 file: (not set)
432 test_expect_success 'negation of OPT_NONEG flags is not ambiguous' '
433 test-parse-options --no-ambig >output 2>output.err &&
434 test_must_be_empty output.err &&
435 test_cmp expect output
438 cat >>expect <<\EOF
439 list: foo
440 list: bar
441 list: baz
443 test_expect_success '--list keeps list of strings' '
444 test-parse-options --list foo --list=bar --list=baz >output &&
445 test_cmp expect output
448 test_expect_success '--no-list resets list' '
449 test-parse-options --list=other --list=irrelevant --list=options \
450 --no-list --list=foo --list=bar --list=baz >output &&
451 test_cmp expect output
454 cat >expect <<\EOF
455 boolean: 0
456 integer: 0
457 magnitude: 0
458 timestamp: 0
459 string: (not set)
460 abbrev: 7
461 verbose: -1
462 quiet: 3
463 dry run: no
464 file: (not set)
467 test_expect_success 'multiple quiet levels' '
468 test-parse-options -q -q -q >output 2>output.err &&
469 test_must_be_empty output.err &&
470 test_cmp expect output
473 cat >expect <<\EOF
474 boolean: 0
475 integer: 0
476 magnitude: 0
477 timestamp: 0
478 string: (not set)
479 abbrev: 7
480 verbose: 3
481 quiet: 0
482 dry run: no
483 file: (not set)
486 test_expect_success 'multiple verbose levels' '
487 test-parse-options -v -v -v >output 2>output.err &&
488 test_must_be_empty output.err &&
489 test_cmp expect output
492 cat >expect <<\EOF
493 boolean: 0
494 integer: 0
495 magnitude: 0
496 timestamp: 0
497 string: (not set)
498 abbrev: 7
499 verbose: -1
500 quiet: 0
501 dry run: no
502 file: (not set)
505 test_expect_success '--no-quiet sets --quiet to 0' '
506 test-parse-options --no-quiet >output 2>output.err &&
507 test_must_be_empty output.err &&
508 test_cmp expect output
511 cat >expect <<\EOF
512 boolean: 0
513 integer: 0
514 magnitude: 0
515 timestamp: 0
516 string: (not set)
517 abbrev: 7
518 verbose: -1
519 quiet: 0
520 dry run: no
521 file: (not set)
524 test_expect_success '--no-quiet resets multiple -q to 0' '
525 test-parse-options -q -q -q --no-quiet >output 2>output.err &&
526 test_must_be_empty output.err &&
527 test_cmp expect output
530 cat >expect <<\EOF
531 boolean: 0
532 integer: 0
533 magnitude: 0
534 timestamp: 0
535 string: (not set)
536 abbrev: 7
537 verbose: 0
538 quiet: 0
539 dry run: no
540 file: (not set)
543 test_expect_success '--no-verbose sets verbose to 0' '
544 test-parse-options --no-verbose >output 2>output.err &&
545 test_must_be_empty output.err &&
546 test_cmp expect output
549 cat >expect <<\EOF
550 boolean: 0
551 integer: 0
552 magnitude: 0
553 timestamp: 0
554 string: (not set)
555 abbrev: 7
556 verbose: 0
557 quiet: 0
558 dry run: no
559 file: (not set)
562 test_expect_success '--no-verbose resets multiple verbose to 0' '
563 test-parse-options -v -v -v --no-verbose >output 2>output.err &&
564 test_must_be_empty output.err &&
565 test_cmp expect output
568 test_done