blob: 06d286622a52928aa2b0c87d4eee51dc3e32dbc5 [file] [log] [blame]
Xavier de Gaye5bccb0e2016-12-19 10:46:14 +01001from test.support import verbose, is_android
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):
147 self._test_formatfunc(format, value, out,
148 func=locale.format, **format_opts)
149
150 def _test_format_string(self, format, value, out, **format_opts):
151 self._test_formatfunc(format, value, out,
152 func=locale.format_string, **format_opts)
153
154 def _test_currency(self, value, out, **format_opts):
155 self.assertEqual(locale.currency(value, **format_opts), out)
156
157
158class EnUSNumberFormatting(BaseFormattingTest):
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000159 # XXX there is a grouping + padding bug when the thousands separator
160 # is empty but the grouping array contains values (e.g. Solaris 10)
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000161
162 def setUp(self):
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000163 self.sep = locale.localeconv()['thousands_sep']
164
165 def test_grouping(self):
166 self._test_format("%f", 1024, grouping=1, out='1%s024.000000' % self.sep)
167 self._test_format("%f", 102, grouping=1, out='102.000000')
168 self._test_format("%f", -42, grouping=1, out='-42.000000')
169 self._test_format("%+f", -42, grouping=1, out='-42.000000')
170
171 def test_grouping_and_padding(self):
172 self._test_format("%20.f", -42, grouping=1, out='-42'.rjust(20))
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000173 if self.sep:
174 self._test_format("%+10.f", -4200, grouping=1,
175 out=('-4%s200' % self.sep).rjust(10))
176 self._test_format("%-10.f", -4200, grouping=1,
177 out=('-4%s200' % self.sep).ljust(10))
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000178
179 def test_integer_grouping(self):
180 self._test_format("%d", 4200, grouping=True, out='4%s200' % self.sep)
181 self._test_format("%+d", 4200, grouping=True, out='+4%s200' % self.sep)
182 self._test_format("%+d", -4200, grouping=True, out='-4%s200' % self.sep)
183
Antoine Pitrou350370c2009-03-14 00:13:13 +0000184 def test_integer_grouping_and_padding(self):
185 self._test_format("%10d", 4200, grouping=True,
186 out=('4%s200' % self.sep).rjust(10))
187 self._test_format("%-10d", -4200, grouping=True,
188 out=('-4%s200' % self.sep).ljust(10))
189
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000190 def test_simple(self):
191 self._test_format("%f", 1024, grouping=0, out='1024.000000')
192 self._test_format("%f", 102, grouping=0, out='102.000000')
193 self._test_format("%f", -42, grouping=0, out='-42.000000')
194 self._test_format("%+f", -42, grouping=0, out='-42.000000')
195
196 def test_padding(self):
197 self._test_format("%20.f", -42, grouping=0, out='-42'.rjust(20))
198 self._test_format("%+10.f", -4200, grouping=0, out='-4200'.rjust(10))
199 self._test_format("%-10.f", 4200, grouping=0, out='4200'.ljust(10))
200
Garvit Khatri1cf93a72017-03-28 23:43:38 +0800201 def test_format_deprecation(self):
202 with self.assertWarns(DeprecationWarning):
203 locale.format("%-10.f", 4200, grouping=True)
204
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000205 def test_complex_formatting(self):
206 # Spaces in formatting string
207 self._test_format_string("One million is %i", 1000000, grouping=1,
208 out='One million is 1%s000%s000' % (self.sep, self.sep))
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 # Dots in formatting string
212 self._test_format_string(".%f.", 1000.0, out='.1000.000000.')
213 # Padding
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000214 if self.sep:
215 self._test_format_string("--> %10.2f", 4200, grouping=1,
216 out='--> ' + ('4%s200.00' % self.sep).rjust(10))
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000217 # Asterisk formats
218 self._test_format_string("%10.*f", (2, 1000), grouping=0,
219 out='1000.00'.rjust(10))
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000220 if self.sep:
221 self._test_format_string("%*.*f", (10, 2, 1000), grouping=1,
222 out=('1%s000.00' % self.sep).rjust(10))
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000223 # Test more-in-one
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000224 if self.sep:
225 self._test_format_string("int %i float %.2f str %s",
226 (1000, 1000.0, 'str'), grouping=1,
227 out='int 1%s000 float 1%s000.00 str str' %
228 (self.sep, self.sep))
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000229
230
R. David Murraye59482e2009-04-01 03:42:00 +0000231class TestFormatPatternArg(unittest.TestCase):
232 # Test handling of pattern argument of format
233
234 def test_onlyOnePattern(self):
235 # Issue 2522: accept exactly one % pattern, and no extra chars.
236 self.assertRaises(ValueError, locale.format, "%f\n", 'foo')
237 self.assertRaises(ValueError, locale.format, "%f\r", 'foo')
238 self.assertRaises(ValueError, locale.format, "%f\r\n", 'foo')
239 self.assertRaises(ValueError, locale.format, " %f", 'foo')
240 self.assertRaises(ValueError, locale.format, "%fg", 'foo')
241 self.assertRaises(ValueError, locale.format, "%^g", 'foo')
R. David Murrayad78d152010-04-27 02:45:53 +0000242 self.assertRaises(ValueError, locale.format, "%f%%", 'foo')
243
244
245class TestLocaleFormatString(unittest.TestCase):
246 """General tests on locale.format_string"""
247
248 def test_percent_escape(self):
249 self.assertEqual(locale.format_string('%f%%', 1.0), '%f%%' % 1.0)
250 self.assertEqual(locale.format_string('%d %f%%d', (1, 1.0)),
251 '%d %f%%d' % (1, 1.0))
252 self.assertEqual(locale.format_string('%(foo)s %%d', {'foo': 'bar'}),
253 ('%(foo)s %%d' % {'foo': 'bar'}))
254
255 def test_mapping(self):
256 self.assertEqual(locale.format_string('%(foo)s bing.', {'foo': 'bar'}),
257 ('%(foo)s bing.' % {'foo': 'bar'}))
258 self.assertEqual(locale.format_string('%(foo)s', {'foo': 'bar'}),
259 ('%(foo)s' % {'foo': 'bar'}))
260
R. David Murraye59482e2009-04-01 03:42:00 +0000261
262
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000263class TestNumberFormatting(BaseLocalizedTest, EnUSNumberFormatting):
264 # Test number formatting with a real English locale.
265
266 locale_type = locale.LC_NUMERIC
267
268 def setUp(self):
269 BaseLocalizedTest.setUp(self)
270 EnUSNumberFormatting.setUp(self)
271
272
273class TestEnUSNumberFormatting(EnUSCookedTest, EnUSNumberFormatting):
274 # Test number formatting with a cooked "en_US" locale.
275
276 def setUp(self):
277 EnUSCookedTest.setUp(self)
278 EnUSNumberFormatting.setUp(self)
279
280 def test_currency(self):
281 self._test_currency(50000, "$50000.00")
282 self._test_currency(50000, "$50,000.00", grouping=True)
283 self._test_currency(50000, "USD 50,000.00",
284 grouping=True, international=True)
285
286
287class TestCNumberFormatting(CCookedTest, BaseFormattingTest):
288 # Test number formatting with a cooked "C" locale.
289
290 def test_grouping(self):
291 self._test_format("%.2f", 12345.67, grouping=True, out='12345.67')
292
293 def test_grouping_and_padding(self):
294 self._test_format("%9.2f", 12345.67, grouping=True, out=' 12345.67')
295
296
Antoine Pitrou350370c2009-03-14 00:13:13 +0000297class TestFrFRNumberFormatting(FrFRCookedTest, BaseFormattingTest):
298 # Test number formatting with a cooked "fr_FR" locale.
299
300 def test_decimal_point(self):
301 self._test_format("%.2f", 12345.67, out='12345,67')
302
303 def test_grouping(self):
304 self._test_format("%.2f", 345.67, grouping=True, out='345,67')
305 self._test_format("%.2f", 12345.67, grouping=True, out='12 345,67')
306
307 def test_grouping_and_padding(self):
308 self._test_format("%6.2f", 345.67, grouping=True, out='345,67')
309 self._test_format("%7.2f", 345.67, grouping=True, out=' 345,67')
310 self._test_format("%8.2f", 12345.67, grouping=True, out='12 345,67')
311 self._test_format("%9.2f", 12345.67, grouping=True, out='12 345,67')
312 self._test_format("%10.2f", 12345.67, grouping=True, out=' 12 345,67')
313 self._test_format("%-6.2f", 345.67, grouping=True, out='345,67')
314 self._test_format("%-7.2f", 345.67, grouping=True, out='345,67 ')
315 self._test_format("%-8.2f", 12345.67, grouping=True, out='12 345,67')
316 self._test_format("%-9.2f", 12345.67, grouping=True, out='12 345,67')
317 self._test_format("%-10.2f", 12345.67, grouping=True, out='12 345,67 ')
318
319 def test_integer_grouping(self):
320 self._test_format("%d", 200, grouping=True, out='200')
321 self._test_format("%d", 4200, grouping=True, out='4 200')
322
323 def test_integer_grouping_and_padding(self):
324 self._test_format("%4d", 4200, grouping=True, out='4 200')
325 self._test_format("%5d", 4200, grouping=True, out='4 200')
326 self._test_format("%10d", 4200, grouping=True, out='4 200'.rjust(10))
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'.ljust(10))
330
331 def test_currency(self):
332 euro = '\u20ac'
333 self._test_currency(50000, "50000,00 " + euro)
334 self._test_currency(50000, "50 000,00 " + euro, grouping=True)
335 # XXX is the trailing space a bug?
336 self._test_currency(50000, "50 000,00 EUR ",
337 grouping=True, international=True)
338
339
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000340class TestCollation(unittest.TestCase):
341 # Test string collation functions
342
343 def test_strcoll(self):
344 self.assertLess(locale.strcoll('a', 'b'), 0)
345 self.assertEqual(locale.strcoll('a', 'a'), 0)
346 self.assertGreater(locale.strcoll('b', 'a'), 0)
347
348 def test_strxfrm(self):
349 self.assertLess(locale.strxfrm('a'), locale.strxfrm('b'))
350
351
352class TestEnUSCollation(BaseLocalizedTest, TestCollation):
353 # Test string collation functions with a real English locale
354
355 locale_type = locale.LC_ALL
356
357 def setUp(self):
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000358 enc = codecs.lookup(locale.getpreferredencoding(False) or 'ascii').name
359 if enc not in ('utf-8', 'iso8859-1', 'cp1252'):
360 raise unittest.SkipTest('encoding not suitable')
Xavier de Gaye5bccb0e2016-12-19 10:46:14 +0100361 if enc != 'iso8859-1' and (sys.platform == 'darwin' or is_android or
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000362 sys.platform.startswith('freebsd')):
363 raise unittest.SkipTest('wcscoll/wcsxfrm have known bugs')
Hirokazu Yamamotob3b390d2010-09-23 15:20:15 +0000364 BaseLocalizedTest.setUp(self)
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000365
366 def test_strcoll_with_diacritic(self):
367 self.assertLess(locale.strcoll('à', 'b'), 0)
368
369 def test_strxfrm_with_diacritic(self):
370 self.assertLess(locale.strxfrm('à'), locale.strxfrm('b'))
371
372
Serhiy Storchaka16f02d22013-12-19 21:21:40 +0200373class NormalizeTest(unittest.TestCase):
374 def check(self, localename, expected):
375 self.assertEqual(locale.normalize(localename), expected, msg=localename)
376
377 def test_locale_alias(self):
378 for localename, alias in locale.locale_alias.items():
379 with self.subTest(locale=(localename, alias)):
380 self.check(localename, alias)
381
382 def test_empty(self):
383 self.check('', '')
384
385 def test_c(self):
386 self.check('c', 'C')
387 self.check('posix', 'C')
388
389 def test_english(self):
390 self.check('en', 'en_US.ISO8859-1')
391 self.check('EN', 'en_US.ISO8859-1')
Serhiy Storchaka8c4f57d2013-12-27 00:56:53 +0200392 self.check('en.iso88591', 'en_US.ISO8859-1')
Serhiy Storchaka16f02d22013-12-19 21:21:40 +0200393 self.check('en_US', 'en_US.ISO8859-1')
394 self.check('en_us', 'en_US.ISO8859-1')
395 self.check('en_GB', 'en_GB.ISO8859-1')
396 self.check('en_US.UTF-8', 'en_US.UTF-8')
397 self.check('en_US.utf8', 'en_US.UTF-8')
398 self.check('en_US:UTF-8', 'en_US.UTF-8')
399 self.check('en_US.ISO8859-1', 'en_US.ISO8859-1')
400 self.check('en_US.US-ASCII', 'en_US.ISO8859-1')
Serhiy Storchaka8c4f57d2013-12-27 00:56:53 +0200401 self.check('en_US.88591', 'en_US.ISO8859-1')
402 self.check('en_US.885915', 'en_US.ISO8859-15')
Serhiy Storchaka16f02d22013-12-19 21:21:40 +0200403 self.check('english', 'en_EN.ISO8859-1')
Serhiy Storchaka8c4f57d2013-12-27 00:56:53 +0200404 self.check('english_uk.ascii', 'en_GB.ISO8859-1')
Serhiy Storchaka16f02d22013-12-19 21:21:40 +0200405
406 def test_hyphenated_encoding(self):
407 self.check('az_AZ.iso88599e', 'az_AZ.ISO8859-9E')
408 self.check('az_AZ.ISO8859-9E', 'az_AZ.ISO8859-9E')
409 self.check('tt_RU.koi8c', 'tt_RU.KOI8-C')
410 self.check('tt_RU.KOI8-C', 'tt_RU.KOI8-C')
411 self.check('lo_LA.cp1133', 'lo_LA.IBM-CP1133')
412 self.check('lo_LA.ibmcp1133', 'lo_LA.IBM-CP1133')
413 self.check('lo_LA.IBM-CP1133', 'lo_LA.IBM-CP1133')
414 self.check('uk_ua.microsoftcp1251', 'uk_UA.CP1251')
415 self.check('uk_ua.microsoft-cp1251', 'uk_UA.CP1251')
416 self.check('ka_ge.georgianacademy', 'ka_GE.GEORGIAN-ACADEMY')
417 self.check('ka_GE.GEORGIAN-ACADEMY', 'ka_GE.GEORGIAN-ACADEMY')
418 self.check('cs_CZ.iso88592', 'cs_CZ.ISO8859-2')
419 self.check('cs_CZ.ISO8859-2', 'cs_CZ.ISO8859-2')
420
421 def test_euro_modifier(self):
422 self.check('de_DE@euro', 'de_DE.ISO8859-15')
423 self.check('en_US.ISO8859-15@euro', 'en_US.ISO8859-15')
Serhiy Storchaka8c4f57d2013-12-27 00:56:53 +0200424 self.check('de_DE.utf8@euro', 'de_DE.UTF-8')
Serhiy Storchaka16f02d22013-12-19 21:21:40 +0200425
426 def test_latin_modifier(self):
427 self.check('be_BY.UTF-8@latin', 'be_BY.UTF-8@latin')
428 self.check('sr_RS.UTF-8@latin', 'sr_RS.UTF-8@latin')
Serhiy Storchaka8c4f57d2013-12-27 00:56:53 +0200429 self.check('sr_RS.UTF-8@latn', 'sr_RS.UTF-8@latin')
Serhiy Storchaka16f02d22013-12-19 21:21:40 +0200430
431 def test_valencia_modifier(self):
432 self.check('ca_ES.UTF-8@valencia', 'ca_ES.UTF-8@valencia')
Serhiy Storchaka4601df52014-10-02 11:36:12 +0300433 self.check('ca_ES@valencia', 'ca_ES.ISO8859-15@valencia')
Serhiy Storchaka16f02d22013-12-19 21:21:40 +0200434 self.check('ca@valencia', 'ca_ES.ISO8859-1@valencia')
435
Serhiy Storchaka5eb01532013-12-26 21:20:59 +0200436 def test_devanagari_modifier(self):
437 self.check('ks_IN.UTF-8@devanagari', 'ks_IN.UTF-8@devanagari')
438 self.check('ks_IN@devanagari', 'ks_IN.UTF-8@devanagari')
439 self.check('ks@devanagari', 'ks_IN.UTF-8@devanagari')
440 self.check('ks_IN.UTF-8', 'ks_IN.UTF-8')
441 self.check('ks_IN', 'ks_IN.UTF-8')
442 self.check('ks', 'ks_IN.UTF-8')
443 self.check('sd_IN.UTF-8@devanagari', 'sd_IN.UTF-8@devanagari')
444 self.check('sd_IN@devanagari', 'sd_IN.UTF-8@devanagari')
445 self.check('sd@devanagari', 'sd_IN.UTF-8@devanagari')
446 self.check('sd_IN.UTF-8', 'sd_IN.UTF-8')
447 self.check('sd_IN', 'sd_IN.UTF-8')
448 self.check('sd', 'sd_IN.UTF-8')
449
Serhiy Storchaka8c4f57d2013-12-27 00:56:53 +0200450 def test_euc_encoding(self):
451 self.check('ja_jp.euc', 'ja_JP.eucJP')
452 self.check('ja_jp.eucjp', 'ja_JP.eucJP')
453 self.check('ko_kr.euc', 'ko_KR.eucKR')
454 self.check('ko_kr.euckr', 'ko_KR.eucKR')
455 self.check('zh_cn.euc', 'zh_CN.eucCN')
456 self.check('zh_tw.euc', 'zh_TW.eucTW')
Benjamin Petersondf828082017-03-19 23:49:43 -0700457 self.check('zh_tw.euctw', 'zh_TW.eucTW')
Serhiy Storchaka8c4f57d2013-12-27 00:56:53 +0200458
459 def test_japanese(self):
460 self.check('ja', 'ja_JP.eucJP')
461 self.check('ja.jis', 'ja_JP.JIS7')
462 self.check('ja.sjis', 'ja_JP.SJIS')
463 self.check('ja_jp', 'ja_JP.eucJP')
464 self.check('ja_jp.ajec', 'ja_JP.eucJP')
465 self.check('ja_jp.euc', 'ja_JP.eucJP')
466 self.check('ja_jp.eucjp', 'ja_JP.eucJP')
467 self.check('ja_jp.iso-2022-jp', 'ja_JP.JIS7')
468 self.check('ja_jp.iso2022jp', 'ja_JP.JIS7')
469 self.check('ja_jp.jis', 'ja_JP.JIS7')
470 self.check('ja_jp.jis7', 'ja_JP.JIS7')
471 self.check('ja_jp.mscode', 'ja_JP.SJIS')
472 self.check('ja_jp.pck', 'ja_JP.SJIS')
473 self.check('ja_jp.sjis', 'ja_JP.SJIS')
474 self.check('ja_jp.ujis', 'ja_JP.eucJP')
475 self.check('ja_jp.utf8', 'ja_JP.UTF-8')
476 self.check('japan', 'ja_JP.eucJP')
477 self.check('japanese', 'ja_JP.eucJP')
478 self.check('japanese-euc', 'ja_JP.eucJP')
479 self.check('japanese.euc', 'ja_JP.eucJP')
480 self.check('japanese.sjis', 'ja_JP.SJIS')
481 self.check('jp_jp', 'ja_JP.eucJP')
482
Serhiy Storchaka16f02d22013-12-19 21:21:40 +0200483
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000484class TestMiscellaneous(unittest.TestCase):
485 def test_getpreferredencoding(self):
486 # Invoke getpreferredencoding to make sure it does not cause exceptions.
487 enc = locale.getpreferredencoding()
488 if enc:
489 # If encoding non-empty, make sure it is valid
490 codecs.lookup(enc)
491
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000492 def test_strcoll_3303(self):
493 # test crasher from bug #3303
494 self.assertRaises(TypeError, locale.strcoll, "a", None)
495 self.assertRaises(TypeError, locale.strcoll, b"a", None)
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000496
Amaury Forgeot d'Arc64f3ca42009-12-01 21:59:18 +0000497 def test_setlocale_category(self):
498 locale.setlocale(locale.LC_ALL)
499 locale.setlocale(locale.LC_TIME)
500 locale.setlocale(locale.LC_CTYPE)
501 locale.setlocale(locale.LC_COLLATE)
502 locale.setlocale(locale.LC_MONETARY)
503 locale.setlocale(locale.LC_NUMERIC)
504
505 # crasher from bug #7419
506 self.assertRaises(locale.Error, locale.setlocale, 12345)
507
Antoine Pitrou0e3c5a82011-07-24 02:40:25 +0200508 def test_getsetlocale_issue1813(self):
509 # Issue #1813: setting and getting the locale under a Turkish locale
Antoine Pitroud05066d2011-07-26 23:55:33 +0200510 oldlocale = locale.setlocale(locale.LC_CTYPE)
Antoine Pitrou0e3c5a82011-07-24 02:40:25 +0200511 self.addCleanup(locale.setlocale, locale.LC_CTYPE, oldlocale)
512 try:
513 locale.setlocale(locale.LC_CTYPE, 'tr_TR')
514 except locale.Error:
515 # Unsupported locale on this system
516 self.skipTest('test needs Turkish locale')
Antoine Pitroud05066d2011-07-26 23:55:33 +0200517 loc = locale.getlocale(locale.LC_CTYPE)
Antoine Pitrou83d21932011-07-26 14:45:22 +0200518 if verbose:
Serhiy Storchaka09545852015-02-18 08:04:37 +0200519 print('testing with %a' % (loc,), end=' ', flush=True)
Antoine Pitrou0e3c5a82011-07-24 02:40:25 +0200520 locale.setlocale(locale.LC_CTYPE, loc)
Antoine Pitroud05066d2011-07-26 23:55:33 +0200521 self.assertEqual(loc, locale.getlocale(locale.LC_CTYPE))
Antoine Pitrou0e3c5a82011-07-24 02:40:25 +0200522
Petri Lehtinen3c85fe02011-11-04 21:35:07 +0200523 def test_invalid_locale_format_in_localetuple(self):
524 with self.assertRaises(TypeError):
525 locale.setlocale(locale.LC_ALL, b'fi_FI')
526
527 def test_invalid_iterable_in_localetuple(self):
528 with self.assertRaises(TypeError):
529 locale.setlocale(locale.LC_ALL, (b'not', b'valid'))
530
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000531
Antoine Pitroub64bca92014-10-23 22:52:31 +0200532class BaseDelocalizeTest(BaseLocalizedTest):
533
534 def _test_delocalize(self, value, out):
535 self.assertEqual(locale.delocalize(value), out)
536
537 def _test_atof(self, value, out):
538 self.assertEqual(locale.atof(value), out)
539
540 def _test_atoi(self, value, out):
541 self.assertEqual(locale.atoi(value), out)
542
543
544class TestEnUSDelocalize(EnUSCookedTest, BaseDelocalizeTest):
545
546 def test_delocalize(self):
547 self._test_delocalize('50000.00', '50000.00')
548 self._test_delocalize('50,000.00', '50000.00')
549
550 def test_atof(self):
551 self._test_atof('50000.00', 50000.)
552 self._test_atof('50,000.00', 50000.)
553
554 def test_atoi(self):
555 self._test_atoi('50000', 50000)
556 self._test_atoi('50,000', 50000)
557
558
559class TestCDelocalizeTest(CCookedTest, BaseDelocalizeTest):
560
561 def test_delocalize(self):
562 self._test_delocalize('50000.00', '50000.00')
563
564 def test_atof(self):
565 self._test_atof('50000.00', 50000.)
566
567 def test_atoi(self):
568 self._test_atoi('50000', 50000)
569
570
571class TestfrFRDelocalizeTest(FrFRCookedTest, BaseDelocalizeTest):
572
573 def test_delocalize(self):
574 self._test_delocalize('50000,00', '50000.00')
575 self._test_delocalize('50 000,00', '50000.00')
576
577 def test_atof(self):
578 self._test_atof('50000,00', 50000.)
579 self._test_atof('50 000,00', 50000.)
580
581 def test_atoi(self):
582 self._test_atoi('50000', 50000)
583 self._test_atoi('50 000', 50000)
584
585
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000586if __name__ == '__main__':
Serhiy Storchaka880254e2013-07-17 13:23:45 +0300587 unittest.main()