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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/runtime/runtime-regexp.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/mjsunit/harmony/regexp-named-captures.js
diff --git a/test/mjsunit/harmony/regexp-named-captures.js b/test/mjsunit/harmony/regexp-named-captures.js
index 190566db4cf8df3749b228ce6cc569b3c1e9cb70..33013c6c9b4b41b88d54b2863ffe1af49bc4ea34 100644
--- a/test/mjsunit/harmony/regexp-named-captures.js
+++ b/test/mjsunit/harmony/regexp-named-captures.js
@@ -96,3 +96,115 @@ assertThrows('/(?<𐒤>a)/u', SyntaxError); // ID_Continue but not ID_Start.
// The '__proto__' property on the groups object.
assertEquals(undefined, /(?<a>.)/u.exec("a").groups.__proto__);
assertEquals("a", /(?<__proto__>a)/u.exec("a").groups.__proto__);
+
+// @@replace with a callable replacement argument (no named captures).
+{
+ let result = "abcd".replace(/(.)(.)/u, (match, fst, snd, offset, str) => {
+ assertEquals("ab", match);
+ assertEquals("a", fst);
+ assertEquals("b", snd);
+ assertEquals(0, offset);
+ assertEquals("abcd", str);
+ return `${snd}${fst}`;
+ });
+ assertEquals("bacd", result);
+}
+
+// @@replace with a callable replacement argument (global, named captures).
+{
+ let i = 0;
+ let result = "abcd".replace(/(?<fst>.)(?<snd>.)/gu,
+ (match, fst, snd, offset, str, groups) => {
+ if (i == 0) {
+ assertEquals("ab", match);
+ assertEquals("a", groups.fst);
+ assertEquals("b", groups.snd);
+ assertEquals("a", fst);
+ assertEquals("b", snd);
+ assertEquals(0, offset);
+ assertEquals("abcd", str);
+ } else if (i == 1) {
+ assertEquals("cd", match);
+ assertEquals("c", groups.fst);
+ assertEquals("d", groups.snd);
+ assertEquals("c", fst);
+ assertEquals("d", snd);
+ assertEquals(2, offset);
+ assertEquals("abcd", str);
+ } else {
+ assertUnreachable();
+ }
+ i++;
+ return `${groups.snd}${groups.fst}`;
+ });
+ assertEquals("badc", result);
+}
+
+// @@replace with a callable replacement argument (non-global, named captures).
+{
+ let result = "abcd".replace(/(?<fst>.)(?<snd>.)/u,
+ (match, fst, snd, offset, str, groups) => {
+ assertEquals("ab", match);
+ assertEquals("a", groups.fst);
+ assertEquals("b", groups.snd);
+ assertEquals("a", fst);
+ assertEquals("b", snd);
+ assertEquals(0, offset);
+ assertEquals("abcd", str);
+ return `${groups.snd}${groups.fst}`;
+ });
+ assertEquals("bacd", result);
+}
+
+function toSlowMode(re) {
+ re.exec = (str) => RegExp.prototype.exec.call(re, str);
+ return re;
+}
+
+// @@replace with a callable replacement argument (slow, global,
+// named captures).
+{
+ let i = 0;
+ let re = toSlowMode(/(?<fst>.)(?<snd>.)/gu);
+ let result = "abcd".replace(re, (match, fst, snd, offset, str, groups) => {
+ if (i == 0) {
+ assertEquals("ab", match);
+ assertEquals("a", groups.fst);
+ assertEquals("b", groups.snd);
+ assertEquals("a", fst);
+ assertEquals("b", snd);
+ assertEquals(0, offset);
+ assertEquals("abcd", str);
+ } else if (i == 1) {
+ assertEquals("cd", match);
+ assertEquals("c", groups.fst);
+ assertEquals("d", groups.snd);
+ assertEquals("c", fst);
+ assertEquals("d", snd);
+ assertEquals(2, offset);
+ assertEquals("abcd", str);
+ } else {
+ assertUnreachable();
+ }
+ i++;
+ return `${groups.snd}${groups.fst}`;
+ });
+ assertEquals("badc", result);
+}
+
+// @@replace with a callable replacement argument (slow, non-global,
+// named captures).
+{
+ let re = toSlowMode(/(?<fst>.)(?<snd>.)/u);
+ let result = "abcd".replace(re, (match, fst, snd, offset, str, groups) => {
+ assertEquals("ab", match);
+ assertEquals("a", groups.fst);
+ assertEquals("b", groups.snd);
+ assertEquals("a", fst);
+ assertEquals("b", snd);
+ assertEquals(0, offset);
+ assertEquals("abcd", str);
+ return `${groups.snd}${groups.fst}`;
+ });
+ assertEquals("bacd", result);
+}
« 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