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

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

Issue 2275553005: //base: Make ScopedTempDir::path() a GetPath() with a DCHECK (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments Created 4 years, 3 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/files/scoped_temp_dir_unittest.cc ('k') | base/mac/mac_util_unittest.mm » ('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 <memory>
6 #include <string> 6 #include <string>
7 7
8 #include "base/files/file_util.h" 8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
10 #include "base/json/json_file_value_serializer.h" 10 #include "base/json/json_file_value_serializer.h"
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code); 141 ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code);
142 // Verify if the same JSON is still there. 142 // Verify if the same JSON is still there.
143 CheckJSONIsStillTheSame(*value); 143 CheckJSONIsStillTheSame(*value);
144 } 144 }
145 145
146 // Test proper JSON deserialization from file is working. 146 // Test proper JSON deserialization from file is working.
147 TEST(JSONValueDeserializerTest, ReadProperJSONFromFile) { 147 TEST(JSONValueDeserializerTest, ReadProperJSONFromFile) {
148 ScopedTempDir tempdir; 148 ScopedTempDir tempdir;
149 ASSERT_TRUE(tempdir.CreateUniqueTempDir()); 149 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
150 // Write it down in the file. 150 // Write it down in the file.
151 FilePath temp_file(tempdir.path().AppendASCII("test.json")); 151 FilePath temp_file(tempdir.GetPath().AppendASCII("test.json"));
152 ASSERT_EQ(static_cast<int>(strlen(kProperJSON)), 152 ASSERT_EQ(static_cast<int>(strlen(kProperJSON)),
153 WriteFile(temp_file, kProperJSON, strlen(kProperJSON))); 153 WriteFile(temp_file, kProperJSON, strlen(kProperJSON)));
154 154
155 // Try to deserialize it through the serializer. 155 // Try to deserialize it through the serializer.
156 JSONFileValueDeserializer file_deserializer(temp_file); 156 JSONFileValueDeserializer file_deserializer(temp_file);
157 157
158 int error_code = 0; 158 int error_code = 0;
159 std::string error_message; 159 std::string error_message;
160 std::unique_ptr<Value> value = 160 std::unique_ptr<Value> value =
161 file_deserializer.Deserialize(&error_code, &error_message); 161 file_deserializer.Deserialize(&error_code, &error_message);
162 ASSERT_TRUE(value); 162 ASSERT_TRUE(value);
163 ASSERT_EQ(0, error_code); 163 ASSERT_EQ(0, error_code);
164 ASSERT_TRUE(error_message.empty()); 164 ASSERT_TRUE(error_message.empty());
165 // Verify if the same JSON is still there. 165 // Verify if the same JSON is still there.
166 CheckJSONIsStillTheSame(*value); 166 CheckJSONIsStillTheSame(*value);
167 } 167 }
168 168
169 // Test that trialing commas are only properly deserialized from file when 169 // Test that trialing commas are only properly deserialized from file when
170 // the proper flag for that is set. 170 // the proper flag for that is set.
171 TEST(JSONValueDeserializerTest, ReadJSONWithCommasFromFile) { 171 TEST(JSONValueDeserializerTest, ReadJSONWithCommasFromFile) {
172 ScopedTempDir tempdir; 172 ScopedTempDir tempdir;
173 ASSERT_TRUE(tempdir.CreateUniqueTempDir()); 173 ASSERT_TRUE(tempdir.CreateUniqueTempDir());
174 // Write it down in the file. 174 // Write it down in the file.
175 FilePath temp_file(tempdir.path().AppendASCII("test.json")); 175 FilePath temp_file(tempdir.GetPath().AppendASCII("test.json"));
176 ASSERT_EQ(static_cast<int>(strlen(kProperJSONWithCommas)), 176 ASSERT_EQ(static_cast<int>(strlen(kProperJSONWithCommas)),
177 WriteFile(temp_file, kProperJSONWithCommas, 177 WriteFile(temp_file, kProperJSONWithCommas,
178 strlen(kProperJSONWithCommas))); 178 strlen(kProperJSONWithCommas)));
179 179
180 // Try to deserialize it through the serializer. 180 // Try to deserialize it through the serializer.
181 JSONFileValueDeserializer file_deserializer(temp_file); 181 JSONFileValueDeserializer file_deserializer(temp_file);
182 // This must fail without the proper flag. 182 // This must fail without the proper flag.
183 int error_code = 0; 183 int error_code = 0;
184 std::string error_message; 184 std::string error_message;
185 std::unique_ptr<Value> value = 185 std::unique_ptr<Value> value =
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
424 int int_value = 0; 424 int int_value = 0;
425 ASSERT_TRUE(root_dict->GetInteger("int", &int_value)); 425 ASSERT_TRUE(root_dict->GetInteger("int", &int_value));
426 ASSERT_EQ(42, int_value); 426 ASSERT_EQ(42, int_value);
427 427
428 std::string string_value; 428 std::string string_value;
429 ASSERT_TRUE(root_dict->GetString("string", &string_value)); 429 ASSERT_TRUE(root_dict->GetString("string", &string_value));
430 ASSERT_EQ("hello", string_value); 430 ASSERT_EQ("hello", string_value);
431 431
432 // Now try writing. 432 // Now try writing.
433 const FilePath written_file_path = 433 const FilePath written_file_path =
434 temp_dir_.path().AppendASCII("test_output.js"); 434 temp_dir_.GetPath().AppendASCII("test_output.js");
435 435
436 ASSERT_FALSE(PathExists(written_file_path)); 436 ASSERT_FALSE(PathExists(written_file_path));
437 JSONFileValueSerializer serializer(written_file_path); 437 JSONFileValueSerializer serializer(written_file_path);
438 ASSERT_TRUE(serializer.Serialize(*root_dict)); 438 ASSERT_TRUE(serializer.Serialize(*root_dict));
439 ASSERT_TRUE(PathExists(written_file_path)); 439 ASSERT_TRUE(PathExists(written_file_path));
440 440
441 // Now compare file contents. 441 // Now compare file contents.
442 EXPECT_TRUE(TextContentsEqual(original_file_path, written_file_path)); 442 EXPECT_TRUE(TextContentsEqual(original_file_path, written_file_path));
443 EXPECT_TRUE(DeleteFile(written_file_path, false)); 443 EXPECT_TRUE(DeleteFile(written_file_path, false));
444 } 444 }
445 445
446 TEST_F(JSONFileValueSerializerTest, RoundtripNested) { 446 TEST_F(JSONFileValueSerializerTest, RoundtripNested) {
447 FilePath original_file_path; 447 FilePath original_file_path;
448 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path)); 448 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path));
449 original_file_path = 449 original_file_path =
450 original_file_path.AppendASCII("serializer_nested_test.json"); 450 original_file_path.AppendASCII("serializer_nested_test.json");
451 451
452 ASSERT_TRUE(PathExists(original_file_path)); 452 ASSERT_TRUE(PathExists(original_file_path));
453 453
454 JSONFileValueDeserializer deserializer(original_file_path); 454 JSONFileValueDeserializer deserializer(original_file_path);
455 std::unique_ptr<Value> root = deserializer.Deserialize(nullptr, nullptr); 455 std::unique_ptr<Value> root = deserializer.Deserialize(nullptr, nullptr);
456 ASSERT_TRUE(root); 456 ASSERT_TRUE(root);
457 457
458 // Now try writing. 458 // Now try writing.
459 FilePath written_file_path = temp_dir_.path().AppendASCII("test_output.json"); 459 FilePath written_file_path =
460 temp_dir_.GetPath().AppendASCII("test_output.json");
460 461
461 ASSERT_FALSE(PathExists(written_file_path)); 462 ASSERT_FALSE(PathExists(written_file_path));
462 JSONFileValueSerializer serializer(written_file_path); 463 JSONFileValueSerializer serializer(written_file_path);
463 ASSERT_TRUE(serializer.Serialize(*root)); 464 ASSERT_TRUE(serializer.Serialize(*root));
464 ASSERT_TRUE(PathExists(written_file_path)); 465 ASSERT_TRUE(PathExists(written_file_path));
465 466
466 // Now compare file contents. 467 // Now compare file contents.
467 EXPECT_TRUE(TextContentsEqual(original_file_path, written_file_path)); 468 EXPECT_TRUE(TextContentsEqual(original_file_path, written_file_path));
468 EXPECT_TRUE(DeleteFile(written_file_path, false)); 469 EXPECT_TRUE(DeleteFile(written_file_path, false));
469 } 470 }
470 471
471 TEST_F(JSONFileValueSerializerTest, NoWhitespace) { 472 TEST_F(JSONFileValueSerializerTest, NoWhitespace) {
472 FilePath source_file_path; 473 FilePath source_file_path;
473 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &source_file_path)); 474 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &source_file_path));
474 source_file_path = 475 source_file_path =
475 source_file_path.AppendASCII("serializer_test_nowhitespace.json"); 476 source_file_path.AppendASCII("serializer_test_nowhitespace.json");
476 ASSERT_TRUE(PathExists(source_file_path)); 477 ASSERT_TRUE(PathExists(source_file_path));
477 JSONFileValueDeserializer deserializer(source_file_path); 478 JSONFileValueDeserializer deserializer(source_file_path);
478 std::unique_ptr<Value> root = deserializer.Deserialize(nullptr, nullptr); 479 std::unique_ptr<Value> root = deserializer.Deserialize(nullptr, nullptr);
479 ASSERT_TRUE(root); 480 ASSERT_TRUE(root);
480 } 481 }
481 482
482 } // namespace 483 } // namespace
483 484
484 } // namespace base 485 } // namespace base
OLDNEW
« no previous file with comments | « base/files/scoped_temp_dir_unittest.cc ('k') | base/mac/mac_util_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698