1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:38:11 +00:00

LibJS: Convert Array tests to new testing framework

This commit is contained in:
Linus Groh 2020-07-04 18:56:48 +01:00 committed by Andreas Kling
parent 8ebdf685a6
commit 461d90d042
33 changed files with 1315 additions and 1382 deletions

View file

@ -1,28 +1,26 @@
load("test-common.js");
test("length is 1", () => {
expect(Array.isArray).toHaveLength(1);
});
try {
assert(Array.isArray.length === 1);
test("arguments that evaluate to false", () => {
expect(Array.isArray()).toBeFalse();
expect(Array.isArray("1")).toBeFalse();
expect(Array.isArray("foo")).toBeFalse();
expect(Array.isArray(1)).toBeFalse();
expect(Array.isArray(1, 2, 3)).toBeFalse();
expect(Array.isArray(undefined)).toBeFalse();
expect(Array.isArray(null)).toBeFalse();
expect(Array.isArray(Infinity)).toBeFalse();
expect(Array.isArray({})).toBeFalse();
});
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);
test("arguments that evaluate to true", () => {
expect(Array.isArray([])).toBeTrue();
expect(Array.isArray([1])).toBeTrue();
expect(Array.isArray([1, 2, 3])).toBeTrue();
expect(Array.isArray(new Array())).toBeTrue();
expect(Array.isArray(new Array(10))).toBeTrue();
expect(Array.isArray(new Array("a", "b", "c"))).toBeTrue();
// FIXME: Array.prototype is supposed to be an array!
// assert(Array.isArray(Array.prototype) === true);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}
// expect(Array.isArray(Array.prototype)).toBeTrue();
});

View file

@ -1,55 +1,53 @@
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;
},
test("constructor properties", () => {
expect(Array).toHaveLength(1);
expect(Array.name).toBe("Array");
expect(Array.prototype.length).toBe(0);
});
assert(a.toString() === "1,2,3,10");
describe("errors", () => {
test("invalid array length", () => {
[-1, -100, -0.1, 0.1, 1.23, Infinity, -Infinity, NaN].forEach(value => {
assertThrowsError(
() => {
expect(() => {
new Array(value);
},
{
error: TypeError,
message: "Invalid array length",
}
);
}).toThrowWithMessage(TypeError, "Invalid array length");
});
});
});
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}
describe("normal behavior", () => {
test("typeof", () => {
expect(typeof Array()).toBe("object");
expect(typeof new Array()).toBe("object");
});
test("constructor with single numeric argument", () => {
var a = new Array(5);
expect(a instanceof Array).toBeTrue();
expect(a).toHaveLength(5);
});
test("constructor with single non-numeric argument", () => {
var a = new Array("5");
expect(a instanceof Array).toBeTrue();
expect(a).toHaveLength(1);
expect(a[0]).toBe("5");
});
test("constructor with multiple numeric arguments", () => {
var a = new Array(1, 2, 3);
expect(a instanceof Array).toBeTrue();
expect(a).toHaveLength(3);
expect(a[0]).toBe(1);
expect(a[1]).toBe(2);
expect(a[2]).toBe(3);
});
test("constructor with single array argument", () => {
var a = new Array([1, 2, 3]);
expect(a instanceof Array).toBeTrue();
expect(a).toHaveLength(1);
expect(a[0][0]).toBe(1);
expect(a[0][1]).toBe(2);
expect(a[0][2]).toBe(3);
});
});

View file

@ -1,50 +1,59 @@
load("test-common.js");
test("length is 0", () => {
expect(Array.of).toHaveLength(0);
});
try {
assert(Array.of.length === 0);
describe("normal behavior", () => {
test("single numeric argument", () => {
var a = Array.of(5);
expect(a instanceof Array).toBeTrue();
expect(a).toHaveLength(1);
expect(a[0]).toBe(5);
});
assert(typeof Array.of() === "object");
test("single non-numeric argument", () => {
var a = Array.of("5");
expect(a instanceof Array).toBeTrue();
expect(a).toHaveLength(1);
expect(a[0]).toBe("5");
});
var a;
test("single infinite numeric argument", () => {
var a = Array.of(Infinity);
expect(a instanceof Array).toBeTrue();
expect(a).toHaveLength(1);
expect(a[0]).toBe(Infinity);
});
a = Array.of(5);
assert(a.length === 1);
assert(a[0] === 5);
test("multiple numeric arguments", () => {
var a = Array.of(1, 2, 3);
expect(a instanceof Array).toBeTrue();
expect(a).toHaveLength(3);
expect(a[0]).toBe(1);
expect(a[1]).toBe(2);
expect(a[2]).toBe(3);
});
a = Array.of("5");
assert(a.length === 1);
assert(a[0] === "5");
test("single array argument", () => {
var a = Array.of([1, 2, 3]);
expect(a instanceof Array).toBeTrue();
expect(a).toHaveLength(1);
expect(a[0][0]).toBe(1);
expect(a[0][1]).toBe(2);
expect(a[0][2]).toBe(3);
});
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];
test("getter property is included in returned array", () => {
var 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);
}
var a = Array.of(...t);
expect(a).toHaveLength(4);
expect(a[0]).toBe(1);
expect(a[1]).toBe(2);
expect(a[2]).toBe(3);
expect(a[3]).toBe(4);
});
});

View file

