|
15 | 15 | import argparse
|
16 | 16 | import warnings
|
17 | 17 |
|
18 |
| -from test.support import os_helper |
| 18 | +from test.support import os_helper, captured_stderr |
19 | 19 | from unittest import mock
|
20 | 20 |
|
21 | 21 |
|
@@ -1382,6 +1382,19 @@ class TestPositionalsActionAppend(ParserTestCase):
|
1382 | 1382 | ('a b c', NS(spam=['a', ['b', 'c']])),
|
1383 | 1383 | ]
|
1384 | 1384 |
|
| 1385 | + |
| 1386 | +class TestPositionalsActionExtend(ParserTestCase): |
| 1387 | + """Test the 'extend' action""" |
| 1388 | + |
| 1389 | + argument_signatures = [ |
| 1390 | + Sig('spam', action='extend'), |
| 1391 | + Sig('spam', action='extend', nargs=2), |
| 1392 | + ] |
| 1393 | + failures = ['', '--foo', 'a', 'a b', 'a b c d'] |
| 1394 | + successes = [ |
| 1395 | + ('a b c', NS(spam=['a', 'b', 'c'])), |
| 1396 | + ] |
| 1397 | + |
1385 | 1398 | # ========================================
|
1386 | 1399 | # Combined optionals and positionals tests
|
1387 | 1400 | # ========================================
|
@@ -1419,6 +1432,32 @@ class TestOptionalsAlmostNumericAndPositionals(ParserTestCase):
|
1419 | 1432 | ]
|
1420 | 1433 |
|
1421 | 1434 |
|
| 1435 | +class TestOptionalsAndPositionalsAppend(ParserTestCase): |
| 1436 | + argument_signatures = [ |
| 1437 | + Sig('foo', nargs='*', action='append'), |
| 1438 | + Sig('--bar'), |
| 1439 | + ] |
| 1440 | + failures = ['-foo'] |
| 1441 | + successes = [ |
| 1442 | + ('a b', NS(foo=[['a', 'b']], bar=None)), |
| 1443 | + ('--bar a b', NS(foo=[['b']], bar='a')), |
| 1444 | + ('a b --bar c', NS(foo=[['a', 'b']], bar='c')), |
| 1445 | + ] |
| 1446 | + |
| 1447 | + |
| 1448 | +class TestOptionalsAndPositionalsExtend(ParserTestCase): |
| 1449 | + argument_signatures = [ |
| 1450 | + Sig('foo', nargs='*', action='extend'), |
| 1451 | + Sig('--bar'), |
| 1452 | + ] |
| 1453 | + failures = ['-foo'] |
| 1454 | + successes = [ |
| 1455 | + ('a b', NS(foo=['a', 'b'], bar=None)), |
| 1456 | + ('--bar a b', NS(foo=['b'], bar='a')), |
| 1457 | + ('a b --bar c', NS(foo=['a', 'b'], bar='c')), |
| 1458 | + ] |
| 1459 | + |
| 1460 | + |
1422 | 1461 | class TestEmptyAndSpaceContainingArguments(ParserTestCase):
|
1423 | 1462 |
|
1424 | 1463 | argument_signatures = [
|
@@ -1899,6 +1938,10 @@ def test_open_args(self):
|
1899 | 1938 | type('foo')
|
1900 | 1939 | m.assert_called_with('foo', *args)
|
1901 | 1940 |
|
| 1941 | + def test_invalid_file_type(self): |
| 1942 | + with self.assertRaises(ValueError): |
| 1943 | + argparse.FileType('b')('-test') |
| 1944 | + |
1902 | 1945 |
|
1903 | 1946 | class TestFileTypeMissingInitialization(TestCase):
|
1904 | 1947 | """
|
@@ -2092,6 +2135,27 @@ class TestActionExtend(ParserTestCase):
|
2092 | 2135 | ('--foo f1 --foo f2 f3 f4', NS(foo=['f1', 'f2', 'f3', 'f4'])),
|
2093 | 2136 | ]
|
2094 | 2137 |
|
| 2138 | + |
| 2139 | +class TestInvalidAction(TestCase): |
| 2140 | + """Test invalid user defined Action""" |
| 2141 | + |
| 2142 | + class ActionWithoutCall(argparse.Action): |
| 2143 | + pass |
| 2144 | + |
| 2145 | + def test_invalid_type(self): |
| 2146 | + parser = argparse.ArgumentParser() |
| 2147 | + |
| 2148 | + parser.add_argument('--foo', action=self.ActionWithoutCall) |
| 2149 | + self.assertRaises(NotImplementedError, parser.parse_args, ['--foo', 'bar']) |
| 2150 | + |
| 2151 | + def test_modified_invalid_action(self): |
| 2152 | + parser = ErrorRaisingArgumentParser() |
| 2153 | + action = parser.add_argument('--foo') |
| 2154 | + # Someone got crazy and did this |
| 2155 | + action.type = 1 |
| 2156 | + self.assertRaises(ArgumentParserError, parser.parse_args, ['--foo', 'bar']) |
| 2157 | + |
| 2158 | + |
2095 | 2159 | # ================
|
2096 | 2160 | # Subparsers tests
|
2097 | 2161 | # ================
|
@@ -2727,6 +2791,9 @@ def test_groups_parents(self):
|
2727 | 2791 | -x X
|
2728 | 2792 | '''.format(progname, ' ' if progname else '' )))
|
2729 | 2793 |
|
| 2794 | + def test_wrong_type_parents(self): |
| 2795 | + self.assertRaises(TypeError, ErrorRaisingArgumentParser, parents=[1]) |
| 2796 | + |
2730 | 2797 | # ==============================
|
2731 | 2798 | # Mutually exclusive group tests
|
2732 | 2799 | # ==============================
|
@@ -4743,6 +4810,9 @@ def test_invalid_option_strings(self):
|
4743 | 4810 | self.assertValueError('--')
|
4744 | 4811 | self.assertValueError('---')
|
4745 | 4812 |
|
| 4813 | + def test_invalid_prefix(self): |
| 4814 | + self.assertValueError('--foo', '+foo') |
| 4815 | + |
4746 | 4816 | def test_invalid_type(self):
|
4747 | 4817 | self.assertValueError('--foo', type='int')
|
4748 | 4818 | self.assertValueError('--foo', type=(int, float))
|
@@ -4807,6 +4877,9 @@ def test_parsers_action_missing_params(self):
|
4807 | 4877 | self.assertTypeError('command', action='parsers',
|
4808 | 4878 | parser_class=argparse.ArgumentParser)
|
4809 | 4879 |
|
| 4880 | + def test_version_missing_params(self): |
| 4881 | + self.assertTypeError('command', action='version') |
| 4882 | + |
4810 | 4883 | def test_required_positional(self):
|
4811 | 4884 | self.assertTypeError('foo', required=True)
|
4812 | 4885 |
|
@@ -5400,6 +5473,17 @@ def test_exclusive_incompatible(self):
|
5400 | 5473 | self.assertRaises(TypeError, parser.parse_intermixed_args, [])
|
5401 | 5474 | self.assertEqual(group.required, True)
|
5402 | 5475 |
|
| 5476 | + def test_invalid_args(self): |
| 5477 | + parser = ErrorRaisingArgumentParser(prog='PROG') |
| 5478 | + self.assertRaises(ArgumentParserError, parser.parse_intermixed_args, ['a']) |
| 5479 | + |
| 5480 | + parser = ErrorRaisingArgumentParser(prog='PROG') |
| 5481 | + parser.add_argument('--foo', nargs="*") |
| 5482 | + parser.add_argument('foo') |
| 5483 | + with captured_stderr() as stderr: |
| 5484 | + parser.parse_intermixed_args(['hello', '--foo']) |
| 5485 | + self.assertIn("UserWarning", stderr.getvalue()) |
| 5486 | + |
5403 | 5487 | class TestIntermixedMessageContentError(TestCase):
|
5404 | 5488 | # case where Intermixed gives different error message
|
5405 | 5489 | # error is raised by 1st parsing step
|
|
0 commit comments