forked from google/closure-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegExpTreeTest.java
More file actions
222 lines (191 loc) · 8.46 KB
/
Copy pathRegExpTreeTest.java
File metadata and controls
222 lines (191 loc) · 8.46 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* Copyright 2014 The Closure Compiler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.javascript.jscomp.regex;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import com.google.common.truth.ThrowableSubject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class RegExpTreeTest {
private String parseRegExpAndPrintPattern(String regex, String flags) {
RegExpTree tree = RegExpTree.parseRegExp(regex, flags);
StringBuilder sb = new StringBuilder();
tree.appendSourceCode(sb);
return sb.toString();
}
private RuntimeException exceptionFrom(String regex, String flags) {
try {
String printed = parseRegExpAndPrintPattern(regex, flags);
assertWithMessage("Expected exception, but none was thrown. Instead got back: " + printed)
.fail();
throw new AssertionError(); // unreachable
} catch (RuntimeException thrownException) {
return thrownException;
}
}
private void assertRegexCompilesTo(String regex, String flags, String expected) {
assertThat(parseRegExpAndPrintPattern(regex, flags)).isEqualTo(expected);
}
private ThrowableSubject assertRegexThrowsExceptionThat(String regex, String flags) {
return assertThat(exceptionFrom(regex, flags));
}
private void assertRegexCompilesToSame(String regex, String flags) {
assertRegexCompilesTo(regex, flags, regex);
}
@Test
public void testValidEs2018LookbehindAssertions() {
assertRegexCompilesToSame("(?<=asdf)", "");
assertRegexCompilesToSame("(?<!asdf)", "");
assertRegexCompilesToSame("(?<=(?<!asdf))", "");
assertRegexCompilesToSame("(?<=(?<!asdf))", "");
}
@Test
public void testInvalidEs2018LookbehindAssertions() {
assertRegexThrowsExceptionThat("(?<asdf)", "")
.hasMessageThat()
.isEqualTo("Invalid capture group name: <asdf)");
}
@Test
public void testValidEs2018UnicodePropertyEscapes() {
assertRegexCompilesToSame("\\p{Script=Greek}", "u");
assertRegexCompilesToSame("\\P{Script=Greek}", "u");
assertRegexCompilesToSame("\\p{Letter}", "u");
assertRegexCompilesToSame("\\P{Letter}", "u");
assertRegexCompilesTo("\\p", "", "p"); // Without 'u' flag, '\p' is just 'p'
assertRegexCompilesTo("\\P", "", "P");
}
@Test
public void testInvalidEs2018UnicodePropertyEscapes() {
assertRegexThrowsExceptionThat("\\p{", "u")
.hasMessageThat()
.isEqualTo("Malformed Unicode Property Escape: expected '=' or '}' after \\p{");
assertRegexThrowsExceptionThat("\\P{", "u")
.hasMessageThat()
.isEqualTo("Malformed Unicode Property Escape: expected '=' or '}' after \\P{");
assertRegexThrowsExceptionThat("\\p{=Greek}", "u")
.hasMessageThat()
.isEqualTo("if '=' is present in a unicode property escape, the name cannot be empty");
assertRegexThrowsExceptionThat("\\P{=Greek}", "u")
.hasMessageThat()
.isEqualTo("if '=' is present in a unicode property escape, the name cannot be empty");
assertRegexThrowsExceptionThat("\\p{}", "u")
.hasMessageThat()
.isEqualTo("unicode property escape value cannot be empty");
assertRegexThrowsExceptionThat("\\P{}", "u")
.hasMessageThat()
.isEqualTo("unicode property escape value cannot be empty");
}
@Test
public void testValidEs2018RegexNamedCaptureGroups() {
assertRegexCompilesToSame("(?<name>)", "");
assertRegexCompilesToSame("(?<h$h1h_>)", "u");
assertRegexCompilesToSame("(?<$var_name>blah)", "");
assertRegexCompilesToSame("(?<_var_name>>>>)", "");
}
@Test
public void testInvalidEs2018RegexNamedCaptureGroups() {
assertRegexThrowsExceptionThat("(?<name)", "")
.hasMessageThat()
.isEqualTo("Invalid capture group name: <name)");
assertRegexThrowsExceptionThat("(?<1b>)", "")
.hasMessageThat()
.isEqualTo("Invalid capture group name: <1b>)");
assertRegexThrowsExceptionThat("(?<>)", "")
.hasMessageThat()
.isEqualTo("Invalid capture group name: <>)");
assertRegexThrowsExceptionThat("(?<.name>)", "")
.hasMessageThat()
.isEqualTo("Invalid capture group name: <.name>)");
}
@Test
public void testNumCapturingGroups() {
assertRegexCompilesToSame("(h(i))\\2", "");
// TODO(b/116048051): reference to non-existent capture group should be an error.
assertRegexCompilesTo("(h(i))\\3", "", "(h(i))\\x03");
assertRegexCompilesToSame("(?<foo>.*(?<bar>))", "");
}
@Test
public void testValidEs2018CaptureNameBackreferencing() {
assertRegexCompilesToSame("(?<name>)\\k<name>", "");
// Note that (?: ) only used for printing purposes to
// indicate the nesting structure of Concatenation nodes.
// It is not actually what the compiler prints out as source code.
assertRegexCompilesTo(
"(?<foo>(?<bar>))\\k<foo>\\k<bar>", "", "(?:(?<foo>(?<bar>))\\k<foo>)\\k<bar>");
assertRegexCompilesTo(
"(?<foo>(?<bar>)\\k<bar>)\\k<foo>", "", "(?<foo>(?<bar>)\\k<bar>)\\k<foo>");
// The below examples where the backreference comes before the definition of named groups is
// allowed syntactically, although it is not able to reference the original group semantically
assertRegexCompilesToSame("\\k<foo>(?<foo>)", "");
assertRegexCompilesToSame("\\k<foo>(?<foo>\\k<bar>(?<bar>))", "");
// Backreferencing the name in the group it is defined is also allowed
assertRegexCompilesToSame("(?<foo>\\k<foo>)", "");
}
@Test
public void testInvalidEs2018CaptureNameBackreferencing() {
assertRegexThrowsExceptionThat("(?<foo>)\\k<bar>", "")
.hasMessageThat()
.isEqualTo("Invalid named capture referenced: \\k<bar>");
assertRegexThrowsExceptionThat("(?<foo>)\\k<foo", "")
.hasMessageThat()
.isEqualTo("Malformed named capture group: <foo");
assertRegexThrowsExceptionThat("\\k<1b>(?<foo>)", "")
.hasMessageThat()
.isEqualTo("Invalid capture group name: <1b>(?<foo>)");
// Even though enclosed in (?<>), 'foo' not a capture name definition
assertRegexThrowsExceptionThat("[(?<foo>)](?<bar>)\\k<foo>", "")
.hasMessageThat()
.isEqualTo("Invalid named capture referenced: \\k<foo>");
}
@Test
public void testBackreferencingTreatedAsStringIfNoGroup() {
// Backreferencing without named group definitions is just treated as normal string
assertRegexCompilesTo("\\k<foo>", "", "k<foo>");
// Note that the (?: ) that is wrapped around "k<" at expected output is only used for printing
// purposes to indicate the nesting structure of Concatenation nodes.
// It is not actually what the compiler prints out as source code.
assertRegexCompilesTo("\\k<.", "", "(?:k<).");
// Even though enclosed in (?<>), 'foo' not a capture name definition
// (?: ) in expected output serves same purpose as above test
assertRegexCompilesTo("[(?<foo>)]\\k<foo>", "", "(?:[()<>?fo]k)<foo>");
}
@Test
public void testValidUnicodeEscape() {
assertRegexCompilesTo("\\u0061", "", "a");
assertRegexCompilesTo("\\u10b1", "u", "\\u10b1");
assertRegexCompilesTo("\\u{61}", "u", "a");
assertRegexCompilesTo("\\u{10b1}", "u", "\\u10b1");
assertRegexCompilesTo("\\u{1bc}", "u", "\\u01bc");
assertRegexCompilesTo("\\u{100A3}", "u", "\\ud800\\udca3");
}
@Test
public void testInvalidUnicodeEscape() {
assertRegexThrowsExceptionThat("\\u{a012", "u")
.hasMessageThat()
.isEqualTo("Malformed unicode escape: expected '}' after {a012");
assertRegexThrowsExceptionThat("\\u{}", "u")
.hasMessageThat()
.isEqualTo("Invalid unicode escape: {}");
assertRegexThrowsExceptionThat("\\u{10za}", "u")
.hasMessageThat()
.isEqualTo("Invalid character in unicode escape: z");
assertRegexThrowsExceptionThat("\\u{FFFFFF}", "u")
.hasMessageThat()
.isEqualTo("Unicode must not be greater than 0x10FFFF: {FFFFFF}");
}
}