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

Side by Side Diff: base/json/json_value_converter_unittest.cc

Issue 9960077: Modify the base::JSONReader interface to take a set of options rather than a boolean flag. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 8 years, 8 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 TEST(JSONValueConverterTest, ParseSimpleMessage) { 72 TEST(JSONValueConverterTest, ParseSimpleMessage) {
73 const char normal_data[] = 73 const char normal_data[] =
74 "{\n" 74 "{\n"
75 " \"foo\": 1,\n" 75 " \"foo\": 1,\n"
76 " \"bar\": \"bar\",\n" 76 " \"bar\": \"bar\",\n"
77 " \"baz\": true,\n" 77 " \"baz\": true,\n"
78 " \"simple_enum\": \"foo\"," 78 " \"simple_enum\": \"foo\","
79 " \"ints\": [1, 2]" 79 " \"ints\": [1, 2]"
80 "}\n"; 80 "}\n";
81 81
82 scoped_ptr<Value> value(base::JSONReader::Read(normal_data, false)); 82 scoped_ptr<Value> value(base::JSONReader::Read(normal_data));
83 SimpleMessage message; 83 SimpleMessage message;
84 base::JSONValueConverter<SimpleMessage> converter; 84 base::JSONValueConverter<SimpleMessage> converter;
85 EXPECT_TRUE(converter.Convert(*value.get(), &message)); 85 EXPECT_TRUE(converter.Convert(*value.get(), &message));
86 86
87 EXPECT_EQ(1, message.foo); 87 EXPECT_EQ(1, message.foo);
88 EXPECT_EQ("bar", message.bar); 88 EXPECT_EQ("bar", message.bar);
89 EXPECT_TRUE(message.baz); 89 EXPECT_TRUE(message.baz);
90 EXPECT_EQ(SimpleMessage::FOO, message.simple_enum); 90 EXPECT_EQ(SimpleMessage::FOO, message.simple_enum);
91 EXPECT_EQ(2, static_cast<int>(message.ints.size())); 91 EXPECT_EQ(2, static_cast<int>(message.ints.size()));
92 EXPECT_EQ(1, *(message.ints[0])); 92 EXPECT_EQ(1, *(message.ints[0]));
(...skipping 14 matching lines...) Expand all
107 " \"bar\": \"foobar\",\n" 107 " \"bar\": \"foobar\",\n"
108 " \"baz\": true\n" 108 " \"baz\": true\n"
109 " },\n" 109 " },\n"
110 " {\n" 110 " {\n"
111 " \"foo\": 3,\n" 111 " \"foo\": 3,\n"
112 " \"bar\": \"barbaz\",\n" 112 " \"bar\": \"barbaz\",\n"
113 " \"baz\": false\n" 113 " \"baz\": false\n"
114 " }]\n" 114 " }]\n"
115 "}\n"; 115 "}\n";
116 116
117 scoped_ptr<Value> value(base::JSONReader::Read(normal_data, false)); 117 scoped_ptr<Value> value(base::JSONReader::Read(normal_data));
118 NestedMessage message; 118 NestedMessage message;
119 base::JSONValueConverter<NestedMessage> converter; 119 base::JSONValueConverter<NestedMessage> converter;
120 EXPECT_TRUE(converter.Convert(*value.get(), &message)); 120 EXPECT_TRUE(converter.Convert(*value.get(), &message));
121 121
122 EXPECT_EQ(1.0, message.foo); 122 EXPECT_EQ(1.0, message.foo);
123 EXPECT_EQ(1, message.child.foo); 123 EXPECT_EQ(1, message.child.foo);
124 EXPECT_EQ("bar", message.child.bar); 124 EXPECT_EQ("bar", message.child.bar);
125 EXPECT_TRUE(message.child.baz); 125 EXPECT_TRUE(message.child.baz);
126 126
127 EXPECT_EQ(2, static_cast<int>(message.children.size())); 127 EXPECT_EQ(2, static_cast<int>(message.children.size()));
(...skipping 12 matching lines...) Expand all
140 140
141 TEST(JSONValueConverterTest, ParseFailures) { 141 TEST(JSONValueConverterTest, ParseFailures) {
142 const char normal_data[] = 142 const char normal_data[] =
143 "{\n" 143 "{\n"
144 " \"foo\": 1,\n" 144 " \"foo\": 1,\n"
145 " \"bar\": 2,\n" // "bar" is an integer here. 145 " \"bar\": 2,\n" // "bar" is an integer here.
146 " \"baz\": true,\n" 146 " \"baz\": true,\n"
147 " \"ints\": [1, 2]" 147 " \"ints\": [1, 2]"
148 "}\n"; 148 "}\n";
149 149
150 scoped_ptr<Value> value(base::JSONReader::Read(normal_data, false)); 150 scoped_ptr<Value> value(base::JSONReader::Read(normal_data));
151 SimpleMessage message; 151 SimpleMessage message;
152 base::JSONValueConverter<SimpleMessage> converter; 152 base::JSONValueConverter<SimpleMessage> converter;
153 EXPECT_FALSE(converter.Convert(*value.get(), &message)); 153 EXPECT_FALSE(converter.Convert(*value.get(), &message));
154 // Do not check the values below. |message| may be modified during 154 // Do not check the values below. |message| may be modified during
155 // Convert() even it fails. 155 // Convert() even it fails.
156 } 156 }
157 157
158 TEST(JSONValueConverterTest, ParseWithMissingFields) { 158 TEST(JSONValueConverterTest, ParseWithMissingFields) {
159 const char normal_data[] = 159 const char normal_data[] =
160 "{\n" 160 "{\n"
161 " \"foo\": 1,\n" 161 " \"foo\": 1,\n"
162 " \"baz\": true,\n" 162 " \"baz\": true,\n"
163 " \"ints\": [1, 2]" 163 " \"ints\": [1, 2]"
164 "}\n"; 164 "}\n";
165 165
166 scoped_ptr<Value> value(base::JSONReader::Read(normal_data, false)); 166 scoped_ptr<Value> value(base::JSONReader::Read(normal_data));
167 SimpleMessage message; 167 SimpleMessage message;
168 base::JSONValueConverter<SimpleMessage> converter; 168 base::JSONValueConverter<SimpleMessage> converter;
169 // Convert() still succeeds even if the input doesn't have "bar" field. 169 // Convert() still succeeds even if the input doesn't have "bar" field.
170 EXPECT_TRUE(converter.Convert(*value.get(), &message)); 170 EXPECT_TRUE(converter.Convert(*value.get(), &message));
171 171
172 EXPECT_EQ(1, message.foo); 172 EXPECT_EQ(1, message.foo);
173 EXPECT_TRUE(message.baz); 173 EXPECT_TRUE(message.baz);
174 EXPECT_EQ(2, static_cast<int>(message.ints.size())); 174 EXPECT_EQ(2, static_cast<int>(message.ints.size()));
175 EXPECT_EQ(1, *(message.ints[0])); 175 EXPECT_EQ(1, *(message.ints[0]));
176 EXPECT_EQ(2, *(message.ints[1])); 176 EXPECT_EQ(2, *(message.ints[1]));
177 } 177 }
178 178
179 TEST(JSONValueConverterTest, EnumParserFails) { 179 TEST(JSONValueConverterTest, EnumParserFails) {
180 const char normal_data[] = 180 const char normal_data[] =
181 "{\n" 181 "{\n"
182 " \"foo\": 1,\n" 182 " \"foo\": 1,\n"
183 " \"bar\": \"bar\",\n" 183 " \"bar\": \"bar\",\n"
184 " \"baz\": true,\n" 184 " \"baz\": true,\n"
185 " \"simple_enum\": \"baz\"," 185 " \"simple_enum\": \"baz\","
186 " \"ints\": [1, 2]" 186 " \"ints\": [1, 2]"
187 "}\n"; 187 "}\n";
188 188
189 scoped_ptr<Value> value(base::JSONReader::Read(normal_data, false)); 189 scoped_ptr<Value> value(base::JSONReader::Read(normal_data));
190 SimpleMessage message; 190 SimpleMessage message;
191 base::JSONValueConverter<SimpleMessage> converter; 191 base::JSONValueConverter<SimpleMessage> converter;
192 EXPECT_FALSE(converter.Convert(*value.get(), &message)); 192 EXPECT_FALSE(converter.Convert(*value.get(), &message));
193 // No check the values as mentioned above. 193 // No check the values as mentioned above.
194 } 194 }
195 195
196 TEST(JSONValueConverterTest, RepeatedValueErrorInTheMiddle) { 196 TEST(JSONValueConverterTest, RepeatedValueErrorInTheMiddle) {
197 const char normal_data[] = 197 const char normal_data[] =
198 "{\n" 198 "{\n"
199 " \"foo\": 1,\n" 199 " \"foo\": 1,\n"
200 " \"bar\": \"bar\",\n" 200 " \"bar\": \"bar\",\n"
201 " \"baz\": true,\n" 201 " \"baz\": true,\n"
202 " \"simple_enum\": \"baz\"," 202 " \"simple_enum\": \"baz\","
203 " \"ints\": [1, false]" 203 " \"ints\": [1, false]"
204 "}\n"; 204 "}\n";
205 205
206 scoped_ptr<Value> value(base::JSONReader::Read(normal_data, false)); 206 scoped_ptr<Value> value(base::JSONReader::Read(normal_data));
207 SimpleMessage message; 207 SimpleMessage message;
208 base::JSONValueConverter<SimpleMessage> converter; 208 base::JSONValueConverter<SimpleMessage> converter;
209 EXPECT_FALSE(converter.Convert(*value.get(), &message)); 209 EXPECT_FALSE(converter.Convert(*value.get(), &message));
210 // No check the values as mentioned above. 210 // No check the values as mentioned above.
211 } 211 }
212 212
213 } // namespace base 213 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698