blob: 7fdb6dafdd0030f0c12bc3a11145411c912a3e85 [file] [log] [blame]
Benjamin Petersone549ead2009-03-28 21:42:05 +00001from test.support import run_unittest, verbose
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
Martin v. Löwis88ad12a2001-04-13 08:09:50 +00006
Antoine Pitrou13856ea2008-07-26 21:02:53 +00007enUS_locale = None
8
9def get_enUS_locale():
10 global enUS_locale
Ronald Oussoren501aeff2010-04-18 15:02:38 +000011 if sys.platform == 'darwin':
12 import os
13 tlocs = ("en_US.UTF-8", "en_US.ISO8859-1", "en_US")
14 if int(os.uname()[2].split('.')[0]) < 10:
15 # The locale test work fine on OSX 10.6, I (ronaldoussoren)
16 # haven't had time yet to verify if tests work on OSX 10.5
17 # (10.4 is known to be bad)
18 raise unittest.SkipTest("Locale support on MacOSX is minimal")
19 elif sys.platform.startswith("win"):
Antoine Pitrou13856ea2008-07-26 21:02:53 +000020 tlocs = ("En", "English")
21 else:
Antoine Pitrou6a448d42009-10-19 19:43:09 +000022 tlocs = ("en_US.UTF-8", "en_US.ISO8859-1", "en_US.US-ASCII", "en_US")
Antoine Pitrou13856ea2008-07-26 21:02:53 +000023 oldlocale = locale.setlocale(locale.LC_NUMERIC)
24 for tloc in tlocs:
25 try:
26 locale.setlocale(locale.LC_NUMERIC, tloc)
27 except locale.Error:
28 continue
29 break
30 else:
Benjamin Petersone549ead2009-03-28 21:42:05 +000031 raise unittest.SkipTest(
Antoine Pitrou13856ea2008-07-26 21:02:53 +000032 "Test locale not supported (tried %s)" % (', '.join(tlocs)))
33 enUS_locale = tloc
34 locale.setlocale(locale.LC_NUMERIC, oldlocale)
35
36
Antoine Pitrou83d6a872008-07-25 21:45:08 +000037class BaseLocalizedTest(unittest.TestCase):
38 #
39 # Base class for tests using a real locale
40 #
Martin v. Löwis88ad12a2001-04-13 08:09:50 +000041
Antoine Pitrou83d6a872008-07-25 21:45:08 +000042 def setUp(self):
Antoine Pitrou83d6a872008-07-25 21:45:08 +000043 self.oldlocale = locale.setlocale(self.locale_type)
Antoine Pitrou13856ea2008-07-26 21:02:53 +000044 locale.setlocale(self.locale_type, enUS_locale)
Martin v. Löwis88ad12a2001-04-13 08:09:50 +000045 if verbose:
Antoine Pitrou13856ea2008-07-26 21:02:53 +000046 print("testing with \"%s\"..." % enUS_locale, end=' ')
Martin v. Löwis88ad12a2001-04-13 08:09:50 +000047
Antoine Pitrou83d6a872008-07-25 21:45:08 +000048 def tearDown(self):
49 locale.setlocale(self.locale_type, self.oldlocale)
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):
148 self._test_formatfunc(format, value, out,
149 func=locale.format, **format_opts)
150
151 def _test_format_string(self, format, value, out, **format_opts):
152 self._test_formatfunc(format, value, out,
153 func=locale.format_string, **format_opts)
154
155 def _test_currency(self, value, out, **format_opts):
156 self.assertEqual(locale.currency(value, **format_opts), out)
157
158
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
202 def test_complex_formatting(self):
203 # Spaces in formatting string
204 self._test_format_string("One million is %i", 1000000, grouping=1,
205 out='One million is 1%s000%s000' % (self.sep, self.sep))
206 self._test_format_string("One million is %i", 1000000, grouping=1,
207 out='One million is 1%s000%s000' % (self.sep, self.sep))
208 # Dots in formatting string
209 self._test_format_string(".%f.", 1000.0, out='.1000.000000.')
210 # Padding
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000211 if self.sep:
212 self._test_format_string("--> %10.2f", 4200, grouping=1,
213 out='--> ' + ('4%s200.00' % self.sep).rjust(10))
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000214 # Asterisk formats
215 self._test_format_string("%10.*f", (2, 1000), grouping=0,
216 out='1000.00'.rjust(10))
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000217 if self.sep:
218 self._test_format_string("%*.*f", (10, 2, 1000), grouping=1,
219 out=('1%s000.00' % self.sep).rjust(10))
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000220 # Test more-in-one
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000221 if self.sep:
222 self._test_format_string("int %i float %.2f str %s",
223 (1000, 1000.0, 'str'), grouping=1,
224 out='int 1%s000 float 1%s000.00 str str' %
225 (self.sep, self.sep))
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000226
227
R. David Murraye59482e2009-04-01 03:42:00 +0000228class TestFormatPatternArg(unittest.TestCase):
229 # Test handling of pattern argument of format
230
231 def test_onlyOnePattern(self):
232 # Issue 2522: accept exactly one % pattern, and no extra chars.
233 self.assertRaises(ValueError, locale.format, "%f\n", 'foo')
234 self.assertRaises(ValueError, locale.format, "%f\r", 'foo')
235 self.assertRaises(ValueError, locale.format, "%f\r\n", 'foo')
236 self.assertRaises(ValueError, locale.format, " %f", 'foo')
237 self.assertRaises(ValueError, locale.format, "%fg", 'foo')
238 self.assertRaises(ValueError, locale.format, "%^g", 'foo')
R. David Murrayad78d152010-04-27 02:45:53 +0000239 self.assertRaises(ValueError, locale.format, "%f%%", 'foo')
240
241
242class TestLocaleFormatString(unittest.TestCase):
243 """General tests on locale.format_string"""
244
245 def test_percent_escape(self):
246 self.assertEqual(locale.format_string('%f%%', 1.0), '%f%%' % 1.0)
247 self.assertEqual(locale.format_string('%d %f%%d', (1, 1.0)),
248 '%d %f%%d' % (1, 1.0))
249 self.assertEqual(locale.format_string('%(foo)s %%d', {'foo': 'bar'}),
250 ('%(foo)s %%d' % {'foo': 'bar'}))
251
252 def test_mapping(self):
253 self.assertEqual(locale.format_string('%(foo)s bing.', {'foo': 'bar'}),
254 ('%(foo)s bing.' % {'foo': 'bar'}))
255 self.assertEqual(locale.format_string('%(foo)s', {'foo': 'bar'}),
256 ('%(foo)s' % {'foo': 'bar'}))
257
R. David Murraye59482e2009-04-01 03:42:00 +0000258
259
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000260class TestNumberFormatting(BaseLocalizedTest, EnUSNumberFormatting):
261 # Test number formatting with a real English locale.
262
263 locale_type = locale.LC_NUMERIC
264
265 def setUp(self):
266 BaseLocalizedTest.setUp(self)
267 EnUSNumberFormatting.setUp(self)
268
269
270class TestEnUSNumberFormatting(EnUSCookedTest, EnUSNumberFormatting):
271 # Test number formatting with a cooked "en_US" locale.
272
273 def setUp(self):
274 EnUSCookedTest.setUp(self)
275 EnUSNumberFormatting.setUp(self)
276
277 def test_currency(self):
278 self._test_currency(50000, "$50000.00")
279 self._test_currency(50000, "$50,000.00", grouping=True)
280 self._test_currency(50000, "USD 50,000.00",
281 grouping=True, international=True)
282
283
284class TestCNumberFormatting(CCookedTest, BaseFormattingTest):
285 # Test number formatting with a cooked "C" locale.
286
287 def test_grouping(self):
288 self._test_format("%.2f", 12345.67, grouping=True, out='12345.67')
289
290 def test_grouping_and_padding(self):
291 self._test_format("%9.2f", 12345.67, grouping=True, out=' 12345.67')
292
293
Antoine Pitrou350370c2009-03-14 00:13:13 +0000294class TestFrFRNumberFormatting(FrFRCookedTest, BaseFormattingTest):
295 # Test number formatting with a cooked "fr_FR" locale.
296
297 def test_decimal_point(self):
298 self._test_format("%.2f", 12345.67, out='12345,67')
299
300 def test_grouping(self):
301 self._test_format("%.2f", 345.67, grouping=True, out='345,67')
302 self._test_format("%.2f", 12345.67, grouping=True, out='12 345,67')
303
304 def test_grouping_and_padding(self):
305 self._test_format("%6.2f", 345.67, grouping=True, out='345,67')
306 self._test_format("%7.2f", 345.67, grouping=True, out=' 345,67')
307 self._test_format("%8.2f", 12345.67, grouping=True, out='12 345,67')
308 self._test_format("%9.2f", 12345.67, grouping=True, out='12 345,67')
309 self._test_format("%10.2f", 12345.67, grouping=True, out=' 12 345,67')
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
316 def test_integer_grouping(self):
317 self._test_format("%d", 200, grouping=True, out='200')
318 self._test_format("%d", 4200, grouping=True, out='4 200')
319
320 def test_integer_grouping_and_padding(self):
321 self._test_format("%4d", 4200, grouping=True, out='4 200')
322 self._test_format("%5d", 4200, grouping=True, out='4 200')
323 self._test_format("%10d", 4200, grouping=True, out='4 200'.rjust(10))
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'.ljust(10))
327
328 def test_currency(self):
329 euro = '\u20ac'
330 self._test_currency(50000, "50000,00 " + euro)
331 self._test_currency(50000, "50 000,00 " + euro, grouping=True)
332 # XXX is the trailing space a bug?
333 self._test_currency(50000, "50 000,00 EUR ",
334 grouping=True, international=True)
335
336
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000337class TestCollation(unittest.TestCase):
338 # Test string collation functions
339
340 def test_strcoll(self):
341 self.assertLess(locale.strcoll('a', 'b'), 0)
342 self.assertEqual(locale.strcoll('a', 'a'), 0)
343 self.assertGreater(locale.strcoll('b', 'a'), 0)
344
345 def test_strxfrm(self):
346 self.assertLess(locale.strxfrm('a'), locale.strxfrm('b'))
347
348
349class TestEnUSCollation(BaseLocalizedTest, TestCollation):
350 # Test string collation functions with a real English locale
351
352 locale_type = locale.LC_ALL
353
354 def setUp(self):
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000355 enc = codecs.lookup(locale.getpreferredencoding(False) or 'ascii').name
356 if enc not in ('utf-8', 'iso8859-1', 'cp1252'):
357 raise unittest.SkipTest('encoding not suitable')
358 if enc != 'iso8859-1' and (sys.platform == 'darwin' or
359 sys.platform.startswith('freebsd')):
360 raise unittest.SkipTest('wcscoll/wcsxfrm have known bugs')
Hirokazu Yamamotob3b390d2010-09-23 15:20:15 +0000361 BaseLocalizedTest.setUp(self)
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000362
363 def test_strcoll_with_diacritic(self):
364 self.assertLess(locale.strcoll('à', 'b'), 0)
365
366 def test_strxfrm_with_diacritic(self):
367 self.assertLess(locale.strxfrm('à'), locale.strxfrm('b'))
368
369
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000370class TestMiscellaneous(unittest.TestCase):
371 def test_getpreferredencoding(self):
372 # Invoke getpreferredencoding to make sure it does not cause exceptions.
373 enc = locale.getpreferredencoding()
374 if enc:
375 # If encoding non-empty, make sure it is valid
376 codecs.lookup(enc)
377
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000378 def test_strcoll_3303(self):
379 # test crasher from bug #3303
380 self.assertRaises(TypeError, locale.strcoll, "a", None)
381 self.assertRaises(TypeError, locale.strcoll, b"a", None)
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000382
Amaury Forgeot d'Arc64f3ca42009-12-01 21:59:18 +0000383 def test_setlocale_category(self):
384 locale.setlocale(locale.LC_ALL)
385 locale.setlocale(locale.LC_TIME)
386 locale.setlocale(locale.LC_CTYPE)
387 locale.setlocale(locale.LC_COLLATE)
388 locale.setlocale(locale.LC_MONETARY)
389 locale.setlocale(locale.LC_NUMERIC)
390
391 # crasher from bug #7419
392 self.assertRaises(locale.Error, locale.setlocale, 12345)
393
Antoine Pitrou0e3c5a82011-07-24 02:40:25 +0200394 def test_getsetlocale_issue1813(self):
395 # Issue #1813: setting and getting the locale under a Turkish locale
Antoine Pitroud05066d2011-07-26 23:55:33 +0200396 oldlocale = locale.setlocale(locale.LC_CTYPE)
Antoine Pitrou0e3c5a82011-07-24 02:40:25 +0200397 self.addCleanup(locale.setlocale, locale.LC_CTYPE, oldlocale)
398 try:
399 locale.setlocale(locale.LC_CTYPE, 'tr_TR')
400 except locale.Error:
401 # Unsupported locale on this system
402 self.skipTest('test needs Turkish locale')
Antoine Pitroud05066d2011-07-26 23:55:33 +0200403 loc = locale.getlocale(locale.LC_CTYPE)
Antoine Pitrou83d21932011-07-26 14:45:22 +0200404 if verbose:
405 print('got locale %a' % (loc,))
Antoine Pitrou0e3c5a82011-07-24 02:40:25 +0200406 locale.setlocale(locale.LC_CTYPE, loc)
Antoine Pitroud05066d2011-07-26 23:55:33 +0200407 self.assertEqual(loc, locale.getlocale(locale.LC_CTYPE))
Antoine Pitrou0e3c5a82011-07-24 02:40:25 +0200408
Petri Lehtinen3c85fe02011-11-04 21:35:07 +0200409 def test_invalid_locale_format_in_localetuple(self):
410 with self.assertRaises(TypeError):
411 locale.setlocale(locale.LC_ALL, b'fi_FI')
412
413 def test_invalid_iterable_in_localetuple(self):
414 with self.assertRaises(TypeError):
415 locale.setlocale(locale.LC_ALL, (b'not', b'valid'))
416
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000417
418def test_main():
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000419 tests = [
420 TestMiscellaneous,
R. David Murraye59482e2009-04-01 03:42:00 +0000421 TestFormatPatternArg,
R. David Murrayad78d152010-04-27 02:45:53 +0000422 TestLocaleFormatString,
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000423 TestEnUSNumberFormatting,
Antoine Pitrou350370c2009-03-14 00:13:13 +0000424 TestCNumberFormatting,
425 TestFrFRNumberFormatting,
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000426 TestCollation
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000427 ]
Benjamin Petersone549ead2009-03-28 21:42:05 +0000428 # SkipTest can't be raised inside unittests, handle it manually instead
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000429 try:
430 get_enUS_locale()
Benjamin Petersone549ead2009-03-28 21:42:05 +0000431 except unittest.SkipTest as e:
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000432 if verbose:
433 print("Some tests will be disabled: %s" % e)
434 else:
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000435 tests += [TestNumberFormatting, TestEnUSCollation]
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000436 run_unittest(*tests)
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000437
438if __name__ == '__main__':
439 test_main()