| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from babel import Locale |
| 4 |
from babel.core import ( |
| 5 |
get_global, |
| 6 |
get_locale_identifier, |
| 7 |
negotiate_locale, |
| 8 |
parse_locale, |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
def test_locale_parse_language(benchmark, fi_locale): |
| 13 |
# The most common shape: a plain language tag, or an already-parsed Locale. |
| 14 |
assert benchmark(lambda: Locale.parse(fi_locale)).language == "fi" |
| 15 |
|
| 16 |
|
| 17 |
def test_locale_parse_full_tag(benchmark): |
| 18 |
assert str(benchmark(lambda: Locale.parse("zh-Hans-CN", sep="-"))) == "zh_Hans_CN" |
| 19 |
|
| 20 |
|
| 21 |
def test_locale_parse_with_variant(benchmark): |
| 22 |
assert str(benchmark(lambda: Locale.parse("en_US_POSIX"))) == "en_US_POSIX" |
| 23 |
|
| 24 |
|
| 25 |
def test_locale_parse_likely_subtags(benchmark): |
| 26 |
# zh_TW only exists via likely subtag resolution, i.e. the slow path. |
| 27 |
assert str(benchmark(lambda: Locale.parse("zh_TW"))) == "zh_Hant_TW" |
| 28 |
|
| 29 |
|
| 30 |
def test_locale_construct(benchmark): |
| 31 |
assert str(benchmark(lambda: Locale("en", "US"))) == "en_US" |
| 32 |
|
| 33 |
|
| 34 |
def test_negotiate_locale(benchmark): |
| 35 |
preferred = ["fi_FI", "en-US", "de"] |
| 36 |
available = ["en", "de", "fi"] |
| 37 |
assert benchmark(lambda: negotiate_locale(preferred, available)) == "fi" |
| 38 |
|
| 39 |
|
| 40 |
def test_parse_locale(benchmark): |
| 41 |
assert benchmark(lambda: parse_locale("en_US.UTF-8")) == ("en", "US", None, None) |
| 42 |
|
| 43 |
|
| 44 |
def test_get_locale_identifier(benchmark): |
| 45 |
parts = ("zh", "CN", "Hans", None) |
| 46 |
assert benchmark(lambda: get_locale_identifier(parts)) == "zh_Hans_CN" |
| 47 |
|
| 48 |
|
| 49 |
def test_get_global(benchmark): |
| 50 |
assert benchmark(lambda: get_global("zone_territories")["Europe/Helsinki"]) == "FI" |
| 51 |
|
| 52 |
|
| 53 |
def test_locale_get_display_name(benchmark): |
| 54 |
locale = Locale.parse("fi") |
| 55 |
en = Locale.parse("en") |
| 56 |
assert benchmark(lambda: locale.get_display_name(en)) == "Finnish" |