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