| 1 |
# |
| 2 |
# Copyright (C) 2007-2011 Edgewall Software, 2013-2025 the Babel team |
| 3 |
# All rights reserved. |
| 4 |
# |
| 5 |
# This software is licensed as described in the file LICENSE, which |
| 6 |
# you should have received as part of this distribution. The terms |
| 7 |
# are also available at https://github.com/python-babel/babel/blob/master/LICENSE. |
| 8 |
# |
| 9 |
# This software consists of voluntary contributions made by many |
| 10 |
# individuals. For the exact contribution history, see the revision |
| 11 |
# history and logs, available at https://github.com/python-babel/babel/commits/master/. |
| 12 |
|
| 13 |
import decimal |
| 14 |
|
| 15 |
import pytest |
| 16 |
|
| 17 |
from babel import numbers |
| 18 |
|
| 19 |
|
| 20 |
def test_can_parse_decimals(): |
| 21 |
assert decimal.Decimal('1099.98') == numbers.parse_decimal('1,099.98', locale='en_US') |
| 22 |
assert decimal.Decimal('1099.98') == numbers.parse_decimal('1.099,98', locale='de') |
| 23 |
assert decimal.Decimal('1099.98') == numbers.parse_decimal('1,099.98', locale='ar', numbering_system="default") |
| 24 |
assert decimal.Decimal('1099.98') == numbers.parse_decimal('1٬099٫98', locale='ar_EG', numbering_system="default") |
| 25 |
with pytest.raises(numbers.NumberFormatError): |
| 26 |
numbers.parse_decimal('2,109,998', locale='de') |
| 27 |
with pytest.raises(numbers.UnsupportedNumberingSystemError): |
| 28 |
numbers.parse_decimal('2,109,998', locale='de', numbering_system="unknown") |
| 29 |
|
| 30 |
|
| 31 |
def test_parse_decimal_strict_mode(): |
| 32 |
# Numbers with a misplaced grouping symbol should be rejected |
| 33 |
with pytest.raises(numbers.NumberFormatError) as info: |
| 34 |
numbers.parse_decimal('11.11', locale='de', strict=True) |
| 35 |
assert info.value.suggestions == ['1.111', '11,11'] |
| 36 |
# Numbers with two misplaced grouping symbols should be rejected |
| 37 |
with pytest.raises(numbers.NumberFormatError) as info: |
| 38 |
numbers.parse_decimal('80.00.00', locale='de', strict=True) |
| 39 |
assert info.value.suggestions == ['800.000'] |
| 40 |
# Partially grouped numbers should be rejected |
| 41 |
with pytest.raises(numbers.NumberFormatError) as info: |
| 42 |
numbers.parse_decimal('2000,000', locale='en_US', strict=True) |
| 43 |
assert info.value.suggestions == ['2,000,000', '2,000'] |
| 44 |
# Numbers with duplicate grouping symbols should be rejected |
| 45 |
with pytest.raises(numbers.NumberFormatError) as info: |
| 46 |
numbers.parse_decimal('0,,000', locale='en_US', strict=True) |
| 47 |
assert info.value.suggestions == ['0'] |
| 48 |
# Return only suggestion for 0 on strict |
| 49 |
with pytest.raises(numbers.NumberFormatError) as info: |
| 50 |
numbers.parse_decimal('0.00', locale='de', strict=True) |
| 51 |
assert info.value.suggestions == ['0'] |
| 52 |
# Properly formatted numbers should be accepted |
| 53 |
assert str(numbers.parse_decimal('1.001', locale='de', strict=True)) == '1001' |
| 54 |
# Trailing zeroes should be accepted |
| 55 |
assert str(numbers.parse_decimal('3.00', locale='en_US', strict=True)) == '3.00' |
| 56 |
# Numbers with a grouping symbol and no trailing zeroes should be accepted |
| 57 |
assert str(numbers.parse_decimal('3,400.6', locale='en_US', strict=True)) == '3400.6' |
| 58 |
# Numbers with a grouping symbol and trailing zeroes (not all zeroes after decimal) should be accepted |
| 59 |
assert str(numbers.parse_decimal('3,400.60', locale='en_US', strict=True)) == '3400.60' |
| 60 |
# Numbers with a grouping symbol and trailing zeroes (all zeroes after decimal) should be accepted |
| 61 |
assert str(numbers.parse_decimal('3,400.00', locale='en_US', strict=True)) == '3400.00' |
| 62 |
assert str(numbers.parse_decimal('3,400.0000', locale='en_US', strict=True)) == '3400.0000' |
| 63 |
# Numbers with a grouping symbol and no decimal part should be accepted |
| 64 |
assert str(numbers.parse_decimal('3,800', locale='en_US', strict=True)) == '3800' |
| 65 |
# Numbers without any grouping symbol should be accepted |
| 66 |
assert str(numbers.parse_decimal('2000.1', locale='en_US', strict=True)) == '2000.1' |
| 67 |
# Numbers without any grouping symbol and no decimal should be accepted |
| 68 |
assert str(numbers.parse_decimal('2580', locale='en_US', strict=True)) == '2580' |
| 69 |
# High precision numbers should be accepted |
| 70 |
assert str(numbers.parse_decimal('5,000001', locale='fr', strict=True)) == '5.000001' |