| OLD | NEW |
| 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/file_util.h" | 7 #include "base/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 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 CheckJSONIsStillTheSame(*value); | 111 CheckJSONIsStillTheSame(*value); |
| 112 } | 112 } |
| 113 | 113 |
| 114 // Test proper JSON [de]serialization from file is working. | 114 // Test proper JSON [de]serialization from file is working. |
| 115 TEST(JSONValueSerializerTest, ReadProperJSONFromFile) { | 115 TEST(JSONValueSerializerTest, ReadProperJSONFromFile) { |
| 116 ScopedTempDir tempdir; | 116 ScopedTempDir tempdir; |
| 117 ASSERT_TRUE(tempdir.CreateUniqueTempDir()); | 117 ASSERT_TRUE(tempdir.CreateUniqueTempDir()); |
| 118 // Write it down in the file. | 118 // Write it down in the file. |
| 119 FilePath temp_file(tempdir.path().AppendASCII("test.json")); | 119 FilePath temp_file(tempdir.path().AppendASCII("test.json")); |
| 120 ASSERT_EQ(static_cast<int>(strlen(kProperJSON)), | 120 ASSERT_EQ(static_cast<int>(strlen(kProperJSON)), |
| 121 file_util::WriteFile(temp_file, kProperJSON, strlen(kProperJSON))); | 121 WriteFile(temp_file, kProperJSON, strlen(kProperJSON))); |
| 122 | 122 |
| 123 // Try to deserialize it through the serializer. | 123 // Try to deserialize it through the serializer. |
| 124 JSONFileValueSerializer file_deserializer(temp_file); | 124 JSONFileValueSerializer file_deserializer(temp_file); |
| 125 | 125 |
| 126 int error_code = 0; | 126 int error_code = 0; |
| 127 std::string error_message; | 127 std::string error_message; |
| 128 scoped_ptr<Value> value( | 128 scoped_ptr<Value> value( |
| 129 file_deserializer.Deserialize(&error_code, &error_message)); | 129 file_deserializer.Deserialize(&error_code, &error_message)); |
| 130 ASSERT_TRUE(value.get()); | 130 ASSERT_TRUE(value.get()); |
| 131 ASSERT_EQ(0, error_code); | 131 ASSERT_EQ(0, error_code); |
| 132 ASSERT_TRUE(error_message.empty()); | 132 ASSERT_TRUE(error_message.empty()); |
| 133 // Verify if the same JSON is still there. | 133 // Verify if the same JSON is still there. |
| 134 CheckJSONIsStillTheSame(*value); | 134 CheckJSONIsStillTheSame(*value); |
| 135 } | 135 } |
| 136 | 136 |
| 137 // Test that trialing commas are only properly deserialized from file when | 137 // Test that trialing commas are only properly deserialized from file when |
| 138 // the proper flag for that is set. | 138 // the proper flag for that is set. |
| 139 TEST(JSONValueSerializerTest, ReadJSONWithCommasFromFile) { | 139 TEST(JSONValueSerializerTest, ReadJSONWithCommasFromFile) { |
| 140 ScopedTempDir tempdir; | 140 ScopedTempDir tempdir; |
| 141 ASSERT_TRUE(tempdir.CreateUniqueTempDir()); | 141 ASSERT_TRUE(tempdir.CreateUniqueTempDir()); |
| 142 // Write it down in the file. | 142 // Write it down in the file. |
| 143 FilePath temp_file(tempdir.path().AppendASCII("test.json")); | 143 FilePath temp_file(tempdir.path().AppendASCII("test.json")); |
| 144 ASSERT_EQ(static_cast<int>(strlen(kProperJSONWithCommas)), | 144 ASSERT_EQ(static_cast<int>(strlen(kProperJSONWithCommas)), |
| 145 file_util::WriteFile(temp_file, | 145 WriteFile(temp_file, kProperJSONWithCommas, |
| 146 kProperJSONWithCommas, | 146 strlen(kProperJSONWithCommas))); |
| 147 strlen(kProperJSONWithCommas))); | |
| 148 | 147 |
| 149 // Try to deserialize it through the serializer. | 148 // Try to deserialize it through the serializer. |
| 150 JSONFileValueSerializer file_deserializer(temp_file); | 149 JSONFileValueSerializer file_deserializer(temp_file); |
| 151 // This must fail without the proper flag. | 150 // This must fail without the proper flag. |
| 152 int error_code = 0; | 151 int error_code = 0; |
| 153 std::string error_message; | 152 std::string error_message; |
| 154 scoped_ptr<Value> value( | 153 scoped_ptr<Value> value( |
| 155 file_deserializer.Deserialize(&error_code, &error_message)); | 154 file_deserializer.Deserialize(&error_code, &error_message)); |
| 156 ASSERT_FALSE(value.get()); | 155 ASSERT_FALSE(value.get()); |
| 157 ASSERT_NE(0, error_code); | 156 ASSERT_NE(0, error_code); |
| (...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 ASSERT_TRUE(PathExists(source_file_path)); | 462 ASSERT_TRUE(PathExists(source_file_path)); |
| 464 JSONFileValueSerializer serializer(source_file_path); | 463 JSONFileValueSerializer serializer(source_file_path); |
| 465 scoped_ptr<Value> root; | 464 scoped_ptr<Value> root; |
| 466 root.reset(serializer.Deserialize(NULL, NULL)); | 465 root.reset(serializer.Deserialize(NULL, NULL)); |
| 467 ASSERT_TRUE(root.get()); | 466 ASSERT_TRUE(root.get()); |
| 468 } | 467 } |
| 469 | 468 |
| 470 } // namespace | 469 } // namespace |
| 471 | 470 |
| 472 } // namespace base | 471 } // namespace base |
| OLD | NEW |