2 # David Goodger <dgoodger@bigfoot.com> 2000-08-19
5 from getopt
import GetoptError
6 from test_support
import verify
, verbose
8 def expectException(teststr
, expected
, failure
=AssertionError):
9 """Executes a statement passed in teststr, and raises an exception
10 (failure) if the expected exception is *not* raised."""
19 print 'Running tests on getopt.short_has_arg'
20 verify(getopt
.short_has_arg('a', 'a:'))
21 verify(not getopt
.short_has_arg('a', 'a'))
22 expectException("tmp = getopt.short_has_arg('a', 'b')", GetoptError
)
23 expectException("tmp = getopt.short_has_arg('a', '')", GetoptError
)
26 print 'Running tests on getopt.long_has_args'
27 has_arg
, option
= getopt
.long_has_args('abc', ['abc='])
29 verify(option
== 'abc')
30 has_arg
, option
= getopt
.long_has_args('abc', ['abc'])
32 verify(option
== 'abc')
33 has_arg
, option
= getopt
.long_has_args('abc', ['abcd'])
35 verify(option
== 'abcd')
36 expectException("has_arg, option = getopt.long_has_args('abc', ['def'])",
38 expectException("has_arg, option = getopt.long_has_args('abc', [])",
40 expectException("has_arg, option = " + \
41 "getopt.long_has_args('abc', ['abcd','abcde'])",
45 print 'Running tests on getopt.do_shorts'
46 opts
, args
= getopt
.do_shorts([], 'a', 'a', [])
47 verify(opts
== [('-a', '')])
49 opts
, args
= getopt
.do_shorts([], 'a1', 'a:', [])
50 verify(opts
== [('-a', '1')])
52 #opts, args = getopt.do_shorts([], 'a=1', 'a:', [])
53 #verify(opts == [('-a', '1')])
55 opts
, args
= getopt
.do_shorts([], 'a', 'a:', ['1'])
56 verify(opts
== [('-a', '1')])
58 opts
, args
= getopt
.do_shorts([], 'a', 'a:', ['1', '2'])
59 verify(opts
== [('-a', '1')])
61 expectException("opts, args = getopt.do_shorts([], 'a1', 'a', [])",
63 expectException("opts, args = getopt.do_shorts([], 'a', 'a:', [])",
67 print 'Running tests on getopt.do_longs'
68 opts
, args
= getopt
.do_longs([], 'abc', ['abc'], [])
69 verify(opts
== [('--abc', '')])
71 opts
, args
= getopt
.do_longs([], 'abc=1', ['abc='], [])
72 verify(opts
== [('--abc', '1')])
74 opts
, args
= getopt
.do_longs([], 'abc=1', ['abcd='], [])
75 verify(opts
== [('--abcd', '1')])
77 opts
, args
= getopt
.do_longs([], 'abc', ['ab', 'abc', 'abcd'], [])
78 verify(opts
== [('--abc', '')])
80 # Much like the preceding, except with a non-alpha character ("-") in
81 # option name that precedes "="; failed in
82 # http://sourceforge.net/bugs/?func=detailbug&bug_id=126863&group_id=5470
83 opts
, args
= getopt
.do_longs([], 'foo=42', ['foo-bar', 'foo=',], [])
84 verify(opts
== [('--foo', '42')])
86 expectException("opts, args = getopt.do_longs([], 'abc=1', ['abc'], [])",
88 expectException("opts, args = getopt.do_longs([], 'abc', ['abc='], [])",
91 # note: the empty string between '-a' and '--beta' is significant:
92 # it simulates an empty string option argument ('-a ""') on the command line.
93 cmdline
= ['-a', '1', '-b', '--alpha=2', '--beta', '-a', '3', '-a', '',
94 '--beta', 'arg1', 'arg2']
97 print 'Running tests on getopt.getopt'
98 opts
, args
= getopt
.getopt(cmdline
, 'a:b', ['alpha=', 'beta'])
99 verify(opts
== [('-a', '1'), ('-b', ''), ('--alpha', '2'), ('--beta', ''),
100 ('-a', '3'), ('-a', ''), ('--beta', '')] )
101 # Note ambiguity of ('-b', '') and ('-a', '') above. This must be
102 # accounted for in the code that calls getopt().
103 verify(args
== ['arg1', 'arg2'])
106 "opts, args = getopt.getopt(cmdline, 'a:b', ['alpha', 'beta'])",
110 print "Module getopt: tests completed successfully."