summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Tests/JSON.parse.js
blob: 40f79cee60b309536972d77ddff941b047bf85c1 (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
load("test-common.js");

try {
    assert(JSON.parse.length === 2);

    const properties = [
        ["5", 5],
        ["null", null],
        ["true", true],
        ["false", false],
        ['"test"', "test"],
        ['[1,2,"foo"]', [1, 2, "foo"]],
        ['{"foo":1,"bar":"baz"}', { foo: 1, bar: "baz" }],
    ];

    properties.forEach(testCase => {
        assertDeepEquals(JSON.parse(testCase[0]), testCase[1]);
    });

    let syntaxErrors = [
        undefined,
        NaN,
        -NaN,
        Infinity,
        -Infinity,
        '{ "foo" }',
        '{ foo: "bar" }',
        "[1,2,3,]",
        "[1,2,3, ]",
        '{ "foo": "bar",}',
        '{ "foo": "bar", }',
    ];

    syntaxErrors.forEach(error => assertThrowsError(() => {
        JSON.parse(error);
    }, {
        error: SyntaxError,
    }));

    console.log("PASS");
} catch (e) {
    console.log("FAIL: " + e);
}