@ -1,164 +1,150 @@
load("test-common.js");
try {
describe("ability to work with generic non-array objects", () => {
test("push, pop", () => {
[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");
expect(Array.prototype.push.call(o, "foo")).toBe(1);
expect(o).toHaveLength(1);
expect(o[0]).toBe("foo");
expect(Array.prototype.push.call(o, "bar", "baz")).toBe(3);
expect(o).toHaveLength(3);
expect(o[0]).toBe("foo");
expect(o[1]).toBe("bar");
expect(o[2]).toBe("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);
expect(Array.prototype.pop.call(o)).toBe("baz");
expect(o).toHaveLength(2);
expect(Array.prototype.pop.call(o)).toBe("bar");
expect(o).toHaveLength(1);
expect(Array.prototype.pop.call(o)).toBe("foo");
expect(o).toHaveLength(0);
expect(Array.prototype.pop.call(o)).toBeUndefined();
expect(o).toHaveLength(0);
o.length = length;
assert(Array.prototype.pop.call(o) === undefined);
assert(o.length === 0);
expect(Array.prototype.pop.call(o)).toBeUndefined();
expect(o).toHaveLength(0);
});
});
{
test("splice", () => {
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);
}
expect(o).toHaveLength(3);
expect(o[0]).toBe("hello");
expect(o[1]).toBe("friends");
expect(o[2]).toBe("serenity");
expect(removed).toHaveLength(2);
expect(removed[0]).toBe("hello");
expect(removed[1]).toBeUndefined();
});
{
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"
test("join", () => {
expect(Array.prototype.join.call({})).toBe("");
expect(Array.prototype.join.call({ length: "foo" })).toBe("");
expect(Array.prototype.join.call({ length: 3 })).toBe(",,");
expect(Array.prototype.join.call({ length: 2, 0: "foo", 1: "bar" })).toBe("foo,bar");
expect(Array.prototype.join.call({ length: 2, 0: "foo", 1: "bar", 2: "baz" })).toBe(
"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" }, "~") ===
expect(Array.prototype.join.call({ length: 3, 1: "bar" }, "~")).toBe("~bar~");
expect(Array.prototype.join.call({ length: 3, 0: "foo", 1: "bar", 2: "baz" }, "~")).toBe(
"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");
}
// FIXME: test-js asserts when this is just called "toString" ಠ_ಠ
test("toString (FIXME)", () => {
expect(Array.prototype.toString.call({})).toBe("[object Object]");
expect(Array.prototype.toString.call({ join: "foo" })).toBe("[object Object]");
expect(Array.prototype.toString.call({ join: () => "foo" })).toBe("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);
}
test("indexOf", () => {
expect(Array.prototype.indexOf.call({})).toBe(-1);
expect(Array.prototype.indexOf.call({ 0: undefined })).toBe(-1);
expect(Array.prototype.indexOf.call({ length: 1, 0: undefined })).toBe(0);
expect(Array.prototype.indexOf.call({ length: 1, 2: "foo" }, "foo")).toBe(-1);
expect(Array.prototype.indexOf.call({ length: 5, 2: "foo" }, "foo")).toBe(2);
expect(Array.prototype.indexOf.call({ length: 5, 2: "foo", 4: "foo" }, "foo", 3)).toBe(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
test("lastIndexOf", () => {
expect(Array.prototype.lastIndexOf.call({})).toBe(-1);
expect(Array.prototype.lastIndexOf.call({ 0: undefined })).toBe(-1);
expect(Array.prototype.lastIndexOf.call({ length: 1, 0: undefined })).toBe(0);
expect(Array.prototype.lastIndexOf.call({ length: 1, 2: "foo" }, "foo")).toBe(-1);
expect(Array.prototype.lastIndexOf.call({ length: 5, 2: "foo" }, "foo")).toBe(2);
expect(Array.prototype.lastIndexOf.call({ length: 5, 2: "foo", 4: "foo" }, "foo")).toBe(4);
expect(Array.prototype.lastIndexOf.call({ length: 5, 2: "foo", 4: "foo" }, "foo", -2)).toBe(
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);
}
test("includes", () => {
expect(Array.prototype.includes.call({})).toBeFalse();
expect(Array.prototype.includes.call({ 0: undefined })).toBeFalse();
expect(Array.prototype.includes.call({ length: 1, 0: undefined })).toBeTrue();
expect(Array.prototype.includes.call({ length: 1, 2: "foo" }, "foo")).toBeFalse();
expect(Array.prototype.includes.call({ length: 5, 2: "foo" }, "foo")).toBeTrue();
});
const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
{
assertVisitsAll(
visit => {
Array.prototype.every.call(o, function (value) {
visit(value);
test("every", () => {
const visited = [];
Array.prototype.every.call(o, value => {
visited.push(value);
return true;
});
},
["foo", "bar", "baz"]
);
}
expect(visited).toEqual(["foo", "bar", "baz"]);
});
test("find, findIndex", () => {
["find", "findIndex"].forEach(name => {
assertVisitsAll(
visit => {
Array.prototype[name].call(o, function (value) {
visit(value);
const visited = [];
Array.prototype[name].call(o, value => {
visited.push(value);
return false;
});
},
["foo", "bar", undefined, "baz", undefined]
);
expect(visited).toEqual(["foo", "bar", undefined, "baz", undefined]);
});
});
test("filter, forEach, map, some", () => {
["filter", "forEach", "map", "some"].forEach(name => {
assertVisitsAll(
visit => {
Array.prototype[name].call(o, function (value) {
visit(value);
const visited = [];
Array.prototype[name].call(o, value => {
visited.push(value);
return false;
});
},
["foo", "bar", "baz"]
);
expect(visited).toEqual(["foo", "bar", "baz"]);
});
{
assertVisitsAll(
visit => {
});
test("reduce", () => {
const visited = [];
Array.prototype.reduce.call(
o,
function (_, value) {
visit(value);
(_, value) => {
visited.push(value);
return false;
},
"initial"
);
},
["foo", "bar", "baz"]
);
}
expect(visited).toEqual(["foo", "bar", "baz"]);
});
{
assertVisitsAll(
visit => {
test("reduceRight", () => {
const visited = [];
Array.prototype.reduceRight.call(
o,
function (_, value) {
visit(value);
(_, value) => {
visited.push(value);
return false;
},
"initial"
);
},
["baz", "bar", "foo"]
);
}
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}
expect(visited).toEqual(["baz", "bar", "foo"]);
});
});

View file

@ -1,36 +1,43 @@
load("test-common.js");
test("length is 1", () => {
expect(Array.prototype.concat).toHaveLength(1);
});
try {
assert(Array.prototype.concat.length === 1);
describe("normal behavior", () => {
var array = ["hello"];
var array = ["hello", "friends"];
test("no arguments", () => {
var concatenated = array.concat();
expect(array).toHaveLength(1);
expect(concatenated).toHaveLength(1);
});
var array_concat = array.concat();
assert(array_concat.length === array.length);
test("single argument", () => {
var concatenated = array.concat("friends");
expect(array).toHaveLength(1);
expect(concatenated).toHaveLength(2);
expect(concatenated[0]).toBe("hello");
expect(concatenated[1]).toBe("friends");
});
array_concat = array.concat(1);
assert(array_concat.length === 3);
assert(array_concat[2] === 1);
test("single array argument", () => {
var concatenated = array.concat([1, 2, 3]);
expect(array).toHaveLength(1);
expect(concatenated).toHaveLength(4);
expect(concatenated[0]).toBe("hello");
expect(concatenated[1]).toBe(1);
expect(concatenated[2]).toBe(2);
expect(concatenated[3]).toBe(3);
});
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);
}
test("multiple arguments", () => {
var concatenated = array.concat(false, "serenity", { name: "libjs" }, [1, [2, 3]]);
expect(array).toHaveLength(1);
expect(concatenated).toHaveLength(6);
expect(concatenated[0]).toBe("hello");
expect(concatenated[1]).toBeFalse();
expect(concatenated[2]).toBe("serenity");
expect(concatenated[3]).toEqual({ name: "libjs" });
expect(concatenated[4]).toBe(1);
expect(concatenated[5]).toEqual([2, 3]);
});
});

View file

@ -1,49 +1,55 @@
load("test-common.js");
test("length is 1", () => {
expect(Array.prototype.every).toHaveLength(1);
});
try {
assert(Array.prototype.every.length === 1);
describe("errors", () => {
test("requires at least one argument", () => {
expect(() => {
[].every();
}).toThrowWithMessage(TypeError, "Array.prototype.every() requires at least one argument");
});
assertThrowsError(
() => {
test("callback must be a function", () => {
expect(() => {
[].every(undefined);
},
{
error: TypeError,
message: "undefined is not a function",
}
);
}).toThrowWithMessage(TypeError, "undefined is not a function");
});
});
describe("normal behavior", () => {
test("basic functionality", () => {
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);
expect(arrayOne.every(value => value === "hello")).toBeFalse();
expect(arrayOne.every(value => value === "serenity")).toBeFalse();
expect(arrayOne.every((value, index, arr) => index < 2)).toBeTrue();
expect(arrayOne.every(value => typeof value === "string")).toBeFalse();
expect(arrayOne.every(value => arrayOne.pop())).toBeTrue();
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);
expect(arrayTwo.every((value, index, arr) => index > 0)).toBeFalse();
expect(arrayTwo.every((value, index, arr) => index >= 0)).toBeTrue();
expect(arrayTwo.every(value => typeof value !== "string")).toBeFalse();
expect(arrayTwo.every(value => typeof value === "number")).toBeFalse();
expect(arrayTwo.every(value => value > 0)).toBeFalse();
expect(arrayTwo.every(value => value >= 0 && value < 4)).toBeTrue();
expect(arrayTwo.every(value => arrayTwo.pop())).toBeTrue();
assert(["", "hello", "friends", "serenity"].every(value => value.length >= 0) === true);
assert([].every(value => value === 1) === true);
expect(["", "hello", "friends", "serenity"].every(value => value.length >= 0)).toBeTrue();
});
arrayTwo = [true, false, 1, 2, 3, "3"];
test("empty array", () => {
expect([].every(value => value === 1)).toBeTrue();
});
// Every only goes up to the original length.
assert(
test("elements past the initial array size are ignored", () => {
var array = [1, 2, 3, 4, 5];
expect(
arrayTwo.every((value, index, arr) => {
arr.push("serenity");
return value < 4;
}) === true
);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}
arr.push(6);
return value <= 5;
})
).toBeTrue();
});
});

View file

@ -1,23 +1,20 @@
load("test-common.js");
try {
assert(Array.prototype.fill.length === 1);
test("length is 1", () => {
expect(Array.prototype.fill).toHaveLength(1);
});
test("basic functionality", () => {
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]);
expect(array.fill(0, 2, 4)).toEqual([1, 2, 0, 0]);
expect(array.fill(5, 1)).toEqual([1, 5, 5, 5]);
expect(array.fill(6)).toEqual([6, 6, 6, 6]);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}
expect([1, 2, 3].fill(4)).toEqual([4, 4, 4]);
expect([1, 2, 3].fill(4, 1)).toEqual([1, 4, 4]);
expect([1, 2, 3].fill(4, 1, 2)).toEqual([1, 4, 3]);
expect([1, 2, 3].fill(4, 3, 3)).toEqual([1, 2, 3]);
expect([1, 2, 3].fill(4, -3, -2)).toEqual([4, 2, 3]);
expect([1, 2, 3].fill(4, NaN, NaN)).toEqual([1, 2, 3]);
expect([1, 2, 3].fill(4, 3, 5)).toEqual([1, 2, 3]);
expect(Array(3).fill(4)).toEqual([4, 4, 4]);
});

View file

@ -1,45 +1,45 @@
load("test-common.js");
test("length is 1", () => {
expect(Array.prototype.filter).toHaveLength(1);
});
try {
assert(Array.prototype.filter.length === 1);
assertThrowsError(
() => {
describe("errors", () => {
test("requires at least one argument", () => {
expect(() => {
[].filter();
},
{
error: TypeError,
message: "Array.prototype.filter() requires at least one argument",
}
);
}).toThrowWithMessage(TypeError, "Array.prototype.filter() requires at least one argument");
});
assertThrowsError(
() => {
test("callback must be a function", () => {
expect(() => {
[].filter(undefined);
},
{
error: TypeError,
message: "undefined is not a function",
}
);
}).toThrowWithMessage(TypeError, "undefined is not a function");
});
});
describe("normal behavior", () => {
test("never calls callback with empty array", () => {
var callbackCalled = 0;
var callback = () => {
expect(
[].filter(() => {
callbackCalled++;
};
})
).toEqual([]);
expect(callbackCalled).toBe(0);
});
assert([].filter(callback).length === 0);
assert(callbackCalled === 0);
assert([1, 2, 3].filter(callback).length === 0);
assert(callbackCalled === 3);
test("calls callback once for every item", () => {
var callbackCalled = 0;
expect(
[1, 2, 3].filter(() => {
callbackCalled++;
})
).toEqual([]);
expect(callbackCalled).toBe(3);
});
test("can filter based on callback return value", () => {
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);
expect(evenNumbers).toEqual([0, 2, 4, 6]);
var fruits = [
"Apple",
@ -56,23 +56,13 @@ try {
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);
}
expect(filterItems(fruits, "Berry")).toEqual(["Blueberry", "Raspberry"]);
expect(filterItems(fruits, "P")).toEqual([
"Apple",
"Grape",
"Peach",
"Pineapple",
"Raspberry",
]);
});
});

View file

@ -1,54 +1,65 @@
load("test-common.js");
test("length is 1", () => {
expect(Array.prototype.find).toHaveLength(1);
});
try {
assert(Array.prototype.find.length === 1);
describe("errors", () => {
test("requires at least one argument", () => {
expect(() => {
[].find();
}).toThrowWithMessage(TypeError, "Array.prototype.find() requires at least one argument");
});
assertThrowsError(
() => {
test("callback must be a function", () => {
expect(() => {
[].find(undefined);
},
{
error: TypeError,
message: "undefined is not a function",
}
);
}).toThrowWithMessage(TypeError, "undefined is not a function");
});
});
describe("normal behavior", () => {
test("basic functionality", () => {
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);
expect(array.find(value => value === "hello")).toBe("hello");
expect(array.find((value, index, arr) => index === 1)).toBe("friends");
expect(array.find(value => value == "1")).toBe(1);
expect(array.find(value => value === 1)).toBe(1);
expect(array.find(value => typeof value !== "string")).toBe(1);
expect(array.find(value => typeof value === "boolean")).toBeFalse();
expect(array.find(value => value > 1)).toBe(2);
expect(array.find(value => value > 1 && value < 3)).toBe(2);
expect(array.find(value => value > 100)).toBeUndefined();
expect([].find(value => value === 1)).toBeUndefined();
});
test("never calls callback with empty array", () => {
var callbackCalled = 0;
var callback = () => {
expect(
[].find(() => {
callbackCalled++;
};
})
).toBeUndefined();
expect(callbackCalled).toBe(0);
});
[].find(callback);
assert(callbackCalled === 0);
test("calls callback once for every item", () => {
var callbackCalled = 0;
expect(
[1, 2, 3].find(() => {
callbackCalled++;
})
).toBeUndefined();
expect(callbackCalled).toBe(3);
});
[1, 2, 3].find(callback);
assert(callbackCalled === 3);
callbackCalled = 0;
[1, , , "foo", , undefined, , ,].find(callback);
assert(callbackCalled === 8);
callbackCalled = 0;
test("empty slots are treated as undefined", () => {
var callbackCalled = 0;
expect(
[1, , , "foo", , undefined, , ,].find(value => {
callbackCalled++;
return value === undefined;
})
).toBeUndefined();
expect(callbackCalled).toBe(2);
});
});
assert(callbackCalled === 2);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}

View file

@ -1,54 +1,68 @@
load("test-common.js");
test("length is 1", () => {
expect(Array.prototype.findIndex).toHaveLength(1);
});
try {
assert(Array.prototype.findIndex.length === 1);
assertThrowsError(
() => {
[].findIndex(undefined);
},
{
error: TypeError,
message: "undefined is not a function",
}
describe("errors", () => {
test("requires at least one argument", () => {
expect(() => {
[].findIndex();
}).toThrowWithMessage(
TypeError,
"Array.prototype.findIndex() requires at least one argument"
);
});
test("callback must be a function", () => {
expect(() => {
[].findIndex(undefined);
}).toThrowWithMessage(TypeError, "undefined is not a function");
});
});
describe("normal behavior", () => {
test("basic functionality", () => {
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);
expect(array.findIndex(value => value === "hello")).toBe(0);
expect(array.findIndex((value, index, arr) => index === 1)).toBe(1);
expect(array.findIndex(value => value == "1")).toBe(2);
expect(array.findIndex(value => value === 1)).toBe(2);
expect(array.findIndex(value => typeof value !== "string")).toBe(2);
expect(array.findIndex(value => typeof value === "boolean")).toBe(4);
expect(array.findIndex(value => value > 1)).toBe(3);
expect(array.findIndex(value => value > 1 && value < 3)).toBe(3);
expect(array.findIndex(value => value > 100)).toBe(-1);
expect([].findIndex(value => value === 1)).toBe(-1);
});
test("never calls callback with empty array", () => {
var callbackCalled = 0;
var callback = () => {
expect(
[].findIndex(() => {
callbackCalled++;
};
})
).toBe(-1);
expect(callbackCalled).toBe(0);
});
[].findIndex(callback);
assert(callbackCalled === 0);
test("calls callback once for every item", () => {
var callbackCalled = 0;
expect(
[1, 2, 3].findIndex(() => {
callbackCalled++;
})
).toBe(-1);
expect(callbackCalled).toBe(3);
});
[1, 2, 3].findIndex(callback);
assert(callbackCalled === 3);
callbackCalled = 0;
[1, , , "foo", , undefined, , ,].findIndex(callback);
assert(callbackCalled === 8);
callbackCalled = 0;
test("empty slots are treated as undefined", () => {
var callbackCalled = 0;
expect(
[1, , , "foo", , undefined, , ,].findIndex(value => {
callbackCalled++;
return value === undefined;
})
).toBe(1);
expect(callbackCalled).toBe(2);
});
});
assert(callbackCalled === 2);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}

View file

@ -1,70 +1,70 @@
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);
test("length is 1", () => {
expect(Array.prototype.forEach).toHaveLength(1);
});
callbackCalled = 0;
a.forEach(function (_, _, array) {
describe("errors", () => {
test("requires at least one argument", () => {
expect(() => {
[].forEach();
}).toThrowWithMessage(
TypeError,
"Array.prototype.forEach() requires at least one argument"
);
});
test("callback must be a function", () => {
expect(() => {
[].forEach(undefined);
}).toThrowWithMessage(TypeError, "undefined is not a function");
});
});
describe("normal behavior", () => {
test("never calls callback with empty array", () => {
var callbackCalled = 0;
expect(
[].forEach(() => {
callbackCalled++;
assert(a.length === array.length);
})
).toBeUndefined();
expect(callbackCalled).toBe(0);
});
test("calls callback once for every item", () => {
var callbackCalled = 0;
expect(
[1, 2, 3].forEach(() => {
callbackCalled++;
})
).toBeUndefined();
expect(callbackCalled).toBe(3);
});
test("callback receives value and index", () => {
var a = [1, 2, 3];
a.forEach((value, index) => {
expect(value).toBe(a[index]);
expect(index).toBe(a[index] - 1);
});
});
test("callback receives array", () => {
var callbackCalled = 0;
var a = [1, 2, 3];
a.forEach((_, _, array) => {
callbackCalled++;
expect(a).toEqual(array);
a.push("test");
});
assert(callbackCalled === 3);
assert(a.length === 6);
expect(callbackCalled).toBe(3);
expect(a).toEqual([1, 2, 3, "test", "test", "test"]);
});
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);
}
test("this value can be modified", () => {
var t = [];
[1, 2, 3].forEach(function (value) {
this.push(value);
}, t);
expect(t).toEqual([1, 2, 3]);
});
});

View file

@ -1,22 +1,18 @@
load("test-common.js");
try {
assert(Array.prototype.includes.length === 1);
test("length is 1", () => {
expect(Array.prototype.includes).toHaveLength(1);
});
test("basic functionality", () => {
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);
}
expect([].includes()).toBeFalse();
expect([undefined].includes()).toBeTrue();
expect(array.includes("hello")).toBeTrue();
expect(array.includes(1)).toBeTrue();
expect(array.includes(1, -3)).toBeTrue();
expect(array.includes("serenity")).toBeFalse();
expect(array.includes(false, -1)).toBeTrue();
expect(array.includes(2, -1)).toBeFalse();
expect(array.includes(2, -100)).toBeTrue();
expect(array.includes("friends", 100)).toBeFalse();
});

View file

@ -1,29 +1,25 @@
load("test-common.js");
try {
assert(Array.prototype.indexOf.length === 1);
test("length is 1", () => {
expect(Array.prototype.indexOf).toHaveLength(1);
});
test("basic functionality", () => {
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);
}
expect(array.indexOf("hello")).toBe(0);
expect(array.indexOf("friends")).toBe(1);
expect(array.indexOf(false)).toBe(4);
expect(array.indexOf(false, 2)).toBe(4);
expect(array.indexOf(false, -2)).toBe(4);
expect(array.indexOf(1)).toBe(2);
expect(array.indexOf(1, 1000)).toBe(-1);
expect(array.indexOf(1, -1000)).toBe(2);
expect(array.indexOf("serenity")).toBe(-1);
expect(array.indexOf(false, -1)).toBe(4);
expect(array.indexOf(2, -1)).toBe(-1);
expect(array.indexOf(2, -2)).toBe(3);
expect([].indexOf("serenity")).toBe(-1);
expect([].indexOf("serenity", 10)).toBe(-1);
expect([].indexOf("serenity", -10)).toBe(-1);
expect([].indexOf()).toBe(-1);
expect([undefined].indexOf()).toBe(0);
});

View file

@ -1,19 +1,15 @@
load("test-common.js");
test("length is 1", () => {
expect(Array.prototype.join).toHaveLength(1);
});
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);
}
test("basic functionality", () => {
expect(["hello", "friends"].join()).toBe("hello,friends");
expect(["hello", "friends"].join(" ")).toBe("hello friends");
expect(["hello", "friends", "foo"].join("~", "#")).toBe("hello~friends~foo");
expect([].join()).toBe("");
expect([null].join()).toBe("");
expect([undefined].join()).toBe("");
expect([undefined, null, ""].join()).toBe(",,");
expect([1, null, 2, undefined, 3].join()).toBe("1,,2,,3");
expect(Array(3).join()).toBe(",,");
});

View file

@ -1,26 +1,22 @@
load("test-common.js");
try {
assert(Array.prototype.lastIndexOf.length === 1);
test("length is 1", () => {
expect(Array.prototype.lastIndexOf).toHaveLength(1);
});
test("basic functionality", () => {
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);
}
expect(array.lastIndexOf("hello")).toBe(4);
expect(array.lastIndexOf("hello", 1000)).toBe(4);
expect(array.lastIndexOf(1)).toBe(3);
expect(array.lastIndexOf(1, -1)).toBe(3);
expect(array.lastIndexOf(1, -2)).toBe(3);
expect(array.lastIndexOf(2)).toBe(1);
expect(array.lastIndexOf(2, -3)).toBe(1);
expect(array.lastIndexOf(2, -4)).toBe(1);
expect([].lastIndexOf("hello")).toBe(-1);
expect([].lastIndexOf("hello", 10)).toBe(-1);
expect([].lastIndexOf("hello", -10)).toBe(-1);
expect([].lastIndexOf()).toBe(-1);
expect([undefined].lastIndexOf()).toBe(0);
expect([undefined, undefined, undefined].lastIndexOf()).toBe(2);
});

