Back to index

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

List of all members.

Public Member Functions

def assertUsage
def assertVersion
def test_default_progname
def test_custom_progname
def assertParseOK
def assertRaises
def assertParseFail
def assertOutput
def assertTypeError
def assertHelp

Detailed Description

Test that %prog expands to the right thing in usage, version,
and help strings.

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

def test.test_optparse.TestProgName.assertUsage (   self,
  parser,
  expected_usage 
)

Definition at line 510 of file test_optparse.py.

00510 
00511     def assertUsage(self, parser, expected_usage):
00512         self.assertEqual(parser.get_usage(), expected_usage)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_optparse.TestProgName.assertVersion (   self,
  parser,
  expected_version 
)

Definition at line 513 of file test_optparse.py.

00513 
00514     def assertVersion(self, parser, expected_version):
00515         self.assertEqual(parser.get_version(), expected_version)
00516 

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 534 of file test_optparse.py.

00534 
00535     def test_custom_progname(self):
00536         parser = OptionParser(prog="thingy",
00537                               version="%prog 0.1",
00538                               usage="%prog arg arg")
00539         parser.remove_option("-h")
00540         parser.remove_option("--version")
00541         expected_usage = "Usage: thingy arg arg\n"
00542         self.assertUsage(parser, expected_usage)
00543         self.assertVersion(parser, "thingy 0.1")
00544         self.assertHelp(parser, expected_usage + "\n")
00545 

Here is the call graph for this function:

Definition at line 517 of file test_optparse.py.

00517 
00518     def test_default_progname(self):
00519         # Make sure that program name taken from sys.argv[0] by default.
00520         save_argv = sys.argv[:]
00521         try:
00522             sys.argv[0] = os.path.join("foo", "bar", "baz.py")
00523             parser = OptionParser("%prog ...", version="%prog 1.2")
00524             expected_usage = "Usage: baz.py ...\n"
00525             self.assertUsage(parser, expected_usage)
00526             self.assertVersion(parser, "baz.py 1.2")
00527             self.assertHelp(parser,
00528                             expected_usage + "\n" +
00529                             "Options:\n"
00530                             "  --version   show program's version number and exit\n"
00531                             "  -h, --help  show this help message and exit\n")
00532         finally:
00533             sys.argv[:] = save_argv

Here is the call graph for this function:


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