Back to index

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

List of all members.

Public Member Functions

def setUp
def make_parser
def assertHelpEquals
def test_help
def test_help_old_usage
def test_help_long_opts_first
def test_help_title_formatter
def test_wrap_columns
def test_help_unicode
def test_help_unicode_description
def test_help_description_groups
def assertParseOK
def assertRaises
def assertParseFail
def assertOutput
def assertTypeError
def assertHelp

Public Attributes

 parser

Detailed Description

Definition at line 1430 of file test_optparse.py.


Member Function Documentation

def test.test_optparse.BaseTest.assertHelp (   self,
  parser,
  expected_help 
) [inherited]

Definition at line 183 of file test_optparse.py.

00183 
00184     def assertHelp(self, parser, expected_help):
00185         actual_help = parser.format_help()
00186         if actual_help != expected_help:
00187             raise self.failureException(
00188                 'help text failure; expected:\n"' +
00189                 expected_help + '"; got:\n"' +
00190                 actual_help + '"\n')
00191 
00192 # -- Test make_option() aka Option -------------------------------------
00193 
00194 # It's not necessary to test correct options here.  All the tests in the
00195 # parser.parse_args() section deal with those, because they're needed
00196 # there.

Here is the caller graph for this function:

def test.test_optparse.TestHelp.assertHelpEquals (   self,
  expected_output 
)

Definition at line 1455 of file test_optparse.py.

01455 
01456     def assertHelpEquals(self, expected_output):
01457         save_argv = sys.argv[:]
01458         try:
01459             # Make optparse believe bar.py is being executed.
01460             sys.argv[0] = os.path.join("foo", "bar.py")
01461             self.assertOutput(["-h"], expected_output)
01462         finally:
01463             sys.argv[:] = save_argv

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_optparse.BaseTest.assertOutput (   self,
  cmdline_args,
  expected_output,
  expected_status = 0,
  expected_error = None 
) [inherited]
Assert the parser prints the expected output on stdout.

Definition at line 154 of file test_optparse.py.

00154 
00155                      expected_error=None):
00156         """Assert the parser prints the expected output on stdout."""
00157         save_stdout = sys.stdout
00158         try:
00159             try:
00160                 sys.stdout = StringIO()
00161                 self.parser.parse_args(cmdline_args)
00162             finally:
00163                 output = sys.stdout.getvalue()
00164                 sys.stdout = save_stdout
00165 
00166         except InterceptedError as err:
00167             self.assertTrue(
00168                 isinstance(output, str),
00169                 "expected output to be an ordinary string, not %r"
00170                 % type(output))
00171 
00172             if output != expected_output:
00173                 self.fail("expected: \n'''\n" + expected_output +
00174                           "'''\nbut got \n'''\n" + output + "'''")
00175             self.assertEqual(err.exit_status, expected_status)
00176             self.assertEqual(err.exit_message, expected_error)
00177         else:
00178             self.assertFalse("expected parser.exit()")

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_optparse.BaseTest.assertParseFail (   self,
  cmdline_args,
  expected_output 
) [inherited]
Assert the parser fails with the expected message.  Caller
must ensure that self.parser is an InterceptingOptionParser.

Definition at line 138 of file test_optparse.py.

00138 
00139     def assertParseFail(self, cmdline_args, expected_output):
00140         """
00141         Assert the parser fails with the expected message.  Caller
00142         must ensure that self.parser is an InterceptingOptionParser.
00143         """
00144         try:
00145             self.parser.parse_args(cmdline_args)
00146         except InterceptedError as err:
00147             self.assertEqual(err.error_message, expected_output)
00148         else:
00149             self.assertFalse("expected parse failure")

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_optparse.BaseTest.assertParseOK (   self,
  args,
  expected_opts,
  expected_positional_args 
) [inherited]
Assert the options are what we expected when parsing arguments.

Otherwise, fail with a nicely formatted message.

Keyword arguments:
args -- A list of arguments to parse with OptionParser.
expected_opts -- The options expected.
expected_positional_args -- The positional arguments expected.

Returns the options and positional args for further testing.

Definition at line 49 of file test_optparse.py.

00049 
00050     def assertParseOK(self, args, expected_opts, expected_positional_args):
00051         """Assert the options are what we expected when parsing arguments.
00052 
00053         Otherwise, fail with a nicely formatted message.
00054 
00055         Keyword arguments:
00056         args -- A list of arguments to parse with OptionParser.
00057         expected_opts -- The options expected.
00058         expected_positional_args -- The positional arguments expected.
00059 
00060         Returns the options and positional args for further testing.
00061         """
00062 
00063         (options, positional_args) = self.parser.parse_args(args)
00064         optdict = vars(options)
00065 
00066         self.assertEqual(optdict, expected_opts,
00067                          """
00068 Options are %(optdict)s.
00069 Should be %(expected_opts)s.
00070 Args were %(args)s.""" % locals())
00071 
00072         self.assertEqual(positional_args, expected_positional_args,
00073                          """
00074 Positional arguments are %(positional_args)s.
00075 Should be %(expected_positional_args)s.
00076 Args were %(args)s.""" % locals ())
00077 
00078         return (options, positional_args)

Here is the call graph for this function:

def test.test_optparse.BaseTest.assertRaises (   self,
  func,
  args,
  kwargs,
  expected_exception,
  expected_message 
) [inherited]
Assert that the expected exception is raised when calling a
function, and that the right error message is included with
that exception.

Arguments:
  func -- the function to call
  args -- positional arguments to `func`
  kwargs -- keyword arguments to `func`
  expected_exception -- exception that should be raised
  expected_message -- expected exception message (or pattern
    if a compiled regex object)

Returns the exception raised for further testing.

Definition at line 84 of file test_optparse.py.

00084 
00085                      expected_message):
00086         """
00087         Assert that the expected exception is raised when calling a
00088         function, and that the right error message is included with
00089         that exception.
00090 
00091         Arguments:
00092           func -- the function to call
00093           args -- positional arguments to `func`
00094           kwargs -- keyword arguments to `func`
00095           expected_exception -- exception that should be raised
00096           expected_message -- expected exception message (or pattern
00097             if a compiled regex object)
00098 
00099         Returns the exception raised for further testing.
00100         """
00101         if args is None:
00102             args = ()
00103         if kwargs is None:
00104             kwargs = {}
00105 
00106         try:
00107             func(*args, **kwargs)
00108         except expected_exception as err:
00109             actual_message = str(err)
00110             if isinstance(expected_message, retype):
00111                 self.assertTrue(expected_message.search(actual_message),
00112                              """\
00113 expected exception message pattern:
00114 /%s/
00115 actual exception message:
00116 '''%s'''
00117 """ % (expected_message.pattern, actual_message))
00118             else:
00119                 self.assertEqual(actual_message,
00120                                  expected_message,
00121                                  """\
00122 expected exception message:
00123 '''%s'''
00124 actual exception message:
00125 '''%s'''
00126 """ % (expected_message, actual_message))
00127 
00128             return err
00129         else:
00130             self.fail("""expected exception %(expected_exception)s not raised
00131 called %(func)r
00132 with args %(args)r
00133 and kwargs %(kwargs)r
00134 """ % locals ())
00135 

Here is the call graph for this function:

def test.test_optparse.BaseTest.assertTypeError (   self,
  func,
  expected_message,
  args 
) [inherited]
Assert that TypeError is raised when executing func.

Definition at line 179 of file test_optparse.py.

00179 
00180     def assertTypeError(self, func, expected_message, *args):
00181         """Assert that TypeError is raised when executing func."""
00182         self.assertRaises(func, args, None, TypeError, expected_message)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_optparse.TestHelp.make_parser (   self,
  columns 
)

Definition at line 1434 of file test_optparse.py.

01434 
01435     def make_parser(self, columns):
01436         options = [
01437             make_option("-a", type="string", dest='a',
01438                         metavar="APPLE", help="throw APPLEs at basket"),
01439             make_option("-b", "--boo", type="int", dest='boo',
01440                         metavar="NUM",
01441                         help=
01442                         "shout \"boo!\" NUM times (in order to frighten away "
01443                         "all the evil spirits that cause trouble and mayhem)"),
01444             make_option("--foo", action="append", type="string", dest='foo',
01445                         help="store FOO in the foo list for later fooing"),
01446             ]
01447 
01448         # We need to set COLUMNS for the OptionParser constructor, but
01449         # we must restore its original value -- otherwise, this test
01450         # screws things up for other tests when it's part of the Python
01451         # test suite.
01452         with support.EnvironmentVarGuard() as env:
01453             env['COLUMNS'] = str(columns)
01454             return InterceptingOptionParser(option_list=options)

