|
| 1 | +import "../src/global"; |
| 2 | +import {RE2JS} from "re2js"; |
| 3 | + |
| 4 | +const tests = [ |
| 5 | + { |
| 6 | + paragraph: "hello world", |
| 7 | + regexp: "world", |
| 8 | + description: "basic string pattern", |
| 9 | + }, |
| 10 | + { |
| 11 | + paragraph: "hello world", |
| 12 | + regexp: "foo", |
| 13 | + description: "no match found", |
| 14 | + }, |
| 15 | + { |
| 16 | + paragraph: "foo bar foo baz foo", |
| 17 | + regexp: "foo", |
| 18 | + description: "multiple matches", |
| 19 | + }, |
| 20 | + { |
| 21 | + paragraph: "abc123def456", |
| 22 | + regexp: "(\\d+)", |
| 23 | + description: "match with capturing group", |
| 24 | + }, |
| 25 | + { |
| 26 | + paragraph: "color: #ff0000; background: #00ff00;", |
| 27 | + regexp: "#(?<hex>[0-9a-fA-F]{6})", |
| 28 | + description: "named capturing group", |
| 29 | + }, |
| 30 | + { |
| 31 | + paragraph: "aaaa", |
| 32 | + regexp: "aa", |
| 33 | + description: "overlapping matches", |
| 34 | + }, |
| 35 | + { |
| 36 | + paragraph: "", |
| 37 | + regexp: "a", |
| 38 | + description: "empty string", |
| 39 | + }, |
| 40 | + { |
| 41 | + paragraph: "abc123abc", |
| 42 | + regexp: "^abc|abc$", |
| 43 | + description: "match at start or end", |
| 44 | + }, |
| 45 | + { |
| 46 | + paragraph: "a.b*c+d?", |
| 47 | + regexp: "\\.", |
| 48 | + description: "special character dot", |
| 49 | + }, |
| 50 | +]; |
| 51 | + |
| 52 | +describe("matchAllRE2JS should behave similarly to matchAll", () => { |
| 53 | + tests.forEach((t) => { |
| 54 | + test(t.description, () => { |
| 55 | + const matchAll = Array.from(t.paragraph.matchAll(new RegExp(t.regexp, "g"))); |
| 56 | + const matchAllRE2JS = Array.from(t.paragraph.matchAllRE2JS(RE2JS.compile(t.regexp))); |
| 57 | + expect(matchAllRE2JS).toStrictEqual(matchAll); |
| 58 | + }); |
| 59 | + }); |
| 60 | +}); |
0 commit comments