aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2023-09-05 17:35:28 +0300
committerGitHub <noreply@github.com>2023-09-05 17:35:28 +0300
commitf980cc19b9cafc09ef21e906871f810a1c89e62f (patch)
tree143fb46baaabc2ff479f7fc4566bb7068e0bf575 /Lib/test
parenteaabaac7c099884f92428a7bb04ffa1f1d6080dd (diff)
downloadcpython-f980cc19b9cafc09ef21e906871f810a1c89e62f.tar.gz
cpython-f980cc19b9cafc09ef21e906871f810a1c89e62f.zip
gh-89392: Use unittest test runner for doctests in test_getopt (GH-108916)
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_getopt.py71
1 files changed, 36 insertions, 35 deletions
diff --git a/Lib/test/test_getopt.py b/Lib/test/test_getopt.py
index c96a33b77fe..c8b3442de4a 100644
--- a/Lib/test/test_getopt.py
+++ b/Lib/test/test_getopt.py
@@ -1,8 +1,8 @@
# test_getopt.py
# David Goodger <dgoodger@bigfoot.com> 2000-08-19
-from test.support import verbose, run_doctest
from test.support.os_helper import EnvironmentVarGuard
+import doctest
import unittest
import getopt
@@ -134,48 +134,49 @@ class GetoptTests(unittest.TestCase):
self.assertEqual(opts, [('-a', '')])
self.assertEqual(args, ['arg1', '-b', '1', '--alpha', '--beta=2'])
- def test_libref_examples(self):
- s = """
- Examples from the Library Reference: Doc/lib/libgetopt.tex
+ def test_issue4629(self):
+ longopts, shortopts = getopt.getopt(['--help='], '', ['help='])
+ self.assertEqual(longopts, [('--help', '')])
+ longopts, shortopts = getopt.getopt(['--help=x'], '', ['help='])
+ self.assertEqual(longopts, [('--help', 'x')])
+ self.assertRaises(getopt.GetoptError, getopt.getopt, ['--help='], '', ['help'])
- An example using only Unix style options:
+def test_libref_examples():
+ """
+ Examples from the Library Reference: Doc/lib/libgetopt.tex
+ An example using only Unix style options:
- >>> import getopt
- >>> args = '-a -b -cfoo -d bar a1 a2'.split()
- >>> args
- ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
- >>> optlist, args = getopt.getopt(args, 'abc:d:')
- >>> optlist
- [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
- >>> args
- ['a1', 'a2']
- Using long option names is equally easy:
+ >>> import getopt
+ >>> args = '-a -b -cfoo -d bar a1 a2'.split()
+ >>> args
+ ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
+ >>> optlist, args = getopt.getopt(args, 'abc:d:')
+ >>> optlist
+ [('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
+ >>> args
+ ['a1', 'a2']
+ Using long option names is equally easy:
- >>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
- >>> args = s.split()
- >>> args
- ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
- >>> optlist, args = getopt.getopt(args, 'x', [
- ... 'condition=', 'output-file=', 'testing'])
- >>> optlist
- [('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')]
- >>> args
- ['a1', 'a2']
- """
- import types
- m = types.ModuleType("libreftest", s)
- run_doctest(m, verbose)
+ >>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
+ >>> args = s.split()
+ >>> args
+ ['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
+ >>> optlist, args = getopt.getopt(args, 'x', [
+ ... 'condition=', 'output-file=', 'testing'])
+ >>> optlist
+ [('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')]
+ >>> args
+ ['a1', 'a2']
+ """
+
+def load_tests(loader, tests, pattern):
+ tests.addTest(doctest.DocTestSuite())
+ return tests
- def test_issue4629(self):
- longopts, shortopts = getopt.getopt(['--help='], '', ['help='])
- self.assertEqual(longopts, [('--help', '')])
- longopts, shortopts = getopt.getopt(['--help=x'], '', ['help='])
- self.assertEqual(longopts, [('--help', 'x')])
- self.assertRaises(getopt.GetoptError, getopt.getopt, ['--help='], '', ['help'])
if __name__ == "__main__":
unittest.main()