blob: 251dc7acbbfa42be0254f41e13f2fd69e213f9c4 [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 Pitrou83d6a872008-07-25 21:45:08 +00007class BaseLocalizedTest(unittest.TestCase):
8 #
9 # Base class for tests using a real locale
10 #
Martin v. Löwis88ad12a2001-04-13 08:09:50 +000011
Antoine Pitrou83d6a872008-07-25 21:45:08 +000012 if sys.platform.startswith("win"):
13 tlocs = ("En", "English")
14 else:
15 tlocs = ("en_US.UTF-8", "en_US.US-ASCII", "en_US")
Guido van Rossumfc349862001-04-15 13:15:56 +000016
Antoine Pitrou83d6a872008-07-25 21:45:08 +000017 def setUp(self):
18 if sys.platform == 'darwin':
19 raise TestSkipped(
20 "Locale support on MacOSX is minimal and cannot be tested")
21 self.oldlocale = locale.setlocale(self.locale_type)
22 for tloc in self.tlocs:
23 try:
24 locale.setlocale(self.locale_type, tloc)
25 except locale.Error:
26 continue
27 break
Martin v. Löwis88ad12a2001-04-13 08:09:50 +000028 else:
Antoine Pitrou83d6a872008-07-25 21:45:08 +000029 raise TestSkipped(
30 "Test locale not supported (tried %s)" % (', '.join(self.tlocs)))
Martin v. Löwis88ad12a2001-04-13 08:09:50 +000031 if verbose:
Antoine Pitrou83d6a872008-07-25 21:45:08 +000032 print("testing with \"%s\"..." % tloc, end=' ')
Martin v. Löwis88ad12a2001-04-13 08:09:50 +000033
Antoine Pitrou83d6a872008-07-25 21:45:08 +000034 def tearDown(self):
35 locale.setlocale(self.locale_type, self.oldlocale)
Thomas Wouters477c8d52006-05-27 19:21:47 +000036
Thomas Wouters477c8d52006-05-27 19:21:47 +000037
Antoine Pitrou83d6a872008-07-25 21:45:08 +000038class BaseCookedTest(unittest.TestCase):
39 #
40 # Base class for tests using cooked localeconv() values
41 #
Georg Brandl3dbca812008-07-23 16:10:53 +000042
Antoine Pitrou83d6a872008-07-25 21:45:08 +000043 def setUp(self):
44 locale._override_localeconv = self.cooked_values
45
46 def tearDown(self):
47 locale._override_localeconv = {}
48
49class CCookedTest(BaseCookedTest):
50 # A cooked "C" locale
51
52 cooked_values = {
53 'currency_symbol': '',
54 'decimal_point': '.',
55 'frac_digits': 127,
56 'grouping': [],
57 'int_curr_symbol': '',
58 'int_frac_digits': 127,
59 'mon_decimal_point': '',
60 'mon_grouping': [],
61 'mon_thousands_sep': '',
62 'n_cs_precedes': 127,
63 'n_sep_by_space': 127,
64 'n_sign_posn': 127,
65 'negative_sign': '',
66 'p_cs_precedes': 127,
67 'p_sep_by_space': 127,
68 'p_sign_posn': 127,
69 'positive_sign': '',
70 'thousands_sep': ''
71 }
72
73class EnUSCookedTest(BaseCookedTest):
74 # A cooked "en_US" locale
75
76 cooked_values = {
77 'currency_symbol': '$',
78 'decimal_point': '.',
79 'frac_digits': 2,
80 'grouping': [3, 3, 0],
81 'int_curr_symbol': 'USD ',
82 'int_frac_digits': 2,
83 'mon_decimal_point': '.',
84 'mon_grouping': [3, 3, 0],
85 'mon_thousands_sep': ',',
86 'n_cs_precedes': 1,
87 'n_sep_by_space': 0,
88 'n_sign_posn': 1,
89 'negative_sign': '-',
90 'p_cs_precedes': 1,
91 'p_sep_by_space': 0,
92 'p_sign_posn': 1,
93 'positive_sign': '',
94 'thousands_sep': ','
95 }
96
97
98class BaseFormattingTest(object):
99 #
100 # Utility functions for formatting tests
101 #
102
103 def _test_formatfunc(self, format, value, out, func, **format_opts):
104 self.assertEqual(
105 func(format, value, **format_opts), out)
106
107 def _test_format(self, format, value, out, **format_opts):
108 self._test_formatfunc(format, value, out,
109 func=locale.format, **format_opts)
110
111 def _test_format_string(self, format, value, out, **format_opts):
112 self._test_formatfunc(format, value, out,
113 func=locale.format_string, **format_opts)
114
115 def _test_currency(self, value, out, **format_opts):
116 self.assertEqual(locale.currency(value, **format_opts), out)
117
118
119class EnUSNumberFormatting(BaseFormattingTest):
120
121 def setUp(self):
122 # NOTE: On Solaris 10, the thousands_sep is the empty string
123 self.sep = locale.localeconv()['thousands_sep']
124
125 def test_grouping(self):
126 self._test_format("%f", 1024, grouping=1, out='1%s024.000000' % self.sep)
127 self._test_format("%f", 102, grouping=1, out='102.000000')
128 self._test_format("%f", -42, grouping=1, out='-42.000000')
129 self._test_format("%+f", -42, grouping=1, out='-42.000000')
130
131 def test_grouping_and_padding(self):
132 self._test_format("%20.f", -42, grouping=1, out='-42'.rjust(20))
133 self._test_format("%+10.f", -4200, grouping=1,
134 out=('-4%s200' % self.sep).rjust(10))
135 self._test_format("%-10.f", -4200, grouping=1,
136 out=('-4%s200' % self.sep).ljust(10))
137
138 def test_integer_grouping(self):
139 self._test_format("%d", 4200, grouping=True, out='4%s200' % self.sep)
140 self._test_format("%+d", 4200, grouping=True, out='+4%s200' % self.sep)
141 self._test_format("%+d", -4200, grouping=True, out='-4%s200' % self.sep)
142
143 def test_simple(self):
144 self._test_format("%f", 1024, grouping=0, out='1024.000000')
145 self._test_format("%f", 102, grouping=0, out='102.000000')
146 self._test_format("%f", -42, grouping=0, out='-42.000000')
147 self._test_format("%+f", -42, grouping=0, out='-42.000000')
148
149 def test_padding(self):
150 self._test_format("%20.f", -42, grouping=0, out='-42'.rjust(20))
151 self._test_format("%+10.f", -4200, grouping=0, out='-4200'.rjust(10))
152 self._test_format("%-10.f", 4200, grouping=0, out='4200'.ljust(10))
153
154 def test_complex_formatting(self):
155 # Spaces in formatting string
156 self._test_format_string("One million is %i", 1000000, grouping=1,
157 out='One million is 1%s000%s000' % (self.sep, self.sep))
158 self._test_format_string("One million is %i", 1000000, grouping=1,
159 out='One million is 1%s000%s000' % (self.sep, self.sep))
160 # Dots in formatting string
161 self._test_format_string(".%f.", 1000.0, out='.1000.000000.')
162 # Padding
163 self._test_format_string("--> %10.2f", 4200, grouping=1,
164 out='--> ' + ('4%s200.00' % self.sep).rjust(10))
165 # Asterisk formats
166 self._test_format_string("%10.*f", (2, 1000), grouping=0,
167 out='1000.00'.rjust(10))
168 self._test_format_string("%*.*f", (10, 2, 1000), grouping=1,
169 out=('1%s000.00' % self.sep).rjust(10))
170 # Test more-in-one
171 self._test_format_string("int %i float %.2f str %s",
172 (1000, 1000.0, 'str'), grouping=1,
173 out='int 1%s000 float 1%s000.00 str str' % (self.sep, self.sep))
174
175
176class TestNumberFormatting(BaseLocalizedTest, EnUSNumberFormatting):
177 # Test number formatting with a real English locale.
178
179 locale_type = locale.LC_NUMERIC
180
181 def setUp(self):
182 BaseLocalizedTest.setUp(self)
183 EnUSNumberFormatting.setUp(self)
184
185
186class TestEnUSNumberFormatting(EnUSCookedTest, EnUSNumberFormatting):
187 # Test number formatting with a cooked "en_US" locale.
188
189 def setUp(self):
190 EnUSCookedTest.setUp(self)
191 EnUSNumberFormatting.setUp(self)
192
193 def test_currency(self):
194 self._test_currency(50000, "$50000.00")
195 self._test_currency(50000, "$50,000.00", grouping=True)
196 self._test_currency(50000, "USD 50,000.00",
197 grouping=True, international=True)
198
199
200class TestCNumberFormatting(CCookedTest, BaseFormattingTest):
201 # Test number formatting with a cooked "C" locale.
202
203 def test_grouping(self):
204 self._test_format("%.2f", 12345.67, grouping=True, out='12345.67')
205
206 def test_grouping_and_padding(self):
207 self._test_format("%9.2f", 12345.67, grouping=True, out=' 12345.67')
208
209
210class TestMiscellaneous(unittest.TestCase):
211 def test_getpreferredencoding(self):
212 # Invoke getpreferredencoding to make sure it does not cause exceptions.
213 enc = locale.getpreferredencoding()
214 if enc:
215 # If encoding non-empty, make sure it is valid
216 codecs.lookup(enc)
217
218 if hasattr(locale, "strcoll"):
219 def test_strcoll_3303(self):
220 # test crasher from bug #3303
221 self.assertRaises(TypeError, locale.strcoll, "a", None)
222 self.assertRaises(TypeError, locale.strcoll, b"a", None)
223
224
225def test_main():
226 run_unittest(__name__)
227
228if __name__ == '__main__':
229 test_main()