diff options
author | scr@chromium.org <scr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-04 17:52:01 +0000 |
---|---|---|
committer | scr@chromium.org <scr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-04 17:52:01 +0000 |
commit | a3ba262317957993706f7f1f8328f137501a4d31 (patch) | |
tree | 0688a4f3360804530a7ec1c741c535a0f06f3cd3 /third_party | |
parent | e8fba1724fc36e1fd5c9f81ddefb1bfead235b70 (diff) | |
download | chromium_src-a3ba262317957993706f7f1f8328f137501a4d31.zip chromium_src-a3ba262317957993706f7f1f8328f137501a4d31.tar.gz chromium_src-a3ba262317957993706f7f1f8328f137501a4d31.tar.bz2 |
Changes to verify all addresses in examples from countryinfo.txt.
BUG=
Review URL: https://codereview.chromium.org/181593007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@254781 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'third_party')
5 files changed, 267 insertions, 18 deletions
diff --git a/third_party/libaddressinput/chromium/cpp/test/address_validator_test.cc b/third_party/libaddressinput/chromium/cpp/test/address_validator_test.cc index 59c8eab..9d742fc 100644 --- a/third_party/libaddressinput/chromium/cpp/test/address_validator_test.cc +++ b/third_party/libaddressinput/chromium/cpp/test/address_validator_test.cc @@ -12,11 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include <libaddressinput/address_validator.h> +#include "address_validator_test.h" #include <libaddressinput/address_data.h> #include <libaddressinput/address_field.h> #include <libaddressinput/address_problem.h> +#include <libaddressinput/address_validator.h> #include <libaddressinput/downloader.h> #include <libaddressinput/load_rules_delegate.h> #include <libaddressinput/storage.h> @@ -36,12 +37,6 @@ namespace i18n { namespace addressinput { -scoped_ptr<AddressValidator> BuildAddressValidatorForTesting( - const std::string& validation_data_url, - scoped_ptr<Downloader> downloader, - scoped_ptr<Storage> storage, - LoadRulesDelegate* load_rules_delegate); - class AddressValidatorTest : public testing::TestWithParam<std::string>, public LoadRulesDelegate { public: diff --git a/third_party/libaddressinput/chromium/cpp/test/address_validator_test.h b/third_party/libaddressinput/chromium/cpp/test/address_validator_test.h new file mode 100644 index 0000000..d9b960c --- /dev/null +++ b/third_party/libaddressinput/chromium/cpp/test/address_validator_test.h @@ -0,0 +1,40 @@ +// Copyright (C) 2014 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef I18N_LIBADDRESSINPUT_TEST_ADDRESS_VALIDATOR_TEST_H_ +#define I18N_LIBADDRESSINPUT_TEST_ADDRESS_VALIDATOR_TEST_H_ + +#include <string> + +#include <libaddressinput/util/scoped_ptr.h> + +namespace i18n { +namespace addressinput { + +class AddressValidator; +class Downloader; +class LoadRulesDelegate; +class Storage; + +// Provided by address_validator.cc +extern scoped_ptr<AddressValidator> BuildAddressValidatorForTesting( + const std::string& validation_data_url, + scoped_ptr<Downloader> downloader, + scoped_ptr<Storage> storage, + LoadRulesDelegate* load_rules_delegate); + +} // addressinput +} // i18n + +#endif // I18N_LIBADDRESSINPUT_TEST_ADDRESS_VALIDATOR_TEST_H_ diff --git a/third_party/libaddressinput/chromium/cpp/test/countryinfo_example_addresses_test.cc b/third_party/libaddressinput/chromium/cpp/test/countryinfo_example_addresses_test.cc new file mode 100644 index 0000000..32a2deb --- /dev/null +++ b/third_party/libaddressinput/chromium/cpp/test/countryinfo_example_addresses_test.cc @@ -0,0 +1,210 @@ +// Copyright (C) 2014 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include <libaddressinput/address_data.h> +#include <libaddressinput/address_field.h> +#include <libaddressinput/address_problem.h> +#include <libaddressinput/address_validator.h> +#include <libaddressinput/callback.h> +#include <libaddressinput/downloader.h> +#include <libaddressinput/load_rules_delegate.h> +#include <libaddressinput/storage.h> +#include <libaddressinput/util/scoped_ptr.h> + +#include <algorithm> +#include <sstream> +#include <string> + +#include <gtest/gtest.h> + +#include "address_validator_test.h" +#include "fake_downloader.h" +#include "fake_storage.h" +#include "util/json.h" +#include "util/string_split.h" + +namespace i18n { +namespace addressinput { + +namespace { +class AddressProblemEqualsString + : public std::binary_function<AddressProblem, std::string, bool> { + public: + bool operator()(const AddressProblem& ap, const std::string& ep) const { + std::ostringstream oss; + oss << ap; + return oss.str() == ep; + } +}; + +} // namespace + +class ExampleAddressValidatorTest + : public testing::Test, public LoadRulesDelegate { + public: + ExampleAddressValidatorTest() {} + + virtual ~ExampleAddressValidatorTest() {} + + // testing::Test overrides. + virtual void SetUp() OVERRIDE { + downloader_.reset(new FakeDownloader); + + validator_ = BuildAddressValidatorForTesting( + FakeDownloader::kFakeDataUrl, + scoped_ptr<Downloader>(new FakeDownloader), + scoped_ptr<Storage>(new FakeStorage), + this); + } + + void OnDownloaded(bool success, + const std::string& url, + scoped_ptr<std::string> downloaded_data) { + EXPECT_TRUE(success); + EXPECT_FALSE(downloaded_data->empty()); + data_ = downloaded_data.Pass(); + } + + protected: + scoped_ptr<Downloader> downloader_; + scoped_ptr<AddressValidator> validator_; + scoped_ptr<std::string> data_; + + void TestCountryType(const scoped_ptr<Json>& json, + const std::string& country, + const std::string& type) { + scoped_ptr<Json> default_json; + + std::string default_key = "examples/" + country + "/" + type + "/_default"; + + ASSERT_TRUE(json->GetJsonValueForKey(default_key, &default_json)); + + scoped_ptr<Json> fields_json; + ASSERT_TRUE(default_json->GetJsonValueForKey( + "fields", &fields_json)); + + AddressData address; + for (int i = 1; i < 10; ++i) { + std::string street_key = "street"; + street_key.append(1, static_cast<char>('0' + i)); + std::string street_field; + if (!fields_json->GetStringValueForKey(street_key, &street_field)) + break; + + address.address_lines.push_back(street_field); + } + address.country_code = country; + fields_json->GetStringValueForKey("state", &address.administrative_area); + fields_json->GetStringValueForKey("city", &address.locality); + fields_json->GetStringValueForKey("locality", &address.dependent_locality); + fields_json->GetStringValueForKey("sorting_code", &address.sorting_code); + fields_json->GetStringValueForKey("zip", &address.postal_code); + fields_json->GetStringValueForKey("organization", &address.organization); + fields_json->GetStringValueForKey("name", &address.recipient); + + AddressProblems problems; + EXPECT_EQ(AddressValidator::SUCCESS, validator_->ValidateAddress( + address, AddressProblemFilter(), &problems)); + + std::string expected_problems_str; + std::vector<std::string> expected_problems; + if (default_json->GetStringValueForKey( + "problems", &expected_problems_str)) { + SplitString(expected_problems_str, '~', &expected_problems); + } + if (expected_problems.size() == problems.size()) { + EXPECT_TRUE(equal(problems.begin(), problems.end(), + expected_problems.begin(), + AddressProblemEqualsString())); + } else { + EXPECT_EQ(expected_problems.size(), problems.size()); + for (AddressProblems::const_iterator it = problems.begin(); + it != problems.end(); ++it) { + ADD_FAILURE() << "problem for " << default_key << ':' << *it; + } + } + } + + void TestCountry(const std::string& country) { + validator_->LoadRules(country); + std::string key = "examples/" + country; + std::string url = FakeDownloader::kFakeDataUrl + key; + scoped_ptr<Json> json(Json::Build()); + scoped_ptr<Json> json_country; + DownloadJsonValueForKey(key, &json, &json_country); + + std::string types_str; + ASSERT_TRUE(json_country->GetStringValueForKey("types", &types_str)); + std::vector<std::string> types; + SplitString(types_str, '~', &types); + + for (std::vector<std::string>::const_iterator it = types.begin(), + itend = types.end(); + it != itend; ++it) { + TestCountryType(json, country, *it); + } + } + + std::string DownloadString(const std::string& url) { + data_.reset(); + downloader_->Download( + url, + BuildScopedPtrCallback(dynamic_cast<ExampleAddressValidatorTest*>(this), + &ExampleAddressValidatorTest::OnDownloaded)); + return *data_; + } + + void DownloadJson(const std::string& key, scoped_ptr<Json>* json) { + std::string url = FakeDownloader::kFakeDataUrl + key; + ASSERT_TRUE((*json)->ParseObject(DownloadString(url))); + } + + void DownloadJsonValueForKey(const std::string& key, + scoped_ptr<Json>* json, + scoped_ptr<Json>* newjson) { + DownloadJson(key, json); + ASSERT_TRUE((*json)->GetJsonValueForKey(key, newjson)); + } + + private: + // LoadRulesDelegate implementation. + virtual void OnAddressValidationRulesLoaded(const std::string& country_code, + bool success) { + AddressData address_data; + address_data.country_code = country_code; + AddressValidator::Status status = + validator_->ValidateAddress(address_data, AddressProblemFilter(), NULL); + EXPECT_TRUE(success); + EXPECT_EQ(AddressValidator::SUCCESS, status); + } +}; + +TEST_F(ExampleAddressValidatorTest, examples) { + scoped_ptr<Json> json(Json::Build()); + scoped_ptr<Json> json_examples; + DownloadJsonValueForKey("examples", &json, &json_examples); + + std::string countries_str; + ASSERT_TRUE(json_examples->GetStringValueForKey("countries", &countries_str)); + std::vector<std::string> countries; + SplitString(countries_str, '~', &countries); + + for (std::vector<std::string>::const_iterator it = countries.begin(); + it != countries.end(); ++it) { + TestCountry(*it); + } +} + +} // addressinput +} // i18n diff --git a/third_party/libaddressinput/chromium/cpp/test/fake_downloader.cc b/third_party/libaddressinput/chromium/cpp/test/fake_downloader.cc index a58679a..d6b63f3 100644 --- a/third_party/libaddressinput/chromium/cpp/test/fake_downloader.cc +++ b/third_party/libaddressinput/chromium/cpp/test/fake_downloader.cc @@ -34,14 +34,20 @@ const char kDataFileName[] = TEST_DATA_DIR "/countryinfo.txt"; // The number of characters in the fake data URL prefix. const size_t kFakeDataUrlLength = sizeof FakeDownloader::kFakeDataUrl - 1; -// Each key begins with this string. -const char kKeyPrefix[] = "data/"; +std::string CCKey(const std::string& key) { + const char kSplitChar = '/'; -// The number of characters in the key prefix. -const size_t kKeyPrefixLength = sizeof kKeyPrefix - 1; + std::string::size_type split = key.find(kSplitChar); + if (split == std::string::npos) { + return key; + } + split = key.find(kSplitChar, split + 1); + if (split == std::string::npos) { + return key; + } -// The number of characters in a country-level key. -const size_t kCountryKeyLength = kKeyPrefixLength + 2; + return key.substr(0, split); +} std::map<std::string, std::string> InitData() { std::map<std::string, std::string> data; @@ -51,9 +57,6 @@ std::map<std::string, std::string> InitData() { std::string line; while (file.good()) { std::getline(file, line); - if (line.compare(0, kKeyPrefixLength, kKeyPrefix, kKeyPrefixLength) != 0) { - continue; - } std::string::size_type divider = line.find('='); if (divider == std::string::npos) { @@ -61,9 +64,9 @@ std::map<std::string, std::string> InitData() { } std::string key = line.substr(0, divider); + std::string cc_key = CCKey(key); std::string value = line.substr(divider + 1); - std::string url = - FakeDownloader::kFakeDataUrl + key.substr(0, kCountryKeyLength); + std::string url = FakeDownloader::kFakeDataUrl + cc_key; std::map<std::string, std::string>::iterator data_it = data.find(url); if (data_it != data.end()) { data_it->second += ", \"" + key + "\": " + value; diff --git a/third_party/libaddressinput/libaddressinput.gyp b/third_party/libaddressinput/libaddressinput.gyp index 8c33175..04c3f95 100644 --- a/third_party/libaddressinput/libaddressinput.gyp +++ b/third_party/libaddressinput/libaddressinput.gyp @@ -134,6 +134,7 @@ '<(libaddressinput_dir)/cpp/test/address_ui_test.cc', '<(libaddressinput_dir)/cpp/test/address_validator_test.cc', '<(libaddressinput_dir)/cpp/test/country_rules_aggregator_test.cc', + '<(libaddressinput_dir)/cpp/test/countryinfo_example_addresses_test.cc', '<(libaddressinput_dir)/cpp/test/fake_downloader.cc', '<(libaddressinput_dir)/cpp/test/fake_downloader.h', '<(libaddressinput_dir)/cpp/test/fake_downloader_test.cc', |