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

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

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

Powered by Google App Engine
This is Rietveld 408576698