mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 05:27: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:
parent
6c53475143
commit
ec898a3370
2 changed files with 42 additions and 12 deletions
|
@ -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");
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue