1/*
2 * Copyright (C) 2017-2019 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27
28#include "Test.h"
29#include "WTFStringUtilities.h"
30#include <wtf/JSONValues.h>
31
32namespace TestWebKitAPI {
33
34TEST(JSONValue, Construct)
35{
36 {
37 Ref<JSON::Value> value = JSON::Value::null();
38 EXPECT_TRUE(value->type() == JSON::Value::Type::Null);
39 EXPECT_TRUE(value->isNull());
40 }
41
42 {
43 Ref<JSON::Value> value = JSON::Value::create(true);
44 EXPECT_TRUE(value->type() == JSON::Value::Type::Boolean);
45 bool booleanValue;
46 EXPECT_TRUE(value->asBoolean(booleanValue));
47 EXPECT_EQ(booleanValue, true);
48
49 value = JSON::Value::create(false);
50 EXPECT_TRUE(value->type() == JSON::Value::Type::Boolean);
51 EXPECT_TRUE(value->asBoolean(booleanValue));
52 EXPECT_EQ(booleanValue, false);
53 }
54
55 {
56 Ref<JSON::Value> value = JSON::Value::create(1);
57 EXPECT_TRUE(value->type() == JSON::Value::Type::Integer);
58 int integerValue;
59 EXPECT_TRUE(value->asInteger(integerValue));
60 EXPECT_EQ(integerValue, 1);
61
62 // Doubles can be get as integers, but not the other way around.
63 float floatValue;
64 EXPECT_FALSE(value->asDouble(floatValue));
65
66 value = JSON::Value::create(std::numeric_limits<int>::max());
67 EXPECT_TRUE(value->asInteger(integerValue));
68 EXPECT_EQ(integerValue, std::numeric_limits<int>::max());
69 unsigned unsignedValue;
70 EXPECT_TRUE(value->asInteger(unsignedValue));
71 long longValue;
72 EXPECT_TRUE(value->asInteger(longValue));
73 long long longLongValue;
74 EXPECT_TRUE(value->asInteger(longLongValue));
75 unsigned long unsignedLongValue;
76 EXPECT_TRUE(value->asInteger(unsignedLongValue));
77 unsigned long long unsignedLongLongValue;
78 EXPECT_TRUE(value->asInteger(unsignedLongLongValue));
79 }
80
81 {
82 Ref<JSON::Value> value = JSON::Value::create(1.5);
83 EXPECT_TRUE(value->type() == JSON::Value::Type::Double);
84 double doubleValue;
85 EXPECT_TRUE(value->asDouble(doubleValue));
86 EXPECT_EQ(doubleValue, 1.5);
87
88 float floatValue;
89 EXPECT_TRUE(value->asDouble(floatValue));
90
91 int integerValue;
92 EXPECT_TRUE(value->asInteger(integerValue));
93 EXPECT_EQ(integerValue, static_cast<int>(1.5));
94
95 unsigned unsignedValue;
96 EXPECT_TRUE(value->asInteger(unsignedValue));
97 long longValue;
98 EXPECT_TRUE(value->asInteger(longValue));
99 long long longLongValue;
100 EXPECT_TRUE(value->asInteger(longLongValue));
101 unsigned long unsignedLongValue;
102 EXPECT_TRUE(value->asInteger(unsignedLongValue));
103 unsigned long long unsignedLongLongValue;
104 EXPECT_TRUE(value->asInteger(unsignedLongLongValue));
105 }
106
107 {
108 Ref<JSON::Value> value = JSON::Value::create("webkit");
109 EXPECT_TRUE(value->type() == JSON::Value::Type::String);
110 String stringValue;
111 EXPECT_TRUE(value->asString(stringValue));
112 EXPECT_EQ(stringValue, "webkit");
113
114 String nullString;
115 value = JSON::Value::create(nullString);
116 EXPECT_TRUE(value->asString(stringValue));
117 EXPECT_TRUE(stringValue.isNull());
118
119 value = JSON::Value::create(emptyString());
120 EXPECT_TRUE(value->asString(stringValue));
121 EXPECT_FALSE(stringValue.isNull());
122 EXPECT_TRUE(stringValue.isEmpty());
123 }
124
125 {
126 Ref<JSON::Array> array = JSON::Array::create();
127 EXPECT_TRUE(array->type() == JSON::Value::Type::Array);
128 EXPECT_EQ(array->length(), 0U);
129 }
130
131 {
132 Ref<JSON::Object> object = JSON::Object::create();
133 EXPECT_TRUE(object->type() == JSON::Value::Type::Object);
134 EXPECT_EQ(object->size(), 0);
135 }
136
137 {
138 auto array = JSON::ArrayOf<String>::create();
139 EXPECT_TRUE(array->type() == JSON::Value::Type::Array);
140 EXPECT_EQ(array->length(), 0U);
141 }
142}
143
144TEST(JSONArray, Basic)
145{
146 Ref<JSON::Array> array = JSON::Array::create();
147
148 array->pushValue(JSON::Value::null());
149 EXPECT_EQ(array->length(), 1U);
150 auto value = array->get(0);
151 EXPECT_TRUE(value->isNull());
152
153 array->pushBoolean(true);
154 EXPECT_EQ(array->length(), 2U);
155 value = array->get(1);
156 EXPECT_TRUE(value->type() == JSON::Value::Type::Boolean);
157 bool booleanValue;
158 EXPECT_TRUE(value->asBoolean(booleanValue));
159 EXPECT_EQ(booleanValue, true);
160
161 array->pushInteger(1);
162 EXPECT_EQ(array->length(), 3U);
163 value = array->get(2);
164 EXPECT_TRUE(value->type() == JSON::Value::Type::Integer);
165 int integerValue;
166 EXPECT_TRUE(value->asInteger(integerValue));
167 EXPECT_EQ(integerValue, 1);
168
169 array->pushDouble(1.5);
170 EXPECT_EQ(array->length(), 4U);
171 value = array->get(3);
172 EXPECT_TRUE(value->type() == JSON::Value::Type::Double);
173 double doubleValue;
174 EXPECT_TRUE(value->asDouble(doubleValue));
175 EXPECT_EQ(doubleValue, 1.5);
176
177 array->pushString("webkit");
178 EXPECT_EQ(array->length(), 5U);
179 value = array->get(4);
180 EXPECT_TRUE(value->type() == JSON::Value::Type::String);
181 String stringValue;
182 EXPECT_TRUE(value->asString(stringValue));
183 EXPECT_EQ(stringValue, "webkit");
184
185 array->pushObject(JSON::Object::create());
186 EXPECT_EQ(array->length(), 6U);
187 value = array->get(5);
188 EXPECT_TRUE(value->type() == JSON::Value::Type::Object);
189 RefPtr<JSON::Object> objectValue;
190 EXPECT_TRUE(value->asObject(objectValue));
191 EXPECT_EQ(objectValue->size(), 0);
192
193 array->pushArray(JSON::Array::create());
194 EXPECT_EQ(array->length(), 7U);
195 value = array->get(6);
196 EXPECT_TRUE(value->type() == JSON::Value::Type::Array);
197 RefPtr<JSON::Array> arrayValue;
198 EXPECT_TRUE(value->asArray(arrayValue));
199 EXPECT_EQ(arrayValue->length(), 0U);
200
201 auto it = array->begin();
202 value = it->get();
203 EXPECT_TRUE(value->type() == JSON::Value::Type::Null);
204 ++it;
205 EXPECT_FALSE(it == array->end());
206
207 value = it->get();
208 EXPECT_TRUE(value->type() == JSON::Value::Type::Boolean);
209 ++it;
210 EXPECT_FALSE(it == array->end());
211
212 value = it->get();
213 EXPECT_TRUE(value->type() == JSON::Value::Type::Integer);
214 ++it;
215 EXPECT_FALSE(it == array->end());
216
217 value = it->get();
218 EXPECT_TRUE(value->type() == JSON::Value::Type::Double);
219 ++it;
220 EXPECT_FALSE(it == array->end());
221
222 value = it->get();
223 EXPECT_TRUE(value->type() == JSON::Value::Type::String);
224 ++it;
225 EXPECT_FALSE(it == array->end());
226
227 value = it->get();
228 EXPECT_TRUE(value->type() == JSON::Value::Type::Object);
229 ++it;
230 EXPECT_FALSE(it == array->end());
231
232 value = it->get();
233 EXPECT_TRUE(value->type() == JSON::Value::Type::Array);
234 ++it;
235 EXPECT_TRUE(it == array->end());
236}
237
238TEST(JSONArrayOf, Basic)
239{
240 Ref<JSON::ArrayOf<JSON::Value>> array = JSON::ArrayOf<JSON::Value>::create();
241
242 array->addItem(JSON::Value::null());
243 EXPECT_EQ(array->length(), 1U);
244 auto value = array->get(0);
245 EXPECT_TRUE(value->isNull());
246
247 // Note: addItem(bool) is not implemented because it would be ambiguous
248 // with addItem(RefPtr<T>), and the latter is much more common to do.
249
250 array->addItem(1);
251 EXPECT_EQ(array->length(), 2U);
252 value = array->get(1);
253 EXPECT_TRUE(value->type() == JSON::Value::Type::Integer);
254 int integerValue;
255 EXPECT_TRUE(value->asInteger(integerValue));
256 EXPECT_EQ(integerValue, 1);
257
258 array->addItem(1.5);
259 EXPECT_EQ(array->length(), 3U);
260 value = array->get(2);
261 EXPECT_TRUE(value->type() == JSON::Value::Type::Double);
262 double doubleValue;
263 EXPECT_TRUE(value->asDouble(doubleValue));
264 EXPECT_EQ(doubleValue, 1.5);
265
266 array->addItem("webkit");
267 EXPECT_EQ(array->length(), 4U);
268 value = array->get(3);
269 EXPECT_TRUE(value->type() == JSON::Value::Type::String);
270 String stringValue;
271 EXPECT_TRUE(value->asString(stringValue));
272 EXPECT_EQ(stringValue, "webkit");
273
274 array->addItem(JSON::Object::create());
275 EXPECT_EQ(array->length(), 5U);
276 value = array->get(4);
277 EXPECT_TRUE(value->type() == JSON::Value::Type::Object);
278 RefPtr<JSON::Object> objectValue;
279 EXPECT_TRUE(value->asObject(objectValue));
280 EXPECT_EQ(objectValue->size(), 0);
281
282 array->addItem(JSON::Array::create());
283 EXPECT_EQ(array->length(), 6U);
284 value = array->get(5);
285 EXPECT_TRUE(value->type() == JSON::Value::Type::Array);
286 RefPtr<JSON::Array> arrayValue;
287 EXPECT_TRUE(value->asArray(arrayValue));
288 EXPECT_EQ(arrayValue->length(), 0U);
289
290 auto it = array->begin();
291 value = it->get();
292 EXPECT_TRUE(value->type() == JSON::Value::Type::Null);
293 ++it;
294 EXPECT_FALSE(it == array->end());
295
296 value = it->get();
297 EXPECT_TRUE(value->type() == JSON::Value::Type::Integer);
298 ++it;
299 EXPECT_FALSE(it == array->end());
300
301 value = it->get();
302 EXPECT_TRUE(value->type() == JSON::Value::Type::Double);
303 ++it;
304 EXPECT_FALSE(it == array->end());
305
306 value = it->get();
307 EXPECT_TRUE(value->type() == JSON::Value::Type::String);
308 ++it;
309 EXPECT_FALSE(it == array->end());
310
311 value = it->get();
312 EXPECT_TRUE(value->type() == JSON::Value::Type::Object);
313 ++it;
314 EXPECT_FALSE(it == array->end());
315
316 value = it->get();
317 EXPECT_TRUE(value->type() == JSON::Value::Type::Array);
318 ++it;
319 EXPECT_TRUE(it == array->end());
320}
321
322TEST(JSONObject, Basic)
323{
324 Ref<JSON::Object> object = JSON::Object::create();
325
326 object->setValue("null", JSON::Value::null());
327 EXPECT_EQ(object->size(), 1);
328 RefPtr<JSON::Value> value;
329 EXPECT_TRUE(object->getValue("null", value));
330 EXPECT_TRUE(value->isNull());
331
332 object->setBoolean("boolean", true);
333 EXPECT_EQ(object->size(), 2);
334 bool booleanValue;
335 EXPECT_TRUE(object->getBoolean("boolean", booleanValue));
336 EXPECT_EQ(booleanValue, true);
337
338 object->setInteger("integer", 1);
339 EXPECT_EQ(object->size(), 3);
340 int integerValue;
341 EXPECT_TRUE(object->getInteger("integer", integerValue));
342 EXPECT_EQ(integerValue, 1);
343
344 object->setDouble("double", 1.5);
345 EXPECT_EQ(object->size(), 4);
346 double doubleValue;
347 EXPECT_TRUE(object->getDouble("double", doubleValue));
348 EXPECT_EQ(doubleValue, 1.5);
349
350 object->setString("string", "webkit");
351 EXPECT_EQ(object->size(), 5);
352 String stringValue;
353 EXPECT_TRUE(object->getString("string", stringValue));
354 EXPECT_EQ(stringValue, "webkit");
355
356 object->setObject("object", JSON::Object::create());
357 EXPECT_EQ(object->size(), 6);
358 RefPtr<JSON::Object> objectValue;
359 EXPECT_TRUE(object->getObject("object", objectValue));
360 EXPECT_EQ(objectValue->size(), 0);
361
362 object->setArray("array", JSON::Array::create());
363 EXPECT_EQ(object->size(), 7);
364 RefPtr<JSON::Array> arrayValue;
365 EXPECT_TRUE(object->getArray("array", arrayValue));
366 EXPECT_EQ(arrayValue->length(), 0U);
367
368 Vector<const char*> keys = { "null", "boolean", "integer", "double", "string", "object", "array" };
369 auto end = object->end();
370 for (auto it = object->begin(); it != end; ++it) {
371 auto position = keys.find(it->key);
372 EXPECT_NE(position, notFound);
373 EXPECT_TRUE(it == object->find(keys[position]));
374 keys.remove(position);
375 }
376 EXPECT_TRUE(keys.isEmpty());
377
378 object->remove("null");
379 EXPECT_EQ(object->size(), 6);
380 EXPECT_TRUE(object->find("null") == object->end());
381
382 object->remove("boolean");
383 EXPECT_EQ(object->size(), 5);
384 EXPECT_TRUE(object->find("boolean") == object->end());
385
386 object->remove("integer");
387 EXPECT_EQ(object->size(), 4);
388 EXPECT_TRUE(object->find("integer") == object->end());
389
390 object->remove("double");
391 EXPECT_EQ(object->size(), 3);
392 EXPECT_TRUE(object->find("double") == object->end());
393
394 object->remove("string");
395 EXPECT_EQ(object->size(), 2);
396 EXPECT_TRUE(object->find("string") == object->end());
397
398 object->remove("object");
399 EXPECT_EQ(object->size(), 1);
400 EXPECT_TRUE(object->find("object") == object->end());
401
402 object->remove("array");
403 EXPECT_EQ(object->size(), 0);
404 EXPECT_TRUE(object->find("array") == object->end());
405}
406
407TEST(JSONValue, ToJSONString)
408{
409 {
410 Ref<JSON::Value> value = JSON::Value::null();
411 EXPECT_EQ(value->toJSONString(), "null");
412 }
413
414 {
415 Ref<JSON::Value> value = JSON::Value::create(true);
416 EXPECT_EQ(value->toJSONString(), "true");
417
418 value = JSON::Value::create(false);
419 EXPECT_EQ(value->toJSONString(), "false");
420 }
421
422 {
423 Ref<JSON::Value> value = JSON::Value::create(1);
424 EXPECT_EQ(value->toJSONString(), "1");
425 }
426
427 {
428 Ref<JSON::Value> value = JSON::Value::create(1.5);
429 EXPECT_EQ(value->toJSONString(), "1.5");
430 }
431
432 {
433 Ref<JSON::Value> value = JSON::Value::create("webkit");
434 EXPECT_EQ(value->toJSONString(), "\"webkit\"");
435 }
436
437 {
438 Ref<JSON::Array> array = JSON::Array::create();
439 EXPECT_EQ(array->toJSONString(), "[]");
440 array->pushValue(JSON::Value::null());
441 EXPECT_EQ(array->toJSONString(), "[null]");
442 array->pushBoolean(true);
443 array->pushBoolean(false);
444 EXPECT_EQ(array->toJSONString(), "[null,true,false]");
445 array->pushInteger(1);
446 array->pushDouble(1.5);
447 EXPECT_EQ(array->toJSONString(), "[null,true,false,1,1.5]");
448 array->pushString("webkit");
449 EXPECT_EQ(array->toJSONString(), "[null,true,false,1,1.5,\"webkit\"]");
450 Ref<JSON::Array> subArray = JSON::Array::create();
451 subArray->pushString("string");
452 subArray->pushObject(JSON::Object::create());
453 EXPECT_EQ(subArray->toJSONString(), "[\"string\",{}]");
454 array->pushArray(WTFMove(subArray));
455 EXPECT_EQ(array->toJSONString(), "[null,true,false,1,1.5,\"webkit\",[\"string\",{}]]");
456 }
457
458 {
459 Ref<JSON::Object> object = JSON::Object::create();
460 EXPECT_EQ(object->toJSONString(), "{}");
461 object->setValue("null", JSON::Value::null());
462 EXPECT_EQ(object->toJSONString(), "{\"null\":null}");
463 object->setBoolean("boolean", true);
464 EXPECT_EQ(object->toJSONString(), "{\"null\":null,\"boolean\":true}");
465 object->setDouble("double", 1.5);
466 EXPECT_EQ(object->toJSONString(), "{\"null\":null,\"boolean\":true,\"double\":1.5}");
467 object->setString("string", "webkit");
468 EXPECT_EQ(object->toJSONString(), "{\"null\":null,\"boolean\":true,\"double\":1.5,\"string\":\"webkit\"}");
469 object->setArray("array", JSON::Array::create());
470 EXPECT_EQ(object->toJSONString(), "{\"null\":null,\"boolean\":true,\"double\":1.5,\"string\":\"webkit\",\"array\":[]}");
471 Ref<JSON::Object> subObject = JSON::Object::create();
472 subObject->setString("foo", "bar");
473 subObject->setInteger("baz", 25);
474 object->setObject("object", WTFMove(subObject));
475 EXPECT_EQ(object->toJSONString(), "{\"null\":null,\"boolean\":true,\"double\":1.5,\"string\":\"webkit\",\"array\":[],\"object\":{\"foo\":\"bar\",\"baz\":25}}");
476 }
477}
478
479TEST(JSONValue, ParseJSON)
480{
481 {
482 RefPtr<JSON::Value> value;
483 EXPECT_TRUE(JSON::Value::parseJSON("null", value));
484 EXPECT_TRUE(value->isNull());
485 }
486
487 {
488 RefPtr<JSON::Value> value;
489 EXPECT_TRUE(JSON::Value::parseJSON("true", value));
490 EXPECT_TRUE(value->type() == JSON::Value::Type::Boolean);
491 bool booleanValue;
492 EXPECT_TRUE(value->asBoolean(booleanValue));
493 EXPECT_EQ(booleanValue, true);
494
495 EXPECT_TRUE(JSON::Value::parseJSON("false", value));
496 EXPECT_TRUE(value->type() == JSON::Value::Type::Boolean);
497 EXPECT_TRUE(value->asBoolean(booleanValue));
498 EXPECT_EQ(booleanValue, false);
499 }
500
501 {
502 RefPtr<JSON::Value> value;
503 EXPECT_TRUE(JSON::Value::parseJSON("1", value));
504 // Numbers are always parsed as double.
505 EXPECT_TRUE(value->type() == JSON::Value::Type::Double);
506 double doubleValue;
507 EXPECT_TRUE(value->asDouble(doubleValue));
508 EXPECT_EQ(doubleValue, 1.0);
509
510 EXPECT_TRUE(JSON::Value::parseJSON("1.5", value));
511 EXPECT_TRUE(value->type() == JSON::Value::Type::Double);
512 EXPECT_TRUE(value->asDouble(doubleValue));
513 EXPECT_EQ(doubleValue, 1.5);
514 }
515
516 {
517 RefPtr<JSON::Value> value;
518 EXPECT_TRUE(JSON::Value::parseJSON("\"string\"", value));
519 EXPECT_TRUE(value->type() == JSON::Value::Type::String);
520 String stringValue;
521 EXPECT_TRUE(value->asString(stringValue));
522 EXPECT_EQ(stringValue, "string");
523 }
524
525 {
526 RefPtr<JSON::Value> value;
527 EXPECT_TRUE(JSON::Value::parseJSON("[]", value));
528 EXPECT_TRUE(value->type() == JSON::Value::Type::Array);
529 RefPtr<JSON::Array> arrayValue;
530 EXPECT_TRUE(value->asArray(arrayValue));
531 EXPECT_EQ(arrayValue->length(), 0U);
532
533 EXPECT_TRUE(JSON::Value::parseJSON("[null, 1 ,2.5,[{\"foo\":\"bar\"},{\"baz\":false}],\"webkit\"]", value));
534 EXPECT_TRUE(value->type() == JSON::Value::Type::Array);
535 EXPECT_TRUE(value->asArray(arrayValue));
536 EXPECT_EQ(arrayValue->length(), 5U);
537 auto it = arrayValue->begin();
538 RefPtr<JSON::Value> itemValue = it->get();
539 EXPECT_TRUE(itemValue->type() == JSON::Value::Type::Null);
540 ++it;
541 EXPECT_FALSE(it == arrayValue->end());
542
543 itemValue = it->get();
544 EXPECT_TRUE(itemValue->type() == JSON::Value::Type::Double);
545 int integerValue;
546 EXPECT_TRUE(itemValue->asInteger(integerValue));
547 EXPECT_EQ(integerValue, 1);
548 ++it;
549 EXPECT_FALSE(it == arrayValue->end());
550
551 itemValue = it->get();
552 EXPECT_TRUE(itemValue->type() == JSON::Value::Type::Double);
553 double doubleValue;
554 EXPECT_TRUE(itemValue->asDouble(doubleValue));
555 EXPECT_EQ(doubleValue, 2.5);
556 ++it;
557 EXPECT_FALSE(it == arrayValue->end());
558
559 itemValue = it->get();
560 EXPECT_TRUE(itemValue->type() == JSON::Value::Type::Array);
561 RefPtr<JSON::Array> subArrayValue;
562 EXPECT_TRUE(itemValue->asArray(subArrayValue));
563 EXPECT_EQ(subArrayValue->length(), 2U);
564 RefPtr<JSON::Value> objectValue = subArrayValue->get(0);
565 EXPECT_TRUE(objectValue->type() == JSON::Value::Type::Object);
566 RefPtr<JSON::Object> object;
567 EXPECT_TRUE(objectValue->asObject(object));
568 EXPECT_EQ(object->size(), 1);
569 String stringValue;
570 EXPECT_TRUE(object->getString("foo", stringValue));
571 EXPECT_EQ(stringValue, "bar");
572 objectValue = subArrayValue->get(1);
573 EXPECT_TRUE(objectValue->type() == JSON::Value::Type::Object);
574 EXPECT_TRUE(objectValue->asObject(object));
575 EXPECT_EQ(object->size(), 1);
576 bool booleanValue;
577 EXPECT_TRUE(object->getBoolean("baz", booleanValue));
578 EXPECT_EQ(booleanValue, false);
579 ++it;
580 EXPECT_FALSE(it == arrayValue->end());
581
582 itemValue = it->get();
583 EXPECT_TRUE(itemValue->type() == JSON::Value::Type::String);
584 EXPECT_TRUE(itemValue->asString(stringValue));
585 EXPECT_EQ(stringValue, "webkit");
586 ++it;
587 EXPECT_TRUE(it == arrayValue->end());
588 }
589
590 {
591 RefPtr<JSON::Value> value;
592 EXPECT_TRUE(JSON::Value::parseJSON("{}", value));
593 EXPECT_TRUE(value->type() == JSON::Value::Type::Object);
594 RefPtr<JSON::Object> objectValue;
595 EXPECT_TRUE(value->asObject(objectValue));
596 EXPECT_EQ(objectValue->size(), 0);
597
598 EXPECT_TRUE(JSON::Value::parseJSON("{\"foo\": \"bar\", \"baz\": {\"sub\":[null,false]}}", value));
599 EXPECT_TRUE(value->type() == JSON::Value::Type::Object);
600 EXPECT_TRUE(value->asObject(objectValue));
601 EXPECT_EQ(objectValue->size(), 2);
602
603 String stringValue;
604 EXPECT_TRUE(objectValue->getString("foo", stringValue));
605 EXPECT_EQ(stringValue, "bar");
606
607 RefPtr<JSON::Object> object;
608 EXPECT_TRUE(objectValue->getObject("baz", object));
609 EXPECT_EQ(object->size(), 1);
610 RefPtr<JSON::Array> array;
611 EXPECT_TRUE(object->getArray("sub", array));
612 EXPECT_EQ(array->length(), 2U);
613 }
614
615 {
616 RefPtr<JSON::Value> value;
617 EXPECT_FALSE(JSON::Value::parseJSON(",", value));
618 EXPECT_FALSE(JSON::Value::parseJSON("\"foo", value));
619 EXPECT_FALSE(JSON::Value::parseJSON("foo\"", value));
620 EXPECT_FALSE(JSON::Value::parseJSON("TrUe", value));
621 EXPECT_FALSE(JSON::Value::parseJSON("False", value));
622 EXPECT_FALSE(JSON::Value::parseJSON("1.d", value));
623 EXPECT_FALSE(JSON::Value::parseJSON("[1,]", value));
624 EXPECT_FALSE(JSON::Value::parseJSON("1,2]", value));
625 EXPECT_FALSE(JSON::Value::parseJSON("[1,2", value));
626 EXPECT_FALSE(JSON::Value::parseJSON("[1 2]", value));
627 EXPECT_FALSE(JSON::Value::parseJSON("[1,2]]", value));
628 EXPECT_FALSE(JSON::Value::parseJSON("[1,2],", value));
629 EXPECT_FALSE(JSON::Value::parseJSON("{foo:\"bar\"}", value));
630 EXPECT_FALSE(JSON::Value::parseJSON("{\"foo\":bar}", value));
631 EXPECT_FALSE(JSON::Value::parseJSON("{\"foo:\"bar\"}", value));
632 EXPECT_FALSE(JSON::Value::parseJSON("{foo\":\"bar\"}", value));
633 EXPECT_FALSE(JSON::Value::parseJSON("{{\"foo\":\"bar\"}}", value));
634 EXPECT_FALSE(JSON::Value::parseJSON("{\"foo\":\"bar\"},", value));
635 EXPECT_FALSE(JSON::Value::parseJSON("[{\"foo\":\"bar\"},{\"baz\":false},]", value));
636 EXPECT_FALSE(JSON::Value::parseJSON("[{\"foo\":{\"baz\":false}]", value));
637 }
638
639 {
640 RefPtr<JSON::Value> value;
641 EXPECT_TRUE(JSON::Value::parseJSON(" \"foo\" \n", value));
642 String stringValue;
643 EXPECT_TRUE(value->asString(stringValue));
644 EXPECT_EQ("foo", stringValue);
645 }
646
647 {
648 RefPtr<JSON::Value> value;
649 EXPECT_TRUE(JSON::Value::parseJSON(" 1", value));
650 EXPECT_TRUE(JSON::Value::parseJSON("\t1", value));
651 EXPECT_TRUE(JSON::Value::parseJSON("\n1", value));
652 EXPECT_TRUE(JSON::Value::parseJSON("1 ", value));
653 EXPECT_TRUE(JSON::Value::parseJSON("1\t", value));
654 EXPECT_TRUE(JSON::Value::parseJSON("1\n", value));
655 EXPECT_TRUE(JSON::Value::parseJSON(" 1 ", value));
656 EXPECT_TRUE(JSON::Value::parseJSON(" {} ", value));
657 EXPECT_TRUE(JSON::Value::parseJSON(" [] ", value));
658 EXPECT_TRUE(JSON::Value::parseJSON("\"\\xFF\"", value));
659 EXPECT_TRUE(JSON::Value::parseJSON("\"\\u1234\"", value));
660
661 EXPECT_FALSE(JSON::Value::parseJSON("1 1", value));
662 EXPECT_FALSE(JSON::Value::parseJSON("{} {}", value));
663 EXPECT_FALSE(JSON::Value::parseJSON("[] []", value));
664 EXPECT_FALSE(JSON::Value::parseJSON("\"\\", value));
665 EXPECT_FALSE(JSON::Value::parseJSON("\"\\xF", value));
666 EXPECT_FALSE(JSON::Value::parseJSON("\"\\xF\"", value));
667 EXPECT_FALSE(JSON::Value::parseJSON("\"\\xF \"", value));
668 EXPECT_FALSE(JSON::Value::parseJSON("\"\\u1", value));
669 EXPECT_FALSE(JSON::Value::parseJSON("\"\\u1\"", value));
670 EXPECT_FALSE(JSON::Value::parseJSON("\"\\u1 \"", value));
671 EXPECT_FALSE(JSON::Value::parseJSON("\"\\u12", value));
672 EXPECT_FALSE(JSON::Value::parseJSON("\"\\u12\"", value));
673 EXPECT_FALSE(JSON::Value::parseJSON("\"\\u12 \"", value));
674 EXPECT_FALSE(JSON::Value::parseJSON("\"\\u123", value));
675 EXPECT_FALSE(JSON::Value::parseJSON("\"\\u123\"", value));
676 EXPECT_FALSE(JSON::Value::parseJSON("\"\\u123 \"", value));
677 }
678}
679
680TEST(JSONValue, MemoryCost)
681{
682 {
683 Ref<JSON::Value> value = JSON::Value::null();
684 size_t memoryCost = value->memoryCost();
685 EXPECT_GT(memoryCost, 0U);
686 EXPECT_LE(memoryCost, 8U);
687 }
688
689 {
690 Ref<JSON::Value> value = JSON::Value::create(true);
691 size_t memoryCost = value->memoryCost();
692 EXPECT_GT(memoryCost, 0U);
693 EXPECT_LE(memoryCost, 8U);
694 }
695
696 {
697 Ref<JSON::Value> value = JSON::Value::create(1.0);
698 size_t memoryCost = value->memoryCost();
699 EXPECT_GT(memoryCost, 0U);
700 EXPECT_LE(memoryCost, 8U);
701 }
702
703 {
704 Ref<JSON::Value> value = JSON::Value::create(1);
705 size_t memoryCost = value->memoryCost();
706 EXPECT_GT(memoryCost, 0U);
707 EXPECT_LE(memoryCost, 8U);
708 }
709
710 {
711 Ref<JSON::Value> value = JSON::Value::create("test");
712 size_t memoryCost = value->memoryCost();
713 EXPECT_GT(memoryCost, 0U);
714 EXPECT_LE(memoryCost, 36U);
715 }
716
717 {
718 Ref<JSON::Value> value = JSON::Value::create("");
719 size_t memoryCost = value->memoryCost();
720 EXPECT_GT(memoryCost, 0U);
721 EXPECT_LE(memoryCost, 32U);
722 }
723
724 {
725 Ref<JSON::Value> value = JSON::Value::create(String());
726 size_t memoryCost = value->memoryCost();
727 EXPECT_GT(memoryCost, 0U);
728 EXPECT_LE(memoryCost, 8U);
729 }
730
731 {
732 Ref<JSON::Value> valueA = JSON::Value::create("t");
733 Ref<JSON::Value> valueB = JSON::Value::create("te");
734 Ref<JSON::Value> valueC = JSON::Value::create("tes");
735 Ref<JSON::Value> valueD = JSON::Value::create("test");
736 EXPECT_LT(valueA->memoryCost(), valueB->memoryCost());
737 EXPECT_LT(valueB->memoryCost(), valueC->memoryCost());
738 EXPECT_LT(valueC->memoryCost(), valueD->memoryCost());
739 }
740
741 {
742 Ref<JSON::Value> valueA = JSON::Value::create("t");
743 Ref<JSON::Value> valueB = JSON::Value::create("😀");
744 EXPECT_LT(valueA->memoryCost(), valueB->memoryCost());
745 }
746
747 {
748 Ref<JSON::Object> value = JSON::Object::create();
749 value->setValue("test", JSON::Value::null());
750 size_t memoryCost = value->memoryCost();
751 EXPECT_GT(memoryCost, 0U);
752 EXPECT_LE(memoryCost, 20U);
753 }
754
755 {
756 Ref<JSON::Object> value = JSON::Object::create();
757 size_t memoryCost = value->memoryCost();
758 EXPECT_GT(memoryCost, 0U);
759 EXPECT_LE(memoryCost, 8U);
760 }
761
762 {
763 Ref<JSON::Object> value = JSON::Object::create();
764
765 value->setValue("1", JSON::Value::null());
766 size_t memoryCost1 = value->memoryCost();
767
768 value->setValue("2", JSON::Value::null());
769 size_t memoryCost2 = value->memoryCost();
770
771 value->setValue("3", JSON::Value::null());
772 size_t memoryCost3 = value->memoryCost();
773
774 value->setValue("4", JSON::Value::null());
775 size_t memoryCost4 = value->memoryCost();
776
777 EXPECT_LT(memoryCost1, memoryCost2);
778 EXPECT_LT(memoryCost2, memoryCost3);
779 EXPECT_LT(memoryCost3, memoryCost4);
780 }
781
782 {
783 Ref<JSON::Array> value = JSON::Array::create();
784 value->pushValue(JSON::Value::null());
785 size_t memoryCost = value->memoryCost();
786 EXPECT_GT(memoryCost, 0U);
787 EXPECT_LE(memoryCost, 16U);
788 }
789
790 {
791 Ref<JSON::Array> value = JSON::Array::create();
792 size_t memoryCost = value->memoryCost();
793 EXPECT_GT(memoryCost, 0U);
794 EXPECT_LE(memoryCost, 8U);
795 }
796
797 {
798 Ref<JSON::Array> value = JSON::Array::create();
799
800 value->pushValue(JSON::Value::null());
801 size_t memoryCost1 = value->memoryCost();
802
803 value->pushValue(JSON::Value::null());
804 size_t memoryCost2 = value->memoryCost();
805
806 value->pushValue(JSON::Value::null());
807 size_t memoryCost3 = value->memoryCost();
808
809 value->pushValue(JSON::Value::null());
810 size_t memoryCost4 = value->memoryCost();
811
812 EXPECT_LT(memoryCost1, memoryCost2);
813 EXPECT_LT(memoryCost2, memoryCost3);
814 EXPECT_LT(memoryCost3, memoryCost4);
815 }
816}
817
818} // namespace TestWebKitAPI
819