diff options
author | Timothy Flynn <trflynn89@pm.me> | 2023-01-19 10:53:20 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-01-19 20:57:30 +0000 |
commit | ca62aeb6bd184adb2644e5d179f60cb3a42171b4 (patch) | |
tree | 33230228acafff94573e17adad17025b405a9dcb /Tests | |
parent | 618714e29ab8669700c3b690afb1ebce3540956f (diff) | |
download | serenity-ca62aeb6bd184adb2644e5d179f60cb3a42171b4.zip |
LibLocale+LibJS: Port locale parsing and processing to String
In order to prevent this commit from having to refactor almost all of
Intl, the goal here is to update the internal parsing/canonicalization
of locales within LibLocale only. Call sites which are already equiped
to handle String and OOM errors do so, however.
Diffstat (limited to 'Tests')
-rw-r--r-- | Tests/LibLocale/TestLocale.cpp | 107 |
1 files changed, 77 insertions, 30 deletions
diff --git a/Tests/LibLocale/TestLocale.cpp b/Tests/LibLocale/TestLocale.cpp index 7df720eb7c..7300faddac 100644 --- a/Tests/LibLocale/TestLocale.cpp +++ b/Tests/LibLocale/TestLocale.cpp @@ -87,20 +87,34 @@ TEST_CASE(is_type_identifier) EXPECT(!Locale::is_type_identifier("aaaa-"sv)); } +template<typename LHS, typename RHS> +[[nodiscard]] static bool compare_vectors(LHS const& lhs, RHS const& rhs) +{ + if (lhs.size() != rhs.size()) + return false; + + for (size_t i = 0; i < lhs.size(); ++i) { + if (lhs[i] != rhs[i]) + return false; + } + + return true; +} + TEST_CASE(parse_unicode_locale_id) { auto fail = [](StringView locale) { - auto locale_id = Locale::parse_unicode_locale_id(locale); + auto locale_id = MUST(Locale::parse_unicode_locale_id(locale)); EXPECT(!locale_id.has_value()); }; - auto pass = [](StringView locale, Optional<StringView> expected_language, Optional<StringView> expected_script, Optional<StringView> expected_region, Vector<DeprecatedString> expected_variants) { - auto locale_id = Locale::parse_unicode_locale_id(locale); + auto pass = [](StringView locale, Optional<StringView> expected_language, Optional<StringView> expected_script, Optional<StringView> expected_region, Vector<StringView> expected_variants) { + auto locale_id = MUST(Locale::parse_unicode_locale_id(locale)); VERIFY(locale_id.has_value()); EXPECT_EQ(locale_id->language_id.language, expected_language); EXPECT_EQ(locale_id->language_id.script, expected_script); EXPECT_EQ(locale_id->language_id.region, expected_region); - EXPECT_EQ(locale_id->language_id.variants, expected_variants); + EXPECT(compare_vectors(locale_id->language_id.variants, expected_variants)); }; fail("a"sv); @@ -120,17 +134,27 @@ TEST_CASE(parse_unicode_locale_id) TEST_CASE(parse_unicode_locale_id_with_unicode_locale_extension) { + struct LocaleExtension { + struct Keyword { + StringView key {}; + StringView value {}; + }; + + Vector<StringView> attributes {}; + Vector<Keyword> keywords {}; + }; + auto fail = [](StringView locale) { - auto locale_id = Locale::parse_unicode_locale_id(locale); + auto locale_id = MUST(Locale::parse_unicode_locale_id(locale)); EXPECT(!locale_id.has_value()); }; - auto pass = [](StringView locale, Locale::LocaleExtension const& expected_extension) { - auto locale_id = Locale::parse_unicode_locale_id(locale); + auto pass = [](StringView locale, LocaleExtension const& expected_extension) { + auto locale_id = MUST(Locale::parse_unicode_locale_id(locale)); VERIFY(locale_id.has_value()); EXPECT_EQ(locale_id->extensions.size(), 1u); auto const& actual_extension = locale_id->extensions[0].get<Locale::LocaleExtension>(); - VERIFY(actual_extension.attributes == expected_extension.attributes); + EXPECT(compare_vectors(actual_extension.attributes, expected_extension.attributes)); EXPECT_EQ(actual_extension.keywords.size(), expected_extension.keywords.size()); for (size_t i = 0; i < actual_extension.keywords.size(); ++i) { @@ -166,12 +190,30 @@ TEST_CASE(parse_unicode_locale_id_with_unicode_locale_extension) TEST_CASE(parse_unicode_locale_id_with_transformed_extension) { + struct TransformedExtension { + struct LanguageID { + bool is_root { false }; + Optional<StringView> language {}; + Optional<StringView> script {}; + Optional<StringView> region {}; + Vector<StringView> variants {}; + }; + + struct TransformedField { + StringView key {}; + StringView value {}; + }; + + Optional<LanguageID> language {}; + Vector<TransformedField> fields {}; + }; + auto fail = [](StringView locale) { - auto locale_id = Locale::parse_unicode_locale_id(locale); + auto locale_id = MUST(Locale::parse_unicode_locale_id(locale)); EXPECT(!locale_id.has_value()); }; - auto pass = [](StringView locale, Locale::TransformedExtension const& expected_extension) { - auto locale_id = Locale::parse_unicode_locale_id(locale); + auto pass = [](StringView locale, TransformedExtension const& expected_extension) { + auto locale_id = MUST(Locale::parse_unicode_locale_id(locale)); VERIFY(locale_id.has_value()); EXPECT_EQ(locale_id->extensions.size(), 1u); @@ -182,7 +224,7 @@ TEST_CASE(parse_unicode_locale_id_with_transformed_extension) EXPECT_EQ(actual_extension.language->language, expected_extension.language->language); EXPECT_EQ(actual_extension.language->script, expected_extension.language->script); EXPECT_EQ(actual_extension.language->region, expected_extension.language->region); - EXPECT_EQ(actual_extension.language->variants, expected_extension.language->variants); + EXPECT(compare_vectors(actual_extension.language->variants, expected_extension.language->variants)); } EXPECT_EQ(actual_extension.fields.size(), expected_extension.fields.size()); @@ -216,28 +258,33 @@ TEST_CASE(parse_unicode_locale_id_with_transformed_extension) fail("en-t-k0-aa"sv); fail("en-t-k0-aaaaaaaaa"sv); - pass("en-t-en"sv, { Locale::LanguageID { false, "en"sv }, {} }); - pass("en-t-en-latn"sv, { Locale::LanguageID { false, "en"sv, "latn"sv }, {} }); - pass("en-t-en-us"sv, { Locale::LanguageID { false, "en"sv, {}, "us"sv }, {} }); - pass("en-t-en-latn-us"sv, { Locale::LanguageID { false, "en"sv, "latn"sv, "us"sv }, {} }); - pass("en-t-en-posix"sv, { Locale::LanguageID { false, "en"sv, {}, {}, { "posix"sv } }, {} }); - pass("en-t-en-latn-posix"sv, { Locale::LanguageID { false, "en"sv, "latn"sv, {}, { "posix"sv } }, {} }); - pass("en-t-en-us-posix"sv, { Locale::LanguageID { false, "en"sv, {}, "us"sv, { "posix"sv } }, {} }); - pass("en-t-en-latn-us-posix"sv, { Locale::LanguageID { false, "en"sv, "latn"sv, "us"sv, { "posix"sv } }, {} }); + pass("en-t-en"sv, { TransformedExtension::LanguageID { false, "en"sv }, {} }); + pass("en-t-en-latn"sv, { TransformedExtension::LanguageID { false, "en"sv, "latn"sv }, {} }); + pass("en-t-en-us"sv, { TransformedExtension::LanguageID { false, "en"sv, {}, "us"sv }, {} }); + pass("en-t-en-latn-us"sv, { TransformedExtension::LanguageID { false, "en"sv, "latn"sv, "us"sv }, {} }); + pass("en-t-en-posix"sv, { TransformedExtension::LanguageID { false, "en"sv, {}, {}, { "posix"sv } }, {} }); + pass("en-t-en-latn-posix"sv, { TransformedExtension::LanguageID { false, "en"sv, "latn"sv, {}, { "posix"sv } }, {} }); + pass("en-t-en-us-posix"sv, { TransformedExtension::LanguageID { false, "en"sv, {}, "us"sv, { "posix"sv } }, {} }); + pass("en-t-en-latn-us-posix"sv, { TransformedExtension::LanguageID { false, "en"sv, "latn"sv, "us"sv, { "posix"sv } }, {} }); pass("en-t-k0-aaa"sv, { {}, { { "k0"sv, { "aaa"sv } } } }); pass("en-t-k0-aaa-bbbb"sv, { {}, { { "k0"sv, "aaa-bbbb"sv } } }); pass("en-t-k0-aaa-k1-bbbb"sv, { {}, { { "k0"sv, { "aaa"sv } }, { "k1"sv, "bbbb"sv } } }); - pass("en-t-en-k0-aaa"sv, { Locale::LanguageID { false, "en"sv }, { { "k0"sv, "aaa"sv } } }); + pass("en-t-en-k0-aaa"sv, { TransformedExtension::LanguageID { false, "en"sv }, { { "k0"sv, "aaa"sv } } }); } TEST_CASE(parse_unicode_locale_id_with_other_extension) { + struct OtherExtension { + char key {}; + StringView value {}; + }; + auto fail = [](StringView locale) { - auto locale_id = Locale::parse_unicode_locale_id(locale); + auto locale_id = MUST(Locale::parse_unicode_locale_id(locale)); EXPECT(!locale_id.has_value()); }; - auto pass = [](StringView locale, Locale::OtherExtension const& expected_extension) { - auto locale_id = Locale::parse_unicode_locale_id(locale); + auto pass = [](StringView locale, OtherExtension const& expected_extension) { + auto locale_id = MUST(Locale::parse_unicode_locale_id(locale)); VERIFY(locale_id.has_value()); EXPECT_EQ(locale_id->extensions.size(), 1u); @@ -267,13 +314,13 @@ TEST_CASE(parse_unicode_locale_id_with_other_extension) TEST_CASE(parse_unicode_locale_id_with_private_use_extension) { auto fail = [](StringView locale) { - auto locale_id = Locale::parse_unicode_locale_id(locale); + auto locale_id = MUST(Locale::parse_unicode_locale_id(locale)); EXPECT(!locale_id.has_value()); }; - auto pass = [](StringView locale, Vector<DeprecatedString> const& expected_extension) { - auto locale_id = Locale::parse_unicode_locale_id(locale); + auto pass = [](StringView locale, Vector<StringView> const& expected_extension) { + auto locale_id = MUST(Locale::parse_unicode_locale_id(locale)); VERIFY(locale_id.has_value()); - EXPECT_EQ(locale_id->private_use_extensions, expected_extension); + EXPECT(compare_vectors(locale_id->private_use_extensions, expected_extension)); }; fail("en-x"sv); @@ -291,10 +338,10 @@ TEST_CASE(parse_unicode_locale_id_with_private_use_extension) TEST_CASE(canonicalize_unicode_locale_id) { auto test = [](StringView locale, StringView expected_canonical_locale) { - auto locale_id = Locale::parse_unicode_locale_id(locale); + auto locale_id = MUST(Locale::parse_unicode_locale_id(locale)); VERIFY(locale_id.has_value()); - auto canonical_locale = Locale::canonicalize_unicode_locale_id(*locale_id); + auto canonical_locale = MUST(Locale::canonicalize_unicode_locale_id(*locale_id)); EXPECT_EQ(*canonical_locale, expected_canonical_locale); }; |