]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | from test.test_support import run_unittest, verbose\r |
2 | import unittest\r | |
3 | import locale\r | |
4 | import sys\r | |
5 | import codecs\r | |
6 | \r | |
7 | \r | |
8 | enUS_locale = None\r | |
9 | \r | |
10 | def get_enUS_locale():\r | |
11 | global enUS_locale\r | |
12 | if sys.platform == 'darwin':\r | |
13 | import os\r | |
14 | tlocs = ("en_US.UTF-8", "en_US.ISO8859-1", "en_US")\r | |
15 | if int(os.uname()[2].split('.')[0]) < 10:\r | |
16 | # The locale test work fine on OSX 10.6, I (ronaldoussoren)\r | |
17 | # haven't had time yet to verify if tests work on OSX 10.5\r | |
18 | # (10.4 is known to be bad)\r | |
19 | raise unittest.SkipTest("Locale support on MacOSX is minimal")\r | |
20 | if sys.platform.startswith("win"):\r | |
21 | tlocs = ("En", "English")\r | |
22 | else:\r | |
23 | tlocs = ("en_US.UTF-8", "en_US.US-ASCII", "en_US")\r | |
24 | oldlocale = locale.setlocale(locale.LC_NUMERIC)\r | |
25 | for tloc in tlocs:\r | |
26 | try:\r | |
27 | locale.setlocale(locale.LC_NUMERIC, tloc)\r | |
28 | except locale.Error:\r | |
29 | continue\r | |
30 | break\r | |
31 | else:\r | |
32 | raise unittest.SkipTest(\r | |
33 | "Test locale not supported (tried %s)" % (', '.join(tlocs)))\r | |
34 | enUS_locale = tloc\r | |
35 | locale.setlocale(locale.LC_NUMERIC, oldlocale)\r | |
36 | \r | |
37 | \r | |
38 | class BaseLocalizedTest(unittest.TestCase):\r | |
39 | #\r | |
40 | # Base class for tests using a real locale\r | |
41 | #\r | |
42 | \r | |
43 | def setUp(self):\r | |
44 | self.oldlocale = locale.setlocale(self.locale_type)\r | |
45 | locale.setlocale(self.locale_type, enUS_locale)\r | |
46 | if verbose:\r | |
47 | print "testing with \"%s\"..." % enUS_locale,\r | |
48 | \r | |
49 | def tearDown(self):\r | |
50 | locale.setlocale(self.locale_type, self.oldlocale)\r | |
51 | \r | |
52 | \r | |
53 | class BaseCookedTest(unittest.TestCase):\r | |
54 | #\r | |
55 | # Base class for tests using cooked localeconv() values\r | |
56 | #\r | |
57 | \r | |
58 | def setUp(self):\r | |
59 | locale._override_localeconv = self.cooked_values\r | |
60 | \r | |
61 | def tearDown(self):\r | |
62 | locale._override_localeconv = {}\r | |
63 | \r | |
64 | \r | |
65 | class CCookedTest(BaseCookedTest):\r | |
66 | # A cooked "C" locale\r | |
67 | \r | |
68 | cooked_values = {\r | |
69 | 'currency_symbol': '',\r | |
70 | 'decimal_point': '.',\r | |
71 | 'frac_digits': 127,\r | |
72 | 'grouping': [],\r | |
73 | 'int_curr_symbol': '',\r | |
74 | 'int_frac_digits': 127,\r | |
75 | 'mon_decimal_point': '',\r | |
76 | 'mon_grouping': [],\r | |
77 | 'mon_thousands_sep': '',\r | |
78 | 'n_cs_precedes': 127,\r | |
79 | 'n_sep_by_space': 127,\r | |
80 | 'n_sign_posn': 127,\r | |
81 | 'negative_sign': '',\r | |
82 | 'p_cs_precedes': 127,\r | |
83 | 'p_sep_by_space': 127,\r | |
84 | 'p_sign_posn': 127,\r | |
85 | 'positive_sign': '',\r | |
86 | 'thousands_sep': ''\r | |
87 | }\r | |
88 | \r | |
89 | class EnUSCookedTest(BaseCookedTest):\r | |
90 | # A cooked "en_US" locale\r | |
91 | \r | |
92 | cooked_values = {\r | |
93 | 'currency_symbol': '$',\r | |
94 | 'decimal_point': '.',\r | |
95 | 'frac_digits': 2,\r | |
96 | 'grouping': [3, 3, 0],\r | |
97 | 'int_curr_symbol': 'USD ',\r | |
98 | 'int_frac_digits': 2,\r | |
99 | 'mon_decimal_point': '.',\r | |
100 | 'mon_grouping': [3, 3, 0],\r | |
101 | 'mon_thousands_sep': ',',\r | |
102 | 'n_cs_precedes': 1,\r | |
103 | 'n_sep_by_space': 0,\r | |
104 | 'n_sign_posn': 1,\r | |
105 | 'negative_sign': '-',\r | |
106 | 'p_cs_precedes': 1,\r | |
107 | 'p_sep_by_space': 0,\r | |
108 | 'p_sign_posn': 1,\r | |
109 | 'positive_sign': '',\r | |
110 | 'thousands_sep': ','\r | |
111 | }\r | |
112 | \r | |
113 | \r | |
114 | class FrFRCookedTest(BaseCookedTest):\r | |
115 | # A cooked "fr_FR" locale with a space character as decimal separator\r | |
116 | # and a non-ASCII currency symbol.\r | |
117 | \r | |
118 | cooked_values = {\r | |
119 | 'currency_symbol': '\xe2\x82\xac',\r | |
120 | 'decimal_point': ',',\r | |
121 | 'frac_digits': 2,\r | |
122 | 'grouping': [3, 3, 0],\r | |
123 | 'int_curr_symbol': 'EUR ',\r | |
124 | 'int_frac_digits': 2,\r | |
125 | 'mon_decimal_point': ',',\r | |
126 | 'mon_grouping': [3, 3, 0],\r | |
127 | 'mon_thousands_sep': ' ',\r | |
128 | 'n_cs_precedes': 0,\r | |
129 | 'n_sep_by_space': 1,\r | |
130 | 'n_sign_posn': 1,\r | |
131 | 'negative_sign': '-',\r | |
132 | 'p_cs_precedes': 0,\r | |
133 | 'p_sep_by_space': 1,\r | |
134 | 'p_sign_posn': 1,\r | |
135 | 'positive_sign': '',\r | |
136 | 'thousands_sep': ' '\r | |
137 | }\r | |
138 | \r | |
139 | \r | |
140 | class BaseFormattingTest(object):\r | |
141 | #\r | |
142 | # Utility functions for formatting tests\r | |
143 | #\r | |
144 | \r | |
145 | def _test_formatfunc(self, format, value, out, func, **format_opts):\r | |
146 | self.assertEqual(\r | |
147 | func(format, value, **format_opts), out)\r | |
148 | \r | |
149 | def _test_format(self, format, value, out, **format_opts):\r | |
150 | self._test_formatfunc(format, value, out,\r | |
151 | func=locale.format, **format_opts)\r | |
152 | \r | |
153 | def _test_format_string(self, format, value, out, **format_opts):\r | |
154 | self._test_formatfunc(format, value, out,\r | |
155 | func=locale.format_string, **format_opts)\r | |
156 | \r | |
157 | def _test_currency(self, value, out, **format_opts):\r | |
158 | self.assertEqual(locale.currency(value, **format_opts), out)\r | |
159 | \r | |
160 | \r | |
161 | class EnUSNumberFormatting(BaseFormattingTest):\r | |
162 | # XXX there is a grouping + padding bug when the thousands separator\r | |
163 | # is empty but the grouping array contains values (e.g. Solaris 10)\r | |
164 | \r | |
165 | def setUp(self):\r | |
166 | self.sep = locale.localeconv()['thousands_sep']\r | |
167 | \r | |
168 | def test_grouping(self):\r | |
169 | self._test_format("%f", 1024, grouping=1, out='1%s024.000000' % self.sep)\r | |
170 | self._test_format("%f", 102, grouping=1, out='102.000000')\r | |
171 | self._test_format("%f", -42, grouping=1, out='-42.000000')\r | |
172 | self._test_format("%+f", -42, grouping=1, out='-42.000000')\r | |
173 | \r | |
174 | def test_grouping_and_padding(self):\r | |
175 | self._test_format("%20.f", -42, grouping=1, out='-42'.rjust(20))\r | |
176 | if self.sep:\r | |
177 | self._test_format("%+10.f", -4200, grouping=1,\r | |
178 | out=('-4%s200' % self.sep).rjust(10))\r | |
179 | self._test_format("%-10.f", -4200, grouping=1,\r | |
180 | out=('-4%s200' % self.sep).ljust(10))\r | |
181 | \r | |
182 | def test_integer_grouping(self):\r | |
183 | self._test_format("%d", 4200, grouping=True, out='4%s200' % self.sep)\r | |
184 | self._test_format("%+d", 4200, grouping=True, out='+4%s200' % self.sep)\r | |
185 | self._test_format("%+d", -4200, grouping=True, out='-4%s200' % self.sep)\r | |
186 | \r | |
187 | def test_integer_grouping_and_padding(self):\r | |
188 | self._test_format("%10d", 4200, grouping=True,\r | |
189 | out=('4%s200' % self.sep).rjust(10))\r | |
190 | self._test_format("%-10d", -4200, grouping=True,\r | |
191 | out=('-4%s200' % self.sep).ljust(10))\r | |
192 | \r | |
193 | def test_simple(self):\r | |
194 | self._test_format("%f", 1024, grouping=0, out='1024.000000')\r | |
195 | self._test_format("%f", 102, grouping=0, out='102.000000')\r | |
196 | self._test_format("%f", -42, grouping=0, out='-42.000000')\r | |
197 | self._test_format("%+f", -42, grouping=0, out='-42.000000')\r | |
198 | \r | |
199 | def test_padding(self):\r | |
200 | self._test_format("%20.f", -42, grouping=0, out='-42'.rjust(20))\r | |
201 | self._test_format("%+10.f", -4200, grouping=0, out='-4200'.rjust(10))\r | |
202 | self._test_format("%-10.f", 4200, grouping=0, out='4200'.ljust(10))\r | |
203 | \r | |
204 | def test_complex_formatting(self):\r | |
205 | # Spaces in formatting string\r | |
206 | self._test_format_string("One million is %i", 1000000, grouping=1,\r | |
207 | out='One million is 1%s000%s000' % (self.sep, self.sep))\r | |
208 | self._test_format_string("One million is %i", 1000000, grouping=1,\r | |
209 | out='One million is 1%s000%s000' % (self.sep, self.sep))\r | |
210 | # Dots in formatting string\r | |
211 | self._test_format_string(".%f.", 1000.0, out='.1000.000000.')\r | |
212 | # Padding\r | |
213 | if self.sep:\r | |
214 | self._test_format_string("--> %10.2f", 4200, grouping=1,\r | |
215 | out='--> ' + ('4%s200.00' % self.sep).rjust(10))\r | |
216 | # Asterisk formats\r | |
217 | self._test_format_string("%10.*f", (2, 1000), grouping=0,\r | |
218 | out='1000.00'.rjust(10))\r | |
219 | if self.sep:\r | |
220 | self._test_format_string("%*.*f", (10, 2, 1000), grouping=1,\r | |
221 | out=('1%s000.00' % self.sep).rjust(10))\r | |
222 | # Test more-in-one\r | |
223 | if self.sep:\r | |
224 | self._test_format_string("int %i float %.2f str %s",\r | |
225 | (1000, 1000.0, 'str'), grouping=1,\r | |
226 | out='int 1%s000 float 1%s000.00 str str' %\r | |
227 | (self.sep, self.sep))\r | |
228 | \r | |
229 | \r | |
230 | class TestFormatPatternArg(unittest.TestCase):\r | |
231 | # Test handling of pattern argument of format\r | |
232 | \r | |
233 | def test_onlyOnePattern(self):\r | |
234 | # Issue 2522: accept exactly one % pattern, and no extra chars.\r | |
235 | self.assertRaises(ValueError, locale.format, "%f\n", 'foo')\r | |
236 | self.assertRaises(ValueError, locale.format, "%f\r", 'foo')\r | |
237 | self.assertRaises(ValueError, locale.format, "%f\r\n", 'foo')\r | |
238 | self.assertRaises(ValueError, locale.format, " %f", 'foo')\r | |
239 | self.assertRaises(ValueError, locale.format, "%fg", 'foo')\r | |
240 | self.assertRaises(ValueError, locale.format, "%^g", 'foo')\r | |
241 | self.assertRaises(ValueError, locale.format, "%f%%", 'foo')\r | |
242 | \r | |
243 | \r | |
244 | class TestLocaleFormatString(unittest.TestCase):\r | |
245 | """General tests on locale.format_string"""\r | |
246 | \r | |
247 | def test_percent_escape(self):\r | |
248 | self.assertEqual(locale.format_string('%f%%', 1.0), '%f%%' % 1.0)\r | |
249 | self.assertEqual(locale.format_string('%d %f%%d', (1, 1.0)),\r | |
250 | '%d %f%%d' % (1, 1.0))\r | |
251 | self.assertEqual(locale.format_string('%(foo)s %%d', {'foo': 'bar'}),\r | |
252 | ('%(foo)s %%d' % {'foo': 'bar'}))\r | |
253 | \r | |
254 | def test_mapping(self):\r | |
255 | self.assertEqual(locale.format_string('%(foo)s bing.', {'foo': 'bar'}),\r | |
256 | ('%(foo)s bing.' % {'foo': 'bar'}))\r | |
257 | self.assertEqual(locale.format_string('%(foo)s', {'foo': 'bar'}),\r | |
258 | ('%(foo)s' % {'foo': 'bar'}))\r | |
259 | \r | |
260 | \r | |
261 | \r | |
262 | class TestNumberFormatting(BaseLocalizedTest, EnUSNumberFormatting):\r | |
263 | # Test number formatting with a real English locale.\r | |
264 | \r | |
265 | locale_type = locale.LC_NUMERIC\r | |
266 | \r | |
267 | def setUp(self):\r | |
268 | BaseLocalizedTest.setUp(self)\r | |
269 | EnUSNumberFormatting.setUp(self)\r | |
270 | \r | |
271 | \r | |
272 | class TestEnUSNumberFormatting(EnUSCookedTest, EnUSNumberFormatting):\r | |
273 | # Test number formatting with a cooked "en_US" locale.\r | |
274 | \r | |
275 | def setUp(self):\r | |
276 | EnUSCookedTest.setUp(self)\r | |
277 | EnUSNumberFormatting.setUp(self)\r | |
278 | \r | |
279 | def test_currency(self):\r | |
280 | self._test_currency(50000, "$50000.00")\r | |
281 | self._test_currency(50000, "$50,000.00", grouping=True)\r | |
282 | self._test_currency(50000, "USD 50,000.00",\r | |
283 | grouping=True, international=True)\r | |
284 | \r | |
285 | \r | |
286 | class TestCNumberFormatting(CCookedTest, BaseFormattingTest):\r | |
287 | # Test number formatting with a cooked "C" locale.\r | |
288 | \r | |
289 | def test_grouping(self):\r | |
290 | self._test_format("%.2f", 12345.67, grouping=True, out='12345.67')\r | |
291 | \r | |
292 | def test_grouping_and_padding(self):\r | |
293 | self._test_format("%9.2f", 12345.67, grouping=True, out=' 12345.67')\r | |
294 | \r | |
295 | \r | |
296 | class TestFrFRNumberFormatting(FrFRCookedTest, BaseFormattingTest):\r | |
297 | # Test number formatting with a cooked "fr_FR" locale.\r | |
298 | \r | |
299 | def test_decimal_point(self):\r | |
300 | self._test_format("%.2f", 12345.67, out='12345,67')\r | |
301 | \r | |
302 | def test_grouping(self):\r | |
303 | self._test_format("%.2f", 345.67, grouping=True, out='345,67')\r | |
304 | self._test_format("%.2f", 12345.67, grouping=True, out='12 345,67')\r | |
305 | \r | |
306 | def test_grouping_and_padding(self):\r | |
307 | self._test_format("%6.2f", 345.67, grouping=True, out='345,67')\r | |
308 | self._test_format("%7.2f", 345.67, grouping=True, out=' 345,67')\r | |
309 | self._test_format("%8.2f", 12345.67, grouping=True, out='12 345,67')\r | |
310 | self._test_format("%9.2f", 12345.67, grouping=True, out='12 345,67')\r | |
311 | self._test_format("%10.2f", 12345.67, grouping=True, out=' 12 345,67')\r | |
312 | self._test_format("%-6.2f", 345.67, grouping=True, out='345,67')\r | |
313 | self._test_format("%-7.2f", 345.67, grouping=True, out='345,67 ')\r | |
314 | self._test_format("%-8.2f", 12345.67, grouping=True, out='12 345,67')\r | |
315 | self._test_format("%-9.2f", 12345.67, grouping=True, out='12 345,67')\r | |
316 | self._test_format("%-10.2f", 12345.67, grouping=True, out='12 345,67 ')\r | |
317 | \r | |
318 | def test_integer_grouping(self):\r | |
319 | self._test_format("%d", 200, grouping=True, out='200')\r | |
320 | self._test_format("%d", 4200, grouping=True, out='4 200')\r | |
321 | \r | |
322 | def test_integer_grouping_and_padding(self):\r | |
323 | self._test_format("%4d", 4200, grouping=True, out='4 200')\r | |
324 | self._test_format("%5d", 4200, grouping=True, out='4 200')\r | |
325 | self._test_format("%10d", 4200, grouping=True, out='4 200'.rjust(10))\r | |
326 | self._test_format("%-4d", 4200, grouping=True, out='4 200')\r | |
327 | self._test_format("%-5d", 4200, grouping=True, out='4 200')\r | |
328 | self._test_format("%-10d", 4200, grouping=True, out='4 200'.ljust(10))\r | |
329 | \r | |
330 | def test_currency(self):\r | |
331 | euro = u'\u20ac'.encode('utf-8')\r | |
332 | self._test_currency(50000, "50000,00 " + euro)\r | |
333 | self._test_currency(50000, "50 000,00 " + euro, grouping=True)\r | |
334 | # XXX is the trailing space a bug?\r | |
335 | self._test_currency(50000, "50 000,00 EUR ",\r | |
336 | grouping=True, international=True)\r | |
337 | \r | |
338 | \r | |
339 | class TestStringMethods(BaseLocalizedTest):\r | |
340 | locale_type = locale.LC_CTYPE\r | |
341 | \r | |
342 | if sys.platform != 'sunos5' and not sys.platform.startswith("win"):\r | |
343 | # Test BSD Rune locale's bug for isctype functions.\r | |
344 | \r | |
345 | def test_isspace(self):\r | |
346 | self.assertEqual('\x20'.isspace(), True)\r | |
347 | self.assertEqual('\xa0'.isspace(), False)\r | |
348 | self.assertEqual('\xa1'.isspace(), False)\r | |
349 | \r | |
350 | def test_isalpha(self):\r | |
351 | self.assertEqual('\xc0'.isalpha(), False)\r | |
352 | \r | |
353 | def test_isalnum(self):\r | |
354 | self.assertEqual('\xc0'.isalnum(), False)\r | |
355 | \r | |
356 | def test_isupper(self):\r | |
357 | self.assertEqual('\xc0'.isupper(), False)\r | |
358 | \r | |
359 | def test_islower(self):\r | |
360 | self.assertEqual('\xc0'.islower(), False)\r | |
361 | \r | |
362 | def test_lower(self):\r | |
363 | self.assertEqual('\xcc\x85'.lower(), '\xcc\x85')\r | |
364 | \r | |
365 | def test_upper(self):\r | |
366 | self.assertEqual('\xed\x95\xa0'.upper(), '\xed\x95\xa0')\r | |
367 | \r | |
368 | def test_strip(self):\r | |
369 | self.assertEqual('\xed\x95\xa0'.strip(), '\xed\x95\xa0')\r | |
370 | \r | |
371 | def test_split(self):\r | |
372 | self.assertEqual('\xec\xa0\xbc'.split(), ['\xec\xa0\xbc'])\r | |
373 | \r | |
374 | \r | |
375 | class TestMiscellaneous(unittest.TestCase):\r | |
376 | def test_getpreferredencoding(self):\r | |
377 | # Invoke getpreferredencoding to make sure it does not cause exceptions.\r | |
378 | enc = locale.getpreferredencoding()\r | |
379 | if enc:\r | |
380 | # If encoding non-empty, make sure it is valid\r | |
381 | codecs.lookup(enc)\r | |
382 | \r | |
383 | if hasattr(locale, "strcoll"):\r | |
384 | def test_strcoll_3303(self):\r | |
385 | # test crasher from bug #3303\r | |
386 | self.assertRaises(TypeError, locale.strcoll, u"a", None)\r | |
387 | \r | |
388 | def test_setlocale_category(self):\r | |
389 | locale.setlocale(locale.LC_ALL)\r | |
390 | locale.setlocale(locale.LC_TIME)\r | |
391 | locale.setlocale(locale.LC_CTYPE)\r | |
392 | locale.setlocale(locale.LC_COLLATE)\r | |
393 | locale.setlocale(locale.LC_MONETARY)\r | |
394 | locale.setlocale(locale.LC_NUMERIC)\r | |
395 | \r | |
396 | # crasher from bug #7419\r | |
397 | self.assertRaises(locale.Error, locale.setlocale, 12345)\r | |
398 | \r | |
399 | \r | |
400 | def test_main():\r | |
401 | tests = [\r | |
402 | TestMiscellaneous,\r | |
403 | TestFormatPatternArg,\r | |
404 | TestLocaleFormatString,\r | |
405 | TestEnUSNumberFormatting,\r | |
406 | TestCNumberFormatting,\r | |
407 | TestFrFRNumberFormatting,\r | |
408 | ]\r | |
409 | # SkipTest can't be raised inside unittests, handle it manually instead\r | |
410 | try:\r | |
411 | get_enUS_locale()\r | |
412 | except unittest.SkipTest as e:\r | |
413 | if verbose:\r | |
414 | print "Some tests will be disabled: %s" % e\r | |
415 | else:\r | |
416 | tests += [TestNumberFormatting, TestStringMethods]\r | |
417 | run_unittest(*tests)\r | |
418 | \r | |
419 | if __name__ == '__main__':\r | |
420 | test_main()\r |