Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Private Member Functions
test.test_argparse.TestAddSubparsers Class Reference
Inheritance diagram for test.test_argparse.TestAddSubparsers:
Inheritance graph
[legend]
Collaboration diagram for test.test_argparse.TestAddSubparsers:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def assertArgumentParserError
def setUp
def test_parse_args_failures
def test_parse_args
def test_parse_known_args
def test_dest
def test_help
def test_help_extra_prefix_chars
def test_help_alternate_prefix_chars
def test_parser_command_help
def test_subparser_title_help
def test_subparser1_help
def test_subparser2_help
def test_alias_invocation
def test_error_alias_invocation
def test_alias_help
def assertEqual

Public Attributes

 parser
 command_help_parser
 maxDiff

Private Member Functions

def _get_parser
def _test_subparser_help

Detailed Description

Test the add_subparsers method

Definition at line 1711 of file test_argparse.py.


Member Function Documentation

def test.test_argparse.TestAddSubparsers._get_parser (   self,
  subparser_help = False,
  prefix_chars = None,
  aliases = False 
) [private]

Definition at line 1718 of file test_argparse.py.

01718 
01719                     aliases=False):
01720         # create a parser with a subparsers argument
01721         if prefix_chars:
01722             parser = ErrorRaisingArgumentParser(
01723                 prog='PROG', description='main description', prefix_chars=prefix_chars)
01724             parser.add_argument(
01725                 prefix_chars[0] * 2 + 'foo', action='store_true', help='foo help')
01726         else:
01727             parser = ErrorRaisingArgumentParser(
01728                 prog='PROG', description='main description')
01729             parser.add_argument(
01730                 '--foo', action='store_true', help='foo help')
01731         parser.add_argument(
01732             'bar', type=float, help='bar help')
01733 
01734         # check that only one subparsers argument can be added
01735         subparsers_kwargs = {}
01736         if aliases:
01737             subparsers_kwargs['metavar'] = 'COMMAND'
01738             subparsers_kwargs['title'] = 'commands'
01739         else:
01740             subparsers_kwargs['help'] = 'command help'
01741         subparsers = parser.add_subparsers(**subparsers_kwargs)
01742         self.assertArgumentParserError(parser.add_subparsers)
01743 
01744         # add first sub-parser
01745         parser1_kwargs = dict(description='1 description')
01746         if subparser_help:
01747             parser1_kwargs['help'] = '1 help'
01748         if aliases:
01749             parser1_kwargs['aliases'] = ['1alias1', '1alias2']
01750         parser1 = subparsers.add_parser('1', **parser1_kwargs)
01751         parser1.add_argument('-w', type=int, help='w help')
01752         parser1.add_argument('x', choices='abc', help='x help')
01753 
01754         # add second sub-parser
01755         parser2_kwargs = dict(description='2 description')
01756         if subparser_help:
01757             parser2_kwargs['help'] = '2 help'
01758         parser2 = subparsers.add_parser('2', **parser2_kwargs)
01759         parser2.add_argument('-y', choices='123', help='y help')
01760         parser2.add_argument('z', type=complex, nargs='*', help='z help')
01761 
01762         # return the main parser
01763         return parser

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_argparse.TestAddSubparsers._test_subparser_help (   self,
  args_str,
  expected_help 
) [private]

Definition at line 1927 of file test_argparse.py.

01927 
01928     def _test_subparser_help(self, args_str, expected_help):
01929         try:
01930             self.parser.parse_args(args_str.split())
01931         except ArgumentParserError:
01932             err = sys.exc_info()[1]
01933             if err.stdout != expected_help:
01934                 print(repr(expected_help))
01935                 print(repr(err.stdout))
01936             self.assertEqual(err.stdout, expected_help)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 1714 of file test_argparse.py.

01714 
01715     def assertArgumentParserError(self, *args, **kwargs):
01716         self.assertRaises(ArgumentParserError, *args, **kwargs)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_argparse.TestCase.assertEqual (   self,
  obj1,
  obj2 
) [inherited]

Definition at line 22 of file test_argparse.py.

00022 
00023     def assertEqual(self, obj1, obj2):
00024         if obj1 != obj2:
00025             print('')
00026             print(repr(obj1))
00027             print(repr(obj2))
00028             print(obj1)
00029             print(obj2)
00030         super(TestCase, self).assertEqual(obj1, obj2)

Reimplemented from test.test_argparse.TestCase.

Definition at line 1764 of file test_argparse.py.

01764 
01765     def setUp(self):
01766         super().setUp()
01767         self.parser = self._get_parser()
01768         self.command_help_parser = self._get_parser(subparser_help=True)

Here is the caller graph for this function:

Definition at line 1981 of file test_argparse.py.

