summaryrefslogtreecommitdiffstats
path: root/js/src/tests/test262/built-ins/RegExpStringIteratorPrototype/next/custom-regexpexec.js
blob: b7970c1db23b67e04fc65b48a075030490383d38 (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
// Copyright (C) 2018 Peter Wong. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: pending
description: Behavior with a custom RegExp exec
info: |
  %RegExpStringIteratorPrototype%.next ( )
    [...]
    9. Let match be ? RegExpExec(R, S).

  Runtime Semantics: RegExpExec ( R, S )
    1. Assert: Type(R) is Object.
    2. Assert: Type(S) is String.
    3. Let exec be ? Get(R, "exec").
    4. If IsCallable(exec) is true, then
      a. Let result be ? Call(exec, R, « S »).
      b. If Type(result) is neither Object or Null, throw a TypeError exception.
      c. Return result.
features: [Symbol.matchAll]
---*/

var regexp = /./g;
var str = 'abc';
var iter = regexp[Symbol.matchAll](str);

var callArgs, callCount;
function callNextWithExecReturnValue(returnValue) {
  callArgs = undefined;
  callCount = 0;

  RegExp.prototype.exec = function() {
    callArgs = arguments;
    callCount++;
    return returnValue;
  }

  return iter.next();
}

var firstExecReturnValue = ['ab'];
var result = callNextWithExecReturnValue(firstExecReturnValue);
assert.sameValue(result.value, firstExecReturnValue);
assert(!result.done);

assert.sameValue(callArgs.length, 1);
assert.sameValue(callArgs[0], str);
assert.sameValue(callCount, 1);

result = callNextWithExecReturnValue(null);
assert.sameValue(result.value, undefined);
assert(result.done);

assert.sameValue(callArgs.length, 1);
assert.sameValue(callArgs[0], str);
assert.sameValue(callCount, 1);

reportCompare(0, 0);