blob: b82d2b9cbc0d22615b3586da63ad95a5ec4de17b [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
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"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070018#include "util/Util.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080019
20#include <gtest/gtest.h>
21#include <string>
22
23namespace aapt {
24
Adam Lesinskicacb28f2016-10-19 12:18:14 -070025static ::testing::AssertionResult TestLanguage(const char* input,
26 const char* lang) {
27 std::vector<std::string> parts = util::splitAndLowercase(input, '-');
28 LocaleValue lv;
29 ssize_t count = lv.initFromParts(std::begin(parts), std::end(parts));
30 if (count < 0) {
31 return ::testing::AssertionFailure() << " failed to parse '" << input
32 << "'.";
33 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080034
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 if (count != 1) {
36 return ::testing::AssertionFailure()
37 << count << " parts were consumed parsing '" << input
38 << "' but expected 1.";
39 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080040
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 if (memcmp(lv.language, lang, std::min(strlen(lang), sizeof(lv.language))) !=
42 0) {
43 return ::testing::AssertionFailure()
44 << "expected " << lang << " but got "
45 << std::string(lv.language, sizeof(lv.language)) << ".";
46 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080047
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 return ::testing::AssertionSuccess();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080049}
50
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051static ::testing::AssertionResult TestLanguageRegion(const char* input,
52 const char* lang,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080053 const char* region) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 std::vector<std::string> parts = util::splitAndLowercase(input, '-');
55 LocaleValue lv;
56 ssize_t count = lv.initFromParts(std::begin(parts), std::end(parts));
57 if (count < 0) {
58 return ::testing::AssertionFailure() << " failed to parse '" << input
59 << "'.";
60 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080061
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 if (count != 2) {
63 return ::testing::AssertionFailure()
64 << count << " parts were consumed parsing '" << input
65 << "' but expected 2.";
66 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080067
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 if (memcmp(lv.language, lang, std::min(strlen(lang), sizeof(lv.language))) !=
69 0) {
70 return ::testing::AssertionFailure()
71 << "expected " << input << " but got "
72 << std::string(lv.language, sizeof(lv.language)) << ".";
73 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080074
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 if (memcmp(lv.region, region, std::min(strlen(region), sizeof(lv.region))) !=
76 0) {
77 return ::testing::AssertionFailure()
78 << "expected " << region << " but got "
79 << std::string(lv.region, sizeof(lv.region)) << ".";
80 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080081
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 return ::testing::AssertionSuccess();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080083}
84
85TEST(ConfigDescriptionTest, ParseLanguage) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 EXPECT_TRUE(TestLanguage("en", "en"));
87 EXPECT_TRUE(TestLanguage("fr", "fr"));
88 EXPECT_FALSE(TestLanguage("land", ""));
89 EXPECT_TRUE(TestLanguage("fr-land", "fr"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080090
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 EXPECT_TRUE(TestLanguageRegion("fr-rCA", "fr", "CA"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080092}
93
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094} // namespace aapt