1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 21:47:46 +00:00

LibJS: Convert most builtin tests to new system

This commit is contained in:
Matthew Olsson 2020-07-04 10:09:48 -07:00 committed by Andreas Kling
parent 46581773c1
commit 3f97d75778
107 changed files with 2031 additions and 2243 deletions

View file

@ -1,15 +1,9 @@
load("test-common.js");
try {
test("basic functionality", () => {
let string = `{"var1":10,"var2":"hello","var3":{"nested":5}}`;
let object = JSON.parse(string, (key, value) => typeof value === "number" ? value * 2 : value);
assertDeepEquals(object, { var1: 20, var2: "hello", var3: { nested: 10 } });
expect(object).toEqual({ var1: 20, var2: "hello", var3: { nested: 10 } });
object = JSON.parse(string, (key, value) => typeof value === "number" ? undefined : value);
assertDeepEquals(object, { var2: "hello", var3: {} });
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}
expect(object).toEqual({ var2: "hello", var3: {} });
});

View file

@ -1,7 +1,5 @@
load("test-common.js");
try {
assert(JSON.parse.length === 2);
test("basic functionality", () => {
expect(JSON.parse).toHaveLength(2);
const properties = [
["5", 5],
@ -14,10 +12,12 @@ try {
];
properties.forEach(testCase => {
assertDeepEquals(JSON.parse(testCase[0]), testCase[1]);
expect(JSON.parse(testCase[0])).toEqual(testCase[1]);
});
});
let syntaxErrors = [
test("syntax errors", () => {
[
undefined,
NaN,
-NaN,
@ -29,15 +29,9 @@ try {
"[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);
}
].forEach(test => {
expect(() => {
JSON.parse(test);
}).toThrow(SyntaxError);
});
});

View file

@ -1,8 +1,4 @@
load("test-common.js");
try {
assert(JSON.stringify.length === 3);
test("basic functionality", () => {
let o = {
key1: "key1",
key2: "key2",
@ -28,9 +24,5 @@ try {
o.key1 = "key1";
assert(JSON.stringify(o) === '{"0":0,"1":1,"2":2,"key2":"key2","defined":"defined","key4":"key4","key1":"key1"}');
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}
expect(JSON.stringify(o)).toBe('{"0":0,"1":1,"2":2,"key2":"key2","defined":"defined","key4":"key4","key1":"key1"}');
});

View file

@ -1,6 +1,4 @@
load("test-common.js");
try {
test("basic functionality", () => {
let p = new Proxy([], {
get(_, key) {
if (key === "length")
@ -9,10 +7,6 @@ try {
},
});
assert(JSON.stringify(p) === "[0,1,2]");
assert(JSON.stringify([[new Proxy(p, {})]]) === "[[[0,1,2]]]");
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}
expect(JSON.stringify(p)).toBe("[0,1,2]");
expect(JSON.stringify([[new Proxy(p, {})]])).toBe("[[[0,1,2]]]");
});

View file

@ -1,6 +1,4 @@
load("test-common.js");
try {
test("basic functionality", () => {
let o = {
var1: "foo",
var2: 42,
@ -25,15 +23,11 @@ try {
return value;
});
assert(string === '{"var1":"foo","var2":42,"arr":[1,2,{"nested":{},"x":20}],"obj":{"subarr":[3,4,5]}}');
expect(string).toBe('{"var1":"foo","var2":42,"arr":[1,2,{"nested":{},"x":20}],"obj":{"subarr":[3,4,5]}}');
string = JSON.stringify(o, ["var1", "var1", "var2", "obj"]);
assert(string == '{"var1":"foo","var2":42,"obj":{}}');
expect(string).toBe('{"var1":"foo","var2":42,"obj":{}}');
string = JSON.stringify(o, ["var1", "var1", "var2", "obj", "subarr"]);
assert(string == '{"var1":"foo","var2":42,"obj":{"subarr":[3]}}');
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}
expect(string).toBe('{"var1":"foo","var2":42,"obj":{"subarr":[3]}}');
});

View file

@ -1,6 +1,4 @@
load("test-common.js");
try {
test("basic functionality", () => {
let o = {
foo: 1,
bar: "baz",
@ -26,7 +24,7 @@ try {
}
}`;
assert(string === expected);
expect(string).toBe(expected);
string = JSON.stringify(o, null, "abcd");
expected =
@ -43,9 +41,5 @@ abcdabcd]
abcd}
}`;
assert(string === expected);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}
expect(string).toBe(expected);
});

View file

@ -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");
});
});
});