~ubuntu-branches/ubuntu/maverick/python3.1/maverick

« back to all changes in this revision

Viewing changes to Lib/test/test__locale.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-03-23 00:01:27 UTC
  • Revision ID: james.westby@ubuntu.com-20090323000127-5fstfxju4ufrhthq
Tags: upstream-3.1~a1+20090322
ImportĀ upstreamĀ versionĀ 3.1~a1+20090322

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from test.support import verbose, TestSkipped, run_unittest
 
2
from _locale import (setlocale, LC_ALL, LC_CTYPE, LC_NUMERIC, RADIXCHAR, THOUSEP, nl_langinfo,
 
3
                    localeconv, Error)
 
4
import unittest
 
5
from platform import uname
 
6
 
 
7
if 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")
 
11
 
 
12
candidate_locales = ['es_UY', 'fr_FR', 'fi_FI', 'es_CO', 'pt_PT', 'it_IT',
 
13
    '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',
 
20
    'eu_ES', 'vi_VN', 'af_ZA', 'nb_NO', 'en_DK', 'tg_TJ', 'en_US',
 
21
    'es_ES.ISO8859-1', 'fr_FR.ISO8859-15', 'ru_RU.KOI8-R', 'ko_KR.eucKR']
 
22
 
 
23
# 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 '' .
 
26
known_numerics = {'fr_FR' : (',', ''), 'en_US':('.', ',')}
 
27
 
 
28
class _LocaleTests(unittest.TestCase):
 
29
 
 
30
    def setUp(self):
 
31
        self.oldlocale = setlocale(LC_ALL)
 
32
 
 
33
    def tearDown(self):
 
34
        setlocale(LC_ALL, self.oldlocale)
 
35
 
 
36
    # 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,
 
48
                                    ('', ''))[data_type == 'thousands_sep']
 
49
        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)
 
61
                setlocale(LC_CTYPE, loc)
 
62
            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)
 
73
                setlocale(LC_CTYPE, loc)
 
74
            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
 
82
        for loc in candidate_locales:
 
83
            try:
 
84
                setlocale(LC_NUMERIC, loc)
 
85
                setlocale(LC_CTYPE, loc)
 
86
            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]
 
92
                try:
 
93
                    set_locale = setlocale(LC_NUMERIC)
 
94
                except Error:
 
95
                    set_locale = "<not able to determine>"
 
96
                self.assertEquals(nl_radixchar, li_radixchar,
 
97
                                "%s (nl_langinfo) != %s (localeconv) "
 
98
                                "(set to %s, using %s)" % (
 
99
                                                nl_radixchar, li_radixchar,
 
100
                                                loc, set_locale))
 
101
 
 
102
    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)
 
108
                setlocale(LC_CTYPE, loc)
 
109
            except Error:
 
110
                continue
 
111
 
 
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
 
 
116
            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)
 
120
            if localeconv()['decimal_point'] != '.':
 
121
                self.assertRaises(ValueError, float,
 
122
                                  localeconv()['decimal_point'].join(['1', '23']))
 
123
 
 
124
def test_main():
 
125
    run_unittest(_LocaleTests)
 
126
 
 
127
if __name__ == '__main__':
 
128
    test_main()