ladybird/Tests/LibLocale/TestDisplayNames.cpp
Timothy Flynn 9724a25daf LibJS+LibLocale: Replace canonical locales and display names with ICU
Note: We keep locale parsing and syntactic validation as-is. ECMA-402
places additional restrictions on locales above what is required by the
Unicode spec. ICU doesn't provide methods that let us easily check those
restrictions, whereas LibLocale does. Other browsers also implement
their own validators here.

This introduces a locale cache to re-use parsed locale data and various
related structures (not doing so has a non-negligible performance impact
on Intl tests).

The existing APIs for canonicalization and display names are pretty
intertwined, so they must both be adapted at once here. The results of
canonicalization are slightly different on some edge cases. But the
changed results are actually now aligned with Chrome and Safari.
2024-06-09 10:47:28 +02:00

82 lines
2.8 KiB
C++

/*
* Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibTest/TestCase.h>
#include <LibLocale/DisplayNames.h>
#include <LibLocale/Locale.h>
TEST_CASE(locale_mappings_en)
{
auto language = Locale::language_display_name("en"sv, "en"sv, Locale::LanguageDisplay::Standard);
EXPECT(language.has_value());
EXPECT_EQ(*language, "English"sv);
language = Locale::language_display_name("en"sv, "i-defintely-don't-exist"sv, Locale::LanguageDisplay::Standard);
EXPECT(!language.has_value());
auto territory = Locale::region_display_name("en"sv, "US"sv);
EXPECT(territory.has_value());
EXPECT_EQ(*territory, "United States"sv);
territory = Locale::region_display_name("en"sv, "i-defintely-don't-exist"sv);
EXPECT(!territory.has_value());
auto script = Locale::script_display_name("en"sv, "Latn"sv);
EXPECT(script.has_value());
EXPECT_EQ(*script, "Latin"sv);
script = Locale::script_display_name("en"sv, "i-defintely-don't-exist"sv);
EXPECT(!script.has_value());
}
TEST_CASE(locale_mappings_fr)
{
auto language = Locale::language_display_name("fr"sv, "en"sv, Locale::LanguageDisplay::Standard);
EXPECT(language.has_value());
EXPECT_EQ(*language, "anglais"sv);
language = Locale::language_display_name("fr"sv, "i-defintely-don't-exist"sv, Locale::LanguageDisplay::Standard);
EXPECT(!language.has_value());
auto territory = Locale::region_display_name("fr"sv, "US"sv);
EXPECT(territory.has_value());
EXPECT_EQ(*territory, "États-Unis"sv);
territory = Locale::region_display_name("fr"sv, "i-defintely-don't-exist"sv);
EXPECT(!territory.has_value());
auto script = Locale::script_display_name("fr"sv, "Latn"sv);
EXPECT(script.has_value());
EXPECT_EQ(*script, "latin"sv);
script = Locale::script_display_name("fr"sv, "i-defintely-don't-exist"sv);
EXPECT(!script.has_value());
}
TEST_CASE(locale_mappings_root)
{
auto language = Locale::language_display_name("und"sv, "en"sv, Locale::LanguageDisplay::Standard);
EXPECT(language.has_value());
EXPECT_EQ(*language, "en"sv);
language = Locale::language_display_name("und"sv, "i-defintely-don't-exist"sv, Locale::LanguageDisplay::Standard);
EXPECT(!language.has_value());
auto territory = Locale::region_display_name("und"sv, "US"sv);
EXPECT(territory.has_value());
EXPECT_EQ(*territory, "US"sv);
territory = Locale::region_display_name("und"sv, "i-defintely-don't-exist"sv);
EXPECT(!territory.has_value());
auto script = Locale::script_display_name("und"sv, "Latn"sv);
EXPECT(script.has_value());
EXPECT_EQ(*script, "Latn"sv);
script = Locale::script_display_name("und"sv, "i-defintely-don't-exist"sv);
EXPECT(!script.has_value());
}