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

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

Issue 2764343004: [regexp] Named capture support for callable replacements (Closed)
Patch Set: Final tweaks Created 3 years, 9 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 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 assertEquals("a", /(?<$𐒤>a)/u.exec("bab").groups.$𐒤); 89 assertEquals("a", /(?<$𐒤>a)/u.exec("bab").groups.$𐒤);
90 assertEquals("a", /(?<_\u200C>a)/u.exec("bab").groups._\u200C); 90 assertEquals("a", /(?<_\u200C>a)/u.exec("bab").groups._\u200C);
91 assertEquals("a", /(?<_\u200D>a)/u.exec("bab").groups._\u200D); 91 assertEquals("a", /(?<_\u200D>a)/u.exec("bab").groups._\u200D);
92 assertEquals("a", /(?<ಠ_ಠ>a)/u.exec("bab").groups.ಠ_ಠ); 92 assertEquals("a", /(?<ಠ_ಠ>a)/u.exec("bab").groups.ಠ_ಠ);
93 assertThrows('/(?<❤>a)/u', SyntaxError); 93 assertThrows('/(?<❤>a)/u', SyntaxError);
94 assertThrows('/(?<𐒤>a)/u', SyntaxError); // ID_Continue but not ID_Start. 94 assertThrows('/(?<𐒤>a)/u', SyntaxError); // ID_Continue but not ID_Start.
95 95
96 // The '__proto__' property on the groups object. 96 // The '__proto__' property on the groups object.
97 assertEquals(undefined, /(?<a>.)/u.exec("a").groups.__proto__); 97 assertEquals(undefined, /(?<a>.)/u.exec("a").groups.__proto__);
98 assertEquals("a", /(?<__proto__>a)/u.exec("a").groups.__proto__); 98 assertEquals("a", /(?<__proto__>a)/u.exec("a").groups.__proto__);
99
100 // @@replace with a callable replacement argument (no named captures).
101 {
102 let result = "abcd".replace(/(.)(.)/u, (match, fst, snd, offset, str) => {
103 assertEquals("ab", match);
104 assertEquals("a", fst);
105 assertEquals("b", snd);
106 assertEquals(0, offset);
107 assertEquals("abcd", str);
108 return `${snd}${fst}`;
109 });
110 assertEquals("bacd", result);
111 }
112
113 // @@replace with a callable replacement argument (global, named captures).
114 {
115 let i = 0;
116 let result = "abcd".replace(/(?<fst>.)(?<snd>.)/gu,
117 (match, fst, snd, offset, str, groups) => {
118 if (i == 0) {
119 assertEquals("ab", match);
120 assertEquals("a", groups.fst);
121 assertEquals("b", groups.snd);
122 assertEquals("a", fst);
123 assertEquals("b", snd);
124 assertEquals(0, offset);
125 assertEquals("abcd", str);
126 } else if (i == 1) {
127 assertEquals("cd", match);
128 assertEquals("c", groups.fst);
129 assertEquals("d", groups.snd);
130 assertEquals("c", fst);
131 assertEquals("d", snd);
132 assertEquals(2, offset);
133 assertEquals("abcd", str);
134 } else {
135 assertUnreachable();
136 }
137 i++;
138 return `${groups.snd}${groups.fst}`;
139 });
140 assertEquals("badc", result);
141 }
142
143 // @@replace with a callable replacement argument (non-global, named captures).
144 {
145 let result = "abcd".replace(/(?<fst>.)(?<snd>.)/u,
146 (match, fst, snd, offset, str, groups) => {
147 assertEquals("ab", match);
148 assertEquals("a", groups.fst);
149 assertEquals("b", groups.snd);
150 assertEquals("a", fst);
151 assertEquals("b", snd);
152 assertEquals(0, offset);
153 assertEquals("abcd", str);
154 return `${groups.snd}${groups.fst}`;
155 });
156 assertEquals("bacd", result);
157 }
158
159 function toSlowMode(re) {
160 re.exec = (str) => RegExp.prototype.exec.call(re, str);
161 return re;
162 }
163
164 // @@replace with a callable replacement argument (slow, global,
165 // named captures).
166 {
167 let i = 0;
168 let re = toSlowMode(/(?<fst>.)(?<snd>.)/gu);
169 let result = "abcd".replace(re, (match, fst, snd, offset, str, groups) => {
170 if (i == 0) {
171 assertEquals("ab", match);
172 assertEquals("a", groups.fst);
173 assertEquals("b", groups.snd);
174 assertEquals("a", fst);
175 assertEquals("b", snd);
176 assertEquals(0, offset);
177 assertEquals("abcd", str);
178 } else if (i == 1) {
179 assertEquals("cd", match);
180 assertEquals("c", groups.fst);
181 assertEquals("d", groups.snd);
182 assertEquals("c", fst);
183 assertEquals("d", snd);
184 assertEquals(2, offset);
185 assertEquals("abcd", str);
186 } else {
187 assertUnreachable();
188 }
189 i++;
190 return `${groups.snd}${groups.fst}`;
191 });
192 assertEquals("badc", result);
193 }
194
195 // @@replace with a callable replacement argument (slow, non-global,
196 // named captures).
197 {
198 let re = toSlowMode(/(?<fst>.)(?<snd>.)/u);
199 let result = "abcd".replace(re, (match, fst, snd, offset, str, groups) => {
200 assertEquals("ab", match);
201 assertEquals("a", groups.fst);
202 assertEquals("b", groups.snd);
203 assertEquals("a", fst);
204 assertEquals("b", snd);
205 assertEquals(0, offset);
206 assertEquals("abcd", str);
207 return `${groups.snd}${groups.fst}`;
208 });
209 assertEquals("bacd", result);
210 }
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