blob: 8c17f8770c7419991fd1786f990a13cac6a6347b [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
Antoine Pitrou13856ea2008-07-26 21:02:53 +000011 if sys.platform.startswith("win"):
12 tlocs = ("En", "English")
13 else:
Antoine Pitrou6a448d42009-10-19 19:43:09 +000014 tlocs = ("en_US.UTF-8", "en_US.ISO8859-1", "en_US.US-ASCII", "en_US")
Antoine Pitrou13856ea2008-07-26 21:02:53 +000015 oldlocale = locale.setlocale(locale.LC_NUMERIC)
16 for tloc in tlocs:
17 try:
18 locale.setlocale(locale.LC_NUMERIC, tloc)
19 except locale.Error:
20 continue
21 break
22 else:
Benjamin Petersone549ead2009-03-28 21:42:05 +000023 raise unittest.SkipTest(
Antoine Pitrou13856ea2008-07-26 21:02:53 +000024 "Test locale not supported (tried %s)" % (', '.join(tlocs)))
25 enUS_locale = tloc
26 locale.setlocale(locale.LC_NUMERIC, oldlocale)
27
28
Antoine Pitrou83d6a872008-07-25 21:45:08 +000029class BaseLocalizedTest(unittest.TestCase):
30 #
31 # Base class for tests using a real locale
32 #
Martin v. Löwis88ad12a2001-04-13 08:09:50 +000033
Antoine Pitrou83d6a872008-07-25 21:45:08 +000034 def setUp(self):
Antoine Pitrou83d6a872008-07-25 21:45:08 +000035 self.oldlocale = locale.setlocale(self.locale_type)
Antoine Pitrou13856ea2008-07-26 21:02:53 +000036 locale.setlocale(self.locale_type, enUS_locale)
Martin v. Löwis88ad12a2001-04-13 08:09:50 +000037 if verbose:
Antoine Pitrou13856ea2008-07-26 21:02:53 +000038 print("testing with \"%s\"..." % enUS_locale, end=' ')
Martin v. Löwis88ad12a2001-04-13 08:09:50 +000039
Antoine Pitrou83d6a872008-07-25 21:45:08 +000040 def tearDown(self):
41 locale.setlocale(self.locale_type, self.oldlocale)
Thomas Wouters477c8d52006-05-27 19:21:47 +000042
Thomas Wouters477c8d52006-05-27 19:21:47 +000043
Antoine Pitrou83d6a872008-07-25 21:45:08 +000044class BaseCookedTest(unittest.TestCase):
45 #
46 # Base class for tests using cooked localeconv() values
47 #
Georg Brandl3dbca812008-07-23 16:10:53 +000048
Antoine Pitrou83d6a872008-07-25 21:45:08 +000049 def setUp(self):
50 locale._override_localeconv = self.cooked_values
51
52 def tearDown(self):
53 locale._override_localeconv = {}
54
55class CCookedTest(BaseCookedTest):
56 # A cooked "C" locale
57
58 cooked_values = {
59 'currency_symbol': '',
60 'decimal_point': '.',
61 'frac_digits': 127,
62 'grouping': [],
63 'int_curr_symbol': '',
64 'int_frac_digits': 127,
65 'mon_decimal_point': '',
66 'mon_grouping': [],
67 'mon_thousands_sep': '',
68 'n_cs_precedes': 127,
69 'n_sep_by_space': 127,
70 'n_sign_posn': 127,
71 'negative_sign': '',
72 'p_cs_precedes': 127,
73 'p_sep_by_space': 127,
74 'p_sign_posn': 127,
75 'positive_sign': '',
76 'thousands_sep': ''
77 }
78
79class EnUSCookedTest(BaseCookedTest):
80 # A cooked "en_US" locale
81
82 cooked_values = {
83 'currency_symbol': '$',
84 'decimal_point': '.',
85 'frac_digits': 2,
86 'grouping': [3, 3, 0],
87 'int_curr_symbol': 'USD ',
88 'int_frac_digits': 2,
89 'mon_decimal_point': '.',
90 'mon_grouping': [3, 3, 0],
91 'mon_thousands_sep': ',',
92 'n_cs_precedes': 1,
93 'n_sep_by_space': 0,
94 'n_sign_posn': 1,
95 'negative_sign': '-',
96 'p_cs_precedes': 1,
97 'p_sep_by_space': 0,
98 'p_sign_posn': 1,
99 'positive_sign': '',
100 'thousands_sep': ','
101 }
102
103
Antoine Pitrou350370c2009-03-14 00:13:13 +0000104class FrFRCookedTest(BaseCookedTest):
105 # A cooked "fr_FR" locale with a space character as decimal separator
106 # and a non-ASCII currency symbol.
107
108 cooked_values = {
109 'currency_symbol': '\u20ac',
110 'decimal_point': ',',
111 'frac_digits': 2,
112 'grouping': [3, 3, 0],
113 'int_curr_symbol': 'EUR ',
114 'int_frac_digits': 2,
115 'mon_decimal_point': ',',
116 'mon_grouping': [3, 3, 0],
117 'mon_thousands_sep': ' ',
118 'n_cs_precedes': 0,
119 'n_sep_by_space': 1,
120 'n_sign_posn': 1,
121 'negative_sign': '-',
122 'p_cs_precedes': 0,
123 'p_sep_by_space': 1,
124 'p_sign_posn': 1,
125 'positive_sign': '',
126 'thousands_sep': ' '
127 }
128
129
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000130class BaseFormattingTest(object):
131 #
132 # Utility functions for formatting tests
133 #
134
135 def _test_formatfunc(self, format, value, out, func, **format_opts):
136 self.assertEqual(
137 func(format, value, **format_opts), out)
138
139 def _test_format(self, format, value, out, **format_opts):
140 self._test_formatfunc(format, value, out,
141 func=locale.format, **format_opts)
142
143 def _test_format_string(self, format, value, out, **format_opts):
144 self._test_formatfunc(format, value, out,
145 func=locale.format_string, **format_opts)
146
147 def _test_currency(self, value, out, **format_opts):
148 self.assertEqual(locale.currency(value, **format_opts), out)
149
150
151class EnUSNumberFormatting(BaseFormattingTest):
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000152 # XXX there is a grouping + padding bug when the thousands separator
153 # is empty but the grouping array contains values (e.g. Solaris 10)
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000154
155 def setUp(self):
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000156 self.sep = locale.localeconv()['thousands_sep']
157
158 def test_grouping(self):
159 self._test_format("%f", 1024, grouping=1, out='1%s024.000000' % self.sep)
160 self._test_format("%f", 102, grouping=1, out='102.000000')
161 self._test_format("%f", -42, grouping=1, out='-42.000000')
162 self._test_format("%+f", -42, grouping=1, out='-42.000000')
163
164 def test_grouping_and_padding(self):
165 self._test_format("%20.f", -42, grouping=1, out='-42'.rjust(20))
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000166 if self.sep:
167 self._test_format("%+10.f", -4200, grouping=1,
168 out=('-4%s200' % self.sep).rjust(10))
169 self._test_format("%-10.f", -4200, grouping=1,
170 out=('-4%s200' % self.sep).ljust(10))
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000171
172 def test_integer_grouping(self):
173 self._test_format("%d", 4200, grouping=True, out='4%s200' % self.sep)
174 self._test_format("%+d", 4200, grouping=True, out='+4%s200' % self.sep)
175 self._test_format("%+d", -4200, grouping=True, out='-4%s200' % self.sep)
176
Antoine Pitrou350370c2009-03-14 00:13:13 +0000177 def test_integer_grouping_and_padding(self):
178 self._test_format("%10d", 4200, grouping=True,
179 out=('4%s200' % self.sep).rjust(10))
180 self._test_format("%-10d", -4200, grouping=True,
181 out=('-4%s200' % self.sep).ljust(10))
182
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000183 def test_simple(self):
184 self._test_format("%f", 1024, grouping=0, out='1024.000000')
185 self._test_format("%f", 102, grouping=0, out='102.000000')
186 self._test_format("%f", -42, grouping=0, out='-42.000000')
187 self._test_format("%+f", -42, grouping=0, out='-42.000000')
188
189 def test_padding(self):
190 self._test_format("%20.f", -42, grouping=0, out='-42'.rjust(20))
191 self._test_format("%+10.f", -4200, grouping=0, out='-4200'.rjust(10))
192 self._test_format("%-10.f", 4200, grouping=0, out='4200'.ljust(10))
193
194 def test_complex_formatting(self):
195 # Spaces in formatting string
196 self._test_format_string("One million is %i", 1000000, grouping=1,
197 out='One million is 1%s000%s000' % (self.sep, self.sep))
198 self._test_format_string("One million is %i", 1000000, grouping=1,
199 out='One million is 1%s000%s000' % (self.sep, self.sep))
200 # Dots in formatting string
201 self._test_format_string(".%f.", 1000.0, out='.1000.000000.')
202 # Padding
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000203 if self.sep:
204 self._test_format_string("--> %10.2f", 4200, grouping=1,
205 out='--> ' + ('4%s200.00' % self.sep).rjust(10))
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000206 # Asterisk formats
207 self._test_format_string("%10.*f", (2, 1000), grouping=0,
208 out='1000.00'.rjust(10))
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000209 if self.sep:
210 self._test_format_string("%*.*f", (10, 2, 1000), grouping=1,
211 out=('1%s000.00' % self.sep).rjust(10))
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000212 # Test more-in-one
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000213 if self.sep:
214 self._test_format_string("int %i float %.2f str %s",
215 (1000, 1000.0, 'str'), grouping=1,
216 out='int 1%s000 float 1%s000.00 str str' %
217 (self.sep, self.sep))
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000218
219
R. David Murraye59482e2009-04-01 03:42:00 +0000220class TestFormatPatternArg(unittest.TestCase):
221 # Test handling of pattern argument of format
222
223 def test_onlyOnePattern(self):
224 # Issue 2522: accept exactly one % pattern, and no extra chars.
225 self.assertRaises(ValueError, locale.format, "%f\n", 'foo')
226 self.assertRaises(ValueError, locale.format, "%f\r", 'foo')
227 self.assertRaises(ValueError, locale.format, "%f\r\n", 'foo')
228 self.assertRaises(ValueError, locale.format, " %f", 'foo')
229 self.assertRaises(ValueError, locale.format, "%fg", 'foo')
230 self.assertRaises(ValueError, locale.format, "%^g", 'foo')
231
232
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000233class TestNumberFormatting(BaseLocalizedTest, EnUSNumberFormatting):
234 # Test number formatting with a real English locale.
235
236 locale_type = locale.LC_NUMERIC
237
238 def setUp(self):
239 BaseLocalizedTest.setUp(self)
240 EnUSNumberFormatting.setUp(self)
241
242
243class TestEnUSNumberFormatting(EnUSCookedTest, EnUSNumberFormatting):
244 # Test number formatting with a cooked "en_US" locale.
245
246 def setUp(self):
247 EnUSCookedTest.setUp(self)
248 EnUSNumberFormatting.setUp(self)
249
250 def test_currency(self):
251 self._test_currency(50000, "$50000.00")
252 self._test_currency(50000, "$50,000.00", grouping=True)
253 self._test_currency(50000, "USD 50,000.00",
254 grouping=True, international=True)
255
256
257class TestCNumberFormatting(CCookedTest, BaseFormattingTest):
258 # Test number formatting with a cooked "C" locale.
259
260 def test_grouping(self):
261 self._test_format("%.2f", 12345.67, grouping=True, out='12345.67')
262
263 def test_grouping_and_padding(self):
264 self._test_format("%9.2f", 12345.67, grouping=True, out=' 12345.67')
265
266
Antoine Pitrou350370c2009-03-14 00:13:13 +0000267class TestFrFRNumberFormatting(FrFRCookedTest, BaseFormattingTest):
268 # Test number formatting with a cooked "fr_FR" locale.
269
270 def test_decimal_point(self):
271 self._test_format("%.2f", 12345.67, out='12345,67')
272
273 def test_grouping(self):
274 self._test_format("%.2f", 345.67, grouping=True, out='345,67')
275 self._test_format("%.2f", 12345.67, grouping=True, out='12 345,67')
276
277 def test_grouping_and_padding(self):
278 self._test_format("%6.2f", 345.67, grouping=True, out='345,67')
279 self._test_format("%7.2f", 345.67, grouping=True, out=' 345,67')
280 self._test_format("%8.2f", 12345.67, grouping=True, out='12 345,67')
281 self._test_format("%9.2f", 12345.67, grouping=True, out='12 345,67')
282 self._test_format("%10.2f", 12345.67, grouping=True, out=' 12 345,67')
283 self._test_format("%-6.2f", 345.67, grouping=True, out='345,67')
284 self._test_format("%-7.2f", 345.67, grouping=True, out='345,67 ')
285 self._test_format("%-8.2f", 12345.67, grouping=True, out='12 345,67')
286 self._test_format("%-9.2f", 12345.67, grouping=True, out='12 345,67')
287 self._test_format("%-10.2f", 12345.67, grouping=True, out='12 345,67 ')
288
289 def test_integer_grouping(self):
290 self._test_format("%d", 200, grouping=True, out='200')
291 self._test_format("%d", 4200, grouping=True, out='4 200')
292
293 def test_integer_grouping_and_padding(self):
294 self._test_format("%4d", 4200, grouping=True, out='4 200')
295 self._test_format("%5d", 4200, grouping=True, out='4 200')
296 self._test_format("%10d", 4200, grouping=True, out='4 200'.rjust(10))
297 self._test_format("%-4d", 4200, grouping=True, out='4 200')
298 self._test_format("%-5d", 4200, grouping=True, out='4 200')
299 self._test_format("%-10d", 4200, grouping=True, out='4 200'.ljust(10))
300
301 def test_currency(self):
302 euro = '\u20ac'
303 self._test_currency(50000, "50000,00 " + euro)
304 self._test_currency(50000, "50 000,00 " + euro, grouping=True)
305 # XXX is the trailing space a bug?
306 self._test_currency(50000, "50 000,00 EUR ",
307 grouping=True, international=True)
308
309
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000310class TestCollation(unittest.TestCase):
311 # Test string collation functions
312
313 def test_strcoll(self):
314 self.assertLess(locale.strcoll('a', 'b'), 0)
315 self.assertEqual(locale.strcoll('a', 'a'), 0)
316 self.assertGreater(locale.strcoll('b', 'a'), 0)
317
318 def test_strxfrm(self):
319 self.assertLess(locale.strxfrm('a'), locale.strxfrm('b'))
320
321
322class TestEnUSCollation(BaseLocalizedTest, TestCollation):
323 # Test string collation functions with a real English locale
324
325 locale_type = locale.LC_ALL
326
327 def setUp(self):
328 BaseLocalizedTest.setUp(self)
329 enc = codecs.lookup(locale.getpreferredencoding(False) or 'ascii').name
330 if enc not in ('utf-8', 'iso8859-1', 'cp1252'):
331 raise unittest.SkipTest('encoding not suitable')
332 if enc != 'iso8859-1' and (sys.platform == 'darwin' or
333 sys.platform.startswith('freebsd')):
334 raise unittest.SkipTest('wcscoll/wcsxfrm have known bugs')
335
336 def test_strcoll_with_diacritic(self):
337 self.assertLess(locale.strcoll('à', 'b'), 0)
338
339 def test_strxfrm_with_diacritic(self):
340 self.assertLess(locale.strxfrm('à'), locale.strxfrm('b'))
341
342
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000343class TestMiscellaneous(unittest.TestCase):
344 def test_getpreferredencoding(self):
345 # Invoke getpreferredencoding to make sure it does not cause exceptions.
346 enc = locale.getpreferredencoding()
347 if enc:
348 # If encoding non-empty, make sure it is valid
349 codecs.lookup(enc)
350
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000351 def test_strcoll_3303(self):
352 # test crasher from bug #3303
353 self.assertRaises(TypeError, locale.strcoll, "a", None)
354 self.assertRaises(TypeError, locale.strcoll, b"a", None)
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000355
Amaury Forgeot d'Arc64f3ca42009-12-01 21:59:18 +0000356 def test_setlocale_category(self):
357 locale.setlocale(locale.LC_ALL)
358 locale.setlocale(locale.LC_TIME)
359 locale.setlocale(locale.LC_CTYPE)
360 locale.setlocale(locale.LC_COLLATE)
361 locale.setlocale(locale.LC_MONETARY)
362 locale.setlocale(locale.LC_NUMERIC)
363
364 # crasher from bug #7419
365 self.assertRaises(locale.Error, locale.setlocale, 12345)
366
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000367
368def test_main():
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000369 tests = [
370 TestMiscellaneous,
R. David Murraye59482e2009-04-01 03:42:00 +0000371 TestFormatPatternArg,
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000372 TestEnUSNumberFormatting,
Antoine Pitrou350370c2009-03-14 00:13:13 +0000373 TestCNumberFormatting,
374 TestFrFRNumberFormatting,
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000375 TestCollation
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000376 ]
Benjamin Petersone549ead2009-03-28 21:42:05 +0000377 # SkipTest can't be raised inside unittests, handle it manually instead
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000378 try:
379 get_enUS_locale()
Benjamin Petersone549ead2009-03-28 21:42:05 +0000380 except unittest.SkipTest as e:
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000381 if verbose:
382 print("Some tests will be disabled: %s" % e)
383 else:
Antoine Pitrou6a448d42009-10-19 19:43:09 +0000384 tests += [TestNumberFormatting, TestEnUSCollation]
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000385 run_unittest(*tests)
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000386
387if __name__ == '__main__':
388 test_main()