Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(39402)

Delta Between Two Patch Sets: Lib/test/test_argparse.py

Issue 9253: argparse: optional subparsers
Left Patch Set: Created 7 years, 8 months ago
Right Patch Set: Created 6 years, 5 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/argparse.py ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(no file at all)
1 # Author: Steven J. Bethard <steven.bethard@gmail.com>. 1 # Author: Steven J. Bethard <steven.bethard@gmail.com>.
2 2
3 import codecs 3 import codecs
4 import inspect 4 import inspect
5 import os 5 import os
6 import shutil 6 import shutil
7 import stat 7 import stat
8 import sys 8 import sys
9 import textwrap 9 import textwrap
10 import tempfile 10 import tempfile
(...skipping 1815 matching lines...) Expand 10 before | Expand all | Expand 10 after
1826 self.parser = self._get_parser() 1826 self.parser = self._get_parser()
1827 self.command_help_parser = self._get_parser(subparser_help=True) 1827 self.command_help_parser = self._get_parser(subparser_help=True)
1828 1828
1829 def test_parse_args_failures(self): 1829 def test_parse_args_failures(self):
1830 # check some failure cases: 1830 # check some failure cases:
1831 for args_str in ['', 'a', 'a a', '0.5 a', '0.5 1', 1831 for args_str in ['', 'a', 'a a', '0.5 a', '0.5 1',
1832 '0.5 1 -y', '0.5 2 -w']: 1832 '0.5 1 -y', '0.5 2 -w']:
1833 args = args_str.split() 1833 args = args_str.split()
1834 self.assertArgumentParserError(self.parser.parse_args, args) 1834 self.assertArgumentParserError(self.parser.parse_args, args)
1835 1835
1836 def test_parse_args_missing_cmd(self):
1837 # test error msg when cmd is missing
1838 for args_str,reg_str in [['0.5','the following arguments are required']] :
1839 args = args_str.split()
1840 self.assertRaisesRegexp(ArgumentParserError, reg_str,
1841 self.parser.parse_args, args)
1842 # was - no error (effectively optional argument)
1843
1844 def test_parse_args_invalid_choice(self):
1845 # test error msg when cmd is wrong choice
1846 for args_str,reg_str in [['0.5 0','error: argument {1,2,3}: invalid choi ce:']]:
1847 args = args_str.split()
1848 self.assertRaisesRegexp(ArgumentParserError, reg_str,
1849 self.parser.parse_args, args)
1850 # was - 'error: invalid choice:'
1851
1852 def test_required(self):
1853 # test ability to change 'required'; default is True
1854 parser = ErrorRaisingArgumentParser()
1855 parser.add_argument('--foo', action='store_true')
1856 subparsers = parser.add_subparsers(required=False)
1857 parser1 = subparsers.add_parser('1')
1858 parser1.add_argument('baz')
1859 self.assertEqual(NS(foo=False), parser.parse_args([]))
1860
1836 def test_parse_args(self): 1861 def test_parse_args(self):
1837 # check some non-failure cases: 1862 # check some non-failure cases:
1838 self.assertEqual( 1863 self.assertEqual(
1839 self.parser.parse_args('0.5 1 b -w 7'.split()), 1864 self.parser.parse_args('0.5 1 b -w 7'.split()),
1840 NS(foo=False, bar=0.5, w=7, x='b'), 1865 NS(foo=False, bar=0.5, w=7, x='b'),
1841 ) 1866 )
1842 self.assertEqual( 1867 self.assertEqual(
1843 self.parser.parse_args('0.25 --foo 2 -y 2 3j -- -1j'.split()), 1868 self.parser.parse_args('0.25 --foo 2 -y 2 3j -- -1j'.split()),
1844 NS(foo=True, bar=0.25, y='2', z=[3j, -1j]), 1869 NS(foo=True, bar=0.25, y='2', z=[3j, -1j]),
1845 ) 1870 )
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
2035 ) 2060 )
2036 self.assertEqual( 2061 self.assertEqual(
2037 parser.parse_known_args('0.5 1alias2 b'.split()), 2062 parser.parse_known_args('0.5 1alias2 b'.split()),
2038 (NS(foo=False, bar=0.5, w=None, x='b'), []), 2063 (NS(foo=False, bar=0.5, w=None, x='b'), []),
2039 ) 2064 )
2040 2065
2041 def test_error_alias_invocation(self): 2066 def test_error_alias_invocation(self):
2042 parser = self._get_parser(aliases=True) 2067 parser = self._get_parser(aliases=True)
2043 self.assertArgumentParserError(parser.parse_args, 2068 self.assertArgumentParserError(parser.parse_args,
2044 '0.5 1alias3 b'.split()) 2069 '0.5 1alias3 b'.split())
2070 # with metavar, the error message has not changed
2071 self.assertRaisesRegexp(ArgumentParserError,
2072 'error: argument COMMAND: invalid choice:',
2073 parser.parse_args, '0.5 1alias3 b'.split())
2045 2074
2046 def test_alias_help(self): 2075 def test_alias_help(self):
2047 parser = self._get_parser(aliases=True, subparser_help=True) 2076 parser = self._get_parser(aliases=True, subparser_help=True)
2048 self.maxDiff = None 2077 self.maxDiff = None
2049 self.assertEqual(parser.format_help(), textwrap.dedent("""\ 2078 self.assertEqual(parser.format_help(), textwrap.dedent("""\
2050 usage: PROG [-h] [--foo] bar COMMAND ... 2079 usage: PROG [-h] [--foo] bar COMMAND ...
2051 2080
2052 main description 2081 main description
2053 2082
2054 positional arguments: 2083 positional arguments:
(...skipping 2852 matching lines...) Expand 10 before | Expand all | Expand 10 after
4907 def test_main(): 4936 def test_main():
4908 support.run_unittest(__name__) 4937 support.run_unittest(__name__)
4909 # Remove global references to avoid looking like we have refleaks. 4938 # Remove global references to avoid looking like we have refleaks.
4910 RFile.seen = {} 4939 RFile.seen = {}
4911 WFile.seen = set() 4940 WFile.seen = set()
4912 4941
4913 4942
4914 4943
4915 if __name__ == '__main__': 4944 if __name__ == '__main__':
4916 test_main() 4945 test_main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+