summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorLuke Plant <L.Plant.98@cantab.net>2018-06-07 15:21:19 +0300
committerLuke Plant <L.Plant.98@cantab.net>2018-06-07 15:37:27 +0300
commit4c7e9b645cfec72672f836e04d8587ca66ba98c3 (patch)
tree7be9d2495b8fc3799643299c0adcd070bca90a9e /tests
parent4b5097f6064dda281ce7a1c5ef74e047cfdc558c (diff)
downloadbabel-4c7e9b645cfec72672f836e04d8587ca66ba98c3.tar.gz
Simplify format_currency code by pulling out/using helpers.
In detail: 1. Use the already existing get_currency_name function which does the plural form logic already. 2. Create a similar `get_currency_unit_pattern` function. This function could be useful if someone wanted to write something very similar to the _format_currency_long_name functionality but with some different customizations, so it is now publicly documented. 3. Simplify the _format_currency_long_name function - it is now much flatter. 4. Add more tests to ensure certain cases are really covered. (e.g. different unit patterns depending on the count) An upshot of the changes is that we have reduced (and made more consistent) the number of places where we need to peek into `Locale._data` - get_currency_name and get_currency_unit_pattern are the only places that babel.numbers does this.
Diffstat (limited to 'tests')
-rw-r--r--tests/test_numbers.py21
1 files changed, 20 insertions, 1 deletions
diff --git a/tests/test_numbers.py b/tests/test_numbers.py
index f0239cb..32f4280 100644
--- a/tests/test_numbers.py
+++ b/tests/test_numbers.py
@@ -19,7 +19,7 @@ from datetime import date
from babel import Locale, localedata, numbers
from babel.numbers import (
list_currencies, validate_currency, UnknownCurrencyError, is_currency, normalize_currency,
- get_currency_precision, get_decimal_precision)
+ get_currency_precision, get_decimal_precision, get_currency_unit_pattern)
from babel.localedata import locale_identifiers
from babel._compat import decimal
@@ -228,6 +228,17 @@ def test_get_currency_precision():
assert get_currency_precision('JPY') == 0
+def test_get_currency_unit_pattern():
+ assert get_currency_unit_pattern('USD', locale='en_US') == '{0} {1}'
+ assert get_currency_unit_pattern('USD', locale='es_GT') == '{1} {0}'
+
+ # 'ro' locale various pattern according to count
+ assert get_currency_unit_pattern('USD', locale='ro', count=1) == '{0} {1}'
+ assert get_currency_unit_pattern('USD', locale='ro', count=2) == '{0} {1}'
+ assert get_currency_unit_pattern('USD', locale='ro', count=100) == '{0} de {1}'
+ assert get_currency_unit_pattern('USD', locale='ro') == '{0} de {1}'
+
+
def test_get_territory_currencies():
assert numbers.get_territory_currencies('AT', date(1995, 1, 1)) == ['ATS']
assert numbers.get_territory_currencies('AT', date(2011, 1, 1)) == ['EUR']
@@ -434,6 +445,14 @@ def test_format_currency_long_display_name():
assert (numbers.format_currency(1099.98, 'XAB', locale='en_US', format_type='name')
== u'1,099.98 XAB')
+ # Test for finding different unit patterns depending on count
+ assert (numbers.format_currency(1, 'USD', locale='ro', format_type='name')
+ == u'1,00 dolar american')
+ assert (numbers.format_currency(2, 'USD', locale='ro', format_type='name')
+ == u'2,00 dolari americani')
+ assert (numbers.format_currency(100, 'USD', locale='ro', format_type='name')
+ == u'100,00 de dolari americani')
+
def test_format_currency_long_display_name_all():
for locale_code in localedata.locale_identifiers():