Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.test_getopt.GetoptTests Class Reference

List of all members.

Public Member Functions

def setUp
def tearDown
def assertError
def test_short_has_arg
def test_long_has_args
def test_do_shorts
def test_do_longs
def test_getopt
def test_gnu_getopt
def test_libref_examples
def test_issue4629

Public Attributes

 env

Detailed Description

Definition at line 11 of file test_getopt.py.


Member Function Documentation

def test.test_getopt.GetoptTests.assertError (   self,
  args,
  kwargs 
)

Definition at line 21 of file test_getopt.py.

00021 
00022     def assertError(self, *args, **kwargs):
00023         self.assertRaises(getopt.GetoptError, *args, **kwargs)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 12 of file test_getopt.py.

00012 
00013     def setUp(self):
00014         self.env = EnvironmentVarGuard()
00015         if "POSIXLY_CORRECT" in self.env:
00016             del self.env["POSIXLY_CORRECT"]

Here is the caller graph for this function:

Definition at line 17 of file test_getopt.py.

00017 
00018     def tearDown(self):
00019         self.env.__exit__()
00020         del self.env

Here is the caller graph for this function:

Definition at line 70 of file test_getopt.py.

00070 
00071     def test_do_longs(self):
00072         opts, args = getopt.do_longs([], 'abc', ['abc'], [])
00073         self.assertEqual(opts, [('--abc', '')])
00074         self.assertEqual(args, [])
00075 
00076         opts, args = getopt.do_longs([], 'abc=1', ['abc='], [])
00077         self.assertEqual(opts, [('--abc', '1')])
00078         self.assertEqual(args, [])
00079 
00080         opts, args = getopt.do_longs([], 'abc=1', ['abcd='], [])
00081         self.assertEqual(opts, [('--abcd', '1')])
00082         self.assertEqual(args, [])
00083 
00084         opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], [])
00085         self.assertEqual(opts, [('--abc', '')])
00086         self.assertEqual(args, [])
00087 
00088         # Much like the preceding, except with a non-alpha character ("-") in
00089         # option name that precedes "="; failed in
00090         # http://python.org/sf/126863
00091         opts, args = getopt.do_longs([], 'foo=42', ['foo-bar', 'foo=',], [])
00092         self.assertEqual(opts, [('--foo', '42')])
00093         self.assertEqual(args, [])
00094 
00095         self.assertError(getopt.do_longs, [], 'abc=1', ['abc'], [])
00096         self.assertError(getopt.do_longs, [], 'abc', ['abc='], [])

Here is the call graph for this function:

Definition at line 46 of file test_getopt.py.

00046 
00047     def test_do_shorts(self):
00048         opts, args = getopt.do_shorts([], 'a', 'a', [])
00049         self.assertEqual(opts, [('-a', '')])
00050         self.assertEqual(args, [])
00051 
00052         opts, args = getopt.do_shorts([], 'a1', 'a:', [])
00053         self.assertEqual(opts, [('-a', '1')])
00054         self.assertEqual(args, [])
00055 
00056         #opts, args = getopt.do_shorts([], 'a=1', 'a:', [])
00057         #self.assertEqual(opts, [('-a', '1')])
00058         #self.assertEqual(args, [])
00059 
00060         opts, args = getopt.do_shorts([], 'a', 'a:', ['1'])
00061         self.assertEqual(opts, [('-a', '1')])
00062         self.assertEqual(args, [])
00063 
00064         opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2'])
00065         self.assertEqual(opts, [('-a', '1')])
00066         self.assertEqual(args, ['2'])
00067 
00068         self.assertError(getopt.do_shorts, [], 'a1', 'a', [])
00069         self.assertError(getopt.do_shorts, [], 'a', 'a:', [])

Here is the call graph for this function:

Definition at line 97 of file test_getopt.py.

00097 
00098     def test_getopt(self):
00099         # note: the empty string between '-a' and '--beta' is significant:
00100         # it simulates an empty string option argument ('-a ""') on the
00101         # command line.
00102         cmdline = ['-a', '1', '-b', '--alpha=2', '--beta', '-a', '3', '-a',
00103                    '', '--beta', 'arg1', 'arg2']
00104 
00105         opts, args = getopt.getopt(cmdline, 'a:b', ['alpha=', 'beta'])
00106         self.assertEqual(opts, [('-a', '1'), ('-b', ''),
00107                                 ('--alpha', '2'), ('--beta', ''),
00108                                 ('-a', '3'), ('-a', ''), ('--beta', '')])
00109         # Note ambiguity of ('-b', '') and ('-a', '') above. This must be
00110         # accounted for in the code that calls getopt().
00111         self.assertEqual(args, ['arg1', 'arg2'])
00112 
00113         self.assertError(getopt.getopt, cmdline, 'a:b', ['alpha', 'beta'])