Here is the caller graph for this function:

Definition at line 1431 of file test_optparse.py.

01431 
01432     def setUp(self):
01433         self.parser = self.make_parser(80)

Here is the caller graph for this function:

Definition at line 1464 of file test_optparse.py.

01464 
01465     def test_help(self):
01466         self.assertHelpEquals(_expected_help_basic)

Here is the call graph for this function:

Definition at line 1509 of file test_optparse.py.

01509 
01510     def test_help_description_groups(self):
01511         self.parser.set_description(
01512             "This is the program description for %prog.  %prog has "
01513             "an option group as well as single options.")
01514 
01515         group = OptionGroup(
01516             self.parser, "Dangerous Options",
01517             "Caution: use of these options is at your own risk.  "
01518             "It is believed that some of them bite.")
01519         group.add_option("-g", action="store_true", help="Group option.")
01520         self.parser.add_option_group(group)
01521 
01522         expect = """\
01523 Usage: bar.py [options]
01524 
01525 This is the program description for bar.py.  bar.py has an option group as
01526 well as single options.
01527 
01528 Options:
01529   -a APPLE           throw APPLEs at basket
01530   -b NUM, --boo=NUM  shout "boo!" NUM times (in order to frighten away all the
01531                      evil spirits that cause trouble and mayhem)
01532   --foo=FOO          store FOO in the foo list for later fooing
01533   -h, --help         show this help message and exit
01534 
01535   Dangerous Options:
01536     Caution: use of these options is at your own risk.  It is believed
01537     that some of them bite.
01538 
01539     -g               Group option.
01540 """
01541 
01542         self.assertHelpEquals(expect)
01543 
01544         self.parser.epilog = "Please report bugs to /dev/null."
01545         self.assertHelpEquals(expect + "\nPlease report bugs to /dev/null.\n")
01546 

Here is the call graph for this function:

Definition at line 1471 of file test_optparse.py.

01471 
01472     def test_help_long_opts_first(self):
01473         self.parser.formatter.short_first = 0
01474         self.assertHelpEquals(_expected_help_long_opts_first)

Here is the call graph for this function:

Definition at line 1467 of file test_optparse.py.

01467 
01468     def test_help_old_usage(self):
01469         self.parser.set_usage("Usage: %prog [options]")
01470         self.assertHelpEquals(_expected_help_basic)

Here is the call graph for this function:

Definition at line 1475 of file test_optparse.py.

01475 
01476     def test_help_title_formatter(self):
01477         with support.EnvironmentVarGuard() as env:
01478             env["COLUMNS"] = "80"
01479             self.parser.formatter = TitledHelpFormatter()
01480             self.assertHelpEquals(_expected_help_title_formatter)

Here is the call graph for this function:

Definition at line 1488 of file test_optparse.py.

01488 
01489     def test_help_unicode(self):
01490         self.parser = InterceptingOptionParser(usage=SUPPRESS_USAGE)
01491         self.parser.add_option("-a", action="store_true", help="ol\u00E9!")
01492         expect = """\
01493 Options:
01494   -h, --help  show this help message and exit
01495   -a          ol\u00E9!
01496 """
01497         self.assertHelpEquals(expect)

Here is the call graph for this function:

Definition at line 1498 of file test_optparse.py.

01498 
01499     def test_help_unicode_description(self):
01500         self.parser = InterceptingOptionParser(usage=SUPPRESS_USAGE,
01501                                                description="ol\u00E9!")
01502         expect = """\
01503 ol\u00E9!
01504 
01505 Options:
01506   -h, --help  show this help message and exit
01507 """
01508         self.assertHelpEquals(expect)

Here is the call graph for this function:

Definition at line 1481 of file test_optparse.py.

01481 
01482     def test_wrap_columns(self):
01483         # Ensure that wrapping respects $COLUMNS environment variable.
01484         # Need to reconstruct the parser, since that's the only time
01485         # we look at $COLUMNS.
01486         self.parser = self.make_parser(60)
01487         self.assertHelpEquals(_expected_help_short_lines)

Here is the call graph for this function:


Member Data Documentation

Definition at line 1432 of file test_optparse.py.


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