diff -r 574cbf457211 Lib/test/test_locale.py --- a/Lib/test/test_locale.py Fri Jan 18 12:58:50 2008 +0100 +++ b/Lib/test/test_locale.py Fri Jan 18 14:44:10 2008 +0100 @@ -1,115 +1,150 @@ from test.test_support import verbose, T -from test.test_support import verbose, TestSkipped +from test import test_support +import unittest import locale import sys +import codecs -if sys.platform == 'darwin': - raise TestSkipped("Locale support on MacOSX is minimal and cannot be tested") -oldlocale = locale.setlocale(locale.LC_NUMERIC) -if sys.platform.startswith("win"): - tlocs = ("En", "English") -else: - tlocs = ("en_US.UTF-8", "en_US.US-ASCII", "en_US") +class BaseLocalizedTest(unittest.TestCase): + if sys.platform.startswith("win"): + tlocs = ("En", "English") + else: + tlocs = ("en_US.UTF-8", "en_US.US-ASCII", "en_US") -for tloc in tlocs: - try: - locale.setlocale(locale.LC_NUMERIC, tloc) - break - except locale.Error: - continue -else: - raise ImportError, "test locale not supported (tried %s)"%(', '.join(tlocs)) + def setUp(self): + if sys.platform == 'darwin': + raise test_support.TestSkipped( + "Locale support on MacOSX is minimal and cannot be tested") + self.oldlocale = locale.setlocale(self.locale_type) + for tloc in self.tlocs: + try: + locale.setlocale(self.locale_type, tloc) + except locale.Error: + continue + break + else: + raise test_support.TestSkipped( + "Test locale not supported (tried %s)" % (', '.join(self.tlocs))) + if test_support.verbose: + print "testing with \"%s\"..." % tloc, -def testformat(formatstr, value, grouping = 0, output=None, func=locale.format): - if verbose: - if output: - print "%s %% %s =? %s ..." %\ - (repr(formatstr), repr(value), repr(output)), - else: - print "%s %% %s works? ..." % (repr(formatstr), repr(value)), - result = func(formatstr, value, grouping = grouping) - if output and result != output: - if verbose: - print 'no' - print "%s %% %s == %s != %s" %\ - (repr(formatstr), repr(value), repr(result), repr(output)) - else: - if verbose: - print "yes" + def tearDown(self): + locale.setlocale(self.locale_type, self.oldlocale) -try: - # On Solaris 10, the thousands_sep is the empty string - sep = locale.localeconv()['thousands_sep'] - testformat("%f", 1024, grouping=1, output='1%s024.000000' % sep) - testformat("%f", 102, grouping=1, output='102.000000') - testformat("%f", -42, grouping=1, output='-42.000000') - testformat("%+f", -42, grouping=1, output='-42.000000') - testformat("%20.f", -42, grouping=1, output=' -42') - testformat("%+10.f", -4200, grouping=1, output=' -4%s200' % sep) - testformat("%-10.f", 4200, grouping=1, output='4%s200 ' % sep) - # Invoke getpreferredencoding to make sure it does not cause exceptions, - locale.getpreferredencoding() - # === Test format() with more complex formatting strings - # test if grouping is independent from other characters in formatting string - testformat("One million is %i", 1000000, grouping=1, - output='One million is 1%s000%s000' % (sep, sep), - func=locale.format_string) - testformat("One million is %i", 1000000, grouping=1, - output='One million is 1%s000%s000' % (sep, sep), - func=locale.format_string) - # test dots in formatting string - testformat(".%f.", 1000.0, output='.1000.000000.', func=locale.format_string) - # test floats - testformat("--> %10.2f", 1000.0, grouping=1, output='--> 1%s000.00' % sep, - func=locale.format_string) - # test asterisk formats - testformat("%10.*f", (2, 1000.0), grouping=0, output=' 1000.00', - func=locale.format_string) - testformat("%*.*f", (10, 2, 1000.0), grouping=1, output=' 1%s000.00' % sep, - func=locale.format_string) - # test more-in-one - testformat("int %i float %.2f str %s", (1000, 1000.0, 'str'), grouping=1, - output='int 1%s000 float 1%s000.00 str str' % (sep, sep), - func=locale.format_string) +class TestNumberFormatting(BaseLocalizedTest): + locale_type = locale.LC_NUMERIC -finally: - locale.setlocale(locale.LC_NUMERIC, oldlocale) + def setUp(self): + BaseLocalizedTest.setUp(self) + # NOTE: On Solaris 10, the thousands_sep is the empty string + self.sep = locale.localeconv()['thousands_sep'] + def _test_formatfunc(self, format, value, output, func, **format_opts): + self.assertEqual(func(format, value, **format_opts), output) -# Test BSD Rune locale's bug for isctype functions. -def teststrop(s, method, output): - if verbose: - print "%s.%s() =? %s ..." % (repr(s), method, repr(output)), - result = getattr(s, method)() - if result != output: - if verbose: - print "no" - print "%s.%s() == %s != %s" % (repr(s), method, repr(result), - repr(output)) - elif verbose: - print "yes" + def _test_format(self, format, value, output, **format_opts): + self._test_formatfunc(format, value, output, locale.format, **format_opts) -try: - if sys.platform == 'sunos5': - # On Solaris, in en_US.UTF-8, \xa0 is a space - raise locale.Error - oldlocale = locale.setlocale(locale.LC_CTYPE) - locale.setlocale(locale.LC_CTYPE, 'en_US.UTF-8') -except locale.Error: - pass -else: - try: - teststrop('\x20', 'isspace', True) - teststrop('\xa0', 'isspace', False) - teststrop('\xa1', 'isspace', False) - teststrop('\xc0', 'isalpha', False) - teststrop('\xc0', 'isalnum', False) - teststrop('\xc0', 'isupper', False) - teststrop('\xc0', 'islower', False) - teststrop('\xec\xa0\xbc', 'split', ['\xec\xa0\xbc']) - teststrop('\xed\x95\xa0', 'strip', '\xed\x95\xa0') - teststrop('\xcc\x85', 'lower', '\xcc\x85') - teststrop('\xed\x95\xa0', 'upper', '\xed\x95\xa0') - finally: - locale.setlocale(locale.LC_CTYPE, oldlocale) + def _test_format_string(self, format, value, output, **format_opts): + self._test_formatfunc(format, value, output, locale.format_string, **format_opts) + + def test_grouping(self): + self._test_format("%f", 1024, grouping=1, output='1%s024.000000' % self.sep) + self._test_format("%f", 102, grouping=1, output='102.000000') + self._test_format("%f", -42, grouping=1, output='-42.000000') + self._test_format("%+f", -42, grouping=1, output='-42.000000') + + def test_grouping_and_padding(self): + self._test_format("%20.f", -42, grouping=1, output='-42'.rjust(20)) + self._test_format("%+10.f", -4200, grouping=1, + output=('-4%s200' % self.sep).rjust(10)) + self._test_format("%-10.f", -4200, grouping=1, + output=('-4%s200' % self.sep).ljust(10)) + + def test_simple(self): + self._test_format("%f", 1024, grouping=0, output='1024.000000') + self._test_format("%f", 102, grouping=0, output='102.000000') + self._test_format("%f", -42, grouping=0, output='-42.000000') + self._test_format("%+f", -42, grouping=0, output='-42.000000') + + def test_padding(self): + self._test_format("%20.f", -42, grouping=0, output='-42'.rjust(20)) + self._test_format("%+10.f", -4200, grouping=0, output='-4200'.rjust(10)) + self._test_format("%-10.f", 4200, grouping=0, output='4200'.ljust(10)) + + def test_complex_formatting(self): + # Spaces in formatting string + self._test_format_string("One million is %i", 1000000, grouping=1, + output='One million is 1%s000%s000' % (self.sep, self.sep)) + self._test_format_string("One million is %i", 1000000, grouping=1, + output='One million is 1%s000%s000' % (self.sep, self.sep)) + # Dots in formatting string + self._test_format_string(".%f.", 1000.0, output='.1000.000000.') + # Padding + self._test_format_string("--> %10.2f", 4200, grouping=1, + output='--> ' + ('4%s200.00' % self.sep).rjust(10)) + # Asterisk formats + self._test_format_string("%10.*f", (2, 1000), grouping=0, + output='1000.00'.rjust(10)) + self._test_format_string("%*.*f", (10, 2, 1000), grouping=1, + output=('1%s000.00' % self.sep).rjust(10)) + # Test more-in-one + self._test_format_string("int %i float %.2f str %s", + (1000, 1000.0, 'str'), grouping=1, + output='int 1%s000 float 1%s000.00 str str' % (self.sep, self.sep)) + + +class TestMiscellaneous(unittest.TestCase): + def test_getpreferredencoding(self): + """ + Invoke getpreferredencoding to make sure it does not cause exceptions. + """ + enc = locale.getpreferredencoding() + if enc: + # If encoding non-empty, make sure it is valid + codecs.lookup(enc) + + +class TestStringMethods(BaseLocalizedTest): + locale_type = locale.LC_CTYPE + + # Test BSD Rune locale's bug for isctype functions. + + def test_isspace(self): + self.assertEqual('\x20'.isspace(), True) + if sys.platform == 'sunos5': + # On Solaris, in en_US.UTF-8, \xa0 is a space + self.assertEqual('\xa0'.isspace(), False) + self.assertEqual('\xa1'.isspace(), False) + + def test_isalpha(self): + self.assertEqual('\xc0'.isalpha(), False) + + def test_isalnum(self): + self.assertEqual('\xc0'.isalnum(), False) + + def test_isupper(self): + self.assertEqual('\xc0'.isupper(), False) + + def test_islower(self): + self.assertEqual('\xc0'.islower(), False) + + def test_lower(self): + self.assertEqual('\xcc\x85'.lower(), '\xcc\x85') + + def test_upper(self): + self.assertEqual('\xed\x95\xa0'.upper(), '\xed\x95\xa0') + + def test_strip(self): + self.assertEqual('\xed\x95\xa0'.strip(), '\xed\x95\xa0') + + def test_split(self): + self.assertEqual('\xec\xa0\xbc'.split(), ['\xec\xa0\xbc']) + + +def test_main(): + test_support.run_unittest(__name__) + +if __name__ == '__main__': + test_main()