1 // Copyright (C) 2014 Google Inc. 2 // 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 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 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. 14 15 #include "lookup_key.h" 16 17 #include <libaddressinput/address_data.h> 18 #include <libaddressinput/util/basictypes.h> 19 20 #include <cstddef> 21 22 #include <gtest/gtest.h> 23 24 namespace { 25 26 using i18n::addressinput::AddressData; 27 using i18n::addressinput::LookupKey; 28 29 const size_t kMaxDepth = arraysize(LookupKey::kHierarchy) - 1; 30 31 TEST(LookupKeyTest, Empty) { 32 AddressData address; 33 LookupKey lookup_key; 34 lookup_key.FromAddress(address); 35 EXPECT_EQ("data/ZZ", lookup_key.ToKeyString(kMaxDepth)); 36 } 37 38 TEST(LookupKeyTest, AddressDepth1) { 39 AddressData address; 40 address.region_code = "111"; 41 LookupKey lookup_key; 42 lookup_key.FromAddress(address); 43 EXPECT_EQ(0, lookup_key.GetDepth()); 44 EXPECT_EQ("data/111", lookup_key.ToKeyString(kMaxDepth)); 45 } 46 47 TEST(LookupKeyTest, AddressDepth2) { 48 AddressData address; 49 address.region_code = "111"; 50 address.administrative_area = "222"; 51 LookupKey lookup_key; 52 lookup_key.FromAddress(address); 53 EXPECT_EQ(1, lookup_key.GetDepth()); 54 EXPECT_EQ("data/111/222", lookup_key.ToKeyString(kMaxDepth)); 55 } 56 57 TEST(LookupKeyTest, AddressDepth3) { 58 AddressData address; 59 address.region_code = "111"; 60 address.administrative_area = "222"; 61 address.locality = "333"; 62 LookupKey lookup_key; 63 lookup_key.FromAddress(address); 64 EXPECT_EQ(2, lookup_key.GetDepth()); 65 EXPECT_EQ("data/111/222/333", lookup_key.ToKeyString(kMaxDepth)); 66 } 67 68 TEST(LookupKeyTest, AddressDepth4) { 69 AddressData address; 70 address.region_code = "111"; 71 address.administrative_area = "222"; 72 address.locality = "333"; 73 address.dependent_locality = "444"; 74 LookupKey lookup_key; 75 lookup_key.FromAddress(address); 76 EXPECT_EQ(3, lookup_key.GetDepth()); 77 EXPECT_EQ("data/111/222/333/444", lookup_key.ToKeyString(kMaxDepth)); 78 } 79 80 TEST(LookupKeyTest, AddressDepthNonContiguous) { 81 AddressData address; 82 address.region_code = "111"; 83 address.administrative_area = "222"; 84 // No LOCALITY specified. 85 address.dependent_locality = "444"; 86 LookupKey lookup_key; 87 lookup_key.FromAddress(address); 88 EXPECT_EQ(1, lookup_key.GetDepth()); 89 EXPECT_EQ("data/111/222", lookup_key.ToKeyString(kMaxDepth)); 90 } 91 92 TEST(LookupKeyTest, RequestDepth) { 93 AddressData address; 94 address.region_code = "111"; 95 address.administrative_area = "222"; 96 address.locality = "333"; 97 address.dependent_locality = "444"; 98 LookupKey lookup_key; 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)); 104 } 105 106 TEST(LookupKeyTest, WithLanguageCodeDefaultLanguage) { 107 AddressData address; 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)); 116 } 117 118 TEST(LookupKeyTest, WithLanguageCodeAlternateLanguage) { 119 AddressData address; 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)); 128 } 129 130 TEST(LookupKeyTest, WithLanguageCodeInvalidLanguage) { 131 AddressData address; 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)); 140 } 141 142 TEST(LookupKeyTest, WithLanguageCodeAlternateLanguageNoState) { 143 AddressData address; 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)); 152 } 153 154 TEST(LookupKeyTest, GetRegionCode) { 155 AddressData address; 156 address.region_code = "rrr"; 157 LookupKey lookup_key; 158 lookup_key.FromAddress(address); 159 EXPECT_EQ(address.region_code, lookup_key.GetRegionCode()); 160 } 161 162 TEST(LookupKeyTest, FromAddressClearsExistingNodes) { 163 AddressData address; 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)); 172 } 173 174 } // namespace 175