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