| 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/json/json_reader.h" | 5 #include "base/json/json_reader.h" |
| 6 | 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "base/base_paths.h" |
| 8 #include "base/file_util.h" |
| 8 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/path_service.h" |
| 9 #include "base/string_piece.h" | 11 #include "base/string_piece.h" |
| 10 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 11 #include "base/values.h" | 13 #include "base/values.h" |
| 12 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 16 |
| 14 namespace base { | 17 namespace base { |
| 15 | 18 |
| 16 TEST(JSONReaderTest, Reading) { | 19 TEST(JSONReaderTest, Reading) { |
| 17 // some whitespace checking | 20 // some whitespace checking |
| 18 scoped_ptr<Value> root; | 21 scoped_ptr<Value> root; |
| 19 root.reset(JSONReader().JsonToValue(" null ", false, false)); | 22 root.reset(JSONReader().JsonToValue(" null ", false, false)); |
| 20 ASSERT_TRUE(root.get()); | 23 ASSERT_TRUE(root.get()); |
| 21 EXPECT_TRUE(root->IsType(Value::TYPE_NULL)); | 24 EXPECT_TRUE(root->IsType(Value::TYPE_NULL)); |
| 22 | 25 |
| (...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 493 root.reset(JSONReader::Read("null", false)); | 496 root.reset(JSONReader::Read("null", false)); |
| 494 EXPECT_FALSE(root.get()); | 497 EXPECT_FALSE(root.get()); |
| 495 root.reset(JSONReader::Read("true", false)); | 498 root.reset(JSONReader::Read("true", false)); |
| 496 EXPECT_FALSE(root.get()); | 499 EXPECT_FALSE(root.get()); |
| 497 root.reset(JSONReader::Read("10", false)); | 500 root.reset(JSONReader::Read("10", false)); |
| 498 EXPECT_FALSE(root.get()); | 501 EXPECT_FALSE(root.get()); |
| 499 root.reset(JSONReader::Read("\"root\"", false)); | 502 root.reset(JSONReader::Read("\"root\"", false)); |
| 500 EXPECT_FALSE(root.get()); | 503 EXPECT_FALSE(root.get()); |
| 501 } | 504 } |
| 502 | 505 |
| 506 TEST(JSONReaderTest, ReadFromFile) { |
| 507 FilePath path; |
| 508 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &path)); |
| 509 path = path.Append(FILE_PATH_LITERAL("base")) |
| 510 .Append(FILE_PATH_LITERAL("data")) |
| 511 .Append(FILE_PATH_LITERAL("json")); |
| 512 |
| 513 std::string input; |
| 514 ASSERT_TRUE(file_util::ReadFileToString( |
| 515 path.Append(FILE_PATH_LITERAL("bom_feff.json")), &input)); |
| 516 |
| 517 JSONReader reader; |
| 518 scoped_ptr<Value> root(reader.JsonToValue(input, false, false)); |
| 519 ASSERT_TRUE(root.get()) << reader.GetErrorMessage(); |
| 520 EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); |
| 521 } |
| 522 |
| 503 TEST(JSONReaderTest, ErrorMessages) { | 523 TEST(JSONReaderTest, ErrorMessages) { |
| 504 // Error strings should not be modified in case of success. | 524 // Error strings should not be modified in case of success. |
| 505 std::string error_message; | 525 std::string error_message; |
| 506 int error_code = 0; | 526 int error_code = 0; |
| 507 scoped_ptr<Value> root; | 527 scoped_ptr<Value> root; |
| 508 root.reset(JSONReader::ReadAndReturnError("[42]", false, | 528 root.reset(JSONReader::ReadAndReturnError("[42]", false, |
| 509 &error_code, &error_message)); | 529 &error_code, &error_message)); |
| 510 EXPECT_TRUE(error_message.empty()); | 530 EXPECT_TRUE(error_message.empty()); |
| 511 EXPECT_EQ(0, error_code); | 531 EXPECT_EQ(0, error_code); |
| 512 | 532 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 590 | 610 |
| 591 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\q\"]", false, | 611 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\q\"]", false, |
| 592 &error_code, &error_message)); | 612 &error_code, &error_message)); |
| 593 EXPECT_FALSE(root.get()); | 613 EXPECT_FALSE(root.get()); |
| 594 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape), | 614 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape), |
| 595 error_message); | 615 error_message); |
| 596 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code); | 616 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code); |
| 597 } | 617 } |
| 598 | 618 |
| 599 } // namespace base | 619 } // namespace base |
| OLD | NEW |