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

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 comment. 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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
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.
90 TEST(JSONValueSerializerTest, ReadProperJSONFromString) { 90 TEST(JSONValueSerializerTest, ReadProperJSONFromString) {
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( 96 scoped_ptr<Value> value(
97 str_deserializer.Deserialize(&error_code, &error_message)); 97 str_deserializer.Deserialize(&error_code, &error_message));
98 ASSERT_TRUE(value.get()); 98 ASSERT_TRUE(value.get());
99 ASSERT_EQ(0, error_code); 99 ASSERT_EQ(0, error_code);
100 ASSERT_TRUE(error_message.empty()); 100 ASSERT_TRUE(error_message.empty());
101 // Verify if the same JSON is still there. 101 // Verify if the same JSON is still there.
102 CheckJSONIsStillTheSame(*value); 102 CheckJSONIsStillTheSame(*value);
103 } 103 }
104 104
105 // Test proper JSON deserialization from a string pointer is working. 105 // Test proper JSON deserialization from a string pointer is working.
106 TEST(JSONValueSerializerTest, ReadProperJSONFromStringPointer) { 106 TEST(JSONValueSerializerTest, ReadProperJSONFromStringPointer) {
Matt Giuca 2015/02/23 02:41:24 Just delete this test. It exists solely to test th
prashant.hiremath 2015/02/24 12:03:17 This was edited blindly, so one good learning for
107 // Try to deserialize a string pointer through the serializer. (This exercises 107 // Try to deserialize a string pointer through the serializer. (This exercises
108 // a separate code path to passing a StringPiece.) 108 // a separate code path to passing a StringPiece.)
109 std::string proper_json(kProperJSON); 109 std::string proper_json(kProperJSON);
110 JSONStringValueSerializer str_deserializer(&proper_json); 110 JSONStringValueDeserializer str_deserializer(proper_json);
111 111
112 int error_code = 0; 112 int error_code = 0;
113 std::string error_message; 113 std::string error_message;
114 scoped_ptr<Value> value( 114 scoped_ptr<Value> value(
115 str_deserializer.Deserialize(&error_code, &error_message)); 115 str_deserializer.Deserialize(&error_code, &error_message));
116 ASSERT_TRUE(value.get()); 116 ASSERT_TRUE(value.get());
117 ASSERT_EQ(0, error_code); 117 ASSERT_EQ(0, error_code);
118 ASSERT_TRUE(error_message.empty()); 118 ASSERT_TRUE(error_message.empty());
119 // Verify if the same JSON is still there. 119 // Verify if the same JSON is still there.
120 CheckJSONIsStillTheSame(*value); 120 CheckJSONIsStillTheSame(*value);
121 } 121 }
122 122
123 // Test proper JSON deserialization from a StringPiece substring. 123 // Test proper JSON deserialization from a StringPiece substring.
124 TEST(JSONValueSerializerTest, ReadProperJSONFromStringPiece) { 124 TEST(JSONValueSerializerTest, ReadProperJSONFromStringPiece) {
125 // Create a StringPiece for the substring of kProperJSONPadded that matches 125 // Create a StringPiece for the substring of kProperJSONPadded that matches
126 // kProperJSON. 126 // kProperJSON.
127 base::StringPiece proper_json(kProperJSONPadded); 127 base::StringPiece proper_json(kProperJSONPadded);
128 proper_json = proper_json.substr(5, proper_json.length() - 10); 128 proper_json = proper_json.substr(5, proper_json.length() - 10);
129 JSONStringValueSerializer str_deserializer(proper_json); 129 JSONStringValueDeserializer str_deserializer(proper_json);
130 130
131 int error_code = 0; 131 int error_code = 0;
132 std::string error_message; 132 std::string error_message;
133 scoped_ptr<Value> value( 133 scoped_ptr<Value> value(
134 str_deserializer.Deserialize(&error_code, &error_message)); 134 str_deserializer.Deserialize(&error_code, &error_message));
135 ASSERT_TRUE(value.get()); 135 ASSERT_TRUE(value.get());
136 ASSERT_EQ(0, error_code); 136 ASSERT_EQ(0, error_code);
137 ASSERT_TRUE(error_message.empty()); 137 ASSERT_TRUE(error_message.empty());
138 // Verify if the same JSON is still there. 138 // Verify if the same JSON is still there.
139 CheckJSONIsStillTheSame(*value); 139 CheckJSONIsStillTheSame(*value);
140 } 140 }
141 141
142 // Test that trialing commas are only properly deserialized from string when 142 // Test that trialing commas are only properly deserialized from string when
143 // the proper flag for that is set. 143 // the proper flag for that is set.
144 TEST(JSONValueSerializerTest, ReadJSONWithTrailingCommasFromString) { 144 TEST(JSONValueSerializerTest, ReadJSONWithTrailingCommasFromString) {
145 // Try to deserialize it through the serializer. 145 // Try to deserialize it through the serializer.
146 JSONStringValueSerializer str_deserializer(kProperJSONWithCommas); 146 JSONStringValueDeserializer str_deserializer(kProperJSONWithCommas);
147 147
148 int error_code = 0; 148 int error_code = 0;
149 std::string error_message; 149 std::string error_message;
150 scoped_ptr<Value> value( 150 scoped_ptr<Value> value(
151 str_deserializer.Deserialize(&error_code, &error_message)); 151 str_deserializer.Deserialize(&error_code, &error_message));
152 ASSERT_FALSE(value.get()); 152 ASSERT_FALSE(value.get());
153 ASSERT_NE(0, error_code); 153 ASSERT_NE(0, error_code);
154 ASSERT_FALSE(error_message.empty()); 154 ASSERT_FALSE(error_message.empty());
155 // Now the flag is set and it must pass. 155 // Now the flag is set and it must pass.
156 str_deserializer.set_allow_trailing_comma(true); 156 str_deserializer.set_allow_trailing_comma(true);
157 value.reset(str_deserializer.Deserialize(&error_code, &error_message)); 157 value.reset(str_deserializer.Deserialize(&error_code, &error_message));
158 ASSERT_TRUE(value.get()); 158 ASSERT_TRUE(value.get());
159 ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code); 159 ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code);
160 // Verify if the same JSON is still there. 160 // Verify if the same JSON is still there.
161 CheckJSONIsStillTheSame(*value); 161 CheckJSONIsStillTheSame(*value);
162 } 162 }
163 163
164 // Test proper JSON [de]serialization from file is working. 164 // Test proper JSON [de]serialization from file is working.
165 TEST(JSONValueSerializerTest, ReadProperJSONFromFile) { 165 TEST(JSONValueSerializerTest, ReadProperJSONFromFile) {
166 ScopedTempDir tempdir; 166 ScopedTempDir tempdir;
167 ASSERT_TRUE(tempdir.CreateUniqueTempDir()); 167 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
168 // Write it down in the file. 168 // Write it down in the file.
169 FilePath temp_file(tempdir.path().AppendASCII("test.json")); 169 FilePath temp_file(tempdir.path().AppendASCII("test.json"));
170 ASSERT_EQ(static_cast<int>(strlen(kProperJSON)), 170 ASSERT_EQ(static_cast<int>(strlen(kProperJSON)),
171 WriteFile(temp_file, kProperJSON, strlen(kProperJSON))); 171 WriteFile(temp_file, kProperJSON, strlen(kProperJSON)));
172 172
173 // Try to deserialize it through the serializer. 173 // Try to deserialize it through the serializer.
174 JSONFileValueSerializer file_deserializer(temp_file); 174 JSONFileValueDeserializer file_deserializer(temp_file);
175 175
176 int error_code = 0; 176 int error_code = 0;
177 std::string error_message; 177 std::string error_message;
178 scoped_ptr<Value> value( 178 scoped_ptr<Value> value(
179 file_deserializer.Deserialize(&error_code, &error_message)); 179 file_deserializer.Deserialize(&error_code, &error_message));
180 ASSERT_TRUE(value.get()); 180 ASSERT_TRUE(value.get());
181 ASSERT_EQ(0, error_code); 181 ASSERT_EQ(0, error_code);
182 ASSERT_TRUE(error_message.empty()); 182 ASSERT_TRUE(error_message.empty());
183 // Verify if the same JSON is still there. 183 // Verify if the same JSON is still there.
184 CheckJSONIsStillTheSame(*value); 184 CheckJSONIsStillTheSame(*value);
185 } 185 }
186 186
187 // Test that trialing commas are only properly deserialized from file when 187 // Test that trialing commas are only properly deserialized from file when
188 // the proper flag for that is set. 188 // the proper flag for that is set.
189 TEST(JSONValueSerializerTest, ReadJSONWithCommasFromFile) { 189 TEST(JSONValueSerializerTest, ReadJSONWithCommasFromFile) {
190 ScopedTempDir tempdir; 190 ScopedTempDir tempdir;
191 ASSERT_TRUE(tempdir.CreateUniqueTempDir()); 191 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
192 // Write it down in the file. 192 // Write it down in the file.
193 FilePath temp_file(tempdir.path().AppendASCII("test.json")); 193 FilePath temp_file(tempdir.path().AppendASCII("test.json"));
194 ASSERT_EQ(static_cast<int>(strlen(kProperJSONWithCommas)), 194 ASSERT_EQ(static_cast<int>(strlen(kProperJSONWithCommas)),
195 WriteFile(temp_file, kProperJSONWithCommas, 195 WriteFile(temp_file, kProperJSONWithCommas,
196 strlen(kProperJSONWithCommas))); 196 strlen(kProperJSONWithCommas)));
197 197
198 // Try to deserialize it through the serializer. 198 // Try to deserialize it through the serializer.
199 JSONFileValueSerializer file_deserializer(temp_file); 199 JSONFileValueDeserializer file_deserializer(temp_file);
200 // This must fail without the proper flag. 200 // This must fail without the proper flag.
201 int error_code = 0; 201 int error_code = 0;
202 std::string error_message; 202 std::string error_message;
203 scoped_ptr<Value> value( 203 scoped_ptr<Value> value(
204 file_deserializer.Deserialize(&error_code, &error_message)); 204 file_deserializer.Deserialize(&error_code, &error_message));
205 ASSERT_FALSE(value.get()); 205 ASSERT_FALSE(value.get());
206 ASSERT_NE(0, error_code); 206 ASSERT_NE(0, error_code);
207 ASSERT_FALSE(error_message.empty()); 207 ASSERT_FALSE(error_message.empty());
208 // Now the flag is set and it must pass. 208 // Now the flag is set and it must pass.
209 file_deserializer.set_allow_trailing_comma(true); 209 file_deserializer.set_allow_trailing_comma(true);
210 value.reset(file_deserializer.Deserialize(&error_code, &error_message)); 210 value.reset(file_deserializer.Deserialize(&error_code, &error_message));
211 ASSERT_TRUE(value.get()); 211 ASSERT_TRUE(value.get());
212 ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code); 212 ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code);
213 // Verify if the same JSON is still there. 213 // Verify if the same JSON is still there.
214 CheckJSONIsStillTheSame(*value); 214 CheckJSONIsStillTheSame(*value);
215 } 215 }
216 216
217 TEST(JSONValueSerializerTest, Roundtrip) { 217 TEST(JSONValueSerializerTest, Roundtrip) {
218 static const char kOriginalSerialization[] = 218 static const char kOriginalSerialization[] =
219 "{\"bool\":true,\"double\":3.14,\"int\":42,\"list\":[1,2],\"null\":null}"; 219 "{\"bool\":true,\"double\":3.14,\"int\":42,\"list\":[1,2],\"null\":null}";
220 JSONStringValueSerializer serializer(kOriginalSerialization); 220 JSONStringValueDeserializer deserializer(kOriginalSerialization);
221 scoped_ptr<Value> root(serializer.Deserialize(NULL, NULL)); 221 scoped_ptr<Value> root(deserializer.Deserialize(NULL, NULL));
222 ASSERT_TRUE(root.get()); 222 ASSERT_TRUE(root.get());
223 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 223 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
224 224
225 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); 225 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get());
226 226
227 Value* null_value = NULL; 227 Value* null_value = NULL;
228 ASSERT_TRUE(root_dict->Get("null", &null_value)); 228 ASSERT_TRUE(root_dict->Get("null", &null_value));
229 ASSERT_TRUE(null_value); 229 ASSERT_TRUE(null_value);
230 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL)); 230 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL));
231 231
232 bool bool_value = false; 232 bool bool_value = false;
233 ASSERT_TRUE(root_dict->GetBoolean("bool", &bool_value)); 233 ASSERT_TRUE(root_dict->GetBoolean("bool", &bool_value));
234 ASSERT_TRUE(bool_value); 234 ASSERT_TRUE(bool_value);
235 235
236 int int_value = 0; 236 int int_value = 0;
237 ASSERT_TRUE(root_dict->GetInteger("int", &int_value)); 237 ASSERT_TRUE(root_dict->GetInteger("int", &int_value));
238 ASSERT_EQ(42, int_value); 238 ASSERT_EQ(42, int_value);
239 239
240 double double_value = 0.0; 240 double double_value = 0.0;
241 ASSERT_TRUE(root_dict->GetDouble("double", &double_value)); 241 ASSERT_TRUE(root_dict->GetDouble("double", &double_value));
242 ASSERT_DOUBLE_EQ(3.14, double_value); 242 ASSERT_DOUBLE_EQ(3.14, double_value);
243 243
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; 244 std::string test_serialization;
249 JSONStringValueSerializer mutable_serializer(&test_serialization); 245 JSONStringValueSerializer mutable_serializer(&test_serialization);
250 ASSERT_TRUE(mutable_serializer.Serialize(*root_dict)); 246 ASSERT_TRUE(mutable_serializer.Serialize(*root_dict));
251 ASSERT_EQ(kOriginalSerialization, test_serialization); 247 ASSERT_EQ(kOriginalSerialization, test_serialization);
252 248
253 mutable_serializer.set_pretty_print(true); 249 mutable_serializer.set_pretty_print(true);
254 ASSERT_TRUE(mutable_serializer.Serialize(*root_dict)); 250 ASSERT_TRUE(mutable_serializer.Serialize(*root_dict));
255 // JSON output uses a different newline style on Windows than on other 251 // JSON output uses a different newline style on Windows than on other
256 // platforms. 252 // platforms.
257 #if defined(OS_WIN) 253 #if defined(OS_WIN)
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 root.SetString("web", test); 320 root.SetString("web", test);
325 321
326 static const char kExpected[] = "{\"web\":\"\xE7\xBD\x91\xE9\xA1\xB5\"}"; 322 static const char kExpected[] = "{\"web\":\"\xE7\xBD\x91\xE9\xA1\xB5\"}";
327 323
328 std::string actual; 324 std::string actual;
329 JSONStringValueSerializer serializer(&actual); 325 JSONStringValueSerializer serializer(&actual);
330 ASSERT_TRUE(serializer.Serialize(root)); 326 ASSERT_TRUE(serializer.Serialize(root));
331 ASSERT_EQ(kExpected, actual); 327 ASSERT_EQ(kExpected, actual);
332 328
333 // escaped ascii text -> json 329 // escaped ascii text -> json
334 JSONStringValueSerializer deserializer(kExpected); 330 JSONStringValueDeserializer deserializer(kExpected);
335 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL, NULL)); 331 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL, NULL));
336 ASSERT_TRUE(deserial_root.get()); 332 ASSERT_TRUE(deserial_root.get());
337 DictionaryValue* dict_root = 333 DictionaryValue* dict_root =
338 static_cast<DictionaryValue*>(deserial_root.get()); 334 static_cast<DictionaryValue*>(deserial_root.get());
339 string16 web_value; 335 string16 web_value;
340 ASSERT_TRUE(dict_root->GetString("web", &web_value)); 336 ASSERT_TRUE(dict_root->GetString("web", &web_value));
341 ASSERT_EQ(test, web_value); 337 ASSERT_EQ(test, web_value);
342 } 338 }
343 339
344 TEST(JSONValueSerializerTest, HexStrings) { 340 TEST(JSONValueSerializerTest, HexStrings) {
345 // hex string json -> escaped ascii text 341 // hex string json -> escaped ascii text
346 DictionaryValue root; 342 DictionaryValue root;
347 string16 test(WideToUTF16(L"\x01\x02")); 343 string16 test(WideToUTF16(L"\x01\x02"));
348 root.SetString("test", test); 344 root.SetString("test", test);
349 345
350 static const char kExpected[] = "{\"test\":\"\\u0001\\u0002\"}"; 346 static const char kExpected[] = "{\"test\":\"\\u0001\\u0002\"}";
351 347
352 std::string actual; 348 std::string actual;
353 JSONStringValueSerializer serializer(&actual); 349 JSONStringValueSerializer serializer(&actual);
354 ASSERT_TRUE(serializer.Serialize(root)); 350 ASSERT_TRUE(serializer.Serialize(root));
355 ASSERT_EQ(kExpected, actual); 351 ASSERT_EQ(kExpected, actual);
356 352
357 // escaped ascii text -> json 353 // escaped ascii text -> json
358 JSONStringValueSerializer deserializer(kExpected); 354 JSONStringValueDeserializer deserializer(kExpected);
359 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL, NULL)); 355 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL, NULL));
360 ASSERT_TRUE(deserial_root.get()); 356 ASSERT_TRUE(deserial_root.get());
361 DictionaryValue* dict_root = 357 DictionaryValue* dict_root =
362 static_cast<DictionaryValue*>(deserial_root.get()); 358 static_cast<DictionaryValue*>(deserial_root.get());
363 string16 test_value; 359 string16 test_value;
364 ASSERT_TRUE(dict_root->GetString("test", &test_value)); 360 ASSERT_TRUE(dict_root->GetString("test", &test_value));
365 ASSERT_EQ(test, test_value); 361 ASSERT_EQ(test, test_value);
366 362
367 // Test converting escaped regular chars 363 // Test converting escaped regular chars
368 static const char kEscapedChars[] = "{\"test\":\"\\u0067\\u006f\"}"; 364 static const char kEscapedChars[] = "{\"test\":\"\\u0067\\u006f\"}";
369 JSONStringValueSerializer deserializer2(kEscapedChars); 365 JSONStringValueDeserializer deserializer2(kEscapedChars);
370 deserial_root.reset(deserializer2.Deserialize(NULL, NULL)); 366 deserial_root.reset(deserializer2.Deserialize(NULL, NULL));
371 ASSERT_TRUE(deserial_root.get()); 367 ASSERT_TRUE(deserial_root.get());
372 dict_root = static_cast<DictionaryValue*>(deserial_root.get()); 368 dict_root = static_cast<DictionaryValue*>(deserial_root.get());
373 ASSERT_TRUE(dict_root->GetString("test", &test_value)); 369 ASSERT_TRUE(dict_root->GetString("test", &test_value));
374 ASSERT_EQ(ASCIIToUTF16("go"), test_value); 370 ASSERT_EQ(ASCIIToUTF16("go"), test_value);
375 } 371 }
376 372
377 TEST(JSONValueSerializerTest, AllowTrailingComma) { 373 TEST(JSONValueSerializerTest, AllowTrailingComma) {
378 scoped_ptr<Value> root; 374 scoped_ptr<Value> root;
379 scoped_ptr<Value> root_expected; 375 scoped_ptr<Value> root_expected;
380 static const char kTestWithCommas[] = "{\"key\": [true,],}"; 376 static const char kTestWithCommas[] = "{\"key\": [true,],}";
381 static const char kTestNoCommas[] = "{\"key\": [true]}"; 377 static const char kTestNoCommas[] = "{\"key\": [true]}";
382 378
383 JSONStringValueSerializer serializer(kTestWithCommas); 379 JSONStringValueDeserializer deserializer(kTestWithCommas);
384 serializer.set_allow_trailing_comma(true); 380 deserializer.set_allow_trailing_comma(true);
385 JSONStringValueSerializer serializer_expected(kTestNoCommas); 381 JSONStringValueDeserializer deserializer_expected(kTestNoCommas);
386 root.reset(serializer.Deserialize(NULL, NULL)); 382 root.reset(deserializer.Deserialize(NULL, NULL));
387 ASSERT_TRUE(root.get()); 383 ASSERT_TRUE(root.get());
388 root_expected.reset(serializer_expected.Deserialize(NULL, NULL)); 384 root_expected.reset(deserializer_expected.Deserialize(NULL, NULL));
389 ASSERT_TRUE(root_expected.get()); 385 ASSERT_TRUE(root_expected.get());
390 ASSERT_TRUE(root->Equals(root_expected.get())); 386 ASSERT_TRUE(root->Equals(root_expected.get()));
391 } 387 }
392 388
393 TEST(JSONValueSerializerTest, JSONReaderComments) { 389 TEST(JSONValueSerializerTest, JSONReaderComments) {
394 ValidateJsonList("[ // 2, 3, ignore me ] \n1 ]"); 390 ValidateJsonList("[ // 2, 3, ignore me ] \n1 ]");
395 ValidateJsonList("[ /* 2, \n3, ignore me ]*/ \n1 ]"); 391 ValidateJsonList("[ /* 2, \n3, ignore me ]*/ \n1 ]");
396 ValidateJsonList("//header\n[ // 2, \n// 3, \n1 ]// footer"); 392 ValidateJsonList("//header\n[ // 2, \n// 3, \n1 ]// footer");
397 ValidateJsonList("/*\n[ // 2, \n// 3, \n1 ]*/[1]"); 393 ValidateJsonList("/*\n[ // 2, \n// 3, \n1 ]*/[1]");
398 ValidateJsonList("[ 1 /* one */ ] /* end */"); 394 ValidateJsonList("[ 1 /* one */ ] /* end */");
(...skipping 29 matching lines...) Expand all
428 }; 424 };
429 425
430 TEST_F(JSONFileValueSerializerTest, Roundtrip) { 426 TEST_F(JSONFileValueSerializerTest, Roundtrip) {
431 base::FilePath original_file_path; 427 base::FilePath original_file_path;
432 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path)); 428 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path));
433 original_file_path = 429 original_file_path =
434 original_file_path.Append(FILE_PATH_LITERAL("serializer_test.json")); 430 original_file_path.Append(FILE_PATH_LITERAL("serializer_test.json"));
435 431
436 ASSERT_TRUE(PathExists(original_file_path)); 432 ASSERT_TRUE(PathExists(original_file_path));
437 433
438 JSONFileValueSerializer deserializer(original_file_path); 434 JSONFileValueDeserializer deserializer(original_file_path);
439 scoped_ptr<Value> root; 435 scoped_ptr<Value> root;
440 root.reset(deserializer.Deserialize(NULL, NULL)); 436 root.reset(deserializer.Deserialize(NULL, NULL));
441 437
442 ASSERT_TRUE(root.get()); 438 ASSERT_TRUE(root.get());
443 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 439 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
444 440
445 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); 441 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get());
446 442
447 Value* null_value = NULL; 443 Value* null_value = NULL;
448 ASSERT_TRUE(root_dict->Get("null", &null_value)); 444 ASSERT_TRUE(root_dict->Get("null", &null_value));
(...skipping 27 matching lines...) Expand all
476 } 472 }
477 473
478 TEST_F(JSONFileValueSerializerTest, RoundtripNested) { 474 TEST_F(JSONFileValueSerializerTest, RoundtripNested) {
479 base::FilePath original_file_path; 475 base::FilePath original_file_path;
480 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path)); 476 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path));
481 original_file_path = original_file_path.Append( 477 original_file_path = original_file_path.Append(
482 FILE_PATH_LITERAL("serializer_nested_test.json")); 478 FILE_PATH_LITERAL("serializer_nested_test.json"));
483 479
484 ASSERT_TRUE(PathExists(original_file_path)); 480 ASSERT_TRUE(PathExists(original_file_path));
485 481
486 JSONFileValueSerializer deserializer(original_file_path); 482 JSONFileValueDeserializer deserializer(original_file_path);
487 scoped_ptr<Value> root; 483 scoped_ptr<Value> root;
488 root.reset(deserializer.Deserialize(NULL, NULL)); 484 root.reset(deserializer.Deserialize(NULL, NULL));
489 ASSERT_TRUE(root.get()); 485 ASSERT_TRUE(root.get());
490 486
491 // Now try writing. 487 // Now try writing.
492 base::FilePath written_file_path = temp_dir_.path().Append( 488 base::FilePath written_file_path = temp_dir_.path().Append(
493 FILE_PATH_LITERAL("test_output.json")); 489 FILE_PATH_LITERAL("test_output.json"));
494 490
495 ASSERT_FALSE(PathExists(written_file_path)); 491 ASSERT_FALSE(PathExists(written_file_path));
496 JSONFileValueSerializer serializer(written_file_path); 492 JSONFileValueSerializer serializer(written_file_path);
497 ASSERT_TRUE(serializer.Serialize(*root)); 493 ASSERT_TRUE(serializer.Serialize(*root));
498 ASSERT_TRUE(PathExists(written_file_path)); 494 ASSERT_TRUE(PathExists(written_file_path));
499 495
500 // Now compare file contents. 496 // Now compare file contents.
501 EXPECT_TRUE(TextContentsEqual(original_file_path, written_file_path)); 497 EXPECT_TRUE(TextContentsEqual(original_file_path, written_file_path));
502 EXPECT_TRUE(base::DeleteFile(written_file_path, false)); 498 EXPECT_TRUE(base::DeleteFile(written_file_path, false));
503 } 499 }
504 500
505 TEST_F(JSONFileValueSerializerTest, NoWhitespace) { 501 TEST_F(JSONFileValueSerializerTest, NoWhitespace) {
506 base::FilePath source_file_path; 502 base::FilePath source_file_path;
507 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &source_file_path)); 503 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &source_file_path));
508 source_file_path = source_file_path.Append( 504 source_file_path = source_file_path.Append(
509 FILE_PATH_LITERAL("serializer_test_nowhitespace.json")); 505 FILE_PATH_LITERAL("serializer_test_nowhitespace.json"));
510 ASSERT_TRUE(PathExists(source_file_path)); 506 ASSERT_TRUE(PathExists(source_file_path));
511 JSONFileValueSerializer serializer(source_file_path); 507 JSONFileValueDeserializer deserializer(source_file_path);
512 scoped_ptr<Value> root; 508 scoped_ptr<Value> root;
513 root.reset(serializer.Deserialize(NULL, NULL)); 509 root.reset(deserializer.Deserialize(NULL, NULL));
514 ASSERT_TRUE(root.get()); 510 ASSERT_TRUE(root.get());
515 } 511 }
516 512
517 } // namespace 513 } // namespace
518 514
519 } // namespace base 515 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698