Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 1 | from test.support import run_unittest, verbose |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 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': |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 12 | raise unittest.SkipTest("Locale support on MacOSX is minimal") |
Antoine Pitrou | 13856ea | 2008-07-26 21:02:53 +0000 | [diff] [blame] | 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: |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 25 | raise unittest.SkipTest( |
Antoine Pitrou | 13856ea | 2008-07-26 21:02:53 +0000 | [diff] [blame] | 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 | |
Antoine Pitrou | 350370c | 2009-03-14 00:13:13 +0000 | [diff] [blame] | 106 | class FrFRCookedTest(BaseCookedTest): |
| 107 | # A cooked "fr_FR" locale with a space character as decimal separator |
| 108 | # and a non-ASCII currency symbol. |
| 109 | |
| 110 | cooked_values = { |
| 111 | 'currency_symbol': '\u20ac', |
| 112 | 'decimal_point': ',', |
| 113 | 'frac_digits': 2, |
| 114 | 'grouping': [3, 3, 0], |
| 115 | 'int_curr_symbol': 'EUR ', |
| 116 | 'int_frac_digits': 2, |
| 117 | 'mon_decimal_point': ',', |
| 118 | 'mon_grouping': [3, 3, 0], |
| 119 | 'mon_thousands_sep': ' ', |
| 120 | 'n_cs_precedes': 0, |
| 121 | 'n_sep_by_space': 1, |
| 122 | 'n_sign_posn': 1, |
| 123 | 'negative_sign': '-', |
| 124 | 'p_cs_precedes': 0, |
| 125 | 'p_sep_by_space': 1, |
| 126 | 'p_sign_posn': 1, |
| 127 | 'positive_sign': '', |
| 128 | 'thousands_sep': ' ' |
| 129 | } |
| 130 | |
| 131 | |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 132 | class BaseFormattingTest(object): |
| 133 | # |
| 134 | # Utility functions for formatting tests |
| 135 | # |
| 136 | |
| 137 | def _test_formatfunc(self, format, value, out, func, **format_opts): |
| 138 | self.assertEqual( |
| 139 | func(format, value, **format_opts), out) |
| 140 | |
| 141 | def _test_format(self, format, value, out, **format_opts): |
| 142 | self._test_formatfunc(format, value, out, |
| 143 | func=locale.format, **format_opts) |
| 144 | |
| 145 | def _test_format_string(self, format, value, out, **format_opts): |
| 146 | self._test_formatfunc(format, value, out, |
| 147 | func=locale.format_string, **format_opts) |
| 148 | |
| 149 | def _test_currency(self, value, out, **format_opts): |
| 150 | self.assertEqual(locale.currency(value, **format_opts), out) |
| 151 | |
| 152 | |
| 153 | class EnUSNumberFormatting(BaseFormattingTest): |
Antoine Pitrou | 13856ea | 2008-07-26 21:02:53 +0000 | [diff] [blame] | 154 | # XXX there is a grouping + padding bug when the thousands separator |
| 155 | # is empty but the grouping array contains values (e.g. Solaris 10) |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 156 | |
| 157 | def setUp(self): |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 158 | self.sep = locale.localeconv()['thousands_sep'] |
| 159 | |
| 160 | def test_grouping(self): |
| 161 | self._test_format("%f", 1024, grouping=1, out='1%s024.000000' % self.sep) |
| 162 | self._test_format("%f", 102, grouping=1, out='102.000000') |
| 163 | self._test_format("%f", -42, grouping=1, out='-42.000000') |
| 164 | self._test_format("%+f", -42, grouping=1, out='-42.000000') |
| 165 | |
| 166 | def test_grouping_and_padding(self): |
| 167 | self._test_format("%20.f", -42, grouping=1, out='-42'.rjust(20)) |
Antoine Pitrou | 13856ea | 2008-07-26 21:02:53 +0000 | [diff] [blame] | 168 | if self.sep: |
| 169 | self._test_format("%+10.f", -4200, grouping=1, |
| 170 | out=('-4%s200' % self.sep).rjust(10)) |
| 171 | self._test_format("%-10.f", -4200, grouping=1, |
| 172 | out=('-4%s200' % self.sep).ljust(10)) |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 173 | |
| 174 | def test_integer_grouping(self): |
| 175 | self._test_format("%d", 4200, grouping=True, out='4%s200' % self.sep) |
| 176 | self._test_format("%+d", 4200, grouping=True, out='+4%s200' % self.sep) |
| 177 | self._test_format("%+d", -4200, grouping=True, out='-4%s200' % self.sep) |
| 178 | |
Antoine Pitrou | 350370c | 2009-03-14 00:13:13 +0000 | [diff] [blame] | 179 | def test_integer_grouping_and_padding(self): |
| 180 | self._test_format("%10d", 4200, grouping=True, |
| 181 | out=('4%s200' % self.sep).rjust(10)) |
| 182 | self._test_format("%-10d", -4200, grouping=True, |
| 183 | out=('-4%s200' % self.sep).ljust(10)) |
| 184 | |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 185 | def test_simple(self): |
| 186 | self._test_format("%f", 1024, grouping=0, out='1024.000000') |
| 187 | self._test_format("%f", 102, grouping=0, out='102.000000') |
| 188 | self._test_format("%f", -42, grouping=0, out='-42.000000') |
| 189 | self._test_format("%+f", -42, grouping=0, out='-42.000000') |
| 190 | |
| 191 | def test_padding(self): |
| 192 | self._test_format("%20.f", -42, grouping=0, out='-42'.rjust(20)) |
| 193 | self._test_format("%+10.f", -4200, grouping=0, out='-4200'.rjust(10)) |
| 194 | self._test_format("%-10.f", 4200, grouping=0, out='4200'.ljust(10)) |
| 195 | |
| 196 | def test_complex_formatting(self): |
| 197 | # Spaces in formatting string |
| 198 | self._test_format_string("One million is %i", 1000000, grouping=1, |
| 199 | out='One million is 1%s000%s000' % (self.sep, self.sep)) |
| 200 | self._test_format_string("One million is %i", 1000000, grouping=1, |
| 201 | out='One million is 1%s000%s000' % (self.sep, self.sep)) |
| 202 | # Dots in formatting string |
| 203 | self._test_format_string(".%f.", 1000.0, out='.1000.000000.') |
| 204 | # Padding |
Antoine Pitrou | 13856ea | 2008-07-26 21:02:53 +0000 | [diff] [blame] | 205 | if self.sep: |
| 206 | self._test_format_string("--> %10.2f", 4200, grouping=1, |
| 207 | out='--> ' + ('4%s200.00' % self.sep).rjust(10)) |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 208 | # Asterisk formats |
| 209 | self._test_format_string("%10.*f", (2, 1000), grouping=0, |
| 210 | out='1000.00'.rjust(10)) |
Antoine Pitrou | 13856ea | 2008-07-26 21:02:53 +0000 | [diff] [blame] | 211 | if self.sep: |
| 212 | self._test_format_string("%*.*f", (10, 2, 1000), grouping=1, |
| 213 | out=('1%s000.00' % self.sep).rjust(10)) |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 214 | # Test more-in-one |
Antoine Pitrou | 13856ea | 2008-07-26 21:02:53 +0000 | [diff] [blame] | 215 | if self.sep: |
| 216 | self._test_format_string("int %i float %.2f str %s", |
| 217 | (1000, 1000.0, 'str'), grouping=1, |
| 218 | out='int 1%s000 float 1%s000.00 str str' % |
| 219 | (self.sep, self.sep)) |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 220 | |
| 221 | |
R. David Murray | e59482e | 2009-04-01 03:42:00 +0000 | [diff] [blame] | 222 | class TestFormatPatternArg(unittest.TestCase): |
| 223 | # Test handling of pattern argument of format |
| 224 | |
| 225 | def test_onlyOnePattern(self): |
| 226 | # Issue 2522: accept exactly one % pattern, and no extra chars. |
| 227 | self.assertRaises(ValueError, locale.format, "%f\n", 'foo') |
| 228 | self.assertRaises(ValueError, locale.format, "%f\r", 'foo') |
| 229 | self.assertRaises(ValueError, locale.format, "%f\r\n", 'foo') |
| 230 | self.assertRaises(ValueError, locale.format, " %f", 'foo') |
| 231 | self.assertRaises(ValueError, locale.format, "%fg", 'foo') |
| 232 | self.assertRaises(ValueError, locale.format, "%^g", 'foo') |
| 233 | |
| 234 | |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 235 | class TestNumberFormatting(BaseLocalizedTest, EnUSNumberFormatting): |
| 236 | # Test number formatting with a real English locale. |
| 237 | |
| 238 | locale_type = locale.LC_NUMERIC |
| 239 | |
| 240 | def setUp(self): |
| 241 | BaseLocalizedTest.setUp(self) |
| 242 | EnUSNumberFormatting.setUp(self) |
| 243 | |
| 244 | |
| 245 | class TestEnUSNumberFormatting(EnUSCookedTest, EnUSNumberFormatting): |
| 246 | # Test number formatting with a cooked "en_US" locale. |
| 247 | |
| 248 | def setUp(self): |
| 249 | EnUSCookedTest.setUp(self) |
| 250 | EnUSNumberFormatting.setUp(self) |
| 251 | |
| 252 | def test_currency(self): |
| 253 | self._test_currency(50000, "$50000.00") |
| 254 | self._test_currency(50000, "$50,000.00", grouping=True) |
| 255 | self._test_currency(50000, "USD 50,000.00", |
| 256 | grouping=True, international=True) |
| 257 | |
| 258 | |
| 259 | class TestCNumberFormatting(CCookedTest, BaseFormattingTest): |
| 260 | # Test number formatting with a cooked "C" locale. |
| 261 | |
| 262 | def test_grouping(self): |
| 263 | self._test_format("%.2f", 12345.67, grouping=True, out='12345.67') |
| 264 | |
| 265 | def test_grouping_and_padding(self): |
| 266 | self._test_format("%9.2f", 12345.67, grouping=True, out=' 12345.67') |
| 267 | |
| 268 | |
Antoine Pitrou | 350370c | 2009-03-14 00:13:13 +0000 | [diff] [blame] | 269 | class TestFrFRNumberFormatting(FrFRCookedTest, BaseFormattingTest): |
| 270 | # Test number formatting with a cooked "fr_FR" locale. |
| 271 | |
| 272 | def test_decimal_point(self): |
| 273 | self._test_format("%.2f", 12345.67, out='12345,67') |
| 274 | |
| 275 | def test_grouping(self): |
| 276 | self._test_format("%.2f", 345.67, grouping=True, out='345,67') |
| 277 | self._test_format("%.2f", 12345.67, grouping=True, out='12 345,67') |
| 278 | |
| 279 | def test_grouping_and_padding(self): |
| 280 | self._test_format("%6.2f", 345.67, grouping=True, out='345,67') |
| 281 | self._test_format("%7.2f", 345.67, grouping=True, out=' 345,67') |
| 282 | self._test_format("%8.2f", 12345.67, grouping=True, out='12 345,67') |
| 283 | self._test_format("%9.2f", 12345.67, grouping=True, out='12 345,67') |
| 284 | self._test_format("%10.2f", 12345.67, grouping=True, out=' 12 345,67') |
| 285 | self._test_format("%-6.2f", 345.67, grouping=True, out='345,67') |
| 286 | self._test_format("%-7.2f", 345.67, grouping=True, out='345,67 ') |
| 287 | self._test_format("%-8.2f", 12345.67, grouping=True, out='12 345,67') |
| 288 | self._test_format("%-9.2f", 12345.67, grouping=True, out='12 345,67') |
| 289 | self._test_format("%-10.2f", 12345.67, grouping=True, out='12 345,67 ') |
| 290 | |
| 291 | def test_integer_grouping(self): |
| 292 | self._test_format("%d", 200, grouping=True, out='200') |
| 293 | self._test_format("%d", 4200, grouping=True, out='4 200') |
| 294 | |
| 295 | def test_integer_grouping_and_padding(self): |
| 296 | self._test_format("%4d", 4200, grouping=True, out='4 200') |
| 297 | self._test_format("%5d", 4200, grouping=True, out='4 200') |
| 298 | self._test_format("%10d", 4200, grouping=True, out='4 200'.rjust(10)) |
| 299 | self._test_format("%-4d", 4200, grouping=True, out='4 200') |
| 300 | self._test_format("%-5d", 4200, grouping=True, out='4 200') |
| 301 | self._test_format("%-10d", 4200, grouping=True, out='4 200'.ljust(10)) |
| 302 | |
| 303 | def test_currency(self): |
| 304 | euro = '\u20ac' |
| 305 | self._test_currency(50000, "50000,00 " + euro) |
| 306 | self._test_currency(50000, "50 000,00 " + euro, grouping=True) |
| 307 | # XXX is the trailing space a bug? |
| 308 | self._test_currency(50000, "50 000,00 EUR ", |
| 309 | grouping=True, international=True) |
| 310 | |
| 311 | |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 312 | class TestMiscellaneous(unittest.TestCase): |
| 313 | def test_getpreferredencoding(self): |
| 314 | # Invoke getpreferredencoding to make sure it does not cause exceptions. |
| 315 | enc = locale.getpreferredencoding() |
| 316 | if enc: |
| 317 | # If encoding non-empty, make sure it is valid |
| 318 | codecs.lookup(enc) |
| 319 | |
| 320 | if hasattr(locale, "strcoll"): |
| 321 | def test_strcoll_3303(self): |
| 322 | # test crasher from bug #3303 |
| 323 | self.assertRaises(TypeError, locale.strcoll, "a", None) |
| 324 | self.assertRaises(TypeError, locale.strcoll, b"a", None) |
| 325 | |
| 326 | |
| 327 | def test_main(): |
Antoine Pitrou | 13856ea | 2008-07-26 21:02:53 +0000 | [diff] [blame] | 328 | tests = [ |
| 329 | TestMiscellaneous, |
R. David Murray | e59482e | 2009-04-01 03:42:00 +0000 | [diff] [blame] | 330 | TestFormatPatternArg, |
Antoine Pitrou | 13856ea | 2008-07-26 21:02:53 +0000 | [diff] [blame] | 331 | TestEnUSNumberFormatting, |
Antoine Pitrou | 350370c | 2009-03-14 00:13:13 +0000 | [diff] [blame] | 332 | TestCNumberFormatting, |
| 333 | TestFrFRNumberFormatting, |
Antoine Pitrou | 13856ea | 2008-07-26 21:02:53 +0000 | [diff] [blame] | 334 | ] |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 335 | # SkipTest can't be raised inside unittests, handle it manually instead |
Antoine Pitrou | 13856ea | 2008-07-26 21:02:53 +0000 | [diff] [blame] | 336 | try: |
| 337 | get_enUS_locale() |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 338 | except unittest.SkipTest as e: |
Antoine Pitrou | 13856ea | 2008-07-26 21:02:53 +0000 | [diff] [blame] | 339 | if verbose: |
| 340 | print("Some tests will be disabled: %s" % e) |
| 341 | else: |
| 342 | tests += [TestNumberFormatting] |
| 343 | run_unittest(*tests) |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 344 | |
| 345 | if __name__ == '__main__': |
| 346 | test_main() |