mirror of
https://github.com/RGBCube/serenity
synced 2025-05-24 01:05:08 +00:00
LibJS: Convert Array tests to new testing framework
This commit is contained in:
parent
8ebdf685a6
commit
461d90d042
33 changed files with 1315 additions and 1382 deletions
|
@ -1,51 +1,37 @@
|
|||
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(
|
||||
() => {
|
||||
describe("errors", () => {
|
||||
test("invalid array length value", () => {
|
||||
var a = [1, 2, 3];
|
||||
[undefined, "foo", -1, Infinity, -Infinity, NaN].forEach(value => {
|
||||
expect(() => {
|
||||
a.length = value;
|
||||
},
|
||||
{
|
||||
error: RangeError,
|
||||
message: "Invalid array length",
|
||||
}
|
||||
);
|
||||
assert(a.length === 1);
|
||||
}).toThrowWithMessage(RangeError, "Invalid array length");
|
||||
expect(a).toHaveLength(3);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
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]);
|
||||
});
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue