Back to index

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

List of all members.

Public Member Functions

def setUp
def test_option_default
def test_parser_default_1
def test_parser_default_2
def test_no_default
def test_default_none_1
def test_default_none_2
def test_float_default
def test_alt_expand
def test_no_expand
def assertParseOK
def assertRaises
def assertParseFail
def assertOutput
def assertTypeError
def assertHelp

Public Attributes

 parser
 help_prefix
 file_help
 expected_help_file
 expected_help_none

Detailed Description

Definition at line 546 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.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:

Definition at line 547 of file test_optparse.py.

00547 
00548     def setUp(self):
00549         self.parser = OptionParser(prog="test")
00550         self.help_prefix = """\
00551 Usage: test [options]
00552 
00553 Options:
00554   -h, --help            show this help message and exit
00555 """
00556         self.file_help = "read from FILE [default: %default]"
00557         self.expected_help_file = self.help_prefix + \
00558             "  -f FILE, --file=FILE  read from FILE [default: foo.txt]\n"
00559         self.expected_help_none = self.help_prefix + \
00560             "  -f FILE, --file=FILE  read from FILE [default: none]\n"

Here is the caller graph for this function:

Definition at line 605 of file test_optparse.py.

00605 
00606     def test_alt_expand(self):
00607         self.parser.add_option("-f", "--file",
00608                                default="foo.txt",
00609                                help="read from FILE [default: *DEFAULT*]")
00610         self.parser.formatter.default_tag = "*DEFAULT*"
00611         self.assertHelp(self.parser, self.expected_help_file)

Here is the call graph for this function:

Definition at line 584 of file test_optparse.py.

00584 
00585     def test_default_none_1(self):
00586         self.parser.add_option("-f", "--file",
00587                                default=None,
00588                                help=self.file_help)
00589         self.assertHelp(self.parser, self.expected_help_none)

Here is the call graph for this function:

Definition at line 590 of file test_optparse.py.

00590 
00591     def test_default_none_2(self):
00592         self.parser.add_option("-f", "--file",
00593                                help=self.file_help)
00594         self.parser.set_defaults(file=None)
00595         self.assertHelp(self.parser, self.expected_help_none)

Here is the call graph for this function:

Definition at line 596 of file test_optparse.py.

00596 
00597     def test_float_default(self):
00598         self.parser.add_option(
00599             "-p", "--prob",
00600             help="blow up with probability PROB [default: %default]")
00601         self.parser.set_defaults(prob=0.43)
00602         expected_help = self.help_prefix + \
00603             "  -p PROB, --prob=PROB  blow up with probability PROB [default: 0.43]\n"
00604         self.assertHelp(self.parser, expected_help)

Here is the call graph for this function:

Definition at line 579 of file test_optparse.py.

00579 
00580     def test_no_default(self):
00581         self.parser.add_option("-f", "--file",
00582                                help=self.file_help)
00583         self.assertHelp(self.parser, self.expected_help_none)

Here is the call graph for this function:

Definition at line 612 of file test_optparse.py.

00612 
00613     def test_no_expand(self):
00614         self.parser.add_option("-f", "--file",
00615                                default="foo.txt",
00616                                help="read from %default file")
00617         self.parser.formatter.default_tag = None
00618         expected_help = self.help_prefix + \
00619             "  -f FILE, --file=FILE  read from %default file\n"
00620         self.assertHelp(self.parser, expected_help)
00621 
00622 
00623 # -- Test parser.parse_args() ------------------------------------------

Here is the call graph for this function:

Definition at line 561 of file test_optparse.py.

00561 
00562     def test_option_default(self):
00563         self.parser.add_option("-f", "--file",
00564                                default="foo.txt",
00565                                help=self.file_help)
00566         self.assertHelp(self.parser, self.expected_help_file)

Here is the call graph for this function:

Definition at line 567 of file test_optparse.py.

00567 
00568     def test_parser_default_1(self):
00569         self.parser.add_option("-f", "--file",
00570                                help=self.file_help)
00571         self.parser.set_default('file', "foo.txt")
00572         self.assertHelp(self.parser, self.expected_help_file)

Here is the call graph for this function:

Definition at line 573 of file test_optparse.py.

00573 
00574     def test_parser_default_2(self):
00575         self.parser.add_option("-f", "--file",
00576                                help=self.file_help)
00577         self.parser.set_defaults(file="foo.txt")
00578         self.assertHelp(self.parser, self.expected_help_file)

Here is the call graph for this function:


Member Data Documentation

Definition at line 556 of file test_optparse.py.

Definition at line 558 of file test_optparse.py.

Definition at line 555 of file test_optparse.py.

Definition at line 549 of file test_optparse.py.

Definition at line 548 of file test_optparse.py.


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