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 |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 6 | from test.test_support import verify, verbose |
Martin v. Löwis | 446a25f | 2002-06-06 10:58:36 +0000 | [diff] [blame] | 7 | import os |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 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 | if verbose: |
| 20 | print 'Running tests on getopt.short_has_arg' |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 21 | verify(getopt.short_has_arg('a', 'a:')) |
| 22 | verify(not getopt.short_has_arg('a', 'a')) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 23 | expectException("tmp = getopt.short_has_arg('a', 'b')", GetoptError) |
| 24 | expectException("tmp = getopt.short_has_arg('a', '')", GetoptError) |
| 25 | |
| 26 | if verbose: |
| 27 | print 'Running tests on getopt.long_has_args' |
| 28 | has_arg, option = getopt.long_has_args('abc', ['abc=']) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 29 | verify(has_arg) |
| 30 | verify(option == 'abc') |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 31 | has_arg, option = getopt.long_has_args('abc', ['abc']) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 32 | verify(not has_arg) |
| 33 | verify(option == 'abc') |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 34 | has_arg, option = getopt.long_has_args('abc', ['abcd']) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 35 | verify(not has_arg) |
| 36 | verify(option == 'abcd') |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 37 | expectException("has_arg, option = getopt.long_has_args('abc', ['def'])", |
| 38 | GetoptError) |
| 39 | expectException("has_arg, option = getopt.long_has_args('abc', [])", |
| 40 | GetoptError) |
| 41 | expectException("has_arg, option = " + \ |
| 42 | "getopt.long_has_args('abc', ['abcd','abcde'])", |
| 43 | GetoptError) |
| 44 | |
| 45 | if verbose: |
| 46 | print 'Running tests on getopt.do_shorts' |
| 47 | opts, args = getopt.do_shorts([], 'a', 'a', []) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 48 | verify(opts == [('-a', '')]) |
| 49 | verify(args == []) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 50 | opts, args = getopt.do_shorts([], 'a1', 'a:', []) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 51 | verify(opts == [('-a', '1')]) |
| 52 | verify(args == []) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 53 | #opts, args = getopt.do_shorts([], 'a=1', 'a:', []) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 54 | #verify(opts == [('-a', '1')]) |
| 55 | #verify(args == []) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 56 | opts, args = getopt.do_shorts([], 'a', 'a:', ['1']) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 57 | verify(opts == [('-a', '1')]) |
| 58 | verify(args == []) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 59 | opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2']) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 60 | verify(opts == [('-a', '1')]) |
| 61 | verify(args == ['2']) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 62 | expectException("opts, args = getopt.do_shorts([], 'a1', 'a', [])", |
| 63 | GetoptError) |
| 64 | expectException("opts, args = getopt.do_shorts([], 'a', 'a:', [])", |
| 65 | GetoptError) |
| 66 | |
| 67 | if verbose: |
| 68 | print 'Running tests on getopt.do_longs' |
| 69 | opts, args = getopt.do_longs([], 'abc', ['abc'], []) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 70 | verify(opts == [('--abc', '')]) |
| 71 | verify(args == []) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 72 | opts, args = getopt.do_longs([], 'abc=1', ['abc='], []) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 73 | verify(opts == [('--abc', '1')]) |
| 74 | verify(args == []) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 75 | opts, args = getopt.do_longs([], 'abc=1', ['abcd='], []) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 76 | verify(opts == [('--abcd', '1')]) |
| 77 | verify(args == []) |
Tim Peters | da7bf4e | 2000-12-27 08:03:20 +0000 | [diff] [blame] | 78 | opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], []) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 79 | verify(opts == [('--abc', '')]) |
| 80 | verify(args == []) |
Tim Peters | da7bf4e | 2000-12-27 08:03:20 +0000 | [diff] [blame] | 81 | # Much like the preceding, except with a non-alpha character ("-") in |
| 82 | # option name that precedes "="; failed in |
| 83 | # http://sourceforge.net/bugs/?func=detailbug&bug_id=126863&group_id=5470 |
| 84 | opts, args = getopt.do_longs([], 'foo=42', ['foo-bar', 'foo=',], []) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 85 | verify(opts == [('--foo', '42')]) |
| 86 | verify(args == []) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 87 | expectException("opts, args = getopt.do_longs([], 'abc=1', ['abc'], [])", |
| 88 | GetoptError) |
| 89 | expectException("opts, args = getopt.do_longs([], 'abc', ['abc='], [])", |
| 90 | GetoptError) |
| 91 | |
| 92 | # note: the empty string between '-a' and '--beta' is significant: |
| 93 | # it simulates an empty string option argument ('-a ""') on the command line. |
| 94 | cmdline = ['-a', '1', '-b', '--alpha=2', '--beta', '-a', '3', '-a', '', |
| 95 | '--beta', 'arg1', 'arg2'] |
| 96 | |
| 97 | if verbose: |
| 98 | print 'Running tests on getopt.getopt' |
| 99 | opts, args = getopt.getopt(cmdline, 'a:b', ['alpha=', 'beta']) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 100 | verify(opts == [('-a', '1'), ('-b', ''), ('--alpha', '2'), ('--beta', ''), |
| 101 | ('-a', '3'), ('-a', ''), ('--beta', '')] ) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 102 | # Note ambiguity of ('-b', '') and ('-a', '') above. This must be |
| 103 | # accounted for in the code that calls getopt(). |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 104 | verify(args == ['arg1', 'arg2']) |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 105 | |
| 106 | expectException( |
| 107 | "opts, args = getopt.getopt(cmdline, 'a:b', ['alpha', 'beta'])", |
| 108 | GetoptError) |
| 109 | |
Martin v. Löwis | 446a25f | 2002-06-06 10:58:36 +0000 | [diff] [blame] | 110 | # Test handling of GNU style scanning mode. |
| 111 | if verbose: |
| 112 | print 'Running tests on getopt.gnu_getopt' |
| 113 | cmdline = ['-a', 'arg1', '-b', '1', '--alpha', '--beta=2'] |
| 114 | # GNU style |
| 115 | opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta=']) |
| 116 | verify(opts == [('-a', ''), ('-b', '1'), ('--alpha', ''), ('--beta', '2')]) |
| 117 | verify(args == ['arg1']) |
| 118 | # Posix style via + |
| 119 | opts, args = getopt.gnu_getopt(cmdline, '+ab:', ['alpha', 'beta=']) |
| 120 | verify(opts == [('-a', '')]) |
| 121 | verify(args == ['arg1', '-b', '1', '--alpha', '--beta=2']) |
| 122 | # Posix style via POSIXLY_CORRECT |
| 123 | os.environ["POSIXLY_CORRECT"] = "1" |
| 124 | opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta=']) |
| 125 | verify(opts == [('-a', '')]) |
| 126 | verify(args == ['arg1', '-b', '1', '--alpha', '--beta=2']) |
| 127 | |
| 128 | |
Tim Peters | 571bb8f | 2000-08-20 04:18:40 +0000 | [diff] [blame] | 129 | if verbose: |
| 130 | print "Module getopt: tests completed successfully." |