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

test-js: Use prettier and format all files

This commit is contained in:
Matthew Olsson 2020-07-05 09:27:00 -07:00 committed by Andreas Kling
parent e532888242
commit 6d58c48c2f
248 changed files with 8291 additions and 7725 deletions

View file

@ -1,44 +1,49 @@
describe("correct behavior", () => {
test("constructor properties", () => {
expect(Function).toHaveLength(1);
expect(Function.name).toBe("Function");
expect(Function.prototype).toHaveLength(0);
expect(Function.prototype.name).toBe("");
});
test("constructor properties", () => {
expect(Function).toHaveLength(1);
expect(Function.name).toBe("Function");
expect(Function.prototype).toHaveLength(0);
expect(Function.prototype.name).toBe("");
});
test("typeof", () => {
expect(typeof Function()).toBe("function");
expect(typeof new Function()).toBe("function");
});
test("typeof", () => {
expect(typeof Function()).toBe("function");
expect(typeof new Function()).toBe("function");
});
test("basic functionality", () => {
expect(Function()()).toBe(undefined);
expect(new Function()()).toBe(undefined);
expect(Function("return 42")()).toBe(42);
expect(new Function("return 42")()).toBe(42);
expect(new Function("foo", "return foo")(42)).toBe(42);
expect(new Function("foo,bar", "return foo + bar")(1, 2)).toBe(3);
expect(new Function("foo", "bar", "return foo + bar")(1, 2)).toBe(3);
expect(new Function("foo", "bar,baz", "return foo + bar + baz")(1, 2, 3)).toBe(6);
expect(new Function("foo", "bar", "baz", "return foo + bar + baz")(1, 2, 3)).toBe(6);
expect(new Function("foo", "if (foo) { return 42; } else { return 'bar'; }")(true)).toBe(42);
expect(new Function("foo", "if (foo) { return 42; } else { return 'bar'; }")(false)).toBe("bar");
expect(new Function("return typeof Function()")()).toBe("function");
expect(new Function("x", "return function (y) { return x + y };")(1)(2)).toBe(3);
test("basic functionality", () => {
expect(Function()()).toBeUndefined();
expect(new Function()()).toBeUndefined();
expect(Function("return 42")()).toBe(42);
expect(new Function("return 42")()).toBe(42);
expect(new Function("foo", "return foo")(42)).toBe(42);
expect(new Function("foo,bar", "return foo + bar")(1, 2)).toBe(3);
expect(new Function("foo", "bar", "return foo + bar")(1, 2)).toBe(3);
expect(new Function("foo", "bar,baz", "return foo + bar + baz")(1, 2, 3)).toBe(6);
expect(new Function("foo", "bar", "baz", "return foo + bar + baz")(1, 2, 3)).toBe(6);
expect(new Function("foo", "if (foo) { return 42; } else { return 'bar'; }")(true)).toBe(42);
expect(new Function("foo", "if (foo) { return 42; } else { return 'bar'; }")(false)).toBe(
"bar"
);
expect(new Function("return typeof Function()")()).toBe("function");
expect(new Function("x", "return function (y) { return x + y };")(1)(2)).toBe(3);
expect(new Function().name).toBe("anonymous");
expect(new Function().toString()).toBe("function anonymous() {\n ???\n}");
});
expect(new Function().name).toBe("anonymous");
expect(new Function().toString()).toBe("function anonymous() {\n ???\n}");
});
});
describe("errors", () => {
test("syntax error", () => {
expect(() => {
new Function("[");
})
// This might be confusing at first but keep in mind it's actually parsing
// function anonymous() { [ }
// This is in line with what other engines are reporting.
.toThrowWithMessage(SyntaxError, "Unexpected token CurlyClose. Expected BracketClose (line: 1, column: 26)");
});
test("syntax error", () => {
expect(() => {
new Function("[");
})
// This might be confusing at first but keep in mind it's actually parsing
// function anonymous() { [ }
// This is in line with what other engines are reporting.
.toThrowWithMessage(
SyntaxError,
"Unexpected token CurlyClose. Expected BracketClose (line: 1, column: 26)"
);
});
});

