blob: d93b3ad048be228c5571087c59ef61ebe7cb771c [file] [log] [blame]
Victor Stinner9acc6a02017-04-03 18:09:55 +02001from test.support import verbose, is_android, check_warnings
Antoine Pitrou83d6a872008-07-25 21:45:08 +00002import unittest
Martin v. Löwis88ad12a2001-04-13 08:09:50 +00003import locale
Guido van Rossumfc349862001-04-15 13:15:56 +00004import sys
Antoine Pitrou83d6a872008-07-25 21:45:08 +00005import codecs
Garvit Khatri1cf93a72017-03-28 23:43:38 +08006import warnings
Martin v. Löwis88ad12a2001-04-13 08:09:50 +00007
Antoine Pitrou83d6a872008-07-25 21:45:08 +00008class BaseLocalizedTest(unittest.TestCase):
9 #
10 # Base class for tests using a real locale
11 #
Martin v. Löwis88ad12a2001-04-13 08:09:50 +000012
Serhiy Storchaka880254e2013-07-17 13:23:45 +030013 @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öwis88ad12a2001-04-13 08:09:50 +000042
Serhiy Storchaka880254e2013-07-17 13:23:45 +030043 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 Wouters477c8d52006-05-27 19:21:47 +000049
Thomas Wouters477c8d52006-05-27 19:21:47 +000050
Antoine Pitrou83d6a872008-07-25 21:45:08 +000051class BaseCookedTest(unittest.TestCase):
52 #
53 # Base class for tests using cooked localeconv() values
54 #
Georg Brandl3dbca812008-07-23 16:10:53 +000055
Antoine Pitrou83d6a872008-07-25 21:45:08 +000056 def setUp(self):
57 locale._override_localeconv = self.cooked_values
58
59 def tearDown(self):
60 locale._override_localeconv = {}
61
62class 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
86class 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 Pitrou350370c2009-03-14 00:13:13 +0000111class 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 Pitrou83d6a872008-07-25 21:45:08 +0000137class 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 Stinner9acc6a02017-04-03 18:09:55 +0200147 with check_warnings(('', DeprecationWarning)):
148 self._test_formatfunc(format, value, out,
149 func=locale.format, **format_opts)
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000150
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
159class EnUSNumberFormatting(BaseFormattingTest):
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000160 # 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 Pitrou83d6a872008-07-25 21:45:08 +0000162
163 def setUp(self):
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000164 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 Pitrou13856ea2008-07-26 21:02:53 +0000174 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 Pitrou83d6a872008-07-25 21:45:08 +0000179
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 Pitrou350370c2009-03-14 00:13:13 +0000185 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 Pitrou83d6a872008-07-25 21:45:08 +0000191 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 Khatri1cf93a72017-03-28 23:43:38 +0800202 def test_format_deprecation(self):
203 with self.assertWarns(DeprecationWarning):
204 locale.format("%-10.f", 4200, grouping=True)
205
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000206 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 Pitrou13856ea2008-07-26 21:02:53 +0000215 if self.sep:
216 self._test_format_string("--> %10.2f", 4200, grouping=1,
217 out='--> ' + ('4%s200.00' % self.sep).rjust(10))
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000218 # Asterisk formats
219 self._test_format_string("%10.*f", (2, 1000), grouping=0,
220 out='1000.00'.rjust(10))
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000221 if self.sep:
222 self._test_format_string("%*.*f", (10, 2, 1000), grouping=1,
223 out=('1%s000.00' % self.sep).rjust(10))
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000224 # Test more-in-one
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000225 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 Pitrou83d6a872008-07-25 21:45:08 +0000230
231
R. David Murraye59482e2009-04-01 03:42:00 +0000232class TestFormatPatternArg(unittest.TestCase):
233 # Test handling of pattern argument of format
234
235 def test_onlyOnePattern(self):
Victor Stinner9acc6a02017-04-03 18:09:55 +0200236 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 Murrayad78d152010-04-27 02:45:53 +0000245
246
247class 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 Murraye59482e2009-04-01 03:42:00 +0000263
264
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000265class 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
275class 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
289class 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 Pitrou350370c2009-03-14 00:13:13 +0000299class 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 Pitrou6a448d42009-10-19 19:43:09 +0000342class 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 Storchakaf7eae0a2017-06-28 08:30:06 +0300349 # embedded null character
350 self.assertRaises(ValueError, locale.strcoll, 'a\0', 'a')
351 self.assertRaises(ValueError, locale.strcoll, 'a', 'a\0')
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000352
353 def test_strxfrm(self):
354 self.assertLess(locale.strxfrm('a'), locale.strxfrm('b'))
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +0300355 # embedded null character
356 self.assertRaises(ValueError, locale.strxfrm, 'a\0')
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000357
358
359class 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 Pitrou6a448d42009-10-19 19:43:09 +0000365 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 Gaye5bccb0e2016-12-19 10:46:14 +0100368 if enc != 'iso8859-1' and (sys.platform == 'darwin' or is_android or
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000369 sys.platform.startswith('freebsd')):
370 raise unittest.SkipTest('wcscoll/wcsxfrm have known bugs')
Hirokazu Yamamotob3b390d2010-09-23 15:20:15 +0000371 BaseLocalizedTest.setUp(self)
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000372
Victor Stinner5de85a12017-04-04 10:35:15 +0200373 @unittest.skipIf(sys.platform.startswith('aix'),
374 'bpo-29972: broken test on AIX')
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000375 def test_strcoll_with_diacritic(self):
376 self.assertLess(locale.strcoll('à', 'b'), 0)
377
Victor Stinner5de85a12017-04-04 10:35:15 +0200378 @unittest.skipIf(sys.platform.startswith('aix'),
379 'bpo-29972: broken test on AIX')
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000380 def test_strxfrm_with_diacritic(self):
381 self.assertLess(locale.strxfrm('à'), locale.strxfrm('b'))
382
383
Serhiy Storchaka16f02d22013-12-19 21:21:40 +0200384class 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 Storchaka8c4f57d2013-12-27 00:56:53 +0200403 self.check('en.iso88591', 'en_US.ISO8859-1')
Serhiy Storchaka16f02d22013-12-19 21:21:40 +0200404 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 Storchaka8c4f57d2013-12-27 00:56:53 +0200412 self.check('en_US.88591', 'en_US.ISO8859-1')
413 self.check('en_US.885915', 'en_US.ISO8859-15')
Serhiy Storchaka16f02d22013-12-19 21:21:40 +0200414 self.check('english', 'en_EN.ISO8859-1')
Serhiy Storchaka8c4f57d2013-12-27 00:56:53 +0200415 self.check('english_uk.ascii', 'en_GB.ISO8859-1')
Serhiy Storchaka16f02d22013-12-19 21:21:40 +0200416
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 Storchaka8c4f57d2013-12-27 00:56:53 +0200435 self.check('de_DE.utf8@euro', 'de_DE.UTF-8')
Serhiy Storchaka16f02d22013-12-19 21:21:40 +0200436
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 Storchaka8c4f57d2013-12-27 00:56:53 +0200440 self.check('sr_RS.UTF-8@latn', 'sr_RS.UTF-8@latin')
Serhiy Storchaka16f02d22013-12-19 21:21:40 +0200441
442 def test_valencia_modifier(self):
443 self.check('ca_ES.UTF-8@valencia', 'ca_ES.UTF-8@valencia')
Serhiy Storchaka4601df52014-10-02 11:36:12 +0300444 self.check('ca_ES@valencia', 'ca_ES.ISO8859-15@valencia')
Serhiy Storchaka16f02d22013-12-19 21:21:40 +0200445 self.check('ca@valencia', 'ca_ES.ISO8859-1@valencia')
446
Serhiy Storchaka5eb01532013-12-26 21:20:59 +0200447 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 Storchaka8c4f57d2013-12-27 00:56:53 +0200461 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 Petersondf828082017-03-19 23:49:43 -0700468 self.check('zh_tw.euctw', 'zh_TW.eucTW')
Serhiy Storchaka8c4f57d2013-12-27 00:56:53 +0200469
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 Storchaka16f02d22013-12-19 21:21:40 +0200494
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000495class 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 Pitrou6a448d42009-10-19 19:43:09 +0000503 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 Pitrou83d6a872008-07-25 21:45:08 +0000507
Amaury Forgeot d'Arc64f3ca42009-12-01 21:59:18 +0000508 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 Pitrou0e3c5a82011-07-24 02:40:25 +0200519 def test_getsetlocale_issue1813(self):
520 # Issue #1813: setting and getting the locale under a Turkish locale
Antoine Pitroud05066d2011-07-26 23:55:33 +0200521 oldlocale = locale.setlocale(locale.LC_CTYPE)
Antoine Pitrou0e3c5a82011-07-24 02:40:25 +0200522 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 Pitroud05066d2011-07-26 23:55:33 +0200528 loc = locale.getlocale(locale.LC_CTYPE)
Antoine Pitrou83d21932011-07-26 14:45:22 +0200529 if verbose:
Serhiy Storchaka09545852015-02-18 08:04:37 +0200530 print('testing with %a' % (loc,), end=' ', flush=True)
Antoine Pitrou0e3c5a82011-07-24 02:40:25 +0200531 locale.setlocale(locale.LC_CTYPE, loc)
Antoine Pitroud05066d2011-07-26 23:55:33 +0200532 self.assertEqual(loc, locale.getlocale(locale.LC_CTYPE))
Antoine Pitrou0e3c5a82011-07-24 02:40:25 +0200533
Petri Lehtinen3c85fe02011-11-04 21:35:07 +0200534 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 Pitrou83d6a872008-07-25 21:45:08 +0000542
Antoine Pitroub64bca92014-10-23 22:52:31 +0200543class 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
555class 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
570class 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
582class 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 Pitrou83d6a872008-07-25 21:45:08 +0000597if __name__ == '__main__':
Serhiy Storchaka880254e2013-07-17 13:23:45 +0300598 unittest.main()