1 | n/a | from test.support import verbose, is_android |
---|
2 | n/a | import unittest |
---|
3 | n/a | import locale |
---|
4 | n/a | import sys |
---|
5 | n/a | import codecs |
---|
6 | n/a | |
---|
7 | n/a | class BaseLocalizedTest(unittest.TestCase): |
---|
8 | n/a | # |
---|
9 | n/a | # Base class for tests using a real locale |
---|
10 | n/a | # |
---|
11 | n/a | |
---|
12 | n/a | @classmethod |
---|
13 | n/a | def setUpClass(cls): |
---|
14 | n/a | if sys.platform == 'darwin': |
---|
15 | n/a | import os |
---|
16 | n/a | tlocs = ("en_US.UTF-8", "en_US.ISO8859-1", "en_US") |
---|
17 | n/a | if int(os.uname().release.split('.')[0]) < 10: |
---|
18 | n/a | # The locale test work fine on OSX 10.6, I (ronaldoussoren) |
---|
19 | n/a | # haven't had time yet to verify if tests work on OSX 10.5 |
---|
20 | n/a | # (10.4 is known to be bad) |
---|
21 | n/a | raise unittest.SkipTest("Locale support on MacOSX is minimal") |
---|
22 | n/a | elif sys.platform.startswith("win"): |
---|
23 | n/a | tlocs = ("En", "English") |
---|
24 | n/a | else: |
---|
25 | n/a | tlocs = ("en_US.UTF-8", "en_US.ISO8859-1", |
---|
26 | n/a | "en_US.US-ASCII", "en_US") |
---|
27 | n/a | try: |
---|
28 | n/a | oldlocale = locale.setlocale(locale.LC_NUMERIC) |
---|
29 | n/a | for tloc in tlocs: |
---|
30 | n/a | try: |
---|
31 | n/a | locale.setlocale(locale.LC_NUMERIC, tloc) |
---|
32 | n/a | except locale.Error: |
---|
33 | n/a | continue |
---|
34 | n/a | break |
---|
35 | n/a | else: |
---|
36 | n/a | raise unittest.SkipTest("Test locale not supported " |
---|
37 | n/a | "(tried %s)" % (', '.join(tlocs))) |
---|
38 | n/a | cls.enUS_locale = tloc |
---|
39 | n/a | finally: |
---|
40 | n/a | locale.setlocale(locale.LC_NUMERIC, oldlocale) |
---|
41 | n/a | |
---|
42 | n/a | def setUp(self): |
---|
43 | n/a | oldlocale = locale.setlocale(self.locale_type) |
---|
44 | n/a | self.addCleanup(locale.setlocale, self.locale_type, oldlocale) |
---|
45 | n/a | locale.setlocale(self.locale_type, self.enUS_locale) |
---|
46 | n/a | if verbose: |
---|
47 | n/a | print("testing with %r..." % self.enUS_locale, end=' ', flush=True) |
---|
48 | n/a | |
---|
49 | n/a | |
---|
50 | n/a | class BaseCookedTest(unittest.TestCase): |
---|
51 | n/a | # |
---|
52 | n/a | # Base class for tests using cooked localeconv() values |
---|
53 | n/a | # |
---|
54 | n/a | |
---|
55 | n/a | def setUp(self): |
---|
56 | n/a | locale._override_localeconv = self.cooked_values |
---|
57 | n/a | |
---|
58 | n/a | def tearDown(self): |
---|
59 | n/a | locale._override_localeconv = {} |
---|
60 | n/a | |
---|
61 | n/a | class CCookedTest(BaseCookedTest): |
---|
62 | n/a | # A cooked "C" locale |
---|
63 | n/a | |
---|
64 | n/a | cooked_values = { |
---|
65 | n/a | 'currency_symbol': '', |
---|
66 | n/a | 'decimal_point': '.', |
---|
67 | n/a | 'frac_digits': 127, |
---|
68 | n/a | 'grouping': [], |
---|
69 | n/a | 'int_curr_symbol': '', |
---|
70 | n/a | 'int_frac_digits': 127, |
---|
71 | n/a | 'mon_decimal_point': '', |
---|
72 | n/a | 'mon_grouping': [], |
---|
73 | n/a | 'mon_thousands_sep': '', |
---|
74 | n/a | 'n_cs_precedes': 127, |
---|
75 | n/a | 'n_sep_by_space': 127, |
---|
76 | n/a | 'n_sign_posn': 127, |
---|
77 | n/a | 'negative_sign': '', |
---|
78 | n/a | 'p_cs_precedes': 127, |
---|
79 | n/a | 'p_sep_by_space': 127, |
---|
80 | n/a | 'p_sign_posn': 127, |
---|
81 | n/a | 'positive_sign': '', |
---|
82 | n/a | 'thousands_sep': '' |
---|
83 | n/a | } |
---|
84 | n/a | |
---|
85 | n/a | class EnUSCookedTest(BaseCookedTest): |
---|
86 | n/a | # A cooked "en_US" locale |
---|
87 | n/a | |
---|
88 | n/a | cooked_values = { |
---|
89 | n/a | 'currency_symbol': '$', |
---|
90 | n/a | 'decimal_point': '.', |
---|
91 | n/a | 'frac_digits': 2, |
---|
92 | n/a | 'grouping': [3, 3, 0], |
---|
93 | n/a | 'int_curr_symbol': 'USD ', |
---|
94 | n/a | 'int_frac_digits': 2, |
---|
95 | n/a | 'mon_decimal_point': '.', |
---|
96 | n/a | 'mon_grouping': [3, 3, 0], |
---|
97 | n/a | 'mon_thousands_sep': ',', |
---|
98 | n/a | 'n_cs_precedes': 1, |
---|
99 | n/a | 'n_sep_by_space': 0, |
---|
100 | n/a | 'n_sign_posn': 1, |
---|
101 | n/a | 'negative_sign': '-', |
---|
102 | n/a | 'p_cs_precedes': 1, |
---|
103 | n/a | 'p_sep_by_space': 0, |
---|
104 | n/a | 'p_sign_posn': 1, |
---|
105 | n/a | 'positive_sign': '', |
---|
106 | n/a | 'thousands_sep': ',' |
---|
107 | n/a | } |
---|
108 | n/a | |
---|
109 | n/a | |
---|
110 | n/a | class FrFRCookedTest(BaseCookedTest): |
---|
111 | n/a | # A cooked "fr_FR" locale with a space character as decimal separator |
---|
112 | n/a | # and a non-ASCII currency symbol. |
---|
113 | n/a | |
---|
114 | n/a | cooked_values = { |
---|
115 | n/a | 'currency_symbol': '\u20ac', |
---|
116 | n/a | 'decimal_point': ',', |
---|
117 | n/a | 'frac_digits': 2, |
---|
118 | n/a | 'grouping': [3, 3, 0], |
---|
119 | n/a | 'int_curr_symbol': 'EUR ', |
---|
120 | n/a | 'int_frac_digits': 2, |
---|
121 | n/a | 'mon_decimal_point': ',', |
---|
122 | n/a | 'mon_grouping': [3, 3, 0], |
---|
123 | n/a | 'mon_thousands_sep': ' ', |
---|
124 | n/a | 'n_cs_precedes': 0, |
---|
125 | n/a | 'n_sep_by_space': 1, |
---|
126 | n/a | 'n_sign_posn': 1, |
---|
127 | n/a | 'negative_sign': '-', |
---|
128 | n/a | 'p_cs_precedes': 0, |
---|
129 | n/a | 'p_sep_by_space': 1, |
---|
130 | n/a | 'p_sign_posn': 1, |
---|
131 | n/a | 'positive_sign': '', |
---|
132 | n/a | 'thousands_sep': ' ' |
---|
133 | n/a | } |
---|
134 | n/a | |
---|
135 | n/a | |
---|
136 | n/a | class BaseFormattingTest(object): |
---|
137 | n/a | # |
---|
138 | n/a | # Utility functions for formatting tests |
---|
139 | n/a | # |
---|
140 | n/a | |
---|
141 | n/a | def _test_formatfunc(self, format, value, out, func, **format_opts): |
---|
142 | n/a | self.assertEqual( |
---|
143 | n/a | func(format, value, **format_opts), out) |
---|
144 | n/a | |
---|
145 | n/a | def _test_format(self, format, value, out, **format_opts): |
---|
146 | n/a | self._test_formatfunc(format, value, out, |
---|
147 | n/a | func=locale.format, **format_opts) |
---|
148 | n/a | |
---|
149 | n/a | def _test_format_string(self, format, value, out, **format_opts): |
---|
150 | n/a | self._test_formatfunc(format, value, out, |
---|
151 | n/a | func=locale.format_string, **format_opts) |
---|
152 | n/a | |
---|
153 | n/a | def _test_currency(self, value, out, **format_opts): |
---|
154 | n/a | self.assertEqual(locale.currency(value, **format_opts), out) |
---|
155 | n/a | |
---|
156 | n/a | |
---|
157 | n/a | class EnUSNumberFormatting(BaseFormattingTest): |
---|
158 | n/a | # XXX there is a grouping + padding bug when the thousands separator |
---|
159 | n/a | # is empty but the grouping array contains values (e.g. Solaris 10) |
---|
160 | n/a | |
---|
161 | n/a | def setUp(self): |
---|
162 | n/a | self.sep = locale.localeconv()['thousands_sep'] |
---|
163 | n/a | |
---|
164 | n/a | def test_grouping(self): |
---|
165 | n/a | self._test_format("%f", 1024, grouping=1, out='1%s024.000000' % self.sep) |
---|
166 | n/a | self._test_format("%f", 102, grouping=1, out='102.000000') |
---|
167 | n/a | self._test_format("%f", -42, grouping=1, out='-42.000000') |
---|
168 | n/a | self._test_format("%+f", -42, grouping=1, out='-42.000000') |
---|
169 | n/a | |
---|
170 | n/a | def test_grouping_and_padding(self): |
---|
171 | n/a | self._test_format("%20.f", -42, grouping=1, out='-42'.rjust(20)) |
---|
172 | n/a | if self.sep: |
---|
173 | n/a | self._test_format("%+10.f", -4200, grouping=1, |
---|
174 | n/a | out=('-4%s200' % self.sep).rjust(10)) |
---|
175 | n/a | self._test_format("%-10.f", -4200, grouping=1, |
---|
176 | n/a | out=('-4%s200' % self.sep).ljust(10)) |
---|
177 | n/a | |
---|
178 | n/a | def test_integer_grouping(self): |
---|
179 | n/a | self._test_format("%d", 4200, grouping=True, out='4%s200' % self.sep) |
---|
180 | n/a | self._test_format("%+d", 4200, grouping=True, out='+4%s200' % self.sep) |
---|
181 | n/a | self._test_format("%+d", -4200, grouping=True, out='-4%s200' % self.sep) |
---|
182 | n/a | |
---|
183 | n/a | def test_integer_grouping_and_padding(self): |
---|
184 | n/a | self._test_format("%10d", 4200, grouping=True, |
---|
185 | n/a | out=('4%s200' % self.sep).rjust(10)) |
---|
186 | n/a | self._test_format("%-10d", -4200, grouping=True, |
---|
187 | n/a | out=('-4%s200' % self.sep).ljust(10)) |
---|
188 | n/a | |
---|
189 | n/a | def test_simple(self): |
---|
190 | n/a | self._test_format("%f", 1024, grouping=0, out='1024.000000') |
---|
191 | n/a | self._test_format("%f", 102, grouping=0, out='102.000000') |
---|
192 | n/a | self._test_format("%f", -42, grouping=0, out='-42.000000') |
---|
193 | n/a | self._test_format("%+f", -42, grouping=0, out='-42.000000') |
---|
194 | n/a | |
---|
195 | n/a | def test_padding(self): |
---|
196 | n/a | self._test_format("%20.f", -42, grouping=0, out='-42'.rjust(20)) |
---|
197 | n/a | self._test_format("%+10.f", -4200, grouping=0, out='-4200'.rjust(10)) |
---|
198 | n/a | self._test_format("%-10.f", 4200, grouping=0, out='4200'.ljust(10)) |
---|
199 | n/a | |
---|
200 | n/a | def test_complex_formatting(self): |
---|
201 | n/a | # Spaces in formatting string |
---|
202 | n/a | self._test_format_string("One million is %i", 1000000, grouping=1, |
---|
203 | n/a | out='One million is 1%s000%s000' % (self.sep, self.sep)) |
---|
204 | n/a | self._test_format_string("One million is %i", 1000000, grouping=1, |
---|
205 | n/a | out='One million is 1%s000%s000' % (self.sep, self.sep)) |
---|
206 | n/a | # Dots in formatting string |
---|
207 | n/a | self._test_format_string(".%f.", 1000.0, out='.1000.000000.') |
---|
208 | n/a | # Padding |
---|
209 | n/a | if self.sep: |
---|
210 | n/a | self._test_format_string("--> %10.2f", 4200, grouping=1, |
---|
211 | n/a | out='--> ' + ('4%s200.00' % self.sep).rjust(10)) |
---|
212 | n/a | # Asterisk formats |
---|
213 | n/a | self._test_format_string("%10.*f", (2, 1000), grouping=0, |
---|
214 | n/a | out='1000.00'.rjust(10)) |
---|
215 | n/a | if self.sep: |
---|
216 | n/a | self._test_format_string("%*.*f", (10, 2, 1000), grouping=1, |
---|
217 | n/a | out=('1%s000.00' % self.sep).rjust(10)) |
---|
218 | n/a | # Test more-in-one |
---|
219 | n/a | if self.sep: |
---|
220 | n/a | self._test_format_string("int %i float %.2f str %s", |
---|
221 | n/a | (1000, 1000.0, 'str'), grouping=1, |
---|
222 | n/a | out='int 1%s000 float 1%s000.00 str str' % |
---|
223 | n/a | (self.sep, self.sep)) |
---|
224 | n/a | |
---|
225 | n/a | |
---|
226 | n/a | class TestFormatPatternArg(unittest.TestCase): |
---|
227 | n/a | # Test handling of pattern argument of format |
---|
228 | n/a | |
---|
229 | n/a | def test_onlyOnePattern(self): |
---|
230 | n/a | # Issue 2522: accept exactly one % pattern, and no extra chars. |
---|
231 | n/a | self.assertRaises(ValueError, locale.format, "%f\n", 'foo') |
---|
232 | n/a | self.assertRaises(ValueError, locale.format, "%f\r", 'foo') |
---|
233 | n/a | self.assertRaises(ValueError, locale.format, "%f\r\n", 'foo') |
---|
234 | n/a | self.assertRaises(ValueError, locale.format, " %f", 'foo') |
---|
235 | n/a | self.assertRaises(ValueError, locale.format, "%fg", 'foo') |
---|
236 | n/a | self.assertRaises(ValueError, locale.format, "%^g", 'foo') |
---|
237 | n/a | self.assertRaises(ValueError, locale.format, "%f%%", 'foo') |
---|
238 | n/a | |
---|
239 | n/a | |
---|
240 | n/a | class TestLocaleFormatString(unittest.TestCase): |
---|
241 | n/a | """General tests on locale.format_string""" |
---|
242 | n/a | |
---|
243 | n/a | def test_percent_escape(self): |
---|
244 | n/a | self.assertEqual(locale.format_string('%f%%', 1.0), '%f%%' % 1.0) |
---|
245 | n/a | self.assertEqual(locale.format_string('%d %f%%d', (1, 1.0)), |
---|
246 | n/a | '%d %f%%d' % (1, 1.0)) |
---|
247 | n/a | self.assertEqual(locale.format_string('%(foo)s %%d', {'foo': 'bar'}), |
---|
248 | n/a | ('%(foo)s %%d' % {'foo': 'bar'})) |
---|
249 | n/a | |
---|
250 | n/a | def test_mapping(self): |
---|
251 | n/a | self.assertEqual(locale.format_string('%(foo)s bing.', {'foo': 'bar'}), |
---|
252 | n/a | ('%(foo)s bing.' % {'foo': 'bar'})) |
---|
253 | n/a | self.assertEqual(locale.format_string('%(foo)s', {'foo': 'bar'}), |
---|
254 | n/a | ('%(foo)s' % {'foo': 'bar'})) |
---|
255 | n/a | |
---|
256 | n/a | |
---|
257 | n/a | |
---|
258 | n/a | class TestNumberFormatting(BaseLocalizedTest, EnUSNumberFormatting): |
---|
259 | n/a | # Test number formatting with a real English locale. |
---|
260 | n/a | |
---|
261 | n/a | locale_type = locale.LC_NUMERIC |
---|
262 | n/a | |
---|
263 | n/a | def setUp(self): |
---|
264 | n/a | BaseLocalizedTest.setUp(self) |
---|
265 | n/a | EnUSNumberFormatting.setUp(self) |
---|
266 | n/a | |
---|
267 | n/a | |
---|
268 | n/a | class TestEnUSNumberFormatting(EnUSCookedTest, EnUSNumberFormatting): |
---|
269 | n/a | # Test number formatting with a cooked "en_US" locale. |
---|
270 | n/a | |
---|
271 | n/a | def setUp(self): |
---|
272 | n/a | EnUSCookedTest.setUp(self) |
---|
273 | n/a | EnUSNumberFormatting.setUp(self) |
---|
274 | n/a | |
---|
275 | n/a | def test_currency(self): |
---|
276 | n/a | self._test_currency(50000, "$50000.00") |
---|
277 | n/a | self._test_currency(50000, "$50,000.00", grouping=True) |
---|
278 | n/a | self._test_currency(50000, "USD 50,000.00", |
---|
279 | n/a | grouping=True, international=True) |
---|
280 | n/a | |
---|
281 | n/a | |
---|
282 | n/a | class TestCNumberFormatting(CCookedTest, BaseFormattingTest): |
---|
283 | n/a | # Test number formatting with a cooked "C" locale. |
---|
284 | n/a | |
---|
285 | n/a | def test_grouping(self): |
---|
286 | n/a | self._test_format("%.2f", 12345.67, grouping=True, out='12345.67') |
---|
287 | n/a | |
---|
288 | n/a | def test_grouping_and_padding(self): |
---|
289 | n/a | self._test_format("%9.2f", 12345.67, grouping=True, out=' 12345.67') |
---|
290 | n/a | |
---|
291 | n/a | |
---|
292 | n/a | class TestFrFRNumberFormatting(FrFRCookedTest, BaseFormattingTest): |
---|
293 | n/a | # Test number formatting with a cooked "fr_FR" locale. |
---|
294 | n/a | |
---|
295 | n/a | def test_decimal_point(self): |
---|
296 | n/a | self._test_format("%.2f", 12345.67, out='12345,67') |
---|
297 | n/a | |
---|
298 | n/a | def test_grouping(self): |
---|
299 | n/a | self._test_format("%.2f", 345.67, grouping=True, out='345,67') |
---|
300 | n/a | self._test_format("%.2f", 12345.67, grouping=True, out='12 345,67') |
---|
301 | n/a | |
---|
302 | n/a | def test_grouping_and_padding(self): |
---|
303 | n/a | self._test_format("%6.2f", 345.67, grouping=True, out='345,67') |
---|
304 | n/a | self._test_format("%7.2f", 345.67, grouping=True, out=' 345,67') |
---|
305 | n/a | self._test_format("%8.2f", 12345.67, grouping=True, out='12 345,67') |
---|
306 | n/a | self._test_format("%9.2f", 12345.67, grouping=True, out='12 345,67') |
---|
307 | n/a | self._test_format("%10.2f", 12345.67, grouping=True, out=' 12 345,67') |
---|
308 | n/a | self._test_format("%-6.2f", 345.67, grouping=True, out='345,67') |
---|
309 | n/a | self._test_format("%-7.2f", 345.67, grouping=True, out='345,67 ') |
---|
310 | n/a | self._test_format("%-8.2f", 12345.67, grouping=True, out='12 345,67') |
---|
311 | n/a | self._test_format("%-9.2f", 12345.67, grouping=True, out='12 345,67') |
---|
312 | n/a | self._test_format("%-10.2f", 12345.67, grouping=True, out='12 345,67 ') |
---|
313 | n/a | |
---|
314 | n/a | def test_integer_grouping(self): |
---|
315 | n/a | self._test_format("%d", 200, grouping=True, out='200') |
---|
316 | n/a | self._test_format("%d", 4200, grouping=True, out='4 200') |
---|
317 | n/a | |
---|
318 | n/a | def test_integer_grouping_and_padding(self): |
---|
319 | n/a | self._test_format("%4d", 4200, grouping=True, out='4 200') |
---|
320 | n/a | self._test_format("%5d", 4200, grouping=True, out='4 200') |
---|
321 | n/a | self._test_format("%10d", 4200, grouping=True, out='4 200'.rjust(10)) |
---|
322 | n/a | self._test_format("%-4d", 4200, grouping=True, out='4 200') |
---|
323 | n/a | self._test_format("%-5d", 4200, grouping=True, out='4 200') |
---|
324 | n/a | self._test_format("%-10d", 4200, grouping=True, out='4 200'.ljust(10)) |
---|
325 | n/a | |
---|
326 | n/a | def test_currency(self): |
---|
327 | n/a | euro = '\u20ac' |
---|
328 | n/a | self._test_currency(50000, "50000,00 " + euro) |
---|
329 | n/a | self._test_currency(50000, "50 000,00 " + euro, grouping=True) |
---|
330 | n/a | # XXX is the trailing space a bug? |
---|
331 | n/a | self._test_currency(50000, "50 000,00 EUR ", |
---|
332 | n/a | grouping=True, international=True) |
---|
333 | n/a | |
---|
334 | n/a | |
---|
335 | n/a | class TestCollation(unittest.TestCase): |
---|
336 | n/a | # Test string collation functions |
---|
337 | n/a | |
---|
338 | n/a | def test_strcoll(self): |
---|
339 | n/a | self.assertLess(locale.strcoll('a', 'b'), 0) |
---|
340 | n/a | self.assertEqual(locale.strcoll('a', 'a'), 0) |
---|
341 | n/a | self.assertGreater(locale.strcoll('b', 'a'), 0) |
---|
342 | n/a | |
---|
343 | n/a | def test_strxfrm(self): |
---|
344 | n/a | self.assertLess(locale.strxfrm('a'), locale.strxfrm('b')) |
---|
345 | n/a | |
---|
346 | n/a | |
---|
347 | n/a | class TestEnUSCollation(BaseLocalizedTest, TestCollation): |
---|
348 | n/a | # Test string collation functions with a real English locale |
---|
349 | n/a | |
---|
350 | n/a | locale_type = locale.LC_ALL |
---|
351 | n/a | |
---|
352 | n/a | def setUp(self): |
---|
353 | n/a | enc = codecs.lookup(locale.getpreferredencoding(False) or 'ascii').name |
---|
354 | n/a | if enc not in ('utf-8', 'iso8859-1', 'cp1252'): |
---|
355 | n/a | raise unittest.SkipTest('encoding not suitable') |
---|
356 | n/a | if enc != 'iso8859-1' and (sys.platform == 'darwin' or is_android or |
---|
357 | n/a | sys.platform.startswith('freebsd')): |
---|
358 | n/a | raise unittest.SkipTest('wcscoll/wcsxfrm have known bugs') |
---|
359 | n/a | BaseLocalizedTest.setUp(self) |
---|
360 | n/a | |
---|
361 | n/a | def test_strcoll_with_diacritic(self): |
---|
362 | n/a | self.assertLess(locale.strcoll('Ã ', 'b'), 0) |
---|
363 | n/a | |
---|
364 | n/a | def test_strxfrm_with_diacritic(self): |
---|
365 | n/a | self.assertLess(locale.strxfrm('Ã '), locale.strxfrm('b')) |
---|
366 | n/a | |
---|
367 | n/a | |
---|
368 | n/a | class NormalizeTest(unittest.TestCase): |
---|
369 | n/a | def check(self, localename, expected): |
---|
370 | n/a | self.assertEqual(locale.normalize(localename), expected, msg=localename) |
---|
371 | n/a | |
---|
372 | n/a | def test_locale_alias(self): |
---|
373 | n/a | for localename, alias in locale.locale_alias.items(): |
---|
374 | n/a | with self.subTest(locale=(localename, alias)): |
---|
375 | n/a | self.check(localename, alias) |
---|
376 | n/a | |
---|
377 | n/a | def test_empty(self): |
---|
378 | n/a | self.check('', '') |
---|
379 | n/a | |
---|
380 | n/a | def test_c(self): |
---|
381 | n/a | self.check('c', 'C') |
---|
382 | n/a | self.check('posix', 'C') |
---|
383 | n/a | |
---|
384 | n/a | def test_english(self): |
---|
385 | n/a | self.check('en', 'en_US.ISO8859-1') |
---|
386 | n/a | self.check('EN', 'en_US.ISO8859-1') |
---|
387 | n/a | self.check('en.iso88591', 'en_US.ISO8859-1') |
---|
388 | n/a | self.check('en_US', 'en_US.ISO8859-1') |
---|
389 | n/a | self.check('en_us', 'en_US.ISO8859-1') |
---|
390 | n/a | self.check('en_GB', 'en_GB.ISO8859-1') |
---|
391 | n/a | self.check('en_US.UTF-8', 'en_US.UTF-8') |
---|
392 | n/a | self.check('en_US.utf8', 'en_US.UTF-8') |
---|
393 | n/a | self.check('en_US:UTF-8', 'en_US.UTF-8') |
---|
394 | n/a | self.check('en_US.ISO8859-1', 'en_US.ISO8859-1') |
---|
395 | n/a | self.check('en_US.US-ASCII', 'en_US.ISO8859-1') |
---|
396 | n/a | self.check('en_US.88591', 'en_US.ISO8859-1') |
---|
397 | n/a | self.check('en_US.885915', 'en_US.ISO8859-15') |
---|
398 | n/a | self.check('english', 'en_EN.ISO8859-1') |
---|
399 | n/a | self.check('english_uk.ascii', 'en_GB.ISO8859-1') |
---|
400 | n/a | |
---|
401 | n/a | def test_hyphenated_encoding(self): |
---|
402 | n/a | self.check('az_AZ.iso88599e', 'az_AZ.ISO8859-9E') |
---|
403 | n/a | self.check('az_AZ.ISO8859-9E', 'az_AZ.ISO8859-9E') |
---|
404 | n/a | self.check('tt_RU.koi8c', 'tt_RU.KOI8-C') |
---|
405 | n/a | self.check('tt_RU.KOI8-C', 'tt_RU.KOI8-C') |
---|
406 | n/a | self.check('lo_LA.cp1133', 'lo_LA.IBM-CP1133') |
---|
407 | n/a | self.check('lo_LA.ibmcp1133', 'lo_LA.IBM-CP1133') |
---|
408 | n/a | self.check('lo_LA.IBM-CP1133', 'lo_LA.IBM-CP1133') |
---|
409 | n/a | self.check('uk_ua.microsoftcp1251', 'uk_UA.CP1251') |
---|
410 | n/a | self.check('uk_ua.microsoft-cp1251', 'uk_UA.CP1251') |
---|
411 | n/a | self.check('ka_ge.georgianacademy', 'ka_GE.GEORGIAN-ACADEMY') |
---|
412 | n/a | self.check('ka_GE.GEORGIAN-ACADEMY', 'ka_GE.GEORGIAN-ACADEMY') |
---|
413 | n/a | self.check('cs_CZ.iso88592', 'cs_CZ.ISO8859-2') |
---|
414 | n/a | self.check('cs_CZ.ISO8859-2', 'cs_CZ.ISO8859-2') |
---|
415 | n/a | |
---|
416 | n/a | def test_euro_modifier(self): |
---|
417 | n/a | self.check('de_DE@euro', 'de_DE.ISO8859-15') |
---|
418 | n/a | self.check('en_US.ISO8859-15@euro', 'en_US.ISO8859-15') |
---|
419 | n/a | self.check('de_DE.utf8@euro', 'de_DE.UTF-8') |
---|
420 | n/a | |
---|
421 | n/a | def test_latin_modifier(self): |
---|
422 | n/a | self.check('be_BY.UTF-8@latin', 'be_BY.UTF-8@latin') |
---|
423 | n/a | self.check('sr_RS.UTF-8@latin', 'sr_RS.UTF-8@latin') |
---|
424 | n/a | self.check('sr_RS.UTF-8@latn', 'sr_RS.UTF-8@latin') |
---|
425 | n/a | |
---|
426 | n/a | def test_valencia_modifier(self): |
---|
427 | n/a | self.check('ca_ES.UTF-8@valencia', 'ca_ES.UTF-8@valencia') |
---|
428 | n/a | self.check('ca_ES@valencia', 'ca_ES.ISO8859-15@valencia') |
---|
429 | n/a | self.check('ca@valencia', 'ca_ES.ISO8859-1@valencia') |
---|
430 | n/a | |
---|
431 | n/a | def test_devanagari_modifier(self): |
---|
432 | n/a | self.check('ks_IN.UTF-8@devanagari', 'ks_IN.UTF-8@devanagari') |
---|
433 | n/a | self.check('ks_IN@devanagari', 'ks_IN.UTF-8@devanagari') |
---|
434 | n/a | self.check('ks@devanagari', 'ks_IN.UTF-8@devanagari') |
---|
435 | n/a | self.check('ks_IN.UTF-8', 'ks_IN.UTF-8') |
---|
436 | n/a | self.check('ks_IN', 'ks_IN.UTF-8') |
---|
437 | n/a | self.check('ks', 'ks_IN.UTF-8') |
---|
438 | n/a | self.check('sd_IN.UTF-8@devanagari', 'sd_IN.UTF-8@devanagari') |
---|
439 | n/a | self.check('sd_IN@devanagari', 'sd_IN.UTF-8@devanagari') |
---|
440 | n/a | self.check('sd@devanagari', 'sd_IN.UTF-8@devanagari') |
---|
441 | n/a | self.check('sd_IN.UTF-8', 'sd_IN.UTF-8') |
---|
442 | n/a | self.check('sd_IN', 'sd_IN.UTF-8') |
---|
443 | n/a | self.check('sd', 'sd_IN.UTF-8') |
---|
444 | n/a | |
---|
445 | n/a | def test_euc_encoding(self): |
---|
446 | n/a | self.check('ja_jp.euc', 'ja_JP.eucJP') |
---|
447 | n/a | self.check('ja_jp.eucjp', 'ja_JP.eucJP') |
---|
448 | n/a | self.check('ko_kr.euc', 'ko_KR.eucKR') |
---|
449 | n/a | self.check('ko_kr.euckr', 'ko_KR.eucKR') |
---|
450 | n/a | self.check('zh_cn.euc', 'zh_CN.eucCN') |
---|
451 | n/a | self.check('zh_tw.euc', 'zh_TW.eucTW') |
---|
452 | n/a | self.check('zh_tw.euctw', 'zh_TW.eucTW') |
---|
453 | n/a | |
---|
454 | n/a | def test_japanese(self): |
---|
455 | n/a | self.check('ja', 'ja_JP.eucJP') |
---|
456 | n/a | self.check('ja.jis', 'ja_JP.JIS7') |
---|
457 | n/a | self.check('ja.sjis', 'ja_JP.SJIS') |
---|
458 | n/a | self.check('ja_jp', 'ja_JP.eucJP') |
---|
459 | n/a | self.check('ja_jp.ajec', 'ja_JP.eucJP') |
---|
460 | n/a | self.check('ja_jp.euc', 'ja_JP.eucJP') |
---|
461 | n/a | self.check('ja_jp.eucjp', 'ja_JP.eucJP') |
---|
462 | n/a | self.check('ja_jp.iso-2022-jp', 'ja_JP.JIS7') |
---|
463 | n/a | self.check('ja_jp.iso2022jp', 'ja_JP.JIS7') |
---|
464 | n/a | self.check('ja_jp.jis', 'ja_JP.JIS7') |
---|
465 | n/a | self.check('ja_jp.jis7', 'ja_JP.JIS7') |
---|
466 | n/a | self.check('ja_jp.mscode', 'ja_JP.SJIS') |
---|
467 | n/a | self.check('ja_jp.pck', 'ja_JP.SJIS') |
---|
468 | n/a | self.check('ja_jp.sjis', 'ja_JP.SJIS') |
---|
469 | n/a | self.check('ja_jp.ujis', 'ja_JP.eucJP') |
---|
470 | n/a | self.check('ja_jp.utf8', 'ja_JP.UTF-8') |
---|
471 | n/a | self.check('japan', 'ja_JP.eucJP') |
---|
472 | n/a | self.check('japanese', 'ja_JP.eucJP') |
---|
473 | n/a | self.check('japanese-euc', 'ja_JP.eucJP') |
---|
474 | n/a | self.check('japanese.euc', 'ja_JP.eucJP') |
---|
475 | n/a | self.check('japanese.sjis', 'ja_JP.SJIS') |
---|
476 | n/a | self.check('jp_jp', 'ja_JP.eucJP') |
---|
477 | n/a | |
---|
478 | n/a | |
---|
479 | n/a | class TestMiscellaneous(unittest.TestCase): |
---|
480 | n/a | def test_getpreferredencoding(self): |
---|
481 | n/a | # Invoke getpreferredencoding to make sure it does not cause exceptions. |
---|
482 | n/a | enc = locale.getpreferredencoding() |
---|
483 | n/a | if enc: |
---|
484 | n/a | # If encoding non-empty, make sure it is valid |
---|
485 | n/a | codecs.lookup(enc) |
---|
486 | n/a | |
---|
487 | n/a | def test_strcoll_3303(self): |
---|
488 | n/a | # test crasher from bug #3303 |
---|
489 | n/a | self.assertRaises(TypeError, locale.strcoll, "a", None) |
---|
490 | n/a | self.assertRaises(TypeError, locale.strcoll, b"a", None) |
---|
491 | n/a | |
---|
492 | n/a | def test_setlocale_category(self): |
---|
493 | n/a | locale.setlocale(locale.LC_ALL) |
---|
494 | n/a | locale.setlocale(locale.LC_TIME) |
---|
495 | n/a | locale.setlocale(locale.LC_CTYPE) |
---|
496 | n/a | locale.setlocale(locale.LC_COLLATE) |
---|
497 | n/a | locale.setlocale(locale.LC_MONETARY) |
---|
498 | n/a | locale.setlocale(locale.LC_NUMERIC) |
---|
499 | n/a | |
---|
500 | n/a | # crasher from bug #7419 |
---|
501 | n/a | self.assertRaises(locale.Error, locale.setlocale, 12345) |
---|
502 | n/a | |
---|
503 | n/a | def test_getsetlocale_issue1813(self): |
---|
504 | n/a | # Issue #1813: setting and getting the locale under a Turkish locale |
---|
505 | n/a | oldlocale = locale.setlocale(locale.LC_CTYPE) |
---|
506 | n/a | self.addCleanup(locale.setlocale, locale.LC_CTYPE, oldlocale) |
---|
507 | n/a | try: |
---|
508 | n/a | locale.setlocale(locale.LC_CTYPE, 'tr_TR') |
---|
509 | n/a | except locale.Error: |
---|
510 | n/a | # Unsupported locale on this system |
---|
511 | n/a | self.skipTest('test needs Turkish locale') |
---|
512 | n/a | loc = locale.getlocale(locale.LC_CTYPE) |
---|
513 | n/a | if verbose: |
---|
514 | n/a | print('testing with %a' % (loc,), end=' ', flush=True) |
---|
515 | n/a | locale.setlocale(locale.LC_CTYPE, loc) |
---|
516 | n/a | self.assertEqual(loc, locale.getlocale(locale.LC_CTYPE)) |
---|
517 | n/a | |
---|
518 | n/a | def test_invalid_locale_format_in_localetuple(self): |
---|
519 | n/a | with self.assertRaises(TypeError): |
---|
520 | n/a | locale.setlocale(locale.LC_ALL, b'fi_FI') |
---|
521 | n/a | |
---|
522 | n/a | def test_invalid_iterable_in_localetuple(self): |
---|
523 | n/a | with self.assertRaises(TypeError): |
---|
524 | n/a | locale.setlocale(locale.LC_ALL, (b'not', b'valid')) |
---|
525 | n/a | |
---|
526 | n/a | |
---|
527 | n/a | class BaseDelocalizeTest(BaseLocalizedTest): |
---|
528 | n/a | |
---|
529 | n/a | def _test_delocalize(self, value, out): |
---|
530 | n/a | self.assertEqual(locale.delocalize(value), out) |
---|
531 | n/a | |
---|
532 | n/a | def _test_atof(self, value, out): |
---|
533 | n/a | self.assertEqual(locale.atof(value), out) |
---|
534 | n/a | |
---|
535 | n/a | def _test_atoi(self, value, out): |
---|
536 | n/a | self.assertEqual(locale.atoi(value), out) |
---|
537 | n/a | |
---|
538 | n/a | |
---|
539 | n/a | class TestEnUSDelocalize(EnUSCookedTest, BaseDelocalizeTest): |
---|
540 | n/a | |
---|
541 | n/a | def test_delocalize(self): |
---|
542 | n/a | self._test_delocalize('50000.00', '50000.00') |
---|
543 | n/a | self._test_delocalize('50,000.00', '50000.00') |
---|
544 | n/a | |
---|
545 | n/a | def test_atof(self): |
---|
546 | n/a | self._test_atof('50000.00', 50000.) |
---|
547 | n/a | self._test_atof('50,000.00', 50000.) |
---|
548 | n/a | |
---|
549 | n/a | def test_atoi(self): |
---|
550 | n/a | self._test_atoi('50000', 50000) |
---|
551 | n/a | self._test_atoi('50,000', 50000) |
---|
552 | n/a | |
---|
553 | n/a | |
---|
554 | n/a | class TestCDelocalizeTest(CCookedTest, BaseDelocalizeTest): |
---|
555 | n/a | |
---|
556 | n/a | def test_delocalize(self): |
---|
557 | n/a | self._test_delocalize('50000.00', '50000.00') |
---|
558 | n/a | |
---|
559 | n/a | def test_atof(self): |
---|
560 | n/a | self._test_atof('50000.00', 50000.) |
---|
561 | n/a | |
---|
562 | n/a | def test_atoi(self): |
---|
563 | n/a | self._test_atoi('50000', 50000) |
---|
564 | n/a | |
---|
565 | n/a | |
---|
566 | n/a | class TestfrFRDelocalizeTest(FrFRCookedTest, BaseDelocalizeTest): |
---|
567 | n/a | |
---|
568 | n/a | def test_delocalize(self): |
---|
569 | n/a | self._test_delocalize('50000,00', '50000.00') |
---|
570 | n/a | self._test_delocalize('50 000,00', '50000.00') |
---|
571 | n/a | |
---|
572 | n/a | def test_atof(self): |
---|
573 | n/a | self._test_atof('50000,00', 50000.) |
---|
574 | n/a | self._test_atof('50 000,00', 50000.) |
---|
575 | n/a | |
---|
576 | n/a | def test_atoi(self): |
---|
577 | n/a | self._test_atoi('50000', 50000) |
---|
578 | n/a | self._test_atoi('50 000', 50000) |
---|
579 | n/a | |
---|
580 | n/a | |
---|
581 | n/a | if __name__ == '__main__': |
---|
582 | n/a | unittest.main() |
---|