1 /*
2  * Copyright (C) 2015 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 "Locale.h"
18 #include "Util.h"
19 
20 #include <gtest/gtest.h>
21 #include <string>
22 
23 namespace aapt {
24 
TestLanguage(const char * input,const char * lang)25 static ::testing::AssertionResult TestLanguage(const char* input, const char* lang) {
26     std::vector<std::string> parts = util::splitAndLowercase(std::string(input), '-');
27     LocaleValue lv;
28     ssize_t count = lv.initFromParts(std::begin(parts), std::end(parts));
29     if (count < 0) {
30         return ::testing::AssertionFailure() << " failed to parse '" << input << "'.";
31     }
32 
33     if (count != 1) {
34         return ::testing::AssertionFailure() << count
35             << " parts were consumed parsing '" << input << "' but expected 1.";
36     }
37 
38     if (memcmp(lv.language, lang, std::min(strlen(lang), sizeof(lv.language))) != 0) {
39         return ::testing::AssertionFailure() << "expected " << lang << " but got "
40             << std::string(lv.language, sizeof(lv.language)) << ".";
41     }
42 
43     return ::testing::AssertionSuccess();
44 }
45 
TestLanguageRegion(const char * input,const char * lang,const char * region)46 static ::testing::AssertionResult TestLanguageRegion(const char* input, const char* lang,
47                                                      const char* region) {
48     std::vector<std::string> parts = util::splitAndLowercase(std::string(input), '-');
49     LocaleValue lv;
50     ssize_t count = lv.initFromParts(std::begin(parts), std::end(parts));
51     if (count < 0) {
52         return ::testing::AssertionFailure() << " failed to parse '" << input << "'.";
53     }
54 
55     if (count != 2) {
56         return ::testing::AssertionFailure() << count
57             << " parts were consumed parsing '" << input << "' but expected 2.";
58     }
59 
60     if (memcmp(lv.language, lang, std::min(strlen(lang), sizeof(lv.language))) != 0) {
61         return ::testing::AssertionFailure() << "expected " << input << " but got "
62             << std::string(lv.language, sizeof(lv.language)) << ".";
63     }
64 
65     if (memcmp(lv.region, region, std::min(strlen(region), sizeof(lv.region))) != 0) {
66         return ::testing::AssertionFailure() << "expected " << region << " but got "
67             << std::string(lv.region, sizeof(lv.region)) << ".";
68     }
69 
70     return ::testing::AssertionSuccess();
71 }
72 
TEST(ConfigDescriptionTest,ParseLanguage)73 TEST(ConfigDescriptionTest, ParseLanguage) {
74     EXPECT_TRUE(TestLanguage("en", "en"));
75     EXPECT_TRUE(TestLanguage("fr", "fr"));
76     EXPECT_FALSE(TestLanguage("land", ""));
77     EXPECT_TRUE(TestLanguage("fr-land", "fr"));
78 
79     EXPECT_TRUE(TestLanguageRegion("fr-rCA", "fr", "CA"));
80 }
81 
82 } // namespace aapt
83