diff options
author | rouslan@chromium.org <rouslan@chromium.org@38ededc0-08b8-5190-f2ac-b31f878777ad> | 2014-05-21 12:59:08 +0000 |
---|---|---|
committer | rouslan@chromium.org <rouslan@chromium.org@38ededc0-08b8-5190-f2ac-b31f878777ad> | 2014-05-21 12:59:08 +0000 |
commit | 41dd744f8493cfa6fc3f7c457cf53995a45e69f9 (patch) | |
tree | 4fbf1cb1db8cc41140dfbd1f6dc87231764f1b4f | |
parent | 7825966d9566c8ced01471f1e6e6bf1e715f6572 (diff) | |
download | src-41dd744f8493cfa6fc3f7c457cf53995a45e69f9.tar.gz |
Enable parsing a Rule object from a JSON object.
TBR=roubert@google.com
Review URL: https://codereview.appspot.com/92520047
git-svn-id: http://libaddressinput.googlecode.com/svn/trunk@230 38ededc0-08b8-5190-f2ac-b31f878777ad
-rw-r--r-- | cpp/src/rule.cc | 6 | ||||
-rw-r--r-- | cpp/src/rule.h | 4 | ||||
-rw-r--r-- | cpp/test/rule_test.cc | 10 |
3 files changed, 18 insertions, 2 deletions
diff --git a/cpp/src/rule.cc b/cpp/src/rule.cc index bd4664c..e06e98c 100644 --- a/cpp/src/rule.cc +++ b/cpp/src/rule.cc @@ -152,7 +152,11 @@ bool Rule::ParseSerializedRule(const std::string& serialized_rule) { if (!json.ParseObject(serialized_rule)) { return false; } + ParseJsonRule(json); + return true; +} +void Rule::ParseJsonRule(const Json& json) { if (json.HasStringValueForKey(kIdKey)) { id_ = json.GetStringValueForKey(kIdKey); } @@ -215,8 +219,6 @@ bool Rule::ParseSerializedRule(const std::string& serialized_rule) { GetMessageIdFromName(json.GetStringValueForKey(kPostalCodeNameTypeKey), GetPostalCodeMessageIds()); } - - return true; } } // namespace addressinput diff --git a/cpp/src/rule.h b/cpp/src/rule.h index 0d9c17a..28c0240 100644 --- a/cpp/src/rule.h +++ b/cpp/src/rule.h @@ -32,6 +32,7 @@ namespace i18n { namespace addressinput { +class Json; class RE2ptr; // Stores address metadata addressing rules, to be used for determining the @@ -57,6 +58,9 @@ class Rule { // format (JSON dictionary). bool ParseSerializedRule(const std::string& serialized_rule); + // Reads data from |json|, which must already have parsed a serialized rule. + void ParseJsonRule(const Json& json); + // Returns the ID string for this rule. const std::string& GetId() const { return id_; } diff --git a/cpp/test/rule_test.cc b/cpp/test/rule_test.cc index 456714a..e463c70 100644 --- a/cpp/test/rule_test.cc +++ b/cpp/test/rule_test.cc @@ -29,6 +29,7 @@ #include "grit.h" #include "messages.h" #include "region_data_constants.h" +#include "util/json.h" namespace { @@ -36,6 +37,7 @@ using i18n::addressinput::AddressField; using i18n::addressinput::ADMIN_AREA; using i18n::addressinput::FormatElement; using i18n::addressinput::INVALID_MESSAGE_ID; +using i18n::addressinput::Json; using i18n::addressinput::LOCALITY; using i18n::addressinput::Localization; using i18n::addressinput::RegionDataConstants; @@ -171,6 +173,14 @@ TEST(RuleTest, PostalCodeMatcherInvalidRegExp) { EXPECT_TRUE(rule.GetPostalCodeMatcher() == NULL); } +TEST(RuleTest, ParsesJsonRuleCorrectly) { + Json json; + ASSERT_TRUE(json.ParseObject("{\"zip\":\"\\\\d{3}\"}")); + Rule rule; + rule.ParseJsonRule(json); + EXPECT_TRUE(rule.GetPostalCodeMatcher() != NULL); +} + TEST(RuleTest, EmptyStringIsNotValid) { Rule rule; EXPECT_FALSE(rule.ParseSerializedRule(std::string())); |