Debian Codegraph @

js/src/tests/test262/built-ins/JSON/parse/reviver-context-source-primitive-literal.js

raw
1 // |reftest| shell-option(--enable-json-parse-with-source) skip-if(!JSON.hasOwnProperty('isRawJSON')||!xulRuntime.shell) -- json-parse-with-source is not enabled unconditionally, requires shell-options
2 // Copyright (C) 2023 the V8 project authors. All rights reserved.
3 // This code is governed by the BSD license found in the LICENSE file.
4
5 /*---
6 esid: sec-json.parse
7 description: >
8 Context argument and its source property behave as expected when parsing a
9 NumericLiteral, NullLiteral, BoolLiteral, or StringLiteral JSON string
10 includes: [compareArray.js, propertyHelper.js]
11 features: [json-parse-with-source]
12 ---*/
13
14 function assertOnlyOwnProperties(object, props, message) {
15 assert.compareArray(Object.getOwnPropertyNames(object), props, `${message}: object should have no other properties than expected`);
16 assert.compareArray(Object.getOwnPropertySymbols(object), [], `${message}: object should have no own symbol properties`);
17 }
18
19 function reviverWithExpectedSources(expectedSources) {
20 let i = 0;
21 return function reviver(key, value, context) {
22 assert.sameValue(typeof context, "object", "context should be an object");
23 assert.sameValue(Object.getPrototypeOf(context), Object.prototype, "context should be a plain object");
24 if (expectedSources[i] !== undefined) {
25 assertOnlyOwnProperties(context, ["source"],
26 "the JSON value is a primitve value, its context should only have a source property");
27 verifyProperty(context, "source", {
28 value: expectedSources[i++],
29 configurable: true,
30 enumerable: true,
31 writable: true,
32 }, { restore: true });
33 } else {
34 assertOnlyOwnProperties(context, [],
35 "the JSON value is an Array or Object, its context should have no property");
36 i++;
37 }
38 return value;
39 };
40 }
41
42 assert.sameValue(1, JSON.parse('1', reviverWithExpectedSources(['1'])));
43 assert.sameValue(1.1, JSON.parse('1.1', reviverWithExpectedSources(['1.1'])));
44 assert.sameValue(-1, JSON.parse('-1', reviverWithExpectedSources(['-1'])));
45 assert.sameValue(
46 -1.1,
47 JSON.parse('-1.1', reviverWithExpectedSources(['-1.1']))
48 );
49 assert.sameValue(
50 11,
51 JSON.parse('1.1e1', reviverWithExpectedSources(['1.1e1']))
52 );
53 assert.sameValue(
54 11,
55 JSON.parse('1.1e+1', reviverWithExpectedSources(['1.1e+1']))
56 );
57 assert.sameValue(
58 0.11,
59 JSON.parse('1.1e-1', reviverWithExpectedSources(['1.1e-1']))
60 );
61 assert.sameValue(
62 11,
63 JSON.parse('1.1E1', reviverWithExpectedSources(['1.1E1']))
64 );
65 assert.sameValue(
66 11,
67 JSON.parse('1.1E+1', reviverWithExpectedSources(['1.1E+1']))
68 );
69 assert.sameValue(
70 0.11,
71 JSON.parse('1.1E-1', reviverWithExpectedSources(['1.1E-1']))
72 );
73
74 // Test NullLiteral, BoolLiteral, StringLiteral
75 assert.sameValue(
76 JSON.parse('null', reviverWithExpectedSources(['null'])),
77 null
78 );
79 assert.sameValue(
80 JSON.parse('true', reviverWithExpectedSources(['true'])),
81 true
82 );
83 assert.sameValue(
84 JSON.parse('false', reviverWithExpectedSources(['false'])),
85 false
86 );
87 assert.sameValue(
88 JSON.parse('"foo"', reviverWithExpectedSources(['"foo"'])),
89 "foo"
90 );
91
92 reportCompare(0, 0);