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

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

Issue 925783002: Split ValueSerializer into separate Serializer and Deserializer classes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Updated as per review comments. Created 5 years, 10 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
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 <string> 5 #include <string>
6 6
7 #include "base/files/file_util.h" 7 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
9 #include "base/json/json_file_value_serializer.h" 9 #include "base/json/json_file_value_serializer.h"
10 #include "base/json/json_reader.h" 10 #include "base/json/json_reader.h"
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST)); 79 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST));
80 ListValue* list = static_cast<ListValue*>(root.get()); 80 ListValue* list = static_cast<ListValue*>(root.get());
81 ASSERT_EQ(1U, list->GetSize()); 81 ASSERT_EQ(1U, list->GetSize());
82 Value* elt = NULL; 82 Value* elt = NULL;
83 ASSERT_TRUE(list->Get(0, &elt)); 83 ASSERT_TRUE(list->Get(0, &elt));
84 int value = 0; 84 int value = 0;
85 ASSERT_TRUE(elt && elt->GetAsInteger(&value)); 85 ASSERT_TRUE(elt && elt->GetAsInteger(&value));
86 ASSERT_EQ(1, value); 86 ASSERT_EQ(1, value);
87 } 87 }
88 88
89 // Test proper JSON [de]serialization from string is working. 89 // Test proper JSON [de]serialization from string is working.
rvargas (doing something else) 2015/02/25 03:38:24 nit: deserialization
90 TEST(JSONValueSerializerTest, ReadProperJSONFromString) { 90 TEST(JSONValueSerializerTest, ReadProperJSONFromString) {
rvargas (doing something else) 2015/02/25 03:38:24 nit: Deserializer. (& others)
prashant.hiremath 2015/02/25 06:01:37 Hi Ricardo, Thanks for the review. I'll change th
Matt Giuca 2015/02/25 06:11:40 I think the test name should probably match the fi
rvargas (doing something else) 2015/02/25 19:00:15 While I agree that in general the test name matche
Matt Giuca 2015/02/25 23:20:49 Acknowledged.
91 // Try to deserialize it through the serializer. 91 // Try to deserialize it through the serializer.
92 JSONStringValueSerializer str_deserializer(kProperJSON); 92 JSONStringValueDeserializer str_deserializer(kProperJSON);
93 93
94 int error_code = 0; 94 int error_code = 0;
95 std::string error_message; 95 std::string error_message;
96 scoped_ptr<Value> value(
97 str_deserializer.Deserialize(&error_code, &error_message));
98 ASSERT_TRUE(value.get());
99 ASSERT_EQ(0, error_code);
100 ASSERT_TRUE(error_message.empty());
101 // Verify if the same JSON is still there.
102 CheckJSONIsStillTheSame(*value);
103 }
104
105 // Test proper JSON deserialization from a string pointer is working.
106 TEST(JSONValueSerializerTest, ReadProperJSONFromStringPointer) {
107 // Try to deserialize a string pointer through the serializer. (This exercises
108 // a separate code path to passing a StringPiece.)
109 std::string proper_json(kProperJSON);
110 JSONStringValueSerializer str_deserializer(&proper_json);
111
112 int error_code = 0;
113 std::string error_message;
114 scoped_ptr<Value> value( 96 scoped_ptr<Value> value(
115 str_deserializer.Deserialize(&error_code, &error_message)); 97 str_deserializer.Deserialize(&error_code, &error_message));
116 ASSERT_TRUE(value.get()); 98 ASSERT_TRUE(value.get());
117 ASSERT_EQ(0, error_code); 99 ASSERT_EQ(0, error_code);
118 ASSERT_TRUE(error_message.empty()); 100 ASSERT_TRUE(error_message.empty());
119 // Verify if the same JSON is still there. 101 // Verify if the same JSON is still there.
120 CheckJSONIsStillTheSame(*value); 102 CheckJSONIsStillTheSame(*value);
121 } 103 }
122 104
123 // Test proper JSON deserialization from a StringPiece substring. 105 // Test proper JSON deserialization from a StringPiece substring.
124 TEST(JSONValueSerializerTest, ReadProperJSONFromStringPiece) { 106 TEST(JSONValueSerializerTest, ReadProperJSONFromStringPiece) {
125 // Create a StringPiece for the substring of kProperJSONPadded that matches 107 // Create a StringPiece for the substring of kProperJSONPadded that matches
126 // kProperJSON. 108 // kProperJSON.
127 base::StringPiece proper_json(kProperJSONPadded); 109 base::StringPiece proper_json(kProperJSONPadded);
128 proper_json = proper_json.substr(5, proper_json.length() - 10); 110 proper_json = proper_json.substr(5, proper_json.length() - 10);
129 JSONStringValueSerializer str_deserializer(proper_json); 111 JSONStringValueDeserializer str_deserializer(proper_json);
130 112
131 int error_code = 0; 113 int error_code = 0;
132 std::string error_message; 114 std::string error_message;
133 scoped_ptr<Value> value( 115 scoped_ptr<Value> value(
134 str_deserializer.Deserialize(&error_code, &error_message)); 116 str_deserializer.Deserialize(&error_code, &error_message));
135 ASSERT_TRUE(value.get()); 117 ASSERT_TRUE(value.get());
136 ASSERT_EQ(0, error_code); 118 ASSERT_EQ(0, error_code);
137 ASSERT_TRUE(error_message.empty()); 119 ASSERT_TRUE(error_message.empty());
138 // Verify if the same JSON is still there. 120 // Verify if the same JSON is still there.
139 CheckJSONIsStillTheSame(*value); 121 CheckJSONIsStillTheSame(*value);
140 } 122 }
141 123
142 // Test that trialing commas are only properly deserialized from string when 124 // Test that trialing commas are only properly deserialized from string when
143 // the proper flag for that is set. 125 // the proper flag for that is set.
144 TEST(JSONValueSerializerTest, ReadJSONWithTrailingCommasFromString) { 126 TEST(JSONValueSerializerTest, ReadJSONWithTrailingCommasFromString) {
145 // Try to deserialize it through the serializer. 127 // Try to deserialize it through the serializer.
146 JSONStringValueSerializer str_deserializer(kProperJSONWithCommas); 128 JSONStringValueDeserializer str_deserializer(kProperJSONWithCommas);
147 129
148 int error_code = 0; 130 int error_code = 0;
149 std::string error_message; 131 std::string error_message;
150 scoped_ptr<Value> value( 132 scoped_ptr<Value> value(
151 str_deserializer.Deserialize(&error_code, &error_message)); 133 str_deserializer.Deserialize(&error_code, &error_message));
152 ASSERT_FALSE(value.get()); 134 ASSERT_FALSE(value.get());
153 ASSERT_NE(0, error_code); 135 ASSERT_NE(0, error_code);
154 ASSERT_FALSE(error_message.empty()); 136 ASSERT_FALSE(error_message.empty());
155 // Now the flag is set and it must pass. 137 // Now the flag is set and it must pass.
156 str_deserializer.set_allow_trailing_comma(true); 138 str_deserializer.set_allow_trailing_comma(true);
157 value.reset(str_deserializer.Deserialize(&error_code, &error_message)); 139 value.reset(str_deserializer.Deserialize(&error_code, &error_message));
158 ASSERT_TRUE(value.get()); 140 ASSERT_TRUE(value.get());
159 ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code); 141 ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code);
160 // Verify if the same JSON is still there. 142 // Verify if the same JSON is still there.
161 CheckJSONIsStillTheSame(*value); 143 CheckJSONIsStillTheSame(*value);
162 } 144 }
163 145
164 // Test proper JSON [de]serialization from file is working. 146 // Test proper JSON [de]serialization from file is working.
165 TEST(JSONValueSerializerTest, ReadProperJSONFromFile) { 147 TEST(JSONValueSerializerTest, ReadProperJSONFromFile) {
166 ScopedTempDir tempdir; 148 ScopedTempDir tempdir;
167 ASSERT_TRUE(tempdir.CreateUniqueTempDir()); 149 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
168 // Write it down in the file. 150 // Write it down in the file.
169 FilePath temp_file(tempdir.path().AppendASCII("test.json")); 151 FilePath temp_file(tempdir.path().AppendASCII("test.json"));
170 ASSERT_EQ(static_cast<int>(strlen(kProperJSON)), 152 ASSERT_EQ(static_cast<int>(strlen(kProperJSON)),
171 WriteFile(temp_file, kProperJSON, strlen(kProperJSON))); 153 WriteFile(temp_file, kProperJSON, strlen(kProperJSON)));
172 154
173 // Try to deserialize it through the serializer. 155 // Try to deserialize it through the serializer.
174 JSONFileValueSerializer file_deserializer(temp_file); 156 JSONFileValueDeserializer file_deserializer(temp_file);
175 157
176 int error_code = 0; 158 int error_code = 0;
177 std::string error_message; 159 std::string error_message;
178 scoped_ptr<Value> value( 160 scoped_ptr<Value> value(
179 file_deserializer.Deserialize(&error_code, &error_message)); 161 file_deserializer.Deserialize(&error_code, &error_message));
180 ASSERT_TRUE(value.get()); 162 ASSERT_TRUE(value.get());
181 ASSERT_EQ(0, error_code); 163 ASSERT_EQ(0, error_code);
182 ASSERT_TRUE(error_message.empty()); 164 ASSERT_TRUE(error_message.empty());
183 // Verify if the same JSON is still there. 165 // Verify if the same JSON is still there.
184 CheckJSONIsStillTheSame(*value); 166 CheckJSONIsStillTheSame(*value);
185 } 167 }
186 168
187 // Test that trialing commas are only properly deserialized from file when 169 // Test that trialing commas are only properly deserialized from file when
188 // the proper flag for that is set. 170 // the proper flag for that is set.
189 TEST(JSONValueSerializerTest, ReadJSONWithCommasFromFile) { 171 TEST(JSONValueSerializerTest, ReadJSONWithCommasFromFile) {
190 ScopedTempDir tempdir; 172 ScopedTempDir tempdir;
191 ASSERT_TRUE(tempdir.CreateUniqueTempDir()); 173 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
192 // Write it down in the file. 174 // Write it down in the file.
193 FilePath temp_file(tempdir.path().AppendASCII("test.json")); 175 FilePath temp_file(tempdir.path().AppendASCII("test.json"));
194 ASSERT_EQ(static_cast<int>(strlen(kProperJSONWithCommas)), 176 ASSERT_EQ(static_cast<int>(strlen(kProperJSONWithCommas)),
195 WriteFile(temp_file, kProperJSONWithCommas, 177 WriteFile(temp_file, kProperJSONWithCommas,
196 strlen(kProperJSONWithCommas))); 178 strlen(kProperJSONWithCommas)));
197 179
198 // Try to deserialize it through the serializer. 180 // Try to deserialize it through the serializer.
199 JSONFileValueSerializer file_deserializer(temp_file); 181 JSONFileValueDeserializer file_deserializer(temp_file);
200 // This must fail without the proper flag. 182 // This must fail without the proper flag.
201 int error_code = 0; 183 int error_code = 0;
202 std::string error_message; 184 std::string error_message;
203 scoped_ptr<Value> value( 185 scoped_ptr<Value> value(
204 file_deserializer.Deserialize(&error_code, &error_message)); 186 file_deserializer.Deserialize(&error_code, &error_message));
205 ASSERT_FALSE(value.get()); 187 ASSERT_FALSE(value.get());
206 ASSERT_NE(0, error_code); 188 ASSERT_NE(0, error_code);
207 ASSERT_FALSE(error_message.empty()); 189 ASSERT_FALSE(error_message.empty());
208 // Now the flag is set and it must pass. 190 // Now the flag is set and it must pass.
209 file_deserializer.set_allow_trailing_comma(true); 191 file_deserializer.set_allow_trailing_comma(true);
210 value.reset(file_deserializer.Deserialize(&error_code, &error_message)); 192 value.reset(file_deserializer.Deserialize(&error_code, &error_message));
211 ASSERT_TRUE(value.get()); 193 ASSERT_TRUE(value.get());
212 ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code); 194 ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code);
213 // Verify if the same JSON is still there. 195 // Verify if the same JSON is still there.
214 CheckJSONIsStillTheSame(*value); 196 CheckJSONIsStillTheSame(*value);
215 } 197 }
216 198
217 TEST(JSONValueSerializerTest, Roundtrip) { 199 TEST(JSONValueSerializerTest, Roundtrip) {
218 static const char kOriginalSerialization[] = 200 static const char kOriginalSerialization[] =
219 "{\"bool\":true,\"double\":3.14,\"int\":42,\"list\":[1,2],\"null\":null}"; 201 "{\"bool\":true,\"double\":3.14,\"int\":42,\"list\":[1,2],\"null\":null}";
220 JSONStringValueSerializer serializer(kOriginalSerialization); 202 JSONStringValueDeserializer deserializer(kOriginalSerialization);
221 scoped_ptr<Value> root(serializer.Deserialize(NULL, NULL)); 203 scoped_ptr<Value> root(deserializer.Deserialize(NULL, NULL));
222 ASSERT_TRUE(root.get()); 204 ASSERT_TRUE(root.get());
223 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 205 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
224 206
225 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); 207 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get());
226 208
227 Value* null_value = NULL; 209 Value* null_value = NULL;
228 ASSERT_TRUE(root_dict->Get("null", &null_value)); 210 ASSERT_TRUE(root_dict->Get("null", &null_value));
229 ASSERT_TRUE(null_value); 211 ASSERT_TRUE(null_value);
230 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL)); 212 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL));
231 213
232 bool bool_value = false; 214 bool bool_value = false;
233 ASSERT_TRUE(root_dict->GetBoolean("bool", &bool_value)); 215 ASSERT_TRUE(root_dict->GetBoolean("bool", &bool_value));
234 ASSERT_TRUE(bool_value); 216 ASSERT_TRUE(bool_value);
235 217
236 int int_value = 0; 218 int int_value = 0;
237 ASSERT_TRUE(root_dict->GetInteger("int", &int_value)); 219 ASSERT_TRUE(root_dict->GetInteger("int", &int_value));
238 ASSERT_EQ(42, int_value); 220 ASSERT_EQ(42, int_value);
239 221
240 double double_value = 0.0; 222 double double_value = 0.0;
241 ASSERT_TRUE(root_dict->GetDouble("double", &double_value)); 223 ASSERT_TRUE(root_dict->GetDouble("double", &double_value));
242 ASSERT_DOUBLE_EQ(3.14, double_value); 224 ASSERT_DOUBLE_EQ(3.14, double_value);
243 225
244 // We shouldn't be able to write using this serializer, since it was
245 // initialized with a const string.
246 ASSERT_FALSE(serializer.Serialize(*root_dict));
247
248 std::string test_serialization; 226 std::string test_serialization;
249 JSONStringValueSerializer mutable_serializer(&test_serialization); 227 JSONStringValueSerializer mutable_serializer(&test_serialization);
250 ASSERT_TRUE(mutable_serializer.Serialize(*root_dict)); 228 ASSERT_TRUE(mutable_serializer.Serialize(*root_dict));
251 ASSERT_EQ(kOriginalSerialization, test_serialization); 229 ASSERT_EQ(kOriginalSerialization, test_serialization);
252 230
253 mutable_serializer.set_pretty_print(true); 231 mutable_serializer.set_pretty_print(true);
254 ASSERT_TRUE(mutable_serializer.Serialize(*root_dict)); 232 ASSERT_TRUE(mutable_serializer.Serialize(*root_dict));
255 // JSON output uses a different newline style on Windows than on other 233 // JSON output uses a different newline style on Windows than on other
256 // platforms. 234 // platforms.
257 #if defined(OS_WIN) 235 #if defined(OS_WIN)
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 root.SetString("web", test); 302 root.SetString("web", test);
325 303
326 static const char kExpected[] = "{\"web\":\"\xE7\xBD\x91\xE9\xA1\xB5\"}"; 304 static const char kExpected[] = "{\"web\":\"\xE7\xBD\x91\xE9\xA1\xB5\"}";
327 305
328 std::string actual; 306 std::string actual;
329 JSONStringValueSerializer serializer(&actual); 307 JSONStringValueSerializer serializer(&actual);
330 ASSERT_TRUE(serializer.Serialize(root)); 308 ASSERT_TRUE(serializer.Serialize(root));
331 ASSERT_EQ(kExpected, actual); 309 ASSERT_EQ(kExpected, actual);
332 310
333 // escaped ascii text -> json 311 // escaped ascii text -> json
334 JSONStringValueSerializer deserializer(kExpected); 312 JSONStringValueDeserializer deserializer(kExpected);
335 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL, NULL)); 313 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL, NULL));
336 ASSERT_TRUE(deserial_root.get()); 314 ASSERT_TRUE(deserial_root.get());
337 DictionaryValue* dict_root = 315 DictionaryValue* dict_root =
338 static_cast<DictionaryValue*>(deserial_root.get()); 316 static_cast<DictionaryValue*>(deserial_root.get());
339 string16 web_value; 317 string16 web_value;
340 ASSERT_TRUE(dict_root->GetString("web", &web_value)); 318 ASSERT_TRUE(dict_root->GetString("web", &web_value));
341 ASSERT_EQ(test, web_value); 319 ASSERT_EQ(test, web_value);
342 } 320 }
343 321
344 TEST(JSONValueSerializerTest, HexStrings) { 322 TEST(JSONValueSerializerTest, HexStrings) {
345 // hex string json -> escaped ascii text 323 // hex string json -> escaped ascii text
346 DictionaryValue root; 324 DictionaryValue root;
347 string16 test(WideToUTF16(L"\x01\x02")); 325 string16 test(WideToUTF16(L"\x01\x02"));
348 root.SetString("test", test); 326 root.SetString("test", test);
349 327
350 static const char kExpected[] = "{\"test\":\"\\u0001\\u0002\"}"; 328 static const char kExpected[] = "{\"test\":\"\\u0001\\u0002\"}";
351 329
352 std::string actual; 330 std::string actual;
353 JSONStringValueSerializer serializer(&actual); 331 JSONStringValueSerializer serializer(&actual);
354 ASSERT_TRUE(serializer.Serialize(root)); 332 ASSERT_TRUE(serializer.Serialize(root));
355 ASSERT_EQ(kExpected, actual); 333 ASSERT_EQ(kExpected, actual);
356 334
357 // escaped ascii text -> json 335 // escaped ascii text -> json
358 JSONStringValueSerializer deserializer(kExpected); 336 JSONStringValueDeserializer deserializer(kExpected);
359 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL, NULL)); 337 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL, NULL));
360 ASSERT_TRUE(deserial_root.get()); 338 ASSERT_TRUE(deserial_root.get());
361 DictionaryValue* dict_root = 339 DictionaryValue* dict_root =
362 static_cast<DictionaryValue*>(deserial_root.get()); 340 static_cast<DictionaryValue*>(deserial_root.get());
363 string16 test_value; 341 string16 test_value;
364 ASSERT_TRUE(dict_root->GetString("test", &test_value)); 342 ASSERT_TRUE(dict_root->GetString("test", &test_value));
365 ASSERT_EQ(test, test_value); 343 ASSERT_EQ(test, test_value);
366 344
367 // Test converting escaped regular chars 345 // Test converting escaped regular chars
368 static const char kEscapedChars[] = "{\"test\":\"\\u0067\\u006f\"}"; 346 static const char kEscapedChars[] = "{\"test\":\"\\u0067\\u006f\"}";
369 JSONStringValueSerializer deserializer2(kEscapedChars); 347 JSONStringValueDeserializer deserializer2(kEscapedChars);
370 deserial_root.reset(deserializer2.Deserialize(NULL, NULL)); 348 deserial_root.reset(deserializer2.Deserialize(NULL, NULL));
371 ASSERT_TRUE(deserial_root.get()); 349 ASSERT_TRUE(deserial_root.get());
372 dict_root = static_cast<DictionaryValue*>(deserial_root.get()); 350 dict_root = static_cast<DictionaryValue*>(deserial_root.get());
373 ASSERT_TRUE(dict_root->GetString("test", &test_value)); 351 ASSERT_TRUE(dict_root->GetString("test", &test_value));
374 ASSERT_EQ(ASCIIToUTF16("go"), test_value); 352 ASSERT_EQ(ASCIIToUTF16("go"), test_value);
375 } 353 }
376 354
377 TEST(JSONValueSerializerTest, AllowTrailingComma) { 355 TEST(JSONValueSerializerTest, AllowTrailingComma) {
378 scoped_ptr<Value> root; 356 scoped_ptr<Value> root;
379 scoped_ptr<Value> root_expected; 357 scoped_ptr<Value> root_expected;
380 static const char kTestWithCommas[] = "{\"key\": [true,],}"; 358 static const char kTestWithCommas[] = "{\"key\": [true,],}";
381 static const char kTestNoCommas[] = "{\"key\": [true]}"; 359 static const char kTestNoCommas[] = "{\"key\": [true]}";
382 360
383 JSONStringValueSerializer serializer(kTestWithCommas); 361 JSONStringValueDeserializer deserializer(kTestWithCommas);
384 serializer.set_allow_trailing_comma(true); 362 deserializer.set_allow_trailing_comma(true);
385 JSONStringValueSerializer serializer_expected(kTestNoCommas); 363 JSONStringValueDeserializer deserializer_expected(kTestNoCommas);
386 root.reset(serializer.Deserialize(NULL, NULL)); 364 root.reset(deserializer.Deserialize(NULL, NULL));
387 ASSERT_TRUE(root.get()); 365 ASSERT_TRUE(root.get());
388 root_expected.reset(serializer_expected.Deserialize(NULL, NULL)); 366 root_expected.reset(deserializer_expected.Deserialize(NULL, NULL));
389 ASSERT_TRUE(root_expected.get()); 367 ASSERT_TRUE(root_expected.get());
390 ASSERT_TRUE(root->Equals(root_expected.get())); 368 ASSERT_TRUE(root->Equals(root_expected.get()));
391 } 369 }
392 370
393 TEST(JSONValueSerializerTest, JSONReaderComments) { 371 TEST(JSONValueSerializerTest, JSONReaderComments) {
394 ValidateJsonList("[ // 2, 3, ignore me ] \n1 ]"); 372 ValidateJsonList("[ // 2, 3, ignore me ] \n1 ]");
395 ValidateJsonList("[ /* 2, \n3, ignore me ]*/ \n1 ]"); 373 ValidateJsonList("[ /* 2, \n3, ignore me ]*/ \n1 ]");
396 ValidateJsonList("//header\n[ // 2, \n// 3, \n1 ]// footer"); 374 ValidateJsonList("//header\n[ // 2, \n// 3, \n1 ]// footer");
397 ValidateJsonList("/*\n[ // 2, \n// 3, \n1 ]*/[1]"); 375 ValidateJsonList("/*\n[ // 2, \n// 3, \n1 ]*/[1]");
398 ValidateJsonList("[ 1 /* one */ ] /* end */"); 376 ValidateJsonList("[ 1 /* one */ ] /* end */");
(...skipping 29 matching lines...) Expand all
428 }; 406 };
429 407
430 TEST_F(JSONFileValueSerializerTest, Roundtrip) { 408 TEST_F(JSONFileValueSerializerTest, Roundtrip) {
431 base::FilePath original_file_path; 409 base::FilePath original_file_path;
432 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path)); 410 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path));
433 original_file_path = 411 original_file_path =
434 original_file_path.Append(FILE_PATH_LITERAL("serializer_test.json")); 412 original_file_path.Append(FILE_PATH_LITERAL("serializer_test.json"));
435 413
436 ASSERT_TRUE(PathExists(original_file_path)); 414 ASSERT_TRUE(PathExists(original_file_path));
437 415
438 JSONFileValueSerializer deserializer(original_file_path); 416 JSONFileValueDeserializer deserializer(original_file_path);
439 scoped_ptr<Value> root; 417 scoped_ptr<Value> root;
440 root.reset(deserializer.Deserialize(NULL, NULL)); 418 root.reset(deserializer.Deserialize(NULL, NULL));
441 419
442 ASSERT_TRUE(root.get()); 420 ASSERT_TRUE(root.get());
443 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 421 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
444 422
445 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); 423 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get());
446 424
447 Value* null_value = NULL; 425 Value* null_value = NULL;
448 ASSERT_TRUE(root_dict->Get("null", &null_value)); 426 ASSERT_TRUE(root_dict->Get("null", &null_value));
(...skipping 27 matching lines...) Expand all
476 } 454 }
477 455
478 TEST_F(JSONFileValueSerializerTest, RoundtripNested) { 456 TEST_F(JSONFileValueSerializerTest, RoundtripNested) {
479 base::FilePath original_file_path; 457 base::FilePath original_file_path;
480 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path)); 458 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path));
481 original_file_path = original_file_path.Append( 459 original_file_path = original_file_path.Append(
482 FILE_PATH_LITERAL("serializer_nested_test.json")); 460 FILE_PATH_LITERAL("serializer_nested_test.json"));
483 461
484 ASSERT_TRUE(PathExists(original_file_path)); 462 ASSERT_TRUE(PathExists(original_file_path));
485 463
486 JSONFileValueSerializer deserializer(original_file_path); 464 JSONFileValueDeserializer deserializer(original_file_path);
487 scoped_ptr<Value> root; 465 scoped_ptr<Value> root;
488 root.reset(deserializer.Deserialize(NULL, NULL)); 466 root.reset(deserializer.Deserialize(NULL, NULL));
489 ASSERT_TRUE(root.get()); 467 ASSERT_TRUE(root.get());
490 468
491 // Now try writing. 469 // Now try writing.
492 base::FilePath written_file_path = temp_dir_.path().Append( 470 base::FilePath written_file_path = temp_dir_.path().Append(
493 FILE_PATH_LITERAL("test_output.json")); 471 FILE_PATH_LITERAL("test_output.json"));
494 472
495 ASSERT_FALSE(PathExists(written_file_path)); 473 ASSERT_FALSE(PathExists(written_file_path));
496 JSONFileValueSerializer serializer(written_file_path); 474 JSONFileValueSerializer serializer(written_file_path);
497 ASSERT_TRUE(serializer.Serialize(*root)); 475 ASSERT_TRUE(serializer.Serialize(*root));
498 ASSERT_TRUE(PathExists(written_file_path)); 476 ASSERT_TRUE(PathExists(written_file_path));
499 477
500 // Now compare file contents. 478 // Now compare file contents.
501 EXPECT_TRUE(TextContentsEqual(original_file_path, written_file_path)); 479 EXPECT_TRUE(TextContentsEqual(original_file_path, written_file_path));
502 EXPECT_TRUE(base::DeleteFile(written_file_path, false)); 480 EXPECT_TRUE(base::DeleteFile(written_file_path, false));
503 } 481 }
504 482
505 TEST_F(JSONFileValueSerializerTest, NoWhitespace) { 483 TEST_F(JSONFileValueSerializerTest, NoWhitespace) {
506 base::FilePath source_file_path; 484 base::FilePath source_file_path;
507 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &source_file_path)); 485 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &source_file_path));
508 source_file_path = source_file_path.Append( 486 source_file_path = source_file_path.Append(
509 FILE_PATH_LITERAL("serializer_test_nowhitespace.json")); 487 FILE_PATH_LITERAL("serializer_test_nowhitespace.json"));
510 ASSERT_TRUE(PathExists(source_file_path)); 488 ASSERT_TRUE(PathExists(source_file_path));
511 JSONFileValueSerializer serializer(source_file_path); 489 JSONFileValueDeserializer deserializer(source_file_path);
512 scoped_ptr<Value> root; 490 scoped_ptr<Value> root;
513 root.reset(serializer.Deserialize(NULL, NULL)); 491 root.reset(deserializer.Deserialize(NULL, NULL));
514 ASSERT_TRUE(root.get()); 492 ASSERT_TRUE(root.get());
515 } 493 }
516 494
517 } // namespace 495 } // namespace
518 496
519 } // namespace base 497 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698