OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/json/json_value_converter.h" | 5 #include "base/json/json_value_converter.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/values.h" | 10 #include "base/values.h" |
11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/string_piece.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 | 15 |
15 namespace base { | 16 namespace base { |
16 namespace { | 17 namespace { |
17 | 18 |
18 // Very simple messages. | 19 // Very simple messages. |
19 struct SimpleMessage { | 20 struct SimpleMessage { |
| 21 enum SimpleEnum { |
| 22 FOO, BAR, |
| 23 }; |
20 int foo; | 24 int foo; |
21 std::string bar; | 25 std::string bar; |
22 bool baz; | 26 bool baz; |
| 27 SimpleEnum simple_enum; |
23 std::vector<int> ints; | 28 std::vector<int> ints; |
24 SimpleMessage() : foo(0), baz(false) {} | 29 SimpleMessage() : foo(0), baz(false) {} |
25 | 30 |
| 31 static bool ParseSimpleEnum(const StringPiece& value, SimpleEnum* field) { |
| 32 if (value == "foo") { |
| 33 *field = FOO; |
| 34 return true; |
| 35 } else if (value == "bar") { |
| 36 *field = BAR; |
| 37 return true; |
| 38 } |
| 39 return false; |
| 40 } |
| 41 |
26 static void RegisterJSONConverter( | 42 static void RegisterJSONConverter( |
27 base::JSONValueConverter<SimpleMessage>* converter) { | 43 base::JSONValueConverter<SimpleMessage>* converter) { |
28 converter->RegisterIntField("foo", &SimpleMessage::foo); | 44 converter->RegisterIntField("foo", &SimpleMessage::foo); |
29 converter->RegisterStringField("bar", &SimpleMessage::bar); | 45 converter->RegisterStringField("bar", &SimpleMessage::bar); |
30 converter->RegisterBoolField("baz", &SimpleMessage::baz); | 46 converter->RegisterBoolField("baz", &SimpleMessage::baz); |
| 47 converter->RegisterCustomField<SimpleEnum>( |
| 48 "simple_enum", &SimpleMessage::simple_enum, &ParseSimpleEnum); |
31 converter->RegisterRepeatedInt("ints", &SimpleMessage::ints); | 49 converter->RegisterRepeatedInt("ints", &SimpleMessage::ints); |
32 } | 50 } |
33 }; | 51 }; |
34 | 52 |
35 // For nested messages. | 53 // For nested messages. |
36 struct NestedMessage { | 54 struct NestedMessage { |
37 double foo; | 55 double foo; |
38 SimpleMessage child; | 56 SimpleMessage child; |
39 std::vector<SimpleMessage> children; | 57 std::vector<SimpleMessage> children; |
40 | 58 |
41 NestedMessage() : foo(0) {} | 59 NestedMessage() : foo(0) {} |
42 | 60 |
43 static void RegisterJSONConverter( | 61 static void RegisterJSONConverter( |
44 base::JSONValueConverter<NestedMessage>* converter) { | 62 base::JSONValueConverter<NestedMessage>* converter) { |
45 converter->RegisterDoubleField("foo", &NestedMessage::foo); | 63 converter->RegisterDoubleField("foo", &NestedMessage::foo); |
46 converter->RegisterNestedField("child", &NestedMessage::child); | 64 converter->RegisterNestedField("child", &NestedMessage::child); |
47 converter->RegisterRepeatedMessage("children", &NestedMessage::children); | 65 converter->RegisterRepeatedMessage("children", &NestedMessage::children); |
48 } | 66 } |
49 }; | 67 }; |
50 | 68 |
51 } // namespace | 69 } // namespace |
52 | 70 |
53 TEST(JSONValueConverterTest, ParseSimpleMessage) { | 71 TEST(JSONValueConverterTest, ParseSimpleMessage) { |
54 const char normal_data[] = | 72 const char normal_data[] = |
55 "{\n" | 73 "{\n" |
56 " \"foo\": 1,\n" | 74 " \"foo\": 1,\n" |
57 " \"bar\": \"bar\",\n" | 75 " \"bar\": \"bar\",\n" |
58 " \"baz\": true,\n" | 76 " \"baz\": true,\n" |
| 77 " \"simple_enum\": \"foo\"," |
59 " \"ints\": [1, 2]" | 78 " \"ints\": [1, 2]" |
60 "}\n"; | 79 "}\n"; |
61 | 80 |
62 scoped_ptr<Value> value(base::JSONReader::Read(normal_data, false)); | 81 scoped_ptr<Value> value(base::JSONReader::Read(normal_data, false)); |
63 SimpleMessage message; | 82 SimpleMessage message; |
64 base::JSONValueConverter<SimpleMessage> converter; | 83 base::JSONValueConverter<SimpleMessage> converter; |
65 EXPECT_TRUE(converter.Convert(*value.get(), &message)); | 84 EXPECT_TRUE(converter.Convert(*value.get(), &message)); |
66 | 85 |
67 EXPECT_EQ(1, message.foo); | 86 EXPECT_EQ(1, message.foo); |
68 EXPECT_EQ("bar", message.bar); | 87 EXPECT_EQ("bar", message.bar); |
69 EXPECT_TRUE(message.baz); | 88 EXPECT_TRUE(message.baz); |
| 89 EXPECT_EQ(SimpleMessage::FOO, message.simple_enum); |
70 EXPECT_EQ(2, static_cast<int>(message.ints.size())); | 90 EXPECT_EQ(2, static_cast<int>(message.ints.size())); |
71 EXPECT_EQ(1, message.ints[0]); | 91 EXPECT_EQ(1, message.ints[0]); |
72 EXPECT_EQ(2, message.ints[1]); | 92 EXPECT_EQ(2, message.ints[1]); |
73 } | 93 } |
74 | 94 |
75 TEST(JSONValueConverterTest, ParseNestedMessage) { | 95 TEST(JSONValueConverterTest, ParseNestedMessage) { |
76 const char normal_data[] = | 96 const char normal_data[] = |
77 "{\n" | 97 "{\n" |
78 " \"foo\": 1.0,\n" | 98 " \"foo\": 1.0,\n" |
79 " \"child\": {\n" | 99 " \"child\": {\n" |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 // Convert() still succeeds even if the input doesn't have "bar" field. | 166 // Convert() still succeeds even if the input doesn't have "bar" field. |
147 EXPECT_TRUE(converter.Convert(*value.get(), &message)); | 167 EXPECT_TRUE(converter.Convert(*value.get(), &message)); |
148 | 168 |
149 EXPECT_EQ(1, message.foo); | 169 EXPECT_EQ(1, message.foo); |
150 EXPECT_TRUE(message.baz); | 170 EXPECT_TRUE(message.baz); |
151 EXPECT_EQ(2, static_cast<int>(message.ints.size())); | 171 EXPECT_EQ(2, static_cast<int>(message.ints.size())); |
152 EXPECT_EQ(1, message.ints[0]); | 172 EXPECT_EQ(1, message.ints[0]); |
153 EXPECT_EQ(2, message.ints[1]); | 173 EXPECT_EQ(2, message.ints[1]); |
154 } | 174 } |
155 | 175 |
| 176 TEST(JSONValueConverterTest, EnumParserFails) { |
| 177 const char normal_data[] = |
| 178 "{\n" |
| 179 " \"foo\": 1,\n" |
| 180 " \"bar\": \"bar\",\n" |
| 181 " \"baz\": true,\n" |
| 182 " \"simple_enum\": \"baz\"," |
| 183 " \"ints\": [1, 2]" |
| 184 "}\n"; |
| 185 |
| 186 scoped_ptr<Value> value(base::JSONReader::Read(normal_data, false)); |
| 187 SimpleMessage message; |
| 188 base::JSONValueConverter<SimpleMessage> converter; |
| 189 EXPECT_FALSE(converter.Convert(*value.get(), &message)); |
| 190 // No check the values as mentioned above. |
| 191 } |
| 192 |
156 } // namespace base | 193 } // namespace base |
OLD | NEW |