From 5fcc2535f96bfce9c1a1ecf9d19a976b9bf6ab6b Mon Sep 17 00:00:00 2001 From: Jonah Lawrence Date: Fri, 4 Nov 2022 09:16:24 -0600 Subject: feat: Support for short compact currency formats (#926) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jun Omae (大前 潤) <42682+jun66j5@users.noreply.github.com> --- tests/test_numbers.py | 29 +++++++++++++++++++++++++++++ tests/test_support.py | 5 +++++ 2 files changed, 34 insertions(+) (limited to 'tests') diff --git a/tests/test_numbers.py b/tests/test_numbers.py index 1b955c9..bb6c4e8 100644 --- a/tests/test_numbers.py +++ b/tests/test_numbers.py @@ -422,6 +422,27 @@ def test_format_currency_format_type(): == u'1.099,98') +def test_format_compact_currency(): + assert numbers.format_compact_currency(1, 'USD', locale='en_US', format_type="short") == u'$1' + assert numbers.format_compact_currency(999, 'USD', locale='en_US', format_type="short") == u'$999' + assert numbers.format_compact_currency(123456789, 'USD', locale='en_US', format_type="short") == u'$123M' + assert numbers.format_compact_currency(123456789, 'USD', locale='en_US', fraction_digits=2, format_type="short") == u'$123.46M' + assert numbers.format_compact_currency(-123456789, 'USD', locale='en_US', fraction_digits=2, format_type="short") == u'-$123.46M' + assert numbers.format_compact_currency(1, 'JPY', locale='ja_JP', format_type="short") == u'¥1' + assert numbers.format_compact_currency(1234, 'JPY', locale='ja_JP', format_type="short") == u'¥1234' + assert numbers.format_compact_currency(123456, 'JPY', locale='ja_JP', format_type="short") == u'¥12万' + assert numbers.format_compact_currency(123456, 'JPY', locale='ja_JP', format_type="short", fraction_digits=2) == u'¥12.35万' + assert numbers.format_compact_currency(123, 'EUR', locale='yav', format_type="short") == '123\xa0€' + assert numbers.format_compact_currency(12345, 'EUR', locale='yav', format_type="short") == '12K\xa0€' + assert numbers.format_compact_currency(123456789, 'EUR', locale='de_DE', fraction_digits=1) == '123,5\xa0Mio.\xa0€' + + +def test_format_compact_currency_invalid_format_type(): + with pytest.raises(numbers.UnknownCurrencyFormatError): + numbers.format_compact_currency(1099.98, 'USD', locale='en_US', + format_type='unknown') + + @pytest.mark.parametrize('input_value, expected_value', [ ('10000', '$10,000.00'), ('1', '$1.00'), @@ -696,3 +717,11 @@ def test_parse_decimal_nbsp_heuristics(): def test_very_small_decimal_no_quantization(): assert numbers.format_decimal(decimal.Decimal('1E-7'), locale='en', decimal_quantization=False) == '0.0000001' + + +def test_single_quotes_in_pattern(): + assert numbers.format_decimal(123, "'@0.#'00'@01'", locale='en') == '@0.#120@01' + + assert numbers.format_decimal(123, "'$'''0", locale='en') == "$'123" + + assert numbers.format_decimal(12, "'#'0 o''clock", locale='en') == "#12 o'clock" diff --git a/tests/test_support.py b/tests/test_support.py index 93ad37e..9447107 100644 --- a/tests/test_support.py +++ b/tests/test_support.py @@ -334,6 +334,11 @@ def test_format_compact_decimal(): assert fmt.compact_decimal(1234567, format_type='long', fraction_digits=2) == '1.23 million' +def test_format_compact_currency(): + fmt = support.Format('en_US') + assert fmt.compact_currency(1234567, "USD", format_type='short', fraction_digits=2) == '$1.23M' + + def test_format_percent(): fmt = support.Format('en_US') assert fmt.percent(0.34) == '34%' -- cgit v1.2.1