blob: aed273d1e63405db71bc25b87c93ce78427edc49 [file] [log] [blame]
Brett Cannon2ad68e62004-09-06 23:30:27 +00001from test.test_support import verbose, TestSkipped, run_unittest
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +00002from _locale import (setlocale, LC_ALL, LC_CTYPE, LC_NUMERIC, RADIXCHAR, THOUSEP, nl_langinfo,
Brett Cannon2ad68e62004-09-06 23:30:27 +00003 localeconv, Error)
Brett Cannon2ad68e62004-09-06 23:30:27 +00004import unittest
Skip Montanarof8948ca2005-09-19 03:54:46 +00005from platform import uname
6
7if uname()[0] == "Darwin":
8 maj, min, mic = [int(part) for part in uname()[2].split(".")]
9 if (maj, min, mic) < (8, 0, 0):
10 raise TestSkipped("locale support broken for OS X < 10.4")
Martin v. Löwisf5b93732003-09-04 18:24:47 +000011
12candidate_locales = ['es_UY', 'fr_FR', 'fi_FI', 'es_CO', 'pt_PT', 'it_IT',
Jeremy Hyltonb7b1db92003-09-10 20:19:54 +000013 'et_EE', 'es_PY', 'no_NO', 'nl_NL', 'lv_LV', 'el_GR', 'be_BY', 'fr_BE',
14 'ro_RO', 'ru_UA', 'ru_RU', 'es_VE', 'ca_ES', 'se_NO', 'es_EC', 'id_ID',
15 'ka_GE', 'es_CL', 'hu_HU', 'wa_BE', 'lt_LT', 'sl_SI', 'hr_HR', 'es_AR',
16 'es_ES', 'oc_FR', 'gl_ES', 'bg_BG', 'is_IS', 'mk_MK', 'de_AT', 'pt_BR',
17 'da_DK', 'nn_NO', 'cs_CZ', 'de_LU', 'es_BO', 'sq_AL', 'sk_SK', 'fr_CH',
18 'de_DE', 'sr_YU', 'br_FR', 'nl_BE', 'sv_FI', 'pl_PL', 'fr_CA', 'fo_FO',
19 'bs_BA', 'fr_LU', 'kl_GL', 'fa_IR', 'de_BE', 'sv_SE', 'it_CH', 'uk_UA',
Brett Cannone94e74a2005-03-01 03:15:50 +000020 'eu_ES', 'vi_VN', 'af_ZA', 'nb_NO', 'en_DK', 'tg_TJ', 'en_US',
Hye-Shik Chang8d2e08d2003-12-19 01:16:03 +000021 'es_ES.ISO8859-1', 'fr_FR.ISO8859-15', 'ru_RU.KOI8-R', 'ko_KR.eucKR']
Martin v. Löwisf5b93732003-09-04 18:24:47 +000022
Brett Cannone94e74a2005-03-01 03:15:50 +000023# List known locale values to test against when available.
24# Dict formatted as ``<locale> : (<decimal_point>, <thousands_sep>)``. If a
25# value is not known, use '' .
26known_numerics = {'fr_FR' : (',', ''), 'en_US':('.', ',')}
27
Brett Cannon2ad68e62004-09-06 23:30:27 +000028class _LocaleTests(unittest.TestCase):
29
30 def setUp(self):
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +000031 self.oldlocale = setlocale(LC_ALL)
Brett Cannon2ad68e62004-09-06 23:30:27 +000032
33 def tearDown(self):
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +000034 setlocale(LC_ALL, self.oldlocale)
Brett Cannon2ad68e62004-09-06 23:30:27 +000035
Brett Cannone94e74a2005-03-01 03:15:50 +000036 # Want to know what value was calculated, what it was compared against,
37 # what function was used for the calculation, what type of data was used,
38 # the locale that was supposedly set, and the actual locale that is set.
39 lc_numeric_err_msg = "%s != %s (%s for %s; set to %s, using %s)"
40
41 def numeric_tester(self, calc_type, calc_value, data_type, used_locale):
42 """Compare calculation against known value, if available"""
43 try:
44 set_locale = setlocale(LC_NUMERIC)
45 except Error:
46 set_locale = "<not able to determine>"
47 known_value = known_numerics.get(used_locale,
Guido van Rossumf40e5762007-05-17 20:58:33 +000048 ('', ''))[data_type == 'thousands_sep']
Brett Cannone94e74a2005-03-01 03:15:50 +000049 if known_value and calc_value:
50 self.assertEquals(calc_value, known_value,
51 self.lc_numeric_err_msg % (
52 calc_value, known_value,
53 calc_type, data_type, set_locale,
54 used_locale))
55
56 def test_lc_numeric_nl_langinfo(self):
57 # Test nl_langinfo against known values
58 for loc in candidate_locales:
59 try:
60 setlocale(LC_NUMERIC, loc)
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +000061 setlocale(LC_CTYPE, loc)
Brett Cannone94e74a2005-03-01 03:15:50 +000062 except Error:
63 continue
64 for li, lc in ((RADIXCHAR, "decimal_point"),
65 (THOUSEP, "thousands_sep")):
66 self.numeric_tester('nl_langinfo', nl_langinfo(li), lc, loc)
67
68 def test_lc_numeric_localeconv(self):
69 # Test localeconv against known values
70 for loc in candidate_locales:
71 try:
72 setlocale(LC_NUMERIC, loc)
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +000073 setlocale(LC_CTYPE, loc)
Brett Cannone94e74a2005-03-01 03:15:50 +000074 except Error:
75 continue
76 for li, lc in ((RADIXCHAR, "decimal_point"),
77 (THOUSEP, "thousands_sep")):
78 self.numeric_tester('localeconv', localeconv()[lc], lc, loc)
79
80 def test_lc_numeric_basic(self):
81 # Test nl_langinfo against localeconv
Brett Cannon2ad68e62004-09-06 23:30:27 +000082 for loc in candidate_locales:
83 try:
84 setlocale(LC_NUMERIC, loc)
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +000085 setlocale(LC_CTYPE, loc)
Brett Cannon2ad68e62004-09-06 23:30:27 +000086 except Error:
87 continue
88 for li, lc in ((RADIXCHAR, "decimal_point"),
89 (THOUSEP, "thousands_sep")):
90 nl_radixchar = nl_langinfo(li)
91 li_radixchar = localeconv()[lc]
Brett Cannon85ae1a62004-09-08 02:02:41 +000092 try:
93 set_locale = setlocale(LC_NUMERIC)
94 except Error:
95 set_locale = "<not able to determine>"
Brett Cannon2ad68e62004-09-06 23:30:27 +000096 self.assertEquals(nl_radixchar, li_radixchar,
Brett Cannone94e74a2005-03-01 03:15:50 +000097 "%s (nl_langinfo) != %s (localeconv) "
98 "(set to %s, using %s)" % (
99 nl_radixchar, li_radixchar,
100 loc, set_locale))
101
Fredrik Lundh24f0fa92005-12-29 20:35:52 +0000102 def test_float_parsing(self):
103 # Bug #1391872: Test whether float parsing is okay on European
104 # locales.
105 for loc in candidate_locales:
106 try:
107 setlocale(LC_NUMERIC, loc)
Martin v. Löwisfe92d0b2008-03-10 10:18:53 +0000108 setlocale(LC_CTYPE, loc)
Fredrik Lundh24f0fa92005-12-29 20:35:52 +0000109 except Error:
110 continue
Neal Norwitzbb459732006-02-19 00:13:15 +0000111
112 # Ignore buggy locale databases. (Mac OS 10.4 and some other BSDs)
113 if loc == 'eu_ES' and localeconv()['decimal_point'] == "' ":
114 continue
115
Brett Cannon2dbf2a92006-01-19 07:09:09 +0000116 self.assertEquals(int(eval('3.14') * 100), 314,
117 "using eval('3.14') failed for %s" % loc)
118 self.assertEquals(int(float('3.14') * 100), 314,
119 "using float('3.14') failed for %s" % loc)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000120 if localeconv()['decimal_point'] != '.':
121 self.assertRaises(ValueError, float,
122 localeconv()['decimal_point'].join(['1', '23']))
Fredrik Lundh24f0fa92005-12-29 20:35:52 +0000123
Brett Cannon2ad68e62004-09-06 23:30:27 +0000124def test_main():
125 run_unittest(_LocaleTests)
126
127if __name__ == '__main__':
128 test_main()