py-cvs-rel2_1 (Rev 1.2) merge
[python/dscho.git] / Lib / test / test_getopt.py
blobf8989f12e67a5f91f666f5787dacc1db793e78cf
1 # test_getopt.py
2 # David Goodger <dgoodger@bigfoot.com> 2000-08-19
4 import getopt
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."""
11 try:
12 exec teststr
13 except expected:
14 pass
15 else:
16 raise failure
18 if verbose:
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)
25 if verbose:
26 print 'Running tests on getopt.long_has_args'
27 has_arg, option = getopt.long_has_args('abc', ['abc='])
28 verify(has_arg)
29 verify(option == 'abc')
30 has_arg, option = getopt.long_has_args('abc', ['abc'])
31 verify(not has_arg)
32 verify(option == 'abc')
33 has_arg, option = getopt.long_has_args('abc', ['abcd'])
34 verify(not has_arg)
35 verify(option == 'abcd')
36 expectException("has_arg, option = getopt.long_has_args('abc', ['def'])",
37 GetoptError)
38 expectException("has_arg, option = getopt.long_has_args('abc', [])",
39 GetoptError)
40 expectException("has_arg, option = " + \
41 "getopt.long_has_args('abc', ['abcd','abcde'])",
42 GetoptError)
44 if verbose:
45 print 'Running tests on getopt.do_shorts'
46 opts, args = getopt.do_shorts([], 'a', 'a', [])
47 verify(opts == [('-a', '')])
48 verify(args == [])
49 opts, args = getopt.do_shorts([], 'a1', 'a:', [])
50 verify(opts == [('-a', '1')])
51 verify(args == [])
52 #opts, args = getopt.do_shorts([], 'a=1', 'a:', [])
53 #verify(opts == [('-a', '1')])
54 #verify(args == [])
55 opts, args = getopt.do_shorts([], 'a', 'a:', ['1'])
56 verify(opts == [('-a', '1')])
57 verify(args == [])
58 opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2'])
59 verify(opts == [('-a', '1')])
60 verify(args == ['2'])
61 expectException("opts, args = getopt.do_shorts([], 'a1', 'a', [])",
62 GetoptError)
63 expectException("opts, args = getopt.do_shorts([], 'a', 'a:', [])",
64 GetoptError)
66 if verbose:
67 print 'Running tests on getopt.do_longs'
68 opts, args = getopt.do_longs([], 'abc', ['abc'], [])
69 verify(opts == [('--abc', '')])
70 verify(args == [])
71 opts, args = getopt.do_longs([], 'abc=1', ['abc='], [])
72 verify(opts == [('--abc', '1')])
73 verify(args == [])
74 opts, args = getopt.do_longs([], 'abc=1', ['abcd='], [])
75 verify(opts == [('--abcd', '1')])
76 verify(args == [])
77 opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], [])
78 verify(opts == [('--abc', '')])
79 verify(args == [])
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')])
85 verify(args == [])
86 expectException("opts, args = getopt.do_longs([], 'abc=1', ['abc'], [])",
87 GetoptError)
88 expectException("opts, args = getopt.do_longs([], 'abc', ['abc='], [])",
89 GetoptError)
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']
96 if verbose:
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'])
105 expectException(
106 "opts, args = getopt.getopt(cmdline, 'a:b', ['alpha', 'beta'])",
107 GetoptError)
109 if verbose:
110 print "Module getopt: tests completed successfully."