ยปCore Development>Code coverage>Lib/test/test_getopt.py

Python code coverage for Lib/test/test_getopt.py

#countcontent
1n/a# test_getopt.py
2n/a# David Goodger <dgoodger@bigfoot.com> 2000-08-19
3n/a
4n/afrom test.support import verbose, run_doctest, EnvironmentVarGuard
5n/aimport unittest
6n/a
7n/aimport getopt
8n/a
9n/asentinel = object()
10n/a
11n/aclass GetoptTests(unittest.TestCase):
12n/a def setUp(self):
13n/a self.env = EnvironmentVarGuard()
14n/a if "POSIXLY_CORRECT" in self.env:
15n/a del self.env["POSIXLY_CORRECT"]
16n/a
17n/a def tearDown(self):
18n/a self.env.__exit__()
19n/a del self.env
20n/a
21n/a def assertError(self, *args, **kwargs):
22n/a self.assertRaises(getopt.GetoptError, *args, **kwargs)
23n/a
24n/a def test_short_has_arg(self):
25n/a self.assertTrue(getopt.short_has_arg('a', 'a:'))
26n/a self.assertFalse(getopt.short_has_arg('a', 'a'))
27n/a self.assertError(getopt.short_has_arg, 'a', 'b')
28n/a
29n/a def test_long_has_args(self):
30n/a has_arg, option = getopt.long_has_args('abc', ['abc='])
31n/a self.assertTrue(has_arg)
32n/a self.assertEqual(option, 'abc')
33n/a
34n/a has_arg, option = getopt.long_has_args('abc', ['abc'])
35n/a self.assertFalse(has_arg)
36n/a self.assertEqual(option, 'abc')
37n/a
38n/a has_arg, option = getopt.long_has_args('abc', ['abcd'])
39n/a self.assertFalse(has_arg)
40n/a self.assertEqual(option, 'abcd')
41n/a
42n/a self.assertError(getopt.long_has_args, 'abc', ['def'])
43n/a self.assertError(getopt.long_has_args, 'abc', [])
44n/a self.assertError(getopt.long_has_args, 'abc', ['abcd','abcde'])
45n/a
46n/a def test_do_shorts(self):
47n/a opts, args = getopt.do_shorts([], 'a', 'a', [])
48n/a self.assertEqual(opts, [('-a', '')])
49n/a self.assertEqual(args, [])
50n/a
51n/a opts, args = getopt.do_shorts([], 'a1', 'a:', [])
52n/a self.assertEqual(opts, [('-a', '1')])
53n/a self.assertEqual(args, [])
54n/a
55n/a #opts, args = getopt.do_shorts([], 'a=1', 'a:', [])
56n/a #self.assertEqual(opts, [('-a', '1')])
57n/a #self.assertEqual(args, [])
58n/a
59n/a opts, args = getopt.do_shorts([], 'a', 'a:', ['1'])
60n/a self.assertEqual(opts, [('-a', '1')])
61n/a self.assertEqual(args, [])
62n/a
63n/a opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2'])
64n/a self.assertEqual(opts, [('-a', '1')])
65n/a self.assertEqual(args, ['2'])
66n/a
67n/a self.assertError(getopt.do_shorts, [], 'a1', 'a', [])
68n/a self.assertError(getopt.do_shorts, [], 'a', 'a:', [])
69n/a
70n/a def test_do_longs(self):
71n/a opts, args = getopt.do_longs([], 'abc', ['abc'], [])
72n/a self.assertEqual(opts, [('--abc', '')])
73n/a self.assertEqual(args, [])
74n/a
75n/a opts, args = getopt.do_longs([], 'abc=1', ['abc='], [])
76n/a self.assertEqual(opts, [('--abc', '1')])
77n/a self.assertEqual(args, [])
78n/a
79n/a opts, args = getopt.do_longs([], 'abc=1', ['abcd='], [])
80n/a self.assertEqual(opts, [('--abcd', '1')])
81n/a self.assertEqual(args, [])
82n/a
83n/a opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], [])
84n/a self.assertEqual(opts, [('--abc', '')])
85n/a self.assertEqual(args, [])
86n/a
87n/a # Much like the preceding, except with a non-alpha character ("-") in
88n/a # option name that precedes "="; failed in
89n/a # http://python.org/sf/126863
90n/a opts, args = getopt.do_longs([], 'foo=42', ['foo-bar', 'foo=',], [])
91n/a self.assertEqual(opts, [('--foo', '42')])
92n/a self.assertEqual(args, [])
93n/a
94n/a self.assertError(getopt.do_longs, [], 'abc=1', ['abc'], [])
95n/a self.assertError(getopt.do_longs, [], 'abc', ['abc='], [])
96n/a
97n/a def test_getopt(self):
98n/a # note: the empty string between '-a' and '--beta' is significant:
99n/a # it simulates an empty string option argument ('-a ""') on the
100n/a # command line.
101n/a cmdline = ['-a', '1', '-b', '--alpha=2', '--beta', '-a', '3', '-a',
102n/a '', '--beta', 'arg1', 'arg2']
103n/a
104n/a opts, args = getopt.getopt(cmdline, 'a:b', ['alpha=', 'beta'])
105n/a self.assertEqual(opts, [('-a', '1'), ('-b', ''),
106n/a ('--alpha', '2'), ('--beta', ''),
107n/a ('-a', '3'), ('-a', ''), ('--beta', '')])
108n/a # Note ambiguity of ('-b', '') and ('-a', '') above. This must be
109n/a # accounted for in the code that calls getopt().
110n/a self.assertEqual(args, ['arg1', 'arg2'])
111n/a
112n/a self.assertError(getopt.getopt, cmdline, 'a:b', ['alpha', 'beta'])
113n/a
114n/a def test_gnu_getopt(self):
115n/a # Test handling of GNU style scanning mode.
116n/a cmdline = ['-a', 'arg1', '-b', '1', '--alpha', '--beta=2']
117n/a
118n/a # GNU style
119n/a opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta='])
120n/a self.assertEqual(args, ['arg1'])
121n/a self.assertEqual(opts, [('-a', ''), ('-b', '1'),
122n/a ('--alpha', ''), ('--beta', '2')])
123n/a
124n/a # recognize "-" as an argument
125n/a opts, args = getopt.gnu_getopt(['-a', '-', '-b', '-'], 'ab:', [])
126n/a self.assertEqual(args, ['-'])
127n/a self.assertEqual(opts, [('-a', ''), ('-b', '-')])
128n/a
129n/a # Posix style via +
130n/a opts, args = getopt.gnu_getopt(cmdline, '+ab:', ['alpha', 'beta='])
131n/a self.assertEqual(opts, [('-a', '')])
132n/a self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2'])
133n/a
134n/a # Posix style via POSIXLY_CORRECT
135n/a self.env["POSIXLY_CORRECT"] = "1"
136n/a opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta='])
137n/a self.assertEqual(opts, [('-a', '')])
138n/a self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2'])
139n/a
140n/a def test_libref_examples(self):
141n/a s = """
142n/a Examples from the Library Reference: Doc/lib/libgetopt.tex
143n/a
144n/a An example using only Unix style options:
145n/a
146n/a
147n/a >>> import getopt
148n/a >>> args = '-a -b -cfoo -d bar a1 a2'.split()
149n/a >>> args
150n/a ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
151n/a >>> optlist, args = getopt.getopt(args, 'abc:d:')
152n/a >>> optlist
153n/a [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
154n/a >>> args
155n/a ['a1', 'a2']
156n/a
157n/a Using long option names is equally easy:
158n/a
159n/a
160n/a >>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
161n/a >>> args = s.split()
162n/a >>> args
163n/a ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
164n/a >>> optlist, args = getopt.getopt(args, 'x', [
165n/a ... 'condition=', 'output-file=', 'testing'])
166n/a >>> optlist
167n/a [('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')]
168n/a >>> args
169n/a ['a1', 'a2']
170n/a """
171n/a
172n/a import types
173n/a m = types.ModuleType("libreftest", s)
174n/a run_doctest(m, verbose)
175n/a
176n/a def test_issue4629(self):
177n/a longopts, shortopts = getopt.getopt(['--help='], '', ['help='])
178n/a self.assertEqual(longopts, [('--help', '')])
179n/a longopts, shortopts = getopt.getopt(['--help=x'], '', ['help='])
180n/a self.assertEqual(longopts, [('--help', 'x')])
181n/a self.assertRaises(getopt.GetoptError, getopt.getopt, ['--help='], '', ['help'])
182n/a
183n/aif __name__ == "__main__":
184n/a unittest.main()