Back to index

python3.2  3.2.2
Public Member Functions | Private Attributes
test.test_argparse.ParserTesterMetaclass Class Reference

List of all members.

Public Member Functions

def __init__

Private Attributes

 _add_arguments
 _parse_args

Detailed Description

Adds parser tests using the class attributes.

Classes of this type should specify the following attributes:

argument_signatures -- a list of Sig objects which specify
    the signatures of Argument objects to be created
failures -- a list of args lists that should cause the parser
    to fail
successes -- a list of (initial_args, options, remaining_args) tuples
    where initial_args specifies the string args to be parsed,
    options is a dict that should match the vars() of the options
    parsed out of initial_args, and remaining_args should be any
    remaining unparsed arguments

Definition at line 138 of file test_argparse.py.


Constructor & Destructor Documentation

def test.test_argparse.ParserTesterMetaclass.__init__ (   cls,
  name,
  bases,
  bodydict 
)

Definition at line 154 of file test_argparse.py.

00154 
00155     def __init__(cls, name, bases, bodydict):
00156         if name == 'ParserTestCase':
00157             return
00158 
00159         # default parser signature is empty
00160         if not hasattr(cls, 'parser_signature'):
00161             cls.parser_signature = Sig()
00162         if not hasattr(cls, 'parser_class'):
00163             cls.parser_class = ErrorRaisingArgumentParser
00164 
00165         # ---------------------------------------
00166         # functions for adding optional arguments
00167         # ---------------------------------------
00168         def no_groups(parser, argument_signatures):
00169             """Add all arguments directly to the parser"""
00170             for sig in argument_signatures:
00171                 parser.add_argument(*sig.args, **sig.kwargs)
00172 
00173         def one_group(parser, argument_signatures):
00174             """Add all arguments under a single group in the parser"""
00175             group = parser.add_argument_group('foo')
00176             for sig in argument_signatures:
00177                 group.add_argument(*sig.args, **sig.kwargs)
00178 
00179         def many_groups(parser, argument_signatures):
00180             """Add each argument in its own group to the parser"""
00181             for i, sig in enumerate(argument_signatures):
00182                 group = parser.add_argument_group('foo:%i' % i)
00183                 group.add_argument(*sig.args, **sig.kwargs)
00184 
00185         # --------------------------
00186         # functions for parsing args
00187         # --------------------------
00188         def listargs(parser, args):
00189             """Parse the args by passing in a list"""
00190             return parser.parse_args(args)
00191 
00192         def sysargs(parser, args):
00193             """Parse the args by defaulting to sys.argv"""
00194             old_sys_argv = sys.argv
00195             sys.argv = [old_sys_argv[0]] + args
00196             try:
00197                 return parser.parse_args()
00198             finally:
00199                 sys.argv = old_sys_argv
00200 
00201         # class that holds the combination of one optional argument
00202         # addition method and one arg parsing method
00203         class AddTests(object):
00204 
00205             def __init__(self, tester_cls, add_arguments, parse_args):
00206                 self._add_arguments = add_arguments
00207                 self._parse_args = parse_args
00208 
00209                 add_arguments_name = self._add_arguments.__name__
00210                 parse_args_name = self._parse_args.__name__
00211                 for test_func in [self.test_failures, self.test_successes]:
00212                     func_name = test_func.__name__
00213                     names = func_name, add_arguments_name, parse_args_name
00214                     test_name = '_'.join(names)
00215 
00216                     def wrapper(self, test_func=test_func):
00217                         test_func(self)
00218                     try:
00219                         wrapper.__name__ = test_name
00220                     except TypeError:
00221                         pass
00222                     setattr(tester_cls, test_name, wrapper)
00223 
00224             def _get_parser(self, tester):
00225                 args = tester.parser_signature.args
00226                 kwargs = tester.parser_signature.kwargs
00227                 parser = tester.parser_class(*args, **kwargs)
00228                 self._add_arguments(parser, tester.argument_signatures)
00229                 return parser
00230 
00231             def test_failures(self, tester):
00232                 parser = self._get_parser(tester)
00233                 for args_str in tester.failures:
00234                     args = args_str.split()
00235                     raises = tester.assertRaises
00236                     raises(ArgumentParserError, parser.parse_args, args)
00237 
00238             def test_successes(self, tester):
00239                 parser = self._get_parser(tester)
00240                 for args, expected_ns in tester.successes:
00241                     if isinstance(args, str):
00242                         args = args.split()
00243                     result_ns = self._parse_args(parser, args)
00244                     tester.assertEqual(expected_ns, result_ns)
00245 
00246         # add tests for each combination of an optionals adding method
00247         # and an arg parsing method
00248         for add_arguments in [no_groups, one_group, many_groups]:
00249             for parse_args in [listargs, sysargs]:
00250                 AddTests(cls, add_arguments, parse_args)

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 205 of file test_argparse.py.

Definition at line 206 of file test_argparse.py.


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