| 1 | # test_getopt.py
|
|---|
| 2 | # David Goodger <[email protected]> 2000-08-19
|
|---|
| 3 |
|
|---|
| 4 | import getopt
|
|---|
| 5 | from getopt import GetoptError
|
|---|
| 6 | from test.test_support import verify, verbose, run_doctest
|
|---|
| 7 | import os
|
|---|
| 8 |
|
|---|
| 9 | def 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 |
|
|---|
| 19 | old_posixly_correct = os.environ.get("POSIXLY_CORRECT")
|
|---|
| 20 | if old_posixly_correct is not None:
|
|---|
| 21 | del os.environ["POSIXLY_CORRECT"]
|
|---|
| 22 |
|
|---|
| 23 | if verbose:
|
|---|
| 24 | print 'Running tests on getopt.short_has_arg'
|
|---|
| 25 | verify(getopt.short_has_arg('a', 'a:'))
|
|---|
| 26 | verify(not getopt.short_has_arg('a', 'a'))
|
|---|
| 27 | expectException("tmp = getopt.short_has_arg('a', 'b')", GetoptError)
|
|---|
| 28 | expectException("tmp = getopt.short_has_arg('a', '')", GetoptError)
|
|---|
| 29 |
|
|---|
| 30 | if verbose:
|
|---|
| 31 | print 'Running tests on getopt.long_has_args'
|
|---|
| 32 | has_arg, option = getopt.long_has_args('abc', ['abc='])
|
|---|
| 33 | verify(has_arg)
|
|---|
| 34 | verify(option == 'abc')
|
|---|
| 35 | has_arg, option = getopt.long_has_args('abc', ['abc'])
|
|---|
| 36 | verify(not has_arg)
|
|---|
| 37 | verify(option == 'abc')
|
|---|
| 38 | has_arg, option = getopt.long_has_args('abc', ['abcd'])
|
|---|
| 39 | verify(not has_arg)
|
|---|
| 40 | verify(option == 'abcd')
|
|---|
| 41 | expectException("has_arg, option = getopt.long_has_args('abc', ['def'])",
|
|---|
| 42 | GetoptError)
|
|---|
| 43 | expectException("has_arg, option = getopt.long_has_args('abc', [])",
|
|---|
| 44 | GetoptError)
|
|---|
| 45 | expectException("has_arg, option = " + \
|
|---|
| 46 | "getopt.long_has_args('abc', ['abcd','abcde'])",
|
|---|
| 47 | GetoptError)
|
|---|
| 48 |
|
|---|
| 49 | if verbose:
|
|---|
| 50 | print 'Running tests on getopt.do_shorts'
|
|---|
| 51 | opts, args = getopt.do_shorts([], 'a', 'a', [])
|
|---|
| 52 | verify(opts == [('-a', '')])
|
|---|
| 53 | verify(args == [])
|
|---|
| 54 | opts, args = getopt.do_shorts([], 'a1', 'a:', [])
|
|---|
| 55 | verify(opts == [('-a', '1')])
|
|---|
| 56 | verify(args == [])
|
|---|
|
|---|