Here is the call graph for this function:

Definition at line 114 of file test_getopt.py.

00114 
00115     def test_gnu_getopt(self):
00116         # Test handling of GNU style scanning mode.
00117         cmdline = ['-a', 'arg1', '-b', '1', '--alpha', '--beta=2']
00118 
00119         # GNU style
00120         opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta='])
00121         self.assertEqual(args, ['arg1'])
00122         self.assertEqual(opts, [('-a', ''), ('-b', '1'),
00123                                 ('--alpha', ''), ('--beta', '2')])
00124 
00125         # recognize "-" as an argument
00126         opts, args = getopt.gnu_getopt(['-a', '-', '-b', '-'], 'ab:', [])
00127         self.assertEqual(args, ['-'])
00128         self.assertEqual(opts, [('-a', ''), ('-b', '-')])
00129 
00130         # Posix style via +
00131         opts, args = getopt.gnu_getopt(cmdline, '+ab:', ['alpha', 'beta='])
00132         self.assertEqual(opts, [('-a', '')])
00133         self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2'])
00134 
00135         # Posix style via POSIXLY_CORRECT
00136         self.env["POSIXLY_CORRECT"] = "1"
00137         opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta='])
00138         self.assertEqual(opts, [('-a', '')])
00139         self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2'])

Here is the call graph for this function:

Definition at line 176 of file test_getopt.py.

00176 
00177     def test_issue4629(self):
00178         longopts, shortopts = getopt.getopt(['--help='], '', ['help='])
00179         self.assertEqual(longopts, [('--help', '')])
00180         longopts, shortopts = getopt.getopt(['--help=x'], '', ['help='])
00181         self.assertEqual(longopts, [('--help', 'x')])
00182         self.assertRaises(getopt.GetoptError, getopt.getopt, ['--help='], '', ['help'])

Here is the call graph for this function:

Definition at line 140 of file test_getopt.py.

00140 
00141     def test_libref_examples(self):
00142         s = """
00143         Examples from the Library Reference:  Doc/lib/libgetopt.tex
00144 
00145         An example using only Unix style options:
00146 
00147 
00148         >>> import getopt
00149         >>> args = '-a -b -cfoo -d bar a1 a2'.split()
00150         >>> args
00151         ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
00152         >>> optlist, args = getopt.getopt(args, 'abc:d:')
00153         >>> optlist
00154         [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
00155         >>> args
00156         ['a1', 'a2']
00157 
00158         Using long option names is equally easy:
00159 
00160 
00161         >>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
00162         >>> args = s.split()
00163         >>> args
00164         ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
00165         >>> optlist, args = getopt.getopt(args, 'x', [
00166         ...     'condition=', 'output-file=', 'testing'])
00167         >>> optlist
00168         [('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')]
00169         >>> args
00170         ['a1', 'a2']
00171         """
00172 
00173         import types
00174         m = types.ModuleType("libreftest", s)
00175         run_doctest(m, verbose)

Here is the call graph for this function:

Definition at line 29 of file test_getopt.py.

00029 
00030     def test_long_has_args(self):
00031         has_arg, option = getopt.long_has_args('abc', ['abc='])
00032         self.assertTrue(has_arg)
00033         self.assertEqual(option, 'abc')
00034 
00035         has_arg, option = getopt.long_has_args('abc', ['abc'])
00036         self.assertFalse(has_arg)
00037         self.assertEqual(option, 'abc')
00038 
00039         has_arg, option = getopt.long_has_args('abc', ['abcd'])
00040         self.assertFalse(has_arg)
00041         self.assertEqual(option, 'abcd')
00042 
00043         self.assertError(getopt.long_has_args, 'abc', ['def'])
00044         self.assertError(getopt.long_has_args, 'abc', [])
00045         self.assertError(getopt.long_has_args, 'abc', ['abcd','abcde'])

Here is the call graph for this function:

Definition at line 24 of file test_getopt.py.

00024 
00025     def test_short_has_arg(self):
00026         self.assertTrue(getopt.short_has_arg('a', 'a:'))
00027         self.assertFalse(getopt.short_has_arg('a', 'a'))
00028         self.assertError(getopt.short_has_arg, 'a', 'b')

Here is the call graph for this function:


Member Data Documentation

Definition at line 13 of file test_getopt.py.


The documentation for this class was generated from the following file: