1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 03:57:44 +00:00

LibJS: Reorganize tests into subfolders

This commit is contained in:
Matthew Olsson 2020-07-02 16:09:21 -07:00 committed by Andreas Kling
parent 21064a1883
commit 4c48c9d69d
213 changed files with 10 additions and 2 deletions

View file

@ -0,0 +1,15 @@
load("test-common.js");
try {
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 } });
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);
}

View file

@ -0,0 +1,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);
}

View file

@ -0,0 +1,36 @@
load("test-common.js");
try {
assert(JSON.stringify.length === 3);
let o = {
key1: "key1",
key2: "key2",
key3: "key3",
};
Object.defineProperty(o, "defined", {
enumerable: true,
get() {
o.prop = "prop";
return "defined";
},
});
o.key4 = "key4";
o[2] = 2;
o[0] = 0;
o[1] = 1;
delete o.key1;
delete o.key3;
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);
}

View file

@ -0,0 +1,18 @@
load("test-common.js");
try {
let p = new Proxy([], {
get(_, key) {
if (key === "length")
return 3;
return Number(key);
},
});
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);
}

View file

@ -0,0 +1,39 @@
load("test-common.js");
try {
let o = {
var1: "foo",
var2: 42,
arr: [1, 2, {
nested: {
hello: "world",
},
get x() { return 10; }
}],
obj: {
subarr: [3],
},
};
let string = JSON.stringify(o, (key, value) => {
if (key === "hello")
return undefined;
if (value === 10)
return 20;
if (key === "subarr")
return [3, 4, 5];
return value;
});
assert(string === '{"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":{}}');
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);
}

View file

@ -0,0 +1,51 @@
load("test-common.js");
try {
let o = {
foo: 1,
bar: "baz",
qux: {
get x() { return 10; },
y() { return 20; },
arr: [1, 2, 3],
}
};
let string = JSON.stringify(o, null, 4);
let expected =
`{
"foo": 1,
"bar": "baz",
"qux": {
"x": 10,
"arr": [
1,
2,
3
]
}
}`;
assert(string === expected);
string = JSON.stringify(o, null, "abcd");
expected =
`{
abcd"foo": 1,
abcd"bar": "baz",
abcd"qux": {
abcdabcd"x": 10,
abcdabcd"arr": [
abcdabcdabcd1,
abcdabcdabcd2,
abcdabcdabcd3
abcdabcd]
abcd}
}`;
assert(string === expected);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}

View file

@ -0,0 +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",
});
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"}'],
[
{
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]);
});
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 => assertThrowsError(() => {
JSON.stringify(bad);
}, {
error: TypeError,
message: "Cannot stringify circular object",
}));
let o = { foo: "bar" };
Object.defineProperty(o, "baz", { value: "qux", enumerable: false });
assert(JSON.stringify(o) === '{"foo":"bar"}');
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}