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