1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 06:57:44 +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,42 +1,44 @@
load("test-common.js");
try {
assert(Function.length === 1);
assert(Function.name === "Function");
assert(Function.prototype.length === 0);
assert(Function.prototype.name === "");
assert(typeof Function() === "function");
assert(typeof new Function() === "function");
assert(Function()() === undefined);
assert(new Function()() === undefined);
assert(Function("return 42")() === 42);
assert(new Function("return 42")() === 42);
assert(new Function("foo", "return foo")(42) === 42);
assert(new Function("foo,bar", "return foo + bar")(1, 2) === 3);
assert(new Function("foo", "bar", "return foo + bar")(1, 2) === 3);
assert(new Function("foo", "bar,baz", "return foo + bar + baz")(1, 2, 3) === 6);
assert(new Function("foo", "bar", "baz", "return foo + bar + baz")(1, 2, 3) === 6);
assert(new Function("foo", "if (foo) { return 42; } else { return 'bar'; }")(true) === 42);
assert(new Function("foo", "if (foo) { return 42; } else { return 'bar'; }")(false) === "bar");
assert(new Function("return typeof Function()")() === "function");
assert(new Function("x", "return function (y) { return x + y };")(1)(2) === 3);
assert(new Function().name === "anonymous");
assert(new Function().toString() === "function anonymous() {\n ???\n}");
assertThrowsError(() => {
new Function("[");
}, {
error: SyntaxError,
// 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.
message: "Unexpected token CurlyClose. Expected BracketClose (line: 1, column: 26)"
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("");
});
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e.message);
}
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);
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)");
});
});