mirror of
https://github.com/RGBCube/serenity
synced 2025-06-01 03:08:13 +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
28
Libraries/LibJS/Tests/builtins/Array/Array.isArray.js
Normal file
28
Libraries/LibJS/Tests/builtins/Array/Array.isArray.js
Normal file
|
@ -0,0 +1,28 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.isArray.length === 1);
|
||||
|
||||
assert(Array.isArray() === false);
|
||||
assert(Array.isArray("1") === false);
|
||||
assert(Array.isArray("foo") === false);
|
||||
assert(Array.isArray(1) === false);
|
||||
assert(Array.isArray(1, 2, 3) === false);
|
||||
assert(Array.isArray(undefined) === false);
|
||||
assert(Array.isArray(null) === false);
|
||||
assert(Array.isArray(Infinity) === false);
|
||||
assert(Array.isArray({}) === false);
|
||||
|
||||
assert(Array.isArray([]) === true);
|
||||
assert(Array.isArray([1]) === true);
|
||||
assert(Array.isArray([1, 2, 3]) === true);
|
||||
assert(Array.isArray(new Array()) === true);
|
||||
assert(Array.isArray(new Array(10)) === true);
|
||||
assert(Array.isArray(new Array("a", "b", "c")) === true);
|
||||
// FIXME: Array.prototype is supposed to be an array!
|
||||
// assert(Array.isArray(Array.prototype) === true);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
48
Libraries/LibJS/Tests/builtins/Array/Array.js
Normal file
48
Libraries/LibJS/Tests/builtins/Array/Array.js
Normal file
|
@ -0,0 +1,48 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.length === 1);
|
||||
assert(Array.name === "Array");
|
||||
assert(Array.prototype.length === 0);
|
||||
|
||||
assert(typeof Array() === "object");
|
||||
assert(typeof new Array() === "object");
|
||||
|
||||
var a;
|
||||
|
||||
a = new Array(5);
|
||||
assert(a.length === 5);
|
||||
|
||||
a = new Array("5");
|
||||
assert(a.length === 1);
|
||||
assert(a[0] === "5");
|
||||
|
||||
a = new Array(1, 2, 3);
|
||||
assert(a.length === 3);
|
||||
assert(a[0] === 1);
|
||||
assert(a[1] === 2);
|
||||
assert(a[2] === 3);
|
||||
|
||||
a = new Array([1, 2, 3]);
|
||||
assert(a.length === 1);
|
||||
assert(a[0][0] === 1);
|
||||
assert(a[0][1] === 2);
|
||||
assert(a[0][2] === 3);
|
||||
|
||||
a = new Array(1, 2, 3);
|
||||
Object.defineProperty(a, 3, { get() { return 10; } });
|
||||
assert(a.toString() === "1,2,3,10");
|
||||
|
||||
[-1, -100, -0.1, 0.1, 1.23, Infinity, -Infinity, NaN].forEach(value => {
|
||||
assertThrowsError(() => {
|
||||
new Array(value);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Invalid array length"
|
||||
});
|
||||
});
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
46
Libraries/LibJS/Tests/builtins/Array/Array.of.js
Normal file
46
Libraries/LibJS/Tests/builtins/Array/Array.of.js
Normal file
|
@ -0,0 +1,46 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.of.length === 0);
|
||||
|
||||
assert(typeof Array.of() === "object");
|
||||
|
||||
var a;
|
||||
|
||||
a = Array.of(5);
|
||||
assert(a.length === 1);
|
||||
assert(a[0] === 5);
|
||||
|
||||
a = Array.of("5");
|
||||
assert(a.length === 1);
|
||||
assert(a[0] === "5");
|
||||
|
||||
a = Array.of(Infinity);
|
||||
assert(a.length === 1);
|
||||
assert(a[0] === Infinity);
|
||||
|
||||
a = Array.of(1, 2, 3);
|
||||
assert(a.length === 3);
|
||||
assert(a[0] === 1);
|
||||
assert(a[1] === 2);
|
||||
assert(a[2] === 3);
|
||||
|
||||
a = Array.of([1, 2, 3]);
|
||||
assert(a.length === 1);
|
||||
assert(a[0][0] === 1);
|
||||
assert(a[0][1] === 2);
|
||||
assert(a[0][2] === 3);
|
||||
|
||||
let t = [1, 2, 3];
|
||||
Object.defineProperty(t, 3, { get() { return 4; } });
|
||||
a = Array.of(...t);
|
||||
assert(a.length === 4);
|
||||
assert(a[0] === 1);
|
||||
assert(a[1] === 2);
|
||||
assert(a[2] === 3);
|
||||
assert(a[3] === 4);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,134 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
[undefined, "foo", -42, 0].forEach(length => {
|
||||
const o = { length };
|
||||
|
||||
assert(Array.prototype.push.call(o, "foo") === 1);
|
||||
assert(o.length === 1);
|
||||
assert(o[0] === "foo");
|
||||
assert(Array.prototype.push.call(o, "bar", "baz") === 3);
|
||||
assert(o.length === 3);
|
||||
assert(o[0] === "foo");
|
||||
assert(o[1] === "bar");
|
||||
assert(o[2] === "baz");
|
||||
|
||||
assert(Array.prototype.pop.call(o) === "baz");
|
||||
assert(o.length === 2);
|
||||
assert(Array.prototype.pop.call(o) === "bar");
|
||||
assert(o.length === 1);
|
||||
assert(Array.prototype.pop.call(o) === "foo");
|
||||
assert(o.length === 0);
|
||||
assert(Array.prototype.pop.call(o) === undefined);
|
||||
assert(o.length === 0);
|
||||
|
||||
o.length = length;
|
||||
assert(Array.prototype.pop.call(o) === undefined);
|
||||
assert(o.length === 0);
|
||||
});
|
||||
|
||||
{
|
||||
const o = { length: 3, 0: "hello", 2: "serenity" };
|
||||
const removed = Array.prototype.splice.call(o, 0, 2, "hello", "friends");
|
||||
assert(o.length === 3);
|
||||
assert(o[0] === "hello");
|
||||
assert(o[1] === "friends");
|
||||
assert(o[2] === "serenity");
|
||||
assert(removed.length === 2);
|
||||
assert(removed[0] === "hello");
|
||||
assert(removed[1] === undefined);
|
||||
}
|
||||
|
||||
{
|
||||
assert(Array.prototype.join.call({}) === "");
|
||||
assert(Array.prototype.join.call({ length: "foo" }) === "");
|
||||
assert(Array.prototype.join.call({ length: 3 }) === ",,");
|
||||
assert(Array.prototype.join.call({ length: 2, 0: "foo", 1: "bar" }) === "foo,bar");
|
||||
assert(Array.prototype.join.call({ length: 2, 0: "foo", 1: "bar", 2: "baz" }) === "foo,bar");
|
||||
assert(Array.prototype.join.call({ length: 3, 1: "bar" }, "~") === "~bar~");
|
||||
assert(Array.prototype.join.call({ length: 3, 0: "foo", 1: "bar", 2: "baz" }, "~") === "foo~bar~baz");
|
||||
}
|
||||
|
||||
{
|
||||
assert(Array.prototype.toString.call({}) === "[object Object]");
|
||||
assert(Array.prototype.toString.call({ join: "foo" }) === "[object Object]");
|
||||
assert(Array.prototype.toString.call({ join: () => "foo" }) === "foo");
|
||||
}
|
||||
|
||||
{
|
||||
assert(Array.prototype.indexOf.call({}) === -1);
|
||||
assert(Array.prototype.indexOf.call({ 0: undefined }) === -1);
|
||||
assert(Array.prototype.indexOf.call({ length: 1, 0: undefined }) === 0);
|
||||
assert(Array.prototype.indexOf.call({ length: 1, 2: "foo" }, "foo") === -1);
|
||||
assert(Array.prototype.indexOf.call({ length: 5, 2: "foo" }, "foo") === 2);
|
||||
assert(Array.prototype.indexOf.call({ length: 5, 2: "foo", 4: "foo" }, "foo", 3) === 4);
|
||||
}
|
||||
|
||||
{
|
||||
assert(Array.prototype.lastIndexOf.call({}) === -1);
|
||||
assert(Array.prototype.lastIndexOf.call({ 0: undefined }) === -1);
|
||||
assert(Array.prototype.lastIndexOf.call({ length: 1, 0: undefined }) === 0);
|
||||
assert(Array.prototype.lastIndexOf.call({ length: 1, 2: "foo" }, "foo") === -1);
|
||||
assert(Array.prototype.lastIndexOf.call({ length: 5, 2: "foo" }, "foo") === 2);
|
||||
assert(Array.prototype.lastIndexOf.call({ length: 5, 2: "foo", 4: "foo" }, "foo") === 4);
|
||||
assert(Array.prototype.lastIndexOf.call({ length: 5, 2: "foo", 4: "foo" }, "foo", -2) === 2);
|
||||
}
|
||||
|
||||
{
|
||||
assert(Array.prototype.includes.call({}) === false);
|
||||
assert(Array.prototype.includes.call({ 0: undefined }) === false);
|
||||
assert(Array.prototype.includes.call({ length: 1, 0: undefined }) === true);
|
||||
assert(Array.prototype.includes.call({ length: 1, 2: "foo" }, "foo") === false);
|
||||
assert(Array.prototype.includes.call({ length: 5, 2: "foo" }, "foo") === true);
|
||||
}
|
||||
|
||||
const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
|
||||
|
||||
{
|
||||
assertVisitsAll(visit => {
|
||||
Array.prototype.every.call(o, function (value) {
|
||||
visit(value);
|
||||
return true;
|
||||
});
|
||||
}, ["foo", "bar", "baz"]);
|
||||
}
|
||||
|
||||
["find", "findIndex"].forEach(name => {
|
||||
assertVisitsAll(visit => {
|
||||
Array.prototype[name].call(o, function (value) {
|
||||
visit(value);
|
||||
return false;
|
||||
});
|
||||
}, ["foo", "bar", undefined, "baz", undefined]);
|
||||
});
|
||||
|
||||
["filter", "forEach", "map", "some"].forEach(name => {
|
||||
assertVisitsAll(visit => {
|
||||
Array.prototype[name].call(o, function (value) {
|
||||
visit(value);
|
||||
return false;
|
||||
});
|
||||
}, ["foo", "bar", "baz"]);
|
||||
});
|
||||
{
|
||||
assertVisitsAll(visit => {
|
||||
Array.prototype.reduce.call(o, function (_, value) {
|
||||
visit(value);
|
||||
return false;
|
||||
}, "initial");
|
||||
}, ["foo", "bar", "baz"]);
|
||||
}
|
||||
|
||||
{
|
||||
assertVisitsAll(visit => {
|
||||
Array.prototype.reduceRight.call(o, function (_, value) {
|
||||
visit(value);
|
||||
return false;
|
||||
}, "initial");
|
||||
}, ["baz", "bar", "foo"]);
|
||||
}
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.concat.length === 1);
|
||||
|
||||
var array = ["hello", "friends"];
|
||||
|
||||
var array_concat = array.concat();
|
||||
assert(array_concat.length === array.length);
|
||||
|
||||
array_concat = array.concat(1)
|
||||
assert(array_concat.length === 3);
|
||||
assert(array_concat[2] === 1);
|
||||
|
||||
array_concat = array.concat([1, 2, 3])
|
||||
assert(array_concat.length === 5);
|
||||
assert(array_concat[2] === 1);
|
||||
assert(array_concat[3] === 2);
|
||||
assert(array_concat[4] === 3);
|
||||
|
||||
array_concat = array.concat(false, "serenity");
|
||||
assert(array_concat.length === 4);
|
||||
assert(array_concat[2] === false);
|
||||
assert(array_concat[3] === "serenity");
|
||||
|
||||
array_concat = array.concat({ name: "libjs" }, [1, [2, 3]]);
|
||||
assert(array_concat.length === 5);
|
||||
assert(array_concat[2].name === "libjs");
|
||||
assert(array_concat[3] === 1);
|
||||
assert(array_concat[4][0] === 2);
|
||||
assert(array_concat[4][1] === 3);
|
||||
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.every.length === 1);
|
||||
|
||||
assertThrowsError(() => {
|
||||
[].every(undefined);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "undefined is not a function"
|
||||
});
|
||||
|
||||
var arrayOne = ["serenity", { test: "serenity"} ];
|
||||
var arrayTwo = [true, false, 1, 2, 3, "3"];
|
||||
|
||||
assert(arrayOne.every(value => value === "hello") === false);
|
||||
assert(arrayOne.every(value => value === "serenity") === false);
|
||||
assert(arrayOne.every((value, index, arr) => index < 2) === true);
|
||||
assert(arrayOne.every(value => typeof(value) === "string") === false);
|
||||
assert(arrayOne.every(value => arrayOne.pop()) === true);
|
||||
|
||||
assert(arrayTwo.every((value, index, arr) => index > 0) === false);
|
||||
assert(arrayTwo.every((value, index, arr) => index >= 0) === true);
|
||||
assert(arrayTwo.every(value => typeof(value) !== "string") === false);
|
||||
assert(arrayTwo.every(value => typeof(value) === "number") === false);
|
||||
assert(arrayTwo.every(value => value > 0) === false);
|
||||
assert(arrayTwo.every(value => value >= 0 && value < 4) === true);
|
||||
assert(arrayTwo.every(value => arrayTwo.pop()) === true);
|
||||
|
||||
assert(["", "hello", "friends", "serenity"].every(value => value.length >= 0) === true);
|
||||
assert([].every(value => value === 1) === true);
|
||||
|
||||
arrayTwo = [true, false, 1, 2, 3, "3"];
|
||||
|
||||
// Every only goes up to the original length.
|
||||
assert(arrayTwo.every((value, index, arr) => {
|
||||
arr.push("serenity");
|
||||
return value < 4;
|
||||
}) === true);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
24
Libraries/LibJS/Tests/builtins/Array/Array.prototype.fill.js
Normal file
24
Libraries/LibJS/Tests/builtins/Array/Array.prototype.fill.js
Normal file
|
@ -0,0 +1,24 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.fill.length === 1);
|
||||
|
||||
var array = [1, 2, 3, 4];
|
||||
assertArrayEquals(array.fill(0, 2, 4), [1, 2, 0, 0]);
|
||||
assertArrayEquals(array.fill(5, 1), [1, 5, 5, 5]);
|
||||
assertArrayEquals(array.fill(6), [6, 6, 6, 6]);
|
||||
|
||||
assertArrayEquals([1, 2, 3].fill(4), [4, 4, 4]);
|
||||
assertArrayEquals([1, 2, 3].fill(4, 1), [1, 4, 4]);
|
||||
assertArrayEquals([1, 2, 3].fill(4, 1, 2), [1, 4, 3]);
|
||||
assertArrayEquals([1, 2, 3].fill(4, 3, 3), [1, 2, 3]);
|
||||
assertArrayEquals([1, 2, 3].fill(4, -3, -2), [4, 2, 3]);
|
||||
assertArrayEquals([1, 2, 3].fill(4, NaN, NaN), [1, 2, 3]);
|
||||
assertArrayEquals([1, 2, 3].fill(4, 3, 5), [1, 2, 3]);
|
||||
assertArrayEquals(Array(3).fill(4), [4, 4, 4]);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.filter.length === 1);
|
||||
|
||||
assertThrowsError(() => {
|
||||
[].filter();
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Array.prototype.filter() requires at least one argument"
|
||||
});
|
||||
|
||||
assertThrowsError(() => {
|
||||
[].filter(undefined);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "undefined is not a function"
|
||||
});
|
||||
|
||||
var callbackCalled = 0;
|
||||
var callback = () => { callbackCalled++; };
|
||||
|
||||
assert([].filter(callback).length === 0);
|
||||
assert(callbackCalled === 0);
|
||||
|
||||
assert([1, 2, 3].filter(callback).length === 0);
|
||||
assert(callbackCalled === 3);
|
||||
|
||||
var evenNumbers = [0, 1, 2, 3, 4, 5, 6, 7].filter(x => x % 2 === 0);
|
||||
assert(evenNumbers.length === 4);
|
||||
assert(evenNumbers[0] === 0);
|
||||
assert(evenNumbers[1] === 2);
|
||||
assert(evenNumbers[2] === 4);
|
||||
assert(evenNumbers[3] === 6);
|
||||
|
||||
var fruits = ["Apple", "Banana", "Blueberry", "Grape", "Mango", "Orange", "Peach", "Pineapple", "Raspberry", "Watermelon"];
|
||||
const filterItems = (arr, query) => {
|
||||
return arr.filter(el => el.toLowerCase().indexOf(query.toLowerCase()) !== -1)
|
||||
};
|
||||
|
||||
var results;
|
||||
|
||||
results = filterItems(fruits, "Berry");
|
||||
assert(results.length === 2);
|
||||
assert(results[0] === "Blueberry");
|
||||
assert(results[1] === "Raspberry");
|
||||
|
||||
results = filterItems(fruits, "P");
|
||||
assert(results.length === 5);
|
||||
assert(results[0] === "Apple");
|
||||
assert(results[1] === "Grape");
|
||||
assert(results[2] === "Peach");
|
||||
assert(results[3] === "Pineapple");
|
||||
assert(results[4] === "Raspberry");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
49
Libraries/LibJS/Tests/builtins/Array/Array.prototype.find.js
Normal file
49
Libraries/LibJS/Tests/builtins/Array/Array.prototype.find.js
Normal file
|
@ -0,0 +1,49 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.find.length === 1);
|
||||
|
||||
assertThrowsError(() => {
|
||||
[].find(undefined);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "undefined is not a function"
|
||||
});
|
||||
|
||||
var array = ["hello", "friends", 1, 2, false];
|
||||
|
||||
assert(array.find(value => value === "hello") === "hello");
|
||||
assert(array.find((value, index, arr) => index === 1) === "friends");
|
||||
assert(array.find(value => value == "1") === 1);
|
||||
assert(array.find(value => value === 1) === 1);
|
||||
assert(array.find(value => typeof(value) !== "string") === 1);
|
||||
assert(array.find(value => typeof(value) === "boolean") === false);
|
||||
assert(array.find(value => value > 1) === 2);
|
||||
assert(array.find(value => value > 1 && value < 3) === 2);
|
||||
assert(array.find(value => value > 100) === undefined);
|
||||
assert([].find(value => value === 1) === undefined);
|
||||
|
||||
var callbackCalled = 0;
|
||||
var callback = () => { callbackCalled++; };
|
||||
|
||||
[].find(callback)
|
||||
assert(callbackCalled === 0);
|
||||
|
||||
[1, 2, 3].find(callback);
|
||||
assert(callbackCalled === 3);
|
||||
|
||||
callbackCalled = 0;
|
||||
[1, , , "foo", , undefined, , ,].find(callback);
|
||||
assert(callbackCalled === 8);
|
||||
|
||||
callbackCalled = 0;
|
||||
[1, , , "foo", , undefined, , ,].find(value => {
|
||||
callbackCalled++;
|
||||
return value === undefined;
|
||||
});
|
||||
assert(callbackCalled === 2);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.findIndex.length === 1);
|
||||
|
||||
assertThrowsError(() => {
|
||||
[].findIndex(undefined);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "undefined is not a function"
|
||||
});
|
||||
|
||||
var array = ["hello", "friends", 1, 2, false];
|
||||
|
||||
assert(array.findIndex(value => value === "hello") === 0);
|
||||
assert(array.findIndex((value, index, arr) => index === 1) === 1);
|
||||
assert(array.findIndex(value => value == "1") === 2);
|
||||
assert(array.findIndex(value => value === 1) === 2);
|
||||
assert(array.findIndex(value => typeof(value) !== "string") === 2);
|
||||
assert(array.findIndex(value => typeof(value) === "boolean") === 4);
|
||||
assert(array.findIndex(value => value > 1) === 3);
|
||||
assert(array.findIndex(value => value > 1 && value < 3) === 3);
|
||||
assert(array.findIndex(value => value > 100) === -1);
|
||||
assert([].findIndex(value => value === 1) === -1);
|
||||
|
||||
var callbackCalled = 0;
|
||||
var callback = () => { callbackCalled++; };
|
||||
|
||||
[].findIndex(callback)
|
||||
assert(callbackCalled === 0);
|
||||
|
||||
[1, 2, 3].findIndex(callback);
|
||||
assert(callbackCalled === 3);
|
||||
|
||||
callbackCalled = 0;
|
||||
[1, , , "foo", , undefined, , ,].findIndex(callback);
|
||||
assert(callbackCalled === 8);
|
||||
|
||||
callbackCalled = 0;
|
||||
[1, , , "foo", , undefined, , ,].findIndex(value => {
|
||||
callbackCalled++;
|
||||
return value === undefined;
|
||||
});
|
||||
assert(callbackCalled === 2);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.forEach.length === 1);
|
||||
|
||||
assertThrowsError(() => {
|
||||
[].forEach();
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Array.prototype.forEach() requires at least one argument"
|
||||
});
|
||||
|
||||
assertThrowsError(() => {
|
||||
[].forEach(undefined);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "undefined is not a function"
|
||||
});
|
||||
|
||||
var a = [1, 2, 3];
|
||||
var o = {};
|
||||
var callbackCalled = 0;
|
||||
var callback = () => { callbackCalled++; };
|
||||
|
||||
assert([].forEach(callback) === undefined);
|
||||
assert(callbackCalled === 0);
|
||||
|
||||
assert(a.forEach(callback) === undefined);
|
||||
assert(callbackCalled === 3);
|
||||
|
||||
callbackCalled = 0;
|
||||
a.forEach(function(value, index) {
|
||||
assert(value === a[index]);
|
||||
assert(index === a[index] - 1);
|
||||
});
|
||||
|
||||
callbackCalled = 0;
|
||||
a.forEach(function(_, _, array) {
|
||||
callbackCalled++;
|
||||
assert(a.length === array.length);
|
||||
a.push("test");
|
||||
});
|
||||
assert(callbackCalled === 3);
|
||||
assert(a.length === 6);
|
||||
|
||||
callbackCalled = 0;
|
||||
a.forEach(function(value, index) {
|
||||
callbackCalled++;
|
||||
this[index] = value;
|
||||
}, o);
|
||||
assert(callbackCalled === 6);
|
||||
assert(o[0] === 1);
|
||||
assert(o[1] === 2);
|
||||
assert(o[2] === 3);
|
||||
assert(o[3] === "test");
|
||||
assert(o[4] === "test");
|
||||
assert(o[5] === "test");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.includes.length === 1);
|
||||
|
||||
var array = ['hello', 'friends', 1, 2, false];
|
||||
|
||||
assert([].includes() === false);
|
||||
assert([undefined].includes() === true);
|
||||
assert(array.includes('hello') === true);
|
||||
assert(array.includes(1) === true);
|
||||
assert(array.includes(1, -3) === true);
|
||||
assert(array.includes('serenity') === false);
|
||||
assert(array.includes(false, -1) === true);
|
||||
assert(array.includes(2, -1) === false);
|
||||
assert(array.includes(2, -100) === true);
|
||||
assert(array.includes('friends', 100) === false);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.indexOf.length === 1);
|
||||
|
||||
var array = ['hello', 'friends', 1, 2, false];
|
||||
|
||||
assert(array.indexOf('hello') === 0);
|
||||
assert(array.indexOf('friends') === 1);
|
||||
assert(array.indexOf(false) === 4);
|
||||
assert(array.indexOf(false, 2) === 4);
|
||||
assert(array.indexOf(false, -2) === 4);
|
||||
assert(array.indexOf(1) === 2);
|
||||
assert(array.indexOf(1, 1000) === -1);
|
||||
assert(array.indexOf(1, -1000) === 2);
|
||||
assert(array.indexOf('serenity') === -1);
|
||||
assert(array.indexOf(false, -1) === 4);
|
||||
assert(array.indexOf(2, -1) === -1);
|
||||
assert(array.indexOf(2, -2) === 3);
|
||||
assert([].indexOf('serenity') === -1);
|
||||
assert([].indexOf('serenity', 10) === -1);
|
||||
assert([].indexOf('serenity', -10) === -1);
|
||||
assert([].indexOf() === -1);
|
||||
assert([undefined].indexOf() === 0);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
19
Libraries/LibJS/Tests/builtins/Array/Array.prototype.join.js
Normal file
19
Libraries/LibJS/Tests/builtins/Array/Array.prototype.join.js
Normal file
|
@ -0,0 +1,19 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.join.length === 1);
|
||||
|
||||
assert(["hello", "friends"].join() === "hello,friends");
|
||||
assert(["hello", "friends"].join(" ") === "hello friends");
|
||||
assert(["hello", "friends", "foo"].join("~", "#") === "hello~friends~foo");
|
||||
assert([].join() === "");
|
||||
assert([null].join() === "");
|
||||
assert([undefined].join() === "");
|
||||
assert([undefined, null, ""].join() === ",,");
|
||||
assert([1, null, 2, undefined, 3].join() === "1,,2,,3");
|
||||
assert(Array(3).join() === ",,");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.lastIndexOf.length === 1);
|
||||
|
||||
var array = [1, 2, 3, 1, "hello"];
|
||||
|
||||
assert(array.lastIndexOf("hello") === 4);
|
||||
assert(array.lastIndexOf("hello", 1000) === 4);
|
||||
assert(array.lastIndexOf(1) === 3);
|
||||
assert(array.lastIndexOf(1, -1) === 3);
|
||||
assert(array.lastIndexOf(1, -2) === 3);
|
||||
assert(array.lastIndexOf(2) === 1);
|
||||
assert(array.lastIndexOf(2, -3) === 1);
|
||||
assert(array.lastIndexOf(2, -4) === 1);
|
||||
assert([].lastIndexOf('hello') === -1);
|
||||
assert([].lastIndexOf('hello', 10) === -1);
|
||||
assert([].lastIndexOf('hello', -10) === -1);
|
||||
assert([].lastIndexOf() === -1);
|
||||
assert([undefined].lastIndexOf() === 0);
|
||||
assert([undefined, undefined, undefined].lastIndexOf() === 2);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
53
Libraries/LibJS/Tests/builtins/Array/Array.prototype.map.js
Normal file
53
Libraries/LibJS/Tests/builtins/Array/Array.prototype.map.js
Normal file
|
@ -0,0 +1,53 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.map.length === 1);
|
||||
|
||||
assertThrowsError(() => {
|
||||
[].map();
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Array.prototype.map() requires at least one argument"
|
||||
});
|
||||
|
||||
assertThrowsError(() => {
|
||||
[].map(undefined);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "undefined is not a function"
|
||||
});
|
||||
|
||||
var callbackCalled = 0;
|
||||
var callback = () => { callbackCalled++; };
|
||||
|
||||
assert([].map(callback).length === 0);
|
||||
assert(callbackCalled === 0);
|
||||
|
||||
assert([1, 2, 3].map(callback).length === 3);
|
||||
assert(callbackCalled === 3);
|
||||
|
||||
callbackCalled = 0;
|
||||
assert([1, , , "foo", , undefined, , ,].map(callback).length === 8);
|
||||
assert(callbackCalled === 3);
|
||||
|
||||
var results = [undefined, null, true, "foo", 42, {}].map((value, index) => "" + index + " -> " + value);
|
||||
assert(results.length === 6);
|
||||
assert(results[0] === "0 -> undefined");
|
||||
assert(results[1] === "1 -> null");
|
||||
assert(results[2] === "2 -> true");
|
||||
assert(results[3] === "3 -> foo");
|
||||
assert(results[4] === "4 -> 42");
|
||||
assert(results[5] === "5 -> [object Object]");
|
||||
|
||||
var squaredNumbers = [0, 1, 2, 3, 4].map(x => x ** 2);
|
||||
assert(squaredNumbers.length === 5);
|
||||
assert(squaredNumbers[0] === 0);
|
||||
assert(squaredNumbers[1] === 1);
|
||||
assert(squaredNumbers[2] === 4);
|
||||
assert(squaredNumbers[3] === 9);
|
||||
assert(squaredNumbers[4] === 16);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
21
Libraries/LibJS/Tests/builtins/Array/Array.prototype.pop.js
Normal file
21
Libraries/LibJS/Tests/builtins/Array/Array.prototype.pop.js
Normal file
|
@ -0,0 +1,21 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var a = [1, 2, 3];
|
||||
var value = a.pop();
|
||||
assert(value === 3);
|
||||
assert(a.length === 2);
|
||||
assert(a[0] === 1);
|
||||
assert(a[1] === 2);
|
||||
|
||||
var a = [];
|
||||
var value = a.pop();
|
||||
assert(value === undefined);
|
||||
assert(a.length === 0);
|
||||
|
||||
assert([,].pop() === undefined);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
30
Libraries/LibJS/Tests/builtins/Array/Array.prototype.push.js
Normal file
30
Libraries/LibJS/Tests/builtins/Array/Array.prototype.push.js
Normal file
|
@ -0,0 +1,30 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.push.length === 1);
|
||||
|
||||
var a = ["hello"];
|
||||
var length = a.push();
|
||||
assert(length === 1);
|
||||
assert(a.length === 1);
|
||||
assert(a[0] === "hello");
|
||||
|
||||
length = a.push("friends");
|
||||
assert(length === 2);
|
||||
assert(a.length === 2);
|
||||
assert(a[0] === "hello");
|
||||
assert(a[1] === "friends");
|
||||
|
||||
length = a.push(1, 2, 3);
|
||||
assert(length === 5);
|
||||
assert(a.length === 5);
|
||||
assert(a[0] === "hello");
|
||||
assert(a[1] === "friends");
|
||||
assert(a[2] === 1);
|
||||
assert(a[3] === 2);
|
||||
assert(a[4] === 3);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
104
Libraries/LibJS/Tests/builtins/Array/Array.prototype.reduce.js
Normal file
104
Libraries/LibJS/Tests/builtins/Array/Array.prototype.reduce.js
Normal file
|
@ -0,0 +1,104 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.reduce.length === 1);
|
||||
|
||||
assertThrowsError(() => {
|
||||
[1].reduce();
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Array.prototype.reduce() requires at least one argument"
|
||||
});
|
||||
|
||||
assertThrowsError(() => {
|
||||
[1].reduce(undefined);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "undefined is not a function"
|
||||
});
|
||||
|
||||
assertThrowsError(() => {
|
||||
[].reduce((a, x) => x);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Reduce of empty array with no initial value"
|
||||
});
|
||||
|
||||
assertThrowsError(() => {
|
||||
[, ,].reduce((a, x) => x);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Reduce of empty array with no initial value"
|
||||
});
|
||||
|
||||
[1, 2].reduce(function () { assert(this === undefined) });
|
||||
|
||||
var callbackCalled = 0;
|
||||
var callback = () => { callbackCalled++; return true };
|
||||
|
||||
assert([1].reduce(callback) === 1);
|
||||
assert(callbackCalled === 0);
|
||||
|
||||
assert([, 1].reduce(callback) === 1);
|
||||
assert(callbackCalled === 0);
|
||||
|
||||
callbackCalled = 0;
|
||||
assert([1, 2, 3].reduce(callback) === true);
|
||||
assert(callbackCalled === 2);
|
||||
|
||||
callbackCalled = 0;
|
||||
assert([, , 1, 2, 3].reduce(callback) === true);
|
||||
assert(callbackCalled === 2);
|
||||
|
||||
callbackCalled = 0;
|
||||
assert([1, , , 10, , 100, , ,].reduce(callback) === true);
|
||||
assert(callbackCalled === 2);
|
||||
|
||||
var constantlySad = () => ":^(";
|
||||
var result = [].reduce(constantlySad, ":^)");
|
||||
assert(result === ":^)");
|
||||
|
||||
result = [":^0"].reduce(constantlySad, ":^)");
|
||||
assert(result === ":^(");
|
||||
|
||||
result = [":^0"].reduce(constantlySad);
|
||||
assert(result === ":^0");
|
||||
|
||||
result = [5, 4, 3, 2, 1].reduce((accum, elem) => accum + elem);
|
||||
assert(result === 15);
|
||||
|
||||
result = [1, 2, 3, 4, 5, 6].reduce((accum, elem) => accum + elem, 100);
|
||||
assert(result === 121);
|
||||
|
||||
result = [6, 5, 4, 3, 2, 1].reduce((accum, elem) => { return accum + elem }, 100);
|
||||
assert(result === 121);
|
||||
|
||||
var indexes = [];
|
||||
result = ["foo", 1, true].reduce((a, v, i) => { indexes.push(i) });
|
||||
assert(result === undefined);
|
||||
assert(indexes.length === 2);
|
||||
assert(indexes[0] === 1);
|
||||
assert(indexes[1] === 2);
|
||||
|
||||
indexes = [];
|
||||
result = ["foo", 1, true].reduce((a, v, i) => { indexes.push(i) }, "foo");
|
||||
assert(result === undefined);
|
||||
assert(indexes.length === 3);
|
||||
assert(indexes[0] === 0);
|
||||
assert(indexes[1] === 1);
|
||||
assert(indexes[2] === 2);
|
||||
|
||||
var mutable = { prop: 0 };
|
||||
result = ["foo", 1, true].reduce((a, v) => { a.prop = v; return a; }, mutable);
|
||||
assert(result === mutable);
|
||||
assert(result.prop === true);
|
||||
|
||||
var a1 = [1, 2];
|
||||
var a2 = null;
|
||||
a1.reduce((a, v, i, t) => { a2 = t });
|
||||
assert(a1 === a2);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,134 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.reduceRight.length === 1);
|
||||
|
||||
assertThrowsError(
|
||||
() => {
|
||||
[1].reduceRight();
|
||||
},
|
||||
{
|
||||
error: TypeError,
|
||||
message: "Array.prototype.reduceRight() requires at least one argument",
|
||||
}
|
||||
);
|
||||
|
||||
assertThrowsError(
|
||||
() => {
|
||||
[1].reduceRight(undefined);
|
||||
},
|
||||
{
|
||||
error: TypeError,
|
||||
message: "undefined is not a function",
|
||||
}
|
||||
);
|
||||
|
||||
assertThrowsError(
|
||||
() => {
|
||||
[].reduceRight((a, x) => x);
|
||||
},
|
||||
{
|
||||
error: TypeError,
|
||||
message: "Reduce of empty array with no initial value",
|
||||
}
|
||||
);
|
||||
|
||||
assertThrowsError(
|
||||
() => {
|
||||
[, ,].reduceRight((a, x) => x);
|
||||
},
|
||||
{
|
||||
error: TypeError,
|
||||
message: "Reduce of empty array with no initial value",
|
||||
}
|
||||
);
|
||||
|
||||
[1, 2].reduceRight(function () {
|
||||
assert(this === undefined);
|
||||
});
|
||||
|
||||
var callbackCalled = 0;
|
||||
var callback = () => {
|
||||
callbackCalled++;
|
||||
return true;
|
||||
};
|
||||
|
||||
assert([1].reduceRight(callback) === 1);
|
||||
assert(callbackCalled === 0);
|
||||
|
||||
assert([1].reduceRight(callback) === 1);
|
||||
assert(callbackCalled === 0);
|
||||
|
||||
callbackCalled = 0;
|
||||
assert([1, 2, 3].reduceRight(callback) === true);
|
||||
assert(callbackCalled === 2);
|
||||
|
||||
callbackCalled = 0;
|
||||
assert([1, 2, 3, ,].reduceRight(callback) === true);
|
||||
assert(callbackCalled === 2);
|
||||
|
||||
callbackCalled = 0;
|
||||
assert([, , , 1, , , 10, , 100, , ,].reduceRight(callback) === true);
|
||||
assert(callbackCalled === 2);
|
||||
|
||||
var constantlySad = () => ":^(";
|
||||
var result = [].reduceRight(constantlySad, ":^)");
|
||||
assert(result === ":^)");
|
||||
|
||||
result = [":^0"].reduceRight(constantlySad, ":^)");
|
||||
assert(result === ":^(");
|
||||
|
||||
result = [":^0"].reduceRight(constantlySad);
|
||||
assert(result === ":^0");
|
||||
|
||||
result = [5, 4, 3, 2, 1].reduceRight((accum, elem) => "" + accum + elem);
|
||||
assert(result === "12345");
|
||||
|
||||
result = [1, 2, 3, 4, 5, 6].reduceRight((accum, elem) => {
|
||||
return "" + accum + elem;
|
||||
}, 100);
|
||||
assert(result === "100654321");
|
||||
|
||||
result = [6, 5, 4, 3, 2, 1].reduceRight((accum, elem) => {
|
||||
return "" + accum + elem;
|
||||
}, 100);
|
||||
assert(result === "100123456");
|
||||
|
||||
var indexes = [];
|
||||
result = ["foo", 1, true].reduceRight((a, v, i) => {
|
||||
indexes.push(i);
|
||||
});
|
||||
assert(result === undefined);
|
||||
assert(indexes.length === 2);
|
||||
assert(indexes[0] === 1);
|
||||
assert(indexes[1] === 0);
|
||||
|
||||
indexes = [];
|
||||
result = ["foo", 1, true].reduceRight((a, v, i) => {
|
||||
indexes.push(i);
|
||||
}, "foo");
|
||||
assert(result === undefined);
|
||||
assert(indexes.length === 3);
|
||||
assert(indexes[0] === 2);
|
||||
assert(indexes[1] === 1);
|
||||
assert(indexes[2] === 0);
|
||||
|
||||
var mutable = { prop: 0 };
|
||||
result = ["foo", 1, true].reduceRight((a, v) => {
|
||||
a.prop = v;
|
||||
return a;
|
||||
}, mutable);
|
||||
assert(result === mutable);
|
||||
assert(result.prop === "foo");
|
||||
|
||||
var a1 = [1, 2];
|
||||
var a2 = null;
|
||||
a1.reduceRight((a, v, i, t) => {
|
||||
a2 = t;
|
||||
});
|
||||
assert(a1 === a2);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.reverse.length === 0);
|
||||
|
||||
var array = [1, 2, 3];
|
||||
|
||||
assert(array[0] === 1);
|
||||
assert(array[1] === 2);
|
||||
assert(array[2] === 3);
|
||||
|
||||
array.reverse();
|
||||
|
||||
assert(array[0] === 3);
|
||||
assert(array[1] === 2);
|
||||
assert(array[2] === 1);
|
||||
|
||||
var array_ref = array.reverse();
|
||||
|
||||
assert(array_ref[0] === 1);
|
||||
assert(array_ref[1] === 2);
|
||||
assert(array_ref[2] === 3);
|
||||
|
||||
assert(array[0] === 1);
|
||||
assert(array[1] === 2);
|
||||
assert(array[2] === 3);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var a = [1, 2, 3];
|
||||
var value = a.shift();
|
||||
assert(value === 1);
|
||||
assert(a.length === 2);
|
||||
assert(a[0] === 2);
|
||||
assert(a[1] === 3);
|
||||
|
||||
var a = [];
|
||||
var value = a.shift();
|
||||
assert(value === undefined);
|
||||
assert(a.length === 0);
|
||||
|
||||
assert([,].shift() === undefined);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.slice.length === 2);
|
||||
|
||||
var array = ["hello", "friends", "serenity", 1];
|
||||
|
||||
var array_slice = array.slice();
|
||||
assert(array_slice.length === array.length);
|
||||
assert(array_slice.length === 4);
|
||||
assert(array_slice[0] === "hello");
|
||||
assert(array_slice[1] === "friends");
|
||||
assert(array_slice[2] === "serenity");
|
||||
assert(array_slice[3] === 1);
|
||||
|
||||
array_slice = array.slice(1)
|
||||
assert(array_slice.length === 3);
|
||||
assert(array_slice[0] === "friends");
|
||||
assert(array_slice[1] === "serenity");
|
||||
assert(array_slice[2] === 1);
|
||||
|
||||
array_slice = array.slice(0, 2);
|
||||
assert(array_slice.length === 2);
|
||||
assert(array_slice[0] === "hello");
|
||||
assert(array_slice[1] === "friends");
|
||||
|
||||
array_slice = array.slice(-1);
|
||||
assert(array_slice.length === 1);
|
||||
assert(array_slice[0] === 1);
|
||||
|
||||
array_slice = array.slice(1, 1);
|
||||
assert(array_slice.length === 0);
|
||||
|
||||
array_slice = array.slice(1, -1);
|
||||
assert(array_slice.length === 2);
|
||||
assert(array_slice[0] === "friends");
|
||||
assert(array_slice[1] === "serenity");
|
||||
|
||||
array_slice = array.slice(2, -1);
|
||||
assert(array_slice.length === 1);
|
||||
assert(array_slice[0] === "serenity");
|
||||
|
||||
array_slice = array.slice(0, 100);
|
||||
assert(array_slice.length === 4);
|
||||
assert(array_slice[0] === "hello");
|
||||
assert(array_slice[1] === "friends");
|
||||
assert(array_slice[2] === "serenity");
|
||||
assert(array_slice[3] === 1);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
34
Libraries/LibJS/Tests/builtins/Array/Array.prototype.some.js
Normal file
34
Libraries/LibJS/Tests/builtins/Array/Array.prototype.some.js
Normal file
|
@ -0,0 +1,34 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.some.length === 1);
|
||||
|
||||
assertThrowsError(() => {
|
||||
[].some(undefined);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "undefined is not a function"
|
||||
});
|
||||
|
||||
var array = ["hello", "friends", 1, 2, false, -42, { name: "serenityos"}];
|
||||
|
||||
assert(array.some(value => value === "hello") === true);
|
||||
assert(array.some(value => value === "serenity") === false);
|
||||
assert(array.some((value, index, arr) => index === 1) === true);
|
||||
assert(array.some(value => value == "1") === true);
|
||||
assert(array.some(value => value === 1) === true);
|
||||
assert(array.some(value => value === 13) === false);
|
||||
assert(array.some(value => typeof(value) !== "string") === true);
|
||||
assert(array.some(value => typeof(value) === "boolean") === true);
|
||||
assert(array.some(value => value > 1) === true);
|
||||
assert(array.some(value => value > 1 && value < 3) === true);
|
||||
assert(array.some(value => value > 100) === false);
|
||||
assert(array.some(value => value < 0) === true);
|
||||
assert(array.some(value => array.pop()) === true);
|
||||
assert(["", "hello", "friends", "serenity"].some(value => value.length === 0) === true);
|
||||
assert([].some(value => value === 1) === false);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,87 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.splice.length === 2);
|
||||
|
||||
var array = ["hello", "friends", "serenity", 1, 2];
|
||||
var removed = array.splice(3);
|
||||
assert(array.length === 3);
|
||||
assert(array[0] === "hello");
|
||||
assert(array[1] === "friends");
|
||||
assert(array[2] === "serenity");
|
||||
assert(removed.length === 2);
|
||||
assert(removed[0] === 1);
|
||||
assert(removed[1] === 2);
|
||||
|
||||
array = ["hello", "friends", "serenity", 1, 2];
|
||||
removed = array.splice(-2);
|
||||
assert(array.length === 3);
|
||||
assert(array[0] === "hello");
|
||||
assert(array[1] === "friends");
|
||||
assert(array[2] === "serenity");
|
||||
assert(removed.length === 2);
|
||||
assert(removed[0] === 1);
|
||||
assert(removed[1] === 2);
|
||||
|
||||
array = ["hello", "friends", "serenity", 1, 2];
|
||||
removed = array.splice(-2, 1);
|
||||
assert(array.length === 4);
|
||||
assert(array[0] === "hello");
|
||||
assert(array[1] === "friends");
|
||||
assert(array[2] === "serenity");
|
||||
assert(array[3] === 2);
|
||||
assert(removed.length === 1);
|
||||
assert(removed[0] === 1);
|
||||
|
||||
array = ["serenity"];
|
||||
removed = array.splice(0, 0, "hello", "friends");
|
||||
assert(array.length === 3);
|
||||
assert(array[0] === "hello");
|
||||
assert(array[1] === "friends");
|
||||
assert(array[2] === "serenity");
|
||||
assert(removed.length === 0);
|
||||
|
||||
array = ["goodbye", "friends", "serenity"];
|
||||
removed = array.splice(0, 1, "hello");
|
||||
assert(array.length === 3);
|
||||
assert(array[0] === "hello");
|
||||
assert(array[1] === "friends");
|
||||
assert(array[2] === "serenity");
|
||||
assert(removed.length === 1);
|
||||
assert(removed[0] === "goodbye");
|
||||
|
||||
array = ["foo", "bar", "baz"];
|
||||
removed = array.splice();
|
||||
assert(array.length === 3);
|
||||
assert(array[0] === "foo");
|
||||
assert(array[1] === "bar");
|
||||
assert(array[2] === "baz");
|
||||
assert(removed.length === 0);
|
||||
|
||||
removed = array.splice(0, 123);
|
||||
assert(array.length === 0);
|
||||
assert(removed.length === 3);
|
||||
assert(removed[0] === "foo");
|
||||
assert(removed[1] === "bar");
|
||||
assert(removed[2] === "baz");
|
||||
|
||||
array = ["foo", "bar", "baz"];
|
||||
removed = array.splice(123, 123);
|
||||
assert(array.length === 3);
|
||||
assert(array[0] === "foo");
|
||||
assert(array[1] === "bar");
|
||||
assert(array[2] === "baz");
|
||||
assert(removed.length === 0);
|
||||
|
||||
array = ["foo", "bar", "baz"];
|
||||
removed = array.splice(-123, 123);
|
||||
assert(array.length === 0);
|
||||
assert(removed.length === 3);
|
||||
assert(removed[0] === "foo");
|
||||
assert(removed[1] === "bar");
|
||||
assert(removed[2] === "baz");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.toLocaleString.length === 0);
|
||||
|
||||
assert([].toLocaleString() === "");
|
||||
assert(["foo"].toLocaleString() === "foo");
|
||||
assert(["foo", "bar"].toLocaleString() === "foo,bar");
|
||||
assert(["foo", undefined, "bar", null, "baz"].toLocaleString() === "foo,,bar,,baz");
|
||||
|
||||
var toStringCalled = 0;
|
||||
var o = {
|
||||
toString: () => {
|
||||
toStringCalled++;
|
||||
return "o";
|
||||
}
|
||||
};
|
||||
assert([o, undefined, o, null, o].toLocaleString() === "o,,o,,o");
|
||||
assert(toStringCalled === 3);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var a = [1, 2, 3];
|
||||
assert(a.toString() === '1,2,3');
|
||||
assert([].toString() === '');
|
||||
assert([5].toString() === '5');
|
||||
|
||||
assert("rgb(" + [10, 11, 12] + ")" === "rgb(10,11,12)");
|
||||
|
||||
assert([undefined, null].toString() === ",");
|
||||
|
||||
a = new Array(5);
|
||||
assert(a.toString() === ",,,,");
|
||||
a[2] = "foo";
|
||||
a[4] = "bar";
|
||||
assert(a.toString() === ",,foo,,bar");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.unshift.length === 1);
|
||||
|
||||
var a = ["hello"];
|
||||
var length = a.unshift();
|
||||
assert(length === 1);
|
||||
assert(a.length === 1);
|
||||
assert(a[0] === "hello");
|
||||
|
||||
length = a.unshift("friends");
|
||||
assert(length === 2);
|
||||
assert(a.length === 2);
|
||||
assert(a[0] === "friends");
|
||||
assert(a[1] === "hello");
|
||||
|
||||
length = a.unshift(1, 2, 3);
|
||||
assert(length === 5);
|
||||
assert(a.length === 5);
|
||||
assert(a[0] === 1);
|
||||
assert(a[1] === 2);
|
||||
assert(a[2] === 3);
|
||||
assert(a[3] === "friends");
|
||||
assert(a[4] === "hello");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
63
Libraries/LibJS/Tests/builtins/Array/array-basic.js
Normal file
63
Libraries/LibJS/Tests/builtins/Array/array-basic.js
Normal file
|
@ -0,0 +1,63 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var a = [1, 2, 3];
|
||||
|
||||
assert(typeof a === "object");
|
||||
assert(a.length === 3);
|
||||
assert(a[0] === 1);
|
||||
assert(a[1] === 2);
|
||||
assert(a[2] === 3);
|
||||
|
||||
a[1] = 5;
|
||||
assert(a[1] === 5);
|
||||
assert(a.length === 3);
|
||||
|
||||
a.push(7);
|
||||
assert(a[3] === 7);
|
||||
assert(a.length === 4);
|
||||
|
||||
a = [,];
|
||||
assert(a.length === 1);
|
||||
assert(a.toString() === "");
|
||||
assert(a[0] === undefined);
|
||||
|
||||
a = [,,,,];
|
||||
assert(a.length === 4);
|
||||
assert(a.toString() === ",,,");
|
||||
assert(a[0] === undefined);
|
||||
assert(a[1] === undefined);
|
||||
assert(a[2] === undefined);
|
||||
assert(a[3] === undefined);
|
||||
|
||||
a = [1,,2,,,3,];
|
||||
assert(a.length === 6);
|
||||
assert(a.toString() === "1,,2,,,3");
|
||||
assert(a[0] === 1);
|
||||
assert(a[1] === undefined);
|
||||
assert(a[2] === 2);
|
||||
assert(a[3] === undefined);
|
||||
assert(a[4] === undefined);
|
||||
assert(a[5] === 3);
|
||||
|
||||
a = [1,,2,,,3,];
|
||||
Object.defineProperty(a, 1, {
|
||||
get() {
|
||||
return this.secret_prop;
|
||||
},
|
||||
set(value) {
|
||||
this.secret_prop = value;
|
||||
},
|
||||
});
|
||||
assert(a.length === 6);
|
||||
assert(a.toString() === "1,,2,,,3");
|
||||
assert(a.secret_prop === undefined);
|
||||
a[1] = 20;
|
||||
assert(a.length === 6);
|
||||
assert(a.toString() === "1,20,2,,,3");
|
||||
assert(a.secret_prop === 20);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
48
Libraries/LibJS/Tests/builtins/Array/array-length-setter.js
Normal file
48
Libraries/LibJS/Tests/builtins/Array/array-length-setter.js
Normal file
|
@ -0,0 +1,48 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var a = [1, 2, 3];
|
||||
|
||||
assert(a.length === 3);
|
||||
assert(a[0] === 1);
|
||||
assert(a[1] === 2);
|
||||
assert(a[2] === 3);
|
||||
|
||||
a.length = 5;
|
||||
assert(a.length === 5);
|
||||
assert(a[0] === 1);
|
||||
assert(a[1] === 2);
|
||||
assert(a[2] === 3);
|
||||
assert(a[3] === undefined);
|
||||
assert(a[4] === undefined);
|
||||
|
||||
a.length = 1;
|
||||
assert(a.length === 1);
|
||||
assert(a[0] === 1);
|
||||
|
||||
a.length = 0;
|
||||
assert(a.length === 0);
|
||||
|
||||
a.length = "42";
|
||||
assert(a.length === 42);
|
||||
|
||||
a.length = [];
|
||||
assert(a.length === 0);
|
||||
|
||||
a.length = true;
|
||||
assert(a.length === 1);
|
||||
|
||||
[undefined, "foo", -1, Infinity, -Infinity, NaN].forEach(value => {
|
||||
assertThrowsError(() => {
|
||||
a.length = value;
|
||||
}, {
|
||||
error: RangeError,
|
||||
message: "Invalid array length"
|
||||
});
|
||||
assert(a.length === 1);
|
||||
});
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var a, callbackCalled;
|
||||
|
||||
callbackCalled = 0;
|
||||
a = [1, 2, 3, 4, 5];
|
||||
a.find(() => {
|
||||
callbackCalled++;
|
||||
a.pop();
|
||||
});
|
||||
assert(callbackCalled === 5);
|
||||
|
||||
callbackCalled = 0;
|
||||
a = [1, 2, 3, 4, 5];
|
||||
a.findIndex(() => {
|
||||
callbackCalled++;
|
||||
a.pop();
|
||||
});
|
||||
assert(callbackCalled === 5);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
44
Libraries/LibJS/Tests/builtins/Array/array-spread.js
Normal file
44
Libraries/LibJS/Tests/builtins/Array/array-spread.js
Normal file
|
@ -0,0 +1,44 @@
|
|||
load("test-common.js");
|
||||
|
||||
function testArray(arr) {
|
||||
return arr.length === 4 &&
|
||||
arr[0] === 0 &&
|
||||
arr[1] === 1 &&
|
||||
arr[2] === 2 &&
|
||||
arr[3] === 3;
|
||||
}
|
||||
|
||||
try {
|
||||
let arr = [0, ...[1, 2], 3];
|
||||
assert(testArray(arr));
|
||||
|
||||
let a = [1, 2];
|
||||
arr = [0, ...a, 3];
|
||||
assert(testArray(arr));
|
||||
|
||||
let obj = { a: [1, 2] };
|
||||
arr = [0, ...obj.a, 3];
|
||||
assert(testArray(arr));
|
||||
|
||||
arr = [...[], ...[...[0, 1, 2]], 3];
|
||||
assert(testArray(arr));
|
||||
|
||||
assertThrowsError(() => {
|
||||
[...1];
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "1 is not iterable",
|
||||
});
|
||||
|
||||
|
||||
assertThrowsError(() => {
|
||||
[...{}];
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "[object Object] is not iterable",
|
||||
});
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue