1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 07:47:37 +00:00

LibJS: Implement Array.prototype.toReversed()

This commit is contained in:
Linus Groh 2022-06-13 07:57:38 +01:00
parent e2a5a27302
commit e4370b7d82
5 changed files with 85 additions and 0 deletions

View file

@ -252,6 +252,7 @@ describe("ability to work with generic non-array objects", () => {
});
test("reverse", () => {
const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
expect(Array.prototype.reverse.call(o)).toEqual({
length: 5,
4: "foo",
@ -341,4 +342,10 @@ describe("ability to work with generic non-array objects", () => {
const trueResult = result.get(trueObject);
expect(trueResult).toEqual(["bar", "baz"]);
});
test("toReversed", () => {
const result = Array.prototype.toReversed.call(o);
expect(result).toEqual([undefined, "baz", undefined, "bar", "foo"]);
expect(result).not.toBe(o);
});
});

View file

@ -0,0 +1,39 @@
describe("normal behavior", () => {
test("length is 0", () => {
expect(Array.prototype.toReversed).toHaveLength(0);
});
test("basic functionality", () => {
const a = [1, 2, 3, 4, 5];
const b = a.toReversed();
expect(a).not.toBe(b);
expect(a).toEqual([1, 2, 3, 4, 5]);
expect(b).toEqual([5, 4, 3, 2, 1]);
});
test("is unscopable", () => {
expect(Array.prototype[Symbol.unscopables].toReversed).toBeTrue();
const array = [];
with (array) {
expect(() => {
toReversed;
}).toThrowWithMessage(ReferenceError, "'toReversed' is not defined");
}
});
});
describe("errors", () => {
test("null or undefined this value", () => {
expect(() => {
Array.prototype.toReversed.call();
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
expect(() => {
Array.prototype.toReversed.call(undefined);
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
expect(() => {
Array.prototype.toReversed.call(null);
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
});
});