forked from dop251/goja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regexp_test.go
207 lines (165 loc) · 3.99 KB
/
regexp_test.go
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package goja
import (
"testing"
)
func TestRegexp1(t *testing.T) {
const SCRIPT = `
var r = new RegExp("(['\"])(.*?)\\1");
var m = r.exec("'test'");
m !== null && m.length == 3 && m[2] === "test";
`
testScript1(SCRIPT, valueTrue, t)
}
func TestRegexp2(t *testing.T) {
const SCRIPT = `
var r = new RegExp("(['\"])(.*?)['\"]");
var m = r.exec("'test'");
m !== null && m.length == 3 && m[2] === "test";
`
testScript1(SCRIPT, valueTrue, t)
}
func TestRegexpLiteral(t *testing.T) {
const SCRIPT = `
var r = /(['\"])(.*?)\1/;
var m = r.exec("'test'");
m !== null && m.length == 3 && m[2] === "test";
`
testScript1(SCRIPT, valueTrue, t)
}
func TestRegexpRe2Unicode(t *testing.T) {
const SCRIPT = `
var r = /(тест)/i;
var m = r.exec("'Тест'");
m !== null && m.length == 2 && m[1] === "Тест";
`
testScript1(SCRIPT, valueTrue, t)
}
func TestRegexpRe2UnicodeTarget(t *testing.T) {
const SCRIPT = `
var r = /(['\"])(.*?)['\"]/i;
var m = r.exec("'Тест'");
m !== null && m.length == 3 && m[2] === "Тест";
`
testScript1(SCRIPT, valueTrue, t)
}
func TestRegexpRegexp2Unicode(t *testing.T) {
const SCRIPT = `
var r = /(['\"])(тест)\1/i;
var m = r.exec("'Тест'");
m !== null && m.length == 3 && m[2] === "Тест";
`
testScript1(SCRIPT, valueTrue, t)
}
func TestRegexpRegexp2UnicodeTarget(t *testing.T) {
const SCRIPT = `
var r = /(['\"])(.*?)\1/;
var m = r.exec("'Тест'");
m !== null && m.length == 3 && m[2] === "Тест";
`
testScript1(SCRIPT, valueTrue, t)
}
func TestRegexpRe2Whitespace(t *testing.T) {
const SCRIPT = `
"\u2000\u2001\u2002\u200b".replace(/\s+/g, "") === "\u200b";
`
testScript1(SCRIPT, valueTrue, t)
}
func TestRegexpRegexp2Whitespace(t *testing.T) {
const SCRIPT = `
"A\u2000\u2001\u2002A\u200b".replace(/(A)\s+\1/g, "") === "\u200b"
`
testScript1(SCRIPT, valueTrue, t)
}
func TestEmptyCharClassRe2(t *testing.T) {
const SCRIPT = `
/[]/.test("\u0000");
`
testScript1(SCRIPT, valueFalse, t)
}
func TestNegatedEmptyCharClassRe2(t *testing.T) {
const SCRIPT = `
/[^]/.test("\u0000");
`
testScript1(SCRIPT, valueTrue, t)
}
func TestEmptyCharClassRegexp2(t *testing.T) {
const SCRIPT = `
/([])\1/.test("\u0000\u0000");
`
testScript1(SCRIPT, valueFalse, t)
}
func TestRegexp2Negate(t *testing.T) {
const SCRIPT = `
/([\D1])\1/.test("aa");
`
testScript1(SCRIPT, valueTrue, t)
}
func TestAlternativeRe2(t *testing.T) {
const SCRIPT = `
/()|/.exec("") !== null;
`
testScript1(SCRIPT, valueTrue, t)
}
func TestRegexpReplaceGlobal(t *testing.T) {
const SCRIPT = `
"QBZPbage\ny_cynprubyqre".replace(/^\s*|\s*$/g, '')
`
testScript1(SCRIPT, asciiString("QBZPbage\ny_cynprubyqre"), t)
}
func TestRegexpNumCaptures(t *testing.T) {
const SCRIPT = `
"Fubpxjnir Synfu 9.0 e115".replace(/([a-zA-Z]|\s)+/, '')
`
testScript1(SCRIPT, asciiString("9.0 e115"), t)
}
func TestRegexpNumCaptures1(t *testing.T) {
const SCRIPT = `
"Fubpxjnir Sy\tfu 9.0 e115".replace(/^.*\s+(\S+\s+\S+$)/, '')
`
testScript1(SCRIPT, asciiString(""), t)
}
func TestRegexpSInClass(t *testing.T) {
const SCRIPT = `
/[\S]/.test("\u2028");
`
testScript1(SCRIPT, valueFalse, t)
}
func TestRegexpDotMatchSlashR(t *testing.T) {
const SCRIPT = `
/./.test("\r");
`
testScript1(SCRIPT, valueFalse, t)
}
func TestRegexpDotMatchSlashRInGroup(t *testing.T) {
const SCRIPT = `
/(.)/.test("\r");
`
testScript1(SCRIPT, valueFalse, t)
}
func TestRegexpSplitWithBackRef(t *testing.T) {
const SCRIPT = `
"a++b+-c".split(/([+-])\1/).join(" $$ ")
`
testScript1(SCRIPT, asciiString("a $$ + $$ b+-c"), t)
}
func TestEscapeNonASCII(t *testing.T) {
const SCRIPT = `
/\⩓/.test("⩓")
`
testScript1(SCRIPT, valueTrue, t)
}
func BenchmarkRegexpSplitWithBackRef(b *testing.B) {
const SCRIPT = `
"aaaaaaaaaaaaaaaaaaaaaaaaa++bbbbbbbbbbbbbbbbbbbbbb+-ccccccccccccccccccccccc".split(/([+-])\1/)
`
b.StopTimer()
prg, err := Compile("test.js", SCRIPT, false)
if err != nil {
b.Fatal(err)
}
vm := New()
b.StartTimer()
for i := 0; i < b.N; i++ {
vm.RunProgram(prg)
}
}