Use order in preferred regions list (#91959)

* Use order in preferred regions list

* Use float for score (inf = exact match)
This commit is contained in:
Michael Hansen 2023-04-24 13:12:38 -05:00 committed by GitHub
parent b601fb17d3
commit b4bd3b97f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 73 additions and 23 deletions

View file

@ -1,6 +1,8 @@
"""Test Home Assistant language util methods."""
from __future__ import annotations
import pytest
from homeassistant.const import MATCH_ALL
from homeassistant.util import language
@ -95,26 +97,54 @@ def test_language_as_region() -> None:
def test_zh_hant() -> None:
"""Test that the zh-Hant matches HK or TW first."""
"""Test that the zh-Hant matches HK or TW."""
assert language.matches(
"zh-Hant",
["en-US", "en-GB", "zh-CN", "zh-HK", "zh-TW"],
["en-US", "en-GB", "zh-CN", "zh-HK"],
) == [
"zh-HK",
"zh-TW",
"zh-CN",
]
assert language.matches(
"zh-Hant",
["en-US", "en-GB", "zh-CN", "zh-TW", "zh-HK"],
["en-US", "en-GB", "zh-CN", "zh-TW"],
) == [
"zh-TW",
"zh-HK",
"zh-CN",
]
@pytest.mark.parametrize("target", ["zh-Hant", "zh-Hans"])
def test_zh_with_country(target: str) -> None:
"""Test that the zh-Hant/zh-Hans still matches country when provided."""
supported = ["en-US", "en-GB", "zh-CN", "zh-HK", "zh-TW"]
assert (
language.matches(
target,
supported,
country="TW",
)[0]
== "zh-TW"
)
assert (
language.matches(
target,
supported,
country="HK",
)[0]
== "zh-HK"
)
assert (
language.matches(
target,
supported,
country="CN",
)[0]
== "zh-CN"
)
def test_zh_hans() -> None:
"""Test that the zh-Hans matches CN first."""
assert language.matches(