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" |
11 #include "base/json/json_string_value_serializer.h" | 11 #include "base/json/json_string_value_serializer.h" |
12 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/path_service.h" |
14 #include "base/string_util.h" | 15 #include "base/string_util.h" |
15 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
16 #include "base/values.h" | 17 #include "base/values.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 | 19 |
19 namespace base { | 20 namespace base { |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 // Some proper JSON to test with: | 24 // Some proper JSON to test with: |
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 | 363 |
363 // You can't nest comments. | 364 // You can't nest comments. |
364 root.reset(JSONReader::Read("/* /* inner */ outer */ [ 1 ]")); | 365 root.reset(JSONReader::Read("/* /* inner */ outer */ [ 1 ]")); |
365 ASSERT_FALSE(root.get()); | 366 ASSERT_FALSE(root.get()); |
366 | 367 |
367 // Not a open comment token. | 368 // Not a open comment token. |
368 root.reset(JSONReader::Read("/ * * / [1]")); | 369 root.reset(JSONReader::Read("/ * * / [1]")); |
369 ASSERT_FALSE(root.get()); | 370 ASSERT_FALSE(root.get()); |
370 } | 371 } |
371 | 372 |
| 373 class JSONFileValueSerializerTest : public testing::Test { |
| 374 protected: |
| 375 virtual void SetUp() OVERRIDE { |
| 376 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 377 } |
| 378 |
| 379 base::ScopedTempDir temp_dir_; |
| 380 }; |
| 381 |
| 382 TEST_F(JSONFileValueSerializerTest, Roundtrip) { |
| 383 base::FilePath original_file_path; |
| 384 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path)); |
| 385 original_file_path = |
| 386 original_file_path.Append(FILE_PATH_LITERAL("serializer_test.json")); |
| 387 |
| 388 ASSERT_TRUE(file_util::PathExists(original_file_path)); |
| 389 |
| 390 JSONFileValueSerializer deserializer(original_file_path); |
| 391 scoped_ptr<Value> root; |
| 392 root.reset(deserializer.Deserialize(NULL, NULL)); |
| 393 |
| 394 ASSERT_TRUE(root.get()); |
| 395 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); |
| 396 |
| 397 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); |
| 398 |
| 399 Value* null_value = NULL; |
| 400 ASSERT_TRUE(root_dict->Get("null", &null_value)); |
| 401 ASSERT_TRUE(null_value); |
| 402 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL)); |
| 403 |
| 404 bool bool_value = false; |
| 405 ASSERT_TRUE(root_dict->GetBoolean("bool", &bool_value)); |
| 406 ASSERT_TRUE(bool_value); |
| 407 |
| 408 int int_value = 0; |
| 409 ASSERT_TRUE(root_dict->GetInteger("int", &int_value)); |
| 410 ASSERT_EQ(42, int_value); |
| 411 |
| 412 std::string string_value; |
| 413 ASSERT_TRUE(root_dict->GetString("string", &string_value)); |
| 414 ASSERT_EQ("hello", string_value); |
| 415 |
| 416 // Now try writing. |
| 417 const base::FilePath written_file_path = |
| 418 temp_dir_.path().Append(FILE_PATH_LITERAL("test_output.js")); |
| 419 |
| 420 ASSERT_FALSE(file_util::PathExists(written_file_path)); |
| 421 JSONFileValueSerializer serializer(written_file_path); |
| 422 ASSERT_TRUE(serializer.Serialize(*root)); |
| 423 ASSERT_TRUE(file_util::PathExists(written_file_path)); |
| 424 |
| 425 // Now compare file contents. |
| 426 EXPECT_TRUE(file_util::TextContentsEqual(original_file_path, |
| 427 written_file_path)); |
| 428 EXPECT_TRUE(file_util::Delete(written_file_path, false)); |
| 429 } |
| 430 |
| 431 TEST_F(JSONFileValueSerializerTest, RoundtripNested) { |
| 432 base::FilePath original_file_path; |
| 433 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path)); |
| 434 original_file_path = original_file_path.Append( |
| 435 FILE_PATH_LITERAL("serializer_nested_test.json")); |
| 436 |
| 437 ASSERT_TRUE(file_util::PathExists(original_file_path)); |
| 438 |
| 439 JSONFileValueSerializer deserializer(original_file_path); |
| 440 scoped_ptr<Value> root; |
| 441 root.reset(deserializer.Deserialize(NULL, NULL)); |
| 442 ASSERT_TRUE(root.get()); |
| 443 |
| 444 // Now try writing. |
| 445 base::FilePath written_file_path = temp_dir_.path().Append( |
| 446 FILE_PATH_LITERAL("test_output.json")); |
| 447 |
| 448 ASSERT_FALSE(file_util::PathExists(written_file_path)); |
| 449 JSONFileValueSerializer serializer(written_file_path); |
| 450 ASSERT_TRUE(serializer.Serialize(*root)); |
| 451 ASSERT_TRUE(file_util::PathExists(written_file_path)); |
| 452 |
| 453 // Now compare file contents. |
| 454 EXPECT_TRUE(file_util::TextContentsEqual(original_file_path, |
| 455 written_file_path)); |
| 456 EXPECT_TRUE(file_util::Delete(written_file_path, false)); |
| 457 } |
| 458 |
| 459 TEST_F(JSONFileValueSerializerTest, NoWhitespace) { |
| 460 base::FilePath source_file_path; |
| 461 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &source_file_path)); |
| 462 source_file_path = source_file_path.Append( |
| 463 FILE_PATH_LITERAL("serializer_test_nowhitespace.json")); |
| 464 ASSERT_TRUE(file_util::PathExists(source_file_path)); |
| 465 JSONFileValueSerializer serializer(source_file_path); |
| 466 scoped_ptr<Value> root; |
| 467 root.reset(serializer.Deserialize(NULL, NULL)); |
| 468 ASSERT_TRUE(root.get()); |
| 469 } |
| 470 |
372 } // namespace | 471 } // namespace |
373 | 472 |
374 } // namespace base | 473 } // namespace base |
OLD | NEW |