mirror of https://github.com/python/cpython
Issue #9554: Use modern unittest features in test_argparse.
Initial patch by Denver Coneybeare and Radu Voicilas.
This commit is contained in:
parent
3cadd802a4
commit
1c5f56a955
|
@ -20,15 +20,6 @@ class StdIOBuffer(StringIO):
|
||||||
|
|
||||||
class TestCase(unittest.TestCase):
|
class TestCase(unittest.TestCase):
|
||||||
|
|
||||||
def assertEqual(self, obj1, obj2):
|
|
||||||
if obj1 != obj2:
|
|
||||||
print('')
|
|
||||||
print(repr(obj1))
|
|
||||||
print(repr(obj2))
|
|
||||||
print(obj1)
|
|
||||||
print(obj2)
|
|
||||||
super(TestCase, self).assertEqual(obj1, obj2)
|
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
# The tests assume that line wrapping occurs at 80 columns, but this
|
# The tests assume that line wrapping occurs at 80 columns, but this
|
||||||
# behaviour can be overridden by setting the COLUMNS environment
|
# behaviour can be overridden by setting the COLUMNS environment
|
||||||
|
@ -1993,14 +1984,9 @@ class TestAddSubparsers(TestCase):
|
||||||
'''))
|
'''))
|
||||||
|
|
||||||
def _test_subparser_help(self, args_str, expected_help):
|
def _test_subparser_help(self, args_str, expected_help):
|
||||||
try:
|
with self.assertRaises(ArgumentParserError) as cm:
|
||||||
self.parser.parse_args(args_str.split())
|
self.parser.parse_args(args_str.split())
|
||||||
except ArgumentParserError:
|
self.assertEqual(expected_help, cm.exception.stdout)
|
||||||
err = sys.exc_info()[1]
|
|
||||||
if err.stdout != expected_help:
|
|
||||||
print(repr(expected_help))
|
|
||||||
print(repr(err.stdout))
|
|
||||||
self.assertEqual(err.stdout, expected_help)
|
|
||||||
|
|
||||||
def test_subparser1_help(self):
|
def test_subparser1_help(self):
|
||||||
self._test_subparser_help('5.0 1 -h', textwrap.dedent('''\
|
self._test_subparser_help('5.0 1 -h', textwrap.dedent('''\
|
||||||
|
@ -2839,15 +2825,15 @@ class TestGetDefault(TestCase):
|
||||||
|
|
||||||
def test_get_default(self):
|
def test_get_default(self):
|
||||||
parser = ErrorRaisingArgumentParser()
|
parser = ErrorRaisingArgumentParser()
|
||||||
self.assertEqual(None, parser.get_default("foo"))
|
self.assertIsNone(parser.get_default("foo"))
|
||||||
self.assertEqual(None, parser.get_default("bar"))
|
self.assertIsNone(parser.get_default("bar"))
|
||||||
|
|
||||||
parser.add_argument("--foo")
|
parser.add_argument("--foo")
|
||||||
self.assertEqual(None, parser.get_default("foo"))
|
self.assertIsNone(parser.get_default("foo"))
|
||||||
self.assertEqual(None, parser.get_default("bar"))
|
self.assertIsNone(parser.get_default("bar"))
|
||||||
|
|
||||||
parser.add_argument("--bar", type=int, default=42)
|
parser.add_argument("--bar", type=int, default=42)
|
||||||
self.assertEqual(None, parser.get_default("foo"))
|
self.assertIsNone(parser.get_default("foo"))
|
||||||
self.assertEqual(42, parser.get_default("bar"))
|
self.assertEqual(42, parser.get_default("bar"))
|
||||||
|
|
||||||
parser.set_defaults(foo="badger")
|
parser.set_defaults(foo="badger")
|
||||||
|
@ -2862,18 +2848,16 @@ class TestNamespaceContainsSimple(TestCase):
|
||||||
|
|
||||||
def test_empty(self):
|
def test_empty(self):
|
||||||
ns = argparse.Namespace()
|
ns = argparse.Namespace()
|
||||||
self.assertEqual('' in ns, False)
|
self.assertNotIn('', ns)
|
||||||
self.assertEqual('' not in ns, True)
|
self.assertNotIn('x', ns)
|
||||||
self.assertEqual('x' in ns, False)
|
|
||||||
|
|
||||||
def test_non_empty(self):
|
def test_non_empty(self):
|
||||||
ns = argparse.Namespace(x=1, y=2)
|
ns = argparse.Namespace(x=1, y=2)
|
||||||
self.assertEqual('x' in ns, True)
|
self.assertNotIn('', ns)
|
||||||
self.assertEqual('x' not in ns, False)
|
self.assertIn('x', ns)
|
||||||
self.assertEqual('y' in ns, True)
|
self.assertIn('y', ns)
|
||||||
self.assertEqual('' in ns, False)
|
self.assertNotIn('xx', ns)
|
||||||
self.assertEqual('xx' in ns, False)
|
self.assertNotIn('z', ns)
|
||||||
self.assertEqual('z' in ns, False)
|
|
||||||
|
|
||||||
# =====================
|
# =====================
|
||||||
# Help formatting tests
|
# Help formatting tests
|
||||||
|
@ -2929,13 +2913,6 @@ class TestHelpFormattingMetaclass(type):
|
||||||
def _test(self, tester, parser_text):
|
def _test(self, tester, parser_text):
|
||||||
expected_text = getattr(tester, self.func_suffix)
|
expected_text = getattr(tester, self.func_suffix)
|
||||||
expected_text = textwrap.dedent(expected_text)
|
expected_text = textwrap.dedent(expected_text)
|
||||||
if expected_text != parser_text:
|
|
||||||
print(repr(expected_text))
|
|
||||||
print(repr(parser_text))
|
|
||||||
for char1, char2 in zip(expected_text, parser_text):
|
|
||||||
if char1 != char2:
|
|
||||||
print('first diff: %r %r' % (char1, char2))
|
|
||||||
break
|
|
||||||
tester.assertEqual(expected_text, parser_text)
|
tester.assertEqual(expected_text, parser_text)
|
||||||
|
|
||||||
def test_format(self, tester):
|
def test_format(self, tester):
|
||||||
|
@ -4216,24 +4193,17 @@ class TestInvalidArgumentConstructors(TestCase):
|
||||||
self.assertValueError('foo', action='baz')
|
self.assertValueError('foo', action='baz')
|
||||||
self.assertValueError('--foo', action=('store', 'append'))
|
self.assertValueError('--foo', action=('store', 'append'))
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
try:
|
with self.assertRaises(ValueError) as cm:
|
||||||
parser.add_argument("--foo", action="store-true")
|
parser.add_argument("--foo", action="store-true")
|
||||||
except ValueError:
|
self.assertIn('unknown action', str(cm.exception))
|
||||||
e = sys.exc_info()[1]
|
|
||||||
expected = 'unknown action'
|
|
||||||
msg = 'expected %r, found %r' % (expected, e)
|
|
||||||
self.assertTrue(expected in str(e), msg)
|
|
||||||
|
|
||||||
def test_multiple_dest(self):
|
def test_multiple_dest(self):
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument(dest='foo')
|
parser.add_argument(dest='foo')
|
||||||
try:
|
with self.assertRaises(ValueError) as cm:
|
||||||
parser.add_argument('bar', dest='baz')
|
parser.add_argument('bar', dest='baz')
|
||||||
except ValueError:
|
self.assertIn('dest supplied twice for positional argument',
|
||||||
e = sys.exc_info()[1]
|
str(cm.exception))
|
||||||
expected = 'dest supplied twice for positional argument'
|
|
||||||
msg = 'expected %r, found %r' % (expected, e)
|
|
||||||
self.assertTrue(expected in str(e), msg)
|
|
||||||
|
|
||||||
def test_no_argument_actions(self):
|
def test_no_argument_actions(self):
|
||||||
for action in ['store_const', 'store_true', 'store_false',
|
for action in ['store_const', 'store_true', 'store_false',
|
||||||
|
@ -4390,18 +4360,10 @@ class TestConflictHandling(TestCase):
|
||||||
class TestOptionalsHelpVersionActions(TestCase):
|
class TestOptionalsHelpVersionActions(TestCase):
|
||||||
"""Test the help and version actions"""
|
"""Test the help and version actions"""
|
||||||
|
|
||||||
def _get_error(self, func, *args, **kwargs):
|
|
||||||
try:
|
|
||||||
func(*args, **kwargs)
|
|
||||||
except ArgumentParserError:
|
|
||||||
return sys.exc_info()[1]
|
|
||||||
else:
|
|
||||||
self.assertRaises(ArgumentParserError, func, *args, **kwargs)
|
|
||||||
|
|
||||||
def assertPrintHelpExit(self, parser, args_str):
|
def assertPrintHelpExit(self, parser, args_str):
|
||||||
self.assertEqual(
|
with self.assertRaises(ArgumentParserError) as cm:
|
||||||
parser.format_help(),
|
parser.parse_args(args_str.split())
|
||||||
self._get_error(parser.parse_args, args_str.split()).stdout)
|
self.assertEqual(parser.format_help(), cm.exception.stdout)
|
||||||
|
|
||||||
def assertArgumentParserError(self, parser, *args):
|
def assertArgumentParserError(self, parser, *args):
|
||||||
self.assertRaises(ArgumentParserError, parser.parse_args, args)
|
self.assertRaises(ArgumentParserError, parser.parse_args, args)
|
||||||
|
@ -4416,8 +4378,9 @@ class TestOptionalsHelpVersionActions(TestCase):
|
||||||
def test_version_format(self):
|
def test_version_format(self):
|
||||||
parser = ErrorRaisingArgumentParser(prog='PPP')
|
parser = ErrorRaisingArgumentParser(prog='PPP')
|
||||||
parser.add_argument('-v', '--version', action='version', version='%(prog)s 3.5')
|
parser.add_argument('-v', '--version', action='version', version='%(prog)s 3.5')
|
||||||
msg = self._get_error(parser.parse_args, ['-v']).stdout
|
with self.assertRaises(ArgumentParserError) as cm:
|
||||||
self.assertEqual('PPP 3.5\n', msg)
|
parser.parse_args(['-v'])
|
||||||
|
self.assertEqual('PPP 3.5\n', cm.exception.stdout)
|
||||||
|
|
||||||
def test_version_no_help(self):
|
def test_version_no_help(self):
|
||||||
parser = ErrorRaisingArgumentParser(add_help=False)
|
parser = ErrorRaisingArgumentParser(add_help=False)
|
||||||
|
@ -4429,8 +4392,9 @@ class TestOptionalsHelpVersionActions(TestCase):
|
||||||
def test_version_action(self):
|
def test_version_action(self):
|
||||||
parser = ErrorRaisingArgumentParser(prog='XXX')
|
parser = ErrorRaisingArgumentParser(prog='XXX')
|
||||||
parser.add_argument('-V', action='version', version='%(prog)s 3.7')
|
parser.add_argument('-V', action='version', version='%(prog)s 3.7')
|
||||||
msg = self._get_error(parser.parse_args, ['-V']).stdout
|
with self.assertRaises(ArgumentParserError) as cm:
|
||||||
self.assertEqual('XXX 3.7\n', msg)
|
parser.parse_args(['-V'])
|
||||||
|
self.assertEqual('XXX 3.7\n', cm.exception.stdout)
|
||||||
|
|
||||||
def test_no_help(self):
|
def test_no_help(self):
|
||||||
parser = ErrorRaisingArgumentParser(add_help=False)
|
parser = ErrorRaisingArgumentParser(add_help=False)
|
||||||
|
@ -4600,14 +4564,10 @@ class TestArgumentTypeError(TestCase):
|
||||||
|
|
||||||
parser = ErrorRaisingArgumentParser(prog='PROG', add_help=False)
|
parser = ErrorRaisingArgumentParser(prog='PROG', add_help=False)
|
||||||
parser.add_argument('x', type=spam)
|
parser.add_argument('x', type=spam)
|
||||||
try:
|
with self.assertRaises(ArgumentParserError) as cm:
|
||||||
parser.parse_args(['XXX'])
|
parser.parse_args(['XXX'])
|
||||||
except ArgumentParserError:
|
self.assertEqual('usage: PROG x\nPROG: error: argument x: spam!\n',
|
||||||
expected = 'usage: PROG x\nPROG: error: argument x: spam!\n'
|
cm.exception.stderr)
|
||||||
msg = sys.exc_info()[1].stderr
|
|
||||||
self.assertEqual(expected, msg)
|
|
||||||
else:
|
|
||||||
self.fail()
|
|
||||||
|
|
||||||
# =========================
|
# =========================
|
||||||
# MessageContentError tests
|
# MessageContentError tests
|
||||||
|
|
|
@ -1399,6 +1399,7 @@ Norman Vine
|
||||||
Pauli Virtanen
|
Pauli Virtanen
|
||||||
Frank Visser
|
Frank Visser
|
||||||
Johannes Vogel
|
Johannes Vogel
|
||||||
|
Radu Voicilas
|
||||||
Alex Volkov
|
Alex Volkov
|
||||||
Martijn Vries
|
Martijn Vries
|
||||||
Sjoerd de Vries
|
Sjoerd de Vries
|
||||||
|
|
|
@ -653,6 +653,9 @@ Documentation
|
||||||
Tests
|
Tests
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
- Issue #9554: Use modern unittest features in test_argparse. Initial patch by
|
||||||
|
Denver Coneybeare and Radu Voicilas.
|
||||||
|
|
||||||
- Issue #20155: Changed HTTP method names in failing tests in test_httpservers
|
- Issue #20155: Changed HTTP method names in failing tests in test_httpservers
|
||||||
so that packet filtering software (specifically Windows Base Filtering Engine)
|
so that packet filtering software (specifically Windows Base Filtering Engine)
|
||||||
does not interfere with the transaction semantics expected by the tests.
|
does not interfere with the transaction semantics expected by the tests.
|
||||||
|
|
Loading…
Reference in New Issue