Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Side by Side Diff: test/mjsunit/harmony/regexp-named-captures.js

Issue 2776263003: [regexp] Handle unmatched groups in callable replacers (Closed)
Patch Set: Rebase Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/runtime/runtime-regexp.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 the V8 project authors. All rights reserved. 1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Flags: --harmony-regexp-named-captures 5 // Flags: --harmony-regexp-named-captures
6 6
7 // Malformed named captures. 7 // Malformed named captures.
8 assertThrows("/(?<>a)/u"); // Empty name. 8 assertThrows("/(?<>a)/u"); // Empty name.
9 assertThrows("/(?<aa)/u"); // Unterminated name. 9 assertThrows("/(?<aa)/u"); // Unterminated name.
10 assertThrows("/(?<42a>a)/u"); // Name starting with digits. 10 assertThrows("/(?<42a>a)/u"); // Name starting with digits.
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 { 101 {
102 let result = "abcd".replace(/(.)(.)/u, (match, fst, snd, offset, str) => { 102 let result = "abcd".replace(/(.)(.)/u, (match, fst, snd, offset, str) => {
103 assertEquals("ab", match); 103 assertEquals("ab", match);
104 assertEquals("a", fst); 104 assertEquals("a", fst);
105 assertEquals("b", snd); 105 assertEquals("b", snd);
106 assertEquals(0, offset); 106 assertEquals(0, offset);
107 assertEquals("abcd", str); 107 assertEquals("abcd", str);
108 return `${snd}${fst}`; 108 return `${snd}${fst}`;
109 }); 109 });
110 assertEquals("bacd", result); 110 assertEquals("bacd", result);
111
112 assertEquals("undefinedbcd", "abcd".replace(/(.)|(.)/u,
113 (match, fst, snd, offset, str) => snd));
111 } 114 }
112 115
113 // @@replace with a callable replacement argument (global, named captures). 116 // @@replace with a callable replacement argument (global, named captures).
114 { 117 {
115 let i = 0; 118 let i = 0;
116 let result = "abcd".replace(/(?<fst>.)(?<snd>.)/gu, 119 let result = "abcd".replace(/(?<fst>.)(?<snd>.)/gu,
117 (match, fst, snd, offset, str, groups) => { 120 (match, fst, snd, offset, str, groups) => {
118 if (i == 0) { 121 if (i == 0) {
119 assertEquals("ab", match); 122 assertEquals("ab", match);
120 assertEquals("a", groups.fst); 123 assertEquals("a", groups.fst);
(...skipping 10 matching lines...) Expand all
131 assertEquals("d", snd); 134 assertEquals("d", snd);
132 assertEquals(2, offset); 135 assertEquals(2, offset);
133 assertEquals("abcd", str); 136 assertEquals("abcd", str);
134 } else { 137 } else {
135 assertUnreachable(); 138 assertUnreachable();
136 } 139 }
137 i++; 140 i++;
138 return `${groups.snd}${groups.fst}`; 141 return `${groups.snd}${groups.fst}`;
139 }); 142 });
140 assertEquals("badc", result); 143 assertEquals("badc", result);
144
145 assertEquals("undefinedundefinedundefinedundefined",
146 "abcd".replace(/(?<fst>.)|(?<snd>.)/gu,
147 (match, fst, snd, offset, str, groups) => groups.snd));
141 } 148 }
142 149
143 // @@replace with a callable replacement argument (non-global, named captures). 150 // @@replace with a callable replacement argument (non-global, named captures).
144 { 151 {
145 let result = "abcd".replace(/(?<fst>.)(?<snd>.)/u, 152 let result = "abcd".replace(/(?<fst>.)(?<snd>.)/u,
146 (match, fst, snd, offset, str, groups) => { 153 (match, fst, snd, offset, str, groups) => {
147 assertEquals("ab", match); 154 assertEquals("ab", match);
148 assertEquals("a", groups.fst); 155 assertEquals("a", groups.fst);
149 assertEquals("b", groups.snd); 156 assertEquals("b", groups.snd);
150 assertEquals("a", fst); 157 assertEquals("a", fst);
151 assertEquals("b", snd); 158 assertEquals("b", snd);
152 assertEquals(0, offset); 159 assertEquals(0, offset);
153 assertEquals("abcd", str); 160 assertEquals("abcd", str);
154 return `${groups.snd}${groups.fst}`; 161 return `${groups.snd}${groups.fst}`;
155 }); 162 });
156 assertEquals("bacd", result); 163 assertEquals("bacd", result);
164
165 assertEquals("undefinedbcd",
166 "abcd".replace(/(?<fst>.)|(?<snd>.)/u,
167 (match, fst, snd, offset, str, groups) => groups.snd));
157 } 168 }
158 169
159 function toSlowMode(re) { 170 function toSlowMode(re) {
160 re.exec = (str) => RegExp.prototype.exec.call(re, str); 171 re.exec = (str) => RegExp.prototype.exec.call(re, str);
161 return re; 172 return re;
162 } 173 }
163 174
164 // @@replace with a callable replacement argument (slow, global, 175 // @@replace with a callable replacement argument (slow, global,
165 // named captures). 176 // named captures).
166 { 177 {
(...skipping 16 matching lines...) Expand all
183 assertEquals("d", snd); 194 assertEquals("d", snd);
184 assertEquals(2, offset); 195 assertEquals(2, offset);
185 assertEquals("abcd", str); 196 assertEquals("abcd", str);
186 } else { 197 } else {
187 assertUnreachable(); 198 assertUnreachable();
188 } 199 }
189 i++; 200 i++;
190 return `${groups.snd}${groups.fst}`; 201 return `${groups.snd}${groups.fst}`;
191 }); 202 });
192 assertEquals("badc", result); 203 assertEquals("badc", result);
204
205 assertEquals("undefinedundefinedundefinedundefined",
206 "abcd".replace(toSlowMode(/(?<fst>.)|(?<snd>.)/gu),
207 (match, fst, snd, offset, str, groups) => groups.snd));
193 } 208 }
194 209
195 // @@replace with a callable replacement argument (slow, non-global, 210 // @@replace with a callable replacement argument (slow, non-global,
196 // named captures). 211 // named captures).
197 { 212 {
198 let re = toSlowMode(/(?<fst>.)(?<snd>.)/u); 213 let re = toSlowMode(/(?<fst>.)(?<snd>.)/u);
199 let result = "abcd".replace(re, (match, fst, snd, offset, str, groups) => { 214 let result = "abcd".replace(re, (match, fst, snd, offset, str, groups) => {
200 assertEquals("ab", match); 215 assertEquals("ab", match);
201 assertEquals("a", groups.fst); 216 assertEquals("a", groups.fst);
202 assertEquals("b", groups.snd); 217 assertEquals("b", groups.snd);
203 assertEquals("a", fst); 218 assertEquals("a", fst);
204 assertEquals("b", snd); 219 assertEquals("b", snd);
205 assertEquals(0, offset); 220 assertEquals(0, offset);
206 assertEquals("abcd", str); 221 assertEquals("abcd", str);
207 return `${groups.snd}${groups.fst}`; 222 return `${groups.snd}${groups.fst}`;
208 }); 223 });
209 assertEquals("bacd", result); 224 assertEquals("bacd", result);
225
226 assertEquals("undefinedbcd",
227 "abcd".replace(toSlowMode(/(?<fst>.)|(?<snd>.)/u),
228 (match, fst, snd, offset, str, groups) => groups.snd));
210 } 229 }
211 230
212 // @@replace with a string replacement argument (no named captures). 231 // @@replace with a string replacement argument (no named captures).
213 { 232 {
214 let re = /(.)(.)/u; 233 let re = /(.)(.)/u;
215 assertEquals("$<snd>$<fst>cd", "abcd".replace(re, "$<snd>$<fst>")); 234 assertEquals("$<snd>$<fst>cd", "abcd".replace(re, "$<snd>$<fst>"));
216 assertEquals("bacd", "abcd".replace(re, "$2$1")); 235 assertEquals("bacd", "abcd".replace(re, "$2$1"));
217 assertEquals("$<sndcd", "abcd".replace(re, "$<snd")); 236 assertEquals("$<sndcd", "abcd".replace(re, "$<snd"));
218 assertEquals("$<42a>cd", "abcd".replace(re, "$<42$1>")); 237 assertEquals("$<42a>cd", "abcd".replace(re, "$<42$1>"));
219 assertEquals("$<thd>cd", "abcd".replace(re, "$<thd>")); 238 assertEquals("$<thd>cd", "abcd".replace(re, "$<thd>"));
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 // named captures). 276 // named captures).
258 { 277 {
259 let re = toSlowMode(/(?<fst>.)(?<snd>.)/u); 278 let re = toSlowMode(/(?<fst>.)(?<snd>.)/u);
260 assertEquals("bacd", "abcd".replace(re, "$<snd>$<fst>")); 279 assertEquals("bacd", "abcd".replace(re, "$<snd>$<fst>"));
261 assertEquals("bacd", "abcd".replace(re, "$2$1")); 280 assertEquals("bacd", "abcd".replace(re, "$2$1"));
262 assertThrows(() => "abcd".replace(re, "$<snd"), SyntaxError); 281 assertThrows(() => "abcd".replace(re, "$<snd"), SyntaxError);
263 assertEquals("cd", "abcd".replace(re, "$<42$1>")); 282 assertEquals("cd", "abcd".replace(re, "$<42$1>"));
264 assertEquals("cd", "abcd".replace(re, "$<thd>")); 283 assertEquals("cd", "abcd".replace(re, "$<thd>"));
265 assertEquals("cd", "abcd".replace(re, "$<$1>")); 284 assertEquals("cd", "abcd".replace(re, "$<$1>"));
266 } 285 }
OLDNEW
« no previous file with comments | « src/runtime/runtime-regexp.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698