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 <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
12 #include "base/memory/scoped_vector.h" | |
13 #include "base/strings/string_piece.h" | 12 #include "base/strings/string_piece.h" |
14 #include "base/values.h" | 13 #include "base/values.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
16 | 15 |
17 namespace base { | 16 namespace base { |
18 namespace { | 17 namespace { |
19 | 18 |
20 // Very simple messages. | 19 // Very simple messages. |
21 struct SimpleMessage { | 20 struct SimpleMessage { |
22 enum SimpleEnum { | 21 enum SimpleEnum { |
23 FOO, BAR, | 22 FOO, BAR, |
24 }; | 23 }; |
25 int foo; | 24 int foo; |
26 std::string bar; | 25 std::string bar; |
27 bool baz; | 26 bool baz; |
28 bool bstruct; | 27 bool bstruct; |
29 SimpleEnum simple_enum; | 28 SimpleEnum simple_enum; |
30 ScopedVector<int> ints; | 29 std::vector<std::unique_ptr<int>> ints; |
31 ScopedVector<std::string> string_values; | 30 std::vector<std::unique_ptr<std::string>> string_values; |
32 SimpleMessage() : foo(0), baz(false), bstruct(false), simple_enum(FOO) {} | 31 SimpleMessage() : foo(0), baz(false), bstruct(false), simple_enum(FOO) {} |
33 | 32 |
34 static bool ParseSimpleEnum(const StringPiece& value, SimpleEnum* field) { | 33 static bool ParseSimpleEnum(const StringPiece& value, SimpleEnum* field) { |
35 if (value == "foo") { | 34 if (value == "foo") { |
36 *field = FOO; | 35 *field = FOO; |
37 return true; | 36 return true; |
38 } else if (value == "bar") { | 37 } else if (value == "bar") { |
39 *field = BAR; | 38 *field = BAR; |
40 return true; | 39 return true; |
41 } | 40 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 "string_values", | 72 "string_values", |
74 &SimpleMessage::string_values, | 73 &SimpleMessage::string_values, |
75 &GetValueString); | 74 &GetValueString); |
76 } | 75 } |
77 }; | 76 }; |
78 | 77 |
79 // For nested messages. | 78 // For nested messages. |
80 struct NestedMessage { | 79 struct NestedMessage { |
81 double foo; | 80 double foo; |
82 SimpleMessage child; | 81 SimpleMessage child; |
83 ScopedVector<SimpleMessage> children; | 82 std::vector<std::unique_ptr<SimpleMessage>> children; |
84 | 83 |
85 NestedMessage() : foo(0) {} | 84 NestedMessage() : foo(0) {} |
86 | 85 |
87 static void RegisterJSONConverter( | 86 static void RegisterJSONConverter( |
88 base::JSONValueConverter<NestedMessage>* converter) { | 87 base::JSONValueConverter<NestedMessage>* converter) { |
89 converter->RegisterDoubleField("foo", &NestedMessage::foo); | 88 converter->RegisterDoubleField("foo", &NestedMessage::foo); |
90 converter->RegisterNestedField("child", &NestedMessage::child); | 89 converter->RegisterNestedField("child", &NestedMessage::child); |
91 converter->RegisterRepeatedMessage("children", &NestedMessage::children); | 90 converter->RegisterRepeatedMessage("children", &NestedMessage::children); |
92 } | 91 } |
93 }; | 92 }; |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 EXPECT_EQ(1.0, message.foo); | 155 EXPECT_EQ(1.0, message.foo); |
157 EXPECT_EQ(1, message.child.foo); | 156 EXPECT_EQ(1, message.child.foo); |
158 EXPECT_EQ("bar", message.child.bar); | 157 EXPECT_EQ("bar", message.child.bar); |
159 EXPECT_TRUE(message.child.baz); | 158 EXPECT_TRUE(message.child.baz); |
160 EXPECT_TRUE(message.child.bstruct); | 159 EXPECT_TRUE(message.child.bstruct); |
161 ASSERT_EQ(2U, message.child.string_values.size()); | 160 ASSERT_EQ(2U, message.child.string_values.size()); |
162 EXPECT_EQ("value_1", *message.child.string_values[0]); | 161 EXPECT_EQ("value_1", *message.child.string_values[0]); |
163 EXPECT_EQ("value_2", *message.child.string_values[1]); | 162 EXPECT_EQ("value_2", *message.child.string_values[1]); |
164 | 163 |
165 EXPECT_EQ(2, static_cast<int>(message.children.size())); | 164 EXPECT_EQ(2, static_cast<int>(message.children.size())); |
166 const SimpleMessage* first_child = message.children[0]; | 165 const SimpleMessage* first_child = message.children[0].get(); |
167 ASSERT_TRUE(first_child); | 166 ASSERT_TRUE(first_child); |
168 EXPECT_EQ(2, first_child->foo); | 167 EXPECT_EQ(2, first_child->foo); |
169 EXPECT_EQ("foobar", first_child->bar); | 168 EXPECT_EQ("foobar", first_child->bar); |
170 EXPECT_TRUE(first_child->baz); | 169 EXPECT_TRUE(first_child->baz); |
171 EXPECT_TRUE(first_child->bstruct); | 170 EXPECT_TRUE(first_child->bstruct); |
172 ASSERT_EQ(1U, first_child->string_values.size()); | 171 ASSERT_EQ(1U, first_child->string_values.size()); |
173 EXPECT_EQ("value_1", *first_child->string_values[0]); | 172 EXPECT_EQ("value_1", *first_child->string_values[0]); |
174 | 173 |
175 const SimpleMessage* second_child = message.children[1]; | 174 const SimpleMessage* second_child = message.children[1].get(); |
176 ASSERT_TRUE(second_child); | 175 ASSERT_TRUE(second_child); |
177 EXPECT_EQ(3, second_child->foo); | 176 EXPECT_EQ(3, second_child->foo); |
178 EXPECT_EQ("barbaz", second_child->bar); | 177 EXPECT_EQ("barbaz", second_child->bar); |
179 EXPECT_FALSE(second_child->baz); | 178 EXPECT_FALSE(second_child->baz); |
180 EXPECT_FALSE(second_child->bstruct); | 179 EXPECT_FALSE(second_child->bstruct); |
181 EXPECT_EQ(0U, second_child->string_values.size()); | 180 EXPECT_EQ(0U, second_child->string_values.size()); |
182 } | 181 } |
183 | 182 |
184 TEST(JSONValueConverterTest, ParseFailures) { | 183 TEST(JSONValueConverterTest, ParseFailures) { |
185 const char normal_data[] = | 184 const char normal_data[] = |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 "}\n"; | 246 "}\n"; |
248 | 247 |
249 std::unique_ptr<Value> value = base::JSONReader::Read(normal_data); | 248 std::unique_ptr<Value> value = base::JSONReader::Read(normal_data); |
250 SimpleMessage message; | 249 SimpleMessage message; |
251 base::JSONValueConverter<SimpleMessage> converter; | 250 base::JSONValueConverter<SimpleMessage> converter; |
252 EXPECT_FALSE(converter.Convert(*value.get(), &message)); | 251 EXPECT_FALSE(converter.Convert(*value.get(), &message)); |
253 // No check the values as mentioned above. | 252 // No check the values as mentioned above. |
254 } | 253 } |
255 | 254 |
256 } // namespace base | 255 } // namespace base |
OLD | NEW |