mirror of
https://github.com/RGBCube/serenity
synced 2025-06-01 06:18:12 +00:00
test-js: Use prettier and format all files
This commit is contained in:
parent
e532888242
commit
6d58c48c2f
248 changed files with 8291 additions and 7725 deletions
|
@ -1,9 +1,9 @@
|
|||
test("basic functionality", () => {
|
||||
let string = `{"var1":10,"var2":"hello","var3":{"nested":5}}`;
|
||||
let string = `{"var1":10,"var2":"hello","var3":{"nested":5}}`;
|
||||
|
||||
let object = JSON.parse(string, (key, value) => typeof value === "number" ? value * 2 : value);
|
||||
expect(object).toEqual({ var1: 20, var2: "hello", var3: { nested: 10 } });
|
||||
let object = JSON.parse(string, (key, value) => (typeof value === "number" ? value * 2 : value));
|
||||
expect(object).toEqual({ var1: 20, var2: "hello", var3: { nested: 10 } });
|
||||
|
||||
object = JSON.parse(string, (key, value) => typeof value === "number" ? undefined : value);
|
||||
expect(object).toEqual({ var2: "hello", var3: {} });
|
||||
object = JSON.parse(string, (key, value) => (typeof value === "number" ? undefined : value));
|
||||
expect(object).toEqual({ var2: "hello", var3: {} });
|
||||
});
|
||||
|
|
|
@ -1,37 +1,37 @@
|
|||
test("basic functionality", () => {
|
||||
expect(JSON.parse).toHaveLength(2);
|
||||
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" }],
|
||||
];
|
||||
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]);
|
||||
});
|
||||
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);
|
||||
});
|
||||
[
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,28 +1,30 @@
|
|||
test("basic functionality", () => {
|
||||
let o = {
|
||||
key1: "key1",
|
||||
key2: "key2",
|
||||
key3: "key3",
|
||||
};
|
||||
let o = {
|
||||
key1: "key1",
|
||||
key2: "key2",
|
||||
key3: "key3",
|
||||
};
|
||||
|
||||
Object.defineProperty(o, "defined", {
|
||||
enumerable: true,
|
||||
get() {
|
||||
o.prop = "prop";
|
||||
return "defined";
|
||||
},
|
||||
});
|
||||
Object.defineProperty(o, "defined", {
|
||||
enumerable: true,
|
||||
get() {
|
||||
o.prop = "prop";
|
||||
return "defined";
|
||||
},
|
||||
});
|
||||
|
||||
o.key4 = "key4";
|
||||
o.key4 = "key4";
|
||||
|
||||
o[2] = 2;
|
||||
o[0] = 0;
|
||||
o[1] = 1;
|
||||
o[2] = 2;
|
||||
o[0] = 0;
|
||||
o[1] = 1;
|
||||
|
||||
delete o.key1;
|
||||
delete o.key3;
|
||||
delete o.key1;
|
||||
delete o.key3;
|
||||
|
||||
o.key1 = "key1";
|
||||
o.key1 = "key1";
|
||||
|
||||
expect(JSON.stringify(o)).toBe('{"0":0,"1":1,"2":2,"key2":"key2","defined":"defined","key4":"key4","key1":"key1"}');
|
||||
expect(JSON.stringify(o)).toBe(
|
||||
'{"0":0,"1":1,"2":2,"key2":"key2","defined":"defined","key4":"key4","key1":"key1"}'
|
||||
);
|
||||
});
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
test("basic functionality", () => {
|
||||
let p = new Proxy([], {
|
||||
get(_, key) {
|
||||
if (key === "length")
|
||||
return 3;
|
||||
return Number(key);
|
||||
},
|
||||
});
|
||||
let p = new Proxy([], {
|
||||
get(_, key) {
|
||||
if (key === "length") return 3;
|
||||
return Number(key);
|
||||
},
|
||||
});
|
||||
|
||||
expect(JSON.stringify(p)).toBe("[0,1,2]");
|
||||
expect(JSON.stringify([[new Proxy(p, {})]])).toBe("[[[0,1,2]]]");
|
||||
expect(JSON.stringify(p)).toBe("[0,1,2]");
|
||||
expect(JSON.stringify([[new Proxy(p, {})]])).toBe("[[[0,1,2]]]");
|
||||
});
|
||||
|
|
|
@ -1,33 +1,38 @@
|
|||
test("basic functionality", () => {
|
||||
let o = {
|
||||
var1: "foo",
|
||||
var2: 42,
|
||||
arr: [1, 2, {
|
||||
nested: {
|
||||
hello: "world",
|
||||
},
|
||||
get x() { return 10; }
|
||||
}],
|
||||
obj: {
|
||||
subarr: [3],
|
||||
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;
|
||||
});
|
||||
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;
|
||||
});
|
||||
|
||||
expect(string).toBe('{"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"]);
|
||||
expect(string).toBe('{"var1":"foo","var2":42,"obj":{}}');
|
||||
string = JSON.stringify(o, ["var1", "var1", "var2", "obj"]);
|
||||
expect(string).toBe('{"var1":"foo","var2":42,"obj":{}}');
|
||||
|
||||
string = JSON.stringify(o, ["var1", "var1", "var2", "obj", "subarr"]);
|
||||
expect(string).toBe('{"var1":"foo","var2":42,"obj":{"subarr":[3]}}');
|
||||
string = JSON.stringify(o, ["var1", "var1", "var2", "obj", "subarr"]);
|
||||
expect(string).toBe('{"var1":"foo","var2":42,"obj":{"subarr":[3]}}');
|
||||
});
|
||||
|
|
|
@ -1,17 +1,20 @@
|
|||
test("basic functionality", () => {
|
||||
let o = {
|
||||
foo: 1,
|
||||
bar: "baz",
|
||||
qux: {
|
||||
get x() { return 10; },
|
||||
y() { return 20; },
|
||||
arr: [1, 2, 3],
|
||||
}
|
||||
};
|
||||
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 =
|
||||
`{
|
||||
let string = JSON.stringify(o, null, 4);
|
||||
let expected = `{
|
||||
"foo": 1,
|
||||
"bar": "baz",
|
||||
"qux": {
|
||||
|
@ -24,11 +27,10 @@ test("basic functionality", () => {
|
|||
}
|
||||
}`;
|
||||
|
||||
expect(string).toBe(expected);
|
||||
expect(string).toBe(expected);
|
||||
|
||||
string = JSON.stringify(o, null, "abcd");
|
||||
expected =
|
||||
`{
|
||||
string = JSON.stringify(o, null, "abcd");
|
||||
expected = `{
|
||||
abcd"foo": 1,
|
||||
abcd"bar": "baz",
|
||||
abcd"qux": {
|
||||
|
@ -41,5 +43,5 @@ abcdabcd]
|
|||
abcd}
|
||||
}`;
|
||||
|
||||
expect(string).toBe(expected);
|
||||
expect(string).toBe(expected);
|
||||
});
|
||||
|
|
|
@ -1,71 +1,71 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length", () => {
|
||||
expect(JSON.stringify).toHaveLength(3);
|
||||
});
|
||||
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("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("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 non-enumerable properties", () => {
|
||||
let o = { foo: "bar" };
|
||||
Object.defineProperty(o, "baz", { value: "qux", enumerable: false });
|
||||
expect(JSON.stringify(o)).toBe('{"foo":"bar"}');
|
||||
});
|
||||
});
|
||||
|
||||
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");
|
||||
});
|
||||
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");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue