blob: 0858b60e9be20737c308739a1332a2cac8602f02 [file] [log] [blame]
Antoine Pitrou83d6a872008-07-25 21:45:08 +00001from test.support import run_unittest, TestSkipped, verbose
2import 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
11 if sys.platform == 'darwin':
12 raise TestSkipped("Locale support on MacOSX is minimal")
13 if sys.platform.startswith("win"):
14 tlocs = ("En", "English")
15 else:
16 tlocs = ("en_US.UTF-8", "en_US.US-ASCII", "en_US")
17 oldlocale = locale.setlocale(locale.LC_NUMERIC)
18 for tloc in tlocs:
19 try:
20 locale.setlocale(locale.LC_NUMERIC, tloc)
21 except locale.Error:
22 continue
23 break
24 else:
25 raise TestSkipped(
26 "Test locale not supported (tried %s)" % (', '.join(tlocs)))
27 enUS_locale = tloc
28 locale.setlocale(locale.LC_NUMERIC, oldlocale)
29
30
Antoine Pitrou83d6a872008-07-25 21:45:08 +000031class BaseLocalizedTest(unittest.TestCase):
32 #
33 # Base class for tests using a real locale
34 #
Martin v. Löwis88ad12a2001-04-13 08:09:50 +000035
Antoine Pitrou83d6a872008-07-25 21:45:08 +000036 def setUp(self):
Antoine Pitrou83d6a872008-07-25 21:45:08 +000037 self.oldlocale = locale.setlocale(self.locale_type)
Antoine Pitrou13856ea2008-07-26 21:02:53 +000038 locale.setlocale(self.locale_type, enUS_locale)
Martin v. Löwis88ad12a2001-04-13 08:09:50 +000039 if verbose:
Antoine Pitrou13856ea2008-07-26 21:02:53 +000040 print("testing with \"%s\"..." % enUS_locale, end=' ')
Martin v. Löwis88ad12a2001-04-13 08:09:50 +000041
Antoine Pitrou83d6a872008-07-25 21:45:08 +000042 def tearDown(self):
43 locale.setlocale(self.locale_type, self.oldlocale)
Thomas Wouters477c8d52006-05-27 19:21:47 +000044
Thomas Wouters477c8d52006-05-27 19:21:47 +000045
Antoine Pitrou83d6a872008-07-25 21:45:08 +000046class BaseCookedTest(unittest.TestCase):
47 #
48 # Base class for tests using cooked localeconv() values
49 #
Georg Brandl3dbca812008-07-23 16:10:53 +000050
Antoine Pitrou83d6a872008-07-25 21:45:08 +000051 def setUp(self):
52 locale._override_localeconv = self.cooked_values
53
54 def tearDown(self):
55 locale._override_localeconv = {}
56
57class CCookedTest(BaseCookedTest):
58 # A cooked "C" locale
59
60 cooked_values = {
61 'currency_symbol': '',
62 'decimal_point': '.',
63 'frac_digits': 127,
64 'grouping': [],
65 'int_curr_symbol': '',
66 'int_frac_digits': 127,
67 'mon_decimal_point': '',
68 'mon_grouping': [],
69 'mon_thousands_sep': '',
70 'n_cs_precedes': 127,
71 'n_sep_by_space': 127,
72 'n_sign_posn': 127,
73 'negative_sign': '',
74 'p_cs_precedes': 127,
75 'p_sep_by_space': 127,
76 'p_sign_posn': 127,
77 'positive_sign': '',
78 'thousands_sep': ''
79 }
80
81class EnUSCookedTest(BaseCookedTest):
82 # A cooked "en_US" locale
83
84 cooked_values = {
85 'currency_symbol': '$',
86 'decimal_point': '.',
87 'frac_digits': 2,
88 'grouping': [3, 3, 0],
89 'int_curr_symbol': 'USD ',
90 'int_frac_digits': 2,
91 'mon_decimal_point': '.',
92 'mon_grouping': [3, 3, 0],
93 'mon_thousands_sep': ',',
94 'n_cs_precedes': 1,
95 'n_sep_by_space': 0,
96 'n_sign_posn': 1,
97 'negative_sign': '-',
98 'p_cs_precedes': 1,
99 'p_sep_by_space': 0,
100 'p_sign_posn': 1,
101 'positive_sign': '',
102 'thousands_sep': ','
103 }
104
105
106class BaseFormattingTest(object):
107 #
108 # Utility functions for formatting tests
109 #
110
111 def _test_formatfunc(self, format, value, out, func, **format_opts):
112 self.assertEqual(
113 func(format, value, **format_opts), out)
114
115 def _test_format(self, format, value, out, **format_opts):
116 self._test_formatfunc(format, value, out,
117 func=locale.format, **format_opts)
118
119 def _test_format_string(self, format, value, out, **format_opts):
120 self._test_formatfunc(format, value, out,
121 func=locale.format_string, **format_opts)
122
123 def _test_currency(self, value, out, **format_opts):
124 self.assertEqual(locale.currency(value, **format_opts), out)
125
126
127class EnUSNumberFormatting(BaseFormattingTest):
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000128 # XXX there is a grouping + padding bug when the thousands separator
129 # is empty but the grouping array contains values (e.g. Solaris 10)
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000130
131 def setUp(self):
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000132 self.sep = locale.localeconv()['thousands_sep']
133
134 def test_grouping(self):
135 self._test_format("%f", 1024, grouping=1, out='1%s024.000000' % self.sep)
136 self._test_format("%f", 102, grouping=1, out='102.000000')
137 self._test_format("%f", -42, grouping=1, out='-42.000000')
138 self._test_format("%+f", -42, grouping=1, out='-42.000000')
139
140 def test_grouping_and_padding(self):
141 self._test_format("%20.f", -42, grouping=1, out='-42'.rjust(20))
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000142 if self.sep:
143 self._test_format("%+10.f", -4200, grouping=1,
144 out=('-4%s200' % self.sep).rjust(10))
145 self._test_format("%-10.f", -4200, grouping=1,
146 out=('-4%s200' % self.sep).ljust(10))
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000147
148 def test_integer_grouping(self):
149 self._test_format("%d", 4200, grouping=True, out='4%s200' % self.sep)
150 self._test_format("%+d", 4200, grouping=True, out='+4%s200' % self.sep)
151 self._test_format("%+d", -4200, grouping=True, out='-4%s200' % self.sep)
152
153 def test_simple(self):
154 self._test_format("%f", 1024, grouping=0, out='1024.000000')
155 self._test_format("%f", 102, grouping=0, out='102.000000')
156 self._test_format("%f", -42, grouping=0, out='-42.000000')
157 self._test_format("%+f", -42, grouping=0, out='-42.000000')
158
159 def test_padding(self):
160 self._test_format("%20.f", -42, grouping=0, out='-42'.rjust(20))
161 self._test_format("%+10.f", -4200, grouping=0, out='-4200'.rjust(10))
162 self._test_format("%-10.f", 4200, grouping=0, out='4200'.ljust(10))
163
164 def test_complex_formatting(self):
165 # Spaces in formatting string
166 self._test_format_string("One million is %i", 1000000, grouping=1,
167 out='One million is 1%s000%s000' % (self.sep, self.sep))
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 # Dots in formatting string
171 self._test_format_string(".%f.", 1000.0, out='.1000.000000.')
172 # Padding
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000173 if self.sep:
174 self._test_format_string("--> %10.2f", 4200, grouping=1,
175 out='--> ' + ('4%s200.00' % self.sep).rjust(10))
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000176 # Asterisk formats
177 self._test_format_string("%10.*f", (2, 1000), grouping=0,
178 out='1000.00'.rjust(10))
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000179 if self.sep:
180 self._test_format_string("%*.*f", (10, 2, 1000), grouping=1,
181 out=('1%s000.00' % self.sep).rjust(10))
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000182 # Test more-in-one
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000183 if self.sep:
184 self._test_format_string("int %i float %.2f str %s",
185 (1000, 1000.0, 'str'), grouping=1,
186 out='int 1%s000 float 1%s000.00 str str' %
187 (self.sep, self.sep))
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000188
189
190class TestNumberFormatting(BaseLocalizedTest, EnUSNumberFormatting):
191 # Test number formatting with a real English locale.
192
193 locale_type = locale.LC_NUMERIC
194
195 def setUp(self):
196 BaseLocalizedTest.setUp(self)
197 EnUSNumberFormatting.setUp(self)
198
199
200class TestEnUSNumberFormatting(EnUSCookedTest, EnUSNumberFormatting):
201 # Test number formatting with a cooked "en_US" locale.
202
203 def setUp(self):
204 EnUSCookedTest.setUp(self)
205 EnUSNumberFormatting.setUp(self)
206
207 def test_currency(self):
208 self._test_currency(50000, "$50000.00")
209 self._test_currency(50000, "$50,000.00", grouping=True)
210 self._test_currency(50000, "USD 50,000.00",
211 grouping=True, international=True)
212
213
214class TestCNumberFormatting(CCookedTest, BaseFormattingTest):
215 # Test number formatting with a cooked "C" locale.
216
217 def test_grouping(self):
218 self._test_format("%.2f", 12345.67, grouping=True, out='12345.67')
219
220 def test_grouping_and_padding(self):
221 self._test_format("%9.2f", 12345.67, grouping=True, out=' 12345.67')
222
223
224class TestMiscellaneous(unittest.TestCase):
225 def test_getpreferredencoding(self):
226 # Invoke getpreferredencoding to make sure it does not cause exceptions.
227 enc = locale.getpreferredencoding()
228 if enc:
229 # If encoding non-empty, make sure it is valid
230 codecs.lookup(enc)
231
232 if hasattr(locale, "strcoll"):
233 def test_strcoll_3303(self):
234 # test crasher from bug #3303
235 self.assertRaises(TypeError, locale.strcoll, "a", None)
236 self.assertRaises(TypeError, locale.strcoll, b"a", None)
237
238
239def test_main():
Antoine Pitrou13856ea2008-07-26 21:02:53 +0000240 tests = [
241 TestMiscellaneous,
242 TestEnUSNumberFormatting,
243 TestCNumberFormatting
244 ]
245 # TestSkipped can't be raised inside unittests, handle it manually instead
246 try:
247 get_enUS_locale()
248 except TestSkipped as e:
249 if verbose:
250 print("Some tests will be disabled: %s" % e)
251 else:
252 tests += [TestNumberFormatting]
253 run_unittest(*tests)
Antoine Pitrou83d6a872008-07-25 21:45:08 +0000254
255if __name__ == '__main__':
256 test_main()