Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / lib / python2.4 / test / test_getopt.py
CommitLineData
920dae64
AT
1# test_getopt.py
2# David Goodger <dgoodger@bigfoot.com> 2000-08-19
3
4import getopt
5from getopt import GetoptError
6from test.test_support import verify, verbose, run_doctest
7import os
8
9def 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
18
19old_posixly_correct = os.environ.get("POSIXLY_CORRECT")
20if old_posixly_correct is not None:
21 del os.environ["POSIXLY_CORRECT"]
22
23if verbose:
24 print 'Running tests on getopt.short_has_arg'
25verify(getopt.short_has_arg('a', 'a:'))
26verify(not getopt.short_has_arg('a', 'a'))
27expectException("tmp = getopt.short_has_arg('a', 'b')", GetoptError)
28expectException("tmp = getopt.short_has_arg('a', '')", GetoptError)
29
30if verbose:
31 print 'Running tests on getopt.long_has_args'
32has_arg, option = getopt.long_has_args('abc', ['abc='])
33verify(has_arg)
34verify(option == 'abc')
35has_arg, option = getopt.long_has_args('abc', ['abc'])
36verify(not has_arg)
37verify(option == 'abc')
38has_arg, option = getopt.long_has_args('abc', ['abcd'])
39verify(not has_arg)
40verify(option == 'abcd')
41expectException("has_arg, option = getopt.long_has_args('abc', ['def'])",
42 GetoptError)
43expectException("has_arg, option = getopt.long_has_args('abc', [])",
44 GetoptError)
45expectException("has_arg, option = " + \
46 "getopt.long_has_args('abc', ['abcd','abcde'])",
47 GetoptError)
48
49if verbose:
50 print 'Running tests on getopt.do_shorts'
51opts, args = getopt.do_shorts([], 'a', 'a', [])
52verify(opts == [('-a', '')])
53verify(args == [])
54opts, args = getopt.do_shorts([], 'a1', 'a:', [])
55verify(opts == [('-a', '1')])
56verify(args == [])
57#opts, args = getopt.do_shorts([], 'a=1', 'a:', [])
58#verify(opts == [('-a', '1')])
59#verify(args == [])
60opts, args = getopt.do_shorts([], 'a', 'a:', ['1'])
61verify(opts == [('-a', '1')])
62verify(args == [])
63opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2'])
64verify(opts == [('-a', '1')])
65verify(args == ['2'])
66expectException("opts, args = getopt.do_shorts([], 'a1', 'a', [])",
67 GetoptError)
68expectException("opts, args = getopt.do_shorts([], 'a', 'a:', [])",
69 GetoptError)
70
71if verbose:
72 print 'Running tests on getopt.do_longs'
73opts, args = getopt.do_longs([], 'abc', ['abc'], [])
74verify(opts == [('--abc', '')])
75verify(args == [])
76opts, args = getopt.do_longs([], 'abc=1', ['abc='], [])
77verify(opts == [('--abc', '1')])
78verify(args == [])
79opts, args = getopt.do_longs([], 'abc=1', ['abcd='], [])
80verify(opts == [('--abcd', '1')])
81verify(args == [])
82opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], [])
83verify(opts == [('--abc', '')])
84verify(args == [])
85# Much like the preceding, except with a non-alpha character ("-") in
86# option name that precedes "="; failed in
87# http://sourceforge.net/bugs/?func=detailbug&bug_id=126863&group_id=5470
88opts, args = getopt.do_longs([], 'foo=42', ['foo-bar', 'foo=',], [])
89verify(opts == [('--foo', '42')])
90verify(args == [])
91expectException("opts, args = getopt.do_longs([], 'abc=1', ['abc'], [])",
92 GetoptError)
93expectException("opts, args = getopt.do_longs([], 'abc', ['abc='], [])",
94 GetoptError)
95
96# note: the empty string between '-a' and '--beta' is significant:
97# it simulates an empty string option argument ('-a ""') on the command line.
98cmdline = ['-a', '1', '-b', '--alpha=2', '--beta', '-a', '3', '-a', '',
99 '--beta', 'arg1', 'arg2']
100
101if verbose:
102 print 'Running tests on getopt.getopt'
103opts, args = getopt.getopt(cmdline, 'a:b', ['alpha=', 'beta'])
104verify(opts == [('-a', '1'), ('-b', ''), ('--alpha', '2'), ('--beta', ''),
105 ('-a', '3'), ('-a', ''), ('--beta', '')] )
106# Note ambiguity of ('-b', '') and ('-a', '') above. This must be
107# accounted for in the code that calls getopt().
108verify(args == ['arg1', 'arg2'])
109
110expectException(
111 "opts, args = getopt.getopt(cmdline, 'a:b', ['alpha', 'beta'])",
112 GetoptError)
113
114# Test handling of GNU style scanning mode.
115if verbose:
116 print 'Running tests on getopt.gnu_getopt'
117cmdline = ['-a', 'arg1', '-b', '1', '--alpha', '--beta=2']
118# GNU style
119opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta='])
120verify(opts == [('-a', ''), ('-b', '1'), ('--alpha', ''), ('--beta', '2')])
121verify(args == ['arg1'])
122# Posix style via +
123opts, args = getopt.gnu_getopt(cmdline, '+ab:', ['alpha', 'beta='])
124verify(opts == [('-a', '')])
125verify(args == ['arg1', '-b', '1', '--alpha', '--beta=2'])
126# Posix style via POSIXLY_CORRECT
127os.environ["POSIXLY_CORRECT"] = "1"
128opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta='])
129verify(opts == [('-a', '')])
130verify(args == ['arg1', '-b', '1', '--alpha', '--beta=2'])
131
132
133if old_posixly_correct is None:
134 del os.environ["POSIXLY_CORRECT"]
135else:
136 os.environ["POSIXLY_CORRECT"] = old_posixly_correct
137
138#------------------------------------------------------------------------------
139
140libreftest = """
141Examples from the Library Reference: Doc/lib/libgetopt.tex
142
143An example using only Unix style options:
144
145
146>>> import getopt
147>>> args = '-a -b -cfoo -d bar a1 a2'.split()
148>>> args
149['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
150>>> optlist, args = getopt.getopt(args, 'abc:d:')
151>>> optlist
152[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
153>>> args
154['a1', 'a2']
155
156Using long option names is equally easy:
157
158
159>>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
160>>> args = s.split()
161>>> args
162['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
163>>> optlist, args = getopt.getopt(args, 'x', [
164... 'condition=', 'output-file=', 'testing'])
165>>> optlist
166[('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')]
167>>> args
168['a1', 'a2']
169
170"""
171
172__test__ = {'libreftest' : libreftest}
173
174import sys
175run_doctest(sys.modules[__name__], verbose)
176
177#------------------------------------------------------------------------------
178
179if verbose:
180 print "Module getopt: tests completed successfully."