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

LibJS: Implement RegExp.prototype.replace with RegExpExec abstraction

Also rename the 'rx' variable to 'regexp_object' to match other RegExp
methods.
This commit is contained in:
Timothy Flynn 2021-07-07 13:13:30 -04:00 committed by Linus Groh
parent 6c53475143
commit ec898a3370
2 changed files with 42 additions and 12 deletions

View file

@ -197,3 +197,38 @@ test("search value is coerced to a string", () => {
expect(newString).toBe("abc");
expect(coerced).toBe("x");
});
test("override exec with function", () => {
let calls = 0;
let re = /test/;
let oldExec = re.exec.bind(re);
re.exec = function (...args) {
++calls;
return oldExec(...args);
};
expect("test".replace(re, "x")).toBe("x");
expect(calls).toBe(1);
});
test("override exec with bad function", () => {
let calls = 0;
let re = /test/;
re.exec = function (...args) {
++calls;
return 4;
};
expect(() => {
"test".replace(re, "x");
}).toThrow(TypeError);
expect(calls).toBe(1);
});
test("override exec with non-function", () => {
let re = /test/;
re.exec = 3;
expect("test".replace(re, "x")).toBe("x");
});