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, |
(...skipping 12 matching lines...) Expand all Loading... |
23 #include <gtest/gtest.h> | 23 #include <gtest/gtest.h> |
24 | 24 |
25 #include "grit/libaddressinput_strings.h" | 25 #include "grit/libaddressinput_strings.h" |
26 #include "region_data_constants.h" | 26 #include "region_data_constants.h" |
27 | 27 |
28 namespace { | 28 namespace { |
29 | 29 |
30 using i18n::addressinput::AddressField; | 30 using i18n::addressinput::AddressField; |
31 using i18n::addressinput::ADMIN_AREA; | 31 using i18n::addressinput::ADMIN_AREA; |
32 using i18n::addressinput::COUNTRY; | 32 using i18n::addressinput::COUNTRY; |
| 33 using i18n::addressinput::FormatElement; |
33 using i18n::addressinput::LOCALITY; | 34 using i18n::addressinput::LOCALITY; |
34 using i18n::addressinput::ORGANIZATION; | 35 using i18n::addressinput::ORGANIZATION; |
35 using i18n::addressinput::POSTAL_CODE; | 36 using i18n::addressinput::POSTAL_CODE; |
36 using i18n::addressinput::RECIPIENT; | 37 using i18n::addressinput::RECIPIENT; |
37 using i18n::addressinput::RegionDataConstants; | 38 using i18n::addressinput::RegionDataConstants; |
38 using i18n::addressinput::Rule; | 39 using i18n::addressinput::Rule; |
39 using i18n::addressinput::STREET_ADDRESS; | 40 using i18n::addressinput::STREET_ADDRESS; |
40 | 41 |
41 bool IsFormatEmpty(const std::vector<std::vector<AddressField> >& format) { | 42 bool IsFormatEmpty(const std::vector<std::vector<FormatElement> >& format) { |
42 for (std::vector<std::vector<AddressField> >::const_iterator | 43 for (std::vector<std::vector<FormatElement> >::const_iterator |
43 it = format.begin(); it != format.end(); ++it) { | 44 it = format.begin(); |
| 45 it != format.end(); |
| 46 ++it) { |
44 if (!it->empty()) { | 47 if (!it->empty()) { |
45 return false; | 48 return false; |
46 } | 49 } |
47 } | 50 } |
48 return true; | 51 return true; |
49 } | 52 } |
50 | 53 |
51 TEST(RuleTest, CopyOverwritesRule) { | 54 TEST(RuleTest, CopyOverwritesRule) { |
52 Rule rule; | 55 Rule rule; |
53 ASSERT_TRUE(rule.ParseSerializedRule( | 56 ASSERT_TRUE(rule.ParseSerializedRule( |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 EXPECT_EQ(rule.GetPostalCodeNameMessageId(), | 184 EXPECT_EQ(rule.GetPostalCodeNameMessageId(), |
182 copy.GetPostalCodeNameMessageId()); | 185 copy.GetPostalCodeNameMessageId()); |
183 EXPECT_EQ(rule.GetInvalidPostalCodeMessageId(), | 186 EXPECT_EQ(rule.GetInvalidPostalCodeMessageId(), |
184 copy.GetInvalidPostalCodeMessageId()); | 187 copy.GetInvalidPostalCodeMessageId()); |
185 } | 188 } |
186 | 189 |
187 TEST(RuleTest, ParseFormatWithNewLines) { | 190 TEST(RuleTest, ParseFormatWithNewLines) { |
188 Rule rule; | 191 Rule rule; |
189 ASSERT_TRUE( | 192 ASSERT_TRUE( |
190 rule.ParseSerializedRule("{\"fmt\":\"%O%n%N%n%A%nAX-%Z %C%nÅLAND\"}")); | 193 rule.ParseSerializedRule("{\"fmt\":\"%O%n%N%n%A%nAX-%Z %C%nÅLAND\"}")); |
191 std::vector<std::vector<AddressField> > expected_format; | 194 |
192 expected_format.push_back(std::vector<AddressField>(1, ORGANIZATION)); | 195 std::vector<std::vector<FormatElement> > expected_format; |
193 expected_format.push_back(std::vector<AddressField>(1, RECIPIENT)); | 196 expected_format.push_back( |
194 expected_format.push_back(std::vector<AddressField>(1, STREET_ADDRESS)); | 197 std::vector<FormatElement>(1, FormatElement(ORGANIZATION))); |
195 expected_format.push_back(std::vector<AddressField>(1, POSTAL_CODE)); | 198 expected_format.push_back( |
196 expected_format.back().push_back(LOCALITY); | 199 std::vector<FormatElement>(1, FormatElement(RECIPIENT))); |
197 expected_format.push_back(std::vector<AddressField>()); | 200 expected_format.push_back( |
| 201 std::vector<FormatElement>(1, FormatElement(STREET_ADDRESS))); |
| 202 expected_format.push_back( |
| 203 std::vector<FormatElement>(1, FormatElement("AX-"))); |
| 204 expected_format.back().push_back(FormatElement(POSTAL_CODE)); |
| 205 expected_format.back().push_back(FormatElement(" ")); |
| 206 expected_format.back().push_back(FormatElement(LOCALITY)); |
| 207 expected_format.push_back( |
| 208 std::vector<FormatElement>(1, FormatElement("ÅLAND"))); |
| 209 |
198 EXPECT_EQ(expected_format, rule.GetFormat()); | 210 EXPECT_EQ(expected_format, rule.GetFormat()); |
199 } | 211 } |
200 | 212 |
201 TEST(RuleTest, DoubleTokenPrefixDoesNotCrash) { | 213 TEST(RuleTest, DoubleTokenPrefixDoesNotCrash) { |
202 Rule rule; | 214 Rule rule; |
203 EXPECT_TRUE(rule.ParseSerializedRule("{\"fmt\":\"%%R\"}")); | 215 EXPECT_TRUE(rule.ParseSerializedRule("{\"fmt\":\"%%R\"}")); |
204 } | 216 } |
205 | 217 |
206 TEST(RuleTest, DoubleNewlineFormatDoesNotCrash) { | 218 TEST(RuleTest, DoubleNewlineFormatDoesNotCrash) { |
207 Rule rule; | 219 Rule rule; |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 Rule rule_; | 418 Rule rule_; |
407 }; | 419 }; |
408 | 420 |
409 // Verifies that an address format does not contain consecutive lines with | 421 // Verifies that an address format does not contain consecutive lines with |
410 // multiple fields each. Such address format (e.g. {{ELEMENT, ELEMENT}, | 422 // multiple fields each. Such address format (e.g. {{ELEMENT, ELEMENT}, |
411 // {ELEMENT, ELEMENT}}) will result in incorrect behavior of BuildComponents() | 423 // {ELEMENT, ELEMENT}}) will result in incorrect behavior of BuildComponents() |
412 // public API. | 424 // public API. |
413 TEST_P(RuleParseTest, ConsecutiveLinesWithMultipleFields) { | 425 TEST_P(RuleParseTest, ConsecutiveLinesWithMultipleFields) { |
414 ASSERT_TRUE(rule_.ParseSerializedRule(GetData())); | 426 ASSERT_TRUE(rule_.ParseSerializedRule(GetData())); |
415 bool previous_line_has_single_field = true; | 427 bool previous_line_has_single_field = true; |
416 for (std::vector<std::vector<AddressField> >::const_iterator | 428 for (std::vector<std::vector<FormatElement> >::const_iterator |
417 line_it = rule_.GetFormat().begin(); | 429 line_it = rule_.GetFormat().begin(); |
418 line_it != rule_.GetFormat().end(); | 430 line_it != rule_.GetFormat().end(); |
419 ++line_it) { | 431 ++line_it) { |
420 if (line_it->empty()) { | 432 int num_fields = 0; |
| 433 for (std::vector<FormatElement>::const_iterator |
| 434 element_it = line_it->begin(); |
| 435 element_it != line_it->end(); |
| 436 ++element_it) { |
| 437 if (element_it->IsField()) { |
| 438 ++num_fields; |
| 439 } |
| 440 } |
| 441 if (num_fields == 0) { |
421 continue; | 442 continue; |
422 } | 443 } |
423 ASSERT_TRUE(line_it->size() == 1 || previous_line_has_single_field) | 444 ASSERT_TRUE(num_fields == 1 || previous_line_has_single_field) |
424 << GetParam() << ": " << GetData(); | 445 << GetParam() << ": " << GetData(); |
425 previous_line_has_single_field = line_it->size() == 1; | 446 previous_line_has_single_field = num_fields == 1; |
426 } | 447 } |
427 } | 448 } |
428 | 449 |
| 450 // Verifies that a street line is surrounded by either newlines or spaces. A |
| 451 // different format will result in incorrect behavior in |
| 452 // AddressData::BuildDisplayLines(). |
| 453 TEST_P(RuleParseTest, StreetAddressSurroundingElements) { |
| 454 ASSERT_TRUE(rule_.ParseSerializedRule(GetData())); |
| 455 for (std::vector<std::vector<FormatElement> >::const_iterator |
| 456 line_it = rule_.GetFormat().begin(); |
| 457 line_it != rule_.GetFormat().end(); |
| 458 ++line_it) { |
| 459 for (size_t i = 0; i < line_it->size(); ++i) { |
| 460 const FormatElement& element = line_it->at(i); |
| 461 if (element.IsField() && element.field == STREET_ADDRESS) { |
| 462 bool surrounded_by_newlines = line_it->size() == 1; |
| 463 bool surrounded_by_spaces = |
| 464 i > 0 && |
| 465 i < line_it->size() - 1 && |
| 466 !line_it->at(i - 1).IsField() && |
| 467 line_it->at(i - 1).literal == " " && |
| 468 !line_it->at(i + 1).IsField() && |
| 469 line_it->at(i + 1).literal == " "; |
| 470 EXPECT_TRUE(surrounded_by_newlines || surrounded_by_spaces) |
| 471 << GetParam() << ": " << GetData(); |
| 472 } |
| 473 } |
| 474 } |
| 475 } |
| 476 |
429 // Test parsing all region data. | 477 // Test parsing all region data. |
430 INSTANTIATE_TEST_CASE_P( | 478 INSTANTIATE_TEST_CASE_P( |
431 AllRulesTest, RuleParseTest, | 479 AllRulesTest, RuleParseTest, |
432 testing::ValuesIn(RegionDataConstants::GetRegionCodes())); | 480 testing::ValuesIn(RegionDataConstants::GetRegionCodes())); |
433 | 481 |
434 } // namespace | 482 } // namespace |
OLD | NEW |