OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "testing/gtest/include/gtest/gtest.h" | 5 #include "base/basictypes.h" |
6 #include "base/json/json_reader.h" | 6 #include "base/json/json_reader.h" |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/string_piece.h" | 8 #include "base/string_piece.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
12 | 13 |
13 namespace base { | 14 namespace base { |
14 | 15 |
15 TEST(JSONReaderTest, Reading) { | 16 TEST(JSONReaderTest, Reading) { |
16 // some whitespace checking | 17 // some whitespace checking |
17 scoped_ptr<Value> root; | 18 scoped_ptr<Value> root; |
18 root.reset(JSONReader().JsonToValue(" null ", false, false)); | 19 root.reset(JSONReader().JsonToValue(" null ", false, false)); |
19 ASSERT_TRUE(root.get()); | 20 ASSERT_TRUE(root.get()); |
20 ASSERT_TRUE(root->IsType(Value::TYPE_NULL)); | 21 ASSERT_TRUE(root->IsType(Value::TYPE_NULL)); |
21 | 22 |
(...skipping 24 matching lines...) Expand all Loading... |
46 ASSERT_TRUE(root->GetAsString(&value)); | 47 ASSERT_TRUE(root->GetAsString(&value)); |
47 ASSERT_EQ("sample string", value); | 48 ASSERT_EQ("sample string", value); |
48 | 49 |
49 // Test number formats | 50 // Test number formats |
50 root.reset(JSONReader().JsonToValue("43", false, false)); | 51 root.reset(JSONReader().JsonToValue("43", false, false)); |
51 ASSERT_TRUE(root.get()); | 52 ASSERT_TRUE(root.get()); |
52 ASSERT_TRUE(root->IsType(Value::TYPE_INTEGER)); | 53 ASSERT_TRUE(root->IsType(Value::TYPE_INTEGER)); |
53 int int_val = 0; | 54 int int_val = 0; |
54 ASSERT_TRUE(root->GetAsInteger(&int_val)); | 55 ASSERT_TRUE(root->GetAsInteger(&int_val)); |
55 ASSERT_EQ(43, int_val); | 56 ASSERT_EQ(43, int_val); |
| 57 root.reset(JSONReader().JsonToValue("2147483648", false, false)); |
| 58 ASSERT_TRUE(root.get()); |
| 59 ASSERT_TRUE(root->IsType(Value::TYPE_INTEGER64)); |
| 60 int64 int64_val = 0; |
| 61 ASSERT_TRUE(root->GetAsInteger64(&int64_val)); |
| 62 ASSERT_EQ(2147483648, int64_val); |
56 | 63 |
57 // According to RFC4627, oct, hex, and leading zeros are invalid JSON. | 64 // According to RFC4627, oct, hex, and leading zeros are invalid JSON. |
58 root.reset(JSONReader().JsonToValue("043", false, false)); | 65 root.reset(JSONReader().JsonToValue("043", false, false)); |
59 ASSERT_FALSE(root.get()); | 66 ASSERT_FALSE(root.get()); |
60 root.reset(JSONReader().JsonToValue("0x43", false, false)); | 67 root.reset(JSONReader().JsonToValue("0x43", false, false)); |
61 ASSERT_FALSE(root.get()); | 68 ASSERT_FALSE(root.get()); |
62 root.reset(JSONReader().JsonToValue("00", false, false)); | 69 root.reset(JSONReader().JsonToValue("00", false, false)); |
63 ASSERT_FALSE(root.get()); | 70 ASSERT_FALSE(root.get()); |
64 | 71 |
65 // Test 0 (which needs to be special cased because of the leading zero | 72 // Test 0 (which needs to be special cased because of the leading zero |
66 // clause). | 73 // clause). |
67 root.reset(JSONReader().JsonToValue("0", false, false)); | 74 root.reset(JSONReader().JsonToValue("0", false, false)); |
68 ASSERT_TRUE(root.get()); | 75 ASSERT_TRUE(root.get()); |
69 ASSERT_TRUE(root->IsType(Value::TYPE_INTEGER)); | 76 ASSERT_TRUE(root->IsType(Value::TYPE_INTEGER)); |
70 int_val = 1; | 77 int_val = 1; |
71 ASSERT_TRUE(root->GetAsInteger(&int_val)); | 78 ASSERT_TRUE(root->GetAsInteger(&int_val)); |
72 ASSERT_EQ(0, int_val); | 79 ASSERT_EQ(0, int_val); |
73 | 80 |
74 // Numbers that overflow ints should succeed, being internally promoted to | 81 // Numbers that overflow int64s should succeed, being internally promoted to |
75 // storage as doubles | 82 // storage as doubles |
76 root.reset(JSONReader().JsonToValue("2147483648", false, false)); | 83 root.reset(JSONReader().JsonToValue("9223372036854775808", false, false)); |
77 ASSERT_TRUE(root.get()); | 84 ASSERT_TRUE(root.get()); |
78 double double_val; | 85 double double_val; |
79 ASSERT_TRUE(root->IsType(Value::TYPE_DOUBLE)); | 86 ASSERT_TRUE(root->IsType(Value::TYPE_DOUBLE)); |
80 double_val = 0.0; | 87 double_val = 0.0; |
81 ASSERT_TRUE(root->GetAsDouble(&double_val)); | 88 ASSERT_TRUE(root->GetAsDouble(&double_val)); |
82 ASSERT_DOUBLE_EQ(2147483648.0, double_val); | 89 ASSERT_DOUBLE_EQ(9223372036854775808.0, double_val); |
83 root.reset(JSONReader().JsonToValue("-2147483649", false, false)); | 90 root.reset(JSONReader().JsonToValue("-9223372036854775809", false, false)); |
84 ASSERT_TRUE(root.get()); | 91 ASSERT_TRUE(root.get()); |
85 ASSERT_TRUE(root->IsType(Value::TYPE_DOUBLE)); | 92 ASSERT_TRUE(root->IsType(Value::TYPE_DOUBLE)); |
86 double_val = 0.0; | 93 double_val = 0.0; |
87 ASSERT_TRUE(root->GetAsDouble(&double_val)); | 94 ASSERT_TRUE(root->GetAsDouble(&double_val)); |
88 ASSERT_DOUBLE_EQ(-2147483649.0, double_val); | 95 ASSERT_DOUBLE_EQ(-9223372036854775809.0, double_val); |
89 | 96 |
90 // Parse a double | 97 // Parse a double |
91 root.reset(JSONReader().JsonToValue("43.1", false, false)); | 98 root.reset(JSONReader().JsonToValue("43.1", false, false)); |
92 ASSERT_TRUE(root.get()); | 99 ASSERT_TRUE(root.get()); |
93 ASSERT_TRUE(root->IsType(Value::TYPE_DOUBLE)); | 100 ASSERT_TRUE(root->IsType(Value::TYPE_DOUBLE)); |
94 double_val = 0.0; | 101 double_val = 0.0; |
95 ASSERT_TRUE(root->GetAsDouble(&double_val)); | 102 ASSERT_TRUE(root->GetAsDouble(&double_val)); |
96 ASSERT_DOUBLE_EQ(43.1, double_val); | 103 ASSERT_DOUBLE_EQ(43.1, double_val); |
97 | 104 |
98 root.reset(JSONReader().JsonToValue("4.3e-1", false, false)); | 105 root.reset(JSONReader().JsonToValue("4.3e-1", false, false)); |
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
557 | 564 |
558 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\q\"]", false, | 565 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\q\"]", false, |
559 &error_code, &error_message)); | 566 &error_code, &error_message)); |
560 EXPECT_FALSE(root.get()); | 567 EXPECT_FALSE(root.get()); |
561 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape), | 568 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape), |
562 error_message); | 569 error_message); |
563 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code); | 570 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code); |
564 } | 571 } |
565 | 572 |
566 } // namespace base | 573 } // namespace base |
OLD | NEW |