[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
| 5 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 93d49d7 | 2009-10-23 20:00:20 | [diff] [blame] | 6 | #include "base/json/json_reader.h" |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 7 | #include "base/scoped_ptr.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 8 | #include "base/values.h" |
[email protected] | b509f7a | 2008-08-08 17:26:42 | [diff] [blame] | 9 | #include "build/build_config.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 10 | |
[email protected] | 93d49d7 | 2009-10-23 20:00:20 | [diff] [blame] | 11 | namespace base { |
| 12 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 13 | TEST(JSONReaderTest, Reading) { |
| 14 | // some whitespace checking |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 15 | scoped_ptr<Value> root; |
| 16 | root.reset(JSONReader().JsonToValue(" null ", false, false)); |
| 17 | ASSERT_TRUE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 18 | ASSERT_TRUE(root->IsType(Value::TYPE_NULL)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 19 | |
| 20 | // Invalid JSON string |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 21 | root.reset(JSONReader().JsonToValue("nu", false, false)); |
| 22 | ASSERT_FALSE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 23 | |
| 24 | // Simple bool |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 25 | root.reset(JSONReader().JsonToValue("true ", false, false)); |
| 26 | ASSERT_TRUE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 27 | ASSERT_TRUE(root->IsType(Value::TYPE_BOOLEAN)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 28 | |
[email protected] | 5b0bc25 | 2009-07-23 21:34:40 | [diff] [blame] | 29 | // Embedded comment |
| 30 | root.reset(JSONReader().JsonToValue("/* comment */null", false, false)); |
| 31 | ASSERT_TRUE(root.get()); |
| 32 | ASSERT_TRUE(root->IsType(Value::TYPE_NULL)); |
| 33 | root.reset(JSONReader().JsonToValue("40 /* comment */", false, false)); |
| 34 | ASSERT_TRUE(root.get()); |
| 35 | ASSERT_TRUE(root->IsType(Value::TYPE_INTEGER)); |
| 36 | root.reset(JSONReader().JsonToValue("true // comment", false, false)); |
| 37 | ASSERT_TRUE(root.get()); |
| 38 | ASSERT_TRUE(root->IsType(Value::TYPE_BOOLEAN)); |
| 39 | root.reset(JSONReader().JsonToValue("/* comment */\"sample string\"", |
| 40 | false, false)); |
| 41 | ASSERT_TRUE(root.get()); |
| 42 | ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); |
| 43 | std::string value; |
| 44 | ASSERT_TRUE(root->GetAsString(&value)); |
| 45 | ASSERT_EQ("sample string", value); |
| 46 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 47 | // Test number formats |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 48 | root.reset(JSONReader().JsonToValue("43", false, false)); |
| 49 | ASSERT_TRUE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 50 | ASSERT_TRUE(root->IsType(Value::TYPE_INTEGER)); |
| 51 | int int_val = 0; |
| 52 | ASSERT_TRUE(root->GetAsInteger(&int_val)); |
| 53 | ASSERT_EQ(43, int_val); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 54 | |
| 55 | // According to RFC4627, oct, hex, and leading zeros are invalid JSON. |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 56 | root.reset(JSONReader().JsonToValue("043", false, false)); |
| 57 | ASSERT_FALSE(root.get()); |
| 58 | root.reset(JSONReader().JsonToValue("0x43", false, false)); |
| 59 | ASSERT_FALSE(root.get()); |
| 60 | root.reset(JSONReader().JsonToValue("00", false, false)); |
| 61 | ASSERT_FALSE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 62 | |
| 63 | // Test 0 (which needs to be special cased because of the leading zero |
| 64 | // clause). |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 65 | root.reset(JSONReader().JsonToValue("0", false, false)); |
| 66 | ASSERT_TRUE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 67 | ASSERT_TRUE(root->IsType(Value::TYPE_INTEGER)); |
| 68 | int_val = 1; |
| 69 | ASSERT_TRUE(root->GetAsInteger(&int_val)); |
| 70 | ASSERT_EQ(0, int_val); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 71 | |
[email protected] | d9023ab | 2008-08-07 17:15:41 | [diff] [blame] | 72 | // Numbers that overflow ints should succeed, being internally promoted to |
| 73 | // storage as doubles |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 74 | root.reset(JSONReader().JsonToValue("2147483648", false, false)); |
| 75 | ASSERT_TRUE(root.get()); |
[email protected] | b509f7a | 2008-08-08 17:26:42 | [diff] [blame] | 76 | double real_val; |
[email protected] | b509f7a | 2008-08-08 17:26:42 | [diff] [blame] | 77 | ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); |
| 78 | real_val = 0.0; |
| 79 | ASSERT_TRUE(root->GetAsReal(&real_val)); |
| 80 | ASSERT_DOUBLE_EQ(2147483648.0, real_val); |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 81 | root.reset(JSONReader().JsonToValue("-2147483649", false, false)); |
| 82 | ASSERT_TRUE(root.get()); |
[email protected] | b509f7a | 2008-08-08 17:26:42 | [diff] [blame] | 83 | ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); |
| 84 | real_val = 0.0; |
| 85 | ASSERT_TRUE(root->GetAsReal(&real_val)); |
| 86 | ASSERT_DOUBLE_EQ(-2147483649.0, real_val); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 87 | |
| 88 | // Parse a double |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 89 | root.reset(JSONReader().JsonToValue("43.1", false, false)); |
| 90 | ASSERT_TRUE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 91 | ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); |
[email protected] | b509f7a | 2008-08-08 17:26:42 | [diff] [blame] | 92 | real_val = 0.0; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 93 | ASSERT_TRUE(root->GetAsReal(&real_val)); |
| 94 | ASSERT_DOUBLE_EQ(43.1, real_val); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 95 | |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 96 | root.reset(JSONReader().JsonToValue("4.3e-1", false, false)); |
| 97 | ASSERT_TRUE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 98 | ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); |
| 99 | real_val = 0.0; |
| 100 | ASSERT_TRUE(root->GetAsReal(&real_val)); |
| 101 | ASSERT_DOUBLE_EQ(.43, real_val); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 102 | |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 103 | root.reset(JSONReader().JsonToValue("2.1e0", false, false)); |
| 104 | ASSERT_TRUE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 105 | ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); |
| 106 | real_val = 0.0; |
| 107 | ASSERT_TRUE(root->GetAsReal(&real_val)); |
| 108 | ASSERT_DOUBLE_EQ(2.1, real_val); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 109 | |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 110 | root.reset(JSONReader().JsonToValue("2.1e+0001", false, false)); |
| 111 | ASSERT_TRUE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 112 | ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); |
| 113 | real_val = 0.0; |
| 114 | ASSERT_TRUE(root->GetAsReal(&real_val)); |
| 115 | ASSERT_DOUBLE_EQ(21.0, real_val); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 116 | |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 117 | root.reset(JSONReader().JsonToValue("0.01", false, false)); |
| 118 | ASSERT_TRUE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 119 | ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); |
| 120 | real_val = 0.0; |
| 121 | ASSERT_TRUE(root->GetAsReal(&real_val)); |
| 122 | ASSERT_DOUBLE_EQ(0.01, real_val); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 123 | |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 124 | root.reset(JSONReader().JsonToValue("1.00", false, false)); |
| 125 | ASSERT_TRUE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 126 | ASSERT_TRUE(root->IsType(Value::TYPE_REAL)); |
| 127 | real_val = 0.0; |
| 128 | ASSERT_TRUE(root->GetAsReal(&real_val)); |
| 129 | ASSERT_DOUBLE_EQ(1.0, real_val); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 130 | |
| 131 | // Fractional parts must have a digit before and after the decimal point. |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 132 | root.reset(JSONReader().JsonToValue("1.", false, false)); |
| 133 | ASSERT_FALSE(root.get()); |
| 134 | root.reset(JSONReader().JsonToValue(".1", false, false)); |
| 135 | ASSERT_FALSE(root.get()); |
| 136 | root.reset(JSONReader().JsonToValue("1.e10", false, false)); |
| 137 | ASSERT_FALSE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 138 | |
| 139 | // Exponent must have a digit following the 'e'. |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 140 | root.reset(JSONReader().JsonToValue("1e", false, false)); |
| 141 | ASSERT_FALSE(root.get()); |
| 142 | root.reset(JSONReader().JsonToValue("1E", false, false)); |
| 143 | ASSERT_FALSE(root.get()); |
| 144 | root.reset(JSONReader().JsonToValue("1e1.", false, false)); |
| 145 | ASSERT_FALSE(root.get()); |
| 146 | root.reset(JSONReader().JsonToValue("1e1.0", false, false)); |
| 147 | ASSERT_FALSE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 148 | |
| 149 | // INF/-INF/NaN are not valid |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 150 | root.reset(JSONReader().JsonToValue("1e1000", false, false)); |
| 151 | ASSERT_FALSE(root.get()); |
| 152 | root.reset(JSONReader().JsonToValue("-1e1000", false, false)); |
| 153 | ASSERT_FALSE(root.get()); |
| 154 | root.reset(JSONReader().JsonToValue("NaN", false, false)); |
| 155 | ASSERT_FALSE(root.get()); |
| 156 | root.reset(JSONReader().JsonToValue("nan", false, false)); |
| 157 | ASSERT_FALSE(root.get()); |
| 158 | root.reset(JSONReader().JsonToValue("inf", false, false)); |
| 159 | ASSERT_FALSE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 160 | |
| 161 | // Invalid number formats |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 162 | root.reset(JSONReader().JsonToValue("4.3.1", false, false)); |
| 163 | ASSERT_FALSE(root.get()); |
| 164 | root.reset(JSONReader().JsonToValue("4e3.1", false, false)); |
| 165 | ASSERT_FALSE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 166 | |
| 167 | // Test string parser |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 168 | root.reset(JSONReader().JsonToValue("\"hello world\"", false, false)); |
| 169 | ASSERT_TRUE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 170 | ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); |
| 171 | std::wstring str_val; |
| 172 | ASSERT_TRUE(root->GetAsString(&str_val)); |
| 173 | ASSERT_EQ(L"hello world", str_val); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 174 | |
| 175 | // Empty string |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 176 | root.reset(JSONReader().JsonToValue("\"\"", false, false)); |
| 177 | ASSERT_TRUE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 178 | ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); |
| 179 | str_val.clear(); |
| 180 | ASSERT_TRUE(root->GetAsString(&str_val)); |
| 181 | ASSERT_EQ(L"", str_val); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 182 | |
| 183 | // Test basic string escapes |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 184 | root.reset(JSONReader().JsonToValue("\" \\\"\\\\\\/\\b\\f\\n\\r\\t\\v\"", |
| 185 | false, false)); |
| 186 | ASSERT_TRUE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 187 | ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); |
| 188 | str_val.clear(); |
| 189 | ASSERT_TRUE(root->GetAsString(&str_val)); |
[email protected] | d46d6f3b | 2008-10-01 17:26:06 | [diff] [blame] | 190 | ASSERT_EQ(L" \"\\/\b\f\n\r\t\v", str_val); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 191 | |
| 192 | // Test hex and unicode escapes including the null character. |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 193 | root.reset(JSONReader().JsonToValue("\"\\x41\\x00\\u1234\"", false, |
[email protected] | e724599 | 2008-07-29 00:01:31 | [diff] [blame] | 194 | false)); |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 195 | ASSERT_TRUE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 196 | ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); |
| 197 | str_val.clear(); |
| 198 | ASSERT_TRUE(root->GetAsString(&str_val)); |
| 199 | ASSERT_EQ(std::wstring(L"A\0\x1234", 3), str_val); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 200 | |
| 201 | // Test invalid strings |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 202 | root.reset(JSONReader().JsonToValue("\"no closing quote", false, false)); |
| 203 | ASSERT_FALSE(root.get()); |
| 204 | root.reset(JSONReader().JsonToValue("\"\\z invalid escape char\"", false, |
| 205 | false)); |
| 206 | ASSERT_FALSE(root.get()); |
| 207 | root.reset(JSONReader().JsonToValue("\"\\xAQ invalid hex code\"", false, |
| 208 | false)); |
| 209 | ASSERT_FALSE(root.get()); |
| 210 | root.reset(JSONReader().JsonToValue("not enough hex chars\\x1\"", false, |
| 211 | false)); |
| 212 | ASSERT_FALSE(root.get()); |
| 213 | root.reset(JSONReader().JsonToValue("\"not enough escape chars\\u123\"", |
| 214 | false, false)); |
| 215 | ASSERT_FALSE(root.get()); |
| 216 | root.reset(JSONReader().JsonToValue("\"extra backslash at end of input\\\"", |
| 217 | false, false)); |
| 218 | ASSERT_FALSE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 219 | |
| 220 | // Basic array |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 221 | root.reset(JSONReader::Read("[true, false, null]", false)); |
| 222 | ASSERT_TRUE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 223 | ASSERT_TRUE(root->IsType(Value::TYPE_LIST)); |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 224 | ListValue* list = static_cast<ListValue*>(root.get()); |
[email protected] | cb2f363 | 2008-08-14 20:27:29 | [diff] [blame] | 225 | ASSERT_EQ(3U, list->GetSize()); |
[email protected] | e724599 | 2008-07-29 00:01:31 | [diff] [blame] | 226 | |
| 227 | // Test with trailing comma. Should be parsed the same as above. |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 228 | scoped_ptr<Value> root2; |
| 229 | root2.reset(JSONReader::Read("[true, false, null, ]", true)); |
| 230 | EXPECT_TRUE(root->Equals(root2.get())); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 231 | |
| 232 | // Empty array |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 233 | root.reset(JSONReader::Read("[]", false)); |
| 234 | ASSERT_TRUE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 235 | ASSERT_TRUE(root->IsType(Value::TYPE_LIST)); |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 236 | list = static_cast<ListValue*>(root.get()); |
[email protected] | cb2f363 | 2008-08-14 20:27:29 | [diff] [blame] | 237 | ASSERT_EQ(0U, list->GetSize()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 238 | |
| 239 | // Nested arrays |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 240 | root.reset(JSONReader::Read("[[true], [], [false, [], [null]], null]", |
| 241 | false)); |
| 242 | ASSERT_TRUE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 243 | ASSERT_TRUE(root->IsType(Value::TYPE_LIST)); |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 244 | list = static_cast<ListValue*>(root.get()); |
[email protected] | cb2f363 | 2008-08-14 20:27:29 | [diff] [blame] | 245 | ASSERT_EQ(4U, list->GetSize()); |
[email protected] | e724599 | 2008-07-29 00:01:31 | [diff] [blame] | 246 | |
| 247 | // Lots of trailing commas. |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 248 | root2.reset(JSONReader::Read("[[true], [], [false, [], [null, ] , ], null,]", |
| 249 | true)); |
| 250 | EXPECT_TRUE(root->Equals(root2.get())); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 251 | |
| 252 | // Invalid, missing close brace. |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 253 | root.reset(JSONReader::Read("[[true], [], [false, [], [null]], null", false)); |
| 254 | ASSERT_FALSE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 255 | |
| 256 | // Invalid, too many commas |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 257 | root.reset(JSONReader::Read("[true,, null]", false)); |
| 258 | ASSERT_FALSE(root.get()); |
| 259 | root.reset(JSONReader::Read("[true,, null]", true)); |
| 260 | ASSERT_FALSE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 261 | |
| 262 | // Invalid, no commas |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 263 | root.reset(JSONReader::Read("[true null]", false)); |
| 264 | ASSERT_FALSE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 265 | |
| 266 | // Invalid, trailing comma |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 267 | root.reset(JSONReader::Read("[true,]", false)); |
| 268 | ASSERT_FALSE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 269 | |
[email protected] | e724599 | 2008-07-29 00:01:31 | [diff] [blame] | 270 | // Valid if we set |allow_trailing_comma| to true. |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 271 | root.reset(JSONReader::Read("[true,]", true)); |
| 272 | ASSERT_TRUE(root.get()); |
[email protected] | e724599 | 2008-07-29 00:01:31 | [diff] [blame] | 273 | ASSERT_TRUE(root->IsType(Value::TYPE_LIST)); |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 274 | list = static_cast<ListValue*>(root.get()); |
[email protected] | cb2f363 | 2008-08-14 20:27:29 | [diff] [blame] | 275 | EXPECT_EQ(1U, list->GetSize()); |
[email protected] | e724599 | 2008-07-29 00:01:31 | [diff] [blame] | 276 | Value* tmp_value = NULL; |
| 277 | ASSERT_TRUE(list->Get(0, &tmp_value)); |
| 278 | EXPECT_TRUE(tmp_value->IsType(Value::TYPE_BOOLEAN)); |
| 279 | bool bool_value = false; |
| 280 | ASSERT_TRUE(tmp_value->GetAsBoolean(&bool_value)); |
| 281 | EXPECT_TRUE(bool_value); |
[email protected] | e724599 | 2008-07-29 00:01:31 | [diff] [blame] | 282 | |
| 283 | // Don't allow empty elements, even if |allow_trailing_comma| is |
| 284 | // true. |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 285 | root.reset(JSONReader::Read("[,]", true)); |
| 286 | EXPECT_FALSE(root.get()); |
| 287 | root.reset(JSONReader::Read("[true,,]", true)); |
| 288 | EXPECT_FALSE(root.get()); |
| 289 | root.reset(JSONReader::Read("[,true,]", true)); |
| 290 | EXPECT_FALSE(root.get()); |
| 291 | root.reset(JSONReader::Read("[true,,false]", true)); |
| 292 | EXPECT_FALSE(root.get()); |
[email protected] | e724599 | 2008-07-29 00:01:31 | [diff] [blame] | 293 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 294 | // Test objects |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 295 | root.reset(JSONReader::Read("{}", false)); |
| 296 | ASSERT_TRUE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 297 | ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 298 | |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 299 | root.reset(JSONReader::Read( |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 300 | "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\" }", false)); |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 301 | ASSERT_TRUE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 302 | ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 303 | DictionaryValue* dict_val = static_cast<DictionaryValue*>(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 304 | real_val = 0.0; |
[email protected] | 9001854 | 2010-08-14 01:49:14 | [diff] [blame^] | 305 | ASSERT_TRUE(dict_val->GetReal("number", &real_val)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 306 | ASSERT_DOUBLE_EQ(9.87654321, real_val); |
| 307 | Value* null_val = NULL; |
[email protected] | 9001854 | 2010-08-14 01:49:14 | [diff] [blame^] | 308 | ASSERT_TRUE(dict_val->Get("null", &null_val)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 309 | ASSERT_TRUE(null_val->IsType(Value::TYPE_NULL)); |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 310 | str_val.clear(); |
| 311 | ASSERT_TRUE(dict_val->GetString(L"S", &str_val)); |
| 312 | ASSERT_EQ(L"str", str_val); |
[email protected] | e724599 | 2008-07-29 00:01:31 | [diff] [blame] | 313 | |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 314 | root2.reset(JSONReader::Read( |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 315 | "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\", }", true)); |
[email protected] | b81637c3 | 2009-06-26 21:17:24 | [diff] [blame] | 316 | ASSERT_TRUE(root2.get()); |
| 317 | EXPECT_TRUE(root->Equals(root2.get())); |
| 318 | |
| 319 | // Test newline equivalence. |
| 320 | root2.reset(JSONReader::Read( |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 321 | "{\n" |
| 322 | " \"number\":9.87654321,\n" |
| 323 | " \"null\":null,\n" |
| 324 | " \"\\x53\":\"str\",\n" |
| 325 | "}\n", true)); |
[email protected] | b81637c3 | 2009-06-26 21:17:24 | [diff] [blame] | 326 | ASSERT_TRUE(root2.get()); |
| 327 | EXPECT_TRUE(root->Equals(root2.get())); |
| 328 | |
| 329 | root2.reset(JSONReader::Read( |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 330 | "{\r\n" |
| 331 | " \"number\":9.87654321,\r\n" |
| 332 | " \"null\":null,\r\n" |
| 333 | " \"\\x53\":\"str\",\r\n" |
| 334 | "}\r\n", true)); |
[email protected] | b81637c3 | 2009-06-26 21:17:24 | [diff] [blame] | 335 | ASSERT_TRUE(root2.get()); |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 336 | EXPECT_TRUE(root->Equals(root2.get())); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 337 | |
| 338 | // Test nesting |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 339 | root.reset(JSONReader::Read( |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 340 | "{\"inner\":{\"array\":[true]},\"false\":false,\"d\":{}}", false)); |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 341 | ASSERT_TRUE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 342 | ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 343 | dict_val = static_cast<DictionaryValue*>(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 344 | DictionaryValue* inner_dict = NULL; |
[email protected] | 9001854 | 2010-08-14 01:49:14 | [diff] [blame^] | 345 | ASSERT_TRUE(dict_val->GetDictionary("inner", &inner_dict)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 346 | ListValue* inner_array = NULL; |
[email protected] | 9001854 | 2010-08-14 01:49:14 | [diff] [blame^] | 347 | ASSERT_TRUE(inner_dict->GetList("array", &inner_array)); |
[email protected] | cb2f363 | 2008-08-14 20:27:29 | [diff] [blame] | 348 | ASSERT_EQ(1U, inner_array->GetSize()); |
[email protected] | e724599 | 2008-07-29 00:01:31 | [diff] [blame] | 349 | bool_value = true; |
[email protected] | 9001854 | 2010-08-14 01:49:14 | [diff] [blame^] | 350 | ASSERT_TRUE(dict_val->GetBoolean("false", &bool_value)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 351 | ASSERT_FALSE(bool_value); |
| 352 | inner_dict = NULL; |
[email protected] | 9001854 | 2010-08-14 01:49:14 | [diff] [blame^] | 353 | ASSERT_TRUE(dict_val->GetDictionary("d", &inner_dict)); |
[email protected] | e724599 | 2008-07-29 00:01:31 | [diff] [blame] | 354 | |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 355 | root2.reset(JSONReader::Read( |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 356 | "{\"inner\": {\"array\":[true] , },\"false\":false,\"d\":{},}", true)); |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 357 | EXPECT_TRUE(root->Equals(root2.get())); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 358 | |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 359 | // Test keys with periods |
| 360 | root.reset(JSONReader::Read( |
| 361 | "{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}", false)); |
| 362 | ASSERT_TRUE(root.get()); |
| 363 | ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); |
| 364 | dict_val = static_cast<DictionaryValue*>(root.get()); |
| 365 | int integer_value = 0; |
[email protected] | 9001854 | 2010-08-14 01:49:14 | [diff] [blame^] | 366 | EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion("a.b", &integer_value)); |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 367 | EXPECT_EQ(3, integer_value); |
[email protected] | 9001854 | 2010-08-14 01:49:14 | [diff] [blame^] | 368 | EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion("c", &integer_value)); |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 369 | EXPECT_EQ(2, integer_value); |
| 370 | inner_dict = NULL; |
[email protected] | 9001854 | 2010-08-14 01:49:14 | [diff] [blame^] | 371 | ASSERT_TRUE(dict_val->GetDictionaryWithoutPathExpansion("d.e.f", |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 372 | &inner_dict)); |
| 373 | ASSERT_EQ(1U, inner_dict->size()); |
[email protected] | 9001854 | 2010-08-14 01:49:14 | [diff] [blame^] | 374 | EXPECT_TRUE(inner_dict->GetIntegerWithoutPathExpansion("g.h.i.j", |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 375 | &integer_value)); |
| 376 | EXPECT_EQ(1, integer_value); |
| 377 | |
| 378 | root.reset(JSONReader::Read("{\"a\":{\"b\":2},\"a.b\":1}", false)); |
| 379 | ASSERT_TRUE(root.get()); |
| 380 | ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); |
| 381 | dict_val = static_cast<DictionaryValue*>(root.get()); |
[email protected] | 9001854 | 2010-08-14 01:49:14 | [diff] [blame^] | 382 | EXPECT_TRUE(dict_val->GetInteger("a.b", &integer_value)); |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 383 | EXPECT_EQ(2, integer_value); |
[email protected] | 9001854 | 2010-08-14 01:49:14 | [diff] [blame^] | 384 | EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion("a.b", &integer_value)); |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 385 | EXPECT_EQ(1, integer_value); |
| 386 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 387 | // Invalid, no closing brace |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 388 | root.reset(JSONReader::Read("{\"a\": true", false)); |
| 389 | ASSERT_FALSE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 390 | |
| 391 | // Invalid, keys must be quoted |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 392 | root.reset(JSONReader::Read("{foo:true}", false)); |
| 393 | ASSERT_FALSE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 394 | |
| 395 | // Invalid, trailing comma |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 396 | root.reset(JSONReader::Read("{\"a\":true,}", false)); |
| 397 | ASSERT_FALSE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 398 | |
| 399 | // Invalid, too many commas |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 400 | root.reset(JSONReader::Read("{\"a\":true,,\"b\":false}", false)); |
| 401 | ASSERT_FALSE(root.get()); |
| 402 | root.reset(JSONReader::Read("{\"a\":true,,\"b\":false}", true)); |
| 403 | ASSERT_FALSE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 404 | |
| 405 | // Invalid, no separator |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 406 | root.reset(JSONReader::Read("{\"a\" \"b\"}", false)); |
| 407 | ASSERT_FALSE(root.get()); |
[email protected] | e724599 | 2008-07-29 00:01:31 | [diff] [blame] | 408 | |
| 409 | // Invalid, lone comma. |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 410 | root.reset(JSONReader::Read("{,}", false)); |
| 411 | ASSERT_FALSE(root.get()); |
| 412 | root.reset(JSONReader::Read("{,}", true)); |
| 413 | ASSERT_FALSE(root.get()); |
| 414 | root.reset(JSONReader::Read("{\"a\":true,,}", true)); |
| 415 | ASSERT_FALSE(root.get()); |
| 416 | root.reset(JSONReader::Read("{,\"a\":true}", true)); |
| 417 | ASSERT_FALSE(root.get()); |
| 418 | root.reset(JSONReader::Read("{\"a\":true,,\"b\":false}", true)); |
| 419 | ASSERT_FALSE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 420 | |
| 421 | // Test stack overflow |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 422 | std::string evil(1000000, '['); |
| 423 | evil.append(std::string(1000000, ']')); |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 424 | root.reset(JSONReader::Read(evil, false)); |
| 425 | ASSERT_FALSE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 426 | |
| 427 | // A few thousand adjacent lists is fine. |
| 428 | std::string not_evil("["); |
| 429 | not_evil.reserve(15010); |
| 430 | for (int i = 0; i < 5000; ++i) { |
| 431 | not_evil.append("[],"); |
| 432 | } |
| 433 | not_evil.append("[]]"); |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 434 | root.reset(JSONReader::Read(not_evil, false)); |
| 435 | ASSERT_TRUE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 436 | ASSERT_TRUE(root->IsType(Value::TYPE_LIST)); |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 437 | list = static_cast<ListValue*>(root.get()); |
[email protected] | cb2f363 | 2008-08-14 20:27:29 | [diff] [blame] | 438 | ASSERT_EQ(5001U, list->GetSize()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 439 | |
| 440 | // Test utf8 encoded input |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 441 | root.reset(JSONReader().JsonToValue("\"\xe7\xbd\x91\xe9\xa1\xb5\"", |
[email protected] | e724599 | 2008-07-29 00:01:31 | [diff] [blame] | 442 | false, false)); |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 443 | ASSERT_TRUE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 444 | ASSERT_TRUE(root->IsType(Value::TYPE_STRING)); |
| 445 | str_val.clear(); |
| 446 | ASSERT_TRUE(root->GetAsString(&str_val)); |
| 447 | ASSERT_EQ(L"\x7f51\x9875", str_val); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 448 | |
[email protected] | c9ec454 | 2008-09-25 21:42:00 | [diff] [blame] | 449 | // Test invalid utf8 encoded input |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 450 | root.reset(JSONReader().JsonToValue("\"345\xb0\xa1\xb0\xa2\"", |
| 451 | false, false)); |
| 452 | ASSERT_FALSE(root.get()); |
| 453 | root.reset(JSONReader().JsonToValue("\"123\xc0\x81\"", |
| 454 | false, false)); |
| 455 | ASSERT_FALSE(root.get()); |
[email protected] | c9ec454 | 2008-09-25 21:42:00 | [diff] [blame] | 456 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 457 | // Test invalid root objects. |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 458 | root.reset(JSONReader::Read("null", false)); |
| 459 | ASSERT_FALSE(root.get()); |
| 460 | root.reset(JSONReader::Read("true", false)); |
| 461 | ASSERT_FALSE(root.get()); |
| 462 | root.reset(JSONReader::Read("10", false)); |
| 463 | ASSERT_FALSE(root.get()); |
| 464 | root.reset(JSONReader::Read("\"root\"", false)); |
| 465 | ASSERT_FALSE(root.get()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 466 | } |
[email protected] | 88e72845 | 2008-12-05 22:14:46 | [diff] [blame] | 467 | |
| 468 | TEST(JSONReaderTest, ErrorMessages) { |
| 469 | // Error strings should not be modified in case of success. |
| 470 | std::string error_message; |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 471 | int error_code = 0; |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 472 | scoped_ptr<Value> root; |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 473 | root.reset(JSONReader::ReadAndReturnError("[42]", false, |
| 474 | &error_code, &error_message)); |
[email protected] | 6160649ec | 2008-12-24 02:07:48 | [diff] [blame] | 475 | EXPECT_TRUE(error_message.empty()); |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 476 | EXPECT_EQ(0, error_code); |
[email protected] | 88e72845 | 2008-12-05 22:14:46 | [diff] [blame] | 477 | |
| 478 | // Test line and column counting |
| 479 | const char* big_json = "[\n0,\n1,\n2,\n3,4,5,6 7,\n8,\n9\n]"; |
| 480 | // error here --------------------------------^ |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 481 | root.reset(JSONReader::ReadAndReturnError(big_json, false, |
| 482 | &error_code, &error_message)); |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 483 | EXPECT_FALSE(root.get()); |
[email protected] | 6160649ec | 2008-12-24 02:07:48 | [diff] [blame] | 484 | EXPECT_EQ(JSONReader::FormatErrorMessage(5, 9, JSONReader::kSyntaxError), |
[email protected] | 88e72845 | 2008-12-05 22:14:46 | [diff] [blame] | 485 | error_message); |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 486 | EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code); |
[email protected] | 88e72845 | 2008-12-05 22:14:46 | [diff] [blame] | 487 | |
| 488 | // Test each of the error conditions |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 489 | root.reset(JSONReader::ReadAndReturnError("{},{}", false, |
| 490 | &error_code, &error_message)); |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 491 | EXPECT_FALSE(root.get()); |
[email protected] | 6160649ec | 2008-12-24 02:07:48 | [diff] [blame] | 492 | EXPECT_EQ(JSONReader::FormatErrorMessage(1, 3, |
[email protected] | 88e72845 | 2008-12-05 22:14:46 | [diff] [blame] | 493 | JSONReader::kUnexpectedDataAfterRoot), error_message); |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 494 | EXPECT_EQ(JSONReader::JSON_UNEXPECTED_DATA_AFTER_ROOT, error_code); |
[email protected] | 88e72845 | 2008-12-05 22:14:46 | [diff] [blame] | 495 | |
| 496 | std::string nested_json; |
| 497 | for (int i = 0; i < 101; ++i) { |
| 498 | nested_json.insert(nested_json.begin(), '['); |
| 499 | nested_json.append(1, ']'); |
| 500 | } |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 501 | root.reset(JSONReader::ReadAndReturnError(nested_json, false, |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 502 | &error_code, &error_message)); |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 503 | EXPECT_FALSE(root.get()); |
[email protected] | 6160649ec | 2008-12-24 02:07:48 | [diff] [blame] | 504 | EXPECT_EQ(JSONReader::FormatErrorMessage(1, 101, JSONReader::kTooMuchNesting), |
[email protected] | 88e72845 | 2008-12-05 22:14:46 | [diff] [blame] | 505 | error_message); |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 506 | EXPECT_EQ(JSONReader::JSON_TOO_MUCH_NESTING, error_code); |
[email protected] | 88e72845 | 2008-12-05 22:14:46 | [diff] [blame] | 507 | |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 508 | root.reset(JSONReader::ReadAndReturnError("42", false, |
| 509 | &error_code, &error_message)); |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 510 | EXPECT_FALSE(root.get()); |
[email protected] | 6160649ec | 2008-12-24 02:07:48 | [diff] [blame] | 511 | EXPECT_EQ(JSONReader::FormatErrorMessage(1, 1, |
[email protected] | 88e72845 | 2008-12-05 22:14:46 | [diff] [blame] | 512 | JSONReader::kBadRootElementType), error_message); |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 513 | EXPECT_EQ(JSONReader::JSON_BAD_ROOT_ELEMENT_TYPE, error_code); |
[email protected] | 88e72845 | 2008-12-05 22:14:46 | [diff] [blame] | 514 | |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 515 | root.reset(JSONReader::ReadAndReturnError("[1,]", false, |
| 516 | &error_code, &error_message)); |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 517 | EXPECT_FALSE(root.get()); |
[email protected] | 6160649ec | 2008-12-24 02:07:48 | [diff] [blame] | 518 | EXPECT_EQ(JSONReader::FormatErrorMessage(1, 4, JSONReader::kTrailingComma), |
[email protected] | 88e72845 | 2008-12-05 22:14:46 | [diff] [blame] | 519 | error_message); |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 520 | EXPECT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code); |
[email protected] | 88e72845 | 2008-12-05 22:14:46 | [diff] [blame] | 521 | |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 522 | root.reset(JSONReader::ReadAndReturnError("{foo:\"bar\"}", false, |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 523 | &error_code, &error_message)); |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 524 | EXPECT_FALSE(root.get()); |
[email protected] | 6160649ec | 2008-12-24 02:07:48 | [diff] [blame] | 525 | EXPECT_EQ(JSONReader::FormatErrorMessage(1, 2, |
[email protected] | 88e72845 | 2008-12-05 22:14:46 | [diff] [blame] | 526 | JSONReader::kUnquotedDictionaryKey), error_message); |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 527 | EXPECT_EQ(JSONReader::JSON_UNQUOTED_DICTIONARY_KEY, error_code); |
[email protected] | 88e72845 | 2008-12-05 22:14:46 | [diff] [blame] | 528 | |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 529 | root.reset(JSONReader::ReadAndReturnError("{\"foo\":\"bar\",}", false, |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 530 | &error_code, &error_message)); |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 531 | EXPECT_FALSE(root.get()); |
[email protected] | 6160649ec | 2008-12-24 02:07:48 | [diff] [blame] | 532 | EXPECT_EQ(JSONReader::FormatErrorMessage(1, 14, JSONReader::kTrailingComma), |
[email protected] | 88e72845 | 2008-12-05 22:14:46 | [diff] [blame] | 533 | error_message); |
| 534 | |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 535 | root.reset(JSONReader::ReadAndReturnError("[nu]", false, |
| 536 | &error_code, &error_message)); |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 537 | EXPECT_FALSE(root.get()); |
[email protected] | 6160649ec | 2008-12-24 02:07:48 | [diff] [blame] | 538 | EXPECT_EQ(JSONReader::FormatErrorMessage(1, 2, JSONReader::kSyntaxError), |
[email protected] | 88e72845 | 2008-12-05 22:14:46 | [diff] [blame] | 539 | error_message); |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 540 | EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code); |
[email protected] | 88e72845 | 2008-12-05 22:14:46 | [diff] [blame] | 541 | |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 542 | root.reset(JSONReader::ReadAndReturnError("[\"xxx\\xq\"]", false, |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 543 | &error_code, &error_message)); |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 544 | EXPECT_FALSE(root.get()); |
[email protected] | 6160649ec | 2008-12-24 02:07:48 | [diff] [blame] | 545 | EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape), |
[email protected] | 88e72845 | 2008-12-05 22:14:46 | [diff] [blame] | 546 | error_message); |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 547 | EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code); |
[email protected] | 88e72845 | 2008-12-05 22:14:46 | [diff] [blame] | 548 | |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 549 | root.reset(JSONReader::ReadAndReturnError("[\"xxx\\uq\"]", false, |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 550 | &error_code, &error_message)); |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 551 | EXPECT_FALSE(root.get()); |
[email protected] | 6160649ec | 2008-12-24 02:07:48 | [diff] [blame] | 552 | EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape), |
[email protected] | 88e72845 | 2008-12-05 22:14:46 | [diff] [blame] | 553 | error_message); |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 554 | EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code); |
[email protected] | 88e72845 | 2008-12-05 22:14:46 | [diff] [blame] | 555 | |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 556 | root.reset(JSONReader::ReadAndReturnError("[\"xxx\\q\"]", false, |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 557 | &error_code, &error_message)); |
[email protected] | b4cebf8 | 2008-12-29 19:59:08 | [diff] [blame] | 558 | EXPECT_FALSE(root.get()); |
[email protected] | 6160649ec | 2008-12-24 02:07:48 | [diff] [blame] | 559 | EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape), |
[email protected] | 88e72845 | 2008-12-05 22:14:46 | [diff] [blame] | 560 | error_message); |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 561 | EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code); |
[email protected] | 88e72845 | 2008-12-05 22:14:46 | [diff] [blame] | 562 | } |
[email protected] | 93d49d7 | 2009-10-23 20:00:20 | [diff] [blame] | 563 | |
| 564 | } // namespace base |