Skip to content
Snippets Groups Projects
Commit c36bdcfe authored by Peter Wong's avatar Peter Wong Committed by Leo Balter
Browse files

Update tests for String.prototype.matchAll (#1990)

- Changes associated with spec changes (tc39/proposal-string-matchall#41)
- Update spec comments
- Added test verifying error thrown when `regexp[Symbol.matchAll]` is not callable
- Added test verifying `ToString` is called on `receiver`
parent b2597d06
No related branches found
No related tags found
No related merge requests found
...@@ -8,14 +8,9 @@ info: | ...@@ -8,14 +8,9 @@ info: |
1. Let O be ? RequireObjectCoercible(this value). 1. Let O be ? RequireObjectCoercible(this value).
2. If regexp is neither undefined nor null, then 2. If regexp is neither undefined nor null, then
[...] [...]
3. Return ? MatchAllIterator(regexp, O). 3. Let S be ? ToString(O).
4. Let rx be ? RegExpCreate(R, "g").
MatchAllIterator( regexp, O ) 5. Return ? Invoke(rx, @@matchAll, « S »).
[...]
2. If ? IsRegExp(regexp) is true, then
[...]
3. Else,
a. Let R be RegExpCreate(regexp, "g").
features: [String.prototype.matchAll] features: [String.prototype.matchAll]
includes: [compareArray.js, compareIterator.js, regExpUtils.js] includes: [compareArray.js, compareIterator.js, regExpUtils.js]
---*/ ---*/
......
// 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 when regexp is null or undefined
info: |
String.prototype.matchAll ( regexp )
1. Let O be ? RequireObjectCoercible(this value).
2. If regexp is neither undefined nor null, then
[...]
3. Let S be ? ToString(O).
4. Let rx be ? RegExpCreate(R, "g").
5. Return ? Invoke(rx, @@matchAll, « S »).
features: [String.prototype.matchAll]
includes: [compareArray.js, compareIterator.js, regExpUtils.js]
---*/
var callCount = 0;
var obj = {};
RegExp.prototype[Symbol.matchAll] = function() {
callCount++;
return obj;
};
assert.sameValue('a'.matchAll(null), obj);
assert.sameValue(callCount, 1);
assert.sameValue(''.matchAll(undefined), obj);
assert.sameValue(callCount, 2);
// 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 when regexp[@@matchAll] is undefined or null
info: |
String.prototype.matchAll ( regexp )
1. Let O be ? RequireObjectCoercible(this value).
2. If regexp is neither undefined nor null, then
a. Let matcher be ? GetMethod(regexp, @@matchAll).
b. If matcher is not undefined, then
[...]
3. Let S be ? ToString(O).
4. Let rx be ? RegExpCreate(R, "g").
5. Return ? Invoke(rx, @@matchAll, « S »).
features: [Symbol.matchAll, String.prototype.matchAll]
---*/
var regexp = /./;
var callCount = 0;
var arg;
var obj = {};
var str = 'abc';
RegExp.prototype[Symbol.matchAll] = function(string) {
arg = string;
callCount++;
return obj;
};
regexp[Symbol.matchAll] = undefined;
assert.sameValue(str.matchAll(regexp), obj);
assert.sameValue(arg, str);
assert.sameValue(callCount, 1);
regexp[Symbol.matchAll] = null;
assert.sameValue(str.matchAll(regexp), obj);
assert.sameValue(arg, str);
assert.sameValue(callCount, 2);
// 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 when regexp[@@matchAll] is not callable
info: |
String.prototype.matchAll ( regexp )
[...]
2. If regexp is neither undefined nor null, then
a. Let matcher be ? GetMethod(regexp, @@matchAll).
features: [Symbol.matchAll, String.prototype.matchAll]
---*/
var regexp = /./;
regexp[Symbol.matchAll] = true;
assert.throws(TypeError, function() {
''.matchAll(regexp);
});
regexp[Symbol.matchAll] = 5;
assert.throws(TypeError, function() {
''.matchAll(regexp);
});
regexp[Symbol.matchAll] = '';
assert.throws(TypeError, function() {
''.matchAll(regexp);
});
regexp[Symbol.matchAll] = Symbol();
assert.throws(TypeError, function() {
''.matchAll(regexp);
});
...@@ -11,17 +11,9 @@ info: | ...@@ -11,17 +11,9 @@ info: |
b. If matcher is not undefined, then b. If matcher is not undefined, then
[...] [...]
[...] [...]
4. Let matcher be ? RegExpCreate(R, "g"). 4. Let rx be ? RegExpCreate(R, "g").
[...] 5. Return ? Invoke(rx, @@matchAll, « S »).
21.2.3.2.3 Runtime Semantics: RegExpCreate ( P, F )
[...]
2. Return ? RegExpInitialize(obj, P, F).
21.2.3.2.2 Runtime Semantics: RegExpInitialize ( obj, pattern, flags )
1. If pattern is undefined, let P be the empty String.
2. Else, let P be ? ToString(pattern).
[...]
features: [Symbol.matchAll, String.prototype.matchAll] features: [Symbol.matchAll, String.prototype.matchAll]
includes: [compareArray.js, compareIterator.js, regExpUtils.js] includes: [compareArray.js, compareIterator.js, regExpUtils.js]
---*/ ---*/
...@@ -29,7 +21,6 @@ includes: [compareArray.js, compareIterator.js, regExpUtils.js] ...@@ -29,7 +21,6 @@ includes: [compareArray.js, compareIterator.js, regExpUtils.js]
delete RegExp.prototype[Symbol.matchAll]; delete RegExp.prototype[Symbol.matchAll];
var str = '/a/g*/b/g'; var str = '/a/g*/b/g';
assert.compareIterator(str.matchAll(/\w/g), [ assert.throws(TypeError, function() {
matchValidator(['/a/g'], 0, str), str.matchAll(/\w/g);
matchValidator(['/b/g'], 5, str) });
]);
// Copyright (C) 2018 Peter Wong. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: pending
description: |
Verify ToString is called when regexp[@@matchAll] is undefined or null
info: |
String.prototype.matchAll ( regexp )
1. Let O be ? RequireObjectCoercible(this value).
2. If regexp is neither undefined nor null, then
a. Let matcher be ? GetMethod(regexp, @@matchAll).
b. If matcher is not undefined, then
[...]
3. Let S be ? ToString(O).
4. Let rx be ? RegExpCreate(R, "g").
5. Return ? Invoke(rx, @@matchAll, « S »).
features: [Symbol.matchAll, String.prototype.matchAll]
---*/
var regexp = /./;
var callCount = 0;
var arg;
var obj = {};
var toStringResult = 'abc';
var receiver = {
[Symbol.toPrimitive]: function() {
callCount++;
return toStringResult;
}
};
RegExp.prototype[Symbol.matchAll] = function(string) {
arg = string;
};
String.prototype.matchAll.call(receiver, null);
assert.sameValue(callCount, 1);
assert.sameValue(arg, toStringResult);
String.prototype.matchAll.call(receiver, undefined);
assert.sameValue(callCount, 2);
assert.sameValue(arg, toStringResult);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment