forked from mysticatea/regexpp
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvalidate-error.ts
176 lines (169 loc) · 4.69 KB
/
validate-error.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import assert from "assert"
import { RegExpValidator } from "../src/index"
import type { RegExpSyntaxError } from "../src/regexp-syntax-error"
const validator = new RegExpValidator()
function getErrorForPattern(
source: string,
start: number,
end: number,
flags: {
unicode?: boolean
unicodeSets?: boolean
},
): RegExpSyntaxError {
try {
validator.validatePattern(source, start, end, flags)
} catch (err) {
const error = err as RegExpSyntaxError
return error
}
return assert.fail("Should fail, but succeeded.")
}
function getErrorForFlags(
source: string,
start: number,
end: number,
): RegExpSyntaxError {
try {
validator.validateFlags(source, start, end)
} catch (err) {
const error = err as RegExpSyntaxError
return error
}
return assert.fail("Should fail, but succeeded.")
}
function getErrorForLiteral(
source: string,
start: number,
end: number,
): RegExpSyntaxError {
try {
validator.validateLiteral(source, start, end)
} catch (err) {
const error = err as RegExpSyntaxError
return error
}
return assert.fail("Should fail, but succeeded.")
}
describe("RegExpValidator#validatePattern error:", () => {
for (const test of [
{
source: "abcd",
start: 0,
end: 2,
flags: { unicode: true, unicodeSets: true },
error: {
message:
"Invalid regular expression: /ab/uv: Invalid regular expression flags",
index: 3,
},
},
{
source: "[A]",
start: 0,
end: 2,
flags: { unicode: true, unicodeSets: false },
error: {
message:
"Invalid regular expression: /[A/u: Unterminated character class",
index: 2,
},
},
{
source: "[[A]]",
start: 0,
end: 4,
flags: { unicode: false, unicodeSets: true },
error: {
message:
"Invalid regular expression: /[[A]/v: Unterminated character class",
index: 4,
},
},
{
source: " /[[A]/v ",
start: 2,
end: 6,
flags: { unicode: false, unicodeSets: true },
error: {
message:
"Invalid regular expression: /[[A]/v: Unterminated character class",
index: 6,
},
},
]) {
it(`${JSON.stringify(test)} should throw syntax error.`, () => {
const error = getErrorForPattern(
test.source,
test.start,
test.end,
test.flags,
)
assert.deepStrictEqual(
{ message: error.message, index: error.index },
test.error,
)
})
}
})
describe("RegExpValidator#validateFlags error:", () => {
for (const test of [
{
source: "abcd",
start: 0,
end: 2,
error: {
message: "Invalid regular expression: Invalid flag 'a'",
index: 0,
},
},
{
source: "dd",
start: 0,
end: 2,
error: {
message: "Invalid regular expression: Duplicated flag 'd'",
index: 0,
},
},
{
source: "/a/dd",
start: 3,
end: 5,
error: {
message: "Invalid regular expression: Duplicated flag 'd'",
index: 3,
},
},
]) {
it(`${JSON.stringify(test)} should throw syntax error.`, () => {
const error = getErrorForFlags(test.source, test.start, test.end)
assert.deepStrictEqual(
{ message: error.message, index: error.index },
test.error,
)
})
}
})
describe("RegExpValidator#validateLiteral error:", () => {
for (const test of [
{
source: " /[/ ",
start: 1,
end: 4,
error: {
message:
"Invalid regular expression: /[/: Unterminated character class",
index: 4,
},
},
]) {
it(`${JSON.stringify(test)} should throw syntax error.`, () => {
const error = getErrorForLiteral(test.source, test.start, test.end)
assert.deepStrictEqual(
{ message: error.message, index: error.index },
test.error,
)
})
}
})