| 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; | 34 using i18n::addressinput::NEWLINE; |
| 35 using i18n::addressinput::ORGANIZATION; | 35 using i18n::addressinput::ORGANIZATION; |
| 36 using i18n::addressinput::POSTAL_CODE; | 36 using i18n::addressinput::POSTAL_CODE; |
| 37 using i18n::addressinput::RECIPIENT; | 37 using i18n::addressinput::RECIPIENT; |
| 38 using i18n::addressinput::RegionDataConstants; | 38 using i18n::addressinput::RegionDataConstants; |
| 39 using i18n::addressinput::Rule; | 39 using i18n::addressinput::Rule; |
| 40 using i18n::addressinput::STREET_ADDRESS; | 40 using i18n::addressinput::STREET_ADDRESS; |
| 41 | 41 |
| 42 TEST(RuleTest, CopyOverwritesRule) { | 42 TEST(RuleTest, CopyOverwritesRule) { |
| 43 Rule rule; | 43 Rule rule; |
| 44 ASSERT_TRUE(rule.ParseSerializedRule("{" | 44 ASSERT_TRUE(rule.ParseSerializedRule( |
| 45 "\"fmt\":\"%S%Z\"," | 45 "{" |
| 46 "\"state_name_type\":\"area\"," | 46 "\"fmt\":\"%S%Z\"," |
| 47 "\"zip_name_type\":\"postal\"," | 47 "\"require\":\"SZ\"," |
| 48 "\"sub_keys\":\"CA~NY~TX\"," | 48 "\"state_name_type\":\"area\"," |
| 49 "\"lang\":\"en\"," | 49 "\"zip_name_type\":\"postal\"," |
| 50 "\"languages\":\"en~fr\"" | 50 "\"sub_keys\":\"CA~NY~TX\"," |
| 51 "}")); | 51 "\"lang\":\"en\"," |
| 52 "\"languages\":\"en~fr\"," |
| 53 "\"zip\":\"\\\\d{5}([ \\\\-]\\\\d{4})?\"" |
| 54 "}")); |
| 52 | 55 |
| 53 Rule copy; | 56 Rule copy; |
| 54 EXPECT_NE(rule.GetFormat(), copy.GetFormat()); | 57 EXPECT_NE(rule.GetFormat(), copy.GetFormat()); |
| 58 EXPECT_NE(rule.GetRequired(), copy.GetRequired()); |
| 55 EXPECT_NE(rule.GetSubKeys(), copy.GetSubKeys()); | 59 EXPECT_NE(rule.GetSubKeys(), copy.GetSubKeys()); |
| 56 EXPECT_NE(rule.GetLanguages(), copy.GetLanguages()); | 60 EXPECT_NE(rule.GetLanguages(), copy.GetLanguages()); |
| 57 EXPECT_NE(rule.GetLanguage(), copy.GetLanguage()); | 61 EXPECT_NE(rule.GetLanguage(), copy.GetLanguage()); |
| 62 EXPECT_NE(rule.GetPostalCodeFormat(), copy.GetPostalCodeFormat()); |
| 58 EXPECT_NE(rule.GetAdminAreaNameMessageId(), | 63 EXPECT_NE(rule.GetAdminAreaNameMessageId(), |
| 59 copy.GetAdminAreaNameMessageId()); | 64 copy.GetAdminAreaNameMessageId()); |
| 60 EXPECT_NE(rule.GetPostalCodeNameMessageId(), | 65 EXPECT_NE(rule.GetPostalCodeNameMessageId(), |
| 61 copy.GetPostalCodeNameMessageId()); | 66 copy.GetPostalCodeNameMessageId()); |
| 62 | 67 |
| 63 copy.CopyFrom(rule); | 68 copy.CopyFrom(rule); |
| 64 EXPECT_EQ(rule.GetFormat(), copy.GetFormat()); | 69 EXPECT_EQ(rule.GetFormat(), copy.GetFormat()); |
| 70 EXPECT_EQ(rule.GetRequired(), copy.GetRequired()); |
| 65 EXPECT_EQ(rule.GetSubKeys(), copy.GetSubKeys()); | 71 EXPECT_EQ(rule.GetSubKeys(), copy.GetSubKeys()); |
| 66 EXPECT_EQ(rule.GetLanguages(), copy.GetLanguages()); | 72 EXPECT_EQ(rule.GetLanguages(), copy.GetLanguages()); |
| 67 EXPECT_EQ(rule.GetLanguage(), copy.GetLanguage()); | 73 EXPECT_EQ(rule.GetLanguage(), copy.GetLanguage()); |
| 74 EXPECT_EQ(rule.GetPostalCodeFormat(), copy.GetPostalCodeFormat()); |
| 68 EXPECT_EQ(rule.GetAdminAreaNameMessageId(), | 75 EXPECT_EQ(rule.GetAdminAreaNameMessageId(), |
| 69 copy.GetAdminAreaNameMessageId()); | 76 copy.GetAdminAreaNameMessageId()); |
| 70 EXPECT_EQ(rule.GetPostalCodeNameMessageId(), | 77 EXPECT_EQ(rule.GetPostalCodeNameMessageId(), |
| 71 copy.GetPostalCodeNameMessageId()); | 78 copy.GetPostalCodeNameMessageId()); |
| 72 } | 79 } |
| 73 | 80 |
| 74 TEST(RuleTest, ParseOverwritesRule) { | 81 TEST(RuleTest, ParseOverwritesRule) { |
| 75 Rule rule; | 82 Rule rule; |
| 76 ASSERT_TRUE(rule.ParseSerializedRule("{" | 83 ASSERT_TRUE(rule.ParseSerializedRule( |
| 77 "\"fmt\":\"%S%Z\"," | 84 "{" |
| 78 "\"state_name_type\":\"area\"," | 85 "\"fmt\":\"%S%Z\"," |
| 79 "\"zip_name_type\":\"postal\"," | 86 "\"require\":\"SZ\"," |
| 80 "\"sub_keys\":\"CA~NY~TX\"," | 87 "\"state_name_type\":\"area\"," |
| 81 "\"lang\":\"en\"," | 88 "\"zip_name_type\":\"postal\"," |
| 82 "\"languages\":\"en~fr\"" | 89 "\"sub_keys\":\"CA~NY~TX\"," |
| 83 "}")); | 90 "\"lang\":\"en\"," |
| 91 "\"languages\":\"en~fr\"," |
| 92 "\"zip\":\"\\\\d{5}([ \\\\-]\\\\d{4})?\"" |
| 93 "}")); |
| 84 EXPECT_FALSE(rule.GetFormat().empty()); | 94 EXPECT_FALSE(rule.GetFormat().empty()); |
| 95 EXPECT_FALSE(rule.GetRequired().empty()); |
| 85 EXPECT_FALSE(rule.GetSubKeys().empty()); | 96 EXPECT_FALSE(rule.GetSubKeys().empty()); |
| 86 EXPECT_FALSE(rule.GetLanguages().empty()); | 97 EXPECT_FALSE(rule.GetLanguages().empty()); |
| 87 EXPECT_FALSE(rule.GetLanguage().empty()); | 98 EXPECT_FALSE(rule.GetLanguage().empty()); |
| 99 EXPECT_FALSE(rule.GetPostalCodeFormat().empty()); |
| 88 EXPECT_EQ(IDS_LIBADDRESSINPUT_I18N_AREA, | 100 EXPECT_EQ(IDS_LIBADDRESSINPUT_I18N_AREA, |
| 89 rule.GetAdminAreaNameMessageId()); | 101 rule.GetAdminAreaNameMessageId()); |
| 90 EXPECT_EQ(IDS_LIBADDRESSINPUT_I18N_POSTAL_CODE_LABEL, | 102 EXPECT_EQ(IDS_LIBADDRESSINPUT_I18N_POSTAL_CODE_LABEL, |
| 91 rule.GetPostalCodeNameMessageId()); | 103 rule.GetPostalCodeNameMessageId()); |
| 92 | 104 |
| 93 ASSERT_TRUE(rule.ParseSerializedRule("{" | 105 ASSERT_TRUE(rule.ParseSerializedRule( |
| 94 "\"fmt\":\"\"," | 106 "{" |
| 95 "\"state_name_type\":\"do_si\"," | 107 "\"fmt\":\"\"," |
| 96 "\"zip_name_type\":\"zip\"," | 108 "\"require\":\"\"," |
| 97 "\"sub_keys\":\"\"," | 109 "\"state_name_type\":\"do_si\"," |
| 98 "\"lang\":\"\"," | 110 "\"zip_name_type\":\"zip\"," |
| 99 "\"languages\":\"\"" | 111 "\"sub_keys\":\"\"," |
| 100 "}")); | 112 "\"lang\":\"\"," |
| 113 "\"languages\":\"\"," |
| 114 "\"zip\":\"\"" |
| 115 "}")); |
| 101 EXPECT_TRUE(rule.GetFormat().empty()); | 116 EXPECT_TRUE(rule.GetFormat().empty()); |
| 117 EXPECT_TRUE(rule.GetRequired().empty()); |
| 102 EXPECT_TRUE(rule.GetSubKeys().empty()); | 118 EXPECT_TRUE(rule.GetSubKeys().empty()); |
| 103 EXPECT_TRUE(rule.GetLanguages().empty()); | 119 EXPECT_TRUE(rule.GetLanguages().empty()); |
| 104 EXPECT_TRUE(rule.GetLanguage().empty()); | 120 EXPECT_TRUE(rule.GetLanguage().empty()); |
| 121 EXPECT_TRUE(rule.GetPostalCodeFormat().empty()); |
| 105 EXPECT_EQ(IDS_LIBADDRESSINPUT_I18N_DO_SI, | 122 EXPECT_EQ(IDS_LIBADDRESSINPUT_I18N_DO_SI, |
| 106 rule.GetAdminAreaNameMessageId()); | 123 rule.GetAdminAreaNameMessageId()); |
| 107 EXPECT_EQ(IDS_LIBADDRESSINPUT_I18N_ZIP_CODE_LABEL, | 124 EXPECT_EQ(IDS_LIBADDRESSINPUT_I18N_ZIP_CODE_LABEL, |
| 108 rule.GetPostalCodeNameMessageId()); | 125 rule.GetPostalCodeNameMessageId()); |
| 109 } | 126 } |
| 110 | 127 |
| 111 TEST(RuleTest, ParsesFormatCorrectly) { | 128 TEST(RuleTest, ParseFormatWithNewLines) { |
| 112 Rule rule; | 129 Rule rule; |
| 113 ASSERT_TRUE(rule.ParseSerializedRule("{\"fmt\":\"%S\"}")); | 130 ASSERT_TRUE( |
| 114 ASSERT_EQ(1, rule.GetFormat().size()); | 131 rule.ParseSerializedRule("{\"fmt\":\"%O%n%N%n%A%nAX-%Z %C%nĂ…LAND\"}")); |
| 115 EXPECT_EQ(ADMIN_AREA, rule.GetFormat()[0]); | 132 std::vector<AddressField> expected; |
| 133 expected.push_back(ORGANIZATION); |
| 134 expected.push_back(static_cast<AddressField>(NEWLINE)); |
| 135 expected.push_back(RECIPIENT); |
| 136 expected.push_back(static_cast<AddressField>(NEWLINE)); |
| 137 expected.push_back(STREET_ADDRESS); |
| 138 expected.push_back(static_cast<AddressField>(NEWLINE)); |
| 139 expected.push_back(POSTAL_CODE); |
| 140 expected.push_back(LOCALITY); |
| 141 expected.push_back(static_cast<AddressField>(NEWLINE)); |
| 142 EXPECT_EQ(expected, rule.GetFormat()); |
| 143 } |
| 144 |
| 145 TEST(RuleTest, DoubleTokenPrefixIsIgnoredInFormat) { |
| 146 Rule rule; |
| 147 ASSERT_TRUE(rule.ParseSerializedRule("{\"fmt\":\"%%R\"}")); |
| 148 EXPECT_EQ(std::vector<AddressField>(1, COUNTRY), rule.GetFormat()); |
| 149 } |
| 150 |
| 151 TEST(RuleTest, PrefixWithoutTokenIsIgnoredInFormat) { |
| 152 Rule rule; |
| 153 ASSERT_TRUE(rule.ParseSerializedRule("{\"fmt\":\"%\"}")); |
| 154 EXPECT_TRUE(rule.GetFormat().empty()); |
| 155 } |
| 156 |
| 157 TEST(RuleTest, EmptyStringFormat) { |
| 158 Rule rule; |
| 159 ASSERT_TRUE(rule.ParseSerializedRule("{\"fmt\":\"\"}")); |
| 160 EXPECT_TRUE(rule.GetFormat().empty()); |
| 161 } |
| 162 |
| 163 TEST(RuleTest, ParseRequiredFields) { |
| 164 Rule rule; |
| 165 ASSERT_TRUE(rule.ParseSerializedRule("{\"require\":\"ONAZC\"}")); |
| 166 std::vector<AddressField> expected; |
| 167 expected.push_back(ORGANIZATION); |
| 168 expected.push_back(RECIPIENT); |
| 169 expected.push_back(STREET_ADDRESS); |
| 170 expected.push_back(POSTAL_CODE); |
| 171 expected.push_back(LOCALITY); |
| 172 EXPECT_EQ(expected, rule.GetRequired()); |
| 173 } |
| 174 |
| 175 TEST(RuleTest, ParseEmptyStringRequiredFields) { |
| 176 Rule rule; |
| 177 ASSERT_TRUE(rule.ParseSerializedRule("{\"require\":\"\"}")); |
| 178 EXPECT_TRUE(rule.GetRequired().empty()); |
| 179 } |
| 180 |
| 181 TEST(RuleTest, ParseInvalidRequiredFields) { |
| 182 Rule rule; |
| 183 ASSERT_TRUE(rule.ParseSerializedRule("{\"require\":\"garbage\"}")); |
| 184 EXPECT_TRUE(rule.GetRequired().empty()); |
| 185 } |
| 186 |
| 187 TEST(RuleTest, ParseDuplicateRequiredFields) { |
| 188 Rule rule; |
| 189 ASSERT_TRUE(rule.ParseSerializedRule("{\"require\":\"SSS\"}")); |
| 190 EXPECT_EQ(std::vector<AddressField>(3, ADMIN_AREA), rule.GetRequired()); |
| 116 } | 191 } |
| 117 | 192 |
| 118 TEST(RuleTest, ParsesSubKeysCorrectly) { | 193 TEST(RuleTest, ParsesSubKeysCorrectly) { |
| 119 Rule rule; | 194 Rule rule; |
| 120 ASSERT_TRUE(rule.ParseSerializedRule("{\"sub_keys\":\"CA~NY~TX\"}")); | 195 ASSERT_TRUE(rule.ParseSerializedRule("{\"sub_keys\":\"CA~NY~TX\"}")); |
| 121 std::vector<std::string> expected; | 196 std::vector<std::string> expected; |
| 122 expected.push_back("CA"); | 197 expected.push_back("CA"); |
| 123 expected.push_back("NY"); | 198 expected.push_back("NY"); |
| 124 expected.push_back("TX"); | 199 expected.push_back("TX"); |
| 125 EXPECT_EQ(expected, rule.GetSubKeys()); | 200 EXPECT_EQ(expected, rule.GetSubKeys()); |
| 126 } | 201 } |
| 127 | 202 |
| 128 TEST(RuleTest, ParsesLanguageCorrectly) { | 203 TEST(RuleTest, ParsesLanguageCorrectly) { |
| 129 Rule rule; | 204 Rule rule; |
| 130 ASSERT_TRUE(rule.ParseSerializedRule("{\"lang\":\"en\"}")); | 205 ASSERT_TRUE(rule.ParseSerializedRule("{\"lang\":\"en\"}")); |
| 131 EXPECT_EQ("en", rule.GetLanguage()); | 206 EXPECT_EQ("en", rule.GetLanguage()); |
| 132 } | 207 } |
| 133 | 208 |
| 134 TEST(RuleTest, ParsesLanguagesCorrectly) { | 209 TEST(RuleTest, ParsesLanguagesCorrectly) { |
| 135 Rule rule; | 210 Rule rule; |
| 136 ASSERT_TRUE(rule.ParseSerializedRule("{\"languages\":\"de~fr~it\"}")); | 211 ASSERT_TRUE(rule.ParseSerializedRule("{\"languages\":\"de~fr~it\"}")); |
| 137 std::vector<std::string> expected; | 212 std::vector<std::string> expected; |
| 138 expected.push_back("de"); | 213 expected.push_back("de"); |
| 139 expected.push_back("fr"); | 214 expected.push_back("fr"); |
| 140 expected.push_back("it"); | 215 expected.push_back("it"); |
| 141 EXPECT_EQ(expected, rule.GetLanguages()); | 216 EXPECT_EQ(expected, rule.GetLanguages()); |
| 142 } | 217 } |
| 143 | 218 |
| 219 TEST(RuleTest, ParsesPostalCodeFormatCorrectly) { |
| 220 Rule rule; |
| 221 ASSERT_TRUE(rule.ParseSerializedRule( |
| 222 "{" |
| 223 "\"zip\":\"\\\\d{5}([ \\\\-]\\\\d{4})?\"" |
| 224 "}")); |
| 225 EXPECT_EQ("\\d{5}([ \\-]\\d{4})?", rule.GetPostalCodeFormat()); |
| 226 } |
| 227 |
| 144 TEST(RuleTest, EmptyStringIsNotValid) { | 228 TEST(RuleTest, EmptyStringIsNotValid) { |
| 145 Rule rule; | 229 Rule rule; |
| 146 EXPECT_FALSE(rule.ParseSerializedRule(std::string())); | 230 EXPECT_FALSE(rule.ParseSerializedRule(std::string())); |
| 147 } | 231 } |
| 148 | 232 |
| 149 TEST(RuleTest, EmptyDictionaryIsValid) { | 233 TEST(RuleTest, EmptyDictionaryIsValid) { |
| 150 Rule rule; | 234 Rule rule; |
| 151 EXPECT_TRUE(rule.ParseSerializedRule("{}")); | 235 EXPECT_TRUE(rule.ParseSerializedRule("{}")); |
| 152 } | 236 } |
| 153 | 237 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 EXPECT_TRUE(rule_.ParseSerializedRule( | 308 EXPECT_TRUE(rule_.ParseSerializedRule( |
| 225 RegionDataConstants::GetRegionData(GetParam()))); | 309 RegionDataConstants::GetRegionData(GetParam()))); |
| 226 } | 310 } |
| 227 | 311 |
| 228 // Test parsing all region data. | 312 // Test parsing all region data. |
| 229 INSTANTIATE_TEST_CASE_P( | 313 INSTANTIATE_TEST_CASE_P( |
| 230 AllRulesTest, RuleParseTest, | 314 AllRulesTest, RuleParseTest, |
| 231 testing::ValuesIn(RegionDataConstants::GetRegionCodes())); | 315 testing::ValuesIn(RegionDataConstants::GetRegionCodes())); |
| 232 | 316 |
| 233 } // namespace | 317 } // namespace |
| OLD | NEW |