01981 
01982     def test_alias_help(self):
01983         parser = self._get_parser(aliases=True, subparser_help=True)
01984         self.maxDiff = None
01985         self.assertEqual(parser.format_help(), textwrap.dedent("""\
01986             usage: PROG [-h] [--foo] bar COMMAND ...
01987 
01988             main description
01989 
01990             positional arguments:
01991               bar                   bar help
01992 
01993             optional arguments:
01994               -h, --help            show this help message and exit
01995               --foo                 foo help
01996 
01997             commands:
01998               COMMAND
01999                 1 (1alias1, 1alias2)
02000                                     1 help
02001                 2                   2 help
02002             """))
02003 
02004 # ============
02005 # Groups tests
02006 # ============

Here is the call graph for this function:

Definition at line 1965 of file test_argparse.py.

01965 
01966     def test_alias_invocation(self):
01967         parser = self._get_parser(aliases=True)
01968         self.assertEqual(
01969             parser.parse_known_args('0.5 1alias1 b'.split()),
01970             (NS(foo=False, bar=0.5, w=None, x='b'), []),
01971         )
01972         self.assertEqual(
01973             parser.parse_known_args('0.5 1alias2 b'.split()),
01974             (NS(foo=False, bar=0.5, w=None, x='b'), []),
01975         )

Here is the call graph for this function:

Definition at line 1813 of file test_argparse.py.

01813 
01814     def test_dest(self):
01815         parser = ErrorRaisingArgumentParser()
01816         parser.add_argument('--foo', action='store_true')
01817         subparsers = parser.add_subparsers(dest='bar')
01818         parser1 = subparsers.add_parser('1')
01819         parser1.add_argument('baz')
01820         self.assertEqual(NS(foo=False, bar='1', baz='2'),
01821                          parser.parse_args('1 2'.split()))

Here is the call graph for this function:

Definition at line 1976 of file test_argparse.py.

01976 
01977     def test_error_alias_invocation(self):
01978         parser = self._get_parser(aliases=True)
01979         self.assertArgumentParserError(parser.parse_args,
01980                                        '0.5 1alias3 b'.split())

Here is the call graph for this function:

Definition at line 1822 of file test_argparse.py.

01822 
01823     def test_help(self):
01824         self.assertEqual(self.parser.format_usage(),
01825                          'usage: PROG [-h] [--foo] bar {1,2} ...\n')
01826         self.assertEqual(self.parser.format_help(), textwrap.dedent('''\
01827             usage: PROG [-h] [--foo] bar {1,2} ...
01828 
01829             main description
01830 
01831             positional arguments:
01832               bar         bar help
01833               {1,2}       command help
01834 
01835             optional arguments:
01836               -h, --help  show this help message and exit
01837               --foo       foo help
01838             '''))

Here is the call graph for this function:

Definition at line 1859 of file test_argparse.py.

01859 
01860     def test_help_alternate_prefix_chars(self):
01861         parser = self._get_parser(prefix_chars='+:/')
01862         self.assertEqual(parser.format_usage(),
01863                          'usage: PROG [+h] [++foo] bar {1,2} ...\n')
01864         self.assertEqual(parser.format_help(), textwrap.dedent('''\
01865             usage: PROG [+h] [++foo] bar {1,2} ...
01866 
01867             main description
01868 
01869             positional arguments:
01870               bar         bar help
01871               {1,2}       command help
01872 
01873             optional arguments:
01874               +h, ++help  show this help message and exit
01875               ++foo       foo help
01876             '''))

Here is the call graph for this function:

Definition at line 1839 of file test_argparse.py.

01839 
01840     def test_help_extra_prefix_chars(self):
01841         # Make sure - is still used for help if it is a non-first prefix char
01842         parser = self._get_parser(prefix_chars='+:-')
01843         self.assertEqual(parser.format_usage(),
01844                          'usage: PROG [-h] [++foo] bar {1,2} ...\n')
01845         self.assertEqual(parser.format_help(), textwrap.dedent('''\
01846             usage: PROG [-h] [++foo] bar {1,2} ...
01847 
01848             main description
01849 
01850             positional arguments:
01851               bar         bar help
01852               {1,2}       command help
01853 
01854             optional arguments:
01855               -h, --help  show this help message and exit
01856               ++foo       foo help
01857             '''))
01858 

Here is the call graph for this function:

Definition at line 1776 of file test_argparse.py.

01776 
01777     def test_parse_args(self):
01778         # check some non-failure cases:
01779         self.assertEqual(
01780             self.parser.parse_args('0.5 1 b -w 7'.split()),
01781             NS(foo=False, bar=0.5, w=7, x='b'),
01782         )
01783         self.assertEqual(
01784             self.parser.parse_args('0.25 --foo 2 -y 2 3j -- -1j'.split()),
01785             NS(foo=True, bar=0.25, y='2', z=[3j, -1j]),
01786         )
01787         self.assertEqual(
01788             self.parser.parse_args('--foo 0.125 1 c'.split()),
01789             NS(foo=True, bar=0.125, w=None, x='c'),
01790         )

