Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(491)

Side by Side Diff: third_party/WebKit/Source/platform/JSONParserTest.cpp

Issue 1734693003: Revert of DevTools: simplify JSONValues API, prepare to the OwnPtr migration. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2016 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 "platform/JSONParser.h" 5 #include "platform/JSONParser.h"
6 6
7 #include "platform/JSONValues.h" 7 #include "platform/JSONValues.h"
8 #include "testing/gtest/include/gtest/gtest.h" 8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "wtf/text/StringBuilder.h" 9 #include "wtf/text/StringBuilder.h"
10 10
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 EXPECT_EQ(40, intVal); 50 EXPECT_EQ(40, intVal);
51 root = parseJSON("true // comment"); 51 root = parseJSON("true // comment");
52 ASSERT_TRUE(root.get()); 52 ASSERT_TRUE(root.get());
53 EXPECT_EQ(JSONValue::TypeBoolean, root->type()); 53 EXPECT_EQ(JSONValue::TypeBoolean, root->type());
54 root = parseJSON("/* comment */\"sample string\""); 54 root = parseJSON("/* comment */\"sample string\"");
55 ASSERT_TRUE(root.get()); 55 ASSERT_TRUE(root.get());
56 EXPECT_TRUE(root->asString(&strVal)); 56 EXPECT_TRUE(root->asString(&strVal));
57 EXPECT_EQ("sample string", strVal); 57 EXPECT_EQ("sample string", strVal);
58 root = parseJSON("[1, /* comment, 2 ] */ \n 3]"); 58 root = parseJSON("[1, /* comment, 2 ] */ \n 3]");
59 ASSERT_TRUE(root.get()); 59 ASSERT_TRUE(root.get());
60 RefPtr<JSONArray> list = JSONArray::cast(root); 60 RefPtr<JSONArray> list;
61 ASSERT_TRUE(list); 61 ASSERT_TRUE(root->asArray(&list));
62 EXPECT_EQ(2u, list->length()); 62 EXPECT_EQ(2u, list->length());
63 tmpValue = list->get(0); 63 tmpValue = list->get(0);
64 ASSERT_TRUE(tmpValue.get()); 64 ASSERT_TRUE(tmpValue.get());
65 EXPECT_TRUE(tmpValue->asNumber(&intVal)); 65 EXPECT_TRUE(tmpValue->asNumber(&intVal));
66 EXPECT_EQ(1, intVal); 66 EXPECT_EQ(1, intVal);
67 tmpValue = list->get(1); 67 tmpValue = list->get(1);
68 ASSERT_TRUE(tmpValue.get()); 68 ASSERT_TRUE(tmpValue.get());
69 EXPECT_TRUE(tmpValue->asNumber(&intVal)); 69 EXPECT_TRUE(tmpValue->asNumber(&intVal));
70 EXPECT_EQ(3, intVal); 70 EXPECT_EQ(3, intVal);
71 root = parseJSON("[1, /*a*/2, 3]"); 71 root = parseJSON("[1, /*a*/2, 3]");
72 ASSERT_TRUE(root.get()); 72 ASSERT_TRUE(root.get());
73 list = JSONArray::cast(root); 73 ASSERT_TRUE(root->asArray(&list));
74 ASSERT_TRUE(list);
75 EXPECT_EQ(3u, list->length()); 74 EXPECT_EQ(3u, list->length());
76 root = parseJSON("/* comment **/42"); 75 root = parseJSON("/* comment **/42");
77 ASSERT_TRUE(root.get()); 76 ASSERT_TRUE(root.get());
78 EXPECT_EQ(JSONValue::TypeNumber, root->type()); 77 EXPECT_EQ(JSONValue::TypeNumber, root->type());
79 EXPECT_TRUE(root->asNumber(&intVal)); 78 EXPECT_TRUE(root->asNumber(&intVal));
80 EXPECT_EQ(42, intVal); 79 EXPECT_EQ(42, intVal);
81 root = parseJSON( 80 root = parseJSON(
82 "/* comment **/\n" 81 "/* comment **/\n"
83 "// */ 43\n" 82 "// */ 43\n"
84 "44"); 83 "44");
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 EXPECT_FALSE(root.get()); 245 EXPECT_FALSE(root.get());
247 root = parseJSON("\"not enough escape chars\\u123\""); 246 root = parseJSON("\"not enough escape chars\\u123\"");
248 EXPECT_FALSE(root.get()); 247 EXPECT_FALSE(root.get());
249 root = parseJSON("\"extra backslash at end of input\\\""); 248 root = parseJSON("\"extra backslash at end of input\\\"");
250 EXPECT_FALSE(root.get()); 249 EXPECT_FALSE(root.get());
251 250
252 // Basic array 251 // Basic array
253 root = parseJSON("[true, false, null]"); 252 root = parseJSON("[true, false, null]");
254 ASSERT_TRUE(root.get()); 253 ASSERT_TRUE(root.get());
255 EXPECT_EQ(JSONValue::TypeArray, root->type()); 254 EXPECT_EQ(JSONValue::TypeArray, root->type());
256 list = JSONArray::cast(root); 255 ASSERT_TRUE(root->asArray(&list));
257 ASSERT_TRUE(list);
258 EXPECT_EQ(3U, list->length()); 256 EXPECT_EQ(3U, list->length());
259 257
260 // Empty array 258 // Empty array
261 root = parseJSON("[]"); 259 root = parseJSON("[]");
262 ASSERT_TRUE(root.get()); 260 ASSERT_TRUE(root.get());
263 EXPECT_EQ(JSONValue::TypeArray, root->type()); 261 EXPECT_EQ(JSONValue::TypeArray, root->type());
264 list = JSONArray::cast(root); 262 ASSERT_TRUE(root->asArray(&list));
265 ASSERT_TRUE(list);
266 EXPECT_EQ(0U, list->length()); 263 EXPECT_EQ(0U, list->length());
267 264
268 // Nested arrays 265 // Nested arrays
269 root = parseJSON("[[true], [], [false, [], [null]], null]"); 266 root = parseJSON("[[true], [], [false, [], [null]], null]");
270 ASSERT_TRUE(root.get()); 267 ASSERT_TRUE(root.get());
271 EXPECT_EQ(JSONValue::TypeArray, root->type()); 268 EXPECT_EQ(JSONValue::TypeArray, root->type());
272 list = JSONArray::cast(root); 269 ASSERT_TRUE(root->asArray(&list));
273 ASSERT_TRUE(list);
274 EXPECT_EQ(4U, list->length()); 270 EXPECT_EQ(4U, list->length());
275 271
276 // Invalid, missing close brace. 272 // Invalid, missing close brace.
277 root = parseJSON("[[true], [], [false, [], [null]], null"); 273 root = parseJSON("[[true], [], [false, [], [null]], null");
278 EXPECT_FALSE(root.get()); 274 EXPECT_FALSE(root.get());
279 275
280 // Invalid, too many commas 276 // Invalid, too many commas
281 root = parseJSON("[true,, null]"); 277 root = parseJSON("[true,, null]");
282 EXPECT_FALSE(root.get()); 278 EXPECT_FALSE(root.get());
283 279
284 // Invalid, no commas 280 // Invalid, no commas
285 root = parseJSON("[true null]"); 281 root = parseJSON("[true null]");
286 EXPECT_FALSE(root.get()); 282 EXPECT_FALSE(root.get());
287 283
288 // Invalid, trailing comma 284 // Invalid, trailing comma
289 root = parseJSON("[true,]"); 285 root = parseJSON("[true,]");
290 EXPECT_FALSE(root.get()); 286 EXPECT_FALSE(root.get());
291 287
292 root = parseJSON("[true]"); 288 root = parseJSON("[true]");
293 ASSERT_TRUE(root.get()); 289 ASSERT_TRUE(root.get());
294 EXPECT_EQ(JSONValue::TypeArray, root->type()); 290 EXPECT_EQ(JSONValue::TypeArray, root->type());
295 list = JSONArray::cast(root); 291 ASSERT_TRUE(root->asArray(&list));
296 ASSERT_TRUE(list);
297 EXPECT_EQ(1U, list->length()); 292 EXPECT_EQ(1U, list->length());
298 tmpValue = list->get(0); 293 tmpValue = list->get(0);
299 ASSERT_TRUE(tmpValue.get()); 294 ASSERT_TRUE(tmpValue.get());
300 EXPECT_EQ(JSONValue::TypeBoolean, tmpValue->type()); 295 EXPECT_EQ(JSONValue::TypeBoolean, tmpValue->type());
301 bool boolValue = false; 296 bool boolValue = false;
302 EXPECT_TRUE(tmpValue->asBoolean(&boolValue)); 297 EXPECT_TRUE(tmpValue->asBoolean(&boolValue));
303 EXPECT_TRUE(boolValue); 298 EXPECT_TRUE(boolValue);
304 299
305 // Don't allow empty elements. 300 // Don't allow empty elements.
306 root = parseJSON("[,]"); 301 root = parseJSON("[,]");
307 EXPECT_FALSE(root.get()); 302 EXPECT_FALSE(root.get());
308 root = parseJSON("[true,,]"); 303 root = parseJSON("[true,,]");
309 EXPECT_FALSE(root.get()); 304 EXPECT_FALSE(root.get());
310 root = parseJSON("[,true,]"); 305 root = parseJSON("[,true,]");
311 EXPECT_FALSE(root.get()); 306 EXPECT_FALSE(root.get());
312 root = parseJSON("[true,,false]"); 307 root = parseJSON("[true,,false]");
313 EXPECT_FALSE(root.get()); 308 EXPECT_FALSE(root.get());
314 309
315 // Test objects 310 // Test objects
316 root = parseJSON("{}"); 311 root = parseJSON("{}");
317 ASSERT_TRUE(root.get()); 312 ASSERT_TRUE(root.get());
318 EXPECT_EQ(JSONValue::TypeObject, root->type()); 313 EXPECT_EQ(JSONValue::TypeObject, root->type());
319 314
320 root = parseJSON("{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\ " }"); 315 root = parseJSON("{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\ " }");
321 ASSERT_TRUE(root.get()); 316 ASSERT_TRUE(root.get());
322 EXPECT_EQ(JSONValue::TypeObject, root->type()); 317 EXPECT_EQ(JSONValue::TypeObject, root->type());
323 RefPtr<JSONObject> objectVal = JSONObject::cast(root); 318 RefPtr<JSONObject> objectVal;
324 ASSERT_TRUE(objectVal); 319 ASSERT_TRUE(root->asObject(&objectVal));
325 doubleVal = 0.0; 320 doubleVal = 0.0;
326 EXPECT_TRUE(objectVal->getNumber("number", &doubleVal)); 321 EXPECT_TRUE(objectVal->getNumber("number", &doubleVal));
327 EXPECT_DOUBLE_EQ(9.87654321, doubleVal); 322 EXPECT_DOUBLE_EQ(9.87654321, doubleVal);
328 RefPtr<JSONValue> nullVal = objectVal->get("null"); 323 RefPtr<JSONValue> nullVal = objectVal->get("null");
329 ASSERT_TRUE(nullVal.get()); 324 ASSERT_TRUE(nullVal.get());
330 EXPECT_EQ(JSONValue::TypeNull, nullVal->type()); 325 EXPECT_EQ(JSONValue::TypeNull, nullVal->type());
331 EXPECT_TRUE(objectVal->getString("S", &strVal)); 326 EXPECT_TRUE(objectVal->getString("S", &strVal));
332 EXPECT_EQ("str", strVal); 327 EXPECT_EQ("str", strVal);
333 328
334 // Test newline equivalence. 329 // Test newline equivalence.
(...skipping 12 matching lines...) Expand all
347 " \"null\":null,\r\n" 342 " \"null\":null,\r\n"
348 " \"\\x53\":\"str\"\r\n" 343 " \"\\x53\":\"str\"\r\n"
349 "}\r\n"); 344 "}\r\n");
350 ASSERT_TRUE(root2.get()); 345 ASSERT_TRUE(root2.get());
351 EXPECT_EQ(root->toJSONString(), root2->toJSONString()); 346 EXPECT_EQ(root->toJSONString(), root2->toJSONString());
352 347
353 // Test nesting 348 // Test nesting
354 root = parseJSON("{\"inner\":{\"array\":[true]},\"false\":false,\"d\":{}}"); 349 root = parseJSON("{\"inner\":{\"array\":[true]},\"false\":false,\"d\":{}}");
355 ASSERT_TRUE(root.get()); 350 ASSERT_TRUE(root.get());
356 EXPECT_EQ(JSONValue::TypeObject, root->type()); 351 EXPECT_EQ(JSONValue::TypeObject, root->type());
357 objectVal = JSONObject::cast(root); 352 ASSERT_TRUE(root->asObject(&objectVal));
358 ASSERT_TRUE(objectVal);
359 RefPtr<JSONObject> innerObject = objectVal->getObject("inner"); 353 RefPtr<JSONObject> innerObject = objectVal->getObject("inner");
360 ASSERT_TRUE(innerObject.get()); 354 ASSERT_TRUE(innerObject.get());
361 RefPtr<JSONArray> innerArray = innerObject->getArray("array"); 355 RefPtr<JSONArray> innerArray = innerObject->getArray("array");
362 ASSERT_TRUE(innerArray.get()); 356 ASSERT_TRUE(innerArray.get());
363 EXPECT_EQ(1U, innerArray->length()); 357 EXPECT_EQ(1U, innerArray->length());
364 boolValue = true; 358 boolValue = true;
365 EXPECT_TRUE(objectVal->getBoolean("false", &boolValue)); 359 EXPECT_TRUE(objectVal->getBoolean("false", &boolValue));
366 EXPECT_FALSE(boolValue); 360 EXPECT_FALSE(boolValue);
367 innerObject = objectVal->getObject("d"); 361 innerObject = objectVal->getObject("d");
368 EXPECT_TRUE(innerObject.get()); 362 EXPECT_TRUE(innerObject.get());
369 363
370 // Test keys with periods 364 // Test keys with periods
371 root = parseJSON("{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}"); 365 root = parseJSON("{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}");
372 ASSERT_TRUE(root.get()); 366 ASSERT_TRUE(root.get());
373 EXPECT_EQ(JSONValue::TypeObject, root->type()); 367 EXPECT_EQ(JSONValue::TypeObject, root->type());
374 objectVal = JSONObject::cast(root); 368 ASSERT_TRUE(root->asObject(&objectVal));
375 ASSERT_TRUE(objectVal);
376 int integerValue = 0; 369 int integerValue = 0;
377 EXPECT_TRUE(objectVal->getNumber("a.b", &integerValue)); 370 EXPECT_TRUE(objectVal->getNumber("a.b", &integerValue));
378 EXPECT_EQ(3, integerValue); 371 EXPECT_EQ(3, integerValue);
379 EXPECT_TRUE(objectVal->getNumber("c", &integerValue)); 372 EXPECT_TRUE(objectVal->getNumber("c", &integerValue));
380 EXPECT_EQ(2, integerValue); 373 EXPECT_EQ(2, integerValue);
381 innerObject = objectVal->getObject("d.e.f"); 374 innerObject = objectVal->getObject("d.e.f");
382 ASSERT_TRUE(innerObject.get()); 375 ASSERT_TRUE(innerObject.get());
383 EXPECT_EQ(1, innerObject->size()); 376 EXPECT_EQ(1, innerObject->size());
384 EXPECT_TRUE(innerObject->getNumber("g.h.i.j", &integerValue)); 377 EXPECT_TRUE(innerObject->getNumber("g.h.i.j", &integerValue));
385 EXPECT_EQ(1, integerValue); 378 EXPECT_EQ(1, integerValue);
386 379
387 root = parseJSON("{\"a\":{\"b\":2},\"a.b\":1}"); 380 root = parseJSON("{\"a\":{\"b\":2},\"a.b\":1}");
388 ASSERT_TRUE(root.get()); 381 ASSERT_TRUE(root.get());
389 EXPECT_EQ(JSONValue::TypeObject, root->type()); 382 EXPECT_EQ(JSONValue::TypeObject, root->type());
390 objectVal = JSONObject::cast(root); 383 ASSERT_TRUE(root->asObject(&objectVal));
391 ASSERT_TRUE(objectVal);
392 innerObject = objectVal->getObject("a"); 384 innerObject = objectVal->getObject("a");
393 ASSERT_TRUE(innerObject.get()); 385 ASSERT_TRUE(innerObject.get());
394 EXPECT_TRUE(innerObject->getNumber("b", &integerValue)); 386 EXPECT_TRUE(innerObject->getNumber("b", &integerValue));
395 EXPECT_EQ(2, integerValue); 387 EXPECT_EQ(2, integerValue);
396 EXPECT_TRUE(objectVal->getNumber("a.b", &integerValue)); 388 EXPECT_TRUE(objectVal->getNumber("a.b", &integerValue));
397 EXPECT_EQ(1, integerValue); 389 EXPECT_EQ(1, integerValue);
398 390
399 // Invalid, no closing brace 391 // Invalid, no closing brace
400 root = parseJSON("{\"a\": true"); 392 root = parseJSON("{\"a\": true");
401 EXPECT_FALSE(root.get()); 393 EXPECT_FALSE(root.get());
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
439 // A few thousand adjacent lists is fine. 431 // A few thousand adjacent lists is fine.
440 StringBuilder notEvil; 432 StringBuilder notEvil;
441 notEvil.reserveCapacity(15010); 433 notEvil.reserveCapacity(15010);
442 notEvil.append('['); 434 notEvil.append('[');
443 for (int i = 0; i < 5000; ++i) 435 for (int i = 0; i < 5000; ++i)
444 notEvil.append("[],"); 436 notEvil.append("[],");
445 notEvil.append("[]]"); 437 notEvil.append("[]]");
446 root = parseJSON(notEvil.toString()); 438 root = parseJSON(notEvil.toString());
447 ASSERT_TRUE(root.get()); 439 ASSERT_TRUE(root.get());
448 EXPECT_EQ(JSONValue::TypeArray, root->type()); 440 EXPECT_EQ(JSONValue::TypeArray, root->type());
449 list = JSONArray::cast(root); 441 ASSERT_TRUE(root->asArray(&list));
450 ASSERT_TRUE(list);
451 EXPECT_EQ(5001U, list->length()); 442 EXPECT_EQ(5001U, list->length());
452 443
453 // Test utf8 encoded input 444 // Test utf8 encoded input
454 root = parseJSON("\"\\xe7\\xbd\\x91\\xe9\\xa1\\xb5\""); 445 root = parseJSON("\"\\xe7\\xbd\\x91\\xe9\\xa1\\xb5\"");
455 ASSERT_TRUE(root.get()); 446 ASSERT_TRUE(root.get());
456 EXPECT_EQ(JSONValue::TypeString, root->type()); 447 EXPECT_EQ(JSONValue::TypeString, root->type());
457 EXPECT_TRUE(root->asString(&strVal)); 448 EXPECT_TRUE(root->asString(&strVal));
458 UChar tmp4[] = {0x7f51, 0x9875}; 449 UChar tmp4[] = {0x7f51, 0x9875};
459 EXPECT_EQ(String(tmp4, WTF_ARRAY_LENGTH(tmp4)), strVal); 450 EXPECT_EQ(String(tmp4, WTF_ARRAY_LENGTH(tmp4)), strVal);
460 451
461 root = parseJSON("{\"path\": \"/tmp/\\xc3\\xa0\\xc3\\xa8\\xc3\\xb2.png\"}"); 452 root = parseJSON("{\"path\": \"/tmp/\\xc3\\xa0\\xc3\\xa8\\xc3\\xb2.png\"}");
462 ASSERT_TRUE(root.get()); 453 ASSERT_TRUE(root.get());
463 EXPECT_EQ(JSONValue::TypeObject, root->type()); 454 EXPECT_EQ(JSONValue::TypeObject, root->type());
464 objectVal = JSONObject::cast(root); 455 EXPECT_TRUE(root->asObject(&objectVal));
465 ASSERT_TRUE(objectVal);
466 EXPECT_TRUE(objectVal->getString("path", &strVal)); 456 EXPECT_TRUE(objectVal->getString("path", &strVal));
467 UChar tmp5[] = {0x2f, 0x74, 0x6d, 0x70, 0x2f, 0xe0, 0xe8, 0xf2, 0x2e, 0x70, 0x6e, 0x67}; 457 UChar tmp5[] = {0x2f, 0x74, 0x6d, 0x70, 0x2f, 0xe0, 0xe8, 0xf2, 0x2e, 0x70, 0x6e, 0x67};
468 EXPECT_EQ(String(tmp5, WTF_ARRAY_LENGTH(tmp5)), strVal); 458 EXPECT_EQ(String(tmp5, WTF_ARRAY_LENGTH(tmp5)), strVal);
469 459
470 // Test invalid utf8 encoded input 460 // Test invalid utf8 encoded input
471 root = parseJSON("\"345\\xb0\\xa1\\xb0\\xa2\""); 461 root = parseJSON("\"345\\xb0\\xa1\\xb0\\xa2\"");
472 ASSERT_FALSE(root.get()); 462 ASSERT_FALSE(root.get());
473 root = parseJSON("\"123\\xc0\\x81\""); 463 root = parseJSON("\"123\\xc0\\x81\"");
474 ASSERT_FALSE(root.get()); 464 ASSERT_FALSE(root.get());
475 root = parseJSON("\"abc\\xc0\\xae\""); 465 root = parseJSON("\"abc\\xc0\\xae\"");
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
542 "//**/" 532 "//**/"
543 }; 533 };
544 534
545 for (size_t i = 0; i < WTF_ARRAY_LENGTH(invalidJson); ++i) { 535 for (size_t i = 0; i < WTF_ARRAY_LENGTH(invalidJson); ++i) {
546 RefPtr<JSONValue> result = parseJSON(invalidJson[i]); 536 RefPtr<JSONValue> result = parseJSON(invalidJson[i]);
547 EXPECT_FALSE(result.get()); 537 EXPECT_FALSE(result.get());
548 } 538 }
549 } 539 }
550 540
551 } // namespace blink 541 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698