|
1 |
| -import { RegExp } from ".."; |
2 |
| -import { expectMatch, expectNotMatch, exec } from "./utils"; |
| 1 | +import { expectMatch, expectNotMatch } from "./utils"; |
3 | 2 |
|
4 |
| -it("dot", () => { |
5 |
| - expectMatch(".", [" ", "B", "|", "9"]); |
6 |
| - expectNotMatch(".", ["", "\n"]); |
| 3 | +it("throws an error if no closing bracket is found", () => { |
| 4 | + // expect(() => new RegExp("[abce")).toThrow(); |
7 | 5 | });
|
8 | 6 |
|
9 |
| -it("digit", () => { |
10 |
| - expectMatch("\\d", ["0", "9"]); |
11 |
| - expectNotMatch("\\d", ["", "b"]); |
| 7 | +it("matches discrete characters", () => { |
| 8 | + expectMatch("[abce]", ["a", "b", "c", "e"]); |
| 9 | + expectNotMatch("[abce]", ["", "f", "h"]); |
12 | 10 | });
|
13 | 11 |
|
14 |
| -it("non-digit", () => { |
15 |
| - expectNotMatch("\\D", ["0", "9", ""]); |
16 |
| - expectMatch("\\D", ["b", "|"]); |
| 12 | +it("matches character ranges", () => { |
| 13 | + expectMatch("[a-c]", ["a", "b", "c"]); |
| 14 | + expectNotMatch("[a-c]", ["d", "e", ""]); |
| 15 | + expectMatch("[K-M]", ["K", "L", "M"]); |
| 16 | + expectNotMatch("[K-M]", ["9", "J"]); |
| 17 | + expectMatch("[0-9]", ["0", "9"]); |
| 18 | + expectNotMatch("[0-9]", ["a", "A"]); |
17 | 19 | });
|
18 | 20 |
|
19 |
| -it("word", () => { |
20 |
| - expectMatch("\\w", ["A", "a", "Z", "z", "0", "9", "_"]); |
21 |
| - expectNotMatch("\\w", ["", "$"]); |
| 21 | +it("matches multiple ranges", () => { |
| 22 | + expectMatch("[a-ce-f]", ["a", "b", "c", "e", "f"]); |
| 23 | + expectNotMatch("[a-ce-f]", ["d"]); |
22 | 24 | });
|
23 | 25 |
|
24 |
| -it("not word", () => { |
25 |
| - expectNotMatch("\\W", ["A", "a", "Z", "z", "0", "9", "_", ""]); |
26 |
| - expectMatch("\\W", ["&", "$"]); |
| 26 | +it("supports closing brackets", () => { |
| 27 | + expectMatch("[]a]", ["]", "a"]); |
27 | 28 | });
|
28 | 29 |
|
29 |
| -it("whitespace", () => { |
30 |
| - expectMatch("\\s", ["\f", "\n", "\r", "\t", "\v"]); |
31 |
| - expectNotMatch("\\s", ["", "a", "0"]); |
| 30 | +it("supports negated sets", () => { |
| 31 | + expectNotMatch("[^a-c]", ["a", "b", "c"]); |
| 32 | + expectMatch("[^a-c]", ["d", "e"]); |
| 33 | + expectNotMatch("[^a-ce-f]", ["a", "b", "c", "e", "f"]); |
| 34 | + expectMatch("[^a-ce-f]", ["d"]); |
32 | 35 | });
|
33 | 36 |
|
34 |
| -it("not whitespace", () => { |
35 |
| - expectNotMatch("\\S", ["", "\f", "\n", "\r", "\t", "\v"]); |
36 |
| - expectMatch("\\S", ["a", "0"]); |
| 37 | +it("treats - as a literal", () => { |
| 38 | + expectMatch("[-abc]", ["-", "a", "b", "c"]); |
| 39 | + expectMatch("[abc-]", ["-", "a", "b", "c"]); |
37 | 40 | });
|
38 | 41 |
|
39 |
| -it("tab, cr, lf, vt, ff", () => { |
40 |
| - expectMatch("\\t", ["\t"]); |
41 |
| - expectMatch("\\r", ["\r"]); |
42 |
| - expectMatch("\\n", ["\n"]); |
43 |
| - expectMatch("\\v", ["\v"]); |
44 |
| - expectMatch("\\f", ["\f"]); |
45 |
| - expectNotMatch("\\t", ["a", " ", ""]); |
| 42 | +it("treats - as a literal in negated sets", () => { |
| 43 | + expectNotMatch("[^-abc]", ["-", "a", "b", "c"]); |
| 44 | + expectMatch("[^-abc]", ["1", "A"]); |
46 | 45 | });
|
47 | 46 |
|
48 |
| -it("escaped dot", () => { |
49 |
| - expectMatch("\\.", ["."]); |
50 |
| - expectNotMatch("\\.", ["", "a"]); |
51 |
| -}); |
52 |
| - |
53 |
| -it("unrecognised character classes are treated as characters", () => { |
54 |
| - expectMatch("\\g\\m", ["gm"]); |
| 47 | +it("supports case insensitive matching", () => { |
| 48 | + // simple ranges |
| 49 | + expectMatch("[a-c]", ["A", "C", "a", "c"], "i"); |
| 50 | + expectNotMatch("[a-c]", ["D", "d"], "i"); |
| 51 | + // complex |
| 52 | + expectMatch("[W-c]", ["W", "w", "C", "c"], "i"); |
| 53 | + expectNotMatch("[W-c]", ["V", "v", "D", "d"], "i"); |
55 | 54 | });
|
0 commit comments