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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
| 9 #include <memory> |
| 10 |
9 #include "base/base_paths.h" | 11 #include "base/base_paths.h" |
10 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
11 #include "base/logging.h" | 13 #include "base/logging.h" |
12 #include "base/macros.h" | 14 #include "base/macros.h" |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "base/path_service.h" | 15 #include "base/path_service.h" |
15 #include "base/strings/string_piece.h" | 16 #include "base/strings/string_piece.h" |
16 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
17 #include "base/values.h" | 18 #include "base/values.h" |
18 #include "build/build_config.h" | 19 #include "build/build_config.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
20 | 21 |
21 namespace base { | 22 namespace base { |
22 | 23 |
23 TEST(JSONReaderTest, Reading) { | 24 TEST(JSONReaderTest, Reading) { |
24 // some whitespace checking | 25 // some whitespace checking |
25 scoped_ptr<Value> root = JSONReader().ReadToValue(" null "); | 26 std::unique_ptr<Value> root = JSONReader().ReadToValue(" null "); |
26 ASSERT_TRUE(root.get()); | 27 ASSERT_TRUE(root.get()); |
27 EXPECT_TRUE(root->IsType(Value::TYPE_NULL)); | 28 EXPECT_TRUE(root->IsType(Value::TYPE_NULL)); |
28 | 29 |
29 // Invalid JSON string | 30 // Invalid JSON string |
30 root = JSONReader().ReadToValue("nu"); | 31 root = JSONReader().ReadToValue("nu"); |
31 EXPECT_FALSE(root.get()); | 32 EXPECT_FALSE(root.get()); |
32 | 33 |
33 // Simple bool | 34 // Simple bool |
34 root = JSONReader().ReadToValue("true "); | 35 root = JSONReader().ReadToValue("true "); |
35 ASSERT_TRUE(root.get()); | 36 ASSERT_TRUE(root.get()); |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 EXPECT_FALSE(root.get()); | 245 EXPECT_FALSE(root.get()); |
245 | 246 |
246 // Basic array | 247 // Basic array |
247 root = JSONReader::Read("[true, false, null]"); | 248 root = JSONReader::Read("[true, false, null]"); |
248 ASSERT_TRUE(root.get()); | 249 ASSERT_TRUE(root.get()); |
249 EXPECT_TRUE(root->IsType(Value::TYPE_LIST)); | 250 EXPECT_TRUE(root->IsType(Value::TYPE_LIST)); |
250 list = static_cast<ListValue*>(root.get()); | 251 list = static_cast<ListValue*>(root.get()); |
251 EXPECT_EQ(3U, list->GetSize()); | 252 EXPECT_EQ(3U, list->GetSize()); |
252 | 253 |
253 // Test with trailing comma. Should be parsed the same as above. | 254 // Test with trailing comma. Should be parsed the same as above. |
254 scoped_ptr<Value> root2 = | 255 std::unique_ptr<Value> root2 = |
255 JSONReader::Read("[true, false, null, ]", JSON_ALLOW_TRAILING_COMMAS); | 256 JSONReader::Read("[true, false, null, ]", JSON_ALLOW_TRAILING_COMMAS); |
256 EXPECT_TRUE(root->Equals(root2.get())); | 257 EXPECT_TRUE(root->Equals(root2.get())); |
257 | 258 |
258 // Empty array | 259 // Empty array |
259 root = JSONReader::Read("[]"); | 260 root = JSONReader::Read("[]"); |
260 ASSERT_TRUE(root.get()); | 261 ASSERT_TRUE(root.get()); |
261 EXPECT_TRUE(root->IsType(Value::TYPE_LIST)); | 262 EXPECT_TRUE(root->IsType(Value::TYPE_LIST)); |
262 list = static_cast<ListValue*>(root.get()); | 263 list = static_cast<ListValue*>(root.get()); |
263 EXPECT_EQ(0U, list->GetSize()); | 264 EXPECT_EQ(0U, list->GetSize()); |
264 | 265 |
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
546 FilePath path; | 547 FilePath path; |
547 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &path)); | 548 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &path)); |
548 path = path.AppendASCII("json"); | 549 path = path.AppendASCII("json"); |
549 ASSERT_TRUE(base::PathExists(path)); | 550 ASSERT_TRUE(base::PathExists(path)); |
550 | 551 |
551 std::string input; | 552 std::string input; |
552 ASSERT_TRUE(ReadFileToString( | 553 ASSERT_TRUE(ReadFileToString( |
553 path.Append(FILE_PATH_LITERAL("bom_feff.json")), &input)); | 554 path.Append(FILE_PATH_LITERAL("bom_feff.json")), &input)); |
554 | 555 |
555 JSONReader reader; | 556 JSONReader reader; |
556 scoped_ptr<Value> root(reader.ReadToValue(input)); | 557 std::unique_ptr<Value> root(reader.ReadToValue(input)); |
557 ASSERT_TRUE(root.get()) << reader.GetErrorMessage(); | 558 ASSERT_TRUE(root.get()) << reader.GetErrorMessage(); |
558 EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); | 559 EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); |
559 } | 560 } |
560 | 561 |
561 // Tests that the root of a JSON object can be deleted safely while its | 562 // Tests that the root of a JSON object can be deleted safely while its |
562 // children outlive it. | 563 // children outlive it. |
563 TEST(JSONReaderTest, StringOptimizations) { | 564 TEST(JSONReaderTest, StringOptimizations) { |
564 scoped_ptr<Value> dict_literal_0; | 565 std::unique_ptr<Value> dict_literal_0; |
565 scoped_ptr<Value> dict_literal_1; | 566 std::unique_ptr<Value> dict_literal_1; |
566 scoped_ptr<Value> dict_string_0; | 567 std::unique_ptr<Value> dict_string_0; |
567 scoped_ptr<Value> dict_string_1; | 568 std::unique_ptr<Value> dict_string_1; |
568 scoped_ptr<Value> list_value_0; | 569 std::unique_ptr<Value> list_value_0; |
569 scoped_ptr<Value> list_value_1; | 570 std::unique_ptr<Value> list_value_1; |
570 | 571 |
571 { | 572 { |
572 scoped_ptr<Value> root = JSONReader::Read( | 573 std::unique_ptr<Value> root = JSONReader::Read( |
573 "{" | 574 "{" |
574 " \"test\": {" | 575 " \"test\": {" |
575 " \"foo\": true," | 576 " \"foo\": true," |
576 " \"bar\": 3.14," | 577 " \"bar\": 3.14," |
577 " \"baz\": \"bat\"," | 578 " \"baz\": \"bat\"," |
578 " \"moo\": \"cow\"" | 579 " \"moo\": \"cow\"" |
579 " }," | 580 " }," |
580 " \"list\": [" | 581 " \"list\": [" |
581 " \"a\"," | 582 " \"a\"," |
582 " \"b\"" | 583 " \"b\"" |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
650 | 651 |
651 TEST(JSONReaderTest, IllegalTrailingNull) { | 652 TEST(JSONReaderTest, IllegalTrailingNull) { |
652 const char json[] = { '"', 'n', 'u', 'l', 'l', '"', '\0' }; | 653 const char json[] = { '"', 'n', 'u', 'l', 'l', '"', '\0' }; |
653 std::string json_string(json, sizeof(json)); | 654 std::string json_string(json, sizeof(json)); |
654 JSONReader reader; | 655 JSONReader reader; |
655 EXPECT_FALSE(reader.ReadToValue(json_string)); | 656 EXPECT_FALSE(reader.ReadToValue(json_string)); |
656 EXPECT_EQ(JSONReader::JSON_UNEXPECTED_DATA_AFTER_ROOT, reader.error_code()); | 657 EXPECT_EQ(JSONReader::JSON_UNEXPECTED_DATA_AFTER_ROOT, reader.error_code()); |
657 } | 658 } |
658 | 659 |
659 } // namespace base | 660 } // namespace base |
OLD | NEW |