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