|
1 from test.test_support import run_unittest, verbose, TestSkipped |
|
2 import unittest |
|
3 import locale |
|
4 import sys |
|
5 import codecs |
|
6 |
|
7 |
|
8 enUS_locale = None |
|
9 |
|
10 def get_enUS_locale(): |
|
11 global enUS_locale |
|
12 if sys.platform == 'darwin': |
|
13 raise TestSkipped("Locale support on MacOSX is minimal") |
|
14 if sys.platform.startswith("win"): |
|
15 tlocs = ("En", "English") |
|
16 else: |
|
17 tlocs = ("en_US.UTF-8", "en_US.US-ASCII", "en_US") |
|
18 oldlocale = locale.setlocale(locale.LC_NUMERIC) |
|
19 for tloc in tlocs: |
|
20 try: |
|
21 locale.setlocale(locale.LC_NUMERIC, tloc) |
|
22 except locale.Error: |
|
23 continue |
|
24 break |
|
25 else: |
|
26 raise TestSkipped( |
|
27 "Test locale not supported (tried %s)" % (', '.join(tlocs))) |
|
28 enUS_locale = tloc |
|
29 locale.setlocale(locale.LC_NUMERIC, oldlocale) |
|
30 |
|
31 |
|
32 class BaseLocalizedTest(unittest.TestCase): |
|
33 # |
|
34 # Base class for tests using a real locale |
|
35 # |
|
36 |
|
37 def setUp(self): |
|
38 self.oldlocale = locale.setlocale(self.locale_type) |
|
39 locale.setlocale(self.locale_type, enUS_locale) |
|
40 if verbose: |
|
41 print "testing with \"%s\"..." % enUS_locale, |
|
42 |
|
43 def tearDown(self): |
|
44 locale.setlocale(self.locale_type, self.oldlocale) |
|
45 |
|
46 |
|
47 class BaseCookedTest(unittest.TestCase): |
|
48 # |
|
49 # Base class for tests using cooked localeconv() values |
|
50 # |
|
51 |
|
52 def setUp(self): |
|
53 locale._override_localeconv = self.cooked_values |
|
54 |
|
55 def tearDown(self): |
|
56 locale._override_localeconv = {} |
|
57 |
|
58 |
|
59 class CCookedTest(BaseCookedTest): |
|
60 # A cooked "C" locale |
|
61 |
|
62 cooked_values = { |
|
63 'currency_symbol': '', |
|
64 'decimal_point': '.', |
|
65 'frac_digits': 127, |
|
66 'grouping': [], |
|
67 'int_curr_symbol': '', |
|
68 'int_frac_digits': 127, |
|
69 'mon_decimal_point': '', |
|
70 'mon_grouping': [], |
|
71 'mon_thousands_sep': '', |
|
72 'n_cs_precedes': 127, |
|
73 'n_sep_by_space': 127, |
|
74 'n_sign_posn': 127, |
|
75 'negative_sign': '', |
|
76 'p_cs_precedes': 127, |
|
77 'p_sep_by_space': 127, |
|
78 'p_sign_posn': 127, |
|
79 'positive_sign': '', |
|
80 'thousands_sep': '' |
|
81 } |
|
82 |
|
83 class EnUSCookedTest(BaseCookedTest): |
|
84 # A cooked "en_US" locale |
|
85 |
|
86 cooked_values = { |
|
87 'currency_symbol': '$', |
|
88 'decimal_point': '.', |
|
89 'frac_digits': 2, |
|
90 'grouping': [3, 3, 0], |
|
91 'int_curr_symbol': 'USD ', |
|
92 'int_frac_digits': 2, |
|
93 'mon_decimal_point': '.', |
|
94 'mon_grouping': [3, 3, 0], |
|
95 'mon_thousands_sep': ',', |
|
96 'n_cs_precedes': 1, |
|
97 'n_sep_by_space': 0, |
|
98 'n_sign_posn': 1, |
|
99 'negative_sign': '-', |
|
100 'p_cs_precedes': 1, |
|
101 'p_sep_by_space': 0, |
|
102 'p_sign_posn': 1, |
|
103 'positive_sign': '', |
|
104 'thousands_sep': ',' |
|
105 } |
|
106 |
|
107 |
|
108 class BaseFormattingTest(object): |
|
109 # |
|
110 # Utility functions for formatting tests |
|
111 # |
|
112 |
|
113 def _test_formatfunc(self, format, value, out, func, **format_opts): |
|
114 self.assertEqual( |
|
115 func(format, value, **format_opts), out) |
|
116 |
|
117 def _test_format(self, format, value, out, **format_opts): |
|
118 self._test_formatfunc(format, value, out, |
|
119 func=locale.format, **format_opts) |
|
120 |
|
121 def _test_format_string(self, format, value, out, **format_opts): |
|
122 self._test_formatfunc(format, value, out, |
|
123 func=locale.format_string, **format_opts) |
|
124 |
|
125 def _test_currency(self, value, out, **format_opts): |
|
126 self.assertEqual(locale.currency(value, **format_opts), out) |
|
127 |
|
128 |
|
129 class EnUSNumberFormatting(BaseFormattingTest): |
|
130 # XXX there is a grouping + padding bug when the thousands separator |
|
131 # is empty but the grouping array contains values (e.g. Solaris 10) |
|
132 |
|
133 def setUp(self): |
|
134 self.sep = locale.localeconv()['thousands_sep'] |
|
135 |
|
136 def test_grouping(self): |
|
137 self._test_format("%f", 1024, grouping=1, out='1%s024.000000' % self.sep) |
|
138 self._test_format("%f", 102, grouping=1, out='102.000000') |
|
139 self._test_format("%f", -42, grouping=1, out='-42.000000') |
|
140 self._test_format("%+f", -42, grouping=1, out='-42.000000') |
|
141 |
|
142 def test_grouping_and_padding(self): |
|
143 self._test_format("%20.f", -42, grouping=1, out='-42'.rjust(20)) |
|
144 if self.sep: |
|
145 self._test_format("%+10.f", -4200, grouping=1, |
|
146 out=('-4%s200' % self.sep).rjust(10)) |
|
147 self._test_format("%-10.f", -4200, grouping=1, |
|
148 out=('-4%s200' % self.sep).ljust(10)) |
|
149 |
|
150 def test_integer_grouping(self): |
|
151 self._test_format("%d", 4200, grouping=True, out='4%s200' % self.sep) |
|
152 self._test_format("%+d", 4200, grouping=True, out='+4%s200' % self.sep) |
|
153 self._test_format("%+d", -4200, grouping=True, out='-4%s200' % self.sep) |
|
154 |
|
155 def test_simple(self): |
|
156 self._test_format("%f", 1024, grouping=0, out='1024.000000') |
|
157 self._test_format("%f", 102, grouping=0, out='102.000000') |
|
158 self._test_format("%f", -42, grouping=0, out='-42.000000') |
|
159 self._test_format("%+f", -42, grouping=0, out='-42.000000') |
|
160 |
|
161 def test_padding(self): |
|
162 self._test_format("%20.f", -42, grouping=0, out='-42'.rjust(20)) |
|
163 self._test_format("%+10.f", -4200, grouping=0, out='-4200'.rjust(10)) |
|
164 self._test_format("%-10.f", 4200, grouping=0, out='4200'.ljust(10)) |
|
165 |
|
166 def test_complex_formatting(self): |
|
167 # Spaces in formatting string |
|
168 self._test_format_string("One million is %i", 1000000, grouping=1, |
|
169 out='One million is 1%s000%s000' % (self.sep, self.sep)) |
|
170 self._test_format_string("One million is %i", 1000000, grouping=1, |
|
171 out='One million is 1%s000%s000' % (self.sep, self.sep)) |
|
172 # Dots in formatting string |
|
173 self._test_format_string(".%f.", 1000.0, out='.1000.000000.') |
|
174 # Padding |
|
175 if self.sep: |
|
176 self._test_format_string("--> %10.2f", 4200, grouping=1, |
|
177 out='--> ' + ('4%s200.00' % self.sep).rjust(10)) |
|
178 # Asterisk formats |
|
179 self._test_format_string("%10.*f", (2, 1000), grouping=0, |
|
180 out='1000.00'.rjust(10)) |
|
181 if self.sep: |
|
182 self._test_format_string("%*.*f", (10, 2, 1000), grouping=1, |
|
183 out=('1%s000.00' % self.sep).rjust(10)) |
|
184 # Test more-in-one |
|
185 if self.sep: |
|
186 self._test_format_string("int %i float %.2f str %s", |
|
187 (1000, 1000.0, 'str'), grouping=1, |
|
188 out='int 1%s000 float 1%s000.00 str str' % |
|
189 (self.sep, self.sep)) |
|
190 |
|
191 |
|
192 class TestNumberFormatting(BaseLocalizedTest, EnUSNumberFormatting): |
|
193 # Test number formatting with a real English locale. |
|
194 |
|
195 locale_type = locale.LC_NUMERIC |
|
196 |
|
197 def setUp(self): |
|
198 BaseLocalizedTest.setUp(self) |
|
199 EnUSNumberFormatting.setUp(self) |
|
200 |
|
201 |
|
202 class TestEnUSNumberFormatting(EnUSCookedTest, EnUSNumberFormatting): |
|
203 # Test number formatting with a cooked "en_US" locale. |
|
204 |
|
205 def setUp(self): |
|
206 EnUSCookedTest.setUp(self) |
|
207 EnUSNumberFormatting.setUp(self) |
|
208 |
|
209 def test_currency(self): |
|
210 self._test_currency(50000, "$50000.00") |
|
211 self._test_currency(50000, "$50,000.00", grouping=True) |
|
212 self._test_currency(50000, "USD 50,000.00", |
|
213 grouping=True, international=True) |
|
214 |
|
215 |
|
216 class TestCNumberFormatting(CCookedTest, BaseFormattingTest): |
|
217 # Test number formatting with a cooked "C" locale. |
|
218 |
|
219 def test_grouping(self): |
|
220 self._test_format("%.2f", 12345.67, grouping=True, out='12345.67') |
|
221 |
|
222 def test_grouping_and_padding(self): |
|
223 self._test_format("%9.2f", 12345.67, grouping=True, out=' 12345.67') |
|
224 |
|
225 |
|
226 class TestStringMethods(BaseLocalizedTest): |
|
227 locale_type = locale.LC_CTYPE |
|
228 |
|
229 if sys.platform != 'sunos5' and not sys.platform.startswith("win"): |
|
230 # Test BSD Rune locale's bug for isctype functions. |
|
231 |
|
232 def test_isspace(self): |
|
233 self.assertEqual('\x20'.isspace(), True) |
|
234 self.assertEqual('\xa0'.isspace(), False) |
|
235 self.assertEqual('\xa1'.isspace(), False) |
|
236 |
|
237 def test_isalpha(self): |
|
238 self.assertEqual('\xc0'.isalpha(), False) |
|
239 |
|
240 def test_isalnum(self): |
|
241 self.assertEqual('\xc0'.isalnum(), False) |
|
242 |
|
243 def test_isupper(self): |
|
244 self.assertEqual('\xc0'.isupper(), False) |
|
245 |
|
246 def test_islower(self): |
|
247 self.assertEqual('\xc0'.islower(), False) |
|
248 |
|
249 def test_lower(self): |
|
250 self.assertEqual('\xcc\x85'.lower(), '\xcc\x85') |
|
251 |
|
252 def test_upper(self): |
|
253 self.assertEqual('\xed\x95\xa0'.upper(), '\xed\x95\xa0') |
|
254 |
|
255 def test_strip(self): |
|
256 self.assertEqual('\xed\x95\xa0'.strip(), '\xed\x95\xa0') |
|
257 |
|
258 def test_split(self): |
|
259 self.assertEqual('\xec\xa0\xbc'.split(), ['\xec\xa0\xbc']) |
|
260 |
|
261 |
|
262 class TestMiscellaneous(unittest.TestCase): |
|
263 def test_getpreferredencoding(self): |
|
264 # Invoke getpreferredencoding to make sure it does not cause exceptions. |
|
265 enc = locale.getpreferredencoding() |
|
266 if enc: |
|
267 # If encoding non-empty, make sure it is valid |
|
268 codecs.lookup(enc) |
|
269 |
|
270 if hasattr(locale, "strcoll"): |
|
271 def test_strcoll_3303(self): |
|
272 # test crasher from bug #3303 |
|
273 self.assertRaises(TypeError, locale.strcoll, u"a", None) |
|
274 |
|
275 |
|
276 def test_main(): |
|
277 tests = [ |
|
278 TestMiscellaneous, |
|
279 TestEnUSNumberFormatting, |
|
280 TestCNumberFormatting |
|
281 ] |
|
282 # TestSkipped can't be raised inside unittests, handle it manually instead |
|
283 try: |
|
284 get_enUS_locale() |
|
285 except TestSkipped as e: |
|
286 if verbose: |
|
287 print "Some tests will be disabled: %s" % e |
|
288 else: |
|
289 tests += [TestNumberFormatting, TestStringMethods] |
|
290 run_unittest(*tests) |
|
291 |
|
292 if __name__ == '__main__': |
|
293 test_main() |