Back to index

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

List of all members.

Public Member Functions

def __init__

Public Attributes

 func_suffix
 std_name

Detailed Description

Definition at line 2811 of file test_argparse.py.


Constructor & Destructor Documentation

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

Definition at line 2813 of file test_argparse.py.

02813 
02814     def __init__(cls, name, bases, bodydict):
02815         if name == 'HelpTestCase':
02816             return
02817 
02818         class AddTests(object):
02819 
02820             def __init__(self, test_class, func_suffix, std_name):
02821                 self.func_suffix = func_suffix
02822                 self.std_name = std_name
02823 
02824                 for test_func in [self.test_format,
02825                                   self.test_print,
02826                                   self.test_print_file]:
02827                     test_name = '%s_%s' % (test_func.__name__, func_suffix)
02828 
02829                     def test_wrapper(self, test_func=test_func):
02830                         test_func(self)
02831                     try:
02832                         test_wrapper.__name__ = test_name
02833                     except TypeError:
02834                         pass
02835                     setattr(test_class, test_name, test_wrapper)
02836 
02837             def _get_parser(self, tester):
02838                 parser = argparse.ArgumentParser(
02839                     *tester.parser_signature.args,
02840                     **tester.parser_signature.kwargs)
02841                 for argument_sig in getattr(tester, 'argument_signatures', []):
02842                     parser.add_argument(*argument_sig.args,
02843                                         **argument_sig.kwargs)
02844                 group_sigs = getattr(tester, 'argument_group_signatures', [])
02845                 for group_sig, argument_sigs in group_sigs:
02846                     group = parser.add_argument_group(*group_sig.args,
02847                                                       **group_sig.kwargs)
02848                     for argument_sig in argument_sigs:
02849                         group.add_argument(*argument_sig.args,
02850                                            **argument_sig.kwargs)
02851                 subparsers_sigs = getattr(tester, 'subparsers_signatures', [])
02852                 if subparsers_sigs:
02853                     subparsers = parser.add_subparsers()
02854                     for subparser_sig in subparsers_sigs:
02855                         subparsers.add_parser(*subparser_sig.args,
02856                                                **subparser_sig.kwargs)
02857                 return parser
02858 
02859             def _test(self, tester, parser_text):
02860                 expected_text = getattr(tester, self.func_suffix)
02861                 expected_text = textwrap.dedent(expected_text)
02862                 if expected_text != parser_text:
02863                     print(repr(expected_text))
02864                     print(repr(parser_text))
02865                     for char1, char2 in zip(expected_text, parser_text):
02866                         if char1 != char2:
02867                             print('first diff: %r %r' % (char1, char2))
02868                             break
02869                 tester.assertEqual(expected_text, parser_text)
02870 
02871             def test_format(self, tester):
02872                 parser = self._get_parser(tester)
02873                 format = getattr(parser, 'format_%s' % self.func_suffix)
02874                 self._test(tester, format())
02875 
02876             def test_print(self, tester):
02877                 parser = self._get_parser(tester)
02878                 print_ = getattr(parser, 'print_%s' % self.func_suffix)
02879                 old_stream = getattr(sys, self.std_name)
02880                 setattr(sys, self.std_name, StdIOBuffer())
02881                 try:
02882                     print_()
02883                     parser_text = getattr(sys, self.std_name).getvalue()
02884                 finally:
02885                     setattr(sys, self.std_name, old_stream)
02886                 self._test(tester, parser_text)
02887 
02888             def test_print_file(self, tester):
02889                 parser = self._get_parser(tester)
02890                 print_ = getattr(parser, 'print_%s' % self.func_suffix)
02891                 sfile = StdIOBuffer()
02892                 print_(sfile)
02893                 parser_text = sfile.getvalue()
02894                 self._test(tester, parser_text)
02895 
02896         # add tests for {format,print}_{usage,help,version}
02897         for func_suffix, std_name in [('usage', 'stdout'),
02898                                       ('help', 'stdout'),
02899                                       ('version', 'stderr')]:
02900             AddTests(cls, func_suffix, std_name)

Here is the caller graph for this function:


Member Data Documentation

Definition at line 2820 of file test_argparse.py.

Definition at line 2821 of file test_argparse.py.


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