/Lib/test/test__locale.py

http://unladen-swallow.googlecode.com/ · Python · 124 lines · 95 code · 16 blank · 13 comment · 28 complexity · 3b315aaf3d7c4da1c3a4153e37d48d6c MD5 · raw file

  1. from test.test_support import verbose, TestSkipped, run_unittest
  2. from _locale import (setlocale, LC_NUMERIC, RADIXCHAR, THOUSEP, nl_langinfo,
  3. localeconv, Error)
  4. import unittest
  5. from platform import uname
  6. if uname()[0] == "Darwin":
  7. maj, min, mic = [int(part) for part in uname()[2].split(".")]
  8. if (maj, min, mic) < (8, 0, 0):
  9. raise TestSkipped("locale support broken for OS X < 10.4")
  10. candidate_locales = ['es_UY', 'fr_FR', 'fi_FI', 'es_CO', 'pt_PT', 'it_IT',
  11. 'et_EE', 'es_PY', 'no_NO', 'nl_NL', 'lv_LV', 'el_GR', 'be_BY', 'fr_BE',
  12. 'ro_RO', 'ru_UA', 'ru_RU', 'es_VE', 'ca_ES', 'se_NO', 'es_EC', 'id_ID',
  13. 'ka_GE', 'es_CL', 'hu_HU', 'wa_BE', 'lt_LT', 'sl_SI', 'hr_HR', 'es_AR',
  14. 'es_ES', 'oc_FR', 'gl_ES', 'bg_BG', 'is_IS', 'mk_MK', 'de_AT', 'pt_BR',
  15. 'da_DK', 'nn_NO', 'cs_CZ', 'de_LU', 'es_BO', 'sq_AL', 'sk_SK', 'fr_CH',
  16. 'de_DE', 'sr_YU', 'br_FR', 'nl_BE', 'sv_FI', 'pl_PL', 'fr_CA', 'fo_FO',
  17. 'bs_BA', 'fr_LU', 'kl_GL', 'fa_IR', 'de_BE', 'sv_SE', 'it_CH', 'uk_UA',
  18. 'eu_ES', 'vi_VN', 'af_ZA', 'nb_NO', 'en_DK', 'tg_TJ', 'en_US',
  19. 'es_ES.ISO8859-1', 'fr_FR.ISO8859-15', 'ru_RU.KOI8-R', 'ko_KR.eucKR']
  20. # List known locale values to test against when available.
  21. # Dict formatted as ``<locale> : (<decimal_point>, <thousands_sep>)``. If a
  22. # value is not known, use '' .
  23. known_numerics = {'fr_FR' : (',', ''), 'en_US':('.', ',')}
  24. class _LocaleTests(unittest.TestCase):
  25. def setUp(self):
  26. self.oldlocale = setlocale(LC_NUMERIC)
  27. def tearDown(self):
  28. setlocale(LC_NUMERIC, self.oldlocale)
  29. # Want to know what value was calculated, what it was compared against,
  30. # what function was used for the calculation, what type of data was used,
  31. # the locale that was supposedly set, and the actual locale that is set.
  32. lc_numeric_err_msg = "%s != %s (%s for %s; set to %s, using %s)"
  33. def numeric_tester(self, calc_type, calc_value, data_type, used_locale):
  34. """Compare calculation against known value, if available"""
  35. try:
  36. set_locale = setlocale(LC_NUMERIC)
  37. except Error:
  38. set_locale = "<not able to determine>"
  39. known_value = known_numerics.get(used_locale,
  40. ('', ''))[data_type is 'thousands_sep']
  41. if known_value and calc_value:
  42. self.assertEquals(calc_value, known_value,
  43. self.lc_numeric_err_msg % (
  44. calc_value, known_value,
  45. calc_type, data_type, set_locale,
  46. used_locale))
  47. def test_lc_numeric_nl_langinfo(self):
  48. # Test nl_langinfo against known values
  49. for loc in candidate_locales:
  50. try:
  51. setlocale(LC_NUMERIC, loc)
  52. except Error:
  53. continue
  54. for li, lc in ((RADIXCHAR, "decimal_point"),
  55. (THOUSEP, "thousands_sep")):
  56. self.numeric_tester('nl_langinfo', nl_langinfo(li), lc, loc)
  57. def test_lc_numeric_localeconv(self):
  58. # Test localeconv against known values
  59. for loc in candidate_locales:
  60. try:
  61. setlocale(LC_NUMERIC, loc)
  62. except Error:
  63. continue
  64. for li, lc in ((RADIXCHAR, "decimal_point"),
  65. (THOUSEP, "thousands_sep")):
  66. self.numeric_tester('localeconv', localeconv()[lc], lc, loc)
  67. def test_lc_numeric_basic(self):
  68. # Test nl_langinfo against localeconv
  69. for loc in candidate_locales:
  70. try:
  71. setlocale(LC_NUMERIC, loc)
  72. except Error:
  73. continue
  74. for li, lc in ((RADIXCHAR, "decimal_point"),
  75. (THOUSEP, "thousands_sep")):
  76. nl_radixchar = nl_langinfo(li)
  77. li_radixchar = localeconv()[lc]
  78. try:
  79. set_locale = setlocale(LC_NUMERIC)
  80. except Error:
  81. set_locale = "<not able to determine>"
  82. self.assertEquals(nl_radixchar, li_radixchar,
  83. "%s (nl_langinfo) != %s (localeconv) "
  84. "(set to %s, using %s)" % (
  85. nl_radixchar, li_radixchar,
  86. loc, set_locale))
  87. def test_float_parsing(self):
  88. # Bug #1391872: Test whether float parsing is okay on European
  89. # locales.
  90. for loc in candidate_locales:
  91. try:
  92. setlocale(LC_NUMERIC, loc)
  93. except Error:
  94. continue
  95. # Ignore buggy locale databases. (Mac OS 10.4 and some other BSDs)
  96. if loc == 'eu_ES' and localeconv()['decimal_point'] == "' ":
  97. continue
  98. self.assertEquals(int(eval('3.14') * 100), 314,
  99. "using eval('3.14') failed for %s" % loc)
  100. self.assertEquals(int(float('3.14') * 100), 314,
  101. "using float('3.14') failed for %s" % loc)
  102. if localeconv()['decimal_point'] != '.':
  103. self.assertRaises(ValueError, float,
  104. localeconv()['decimal_point'].join(['1', '23']))
  105. def test_main():
  106. run_unittest(_LocaleTests)
  107. if __name__ == '__main__':
  108. test_main()