mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 10:57:35 +00:00
LibJS: Implement RegExp.prototype [ @@search ]
String.prototype.search is already implemented, but relies on the well- known Symbol.search, which was not implemented.
This commit is contained in:
parent
aaf5339fae
commit
35a2ba8ed8
3 changed files with 97 additions and 0 deletions
|
@ -0,0 +1,47 @@
|
|||
test("basic functionality", () => {
|
||||
expect(String.prototype.search).toHaveLength(1);
|
||||
|
||||
expect("hello friends".search("h")).toBe(0);
|
||||
expect("hello friends".search("e")).toBe(1);
|
||||
expect("hello friends".search("l")).toBe(2);
|
||||
expect("hello friends".search("o")).toBe(4);
|
||||
expect("hello friends".search("z")).toBe(-1);
|
||||
|
||||
expect("abc123def".search(/\d/)).toBe(3);
|
||||
expect("abcdef".search(/\d/)).toBe(-1);
|
||||
});
|
||||
|
||||
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".search(re)).toBe(0);
|
||||
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".search(re);
|
||||
}).toThrow(TypeError);
|
||||
expect(calls).toBe(1);
|
||||
});
|
||||
|
||||
test("override exec with non-function", () => {
|
||||
let re = /test/;
|
||||
re.exec = 3;
|
||||
expect("test".search(re)).toBe(0);
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue