blob: 1580a79f2521d4a1c9549c80299a19ba85d6ae93 [file] [log] [blame]
Tim Peters571bb8f2000-08-20 04:18:40 +00001# test_getopt.py
2# David Goodger <dgoodger@bigfoot.com> 2000-08-19
3
4import getopt
5from getopt import GetoptError
6from test.test_support import verbose
7
8def 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
17
18if verbose:
19 print 'Running tests on getopt.short_has_arg'
20assert getopt.short_has_arg('a', 'a:')
21assert not getopt.short_has_arg('a', 'a')
22expectException("tmp = getopt.short_has_arg('a', 'b')", GetoptError)
23expectException("tmp = getopt.short_has_arg('a', '')", GetoptError)
24
25if verbose:
26 print 'Running tests on getopt.long_has_args'
27has_arg, option = getopt.long_has_args('abc', ['abc='])
28assert has_arg
29assert option == 'abc'
30has_arg, option = getopt.long_has_args('abc', ['abc'])
31assert not has_arg
32assert option == 'abc'
33has_arg, option = getopt.long_has_args('abc', ['abcd'])
34assert not has_arg
35assert option == 'abcd'
36expectException("has_arg, option = getopt.long_has_args('abc', ['def'])",
37 GetoptError)
38expectException("has_arg, option = getopt.long_has_args('abc', [])",
39 GetoptError)
40expectException("has_arg, option = " + \
41 "getopt.long_has_args('abc', ['abcd','abcde'])",
42 GetoptError)
43
44if verbose:
45 print 'Running tests on getopt.do_shorts'
46opts, args = getopt.do_shorts([], 'a', 'a', [])
47assert opts == [('-a', '')]
48assert args == []
49opts, args = getopt.do_shorts([], 'a1', 'a:', [])
50assert opts == [('-a', '1')]
51assert args == []
52#opts, args = getopt.do_shorts([], 'a=1', 'a:', [])
53#assert opts == [('-a', '1')]
54#assert args == []
55opts, args = getopt.do_shorts([], 'a', 'a:', ['1'])
56assert opts == [('-a', '1')]
57assert args == []
58opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2'])
59assert opts == [('-a', '1')]
60assert args == ['2']
61expectException("opts, args = getopt.do_shorts([], 'a1', 'a', [])",
62 GetoptError)
63expectException("opts, args = getopt.do_shorts([], 'a', 'a:', [])",
64 GetoptError)
65
66if verbose:
67 print 'Running tests on getopt.do_longs'
68opts, args = getopt.do_longs([], 'abc', ['abc'], [])
69assert opts == [('--abc', '')]
70assert args == []
71opts, args = getopt.do_longs([], 'abc=1', ['abc='], [])
72assert opts == [('--abc', '1')]
73assert args == []
74opts, args = getopt.do_longs([], 'abc=1', ['abcd='], [])
75assert opts == [('--abcd', '1')]
76assert args == []
77expectException("opts, args = getopt.do_longs([], 'abc=1', ['abc'], [])",
78 GetoptError)
79expectException("opts, args = getopt.do_longs([], 'abc', ['abc='], [])",
80 GetoptError)
81
82# note: the empty string between '-a' and '--beta' is significant:
83# it simulates an empty string option argument ('-a ""') on the command line.
84cmdline = ['-a', '1', '-b', '--alpha=2', '--beta', '-a', '3', '-a', '',
85 '--beta', 'arg1', 'arg2']
86
87if verbose:
88 print 'Running tests on getopt.getopt'
89opts, args = getopt.getopt(cmdline, 'a:b', ['alpha=', 'beta'])
90assert opts == [('-a', '1'), ('-b', ''), ('--alpha', '2'), ('--beta', ''),
91 ('-a', '3'), ('-a', ''), ('--beta', '')]
92# Note ambiguity of ('-b', '') and ('-a', '') above. This must be
93# accounted for in the code that calls getopt().
94assert args == ['arg1', 'arg2']
95
96expectException(
97 "opts, args = getopt.getopt(cmdline, 'a:b', ['alpha', 'beta'])",
98 GetoptError)
99
100if verbose:
101 print "Module getopt: tests completed successfully."