diff options
author | Greg Ward <gward@python.net> | 2006-06-11 16:24:11 (GMT) |
---|---|---|
committer | Greg Ward <gward@python.net> | 2006-06-11 16:24:11 (GMT) |
commit | 0e0c9f47403b382f72f9dffc5becb1641fa8da48 (patch) | |
tree | cd9b0dd77b4b684c33ee2839604b48905fba5748 /Lib/test/test_optparse.py | |
parent | d1c797e624c26ae99fe1691cf5da73261034a383 (diff) | |
download | cpython-0e0c9f47403b382f72f9dffc5becb1641fa8da48.zip cpython-0e0c9f47403b382f72f9dffc5becb1641fa8da48.tar.gz cpython-0e0c9f47403b382f72f9dffc5becb1641fa8da48.tar.bz2 |
Bug #1498146: fix optparse to handle Unicode strings in option help,
description, and epilog.
Diffstat (limited to 'Lib/test/test_optparse.py')
-rw-r--r-- | Lib/test/test_optparse.py | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/Lib/test/test_optparse.py b/Lib/test/test_optparse.py index 79df906..622d757 100644 --- a/Lib/test/test_optparse.py +++ b/Lib/test/test_optparse.py @@ -15,7 +15,7 @@ import copy import types import unittest -from cStringIO import StringIO +from StringIO import StringIO from pprint import pprint from test import test_support @@ -164,15 +164,23 @@ and kwargs %(kwargs)r expected_error=None): """Assert the parser prints the expected output on stdout.""" save_stdout = sys.stdout + encoding = getattr(save_stdout, 'encoding', None) try: try: sys.stdout = StringIO() + if encoding: + sys.stdout.encoding = encoding self.parser.parse_args(cmdline_args) finally: output = sys.stdout.getvalue() sys.stdout = save_stdout except InterceptedError, err: + self.assert_( + type(output) is types.StringType, + "expected output to be an ordinary string, not %r" + % type(output)) + if output != expected_output: self.fail("expected: \n'''\n" + expected_output + "'''\nbut got \n'''\n" + output + "'''") @@ -1456,6 +1464,10 @@ class TestHelp(BaseTest): return InterceptingOptionParser(option_list=options) def assertHelpEquals(self, expected_output): + if type(expected_output) is types.UnicodeType: + encoding = self.parser._get_encoding(sys.stdout) + expected_output = expected_output.encode(encoding, "replace") + save_argv = sys.argv[:] try: # Make optparse believe bar.py is being executed. @@ -1486,6 +1498,27 @@ class TestHelp(BaseTest): self.parser = self.make_parser(60) self.assertHelpEquals(_expected_help_short_lines) + def test_help_unicode(self): + self.parser = InterceptingOptionParser(usage=SUPPRESS_USAGE) + self.parser.add_option("-a", action="store_true", help=u"ol\u00E9!") + expect = u"""\ +Options: + -h, --help show this help message and exit + -a ol\u00E9! +""" + self.assertHelpEquals(expect) + + def test_help_unicode_description(self): + self.parser = InterceptingOptionParser(usage=SUPPRESS_USAGE, + description=u"ol\u00E9!") + expect = u"""\ +ol\u00E9! + +Options: + -h, --help show this help message and exit +""" + self.assertHelpEquals(expect) + def test_help_description_groups(self): self.parser.set_description( "This is the program description for %prog. %prog has " |