Back to index

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

List of all members.

Public Member Functions

def setUp
def process_tuple
def test_callback_extra_args
def assertParseOK
def assertRaises
def assertParseFail
def assertOutput
def assertTypeError
def assertHelp

Public Attributes

 parser

Detailed Description

Definition at line 1128 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.TestCallbackExtraArgs.process_tuple (   self,
  option,
  opt,
  value,
  parser_,
  len,
  type 
)

Definition at line 1136 of file test_optparse.py.

01136 
01137     def process_tuple(self, option, opt, value, parser_, len, type):
01138         self.assertEqual(len, 3)
01139         self.assertTrue(type is int)
01140 
01141         if opt == "-p":
01142             self.assertEqual(value, "1,2,3")
01143         elif opt == "--point":
01144             self.assertEqual(value, "4,5,6")
01145 
01146         value = tuple(map(type, value.split(",")))
01147         getattr(parser_.values, option.dest).append(value)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 1129 of file test_optparse.py.

01129 
01130     def setUp(self):
01131         options = [make_option("-p", "--point", action="callback",
01132                                callback=self.process_tuple,
01133                                callback_args=(3, int), type="string",
01134                                dest="points", default=[])]
01135         self.parser = OptionParser(option_list=options)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 1148 of file test_optparse.py.

01148 
01149     def test_callback_extra_args(self):
01150         self.assertParseOK(["-p1,2,3", "--point", "4,5,6"],
01151                            {'points': [(1,2,3), (4,5,6)]},
01152                            [])

Here is the call graph for this function:


Member Data Documentation

Definition at line 1134 of file test_optparse.py.


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