blob: 68b4cae44e15bce8707dd845e96b82a13ca69ef6 [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 Lesinski6f6ceb72014-11-14 14:48:12 -080018
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080019#include <string>
20
Adam Lesinskice5e56e2016-10-21 17:56:45 -070021#include "gtest/gtest.h"
22
23#include "util/Util.h"
24
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080025namespace aapt {
26
Adam Lesinskicacb28f2016-10-19 12:18:14 -070027static ::testing::AssertionResult TestLanguage(const char* input,
28 const char* lang) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070029 std::vector<std::string> parts = util::SplitAndLowercase(input, '-');
Adam Lesinskicacb28f2016-10-19 12:18:14 -070030 LocaleValue lv;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070031 ssize_t count = lv.InitFromParts(std::begin(parts), std::end(parts));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070032 if (count < 0) {
33 return ::testing::AssertionFailure() << " failed to parse '" << input
34 << "'.";
35 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080036
Adam Lesinskicacb28f2016-10-19 12:18:14 -070037 if (count != 1) {
38 return ::testing::AssertionFailure()
39 << count << " parts were consumed parsing '" << input
40 << "' but expected 1.";
41 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080042
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 if (memcmp(lv.language, lang, std::min(strlen(lang), sizeof(lv.language))) !=
44 0) {
45 return ::testing::AssertionFailure()
46 << "expected " << lang << " but got "
47 << std::string(lv.language, sizeof(lv.language)) << ".";
48 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080049
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 return ::testing::AssertionSuccess();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080051}
52
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053static ::testing::AssertionResult TestLanguageRegion(const char* input,
54 const char* lang,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080055 const char* region) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 std::vector<std::string> parts = util::SplitAndLowercase(input, '-');
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 LocaleValue lv;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 ssize_t count = lv.InitFromParts(std::begin(parts), std::end(parts));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 if (count < 0) {
60 return ::testing::AssertionFailure() << " failed to parse '" << input
61 << "'.";
62 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080063
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 if (count != 2) {
65 return ::testing::AssertionFailure()
66 << count << " parts were consumed parsing '" << input
67 << "' but expected 2.";
68 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080069
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 if (memcmp(lv.language, lang, std::min(strlen(lang), sizeof(lv.language))) !=
71 0) {
72 return ::testing::AssertionFailure()
73 << "expected " << input << " but got "
74 << std::string(lv.language, sizeof(lv.language)) << ".";
75 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080076
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 if (memcmp(lv.region, region, std::min(strlen(region), sizeof(lv.region))) !=
78 0) {
79 return ::testing::AssertionFailure()
80 << "expected " << region << " but got "
81 << std::string(lv.region, sizeof(lv.region)) << ".";
82 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080083
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 return ::testing::AssertionSuccess();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080085}
86
87TEST(ConfigDescriptionTest, ParseLanguage) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 EXPECT_TRUE(TestLanguage("en", "en"));
89 EXPECT_TRUE(TestLanguage("fr", "fr"));
90 EXPECT_FALSE(TestLanguage("land", ""));
91 EXPECT_TRUE(TestLanguage("fr-land", "fr"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080092
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 EXPECT_TRUE(TestLanguageRegion("fr-rCA", "fr", "CA"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080094}
95
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096} // namespace aapt