Back to index

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

List of all members.

Public Member Functions

def setUp
def test_required_value
def test_invalid_integer
def test_no_such_option
def test_long_invalid_integer
def test_empty
def test_shortopt_empty_longopt_append
def test_long_option_append
def test_option_argument_joined
def test_option_argument_split
def test_option_argument_joined_integer
def test_option_argument_split_negative_integer
def test_long_option_argument_joined
def test_long_option_argument_split
def test_long_option_short_option
def test_abbrev_long_option
def test_defaults
def test_ambiguous_option
def test_short_and_long_option_split
def test_short_option_split_long_option_append
def test_short_option_split_one_positional_arg
def test_short_option_consumes_separator
def test_short_option_joined_and_separator
def test_hyphen_becomes_positional_arg
def test_no_append_versus_append
def test_option_consumes_optionlike_string
def test_combined_single_invalid_option
def assertParseOK
def assertRaises
def assertParseFail
def assertOutput
def assertTypeError
def assertHelp

Public Attributes

 parser

Detailed Description

Definition at line 624 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 625 of file test_optparse.py.

00625 
00626     def setUp(self):
00627         options = [make_option("-a", type="string"),
00628                    make_option("-b", "--boo", type="int", dest='boo'),
00629                    make_option("--foo", action="append")]
00630 
00631         self.parser = InterceptingOptionParser(usage=SUPPRESS_USAGE,
00632                                                option_list=options)

Here is the caller graph for this function:

Definition at line 697 of file test_optparse.py.

00697 
00698     def test_abbrev_long_option(self):
00699         self.assertParseOK(["--f=bar", "-axyz"],
00700                            {'a': 'xyz', 'boo': None, 'foo': ["bar"]},
00701                            [])

Here is the call graph for this function:

Definition at line 707 of file test_optparse.py.

00707 
00708     def test_ambiguous_option(self):
00709         self.parser.add_option("--foz", action="store",
00710                                type="string", dest="foo")
00711         self.assertParseFail(["--f=bar"],
00712                              "ambiguous option: --f (--foo, --foz?)")
00713 

Here is the call graph for this function:

Definition at line 757 of file test_optparse.py.

00757 
00758     def test_combined_single_invalid_option(self):
00759         self.parser.add_option("-t", action="store_true")
00760         self.assertParseFail(["-test"],
00761                              "no such option: -e")

Here is the call graph for this function:

Definition at line 702 of file test_optparse.py.

00702 
00703     def test_defaults(self):
00704         (options, args) = self.parser.parse_args([])
00705         defaults = self.parser.get_default_values()
00706         self.assertEqual(vars(defaults), vars(options))

Here is the call graph for this function:

Definition at line 647 of file test_optparse.py.

00647 
00648     def test_empty(self):
00649         self.assertParseOK([], {'a': None, 'boo': None, 'foo': None}, [])

Here is the call graph for this function:

Definition at line 742 of file test_optparse.py.

00742 
00743     def test_hyphen_becomes_positional_arg(self):
00744         self.assertParseOK(["-ab", "-", "--foo", "bar"],
00745                            {'a': "b", 'boo': None, 'foo': ["bar"]},
00746                            ["-"])

Here is the call graph for this function:

Definition at line 636 of file test_optparse.py.

00636 
00637     def test_invalid_integer(self):
00638         self.assertParseFail(["-b", "5x"],
00639                              "option -b: invalid integer value: '5x'")

Here is the call graph for this function:

Definition at line 643 of file test_optparse.py.

00643 
00644     def test_long_invalid_integer(self):
00645         self.assertParseFail(["--boo=x5"],
00646                              "option --boo: invalid integer value: 'x5'")

Here is the call graph for this function:

Definition at line 655 of file test_optparse.py.

00655 
00656     def test_long_option_append(self):
00657         self.assertParseOK(["--foo", "bar", "--foo", "", "--foo=x"],
00658                            {'a': None,
00659                             'boo': None,
00660                             'foo': ["bar", "", "x"]},
00661                            [])

Here is the call graph for this function:

Definition at line 682 of file test_optparse.py.

00682 
00683     def test_long_option_argument_joined(self):
00684         self.assertParseOK(["--boo=13"],
00685                            {'a': None, 'boo': 13, 'foo': None},
00686                            [])

Here is the call graph for this function:

Definition at line 687 of file test_optparse.py.

00687 
00688     def test_long_option_argument_split(self):
00689         self.assertParseOK(["--boo", "111"],
00690                            {'a': None, 'boo': 111, 'foo': None},
00691                            [])

Here is the call graph for this function:

