1// Copyright (C) 2013 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 "rule.h" 16 17#include <libaddressinput/address_field.h> 18 19#include <string> 20#include <utility> 21#include <vector> 22 23#include <gtest/gtest.h> 24 25#include "address_field_util.h" 26#include "messages.h" 27#include "region_data_constants.h" 28 29namespace { 30 31using i18n::addressinput::AddressField; 32using i18n::addressinput::ADMIN_AREA; 33using i18n::addressinput::LOCALITY; 34using i18n::addressinput::NEWLINE; 35using i18n::addressinput::ORGANIZATION; 36using i18n::addressinput::POSTAL_CODE; 37using i18n::addressinput::RECIPIENT; 38using i18n::addressinput::RegionDataConstants; 39using i18n::addressinput::Rule; 40using i18n::addressinput::STREET_ADDRESS; 41 42TEST(RuleTest, CopyOverwritesRule) { 43 Rule rule; 44 ASSERT_TRUE(rule.ParseSerializedRule("{" 45 "\"fmt\":\"%S%Z\"," 46 "\"state_name_type\":\"area\"," 47 "\"zip_name_type\":\"postal\"," 48 "\"sub_keys\":\"CA~NY~TX\"," 49 "\"lang\":\"en\"," 50 "\"languages\":\"en~fr\"" 51 "}")); 52 53 Rule copy; 54 EXPECT_NE(rule.GetFormat(), copy.GetFormat()); 55 EXPECT_NE(rule.GetSubKeys(), copy.GetSubKeys()); 56 EXPECT_NE(rule.GetLanguages(), copy.GetLanguages()); 57 EXPECT_NE(rule.GetLanguage(), copy.GetLanguage()); 58 EXPECT_NE(rule.GetAdminAreaNameMessageId(), 59 copy.GetAdminAreaNameMessageId()); 60 EXPECT_NE(rule.GetPostalCodeNameMessageId(), 61 copy.GetPostalCodeNameMessageId()); 62 63 copy.CopyFrom(rule); 64 EXPECT_EQ(rule.GetFormat(), copy.GetFormat()); 65 EXPECT_EQ(rule.GetSubKeys(), copy.GetSubKeys()); 66 EXPECT_EQ(rule.GetLanguages(), copy.GetLanguages()); 67 EXPECT_EQ(rule.GetLanguage(), copy.GetLanguage()); 68 EXPECT_EQ(rule.GetAdminAreaNameMessageId(), 69 copy.GetAdminAreaNameMessageId()); 70 EXPECT_EQ(rule.GetPostalCodeNameMessageId(), 71 copy.GetPostalCodeNameMessageId()); 72} 73 74TEST(RuleTest, ParseOverwritesRule) { 75 Rule rule; 76 ASSERT_TRUE(rule.ParseSerializedRule("{" 77 "\"fmt\":\"%S%Z\"," 78 "\"state_name_type\":\"area\"," 79 "\"zip_name_type\":\"postal\"," 80 "\"sub_keys\":\"CA~NY~TX\"," 81 "\"lang\":\"en\"," 82 "\"languages\":\"en~fr\"" 83 "}")); 84 EXPECT_FALSE(rule.GetFormat().empty()); 85 EXPECT_FALSE(rule.GetSubKeys().empty()); 86 EXPECT_FALSE(rule.GetLanguages().empty()); 87 EXPECT_FALSE(rule.GetLanguage().empty()); 88 EXPECT_EQ(IDS_LIBADDRESSINPUT_I18N_AREA, 89 rule.GetAdminAreaNameMessageId()); 90 EXPECT_EQ(IDS_LIBADDRESSINPUT_I18N_POSTAL_CODE_LABEL, 91 rule.GetPostalCodeNameMessageId()); 92 93 ASSERT_TRUE(rule.ParseSerializedRule("{" 94 "\"fmt\":\"\"," 95 "\"state_name_type\":\"do_si\"," 96 "\"zip_name_type\":\"zip\"," 97 "\"sub_keys\":\"\"," 98 "\"lang\":\"\"," 99 "\"languages\":\"\"" 100 "}")); 101 EXPECT_TRUE(rule.GetFormat().empty()); 102 EXPECT_TRUE(rule.GetSubKeys().empty()); 103 EXPECT_TRUE(rule.GetLanguages().empty()); 104 EXPECT_TRUE(rule.GetLanguage().empty()); 105 EXPECT_EQ(IDS_LIBADDRESSINPUT_I18N_DO_SI, 106 rule.GetAdminAreaNameMessageId()); 107 EXPECT_EQ(IDS_LIBADDRESSINPUT_I18N_ZIP_CODE_LABEL, 108 rule.GetPostalCodeNameMessageId()); 109} 110 111TEST(RuleTest, ParsesFormatCorrectly) { 112 Rule rule; 113 ASSERT_TRUE(rule.ParseSerializedRule("{\"fmt\":\"%S\"}")); 114 ASSERT_EQ(1, rule.GetFormat().size()); 115 EXPECT_EQ(ADMIN_AREA, rule.GetFormat()[0]); 116} 117 118TEST(RuleTest, ParsesSubKeysCorrectly) { 119 Rule rule; 120 ASSERT_TRUE(rule.ParseSerializedRule("{\"sub_keys\":\"CA~NY~TX\"}")); 121 std::vector<std::string> expected; 122 expected.push_back("CA"); 123 expected.push_back("NY"); 124 expected.push_back("TX"); 125 EXPECT_EQ(expected, rule.GetSubKeys()); 126} 127 128TEST(RuleTest, ParsesLanguageCorrectly) { 129 Rule rule; 130 ASSERT_TRUE(rule.ParseSerializedRule("{\"lang\":\"en\"}")); 131 EXPECT_EQ("en", rule.GetLanguage()); 132} 133 134TEST(RuleTest, ParsesLanguagesCorrectly) { 135 Rule rule; 136 ASSERT_TRUE(rule.ParseSerializedRule("{\"languages\":\"de~fr~it\"}")); 137 std::vector<std::string> expected; 138 expected.push_back("de"); 139 expected.push_back("fr"); 140 expected.push_back("it"); 141 EXPECT_EQ(expected, rule.GetLanguages()); 142} 143 144TEST(RuleTest, EmptyStringIsNotValid) { 145 Rule rule; 146 EXPECT_FALSE(rule.ParseSerializedRule(std::string())); 147} 148 149TEST(RuleTest, EmptyDictionaryIsValid) { 150 Rule rule; 151 EXPECT_TRUE(rule.ParseSerializedRule("{}")); 152} 153 154// Tests for parsing the postal code name. 155class PostalCodeNameParseTest 156 : public testing::TestWithParam<std::pair<std::string, int> > { 157 protected: 158 Rule rule_; 159}; 160 161// Verifies that a postal code name is parsed correctly. 162TEST_P(PostalCodeNameParseTest, ParsedCorrectly) { 163 ASSERT_TRUE(rule_.ParseSerializedRule(GetParam().first)); 164 EXPECT_EQ(GetParam().second, rule_.GetPostalCodeNameMessageId()); 165} 166 167// Test parsing all postal code names. 168INSTANTIATE_TEST_CASE_P( 169 AllPostalCodeNames, PostalCodeNameParseTest, 170 testing::Values( 171 std::make_pair("{\"zip_name_type\":\"postal\"}", 172 IDS_LIBADDRESSINPUT_I18N_POSTAL_CODE_LABEL), 173 std::make_pair("{\"zip_name_type\":\"zip\"}", 174 IDS_LIBADDRESSINPUT_I18N_ZIP_CODE_LABEL))); 175 176// Tests for parsing the administrative area name. 177class AdminAreaNameParseTest 178 : public testing::TestWithParam<std::pair<std::string, int> > { 179 protected: 180 Rule rule_; 181}; 182 183// Verifies that an administrative area name is parsed correctly. 184TEST_P(AdminAreaNameParseTest, ParsedCorrectly) { 185 ASSERT_TRUE(rule_.ParseSerializedRule(GetParam().first)); 186 EXPECT_EQ(GetParam().second, rule_.GetAdminAreaNameMessageId()); 187} 188 189// Test parsing all administrative area names. 190INSTANTIATE_TEST_CASE_P( 191 AllAdminAreaNames, AdminAreaNameParseTest, 192 testing::Values( 193 std::make_pair("{\"state_name_type\":\"area\"}", 194 IDS_LIBADDRESSINPUT_I18N_AREA), 195 std::make_pair("{\"state_name_type\":\"county\"}", 196 IDS_LIBADDRESSINPUT_I18N_COUNTY_LABEL), 197 std::make_pair("{\"state_name_type\":\"department\"}", 198 IDS_LIBADDRESSINPUT_I18N_DEPARTMENT), 199 std::make_pair("{\"state_name_type\":\"district\"}", 200 IDS_LIBADDRESSINPUT_I18N_DEPENDENT_LOCALITY_LABEL), 201 std::make_pair("{\"state_name_type\":\"do_si\"}", 202 IDS_LIBADDRESSINPUT_I18N_DO_SI), 203 std::make_pair("{\"state_name_type\":\"emirate\"}", 204 IDS_LIBADDRESSINPUT_I18N_EMIRATE), 205 std::make_pair("{\"state_name_type\":\"island\"}", 206 IDS_LIBADDRESSINPUT_I18N_ISLAND), 207 std::make_pair("{\"state_name_type\":\"parish\"}", 208 IDS_LIBADDRESSINPUT_I18N_PARISH), 209 std::make_pair("{\"state_name_type\":\"prefecture\"}", 210 IDS_LIBADDRESSINPUT_I18N_PREFECTURE), 211 std::make_pair("{\"state_name_type\":\"province\"}", 212 IDS_LIBADDRESSINPUT_I18N_PROVINCE), 213 std::make_pair("{\"state_name_type\":\"state\"}", 214 IDS_LIBADDRESSINPUT_I18N_STATE_LABEL))); 215 216// Tests for rule parsing. 217class RuleParseTest : public testing::TestWithParam<std::string> { 218 protected: 219 Rule rule_; 220}; 221 222// Verifies that a region data can be parsed successfully. 223TEST_P(RuleParseTest, RegionDataParsedSuccessfully) { 224 EXPECT_TRUE(rule_.ParseSerializedRule( 225 RegionDataConstants::GetRegionData(GetParam()))); 226} 227 228// Test parsing all region data. 229INSTANTIATE_TEST_CASE_P( 230 AllRulesTest, RuleParseTest, 231 testing::ValuesIn(RegionDataConstants::GetRegionCodes())); 232 233} // namespace 234