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
19#include <string>
20
21#include "gtest/gtest.h"
22
23#include "util/Util.h"
24
25namespace aapt {
26
27static ::testing::AssertionResult TestLanguage(const char* input,
28                                               const char* lang) {
29  std::vector<std::string> parts = util::SplitAndLowercase(input, '-');
30  LocaleValue lv;
31  ssize_t count = lv.InitFromParts(std::begin(parts), std::end(parts));
32  if (count < 0) {
33    return ::testing::AssertionFailure() << " failed to parse '" << input
34                                         << "'.";
35  }
36
37  if (count != 1) {
38    return ::testing::AssertionFailure()
39           << count << " parts were consumed parsing '" << input
40           << "' but expected 1.";
41  }
42
43  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  }
49
50  return ::testing::AssertionSuccess();
51}
52
53static ::testing::AssertionResult TestLanguageRegion(const char* input,
54                                                     const char* lang,
55                                                     const char* region) {
56  std::vector<std::string> parts = util::SplitAndLowercase(input, '-');
57  LocaleValue lv;
58  ssize_t count = lv.InitFromParts(std::begin(parts), std::end(parts));
59  if (count < 0) {
60    return ::testing::AssertionFailure() << " failed to parse '" << input
61                                         << "'.";
62  }
63
64  if (count != 2) {
65    return ::testing::AssertionFailure()
66           << count << " parts were consumed parsing '" << input
67           << "' but expected 2.";
68  }
69
70  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  }
76
77  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  }
83
84  return ::testing::AssertionSuccess();
85}
86
87TEST(ConfigDescriptionTest, ParseLanguage) {
88  EXPECT_TRUE(TestLanguage("en", "en"));
89  EXPECT_TRUE(TestLanguage("fr", "fr"));
90  EXPECT_FALSE(TestLanguage("land", ""));
91  EXPECT_TRUE(TestLanguage("fr-land", "fr"));
92
93  EXPECT_TRUE(TestLanguageRegion("fr-rCA", "fr", "CA"));
94}
95
96}  // namespace aapt
97