From 6de6484e21c3c7b5ab4689b52b094fb4d92750c7 Mon Sep 17 00:00:00 2001 From: Alexey Shvayka Date: Thu, 2 Apr 2020 14:57:24 +0300 Subject: [PATCH] Add functional replacement test --- .../Symbol.replace/named-groups-fn.js | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 test/built-ins/RegExp/prototype/Symbol.replace/named-groups-fn.js diff --git a/test/built-ins/RegExp/prototype/Symbol.replace/named-groups-fn.js b/test/built-ins/RegExp/prototype/Symbol.replace/named-groups-fn.js new file mode 100644 index 0000000000..04f0c30f3b --- /dev/null +++ b/test/built-ins/RegExp/prototype/Symbol.replace/named-groups-fn.js @@ -0,0 +1,59 @@ +// Copyright (C) 2020 Alexey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-regexp.prototype-@@replace +description: > + "groups" value is passed as last argument of replacer unless it is undefined. +info: | + RegExp.prototype [ @@replace ] ( string, replaceValue ) + + [...] + 14. For each result in results, do + [...] + j. Let namedCaptures be ? Get(result, "groups"). + k. If functionalReplace is true, then + [...] + iv. If namedCaptures is not undefined, then + 1. Append namedCaptures as the last element of replacerArgs. + v. Let replValue be ? Call(replaceValue, undefined, replacerArgs). +features: [Symbol.replace, regexp-named-groups] +---*/ + +var matchGroups; +var re = /./; +re.exec = function() { + return { + length: 1, + 0: "a", + index: 0, + groups: matchGroups, + }; +}; + +var replacerCalls = 0; +var replacerLastArg; +var replacer = function() { + replacerCalls++; + replacerLastArg = arguments[arguments.length - 1]; +}; + +matchGroups = null; +re[Symbol.replace]("a", replacer); +assert.sameValue(replacerCalls, 1); +assert.sameValue(replacerLastArg, matchGroups); + +matchGroups = undefined; +re[Symbol.replace]("a", replacer); +assert.sameValue(replacerCalls, 2); +assert.sameValue(replacerLastArg, "a"); + +matchGroups = 10; +re[Symbol.replace]("a", replacer); +assert.sameValue(replacerCalls, 3); +assert.sameValue(replacerLastArg, matchGroups); + +matchGroups = {}; +re[Symbol.replace]("a", replacer); +assert.sameValue(replacerCalls, 4); +assert.sameValue(replacerLastArg, matchGroups);