summaryrefslogtreecommitdiffstats
path: root/js/src/tests/test262/built-ins/RegExp/prototype/Symbol.replace/named-groups-fn.js
blob: 5ef8030922ad656407d478aaf9b1f5d617e279e5 (plain)
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
// 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);

reportCompare(0, 0);