Here is the call graph for this function:

Definition at line 1769 of file test_argparse.py.

01769 
01770     def test_parse_args_failures(self):
01771         # check some failure cases:
01772         for args_str in ['', 'a', 'a a', '0.5 a', '0.5 1',
01773                          '0.5 1 -y', '0.5 2 -w']:
01774             args = args_str.split()
01775             self.assertArgumentParserError(self.parser.parse_args, args)

Here is the call graph for this function:

Definition at line 1791 of file test_argparse.py.

01791 
01792     def test_parse_known_args(self):
01793         self.assertEqual(
01794             self.parser.parse_known_args('0.5 1 b -w 7'.split()),
01795             (NS(foo=False, bar=0.5, w=7, x='b'), []),
01796         )
01797         self.assertEqual(
01798             self.parser.parse_known_args('0.5 -p 1 b -w 7'.split()),
01799             (NS(foo=False, bar=0.5, w=7, x='b'), ['-p']),
01800         )
01801         self.assertEqual(
01802             self.parser.parse_known_args('0.5 1 b -w 7 -p'.split()),
01803             (NS(foo=False, bar=0.5, w=7, x='b'), ['-p']),
01804         )
01805         self.assertEqual(
01806             self.parser.parse_known_args('0.5 1 b -q -rs -w 7'.split()),
01807             (NS(foo=False, bar=0.5, w=7, x='b'), ['-q', '-rs']),
01808         )
01809         self.assertEqual(
01810             self.parser.parse_known_args('0.5 -W 1 b -X Y -w 7 Z'.split()),
01811             (NS(foo=False, bar=0.5, w=7, x='b'), ['-W', '-X', 'Y', 'Z']),
01812         )

Here is the call graph for this function:

Definition at line 1877 of file test_argparse.py.

01877 
01878     def test_parser_command_help(self):
01879         self.assertEqual(self.command_help_parser.format_usage(),
01880                          'usage: PROG [-h] [--foo] bar {1,2} ...\n')
01881         self.assertEqual(self.command_help_parser.format_help(),
01882                          textwrap.dedent('''\
01883             usage: PROG [-h] [--foo] bar {1,2} ...
01884 
01885             main description
01886 
01887             positional arguments:
01888               bar         bar help
01889               {1,2}       command help
01890                 1         1 help
01891                 2         2 help
01892 
01893             optional arguments:
01894               -h, --help  show this help message and exit
01895               --foo       foo help
01896             '''))

Here is the call graph for this function:

Definition at line 1937 of file test_argparse.py.

01937 
01938     def test_subparser1_help(self):
01939         self._test_subparser_help('5.0 1 -h', textwrap.dedent('''\
01940             usage: PROG bar 1 [-h] [-w W] {a,b,c}
01941 
01942             1 description
01943 
01944             positional arguments:
01945               {a,b,c}     x help
01946 
01947             optional arguments:
01948               -h, --help  show this help message and exit
01949               -w W        w help
01950             '''))

Here is the call graph for this function:

Definition at line 1951 of file test_argparse.py.

01951 
01952     def test_subparser2_help(self):
01953         self._test_subparser_help('5.0 2 -h', textwrap.dedent('''\
01954             usage: PROG bar 2 [-h] [-y {1,2,3}] [z [z ...]]
01955 
01956             2 description
01957 
01958             positional arguments:
01959               z           z help
01960 
01961             optional arguments:
01962               -h, --help  show this help message and exit
01963               -y {1,2,3}  y help
01964             '''))

Here is the call graph for this function:

Definition at line 1897 of file test_argparse.py.

01897 
01898     def test_subparser_title_help(self):
01899         parser = ErrorRaisingArgumentParser(prog='PROG',
01900                                             description='main description')
01901         parser.add_argument('--foo', action='store_true', help='foo help')
01902         parser.add_argument('bar', help='bar help')
01903         subparsers = parser.add_subparsers(title='subcommands',
01904                                            description='command help',
01905                                            help='additional text')
01906         parser1 = subparsers.add_parser('1')
01907         parser2 = subparsers.add_parser('2')
01908         self.assertEqual(parser.format_usage(),
01909                          'usage: PROG [-h] [--foo] bar {1,2} ...\n')
01910         self.assertEqual(parser.format_help(), textwrap.dedent('''\
01911             usage: PROG [-h] [--foo] bar {1,2} ...
01912 
01913             main description
01914 
01915             positional arguments:
01916               bar         bar help
01917 
01918             optional arguments:
01919               -h, --help  show this help message and exit
01920               --foo       foo help
01921 
01922             subcommands:
01923               command help
01924 
01925               {1,2}       additional text
01926             '''))

Here is the call graph for this function:


Member Data Documentation

Definition at line 1767 of file test_argparse.py.

Definition at line 1983 of file test_argparse.py.

Definition at line 1766 of file test_argparse.py.


The documentation for this class was generated from the following file: