summaryrefslogtreecommitdiffstats
path: root/js/src/tests/test262/built-ins/String/prototype/replaceAll/searchValue-replacer-RegExp-call-fn.js
blob: b81dfe90ba996e4319cef6dd95df4e9c27f483a8 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (C) 2019 Leo Balter. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-string.prototype.replaceall
description: >
  A RegExp searchValue's Symbol.replace can be called instead of the next steps of replaceAll
info: |
  String.prototype.replaceAll ( searchValue, replaceValue )

  1. Let O be RequireObjectCoercible(this value).
  2. If searchValue is neither undefined nor null, then
    a. Let isRegExp be ? IsRegExp(searchString).
    b. If isRegExp is true, then
      i. Let flags be ? Get(searchValue, "flags").
      ii. Perform ? RequireObjectCoercible(flags).
      iii. If ? ToString(flags) does not contain "g", throw a TypeError exception.
    c. Let replacer be ? GetMethod(searchValue, @@replace).
    d. If replacer is not undefined, then
      i. Return ? Call(replacer, searchValue, « O, replaceValue »).
  3. Let string be ? ToString(O).
  4. Let searchString be ? ToString(searchValue).
  ...
features: [String.prototype.replaceAll, Symbol.replace, class]
includes: [compareArray.js]
---*/

let called = 0;

class RE extends RegExp {
  [Symbol.replace](...args) {
    const actual = super[Symbol.replace](...args);

    // Ordering is intentional to observe call from super
    called += 1;
    return actual;
  }

  toString() {
    throw 'Should not call toString on searchValue';
  }
}

const t = (function() { return this; })();
let calls;

function getFn(val) {
  return function replaceValueFn(...args) {
    calls.push([this, ...args]);
    return val;
  };
}

const samples = [
  [ '(a)', 'aaa abc', 'z', 'zzz zbc' ],
  [ '(a)', 'aaa abc', '$1', '$1$1$1 $1bc' ],
  [ '(a)', 'aaa abc', '$$', '$$$$$$ $$bc' ],
  [ '(a)', 'aaa abc', '$&', '$&$&$& $&bc' ],
  [ '(a)', 'aaa abc', '$\'', '$\'$\'$\' $\'bc' ],
  [ '(a)', 'aaa abc', '$`', '$`$`$` $`bc' ],
];

let count = 0;
for (const [ reStr, thisValue, replaceValue, expected ] of samples) {
  const searchValue = new RE(reStr, 'g');
  const replaceFn = getFn(replaceValue);

  // Observes the toString
  const obj = new String(thisValue);

  called = 0;
  calls = [];

  const actual = obj.replaceAll(searchValue, replaceFn);

  const message = `sample ${count}: '${thisValue}'.replaceAll(/${reStr}/g, () => '${replaceValue}')`;

  assert.sameValue(called, 1, `called -- ${message}`);
  assert.sameValue(actual, expected, `actual -- ${message}`);

  assert.sameValue(calls.length, 4, `calls.length -- ${message}`);
  assert.compareArray(calls[0], [t, 'a', 'a', 0, thisValue]);
  assert.compareArray(calls[1], [t, 'a', 'a', 1, thisValue]);
  assert.compareArray(calls[2], [t, 'a', 'a', 2, thisValue]);
  assert.compareArray(calls[3], [t, 'a', 'a', 4, thisValue]);

  count += 1;
}

const samplesSticky = [
  [ '(a)', 'aaa abc', 'z', 'zzz abc' ],
  [ '(a)', 'aaa abc', '$1', '$1$1$1 abc' ],
  [ '(a)', 'aaa abc', '$$', '$$$$$$ abc' ],
  [ '(a)', 'aaa abc', '$&', '$&$&$& abc' ],
  [ '(a)', 'aaa abc', '$\'', '$\'$\'$\' abc' ],
  [ '(a)', 'aaa abc', '$`', '$`$`$` abc' ],
];

count = 0;
for (const [ reStr, thisValue, replaceValue, expected ] of samplesSticky) {
  const searchValue = new RE(reStr, 'gy');
  const replaceFn = getFn(replaceValue);

  // Observes the toString
  const obj = new String(thisValue);

  called = 0;
  calls = [];

  const actual = obj.replaceAll(searchValue, replaceFn);

  const message = `sample ${count}: '${thisValue}'.replaceAll(/${reStr}/gy, () => '${replaceValue}')`;

  assert.sameValue(called, 1, `called -- ${message}`);
  assert.sameValue(actual, expected, `actual -- ${message}`);

  assert.sameValue(calls.length, 3, `calls.length -- ${message}`);
  assert.compareArray(calls[0], [t, 'a', 'a', 0, thisValue]);
  assert.compareArray(calls[1], [t, 'a', 'a', 1, thisValue]);
  assert.compareArray(calls[2], [t, 'a', 'a', 2, thisValue]);

  count += 1;
}

reportCompare(0, 0);