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 <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 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 // Try to deserialize it through the serializer. | 127 // Try to deserialize it through the serializer. |
128 JSONStringValueDeserializer str_deserializer(kProperJSONWithCommas); | 128 JSONStringValueDeserializer str_deserializer(kProperJSONWithCommas); |
129 | 129 |
130 int error_code = 0; | 130 int error_code = 0; |
131 std::string error_message; | 131 std::string error_message; |
132 std::unique_ptr<Value> value = | 132 std::unique_ptr<Value> value = |
133 str_deserializer.Deserialize(&error_code, &error_message); | 133 str_deserializer.Deserialize(&error_code, &error_message); |
134 ASSERT_FALSE(value); | 134 ASSERT_FALSE(value); |
135 ASSERT_NE(0, error_code); | 135 ASSERT_NE(0, error_code); |
136 ASSERT_FALSE(error_message.empty()); | 136 ASSERT_FALSE(error_message.empty()); |
137 // Now the flag is set and it must pass. | 137 // Repeat with commas allowed. |
138 str_deserializer.set_allow_trailing_comma(true); | 138 JSONStringValueDeserializer str_deserializer2(kProperJSONWithCommas, |
139 value = str_deserializer.Deserialize(&error_code, &error_message); | 139 JSON_ALLOW_TRAILING_COMMAS); |
| 140 value = str_deserializer2.Deserialize(&error_code, &error_message); |
140 ASSERT_TRUE(value); | 141 ASSERT_TRUE(value); |
141 ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code); | 142 ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code); |
142 // Verify if the same JSON is still there. | 143 // Verify if the same JSON is still there. |
143 CheckJSONIsStillTheSame(*value); | 144 CheckJSONIsStillTheSame(*value); |
144 } | 145 } |
145 | 146 |
146 // Test proper JSON deserialization from file is working. | 147 // Test proper JSON deserialization from file is working. |
147 TEST(JSONValueDeserializerTest, ReadProperJSONFromFile) { | 148 TEST(JSONValueDeserializerTest, ReadProperJSONFromFile) { |
148 ScopedTempDir tempdir; | 149 ScopedTempDir tempdir; |
149 ASSERT_TRUE(tempdir.CreateUniqueTempDir()); | 150 ASSERT_TRUE(tempdir.CreateUniqueTempDir()); |
(...skipping 30 matching lines...) Expand all Loading... |
180 // Try to deserialize it through the serializer. | 181 // Try to deserialize it through the serializer. |
181 JSONFileValueDeserializer file_deserializer(temp_file); | 182 JSONFileValueDeserializer file_deserializer(temp_file); |
182 // This must fail without the proper flag. | 183 // This must fail without the proper flag. |
183 int error_code = 0; | 184 int error_code = 0; |
184 std::string error_message; | 185 std::string error_message; |
185 std::unique_ptr<Value> value = | 186 std::unique_ptr<Value> value = |
186 file_deserializer.Deserialize(&error_code, &error_message); | 187 file_deserializer.Deserialize(&error_code, &error_message); |
187 ASSERT_FALSE(value); | 188 ASSERT_FALSE(value); |
188 ASSERT_NE(0, error_code); | 189 ASSERT_NE(0, error_code); |
189 ASSERT_FALSE(error_message.empty()); | 190 ASSERT_FALSE(error_message.empty()); |
190 // Now the flag is set and it must pass. | 191 // Repeat with commas allowed. |
191 file_deserializer.set_allow_trailing_comma(true); | 192 JSONFileValueDeserializer file_deserializer2(temp_file, |
192 value = file_deserializer.Deserialize(&error_code, &error_message); | 193 JSON_ALLOW_TRAILING_COMMAS); |
| 194 value = file_deserializer2.Deserialize(&error_code, &error_message); |
193 ASSERT_TRUE(value); | 195 ASSERT_TRUE(value); |
194 ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code); | 196 ASSERT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code); |
195 // Verify if the same JSON is still there. | 197 // Verify if the same JSON is still there. |
196 CheckJSONIsStillTheSame(*value); | 198 CheckJSONIsStillTheSame(*value); |
197 } | 199 } |
198 | 200 |
199 TEST(JSONValueDeserializerTest, AllowTrailingComma) { | 201 TEST(JSONValueDeserializerTest, AllowTrailingComma) { |
200 static const char kTestWithCommas[] = "{\"key\": [true,],}"; | 202 static const char kTestWithCommas[] = "{\"key\": [true,],}"; |
201 static const char kTestNoCommas[] = "{\"key\": [true]}"; | 203 static const char kTestNoCommas[] = "{\"key\": [true]}"; |
202 | 204 |
203 JSONStringValueDeserializer deserializer(kTestWithCommas); | 205 JSONStringValueDeserializer deserializer(kTestWithCommas, |
204 deserializer.set_allow_trailing_comma(true); | 206 JSON_ALLOW_TRAILING_COMMAS); |
205 JSONStringValueDeserializer deserializer_expected(kTestNoCommas); | 207 JSONStringValueDeserializer deserializer_expected(kTestNoCommas); |
206 std::unique_ptr<Value> root = deserializer.Deserialize(nullptr, nullptr); | 208 std::unique_ptr<Value> root = deserializer.Deserialize(nullptr, nullptr); |
207 ASSERT_TRUE(root); | 209 ASSERT_TRUE(root); |
208 std::unique_ptr<Value> root_expected; | 210 std::unique_ptr<Value> root_expected; |
209 root_expected = deserializer_expected.Deserialize(nullptr, nullptr); | 211 root_expected = deserializer_expected.Deserialize(nullptr, nullptr); |
210 ASSERT_TRUE(root_expected); | 212 ASSERT_TRUE(root_expected); |
211 ASSERT_TRUE(root->Equals(root_expected.get())); | 213 ASSERT_TRUE(root->Equals(root_expected.get())); |
212 } | 214 } |
213 | 215 |
214 TEST(JSONValueSerializerTest, Roundtrip) { | 216 TEST(JSONValueSerializerTest, Roundtrip) { |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
476 source_file_path.AppendASCII("serializer_test_nowhitespace.json"); | 478 source_file_path.AppendASCII("serializer_test_nowhitespace.json"); |
477 ASSERT_TRUE(PathExists(source_file_path)); | 479 ASSERT_TRUE(PathExists(source_file_path)); |
478 JSONFileValueDeserializer deserializer(source_file_path); | 480 JSONFileValueDeserializer deserializer(source_file_path); |
479 std::unique_ptr<Value> root = deserializer.Deserialize(nullptr, nullptr); | 481 std::unique_ptr<Value> root = deserializer.Deserialize(nullptr, nullptr); |
480 ASSERT_TRUE(root); | 482 ASSERT_TRUE(root); |
481 } | 483 } |
482 | 484 |
483 } // namespace | 485 } // namespace |
484 | 486 |
485 } // namespace base | 487 } // namespace base |
OLD | NEW |