summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/JSON/parse-with-source.js
blob: 5e144bd6a4be15ef7f96133b32d46e8607df685d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// |reftest| shell-option(--enable-json-parse-with-source) skip-if(!JSON.hasOwnProperty('isRawJSON')||!xulRuntime.shell)

var actual;

function reviver(key, value, context) {
    assertEq(arguments.length, 3);
    if ("source" in context) {
        actual.push(context["source"]);
    } else { // objects and arrays have no "source"
        actual.push(null);
    }
}

let tests = [
    // STRINGS
    {input: '""', expected: ['""']},
    {input: '"str"', expected: ['"str"']},
    {input: '"str" ', expected: ['"str"']},
    {input: ' "str" ', expected: ['"str"']},
    {input: ' " str"', expected: ['" str"']},
    {input: '"\uD83D\uDE0A\u2764\u2FC1"', expected: ['"\uD83D\uDE0A\u2764\u2FC1"']},
    // NUMBERS
    {input: '1', expected: ['1']},
    {input: ' 1', expected: ['1']},
    {input: '4.2', expected: ['4.2']},
    {input: '4.2 ', expected: ['4.2']},
    {input: '4.2000 ', expected: ['4.2000']},
    {input: '4e2000 ', expected: ['4e2000']},
    {input: '4.4e2000 ', expected: ['4.4e2000']},
    {input: '9007199254740999', expected: ['9007199254740999']},
    {input: '-31', expected: ['-31']},
    {input: '-3.1', expected: ['-3.1']},
    {input: ' -31 ', expected: ['-31']},
    // BOOLEANS
    {input: 'true', expected: ['true']},
    {input: 'true ', expected: ['true']},
    {input: 'false', expected: ['false']},
    {input: ' false', expected: ['false']},
    // NULL
    {input: 'null', expected: ['null']},
    {input: ' null', expected: ['null']},
    {input: '\tnull ', expected: ['null']},
    {input: 'null\t', expected: ['null']},
    // OBJECTS
    {input: '{ }', expected: [null]},
    {input: '{ "4": 1 }', expected: ['1', null]},
    {input: '{ "a": 1 }', expected: ['1', null]},
    {input: '{ "b": 2, "a": 1 }', expected: ['2', '1', null]},
    {input: '{ "b": 2, "1": 1 }', expected: ['1', '2', null]},
    {input: '{ "b": 2, "c": null }', expected: ['2', 'null', null]},
    {input: '{ "b": 2, "b": 1, "b": 4 }', expected: ['4', null]},
    {input: '{ "b": 2, "a": "1" }', expected: ['2', '"1"', null]},
    {input: '{ "b": { "c": 3 }, "a": 1 }', expected: ['3', null, '1', null]},
    // ARRAYS
    {input: '[]', expected: [null]},
    {input: '[1, 5, 2]', expected: ['1', '5', '2', null]},
    {input: '[1, null, 2]', expected: ['1', 'null', '2', null]},
    {input: '[1, {"a":2}, "7"]', expected: ['1', '2', null, '"7"', null]},
    {input: '[1, [2, [3, [4, 5], [6, 7], 8], 9], 10]', expected: ['1', '2', '3', '4', '5', null, '6', '7', null, '8', null, '9', null, '10', null]},
    {input: '[1, [2, [3, [4, 5, 6, 7, 8, 9, 10], []]]]', expected: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', null, null, null, null, null]},
    {input: '{"a": [1, {"b":2}, "7"], "c": 8}', expected: ['1', '2', null, '"7"', null, '8', null]},
];
for (const test of tests) {
    print("Testing " + JSON.stringify(test));
    actual = [];
    JSON.parse(test.input, reviver);
    assertDeepEq(actual, test.expected);
}

// If the constructed object is modified but the result of the modification is
// the same as the original, we should still provide the source
function replace_c_with_same_val(key, value, context) {
    if (key === "a") {
        this["c"] = "ABCDEABCDE";
    }
    if (key) {
       assertEq("source" in context, true);
    }
    return value;
}
JSON.parse('{ "a": "b", "c": "ABCDEABCDE" }', replace_c_with_same_val);

// rawJSON
function assertIsRawJson(rawJson, expectedRawJsonValue) {
    assertEq(null, Object.getPrototypeOf(rawJson));
    assertEq(true, Object.hasOwn(rawJson, 'rawJSON'));
    assertDeepEq(['rawJSON'], Object.getOwnPropertyNames(rawJson));
    assertDeepEq([], Object.getOwnPropertySymbols(rawJson));
    assertEq(expectedRawJsonValue, rawJson.rawJSON);
}

assertEq(true, Object.isFrozen(JSON.rawJSON('"shouldBeFrozen"')));
assertThrowsInstanceOf(() => JSON.rawJSON(), SyntaxError);
assertIsRawJson(JSON.rawJSON(1, 2), '1');

// isRawJSON
assertEq(false, JSON.isRawJSON());
assertEq(false, JSON.isRawJSON({}, {}));
assertEq(false, JSON.isRawJSON({}, JSON.rawJSON(2)));
assertEq(true, JSON.isRawJSON(JSON.rawJSON(1), JSON.rawJSON(2)));

if (typeof reportCompare == 'function')
    reportCompare(0, 0);