|
1 | | -from test.support import verbose, TestSkipped, TestFailed |
| 1 | +from test.support import run_unittest, TestSkipped, verbose |
| 2 | +import unittest |
2 | 3 | import locale |
3 | 4 | import sys |
| 5 | +import codecs |
4 | 6 |
|
5 | | -if sys.platform == 'darwin': |
6 | | - raise TestSkipped( |
7 | | - "Locale support on MacOSX is minimal and cannot be tested") |
8 | | -oldlocale = locale.setlocale(locale.LC_NUMERIC) |
9 | | - |
10 | | -if sys.platform.startswith("win"): |
11 | | - tlocs = ("En", "English") |
12 | | -else: |
13 | | - tlocs = ("en_US.UTF-8", "en_US.US-ASCII", "en_US") |
14 | | - |
15 | | -for tloc in tlocs: |
16 | | - try: |
17 | | - locale.setlocale(locale.LC_NUMERIC, tloc) |
18 | | - break |
19 | | - except locale.Error: |
20 | | - continue |
21 | | -else: |
22 | | - raise ImportError( |
23 | | - "test locale not supported (tried %s)" % (', '.join(tlocs))) |
24 | | - |
25 | | -def testformat(formatstr, value, grouping = 0, output=None, func=locale.format): |
26 | | - if verbose: |
27 | | - if output: |
28 | | - print("%s %% %s =? %s ..." % |
29 | | - (repr(formatstr), repr(value), repr(output)), end=' ') |
30 | | - else: |
31 | | - print("%s %% %s works? ..." % (repr(formatstr), repr(value)), |
32 | | - end=' ') |
33 | | - result = func(formatstr, value, grouping = grouping) |
34 | | - if output and result != output: |
35 | | - if verbose: |
36 | | - print('no') |
37 | | - print("%s %% %s == %s != %s" % |
38 | | - (repr(formatstr), repr(value), repr(result), repr(output))) |
| 7 | +class BaseLocalizedTest(unittest.TestCase): |
| 8 | + # |
| 9 | + # Base class for tests using a real locale |
| 10 | + # |
| 11 | + |
| 12 | + if sys.platform.startswith("win"): |
| 13 | + tlocs = ("En", "English") |
39 | 14 | else: |
| 15 | + tlocs = ("en_US.UTF-8", "en_US.US-ASCII", "en_US") |
| 16 | + |
| 17 | + 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 |
| 28 | + else: |
| 29 | + raise TestSkipped( |
| 30 | + "Test locale not supported (tried %s)" % (', '.join(self.tlocs))) |
40 | 31 | if verbose: |
41 | | - print("yes") |
42 | | - |
43 | | -try: |
44 | | - # On Solaris 10, the thousands_sep is the empty string |
45 | | - sep = locale.localeconv()['thousands_sep'] |
46 | | - testformat("%f", 1024, grouping=1, output='1%s024.000000' % sep) |
47 | | - testformat("%f", 102, grouping=1, output='102.000000') |
48 | | - testformat("%f", -42, grouping=1, output='-42.000000') |
49 | | - testformat("%+f", -42, grouping=1, output='-42.000000') |
50 | | - testformat("%20.f", -42, grouping=1, output=' -42') |
51 | | - testformat("%+10.f", -4200, grouping=1, output=' -4%s200' % sep) |
52 | | - testformat("%-10.f", 4200, grouping=1, output='4%s200 ' % sep) |
53 | | - # Invoke getpreferredencoding to make sure it does not cause exceptions, |
54 | | - locale.getpreferredencoding() |
55 | | - |
56 | | - # === Test format() with more complex formatting strings |
57 | | - # test if grouping is independent from other characters in formatting string |
58 | | - testformat("One million is %i", 1000000, grouping=1, |
59 | | - output='One million is 1%s000%s000' % (sep, sep), |
60 | | - func=locale.format_string) |
61 | | - testformat("One million is %i", 1000000, grouping=1, |
62 | | - output='One million is 1%s000%s000' % (sep, sep), |
63 | | - func=locale.format_string) |
64 | | - # test dots in formatting string |
65 | | - testformat(".%f.", 1000.0, output='.1000.000000.', func=locale.format_string) |
66 | | - # test floats |
67 | | - testformat("--> %10.2f", 1000.0, grouping=1, output='--> 1%s000.00' % sep, |
68 | | - func=locale.format_string) |
69 | | - # test asterisk formats |
70 | | - testformat("%10.*f", (2, 1000.0), grouping=0, output=' 1000.00', |
71 | | - func=locale.format_string) |
72 | | - testformat("%*.*f", (10, 2, 1000.0), grouping=1, output=' 1%s000.00' % sep, |
73 | | - func=locale.format_string) |
74 | | - # test more-in-one |
75 | | - testformat("int %i float %.2f str %s", (1000, 1000.0, 'str'), grouping=1, |
76 | | - output='int 1%s000 float 1%s000.00 str str' % (sep, sep), |
77 | | - func=locale.format_string) |
78 | | - |
79 | | -finally: |
80 | | - locale.setlocale(locale.LC_NUMERIC, oldlocale) |
81 | | - |
82 | | -if hasattr(locale, "strcoll"): |
83 | | - # test crasher from bug #3303 |
84 | | - try: |
85 | | - locale.strcoll("a", None) |
86 | | - except TypeError: |
87 | | - pass |
88 | | - else: |
89 | | - raise TestFailed("TypeError not raised") |
| 32 | + print("testing with \"%s\"..." % tloc, end=' ') |
| 33 | + |
| 34 | + def tearDown(self): |
| 35 | + locale.setlocale(self.locale_type, self.oldlocale) |
| 36 | + |
| 37 | + |
| 38 | +class BaseCookedTest(unittest.TestCase): |
| 39 | + # |
| 40 | + # Base class for tests using cooked localeconv() values |
| 41 | + # |
| 42 | + |
| 43 | + def setUp(self): |
| 44 | + locale._override_localeconv = self.cooked_values |
| 45 | + |
| 46 | + def tearDown(self): |
| 47 | + locale._override_localeconv = {} |
| 48 | + |
| 49 | +class 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 | + |
| 73 | +class 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 | + |
| 98 | +class 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 | + |
| 119 | +class 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 | + |
| 176 | +class 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 | + |
| 186 | +class 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 | + |
| 200 | +class 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 | + |
| 210 | +class 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 | + |
| 225 | +def test_main(): |
| 226 | + run_unittest(__name__) |
| 227 | + |
| 228 | +if __name__ == '__main__': |
| 229 | + test_main() |
0 commit comments