Serhiy Storchaka | 880254e | 2013-07-17 13:23:45 +0300 | [diff] [blame] | 1 | from test.support import 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 | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 7 | class BaseLocalizedTest(unittest.TestCase): |
| 8 | # |
| 9 | # Base class for tests using a real locale |
| 10 | # |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 11 | |
Serhiy Storchaka | 880254e | 2013-07-17 13:23:45 +0300 | [diff] [blame] | 12 | @classmethod |
| 13 | def setUpClass(cls): |
| 14 | if sys.platform == 'darwin': |
| 15 | import os |
| 16 | tlocs = ("en_US.UTF-8", "en_US.ISO8859-1", "en_US") |
| 17 | if int(os.uname().release.split('.')[0]) < 10: |
| 18 | # The locale test work fine on OSX 10.6, I (ronaldoussoren) |
| 19 | # haven't had time yet to verify if tests work on OSX 10.5 |
| 20 | # (10.4 is known to be bad) |
| 21 | raise unittest.SkipTest("Locale support on MacOSX is minimal") |
| 22 | elif sys.platform.startswith("win"): |
| 23 | tlocs = ("En", "English") |
| 24 | else: |
| 25 | tlocs = ("en_US.UTF-8", "en_US.ISO8859-1", |
| 26 | "en_US.US-ASCII", "en_US") |
| 27 | try: |
| 28 | oldlocale = locale.setlocale(locale.LC_NUMERIC) |
| 29 | for tloc in tlocs: |
| 30 | try: |
| 31 | locale.setlocale(locale.LC_NUMERIC, tloc) |
| 32 | except locale.Error: |
| 33 | continue |
| 34 | break |
| 35 | else: |
| 36 | raise unittest.SkipTest("Test locale not supported " |
| 37 | "(tried %s)" % (', '.join(tlocs))) |
| 38 | cls.enUS_locale = tloc |
| 39 | finally: |
| 40 | locale.setlocale(locale.LC_NUMERIC, oldlocale) |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 41 | |
Serhiy Storchaka | 880254e | 2013-07-17 13:23:45 +0300 | [diff] [blame] | 42 | def setUp(self): |
| 43 | oldlocale = locale.setlocale(self.locale_type) |
| 44 | self.addCleanup(locale.setlocale, self.locale_type, oldlocale) |
| 45 | locale.setlocale(self.locale_type, self.enUS_locale) |
| 46 | if verbose: |
| 47 | print("testing with %r..." % self.enUS_locale, end=' ', flush=True) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 48 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 49 | |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 50 | class BaseCookedTest(unittest.TestCase): |
| 51 | # |
| 52 | # Base class for tests using cooked localeconv() values |
| 53 | # |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 54 | |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 55 | def setUp(self): |
| 56 | locale._override_localeconv = self.cooked_values |
| 57 | |
| 58 | def tearDown(self): |
| 59 | locale._override_localeconv = {} |
| 60 | |
| 61 | class CCookedTest(BaseCookedTest): |
| 62 | # A cooked "C" locale |
| 63 | |
| 64 | cooked_values = { |
| 65 | 'currency_symbol': '', |
| 66 | 'decimal_point': '.', |
| 67 | 'frac_digits': 127, |
| 68 | 'grouping': [], |
| 69 | 'int_curr_symbol': '', |
| 70 | 'int_frac_digits': 127, |
| 71 | 'mon_decimal_point': '', |
| 72 | 'mon_grouping': [], |
| 73 | 'mon_thousands_sep': '', |
| 74 | 'n_cs_precedes': 127, |
| 75 | 'n_sep_by_space': 127, |
| 76 | 'n_sign_posn': 127, |
| 77 | 'negative_sign': '', |
| 78 | 'p_cs_precedes': 127, |
| 79 | 'p_sep_by_space': 127, |
| 80 | 'p_sign_posn': 127, |
| 81 | 'positive_sign': '', |
| 82 | 'thousands_sep': '' |
| 83 | } |
| 84 | |
| 85 | class EnUSCookedTest(BaseCookedTest): |
| 86 | # A cooked "en_US" locale |
| 87 | |
| 88 | cooked_values = { |
| 89 | 'currency_symbol': '$', |
| 90 | 'decimal_point': '.', |
| 91 | 'frac_digits': 2, |
| 92 | 'grouping': [3, 3, 0], |
| 93 | 'int_curr_symbol': 'USD ', |
| 94 | 'int_frac_digits': 2, |
| 95 | 'mon_decimal_point': '.', |
| 96 | 'mon_grouping': [3, 3, 0], |
| 97 | 'mon_thousands_sep': ',', |
| 98 | 'n_cs_precedes': 1, |
| 99 | 'n_sep_by_space': 0, |
| 100 | 'n_sign_posn': 1, |
| 101 | 'negative_sign': '-', |
| 102 | 'p_cs_precedes': 1, |
| 103 | 'p_sep_by_space': 0, |
| 104 | 'p_sign_posn': 1, |
| 105 | 'positive_sign': '', |
| 106 | 'thousands_sep': ',' |
| 107 | } |
| 108 | |
| 109 | |
Antoine Pitrou | 350370c | 2009-03-14 00:13:13 +0000 | [diff] [blame] | 110 | class FrFRCookedTest(BaseCookedTest): |
| 111 | # A cooked "fr_FR" locale with a space character as decimal separator |
| 112 | # and a non-ASCII currency symbol. |
| 113 | |
| 114 | cooked_values = { |
| 115 | 'currency_symbol': '\u20ac', |
| 116 | 'decimal_point': ',', |
| 117 | 'frac_digits': 2, |
| 118 | 'grouping': [3, 3, 0], |
| 119 | 'int_curr_symbol': 'EUR ', |
| 120 | 'int_frac_digits': 2, |
| 121 | 'mon_decimal_point': ',', |
| 122 | 'mon_grouping': [3, 3, 0], |
| 123 | 'mon_thousands_sep': ' ', |
| 124 | 'n_cs_precedes': 0, |
| 125 | 'n_sep_by_space': 1, |
| 126 | 'n_sign_posn': 1, |
| 127 | 'negative_sign': '-', |
| 128 | 'p_cs_precedes': 0, |
| 129 | 'p_sep_by_space': 1, |
| 130 | 'p_sign_posn': 1, |
| 131 | 'positive_sign': '', |
| 132 | 'thousands_sep': ' ' |
| 133 | } |
| 134 | |
| 135 | |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 136 | class BaseFormattingTest(object): |
| 137 | # |
| 138 | # Utility functions for formatting tests |
| 139 | # |
| 140 | |
| 141 | def _test_formatfunc(self, format, value, out, func, **format_opts): |
| 142 | self.assertEqual( |
| 143 | func(format, value, **format_opts), out) |
| 144 | |
| 145 | def _test_format(self, format, value, out, **format_opts): |
| 146 | self._test_formatfunc(format, value, out, |
| 147 | func=locale.format, **format_opts) |
| 148 | |
| 149 | def _test_format_string(self, format, value, out, **format_opts): |
| 150 | self._test_formatfunc(format, value, out, |
| 151 | func=locale.format_string, **format_opts) |
| 152 | |
| 153 | def _test_currency(self, value, out, **format_opts): |
| 154 | self.assertEqual(locale.currency(value, **format_opts), out) |
| 155 | |
| 156 | |
| 157 | class EnUSNumberFormatting(BaseFormattingTest): |
Antoine Pitrou | 13856ea | 2008-07-26 21:02:53 +0000 | [diff] [blame] | 158 | # XXX there is a grouping + padding bug when the thousands separator |
| 159 | # is empty but the grouping array contains values (e.g. Solaris 10) |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 160 | |
| 161 | def setUp(self): |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 162 | self.sep = locale.localeconv()['thousands_sep'] |
| 163 | |
| 164 | def test_grouping(self): |
| 165 | self._test_format("%f", 1024, grouping=1, out='1%s024.000000' % self.sep) |
| 166 | self._test_format("%f", 102, grouping=1, out='102.000000') |
| 167 | self._test_format("%f", -42, grouping=1, out='-42.000000') |
| 168 | self._test_format("%+f", -42, grouping=1, out='-42.000000') |
| 169 | |
| 170 | def test_grouping_and_padding(self): |
| 171 | self._test_format("%20.f", -42, grouping=1, out='-42'.rjust(20)) |
Antoine Pitrou | 13856ea | 2008-07-26 21:02:53 +0000 | [diff] [blame] | 172 | if self.sep: |
| 173 | self._test_format("%+10.f", -4200, grouping=1, |
| 174 | out=('-4%s200' % self.sep).rjust(10)) |
| 175 | self._test_format("%-10.f", -4200, grouping=1, |
| 176 | out=('-4%s200' % self.sep).ljust(10)) |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 177 | |
| 178 | def test_integer_grouping(self): |
| 179 | self._test_format("%d", 4200, grouping=True, out='4%s200' % self.sep) |
| 180 | self._test_format("%+d", 4200, grouping=True, out='+4%s200' % self.sep) |
| 181 | self._test_format("%+d", -4200, grouping=True, out='-4%s200' % self.sep) |
| 182 | |
Antoine Pitrou | 350370c | 2009-03-14 00:13:13 +0000 | [diff] [blame] | 183 | def test_integer_grouping_and_padding(self): |
| 184 | self._test_format("%10d", 4200, grouping=True, |
| 185 | out=('4%s200' % self.sep).rjust(10)) |
| 186 | self._test_format("%-10d", -4200, grouping=True, |
| 187 | out=('-4%s200' % self.sep).ljust(10)) |
| 188 | |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 189 | def test_simple(self): |
| 190 | self._test_format("%f", 1024, grouping=0, out='1024.000000') |
| 191 | self._test_format("%f", 102, grouping=0, out='102.000000') |
| 192 | self._test_format("%f", -42, grouping=0, out='-42.000000') |
| 193 | self._test_format("%+f", -42, grouping=0, out='-42.000000') |
| 194 | |
| 195 | def test_padding(self): |
| 196 | self._test_format("%20.f", -42, grouping=0, out='-42'.rjust(20)) |
| 197 | self._test_format("%+10.f", -4200, grouping=0, out='-4200'.rjust(10)) |
| 198 | self._test_format("%-10.f", 4200, grouping=0, out='4200'.ljust(10)) |
| 199 | |
| 200 | def test_complex_formatting(self): |
| 201 | # Spaces in formatting string |
| 202 | self._test_format_string("One million is %i", 1000000, grouping=1, |
| 203 | out='One million is 1%s000%s000' % (self.sep, self.sep)) |
| 204 | self._test_format_string("One million is %i", 1000000, grouping=1, |
| 205 | out='One million is 1%s000%s000' % (self.sep, self.sep)) |
| 206 | # Dots in formatting string |
| 207 | self._test_format_string(".%f.", 1000.0, out='.1000.000000.') |
| 208 | # Padding |
Antoine Pitrou | 13856ea | 2008-07-26 21:02:53 +0000 | [diff] [blame] | 209 | if self.sep: |
| 210 | self._test_format_string("--> %10.2f", 4200, grouping=1, |
| 211 | out='--> ' + ('4%s200.00' % self.sep).rjust(10)) |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 212 | # Asterisk formats |
| 213 | self._test_format_string("%10.*f", (2, 1000), grouping=0, |
| 214 | out='1000.00'.rjust(10)) |
Antoine Pitrou | 13856ea | 2008-07-26 21:02:53 +0000 | [diff] [blame] | 215 | if self.sep: |
| 216 | self._test_format_string("%*.*f", (10, 2, 1000), grouping=1, |
| 217 | out=('1%s000.00' % self.sep).rjust(10)) |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 218 | # Test more-in-one |
Antoine Pitrou | 13856ea | 2008-07-26 21:02:53 +0000 | [diff] [blame] | 219 | if self.sep: |
| 220 | self._test_format_string("int %i float %.2f str %s", |
| 221 | (1000, 1000.0, 'str'), grouping=1, |
| 222 | out='int 1%s000 float 1%s000.00 str str' % |
| 223 | (self.sep, self.sep)) |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 224 | |
| 225 | |
R. David Murray | e59482e | 2009-04-01 03:42:00 +0000 | [diff] [blame] | 226 | class TestFormatPatternArg(unittest.TestCase): |
| 227 | # Test handling of pattern argument of format |
| 228 | |
| 229 | def test_onlyOnePattern(self): |
| 230 | # Issue 2522: accept exactly one % pattern, and no extra chars. |
| 231 | self.assertRaises(ValueError, locale.format, "%f\n", 'foo') |
| 232 | self.assertRaises(ValueError, locale.format, "%f\r", 'foo') |
| 233 | self.assertRaises(ValueError, locale.format, "%f\r\n", 'foo') |
| 234 | self.assertRaises(ValueError, locale.format, " %f", 'foo') |
| 235 | self.assertRaises(ValueError, locale.format, "%fg", 'foo') |
| 236 | self.assertRaises(ValueError, locale.format, "%^g", 'foo') |
R. David Murray | ad78d15 | 2010-04-27 02:45:53 +0000 | [diff] [blame] | 237 | self.assertRaises(ValueError, locale.format, "%f%%", 'foo') |
| 238 | |
| 239 | |
| 240 | class TestLocaleFormatString(unittest.TestCase): |
| 241 | """General tests on locale.format_string""" |
| 242 | |
| 243 | def test_percent_escape(self): |
| 244 | self.assertEqual(locale.format_string('%f%%', 1.0), '%f%%' % 1.0) |
| 245 | self.assertEqual(locale.format_string('%d %f%%d', (1, 1.0)), |
| 246 | '%d %f%%d' % (1, 1.0)) |
| 247 | self.assertEqual(locale.format_string('%(foo)s %%d', {'foo': 'bar'}), |
| 248 | ('%(foo)s %%d' % {'foo': 'bar'})) |
| 249 | |
| 250 | def test_mapping(self): |
| 251 | self.assertEqual(locale.format_string('%(foo)s bing.', {'foo': 'bar'}), |
| 252 | ('%(foo)s bing.' % {'foo': 'bar'})) |
| 253 | self.assertEqual(locale.format_string('%(foo)s', {'foo': 'bar'}), |
| 254 | ('%(foo)s' % {'foo': 'bar'})) |
| 255 | |
R. David Murray | e59482e | 2009-04-01 03:42:00 +0000 | [diff] [blame] | 256 | |
| 257 | |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 258 | class TestNumberFormatting(BaseLocalizedTest, EnUSNumberFormatting): |
| 259 | # Test number formatting with a real English locale. |
| 260 | |
| 261 | locale_type = locale.LC_NUMERIC |
| 262 | |
| 263 | def setUp(self): |
| 264 | BaseLocalizedTest.setUp(self) |
| 265 | EnUSNumberFormatting.setUp(self) |
| 266 | |
| 267 | |
| 268 | class TestEnUSNumberFormatting(EnUSCookedTest, EnUSNumberFormatting): |
| 269 | # Test number formatting with a cooked "en_US" locale. |
| 270 | |
| 271 | def setUp(self): |
| 272 | EnUSCookedTest.setUp(self) |
| 273 | EnUSNumberFormatting.setUp(self) |
| 274 | |
| 275 | def test_currency(self): |
| 276 | self._test_currency(50000, "$50000.00") |
| 277 | self._test_currency(50000, "$50,000.00", grouping=True) |
| 278 | self._test_currency(50000, "USD 50,000.00", |
| 279 | grouping=True, international=True) |
| 280 | |
| 281 | |
| 282 | class TestCNumberFormatting(CCookedTest, BaseFormattingTest): |
| 283 | # Test number formatting with a cooked "C" locale. |
| 284 | |
| 285 | def test_grouping(self): |
| 286 | self._test_format("%.2f", 12345.67, grouping=True, out='12345.67') |
| 287 | |
| 288 | def test_grouping_and_padding(self): |
| 289 | self._test_format("%9.2f", 12345.67, grouping=True, out=' 12345.67') |
| 290 | |
| 291 | |
Antoine Pitrou | 350370c | 2009-03-14 00:13:13 +0000 | [diff] [blame] | 292 | class TestFrFRNumberFormatting(FrFRCookedTest, BaseFormattingTest): |
| 293 | # Test number formatting with a cooked "fr_FR" locale. |
| 294 | |
| 295 | def test_decimal_point(self): |
| 296 | self._test_format("%.2f", 12345.67, out='12345,67') |
| 297 | |
| 298 | def test_grouping(self): |
| 299 | self._test_format("%.2f", 345.67, grouping=True, out='345,67') |
| 300 | self._test_format("%.2f", 12345.67, grouping=True, out='12 345,67') |
| 301 | |
| 302 | def test_grouping_and_padding(self): |
| 303 | self._test_format("%6.2f", 345.67, grouping=True, out='345,67') |
| 304 | self._test_format("%7.2f", 345.67, grouping=True, out=' 345,67') |
| 305 | self._test_format("%8.2f", 12345.67, grouping=True, out='12 345,67') |
| 306 | self._test_format("%9.2f", 12345.67, grouping=True, out='12 345,67') |
| 307 | self._test_format("%10.2f", 12345.67, grouping=True, out=' 12 345,67') |
| 308 | self._test_format("%-6.2f", 345.67, grouping=True, out='345,67') |
| 309 | self._test_format("%-7.2f", 345.67, grouping=True, out='345,67 ') |
| 310 | self._test_format("%-8.2f", 12345.67, grouping=True, out='12 345,67') |
| 311 | self._test_format("%-9.2f", 12345.67, grouping=True, out='12 345,67') |
| 312 | self._test_format("%-10.2f", 12345.67, grouping=True, out='12 345,67 ') |
| 313 | |
| 314 | def test_integer_grouping(self): |
| 315 | self._test_format("%d", 200, grouping=True, out='200') |
| 316 | self._test_format("%d", 4200, grouping=True, out='4 200') |
| 317 | |
| 318 | def test_integer_grouping_and_padding(self): |
| 319 | self._test_format("%4d", 4200, grouping=True, out='4 200') |
| 320 | self._test_format("%5d", 4200, grouping=True, out='4 200') |
| 321 | self._test_format("%10d", 4200, grouping=True, out='4 200'.rjust(10)) |
| 322 | self._test_format("%-4d", 4200, grouping=True, out='4 200') |
| 323 | self._test_format("%-5d", 4200, grouping=True, out='4 200') |
| 324 | self._test_format("%-10d", 4200, grouping=True, out='4 200'.ljust(10)) |
| 325 | |
| 326 | def test_currency(self): |
| 327 | euro = '\u20ac' |
| 328 | self._test_currency(50000, "50000,00 " + euro) |
| 329 | self._test_currency(50000, "50 000,00 " + euro, grouping=True) |
| 330 | # XXX is the trailing space a bug? |
| 331 | self._test_currency(50000, "50 000,00 EUR ", |
| 332 | grouping=True, international=True) |
| 333 | |
| 334 | |
Antoine Pitrou | 6a448d4 | 2009-10-19 19:43:09 +0000 | [diff] [blame] | 335 | class TestCollation(unittest.TestCase): |
| 336 | # Test string collation functions |
| 337 | |
| 338 | def test_strcoll(self): |
| 339 | self.assertLess(locale.strcoll('a', 'b'), 0) |
| 340 | self.assertEqual(locale.strcoll('a', 'a'), 0) |
| 341 | self.assertGreater(locale.strcoll('b', 'a'), 0) |
| 342 | |
| 343 | def test_strxfrm(self): |
| 344 | self.assertLess(locale.strxfrm('a'), locale.strxfrm('b')) |
| 345 | |
| 346 | |
| 347 | class TestEnUSCollation(BaseLocalizedTest, TestCollation): |
| 348 | # Test string collation functions with a real English locale |
| 349 | |
| 350 | locale_type = locale.LC_ALL |
| 351 | |
| 352 | def setUp(self): |
Antoine Pitrou | 6a448d4 | 2009-10-19 19:43:09 +0000 | [diff] [blame] | 353 | enc = codecs.lookup(locale.getpreferredencoding(False) or 'ascii').name |
| 354 | if enc not in ('utf-8', 'iso8859-1', 'cp1252'): |
| 355 | raise unittest.SkipTest('encoding not suitable') |
| 356 | if enc != 'iso8859-1' and (sys.platform == 'darwin' or |
| 357 | sys.platform.startswith('freebsd')): |
| 358 | raise unittest.SkipTest('wcscoll/wcsxfrm have known bugs') |
Hirokazu Yamamoto | b3b390d | 2010-09-23 15:20:15 +0000 | [diff] [blame] | 359 | BaseLocalizedTest.setUp(self) |
Antoine Pitrou | 6a448d4 | 2009-10-19 19:43:09 +0000 | [diff] [blame] | 360 | |
| 361 | def test_strcoll_with_diacritic(self): |
| 362 | self.assertLess(locale.strcoll('à', 'b'), 0) |
| 363 | |
| 364 | def test_strxfrm_with_diacritic(self): |
| 365 | self.assertLess(locale.strxfrm('à'), locale.strxfrm('b')) |
| 366 | |
| 367 | |
Serhiy Storchaka | 16f02d2 | 2013-12-19 21:21:40 +0200 | [diff] [blame] | 368 | class NormalizeTest(unittest.TestCase): |
| 369 | def check(self, localename, expected): |
| 370 | self.assertEqual(locale.normalize(localename), expected, msg=localename) |
| 371 | |
| 372 | def test_locale_alias(self): |
| 373 | for localename, alias in locale.locale_alias.items(): |
| 374 | with self.subTest(locale=(localename, alias)): |
| 375 | self.check(localename, alias) |
| 376 | |
| 377 | def test_empty(self): |
| 378 | self.check('', '') |
| 379 | |
| 380 | def test_c(self): |
| 381 | self.check('c', 'C') |
| 382 | self.check('posix', 'C') |
| 383 | |
| 384 | def test_english(self): |
| 385 | self.check('en', 'en_US.ISO8859-1') |
| 386 | self.check('EN', 'en_US.ISO8859-1') |
Serhiy Storchaka | 8c4f57d | 2013-12-27 00:56:53 +0200 | [diff] [blame] | 387 | self.check('en.iso88591', 'en_US.ISO8859-1') |
Serhiy Storchaka | 16f02d2 | 2013-12-19 21:21:40 +0200 | [diff] [blame] | 388 | self.check('en_US', 'en_US.ISO8859-1') |
| 389 | self.check('en_us', 'en_US.ISO8859-1') |
| 390 | self.check('en_GB', 'en_GB.ISO8859-1') |
| 391 | self.check('en_US.UTF-8', 'en_US.UTF-8') |
| 392 | self.check('en_US.utf8', 'en_US.UTF-8') |
| 393 | self.check('en_US:UTF-8', 'en_US.UTF-8') |
| 394 | self.check('en_US.ISO8859-1', 'en_US.ISO8859-1') |
| 395 | self.check('en_US.US-ASCII', 'en_US.ISO8859-1') |
Serhiy Storchaka | 8c4f57d | 2013-12-27 00:56:53 +0200 | [diff] [blame] | 396 | self.check('en_US.88591', 'en_US.ISO8859-1') |
| 397 | self.check('en_US.885915', 'en_US.ISO8859-15') |
Serhiy Storchaka | 16f02d2 | 2013-12-19 21:21:40 +0200 | [diff] [blame] | 398 | self.check('english', 'en_EN.ISO8859-1') |
Serhiy Storchaka | 8c4f57d | 2013-12-27 00:56:53 +0200 | [diff] [blame] | 399 | self.check('english_uk.ascii', 'en_GB.ISO8859-1') |
Serhiy Storchaka | 16f02d2 | 2013-12-19 21:21:40 +0200 | [diff] [blame] | 400 | |
| 401 | def test_hyphenated_encoding(self): |
| 402 | self.check('az_AZ.iso88599e', 'az_AZ.ISO8859-9E') |
| 403 | self.check('az_AZ.ISO8859-9E', 'az_AZ.ISO8859-9E') |
| 404 | self.check('tt_RU.koi8c', 'tt_RU.KOI8-C') |
| 405 | self.check('tt_RU.KOI8-C', 'tt_RU.KOI8-C') |
| 406 | self.check('lo_LA.cp1133', 'lo_LA.IBM-CP1133') |
| 407 | self.check('lo_LA.ibmcp1133', 'lo_LA.IBM-CP1133') |
| 408 | self.check('lo_LA.IBM-CP1133', 'lo_LA.IBM-CP1133') |
| 409 | self.check('uk_ua.microsoftcp1251', 'uk_UA.CP1251') |
| 410 | self.check('uk_ua.microsoft-cp1251', 'uk_UA.CP1251') |
| 411 | self.check('ka_ge.georgianacademy', 'ka_GE.GEORGIAN-ACADEMY') |
| 412 | self.check('ka_GE.GEORGIAN-ACADEMY', 'ka_GE.GEORGIAN-ACADEMY') |
| 413 | self.check('cs_CZ.iso88592', 'cs_CZ.ISO8859-2') |
| 414 | self.check('cs_CZ.ISO8859-2', 'cs_CZ.ISO8859-2') |
| 415 | |
| 416 | def test_euro_modifier(self): |
| 417 | self.check('de_DE@euro', 'de_DE.ISO8859-15') |
| 418 | self.check('en_US.ISO8859-15@euro', 'en_US.ISO8859-15') |
Serhiy Storchaka | 8c4f57d | 2013-12-27 00:56:53 +0200 | [diff] [blame] | 419 | self.check('de_DE.utf8@euro', 'de_DE.UTF-8') |
Serhiy Storchaka | 16f02d2 | 2013-12-19 21:21:40 +0200 | [diff] [blame] | 420 | |
| 421 | def test_latin_modifier(self): |
| 422 | self.check('be_BY.UTF-8@latin', 'be_BY.UTF-8@latin') |
| 423 | self.check('sr_RS.UTF-8@latin', 'sr_RS.UTF-8@latin') |
Serhiy Storchaka | 8c4f57d | 2013-12-27 00:56:53 +0200 | [diff] [blame] | 424 | self.check('sr_RS.UTF-8@latn', 'sr_RS.UTF-8@latin') |
Serhiy Storchaka | 16f02d2 | 2013-12-19 21:21:40 +0200 | [diff] [blame] | 425 | |
| 426 | def test_valencia_modifier(self): |
| 427 | self.check('ca_ES.UTF-8@valencia', 'ca_ES.UTF-8@valencia') |
Serhiy Storchaka | 4601df5 | 2014-10-02 11:36:12 +0300 | [diff] [blame] | 428 | self.check('ca_ES@valencia', 'ca_ES.ISO8859-15@valencia') |
Serhiy Storchaka | 16f02d2 | 2013-12-19 21:21:40 +0200 | [diff] [blame] | 429 | self.check('ca@valencia', 'ca_ES.ISO8859-1@valencia') |
| 430 | |
Serhiy Storchaka | 5eb0153 | 2013-12-26 21:20:59 +0200 | [diff] [blame] | 431 | def test_devanagari_modifier(self): |
| 432 | self.check('ks_IN.UTF-8@devanagari', 'ks_IN.UTF-8@devanagari') |
| 433 | self.check('ks_IN@devanagari', 'ks_IN.UTF-8@devanagari') |
| 434 | self.check('ks@devanagari', 'ks_IN.UTF-8@devanagari') |
| 435 | self.check('ks_IN.UTF-8', 'ks_IN.UTF-8') |
| 436 | self.check('ks_IN', 'ks_IN.UTF-8') |
| 437 | self.check('ks', 'ks_IN.UTF-8') |
| 438 | self.check('sd_IN.UTF-8@devanagari', 'sd_IN.UTF-8@devanagari') |
| 439 | self.check('sd_IN@devanagari', 'sd_IN.UTF-8@devanagari') |
| 440 | self.check('sd@devanagari', 'sd_IN.UTF-8@devanagari') |
| 441 | self.check('sd_IN.UTF-8', 'sd_IN.UTF-8') |
| 442 | self.check('sd_IN', 'sd_IN.UTF-8') |
| 443 | self.check('sd', 'sd_IN.UTF-8') |
| 444 | |
Serhiy Storchaka | 8c4f57d | 2013-12-27 00:56:53 +0200 | [diff] [blame] | 445 | def test_euc_encoding(self): |
| 446 | self.check('ja_jp.euc', 'ja_JP.eucJP') |
| 447 | self.check('ja_jp.eucjp', 'ja_JP.eucJP') |
| 448 | self.check('ko_kr.euc', 'ko_KR.eucKR') |
| 449 | self.check('ko_kr.euckr', 'ko_KR.eucKR') |
| 450 | self.check('zh_cn.euc', 'zh_CN.eucCN') |
| 451 | self.check('zh_tw.euc', 'zh_TW.eucTW') |
| 452 | self.check('zh_tw.euctw', 'zh_TW.eucTW') |
| 453 | |
| 454 | def test_japanese(self): |
| 455 | self.check('ja', 'ja_JP.eucJP') |
| 456 | self.check('ja.jis', 'ja_JP.JIS7') |
| 457 | self.check('ja.sjis', 'ja_JP.SJIS') |
| 458 | self.check('ja_jp', 'ja_JP.eucJP') |
| 459 | self.check('ja_jp.ajec', 'ja_JP.eucJP') |
| 460 | self.check('ja_jp.euc', 'ja_JP.eucJP') |
| 461 | self.check('ja_jp.eucjp', 'ja_JP.eucJP') |
| 462 | self.check('ja_jp.iso-2022-jp', 'ja_JP.JIS7') |
| 463 | self.check('ja_jp.iso2022jp', 'ja_JP.JIS7') |
| 464 | self.check('ja_jp.jis', 'ja_JP.JIS7') |
| 465 | self.check('ja_jp.jis7', 'ja_JP.JIS7') |
| 466 | self.check('ja_jp.mscode', 'ja_JP.SJIS') |
| 467 | self.check('ja_jp.pck', 'ja_JP.SJIS') |
| 468 | self.check('ja_jp.sjis', 'ja_JP.SJIS') |
| 469 | self.check('ja_jp.ujis', 'ja_JP.eucJP') |
| 470 | self.check('ja_jp.utf8', 'ja_JP.UTF-8') |
| 471 | self.check('japan', 'ja_JP.eucJP') |
| 472 | self.check('japanese', 'ja_JP.eucJP') |
| 473 | self.check('japanese-euc', 'ja_JP.eucJP') |
| 474 | self.check('japanese.euc', 'ja_JP.eucJP') |
| 475 | self.check('japanese.sjis', 'ja_JP.SJIS') |
| 476 | self.check('jp_jp', 'ja_JP.eucJP') |
| 477 | |
Serhiy Storchaka | 16f02d2 | 2013-12-19 21:21:40 +0200 | [diff] [blame] | 478 | |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 479 | class TestMiscellaneous(unittest.TestCase): |
| 480 | def test_getpreferredencoding(self): |
| 481 | # Invoke getpreferredencoding to make sure it does not cause exceptions. |
| 482 | enc = locale.getpreferredencoding() |
| 483 | if enc: |
| 484 | # If encoding non-empty, make sure it is valid |
| 485 | codecs.lookup(enc) |
| 486 | |
Antoine Pitrou | 6a448d4 | 2009-10-19 19:43:09 +0000 | [diff] [blame] | 487 | def test_strcoll_3303(self): |
| 488 | # test crasher from bug #3303 |
| 489 | self.assertRaises(TypeError, locale.strcoll, "a", None) |
| 490 | self.assertRaises(TypeError, locale.strcoll, b"a", None) |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 491 | |
Amaury Forgeot d'Arc | 64f3ca4 | 2009-12-01 21:59:18 +0000 | [diff] [blame] | 492 | def test_setlocale_category(self): |
| 493 | locale.setlocale(locale.LC_ALL) |
| 494 | locale.setlocale(locale.LC_TIME) |
| 495 | locale.setlocale(locale.LC_CTYPE) |
| 496 | locale.setlocale(locale.LC_COLLATE) |
| 497 | locale.setlocale(locale.LC_MONETARY) |
| 498 | locale.setlocale(locale.LC_NUMERIC) |
| 499 | |
| 500 | # crasher from bug #7419 |
| 501 | self.assertRaises(locale.Error, locale.setlocale, 12345) |
| 502 | |
Antoine Pitrou | 0e3c5a8 | 2011-07-24 02:40:25 +0200 | [diff] [blame] | 503 | def test_getsetlocale_issue1813(self): |
| 504 | # Issue #1813: setting and getting the locale under a Turkish locale |
Antoine Pitrou | d05066d | 2011-07-26 23:55:33 +0200 | [diff] [blame] | 505 | oldlocale = locale.setlocale(locale.LC_CTYPE) |
Antoine Pitrou | 0e3c5a8 | 2011-07-24 02:40:25 +0200 | [diff] [blame] | 506 | self.addCleanup(locale.setlocale, locale.LC_CTYPE, oldlocale) |
| 507 | try: |
| 508 | locale.setlocale(locale.LC_CTYPE, 'tr_TR') |
| 509 | except locale.Error: |
| 510 | # Unsupported locale on this system |
| 511 | self.skipTest('test needs Turkish locale') |
Antoine Pitrou | d05066d | 2011-07-26 23:55:33 +0200 | [diff] [blame] | 512 | loc = locale.getlocale(locale.LC_CTYPE) |
Antoine Pitrou | 83d2193 | 2011-07-26 14:45:22 +0200 | [diff] [blame] | 513 | if verbose: |
Serhiy Storchaka | 0954585 | 2015-02-18 08:04:37 +0200 | [diff] [blame] | 514 | print('testing with %a' % (loc,), end=' ', flush=True) |
Antoine Pitrou | 0e3c5a8 | 2011-07-24 02:40:25 +0200 | [diff] [blame] | 515 | locale.setlocale(locale.LC_CTYPE, loc) |
Antoine Pitrou | d05066d | 2011-07-26 23:55:33 +0200 | [diff] [blame] | 516 | self.assertEqual(loc, locale.getlocale(locale.LC_CTYPE)) |
Antoine Pitrou | 0e3c5a8 | 2011-07-24 02:40:25 +0200 | [diff] [blame] | 517 | |
Petri Lehtinen | 3c85fe0 | 2011-11-04 21:35:07 +0200 | [diff] [blame] | 518 | def test_invalid_locale_format_in_localetuple(self): |
| 519 | with self.assertRaises(TypeError): |
| 520 | locale.setlocale(locale.LC_ALL, b'fi_FI') |
| 521 | |
| 522 | def test_invalid_iterable_in_localetuple(self): |
| 523 | with self.assertRaises(TypeError): |
| 524 | locale.setlocale(locale.LC_ALL, (b'not', b'valid')) |
| 525 | |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 526 | |
Antoine Pitrou | 83d6a87 | 2008-07-25 21:45:08 +0000 | [diff] [blame] | 527 | if __name__ == '__main__': |
Serhiy Storchaka | 880254e | 2013-07-17 13:23:45 +0300 | [diff] [blame] | 528 | unittest.main() |