Created
May 27, 2017 01:08
-
-
Save benselme/c524db97112c5302dabbd91775b64b36 to your computer and use it in GitHub Desktop.
testing babel vs icu number formatting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def test_number_pattern_spec_examples(): | |
pattern = u'@@@' | |
assert numbers.format_decimal(12345, pattern) == u'12300' | |
assert numbers.format_decimal(0.12345, pattern) == u'0.123' | |
assert numbers.format_decimal(3.14159, u'@@##') == u'3.142' | |
assert numbers.format_decimal(1.23004, u'@@##') == u'1.23' | |
SCIENTIFICSTYLE = 3 | |
CURRENCYSTYLE = 1 | |
PERCENTSTYLE = 2 | |
NUMBERSTYLE = 0 | |
FORMAT_FUNCTIONS = { | |
NUMBERSTYLE: numbers.format_decimal, | |
CURRENCYSTYLE: numbers.format_currency, | |
PERCENTSTYLE: numbers.format_percent, | |
SCIENTIFICSTYLE: numbers.format_scientific, | |
} | |
BIG_DEC = "bigdec" | |
DOUBLE = "double" | |
INT = "int" | |
def test_icu_data(): | |
with open('/media/benselme/data/babtest.json') as data_file: | |
data = json.load(data_file) | |
numformat_data = data['NumberFormat'] | |
values = numformat_data['values'] | |
values[BIG_DEC] = decimal.Decimal(values[BIG_DEC]) | |
values[DOUBLE] = float(values[DOUBLE]) | |
values[INT] = int(values[INT]) | |
errors = [] | |
locales = numformat_data['locales'] | |
for locale, locale_data in locales.items(): | |
for style, style_data in locale_data.items(): | |
style = int(style) | |
if style == SCIENTIFICSTYLE: | |
continue | |
format_fn = FORMAT_FUNCTIONS[int(style)] | |
for val_type, icu_formatted_val in style_data.items(): | |
if style == CURRENCYSTYLE: | |
babel_formatted = numbers.format_currency( | |
values[val_type], icu_formatted_val['currency'], | |
locale=locale, decimal_quantization=True) | |
icu_formatted_val = icu_formatted_val['value'] | |
else: | |
babel_formatted = format_fn( | |
values[val_type], locale=locale, | |
decimal_quantization=True) | |
if babel_formatted != icu_formatted_val: | |
errors.append( | |
f'Differs from ICU {locale}-{style}-{val_type}\n' | |
f'ICU: "{icu_formatted_val}"\n' | |
f'Babel:"{babel_formatted}"' | |
) | |
if errors: | |
assert False, '\n'.join(errors) + f'\n {len(errors)}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment