mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-04-27 02:05:07 +02:00
Everywhere: Hoist the Libraries folder to the top-level
This commit is contained in:
committed by
Andreas Kling
parent
950e819ee7
commit
93712b24bf
Notes:
github-actions[bot]
2024-11-10 11:51:52 +00:00
Author: https://github.com/trflynn89 Commit: https://github.com/LadybirdBrowser/ladybird/commit/93712b24bf2 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2256 Reviewed-by: https://github.com/sideshowbarker
99
Libraries/LibJS/Tests/builtins/JSON/JSON.stringify.js
Normal file
99
Libraries/LibJS/Tests/builtins/JSON/JSON.stringify.js
Normal file
@@ -0,0 +1,99 @@
|
||||
describe("correct behavior", () => {
|
||||
test("length", () => {
|
||||
expect(JSON.stringify).toHaveLength(3);
|
||||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
[
|
||||
[5, "5"],
|
||||
[undefined, undefined],
|
||||
[null, "null"],
|
||||
[NaN, "null"],
|
||||
[-NaN, "null"],
|
||||
[Infinity, "null"],
|
||||
[-Infinity, "null"],
|
||||
[true, "true"],
|
||||
[false, "false"],
|
||||
["test", '"test"'],
|
||||
[new Number(5), "5"],
|
||||
[new Boolean(false), "false"],
|
||||
[new String("test"), '"test"'],
|
||||
[() => {}, undefined],
|
||||
[[1, 2, "foo"], '[1,2,"foo"]'],
|
||||
[{ foo: 1, bar: "baz", qux() {} }, '{"foo":1,"bar":"baz"}'],
|
||||
[
|
||||
{
|
||||
var1: 1,
|
||||
var2: 2,
|
||||
toJSON(key) {
|
||||
let o = this;
|
||||
o.var2 = 10;
|
||||
return o;
|
||||
},
|
||||
},
|
||||
'{"var1":1,"var2":10}',
|
||||
],
|
||||
].forEach(testCase => {
|
||||
expect(JSON.stringify(testCase[0])).toEqual(testCase[1]);
|
||||
});
|
||||
});
|
||||
|
||||
test("serialize BigInt with a toJSON property", () => {
|
||||
Object.defineProperty(BigInt.prototype, "toJSON", {
|
||||
configurable: true, // Allows deleting this property at the end of this test case.
|
||||
get() {
|
||||
"use strict";
|
||||
return () => typeof this;
|
||||
},
|
||||
});
|
||||
|
||||
expect(JSON.stringify(1n)).toBe('"bigint"');
|
||||
delete BigInt.prototype.toJSON;
|
||||
});
|
||||
|
||||
test("ignores non-enumerable properties", () => {
|
||||
let o = { foo: "bar" };
|
||||
Object.defineProperty(o, "baz", { value: "qux", enumerable: false });
|
||||
expect(JSON.stringify(o)).toBe('{"foo":"bar"}');
|
||||
});
|
||||
|
||||
test("ignores symbol properties", () => {
|
||||
let o = { foo: "bar" };
|
||||
let sym = Symbol("baz");
|
||||
o[sym] = "qux";
|
||||
expect(JSON.stringify(o)).toBe('{"foo":"bar"}');
|
||||
});
|
||||
|
||||
test("escape surrogate codepoints in strings", () => {
|
||||
expect(JSON.stringify("\ud83d\ude04")).toBe('"😄"');
|
||||
expect(JSON.stringify("\ud83d")).toBe('"\\ud83d"');
|
||||
expect(JSON.stringify("\ude04")).toBe('"\\ude04"');
|
||||
expect(JSON.stringify("\ud83d\ud83d\ude04\ud83d\ude04\ude04")).toBe('"\\ud83d😄😄\\ude04"');
|
||||
expect(JSON.stringify("\ude04\ud83d\ude04\ud83d\ude04\ud83d")).toBe('"\\ude04😄😄\\ud83d"');
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("cannot serialize BigInt", () => {
|
||||
expect(() => {
|
||||
JSON.stringify(5n);
|
||||
}).toThrow(TypeError, "Cannot serialize BigInt value to JSON");
|
||||
});
|
||||
|
||||
test("cannot serialize circular structures", () => {
|
||||
let bad1 = {};
|
||||
bad1.foo = bad1;
|
||||
let bad2 = [];
|
||||
bad2[5] = [[[bad2]]];
|
||||
|
||||
let bad3a = { foo: "bar" };
|
||||
let bad3b = [1, 2, bad3a];
|
||||
bad3a.bad = bad3b;
|
||||
|
||||
[bad1, bad2, bad3a].forEach(bad => {
|
||||
expect(() => {
|
||||
JSON.stringify(bad);
|
||||
}).toThrow(TypeError, "Cannot stringify circular object");
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user