2 # Copyright (C) 2001-2010, Parrot Foundation.
7 t/library/getopt_obj.t - testing the PIR module Getopt::Obj
11 % prove t/library/getopt_obj.t
15 This test program tries to handle command line arguments with the
16 module F<runtime/parrot/library/Getopt/Obj.pir>.
21 .include 'test_more.pir'
22 load_bytecode "Getopt/Obj.pbc"
25 test_basic_long_options()
26 test_basic_short_options()
28 test_mixing_long_and_short_with_array()
30 test_bundling_short_options()
31 test_ignored_options()
32 test_double_dash_stop()
39 test_missing_argument()
43 .sub test_basic_long_options
45 argv = new 'ResizablePMCArray'
46 push argv, '--foo=bar'
47 push argv, '--bar=3.14'
54 getopts = new ['Getopt';'Obj']
76 $P1 = getopts."get_options"(argv)
79 is($S0, 'bar', 'basic long options')
82 is($S0, 3.14, 'basic long options')
85 is($S0, 3, 'basic long options')
88 is($S0, 1, 'basic long options')
91 is($S0, 'frobz', 'basic long options')
95 .sub test_basic_short_options
97 argv = new 'ResizablePMCArray'
104 getopts = new ['Getopt';'Obj']
106 $P0 = getopts."add"()
110 $P0 = getopts."add"()
114 $P0 = getopts."add"()
117 $P1 = getopts."get_options"(argv)
120 is($S0, 'bar', 'basic short options')
123 is($S0, 'bc', 'basic short options')
126 is($S0, '1', 'basic short options')
130 .sub test_simple_array
132 argv = new 'ResizablePMCArray'
137 getopts = new ['Getopt';'Obj']
139 $P0 = getopts."add"()
143 $P1 = getopts."get_options"(argv)
146 is($S0, 'inca', 'simple array')
149 is($S0, 'incb', 'simple array')
154 .sub test_mixing_long_and_short_with_array
156 argv = new 'ResizablePMCArray'
158 push argv, '--include=incb'
161 getopts = new ['Getopt';'Obj']
163 $P0 = getopts."add"()
164 $P0."long"("include")
168 $P1 = getopts."get_options"(argv)
170 $S0 = $P1["include";0]
171 is($S0, 'inca', 'mixing long and short with array')
173 $S0 = $P1["include";1]
174 is($S0, 'incb', 'mixing long and short with array')
181 argv = new 'ResizablePMCArray'
182 push argv, '-Dfoo=bar'
183 push argv, '--define=bax=baz'
184 push argv, '-Dfoobar'
187 getopts = new ['Getopt';'Obj']
189 $P0 = getopts."add"()
194 $P1 = getopts."get_options"(argv)
196 $S0 = $P1["define";"foo"]
197 is($S0, 'bar', 'hash')
199 $S0 = $P1["define";"bax"]
200 is($S0, 'baz', 'hash')
202 $S0 = $P1["define";"foobar"]
208 .sub test_bundling_short_options
210 argv = new 'ResizablePMCArray'
214 getopts = new ['Getopt';'Obj']
216 $P0 = getopts."add"()
218 $P0 = getopts."add"()
220 $P0 = getopts."add"()
223 $P1 = getopts."get_options"(argv)
226 is($S0, 1, 'bundling short options')
229 is($S0, 1, 'bundling short options')
232 is($S0, 1, 'bundling short options')
237 .sub test_ignored_options
239 argv = new 'ResizablePMCArray'
240 push argv, '--ignore'
244 getopts = new ['Getopt';'Obj']
245 getopts."notOptStop"(1)
247 $P0 = getopts."add"()
250 $P1 = getopts."get_options"(argv)
253 is($S0, '1', 'ignored options')
256 is($S0, '--ignore', 'ignored options')
261 .sub test_double_dash_stop
263 argv = new 'ResizablePMCArray'
269 getopts = new ['Getopt';'Obj']
271 $P0 = getopts."add"()
273 $P0 = getopts."add"()
276 $P1 = getopts."get_options"(argv)
279 is($S0, 1, 'double dash stop')
281 # Hash sets an nonexistant value to ''
283 is($S0, '', 'double dash stop')
286 is($S0, '--bar', 'double dash stop')
293 argv = new 'ResizablePMCArray'
299 getopts = new ['Getopt';'Obj']
300 getopts."notOptStop"(1)
302 $P0 = getopts."add"()
304 $P0 = getopts."add"()
307 $P1 = getopts."get_options"(argv)
310 is($S0, 1, 'notOptStop')
313 is($S0, '', 'notOptStop')
316 is($S0, 'foo', 'notOptStop')
319 is($S0, '--bar', 'notOptStop')
326 argv = new 'ResizablePMCArray'
332 getopts = new ['Getopt';'Obj']
333 getopts."notOptStop"(1)
335 $P0 = getopts."add"()
340 $P0 = getopts."add"()
345 $P0 = getopts."add"()
350 $P1 = getopts."get_options"(argv)
353 is($S0, '', 'optarg')
356 is($S0, '', 'optarg')
359 is($S0, 'bar', 'optarg')
364 .sub test_pass_through
366 argv = new 'ResizablePMCArray'
373 getopts = new ['Getopt';'Obj']
375 $P0 = getopts."add"()
377 $P0 = getopts."add"()
380 $P1 = getopts."get_options"(argv)
383 is($S0, 1, 'pass through')
386 is($S0, 1, 'pass through')
389 is($S0, 'foo', 'pass through')
392 is($S0, 'bar', 'pass through')
399 argv = new 'ResizablePMCArray'
405 getopts = new ['Getopt';'Obj']
407 $P0 = getopts."add"()
409 $P0 = getopts."add"()
412 $P1 = getopts."get_options"(argv)
415 is($S0, 1, 'lone dash')
418 is($S0, 1, 'lone dash')
421 is($S0, '-', 'lone dash')
426 .sub test_push_interface
428 argv = new 'ResizablePMCArray'
429 push argv, '--foo=file'
430 push argv, '-bfile.txt'
439 push argv, '-Dfoo=bar'
440 push argv, '--define=bax=baz'
441 push argv, '-Dfoobar'
444 getopts = new ['Getopt';'Obj']
446 push getopts, 'foo=s'
447 push getopts, 'bar|b=s'
448 push getopts, 'bax|x=s'
449 push getopts, 'baz|z:s'
451 push getopts, 'define|D:%'
453 $P1 = getopts."get_options"(argv)
456 is($S0, 'file', 'push interface')
459 is($S0, 'file.txt', 'push interface')
462 is($S0, 'file.t', 'push interface')
465 is($S0, '', 'push interface')
468 is($S0, 'texta', 'push interface')
471 is($S0, 'textb', 'push interface')
473 $S0 = $P1["define";"foo"]
474 is($S0, 'bar', 'push interface')
476 $S0 = $P1["define";"bax"]
477 is($S0, 'baz', 'push interface')
479 $S0 = $P1["define";"foobar"]
480 is($S0, 1, 'push interface')
483 is($S0, 'text', 'push interface')
488 .sub test_missing_spec
489 dies_ok(<<'CODE', 'missing_spec')
492 argv = new 'ResizablePMCArray'
493 push argv, '--foo=file'
494 load_bytecode "Getopt/Obj.pbc"
496 getopts = new ['Getopt';'Obj']
497 $P1 = getopts."get_options"(argv)
511 .sub test_missing_argument
512 dies_ok(<<'CODE', "missing argument")
515 argv = new 'ResizablePMCArray'
516 push argv, '--foo=file'
518 load_bytecode "Getopt/Obj.pbc"
520 getopts = new ['Getopt';'Obj']
521 push getopts, 'foo=s'
522 push getopts, 'bar=s'
523 $P1 = getopts."get_options"(argv)
538 Joshua Isom - C<loneowl@ritalin.shout.net>
542 F<runtime/parrot/library/Getopt/Obj.pir>
550 # vim: expandtab shiftwidth=4 ft=pir: