Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 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" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 18 | #include "util/Util.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 19 | |
| 20 | #include <gtest/gtest.h> |
| 21 | #include <string> |
| 22 | |
| 23 | namespace aapt { |
| 24 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 25 | static ::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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 34 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 35 | if (count != 1) { |
| 36 | return ::testing::AssertionFailure() |
| 37 | << count << " parts were consumed parsing '" << input |
| 38 | << "' but expected 1."; |
| 39 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 40 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 41 | 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 47 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 48 | return ::testing::AssertionSuccess(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 51 | static ::testing::AssertionResult TestLanguageRegion(const char* input, |
| 52 | const char* lang, |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 53 | const char* region) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 54 | 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 61 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 62 | if (count != 2) { |
| 63 | return ::testing::AssertionFailure() |
| 64 | << count << " parts were consumed parsing '" << input |
| 65 | << "' but expected 2."; |
| 66 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 67 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 68 | 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 74 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 75 | 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 81 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 82 | return ::testing::AssertionSuccess(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | TEST(ConfigDescriptionTest, ParseLanguage) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 86 | EXPECT_TRUE(TestLanguage("en", "en")); |
| 87 | EXPECT_TRUE(TestLanguage("fr", "fr")); |
| 88 | EXPECT_FALSE(TestLanguage("land", "")); |
| 89 | EXPECT_TRUE(TestLanguage("fr-land", "fr")); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 90 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 91 | EXPECT_TRUE(TestLanguageRegion("fr-rCA", "fr", "CA")); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 94 | } // namespace aapt |