blob: f8989f12e67a5f91f666f5787dacc1db793e78cf [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
Marc-André Lemburg36619082001-01-17 19:11:13 +00006from test_support import verify, 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'
Marc-André Lemburg36619082001-01-17 19:11:13 +000020verify(getopt.short_has_arg('a', 'a:'))
21verify(not getopt.short_has_arg('a', 'a'))
Tim Peters571bb8f2000-08-20 04:18:40 +000022expectException("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='])
Marc-André Lemburg36619082001-01-17 19:11:13 +000028verify(has_arg)
29verify(option == 'abc')
Tim Peters571bb8f2000-08-20 04:18:40 +000030has_arg, option = getopt.long_has_args('abc', ['abc'])
Marc-André Lemburg36619082001-01-17 19:11:13 +000031verify(not has_arg)
32verify(option == 'abc')
Tim Peters571bb8f2000-08-20 04:18:40 +000033has_arg, option = getopt.long_has_args('abc', ['abcd'])
Marc-André Lemburg36619082001-01-17 19:11:13 +000034verify(not has_arg)
35verify(option == 'abcd')
Tim Peters571bb8f2000-08-20 04:18:40 +000036expectException("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', [])
Marc-André Lemburg36619082001-01-17 19:11:13 +000047verify(opts == [('-a', '')])
48verify(args == [])
Tim Peters571bb8f2000-08-20 04:18:40 +000049opts, args = getopt.do_shorts([], 'a1', 'a:', [])
Marc-André Lemburg36619082001-01-17 19:11:13 +000050verify(opts == [('-a', '1')])
51verify(args == [])
Tim Peters571bb8f2000-08-20 04:18:40 +000052#opts, args = getopt.do_shorts([], 'a=1', 'a:', [])
Marc-André Lemburg36619082001-01-17 19:11:13 +000053#verify(opts == [('-a', '1')])
54#verify(args == [])
Tim Peters571bb8f2000-08-20 04:18:40 +000055opts, args = getopt.do_shorts([], 'a', 'a:', ['1'])
Marc-André Lemburg36619082001-01-17 19:11:13 +000056verify(opts == [('-a', '1')])
57verify(args == [])
Tim Peters571bb8f2000-08-20 04:18:40 +000058opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2'])
Marc-André Lemburg36619082001-01-17 19:11:13 +000059verify(opts == [('-a', '1')])
60verify(args == ['2'])
Tim Peters571bb8f2000-08-20 04:18:40 +000061expectException("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'], [])
Marc-André Lemburg36619082001-01-17 19:11:13 +000069verify(opts == [('--abc', '')])
70verify(args == [])
Tim Peters571bb8f2000-08-20 04:18:40 +000071opts, args = getopt.do_longs([], 'abc=1', ['abc='], [])
Marc-André Lemburg36619082001-01-17 19:11:13 +000072verify(opts == [('--abc', '1')])
73verify(args == [])
Tim Peters571bb8f2000-08-20 04:18:40 +000074opts, args = getopt.do_longs([], 'abc=1', ['abcd='], [])
Marc-André Lemburg36619082001-01-17 19:11:13 +000075verify(opts == [('--abcd', '1')])
76verify(args == [])
Tim Petersda7bf4e2000-12-27 08:03:20 +000077opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], [])
Marc-André Lemburg36619082001-01-17 19:11:13 +000078verify(opts == [('--abc', '')])
79verify(args == [])
Tim Petersda7bf4e2000-12-27 08:03:20 +000080# 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=',], [])
Marc-André Lemburg36619082001-01-17 19:11:13 +000084verify(opts == [('--foo', '42')])
85verify(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'])
Marc-André Lemburg36619082001-01-17 19:11:13 +000099verify(opts == [('-a', '1'), ('-b', ''), ('--alpha', '2'), ('--beta', ''),
100 ('-a', '3'), ('-a', ''), ('--beta', '')] )
Tim Peters571bb8f2000-08-20 04:18:40 +0000101# Note ambiguity of ('-b', '') and ('-a', '') above. This must be
102# accounted for in the code that calls getopt().
Marc-André Lemburg36619082001-01-17 19:11:13 +0000103verify(args == ['arg1', 'arg2'])
Tim Peters571bb8f2000-08-20 04:18:40 +0000104
105expectException(
106 "opts, args = getopt.getopt(cmdline, 'a:b', ['alpha', 'beta'])",
107 GetoptError)
108
109if verbose:
110 print "Module getopt: tests completed successfully."