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