From a47c485bb7c6d73ac703f269effcef13f6028c47 Mon Sep 17 00:00:00 2001 From: eyaler Date: Fri, 20 Jan 2023 17:34:11 +0200 Subject: [PATCH] improved coverage, improved custom currency seperator, fixed silent failure in base test --- num2words/lang_HE.py | 15 ++++++------- tests/test_base.py | 5 ++--- tests/test_he.py | 51 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 58 insertions(+), 13 deletions(-) diff --git a/num2words/lang_HE.py b/num2words/lang_HE.py index b114fb46..8898d667 100644 --- a/num2words/lang_HE.py +++ b/num2words/lang_HE.py @@ -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] @@ -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 = [ @@ -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)) diff --git a/tests/test_base.py b/tests/test_base.py index 6500db6e..efa1bc81 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -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 diff --git a/tests/test_he.py b/tests/test_he.py index 4c0b490c..b6643e95 100644 --- a/tests/test_he.py +++ b/tests/test_he.py @@ -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'אפס') @@ -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( @@ -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() @@ -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") @@ -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)