[Python-checkins] python/dist/src/Lib/test test__locale.py,1.8,1.9

bcannon at users.sourceforge.net bcannon at users.sourceforge.net
Tue Mar 1 04:15:54 CET 2005


Update of /cvsroot/python/python/dist/src/Lib/test
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23998/Lib/test

Modified Files:
	test__locale.py 
Log Message:
Make test__locale more fine-grained.  Now test localeconv and nl_langinfo
individually as tests.

Also improved output when the test fails.


Index: test__locale.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test__locale.py,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- test__locale.py	8 Sep 2004 02:02:41 -0000	1.8
+++ test__locale.py	1 Mar 2005 03:15:50 -0000	1.9
@@ -11,9 +11,14 @@
     'da_DK', 'nn_NO', 'cs_CZ', 'de_LU', 'es_BO', 'sq_AL', 'sk_SK', 'fr_CH',
     'de_DE', 'sr_YU', 'br_FR', 'nl_BE', 'sv_FI', 'pl_PL', 'fr_CA', 'fo_FO',
     'bs_BA', 'fr_LU', 'kl_GL', 'fa_IR', 'de_BE', 'sv_SE', 'it_CH', 'uk_UA',
-    'eu_ES', 'vi_VN', 'af_ZA', 'nb_NO', 'en_DK', 'tg_TJ',
+    'eu_ES', 'vi_VN', 'af_ZA', 'nb_NO', 'en_DK', 'tg_TJ', 'en_US',
     'es_ES.ISO8859-1', 'fr_FR.ISO8859-15', 'ru_RU.KOI8-R', 'ko_KR.eucKR']
 
+# List known locale values to test against when available.
+# Dict formatted as ``<locale> : (<decimal_point>, <thousands_sep>)``.  If a
+# value is not known, use '' .
+known_numerics = {'fr_FR' : (',', ''), 'en_US':('.', ',')}
+
 class _LocaleTests(unittest.TestCase):
 
     def setUp(self):
@@ -22,7 +27,50 @@
     def tearDown(self):
         setlocale(LC_NUMERIC, self.oldlocale)
 
-    def test_lc_numeric(self):
+    # Want to know what value was calculated, what it was compared against,
+    # what function was used for the calculation, what type of data was used,
+    # the locale that was supposedly set, and the actual locale that is set.
+    lc_numeric_err_msg = "%s != %s (%s for %s; set to %s, using %s)"
+
+    def numeric_tester(self, calc_type, calc_value, data_type, used_locale):
+        """Compare calculation against known value, if available"""
+        try:
+            set_locale = setlocale(LC_NUMERIC)
+        except Error:
+            set_locale = "<not able to determine>"
+        known_value = known_numerics.get(used_locale,
+                                    ('', ''))[data_type is 'thousands_sep']
+        if known_value and calc_value:
+            self.assertEquals(calc_value, known_value,
+                                self.lc_numeric_err_msg % (
+                                    calc_value, known_value,
+                                    calc_type, data_type, set_locale,
+                                    used_locale))
+
+    def test_lc_numeric_nl_langinfo(self):
+        # Test nl_langinfo against known values
+        for loc in candidate_locales:
+            try:
+                setlocale(LC_NUMERIC, loc)
+            except Error:
+                continue
+            for li, lc in ((RADIXCHAR, "decimal_point"),
+                            (THOUSEP, "thousands_sep")):
+                self.numeric_tester('nl_langinfo', nl_langinfo(li), lc, loc)
+
+    def test_lc_numeric_localeconv(self):
+        # Test localeconv against known values
+        for loc in candidate_locales:
+            try:
+                setlocale(LC_NUMERIC, loc)
+            except Error:
+                continue
+            for li, lc in ((RADIXCHAR, "decimal_point"),
+                            (THOUSEP, "thousands_sep")):
+                self.numeric_tester('localeconv', localeconv()[lc], lc, loc)
+
+    def test_lc_numeric_basic(self):
+        # Test nl_langinfo against localeconv
         for loc in candidate_locales:
             try:
                 setlocale(LC_NUMERIC, loc)
@@ -32,18 +80,17 @@
                             (THOUSEP, "thousands_sep")):
                 nl_radixchar = nl_langinfo(li)
                 li_radixchar = localeconv()[lc]
-                # Both with seeing what the locale is set to in order to detect
-                # when setlocale lies and says it accepted the locale setting
-                # but in actuality didn't use it (as seen in OS X 10.3)
                 try:
                     set_locale = setlocale(LC_NUMERIC)
                 except Error:
                     set_locale = "<not able to determine>"
                 self.assertEquals(nl_radixchar, li_radixchar,
-                                    "%s != %s (%s); "
-                                    "supposed to be %s, set to %s" %
-                                        (nl_radixchar, li_radixchar, lc,
-                                         loc, set_locale))
+                                "%s (nl_langinfo) != %s (localeconv) "
+                                "(set to %s, using %s)" % (
+                                                nl_radixchar, li_radixchar,
+                                                loc, set_locale))
+
+
 
 def test_main():
     run_unittest(_LocaleTests)



More information about the Python-checkins mailing list