1 // Copyright (C) 2014 Google Inc.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 #include "lookup_key.h"
17 #include <libaddressinput/address_data.h>
18 #include <libaddressinput/util/basictypes.h>
22 #include <gtest/gtest.h>
26 using i18n::addressinput::AddressData;
27 using i18n::addressinput::LookupKey;
29 const size_t kMaxDepth = arraysize(LookupKey::kHierarchy) - 1;
31 TEST(LookupKeyTest, Empty) {
34 lookup_key.FromAddress(address);
35 EXPECT_EQ("data/ZZ", lookup_key.ToKeyString(kMaxDepth));
38 TEST(LookupKeyTest, AddressDepth1) {
40 address.region_code = "111";
42 lookup_key.FromAddress(address);
43 EXPECT_EQ(0, lookup_key.GetDepth());
44 EXPECT_EQ("data/111", lookup_key.ToKeyString(kMaxDepth));
47 TEST(LookupKeyTest, AddressDepth2) {
49 address.region_code = "111";
50 address.administrative_area = "222";
52 lookup_key.FromAddress(address);
53 EXPECT_EQ(1, lookup_key.GetDepth());
54 EXPECT_EQ("data/111/222", lookup_key.ToKeyString(kMaxDepth));
57 TEST(LookupKeyTest, AddressDepth3) {
59 address.region_code = "111";
60 address.administrative_area = "222";
61 address.locality = "333";
63 lookup_key.FromAddress(address);
64 EXPECT_EQ(2, lookup_key.GetDepth());
65 EXPECT_EQ("data/111/222/333", lookup_key.ToKeyString(kMaxDepth));
68 TEST(LookupKeyTest, AddressDepth4) {
70 address.region_code = "111";
71 address.administrative_area = "222";
72 address.locality = "333";
73 address.dependent_locality = "444";
75 lookup_key.FromAddress(address);
76 EXPECT_EQ(3, lookup_key.GetDepth());
77 EXPECT_EQ("data/111/222/333/444", lookup_key.ToKeyString(kMaxDepth));
80 TEST(LookupKeyTest, AddressDepthNonContiguous) {
82 address.region_code = "111";
83 address.administrative_area = "222";
84 // No LOCALITY specified.
85 address.dependent_locality = "444";
87 lookup_key.FromAddress(address);
88 EXPECT_EQ(1, lookup_key.GetDepth());
89 EXPECT_EQ("data/111/222", lookup_key.ToKeyString(kMaxDepth));
92 TEST(LookupKeyTest, RequestDepth) {
94 address.region_code = "111";
95 address.administrative_area = "222";
96 address.locality = "333";
97 address.dependent_locality = "444";
99 lookup_key.FromAddress(address);
100 EXPECT_EQ("data/111", lookup_key.ToKeyString(0));
101 EXPECT_EQ("data/111/222", lookup_key.ToKeyString(1));
102 EXPECT_EQ("data/111/222/333", lookup_key.ToKeyString(2));
103 EXPECT_EQ("data/111/222/333/444", lookup_key.ToKeyString(3));
106 TEST(LookupKeyTest, WithLanguageCodeDefaultLanguage) {
108 // Use real data here as the choice of adding a language requires metadata.
109 address.region_code = "CA";
110 address.administrative_area = "ON";
111 address.language_code = "en";
112 LookupKey lookup_key;
113 lookup_key.FromAddress(address);
114 EXPECT_EQ("data/CA", lookup_key.ToKeyString(0));
115 EXPECT_EQ("data/CA/ON", lookup_key.ToKeyString(1));
118 TEST(LookupKeyTest, WithLanguageCodeAlternateLanguage) {
120 // Use real data here as the choice of adding a language requires metadata.
121 address.region_code = "CA";
122 address.administrative_area = "ON";
123 address.language_code = "fr";
124 LookupKey lookup_key;
125 lookup_key.FromAddress(address);
126 EXPECT_EQ("data/CA--fr", lookup_key.ToKeyString(0));
127 EXPECT_EQ("data/CA/ON--fr", lookup_key.ToKeyString(1));
130 TEST(LookupKeyTest, WithLanguageCodeInvalidLanguage) {
132 // Use real data here as the choice of adding a language requires metadata.
133 address.region_code = "CA";
134 address.administrative_area = "ON";
135 address.language_code = "de";
136 LookupKey lookup_key;
137 lookup_key.FromAddress(address);
138 EXPECT_EQ("data/CA", lookup_key.ToKeyString(0));
139 EXPECT_EQ("data/CA/ON", lookup_key.ToKeyString(1));
142 TEST(LookupKeyTest, WithLanguageCodeAlternateLanguageNoState) {
144 // Use real data here as the choice of adding a language requires metadata.
145 // Afgahnistan has multiple languages (including Pashto as an alternative)
146 // but no subregions.
147 address.region_code = "AF";
148 address.language_code = "ps";
149 LookupKey lookup_key;
150 lookup_key.FromAddress(address);
151 EXPECT_EQ("data/AF", lookup_key.ToKeyString(0));
154 TEST(LookupKeyTest, GetRegionCode) {
156 address.region_code = "rrr";
157 LookupKey lookup_key;
158 lookup_key.FromAddress(address);
159 EXPECT_EQ(address.region_code, lookup_key.GetRegionCode());
162 TEST(LookupKeyTest, FromAddressClearsExistingNodes) {
164 address.region_code = "111";
165 address.administrative_area = "222";
166 LookupKey lookup_key;
167 lookup_key.FromAddress(address);
168 EXPECT_EQ("data/111/222", lookup_key.ToKeyString(kMaxDepth));
169 address.administrative_area.clear();
170 lookup_key.FromAddress(address);
171 EXPECT_EQ("data/111", lookup_key.ToKeyString(kMaxDepth));