]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-89392: Use unittest test runner for doctests in test_getopt (GH-108916...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 8 Sep 2023 13:17:14 +0000 (06:17 -0700)
committerGitHub <noreply@github.com>
Fri, 8 Sep 2023 13:17:14 +0000 (15:17 +0200)
gh-89392: Use unittest test runner for doctests in test_getopt (GH-108916)
(cherry picked from commit f980cc19b9cafc09ef21e906871f810a1c89e62f)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Lib/test/test_getopt.py

index c96a33b77fe2721d8f4246ed46b41bc1ba9f312d..c8b3442de4aa77b994d9ba3cc014c023ff7ae78e 100644 (file)
@@ -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()