1 // Copyright (C) 2012 The Libphonenumber Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // Author: Patrick Mezard
16
17 #include "phonenumbers/geocoding/mapping_file_provider.h"
18
19 #include <gtest/gtest.h> // NOLINT(build/include_order)
20
21 #include "phonenumbers/geocoding/geocoding_data.h"
22
23 namespace i18n {
24 namespace phonenumbers {
25
26 using std::string;
27
28 namespace {
29
30 #define COUNTRY_LANGUAGES(code, languagelist) \
31 const char* country_languages_##code[] = languagelist; \
32 const CountryLanguages country_##code = { \
33 country_languages_##code, \
34 sizeof(country_languages_##code) / sizeof(*country_languages_##code), \
35 };
36
37 // Array literals cannot be passed as regular macro arguments, the separating
38 // commas are interpreted as macro arguments separators. The following dummy
39 // variadic macro wraps the array commas, and appears as a single argument to an
40 // outer macro call.
41 #define ARRAY_WRAPPER(...) __VA_ARGS__
42
43 const int country_calling_codes[] = {1, 41, 65, 86};
44
45 const int country_calling_codes_size =
46 sizeof(country_calling_codes) / sizeof(*country_calling_codes);
47
48 COUNTRY_LANGUAGES(1, ARRAY_WRAPPER({"en"}));
49 COUNTRY_LANGUAGES(41, ARRAY_WRAPPER({"de", "fr", "it", "rm"}));
50 COUNTRY_LANGUAGES(65, ARRAY_WRAPPER({"en", "ms", "ta", "zh_Hans"}));
51 COUNTRY_LANGUAGES(86, ARRAY_WRAPPER({"en", "zh", "zh_Hant"}));
52
53 const CountryLanguages* country_languages[] = {
54 &country_1,
55 &country_41,
56 &country_65,
57 &country_86,
58 };
59
test_get_country_languages(int index)60 const CountryLanguages* test_get_country_languages(int index) {
61 return country_languages[index];
62 }
63
64 } // namespace
65
TEST(MappingFileProviderTest,TestGetFileName)66 TEST(MappingFileProviderTest, TestGetFileName) {
67 MappingFileProvider provider(country_calling_codes,
68 country_calling_codes_size,
69 test_get_country_languages);
70
71 string filename;
72 EXPECT_EQ("1_en", provider.GetFileName(1, "en", "", "", &filename));
73 EXPECT_EQ("1_en", provider.GetFileName(1, "en", "", "US", &filename));
74 EXPECT_EQ("1_en", provider.GetFileName(1, "en", "", "GB", &filename));
75 EXPECT_EQ("41_de", provider.GetFileName(41, "de", "", "CH", &filename));
76 EXPECT_EQ("", provider.GetFileName(44, "en", "", "GB", &filename));
77 EXPECT_EQ("86_zh", provider.GetFileName(86, "zh", "", "", &filename));
78 EXPECT_EQ("86_zh", provider.GetFileName(86, "zh", "Hans", "", &filename));
79 EXPECT_EQ("86_zh", provider.GetFileName(86, "zh", "", "CN", &filename));
80 EXPECT_EQ("", provider.GetFileName(86, "", "", "CN", &filename));
81 EXPECT_EQ("86_zh", provider.GetFileName(86, "zh", "Hans", "CN", &filename));
82 EXPECT_EQ("86_zh", provider.GetFileName(86, "zh", "Hans", "SG", &filename));
83 EXPECT_EQ("86_zh", provider.GetFileName(86, "zh", "", "SG", &filename));
84 EXPECT_EQ("86_zh_Hant", provider.GetFileName(86, "zh", "", "TW", &filename));
85 EXPECT_EQ("86_zh_Hant", provider.GetFileName(86, "zh", "", "HK", &filename));
86 EXPECT_EQ("86_zh_Hant", provider.GetFileName(86, "zh", "Hant", "TW",
87 &filename));
88 }
89
90 } // namespace phonenumbers
91 } // namespace i18n
92