blob: 11bf254e9774b14c1ff0a3f8b2457d05f3c5bb7b [file] [log] [blame]
Victor Stinner8fbbdf02018-06-22 19:25:44 +02001import codecs
Martin v. Löwis88ad12a2001-04-13 08:09:50 +00002import locale
Guido van Rossumfc349862001-04-15 13:15:56 +00003import sys
Victor Stinner8fbbdf02018-06-22 19:25:44 +02004import unittest
Garvit Khatri1cf93a72017-03-28 23:43:38 +08005import warnings
Victor Stinner8fbbdf02018-06-22 19:25:44 +02006from test import support
7
Martin v. Löwis88ad12a2001-04-13 08:09:50 +00008
Antoine Pitrou83d6a872008-07-25 21:45:08 +00009class BaseLocalizedTest(unittest.TestCase):
10 #
11 # Base class for tests using a real locale
12 #
Martin v. Löwis88ad12a2001-04-13 08:09:50 +000013
Serhiy Storchaka880254e2013-07-17 13:23:45 +030014 @classmethod
15 def setUpClass(cls):
Victor Stinner8fbbdf02018-06-22 19:25:44 +020016 if support.MACOS:
Serhiy Storchaka880254e2013-07-17 13:23:45 +030017 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öwis88ad12a2001-04-13 08:09:50 +000043
Serhiy Storchaka880254e2013-07-17 13:23:45 +030044 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 Stinner8fbbdf02018-06-22 19:25:44 +020048 if support.verbose:
Serhiy Storchaka880254e2013-07-17 13:23:45 +030049 print("testing with %r..." % self.enUS_locale, end=' ', flush=True)
Thomas Wouters477c8d52006-05-27 19:21:47 +000050
Thomas Wouters477c8d52006-05-27 19:21:47 +000051
Antoine Pitrou83d6a872008-07-25 21:45:08 +000052class BaseCookedTest(unittest.TestCase):
53 #
54 # Base class for tests using cooked localeconv() values
55 #
Georg Brandl3dbca812008-07-23 16:10:53 +000056
Antoine Pitrou83d6a872008-07-25 21:45:08 +000057 def setUp(self):
58 locale._override_localeconv = self.cooked_values
59
60 def tearDown(self):
61 locale._override_localeconv = {}
62
63class 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
87class 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 Pitrou350370c2009-03-14 00:13:13 +0000112class 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 Pitrou83d6a872008-07-25 21:45:08 +0000138class 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 Stinner8fbbdf02018-06-22 19:25:44 +0200148 with support.check_warnings(('', DeprecationWarning)):
Victor Stinner9acc6a02017-04-03 18:09:55 +0200149 self._test_formatfunc(format, value, out,
150 func=locale.format, **format_opts)
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000151
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
160class EnUSNumberFormatting(BaseFormattingTest):
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000161 # 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 Pitrou83d6a872008-07-25 21:45:08 +0000163
164 def setUp(self):
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000165 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 Pitrou13856ea2008-07-26 21:02:53 +0000175 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 Pitrou83d6a872008-07-25 21:45:08 +0000180
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 Pitrou350370c2009-03-14 00:13:13 +0000186 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 Pitrou83d6a872008-07-25 21:45:08 +0000192 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 Khatri1cf93a72017-03-28 23:43:38 +0800203 def test_format_deprecation(self):
204 with self.assertWarns(DeprecationWarning):
205 locale.format("%-10.f", 4200, grouping=True)
206
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000207 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 Pitrou13856ea2008-07-26 21:02:53 +0000216 if self.sep:
217 self._test_format_string("--> %10.2f", 4200, grouping=1,
218 out='--> ' + ('4%s200.00' % self.sep).rjust(10))
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000219 # Asterisk formats
220 self._test_format_string("%10.*f", (2, 1000), grouping=0,
221 out='1000.00'.rjust(10))
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000222 if self.sep:
223 self._test_format_string("%*.*f", (10, 2, 1000), grouping=1,
224 out=('1%s000.00' % self.sep).rjust(10))
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000225 # Test more-in-one
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000226 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 Pitrou83d6a872008-07-25 21:45:08 +0000231
232
R. David Murraye59482e2009-04-01 03:42:00 +0000233class TestFormatPatternArg(unittest.TestCase):
234 # Test handling of pattern argument of format
235
236 def test_onlyOnePattern(self):
Victor Stinner8fbbdf02018-06-22 19:25:44 +0200237 with support.check_warnings(('', DeprecationWarning)):
Victor Stinner9acc6a02017-04-03 18:09:55 +0200238 # 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 Murrayad78d152010-04-27 02:45:53 +0000246
247
248class 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 Murraye59482e2009-04-01 03:42:00 +0000264
265
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000266class 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
276class 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
290class 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 Pitrou350370c2009-03-14 00:13:13 +0000300class 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 Pitrou6a448d42009-10-19 19:43:09 +0000343class 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 Storchakaf7eae0a2017-06-28 08:30:06 +0300350 # embedded null character
351 self.assertRaises(ValueError, locale.strcoll, 'a\0', 'a')
352 self.assertRaises(ValueError, locale.strcoll, 'a', 'a\0')
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000353
354 def test_strxfrm(self):
355 self.assertLess(locale.strxfrm('a'), locale.strxfrm('b'))
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300356 # embedded null character
357 self.assertRaises(ValueError, locale.strxfrm, 'a\0')
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000358
359
360class 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 Pitrou6a448d42009-10-19 19:43:09 +0000366 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 Stinner8fbbdf02018-06-22 19:25:44 +0200369 if enc != 'iso8859-1' and (support.MACOS or support.ANDROID or
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000370 sys.platform.startswith('freebsd')):
371 raise unittest.SkipTest('wcscoll/wcsxfrm have known bugs')
Hirokazu Yamamotob3b390d2010-09-23 15:20:15 +0000372 BaseLocalizedTest.setUp(self)
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000373
Victor Stinner5de85a12017-04-04 10:35:15 +0200374 @unittest.skipIf(sys.platform.startswith('aix'),
375 'bpo-29972: broken test on AIX')
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000376 def test_strcoll_with_diacritic(self):
377 self.assertLess(locale.strcoll('à', 'b'), 0)
378
Victor Stinner5de85a12017-04-04 10:35:15 +0200379 @unittest.skipIf(sys.platform.startswith('aix'),
380 'bpo-29972: broken test on AIX')
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000381 def test_strxfrm_with_diacritic(self):
382 self.assertLess(locale.strxfrm('à'), locale.strxfrm('b'))
383
384
Serhiy Storchaka16f02d22013-12-19 21:21:40 +0200385class 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 Storchaka8c4f57d2013-12-27 00:56:53 +0200404 self.check('en.iso88591', 'en_US.ISO8859-1')
Serhiy Storchaka16f02d22013-12-19 21:21:40 +0200405 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 Storchaka8c4f57d2013-12-27 00:56:53 +0200413 self.check('en_US.88591', 'en_US.ISO8859-1')
414 self.check('en_US.885915', 'en_US.ISO8859-15')
Serhiy Storchaka16f02d22013-12-19 21:21:40 +0200415 self.check('english', 'en_EN.ISO8859-1')
Serhiy Storchaka8c4f57d2013-12-27 00:56:53 +0200416 self.check('english_uk.ascii', 'en_GB.ISO8859-1')
Serhiy Storchaka16f02d22013-12-19 21:21:40 +0200417
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 Storchaka8c4f57d2013-12-27 00:56:53 +0200436 self.check('de_DE.utf8@euro', 'de_DE.UTF-8')
Serhiy Storchaka16f02d22013-12-19 21:21:40 +0200437
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 Storchaka8c4f57d2013-12-27 00:56:53 +0200441 self.check('sr_RS.UTF-8@latn', 'sr_RS.UTF-8@latin')
Serhiy Storchaka16f02d22013-12-19 21:21:40 +0200442
443 def test_valencia_modifier(self):
444 self.check('ca_ES.UTF-8@valencia', 'ca_ES.UTF-8@valencia')
Serhiy Storchakacedc9b72018-05-06 08:46:15 +0300445 self.check('ca_ES@valencia', 'ca_ES.UTF-8@valencia')
Serhiy Storchaka16f02d22013-12-19 21:21:40 +0200446 self.check('ca@valencia', 'ca_ES.ISO8859-1@valencia')
447
Serhiy Storchaka5eb01532013-12-26 21:20:59 +0200448 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 Storchaka8c4f57d2013-12-27 00:56:53 +0200462 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 Petersondf828082017-03-19 23:49:43 -0700469 self.check('zh_tw.euctw', 'zh_TW.eucTW')
Serhiy Storchaka8c4f57d2013-12-27 00:56:53 +0200470
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 Storchaka16f02d22013-12-19 21:21:40 +0200495
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000496class 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 Pitrou6a448d42009-10-19 19:43:09 +0000504 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 Pitrou83d6a872008-07-25 21:45:08 +0000508
Amaury Forgeot d'Arc64f3ca42009-12-01 21:59:18 +0000509 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 Pitrou0e3c5a82011-07-24 02:40:25 +0200520 def test_getsetlocale_issue1813(self):
521 # Issue #1813: setting and getting the locale under a Turkish locale
Antoine Pitroud05066d2011-07-26 23:55:33 +0200522 oldlocale = locale.setlocale(locale.LC_CTYPE)
Antoine Pitrou0e3c5a82011-07-24 02:40:25 +0200523 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 Pitroud05066d2011-07-26 23:55:33 +0200529 loc = locale.getlocale(locale.LC_CTYPE)
Victor Stinner8fbbdf02018-06-22 19:25:44 +0200530 if support.verbose:
Serhiy Storchaka09545852015-02-18 08:04:37 +0200531 print('testing with %a' % (loc,), end=' ', flush=True)
Antoine Pitrou0e3c5a82011-07-24 02:40:25 +0200532 locale.setlocale(locale.LC_CTYPE, loc)
Antoine Pitroud05066d2011-07-26 23:55:33 +0200533 self.assertEqual(loc, locale.getlocale(locale.LC_CTYPE))
Antoine Pitrou0e3c5a82011-07-24 02:40:25 +0200534
Petri Lehtinen3c85fe02011-11-04 21:35:07 +0200535 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 Pitrou83d6a872008-07-25 21:45:08 +0000543
Antoine Pitroub64bca92014-10-23 22:52:31 +0200544class 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
556class 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
571class 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
583class 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 Pitrou83d6a872008-07-25 21:45:08 +0000598if __name__ == '__main__':
Serhiy Storchaka880254e2013-07-17 13:23:45 +0300599 unittest.main()