source: trunk/essentials/dev-lang/python/Lib/test/test_getopt.py@ 3951

Last change on this file since 3951 was 3225, checked in by bird, 19 years ago

Python 2.5

File size: 6.0 KB
Line 
1# test_getopt.py
2# David Goodger <[email protected]> 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 == [])