View file

@ -1,49 +1,51 @@
test("basic functionality", () => {
function Foo(arg) {
this.foo = arg;
}
function Bar(arg) {
this.bar = arg;
}
function FooBar(arg) {
Foo.apply(this, [arg]);
Bar.apply(this, [arg]);
}
function FooBarBaz(arg) {
Foo.apply(this, [arg]);
Bar.apply(this, [arg]);
this.baz = arg;
}
function Foo(arg) {
this.foo = arg;
}
function Bar(arg) {
this.bar = arg;
}
function FooBar(arg) {
Foo.apply(this, [arg]);
Bar.apply(this, [arg]);
}
function FooBarBaz(arg) {
Foo.apply(this, [arg]);
Bar.apply(this, [arg]);
this.baz = arg;
}
expect(Function.prototype.apply).toHaveLength(2);
expect(Function.prototype.apply).toHaveLength(2);
var foo = new Foo("test");
expect(foo.foo).toBe("test");
expect(foo.bar).toBeUndefined();
expect(foo.baz).toBeUndefined();
var foo = new Foo("test");
expect(foo.foo).toBe("test");
expect(foo.bar).toBeUndefined();
expect(foo.baz).toBeUndefined();
var bar = new Bar("test");
expect(bar.foo).toBeUndefined();
expect(bar.bar).toBe("test");
expect(bar.baz).toBeUndefined();
var bar = new Bar("test");
expect(bar.foo).toBeUndefined();
expect(bar.bar).toBe("test");
expect(bar.baz).toBeUndefined();
var foobar = new FooBar("test");
expect(foobar.foo).toBe("test");
expect(foobar.bar).toBe("test");
expect(foobar.baz).toBeUndefined();
var foobar = new FooBar("test");
expect(foobar.foo).toBe("test");
expect(foobar.bar).toBe("test");
expect(foobar.baz).toBeUndefined();
var foobarbaz = new FooBarBaz("test");
expect(foobarbaz.foo).toBe("test");
expect(foobarbaz.bar).toBe("test");
expect(foobarbaz.baz).toBe("test");
var foobarbaz = new FooBarBaz("test");
expect(foobarbaz.foo).toBe("test");
expect(foobarbaz.bar).toBe("test");
expect(foobarbaz.baz).toBe("test");
expect(Math.abs.apply(null, [-1])).toBe(1);
expect(Math.abs.apply(null, [-1])).toBe(1);
var add = (x, y) => x + y;
expect(add.apply(null, [1, 2])).toBe(3);
var add = (x, y) => x + y;
expect(add.apply(null, [1, 2])).toBe(3);
var multiply = function (x, y) { return x * y; };
expect(multiply.apply(null, [3, 4])).toBe(12);
var multiply = function (x, y) {
return x * y;
};
expect(multiply.apply(null, [3, 4])).toBe(12);
expect((() => this).apply("foo")).toBe(globalThis);
expect((() => this).apply("foo")).toBe(globalThis);
});

View file

