mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 19:37:35 +00:00
LibJS: Implement Iterator.prototype.drop
This commit is contained in:
parent
0e2f9f006d
commit
67028ee3a3
4 changed files with 192 additions and 0 deletions
|
@ -0,0 +1,118 @@
|
|||
describe("errors", () => {
|
||||
test("called with non-numeric object", () => {
|
||||
expect(() => {
|
||||
Iterator.prototype.drop(Symbol.hasInstance);
|
||||
}).toThrowWithMessage(TypeError, "Cannot convert symbol to number");
|
||||
});
|
||||
|
||||
test("called with invalid numbers", () => {
|
||||
expect(() => {
|
||||
Iterator.prototype.drop(NaN);
|
||||
}).toThrowWithMessage(RangeError, "limit must not be NaN");
|
||||
|
||||
expect(() => {
|
||||
Iterator.prototype.drop(-1);
|
||||
}).toThrowWithMessage(RangeError, "limit must not be negative");
|
||||
});
|
||||
|
||||
test("iterator's next method throws", () => {
|
||||
function TestError() {}
|
||||
|
||||
class TestIterator extends Iterator {
|
||||
next() {
|
||||
throw new TestError();
|
||||
}
|
||||
}
|
||||
|
||||
expect(() => {
|
||||
const iterator = new TestIterator().drop(1);
|
||||
iterator.next();
|
||||
}).toThrow(TestError);
|
||||
});
|
||||
|
||||
test("value returned by iterator's next method throws", () => {
|
||||
function TestError() {}
|
||||
|
||||
class TestIterator extends Iterator {
|
||||
next() {
|
||||
return {
|
||||
done: false,
|
||||
get value() {
|
||||
throw new TestError();
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
expect(() => {
|
||||
const iterator = new TestIterator().drop(1);
|
||||
iterator.next();
|
||||
}).toThrow(TestError);
|
||||
});
|
||||
});
|
||||
|
||||
describe("normal behavior", () => {
|
||||
test("length is 1", () => {
|
||||
expect(Iterator.prototype.drop).toHaveLength(1);
|
||||
});
|
||||
|
||||
test("drop zero values", () => {
|
||||
function* generator() {
|
||||
yield "a";
|
||||
yield "b";
|
||||
}
|
||||
|
||||
const iterator = generator().drop(0);
|
||||
|
||||
let value = iterator.next();
|
||||
expect(value.value).toBe("a");
|
||||
expect(value.done).toBeFalse();
|
||||
|
||||
value = iterator.next();
|
||||
expect(value.value).toBe("b");
|
||||
expect(value.done).toBeFalse();
|
||||
|
||||
value = iterator.next();
|
||||
expect(value.value).toBeUndefined();
|
||||
expect(value.done).toBeTrue();
|
||||
});
|
||||
|
||||
test("drop fewer than the number of values", () => {
|
||||
function* generator() {
|
||||
yield "a";
|
||||
yield "b";
|
||||
yield "c";
|
||||
}
|
||||
|
||||
const iterator = generator().drop(1);
|
||||
|
||||
let value = iterator.next();
|
||||
expect(value.value).toBe("b");
|
||||
expect(value.done).toBeFalse();
|
||||
|
||||
value = iterator.next();
|
||||
expect(value.value).toBe("c");
|
||||
expect(value.done).toBeFalse();
|
||||
|
||||
value = iterator.next();
|
||||
expect(value.value).toBeUndefined();
|
||||
expect(value.done).toBeTrue();
|
||||
});
|
||||
|
||||
test("drop more than the number of values", () => {
|
||||
function* generator() {
|
||||
yield "a";
|
||||
yield "b";
|
||||
}
|
||||
|
||||
const iterator = generator().drop(Infinity);
|
||||
|
||||
let value = iterator.next();
|
||||
expect(value.value).toBeUndefined();
|
||||
expect(value.done).toBeTrue();
|
||||
|
||||
value = iterator.next();
|
||||
expect(value.value).toBeUndefined();
|
||||
expect(value.done).toBeTrue();
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue