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 <libaddressinput/address_ui.h> | 15 #include <libaddressinput/address_ui.h> |
16 | 16 |
17 #include <libaddressinput/address_field.h> | 17 #include <libaddressinput/address_field.h> |
18 #include <libaddressinput/address_ui_component.h> | 18 #include <libaddressinput/address_ui_component.h> |
19 #include <libaddressinput/localization.h> | 19 #include <libaddressinput/localization.h> |
20 | 20 |
| 21 #include <set> |
21 #include <string> | 22 #include <string> |
22 #include <vector> | 23 #include <vector> |
23 | 24 |
24 #include <gtest/gtest.h> | 25 #include <gtest/gtest.h> |
25 | 26 |
26 namespace { | 27 namespace { |
27 | 28 |
28 using i18n::addressinput::AddressField; | 29 using i18n::addressinput::AddressField; |
29 using i18n::addressinput::AddressUiComponent; | 30 using i18n::addressinput::AddressUiComponent; |
30 using i18n::addressinput::BuildComponents; | 31 using i18n::addressinput::BuildComponents; |
31 using i18n::addressinput::COUNTRY; | 32 using i18n::addressinput::COUNTRY; |
| 33 using i18n::addressinput::GetCompactAddressLinesSeparator; |
32 using i18n::addressinput::GetRegionCodes; | 34 using i18n::addressinput::GetRegionCodes; |
33 using i18n::addressinput::Localization; | 35 using i18n::addressinput::Localization; |
34 using i18n::addressinput::RECIPIENT; | 36 using i18n::addressinput::RECIPIENT; |
35 | 37 |
36 // Returns testing::AssertionSuccess if the |components| are valid. Uses | 38 // Returns testing::AssertionSuccess if the |components| are valid. |
37 // |region_code| in test failure messages. | |
38 testing::AssertionResult ComponentsAreValid( | 39 testing::AssertionResult ComponentsAreValid( |
39 const std::vector<AddressUiComponent>& components) { | 40 const std::vector<AddressUiComponent>& components) { |
40 if (components.empty()) { | 41 if (components.empty()) { |
41 return testing::AssertionFailure() << "no components"; | 42 return testing::AssertionFailure() << "no components"; |
42 } | 43 } |
43 | 44 |
| 45 std::set<AddressField> fields; |
44 for (std::vector<AddressUiComponent>::const_iterator | 46 for (std::vector<AddressUiComponent>::const_iterator |
45 component_it = components.begin(); | 47 component_it = components.begin(); |
46 component_it != components.end(); ++component_it) { | 48 component_it != components.end(); |
| 49 ++component_it) { |
47 static const AddressField kMinAddressField = COUNTRY; | 50 static const AddressField kMinAddressField = COUNTRY; |
48 static const AddressField kMaxAddressField = RECIPIENT; | 51 static const AddressField kMaxAddressField = RECIPIENT; |
49 if (component_it->field < kMinAddressField || | 52 if (component_it->field < kMinAddressField || |
50 component_it->field > kMaxAddressField) { | 53 component_it->field > kMaxAddressField) { |
51 return testing::AssertionFailure() << "unexpected field " | 54 return testing::AssertionFailure() << "unexpected input field " |
52 << component_it->field; | 55 << component_it->field; |
53 } | 56 } |
54 | 57 |
| 58 if (fields.find(component_it->field) != fields.end()) { |
| 59 return testing::AssertionFailure() << "duplicate input field " |
| 60 << component_it->field; |
| 61 } |
| 62 fields.insert(component_it->field); |
| 63 |
55 if (component_it->name.empty()) { | 64 if (component_it->name.empty()) { |
56 return testing::AssertionFailure() << "empty field name for field " | 65 return testing::AssertionFailure() << "empty name for input field " |
57 << component_it->field; | 66 << component_it->field; |
58 } | 67 } |
59 } | 68 } |
60 | 69 |
61 return testing::AssertionSuccess(); | 70 return testing::AssertionSuccess(); |
62 } | 71 } |
63 | 72 |
64 // Tests for address UI functions. | 73 // Tests for address UI functions. |
65 class AddressUiTest : public testing::TestWithParam<std::string> { | 74 class AddressUiTest : public testing::TestWithParam<std::string> { |
66 protected: | 75 protected: |
(...skipping 15 matching lines...) Expand all Loading... |
82 INSTANTIATE_TEST_CASE_P( | 91 INSTANTIATE_TEST_CASE_P( |
83 AllRegions, AddressUiTest, | 92 AllRegions, AddressUiTest, |
84 testing::ValuesIn(GetRegionCodes())); | 93 testing::ValuesIn(GetRegionCodes())); |
85 | 94 |
86 // Verifies that BuildComponents() returns an empty vector for an invalid region | 95 // Verifies that BuildComponents() returns an empty vector for an invalid region |
87 // code. | 96 // code. |
88 TEST_F(AddressUiTest, InvalidRegionCodeReturnsEmptyVector) { | 97 TEST_F(AddressUiTest, InvalidRegionCodeReturnsEmptyVector) { |
89 EXPECT_TRUE(BuildComponents("INVALID-REGION-CODE", localization_).empty()); | 98 EXPECT_TRUE(BuildComponents("INVALID-REGION-CODE", localization_).empty()); |
90 } | 99 } |
91 | 100 |
| 101 struct SeparatorData { |
| 102 SeparatorData(const std::string& language_code, |
| 103 const std::string& country_code, |
| 104 const std::string& compact_line_separator) |
| 105 : language_code(language_code), |
| 106 country_code(country_code), |
| 107 compact_line_separator(compact_line_separator) {} |
| 108 |
| 109 ~SeparatorData() {} |
| 110 |
| 111 std::string language_code; |
| 112 std::string country_code; |
| 113 std::string compact_line_separator; |
| 114 }; |
| 115 |
| 116 // Tests for compact line separator. |
| 117 class CompactLineSeparatorTest |
| 118 : public testing::TestWithParam<SeparatorData> {}; |
| 119 |
| 120 TEST_P(CompactLineSeparatorTest, BasicTest) { |
| 121 EXPECT_EQ(GetParam().compact_line_separator, |
| 122 GetCompactAddressLinesSeparator(GetParam().language_code, |
| 123 GetParam().country_code)); |
| 124 } |
| 125 |
| 126 INSTANTIATE_TEST_CASE_P( |
| 127 CompactLineSeparators, CompactLineSeparatorTest, |
| 128 testing::Values( |
| 129 SeparatorData("", "AD", ", "), |
| 130 SeparatorData("", "XX", ", "), |
| 131 SeparatorData("ja", "JP", ""), |
| 132 SeparatorData("zh", "HK", ""), |
| 133 SeparatorData("zh-hans", "CN", ""), |
| 134 SeparatorData("ar", "YE", "، "), |
| 135 SeparatorData("ko", "KR", " "), |
| 136 SeparatorData("th", "TH", " "), |
| 137 SeparatorData("en", "US", ", "))); |
| 138 |
| 139 |
92 } // namespace | 140 } // namespace |
OLD | NEW |