Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3744)

Unified Diff: base/json/json_value_converter_unittest.cc

Issue 9184002: Add custom field converter to JSONValueConverter. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Add comments for custom fields Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/json/json_value_converter.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/json/json_value_converter_unittest.cc
diff --git a/base/json/json_value_converter_unittest.cc b/base/json/json_value_converter_unittest.cc
index 73ed2299f1fc1565287906ef22237bd8a31f9050..b91dd6f9c4a71f2c7988564c2daeab25360dafbb 100644
--- a/base/json/json_value_converter_unittest.cc
+++ b/base/json/json_value_converter_unittest.cc
@@ -10,6 +10,7 @@
#include "base/values.h"
#include "base/json/json_reader.h"
#include "base/memory/scoped_ptr.h"
+#include "base/string_piece.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
@@ -17,17 +18,34 @@ namespace {
// Very simple messages.
struct SimpleMessage {
+ enum SimpleEnum {
+ FOO, BAR,
+ };
int foo;
std::string bar;
bool baz;
+ SimpleEnum simple_enum;
std::vector<int> ints;
SimpleMessage() : foo(0), baz(false) {}
+ static bool ParseSimpleEnum(const StringPiece& value, SimpleEnum* field) {
+ if (value == "foo") {
+ *field = FOO;
+ return true;
+ } else if (value == "bar") {
+ *field = BAR;
+ return true;
+ }
+ return false;
+ }
+
static void RegisterJSONConverter(
base::JSONValueConverter<SimpleMessage>* converter) {
converter->RegisterIntField("foo", &SimpleMessage::foo);
converter->RegisterStringField("bar", &SimpleMessage::bar);
converter->RegisterBoolField("baz", &SimpleMessage::baz);
+ converter->RegisterCustomField<SimpleEnum>(
+ "simple_enum", &SimpleMessage::simple_enum, &ParseSimpleEnum);
converter->RegisterRepeatedInt("ints", &SimpleMessage::ints);
}
};
@@ -56,6 +74,7 @@ TEST(JSONValueConverterTest, ParseSimpleMessage) {
" \"foo\": 1,\n"
" \"bar\": \"bar\",\n"
" \"baz\": true,\n"
+ " \"simple_enum\": \"foo\","
" \"ints\": [1, 2]"
"}\n";
@@ -67,6 +86,7 @@ TEST(JSONValueConverterTest, ParseSimpleMessage) {
EXPECT_EQ(1, message.foo);
EXPECT_EQ("bar", message.bar);
EXPECT_TRUE(message.baz);
+ EXPECT_EQ(SimpleMessage::FOO, message.simple_enum);
EXPECT_EQ(2, static_cast<int>(message.ints.size()));
EXPECT_EQ(1, message.ints[0]);
EXPECT_EQ(2, message.ints[1]);
@@ -153,4 +173,21 @@ TEST(JSONValueConverterTest, ParseWithMissingFields) {
EXPECT_EQ(2, message.ints[1]);
}
+TEST(JSONValueConverterTest, EnumParserFails) {
+ const char normal_data[] =
+ "{\n"
+ " \"foo\": 1,\n"
+ " \"bar\": \"bar\",\n"
+ " \"baz\": true,\n"
+ " \"simple_enum\": \"baz\","
+ " \"ints\": [1, 2]"
+ "}\n";
+
+ scoped_ptr<Value> value(base::JSONReader::Read(normal_data, false));
+ SimpleMessage message;
+ base::JSONValueConverter<SimpleMessage> converter;
+ EXPECT_FALSE(converter.Convert(*value.get(), &message));
+ // No check the values as mentioned above.
+}
+
} // namespace base
« no previous file with comments | « base/json/json_value_converter.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698