| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from babel.units import format_compound_unit, format_unit, get_unit_name |
| 4 |
|
| 5 |
|
| 6 |
def test_format_unit_long(benchmark, fi_locale): |
| 7 |
assert ( |
| 8 |
benchmark(lambda: format_unit(15.5, "length-mile", locale=fi_locale)) == "15,5 mailia" |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
def test_format_unit_short(benchmark, fi_locale): |
| 13 |
assert ( |
| 14 |
benchmark(lambda: format_unit(15.5, "length-mile", "short", locale=fi_locale)) |
| 15 |
== "15,5 mi" |
| 16 |
) |
| 17 |
|
| 18 |
|
| 19 |
def test_format_unit_length_fallback(benchmark): |
| 20 |
# et's "long" duration-month only has a "per" pattern, so this falls back to "short". |
| 21 |
assert benchmark(lambda: format_unit(1, "duration-month", "long", locale="et")) == "1 kuu" |
| 22 |
|
| 23 |
|
| 24 |
def test_format_compound_unit_predefined(benchmark, fi_locale): |
| 25 |
# Resolves to the predefined speed-kilometer-per-hour pattern. |
| 26 |
expected = "150 kilometriä tunnissa" |
| 27 |
assert ( |
| 28 |
benchmark( |
| 29 |
lambda: format_compound_unit( |
| 30 |
150, |
| 31 |
"kilometer", |
| 32 |
denominator_unit="hour", |
| 33 |
locale=fi_locale, |
| 34 |
), |
| 35 |
) |
| 36 |
== expected |
| 37 |
) |
| 38 |
|
| 39 |
|
| 40 |
def test_format_compound_unit_constructed(benchmark, fi_locale): |
| 41 |
# No predefined compound unit; both sides get formatted and joined with the "per" pattern. |
| 42 |
expected = "32,5 am. tonnia/15 tuntia" |
| 43 |
assert ( |
| 44 |
benchmark( |
| 45 |
lambda: format_compound_unit( |
| 46 |
32.5, |
| 47 |
"ton", |
| 48 |
15, |
| 49 |
denominator_unit="hour", |
| 50 |
locale=fi_locale, |
| 51 |
), |
| 52 |
) |
| 53 |
== expected |
| 54 |
) |
| 55 |
|
| 56 |
|
| 57 |
def test_get_unit_name(benchmark, fi_locale): |
| 58 |
# An unqualified unit id, so the pattern table is scanned to qualify it. |
| 59 |
assert benchmark(lambda: get_unit_name("radian", locale=fi_locale)) == "radiaanit" |