diff options
author | Tim Peters <tim.peters@gmail.com> | 2000-08-20 04:18:40 (GMT) |
---|---|---|
committer | Tim Peters <tim.peters@gmail.com> | 2000-08-20 04:18:40 (GMT) |
commit | 571bb8fc7276a89f90465dfda5fbf1528656bc3e (patch) | |
tree | c81ca254f26a19a5489c5d9192d3f47e4141bdf9 /Lib/test/test_getopt.py | |
parent | 8bad61288186aab93b8aaf6b229c07211e1c77d7 (diff) | |
download | cpython-571bb8fc7276a89f90465dfda5fbf1528656bc3e.zip cpython-571bb8fc7276a89f90465dfda5fbf1528656bc3e.tar.gz cpython-571bb8fc7276a89f90465dfda5fbf1528656bc3e.tar.bz2 |
David Goodger's new getopt test module (thanks, David!).
https://sourceforge.net/patch/?func=detailpatch&patch_id=101110&group_id=5470
Accepted as-is, except for purging an "import *".
Diffstat (limited to 'Lib/test/test_getopt.py')
-rw-r--r-- | Lib/test/test_getopt.py | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/Lib/test/test_getopt.py b/Lib/test/test_getopt.py new file mode 100644 index 0000000..1580a79 --- /dev/null +++ b/Lib/test/test_getopt.py @@ -0,0 +1,101 @@ +# test_getopt.py +# David Goodger <dgoodger@bigfoot.com> 2000-08-19 + +import getopt +from getopt import GetoptError +from test.test_support import verbose + +def expectException(teststr, expected, failure=AssertionError): + """Executes a statement passed in teststr, and raises an exception + (failure) if the expected exception is *not* raised.""" + try: + exec teststr + except expected: + pass + else: + raise failure + +if verbose: + print 'Running tests on getopt.short_has_arg' +assert getopt.short_has_arg('a', 'a:') +assert not getopt.short_has_arg('a', 'a') +expectException("tmp = getopt.short_has_arg('a', 'b')", GetoptError) +expectException("tmp = getopt.short_has_arg('a', '')", GetoptError) + +if verbose: + print 'Running tests on getopt.long_has_args' +has_arg, option = getopt.long_has_args('abc', ['abc=']) +assert has_arg +assert option == 'abc' +has_arg, option = getopt.long_has_args('abc', ['abc']) +assert not has_arg +assert option == 'abc' +has_arg, option = getopt.long_has_args('abc', ['abcd']) +assert not has_arg +assert option == 'abcd' +expectException("has_arg, option = getopt.long_has_args('abc', ['def'])", + GetoptError) +expectException("has_arg, option = getopt.long_has_args('abc', [])", + GetoptError) +expectException("has_arg, option = " + \ + "getopt.long_has_args('abc', ['abcd','abcde'])", + GetoptError) + +if verbose: + print 'Running tests on getopt.do_shorts' +opts, args = getopt.do_shorts([], 'a', 'a', []) +assert opts == [('-a', '')] +assert args == [] +opts, args = getopt.do_shorts([], 'a1', 'a:', []) +assert opts == [('-a', '1')] +assert args == [] +#opts, args = getopt.do_shorts([], 'a=1', 'a:', []) +#assert opts == [('-a', '1')] +#assert args == [] +opts, args = getopt.do_shorts([], 'a', 'a:', ['1']) +assert opts == [('-a', '1')] +assert args == [] +opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2']) +assert opts == [('-a', '1')] +assert args == ['2'] +expectException("opts, args = getopt.do_shorts([], 'a1', 'a', [])", + GetoptError) +expectException("opts, args = getopt.do_shorts([], 'a', 'a:', [])", + GetoptError) + +if verbose: + print 'Running tests on getopt.do_longs' +opts, args = getopt.do_longs([], 'abc', ['abc'], []) +assert opts == [('--abc', '')] +assert args == [] +opts, args = getopt.do_longs([], 'abc=1', ['abc='], []) +assert opts == [('--abc', '1')] +assert args == [] +opts, args = getopt.do_longs([], 'abc=1', ['abcd='], []) +assert opts == [('--abcd', '1')] +assert args == [] +expectException("opts, args = getopt.do_longs([], 'abc=1', ['abc'], [])", + GetoptError) +expectException("opts, args = getopt.do_longs([], 'abc', ['abc='], [])", + GetoptError) + +# note: the empty string between '-a' and '--beta' is significant: +# it simulates an empty string option argument ('-a ""') on the command line. +cmdline = ['-a', '1', '-b', '--alpha=2', '--beta', '-a', '3', '-a', '', + '--beta', 'arg1', 'arg2'] + +if verbose: + print 'Running tests on getopt.getopt' +opts, args = getopt.getopt(cmdline, 'a:b', ['alpha=', 'beta']) +assert opts == [('-a', '1'), ('-b', ''), ('--alpha', '2'), ('--beta', ''), + ('-a', '3'), ('-a', ''), ('--beta', '')] +# Note ambiguity of ('-b', '') and ('-a', '') above. This must be +# accounted for in the code that calls getopt(). +assert args == ['arg1', 'arg2'] + +expectException( + "opts, args = getopt.getopt(cmdline, 'a:b', ['alpha', 'beta'])", + GetoptError) + +if verbose: + print "Module getopt: tests completed successfully." |