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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
7 #include "base/json/json_file_value_serializer.h" | 7 #include "base/json/json_file_value_serializer.h" |
8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
9 #include "base/json/json_string_value_serializer.h" | 9 #include "base/json/json_string_value_serializer.h" |
10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 root.reset(serializer.Deserialize(NULL, NULL)); | 188 root.reset(serializer.Deserialize(NULL, NULL)); |
189 ASSERT_TRUE(root.get()); | 189 ASSERT_TRUE(root.get()); |
190 root_expected.reset(serializer_expected.Deserialize(NULL, NULL)); | 190 root_expected.reset(serializer_expected.Deserialize(NULL, NULL)); |
191 ASSERT_TRUE(root_expected.get()); | 191 ASSERT_TRUE(root_expected.get()); |
192 ASSERT_TRUE(root->Equals(root_expected.get())); | 192 ASSERT_TRUE(root->Equals(root_expected.get())); |
193 } | 193 } |
194 | 194 |
195 namespace { | 195 namespace { |
196 | 196 |
197 void ValidateJsonList(const std::string& json) { | 197 void ValidateJsonList(const std::string& json) { |
198 scoped_ptr<Value> root(base::JSONReader::Read(json, false)); | 198 scoped_ptr<Value> root(base::JSONReader::Read(json)); |
199 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST)); | 199 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST)); |
200 ListValue* list = static_cast<ListValue*>(root.get()); | 200 ListValue* list = static_cast<ListValue*>(root.get()); |
201 ASSERT_EQ(1U, list->GetSize()); | 201 ASSERT_EQ(1U, list->GetSize()); |
202 Value* elt = NULL; | 202 Value* elt = NULL; |
203 ASSERT_TRUE(list->Get(0, &elt)); | 203 ASSERT_TRUE(list->Get(0, &elt)); |
204 int value = 0; | 204 int value = 0; |
205 ASSERT_TRUE(elt && elt->GetAsInteger(&value)); | 205 ASSERT_TRUE(elt && elt->GetAsInteger(&value)); |
206 ASSERT_EQ(1, value); | 206 ASSERT_EQ(1, value); |
207 } | 207 } |
208 | 208 |
209 } // namespace | 209 } // namespace |
210 | 210 |
211 TEST(JSONValueSerializerTest, JSONReaderComments) { | 211 TEST(JSONValueSerializerTest, JSONReaderComments) { |
212 ValidateJsonList("[ // 2, 3, ignore me ] \n1 ]"); | 212 ValidateJsonList("[ // 2, 3, ignore me ] \n1 ]"); |
213 ValidateJsonList("[ /* 2, \n3, ignore me ]*/ \n1 ]"); | 213 ValidateJsonList("[ /* 2, \n3, ignore me ]*/ \n1 ]"); |
214 ValidateJsonList("//header\n[ // 2, \n// 3, \n1 ]// footer"); | 214 ValidateJsonList("//header\n[ // 2, \n// 3, \n1 ]// footer"); |
215 ValidateJsonList("/*\n[ // 2, \n// 3, \n1 ]*/[1]"); | 215 ValidateJsonList("/*\n[ // 2, \n// 3, \n1 ]*/[1]"); |
216 ValidateJsonList("[ 1 /* one */ ] /* end */"); | 216 ValidateJsonList("[ 1 /* one */ ] /* end */"); |
217 ValidateJsonList("[ 1 //// ,2\r\n ]"); | 217 ValidateJsonList("[ 1 //// ,2\r\n ]"); |
218 | 218 |
219 scoped_ptr<Value> root; | 219 scoped_ptr<Value> root; |
220 | 220 |
221 // It's ok to have a comment in a string. | 221 // It's ok to have a comment in a string. |
222 root.reset(base::JSONReader::Read("[\"// ok\\n /* foo */ \"]", false)); | 222 root.reset(base::JSONReader::Read("[\"// ok\\n /* foo */ \"]")); |
223 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST)); | 223 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST)); |
224 ListValue* list = static_cast<ListValue*>(root.get()); | 224 ListValue* list = static_cast<ListValue*>(root.get()); |
225 ASSERT_EQ(1U, list->GetSize()); | 225 ASSERT_EQ(1U, list->GetSize()); |
226 Value* elt = NULL; | 226 Value* elt = NULL; |
227 ASSERT_TRUE(list->Get(0, &elt)); | 227 ASSERT_TRUE(list->Get(0, &elt)); |
228 std::string value; | 228 std::string value; |
229 ASSERT_TRUE(elt && elt->GetAsString(&value)); | 229 ASSERT_TRUE(elt && elt->GetAsString(&value)); |
230 ASSERT_EQ("// ok\n /* foo */ ", value); | 230 ASSERT_EQ("// ok\n /* foo */ ", value); |
231 | 231 |
232 // You can't nest comments. | 232 // You can't nest comments. |
233 root.reset(base::JSONReader::Read("/* /* inner */ outer */ [ 1 ]", false)); | 233 root.reset(base::JSONReader::Read("/* /* inner */ outer */ [ 1 ]")); |
234 ASSERT_FALSE(root.get()); | 234 ASSERT_FALSE(root.get()); |
235 | 235 |
236 // Not a open comment token. | 236 // Not a open comment token. |
237 root.reset(base::JSONReader::Read("/ * * / [1]", false)); | 237 root.reset(base::JSONReader::Read("/ * * / [1]")); |
238 ASSERT_FALSE(root.get()); | 238 ASSERT_FALSE(root.get()); |
239 } | 239 } |
240 | 240 |
241 class JSONFileValueSerializerTest : public testing::Test { | 241 class JSONFileValueSerializerTest : public testing::Test { |
242 protected: | 242 protected: |
243 virtual void SetUp() { | 243 virtual void SetUp() { |
244 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 244 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
245 } | 245 } |
246 | 246 |
247 ScopedTempDir temp_dir_; | 247 ScopedTempDir temp_dir_; |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 FilePath source_file_path; | 330 FilePath source_file_path; |
331 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_file_path)); | 331 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_file_path)); |
332 source_file_path = source_file_path.Append( | 332 source_file_path = source_file_path.Append( |
333 FILE_PATH_LITERAL("serializer_test_nowhitespace.js")); | 333 FILE_PATH_LITERAL("serializer_test_nowhitespace.js")); |
334 ASSERT_TRUE(file_util::PathExists(source_file_path)); | 334 ASSERT_TRUE(file_util::PathExists(source_file_path)); |
335 JSONFileValueSerializer serializer(source_file_path); | 335 JSONFileValueSerializer serializer(source_file_path); |
336 scoped_ptr<Value> root; | 336 scoped_ptr<Value> root; |
337 root.reset(serializer.Deserialize(NULL, NULL)); | 337 root.reset(serializer.Deserialize(NULL, NULL)); |
338 ASSERT_TRUE(root.get()); | 338 ASSERT_TRUE(root.get()); |
339 } | 339 } |
OLD | NEW |