-
-
Notifications
You must be signed in to change notification settings - Fork 31k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…GH-108916) (#108919) gh-89392: Use unittest test runner for doctests in test_getopt (GH-108916) (cherry picked from commit f980cc1) Co-authored-by: Serhiy Storchaka <[email protected]>
- Loading branch information
1 parent
da02508
commit 038b0a9
Showing
1 changed file
with
36 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
# test_getopt.py | ||
# David Goodger <[email protected]> 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 @@ def test_gnu_getopt(self): | |
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() |