|
1 | 1 | # test_getopt.py
|
2 | 2 | # David Goodger <dgoodger@bigfoot.com> 2000-08-19
|
3 | 3 |
|
4 |
| -from test.support import verbose, run_doctest |
5 | 4 | from test.support.os_helper import EnvironmentVarGuard
|
| 5 | +import doctest |
6 | 6 | import unittest
|
7 | 7 |
|
8 | 8 | import getopt
|
@@ -134,48 +134,49 @@ def test_gnu_getopt(self):
|
134 | 134 | self.assertEqual(opts, [('-a', '')])
|
135 | 135 | self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2'])
|
136 | 136 |
|
137 |
| - def test_libref_examples(self): |
138 |
| - s = """ |
139 |
| - Examples from the Library Reference: Doc/lib/libgetopt.tex |
| 137 | + def test_issue4629(self): |
| 138 | + longopts, shortopts = getopt.getopt(['--help='], '', ['help=']) |
| 139 | + self.assertEqual(longopts, [('--help', '')]) |
| 140 | + longopts, shortopts = getopt.getopt(['--help=x'], '', ['help=']) |
| 141 | + self.assertEqual(longopts, [('--help', 'x')]) |
| 142 | + self.assertRaises(getopt.GetoptError, getopt.getopt, ['--help='], '', ['help']) |
140 | 143 |
|
141 |
| - An example using only Unix style options: |
| 144 | +def test_libref_examples(): |
| 145 | + """ |
| 146 | + Examples from the Library Reference: Doc/lib/libgetopt.tex |
142 | 147 |
|
| 148 | + An example using only Unix style options: |
143 | 149 |
|
144 |
| - >>> import getopt |
145 |
| - >>> args = '-a -b -cfoo -d bar a1 a2'.split() |
146 |
| - >>> args |
147 |
| - ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2'] |
148 |
| - >>> optlist, args = getopt.getopt(args, 'abc:d:') |
149 |
| - >>> optlist |
150 |
| - [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')] |
151 |
| - >>> args |
152 |
| - ['a1', 'a2'] |
153 | 150 |
|
154 |
| - Using long option names is equally easy: |
| 151 | + >>> import getopt |
| 152 | + >>> args = '-a -b -cfoo -d bar a1 a2'.split() |
| 153 | + >>> args |
| 154 | + ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2'] |
| 155 | + >>> optlist, args = getopt.getopt(args, 'abc:d:') |
| 156 | + >>> optlist |
| 157 | + [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')] |
| 158 | + >>> args |
| 159 | + ['a1', 'a2'] |
155 | 160 |
|
| 161 | + Using long option names is equally easy: |
156 | 162 |
|
157 |
| - >>> s = '--condition=foo --testing --output-file abc.def -x a1 a2' |
158 |
| - >>> args = s.split() |
159 |
| - >>> args |
160 |
| - ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2'] |
161 |
| - >>> optlist, args = getopt.getopt(args, 'x', [ |
162 |
| - ... 'condition=', 'output-file=', 'testing']) |
163 |
| - >>> optlist |
164 |
| - [('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')] |
165 |
| - >>> args |
166 |
| - ['a1', 'a2'] |
167 |
| - """ |
168 | 163 |
|
169 |
| - import types |
170 |
| - m = types.ModuleType("libreftest", s) |
171 |
| - run_doctest(m, verbose) |
| 164 | + >>> s = '--condition=foo --testing --output-file abc.def -x a1 a2' |
| 165 | + >>> args = s.split() |
| 166 | + >>> args |
| 167 | + ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2'] |
| 168 | + >>> optlist, args = getopt.getopt(args, 'x', [ |
| 169 | + ... 'condition=', 'output-file=', 'testing']) |
| 170 | + >>> optlist |
| 171 | + [('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')] |
| 172 | + >>> args |
| 173 | + ['a1', 'a2'] |
| 174 | + """ |
| 175 | + |
| 176 | +def load_tests(loader, tests, pattern): |
| 177 | + tests.addTest(doctest.DocTestSuite()) |
| 178 | + return tests |
172 | 179 |
|
173 |
| - def test_issue4629(self): |
174 |
| - longopts, shortopts = getopt.getopt(['--help='], '', ['help=']) |
175 |
| - self.assertEqual(longopts, [('--help', '')]) |
176 |
| - longopts, shortopts = getopt.getopt(['--help=x'], '', ['help=']) |
177 |
| - self.assertEqual(longopts, [('--help', 'x')]) |
178 |
| - self.assertRaises(getopt.GetoptError, getopt.getopt, ['--help='], '', ['help']) |
179 | 180 |
|
180 | 181 | if __name__ == "__main__":
|
181 | 182 | unittest.main()
|
0 commit comments