| 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/memory/scoped_vector.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 { |
| 20 int foo; | 21 int foo; |
| 21 std::string bar; | 22 std::string bar; |
| 22 bool baz; | 23 bool baz; |
| 23 std::vector<int> ints; | 24 std::vector<int> ints; |
| 24 SimpleMessage() : foo(0), baz(false) {} | 25 SimpleMessage() : foo(0), baz(false) {} |
| 25 | 26 |
| 26 static void RegisterJSONConverter( | 27 static void RegisterJSONConverter( |
| 27 base::JSONValueConverter<SimpleMessage>* converter) { | 28 base::JSONValueConverter<SimpleMessage>* converter) { |
| 28 converter->RegisterIntField("foo", &SimpleMessage::foo); | 29 converter->RegisterIntField("foo", &SimpleMessage::foo); |
| 29 converter->RegisterStringField("bar", &SimpleMessage::bar); | 30 converter->RegisterStringField("bar", &SimpleMessage::bar); |
| 30 converter->RegisterBoolField("baz", &SimpleMessage::baz); | 31 converter->RegisterBoolField("baz", &SimpleMessage::baz); |
| 31 converter->RegisterRepeatedInt("ints", &SimpleMessage::ints); | 32 converter->RegisterRepeatedInt("ints", &SimpleMessage::ints); |
| 32 } | 33 } |
| 33 }; | 34 }; |
| 34 | 35 |
| 35 // For nested messages. | 36 // For nested messages. |
| 36 struct NestedMessage { | 37 struct NestedMessage { |
| 37 double foo; | 38 double foo; |
| 38 SimpleMessage child; | 39 SimpleMessage child; |
| 39 std::vector<SimpleMessage> children; | 40 ScopedVector<SimpleMessage> children; |
| 40 | 41 |
| 41 NestedMessage() : foo(0) {} | 42 NestedMessage() : foo(0) {} |
| 42 | 43 |
| 43 static void RegisterJSONConverter( | 44 static void RegisterJSONConverter( |
| 44 base::JSONValueConverter<NestedMessage>* converter) { | 45 base::JSONValueConverter<NestedMessage>* converter) { |
| 45 converter->RegisterDoubleField("foo", &NestedMessage::foo); | 46 converter->RegisterDoubleField("foo", &NestedMessage::foo); |
| 46 converter->RegisterNestedField("child", &NestedMessage::child); | 47 converter->RegisterNestedField("child", &NestedMessage::child); |
| 47 converter->RegisterRepeatedMessage("children", &NestedMessage::children); | 48 converter->RegisterRepeatedMessage("children", &NestedMessage::children); |
| 48 } | 49 } |
| 49 }; | 50 }; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 NestedMessage message; | 98 NestedMessage message; |
| 98 base::JSONValueConverter<NestedMessage> converter; | 99 base::JSONValueConverter<NestedMessage> converter; |
| 99 EXPECT_TRUE(converter.Convert(*value.get(), &message)); | 100 EXPECT_TRUE(converter.Convert(*value.get(), &message)); |
| 100 | 101 |
| 101 EXPECT_EQ(1.0, message.foo); | 102 EXPECT_EQ(1.0, message.foo); |
| 102 EXPECT_EQ(1, message.child.foo); | 103 EXPECT_EQ(1, message.child.foo); |
| 103 EXPECT_EQ("bar", message.child.bar); | 104 EXPECT_EQ("bar", message.child.bar); |
| 104 EXPECT_TRUE(message.child.baz); | 105 EXPECT_TRUE(message.child.baz); |
| 105 | 106 |
| 106 EXPECT_EQ(2, static_cast<int>(message.children.size())); | 107 EXPECT_EQ(2, static_cast<int>(message.children.size())); |
| 107 const SimpleMessage& first_child = message.children[0]; | 108 const SimpleMessage* first_child = message.children[0]; |
| 108 EXPECT_EQ(2, first_child.foo); | 109 ASSERT_TRUE(first_child); |
| 109 EXPECT_EQ("foobar", first_child.bar); | 110 EXPECT_EQ(2, first_child->foo); |
| 110 EXPECT_TRUE(first_child.baz); | 111 EXPECT_EQ("foobar", first_child->bar); |
| 112 EXPECT_TRUE(first_child->baz); |
| 111 | 113 |
| 112 const SimpleMessage& second_child = message.children[1]; | 114 const SimpleMessage* second_child = message.children[1]; |
| 113 EXPECT_EQ(3, second_child.foo); | 115 ASSERT_TRUE(second_child); |
| 114 EXPECT_EQ("barbaz", second_child.bar); | 116 EXPECT_EQ(3, second_child->foo); |
| 115 EXPECT_FALSE(second_child.baz); | 117 EXPECT_EQ("barbaz", second_child->bar); |
| 118 EXPECT_FALSE(second_child->baz); |
| 116 } | 119 } |
| 117 | 120 |
| 118 TEST(JSONValueConverterTest, ParseFailures) { | 121 TEST(JSONValueConverterTest, ParseFailures) { |
| 119 const char normal_data[] = | 122 const char normal_data[] = |
| 120 "{\n" | 123 "{\n" |
| 121 " \"foo\": 1,\n" | 124 " \"foo\": 1,\n" |
| 122 " \"bar\": 2,\n" // "bar" is an integer here. | 125 " \"bar\": 2,\n" // "bar" is an integer here. |
| 123 " \"baz\": true,\n" | 126 " \"baz\": true,\n" |
| 124 " \"ints\": [1, 2]" | 127 " \"ints\": [1, 2]" |
| 125 "}\n"; | 128 "}\n"; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 147 EXPECT_TRUE(converter.Convert(*value.get(), &message)); | 150 EXPECT_TRUE(converter.Convert(*value.get(), &message)); |
| 148 | 151 |
| 149 EXPECT_EQ(1, message.foo); | 152 EXPECT_EQ(1, message.foo); |
| 150 EXPECT_TRUE(message.baz); | 153 EXPECT_TRUE(message.baz); |
| 151 EXPECT_EQ(2, static_cast<int>(message.ints.size())); | 154 EXPECT_EQ(2, static_cast<int>(message.ints.size())); |
| 152 EXPECT_EQ(1, message.ints[0]); | 155 EXPECT_EQ(1, message.ints[0]); |
| 153 EXPECT_EQ(2, message.ints[1]); | 156 EXPECT_EQ(2, message.ints[1]); |
| 154 } | 157 } |
| 155 | 158 |
| 156 } // namespace base | 159 } // namespace base |
| OLD | NEW |