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 "region_data_constants.h" | |
26 | |
27 namespace { | |
28 | |
29 using i18n::addressinput::AddressField; | |
30 using i18n::addressinput::ADMIN_AREA; | |
31 using i18n::addressinput::COUNTRY; | |
32 using i18n::addressinput::FormatElement; | |
33 using i18n::addressinput::LOCALITY; | |
34 using i18n::addressinput::POSTAL_CODE; | |
35 using i18n::addressinput::RECIPIENT; | |
36 using i18n::addressinput::RegionDataConstants; | |
37 using i18n::addressinput::Rule; | |
38 using i18n::addressinput::STREET_ADDRESS; | |
39 | |
40 bool IsFormatEmpty(const std::vector<std::vector<FormatElement> >& format) { | |
41 for (std::vector<std::vector<FormatElement> >::const_iterator | |
42 it = format.begin(); | |
43 it != format.end(); | |
44 ++it) { | |
45 if (!it->empty()) { | |
46 return false; | |
47 } | |
48 } | |
49 return true; | |
50 } | |
51 | |
52 TEST(RuleTest, CopyOverwritesRule) { | |
53 Rule rule; | |
54 ASSERT_TRUE(rule.ParseSerializedRule( | |
55 "{" | |
56 "\"fmt\":\"%S%Z\"," | |
57 "\"require\":\"SZ\"," | |
58 "\"state_name_type\":\"area\"," | |
59 "\"zip_name_type\":\"postal\"," | |
60 "\"sub_keys\":\"CA~NY~TX\"," | |
61 "\"lang\":\"en\"," | |
62 "\"languages\":\"en~fr\"," | |
63 "\"zip\":\"\\\\d{5}([ \\\\-]\\\\d{4})?\"" | |
64 "}")); | |
65 | |
66 Rule copy; | |
67 EXPECT_NE(rule.GetFormat(), copy.GetFormat()); | |
68 EXPECT_NE(rule.GetRequired(), copy.GetRequired()); | |
69 EXPECT_NE(rule.GetSubKeys(), copy.GetSubKeys()); | |
70 EXPECT_NE(rule.GetLanguages(), copy.GetLanguages()); | |
71 EXPECT_NE(rule.GetLanguage(), copy.GetLanguage()); | |
72 EXPECT_NE(rule.GetPostalCodeFormat(), copy.GetPostalCodeFormat()); | |
73 EXPECT_NE(rule.GetAdminAreaNameType(), | |
74 copy.GetAdminAreaNameType()); | |
75 EXPECT_NE(rule.GetPostalCodeNameType(), | |
76 copy.GetPostalCodeNameType()); | |
77 | |
78 copy.CopyFrom(rule); | |
79 EXPECT_EQ(rule.GetFormat(), copy.GetFormat()); | |
80 EXPECT_EQ(rule.GetRequired(), copy.GetRequired()); | |
81 EXPECT_EQ(rule.GetSubKeys(), copy.GetSubKeys()); | |
82 EXPECT_EQ(rule.GetLanguages(), copy.GetLanguages()); | |
83 EXPECT_EQ(rule.GetLanguage(), copy.GetLanguage()); | |
84 EXPECT_EQ(rule.GetPostalCodeFormat(), copy.GetPostalCodeFormat()); | |
85 EXPECT_EQ(rule.GetAdminAreaNameType(), | |
86 copy.GetAdminAreaNameType()); | |
87 EXPECT_EQ(rule.GetPostalCodeNameType(), | |
88 copy.GetPostalCodeNameType()); | |
89 } | |
90 | |
91 TEST(RuleTest, ParseOverwritesRule) { | |
92 Rule rule; | |
93 ASSERT_TRUE(rule.ParseSerializedRule( | |
94 "{" | |
95 "\"fmt\":\"%S%Z\"," | |
96 "\"require\":\"SZ\"," | |
97 "\"state_name_type\":\"area\"," | |
98 "\"zip_name_type\":\"postal\"," | |
99 "\"sub_keys\":\"CA~NY~TX\"," | |
100 "\"lang\":\"en\"," | |
101 "\"languages\":\"en~fr\"," | |
102 "\"zip\":\"\\\\d{5}([ \\\\-]\\\\d{4})?\"" | |
103 "}")); | |
104 EXPECT_FALSE(IsFormatEmpty(rule.GetFormat())); | |
105 EXPECT_FALSE(rule.GetRequired().empty()); | |
106 EXPECT_FALSE(rule.GetSubKeys().empty()); | |
107 EXPECT_FALSE(rule.GetLanguages().empty()); | |
108 EXPECT_FALSE(rule.GetLanguage().empty()); | |
109 EXPECT_FALSE(rule.GetPostalCodeFormat().empty()); | |
110 EXPECT_EQ("area", rule.GetAdminAreaNameType()); | |
111 EXPECT_EQ("postal", rule.GetPostalCodeNameType()); | |
112 | |
113 ASSERT_TRUE(rule.ParseSerializedRule( | |
114 "{" | |
115 "\"fmt\":\"\"," | |
116 "\"require\":\"\"," | |
117 "\"state_name_type\":\"do_si\"," | |
118 "\"zip_name_type\":\"zip\"," | |
119 "\"sub_keys\":\"\"," | |
120 "\"lang\":\"\"," | |
121 "\"languages\":\"\"," | |
122 "\"zip\":\"\"" | |
123 "}")); | |
124 EXPECT_TRUE(IsFormatEmpty(rule.GetFormat())); | |
125 EXPECT_TRUE(rule.GetRequired().empty()); | |
126 EXPECT_TRUE(rule.GetSubKeys().empty()); | |
127 EXPECT_TRUE(rule.GetLanguages().empty()); | |
128 EXPECT_TRUE(rule.GetLanguage().empty()); | |
129 EXPECT_TRUE(rule.GetPostalCodeFormat().empty()); | |
130 EXPECT_EQ("do_si", rule.GetAdminAreaNameType()); | |
131 EXPECT_EQ("zip", rule.GetPostalCodeNameType()); | |
132 } | |
133 | |
134 TEST(RuleTest, ParseEmptyDataDoesNotOverwriteRule) { | |
135 Rule rule; | |
136 ASSERT_TRUE(rule.ParseSerializedRule( | |
137 "{" | |
138 "\"fmt\":\"%S%Z\"," | |
139 "\"require\":\"SZ\"," | |
140 "\"state_name_type\":\"area\"," | |
141 "\"zip_name_type\":\"postal\"," | |
142 "\"sub_keys\":\"CA~NY~TX\"," | |
143 "\"lang\":\"en\"," | |
144 "\"languages\":\"en~fr\"," | |
145 "\"zip\":\"\\\\d{5}([ \\\\-]\\\\d{4})?\"" | |
146 "}")); | |
147 | |
148 Rule copy; | |
149 copy.CopyFrom(rule); | |
150 ASSERT_TRUE(copy.ParseSerializedRule("{}")); | |
151 | |
152 EXPECT_EQ(rule.GetFormat(), copy.GetFormat()); | |
153 EXPECT_EQ(rule.GetRequired(), copy.GetRequired()); | |
154 EXPECT_EQ(rule.GetSubKeys(), copy.GetSubKeys()); | |
155 EXPECT_EQ(rule.GetLanguages(), copy.GetLanguages()); | |
156 EXPECT_EQ(rule.GetLanguage(), copy.GetLanguage()); | |
157 EXPECT_EQ(rule.GetPostalCodeFormat(), copy.GetPostalCodeFormat()); | |
158 EXPECT_EQ(rule.GetAdminAreaNameType(), | |
159 copy.GetAdminAreaNameType()); | |
160 EXPECT_EQ(rule.GetPostalCodeNameType(), | |
161 copy.GetPostalCodeNameType()); | |
162 } | |
163 | |
164 TEST(RuleTest, ParseFormatWithNewLines) { | |
165 Rule rule; | |
166 ASSERT_TRUE( | |
167 rule.ParseSerializedRule("{\"fmt\":\"%O%n%N%n%A%nAX-%Z %C%nÅLAND\"}")); | |
168 | |
169 std::vector<std::vector<FormatElement> > expected_format; | |
170 expected_format.push_back( | |
171 std::vector<FormatElement>(1, FormatElement(RECIPIENT))); | |
172 expected_format.push_back( | |
173 std::vector<FormatElement>(1, FormatElement(STREET_ADDRESS))); | |
174 expected_format.push_back( | |
175 std::vector<FormatElement>(1, FormatElement("AX-"))); | |
176 expected_format.back().push_back(FormatElement(POSTAL_CODE)); | |
177 expected_format.back().push_back(FormatElement(" ")); | |
178 expected_format.back().push_back(FormatElement(LOCALITY)); | |
179 expected_format.push_back( | |
180 std::vector<FormatElement>(1, FormatElement("ÅLAND"))); | |
181 | |
182 EXPECT_EQ(expected_format, rule.GetFormat()); | |
183 } | |
184 | |
185 TEST(RuleTest, DoubleTokenPrefixDoesNotCrash) { | |
186 Rule rule; | |
187 EXPECT_TRUE(rule.ParseSerializedRule("{\"fmt\":\"%%R\"}")); | |
188 } | |
189 | |
190 TEST(RuleTest, DoubleNewlineFormatDoesNotCrash) { | |
191 Rule rule; | |
192 EXPECT_TRUE(rule.ParseSerializedRule("{\"fmt\":\"%n%n\"}")); | |
193 } | |
194 | |
195 TEST(RuleTest, FormatTokenWithoutPrefixDoesNotCrash) { | |
196 Rule rule; | |
197 ASSERT_TRUE(rule.ParseSerializedRule("{\"fmt\":\"R\"}")); | |
198 } | |
199 | |
200 TEST(RuleTest, ParseDuplicateTokenInFormatDoesNotCrash) { | |
201 Rule rule; | |
202 EXPECT_TRUE(rule.ParseSerializedRule("{\"fmt\":\"%R%R\"}")); | |
203 } | |
204 | |
205 TEST(RuleTest, ParseInvalidFormatFieldsDoesNotCrash) { | |
206 Rule rule; | |
207 EXPECT_TRUE(rule.ParseSerializedRule("{\"fmt\":\"%K%L\"}")); | |
208 } | |
209 | |
210 TEST(RuleTest, PrefixWithoutTokenFormatDoesNotCrash) { | |
211 Rule rule; | |
212 EXPECT_TRUE(rule.ParseSerializedRule("{\"fmt\":\"%\"}")); | |
213 } | |
214 | |
215 TEST(RuleTest, EmptyStringFormatDoesNotCrash) { | |
216 Rule rule; | |
217 EXPECT_TRUE(rule.ParseSerializedRule("{\"fmt\":\"\"}")); | |
218 } | |
219 | |
220 TEST(RuleTest, ParseRequiredFields) { | |
221 Rule rule; | |
222 ASSERT_TRUE(rule.ParseSerializedRule("{\"require\":\"ONAZC\"}")); | |
223 std::vector<AddressField> expected; | |
224 expected.push_back(RECIPIENT); | |
225 expected.push_back(STREET_ADDRESS); | |
226 expected.push_back(POSTAL_CODE); | |
227 expected.push_back(LOCALITY); | |
228 EXPECT_EQ(expected, rule.GetRequired()); | |
229 } | |
230 | |
231 TEST(RuleTest, ParseEmptyStringRequiredFields) { | |
232 Rule rule; | |
233 ASSERT_TRUE(rule.ParseSerializedRule("{\"require\":\"\"}")); | |
234 EXPECT_TRUE(rule.GetRequired().empty()); | |
235 } | |
236 | |
237 TEST(RuleTest, ParseInvalidRequiredFields) { | |
238 Rule rule; | |
239 ASSERT_TRUE(rule.ParseSerializedRule("{\"require\":\"garbage\"}")); | |
240 EXPECT_TRUE(rule.GetRequired().empty()); | |
241 } | |
242 | |
243 TEST(RuleTest, ParseDuplicateRequiredFields) { | |
244 Rule rule; | |
245 ASSERT_TRUE(rule.ParseSerializedRule("{\"require\":\"SSS\"}")); | |
246 EXPECT_EQ(std::vector<AddressField>(3, ADMIN_AREA), rule.GetRequired()); | |
247 } | |
248 | |
249 TEST(RuleTest, ParsesSubKeysCorrectly) { | |
250 Rule rule; | |
251 ASSERT_TRUE(rule.ParseSerializedRule("{\"sub_keys\":\"CA~NY~TX\"}")); | |
252 std::vector<std::string> expected; | |
253 expected.push_back("CA"); | |
254 expected.push_back("NY"); | |
255 expected.push_back("TX"); | |
256 EXPECT_EQ(expected, rule.GetSubKeys()); | |
257 } | |
258 | |
259 TEST(RuleTest, ParsesLanguageCorrectly) { | |
260 Rule rule; | |
261 ASSERT_TRUE(rule.ParseSerializedRule("{\"lang\":\"en\"}")); | |
262 EXPECT_EQ("en", rule.GetLanguage()); | |
263 } | |
264 | |
265 TEST(RuleTest, ParsesLanguagesCorrectly) { | |
266 Rule rule; | |
267 ASSERT_TRUE(rule.ParseSerializedRule("{\"languages\":\"de~fr~it\"}")); | |
268 std::vector<std::string> expected; | |
269 expected.push_back("de"); | |
270 expected.push_back("fr"); | |
271 expected.push_back("it"); | |
272 EXPECT_EQ(expected, rule.GetLanguages()); | |
273 } | |
274 | |
275 TEST(RuleTest, ParsesPostalCodeFormatCorrectly) { | |
276 Rule rule; | |
277 ASSERT_TRUE(rule.ParseSerializedRule( | |
278 "{" | |
279 "\"zip\":\"\\\\d{5}([ \\\\-]\\\\d{4})?\"" | |
280 "}")); | |
281 EXPECT_EQ("\\d{5}([ \\-]\\d{4})?", rule.GetPostalCodeFormat()); | |
282 } | |
283 | |
284 TEST(RuleTest, EmptyStringIsNotValid) { | |
285 Rule rule; | |
286 EXPECT_FALSE(rule.ParseSerializedRule(std::string())); | |
287 } | |
288 | |
289 TEST(RuleTest, EmptyDictionaryIsValid) { | |
290 Rule rule; | |
291 EXPECT_TRUE(rule.ParseSerializedRule("{}")); | |
292 } | |
293 | |
294 TEST(RuleTest, ParseSubKeyTest) { | |
295 i18n::addressinput::Rule rule; | |
296 ASSERT_TRUE(rule.ParseSerializedRule( | |
297 "{ \"sub_keys\": \"FOO~BAR~BAZ\"," | |
298 " \"sub_names\": \"Foolandia~Bartopolis~Bazmonia\"," | |
299 " \"sub_lnames\": \"Foolandia2~Bartopolis2~Bazmonia2\" }")); | |
300 EXPECT_EQ(3U, rule.GetSubKeys().size()); | |
301 | |
302 std::string sub_key; | |
303 EXPECT_TRUE(rule.CanonicalizeSubKey("BAR", false, &sub_key)); | |
304 EXPECT_EQ("BAR", sub_key); | |
305 sub_key.clear(); | |
306 | |
307 EXPECT_TRUE(rule.CanonicalizeSubKey("Bartopolis", false, &sub_key)); | |
308 EXPECT_EQ("BAR", sub_key); | |
309 sub_key.clear(); | |
310 | |
311 // Unlatinize. | |
312 EXPECT_TRUE(rule.CanonicalizeSubKey("Bartopolis2", false, &sub_key)); | |
313 EXPECT_EQ("BAR", sub_key); | |
314 sub_key.clear(); | |
315 | |
316 // Keep input latin. | |
317 EXPECT_TRUE(rule.CanonicalizeSubKey("Bartopolis2", true, &sub_key)); | |
318 EXPECT_EQ("Bartopolis2", sub_key); | |
319 sub_key.clear(); | |
320 | |
321 EXPECT_FALSE(rule.CanonicalizeSubKey("Beertopia", false, &sub_key)); | |
322 EXPECT_EQ("", sub_key); | |
323 } | |
324 | |
325 struct LabelData { | |
326 LabelData(const std::string& data, const std::string& name_type) | |
327 : data(data), name_type(name_type) {} | |
328 | |
329 ~LabelData() {} | |
330 | |
331 std::string data; | |
332 std::string name_type; | |
333 }; | |
334 | |
335 // Tests for parsing the postal code name. | |
336 class PostalCodeNameParseTest : public testing::TestWithParam<LabelData> { | |
337 protected: | |
338 Rule rule_; | |
339 }; | |
340 | |
341 // Verifies that a postal code name is parsed correctly. | |
342 TEST_P(PostalCodeNameParseTest, ParsedCorrectly) { | |
343 ASSERT_TRUE(rule_.ParseSerializedRule(GetParam().data)); | |
344 EXPECT_EQ(GetParam().name_type, rule_.GetPostalCodeNameType()); | |
345 } | |
346 | |
347 // Test parsing all postal code names. | |
348 INSTANTIATE_TEST_CASE_P( | |
349 AllPostalCodeNames, PostalCodeNameParseTest, | |
350 testing::Values( | |
351 LabelData("{\"zip_name_type\":\"postal\"}", "postal"), | |
352 LabelData("{\"zip_name_type\":\"zip\"}", "zip"))); | |
353 | |
354 // Tests for parsing the administrative area name. | |
355 class AdminAreaNameParseTest : public testing::TestWithParam<LabelData> { | |
356 protected: | |
357 Rule rule_; | |
358 }; | |
359 | |
360 // Verifies that an administrative area name is parsed correctly. | |
361 TEST_P(AdminAreaNameParseTest, ParsedCorrectly) { | |
362 ASSERT_TRUE(rule_.ParseSerializedRule(GetParam().data)); | |
363 EXPECT_EQ(GetParam().name_type, rule_.GetAdminAreaNameType()); | |
364 } | |
365 | |
366 // Test parsing all administrative area names. | |
367 INSTANTIATE_TEST_CASE_P( | |
368 AllAdminAreaNames, AdminAreaNameParseTest, | |
369 testing::Values( | |
370 LabelData("{\"state_name_type\":\"area\"}", "area"), | |
371 LabelData("{\"state_name_type\":\"county\"}", "county"), | |
372 LabelData("{\"state_name_type\":\"department\"}", "department"), | |
373 LabelData("{\"state_name_type\":\"district\"}", "district"), | |
374 LabelData("{\"state_name_type\":\"do_si\"}", "do_si"), | |
375 LabelData("{\"state_name_type\":\"emirate\"}", "emirate"), | |
376 LabelData("{\"state_name_type\":\"island\"}", "island"), | |
377 LabelData("{\"state_name_type\":\"parish\"}", "parish"), | |
378 LabelData("{\"state_name_type\":\"prefecture\"}", "prefecture"), | |
379 LabelData("{\"state_name_type\":\"province\"}", "province"), | |
380 LabelData("{\"state_name_type\":\"state\"}", "state"))); | |
381 | |
382 // Verifies that an address format does not contain consecutive lines with | |
383 // multiple fields each. Such address format (e.g. {{ELEMENT, ELEMENT}, | |
384 // {ELEMENT, ELEMENT}}) will result in incorrect behavior of BuildComponents() | |
385 // public API. | |
386 TEST(RuleParseTest, ConsecutiveLinesWithMultipleFields) { | |
387 const std::vector<std::string>& region_codes = | |
388 RegionDataConstants::GetRegionCodes(); | |
389 Rule rule; | |
390 for (size_t i = 0; i < region_codes.size(); ++i) { | |
391 const std::string& region_data = | |
392 RegionDataConstants::GetRegionData(region_codes[i]); | |
393 SCOPED_TRACE(region_codes[i] + ": " + region_data); | |
394 | |
395 ASSERT_TRUE(rule.ParseSerializedRule(region_data)); | |
396 bool previous_line_has_single_field = true; | |
397 for (std::vector<std::vector<FormatElement> >::const_iterator | |
398 line_it = rule.GetFormat().begin(); | |
399 line_it != rule.GetFormat().end(); | |
400 ++line_it) { | |
401 int num_fields = 0; | |
402 for (std::vector<FormatElement>::const_iterator | |
403 element_it = line_it->begin(); | |
404 element_it != line_it->end(); | |
405 ++element_it) { | |
406 if (element_it->IsField()) { | |
407 ++num_fields; | |
408 } | |
409 } | |
410 if (num_fields == 0) { | |
411 continue; | |
412 } | |
413 ASSERT_TRUE(num_fields == 1 || previous_line_has_single_field); | |
414 previous_line_has_single_field = num_fields == 1; | |
415 } | |
416 } | |
417 } | |
418 | |
419 // Verifies that a street line is surrounded by either newlines or spaces. A | |
420 // different format will result in incorrect behavior in | |
421 // AddressData::BuildDisplayLines(). | |
422 TEST(RuleParseTest, StreetAddressSurroundingElements) { | |
423 const std::vector<std::string>& region_codes = | |
424 RegionDataConstants::GetRegionCodes(); | |
425 Rule rule; | |
426 for (size_t i = 0; i < region_codes.size(); ++i) { | |
427 const std::string& region_data = | |
428 RegionDataConstants::GetRegionData(region_codes[i]); | |
429 SCOPED_TRACE(region_codes[i] + ": " + region_data); | |
430 | |
431 ASSERT_TRUE(rule.ParseSerializedRule(region_data)); | |
432 for (std::vector<std::vector<FormatElement> >::const_iterator | |
433 line_it = rule.GetFormat().begin(); | |
434 line_it != rule.GetFormat().end(); | |
435 ++line_it) { | |
436 for (size_t i = 0; i < line_it->size(); ++i) { | |
437 const FormatElement& element = line_it->at(i); | |
438 if (element.IsField() && element.field == STREET_ADDRESS) { | |
439 bool surrounded_by_newlines = line_it->size() == 1; | |
440 bool surrounded_by_spaces = | |
441 i > 0 && | |
442 i < line_it->size() - 1 && | |
443 !line_it->at(i - 1).IsField() && | |
444 line_it->at(i - 1).literal == " " && | |
445 !line_it->at(i + 1).IsField() && | |
446 line_it->at(i + 1).literal == " "; | |
447 EXPECT_TRUE(surrounded_by_newlines || surrounded_by_spaces); | |
448 } | |
449 } | |
450 } | |
451 } | |
452 } | |
453 | |
454 } // namespace | |
OLD | NEW |