| 1 |
import pytest |
| 2 |
|
| 3 |
from babel.units import format_unit, get_unit_name |
| 4 |
|
| 5 |
|
| 6 |
# New units in CLDR 46 |
| 7 |
@pytest.mark.parametrize(('unit', 'count', 'expected'), [ |
| 8 |
('speed-light-speed', 1, '1 světlo'), |
| 9 |
('speed-light-speed', 2, '2 světla'), |
| 10 |
('speed-light-speed', 5, '5 světel'), |
| 11 |
('concentr-part-per-1e9', 1, '1 částice na miliardu'), |
| 12 |
('concentr-part-per-1e9', 2, '2 částice na miliardu'), |
| 13 |
('concentr-part-per-1e9', 5, '5 částic na miliardu'), |
| 14 |
('duration-night', 1, '1 noc'), |
| 15 |
('duration-night', 2, '2 noci'), |
| 16 |
('duration-night', 5, '5 nocí'), |
| 17 |
]) |
| 18 |
def test_new_cldr46_units(unit, count, expected): |
| 19 |
assert format_unit(count, unit, locale='cs_CZ') == expected |
| 20 |
|
| 21 |
|
| 22 |
@pytest.mark.parametrize('count, unit, locale, length, expected', [ |
| 23 |
(1, 'duration-month', 'et', 'long', '1 kuu'), |
| 24 |
(1, 'duration-minute', 'et', 'narrow', '1 min'), |
| 25 |
(2, 'duration-minute', 'et', 'narrow', '2 min'), |
| 26 |
(2, 'digital-byte', 'et', 'long', '2 baiti'), |
| 27 |
(1, 'duration-day', 'it', 'long', '1 giorno'), |
| 28 |
(1, 'duration-day', 'it', 'short', '1 giorno'), |
| 29 |
]) |
| 30 |
def test_issue_1217(count, unit, locale, length, expected): |
| 31 |
assert format_unit(count, unit, length, locale=locale) == expected |
| 32 |
|
| 33 |
|
| 34 |
def test_deprecated_unit_ids(): |
| 35 |
for id in ("concentr-permillion", "concentr-portion", "concentr-portion-per-1e9"): |
| 36 |
with pytest.warns(DeprecationWarning, match=id): |
| 37 |
format_unit(1, id, locale='en') |
| 38 |
|
| 39 |
|
| 40 |
@pytest.mark.parametrize('count, unit, locale, length, expected', [ |
| 41 |
# Root aliases `duration-*-person` to `duration-*`; |
| 42 |
# no locale defines the person variants at all. |
| 43 |
# These resolve length-preservingly (person long -> base long), |
| 44 |
# matching ICU's `-person`-stripping behavior |
| 45 |
# (see `getMeasureData` in https://github.com/unicode-org/icu/blob/main/icu4c/source/i18n/number_longnames.cpp |
| 46 |
# and https://unicode-org.atlassian.net/browse/ICU-20400). |
| 47 |
# This deliberately bends literal TR35 alias resolution. |
| 48 |
# See `parse_unit_patterns` in `scripts/import_cldr.py` for the full story. |
| 49 |
(2, 'duration-day-person', 'af', 'long', '2 dae'), |
| 50 |
(3, 'duration-day-person', 'fi', 'long', '3 päivää'), |
| 51 |
(3, 'duration-day-person', 'fi', 'short', '3 pv'), |
| 52 |
(3, 'duration-day-person', 'fi', 'narrow', '3pv'), |
| 53 |
(3, 'duration-year-person', 'fi', 'long', '3 vuotta'), |
| 54 |
# `fi` defines `energy-foodcalorie` at long and narrow but not short; |
| 55 |
# the root alias fills short in from `energy-kilocalorie`. |
| 56 |
(3, 'energy-foodcalorie', 'fi', 'short', '3 kcal'), |
| 57 |
# `fi` defines `graphics-dot` at short and narrow but not long; |
| 58 |
# the root alias fills long in from short. |
| 59 |
(3, 'graphics-dot', 'fi', 'long', '3 pistettä'), |
| 60 |
# `cs` defines `graphics-dot` itself but not its short forms; |
| 61 |
# the root aliases short to `graphics-pixel` short rather than the display name. |
| 62 |
(3, 'graphics-dot', 'cs', 'short', '3 px'), |
| 63 |
]) |
| 64 |
def test_issue_1076_unit_aliases(count, unit, locale, length, expected): |
| 65 |
assert format_unit(count, unit, length, locale=locale) == expected |
| 66 |
|
| 67 |
|
| 68 |
def test_issue_1076_unit_name_length_aliases(): |
| 69 |
# Finnish defines no narrow display name for days; root aliases narrow to |
| 70 |
# short. This used to return None. |
| 71 |
assert get_unit_name('duration-day', length='narrow', locale='fi') == 'pv' |
| 72 |
# The person variant resolves length-preservingly to `duration-day`. |
| 73 |
assert get_unit_name('duration-day-person', length='long', locale='fi') == 'päivät' |
| 74 |
assert get_unit_name('duration-day-person', length='short', locale='fi') == 'pv' |