mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 07:18:11 +00:00
LibJS: Convert most builtin tests to new system
This commit is contained in:
parent
46581773c1
commit
3f97d75778
107 changed files with 2031 additions and 2243 deletions
|
@ -1,71 +1,71 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(JSON.stringify.length === 3);
|
||||
|
||||
assertThrowsError(() => {
|
||||
JSON.stringify(5n);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Cannot serialize BigInt value to JSON",
|
||||
describe("correct behavior", () => {
|
||||
test("length", () => {
|
||||
expect(JSON.stringify).toHaveLength(3);
|
||||
});
|
||||
|
||||
const properties = [
|
||||
[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"}'],
|
||||
test("basic functionality", () => {
|
||||
[
|
||||
{
|
||||
var1: 1,
|
||||
var2: 2,
|
||||
toJSON(key) {
|
||||
let o = this;
|
||||
o.var2 = 10;
|
||||
return o;
|
||||
}
|
||||
},
|
||||
'{"var1":1,"var2":10}',
|
||||
],
|
||||
];
|
||||
|
||||
properties.forEach(testCase => {
|
||||
assert(JSON.stringify(testCase[0]) === testCase[1]);
|
||||
[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]);
|
||||
});
|
||||
});
|
||||
|
||||
let bad1 = {};
|
||||
bad1.foo = bad1;
|
||||
let bad2 = [];
|
||||
bad2[5] = [[[bad2]]];
|
||||
test("ignores non-enumerable properties", () => {
|
||||
let o = { foo: "bar" };
|
||||
Object.defineProperty(o, "baz", { value: "qux", enumerable: false });
|
||||
expect(JSON.stringify(o)).toBe('{"foo":"bar"}');
|
||||
});
|
||||
});
|
||||
|
||||
let bad3a = { foo: "bar" };
|
||||
let bad3b = [1, 2, bad3a];
|
||||
bad3a.bad = bad3b;
|
||||
describe("errors", () => {
|
||||
test("cannot serialize BigInt", () => {
|
||||
expect(() => {
|
||||
JSON.stringify(5n);
|
||||
}).toThrow(TypeError, "Cannot serialize BigInt value to JSON");
|
||||
});
|
||||
|
||||
[bad1, bad2, bad3a].forEach(bad => assertThrowsError(() => {
|
||||
JSON.stringify(bad);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Cannot stringify circular object",
|
||||
}));
|
||||
test("cannot serialize circular structures", () => {
|
||||
let bad1 = {};
|
||||
bad1.foo = bad1;
|
||||
let bad2 = [];
|
||||
bad2[5] = [[[bad2]]];
|
||||
|
||||
let o = { foo: "bar" };
|
||||
Object.defineProperty(o, "baz", { value: "qux", enumerable: false });
|
||||
assert(JSON.stringify(o) === '{"foo":"bar"}');
|
||||
let bad3a = { foo: "bar" };
|
||||
let bad3b = [1, 2, bad3a];
|
||||
bad3a.bad = bad3b;
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
[bad1, bad2, bad3a].forEach(bad => {
|
||||
expect(() => {
|
||||
JSON.stringify(bad);
|
||||
}).toThrow(TypeError, "Cannot stringify circular object");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue