[rac] Parse postal code formats and required fields in libaddressinput.
[chromium-blink-merge.git] / third_party / libaddressinput / chromium / cpp / test / rule_test.cc
blobb484555bcdb94364b721db134eb1ab21ecf9620b
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.
15 #include "rule.h"
17 #include <libaddressinput/address_field.h>
19 #include <string>
20 #include <utility>
21 #include <vector>
23 #include <gtest/gtest.h>
25 #include "messages.h"
26 #include "region_data_constants.h"
28 namespace {
30 using i18n::addressinput::AddressField;
31 using i18n::addressinput::ADMIN_AREA;
32 using i18n::addressinput::COUNTRY;
33 using i18n::addressinput::LOCALITY;
34 using i18n::addressinput::ORGANIZATION;
35 using i18n::addressinput::POSTAL_CODE;
36 using i18n::addressinput::RECIPIENT;
37 using i18n::addressinput::RegionDataConstants;
38 using i18n::addressinput::Rule;
39 using i18n::addressinput::STREET_ADDRESS;
41 bool IsFormatEmpty(const std::vector<std::vector<AddressField> >& format) {
42 for (std::vector<std::vector<AddressField> >::const_iterator
43 it = format.begin(); it != format.end(); ++it) {
44 if (!it->empty()) {
45 return false;
48 return true;
51 TEST(RuleTest, CopyOverwritesRule) {
52 Rule rule;
53 ASSERT_TRUE(rule.ParseSerializedRule(
54 "{"
55 "\"fmt\":\"%S%Z\","
56 "\"require\":\"SZ\","
57 "\"state_name_type\":\"area\","
58 "\"zip_name_type\":\"postal\","
59 "\"sub_keys\":\"CA~NY~TX\","
60 "\"lang\":\"en\","
61 "\"languages\":\"en~fr\","
62 "\"zip\":\"\\\\d{5}([ \\\\-]\\\\d{4})?\""
63 "}"));
65 Rule copy;
66 EXPECT_NE(rule.GetFormat(), copy.GetFormat());
67 EXPECT_NE(rule.GetRequired(), copy.GetRequired());
68 EXPECT_NE(rule.GetSubKeys(), copy.GetSubKeys());
69 EXPECT_NE(rule.GetLanguages(), copy.GetLanguages());
70 EXPECT_NE(rule.GetLanguage(), copy.GetLanguage());
71 EXPECT_NE(rule.GetPostalCodeFormat(), copy.GetPostalCodeFormat());
72 EXPECT_NE(rule.GetAdminAreaNameMessageId(),
73 copy.GetAdminAreaNameMessageId());
74 EXPECT_NE(rule.GetPostalCodeNameMessageId(),
75 copy.GetPostalCodeNameMessageId());
77 copy.CopyFrom(rule);
78 EXPECT_EQ(rule.GetFormat(), copy.GetFormat());
79 EXPECT_EQ(rule.GetRequired(), copy.GetRequired());
80 EXPECT_EQ(rule.GetSubKeys(), copy.GetSubKeys());
81 EXPECT_EQ(rule.GetLanguages(), copy.GetLanguages());
82 EXPECT_EQ(rule.GetLanguage(), copy.GetLanguage());
83 EXPECT_EQ(rule.GetPostalCodeFormat(), copy.GetPostalCodeFormat());
84 EXPECT_EQ(rule.GetAdminAreaNameMessageId(),
85 copy.GetAdminAreaNameMessageId());
86 EXPECT_EQ(rule.GetPostalCodeNameMessageId(),
87 copy.GetPostalCodeNameMessageId());
90 TEST(RuleTest, ParseOverwritesRule) {
91 Rule rule;
92 ASSERT_TRUE(rule.ParseSerializedRule(
93 "{"
94 "\"fmt\":\"%S%Z\","
95 "\"require\":\"SZ\","
96 "\"state_name_type\":\"area\","
97 "\"zip_name_type\":\"postal\","
98 "\"sub_keys\":\"CA~NY~TX\","
99 "\"lang\":\"en\","
100 "\"languages\":\"en~fr\","
101 "\"zip\":\"\\\\d{5}([ \\\\-]\\\\d{4})?\""
102 "}"));
103 EXPECT_FALSE(IsFormatEmpty(rule.GetFormat()));
104 EXPECT_FALSE(rule.GetRequired().empty());
105 EXPECT_FALSE(rule.GetSubKeys().empty());
106 EXPECT_FALSE(rule.GetLanguages().empty());
107 EXPECT_FALSE(rule.GetLanguage().empty());
108 EXPECT_FALSE(rule.GetPostalCodeFormat().empty());
109 EXPECT_EQ(IDS_LIBADDRESSINPUT_I18N_AREA,
110 rule.GetAdminAreaNameMessageId());
111 EXPECT_EQ(IDS_LIBADDRESSINPUT_I18N_POSTAL_CODE_LABEL,
112 rule.GetPostalCodeNameMessageId());
114 ASSERT_TRUE(rule.ParseSerializedRule(
116 "\"fmt\":\"\","
117 "\"require\":\"\","
118 "\"state_name_type\":\"do_si\","
119 "\"zip_name_type\":\"zip\","
120 "\"sub_keys\":\"\","
121 "\"lang\":\"\","
122 "\"languages\":\"\","
123 "\"zip\":\"\""
124 "}"));
125 EXPECT_TRUE(IsFormatEmpty(rule.GetFormat()));
126 EXPECT_TRUE(rule.GetRequired().empty());
127 EXPECT_TRUE(rule.GetSubKeys().empty());
128 EXPECT_TRUE(rule.GetLanguages().empty());
129 EXPECT_TRUE(rule.GetLanguage().empty());
130 EXPECT_TRUE(rule.GetPostalCodeFormat().empty());
131 EXPECT_EQ(IDS_LIBADDRESSINPUT_I18N_DO_SI,
132 rule.GetAdminAreaNameMessageId());
133 EXPECT_EQ(IDS_LIBADDRESSINPUT_I18N_ZIP_CODE_LABEL,
134 rule.GetPostalCodeNameMessageId());
137 TEST(RuleTest, ParseEmptyDataDoesNotOverwriteRule) {
138 Rule rule;
139 ASSERT_TRUE(rule.ParseSerializedRule(
141 "\"fmt\":\"%S%Z\","
142 "\"require\":\"SZ\","
143 "\"state_name_type\":\"area\","
144 "\"zip_name_type\":\"postal\","
145 "\"sub_keys\":\"CA~NY~TX\","
146 "\"lang\":\"en\","
147 "\"languages\":\"en~fr\","
148 "\"zip\":\"\\\\d{5}([ \\\\-]\\\\d{4})?\""
149 "}"));
151 Rule copy;
152 copy.CopyFrom(rule);
153 ASSERT_TRUE(copy.ParseSerializedRule("{}"));
155 EXPECT_EQ(rule.GetFormat(), copy.GetFormat());
156 EXPECT_EQ(rule.GetRequired(), copy.GetRequired());
157 EXPECT_EQ(rule.GetSubKeys(), copy.GetSubKeys());
158 EXPECT_EQ(rule.GetLanguages(), copy.GetLanguages());
159 EXPECT_EQ(rule.GetLanguage(), copy.GetLanguage());
160 EXPECT_EQ(rule.GetPostalCodeFormat(), copy.GetPostalCodeFormat());
161 EXPECT_EQ(rule.GetAdminAreaNameMessageId(),
162 copy.GetAdminAreaNameMessageId());
163 EXPECT_EQ(rule.GetPostalCodeNameMessageId(),
164 copy.GetPostalCodeNameMessageId());
167 TEST(RuleTest, ParseFormatWithNewLines) {
168 Rule rule;
169 ASSERT_TRUE(
170 rule.ParseSerializedRule("{\"fmt\":\"%O%n%N%n%A%nAX-%Z %C%nÃ…LAND\"}"));
171 std::vector<std::vector<AddressField> > expected_format;
172 expected_format.push_back(std::vector<AddressField>(1, ORGANIZATION));
173 expected_format.push_back(std::vector<AddressField>(1, RECIPIENT));
174 expected_format.push_back(std::vector<AddressField>(1, STREET_ADDRESS));
175 expected_format.push_back(std::vector<AddressField>(1, POSTAL_CODE));
176 expected_format.back().push_back(LOCALITY);
177 expected_format.push_back(std::vector<AddressField>());
178 EXPECT_EQ(expected_format, rule.GetFormat());
181 TEST(RuleTest, DoubleTokenPrefixDoesNotCrash) {
182 Rule rule;
183 EXPECT_TRUE(rule.ParseSerializedRule("{\"fmt\":\"%%R\"}"));
186 TEST(RuleTest, DoubleNewlineFormatDoesNotCrash) {
187 Rule rule;
188 EXPECT_TRUE(rule.ParseSerializedRule("{\"fmt\":\"%n%n\"}"));
191 TEST(RuleTest, FormatTokenWithoutPrefixDoesNotCrash) {
192 Rule rule;
193 ASSERT_TRUE(rule.ParseSerializedRule("{\"fmt\":\"R\"}"));
196 TEST(RuleTest, ParseDuplicateTokenInFormatDoesNotCrash) {
197 Rule rule;
198 EXPECT_TRUE(rule.ParseSerializedRule("{\"fmt\":\"%R%R\"}"));
201 TEST(RuleTest, ParseInvalidFormatFieldsDoesNotCrash) {
202 Rule rule;
203 EXPECT_TRUE(rule.ParseSerializedRule("{\"fmt\":\"%K%L\"}"));
206 TEST(RuleTest, PrefixWithoutTokenFormatDoesNotCrash) {
207 Rule rule;
208 EXPECT_TRUE(rule.ParseSerializedRule("{\"fmt\":\"%\"}"));
211 TEST(RuleTest, EmptyStringFormatDoesNotCrash) {
212 Rule rule;
213 EXPECT_TRUE(rule.ParseSerializedRule("{\"fmt\":\"\"}"));
216 TEST(RuleTest, ParseRequiredFields) {
217 Rule rule;
218 ASSERT_TRUE(rule.ParseSerializedRule("{\"require\":\"ONAZC\"}"));
219 std::vector<AddressField> expected;
220 expected.push_back(ORGANIZATION);
221 expected.push_back(RECIPIENT);
222 expected.push_back(STREET_ADDRESS);
223 expected.push_back(POSTAL_CODE);
224 expected.push_back(LOCALITY);
225 EXPECT_EQ(expected, rule.GetRequired());
228 TEST(RuleTest, ParseEmptyStringRequiredFields) {
229 Rule rule;
230 ASSERT_TRUE(rule.ParseSerializedRule("{\"require\":\"\"}"));
231 EXPECT_TRUE(rule.GetRequired().empty());
234 TEST(RuleTest, ParseInvalidRequiredFields) {
235 Rule rule;
236 ASSERT_TRUE(rule.ParseSerializedRule("{\"require\":\"garbage\"}"));
237 EXPECT_TRUE(rule.GetRequired().empty());
240 TEST(RuleTest, ParseDuplicateRequiredFields) {
241 Rule rule;
242 ASSERT_TRUE(rule.ParseSerializedRule("{\"require\":\"SSS\"}"));
243 EXPECT_EQ(std::vector<AddressField>(3, ADMIN_AREA), rule.GetRequired());
246 TEST(RuleTest, ParsesSubKeysCorrectly) {
247 Rule rule;
248 ASSERT_TRUE(rule.ParseSerializedRule("{\"sub_keys\":\"CA~NY~TX\"}"));
249 std::vector<std::string> expected;
250 expected.push_back("CA");
251 expected.push_back("NY");
252 expected.push_back("TX");
253 EXPECT_EQ(expected, rule.GetSubKeys());
256 TEST(RuleTest, ParsesLanguageCorrectly) {
257 Rule rule;
258 ASSERT_TRUE(rule.ParseSerializedRule("{\"lang\":\"en\"}"));
259 EXPECT_EQ("en", rule.GetLanguage());
262 TEST(RuleTest, ParsesLanguagesCorrectly) {
263 Rule rule;
264 ASSERT_TRUE(rule.ParseSerializedRule("{\"languages\":\"de~fr~it\"}"));
265 std::vector<std::string> expected;
266 expected.push_back("de");
267 expected.push_back("fr");
268 expected.push_back("it");
269 EXPECT_EQ(expected, rule.GetLanguages());
272 TEST(RuleTest, ParsesPostalCodeFormatCorrectly) {
273 Rule rule;
274 ASSERT_TRUE(rule.ParseSerializedRule(
276 "\"zip\":\"\\\\d{5}([ \\\\-]\\\\d{4})?\""
277 "}"));
278 EXPECT_EQ("\\d{5}([ \\-]\\d{4})?", rule.GetPostalCodeFormat());
281 TEST(RuleTest, EmptyStringIsNotValid) {
282 Rule rule;
283 EXPECT_FALSE(rule.ParseSerializedRule(std::string()));
286 TEST(RuleTest, EmptyDictionaryIsValid) {
287 Rule rule;
288 EXPECT_TRUE(rule.ParseSerializedRule("{}"));
291 // Tests for parsing the postal code name.
292 class PostalCodeNameParseTest
293 : public testing::TestWithParam<std::pair<std::string, int> > {
294 protected:
295 Rule rule_;
298 // Verifies that a postal code name is parsed correctly.
299 TEST_P(PostalCodeNameParseTest, ParsedCorrectly) {
300 ASSERT_TRUE(rule_.ParseSerializedRule(GetParam().first));
301 EXPECT_EQ(GetParam().second, rule_.GetPostalCodeNameMessageId());
304 // Test parsing all postal code names.
305 INSTANTIATE_TEST_CASE_P(
306 AllPostalCodeNames, PostalCodeNameParseTest,
307 testing::Values(
308 std::make_pair("{\"zip_name_type\":\"postal\"}",
309 IDS_LIBADDRESSINPUT_I18N_POSTAL_CODE_LABEL),
310 std::make_pair("{\"zip_name_type\":\"zip\"}",
311 IDS_LIBADDRESSINPUT_I18N_ZIP_CODE_LABEL)));
313 // Tests for parsing the administrative area name.
314 class AdminAreaNameParseTest
315 : public testing::TestWithParam<std::pair<std::string, int> > {
316 protected:
317 Rule rule_;
320 // Verifies that an administrative area name is parsed correctly.
321 TEST_P(AdminAreaNameParseTest, ParsedCorrectly) {
322 ASSERT_TRUE(rule_.ParseSerializedRule(GetParam().first));
323 EXPECT_EQ(GetParam().second, rule_.GetAdminAreaNameMessageId());
326 // Test parsing all administrative area names.
327 INSTANTIATE_TEST_CASE_P(
328 AllAdminAreaNames, AdminAreaNameParseTest,
329 testing::Values(
330 std::make_pair("{\"state_name_type\":\"area\"}",
331 IDS_LIBADDRESSINPUT_I18N_AREA),
332 std::make_pair("{\"state_name_type\":\"county\"}",
333 IDS_LIBADDRESSINPUT_I18N_COUNTY_LABEL),
334 std::make_pair("{\"state_name_type\":\"department\"}",
335 IDS_LIBADDRESSINPUT_I18N_DEPARTMENT),
336 std::make_pair("{\"state_name_type\":\"district\"}",
337 IDS_LIBADDRESSINPUT_I18N_DEPENDENT_LOCALITY_LABEL),
338 std::make_pair("{\"state_name_type\":\"do_si\"}",
339 IDS_LIBADDRESSINPUT_I18N_DO_SI),
340 std::make_pair("{\"state_name_type\":\"emirate\"}",
341 IDS_LIBADDRESSINPUT_I18N_EMIRATE),
342 std::make_pair("{\"state_name_type\":\"island\"}",
343 IDS_LIBADDRESSINPUT_I18N_ISLAND),
344 std::make_pair("{\"state_name_type\":\"parish\"}",
345 IDS_LIBADDRESSINPUT_I18N_PARISH),
346 std::make_pair("{\"state_name_type\":\"prefecture\"}",
347 IDS_LIBADDRESSINPUT_I18N_PREFECTURE),
348 std::make_pair("{\"state_name_type\":\"province\"}",
349 IDS_LIBADDRESSINPUT_I18N_PROVINCE),
350 std::make_pair("{\"state_name_type\":\"state\"}",
351 IDS_LIBADDRESSINPUT_I18N_STATE_LABEL)));
353 // Tests for rule parsing.
354 class RuleParseTest : public testing::TestWithParam<std::string> {
355 protected:
356 const std::string& GetData() const {
357 return RegionDataConstants::GetRegionData(GetParam());
360 Rule rule_;
363 // Verifies that an address format does not contain consecutive lines with
364 // multiple fields each. Such address format (e.g. {{ELEMENT, ELEMENT},
365 // {ELEMENT, ELEMENT}}) will result in incorrect behavior of BuildComponents()
366 // public API.
367 TEST_P(RuleParseTest, ConsecutiveLinesWithMultipleFields) {
368 ASSERT_TRUE(rule_.ParseSerializedRule(GetData()));
369 bool previous_line_has_single_field = true;
370 for (std::vector<std::vector<AddressField> >::const_iterator
371 line_it = rule_.GetFormat().begin();
372 line_it != rule_.GetFormat().end();
373 ++line_it) {
374 if (line_it->empty()) {
375 continue;
377 ASSERT_TRUE(line_it->size() == 1 || previous_line_has_single_field)
378 << GetParam() << ": " << GetData();
379 previous_line_has_single_field = line_it->size() == 1;
383 // Test parsing all region data.
384 INSTANTIATE_TEST_CASE_P(
385 AllRulesTest, RuleParseTest,
386 testing::ValuesIn(RegionDataConstants::GetRegionCodes()));
388 } // namespace