ladybird/Userland/Libraries/LibJS/Tests/builtins/JSON/JSON.parse.js
ForLoveOfCats 71ab8fb757 LibJS: Add in-tree test for Json parsing of negative zeros
This mirrors the cases in `built-ins/JSON/parse/text-negative-zero` in
test262
2022-01-19 21:51:09 +00:00

45 lines
1 KiB
JavaScript

test("basic functionality", () => {
expect(JSON.parse).toHaveLength(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 => {
expect(JSON.parse(testCase[0])).toEqual(testCase[1]);
});
});
test("syntax errors", () => {
[
undefined,
NaN,
-NaN,
Infinity,
-Infinity,
'{ "foo" }',
'{ foo: "bar" }',
"[1,2,3,]",
"[1,2,3, ]",
'{ "foo": "bar",}',
'{ "foo": "bar", }',
].forEach(test => {
expect(() => {
JSON.parse(test);
}).toThrow(SyntaxError);
});
});
test("negative zero", () => {
["-0", " \n-0", "-0 \t", "\n\t -0\n ", "-0.0"].forEach(testCase => {
expect(JSON.parse(testCase)).toEqual(-0.0);
});
expect(JSON.parse(-0)).toEqual(0);
});