@ -1,138 +1,149 @@
describe("basic behavior", () => {
test("basic binding", () => {
expect(Function.prototype.bind.length).toBe(1);
test("basic binding", () => {
expect(Function.prototype.bind).toHaveLength(1);
var charAt = String.prototype.charAt.bind("bar");
expect(charAt(0) + charAt(1) + charAt(2)).toBe("bar");
var charAt = String.prototype.charAt.bind("bar");
expect(charAt(0) + charAt(1) + charAt(2)).toBe("bar");
function getB() {
return this.toUpperCase().charAt(0);
}
expect(getB.bind("bar")()).toBe("B");
});
function getB() {
return this.toUpperCase().charAt(0);
}
expect(getB.bind("bar")()).toBe("B");
});
test("bound functions work with array functions", () => {
var Make3 = Number.bind(null, 3);
expect([55].map(Make3)[0]).toBe(3);
test("bound functions work with array functions", () => {
var Make3 = Number.bind(null, 3);
expect([55].map(Make3)[0]).toBe(3);
var MakeTrue = Boolean.bind(null, true);
var MakeTrue = Boolean.bind(null, true);
expect([1, 2, 3].filter(MakeTrue).length).toBe(3);
expect([1, 2, 3].reduce((function (acc, x) { return acc + x }).bind(null, 4, 5))).toBe(9);
expect([1, 2, 3].reduce((function (acc, x) { return acc + x + this; }).bind(3))).toBe(12);
});
expect([1, 2, 3].filter(MakeTrue)).toHaveLength(3);
expect(
[1, 2, 3].reduce(
function (acc, x) {
return acc + x;
}.bind(null, 4, 5)
)
).toBe(9);
expect(
[1, 2, 3].reduce(
function (acc, x) {
return acc + x + this;
}.bind(3)
)
).toBe(12);
});
});
describe("bound function arguments", () => {
function sum(a, b, c) {
return a + b + c;
}
var boundSum = sum.bind(null, 10, 5);
function sum(a, b, c) {
return a + b + c;
}
var boundSum = sum.bind(null, 10, 5);
test("arguments are bound to the function", () => {
expect(boundSum()).toBeNaN();
expect(boundSum(5)).toBe(20);
expect(boundSum(5, 6, 7)).toBe(20);
});
test("arguments are bound to the function", () => {
expect(boundSum()).toBeNaN();
expect(boundSum(5)).toBe(20);
expect(boundSum(5, 6, 7)).toBe(20);
});
test("arguments are appended to a BoundFunction's bound arguments", () => {
expect(boundSum.bind(null, 5)()).toBe(20);
});
test("arguments are appended to a BoundFunction's bound arguments", () => {
expect(boundSum.bind(null, 5)()).toBe(20);
});
test("binding a constructor's arguments", () => {
var Make5 = Number.bind(null, 5);
expect(Make5()).toBe(5);
expect(new Make5().valueOf()).toBe(5);
});
test("binding a constructor's arguments", () => {
var Make5 = Number.bind(null, 5);
expect(Make5()).toBe(5);
expect(new Make5().valueOf()).toBe(5);
});
test("length property", () => {
expect(sum).toHaveLength(3);
expect(boundSum).toHaveLength(1);
expect(boundSum.bind(null, 5)).toHaveLength(0);
expect(boundSum.bind(null, 5, 6, 7, 8)).toHaveLength(0);
});
})
test("length property", () => {
expect(sum).toHaveLength(3);
expect(boundSum).toHaveLength(1);
expect(boundSum.bind(null, 5)).toHaveLength(0);
expect(boundSum.bind(null, 5, 6, 7, 8)).toHaveLength(0);
});
});
describe("bound function |this|", () => {
function identity() {
return this;
function identity() {
return this;
}
test("captures global object as |this| if |this| is null or undefined", () => {
expect(identity.bind()()).toBe(globalThis);
expect(identity.bind(null)()).toBe(globalThis);
expect(identity.bind(undefined)()).toBe(globalThis);
function Foo() {
expect(identity.bind()()).toBe(globalThis);
expect(identity.bind(this)()).toBe(this);
}
new Foo();
});
test("does not capture global object as |this| if |this| is null or undefined in strict mode", () => {
"use strict";
function strictIdentity() {
return this;
}
test("captures global object as |this| if |this| is null or undefined", () => {
expect(identity.bind()()).toBe(globalThis);
expect(identity.bind(null)()).toBe(globalThis);
expect(identity.bind(undefined)()).toBe(globalThis);
expect(strictIdentity.bind()()).toBeUndefined();
expect(strictIdentity.bind(null)()).toBeNull();
expect(strictIdentity.bind(undefined)()).toBeUndefined();
});
function Foo() {
expect(identity.bind()()).toBe(globalThis);
expect(identity.bind(this)()).toBe(this);
}
new Foo();
});
test("primitive |this| values are converted to objects", () => {
expect(identity.bind("foo")()).toBeInstanceOf(String);
expect(identity.bind(123)()).toBeInstanceOf(Number);
expect(identity.bind(true)()).toBeInstanceOf(Boolean);
});
test("does not capture global object as |this| if |this| is null or undefined in strict mode", () => {
"use strict";
test("bound functions retain |this| values passed to them", () => {
var obj = { foo: "bar" };
expect(identity.bind(obj)()).toBe(obj);
});
function strictIdentity() {
return this;
}
test("bound |this| cannot be changed after being set", () => {
expect(identity.bind("foo").bind(123)()).toBeInstanceOf(String);
});
expect(strictIdentity.bind()()).toBeUndefined();
expect(strictIdentity.bind(null)()).toBeNull();
expect(strictIdentity.bind(undefined)()).toBeUndefined();
});
test("primitive |this| values are converted to objects", () => {
expect(identity.bind("foo")()).toBeInstanceOf(String);
expect(identity.bind(123)()).toBeInstanceOf(Number);
expect(identity.bind(true)()).toBeInstanceOf(Boolean);
});
test("bound functions retain |this| values passed to them", () => {
var obj = { foo: "bar" };
expect(identity.bind(obj)()).toBe(obj);
});
test("bound |this| cannot be changed after being set", () => {
expect(identity.bind("foo").bind(123)()).toBeInstanceOf(String);
});
test("arrow functions cannot be bound", () => {
expect((() => this).bind("foo")()).toBe(globalThis)
});
})
test("arrow functions cannot be bound", () => {
expect((() => this).bind("foo")()).toBe(globalThis);
});
});
describe("bound function constructors", () => {
function Bar() {
this.x = 3;
this.y = 4;
}
function Bar() {
this.x = 3;
this.y = 4;
}
Bar.prototype.baz = "baz";
var BoundBar = Bar.bind({ u: 5, v: 6 });
var bar = new BoundBar();
Bar.prototype.baz = "baz";
var BoundBar = Bar.bind({ u: 5, v: 6 });
var bar = new BoundBar();
test("bound |this| value does not affect constructor", () => {
expect(bar.x).toBe(3);
expect(bar.y).toBe(4);
expect(typeof bar.u).toBe("undefined");
expect(typeof bar.v).toBe("undefined");
});
test("bound |this| value does not affect constructor", () => {
expect(bar.x).toBe(3);
expect(bar.y).toBe(4);
expect(typeof bar.u).toBe("undefined");
expect(typeof bar.v).toBe("undefined");
});
test("bound functions retain original prototype", () => {
expect(bar.baz).toBe("baz");
});
test("bound functions retain original prototype", () => {
expect(bar.baz).toBe("baz");
});
test("bound functions do not have a prototype property", () => {
expect(BoundBar).not.toHaveProperty("prototype");
});
test("bound functions do not have a prototype property", () => {
expect(BoundBar).not.toHaveProperty("prototype");
});
});
describe("errors", () => {
test("does not accept non-function values", () => {
expect(() => {
Function.prototype.bind.call("foo");
}).toThrowWithMessage(TypeError, "Not a Function object");
});
test("does not accept non-function values", () => {
expect(() => {
Function.prototype.bind.call("foo");
}).toThrowWithMessage(TypeError, "Not a Function object");
});
});

