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