OLD | NEW |
(Empty) | |
| 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 |
| 29 namespace { |
| 30 |
| 31 using i18n::addressinput::AddressField; |
| 32 using i18n::addressinput::ADMIN_AREA; |
| 33 using i18n::addressinput::LOCALITY; |
| 34 using i18n::addressinput::NEWLINE; |
| 35 using i18n::addressinput::ORGANIZATION; |
| 36 using i18n::addressinput::POSTAL_CODE; |
| 37 using i18n::addressinput::RECIPIENT; |
| 38 using i18n::addressinput::RegionDataConstants; |
| 39 using i18n::addressinput::Rule; |
| 40 using i18n::addressinput::STREET_ADDRESS; |
| 41 |
| 42 TEST(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 "}")); |
| 49 |
| 50 Rule copy; |
| 51 EXPECT_NE(rule.GetFormat(), copy.GetFormat()); |
| 52 EXPECT_NE(rule.GetAdminAreaNameMessageId(), |
| 53 copy.GetAdminAreaNameMessageId()); |
| 54 EXPECT_NE(rule.GetPostalCodeNameMessageId(), |
| 55 copy.GetPostalCodeNameMessageId()); |
| 56 |
| 57 copy.CopyFrom(rule); |
| 58 EXPECT_EQ(rule.GetFormat(), copy.GetFormat()); |
| 59 EXPECT_EQ(rule.GetAdminAreaNameMessageId(), |
| 60 copy.GetAdminAreaNameMessageId()); |
| 61 EXPECT_EQ(rule.GetPostalCodeNameMessageId(), |
| 62 copy.GetPostalCodeNameMessageId()); |
| 63 } |
| 64 |
| 65 TEST(RuleTest, ParseOverwritesRule) { |
| 66 Rule rule; |
| 67 ASSERT_TRUE(rule.ParseSerializedRule("{" |
| 68 "\"fmt\":\"%S%Z\"," |
| 69 "\"state_name_type\":\"area\"," |
| 70 "\"zip_name_type\":\"postal\"" |
| 71 "}")); |
| 72 EXPECT_FALSE(rule.GetFormat().empty()); |
| 73 EXPECT_EQ(IDS_LIBADDRESSINPUT_I18N_AREA, |
| 74 rule.GetAdminAreaNameMessageId()); |
| 75 EXPECT_EQ(IDS_LIBADDRESSINPUT_I18N_POSTAL_CODE_LABEL, |
| 76 rule.GetPostalCodeNameMessageId()); |
| 77 |
| 78 ASSERT_TRUE(rule.ParseSerializedRule("{" |
| 79 "\"fmt\":\"\"," |
| 80 "\"state_name_type\":\"do_si\"," |
| 81 "\"zip_name_type\":\"zip\"" |
| 82 "}")); |
| 83 EXPECT_TRUE(rule.GetFormat().empty()); |
| 84 EXPECT_EQ(IDS_LIBADDRESSINPUT_I18N_DO_SI, |
| 85 rule.GetAdminAreaNameMessageId()); |
| 86 EXPECT_EQ(IDS_LIBADDRESSINPUT_I18N_ZIP_CODE_LABEL, |
| 87 rule.GetPostalCodeNameMessageId()); |
| 88 } |
| 89 |
| 90 TEST(RuleTest, ParsesFormatCorrectly) { |
| 91 Rule rule; |
| 92 ASSERT_TRUE(rule.ParseSerializedRule("{\"fmt\":\"%S\"}")); |
| 93 ASSERT_EQ(1, rule.GetFormat().size()); |
| 94 EXPECT_EQ(ADMIN_AREA, rule.GetFormat()[0]); |
| 95 } |
| 96 |
| 97 TEST(RuleTest, EmptyStringIsNotValid) { |
| 98 Rule rule; |
| 99 EXPECT_FALSE(rule.ParseSerializedRule(std::string())); |
| 100 } |
| 101 |
| 102 TEST(RuleTest, EmptyDictionaryIsValid) { |
| 103 Rule rule; |
| 104 EXPECT_TRUE(rule.ParseSerializedRule("{}")); |
| 105 } |
| 106 |
| 107 // Tests for parsing the postal code name. |
| 108 class PostalCodeNameParseTest |
| 109 : public testing::TestWithParam<std::pair<std::string, int> > { |
| 110 protected: |
| 111 Rule rule_; |
| 112 }; |
| 113 |
| 114 // Verifies that a postal code name is parsed correctly. |
| 115 TEST_P(PostalCodeNameParseTest, ParsedCorrectly) { |
| 116 ASSERT_TRUE(rule_.ParseSerializedRule(GetParam().first)); |
| 117 EXPECT_EQ(GetParam().second, rule_.GetPostalCodeNameMessageId()); |
| 118 } |
| 119 |
| 120 // Test parsing all postal code names. |
| 121 INSTANTIATE_TEST_CASE_P( |
| 122 AllPostalCodeNames, PostalCodeNameParseTest, |
| 123 testing::Values( |
| 124 std::make_pair("{\"zip_name_type\":\"postal\"}", |
| 125 IDS_LIBADDRESSINPUT_I18N_POSTAL_CODE_LABEL), |
| 126 std::make_pair("{\"zip_name_type\":\"zip\"}", |
| 127 IDS_LIBADDRESSINPUT_I18N_ZIP_CODE_LABEL))); |
| 128 |
| 129 // Tests for parsing the administrative area name. |
| 130 class AdminAreaNameParseTest |
| 131 : public testing::TestWithParam<std::pair<std::string, int> > { |
| 132 protected: |
| 133 Rule rule_; |
| 134 }; |
| 135 |
| 136 // Verifies that an administrative area name is parsed correctly. |
| 137 TEST_P(AdminAreaNameParseTest, ParsedCorrectly) { |
| 138 ASSERT_TRUE(rule_.ParseSerializedRule(GetParam().first)); |
| 139 EXPECT_EQ(GetParam().second, rule_.GetAdminAreaNameMessageId()); |
| 140 } |
| 141 |
| 142 // Test parsing all administrative area names. |
| 143 INSTANTIATE_TEST_CASE_P( |
| 144 AllAdminAreaNames, AdminAreaNameParseTest, |
| 145 testing::Values( |
| 146 std::make_pair("{\"state_name_type\":\"area\"}", |
| 147 IDS_LIBADDRESSINPUT_I18N_AREA), |
| 148 std::make_pair("{\"state_name_type\":\"county\"}", |
| 149 IDS_LIBADDRESSINPUT_I18N_COUNTY_LABEL), |
| 150 std::make_pair("{\"state_name_type\":\"department\"}", |
| 151 IDS_LIBADDRESSINPUT_I18N_DEPARTMENT), |
| 152 std::make_pair("{\"state_name_type\":\"district\"}", |
| 153 IDS_LIBADDRESSINPUT_I18N_DEPENDENT_LOCALITY_LABEL), |
| 154 std::make_pair("{\"state_name_type\":\"do_si\"}", |
| 155 IDS_LIBADDRESSINPUT_I18N_DO_SI), |
| 156 std::make_pair("{\"state_name_type\":\"emirate\"}", |
| 157 IDS_LIBADDRESSINPUT_I18N_EMIRATE), |
| 158 std::make_pair("{\"state_name_type\":\"island\"}", |
| 159 IDS_LIBADDRESSINPUT_I18N_ISLAND), |
| 160 std::make_pair("{\"state_name_type\":\"parish\"}", |
| 161 IDS_LIBADDRESSINPUT_I18N_PARISH), |
| 162 std::make_pair("{\"state_name_type\":\"prefecture\"}", |
| 163 IDS_LIBADDRESSINPUT_I18N_PREFECTURE), |
| 164 std::make_pair("{\"state_name_type\":\"province\"}", |
| 165 IDS_LIBADDRESSINPUT_I18N_PROVINCE), |
| 166 std::make_pair("{\"state_name_type\":\"state\"}", |
| 167 IDS_LIBADDRESSINPUT_I18N_STATE_LABEL))); |
| 168 |
| 169 // Tests for rule parsing. |
| 170 class RuleParseTest : public testing::TestWithParam<std::string> { |
| 171 protected: |
| 172 Rule rule_; |
| 173 }; |
| 174 |
| 175 // Verifies that a region data can be parsed successfully. |
| 176 TEST_P(RuleParseTest, RegionDataParsedSuccessfully) { |
| 177 EXPECT_TRUE(rule_.ParseSerializedRule( |
| 178 RegionDataConstants::GetRegionData(GetParam()))); |
| 179 } |
| 180 |
| 181 // Test parsing all region data. |
| 182 INSTANTIATE_TEST_CASE_P( |
| 183 AllRulesTest, RuleParseTest, |
| 184 testing::ValuesIn(RegionDataConstants::GetRegionCodes())); |
| 185 |
| 186 } // namespace |
OLD | NEW |