1 // Copyright (C) 2013 Google Inc.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <libaddressinput/address_field.h>
23 #include <gtest/gtest.h>
26 #include "region_data_constants.h"
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
) {
51 TEST(RuleTest
, CopyOverwritesRule
) {
53 ASSERT_TRUE(rule
.ParseSerializedRule(
57 "\"state_name_type\":\"area\","
58 "\"zip_name_type\":\"postal\","
59 "\"sub_keys\":\"CA~NY~TX\","
61 "\"languages\":\"en~fr\","
62 "\"zip\":\"\\\\d{5}([ \\\\-]\\\\d{4})?\""
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());
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
) {
92 ASSERT_TRUE(rule
.ParseSerializedRule(
96 "\"state_name_type\":\"area\","
97 "\"zip_name_type\":\"postal\","
98 "\"sub_keys\":\"CA~NY~TX\","
100 "\"languages\":\"en~fr\","
101 "\"zip\":\"\\\\d{5}([ \\\\-]\\\\d{4})?\""
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(
118 "\"state_name_type\":\"do_si\","
119 "\"zip_name_type\":\"zip\","
122 "\"languages\":\"\","
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
) {
139 ASSERT_TRUE(rule
.ParseSerializedRule(
142 "\"require\":\"SZ\","
143 "\"state_name_type\":\"area\","
144 "\"zip_name_type\":\"postal\","
145 "\"sub_keys\":\"CA~NY~TX\","
147 "\"languages\":\"en~fr\","
148 "\"zip\":\"\\\\d{5}([ \\\\-]\\\\d{4})?\""
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
) {
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
) {
183 EXPECT_TRUE(rule
.ParseSerializedRule("{\"fmt\":\"%%R\"}"));
186 TEST(RuleTest
, DoubleNewlineFormatDoesNotCrash
) {
188 EXPECT_TRUE(rule
.ParseSerializedRule("{\"fmt\":\"%n%n\"}"));
191 TEST(RuleTest
, FormatTokenWithoutPrefixDoesNotCrash
) {
193 ASSERT_TRUE(rule
.ParseSerializedRule("{\"fmt\":\"R\"}"));
196 TEST(RuleTest
, ParseDuplicateTokenInFormatDoesNotCrash
) {
198 EXPECT_TRUE(rule
.ParseSerializedRule("{\"fmt\":\"%R%R\"}"));
201 TEST(RuleTest
, ParseInvalidFormatFieldsDoesNotCrash
) {
203 EXPECT_TRUE(rule
.ParseSerializedRule("{\"fmt\":\"%K%L\"}"));
206 TEST(RuleTest
, PrefixWithoutTokenFormatDoesNotCrash
) {
208 EXPECT_TRUE(rule
.ParseSerializedRule("{\"fmt\":\"%\"}"));
211 TEST(RuleTest
, EmptyStringFormatDoesNotCrash
) {
213 EXPECT_TRUE(rule
.ParseSerializedRule("{\"fmt\":\"\"}"));
216 TEST(RuleTest
, ParseRequiredFields
) {
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
) {
230 ASSERT_TRUE(rule
.ParseSerializedRule("{\"require\":\"\"}"));
231 EXPECT_TRUE(rule
.GetRequired().empty());
234 TEST(RuleTest
, ParseInvalidRequiredFields
) {
236 ASSERT_TRUE(rule
.ParseSerializedRule("{\"require\":\"garbage\"}"));
237 EXPECT_TRUE(rule
.GetRequired().empty());
240 TEST(RuleTest
, ParseDuplicateRequiredFields
) {
242 ASSERT_TRUE(rule
.ParseSerializedRule("{\"require\":\"SSS\"}"));
243 EXPECT_EQ(std::vector
<AddressField
>(3, ADMIN_AREA
), rule
.GetRequired());
246 TEST(RuleTest
, ParsesSubKeysCorrectly
) {
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
) {
258 ASSERT_TRUE(rule
.ParseSerializedRule("{\"lang\":\"en\"}"));
259 EXPECT_EQ("en", rule
.GetLanguage());
262 TEST(RuleTest
, ParsesLanguagesCorrectly
) {
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
) {
274 ASSERT_TRUE(rule
.ParseSerializedRule(
276 "\"zip\":\"\\\\d{5}([ \\\\-]\\\\d{4})?\""
278 EXPECT_EQ("\\d{5}([ \\-]\\d{4})?", rule
.GetPostalCodeFormat());
281 TEST(RuleTest
, EmptyStringIsNotValid
) {
283 EXPECT_FALSE(rule
.ParseSerializedRule(std::string()));
286 TEST(RuleTest
, EmptyDictionaryIsValid
) {
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> > {
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
,
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> > {
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
,
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
> {
356 const std::string
& GetData() const {
357 return RegionDataConstants::GetRegionData(GetParam());
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()
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();
374 if (line_it
->empty()) {
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()));