View file

@ -1,51 +1,53 @@
test("length", () => {
expect(Function.prototype.call).toHaveLength(1);
expect(Function.prototype.call).toHaveLength(1);
});
test("basic functionality", () => {
function Foo(arg) {
this.foo = arg;
}
function Bar(arg) {
this.bar = arg;
}
function FooBar(arg) {
Foo.call(this, arg);
Bar.call(this, arg);
}
function FooBarBaz(arg) {
Foo.call(this, arg);
Bar.call(this, arg);
this.baz = arg;
}
function Foo(arg) {
this.foo = arg;
}
function Bar(arg) {
this.bar = arg;
}
function FooBar(arg) {
Foo.call(this, arg);
Bar.call(this, arg);
}
function FooBarBaz(arg) {
Foo.call(this, arg);
Bar.call(this, arg);
this.baz = arg;
}
var foo = new Foo("test");
expect(foo.foo).toBe("test");
expect(foo.bar).toBeUndefined();
expect(foo.baz).toBeUndefined();
var foo = new Foo("test");
expect(foo.foo).toBe("test");
expect(foo.bar).toBeUndefined();
expect(foo.baz).toBeUndefined();
var bar = new Bar("test");
expect(bar.foo).toBeUndefined();
expect(bar.bar).toBe("test");
expect(bar.baz).toBeUndefined();
var bar = new Bar("test");
expect(bar.foo).toBeUndefined();
expect(bar.bar).toBe("test");
expect(bar.baz).toBeUndefined();
var foobar = new FooBar("test");
expect(foobar.foo).toBe("test");
expect(foobar.bar).toBe("test");
expect(foobar.baz).toBeUndefined();
var foobar = new FooBar("test");
expect(foobar.foo).toBe("test");
expect(foobar.bar).toBe("test");
expect(foobar.baz).toBeUndefined();
var foobarbaz = new FooBarBaz("test");
expect(foobarbaz.foo).toBe("test");
expect(foobarbaz.bar).toBe("test");
expect(foobarbaz.baz).toBe("test");
var foobarbaz = new FooBarBaz("test");
expect(foobarbaz.foo).toBe("test");
expect(foobarbaz.bar).toBe("test");
expect(foobarbaz.baz).toBe("test");
expect(Math.abs.call(null, -1)).toBe(1);
expect(Math.abs.call(null, -1)).toBe(1);
var add = (x, y) => x + y;
expect(add.call(null, 1, 2)).toBe(3);
var add = (x, y) => x + y;
expect(add.call(null, 1, 2)).toBe(3);
var multiply = function (x, y) { return x * y; };
expect(multiply.call(null, 3, 4)).toBe(12);
var multiply = function (x, y) {
return x * y;
};
expect(multiply.call(null, 3, 4)).toBe(12);
expect((() => this).call("foo")).toBe(globalThis);
expect((() => this).call("foo")).toBe(globalThis);
});

