mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:27:45 +00:00
LibJS: Reorganize tests into subfolders
This commit is contained in:
parent
21064a1883
commit
4c48c9d69d
213 changed files with 10 additions and 2 deletions
42
Libraries/LibJS/Tests/builtins/Function/Function.js
Normal file
42
Libraries/LibJS/Tests/builtins/Function/Function.js
Normal file
|
@ -0,0 +1,42 @@
|
|||
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)"
|
||||
});
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e.message);
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
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;
|
||||
}
|
||||
|
||||
assert(Function.prototype.apply.length === 2);
|
||||
|
||||
var foo = new Foo("test");
|
||||
assert(foo.foo === "test");
|
||||
assert(foo.bar === undefined);
|
||||
assert(foo.baz === undefined);
|
||||
|
||||
var bar = new Bar("test");
|
||||
assert(bar.foo === undefined);
|
||||
assert(bar.bar === "test");
|
||||
assert(bar.baz === undefined);
|
||||
|
||||
var foobar = new FooBar("test");
|
||||
assert(foobar.foo === "test");
|
||||
assert(foobar.bar === "test");
|
||||
assert(foobar.baz === undefined);
|
||||
|
||||
var foobarbaz = new FooBarBaz("test");
|
||||
assert(foobarbaz.foo === "test");
|
||||
assert(foobarbaz.bar === "test");
|
||||
assert(foobarbaz.baz === "test");
|
||||
|
||||
assert(Math.abs.apply(null, [-1]) === 1);
|
||||
|
||||
var add = (x, y) => x + y;
|
||||
assert(add.apply(null, [1, 2]) === 3);
|
||||
|
||||
var multiply = function (x, y) { return x * y; };
|
||||
assert(multiply.apply(null, [3, 4]) === 12);
|
||||
|
||||
assert((() => this).apply("foo") === globalThis);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,124 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Function.prototype.bind.length === 1);
|
||||
|
||||
var charAt = String.prototype.charAt.bind("bar");
|
||||
assert(charAt(0) + charAt(1) + charAt(2) === "bar");
|
||||
|
||||
function getB() {
|
||||
return this.toUpperCase().charAt(0);
|
||||
}
|
||||
assert(getB.bind("bar")() === "B");
|
||||
|
||||
// Bound functions should work with array functions
|
||||
var Make3 = Number.bind(null, 3);
|
||||
assert([55].map(Make3)[0] === 3);
|
||||
|
||||
var MakeTrue = Boolean.bind(null, true);
|
||||
assert([1, 2, 3].filter(MakeTrue).length === 3);
|
||||
|
||||
assert([1, 2, 3].reduce((function (acc, x) { return acc + x }).bind(null, 4, 5)) === 9);
|
||||
|
||||
assert([1, 2, 3].reduce((function (acc, x) { return acc + x + this; }).bind(3)) === 12);
|
||||
|
||||
function sum(a, b, c) {
|
||||
return a + b + c;
|
||||
}
|
||||
|
||||
// Arguments should be able to be bound to a function.
|
||||
var boundSum = sum.bind(null, 10, 5);
|
||||
assert(isNaN(boundSum()));
|
||||
|
||||
assert(boundSum(5) === 20);
|
||||
assert(boundSum(5, 6, 7) === 20);
|
||||
|
||||
// Arguments should be appended to a BoundFunction's bound arguments.
|
||||
assert(boundSum.bind(null, 5)() === 20);
|
||||
|
||||
// A BoundFunction's length property should be adjusted based on the number
|
||||
// of bound arguments.
|
||||
assert(sum.length === 3);
|
||||
assert(boundSum.length === 1);
|
||||
assert(boundSum.bind(null, 5).length === 0);
|
||||
assert(boundSum.bind(null, 5, 6, 7, 8).length === 0);
|
||||
|
||||
function identity() {
|
||||
return this;
|
||||
}
|
||||
|
||||
// It should capture the global object if the |this| value is null or undefined.
|
||||
assert(identity.bind()() === globalThis);
|
||||
assert(identity.bind(null)() === globalThis);
|
||||
assert(identity.bind(undefined)() === globalThis);
|
||||
|
||||
function Foo() {
|
||||
assert(identity.bind()() === globalThis);
|
||||
assert(identity.bind(this)() === this);
|
||||
}
|
||||
new Foo();
|
||||
|
||||
// Primitive |this| values should be converted to objects.
|
||||
assert(identity.bind("foo")() instanceof String);
|
||||
assert(identity.bind(123)() instanceof Number);
|
||||
assert(identity.bind(true)() instanceof Boolean);
|
||||
|
||||
// It should retain |this| values passed to it.
|
||||
var obj = { foo: "bar" };
|
||||
|
||||
assert(identity.bind(obj)() === obj);
|
||||
|
||||
// The bound |this| can not be changed once set
|
||||
assert(identity.bind("foo").bind(123)() instanceof String);
|
||||
|
||||
// The bound |this| value should have no effect on a constructor.
|
||||
function Bar() {
|
||||
this.x = 3;
|
||||
this.y = 4;
|
||||
}
|
||||
Bar.prototype.baz = "baz";
|
||||
|
||||
var BoundBar = Bar.bind({ u: 5, v: 6 });
|
||||
|
||||
var bar = new BoundBar();
|
||||
assert(bar.x === 3);
|
||||
assert(bar.y === 4);
|
||||
assert(typeof bar.u === "undefined");
|
||||
assert(typeof bar.v === "undefined");
|
||||
// Objects constructed from BoundFunctions should retain the prototype of the original function.
|
||||
assert(bar.baz === "baz");
|
||||
// BoundFunctions should not have a prototype property.
|
||||
assert(typeof BoundBar.prototype === "undefined");
|
||||
|
||||
// Function.prototype.bind should not accept non-function values.
|
||||
assertThrowsError(() => {
|
||||
Function.prototype.bind.call("foo");
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Not a Function object"
|
||||
});
|
||||
|
||||
// A constructor's arguments should be able to be bound.
|
||||
var Make5 = Number.bind(null, 5);
|
||||
assert(Make5() === 5);
|
||||
assert(new Make5().valueOf() === 5);
|
||||
|
||||
// Null or undefined should be passed through as a |this| value in strict mode.
|
||||
(() => {
|
||||
"use strict";
|
||||
function strictIdentity() {
|
||||
return this;
|
||||
}
|
||||
|
||||
assert(strictIdentity.bind()() === undefined);
|
||||
assert(strictIdentity.bind(null)() === null);
|
||||
assert(strictIdentity.bind(undefined)() === undefined);
|
||||
})();
|
||||
|
||||
// Arrow functions can not have their |this| value set.
|
||||
assert((() => this).bind("foo")() === globalThis)
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
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;
|
||||
}
|
||||
|
||||
assert(Function.prototype.call.length === 1);
|
||||
|
||||
var foo = new Foo("test");
|
||||
assert(foo.foo === "test");
|
||||
assert(foo.bar === undefined);
|
||||
assert(foo.baz === undefined);
|
||||
|
||||
var bar = new Bar("test");
|
||||
assert(bar.foo === undefined);
|
||||
assert(bar.bar === "test");
|
||||
assert(bar.baz === undefined);
|
||||
|
||||
var foobar = new FooBar("test");
|
||||
assert(foobar.foo === "test");
|
||||
assert(foobar.bar === "test");
|
||||
assert(foobar.baz === undefined);
|
||||
|
||||
var foobarbaz = new FooBarBaz("test");
|
||||
assert(foobarbaz.foo === "test");
|
||||
assert(foobarbaz.bar === "test");
|
||||
assert(foobarbaz.baz === "test");
|
||||
|
||||
assert(Math.abs.call(null, -1) === 1);
|
||||
|
||||
var add = (x, y) => x + y;
|
||||
assert(add.call(null, 1, 2) === 3);
|
||||
|
||||
var multiply = function (x, y) { return x * y; };
|
||||
assert(multiply.call(null, 3, 4) === 12);
|
||||
|
||||
assert((() => this).call("foo") === globalThis);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert((function() {}).toString() === "function () {\n ???\n}");
|
||||
assert((function(foo) {}).toString() === "function (foo) {\n ???\n}");
|
||||
assert((function(foo, bar, baz) {}).toString() === "function (foo, bar, baz) {\n ???\n}");
|
||||
assert((function(foo, bar, baz) {
|
||||
if (foo) {
|
||||
return baz;
|
||||
} else if (bar) {
|
||||
return foo;
|
||||
}
|
||||
return bar + 42;
|
||||
}).toString() === "function (foo, bar, baz) {\n ???\n}");
|
||||
assert(console.log.toString() === "function log() {\n [NativeFunction]\n}");
|
||||
assert(Function.toString() === "function Function() {\n [FunctionConstructor]\n}");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue