summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorArmin Ronacher <armin.ronacher@active-4.com>2013-07-06 16:23:44 +0200
committerArmin Ronacher <armin.ronacher@active-4.com>2013-07-06 16:23:44 +0200
commit2a45afc078647858857c8238f2effd928b1f0371 (patch)
tree4906767f109c51421e251589b73247e42985b360 /tests
parentdc72dedd878feb47a319474b34d2e3179715a6e7 (diff)
parentea4a6ae6f149cc354ab71a33235e3be2de101323 (diff)
downloadbabel-2a45afc078647858857c8238f2effd928b1f0371.tar.gz
Merge branch 'pr/18'
Diffstat (limited to 'tests')
-rw-r--r--tests/conftest.py9
-rw-r--r--tests/test_core.py90
-rw-r--r--tests/test_plural.py22
-rw-r--r--tests/test_support.py3
4 files changed, 64 insertions, 60 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
new file mode 100644
index 0000000..117a130
--- /dev/null
+++ b/tests/conftest.py
@@ -0,0 +1,9 @@
+import os
+import pytest
+
+
+@pytest.fixture
+def os_environ(monkeypatch):
+ mock_environ = dict(os.environ)
+ monkeypatch.setattr(os, 'environ', mock_environ)
+ return mock_environ
diff --git a/tests/test_core.py b/tests/test_core.py
index f00a13d..9341ca8 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -12,7 +12,6 @@
# history and logs, available at http://babel.edgewall.org/log/.
import doctest
-import os
import unittest
import pytest
@@ -20,62 +19,35 @@ from babel import core, Locale
from babel.core import default_locale, Locale
-class LocaleEnvironmentTestMixin(object):
+def test_locale_provides_access_to_cldr_locale_data():
+ locale = Locale('en', 'US')
+ assert u'English (United States)' == locale.display_name
+ assert u'.' == locale.number_symbols['decimal']
- def setUp(self):
- self._old_locale_settings = self.current_locale_settings()
+def test_locale_repr():
+ assert ("Locale('de', territory='DE')" == repr(Locale('de', 'DE')))
+ assert ("Locale('zh', territory='CN', script='Hans')" ==
+ repr(Locale('zh', 'CN', script='Hans')))
- def tearDown(self):
- self.reset_locale_settings(self._old_locale_settings)
+def test_locale_comparison():
+ en_US = Locale('en', 'US')
+ assert en_US == en_US
+ assert None != en_US
- def current_locale_settings(self):
- settings = {}
- for name in ('LC_MESSAGES', 'LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LANG'):
- settings[name] = os.environ.get(name)
- return settings
+ bad_en_US = Locale('en_US')
+ assert en_US != bad_en_US
- def reset_locale_settings(self, settings):
- for name, value in settings.items():
- if value is not None:
- os.environ[name] = value
- elif name in os.environ:
- del os.environ[name]
+def test_can_return_default_locale(os_environ):
+ os_environ['LC_MESSAGES'] = 'fr_FR.UTF-8'
+ assert Locale('fr', 'FR') == Locale.default('LC_MESSAGES')
-class LocaleTest(LocaleEnvironmentTestMixin, unittest.TestCase):
-
- def test_locale_provides_access_to_cldr_locale_data(self):
- locale = Locale('en', 'US')
- self.assertEqual(u'English (United States)', locale.display_name)
- self.assertEqual(u'.', locale.number_symbols['decimal'])
-
- def test_repr(self):
- self.assertEqual("Locale('de', territory='DE')",
- repr(Locale('de', 'DE')))
- self.assertEqual("Locale('zh', territory='CN', script='Hans')",
- repr(Locale('zh', 'CN', script='Hans')))
-
- def test_locale_comparison(self):
- en_US = Locale('en', 'US')
- self.assertEqual(en_US, en_US)
- self.assertNotEqual(None, en_US)
-
- bad_en_US = Locale('en_US')
- self.assertNotEqual(en_US, bad_en_US)
-
- def test_can_return_default_locale(self):
- os.environ['LC_MESSAGES'] = 'fr_FR.UTF-8'
- self.assertEqual(Locale('fr', 'FR'), Locale.default('LC_MESSAGES'))
-
-
-class DefaultLocaleTest(LocaleEnvironmentTestMixin, unittest.TestCase):
-
- def test_ignore_invalid_locales_in_lc_ctype(self):
- # This is a regression test specifically for a bad LC_CTYPE setting on
- # MacOS X 10.6 (#200)
- os.environ['LC_CTYPE'] = 'UTF-8'
- # must not throw an exception
- default_locale('LC_CTYPE')
+def test_ignore_invalid_locales_in_lc_ctype(os_environ):
+ # This is a regression test specifically for a bad LC_CTYPE setting on
+ # MacOS X 10.6 (#200)
+ os_environ['LC_CTYPE'] = 'UTF-8'
+ # must not throw an exception
+ default_locale('LC_CTYPE')
def test_get_global():
@@ -93,11 +65,10 @@ class TestLocaleClass:
assert locale.language == 'en'
assert locale.territory == 'US'
- def test_default(self):
- # TODO isolate this test
+ def test_default(self, os_environ):
for name in ['LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LC_MESSAGES']:
- os.environ[name] = ''
- os.environ['LANG'] = 'fr_FR.UTF-8'
+ os_environ[name] = ''
+ os_environ['LANG'] = 'fr_FR.UTF-8'
default = Locale.default('LC_MESSAGES')
assert (default.language, default.territory) == ('fr', 'FR')
@@ -233,14 +204,13 @@ class TestLocaleClass:
assert Locale('ru').plural_form(100) == 'many'
-def test_default_locale():
- # TODO isolate this test
+def test_default_locale(os_environ):
for name in ['LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LC_MESSAGES']:
- os.environ[name] = ''
- os.environ['LANG'] = 'fr_FR.UTF-8'
+ os_environ[name] = ''
+ os_environ['LANG'] = 'fr_FR.UTF-8'
assert default_locale('LC_MESSAGES') == 'fr_FR'
- os.environ['LC_MESSAGES'] = 'POSIX'
+ os_environ['LC_MESSAGES'] = 'POSIX'
assert default_locale('LC_MESSAGES') == 'en_US_POSIX'
diff --git a/tests/test_plural.py b/tests/test_plural.py
index 644a97d..89a782b 100644
--- a/tests/test_plural.py
+++ b/tests/test_plural.py
@@ -68,3 +68,25 @@ def test_cldr_modulo():
assert plural.cldr_modulo(-3, 5) == -3
assert plural.cldr_modulo(-3, -5) == -3
assert plural.cldr_modulo(3, 5) == 3
+
+
+def test_plural_within_rules():
+ p = plural.PluralRule({'one': 'n is 1', 'few': 'n within 2,4,7..9'})
+ assert repr(p) == "<PluralRule 'one: n is 1, few: n within 2,4,7..9'>"
+ assert plural.to_javascript(p) == (
+ "(function(n) { "
+ "return ((n == 2) || (n == 4) || (n >= 7 && n <= 9))"
+ " ? 'few' : (n == 1) ? 'one' : 'other'; })")
+ assert plural.to_gettext(p) == (
+ 'nplurals=3; plural=(((n == 2) || (n == 4) || (n >= 7 && n <= 9))'
+ ' ? 1 : (n == 1) ? 0 : 2)')
+ assert p(0) == 'other'
+ assert p(1) == 'one'
+ assert p(2) == 'few'
+ assert p(3) == 'other'
+ assert p(4) == 'few'
+ assert p(5) == 'other'
+ assert p(6) == 'other'
+ assert p(7) == 'few'
+ assert p(8) == 'few'
+ assert p(9) == 'few'
diff --git a/tests/test_support.py b/tests/test_support.py
index e165434..303fb3b 100644
--- a/tests/test_support.py
+++ b/tests/test_support.py
@@ -18,12 +18,15 @@ import shutil
from StringIO import StringIO
import tempfile
import unittest
+import pytest
from datetime import date, datetime, timedelta
from babel import support
from babel.messages import Catalog
from babel.messages.mofile import write_mo
+
+@pytest.mark.usefixtures("os_environ")
class TranslationsTestCase(unittest.TestCase):
def setUp(self):