1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "utils/i18n/locale.h"
18
19 #include "gtest/gtest.h"
20
21 namespace libtextclassifier3 {
22 namespace {
23
TEST(LocaleTest,ParseUnknown)24 TEST(LocaleTest, ParseUnknown) {
25 Locale locale = Locale::Invalid();
26 EXPECT_FALSE(locale.IsValid());
27 }
28
TEST(LocaleTest,ParseSwissEnglish)29 TEST(LocaleTest, ParseSwissEnglish) {
30 Locale locale = Locale::FromBCP47("en-CH");
31 EXPECT_TRUE(locale.IsValid());
32 EXPECT_EQ(locale.Language(), "en");
33 EXPECT_EQ(locale.Script(), "");
34 EXPECT_EQ(locale.Region(), "CH");
35 }
36
TEST(LocaleTest,ParseChineseChina)37 TEST(LocaleTest, ParseChineseChina) {
38 Locale locale = Locale::FromBCP47("zh-CN");
39 EXPECT_TRUE(locale.IsValid());
40 EXPECT_EQ(locale.Language(), "zh");
41 EXPECT_EQ(locale.Script(), "");
42 EXPECT_EQ(locale.Region(), "CN");
43 }
44
TEST(LocaleTest,ParseChineseTaiwan)45 TEST(LocaleTest, ParseChineseTaiwan) {
46 Locale locale = Locale::FromBCP47("zh-Hant-TW");
47 EXPECT_TRUE(locale.IsValid());
48 EXPECT_EQ(locale.Language(), "zh");
49 EXPECT_EQ(locale.Script(), "Hant");
50 EXPECT_EQ(locale.Region(), "TW");
51 }
52
TEST(LocaleTest,ParseEnglish)53 TEST(LocaleTest, ParseEnglish) {
54 Locale locale = Locale::FromBCP47("en");
55 EXPECT_TRUE(locale.IsValid());
56 EXPECT_EQ(locale.Language(), "en");
57 EXPECT_EQ(locale.Script(), "");
58 EXPECT_EQ(locale.Region(), "");
59 }
60
TEST(LocaleTest,ParseCineseTraditional)61 TEST(LocaleTest, ParseCineseTraditional) {
62 Locale locale = Locale::FromBCP47("zh-Hant");
63 EXPECT_TRUE(locale.IsValid());
64 EXPECT_EQ(locale.Language(), "zh");
65 EXPECT_EQ(locale.Script(), "Hant");
66 EXPECT_EQ(locale.Region(), "");
67 }
68
TEST(LocaleTest,IsAnyLocaleSupportedMatch)69 TEST(LocaleTest, IsAnyLocaleSupportedMatch) {
70 std::vector<Locale> locales = {Locale::FromBCP47("zh-HK"),
71 Locale::FromBCP47("en-UK")};
72 std::vector<Locale> supported_locales = {Locale::FromBCP47("en")};
73
74 EXPECT_TRUE(Locale::IsAnyLocaleSupported(locales, supported_locales,
75 /*default_value=*/false));
76 }
77
TEST(LocaleTest,IsAnyLocaleSupportedNotMatch)78 TEST(LocaleTest, IsAnyLocaleSupportedNotMatch) {
79 std::vector<Locale> locales = {Locale::FromBCP47("zh-tw")};
80 std::vector<Locale> supported_locales = {Locale::FromBCP47("en"),
81 Locale::FromBCP47("fr")};
82
83 EXPECT_FALSE(Locale::IsAnyLocaleSupported(locales, supported_locales,
84 /*default_value=*/false));
85 }
86
TEST(LocaleTest,IsAnyLocaleSupportedAnyLocale)87 TEST(LocaleTest, IsAnyLocaleSupportedAnyLocale) {
88 std::vector<Locale> locales = {Locale::FromBCP47("zh-tw")};
89 std::vector<Locale> supported_locales = {Locale::FromBCP47("*")};
90
91 EXPECT_TRUE(Locale::IsAnyLocaleSupported(locales, supported_locales,
92 /*default_value=*/false));
93 }
94
TEST(LocaleTest,IsAnyLocaleSupportedEmptyLocales)95 TEST(LocaleTest, IsAnyLocaleSupportedEmptyLocales) {
96 std::vector<Locale> supported_locales = {Locale::FromBCP47("en")};
97
98 EXPECT_TRUE(Locale::IsAnyLocaleSupported({}, supported_locales,
99 /*default_value=*/true));
100 }
101
102 } // namespace
103 } // namespace libtextclassifier3
104