Definition at line 692 of file test_optparse.py.

00692 
00693     def test_long_option_short_option(self):
00694         self.assertParseOK(["--foo=bar", "-axyz"],
00695                            {'a': 'xyz', 'boo': None, 'foo': ["bar"]},
00696                            [])

Here is the call graph for this function:

Definition at line 747 of file test_optparse.py.

00747 
00748     def test_no_append_versus_append(self):
00749         self.assertParseOK(["-b3", "-b", "5", "--foo=bar", "--foo", "baz"],
00750                            {'a': None, 'boo': 5, 'foo': ["bar", "baz"]},
00751                            [])

Here is the call graph for this function:

Definition at line 640 of file test_optparse.py.

00640 
00641     def test_no_such_option(self):
00642         self.assertParseFail(["--boo13"], "no such option: --boo13")

Here is the call graph for this function:

Definition at line 662 of file test_optparse.py.

00662 
00663     def test_option_argument_joined(self):
00664         self.assertParseOK(["-abc"],
00665                            {'a': "bc", 'boo': None, 'foo': None},
00666                            [])

Here is the call graph for this function:

Definition at line 672 of file test_optparse.py.

00672 
00673     def test_option_argument_joined_integer(self):
00674         self.assertParseOK(["-b34"],
00675                            {'a': None, 'boo': 34, 'foo': None},
00676                            [])

Here is the call graph for this function:

Definition at line 667 of file test_optparse.py.

00667 
00668     def test_option_argument_split(self):
00669         self.assertParseOK(["-a", "34"],
00670                            {'a': "34", 'boo': None, 'foo': None},
00671                            [])

Here is the call graph for this function:

Definition at line 677 of file test_optparse.py.

00677 
00678     def test_option_argument_split_negative_integer(self):
00679         self.assertParseOK(["-b", "-5"],
00680                            {'a': None, 'boo': -5, 'foo': None},
00681                            [])

Here is the call graph for this function:

Definition at line 752 of file test_optparse.py.

00752 
00753     def test_option_consumes_optionlike_string(self):
00754         self.assertParseOK(["-a", "-b3"],
00755                            {'a': "-b3", 'boo': None, 'foo': None},
00756                            [])

Here is the call graph for this function:

Definition at line 633 of file test_optparse.py.

00633 
00634     def test_required_value(self):
00635         self.assertParseFail(["-a"], "-a option requires an argument")

Here is the call graph for this function:

Definition at line 714 of file test_optparse.py.

00714 
00715     def test_short_and_long_option_split(self):
00716         self.assertParseOK(["-a", "xyz", "--foo", "bar"],
00717                            {'a': 'xyz', 'boo': None, 'foo': ["bar"]},
00718                            []),

Here is the call graph for this function:

Definition at line 729 of file test_optparse.py.

00729 
00730     def test_short_option_consumes_separator(self):
00731         self.assertParseOK(["-a", "--", "foo", "bar"],
00732                            {'a': "--", 'boo': None, 'foo': None},
00733                            ["foo", "bar"]),
00734         self.assertParseOK(["-a", "--", "--foo", "bar"],
00735                            {'a': "--", 'boo': None, 'foo': ["bar"]},
00736                            []),

Here is the call graph for this function:

Definition at line 737 of file test_optparse.py.

00737 
00738     def test_short_option_joined_and_separator(self):
00739         self.assertParseOK(["-ab", "--", "--foo", "bar"],
00740                            {'a': "b", 'boo': None, 'foo': None},
00741                            ["--foo", "bar"]),

Here is the call graph for this function:

Definition at line 719 of file test_optparse.py.

00719 
00720     def test_short_option_split_long_option_append(self):
00721         self.assertParseOK(["--foo=bar", "-b", "123", "--foo", "baz"],
00722                            {'a': None, 'boo': 123, 'foo': ["bar", "baz"]},
00723                            [])

Here is the call graph for this function:

Definition at line 724 of file test_optparse.py.

00724 
00725     def test_short_option_split_one_positional_arg(self):
00726         self.assertParseOK(["-a", "foo", "bar"],
00727                            {'a': "foo", 'boo': None, 'foo': None},
00728                            ["bar"]),

Here is the call graph for this function:

Definition at line 650 of file test_optparse.py.

00650 
00651     def test_shortopt_empty_longopt_append(self):
00652         self.assertParseOK(["-a", "", "--foo=blah", "--foo="],
00653                            {'a': "", 'boo': None, 'foo': ["blah", ""]},
00654                            [])

Here is the call graph for this function:


Member Data Documentation

Definition at line 630 of file test_optparse.py.


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