| OLD | NEW |
| 1 // Copyright (C) 2013 Google Inc. | 1 // Copyright (C) 2013 Google Inc. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 #include "rule.h" | 15 #include "rule.h" |
| 16 | 16 |
| 17 #include <libaddressinput/address_field.h> | 17 #include <libaddressinput/address_field.h> |
| 18 | 18 |
| 19 #include <string> | 19 #include <string> |
| 20 #include <utility> | 20 #include <utility> |
| 21 #include <vector> | 21 #include <vector> |
| 22 | 22 |
| 23 #include <gtest/gtest.h> | 23 #include <gtest/gtest.h> |
| 24 | 24 |
| 25 #include "address_field_util.h" | |
| 26 #include "messages.h" | 25 #include "messages.h" |
| 27 #include "region_data_constants.h" | 26 #include "region_data_constants.h" |
| 28 | 27 |
| 29 namespace { | 28 namespace { |
| 30 | 29 |
| 31 using i18n::addressinput::AddressField; | 30 using i18n::addressinput::AddressField; |
| 32 using i18n::addressinput::ADMIN_AREA; | 31 using i18n::addressinput::ADMIN_AREA; |
| 32 using i18n::addressinput::COUNTRY; |
| 33 using i18n::addressinput::LOCALITY; | 33 using i18n::addressinput::LOCALITY; |
| 34 using i18n::addressinput::NEWLINE; | |
| 35 using i18n::addressinput::ORGANIZATION; | 34 using i18n::addressinput::ORGANIZATION; |
| 36 using i18n::addressinput::POSTAL_CODE; | 35 using i18n::addressinput::POSTAL_CODE; |
| 37 using i18n::addressinput::RECIPIENT; | 36 using i18n::addressinput::RECIPIENT; |
| 38 using i18n::addressinput::RegionDataConstants; | 37 using i18n::addressinput::RegionDataConstants; |
| 39 using i18n::addressinput::Rule; | 38 using i18n::addressinput::Rule; |
| 40 using i18n::addressinput::STREET_ADDRESS; | 39 using i18n::addressinput::STREET_ADDRESS; |
| 41 | 40 |
| 42 TEST(RuleTest, CopyOverwritesRule) { | 41 TEST(RuleTest, CopyOverwritesRule) { |
| 43 Rule rule; | 42 Rule rule; |
| 44 ASSERT_TRUE(rule.ParseSerializedRule("{" | 43 ASSERT_TRUE(rule.ParseSerializedRule( |
| 45 "\"fmt\":\"%S%Z\"," | 44 "{" |
| 46 "\"state_name_type\":\"area\"," | 45 "\"fmt\":\"%S%Z\"," |
| 47 "\"zip_name_type\":\"postal\"," | 46 "\"require\":\"SZ\"," |
| 48 "\"sub_keys\":\"CA~NY~TX\"," | 47 "\"state_name_type\":\"area\"," |
| 49 "\"lang\":\"en\"," | 48 "\"zip_name_type\":\"postal\"," |
| 50 "\"languages\":\"en~fr\"" | 49 "\"sub_keys\":\"CA~NY~TX\"," |
| 51 "}")); | 50 "\"lang\":\"en\"," |
| 51 "\"languages\":\"en~fr\"," |
| 52 "\"zip\":\"\\\\d{5}([ \\\\-]\\\\d{4})?\"" |
| 53 "}")); |
| 52 | 54 |
| 53 Rule copy; | 55 Rule copy; |
| 54 EXPECT_NE(rule.GetFormat(), copy.GetFormat()); | 56 EXPECT_NE(rule.GetFormat(), copy.GetFormat()); |
| 57 EXPECT_NE(rule.GetRequired(), copy.GetRequired()); |
| 55 EXPECT_NE(rule.GetSubKeys(), copy.GetSubKeys()); | 58 EXPECT_NE(rule.GetSubKeys(), copy.GetSubKeys()); |
| 56 EXPECT_NE(rule.GetLanguages(), copy.GetLanguages()); | 59 EXPECT_NE(rule.GetLanguages(), copy.GetLanguages()); |
| 57 EXPECT_NE(rule.GetLanguage(), copy.GetLanguage()); | 60 EXPECT_NE(rule.GetLanguage(), copy.GetLanguage()); |
| 61 EXPECT_NE(rule.GetPostalCodeFormat(), copy.GetPostalCodeFormat()); |
| 58 EXPECT_NE(rule.GetAdminAreaNameMessageId(), | 62 EXPECT_NE(rule.GetAdminAreaNameMessageId(), |
| 59 copy.GetAdminAreaNameMessageId()); | 63 copy.GetAdminAreaNameMessageId()); |
| 60 EXPECT_NE(rule.GetPostalCodeNameMessageId(), | 64 EXPECT_NE(rule.GetPostalCodeNameMessageId(), |
| 61 copy.GetPostalCodeNameMessageId()); | 65 copy.GetPostalCodeNameMessageId()); |
| 62 | 66 |
| 63 copy.CopyFrom(rule); | 67 copy.CopyFrom(rule); |
| 64 EXPECT_EQ(rule.GetFormat(), copy.GetFormat()); | 68 EXPECT_EQ(rule.GetFormat(), copy.GetFormat()); |
| 69 EXPECT_EQ(rule.GetRequired(), copy.GetRequired()); |
| 65 EXPECT_EQ(rule.GetSubKeys(), copy.GetSubKeys()); | 70 EXPECT_EQ(rule.GetSubKeys(), copy.GetSubKeys()); |
| 66 EXPECT_EQ(rule.GetLanguages(), copy.GetLanguages()); | 71 EXPECT_EQ(rule.GetLanguages(), copy.GetLanguages()); |
| 67 EXPECT_EQ(rule.GetLanguage(), copy.GetLanguage()); | 72 EXPECT_EQ(rule.GetLanguage(), copy.GetLanguage()); |
| 73 EXPECT_EQ(rule.GetPostalCodeFormat(), copy.GetPostalCodeFormat()); |
| 68 EXPECT_EQ(rule.GetAdminAreaNameMessageId(), | 74 EXPECT_EQ(rule.GetAdminAreaNameMessageId(), |
| 69 copy.GetAdminAreaNameMessageId()); | 75 copy.GetAdminAreaNameMessageId()); |
| 70 EXPECT_EQ(rule.GetPostalCodeNameMessageId(), | 76 EXPECT_EQ(rule.GetPostalCodeNameMessageId(), |
| 71 copy.GetPostalCodeNameMessageId()); | 77 copy.GetPostalCodeNameMessageId()); |
| 72 } | 78 } |
| 73 | 79 |
| 74 TEST(RuleTest, ParseOverwritesRule) { | 80 TEST(RuleTest, ParseOverwritesRule) { |
| 75 Rule rule; | 81 Rule rule; |
| 76 ASSERT_TRUE(rule.ParseSerializedRule("{" | 82 ASSERT_TRUE(rule.ParseSerializedRule( |
| 77 "\"fmt\":\"%S%Z\"," | 83 "{" |
| 78 "\"state_name_type\":\"area\"," | 84 "\"fmt\":\"%S%Z\"," |
| 79 "\"zip_name_type\":\"postal\"," | 85 "\"require\":\"SZ\"," |
| 80 "\"sub_keys\":\"CA~NY~TX\"," | 86 "\"state_name_type\":\"area\"," |
| 81 "\"lang\":\"en\"," | 87 "\"zip_name_type\":\"postal\"," |
| 82 "\"languages\":\"en~fr\"" | 88 "\"sub_keys\":\"CA~NY~TX\"," |
| 83 "}")); | 89 "\"lang\":\"en\"," |
| 90 "\"languages\":\"en~fr\"," |
| 91 "\"zip\":\"\\\\d{5}([ \\\\-]\\\\d{4})?\"" |
| 92 "}")); |
| 84 EXPECT_FALSE(rule.GetFormat().empty()); | 93 EXPECT_FALSE(rule.GetFormat().empty()); |
| 94 EXPECT_FALSE(rule.GetRequired().empty()); |
| 85 EXPECT_FALSE(rule.GetSubKeys().empty()); | 95 EXPECT_FALSE(rule.GetSubKeys().empty()); |
| 86 EXPECT_FALSE(rule.GetLanguages().empty()); | 96 EXPECT_FALSE(rule.GetLanguages().empty()); |
| 87 EXPECT_FALSE(rule.GetLanguage().empty()); | 97 EXPECT_FALSE(rule.GetLanguage().empty()); |
| 98 EXPECT_FALSE(rule.GetPostalCodeFormat().empty()); |
| 88 EXPECT_EQ(IDS_LIBADDRESSINPUT_I18N_AREA, | 99 EXPECT_EQ(IDS_LIBADDRESSINPUT_I18N_AREA, |
| 89 rule.GetAdminAreaNameMessageId()); | 100 rule.GetAdminAreaNameMessageId()); |
| 90 EXPECT_EQ(IDS_LIBADDRESSINPUT_I18N_POSTAL_CODE_LABEL, | 101 EXPECT_EQ(IDS_LIBADDRESSINPUT_I18N_POSTAL_CODE_LABEL, |
| 91 rule.GetPostalCodeNameMessageId()); | 102 rule.GetPostalCodeNameMessageId()); |
| 92 | 103 |
| 93 ASSERT_TRUE(rule.ParseSerializedRule("{" | 104 ASSERT_TRUE(rule.ParseSerializedRule( |
| 94 "\"fmt\":\"\"," | 105 "{" |
| 95 "\"state_name_type\":\"do_si\"," | 106 "\"fmt\":\"\"," |
| 96 "\"zip_name_type\":\"zip\"," | 107 "\"require\":\"\"," |
| 97 "\"sub_keys\":\"\"," | 108 "\"state_name_type\":\"do_si\"," |
| 98 "\"lang\":\"\"," | 109 "\"zip_name_type\":\"zip\"," |
| 99 "\"languages\":\"\"" | 110 "\"sub_keys\":\"\"," |
| 100 "}")); | 111 "\"lang\":\"\"," |
| 112 "\"languages\":\"\"," |
| 113 "\"zip\":\"\"" |
| 114 "}")); |
| 101 EXPECT_TRUE(rule.GetFormat().empty()); | 115 EXPECT_TRUE(rule.GetFormat().empty()); |
| 116 EXPECT_TRUE(rule.GetRequired().empty()); |
| 102 EXPECT_TRUE(rule.GetSubKeys().empty()); | 117 EXPECT_TRUE(rule.GetSubKeys().empty()); |
| 103 EXPECT_TRUE(rule.GetLanguages().empty()); | 118 EXPECT_TRUE(rule.GetLanguages().empty()); |
| 104 EXPECT_TRUE(rule.GetLanguage().empty()); | 119 EXPECT_TRUE(rule.GetLanguage().empty()); |
| 120 EXPECT_TRUE(rule.GetPostalCodeFormat().empty()); |
| 105 EXPECT_EQ(IDS_LIBADDRESSINPUT_I18N_DO_SI, | 121 EXPECT_EQ(IDS_LIBADDRESSINPUT_I18N_DO_SI, |
| 106 rule.GetAdminAreaNameMessageId()); | 122 rule.GetAdminAreaNameMessageId()); |
| 107 EXPECT_EQ(IDS_LIBADDRESSINPUT_I18N_ZIP_CODE_LABEL, | 123 EXPECT_EQ(IDS_LIBADDRESSINPUT_I18N_ZIP_CODE_LABEL, |
| 108 rule.GetPostalCodeNameMessageId()); | 124 rule.GetPostalCodeNameMessageId()); |
| 109 } | 125 } |
| 110 | 126 |
| 111 TEST(RuleTest, ParsesFormatCorrectly) { | 127 TEST(RuleTest, ParseFormatWithNewLines) { |
| 112 Rule rule; | 128 Rule rule; |
| 113 ASSERT_TRUE(rule.ParseSerializedRule("{\"fmt\":\"%S\"}")); | 129 ASSERT_TRUE( |
| 114 ASSERT_EQ(1, rule.GetFormat().size()); | 130 rule.ParseSerializedRule("{\"fmt\":\"%O%n%N%n%A%nAX-%Z %C%nĂ…LAND\"}")); |
| 115 EXPECT_EQ(ADMIN_AREA, rule.GetFormat()[0]); | 131 std::vector<std::vector<AddressField> > expected_format; |
| 132 expected_format.push_back(std::vector<AddressField>(1, ORGANIZATION)); |
| 133 expected_format.push_back(std::vector<AddressField>(1, RECIPIENT)); |
| 134 expected_format.push_back(std::vector<AddressField>(1, STREET_ADDRESS)); |
| 135 expected_format.push_back(std::vector<AddressField>(1, POSTAL_CODE)); |
| 136 expected_format.back().push_back(LOCALITY); |
| 137 EXPECT_EQ(expected_format, rule.GetFormat()); |
| 138 } |
| 139 |
| 140 TEST(RuleTest, DoubleTokenPrefixIsIgnoredInFormat) { |
| 141 Rule rule; |
| 142 ASSERT_TRUE(rule.ParseSerializedRule("{\"fmt\":\"%%R\"}")); |
| 143 ASSERT_FALSE(rule.GetFormat().empty()); |
| 144 ASSERT_FALSE(rule.GetFormat().front().empty()); |
| 145 EXPECT_EQ(COUNTRY, rule.GetFormat().front().front()); |
| 146 } |
| 147 |
| 148 TEST(RuleTest, FormatTokenRequiresPrefix) { |
| 149 Rule rule; |
| 150 ASSERT_TRUE(rule.ParseSerializedRule("{\"fmt\":\"R\"}")); |
| 151 EXPECT_TRUE(rule.GetFormat().empty()); |
| 152 } |
| 153 |
| 154 TEST(RuleTest, ParseDuplicateFormat) { |
| 155 Rule rule; |
| 156 ASSERT_TRUE(rule.ParseSerializedRule("{\"fmt\":\"%R%R\"}")); |
| 157 ASSERT_FALSE(rule.GetFormat().empty()); |
| 158 ASSERT_EQ(std::vector<AddressField>(2, COUNTRY), rule.GetFormat().front()); |
| 159 } |
| 160 |
| 161 TEST(RuleTest, ParseInvalidFormatFields) { |
| 162 Rule rule; |
| 163 ASSERT_TRUE(rule.ParseSerializedRule("{\"fmt\":\"%K%L\"}")); |
| 164 EXPECT_TRUE(rule.GetFormat().empty()); |
| 165 } |
| 166 |
| 167 TEST(RuleTest, PrefixWithoutTokenIsIgnoredInFormat) { |
| 168 Rule rule; |
| 169 ASSERT_TRUE(rule.ParseSerializedRule("{\"fmt\":\"%\"}")); |
| 170 EXPECT_TRUE(rule.GetFormat().empty()); |
| 171 } |
| 172 |
| 173 TEST(RuleTest, EmptyStringFormat) { |
| 174 Rule rule; |
| 175 ASSERT_TRUE(rule.ParseSerializedRule("{\"fmt\":\"\"}")); |
| 176 EXPECT_TRUE(rule.GetFormat().empty()); |
| 177 } |
| 178 |
| 179 TEST(RuleTest, ParseRequiredFields) { |
| 180 Rule rule; |
| 181 ASSERT_TRUE(rule.ParseSerializedRule("{\"require\":\"ONAZC\"}")); |
| 182 std::vector<AddressField> expected; |
| 183 expected.push_back(ORGANIZATION); |
| 184 expected.push_back(RECIPIENT); |
| 185 expected.push_back(STREET_ADDRESS); |
| 186 expected.push_back(POSTAL_CODE); |
| 187 expected.push_back(LOCALITY); |
| 188 EXPECT_EQ(expected, rule.GetRequired()); |
| 189 } |
| 190 |
| 191 TEST(RuleTest, ParseEmptyStringRequiredFields) { |
| 192 Rule rule; |
| 193 ASSERT_TRUE(rule.ParseSerializedRule("{\"require\":\"\"}")); |
| 194 EXPECT_TRUE(rule.GetRequired().empty()); |
| 195 } |
| 196 |
| 197 TEST(RuleTest, ParseInvalidRequiredFields) { |
| 198 Rule rule; |
| 199 ASSERT_TRUE(rule.ParseSerializedRule("{\"require\":\"garbage\"}")); |
| 200 EXPECT_TRUE(rule.GetRequired().empty()); |
| 201 } |
| 202 |
| 203 TEST(RuleTest, ParseDuplicateRequiredFields) { |
| 204 Rule rule; |
| 205 ASSERT_TRUE(rule.ParseSerializedRule("{\"require\":\"SSS\"}")); |
| 206 EXPECT_EQ(std::vector<AddressField>(3, ADMIN_AREA), rule.GetRequired()); |
| 116 } | 207 } |
| 117 | 208 |
| 118 TEST(RuleTest, ParsesSubKeysCorrectly) { | 209 TEST(RuleTest, ParsesSubKeysCorrectly) { |
| 119 Rule rule; | 210 Rule rule; |
| 120 ASSERT_TRUE(rule.ParseSerializedRule("{\"sub_keys\":\"CA~NY~TX\"}")); | 211 ASSERT_TRUE(rule.ParseSerializedRule("{\"sub_keys\":\"CA~NY~TX\"}")); |
| 121 std::vector<std::string> expected; | 212 std::vector<std::string> expected; |
| 122 expected.push_back("CA"); | 213 expected.push_back("CA"); |
| 123 expected.push_back("NY"); | 214 expected.push_back("NY"); |
| 124 expected.push_back("TX"); | 215 expected.push_back("TX"); |
| 125 EXPECT_EQ(expected, rule.GetSubKeys()); | 216 EXPECT_EQ(expected, rule.GetSubKeys()); |
| 126 } | 217 } |
| 127 | 218 |
| 128 TEST(RuleTest, ParsesLanguageCorrectly) { | 219 TEST(RuleTest, ParsesLanguageCorrectly) { |
| 129 Rule rule; | 220 Rule rule; |
| 130 ASSERT_TRUE(rule.ParseSerializedRule("{\"lang\":\"en\"}")); | 221 ASSERT_TRUE(rule.ParseSerializedRule("{\"lang\":\"en\"}")); |
| 131 EXPECT_EQ("en", rule.GetLanguage()); | 222 EXPECT_EQ("en", rule.GetLanguage()); |
| 132 } | 223 } |
| 133 | 224 |
| 134 TEST(RuleTest, ParsesLanguagesCorrectly) { | 225 TEST(RuleTest, ParsesLanguagesCorrectly) { |
| 135 Rule rule; | 226 Rule rule; |
| 136 ASSERT_TRUE(rule.ParseSerializedRule("{\"languages\":\"de~fr~it\"}")); | 227 ASSERT_TRUE(rule.ParseSerializedRule("{\"languages\":\"de~fr~it\"}")); |
| 137 std::vector<std::string> expected; | 228 std::vector<std::string> expected; |
| 138 expected.push_back("de"); | 229 expected.push_back("de"); |
| 139 expected.push_back("fr"); | 230 expected.push_back("fr"); |
| 140 expected.push_back("it"); | 231 expected.push_back("it"); |
| 141 EXPECT_EQ(expected, rule.GetLanguages()); | 232 EXPECT_EQ(expected, rule.GetLanguages()); |
| 142 } | 233 } |
| 143 | 234 |
| 235 TEST(RuleTest, ParsesPostalCodeFormatCorrectly) { |
| 236 Rule rule; |
| 237 ASSERT_TRUE(rule.ParseSerializedRule( |
| 238 "{" |
| 239 "\"zip\":\"\\\\d{5}([ \\\\-]\\\\d{4})?\"" |
| 240 "}")); |
| 241 EXPECT_EQ("\\d{5}([ \\-]\\d{4})?", rule.GetPostalCodeFormat()); |
| 242 } |
| 243 |
| 144 TEST(RuleTest, EmptyStringIsNotValid) { | 244 TEST(RuleTest, EmptyStringIsNotValid) { |
| 145 Rule rule; | 245 Rule rule; |
| 146 EXPECT_FALSE(rule.ParseSerializedRule(std::string())); | 246 EXPECT_FALSE(rule.ParseSerializedRule(std::string())); |
| 147 } | 247 } |
| 148 | 248 |
| 149 TEST(RuleTest, EmptyDictionaryIsValid) { | 249 TEST(RuleTest, EmptyDictionaryIsValid) { |
| 150 Rule rule; | 250 Rule rule; |
| 151 EXPECT_TRUE(rule.ParseSerializedRule("{}")); | 251 EXPECT_TRUE(rule.ParseSerializedRule("{}")); |
| 152 } | 252 } |
| 153 | 253 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 EXPECT_TRUE(rule_.ParseSerializedRule( | 324 EXPECT_TRUE(rule_.ParseSerializedRule( |
| 225 RegionDataConstants::GetRegionData(GetParam()))); | 325 RegionDataConstants::GetRegionData(GetParam()))); |
| 226 } | 326 } |
| 227 | 327 |
| 228 // Test parsing all region data. | 328 // Test parsing all region data. |
| 229 INSTANTIATE_TEST_CASE_P( | 329 INSTANTIATE_TEST_CASE_P( |
| 230 AllRulesTest, RuleParseTest, | 330 AllRulesTest, RuleParseTest, |
| 231 testing::ValuesIn(RegionDataConstants::GetRegionCodes())); | 331 testing::ValuesIn(RegionDataConstants::GetRegionCodes())); |
| 232 | 332 |
| 233 } // namespace | 333 } // namespace |
| OLD | NEW |