2 # David Goodger <dgoodger@bigfoot.com> 2000-08-19
5 from getopt
import GetoptError
6 from test
.test_support
import verify
, verbose
9 def expectException(teststr
, expected
, failure
=AssertionError):
10 """Executes a statement passed in teststr, and raises an exception
11 (failure) if the expected exception is *not* raised."""
20 print 'Running tests on getopt.short_has_arg'
21 verify(getopt
.short_has_arg('a', 'a:'))
22 verify(not getopt
.short_has_arg('a', 'a'))
23 expectException("tmp = getopt.short_has_arg('a', 'b')", GetoptError
)
24 expectException("tmp = getopt.short_has_arg('a', '')", GetoptError
)
27 print 'Running tests on getopt.long_has_args'
28 has_arg
, option
= getopt
.long_has_args('abc', ['abc='])
30 verify(option
== 'abc')
31 has_arg
, option
= getopt
.long_has_args('abc', ['abc'])
33 verify(option
== 'abc')
34 has_arg
, option
= getopt
.long_has_args('abc', ['abcd'])
36 verify(option
== 'abcd')
37 expectException("has_arg, option = getopt.long_has_args('abc', ['def'])",
39 expectException("has_arg, option = getopt.long_has_args('abc', [])",
41 expectException("has_arg, option = " + \
42 "getopt.long_has_args('abc', ['abcd','abcde'])",
46 print 'Running tests on getopt.do_shorts'
47 opts
, args
= getopt
.do_shorts([], 'a', 'a', [])
48 verify(opts
== [('-a', '')])
50 opts
, args
= getopt
.do_shorts([], 'a1', 'a:', [])
51 verify(opts
== [('-a', '1')])
53 #opts, args = getopt.do_shorts([], 'a=1', 'a:', [])
54 #verify(opts == [('-a', '1')])
56 opts
, args
= getopt
.do_shorts([], 'a', 'a:', ['1'])
57 verify(opts
== [('-a', '1')])
59 opts
, args
= getopt
.do_shorts([], 'a', 'a:', ['1', '2'])
60 verify(opts
== [('-a', '1')])
62 expectException("opts, args = getopt.do_shorts([], 'a1', 'a', [])",
64 expectException("opts, args = getopt.do_shorts([], 'a', 'a:', [])",
68 print 'Running tests on getopt.do_longs'
69 opts
, args
= getopt
.do_longs([], 'abc', ['abc'], [])
70 verify(opts
== [('--abc', '')])
72 opts
, args
= getopt
.do_longs([], 'abc=1', ['abc='], [])
73 verify(opts
== [('--abc', '1')])
75 opts
, args
= getopt
.do_longs([], 'abc=1', ['abcd='], [])
76 verify(opts
== [('--abcd', '1')])
78 opts
, args
= getopt
.do_longs([], 'abc', ['ab', 'abc', 'abcd'], [])
79 verify(opts
== [('--abc', '')])
81 # Much like the preceding, except with a non-alpha character ("-") in
82 # option name that precedes "="; failed in
83 # http://sourceforge.net/bugs/?func=detailbug&bug_id=126863&group_id=5470
84 opts
, args
= getopt
.do_longs([], 'foo=42', ['foo-bar', 'foo=',], [])
85 verify(opts
== [('--foo', '42')])
87 expectException("opts, args = getopt.do_longs([], 'abc=1', ['abc'], [])",
89 expectException("opts, args = getopt.do_longs([], 'abc', ['abc='], [])",
92 # note: the empty string between '-a' and '--beta' is significant:
93 # it simulates an empty string option argument ('-a ""') on the command line.
94 cmdline
= ['-a', '1', '-b', '--alpha=2', '--beta', '-a', '3', '-a', '',
95 '--beta', 'arg1', 'arg2']
98 print 'Running tests on getopt.getopt'
99 opts
, args
= getopt
.getopt(cmdline
, 'a:b', ['alpha=', 'beta'])
100 verify(opts
== [('-a', '1'), ('-b', ''), ('--alpha', '2'), ('--beta', ''),
101 ('-a', '3'), ('-a', ''), ('--beta', '')] )
102 # Note ambiguity of ('-b', '') and ('-a', '') above. This must be
103 # accounted for in the code that calls getopt().
104 verify(args
== ['arg1', 'arg2'])
107 "opts, args = getopt.getopt(cmdline, 'a:b', ['alpha', 'beta'])",
110 # Test handling of GNU style scanning mode.
112 print 'Running tests on getopt.gnu_getopt'
113 cmdline
= ['-a', 'arg1', '-b', '1', '--alpha', '--beta=2']
115 opts
, args
= getopt
.gnu_getopt(cmdline
, 'ab:', ['alpha', 'beta='])
116 verify(opts
== [('-a', ''), ('-b', '1'), ('--alpha', ''), ('--beta', '2')])
117 verify(args
== ['arg1'])
119 opts
, args
= getopt
.gnu_getopt(cmdline
, '+ab:', ['alpha', 'beta='])
120 verify(opts
== [('-a', '')])
121 verify(args
== ['arg1', '-b', '1', '--alpha', '--beta=2'])
122 # Posix style via POSIXLY_CORRECT
123 os
.environ
["POSIXLY_CORRECT"] = "1"
124 opts
, args
= getopt
.gnu_getopt(cmdline
, 'ab:', ['alpha', 'beta='])
125 verify(opts
== [('-a', '')])
126 verify(args
== ['arg1', '-b', '1', '--alpha', '--beta=2'])
130 print "Module getopt: tests completed successfully."