Skip to content

Commit

Permalink
improved coverage, improved custom currency seperator, fixed silent f…
Browse files Browse the repository at this point in the history
…ailure in base test
  • Loading branch information
eyaler committed Jan 20, 2023
1 parent 7c2fc26 commit a47c485
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 13 deletions.
15 changes: 6 additions & 9 deletions num2words/lang_HE.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,13 @@ def pluralize(self, n, forms, currency=None, prefer_singular=False):
return forms[form]

def to_currency(self, val, currency='ILS', cents=True,
separator=' ' + AND, adjective=False,
separator=AND, adjective=False,
prefer_singular=False, prefer_singular_cents=False):
left, right, is_negative = parse_currency_parts(val)

if not separator.startswith(' '):
separator = ' ' + separator

try:
cr1, cr2 = self.CURRENCY_FORMS[currency]

Expand All @@ -302,7 +305,8 @@ def to_currency(self, val, currency='ILS', cents=True,
construct=right == 2)
else:
cents_str = self._cents_terse(right, currency)
if separator.split()[-1] == AND:
sep_parts = separator.split()
if sep_parts and sep_parts[-1] == AND:
separator += self.makaf

strings = [
Expand All @@ -321,10 +325,3 @@ def to_currency(self, val, currency='ILS', cents=True,
strings[4], strings[5] = strings[5], strings[4]
# In Hebrew the separator is along with the following word
return u'%s%s %s%s%s %s' % tuple(strings)


if __name__ == '__main__':
yo = Num2Word_HE()
nums = [1, 11, 21, 24, 99, 100, 101, 200, 211, 345, 1000, 1011]
for num in nums:
print(num, yo.to_cardinal(num))
5 changes: 2 additions & 3 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ def test_to_currency_not_implemented(self):

def test_error_to_cardinal_float(self):
from num2words.base import Num2Word_Base
self.base = Num2Word_Base()
with self.assertRaises(TypeError):
Num2Word_Base.to_cardinal_float(9)
with self.assertRaises(TypeError):
Num2Word_Base.to_cardinal_float("a")
self.base.to_cardinal_float("a")

def test_error_merge(self):
from num2words.base import Num2Word_Base
Expand Down
51 changes: 50 additions & 1 deletion tests/test_he.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
from unittest import TestCase

from num2words import num2words
from num2words.lang_HE import Num2Word_HE
from num2words.lang_HE import Num2Word_HE, int2word


class Num2WordsHETest(TestCase):
maxDiff = None

def test_negative(self):
self.assertEqual(num2words(-1, lang="he"), u'מינוס אחת')

def test_0(self):
self.assertEqual(num2words(0, lang="he"), u'אפס')

Expand Down Expand Up @@ -313,6 +316,30 @@ def test_to_currency(self):
self.assertEqual(n.to_currency(
-5.05, currency='ILS', cents=False),
u'מינוס חמישה שקלים ו-05 אגורות')
self.assertEqual(n.to_currency(
-5.05, currency='ILS', cents=False, separator='ו'),
u'מינוס חמישה שקלים ו-05 אגורות')
self.assertEqual(n.to_currency(
-5.05, currency='ILS', cents=False, separator='ו-'),
u'מינוס חמישה שקלים ו-05 אגורות')
self.assertEqual(n.to_currency(
-5.05, currency='ILS', cents=False, separator=''),
u'מינוס חמישה שקלים 05 אגורות')
self.assertEqual(n.to_currency(
-5.05, currency='ILS', cents=False, separator='ועוד '),
u'מינוס חמישה שקלים ועוד 05 אגורות')
self.assertEqual(n.to_currency(
-5.05, currency='ILS', cents=False, separator=' ו'),
u'מינוס חמישה שקלים ו-05 אגורות')
self.assertEqual(n.to_currency(
-5.05, currency='ILS', cents=False, separator=' ו-'),
u'מינוס חמישה שקלים ו-05 אגורות')
self.assertEqual(n.to_currency(
-5.05, currency='ILS', cents=False, separator=' '),
u'מינוס חמישה שקלים 05 אגורות')
self.assertEqual(n.to_currency(
-5.05, currency='ILS', cents=False, separator=' ועוד '),
u'מינוס חמישה שקלים ועוד 05 אגורות')
self.assertEqual(n.to_currency(
1.01, currency='ILS'), u'שקל אחד ואגורה אחת')
self.assertEqual(n.to_currency(
Expand All @@ -334,6 +361,14 @@ def test_to_currency(self):
self.assertEqual(n.to_currency(
5.05, currency='USD', prefer_singular=True,
prefer_singular_cents=True), u'חמישה דולר וחמישה סנט')
n.CURRENCY_FORMS['pruta'] = (('פרוטה', 'פרוטות'), ('מאית', 'מאיות'))
self.assertEqual(n.to_currency(
5.05, currency='pruta'), u'חמש פרוטות וחמש מאיות')

def test_to_currency_errors(self):
n = Num2Word_HE()
with self.assertRaises(NotImplementedError):
n.to_currency(1, '')

def test_to_cardinal(self):
n = Num2Word_HE()
Expand Down Expand Up @@ -446,6 +481,17 @@ def test_cardinal_for_float_number(self):
self.assertEqual(num2words(12.594132, lang='he', gender='m'),
u'שנים עשר נקודה חמש תשע ארבע אחת שלוש שתיים')

def test_cardinal_float_precision(self):
n = Num2Word_HE()
self.assertEqual(n.to_cardinal_float("1.23"), 'אחת נקודה שתיים שלוש')
n.precision = 1
self.assertEqual(n.to_cardinal_float("1.2"), 'אחת נקודה שתיים')

def test_error_to_cardinal_float(self):
n = Num2Word_HE()
with self.assertRaises(TypeError):
n.to_cardinal_float("a")

def test_overflow(self):
n = Num2Word_HE()
num2words(n.MAXVAL - 1, lang="he")
Expand All @@ -456,3 +502,6 @@ def test_overflow(self):

with self.assertRaises(OverflowError):
num2words(n.MAXVAL, lang="he", ordinal=True)

with self.assertRaises(OverflowError):
int2word(n.MAXVAL)

0 comments on commit a47c485

Please sign in to comment.