View file

@ -1,15 +1,17 @@
test("basic functionality", () => {
expect((function() {}).toString()).toBe("function () {\n ???\n}");
expect((function(foo) {}).toString()).toBe("function (foo) {\n ???\n}");
expect((function(foo, bar, baz) {}).toString()).toBe("function (foo, bar, baz) {\n ???\n}");
expect((function(foo, bar, baz) {
if (foo) {
return baz;
} else if (bar) {
return foo;
}
return bar + 42;
}).toString()).toBe("function (foo, bar, baz) {\n ???\n}");
expect(console.debug.toString()).toBe("function debug() {\n [NativeFunction]\n}");
expect(Function.toString()).toBe("function Function() {\n [FunctionConstructor]\n}");
expect(function () {}.toString()).toBe("function () {\n ???\n}");
expect(function (foo) {}.toString()).toBe("function (foo) {\n ???\n}");
expect(function (foo, bar, baz) {}.toString()).toBe("function (foo, bar, baz) {\n ???\n}");
expect(
function (foo, bar, baz) {
if (foo) {
return baz;
} else if (bar) {
return foo;
}
return bar + 42;
}.toString()
).toBe("function (foo, bar, baz) {\n ???\n}");
expect(console.debug.toString()).toBe("function debug() {\n [NativeFunction]\n}");
expect(Function.toString()).toBe("function Function() {\n [FunctionConstructor]\n}");
});