aboutsummaryrefslogtreecommitdiff
path: root/cpp/test
diff options
context:
space:
mode:
authorroubert@google.com <roubert@google.com@38ededc0-08b8-5190-f2ac-b31f878777ad>2014-03-21 12:21:22 +0000
committerroubert@google.com <roubert@google.com@38ededc0-08b8-5190-f2ac-b31f878777ad>2014-03-21 12:21:22 +0000
commit7764218473f33ca749ca4a3cc66d1118793481dd (patch)
treefdaed07e3e171f40dacd91beee286416f6d8e1fb /cpp/test
parente0cd6d6862db732f3fd035cdebd3a5d0ee8701c7 (diff)
downloadsrc-7764218473f33ca749ca4a3cc66d1118793481dd.tar.gz
Add the LookupKey helper class.
A LookupKey maps between an AddressData struct and the key string used to request address data from an address data server. git-svn-id: http://libaddressinput.googlecode.com/svn/trunk@195 38ededc0-08b8-5190-f2ac-b31f878777ad
Diffstat (limited to 'cpp/test')
-rw-r--r--cpp/test/lookup_key_test.cc124
1 files changed, 124 insertions, 0 deletions
diff --git a/cpp/test/lookup_key_test.cc b/cpp/test/lookup_key_test.cc
new file mode 100644
index 0000000..c348fca
--- /dev/null
+++ b/cpp/test/lookup_key_test.cc
@@ -0,0 +1,124 @@
+// Copyright (C) 2014 Google Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "lookup_key.h"
+
+#include <libaddressinput/address_data.h>
+#include <libaddressinput/util/basictypes.h>
+
+#include <gtest/gtest.h>
+
+namespace {
+
+using i18n::addressinput::AddressData;
+using i18n::addressinput::LookupKey;
+
+const size_t kMaxDepth = arraysize(LookupKey::kHierarchy) - 1;
+
+TEST(LookupKeyTest, Empty) {
+ AddressData address;
+ LookupKey lookup_key;
+ lookup_key.FromAddress(address);
+ EXPECT_EQ("data/ZZ", lookup_key.ToKeyString(kMaxDepth));
+}
+
+TEST(LookupKeyTest, AddressDepth1) {
+ AddressData address;
+ address.region_code = "111";
+ LookupKey lookup_key;
+ lookup_key.FromAddress(address);
+ EXPECT_EQ(0, lookup_key.GetDepth());
+ EXPECT_EQ("data/111", lookup_key.ToKeyString(kMaxDepth));
+}
+
+TEST(LookupKeyTest, AddressDepth2) {
+ AddressData address;
+ address.region_code = "111";
+ address.administrative_area = "222";
+ LookupKey lookup_key;
+ lookup_key.FromAddress(address);
+ EXPECT_EQ(1, lookup_key.GetDepth());
+ EXPECT_EQ("data/111/222", lookup_key.ToKeyString(kMaxDepth));
+}
+
+TEST(LookupKeyTest, AddressDepth3) {
+ AddressData address;
+ address.region_code = "111";
+ address.administrative_area = "222";
+ address.locality = "333";
+ LookupKey lookup_key;
+ lookup_key.FromAddress(address);
+ EXPECT_EQ(2, lookup_key.GetDepth());
+ EXPECT_EQ("data/111/222/333", lookup_key.ToKeyString(kMaxDepth));
+}
+
+TEST(LookupKeyTest, AddressDepth4) {
+ AddressData address;
+ address.region_code = "111";
+ address.administrative_area = "222";
+ address.locality = "333";
+ address.dependent_locality = "444";
+ LookupKey lookup_key;
+ lookup_key.FromAddress(address);
+ EXPECT_EQ(3, lookup_key.GetDepth());
+ EXPECT_EQ("data/111/222/333/444", lookup_key.ToKeyString(kMaxDepth));
+}
+
+TEST(LookupKeyTest, AddressDepthNonContiguous) {
+ AddressData address;
+ address.region_code = "111";
+ address.administrative_area = "222";
+ // No LOCALITY specified.
+ address.dependent_locality = "444";
+ LookupKey lookup_key;
+ lookup_key.FromAddress(address);
+ EXPECT_EQ(1, lookup_key.GetDepth());
+ EXPECT_EQ("data/111/222", lookup_key.ToKeyString(kMaxDepth));
+}
+
+TEST(LookupKeyTest, RequestDepth) {
+ AddressData address;
+ address.region_code = "111";
+ address.administrative_area = "222";
+ address.locality = "333";
+ address.dependent_locality = "444";
+ LookupKey lookup_key;
+ lookup_key.FromAddress(address);
+ EXPECT_EQ("data/111", lookup_key.ToKeyString(0));
+ EXPECT_EQ("data/111/222", lookup_key.ToKeyString(1));
+ EXPECT_EQ("data/111/222/333", lookup_key.ToKeyString(2));
+ EXPECT_EQ("data/111/222/333/444", lookup_key.ToKeyString(3));
+}
+
+TEST(LookupKeyTest, GetRegionCode) {
+ AddressData address;
+ address.region_code = "rrr";
+ LookupKey lookup_key;
+ lookup_key.FromAddress(address);
+ EXPECT_EQ(address.region_code, lookup_key.GetRegionCode());
+}
+
+TEST(LookupKeyTest, FromAddressClearsExistingNodes) {
+ AddressData address;
+ address.region_code = "111";
+ address.administrative_area = "222";
+ LookupKey lookup_key;
+ lookup_key.FromAddress(address);
+ EXPECT_EQ("data/111/222", lookup_key.ToKeyString(kMaxDepth));
+ address.administrative_area.clear();
+ lookup_key.FromAddress(address);
+ EXPECT_EQ("data/111", lookup_key.ToKeyString(kMaxDepth));
+}
+
+} // namespace