Skip to content

Commit 7406d9c

Browse files
test code refactor
1 parent 72a20af commit 7406d9c

14 files changed

+340
-343
lines changed

test/data.test.js renamed to __spec_tests__/data.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const { RegExp } = require("./util");
1+
const { RegExp } = require("../__tests__/util");
22
const fs = require("fs");
33
const { fail } = require("assert");
44

5-
const data = fs.readFileSync("./test/test.dat", "utf8");
5+
const data = fs.readFileSync("./__spec_tests__/test.dat", "utf8");
66
const lines = data.split("\n");
77

88
const matches = (regex, value) => {
File renamed without changes.

__tests__/alternations.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { RegExp, expectNotMatch, expectMatch, matches } = require("./util");
2+
3+
it("or", () => {
4+
expectMatch("a|b", ["b", "a"]);
5+
expectNotMatch("a|b", ["c"]);
6+
expectMatch("a|br", ["br", "a"]);
7+
expectNotMatch("a|br", ["b", "c"]);
8+
});
9+
10+
it("or multi-term", () => {
11+
expectMatch("a|b|c", ["b", "a", "c"]);
12+
expectNotMatch("a|b|c", ["d"]);
13+
expectMatch("a|br|pc", ["br", "a", "pc"]);
14+
expectNotMatch("a|br|pc", ["b", "pr"]);
15+
});

__tests__/boundary-assertions.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const { RegExp, expectNotMatch, expectMatch, matches } = require("./util");
2+
3+
it("matches end of string", () => {
4+
const regex = new RegExp("a$");
5+
const match = regex.exec("ba");
6+
expect(match.index).toEqual(1);
7+
expect(match.matches[0]).toEqual("a");
8+
expectNotMatch("a$", ["ab"]);
9+
});
10+
11+
it("matches start of string", () => {
12+
expectMatch("^a", ["a"]);
13+
expectNotMatch("^a", ["ba"]);
14+
});
15+
16+
it("handles escaped boundaries", () => {
17+
expectMatch("\\^a", ["^a"]);
18+
expectMatch("a\\$", ["a$"]);
19+
});

__tests__/capture-groups.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const { RegExp, expectNotMatch, expectMatch, matches } = require("./util");
2+
3+
it("supports capture groups", () => {
4+
let match = matches("a(\\d)a", "a3a");
5+
expect(match.index).toEqual(0);
6+
expect(match.input).toEqual("a3a");
7+
expect(match.matches[0]).toEqual("a3a");
8+
expect(match.matches[1]).toEqual("3");
9+
10+
match = matches("a(\\d)a", " a3a");
11+
expect(match.index).toEqual(2);
12+
expect(match.input).toEqual(" a3a");
13+
expect(match.matches[0]).toEqual("a3a");
14+
expect(match.matches[1]).toEqual("3");
15+
16+
match = matches("a(\\d*)a", "a3456a");
17+
expect(match.index).toEqual(0);
18+
expect(match.input).toEqual("a3456a");
19+
expect(match.matches[0]).toEqual("a3456a");
20+
expect(match.matches[1]).toEqual("3456");
21+
22+
match = matches("a*(\\d*)(a*)", "aaa456aaa");
23+
expect(match.index).toEqual(0);
24+
expect(match.input).toEqual("aaa456aaa");
25+
expect(match.matches[0]).toEqual("aaa456aaa");
26+
expect(match.matches[1]).toEqual("456");
27+
expect(match.matches[2]).toEqual("aaa");
28+
});
29+
30+
it.skip("should not return captured values for non-matching alternations", () => {
31+
const match = matches("(a|b)c|a(b|c)", "ab");
32+
expect(match.matches[0]).toEqual("ab");
33+
expect(match.matches[1]).toEqual("");
34+
expect(match.matches[2]).toEqual("b");
35+
});

__tests__/character-classes.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const { RegExp, expectNotMatch, expectMatch, matches } = require("./util");
2+
3+
it("dot", () => {
4+
expectMatch(".", [" ", "B", "|", "9"]);
5+
expectNotMatch(".", ["", "\n"]);
6+
});
7+
8+
it("digit", () => {
9+
expectMatch("\\d", ["0", "9"]);
10+
expectNotMatch("\\d", ["", "b"]);
11+
});
12+
13+
it("non-digit", () => {
14+
expectNotMatch("\\D", ["0", "9", ""]);
15+
expectMatch("\\D", ["b", "|"]);
16+
});
17+
18+
it("word", () => {
19+
expectMatch("\\w", ["A", "a", "Z", "z", "0", "9", "_"]);
20+
expectNotMatch("\\w", ["", "$"]);
21+
});
22+
23+
it("not word", () => {
24+
expectNotMatch("\\W", ["A", "a", "Z", "z", "0", "9", "_", ""]);
25+
expectMatch("\\W", ["&", "$"]);
26+
});
27+
28+
it("whitespace", () => {
29+
expectMatch("\\s", ["\f", "\n", "\r", "\t", "\v"]);
30+
expectNotMatch("\\s", ["", "a", "0"]);
31+
});
32+
33+
it("not whitespace", () => {
34+
expectNotMatch("\\S", ["", "\f", "\n", "\r", "\t", "\v"]);
35+
expectMatch("\\S", ["a", "0"]);
36+
});
37+
38+
it("tab, cr, lf, vt, ff", () => {
39+
expectMatch("\\t", ["\t"]);
40+
expectMatch("\\r", ["\r"]);
41+
expectMatch("\\n", ["\n"]);
42+
expectMatch("\\v", ["\v"]);
43+
expectMatch("\\f", ["\f"]);
44+
expectNotMatch("\\t", ["a", " ", ""]);
45+
});
46+
47+
it("escaped dot", () => {
48+
expectMatch("\\.", ["."]);
49+
expectNotMatch("\\.", ["", "a"]);
50+
});

__tests__/character-sets.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const { RegExp, expectNotMatch, expectMatch, matches } = require("./util");
2+
3+
it("matches discrete characters", () => {
4+
expectMatch("[abce]", ["a", "b", "c", "e"]);
5+
expectNotMatch("[abce]", ["", "f", "h"]);
6+
});
7+
8+
it("matches character ranges", () => {
9+
expectMatch("[a-c]", ["a", "b", "c"]);
10+
expectNotMatch("[a-c]", ["d", "e", ""]);
11+
expectMatch("[K-M]", ["K", "L", "M"]);
12+
expectNotMatch("[K-M]", ["9", "J"]);
13+
expectMatch("[0-9]", ["0", "9"]);
14+
expectNotMatch("[0-9]", ["a", "A"]);
15+
});
16+
17+
it("matches multiple ranges", () => {
18+
expectMatch("[a-ce-f]", ["a", "b", "c", "e", "f"]);
19+
expectNotMatch("[a-ce-f]", ["d"]);
20+
});
21+
22+
it("supports closing brackets", () => {
23+
expectMatch("[]a]", ["]", "a"]);
24+
});
25+
26+
it("supports negated sets", () => {
27+
expectNotMatch("[^a-c]", ["a", "b", "c"]);
28+
expectMatch("[^a-c]", ["d", "e"]);
29+
expectNotMatch("[^a-ce-f]", ["a", "b", "c", "e", "f"]);
30+
expectMatch("[^a-ce-f]", ["d"]);
31+
});
32+
33+
it("treats - as a literal", () => {
34+
expectMatch("[-abc]", ["-", "a", "b", "c"]);
35+
expectMatch("[abc-]", ["-", "a", "b", "c"]);
36+
});
37+
38+
it("treats - as a literal in negated sets", () => {
39+
expectNotMatch("[^-abc]", ["-", "a", "b", "c"]);
40+
expectMatch("[^-abc]", ["1", "A"]);
41+
});

__tests__/characters.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const { RegExp, expectNotMatch, expectMatch, matches } = require("./util");
2+
3+
it("single character", () => {
4+
expectMatch("a", ["a"]);
5+
expectNotMatch("a", ["fish", ""]);
6+
});
7+
8+
it("concatenation", () => {
9+
expectMatch("ab", ["ab"]);
10+
expectNotMatch("ab", ["aac", "aa", ""]);
11+
});

__tests__/index.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
const { RegExp, expectNotMatch, expectMatch, matches } = require("./util");
2+
3+
describe("regexp", () => {
4+
it("match returns correct substring", () => {
5+
const match = matches("\\d", "asd123asd");
6+
expect(match.index).toEqual(3);
7+
expect(match.input).toEqual("asd123asd");
8+
expect(match.matches[0]).toEqual("1");
9+
});
10+
11+
describe("global mode", () => {
12+
it("increments lastIndex", () => {
13+
const regex = new RegExp("\\d+", "g");
14+
const match = regex.exec("dog 23 fish 45 cat");
15+
expect(match.matches[0]).toEqual("23");
16+
expect(regex.lastIndex).toEqual(6);
17+
});
18+
19+
it("uses lastIndex to support multiple matches", () => {
20+
const regex = new RegExp("\\d+", "g");
21+
22+
let match = regex.exec("dog 23 fish 45 cat");
23+
expect(match.matches[0]).toEqual("23");
24+
expect(regex.lastIndex).toEqual(6);
25+
26+
match = regex.exec("dog 23 fish 45 cat");
27+
expect(match.matches[0]).toEqual("45");
28+
expect(regex.lastIndex).toEqual(14);
29+
30+
match = regex.exec("dog 23 fish 45 cat");
31+
expect(match).toBeNull();
32+
expect(regex.lastIndex).toEqual(0);
33+
});
34+
});
35+
36+
describe("non-global mode", () => {
37+
it("doesn't increment lastIndex", () => {
38+
const regex = new RegExp("\\d+");
39+
40+
let match = regex.exec("dog 23 fish 45 cat");
41+
expect(match.matches[0]).toEqual("23");
42+
expect(regex.lastIndex).toEqual(0);
43+
44+
match = regex.exec("dog 23 fish 45 cat");
45+
expect(match.matches[0]).toEqual("23");
46+
expect(regex.lastIndex).toEqual(0);
47+
});
48+
});
49+
});
50+
51+
describe("use cases", () => {
52+
it("matches combinations", () => {
53+
expectMatch("\\s\\w*", [" bar"]);
54+
expectMatch("\\S\\w*", ["foo"]);
55+
});
56+
57+
it("email", () => {
58+
const regex = ".+@.+\\..+";
59+
expect(matches(regex, "[email protected]")).toBeTruthy();
60+
expect(matches(regex, "gmail")).toBeFalsy();
61+
62+
const capturingRegex = "(.+)@(.+)\\.(.+)";
63+
expect(matches(capturingRegex, "[email protected]")).toBeTruthy();
64+
65+
match = matches(capturingRegex, "[email protected]");
66+
expect(match.matches[0]).toEqual("[email protected]");
67+
expect(match.matches[1]).toEqual("colin");
68+
expect(match.matches[2]).toEqual("gmail");
69+
expect(match.matches[3]).toEqual("com");
70+
});
71+
});

__tests__/quantifiers.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const { RegExp, expectNotMatch, expectMatch, matches } = require("./util");
2+
3+
it("matches empty strings", () => {
4+
expectMatch("a?", [""]);
5+
expectMatch("a*", [""]);
6+
});
7+
8+
it("zero or one", () => {
9+
expectMatch("a?", ["a"]);
10+
// expectNotMatch("a?", ["bc"]);
11+
});
12+
13+
it("one or more", () => {
14+
expectMatch("a+", ["a", "aa"]);
15+
expectNotMatch("a+", [""]);
16+
});
17+
18+
it("zero or more", () => {
19+
expectMatch("a*", ["aa", "aaaa"]);
20+
});
21+
22+
it("multiple rules", () => {
23+
expectMatch("a*b", ["b", "ab", "aaaab"]);
24+
expectNotMatch("a*b", ["aaaad"]);
25+
});
26+
27+
it("zero or more is greedy", () => {
28+
let match = matches("a*", "aaaaa");
29+
expect(match).not.toBeNull();
30+
expect(match.matches[0]).toEqual("aaaaa");
31+
});
32+
33+
it("one or more is greedy", () => {
34+
let match = matches("a+", "aaaaa");
35+
console.log(match);
36+
expect(match).not.toBeNull();
37+
expect(match.matches[0]).toEqual("aaaaa");
38+
});

0 commit comments

Comments
 (0)