Benjamin Peterson | bec087f | 2009-03-26 21:10:30 +0000 | [diff] [blame] | 1 | from test.test_support import run_unittest, verbose |
Antoine Pitrou | ba54eda | 2008-07-25 20:40:19 +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 | ba54eda | 2008-07-25 20:40:19 +0000 | [diff] [blame] | 5 | import codecs |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 6 | |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 7 | |
Antoine Pitrou | 524f413 | 2008-07-26 10:29:43 +0000 | [diff] [blame] | 8 | enUS_locale = None |
| 9 | |
| 10 | def get_enUS_locale(): |
| 11 | global enUS_locale |
| 12 | if sys.platform == 'darwin': |
Benjamin Peterson | bec087f | 2009-03-26 21:10:30 +0000 | [diff] [blame] | 13 | raise unittest.SkipTest("Locale support on MacOSX is minimal") |
Antoine Pitrou | 524f413 | 2008-07-26 10:29:43 +0000 | [diff] [blame] | 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: |
Benjamin Peterson | bec087f | 2009-03-26 21:10:30 +0000 | [diff] [blame] | 26 | raise unittest.SkipTest( |
Antoine Pitrou | 524f413 | 2008-07-26 10:29:43 +0000 | [diff] [blame] | 27 | "Test locale not supported (tried %s)" % (', '.join(tlocs))) |
| 28 | enUS_locale = tloc |
| 29 | locale.setlocale(locale.LC_NUMERIC, oldlocale) |
| 30 | |
| 31 | |
Antoine Pitrou | ba54eda | 2008-07-25 20:40:19 +0000 | [diff] [blame] | 32 | class BaseLocalizedTest(unittest.TestCase): |
| 33 | # |
| 34 | # Base class for tests using a real locale |
| 35 | # |
Guido van Rossum | fc34986 | 2001-04-15 13:15:56 +0000 | [diff] [blame] | 36 | |
Antoine Pitrou | ba54eda | 2008-07-25 20:40:19 +0000 | [diff] [blame] | 37 | def setUp(self): |
Antoine Pitrou | ba54eda | 2008-07-25 20:40:19 +0000 | [diff] [blame] | 38 | self.oldlocale = locale.setlocale(self.locale_type) |
Antoine Pitrou | 524f413 | 2008-07-26 10:29:43 +0000 | [diff] [blame] | 39 | locale.setlocale(self.locale_type, enUS_locale) |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 40 | if verbose: |
Antoine Pitrou | 524f413 | 2008-07-26 10:29:43 +0000 | [diff] [blame] | 41 | print "testing with \"%s\"..." % enUS_locale, |
Martin v. Löwis | 88ad12a | 2001-04-13 08:09:50 +0000 | [diff] [blame] | 42 | |
Antoine Pitrou | ba54eda | 2008-07-25 20:40:19 +0000 | [diff] [blame] | 43 | def tearDown(self): |
| 44 | locale.setlocale(self.locale_type, self.oldlocale) |
Hye-Shik Chang | b5047fd | 2004-08-04 06:33:51 +0000 | [diff] [blame] | 45 | |
| 46 | |
Antoine Pitrou | ba54eda | 2008-07-25 20:40:19 +0000 | [diff] [blame] | 47 | class BaseCookedTest(unittest.TestCase): |
| 48 | # |
| 49 | # Base class for tests using cooked localeconv() values |
| 50 | # |
Hye-Shik Chang | b5047fd | 2004-08-04 06:33:51 +0000 | [diff] [blame] | 51 | |
Antoine Pitrou | ba54eda | 2008-07-25 20:40:19 +0000 | [diff] [blame] | 52 | def setUp(self): |
| 53 | locale._override_localeconv = self.cooked_values |
Georg Brandl | 278fc50 | 2008-07-19 12:46:12 +0000 | [diff] [blame] | 54 | |
Antoine Pitrou | ba54eda | 2008-07-25 20:40:19 +0000 | [diff] [blame] | 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 | |
Antoine Pitrou | feeafff | 2009-03-14 00:07:21 +0000 | [diff] [blame] | 108 | class FrFRCookedTest(BaseCookedTest): |
| 109 | # A cooked "fr_FR" locale with a space character as decimal separator |
| 110 | # and a non-ASCII currency symbol. |
| 111 | |
| 112 | cooked_values = { |
| 113 | 'currency_symbol': '\xe2\x82\xac', |
| 114 | 'decimal_point': ',', |
| 115 | 'frac_digits': 2, |
| 116 | 'grouping': [3, 3, 0], |
| 117 | 'int_curr_symbol': 'EUR ', |
| 118 | 'int_frac_digits': 2, |
| 119 | 'mon_decimal_point': ',', |
| 120 | 'mon_grouping': [3, 3, 0], |
| 121 | 'mon_thousands_sep': ' ', |
| 122 | 'n_cs_precedes': 0, |
| 123 | 'n_sep_by_space': 1, |
| 124 | 'n_sign_posn': 1, |
| 125 | 'negative_sign': '-', |
| 126 | 'p_cs_precedes': 0, |
| 127 | 'p_sep_by_space': 1, |
| 128 | 'p_sign_posn': 1, |
| 129 | 'positive_sign': '', |
| 130 | 'thousands_sep': ' ' |
| 131 | } |
| 132 | |
| 133 | |
Antoine Pitrou | ba54eda | 2008-07-25 20:40:19 +0000 | [diff] [blame] | 134 | class BaseFormattingTest(object): |
| 135 | # |
| 136 | # Utility functions for formatting tests |
| 137 | # |
| 138 | |
| 139 | def _test_formatfunc(self, format, value, out, func, **format_opts): |
| 140 | self.assertEqual( |
| 141 | func(format, value, **format_opts), out) |
| 142 | |
| 143 | def _test_format(self, format, value, out, **format_opts): |
| 144 | self._test_formatfunc(format, value, out, |
| 145 | func=locale.format, **format_opts) |
| 146 | |
| 147 | def _test_format_string(self, format, value, out, **format_opts): |
| 148 | self._test_formatfunc(format, value, out, |
| 149 | func=locale.format_string, **format_opts) |
| 150 | |
| 151 | def _test_currency(self, value, out, **format_opts): |
| 152 | self.assertEqual(locale.currency(value, **format_opts), out) |
| 153 | |
| 154 | |
| 155 | class EnUSNumberFormatting(BaseFormattingTest): |
Antoine Pitrou | 36897e1 | 2008-07-26 13:49:13 +0000 | [diff] [blame] | 156 | # XXX there is a grouping + padding bug when the thousands separator |
| 157 | # is empty but the grouping array contains values (e.g. Solaris 10) |
Antoine Pitrou | ba54eda | 2008-07-25 20:40:19 +0000 | [diff] [blame] | 158 | |
| 159 | def setUp(self): |
Antoine Pitrou | ba54eda | 2008-07-25 20:40:19 +0000 | [diff] [blame] | 160 | self.sep = locale.localeconv()['thousands_sep'] |
| 161 | |
| 162 | def test_grouping(self): |
| 163 | self._test_format("%f", 1024, grouping=1, out='1%s024.000000' % self.sep) |
| 164 | self._test_format("%f", 102, grouping=1, out='102.000000') |
| 165 | self._test_format("%f", -42, grouping=1, out='-42.000000') |
| 166 | self._test_format("%+f", -42, grouping=1, out='-42.000000') |
| 167 | |
| 168 | def test_grouping_and_padding(self): |
| 169 | self._test_format("%20.f", -42, grouping=1, out='-42'.rjust(20)) |
Antoine Pitrou | 36897e1 | 2008-07-26 13:49:13 +0000 | [diff] [blame] | 170 | if self.sep: |
Antoine Pitrou | 524f413 | 2008-07-26 10:29:43 +0000 | [diff] [blame] | 171 | self._test_format("%+10.f", -4200, grouping=1, |
| 172 | out=('-4%s200' % self.sep).rjust(10)) |
| 173 | self._test_format("%-10.f", -4200, grouping=1, |
| 174 | out=('-4%s200' % self.sep).ljust(10)) |
Antoine Pitrou | ba54eda | 2008-07-25 20:40:19 +0000 | [diff] [blame] | 175 | |
| 176 | def test_integer_grouping(self): |
| 177 | self._test_format("%d", 4200, grouping=True, out='4%s200' % self.sep) |
| 178 | self._test_format("%+d", 4200, grouping=True, out='+4%s200' % self.sep) |
| 179 | self._test_format("%+d", -4200, grouping=True, out='-4%s200' % self.sep) |
| 180 | |
Antoine Pitrou | feeafff | 2009-03-14 00:07:21 +0000 | [diff] [blame] | 181 | def test_integer_grouping_and_padding(self): |
| 182 | self._test_format("%10d", 4200, grouping=True, |
| 183 | out=('4%s200' % self.sep).rjust(10)) |
| 184 | self._test_format("%-10d", -4200, grouping=True, |
| 185 | out=('-4%s200' % self.sep).ljust(10)) |
| 186 | |
Antoine Pitrou | ba54eda | 2008-07-25 20:40:19 +0000 | [diff] [blame] | 187 | def test_simple(self): |
| 188 | self._test_format("%f", 1024, grouping=0, out='1024.000000') |
| 189 | self._test_format("%f", 102, grouping=0, out='102.000000') |
| 190 | self._test_format("%f", -42, grouping=0, out='-42.000000') |
| 191 | self._test_format("%+f", -42, grouping=0, out='-42.000000') |
| 192 | |
| 193 | def test_padding(self): |
| 194 | self._test_format("%20.f", -42, grouping=0, out='-42'.rjust(20)) |
| 195 | self._test_format("%+10.f", -4200, grouping=0, out='-4200'.rjust(10)) |
| 196 | self._test_format("%-10.f", 4200, grouping=0, out='4200'.ljust(10)) |
| 197 | |
| 198 | def test_complex_formatting(self): |
| 199 | # Spaces in formatting string |
| 200 | self._test_format_string("One million is %i", 1000000, grouping=1, |
| 201 | out='One million is 1%s000%s000' % (self.sep, self.sep)) |
| 202 | self._test_format_string("One million is %i", 1000000, grouping=1, |
| 203 | out='One million is 1%s000%s000' % (self.sep, self.sep)) |
| 204 | # Dots in formatting string |
| 205 | self._test_format_string(".%f.", 1000.0, out='.1000.000000.') |
| 206 | # Padding |
Antoine Pitrou | 36897e1 | 2008-07-26 13:49:13 +0000 | [diff] [blame] | 207 | if self.sep: |
| 208 | self._test_format_string("--> %10.2f", 4200, grouping=1, |
| 209 | out='--> ' + ('4%s200.00' % self.sep).rjust(10)) |
Antoine Pitrou | ba54eda | 2008-07-25 20:40:19 +0000 | [diff] [blame] | 210 | # Asterisk formats |
| 211 | self._test_format_string("%10.*f", (2, 1000), grouping=0, |
| 212 | out='1000.00'.rjust(10)) |
Antoine Pitrou | 36897e1 | 2008-07-26 13:49:13 +0000 | [diff] [blame] | 213 | if self.sep: |
| 214 | self._test_format_string("%*.*f", (10, 2, 1000), grouping=1, |
| 215 | out=('1%s000.00' % self.sep).rjust(10)) |
Antoine Pitrou | ba54eda | 2008-07-25 20:40:19 +0000 | [diff] [blame] | 216 | # Test more-in-one |
Antoine Pitrou | 36897e1 | 2008-07-26 13:49:13 +0000 | [diff] [blame] | 217 | if self.sep: |
| 218 | self._test_format_string("int %i float %.2f str %s", |
| 219 | (1000, 1000.0, 'str'), grouping=1, |
| 220 | out='int 1%s000 float 1%s000.00 str str' % |
| 221 | (self.sep, self.sep)) |
Antoine Pitrou | ba54eda | 2008-07-25 20:40:19 +0000 | [diff] [blame] | 222 | |
| 223 | |
R. David Murray | a83da35 | 2009-04-01 03:21:43 +0000 | [diff] [blame] | 224 | class TestFormatPatternArg(unittest.TestCase): |
| 225 | # Test handling of pattern argument of format |
| 226 | |
| 227 | def test_onlyOnePattern(self): |
| 228 | # Issue 2522: accept exactly one % pattern, and no extra chars. |
| 229 | self.assertRaises(ValueError, locale.format, "%f\n", 'foo') |
| 230 | self.assertRaises(ValueError, locale.format, "%f\r", 'foo') |
| 231 | self.assertRaises(ValueError, locale.format, "%f\r\n", 'foo') |
| 232 | self.assertRaises(ValueError, locale.format, " %f", 'foo') |
| 233 | self.assertRaises(ValueError, locale.format, "%fg", 'foo') |
| 234 | self.assertRaises(ValueError, locale.format, "%^g", 'foo') |
| 235 | |
| 236 | |
Antoine Pitrou | ba54eda | 2008-07-25 20:40:19 +0000 | [diff] [blame] | 237 | class TestNumberFormatting(BaseLocalizedTest, EnUSNumberFormatting): |
| 238 | # Test number formatting with a real English locale. |
| 239 | |
| 240 | locale_type = locale.LC_NUMERIC |
| 241 | |
| 242 | def setUp(self): |
| 243 | BaseLocalizedTest.setUp(self) |
| 244 | EnUSNumberFormatting.setUp(self) |
| 245 | |
| 246 | |
| 247 | class TestEnUSNumberFormatting(EnUSCookedTest, EnUSNumberFormatting): |
| 248 | # Test number formatting with a cooked "en_US" locale. |
| 249 | |
| 250 | def setUp(self): |
| 251 | EnUSCookedTest.setUp(self) |
| 252 | EnUSNumberFormatting.setUp(self) |
| 253 | |
| 254 | def test_currency(self): |
| 255 | self._test_currency(50000, "$50000.00") |
| 256 | self._test_currency(50000, "$50,000.00", grouping=True) |
| 257 | self._test_currency(50000, "USD 50,000.00", |
| 258 | grouping=True, international=True) |
| 259 | |
| 260 | |
| 261 | class TestCNumberFormatting(CCookedTest, BaseFormattingTest): |
| 262 | # Test number formatting with a cooked "C" locale. |
| 263 | |
| 264 | def test_grouping(self): |
| 265 | self._test_format("%.2f", 12345.67, grouping=True, out='12345.67') |
| 266 | |
| 267 | def test_grouping_and_padding(self): |
| 268 | self._test_format("%9.2f", 12345.67, grouping=True, out=' 12345.67') |
| 269 | |
| 270 | |
Antoine Pitrou | feeafff | 2009-03-14 00:07:21 +0000 | [diff] [blame] | 271 | class TestFrFRNumberFormatting(FrFRCookedTest, BaseFormattingTest): |
| 272 | # Test number formatting with a cooked "fr_FR" locale. |
| 273 | |
| 274 | def test_decimal_point(self): |
| 275 | self._test_format("%.2f", 12345.67, out='12345,67') |
| 276 | |
| 277 | def test_grouping(self): |
| 278 | self._test_format("%.2f", 345.67, grouping=True, out='345,67') |
| 279 | self._test_format("%.2f", 12345.67, grouping=True, out='12 345,67') |
| 280 | |
| 281 | def test_grouping_and_padding(self): |
| 282 | self._test_format("%6.2f", 345.67, grouping=True, out='345,67') |
| 283 | self._test_format("%7.2f", 345.67, grouping=True, out=' 345,67') |
| 284 | self._test_format("%8.2f", 12345.67, grouping=True, out='12 345,67') |
| 285 | self._test_format("%9.2f", 12345.67, grouping=True, out='12 345,67') |
| 286 | self._test_format("%10.2f", 12345.67, grouping=True, out=' 12 345,67') |
| 287 | self._test_format("%-6.2f", 345.67, grouping=True, out='345,67') |
| 288 | self._test_format("%-7.2f", 345.67, grouping=True, out='345,67 ') |
| 289 | self._test_format("%-8.2f", 12345.67, grouping=True, out='12 345,67') |
| 290 | self._test_format("%-9.2f", 12345.67, grouping=True, out='12 345,67') |
| 291 | self._test_format("%-10.2f", 12345.67, grouping=True, out='12 345,67 ') |
| 292 | |
| 293 | def test_integer_grouping(self): |
| 294 | self._test_format("%d", 200, grouping=True, out='200') |
| 295 | self._test_format("%d", 4200, grouping=True, out='4 200') |
| 296 | |
| 297 | def test_integer_grouping_and_padding(self): |
| 298 | self._test_format("%4d", 4200, grouping=True, out='4 200') |
| 299 | self._test_format("%5d", 4200, grouping=True, out='4 200') |
| 300 | self._test_format("%10d", 4200, grouping=True, out='4 200'.rjust(10)) |
| 301 | self._test_format("%-4d", 4200, grouping=True, out='4 200') |
| 302 | self._test_format("%-5d", 4200, grouping=True, out='4 200') |
| 303 | self._test_format("%-10d", 4200, grouping=True, out='4 200'.ljust(10)) |
| 304 | |
| 305 | def test_currency(self): |
| 306 | euro = u'\u20ac'.encode('utf-8') |
| 307 | self._test_currency(50000, "50000,00 " + euro) |
| 308 | self._test_currency(50000, "50 000,00 " + euro, grouping=True) |
| 309 | # XXX is the trailing space a bug? |
| 310 | self._test_currency(50000, "50 000,00 EUR ", |
| 311 | grouping=True, international=True) |
| 312 | |
| 313 | |
Antoine Pitrou | 6327e84 | 2008-07-26 11:56:37 +0000 | [diff] [blame] | 314 | class TestStringMethods(BaseLocalizedTest): |
| 315 | locale_type = locale.LC_CTYPE |
Antoine Pitrou | ba54eda | 2008-07-25 20:40:19 +0000 | [diff] [blame] | 316 | |
Antoine Pitrou | 6327e84 | 2008-07-26 11:56:37 +0000 | [diff] [blame] | 317 | if sys.platform != 'sunos5' and not sys.platform.startswith("win"): |
Antoine Pitrou | 524f413 | 2008-07-26 10:29:43 +0000 | [diff] [blame] | 318 | # Test BSD Rune locale's bug for isctype functions. |
Antoine Pitrou | ba54eda | 2008-07-25 20:40:19 +0000 | [diff] [blame] | 319 | |
Antoine Pitrou | 524f413 | 2008-07-26 10:29:43 +0000 | [diff] [blame] | 320 | def test_isspace(self): |
| 321 | self.assertEqual('\x20'.isspace(), True) |
Antoine Pitrou | ba54eda | 2008-07-25 20:40:19 +0000 | [diff] [blame] | 322 | self.assertEqual('\xa0'.isspace(), False) |
Antoine Pitrou | 524f413 | 2008-07-26 10:29:43 +0000 | [diff] [blame] | 323 | self.assertEqual('\xa1'.isspace(), False) |
Antoine Pitrou | ba54eda | 2008-07-25 20:40:19 +0000 | [diff] [blame] | 324 | |
Antoine Pitrou | 524f413 | 2008-07-26 10:29:43 +0000 | [diff] [blame] | 325 | def test_isalpha(self): |
| 326 | self.assertEqual('\xc0'.isalpha(), False) |
Antoine Pitrou | ba54eda | 2008-07-25 20:40:19 +0000 | [diff] [blame] | 327 | |
Antoine Pitrou | 524f413 | 2008-07-26 10:29:43 +0000 | [diff] [blame] | 328 | def test_isalnum(self): |
| 329 | self.assertEqual('\xc0'.isalnum(), False) |
Antoine Pitrou | ba54eda | 2008-07-25 20:40:19 +0000 | [diff] [blame] | 330 | |
Antoine Pitrou | 524f413 | 2008-07-26 10:29:43 +0000 | [diff] [blame] | 331 | def test_isupper(self): |
| 332 | self.assertEqual('\xc0'.isupper(), False) |
Antoine Pitrou | ba54eda | 2008-07-25 20:40:19 +0000 | [diff] [blame] | 333 | |
Antoine Pitrou | 524f413 | 2008-07-26 10:29:43 +0000 | [diff] [blame] | 334 | def test_islower(self): |
| 335 | self.assertEqual('\xc0'.islower(), False) |
Antoine Pitrou | ba54eda | 2008-07-25 20:40:19 +0000 | [diff] [blame] | 336 | |
Antoine Pitrou | 524f413 | 2008-07-26 10:29:43 +0000 | [diff] [blame] | 337 | def test_lower(self): |
| 338 | self.assertEqual('\xcc\x85'.lower(), '\xcc\x85') |
Antoine Pitrou | ba54eda | 2008-07-25 20:40:19 +0000 | [diff] [blame] | 339 | |
Antoine Pitrou | 524f413 | 2008-07-26 10:29:43 +0000 | [diff] [blame] | 340 | def test_upper(self): |
| 341 | self.assertEqual('\xed\x95\xa0'.upper(), '\xed\x95\xa0') |
Antoine Pitrou | ba54eda | 2008-07-25 20:40:19 +0000 | [diff] [blame] | 342 | |
Antoine Pitrou | 524f413 | 2008-07-26 10:29:43 +0000 | [diff] [blame] | 343 | def test_strip(self): |
| 344 | self.assertEqual('\xed\x95\xa0'.strip(), '\xed\x95\xa0') |
Antoine Pitrou | ba54eda | 2008-07-25 20:40:19 +0000 | [diff] [blame] | 345 | |
Antoine Pitrou | 524f413 | 2008-07-26 10:29:43 +0000 | [diff] [blame] | 346 | def test_split(self): |
| 347 | self.assertEqual('\xec\xa0\xbc'.split(), ['\xec\xa0\xbc']) |
Antoine Pitrou | ba54eda | 2008-07-25 20:40:19 +0000 | [diff] [blame] | 348 | |
| 349 | |
| 350 | class TestMiscellaneous(unittest.TestCase): |
| 351 | def test_getpreferredencoding(self): |
| 352 | # Invoke getpreferredencoding to make sure it does not cause exceptions. |
| 353 | enc = locale.getpreferredencoding() |
| 354 | if enc: |
| 355 | # If encoding non-empty, make sure it is valid |
| 356 | codecs.lookup(enc) |
| 357 | |
| 358 | if hasattr(locale, "strcoll"): |
| 359 | def test_strcoll_3303(self): |
| 360 | # test crasher from bug #3303 |
| 361 | self.assertRaises(TypeError, locale.strcoll, u"a", None) |
| 362 | |
Amaury Forgeot d'Arc | d728871 | 2009-12-01 21:51:04 +0000 | [diff] [blame] | 363 | def test_setlocale_category(self): |
| 364 | locale.setlocale(locale.LC_ALL) |
| 365 | locale.setlocale(locale.LC_TIME) |
| 366 | locale.setlocale(locale.LC_CTYPE) |
| 367 | locale.setlocale(locale.LC_COLLATE) |
| 368 | locale.setlocale(locale.LC_MONETARY) |
| 369 | locale.setlocale(locale.LC_NUMERIC) |
| 370 | |
| 371 | # crasher from bug #7419 |
| 372 | self.assertRaises(locale.Error, locale.setlocale, 12345) |
| 373 | |
Antoine Pitrou | ba54eda | 2008-07-25 20:40:19 +0000 | [diff] [blame] | 374 | |
| 375 | def test_main(): |
Antoine Pitrou | 6327e84 | 2008-07-26 11:56:37 +0000 | [diff] [blame] | 376 | tests = [ |
| 377 | TestMiscellaneous, |
R. David Murray | a83da35 | 2009-04-01 03:21:43 +0000 | [diff] [blame] | 378 | TestFormatPatternArg, |
Antoine Pitrou | 6327e84 | 2008-07-26 11:56:37 +0000 | [diff] [blame] | 379 | TestEnUSNumberFormatting, |
Antoine Pitrou | feeafff | 2009-03-14 00:07:21 +0000 | [diff] [blame] | 380 | TestCNumberFormatting, |
| 381 | TestFrFRNumberFormatting, |
Antoine Pitrou | 6327e84 | 2008-07-26 11:56:37 +0000 | [diff] [blame] | 382 | ] |
Benjamin Peterson | 888a39b | 2009-03-26 20:48:25 +0000 | [diff] [blame] | 383 | # SkipTest can't be raised inside unittests, handle it manually instead |
Antoine Pitrou | 524f413 | 2008-07-26 10:29:43 +0000 | [diff] [blame] | 384 | try: |
| 385 | get_enUS_locale() |
Benjamin Peterson | 7fa9459 | 2009-03-26 21:44:43 +0000 | [diff] [blame] | 386 | except unittest.SkipTest as e: |
Antoine Pitrou | 6327e84 | 2008-07-26 11:56:37 +0000 | [diff] [blame] | 387 | if verbose: |
| 388 | print "Some tests will be disabled: %s" % e |
Antoine Pitrou | 524f413 | 2008-07-26 10:29:43 +0000 | [diff] [blame] | 389 | else: |
| 390 | tests += [TestNumberFormatting, TestStringMethods] |
| 391 | run_unittest(*tests) |
Antoine Pitrou | ba54eda | 2008-07-25 20:40:19 +0000 | [diff] [blame] | 392 | |
| 393 | if __name__ == '__main__': |
| 394 | test_main() |