| OLD | NEW |
| (Empty) |
| 1 // Copyright (C) 2014 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. | |
| 14 | |
| 15 #include <libaddressinput/address_data.h> | |
| 16 #include <libaddressinput/address_field.h> | |
| 17 #include <libaddressinput/address_problem.h> | |
| 18 #include <libaddressinput/address_validator.h> | |
| 19 #include <libaddressinput/callback.h> | |
| 20 #include <libaddressinput/downloader.h> | |
| 21 #include <libaddressinput/load_rules_delegate.h> | |
| 22 #include <libaddressinput/storage.h> | |
| 23 #include <libaddressinput/util/scoped_ptr.h> | |
| 24 | |
| 25 #include <algorithm> | |
| 26 #include <sstream> | |
| 27 #include <string> | |
| 28 | |
| 29 #include <gtest/gtest.h> | |
| 30 | |
| 31 #include "address_validator_test.h" | |
| 32 #include "fake_downloader.h" | |
| 33 #include "fake_storage.h" | |
| 34 #include "util/json.h" | |
| 35 #include "util/string_util.h" | |
| 36 | |
| 37 namespace i18n { | |
| 38 namespace addressinput { | |
| 39 | |
| 40 namespace { | |
| 41 class AddressProblemEqualsString | |
| 42 : public std::binary_function<AddressProblem, std::string, bool> { | |
| 43 public: | |
| 44 bool operator()(const AddressProblem& ap, const std::string& ep) const { | |
| 45 std::ostringstream oss; | |
| 46 oss << ap; | |
| 47 return oss.str() == ep; | |
| 48 } | |
| 49 }; | |
| 50 | |
| 51 } // namespace | |
| 52 | |
| 53 class ExampleAddressValidatorTest | |
| 54 : public testing::Test, public LoadRulesDelegate { | |
| 55 public: | |
| 56 ExampleAddressValidatorTest() {} | |
| 57 | |
| 58 virtual ~ExampleAddressValidatorTest() {} | |
| 59 | |
| 60 // testing::Test overrides. | |
| 61 virtual void SetUp() OVERRIDE { | |
| 62 downloader_.reset(new FakeDownloader); | |
| 63 | |
| 64 validator_ = BuildAddressValidatorForTesting( | |
| 65 FakeDownloader::kFakeDataUrl, | |
| 66 scoped_ptr<Downloader>(new FakeDownloader), | |
| 67 scoped_ptr<Storage>(new FakeStorage), | |
| 68 this); | |
| 69 } | |
| 70 | |
| 71 void OnDownloaded(bool success, | |
| 72 const std::string& url, | |
| 73 scoped_ptr<std::string> downloaded_data) { | |
| 74 EXPECT_TRUE(success); | |
| 75 EXPECT_FALSE(downloaded_data->empty()); | |
| 76 data_ = downloaded_data.Pass(); | |
| 77 } | |
| 78 | |
| 79 protected: | |
| 80 scoped_ptr<Downloader> downloader_; | |
| 81 scoped_ptr<AddressValidator> validator_; | |
| 82 scoped_ptr<std::string> data_; | |
| 83 | |
| 84 void TestCountryType(const scoped_ptr<Json>& json, | |
| 85 const std::string& country, | |
| 86 const std::string& type) { | |
| 87 scoped_ptr<Json> default_json; | |
| 88 | |
| 89 std::string default_key = "examples/" + country + "/" + type + "/_default"; | |
| 90 | |
| 91 ASSERT_TRUE(json->GetJsonValueForKey(default_key, &default_json)); | |
| 92 | |
| 93 scoped_ptr<Json> fields_json; | |
| 94 ASSERT_TRUE(default_json->GetJsonValueForKey( | |
| 95 "fields", &fields_json)); | |
| 96 | |
| 97 AddressData address; | |
| 98 for (int i = 1; i < 10; ++i) { | |
| 99 std::string street_key = "street"; | |
| 100 street_key.append(1, static_cast<char>('0' + i)); | |
| 101 std::string street_field; | |
| 102 if (!fields_json->GetStringValueForKey(street_key, &street_field)) | |
| 103 break; | |
| 104 | |
| 105 address.address_line.push_back(street_field); | |
| 106 } | |
| 107 address.region_code = country; | |
| 108 fields_json->GetStringValueForKey("state", &address.administrative_area); | |
| 109 fields_json->GetStringValueForKey("city", &address.locality); | |
| 110 fields_json->GetStringValueForKey("locality", &address.dependent_locality); | |
| 111 fields_json->GetStringValueForKey("sorting_code", &address.sorting_code); | |
| 112 fields_json->GetStringValueForKey("zip", &address.postal_code); | |
| 113 fields_json->GetStringValueForKey("name", &address.recipient); | |
| 114 | |
| 115 AddressProblems problems; | |
| 116 EXPECT_EQ(AddressValidator::SUCCESS, validator_->ValidateAddress( | |
| 117 address, AddressProblemFilter(), &problems)); | |
| 118 | |
| 119 std::string expected_problems_str; | |
| 120 std::vector<std::string> expected_problems; | |
| 121 if (default_json->GetStringValueForKey( | |
| 122 "problems", &expected_problems_str)) { | |
| 123 SplitString(expected_problems_str, '~', &expected_problems); | |
| 124 } | |
| 125 if (expected_problems.size() == problems.size()) { | |
| 126 EXPECT_TRUE(equal(problems.begin(), problems.end(), | |
| 127 expected_problems.begin(), | |
| 128 AddressProblemEqualsString())); | |
| 129 } else { | |
| 130 EXPECT_EQ(expected_problems.size(), problems.size()); | |
| 131 for (AddressProblems::const_iterator it = problems.begin(); | |
| 132 it != problems.end(); ++it) { | |
| 133 ADD_FAILURE() << "problem for " << default_key << ':' << *it; | |
| 134 } | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 void TestCountry(const std::string& country) { | |
| 139 validator_->LoadRules(country); | |
| 140 std::string key = "examples/" + country; | |
| 141 std::string url = FakeDownloader::kFakeDataUrl + key; | |
| 142 scoped_ptr<Json> json(Json::Build()); | |
| 143 scoped_ptr<Json> json_country; | |
| 144 DownloadJsonValueForKey(key, &json, &json_country); | |
| 145 | |
| 146 std::string types_str; | |
| 147 ASSERT_TRUE(json_country->GetStringValueForKey("types", &types_str)); | |
| 148 std::vector<std::string> types; | |
| 149 SplitString(types_str, '~', &types); | |
| 150 | |
| 151 for (std::vector<std::string>::const_iterator it = types.begin(), | |
| 152 itend = types.end(); | |
| 153 it != itend; ++it) { | |
| 154 TestCountryType(json, country, *it); | |
| 155 } | |
| 156 } | |
| 157 | |
| 158 std::string DownloadString(const std::string& url) { | |
| 159 data_.reset(); | |
| 160 downloader_->Download( | |
| 161 url, | |
| 162 BuildScopedPtrCallback(dynamic_cast<ExampleAddressValidatorTest*>(this), | |
| 163 &ExampleAddressValidatorTest::OnDownloaded)); | |
| 164 return *data_; | |
| 165 } | |
| 166 | |
| 167 void DownloadJson(const std::string& key, scoped_ptr<Json>* json) { | |
| 168 std::string url = FakeDownloader::kFakeDataUrl + key; | |
| 169 ASSERT_TRUE((*json)->ParseObject(DownloadString(url))); | |
| 170 } | |
| 171 | |
| 172 void DownloadJsonValueForKey(const std::string& key, | |
| 173 scoped_ptr<Json>* json, | |
| 174 scoped_ptr<Json>* newjson) { | |
| 175 DownloadJson(key, json); | |
| 176 ASSERT_TRUE((*json)->GetJsonValueForKey(key, newjson)); | |
| 177 } | |
| 178 | |
| 179 private: | |
| 180 // LoadRulesDelegate implementation. | |
| 181 virtual void OnAddressValidationRulesLoaded(const std::string& country_code, | |
| 182 bool success) { | |
| 183 AddressData address_data; | |
| 184 address_data.region_code = country_code; | |
| 185 AddressValidator::Status status = | |
| 186 validator_->ValidateAddress(address_data, AddressProblemFilter(), NULL); | |
| 187 EXPECT_TRUE(success); | |
| 188 EXPECT_EQ(AddressValidator::SUCCESS, status); | |
| 189 } | |
| 190 }; | |
| 191 | |
| 192 TEST_F(ExampleAddressValidatorTest, examples) { | |
| 193 scoped_ptr<Json> json(Json::Build()); | |
| 194 scoped_ptr<Json> json_examples; | |
| 195 DownloadJsonValueForKey("examples", &json, &json_examples); | |
| 196 | |
| 197 std::string countries_str; | |
| 198 ASSERT_TRUE(json_examples->GetStringValueForKey("countries", &countries_str)); | |
| 199 std::vector<std::string> countries; | |
| 200 SplitString(countries_str, '~', &countries); | |
| 201 | |
| 202 for (std::vector<std::string>::const_iterator it = countries.begin(); | |
| 203 it != countries.end(); ++it) { | |
| 204 TestCountry(*it); | |
| 205 } | |
| 206 } | |
| 207 | |
| 208 } // addressinput | |
| 209 } // i18n | |
| OLD | NEW |