diff options
author | Isaac Jurado <diptongo@gmail.com> | 2015-10-11 14:26:04 +0200 |
---|---|---|
committer | Isaac Jurado <diptongo@gmail.com> | 2015-10-14 19:54:12 +0200 |
commit | fe77bb3e66a7838b119e6b8ca92c2e0396ed84dd (patch) | |
tree | ebaf38db523250c447d61e52f03f068343f0e9ec | |
parent | b6169be329e121111dc9c3242c3538ee3b0a3aaa (diff) | |
download | babel-fe77bb3e66a7838b119e6b8ca92c2e0396ed84dd.tar.gz |
tests: Use the automatically chosen Decimal class
Now that the babel._compat module can automatically select a faster decimal
implementation if available, be more consistent across the rest of the code when
dealing with Decimal instances.
-rw-r--r-- | babel/plural.py | 7 | ||||
-rw-r--r-- | tests/test_numbers.py | 2 | ||||
-rw-r--r-- | tests/test_plural.py | 23 |
3 files changed, 17 insertions, 15 deletions
diff --git a/babel/plural.py b/babel/plural.py index 50bf541..b5ce9ba 100644 --- a/babel/plural.py +++ b/babel/plural.py @@ -8,10 +8,11 @@ :copyright: (c) 2013 by the Babel Team. :license: BSD, see LICENSE for more details. """ -import decimal import re import sys +from babel._compat import Decimal + _plural_tags = ('zero', 'one', 'two', 'few', 'many', 'other') _fallback_tag = 'other' @@ -32,9 +33,9 @@ def extract_operands(source): # 2.6's Decimal cannot convert from float directly if sys.version_info < (2, 7): n = str(n) - n = decimal.Decimal(n) + n = Decimal(n) - if isinstance(n, decimal.Decimal): + if isinstance(n, Decimal): dec_tuple = n.as_tuple() exp = dec_tuple.exponent fraction_digits = dec_tuple.digits[exp:] if exp < 0 else () diff --git a/tests/test_numbers.py b/tests/test_numbers.py index 0d8fa00..f042833 100644 --- a/tests/test_numbers.py +++ b/tests/test_numbers.py @@ -11,13 +11,13 @@ # individuals. For the exact contribution history, see the revision # history and logs, available at http://babel.edgewall.org/log/. -from decimal import Decimal import unittest import pytest from datetime import date from babel import numbers +from babel._compat import Decimal class FormatDecimalTestCase(unittest.TestCase): diff --git a/tests/test_plural.py b/tests/test_plural.py index 278b5db..b0cad8d 100644 --- a/tests/test_plural.py +++ b/tests/test_plural.py @@ -10,10 +10,11 @@ # This software consists of voluntary contributions made by many # individuals. For the exact contribution history, see the revision # history and logs, available at http://babel.edgewall.org/log/. -import decimal import unittest import pytest + from babel import plural +from babel._compat import Decimal def test_plural_rule(): @@ -33,29 +34,29 @@ def test_plural_rule_operands_i(): 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(Decimal('1.20')) == 'one' + assert rule(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(Decimal('1.23')) == 'one' + assert rule(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(Decimal('1.23')) == 'other' + assert rule(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(Decimal('1.53')) == 'other' + assert rule(Decimal('1.50')) == 'one' assert rule(1.5) == 'one' @@ -250,6 +251,6 @@ EXTRACT_OPERANDS_TESTS = ( @pytest.mark.parametrize('source,n,i,v,w,f,t', EXTRACT_OPERANDS_TESTS) def test_extract_operands(source, n, i, v, w, f, t): - source = decimal.Decimal(source) if isinstance(source, str) else source + source = Decimal(source) if isinstance(source, str) else source assert (plural.extract_operands(source) == - decimal.Decimal(n), i, v, w, f, t) + Decimal(n), i, v, w, f, t) |