blob: 9439522fc90d0056b8ac8dafd91f27db7105d98b [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')
239
240
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000241class TestNumberFormatting(BaseLocalizedTest, EnUSNumberFormatting):
242 # Test number formatting with a real English locale.
243
244 locale_type = locale.LC_NUMERIC
245
246 def setUp(self):
247 BaseLocalizedTest.setUp(self)
248 EnUSNumberFormatting.setUp(self)
249
250
251class TestEnUSNumberFormatting(EnUSCookedTest, EnUSNumberFormatting):
252 # Test number formatting with a cooked "en_US" locale.
253
254 def setUp(self):
255 EnUSCookedTest.setUp(self)
256 EnUSNumberFormatting.setUp(self)
257
258 def test_currency(self):
259 self._test_currency(50000, "$50000.00")
260 self._test_currency(50000, "$50,000.00", grouping=True)
261 self._test_currency(50000, "USD 50,000.00",
262 grouping=True, international=True)
263
264
265class TestCNumberFormatting(CCookedTest, BaseFormattingTest):
266 # Test number formatting with a cooked "C" locale.
267
268 def test_grouping(self):
269 self._test_format("%.2f", 12345.67, grouping=True, out='12345.67')
270
271 def test_grouping_and_padding(self):
272 self._test_format("%9.2f", 12345.67, grouping=True, out=' 12345.67')
273
274
Antoine Pitrou350370c2009-03-14 00:13:13 +0000275class TestFrFRNumberFormatting(FrFRCookedTest, BaseFormattingTest):
276 # Test number formatting with a cooked "fr_FR" locale.
277
278 def test_decimal_point(self):
279 self._test_format("%.2f", 12345.67, out='12345,67')
280
281 def test_grouping(self):
282 self._test_format("%.2f", 345.67, grouping=True, out='345,67')
283 self._test_format("%.2f", 12345.67, grouping=True, out='12 345,67')
284
285 def test_grouping_and_padding(self):
286 self._test_format("%6.2f", 345.67, grouping=True, out='345,67')
287 self._test_format("%7.2f", 345.67, grouping=True, out=' 345,67')
288 self._test_format("%8.2f", 12345.67, grouping=True, out='12 345,67')
289 self._test_format("%9.2f", 12345.67, grouping=True, out='12 345,67')
290 self._test_format("%10.2f", 12345.67, grouping=True, out=' 12 345,67')
291 self._test_format("%-6.2f", 345.67, grouping=True, out='345,67')
292 self._test_format("%-7.2f", 345.67, grouping=True, out='345,67 ')
293 self._test_format("%-8.2f", 12345.67, grouping=True, out='12 345,67')
294 self._test_format("%-9.2f", 12345.67, grouping=True, out='12 345,67')
295 self._test_format("%-10.2f", 12345.67, grouping=True, out='12 345,67 ')
296
297 def test_integer_grouping(self):
298 self._test_format("%d", 200, grouping=True, out='200')
299 self._test_format("%d", 4200, grouping=True, out='4 200')
300
301 def test_integer_grouping_and_padding(self):
302 self._test_format("%4d", 4200, grouping=True, out='4 200')
303 self._test_format("%5d", 4200, grouping=True, out='4 200')
304 self._test_format("%10d", 4200, grouping=True, out='4 200'.rjust(10))
305 self._test_format("%-4d", 4200, grouping=True, out='4 200')
306 self._test_format("%-5d", 4200, grouping=True, out='4 200')
307 self._test_format("%-10d", 4200, grouping=True, out='4 200'.ljust(10))
308
309 def test_currency(self):
310 euro = '\u20ac'
311 self._test_currency(50000, "50000,00 " + euro)
312 self._test_currency(50000, "50 000,00 " + euro, grouping=True)
313 # XXX is the trailing space a bug?
314 self._test_currency(50000, "50 000,00 EUR ",
315 grouping=True, international=True)
316
317
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000318class TestCollation(unittest.TestCase):
319 # Test string collation functions
320
321 def test_strcoll(self):
322 self.assertLess(locale.strcoll('a', 'b'), 0)
323 self.assertEqual(locale.strcoll('a', 'a'), 0)
324 self.assertGreater(locale.strcoll('b', 'a'), 0)
325
326 def test_strxfrm(self):
327 self.assertLess(locale.strxfrm('a'), locale.strxfrm('b'))
328
329
330class TestEnUSCollation(BaseLocalizedTest, TestCollation):
331 # Test string collation functions with a real English locale
332
333 locale_type = locale.LC_ALL
334
335 def setUp(self):
336 BaseLocalizedTest.setUp(self)
337 enc = codecs.lookup(locale.getpreferredencoding(False) or 'ascii').name
338 if enc not in ('utf-8', 'iso8859-1', 'cp1252'):
339 raise unittest.SkipTest('encoding not suitable')
340 if enc != 'iso8859-1' and (sys.platform == 'darwin' or
341 sys.platform.startswith('freebsd')):
342 raise unittest.SkipTest('wcscoll/wcsxfrm have known bugs')
343
344 def test_strcoll_with_diacritic(self):
345 self.assertLess(locale.strcoll('à', 'b'), 0)
346
347 def test_strxfrm_with_diacritic(self):
348 self.assertLess(locale.strxfrm('à'), locale.strxfrm('b'))
349
350
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000351class TestMiscellaneous(unittest.TestCase):
352 def test_getpreferredencoding(self):
353 # Invoke getpreferredencoding to make sure it does not cause exceptions.
354 enc = locale.getpreferredencoding()
355 if enc:
356 # If encoding non-empty, make sure it is valid
357 codecs.lookup(enc)
358
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000359 def test_strcoll_3303(self):
360 # test crasher from bug #3303
361 self.assertRaises(TypeError, locale.strcoll, "a", None)
362 self.assertRaises(TypeError, locale.strcoll, b"a", None)
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000363
Amaury Forgeot d'Arc64f3ca42009-12-01 21:59:18 +0000364 def test_setlocale_category(self):
365 locale.setlocale(locale.LC_ALL)
366 locale.setlocale(locale.LC_TIME)
367 locale.setlocale(locale.LC_CTYPE)
368 locale.setlocale(locale.LC_COLLATE)
369 locale.setlocale(locale.LC_MONETARY)
370 locale.setlocale(locale.LC_NUMERIC)
371
372 # crasher from bug #7419
373 self.assertRaises(locale.Error, locale.setlocale, 12345)
374
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000375
376def test_main():
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000377 tests = [
378 TestMiscellaneous,
R. David Murraye59482e2009-04-01 03:42:00 +0000379 TestFormatPatternArg,
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000380 TestEnUSNumberFormatting,
Antoine Pitrou350370c2009-03-14 00:13:13 +0000381 TestCNumberFormatting,
382 TestFrFRNumberFormatting,
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000383 TestCollation
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000384 ]
Benjamin Petersone549ead2009-03-28 21:42:05 +0000385 # SkipTest can't be raised inside unittests, handle it manually instead
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000386 try:
387 get_enUS_locale()
Benjamin Petersone549ead2009-03-28 21:42:05 +0000388 except unittest.SkipTest as e:
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000389 if verbose:
390 print("Some tests will be disabled: %s" % e)
391 else:
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000392 tests += [TestNumberFormatting, TestEnUSCollation]
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000393 run_unittest(*tests)
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000394
395if __name__ == '__main__':
396 test_main()