]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Make sure extract_operands is always called when using plural.to_python, add tests...
authorbenselme <benselme@gmail.com>
Fri, 9 Jan 2015 20:35:21 +0000 (15:35 -0500)
committerbenselme <benselme@gmail.com>
Fri, 9 Jan 2015 20:35:21 +0000 (15:35 -0500)
babel/plural.py
tests/test_plural.py

index 7ce62e1fb39f704188640f353f8491d28c17ac70..918038e61b5f13be39c337b8184f10c155978bb6 100644 (file)
@@ -132,7 +132,7 @@ class PluralRule(object):
     def __call__(self, n):
         if not hasattr(self, '_func'):
             self._func = to_python(self)
-        return self._func(*extract_operands(n))
+        return self._func(n)
 
 
 def to_javascript(rule):
@@ -180,12 +180,13 @@ def to_python(rule):
     namespace = {
         'IN':       in_range_list,
         'WITHIN':   within_range_list,
-        'MOD':      cldr_modulo
+        'MOD':      cldr_modulo,
+        'extract_operands': extract_operands,
     }
     to_python_func = _PythonCompiler().compile
     result = [
-        'def evaluate(n, i=None, v=0, w=0, f=0, t=0):',
-        ' i = int(n) if i is None else i',
+        'def evaluate(n):',
+        ' n, i, v, w, f, t = extract_operands(n)',
     ]
     for tag, ast in PluralRule.parse(rule).abstract:
         # the str() call is to coerce the tag to the native string.  It's
index ece1358c5ea3696db99e8ac397c27d9f924f159e..278b5dba8bcc39fda1981fda19bc7cd8fe4f5422 100644 (file)
 # individuals. For the exact contribution history, see the revision
 # history and logs, available at http://babel.edgewall.org/log/.
 import decimal
-
-import doctest
 import unittest
 import pytest
-from decimal import Decimal as Dec
 from babel import plural
 
 
@@ -28,6 +25,40 @@ def test_plural_rule():
     assert rule.rules == {'one': 'n is 1'}
 
 
+def test_plural_rule_operands_i():
+    rule = plural.PluralRule({'one': 'i is 1'})
+    assert rule(1.2) == 'one'
+    assert rule(2) == 'other'
+
+
+def test_plural_rule_operands_v():
+    rule = plural.PluralRule({'one': 'v is 2'})
+    assert rule(decimal.Decimal('1.20')) == 'one'
+    assert rule(decimal.Decimal('1.2')) == 'other'
+    assert rule(2) == 'other'
+
+
+def test_plural_rule_operands_w():
+    rule = plural.PluralRule({'one': 'w is 2'})
+    assert rule(decimal.Decimal('1.23')) == 'one'
+    assert rule(decimal.Decimal('1.20')) == 'other'
+    assert rule(1.2) == 'other'
+
+
+def test_plural_rule_operands_f():
+    rule = plural.PluralRule({'one': 'f is 20'})
+    assert rule(decimal.Decimal('1.23')) == 'other'
+    assert rule(decimal.Decimal('1.20')) == 'one'
+    assert rule(1.2) == 'other'
+
+
+def test_plural_rule_operands_t():
+    rule = plural.PluralRule({'one': 't = 5'})
+    assert rule(decimal.Decimal('1.53')) == 'other'
+    assert rule(decimal.Decimal('1.50')) == 'one'
+    assert rule(1.5) == 'one'
+
+
 def test_plural_other_is_ignored():
     rule = plural.PluralRule({'one': 'n is 1', 'other': '@integer 2'})
     assert rule(1) == 'one'
@@ -213,10 +244,12 @@ EXTRACT_OPERANDS_TESTS = (
     ('1.03', '1.03', 1, 2, 2, 3, 3),
     ('1.230', '1.230', 1, 3, 2, 230, 23),
     (-1, 1, 1, 0, 0, 0, 0),
+    (1.3, '1.3', 1, 1, 1, 3, 3),
 )
 
 
 @pytest.mark.parametrize('source,n,i,v,w,f,t', EXTRACT_OPERANDS_TESTS)
 def test_extract_operands(source, n, i, v, w, f, t):
-    assert (plural.extract_operands(decimal.Decimal(source)) ==
+    source = decimal.Decimal(source) if isinstance(source, str) else source
+    assert (plural.extract_operands(source) ==
             decimal.Decimal(n), i, v, w, f, t)