Skip to content

Commit f980cc1

Browse files
gh-89392: Use unittest test runner for doctests in test_getopt (GH-108916)
1 parent eaabaac commit f980cc1

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed

Lib/test/test_getopt.py

+36-35
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# test_getopt.py
22
# David Goodger <dgoodger@bigfoot.com> 2000-08-19
33

4-
from test.support import verbose, run_doctest
54
from test.support.os_helper import EnvironmentVarGuard
5+
import doctest
66
import unittest
77

88
import getopt
@@ -134,48 +134,49 @@ def test_gnu_getopt(self):
134134
self.assertEqual(opts, [('-a', '')])
135135
self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2'])
136136

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'])
140143

141-
An example using only Unix style options:
144+
def test_libref_examples():
145+
"""
146+
Examples from the Library Reference: Doc/lib/libgetopt.tex
142147
148+
An example using only Unix style options:
143149
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']
153150
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']
155160
161+
Using long option names is equally easy:
156162
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-
"""
168163
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
172179

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'])
179180

180181
if __name__ == "__main__":
181182
unittest.main()

0 commit comments

Comments
 (0)