From 02132ffcd837a91aa09b2ff73a8ae67695f03399 Mon Sep 17 00:00:00 2001 From: Alex Morega Date: Fri, 5 Jul 2013 19:33:47 +0200 Subject: [PATCH] copy babel.core doctests as unit tests --- babel/core.py | 2 +- tests/test_core.py | 198 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 197 insertions(+), 3 deletions(-) diff --git a/babel/core.py b/babel/core.py index a72684dd..dc36a06c 100644 --- a/babel/core.py +++ b/babel/core.py @@ -140,7 +140,7 @@ class Locale(object): def default(cls, category=None, aliases=LOCALE_ALIASES): """Return the system default locale for the specified category. - >>> for name in ['LANGUAGE', 'LC_ALL', 'LC_CTYPE']: + >>> for name in ['LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LC_MESSAGES']: ... os.environ[name] = '' >>> os.environ['LANG'] = 'fr_FR.UTF-8' >>> Locale.default('LC_MESSAGES') diff --git a/tests/test_core.py b/tests/test_core.py index 95395b5a..83b80230 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -14,8 +14,9 @@ import doctest import os import unittest +import pytest -from babel import core +from babel import core, Locale from babel.core import default_locale, Locale @@ -77,9 +78,202 @@ class DefaultLocaleTest(LocaleEnvironmentTestMixin, unittest.TestCase): default_locale('LC_CTYPE') +def test_get_global(): + assert core.get_global('zone_aliases')['UTC'] == 'Etc/GMT' + assert core.get_global('zone_territories')['Europe/Berlin'] == 'DE' + + +class TestLocaleClass: + + def test_repr(self): + assert repr(Locale('en', 'US')) == "Locale('en', territory='US')" + + def test_attributes(self): + locale = Locale('en', 'US') + assert locale.language == 'en' + assert locale.territory == 'US' + + def test_default(self): + # TODO isolate this test + for name in ['LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LC_MESSAGES']: + os.environ[name] = '' + os.environ['LANG'] = 'fr_FR.UTF-8' + default = Locale.default('LC_MESSAGES') + assert (default.language, default.territory) == ('fr', 'FR') + + def test_negotiate(self): + de_DE = Locale.negotiate(['de_DE', 'en_US'], ['de_DE', 'de_AT']) + assert (de_DE.language, de_DE.territory) == ('de', 'DE') + de = Locale.negotiate(['de_DE', 'en_US'], ['en', 'de']) + assert (de.language, de.territory) == ('de', None) + nothing = Locale.negotiate(['de_DE', 'de'], ['en_US']) + assert nothing is None + + def test_negotiate_custom_separator(self): + de_DE = Locale.negotiate(['de-DE', 'de'], ['en-us', 'de-de'], sep='-') + assert (de_DE.language, de_DE.territory) == ('de', 'DE') + + def test_parse(self): + l = Locale.parse('de-DE', sep='-') + assert l.display_name == 'Deutsch (Deutschland)' + + de_DE = Locale.parse(l) + assert (de_DE.language, de_DE.territory) == ('de', 'DE') + + def test_get_display_name(self): + zh_CN = Locale('zh', 'CN', script='Hans') + assert zh_CN.get_display_name('en') == 'Chinese (Simplified, China)' + + def test_display_name_property(self): + assert Locale('en').display_name == 'English' + assert Locale('en', 'US').display_name == 'English (United States)' + assert Locale('sv').display_name == 'svenska' + + def test_english_name_property(self): + assert Locale('de').english_name == 'German' + assert Locale('de', 'DE').english_name == 'German (Germany)' + + def test_languages_property(self): + assert Locale('de', 'DE').languages['ja'] == 'Japanisch' + + def test_scripts_property(self): + assert Locale('en', 'US').scripts['Hira'] == 'Hiragana' + + def test_territories_property(self): + assert Locale('es', 'CO').territories['DE'] == 'Alemania' + + def test_variants_property(self): + assert (Locale('de', 'DE').variants['1901'] == + 'Alte deutsche Rechtschreibung') + + def test_currencies_property(self): + assert Locale('en').currencies['COP'] == 'Colombian Peso' + assert Locale('de', 'DE').currencies['COP'] == 'Kolumbianischer Peso' + + def test_currency_symbols_property(self): + assert Locale('en', 'US').currency_symbols['USD'] == '$' + assert Locale('es', 'CO').currency_symbols['USD'] == 'US$' + + def test_number_symbols_property(self): + assert Locale('fr', 'FR').number_symbols['decimal'] == ',' + + def test_decimal_formats(self): + assert Locale('en', 'US').decimal_formats[None].pattern == '#,##0.###' + + def test_currency_formats_property(self): + assert (Locale('en', 'US').currency_formats[None].pattern == + u'\xa4#,##0.00') + + def test_percent_formats_property(self): + assert Locale('en', 'US').percent_formats[None].pattern == '#,##0%' + + def test_scientific_formats_property(self): + assert Locale('en', 'US').scientific_formats[None].pattern == '#E0' + + def test_periods_property(self): + assert Locale('en', 'US').periods['am'] == 'AM' + + def test_days_property(self): + assert Locale('de', 'DE').days['format']['wide'][3] == 'Donnerstag' + + def test_months_property(self): + assert Locale('de', 'DE').months['format']['wide'][10] == 'Oktober' + + def test_quarters_property(self): + assert Locale('de', 'DE').quarters['format']['wide'][1] == '1. Quartal' + + def test_eras_property(self): + assert Locale('en', 'US').eras['wide'][1] == 'Anno Domini' + assert Locale('en', 'US').eras['abbreviated'][0] == 'BC' + + def test_time_zones_property(self): + time_zones = Locale('en', 'US').time_zones + assert (time_zones['Europe/London']['long']['daylight'] == + 'British Summer Time') + assert time_zones['America/St_Johns']['city'] == u'St. John\u2019s' + + def test_meta_zones_property(self): + meta_zones = Locale('en', 'US').meta_zones + assert (meta_zones['Europe_Central']['long']['daylight'] == + 'Central European Summer Time') + + def test_zone_formats_property(self): + assert Locale('en', 'US').zone_formats['fallback'] == '%(1)s (%(0)s)' + assert Locale('pt', 'BR').zone_formats['region'] == u'Hor\xe1rio %s' + + def test_first_week_day_property(self): + assert Locale('de', 'DE').first_week_day == 0 + assert Locale('en', 'US').first_week_day == 6 + + def test_weekend_start_property(self): + assert Locale('de', 'DE').weekend_start == 5 + + def test_weekend_end_property(self): + assert Locale('de', 'DE').weekend_end == 6 + + def test_min_week_days_property(self): + assert Locale('de', 'DE').min_week_days == 4 + + def test_date_formats_property(self): + assert Locale('en', 'US').date_formats['short'].pattern == 'M/d/yy' + assert Locale('fr', 'FR').date_formats['long'].pattern == 'd MMMM y' + + def test_time_formats_property(self): + assert Locale('en', 'US').time_formats['short'].pattern == 'h:mm a' + assert Locale('fr', 'FR').time_formats['long'].pattern == 'HH:mm:ss z' + + def test_datetime_formats_property(self): + assert Locale('en').datetime_formats['full'] == u"{1} 'at' {0}" + assert Locale('th').datetime_formats['medium'] == u'{1}, {0}' + + def test_plural_form_property(self): + assert Locale('en').plural_form(1) == 'one' + assert Locale('en').plural_form(0) == 'other' + assert Locale('fr').plural_form(0) == 'one' + assert Locale('ru').plural_form(100) == 'many' + + +def test_default_locale(): + # TODO isolate this test + for name in ['LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LC_MESSAGES']: + os.environ[name] = '' + os.environ['LANG'] = 'fr_FR.UTF-8' + assert default_locale('LC_MESSAGES') == 'fr_FR' + + os.environ['LC_MESSAGES'] = 'POSIX' + assert default_locale('LC_MESSAGES') == 'en_US_POSIX' + + +def test_negotiate_locale(): + assert (core.negotiate_locale(['de_DE', 'en_US'], ['de_DE', 'de_AT']) == + 'de_DE') + assert core.negotiate_locale(['de_DE', 'en_US'], ['en', 'de']) == 'de' + assert (core.negotiate_locale(['de_DE', 'en_US'], ['de_de', 'de_at']) == + 'de_DE') + assert (core.negotiate_locale(['de_DE', 'en_US'], ['de_de', 'de_at']) == + 'de_DE') + assert (core.negotiate_locale(['ja', 'en_US'], ['ja_JP', 'en_US']) == + 'ja_JP') + assert core.negotiate_locale(['no', 'sv'], ['nb_NO', 'sv_SE']) == 'nb_NO' + +def test_parse_locale(): + assert core.parse_locale('zh_CN') == ('zh', 'CN', None, None) + assert core.parse_locale('zh_Hans_CN') == ('zh', 'CN', 'Hans', None) + assert core.parse_locale('zh-CN', sep='-') == ('zh', 'CN', None, None) + + with pytest.raises(ValueError) as excinfo: + core.parse_locale('not_a_LOCALE_String') + assert (excinfo.value.message == + "'not_a_LOCALE_String' is not a valid locale identifier") + + assert core.parse_locale('it_IT@euro') == ('it', 'IT', None, None) + assert core.parse_locale('en_US.UTF-8') == ('en', 'US', None, None) + assert (core.parse_locale('de_DE.iso885915@euro') == + ('de', 'DE', None, None)) + + def suite(): suite = unittest.TestSuite() - suite.addTest(doctest.DocTestSuite(core)) suite.addTest(unittest.makeSuite(LocaleTest)) suite.addTest(unittest.makeSuite(DefaultLocaleTest)) return suite -- 2.47.2