Bump version number to 2.4.2 to pick up the latest minor bug fixes.
[python/dscho.git] / Lib / test / test_getopt.py
blob70ea65b4b0d62c60a3bb9746168b19da24881fbd
1 # test_getopt.py
2 # David Goodger <dgoodger@bigfoot.com> 2000-08-19
4 import getopt
5 from getopt import GetoptError
6 from test.test_support import verify, verbose
7 import os
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."""
12 try:
13 exec teststr
14 except expected:
15 pass
16 else:
17 raise failure
19 if verbose:
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)
26 if verbose:
27 print 'Running tests on getopt.long_has_args'
28 has_arg, option = getopt.long_has_args('abc', ['abc='])
29 verify(has_arg)
30 verify(option == 'abc')
31 has_arg, option = getopt.long_has_args('abc', ['abc'])
32 verify(not has_arg)
33 verify(option == 'abc')
34 has_arg, option = getopt.long_has_args('abc', ['abcd'])
35 verify(not has_arg)
36 verify(option == 'abcd')
37 expectException("has_arg, option = getopt.long_has_args('abc', ['def'])",
38 GetoptError)
39 expectException("has_arg, option = getopt.long_has_args('abc', [])",
40 GetoptError)
41 expectException("has_arg, option = " + \
42 "getopt.long_has_args('abc', ['abcd','abcde'])",
43 GetoptError)
45 if verbose:
46 print 'Running tests on getopt.do_shorts'
47 opts, args = getopt.do_shorts([], 'a', 'a', [])
48 verify(opts == [('-a', '')])
49 verify(args == [])
50 opts, args = getopt.do_shorts([], 'a1', 'a:', [])
51 verify(opts == [('-a', '1')])
52 verify(args == [])
53 #opts, args = getopt.do_shorts([], 'a=1', 'a:', [])
54 #verify(opts == [('-a', '1')])
55 #verify(args == [])
56 opts, args = getopt.do_shorts([], 'a', 'a:', ['1'])
57 verify(opts == [('-a', '1')])
58 verify(args == [])
59 opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2'])
60 verify(opts == [('-a', '1')])
61 verify(args == ['2'])
62 expectException("opts, args = getopt.do_shorts([], 'a1', 'a', [])",
63 GetoptError)
64 expectException("opts, args = getopt.do_shorts([], 'a', 'a:', [])",
65 GetoptError)
67 if verbose:
68 print 'Running tests on getopt.do_longs'
69 opts, args = getopt.do_longs([], 'abc', ['abc'], [])
70 verify(opts == [('--abc', '')])
71 verify(args == [])
72 opts, args = getopt.do_longs([], 'abc=1', ['abc='], [])
73 verify(opts == [('--abc', '1')])
74 verify(args == [])
75 opts, args = getopt.do_longs([], 'abc=1', ['abcd='], [])
76 verify(opts == [('--abcd', '1')])
77 verify(args == [])
78 opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], [])
79 verify(opts == [('--abc', '')])
80 verify(args == [])
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')])
86 verify(args == [])
87 expectException("opts, args = getopt.do_longs([], 'abc=1', ['abc'], [])",
88 GetoptError)
89 expectException("opts, args = getopt.do_longs([], 'abc', ['abc='], [])",
90 GetoptError)
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']
97 if verbose:
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'])
106 expectException(
107 "opts, args = getopt.getopt(cmdline, 'a:b', ['alpha', 'beta'])",
108 GetoptError)
110 # Test handling of GNU style scanning mode.
111 if verbose:
112 print 'Running tests on getopt.gnu_getopt'
113 cmdline = ['-a', 'arg1', '-b', '1', '--alpha', '--beta=2']
114 # GNU style
115 opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta='])
116 verify(opts == [('-a', ''), ('-b', '1'), ('--alpha', ''), ('--beta', '2')])
117 verify(args == ['arg1'])
118 # Posix style via +
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'])
129 if verbose:
130 print "Module getopt: tests completed successfully."