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