mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 18:28:12 +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
119
Libraries/LibJS/Tests/functions/arrow-functions.js
Normal file
119
Libraries/LibJS/Tests/functions/arrow-functions.js
Normal file
|
@ -0,0 +1,119 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
let getNumber = () => 42;
|
||||
assert(getNumber() === 42);
|
||||
|
||||
getNumber = () => 99;
|
||||
assert(getNumber() === 99);
|
||||
|
||||
let add = (a, b) => a + b;
|
||||
assert(add(2, 3) === 5);
|
||||
|
||||
const addBlock = (a, b) => {
|
||||
let res = a + b;
|
||||
return res;
|
||||
};
|
||||
assert(addBlock(5, 4) === 9);
|
||||
|
||||
let chompy = [(x) => x, 2];
|
||||
assert(chompy.length === 2);
|
||||
assert(chompy[0](1) === 1);
|
||||
|
||||
const makeObject = (a, b) => ({ a, b });
|
||||
const obj = makeObject(33, 44);
|
||||
assert(typeof obj === "object");
|
||||
assert(obj.a === 33);
|
||||
assert(obj.b === 44);
|
||||
|
||||
let returnUndefined = () => { };
|
||||
assert(typeof returnUndefined() === "undefined");
|
||||
|
||||
const makeArray = (a, b) => [a, b];
|
||||
const array = makeArray("3", { foo: 4 });
|
||||
assert(array[0] === "3");
|
||||
assert(array[1].foo === 4);
|
||||
|
||||
let square = x => x * x;
|
||||
assert(square(3) === 9);
|
||||
|
||||
let squareBlock = x => {
|
||||
return x * x;
|
||||
};
|
||||
assert(squareBlock(4) === 16);
|
||||
|
||||
const message = (who => "Hello " + who)("friends!");
|
||||
assert(message === "Hello friends!");
|
||||
|
||||
const sum = ((x, y, z) => x + y + z)(1, 2, 3);
|
||||
assert(sum === 6);
|
||||
|
||||
const product = ((x, y, z) => {
|
||||
let res = x * y * z;
|
||||
return res;
|
||||
})(5, 4, 2);
|
||||
assert(product === 40);
|
||||
|
||||
const half = (x => {
|
||||
return x / 2;
|
||||
})(10);
|
||||
assert(half === 5);
|
||||
|
||||
var foo, bar;
|
||||
foo = bar, baz => {};
|
||||
assert(foo === undefined);
|
||||
assert(bar === undefined);
|
||||
|
||||
function FooBar() {
|
||||
this.x = {
|
||||
y: () => this,
|
||||
z: function () {
|
||||
return (() => this)();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
var foobar = new FooBar();
|
||||
assert(foobar.x.y() === foobar);
|
||||
assert(foobar.x.z() === foobar.x);
|
||||
|
||||
var Baz = () => {};
|
||||
|
||||
assert(Baz.prototype === undefined);
|
||||
|
||||
assertThrowsError(() => {
|
||||
new Baz();
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Baz is not a constructor"
|
||||
});
|
||||
|
||||
(() => {
|
||||
"use strict";
|
||||
assert(isStrictMode());
|
||||
|
||||
(() => {
|
||||
assert(isStrictMode());
|
||||
})();
|
||||
})();
|
||||
|
||||
(() => {
|
||||
'use strict';
|
||||
assert(isStrictMode());
|
||||
})();
|
||||
|
||||
(() => {
|
||||
assert(!isStrictMode());
|
||||
|
||||
(() => {
|
||||
"use strict";
|
||||
assert(isStrictMode());
|
||||
})();
|
||||
|
||||
assert(!isStrictMode());
|
||||
})();
|
||||
|
||||
console.log("PASS");
|
||||
} catch {
|
||||
console.log("FAIL");
|
||||
}
|
17
Libraries/LibJS/Tests/functions/constructor-basic.js
Normal file
17
Libraries/LibJS/Tests/functions/constructor-basic.js
Normal file
|
@ -0,0 +1,17 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
function Foo() {
|
||||
this.x = 123;
|
||||
}
|
||||
|
||||
assert(Foo.prototype.constructor === Foo);
|
||||
|
||||
var foo = new Foo();
|
||||
assert(foo.constructor === Foo);
|
||||
assert(foo.x === 123);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
52
Libraries/LibJS/Tests/functions/function-TypeError.js
Normal file
52
Libraries/LibJS/Tests/functions/function-TypeError.js
Normal file
|
@ -0,0 +1,52 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assertThrowsError(() => {
|
||||
var b = true;
|
||||
b();
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "true is not a function (evaluated from 'b')"
|
||||
});
|
||||
|
||||
assertThrowsError(() => {
|
||||
var n = 100 + 20 + 3;
|
||||
n();
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "123 is not a function (evaluated from 'n')"
|
||||
});
|
||||
|
||||
assertThrowsError(() => {
|
||||
var o = {};
|
||||
o.a();
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "undefined is not a function (evaluated from 'o.a')"
|
||||
});
|
||||
|
||||
assertThrowsError(() => {
|
||||
Math();
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "[object MathObject] is not a function (evaluated from 'Math')"
|
||||
});
|
||||
|
||||
assertThrowsError(() => {
|
||||
new Math();
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "[object MathObject] is not a constructor (evaluated from 'Math')"
|
||||
});
|
||||
|
||||
assertThrowsError(() => {
|
||||
new isNaN();
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "[object NativeFunction] is not a constructor (evaluated from 'isNaN')"
|
||||
});
|
||||
|
||||
console.log("PASS");
|
||||
} catch(e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
112
Libraries/LibJS/Tests/functions/function-default-parameters.js
Normal file
112
Libraries/LibJS/Tests/functions/function-default-parameters.js
Normal file
|
@ -0,0 +1,112 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
function func1(a, b = 1) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
const arrowFunc1 = (a, b = 1) => a + b;
|
||||
|
||||
assert(func1(4, 5) === 9);
|
||||
assert(func1(4) === 5);
|
||||
assert(func1(4, undefined) === 5);
|
||||
assert(Number.isNaN(func1()));
|
||||
|
||||
assert(arrowFunc1(4, 5) === 9);
|
||||
assert(arrowFunc1(4) === 5);
|
||||
assert(arrowFunc1(4, undefined) === 5);
|
||||
assert(Number.isNaN(arrowFunc1()));
|
||||
|
||||
function func2(a = 6) {
|
||||
return typeof a;
|
||||
}
|
||||
|
||||
const arrowFunc2 = (a = 6) => typeof a;
|
||||
|
||||
assert(func2() === "number");
|
||||
assert(func2(5) === "number");
|
||||
assert(func2(undefined) === "number");
|
||||
assert(func2(false) === "boolean");
|
||||
assert(func2(null) === "object");
|
||||
assert(func2({}) === "object");
|
||||
|
||||
assert(arrowFunc2() === "number");
|
||||
assert(arrowFunc2(5) === "number");
|
||||
assert(arrowFunc2(undefined) === "number");
|
||||
assert(arrowFunc2(false) === "boolean");
|
||||
assert(arrowFunc2(null) === "object");
|
||||
assert(arrowFunc2({}) === "object");
|
||||
|
||||
function func3(a = 5, b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
const arrowFunc3 = (a = 5, b) => a + b;
|
||||
|
||||
assert(func3(4, 5) === 9);
|
||||
assert(func3(undefined, 4) === 9);
|
||||
assert(Number.isNaN(func3()));
|
||||
|
||||
assert(arrowFunc3(4, 5) === 9);
|
||||
assert(arrowFunc3(undefined, 4) === 9);
|
||||
assert(Number.isNaN(arrowFunc3()));
|
||||
|
||||
function func4(a, b = a) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
const arrowFunc4 = (a, b = a) => a + b;
|
||||
|
||||
assert(func4(4, 5) === 9);
|
||||
assert(func4(4) === 8);
|
||||
assert(func4("hf") === "hfhf");
|
||||
assert(func4(true) === 2);
|
||||
assert(Number.isNaN(func4()));
|
||||
|
||||
assert(arrowFunc4(4, 5) === 9);
|
||||
assert(arrowFunc4(4) === 8);
|
||||
assert(arrowFunc4("hf") === "hfhf");
|
||||
assert(arrowFunc4(true) === 2);
|
||||
assert(Number.isNaN(arrowFunc4()));
|
||||
|
||||
function func5(a = function() { return 5; }) {
|
||||
return a();
|
||||
}
|
||||
|
||||
const arrowFunc5 = (a = function() { return 5; }) => a();
|
||||
|
||||
assert(func5() === 5);
|
||||
assert(func5(function() { return 10; }) === 10);
|
||||
assert(func5(() => 10) === 10);
|
||||
assert(arrowFunc5() === 5);
|
||||
assert(arrowFunc5(function() { return 10; }) === 10);
|
||||
assert(arrowFunc5(() => 10) === 10);
|
||||
|
||||
function func6(a = () => 5) {
|
||||
return a();
|
||||
}
|
||||
|
||||
const arrowFunc6 = (a = () => 5) => a();
|
||||
|
||||
assert(func6() === 5);
|
||||
assert(func6(function() { return 10; }) === 10);
|
||||
assert(func6(() => 10) === 10);
|
||||
assert(arrowFunc6() === 5);
|
||||
assert(arrowFunc6(function() { return 10; }) === 10);
|
||||
assert(arrowFunc6(() => 10) === 10);
|
||||
|
||||
function func7(a = { foo: "bar" }) {
|
||||
return a.foo;
|
||||
}
|
||||
|
||||
const arrowFunc7 = (a = { foo: "bar" }) => a.foo
|
||||
|
||||
assert(func7() === "bar");
|
||||
assert(func7({ foo: "baz" }) === "baz");
|
||||
assert(arrowFunc7() === "bar");
|
||||
assert(arrowFunc7({ foo: "baz" }) === "baz");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
37
Libraries/LibJS/Tests/functions/function-hoisting.js
Normal file
37
Libraries/LibJS/Tests/functions/function-hoisting.js
Normal file
|
@ -0,0 +1,37 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var callHoisted = hoisted();
|
||||
function hoisted() {
|
||||
return true;
|
||||
}
|
||||
assert(hoisted() === true);
|
||||
assert(callHoisted === true);
|
||||
|
||||
{
|
||||
var callScopedHoisted = scopedHoisted();
|
||||
function scopedHoisted() {
|
||||
return "foo";
|
||||
}
|
||||
assert(scopedHoisted() === "foo");
|
||||
assert(callScopedHoisted === "foo");
|
||||
}
|
||||
assert(scopedHoisted() === "foo");
|
||||
assert(callScopedHoisted === "foo");
|
||||
|
||||
const test = () => {
|
||||
var iife = (function () {
|
||||
return declaredLater();
|
||||
})();
|
||||
function declaredLater() {
|
||||
return "yay";
|
||||
}
|
||||
return iife;
|
||||
};
|
||||
assert(typeof declaredLater === "undefined");
|
||||
assert(test() === "yay");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
27
Libraries/LibJS/Tests/functions/function-length.js
Normal file
27
Libraries/LibJS/Tests/functions/function-length.js
Normal file
|
@ -0,0 +1,27 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
function foo() { }
|
||||
assert(foo.length === 0);
|
||||
assert((foo.length = 5) === 5);
|
||||
assert(foo.length === 0);
|
||||
|
||||
function bar(a, b, c) {}
|
||||
assert(bar.length === 3);
|
||||
assert((bar.length = 5) === 5);
|
||||
assert(bar.length === 3);
|
||||
|
||||
function baz(a, b = 1, c) {}
|
||||
assert(baz.length === 1);
|
||||
assert((baz.length = 5) === 5);
|
||||
assert(baz.length === 1);
|
||||
|
||||
function qux(a, b, ...c) {}
|
||||
assert(qux.length === 2);
|
||||
assert((qux.length = 5) === 5);
|
||||
assert(qux.length === 2);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
13
Libraries/LibJS/Tests/functions/function-missing-arg.js
Normal file
13
Libraries/LibJS/Tests/functions/function-missing-arg.js
Normal file
|
@ -0,0 +1,13 @@
|
|||
load("test-common.js");
|
||||
|
||||
function foo(a, b) { return a + b; }
|
||||
|
||||
try {
|
||||
assert(isNaN(foo()) === true);
|
||||
assert(isNaN(foo(1)) === true);
|
||||
assert(foo(2, 3) === 5);
|
||||
assert(foo(2, 3, 4) === 5);
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
49
Libraries/LibJS/Tests/functions/function-name.js
Normal file
49
Libraries/LibJS/Tests/functions/function-name.js
Normal file
|
@ -0,0 +1,49 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert((function () { }).name === "");
|
||||
|
||||
var foo = function () { }
|
||||
assert(foo.name === "foo");
|
||||
assert((foo.name = "bar") === "bar");
|
||||
assert(foo.name === "foo");
|
||||
|
||||
var a, b;
|
||||
a = b = function () { }
|
||||
assert(a.name === "b");
|
||||
assert(b.name === "b");
|
||||
|
||||
var arr = [
|
||||
function () { },
|
||||
function () { },
|
||||
function () { }
|
||||
];
|
||||
assert(arr[0].name === "arr");
|
||||
assert(arr[1].name === "arr");
|
||||
assert(arr[2].name === "arr");
|
||||
|
||||
var f;
|
||||
var o = { a: function () { } };
|
||||
assert(o.a.name === "a");
|
||||
f = o.a;
|
||||
assert(f.name === "a");
|
||||
assert(o.a.name === "a");
|
||||
o = { ...o, b: f };
|
||||
assert(o.a.name === "a");
|
||||
assert(o.b.name === "a");
|
||||
o.c = function () { };
|
||||
assert(o.c.name === "c");
|
||||
|
||||
function bar() { }
|
||||
assert(bar.name === "bar");
|
||||
assert((bar.name = "baz") === "baz");
|
||||
assert(bar.name === "bar");
|
||||
|
||||
assert(console.log.name === "log");
|
||||
assert((console.log.name = "warn") === "warn");
|
||||
assert(console.log.name === "log");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
57
Libraries/LibJS/Tests/functions/function-rest-params.js
Normal file
57
Libraries/LibJS/Tests/functions/function-rest-params.js
Normal file
|
@ -0,0 +1,57 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
function foo(...a) {
|
||||
assert(a instanceof Array);
|
||||
assert(a.length === 0);
|
||||
}
|
||||
foo();
|
||||
|
||||
function foo1(...a) {
|
||||
assert(a instanceof Array);
|
||||
assert(a.length === 4);
|
||||
assert(a[0] === "foo");
|
||||
assert(a[1] === 123);
|
||||
assert(a[2] === undefined);
|
||||
assert(a[3].foo === "bar");
|
||||
}
|
||||
foo1("foo", 123, undefined, { foo: "bar" });
|
||||
|
||||
function foo2(a, b, ...c) {
|
||||
assert(a === "foo");
|
||||
assert(b === 123);
|
||||
assert(c instanceof Array);
|
||||
assert(c.length === 0);
|
||||
}
|
||||
foo2("foo", 123);
|
||||
|
||||
function foo3(a, b, ...c) {
|
||||
assert(a === "foo");
|
||||
assert(b === 123);
|
||||
assert(c instanceof Array);
|
||||
assert(c.length === 2);
|
||||
assert(c[0] === undefined);
|
||||
assert(c[1].foo === "bar");
|
||||
}
|
||||
foo3("foo", 123, undefined, { foo: "bar" });
|
||||
|
||||
var foo = (...a) => {
|
||||
assert(a instanceof Array);
|
||||
assert(a.length === 0);
|
||||
};
|
||||
foo();
|
||||
|
||||
var foo = (a, b, ...c) => {
|
||||
assert(a === "foo");
|
||||
assert(b === 123);
|
||||
assert(c instanceof Array);
|
||||
assert(c.length === 2);
|
||||
assert(c[0] === undefined);
|
||||
assert(c[1].foo === "bar");
|
||||
};
|
||||
foo("foo", 123, undefined, { foo: "bar" });
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
27
Libraries/LibJS/Tests/functions/function-spread.js
Normal file
27
Libraries/LibJS/Tests/functions/function-spread.js
Normal file
|
@ -0,0 +1,27 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
const sum = (a, b, c) => a + b + c;
|
||||
const a = [1, 2, 3];
|
||||
|
||||
assert(sum(...a) === 6);
|
||||
assert(sum(1, ...a) === 4);
|
||||
assert(sum(...a, 10) === 6);
|
||||
|
||||
const foo = (a, b, c) => c;
|
||||
|
||||
const o = { bar: [1, 2, 3] };
|
||||
assert(foo(...o.bar) === 3);
|
||||
assert(foo(..."abc") === "c");
|
||||
|
||||
assertThrowsError(() => {
|
||||
[...1];
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "1 is not iterable",
|
||||
});
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
52
Libraries/LibJS/Tests/functions/function-strict-mode.js
Normal file
52
Libraries/LibJS/Tests/functions/function-strict-mode.js
Normal file
|
@ -0,0 +1,52 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
(function() {
|
||||
assert(!isStrictMode());
|
||||
})();
|
||||
|
||||
(function() {
|
||||
'use strict';
|
||||
assert(isStrictMode());
|
||||
})();
|
||||
|
||||
(function() {
|
||||
"use strict";
|
||||
assert(isStrictMode());
|
||||
})();
|
||||
|
||||
(function() {
|
||||
`use strict`;
|
||||
assert(!isStrictMode());
|
||||
})();
|
||||
|
||||
(function() {
|
||||
;'use strict';
|
||||
assert(!isStrictMode());
|
||||
})();
|
||||
|
||||
(function() {
|
||||
;"use strict";
|
||||
assert(!isStrictMode());
|
||||
})();
|
||||
|
||||
(function() {
|
||||
"use strict";
|
||||
(function() {
|
||||
assert(isStrictMode());
|
||||
})();
|
||||
})();
|
||||
|
||||
(function() {
|
||||
assert(!isStrictMode());
|
||||
(function(){
|
||||
"use strict";
|
||||
assert(isStrictMode());
|
||||
})();
|
||||
assert(!isStrictMode());
|
||||
})();
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(typeof this === "object");
|
||||
assert(this === globalThis);
|
||||
|
||||
function Foo() {
|
||||
this.x = 5;
|
||||
assert(typeof this === "object");
|
||||
assert(this.x === 5);
|
||||
}
|
||||
|
||||
new Foo();
|
||||
console.log("PASS");
|
||||
} catch (err) {
|
||||
console.log("FAIL: " + err);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue