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

LibJS: Implement RegExp.prototype [ @@matchAll ]

This also allows String.prototype.matchAll to work, as all calls to that
method result in an invocation to @@matchAll.
This commit is contained in:
Timothy Flynn 2021-07-15 09:13:56 -04:00 committed by Linus Groh
parent cfddcad7cf
commit 5135f4000c
3 changed files with 130 additions and 0 deletions

View file

@ -0,0 +1,78 @@
test("invariants", () => {
expect(String.prototype.matchAll).toHaveLength(1);
});
test("error cases", () => {
[null, undefined].forEach(value => {
expect(() => {
value.matchAll("");
}).toThrow(TypeError);
});
expect(() => {
"hello friends".matchAll(/hello/);
}).toThrow(TypeError);
});
test("basic functionality", () => {
expect("hello friends".matchAll(/hello/g)).not.toBeNull();
expect("hello friends".matchAll(/enemies/g)).not.toBeNull();
{
var iterator = "".matchAll(/a/g);
var next = iterator.next();
expect(next.done).toBeTrue();
expect(next.value).toBeUndefined();
next = iterator.next();
expect(next.done).toBeTrue();
expect(next.value).toBeUndefined();
}
{
var iterator = "a".matchAll(/a/g);
var next = iterator.next();
expect(next.done).toBeFalse();
expect(next.value).toEqual(["a"]);
expect(next.value.index).toBe(0);
next = iterator.next();
expect(next.done).toBeTrue();
expect(next.value).toBeUndefined();
}
{
var iterator = "aa".matchAll(/a/g);
var next = iterator.next();
expect(next.done).toBeFalse();
expect(next.value).toEqual(["a"]);
expect(next.value.index).toBe(0);
next = iterator.next();
expect(next.done).toBeFalse();
expect(next.value).toEqual(["a"]);
expect(next.value.index).toBe(1);
next = iterator.next();
expect(next.done).toBeTrue();
expect(next.value).toBeUndefined();
}
{
var iterator = "aba".matchAll(/a/g);
var next = iterator.next();
expect(next.done).toBeFalse();
expect(next.value).toEqual(["a"]);
expect(next.value.index).toBe(0);
next = iterator.next();
expect(next.done).toBeFalse();
expect(next.value).toEqual(["a"]);
expect(next.value.index).toBe(2);
next = iterator.next();
expect(next.done).toBeTrue();
expect(next.value).toBeUndefined();
}
});