Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 1 | from test.support import run_unittest, TestSkipped, verbose |
| 2 | import unittest |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 3 | import locale |
Guido van Rossum | fc34986 | 2001-04-15 13:15:56 +0000 | [diff] [blame] | 4 | import sys |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 5 | import codecs |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 6 | |
Antoine Pitrou | 13856ea | 2008-07-26 21:02:53 +0000 | [diff] [blame] | 7 | enUS_locale = None |
| 8 | |
| 9 | def get_enUS_locale(): |
| 10 | global enUS_locale |
| 11 | if sys.platform == 'darwin': |
| 12 | raise TestSkipped("Locale support on MacOSX is minimal") |
| 13 | if sys.platform.startswith("win"): |
| 14 | tlocs = ("En", "English") |
| 15 | else: |
| 16 | tlocs = ("en_US.UTF-8", "en_US.US-ASCII", "en_US") |
| 17 | oldlocale = locale.setlocale(locale.LC_NUMERIC) |
| 18 | for tloc in tlocs: |
| 19 | try: |
| 20 | locale.setlocale(locale.LC_NUMERIC, tloc) |
| 21 | except locale.Error: |
| 22 | continue |
| 23 | break |
| 24 | else: |
| 25 | raise TestSkipped( |
| 26 | "Test locale not supported (tried %s)" % (', '.join(tlocs))) |
| 27 | enUS_locale = tloc |
| 28 | locale.setlocale(locale.LC_NUMERIC, oldlocale) |
| 29 | |
| 30 | |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 31 | class BaseLocalizedTest(unittest.TestCase): |
| 32 | # |
| 33 | # Base class for tests using a real locale |
| 34 | # |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 35 | |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 36 | def setUp(self): |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 37 | self.oldlocale = locale.setlocale(self.locale_type) |
Antoine Pitrou | 13856ea | 2008-07-26 21:02:53 +0000 | [diff] [blame] | 38 | locale.setlocale(self.locale_type, enUS_locale) |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 39 | if verbose: |
Antoine Pitrou | 13856ea | 2008-07-26 21:02:53 +0000 | [diff] [blame] | 40 | print("testing with \"%s\"..." % enUS_locale, end=' ') |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 41 | |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 42 | def tearDown(self): |
| 43 | locale.setlocale(self.locale_type, self.oldlocale) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 44 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 45 | |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 46 | class BaseCookedTest(unittest.TestCase): |
| 47 | # |
| 48 | # Base class for tests using cooked localeconv() values |
| 49 | # |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 50 | |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 51 | def setUp(self): |
| 52 | locale._override_localeconv = self.cooked_values |
| 53 | |
| 54 | def tearDown(self): |
| 55 | locale._override_localeconv = {} |
| 56 | |
| 57 | class CCookedTest(BaseCookedTest): |
| 58 | # A cooked "C" locale |
| 59 | |
| 60 | cooked_values = { |
| 61 | 'currency_symbol': '', |
| 62 | 'decimal_point': '.', |
| 63 | 'frac_digits': 127, |
| 64 | 'grouping': [], |
| 65 | 'int_curr_symbol': '', |
| 66 | 'int_frac_digits': 127, |
| 67 | 'mon_decimal_point': '', |
| 68 | 'mon_grouping': [], |
| 69 | 'mon_thousands_sep': '', |
| 70 | 'n_cs_precedes': 127, |
| 71 | 'n_sep_by_space': 127, |
| 72 | 'n_sign_posn': 127, |
| 73 | 'negative_sign': '', |
| 74 | 'p_cs_precedes': 127, |
| 75 | 'p_sep_by_space': 127, |
| 76 | 'p_sign_posn': 127, |
| 77 | 'positive_sign': '', |
| 78 | 'thousands_sep': '' |
| 79 | } |
| 80 | |
| 81 | class EnUSCookedTest(BaseCookedTest): |
| 82 | # A cooked "en_US" locale |
| 83 | |
| 84 | cooked_values = { |
| 85 | 'currency_symbol': '$', |
| 86 | 'decimal_point': '.', |
| 87 | 'frac_digits': 2, |
| 88 | 'grouping': [3, 3, 0], |
| 89 | 'int_curr_symbol': 'USD ', |
| 90 | 'int_frac_digits': 2, |
| 91 | 'mon_decimal_point': '.', |
| 92 | 'mon_grouping': [3, 3, 0], |
| 93 | 'mon_thousands_sep': ',', |
| 94 | 'n_cs_precedes': 1, |
| 95 | 'n_sep_by_space': 0, |
| 96 | 'n_sign_posn': 1, |
| 97 | 'negative_sign': '-', |
| 98 | 'p_cs_precedes': 1, |
| 99 | 'p_sep_by_space': 0, |
| 100 | 'p_sign_posn': 1, |
| 101 | 'positive_sign': '', |
| 102 | 'thousands_sep': ',' |
| 103 | } |
| 104 | |
| 105 | |
| 106 | class BaseFormattingTest(object): |
| 107 | # |
| 108 | # Utility functions for formatting tests |
| 109 | # |
| 110 | |
| 111 | def _test_formatfunc(self, format, value, out, func, **format_opts): |
| 112 | self.assertEqual( |
| 113 | func(format, value, **format_opts), out) |
| 114 | |
| 115 | def _test_format(self, format, value, out, **format_opts): |
| 116 | self._test_formatfunc(format, value, out, |
| 117 | func=locale.format, **format_opts) |
| 118 | |
| 119 | def _test_format_string(self, format, value, out, **format_opts): |
| 120 | self._test_formatfunc(format, value, out, |
| 121 | func=locale.format_string, **format_opts) |
| 122 | |
| 123 | def _test_currency(self, value, out, **format_opts): |
| 124 | self.assertEqual(locale.currency(value, **format_opts), out) |
| 125 | |
| 126 | |
| 127 | class EnUSNumberFormatting(BaseFormattingTest): |
Antoine Pitrou | 13856ea | 2008-07-26 21:02:53 +0000 | [diff] [blame] | 128 | # XXX there is a grouping + padding bug when the thousands separator |
| 129 | # is empty but the grouping array contains values (e.g. Solaris 10) |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 130 | |
| 131 | def setUp(self): |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 132 | self.sep = locale.localeconv()['thousands_sep'] |
| 133 | |
| 134 | def test_grouping(self): |
| 135 | self._test_format("%f", 1024, grouping=1, out='1%s024.000000' % self.sep) |
| 136 | self._test_format("%f", 102, grouping=1, out='102.000000') |
| 137 | self._test_format("%f", -42, grouping=1, out='-42.000000') |
| 138 | self._test_format("%+f", -42, grouping=1, out='-42.000000') |
| 139 | |
| 140 | def test_grouping_and_padding(self): |
| 141 | self._test_format("%20.f", -42, grouping=1, out='-42'.rjust(20)) |
Antoine Pitrou | 13856ea | 2008-07-26 21:02:53 +0000 | [diff] [blame] | 142 | if self.sep: |
| 143 | self._test_format("%+10.f", -4200, grouping=1, |
| 144 | out=('-4%s200' % self.sep).rjust(10)) |
| 145 | self._test_format("%-10.f", -4200, grouping=1, |
| 146 | out=('-4%s200' % self.sep).ljust(10)) |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 147 | |
| 148 | def test_integer_grouping(self): |
| 149 | self._test_format("%d", 4200, grouping=True, out='4%s200' % self.sep) |
| 150 | self._test_format("%+d", 4200, grouping=True, out='+4%s200' % self.sep) |
| 151 | self._test_format("%+d", -4200, grouping=True, out='-4%s200' % self.sep) |
| 152 | |
| 153 | def test_simple(self): |
| 154 | self._test_format("%f", 1024, grouping=0, out='1024.000000') |
| 155 | self._test_format("%f", 102, grouping=0, out='102.000000') |
| 156 | self._test_format("%f", -42, grouping=0, out='-42.000000') |
| 157 | self._test_format("%+f", -42, grouping=0, out='-42.000000') |
| 158 | |
| 159 | def test_padding(self): |
| 160 | self._test_format("%20.f", -42, grouping=0, out='-42'.rjust(20)) |
| 161 | self._test_format("%+10.f", -4200, grouping=0, out='-4200'.rjust(10)) |
| 162 | self._test_format("%-10.f", 4200, grouping=0, out='4200'.ljust(10)) |
| 163 | |
| 164 | def test_complex_formatting(self): |
| 165 | # Spaces in formatting string |
| 166 | self._test_format_string("One million is %i", 1000000, grouping=1, |
| 167 | out='One million is 1%s000%s000' % (self.sep, self.sep)) |
| 168 | self._test_format_string("One million is %i", 1000000, grouping=1, |
| 169 | out='One million is 1%s000%s000' % (self.sep, self.sep)) |
| 170 | # Dots in formatting string |
| 171 | self._test_format_string(".%f.", 1000.0, out='.1000.000000.') |
| 172 | # Padding |
Antoine Pitrou | 13856ea | 2008-07-26 21:02:53 +0000 | [diff] [blame] | 173 | if self.sep: |
| 174 | self._test_format_string("--> %10.2f", 4200, grouping=1, |
| 175 | out='--> ' + ('4%s200.00' % self.sep).rjust(10)) |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 176 | # Asterisk formats |
| 177 | self._test_format_string("%10.*f", (2, 1000), grouping=0, |
| 178 | out='1000.00'.rjust(10)) |
Antoine Pitrou | 13856ea | 2008-07-26 21:02:53 +0000 | [diff] [blame] | 179 | if self.sep: |
| 180 | self._test_format_string("%*.*f", (10, 2, 1000), grouping=1, |
| 181 | out=('1%s000.00' % self.sep).rjust(10)) |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 182 | # Test more-in-one |
Antoine Pitrou | 13856ea | 2008-07-26 21:02:53 +0000 | [diff] [blame] | 183 | if self.sep: |
| 184 | self._test_format_string("int %i float %.2f str %s", |
| 185 | (1000, 1000.0, 'str'), grouping=1, |
| 186 | out='int 1%s000 float 1%s000.00 str str' % |
| 187 | (self.sep, self.sep)) |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 188 | |
| 189 | |
| 190 | class TestNumberFormatting(BaseLocalizedTest, EnUSNumberFormatting): |
| 191 | # Test number formatting with a real English locale. |
| 192 | |
| 193 | locale_type = locale.LC_NUMERIC |
| 194 | |
| 195 | def setUp(self): |
| 196 | BaseLocalizedTest.setUp(self) |
| 197 | EnUSNumberFormatting.setUp(self) |
| 198 | |
| 199 | |
| 200 | class TestEnUSNumberFormatting(EnUSCookedTest, EnUSNumberFormatting): |
| 201 | # Test number formatting with a cooked "en_US" locale. |
| 202 | |
| 203 | def setUp(self): |
| 204 | EnUSCookedTest.setUp(self) |
| 205 | EnUSNumberFormatting.setUp(self) |
| 206 | |
| 207 | def test_currency(self): |
| 208 | self._test_currency(50000, "$50000.00") |
| 209 | self._test_currency(50000, "$50,000.00", grouping=True) |
| 210 | self._test_currency(50000, "USD 50,000.00", |
| 211 | grouping=True, international=True) |
| 212 | |
| 213 | |
| 214 | class TestCNumberFormatting(CCookedTest, BaseFormattingTest): |
| 215 | # Test number formatting with a cooked "C" locale. |
| 216 | |
| 217 | def test_grouping(self): |
| 218 | self._test_format("%.2f", 12345.67, grouping=True, out='12345.67') |
| 219 | |
| 220 | def test_grouping_and_padding(self): |
| 221 | self._test_format("%9.2f", 12345.67, grouping=True, out=' 12345.67') |
| 222 | |
| 223 | |
| 224 | class TestMiscellaneous(unittest.TestCase): |
| 225 | def test_getpreferredencoding(self): |
| 226 | # Invoke getpreferredencoding to make sure it does not cause exceptions. |
| 227 | enc = locale.getpreferredencoding() |
| 228 | if enc: |
| 229 | # If encoding non-empty, make sure it is valid |
| 230 | codecs.lookup(enc) |
| 231 | |
| 232 | if hasattr(locale, "strcoll"): |
| 233 | def test_strcoll_3303(self): |
| 234 | # test crasher from bug #3303 |
| 235 | self.assertRaises(TypeError, locale.strcoll, "a", None) |
| 236 | self.assertRaises(TypeError, locale.strcoll, b"a", None) |
| 237 | |
| 238 | |
| 239 | def test_main(): |
Antoine Pitrou | 13856ea | 2008-07-26 21:02:53 +0000 | [diff] [blame] | 240 | tests = [ |
| 241 | TestMiscellaneous, |
| 242 | TestEnUSNumberFormatting, |
| 243 | TestCNumberFormatting |
| 244 | ] |
| 245 | # TestSkipped can't be raised inside unittests, handle it manually instead |
| 246 | try: |
| 247 | get_enUS_locale() |
| 248 | except TestSkipped as e: |
| 249 | if verbose: |
| 250 | print("Some tests will be disabled: %s" % e) |
| 251 | else: |
| 252 | tests += [TestNumberFormatting] |
| 253 | run_unittest(*tests) |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 254 | |
| 255 | if __name__ == '__main__': |
| 256 | test_main() |