View file

@ -1,63 +1,57 @@
load("test-common.js");
test("length is 1", () => {
expect(Array.prototype.map).toHaveLength(1);
});
try {
assert(Array.prototype.map.length === 1);
assertThrowsError(
() => {
describe("errors", () => {
test("requires at least one argument", () => {
expect(() => {
[].map();
},
{
error: TypeError,
message: "Array.prototype.map() requires at least one argument",
}
);
}).toThrowWithMessage(TypeError, "Array.prototype.map() requires at least one argument");
});
assertThrowsError(
() => {
test("callback must be a function", () => {
expect(() => {
[].map(undefined);
},
{
error: TypeError,
message: "undefined is not a function",
}
);
}).toThrowWithMessage(TypeError, "undefined is not a function");
});
});
describe("normal behavior", () => {
test("never calls callback with empty array", () => {
var callbackCalled = 0;
var callback = () => {
expect(
[].map(() => {
callbackCalled++;
};
})
).toEqual([]);
expect(callbackCalled).toBe(0);
});
assert([].map(callback).length === 0);
assert(callbackCalled === 0);
test("calls callback once for every item", () => {
var callbackCalled = 0;
expect(
[1, 2, 3].map(() => {
callbackCalled++;
})
).toEqual([undefined, undefined, undefined]);
expect(callbackCalled).toBe(3);
});
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(
test("can map based on callback return value", () => {
expect(
[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]");
)
).toEqual([
"0 -> undefined",
"1 -> null",
"2 -> true",
"3 -> foo",
"4 -> 42",
"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);
}
expect(squaredNumbers).toEqual([0, 1, 4, 9, 16]);
});
});

View file

@ -1,21 +1,23 @@
load("test-common.js");
test("length is 0", () => {
expect(Array.prototype.pop).toHaveLength(0);
});
try {
describe("normal behavior", () => {
test("array with elements", () => {
var a = [1, 2, 3];
var value = a.pop();
assert(value === 3);
assert(a.length === 2);
assert(a[0] === 1);
assert(a[1] === 2);
expect(a.pop()).toBe(3);
expect(a).toEqual([1, 2]);
});
test("empty array", () => {
var a = [];
var value = a.pop();
assert(value === undefined);
assert(a.length === 0);
expect(a.pop()).toBeUndefined();
expect(a).toEqual([]);
});
assert([,].pop() === undefined);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}
test("array with empty slot", () => {
var a = [,];
expect(a.pop()).toBeUndefined();
expect(a).toEqual([]);
});
});

View file

@ -1,30 +1,23 @@
load("test-common.js");
try {
assert(Array.prototype.push.length === 1);
test("length is 1", () => {
expect(Array.prototype.push).toHaveLength(1);
});
describe("normal behavior", () => {
test("no argument", () => {
var a = ["hello"];
var length = a.push();
assert(length === 1);
assert(a.length === 1);
assert(a[0] === "hello");
expect(a.push()).toBe(1);
expect(a).toEqual(["hello"]);
});
length = a.push("friends");
assert(length === 2);
assert(a.length === 2);
assert(a[0] === "hello");
assert(a[1] === "friends");
test("single argument", () => {
var a = ["hello"];
expect(a.push("friends")).toBe(2);
expect(a).toEqual(["hello", "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);
}
test("multiple arguments", () => {
var a = ["hello", "friends"];
expect(a.push(1, 2, 3)).toBe(5);
expect(a).toEqual(["hello", "friends", 1, 2, 3]);
});
});

View file

@ -1,50 +1,37 @@
load("test-common.js");
test("length is 1", () => {
expect(Array.prototype.reduce).toHaveLength(1);
});
try {
assert(Array.prototype.reduce.length === 1);
describe("errors", () => {
test("requires at least one argument", () => {
expect(() => {
[].reduce();
}).toThrowWithMessage(TypeError, "Array.prototype.reduce() requires at least one argument");
});
assertThrowsError(
() => {
[1].reduce();
},
{
error: TypeError,
message: "Array.prototype.reduce() requires at least one argument",
}
);
test("callback must be a function", () => {
expect(() => {
[].reduce(undefined);
}).toThrowWithMessage(TypeError, "undefined is not a function");
});
assertThrowsError(
() => {
[1].reduce(undefined);
},
{
error: TypeError,
message: "undefined is not a function",
}
);
assertThrowsError(
() => {
test("reduce of empty array with no initial value", () => {
expect(() => {
[].reduce((a, x) => x);
},
{
error: TypeError,
message: "Reduce of empty array with no initial value",
}
);
}).toThrowWithMessage(TypeError, "Reduce of empty array with no initial value");
});
assertThrowsError(
() => {
test("reduce of array with only empty slots and no initial value", () => {
expect(() => {
[, ,].reduce((a, x) => x);
},
{
error: TypeError,
message: "Reduce of empty array with no initial value",
}
);
}).toThrowWithMessage(TypeError, "Reduce of empty array with no initial value");
});
});
describe("normal behavior", () => {
test("basic functionality", () => {
[1, 2].reduce(function () {
assert(this === undefined);
expect(this).toBeUndefined();
});
var callbackCalled = 0;
@ -53,80 +40,74 @@ try {
return true;
};
assert([1].reduce(callback) === 1);
assert(callbackCalled === 0);
expect([1].reduce(callback)).toBe(1);
expect(callbackCalled).toBe(0);
assert([, 1].reduce(callback) === 1);
assert(callbackCalled === 0);
expect([, 1].reduce(callback)).toBe(1);
expect(callbackCalled).toBe(0);
callbackCalled = 0;
assert([1, 2, 3].reduce(callback) === true);
assert(callbackCalled === 2);
expect([1, 2, 3].reduce(callback)).toBeTrue();
expect(callbackCalled).toBe(2);
callbackCalled = 0;
assert([, , 1, 2, 3].reduce(callback) === true);
assert(callbackCalled === 2);
expect([, , 1, 2, 3].reduce(callback)).toBeTrue();
expect(callbackCalled).toBe(2);
callbackCalled = 0;
assert([1, , , 10, , 100, , ,].reduce(callback) === true);
assert(callbackCalled === 2);
expect([1, , , 10, , 100, , ,].reduce(callback)).toBeTrue();
expect(callbackCalled).toBe(2);
var constantlySad = () => ":^(";
var result = [].reduce(constantlySad, ":^)");
assert(result === ":^)");
expect(result).toBe(":^)");
result = [":^0"].reduce(constantlySad, ":^)");
assert(result === ":^(");
expect(result).toBe(":^(");
result = [":^0"].reduce(constantlySad);
assert(result === ":^0");
expect(result).toBe(":^0");
result = [5, 4, 3, 2, 1].reduce((accum, elem) => accum + elem);
assert(result === 15);
expect(result).toBe(15);
result = [1, 2, 3, 4, 5, 6].reduce((accum, elem) => accum + elem, 100);
assert(result === 121);
expect(result).toBe(121);
result = [6, 5, 4, 3, 2, 1].reduce((accum, elem) => {
return accum + elem;
}, 100);
assert(result === 121);
expect(result).toBe(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);
expect(result).toBeUndefined();
expect(indexes.length).toBe(2);
expect(indexes[0]).toBe(1);
expect(indexes[1]).toBe(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);
expect(result).toBeUndefined();
expect(indexes).toEqual([0, 1, 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);
expect(result).toBe(mutable);
expect(result.prop).toBeTrue();
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);
}
expect(a1).toBe(a2);
});
});

View file

@ -1,50 +1,40 @@
load("test-common.js");
test("length is 1", () => {
expect(Array.prototype.reduceRight).toHaveLength(1);
});
try {
assert(Array.prototype.reduceRight.length === 1);
assertThrowsError(
() => {
[1].reduceRight();
},
{
error: TypeError,
message: "Array.prototype.reduceRight() requires at least one argument",
}
describe("errors", () => {
test("requires at least one argument", () => {
expect(() => {
[].reduceRight();
}).toThrowWithMessage(
TypeError,
"Array.prototype.reduceRight() requires at least one argument"
);
});
assertThrowsError(
() => {
[1].reduceRight(undefined);
},
{
error: TypeError,
message: "undefined is not a function",
}
);
test("callback must be a function", () => {
expect(() => {
[].reduceRight(undefined);
}).toThrowWithMessage(TypeError, "undefined is not a function");
});
assertThrowsError(
() => {
test("reduce of empty array with no initial value", () => {
expect(() => {
[].reduceRight((a, x) => x);
},
{
error: TypeError,
message: "Reduce of empty array with no initial value",
}
);
}).toThrowWithMessage(TypeError, "Reduce of empty array with no initial value");
});
assertThrowsError(
() => {
test("reduce of array with only empty slots and no initial value", () => {
expect(() => {
[, ,].reduceRight((a, x) => x);
},
{
error: TypeError,
message: "Reduce of empty array with no initial value",
}
);
}).toThrowWithMessage(TypeError, "Reduce of empty array with no initial value");
});
});
describe("normal behavior", () => {
test("basic functionality", () => {
[1, 2].reduceRight(function () {
assert(this === undefined);
expect(this).toBeUndefined();
});
var callbackCalled = 0;
@ -53,82 +43,76 @@ try {
return true;
};
assert([1].reduceRight(callback) === 1);
assert(callbackCalled === 0);
expect([1].reduceRight(callback)).toBe(1);
expect(callbackCalled).toBe(0);
assert([1].reduceRight(callback) === 1);
assert(callbackCalled === 0);
expect([1].reduceRight(callback)).toBe(1);
expect(callbackCalled).toBe(0);
callbackCalled = 0;
assert([1, 2, 3].reduceRight(callback) === true);
assert(callbackCalled === 2);
expect([1, 2, 3].reduceRight(callback)).toBe(true);
expect(callbackCalled).toBe(2);
callbackCalled = 0;
assert([1, 2, 3, ,].reduceRight(callback) === true);
assert(callbackCalled === 2);
expect([1, 2, 3, ,].reduceRight(callback)).toBe(true);
expect(callbackCalled).toBe(2);
callbackCalled = 0;
assert([, , , 1, , , 10, , 100, , ,].reduceRight(callback) === true);
assert(callbackCalled === 2);
expect([, , , 1, , , 10, , 100, , ,].reduceRight(callback)).toBe(true);
expect(callbackCalled).toBe(2);
var constantlySad = () => ":^(";
var result = [].reduceRight(constantlySad, ":^)");
assert(result === ":^)");
expect(result).toBe(":^)");
result = [":^0"].reduceRight(constantlySad, ":^)");
assert(result === ":^(");
expect(result).toBe(":^(");
result = [":^0"].reduceRight(constantlySad);
assert(result === ":^0");
expect(result).toBe(":^0");
result = [5, 4, 3, 2, 1].reduceRight((accum, elem) => "" + accum + elem);
assert(result === "12345");
expect(result).toBe("12345");
result = [1, 2, 3, 4, 5, 6].reduceRight((accum, elem) => {
return "" + accum + elem;
}, 100);
assert(result === "100654321");
expect(result).toBe("100654321");
result = [6, 5, 4, 3, 2, 1].reduceRight((accum, elem) => {
return "" + accum + elem;
}, 100);
assert(result === "100123456");
expect(result).toBe("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);
expect(result).toBeUndefined();
expect(indexes.length).toBe(2);
expect(indexes[0]).toBe(1);
expect(indexes[1]).toBe(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);
expect(result).toBeUndefined();
expect(indexes).toEqual([2, 1, 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");
expect(result).toBe(mutable);
expect(result.prop).toBe("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);
}
expect(a1).toBe(a2);
});
});

View file

@ -1,31 +1,9 @@
load("test-common.js");
try {
assert(Array.prototype.reverse.length === 0);
test("length is 0", () => {
expect(Array.prototype.reverse).toHaveLength(0);
});
test("basic functionality", () => {
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);
}
expect(array.reverse()).toEqual([3, 2, 1]);
expect(array).toEqual([3, 2, 1]);
});

View file

@ -1,21 +1,23 @@
load("test-common.js");
test("length is 0", () => {
expect(Array.prototype.shift).toHaveLength(0);
});
try {
describe("normal behavior", () => {
test("array with elements", () => {
var a = [1, 2, 3];
var value = a.shift();
assert(value === 1);
assert(a.length === 2);
assert(a[0] === 2);
assert(a[1] === 3);
expect(a.shift()).toBe(1);
expect(a).toEqual([2, 3]);
});
test("empty array", () => {
var a = [];
var value = a.shift();
assert(value === undefined);
assert(a.length === 0);
expect(a.shift()).toBeUndefined();
expect(a).toEqual([]);
});
assert([,].shift() === undefined);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}
test("array with empty slot", () => {
var a = [,];
expect(a.shift()).toBeUndefined();
expect(a).toEqual([]);
});
});

View file

@ -1,53 +1,39 @@
load("test-common.js");
try {
assert(Array.prototype.slice.length === 2);
test("length is 0", () => {
expect(Array.prototype.slice).toHaveLength(2);
});
test("basic functionality", () => {
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);
var slice = array.slice();
expect(array).toEqual(["hello", "friends", "serenity", 1]);
expect(slice).toEqual(["hello", "friends", "serenity", 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);
slice = array.slice(1);
expect(array).toEqual(["hello", "friends", "serenity", 1]);
expect(slice).toEqual(["friends", "serenity", 1]);
array_slice = array.slice(0, 2);
assert(array_slice.length === 2);
assert(array_slice[0] === "hello");
assert(array_slice[1] === "friends");
slice = array.slice(0, 2);
expect(array).toEqual(["hello", "friends", "serenity", 1]);
expect(slice).toEqual(["hello", "friends"]);
array_slice = array.slice(-1);
assert(array_slice.length === 1);
assert(array_slice[0] === 1);
slice = array.slice(-1);
expect(array).toEqual(["hello", "friends", "serenity", 1]);
expect(slice).toEqual([1]);
array_slice = array.slice(1, 1);
assert(array_slice.length === 0);
slice = array.slice(1, 1);
expect(array).toEqual(["hello", "friends", "serenity", 1]);
expect(slice).toEqual([]);
array_slice = array.slice(1, -1);
assert(array_slice.length === 2);
assert(array_slice[0] === "friends");
assert(array_slice[1] === "serenity");
slice = array.slice(1, -1);
expect(array).toEqual(["hello", "friends", "serenity", 1]);
expect(slice).toEqual(["friends", "serenity"]);
array_slice = array.slice(2, -1);
assert(array_slice.length === 1);
assert(array_slice[0] === "serenity");
slice = array.slice(2, -1);
expect(array).toEqual(["hello", "friends", "serenity", 1]);
expect(slice).toEqual(["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);
}
slice = array.slice(0, 100);
expect(array).toEqual(["hello", "friends", "serenity", 1]);
expect(slice).toEqual(["hello", "friends", "serenity", 1]);
});

View file

@ -1,37 +1,37 @@
load("test-common.js");
test("length is 1", () => {
expect(Array.prototype.some).toHaveLength(1);
});
try {
assert(Array.prototype.some.length === 1);
describe("errors", () => {
test("requires at least one argument", () => {
expect(() => {
[].some();
}).toThrowWithMessage(TypeError, "Array.prototype.some() requires at least one argument");
});
assertThrowsError(
() => {
test("callback must be a function", () => {
expect(() => {
[].some(undefined);
},
{
error: TypeError,
message: "undefined is not a function",
}
);
}).toThrowWithMessage(TypeError, "undefined is not a function");
});
});
test("basic functionality", () => {
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);
}
expect(array.some(value => value === "hello")).toBeTrue();
expect(array.some(value => value === "serenity")).toBeFalse();
expect(array.some((value, index, arr) => index === 1)).toBeTrue();
expect(array.some(value => value == "1")).toBeTrue();
expect(array.some(value => value === 1)).toBeTrue();
expect(array.some(value => value === 13)).toBeFalse();
expect(array.some(value => typeof value !== "string")).toBeTrue();
expect(array.some(value => typeof value === "boolean")).toBeTrue();
expect(array.some(value => value > 1)).toBeTrue();
expect(array.some(value => value > 1 && value < 3)).toBeTrue();
expect(array.some(value => value > 100)).toBeFalse();
expect(array.some(value => value < 0)).toBeTrue();
expect(array.some(value => array.pop())).toBeTrue();
expect(["", "hello", "friends", "serenity"].some(value => value.length === 0)).toBeTrue();
expect([].some(value => value === 1)).toBeFalse();
});

View file

@ -1,87 +1,49 @@
load("test-common.js");
try {
assert(Array.prototype.splice.length === 2);
test("length is 2", () => {
expect(Array.prototype.splice).toHaveLength(2);
});
test("basic functionality", () => {
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);
expect(array).toEqual(["hello", "friends", "serenity"]);
expect(removed).toEqual([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);
expect(array).toEqual(["hello", "friends", "serenity"]);
expect(removed).toEqual([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);
expect(array).toEqual(["hello", "friends", "serenity", 2]);
expect(removed).toEqual([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);
expect(array).toEqual(["hello", "friends", "serenity"]);
expect(removed).toEqual([]);
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");
expect(array).toEqual(["hello", "friends", "serenity"]);
expect(removed).toEqual(["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);
expect(array).toEqual(["foo", "bar", "baz"]);
expect(removed).toEqual([]);
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");
expect(array).toEqual([]);
expect(removed).toEqual(["foo", "bar", "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);
expect(array).toEqual(["foo", "bar", "baz"]);
expect(removed).toEqual([]);
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);
}
expect(array).toEqual([]);
expect(removed).toEqual(["foo", "bar", "baz"]);
});

View file

@ -1,24 +1,60 @@
load("test-common.js");
test("length is 0", () => {
expect(Array.prototype.toLocaleString).toHaveLength(0);
});
try {
assert(Array.prototype.toLocaleString.length === 0);
describe("normal behavior", () => {
test("array with no elements", () => {
expect([].toLocaleString()).toBe("");
});
assert([].toLocaleString() === "");
assert(["foo"].toLocaleString() === "foo");
assert(["foo", "bar"].toLocaleString() === "foo,bar");
assert(["foo", undefined, "bar", null, "baz"].toLocaleString() === "foo,,bar,,baz");
test("array with one element", () => {
expect(["foo"].toLocaleString()).toBe("foo");
});
test("array with multiple elements", () => {
expect(["foo", "bar", "baz"].toLocaleString()).toBe("foo,bar,baz");
});
test("number stringification differs from regular toString, for now", () => {
expect([1, 2, 3].toLocaleString()).toBe(
"[object NumberObject],[object NumberObject],[object NumberObject]"
);
});
test("null and undefined result in empty strings", () => {
expect([null].toLocaleString()).toBe("");
expect([undefined].toLocaleString()).toBe("");
expect([undefined, null].toLocaleString()).toBe(",");
});
test("empty values result in empty strings", () => {
expect(new Array(1).toLocaleString()).toBe("");
expect(new Array(3).toLocaleString()).toBe(",,");
var a = new Array(5);
a[2] = "foo";
a[4] = "bar";
expect(a.toLocaleString()).toBe(",,foo,,bar");
});
test("getter property is included in returned string", () => {
var a = ["foo"];
Object.defineProperty(a, 1, {
get() {
return "bar";
},
});
expect(a.toLocaleString()).toBe("foo,bar");
});
test("array with elements that have a custom toString() function", () => {
var toStringCalled = 0;
var o = {
toString: () => {
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);
}
expect([o, undefined, o, null, o].toLocaleString()).toBe("o,,o,,o");
expect(toStringCalled).toBe(3);
});
});

View file

@ -1,22 +1,58 @@
load("test-common.js");
test("length is 0", () => {
expect(Array.prototype.toString).toHaveLength(0);
});
try {
var a = [1, 2, 3];
assert(a.toString() === "1,2,3");
assert([].toString() === "");
assert([5].toString() === "5");
describe("normal behavior", () => {
test("array with no elements", () => {
expect([].toString()).toBe("");
});
assert("rgb(" + [10, 11, 12] + ")" === "rgb(10,11,12)");
test("array with one element", () => {
expect([1].toString()).toBe("1");
});
assert([undefined, null].toString() === ",");
test("array with multiple elements", () => {
expect([1, 2, 3].toString()).toBe("1,2,3");
});
a = new Array(5);
assert(a.toString() === ",,,,");
test("string and array concatenation", () => {
expect("rgb(" + [10, 11, 12] + ")").toBe("rgb(10,11,12)");
});
test("null and undefined result in empty strings", () => {
expect([null].toString()).toBe("");
expect([undefined].toString()).toBe("");
expect([undefined, null].toString()).toBe(",");
});
test("empty values result in empty strings", () => {
expect(new Array(1).toString()).toBe("");
expect(new Array(3).toString()).toBe(",,");
var a = new Array(5);
a[2] = "foo";
a[4] = "bar";
assert(a.toString() === ",,foo,,bar");
expect(a.toString()).toBe(",,foo,,bar");
});
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}
test("getter property is included in returned string", () => {
var a = [1, 2, 3];
Object.defineProperty(a, 3, {
get() {
return 10;
},
});
expect(a.toString()).toBe("1,2,3,10");
});
test("array with elements that have a custom toString() function", () => {
var toStringCalled = 0;
var o = {
toString() {
toStringCalled++;
return "o";
},
};
expect([o, undefined, o, null, o].toString()).toBe("o,,o,,o");
expect(toStringCalled).toBe(3);
});
});

View file

@ -1,30 +1,23 @@
load("test-common.js");
try {
assert(Array.prototype.unshift.length === 1);
test("length is 1", () => {
expect(Array.prototype.unshift).toHaveLength(1);
});
describe("normal behavior", () => {
test("no argument", () => {
var a = ["hello"];
var length = a.unshift();
assert(length === 1);
assert(a.length === 1);
assert(a[0] === "hello");
expect(a.unshift()).toBe(1);
expect(a).toEqual(["hello"]);
});
length = a.unshift("friends");
assert(length === 2);
assert(a.length === 2);
assert(a[0] === "friends");
assert(a[1] === "hello");
test("single argument", () => {
var a = ["hello"];
expect(a.unshift("friends")).toBe(2);
expect(a).toEqual(["friends", "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);
}
test("multiple arguments", () => {
var a = ["friends", "hello"];
expect(a.unshift(1, 2, 3)).toBe(5);
expect(a).toEqual([1, 2, 3, "friends", "hello"]);
});
});

View file

@ -1,63 +1,57 @@
load("test-common.js");
try {
test("basic functionality", () => {
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);
expect(typeof a).toBe("object");
expect(a).toHaveLength(3);
expect(a[0]).toBe(1);
expect(a[1]).toBe(2);
expect(a[2]).toBe(3);
a[1] = 5;
assert(a[1] === 5);
assert(a.length === 3);
expect(a[1]).toBe(5);
expect(a).toHaveLength(3);
a.push(7);
assert(a[3] === 7);
assert(a.length === 4);
expect(a[3]).toBe(7);
expect(a).toHaveLength(4);
a = [,];
assert(a.length === 1);
assert(a.toString() === "");
assert(a[0] === undefined);
expect(a).toHaveLength(1);
expect(a.toString()).toBe("");
expect(a[0]).toBeUndefined();
a = [, , , ,];
assert(a.length === 4);
assert(a.toString() === ",,,");
assert(a[0] === undefined);
assert(a[1] === undefined);
assert(a[2] === undefined);
assert(a[3] === undefined);
expect(a).toHaveLength(4);
expect(a.toString()).toBe(",,,");
expect(a[0]).toBeUndefined();
expect(a[1]).toBeUndefined();
expect(a[2]).toBeUndefined();
expect(a[3]).toBeUndefined();
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);
expect(a).toHaveLength(6);
expect(a.toString()).toBe("1,,2,,,3");
expect(a[0]).toBe(1);
expect(a[1]).toBeUndefined();
expect(a[2]).toBe(2);
expect(a[3]).toBeUndefined();
expect(a[4]).toBeUndefined();
expect(a[5]).toBe(3);
a = [1, , 2, , , 3];
Object.defineProperty(a, 1, {
get() {
return this.secret_prop;
return this.getterSetterValue;
},
set(value) {
this.secret_prop = value;
this.getterSetterValue = value;
},
});
assert(a.length === 6);
assert(a.toString() === "1,,2,,,3");
assert(a.secret_prop === undefined);
expect(a).toHaveLength(6);
expect(a.toString()).toBe("1,,2,,,3");
expect(a.getterSetterValue).toBeUndefined();
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);
}
expect(a).toHaveLength(6);
expect(a.toString()).toBe("1,20,2,,,3");
expect(a.getterSetterValue).toBe(20);
});

View file

@ -1,51 +1,37 @@
load("test-common.js");
try {
describe("errors", () => {
test("invalid array length value", () => {
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(
() => {
expect(() => {
a.length = value;
},
{
error: RangeError,
message: "Invalid array length",
}
);
assert(a.length === 1);
}).toThrowWithMessage(RangeError, "Invalid array length");
expect(a).toHaveLength(3);
});
});
});
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}
describe("normal behavior", () => {
test("extend array by setting length", () => {
var a = [1, 2, 3];
a.length = 5;
expect(a).toEqual([1, 2, 3, undefined, undefined]);
});
test("truncate array by setting length", () => {
var a = [1, 2, 3];
a.length = 2;
expect(a).toEqual([1, 2]);
a.length = 0;
expect(a).toEqual([]);
});
test("length value is coerced to number if possible", () => {
var a = [1, 2, 3];
a.length = "42";
expect(a).toHaveLength(42);
a.length = [];
expect(a).toHaveLength(0);
a.length = true;
expect(a).toHaveLength(1);
});
});

View file

@ -1,6 +1,4 @@
load("test-common.js");
try {
test("Issue #1992, shrinking array during find() iteration", () => {
var a, callbackCalled;
callbackCalled = 0;
@ -9,7 +7,7 @@ try {
callbackCalled++;
a.pop();
});
assert(callbackCalled === 5);
expect(callbackCalled).toBe(5);
callbackCalled = 0;
a = [1, 2, 3, 4, 5];
@ -17,9 +15,5 @@ try {
callbackCalled++;
a.pop();
});
assert(callbackCalled === 5);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}
expect(callbackCalled).toBe(5);
});

View file

@ -1,45 +1,25 @@
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(
() => {
describe("errors", () => {
test("cannot spread number in array", () => {
expect(() => {
[...1];
},
{
error: TypeError,
message: "1 is not iterable",
}
);
}).toThrowWithMessage(TypeError, "1 is not iterable");
});
assertThrowsError(
() => {
test("cannot spread object in array", () => {
expect(() => {
[...{}];
},
{
error: TypeError,
message: "[object Object] is not iterable",
}
);
}).toThrowWithMessage(TypeError, "[object Object] is not iterable");
});
});
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}
test("basic functionality", () => {
expect([1, ...[2, 3], 4]).toEqual([1, 2, 3, 4]);
let a = [2, 3];
expect([1, ...a, 4]).toEqual([1, 2, 3, 4]);
let obj = { a: [2, 3] };
expect([1, ...obj.a, 4]).toEqual([1, 2, 3, 4]);
expect([...[], ...[...[1, 2, 3]], 4]).toEqual([1, 2, 3, 4]);
});

View file

@ -43,6 +43,38 @@
// FIXME: Will eventually not be necessary when all tests are converted
Vector<String> tests_to_run = {
"builtins/Array/Array.js",
"builtins/Array/array-basic.js",
"builtins/Array/array-length-setter.js",
"builtins/Array/array-shrink-during-find-crash.js",
"builtins/Array/array-spread.js",
"builtins/Array/Array.isArray.js",
"builtins/Array/Array.of.js",
"builtins/Array/Array.prototype-generic-functions.js",
"builtins/Array/Array.prototype.concat.js",
"builtins/Array/Array.prototype.every.js",
"builtins/Array/Array.prototype.fill.js",
"builtins/Array/Array.prototype.filter.js",
"builtins/Array/Array.prototype.find.js",
"builtins/Array/Array.prototype.findIndex.js",
"builtins/Array/Array.prototype.forEach.js",
"builtins/Array/Array.prototype.includes.js",
"builtins/Array/Array.prototype.indexOf.js",
"builtins/Array/Array.prototype.join.js",
"builtins/Array/Array.prototype.lastIndexOf.js",
"builtins/Array/Array.prototype.map.js",
"builtins/Array/Array.prototype.pop.js",
"builtins/Array/Array.prototype.push.js",
"builtins/Array/Array.prototype.reduce.js",
"builtins/Array/Array.prototype.reduceRight.js",
"builtins/Array/Array.prototype.reverse.js",
"builtins/Array/Array.prototype.shift.js",
"builtins/Array/Array.prototype.slice.js",
"builtins/Array/Array.prototype.some.js",
"builtins/Array/Array.prototype.splice.js",
"builtins/Array/Array.prototype.toLocaleString.js",
"builtins/Array/Array.prototype.toString.js",
"builtins/Array/Array.prototype.unshift.js",
"builtins/BigInt/BigInt.js",
"builtins/BigInt/bigint-basic.js",
"builtins/BigInt/bigint-number-mix-errors.js",