Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 1 | # test_getopt.py |
| 2 | # David Goodger <dgoodger@bigfoot.com> 2000-08-19 |
| 3 | |
| 4 | import getopt |
| 5 | from getopt import GetoptError |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 6 | from test_support import verify, verbose |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 7 | |
| 8 | def 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 | |
| 18 | if verbose: |
| 19 | print 'Running tests on getopt.short_has_arg' |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 20 | verify(getopt.short_has_arg('a', 'a:')) |
| 21 | verify(not getopt.short_has_arg('a', 'a')) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 22 | expectException("tmp = getopt.short_has_arg('a', 'b')", GetoptError) |
| 23 | expectException("tmp = getopt.short_has_arg('a', '')", GetoptError) |
| 24 | |
| 25 | if verbose: |
| 26 | print 'Running tests on getopt.long_has_args' |
| 27 | has_arg, option = getopt.long_has_args('abc', ['abc=']) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 28 | verify(has_arg) |
| 29 | verify(option == 'abc') |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 30 | has_arg, option = getopt.long_has_args('abc', ['abc']) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 31 | verify(not has_arg) |
| 32 | verify(option == 'abc') |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 33 | has_arg, option = getopt.long_has_args('abc', ['abcd']) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 34 | verify(not has_arg) |
| 35 | verify(option == 'abcd') |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 36 | expectException("has_arg, option = getopt.long_has_args('abc', ['def'])", |
| 37 | GetoptError) |
| 38 | expectException("has_arg, option = getopt.long_has_args('abc', [])", |
| 39 | GetoptError) |
| 40 | expectException("has_arg, option = " + \ |
| 41 | "getopt.long_has_args('abc', ['abcd','abcde'])", |
| 42 | GetoptError) |
| 43 | |
| 44 | if verbose: |
| 45 | print 'Running tests on getopt.do_shorts' |
| 46 | opts, args = getopt.do_shorts([], 'a', 'a', []) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 47 | verify(opts == [('-a', '')]) |
| 48 | verify(args == []) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 49 | opts, args = getopt.do_shorts([], 'a1', 'a:', []) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 50 | verify(opts == [('-a', '1')]) |
| 51 | verify(args == []) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 52 | #opts, args = getopt.do_shorts([], 'a=1', 'a:', []) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 53 | #verify(opts == [('-a', '1')]) |
| 54 | #verify(args == []) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 55 | opts, args = getopt.do_shorts([], 'a', 'a:', ['1']) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 56 | verify(opts == [('-a', '1')]) |
| 57 | verify(args == []) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 58 | opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2']) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 59 | verify(opts == [('-a', '1')]) |
| 60 | verify(args == ['2']) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 61 | expectException("opts, args = getopt.do_shorts([], 'a1', 'a', [])", |
| 62 | GetoptError) |
| 63 | expectException("opts, args = getopt.do_shorts([], 'a', 'a:', [])", |
| 64 | GetoptError) |
| 65 | |
| 66 | if verbose: |
| 67 | print 'Running tests on getopt.do_longs' |
| 68 | opts, args = getopt.do_longs([], 'abc', ['abc'], []) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 69 | verify(opts == [('--abc', '')]) |
| 70 | verify(args == []) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 71 | opts, args = getopt.do_longs([], 'abc=1', ['abc='], []) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 72 | verify(opts == [('--abc', '1')]) |
| 73 | verify(args == []) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 74 | opts, args = getopt.do_longs([], 'abc=1', ['abcd='], []) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 75 | verify(opts == [('--abcd', '1')]) |
| 76 | verify(args == []) |
Tim Peters | da7bf4e | 2000-12-27 08:03:20 +0000 | [diff] [blame] | 77 | opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], []) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 78 | verify(opts == [('--abc', '')]) |
| 79 | verify(args == []) |
Tim Peters | da7bf4e | 2000-12-27 08:03:20 +0000 | [diff] [blame] | 80 | # 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 |
| 83 | opts, args = getopt.do_longs([], 'foo=42', ['foo-bar', 'foo=',], []) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 84 | verify(opts == [('--foo', '42')]) |
| 85 | verify(args == []) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 86 | expectException("opts, args = getopt.do_longs([], 'abc=1', ['abc'], [])", |
| 87 | GetoptError) |
| 88 | expectException("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. |
| 93 | cmdline = ['-a', '1', '-b', '--alpha=2', '--beta', '-a', '3', '-a', '', |
| 94 | '--beta', 'arg1', 'arg2'] |
| 95 | |
| 96 | if verbose: |
| 97 | print 'Running tests on getopt.getopt' |
| 98 | opts, args = getopt.getopt(cmdline, 'a:b', ['alpha=', 'beta']) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 99 | verify(opts == [('-a', '1'), ('-b', ''), ('--alpha', '2'), ('--beta', ''), |
| 100 | ('-a', '3'), ('-a', ''), ('--beta', '')] ) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 101 | # Note ambiguity of ('-b', '') and ('-a', '') above. This must be |
| 102 | # accounted for in the code that calls getopt(). |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 103 | verify(args == ['arg1', 'arg2']) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 104 | |
| 105 | expectException( |
| 106 | "opts, args = getopt.getopt(cmdline, 'a:b', ['alpha', 'beta'])", |
| 107 | GetoptError) |
| 108 | |
| 109 | if verbose: |
| 110 | print "Module getopt: tests completed successfully." |