-
Notifications
You must be signed in to change notification settings - Fork 3
/
regexp-convert.test.ts
42 lines (39 loc) · 1.22 KB
/
regexp-convert.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { expect, test } from "bun:test";
import { convertRegexpToGbnf } from "./regexp-convert";
const tests: Array<[string, string]> = [
[
"^[0-9]+( days| weeks| months)?$",
`[0-9]+ (" days" | " weeks" | " months")?`,
],
[
"^[0-9]+ ( days| weeks| months)?$",
`[0-9]+ " " (" days" | " weeks" | " months")?`,
],
["^[0-9]{2,4}$", `[0-9] [0-9] [0-9]? [0-9]?`],
["^[0-9]{0,4}$", `[0-9]? [0-9]? [0-9]? [0-9]?`],
["^[0-9]{2,}$", `[0-9] [0-9] [0-9]*`],
["^\\w$", `[0-9A-Za-z_]`],
["^\\w+$", `[0-9A-Za-z_]+`],
["^\\w{0,2}$", `[0-9A-Za-z_]? [0-9A-Za-z_]?`],
["^\\w{3,}$", `[0-9A-Za-z_] [0-9A-Za-z_] [0-9A-Za-z_] [0-9A-Za-z_]*`],
["^\\.$", `"."`],
["^.$", `string-char`],
["", `(string-char)*`],
["a", `(string-char)* "a" (string-char)*`],
["^a", `"a" (string-char)*`],
["^a$", `"a"`],
["a$", `(string-char)* "a"`],
["^.*$", `(string-char)*`],
];
tests.forEach(([regexp, gbnf]) => {
test(`Convert Regexp: ${regexp}`, () => {
const resultGbnf = convertRegexpToGbnf(regexp);
expect(resultGbnf).toBe(gbnf);
});
});
const unsupported = ["(^a|b$)"];
unsupported.forEach((regexp) => {
test(`Unsupported Regexp: ${regexp}`, () => {
expect(() => convertRegexpToGbnf(regexp)).toThrow();
});
});