blob: 146020059354cbb99e78ddb068a4e6fc3f7d2298 [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
Guido van Rossumff18b802000-08-21 22:59:29 +00006from test_support import verbose
Tim Peters571bb8f2000-08-20 04:18:40 +00007
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 == []
Tim Petersda7bf4e2000-12-27 08:03:20 +000077opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], [])
78assert opts == [('--abc', '')]
79assert 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
83opts, args = getopt.do_longs([], 'foo=42', ['foo-bar', 'foo=',], [])
84assert opts == [('--foo', '42')]
85assert args == []
Tim Peters571bb8f2000-08-20 04:18:40 +000086expectException("opts, args = getopt.do_longs([], 'abc=1', ['abc'], [])",
87 GetoptError)
88expectException("opts, args = getopt.do_longs([], 'abc', ['abc='], [])",
89 GetoptError)
90
91# note: the empty string between '-a' and '--beta' is significant:
92# it simulates an empty string option argument ('-a ""') on the command line.
93cmdline = ['-a', '1', '-b', '--alpha=2', '--beta', '-a', '3', '-a', '',
94 '--beta', 'arg1', 'arg2']
95
96if verbose:
97 print 'Running tests on getopt.getopt'
98opts, args = getopt.getopt(cmdline, 'a:b', ['alpha=', 'beta'])
99assert 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().
103assert args == ['arg1', 'arg2']
104
105expectException(
106 "opts, args = getopt.getopt(cmdline, 'a:b', ['alpha', 'beta'])",
107 GetoptError)
108
109if verbose:
110 print "Module getopt: tests completed successfully."