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

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

Issue 1852433005: Convert //base to use std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase after r384946 Created 4 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
« no previous file with comments | « base/json/json_value_converter_unittest.cc ('k') | base/json/json_writer_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <memory>
5 #include <string> 6 #include <string>
6 7
7 #include "base/files/file_util.h" 8 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
9 #include "base/json/json_file_value_serializer.h" 10 #include "base/json/json_file_value_serializer.h"
10 #include "base/json/json_reader.h" 11 #include "base/json/json_reader.h"
11 #include "base/json/json_string_value_serializer.h" 12 #include "base/json/json_string_value_serializer.h"
12 #include "base/json/json_writer.h" 13 #include "base/json/json_writer.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/path_service.h" 14 #include "base/path_service.h"
15 #include "base/strings/string_piece.h" 15 #include "base/strings/string_piece.h"
16 #include "base/strings/string_util.h" 16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h" 17 #include "base/strings/utf_string_conversions.h"
18 #include "base/values.h" 18 #include "base/values.h"
19 #include "build/build_config.h" 19 #include "build/build_config.h"
20 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
21 21
22 namespace base { 22 namespace base {
23 23
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 str_serializer.set_pretty_print(true); 69 str_serializer.set_pretty_print(true);
70 ASSERT_TRUE(str_serializer.Serialize(value)); 70 ASSERT_TRUE(str_serializer.Serialize(value));
71 // Unify line endings between platforms. 71 // Unify line endings between platforms.
72 ReplaceSubstringsAfterOffset(&serialized_json, 0, 72 ReplaceSubstringsAfterOffset(&serialized_json, 0,
73 kWinLineEnds, kLinuxLineEnds); 73 kWinLineEnds, kLinuxLineEnds);
74 // Now compare the input with the output. 74 // Now compare the input with the output.
75 ASSERT_EQ(kProperJSON, serialized_json); 75 ASSERT_EQ(kProperJSON, serialized_json);
76 } 76 }
77 77
78 void ValidateJsonList(const std::string& json) { 78 void ValidateJsonList(const std::string& json) {
79 scoped_ptr<Value> root = JSONReader::Read(json); 79 std::unique_ptr<Value> root = JSONReader::Read(json);
80 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST)); 80 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST));
81 ListValue* list = static_cast<ListValue*>(root.get()); 81 ListValue* list = static_cast<ListValue*>(root.get());
82 ASSERT_EQ(1U, list->GetSize()); 82 ASSERT_EQ(1U, list->GetSize());
83 Value* elt = NULL; 83 Value* elt = NULL;
84 ASSERT_TRUE(list->Get(0, &elt)); 84 ASSERT_TRUE(list->Get(0, &elt));
85 int value = 0; 85 int value = 0;
86 ASSERT_TRUE(elt && elt->GetAsInteger(&value)); 86 ASSERT_TRUE(elt && elt->GetAsInteger(&value));
87 ASSERT_EQ(1, value); 87 ASSERT_EQ(1, value);
88 } 88 }
89 89
90 // Test proper JSON deserialization from string is working. 90 // Test proper JSON deserialization from string is working.
91 TEST(JSONValueDeserializerTest, ReadProperJSONFromString) { 91 TEST(JSONValueDeserializerTest, ReadProperJSONFromString) {
92 // Try to deserialize it through the serializer. 92 // Try to deserialize it through the serializer.
93 JSONStringValueDeserializer str_deserializer(kProperJSON); 93 JSONStringValueDeserializer str_deserializer(kProperJSON);
94 94
95 int error_code = 0; 95 int error_code = 0;
96 std::string error_message; 96 std::string error_message;
97 scoped_ptr<Value> value = 97 std::unique_ptr<Value> value =
98 str_deserializer.Deserialize(&error_code, &error_message); 98 str_deserializer.Deserialize(&error_code, &error_message);
99 ASSERT_TRUE(value.get()); 99 ASSERT_TRUE(value.get());
100 ASSERT_EQ(0, error_code); 100 ASSERT_EQ(0, error_code);
101 ASSERT_TRUE(error_message.empty()); 101 ASSERT_TRUE(error_message.empty());
102 // Verify if the same JSON is still there. 102 // Verify if the same JSON is still there.
103 CheckJSONIsStillTheSame(*value); 103 CheckJSONIsStillTheSame(*value);
104 } 104 }
105 105
106 // Test proper JSON deserialization from a StringPiece substring. 106 // Test proper JSON deserialization from a StringPiece substring.
107 TEST(JSONValueDeserializerTest, ReadProperJSONFromStringPiece) { 107 TEST(JSONValueDeserializerTest, ReadProperJSONFromStringPiece) {
108 // Create a StringPiece for the substring of kProperJSONPadded that matches 108 // Create a StringPiece for the substring of kProperJSONPadded that matches
109 // kProperJSON. 109 // kProperJSON.
110 base::StringPiece proper_json(kProperJSONPadded); 110 base::StringPiece proper_json(kProperJSONPadded);
111 proper_json = proper_json.substr(5, proper_json.length() - 10); 111 proper_json = proper_json.substr(5, proper_json.length() - 10);
112 JSONStringValueDeserializer str_deserializer(proper_json); 112 JSONStringValueDeserializer str_deserializer(proper_json);
113 113
114 int error_code = 0; 114 int error_code = 0;
115 std::string error_message; 115 std::string error_message;
116 scoped_ptr<Value> value = 116 std::unique_ptr<Value> value =
117 str_deserializer.Deserialize(&error_code, &error_message); 117 str_deserializer.Deserialize(&error_code, &error_message);
118 ASSERT_TRUE(value.get()); 118 ASSERT_TRUE(value.get());
119 ASSERT_EQ(0, error_code); 119 ASSERT_EQ(0, error_code);
120 ASSERT_TRUE(error_message.empty()); 120 ASSERT_TRUE(error_message.empty());
121 // Verify if the same JSON is still there. 121 // Verify if the same JSON is still there.
122 CheckJSONIsStillTheSame(*value); 122 CheckJSONIsStillTheSame(*value);
123 } 123 }
124 124
125 // Test that trialing commas are only properly deserialized from string when 125 // Test that trialing commas are only properly deserialized from string when
126 // the proper flag for that is set. 126 // the proper flag for that is set.
127 TEST(JSONValueDeserializerTest, ReadJSONWithTrailingCommasFromString) { 127 TEST(JSONValueDeserializerTest, ReadJSONWithTrailingCommasFromString) {
128 // Try to deserialize it through the serializer. 128 // Try to deserialize it through the serializer.
129 JSONStringValueDeserializer str_deserializer(kProperJSONWithCommas); 129 JSONStringValueDeserializer str_deserializer(kProperJSONWithCommas);
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 std::unique_ptr<Value> value =
134 str_deserializer.Deserialize(&error_code, &error_message); 134 str_deserializer.Deserialize(&error_code, &error_message);
135 ASSERT_FALSE(value.get()); 135 ASSERT_FALSE(value.get());
136 ASSERT_NE(0, error_code); 136 ASSERT_NE(0, error_code);
137 ASSERT_FALSE(error_message.empty()); 137 ASSERT_FALSE(error_message.empty());
138 // Now the flag is set and it must pass. 138 // Now the flag is set and it must pass.
139 str_deserializer.set_allow_trailing_comma(true); 139 str_deserializer.set_allow_trailing_comma(true);
140 value = str_deserializer.Deserialize(&error_code, &error_message); 140 value = str_deserializer.Deserialize(&error_code, &error_message);
141 ASSERT_TRUE(value.get()); 141 ASSERT_TRUE(value.get());
142 ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code); 142 ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code);
143 // Verify if the same JSON is still there. 143 // Verify if the same JSON is still there.
144 CheckJSONIsStillTheSame(*value); 144 CheckJSONIsStillTheSame(*value);
145 } 145 }
146 146
147 // Test proper JSON deserialization from file is working. 147 // Test proper JSON deserialization from file is working.
148 TEST(JSONValueDeserializerTest, ReadProperJSONFromFile) { 148 TEST(JSONValueDeserializerTest, ReadProperJSONFromFile) {
149 ScopedTempDir tempdir; 149 ScopedTempDir tempdir;
150 ASSERT_TRUE(tempdir.CreateUniqueTempDir()); 150 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
151 // Write it down in the file. 151 // Write it down in the file.
152 FilePath temp_file(tempdir.path().AppendASCII("test.json")); 152 FilePath temp_file(tempdir.path().AppendASCII("test.json"));
153 ASSERT_EQ(static_cast<int>(strlen(kProperJSON)), 153 ASSERT_EQ(static_cast<int>(strlen(kProperJSON)),
154 WriteFile(temp_file, kProperJSON, strlen(kProperJSON))); 154 WriteFile(temp_file, kProperJSON, strlen(kProperJSON)));
155 155
156 // Try to deserialize it through the serializer. 156 // Try to deserialize it through the serializer.
157 JSONFileValueDeserializer file_deserializer(temp_file); 157 JSONFileValueDeserializer file_deserializer(temp_file);
158 158
159 int error_code = 0; 159 int error_code = 0;
160 std::string error_message; 160 std::string error_message;
161 scoped_ptr<Value> value = 161 std::unique_ptr<Value> value =
162 file_deserializer.Deserialize(&error_code, &error_message); 162 file_deserializer.Deserialize(&error_code, &error_message);
163 ASSERT_TRUE(value.get()); 163 ASSERT_TRUE(value.get());
164 ASSERT_EQ(0, error_code); 164 ASSERT_EQ(0, error_code);
165 ASSERT_TRUE(error_message.empty()); 165 ASSERT_TRUE(error_message.empty());
166 // Verify if the same JSON is still there. 166 // Verify if the same JSON is still there.
167 CheckJSONIsStillTheSame(*value); 167 CheckJSONIsStillTheSame(*value);
168 } 168 }
169 169
170 // Test that trialing commas are only properly deserialized from file when 170 // Test that trialing commas are only properly deserialized from file when
171 // the proper flag for that is set. 171 // the proper flag for that is set.
172 TEST(JSONValueDeserializerTest, ReadJSONWithCommasFromFile) { 172 TEST(JSONValueDeserializerTest, ReadJSONWithCommasFromFile) {
173 ScopedTempDir tempdir; 173 ScopedTempDir tempdir;
174 ASSERT_TRUE(tempdir.CreateUniqueTempDir()); 174 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
175 // Write it down in the file. 175 // Write it down in the file.
176 FilePath temp_file(tempdir.path().AppendASCII("test.json")); 176 FilePath temp_file(tempdir.path().AppendASCII("test.json"));
177 ASSERT_EQ(static_cast<int>(strlen(kProperJSONWithCommas)), 177 ASSERT_EQ(static_cast<int>(strlen(kProperJSONWithCommas)),
178 WriteFile(temp_file, kProperJSONWithCommas, 178 WriteFile(temp_file, kProperJSONWithCommas,
179 strlen(kProperJSONWithCommas))); 179 strlen(kProperJSONWithCommas)));
180 180
181 // Try to deserialize it through the serializer. 181 // Try to deserialize it through the serializer.
182 JSONFileValueDeserializer file_deserializer(temp_file); 182 JSONFileValueDeserializer file_deserializer(temp_file);
183 // This must fail without the proper flag. 183 // This must fail without the proper flag.
184 int error_code = 0; 184 int error_code = 0;
185 std::string error_message; 185 std::string error_message;
186 scoped_ptr<Value> value = 186 std::unique_ptr<Value> value =
187 file_deserializer.Deserialize(&error_code, &error_message); 187 file_deserializer.Deserialize(&error_code, &error_message);
188 ASSERT_FALSE(value.get()); 188 ASSERT_FALSE(value.get());
189 ASSERT_NE(0, error_code); 189 ASSERT_NE(0, error_code);
190 ASSERT_FALSE(error_message.empty()); 190 ASSERT_FALSE(error_message.empty());
191 // Now the flag is set and it must pass. 191 // Now the flag is set and it must pass.
192 file_deserializer.set_allow_trailing_comma(true); 192 file_deserializer.set_allow_trailing_comma(true);
193 value = file_deserializer.Deserialize(&error_code, &error_message); 193 value = file_deserializer.Deserialize(&error_code, &error_message);
194 ASSERT_TRUE(value.get()); 194 ASSERT_TRUE(value.get());
195 ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code); 195 ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code);
196 // Verify if the same JSON is still there. 196 // Verify if the same JSON is still there.
197 CheckJSONIsStillTheSame(*value); 197 CheckJSONIsStillTheSame(*value);
198 } 198 }
199 199
200 TEST(JSONValueDeserializerTest, AllowTrailingComma) { 200 TEST(JSONValueDeserializerTest, AllowTrailingComma) {
201 scoped_ptr<Value> root; 201 std::unique_ptr<Value> root;
202 scoped_ptr<Value> root_expected; 202 std::unique_ptr<Value> root_expected;
203 static const char kTestWithCommas[] = "{\"key\": [true,],}"; 203 static const char kTestWithCommas[] = "{\"key\": [true,],}";
204 static const char kTestNoCommas[] = "{\"key\": [true]}"; 204 static const char kTestNoCommas[] = "{\"key\": [true]}";
205 205
206 JSONStringValueDeserializer deserializer(kTestWithCommas); 206 JSONStringValueDeserializer deserializer(kTestWithCommas);
207 deserializer.set_allow_trailing_comma(true); 207 deserializer.set_allow_trailing_comma(true);
208 JSONStringValueDeserializer deserializer_expected(kTestNoCommas); 208 JSONStringValueDeserializer deserializer_expected(kTestNoCommas);
209 root = deserializer.Deserialize(NULL, NULL); 209 root = deserializer.Deserialize(NULL, NULL);
210 ASSERT_TRUE(root.get()); 210 ASSERT_TRUE(root.get());
211 root_expected = deserializer_expected.Deserialize(NULL, NULL); 211 root_expected = deserializer_expected.Deserialize(NULL, NULL);
212 ASSERT_TRUE(root_expected.get()); 212 ASSERT_TRUE(root_expected.get());
213 ASSERT_TRUE(root->Equals(root_expected.get())); 213 ASSERT_TRUE(root->Equals(root_expected.get()));
214 } 214 }
215 215
216 TEST(JSONValueSerializerTest, Roundtrip) { 216 TEST(JSONValueSerializerTest, Roundtrip) {
217 static const char kOriginalSerialization[] = 217 static const char kOriginalSerialization[] =
218 "{\"bool\":true,\"double\":3.14,\"int\":42,\"list\":[1,2],\"null\":null}"; 218 "{\"bool\":true,\"double\":3.14,\"int\":42,\"list\":[1,2],\"null\":null}";
219 JSONStringValueDeserializer deserializer(kOriginalSerialization); 219 JSONStringValueDeserializer deserializer(kOriginalSerialization);
220 scoped_ptr<Value> root = deserializer.Deserialize(NULL, NULL); 220 std::unique_ptr<Value> root = deserializer.Deserialize(NULL, NULL);
221 ASSERT_TRUE(root.get()); 221 ASSERT_TRUE(root.get());
222 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 222 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
223 223
224 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); 224 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get());
225 225
226 Value* null_value = NULL; 226 Value* null_value = NULL;
227 ASSERT_TRUE(root_dict->Get("null", &null_value)); 227 ASSERT_TRUE(root_dict->Get("null", &null_value));
228 ASSERT_TRUE(null_value); 228 ASSERT_TRUE(null_value);
229 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL)); 229 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL));
230 230
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
320 320
321 static const char kExpected[] = "{\"web\":\"\xE7\xBD\x91\xE9\xA1\xB5\"}"; 321 static const char kExpected[] = "{\"web\":\"\xE7\xBD\x91\xE9\xA1\xB5\"}";
322 322
323 std::string actual; 323 std::string actual;
324 JSONStringValueSerializer serializer(&actual); 324 JSONStringValueSerializer serializer(&actual);
325 ASSERT_TRUE(serializer.Serialize(root)); 325 ASSERT_TRUE(serializer.Serialize(root));
326 ASSERT_EQ(kExpected, actual); 326 ASSERT_EQ(kExpected, actual);
327 327
328 // escaped ascii text -> json 328 // escaped ascii text -> json
329 JSONStringValueDeserializer deserializer(kExpected); 329 JSONStringValueDeserializer deserializer(kExpected);
330 scoped_ptr<Value> deserial_root = deserializer.Deserialize(NULL, NULL); 330 std::unique_ptr<Value> deserial_root = deserializer.Deserialize(NULL, NULL);
331 ASSERT_TRUE(deserial_root.get()); 331 ASSERT_TRUE(deserial_root.get());
332 DictionaryValue* dict_root = 332 DictionaryValue* dict_root =
333 static_cast<DictionaryValue*>(deserial_root.get()); 333 static_cast<DictionaryValue*>(deserial_root.get());
334 string16 web_value; 334 string16 web_value;
335 ASSERT_TRUE(dict_root->GetString("web", &web_value)); 335 ASSERT_TRUE(dict_root->GetString("web", &web_value));
336 ASSERT_EQ(test, web_value); 336 ASSERT_EQ(test, web_value);
337 } 337 }
338 338
339 TEST(JSONValueSerializerTest, HexStrings) { 339 TEST(JSONValueSerializerTest, HexStrings) {
340 // hex string json -> escaped ascii text 340 // hex string json -> escaped ascii text
341 DictionaryValue root; 341 DictionaryValue root;
342 string16 test(WideToUTF16(L"\x01\x02")); 342 string16 test(WideToUTF16(L"\x01\x02"));
343 root.SetString("test", test); 343 root.SetString("test", test);
344 344
345 static const char kExpected[] = "{\"test\":\"\\u0001\\u0002\"}"; 345 static const char kExpected[] = "{\"test\":\"\\u0001\\u0002\"}";
346 346
347 std::string actual; 347 std::string actual;
348 JSONStringValueSerializer serializer(&actual); 348 JSONStringValueSerializer serializer(&actual);
349 ASSERT_TRUE(serializer.Serialize(root)); 349 ASSERT_TRUE(serializer.Serialize(root));
350 ASSERT_EQ(kExpected, actual); 350 ASSERT_EQ(kExpected, actual);
351 351
352 // escaped ascii text -> json 352 // escaped ascii text -> json
353 JSONStringValueDeserializer deserializer(kExpected); 353 JSONStringValueDeserializer deserializer(kExpected);
354 scoped_ptr<Value> deserial_root = deserializer.Deserialize(NULL, NULL); 354 std::unique_ptr<Value> deserial_root = deserializer.Deserialize(NULL, NULL);
355 ASSERT_TRUE(deserial_root.get()); 355 ASSERT_TRUE(deserial_root.get());
356 DictionaryValue* dict_root = 356 DictionaryValue* dict_root =
357 static_cast<DictionaryValue*>(deserial_root.get()); 357 static_cast<DictionaryValue*>(deserial_root.get());
358 string16 test_value; 358 string16 test_value;
359 ASSERT_TRUE(dict_root->GetString("test", &test_value)); 359 ASSERT_TRUE(dict_root->GetString("test", &test_value));
360 ASSERT_EQ(test, test_value); 360 ASSERT_EQ(test, test_value);
361 361
362 // Test converting escaped regular chars 362 // Test converting escaped regular chars
363 static const char kEscapedChars[] = "{\"test\":\"\\u0067\\u006f\"}"; 363 static const char kEscapedChars[] = "{\"test\":\"\\u0067\\u006f\"}";
364 JSONStringValueDeserializer deserializer2(kEscapedChars); 364 JSONStringValueDeserializer deserializer2(kEscapedChars);
365 deserial_root = deserializer2.Deserialize(NULL, NULL); 365 deserial_root = deserializer2.Deserialize(NULL, NULL);
366 ASSERT_TRUE(deserial_root.get()); 366 ASSERT_TRUE(deserial_root.get());
367 dict_root = static_cast<DictionaryValue*>(deserial_root.get()); 367 dict_root = static_cast<DictionaryValue*>(deserial_root.get());
368 ASSERT_TRUE(dict_root->GetString("test", &test_value)); 368 ASSERT_TRUE(dict_root->GetString("test", &test_value));
369 ASSERT_EQ(ASCIIToUTF16("go"), test_value); 369 ASSERT_EQ(ASCIIToUTF16("go"), test_value);
370 } 370 }
371 371
372 TEST(JSONValueSerializerTest, JSONReaderComments) { 372 TEST(JSONValueSerializerTest, JSONReaderComments) {
373 ValidateJsonList("[ // 2, 3, ignore me ] \n1 ]"); 373 ValidateJsonList("[ // 2, 3, ignore me ] \n1 ]");
374 ValidateJsonList("[ /* 2, \n3, ignore me ]*/ \n1 ]"); 374 ValidateJsonList("[ /* 2, \n3, ignore me ]*/ \n1 ]");
375 ValidateJsonList("//header\n[ // 2, \n// 3, \n1 ]// footer"); 375 ValidateJsonList("//header\n[ // 2, \n// 3, \n1 ]// footer");
376 ValidateJsonList("/*\n[ // 2, \n// 3, \n1 ]*/[1]"); 376 ValidateJsonList("/*\n[ // 2, \n// 3, \n1 ]*/[1]");
377 ValidateJsonList("[ 1 /* one */ ] /* end */"); 377 ValidateJsonList("[ 1 /* one */ ] /* end */");
378 ValidateJsonList("[ 1 //// ,2\r\n ]"); 378 ValidateJsonList("[ 1 //// ,2\r\n ]");
379 379
380 // It's ok to have a comment in a string. 380 // It's ok to have a comment in a string.
381 scoped_ptr<Value> root = JSONReader::Read("[\"// ok\\n /* foo */ \"]"); 381 std::unique_ptr<Value> root = JSONReader::Read("[\"// ok\\n /* foo */ \"]");
382 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST)); 382 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST));
383 ListValue* list = static_cast<ListValue*>(root.get()); 383 ListValue* list = static_cast<ListValue*>(root.get());
384 ASSERT_EQ(1U, list->GetSize()); 384 ASSERT_EQ(1U, list->GetSize());
385 Value* elt = NULL; 385 Value* elt = NULL;
386 ASSERT_TRUE(list->Get(0, &elt)); 386 ASSERT_TRUE(list->Get(0, &elt));
387 std::string value; 387 std::string value;
388 ASSERT_TRUE(elt && elt->GetAsString(&value)); 388 ASSERT_TRUE(elt && elt->GetAsString(&value));
389 ASSERT_EQ("// ok\n /* foo */ ", value); 389 ASSERT_EQ("// ok\n /* foo */ ", value);
390 390
391 // You can't nest comments. 391 // You can't nest comments.
(...skipping 14 matching lines...) Expand all
406 406
407 TEST_F(JSONFileValueSerializerTest, Roundtrip) { 407 TEST_F(JSONFileValueSerializerTest, Roundtrip) {
408 base::FilePath original_file_path; 408 base::FilePath original_file_path;
409 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path)); 409 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path));
410 original_file_path = 410 original_file_path =
411 original_file_path.Append(FILE_PATH_LITERAL("serializer_test.json")); 411 original_file_path.Append(FILE_PATH_LITERAL("serializer_test.json"));
412 412
413 ASSERT_TRUE(PathExists(original_file_path)); 413 ASSERT_TRUE(PathExists(original_file_path));
414 414
415 JSONFileValueDeserializer deserializer(original_file_path); 415 JSONFileValueDeserializer deserializer(original_file_path);
416 scoped_ptr<Value> root; 416 std::unique_ptr<Value> root;
417 root = deserializer.Deserialize(NULL, NULL); 417 root = deserializer.Deserialize(NULL, NULL);
418 418
419 ASSERT_TRUE(root.get()); 419 ASSERT_TRUE(root.get());
420 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 420 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
421 421
422 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); 422 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get());
423 423
424 Value* null_value = NULL; 424 Value* null_value = NULL;
425 ASSERT_TRUE(root_dict->Get("null", &null_value)); 425 ASSERT_TRUE(root_dict->Get("null", &null_value));
426 ASSERT_TRUE(null_value); 426 ASSERT_TRUE(null_value);
(...skipping 27 matching lines...) Expand all
454 454
455 TEST_F(JSONFileValueSerializerTest, RoundtripNested) { 455 TEST_F(JSONFileValueSerializerTest, RoundtripNested) {
456 base::FilePath original_file_path; 456 base::FilePath original_file_path;
457 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path)); 457 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path));
458 original_file_path = original_file_path.Append( 458 original_file_path = original_file_path.Append(
459 FILE_PATH_LITERAL("serializer_nested_test.json")); 459 FILE_PATH_LITERAL("serializer_nested_test.json"));
460 460
461 ASSERT_TRUE(PathExists(original_file_path)); 461 ASSERT_TRUE(PathExists(original_file_path));
462 462
463 JSONFileValueDeserializer deserializer(original_file_path); 463 JSONFileValueDeserializer deserializer(original_file_path);
464 scoped_ptr<Value> root; 464 std::unique_ptr<Value> root;
465 root = deserializer.Deserialize(NULL, NULL); 465 root = deserializer.Deserialize(NULL, NULL);
466 ASSERT_TRUE(root.get()); 466 ASSERT_TRUE(root.get());
467 467
468 // Now try writing. 468 // Now try writing.
469 base::FilePath written_file_path = temp_dir_.path().Append( 469 base::FilePath written_file_path = temp_dir_.path().Append(
470 FILE_PATH_LITERAL("test_output.json")); 470 FILE_PATH_LITERAL("test_output.json"));
471 471
472 ASSERT_FALSE(PathExists(written_file_path)); 472 ASSERT_FALSE(PathExists(written_file_path));
473 JSONFileValueSerializer serializer(written_file_path); 473 JSONFileValueSerializer serializer(written_file_path);
474 ASSERT_TRUE(serializer.Serialize(*root)); 474 ASSERT_TRUE(serializer.Serialize(*root));
475 ASSERT_TRUE(PathExists(written_file_path)); 475 ASSERT_TRUE(PathExists(written_file_path));
476 476
477 // Now compare file contents. 477 // Now compare file contents.
478 EXPECT_TRUE(TextContentsEqual(original_file_path, written_file_path)); 478 EXPECT_TRUE(TextContentsEqual(original_file_path, written_file_path));
479 EXPECT_TRUE(base::DeleteFile(written_file_path, false)); 479 EXPECT_TRUE(base::DeleteFile(written_file_path, false));
480 } 480 }
481 481
482 TEST_F(JSONFileValueSerializerTest, NoWhitespace) { 482 TEST_F(JSONFileValueSerializerTest, NoWhitespace) {
483 base::FilePath source_file_path; 483 base::FilePath source_file_path;
484 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &source_file_path)); 484 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &source_file_path));
485 source_file_path = source_file_path.Append( 485 source_file_path = source_file_path.Append(
486 FILE_PATH_LITERAL("serializer_test_nowhitespace.json")); 486 FILE_PATH_LITERAL("serializer_test_nowhitespace.json"));
487 ASSERT_TRUE(PathExists(source_file_path)); 487 ASSERT_TRUE(PathExists(source_file_path));
488 JSONFileValueDeserializer deserializer(source_file_path); 488 JSONFileValueDeserializer deserializer(source_file_path);
489 scoped_ptr<Value> root; 489 std::unique_ptr<Value> root;
490 root = deserializer.Deserialize(NULL, NULL); 490 root = deserializer.Deserialize(NULL, NULL);
491 ASSERT_TRUE(root.get()); 491 ASSERT_TRUE(root.get());
492 } 492 }
493 493
494 } // namespace 494 } // namespace
495 495
496 } // namespace base 496 } // namespace base
OLDNEW
« no previous file with comments | « base/json/json_value_converter_unittest.cc ('k') | base/json/json_writer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698