2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "util/Util.h"
20 #include <gtest/gtest.h>
25 static ::testing::AssertionResult TestLanguage(const char* input, const char* lang) {
26 std::vector<std::string> parts = util::splitAndLowercase(std::string(input), '-');
28 ssize_t count = lv.initFromParts(std::begin(parts), std::end(parts));
30 return ::testing::AssertionFailure() << " failed to parse '" << input << "'.";
34 return ::testing::AssertionFailure() << count
35 << " parts were consumed parsing '" << input << "' but expected 1.";
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)) << ".";
43 return ::testing::AssertionSuccess();
46 static ::testing::AssertionResult TestLanguageRegion(const char* input, const char* lang,
48 std::vector<std::string> parts = util::splitAndLowercase(std::string(input), '-');
50 ssize_t count = lv.initFromParts(std::begin(parts), std::end(parts));
52 return ::testing::AssertionFailure() << " failed to parse '" << input << "'.";
56 return ::testing::AssertionFailure() << count
57 << " parts were consumed parsing '" << input << "' but expected 2.";
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)) << ".";
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)) << ".";
70 return ::testing::AssertionSuccess();
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"));
79 EXPECT_TRUE(TestLanguageRegion("fr-rCA", "fr", "CA"));