import argparse
import warnings
-from test.support import os_helper
+from test.support import os_helper, captured_stderr
from unittest import mock
('a b c', NS(spam=['a', ['b', 'c']])),
]
+
+class TestPositionalsActionExtend(ParserTestCase):
+ """Test the 'extend' action"""
+
+ argument_signatures = [
+ Sig('spam', action='extend'),
+ Sig('spam', action='extend', nargs=2),
+ ]
+ failures = ['', '--foo', 'a', 'a b', 'a b c d']
+ successes = [
+ ('a b c', NS(spam=['a', 'b', 'c'])),
+ ]
+
# ========================================
# Combined optionals and positionals tests
# ========================================
]
+class TestOptionalsAndPositionalsAppend(ParserTestCase):
+ argument_signatures = [
+ Sig('foo', nargs='*', action='append'),
+ Sig('--bar'),
+ ]
+ failures = ['-foo']
+ successes = [
+ ('a b', NS(foo=[['a', 'b']], bar=None)),
+ ('--bar a b', NS(foo=[['b']], bar='a')),
+ ('a b --bar c', NS(foo=[['a', 'b']], bar='c')),
+ ]
+
+
+class TestOptionalsAndPositionalsExtend(ParserTestCase):
+ argument_signatures = [
+ Sig('foo', nargs='*', action='extend'),
+ Sig('--bar'),
+ ]
+ failures = ['-foo']
+ successes = [
+ ('a b', NS(foo=['a', 'b'], bar=None)),
+ ('--bar a b', NS(foo=['b'], bar='a')),
+ ('a b --bar c', NS(foo=['a', 'b'], bar='c')),
+ ]
+
+
class TestEmptyAndSpaceContainingArguments(ParserTestCase):
argument_signatures = [
type('foo')
m.assert_called_with('foo', *args)
+ def test_invalid_file_type(self):
+ with self.assertRaises(ValueError):
+ argparse.FileType('b')('-test')
+
class TestFileTypeMissingInitialization(TestCase):
"""
('--foo f1 --foo f2 f3 f4', NS(foo=['f1', 'f2', 'f3', 'f4'])),
]
+
+class TestInvalidAction(TestCase):
+ """Test invalid user defined Action"""
+
+ class ActionWithoutCall(argparse.Action):
+ pass
+
+ def test_invalid_type(self):
+ parser = argparse.ArgumentParser()
+
+ parser.add_argument('--foo', action=self.ActionWithoutCall)
+ self.assertRaises(NotImplementedError, parser.parse_args, ['--foo', 'bar'])
+
+ def test_modified_invalid_action(self):
+ parser = ErrorRaisingArgumentParser()
+ action = parser.add_argument('--foo')
+ # Someone got crazy and did this
+ action.type = 1
+ self.assertRaises(ArgumentParserError, parser.parse_args, ['--foo', 'bar'])
+
+
# ================
# Subparsers tests
# ================
-x X
'''.format(progname, ' ' if progname else '' )))
+ def test_wrong_type_parents(self):
+ self.assertRaises(TypeError, ErrorRaisingArgumentParser, parents=[1])
+
# ==============================
# Mutually exclusive group tests
# ==============================
self.assertValueError('--')
self.assertValueError('---')
+ def test_invalid_prefix(self):
+ self.assertValueError('--foo', '+foo')
+
def test_invalid_type(self):
self.assertValueError('--foo', type='int')
self.assertValueError('--foo', type=(int, float))
self.assertTypeError('command', action='parsers',
parser_class=argparse.ArgumentParser)
+ def test_version_missing_params(self):
+ self.assertTypeError('command', action='version')
+
def test_required_positional(self):
self.assertTypeError('foo', required=True)
self.assertRaises(TypeError, parser.parse_intermixed_args, [])
self.assertEqual(group.required, True)
+ def test_invalid_args(self):
+ parser = ErrorRaisingArgumentParser(prog='PROG')
+ self.assertRaises(ArgumentParserError, parser.parse_intermixed_args, ['a'])
+
+ parser = ErrorRaisingArgumentParser(prog='PROG')
+ parser.add_argument('--foo', nargs="*")
+ parser.add_argument('foo')
+ with captured_stderr() as stderr:
+ parser.parse_intermixed_args(['hello', '--foo'])
+ self.assertIn("UserWarning", stderr.getvalue())
+
class TestIntermixedMessageContentError(TestCase):
# case where Intermixed gives different error message
# error is raised by 1st parsing step