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