Skip to content
Snippets Groups Projects
Unverified Commit 5b3914a3 authored by Jordan Harband's avatar Jordan Harband
Browse files

String.prototype.matchAll: add tests for stage 3

`String.prototype.matchAll`: Add basic tests.
parent 73b8d9c7
No related branches found
No related tags found
No related merge requests found
// Copyright (C) 2018 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Behavior when error is thrown accessing @@matchAll property
info: |
[...]
4. Let matcher be ? GetMethod(R, @@matchAll).
5. If matcher is not undefined, then
a. Return ? Call(matcher, R, « O »).
features: [Symbol.match, Symbol.matchAll]
---*/
var obj = {};
Object.defineProperty(obj, Symbol.match, {
value: true // to make IsRegExp pass
});
Object.defineProperty(obj, Symbol.matchAll, {
get: function() {
throw new Test262Error();
}
});
assert.throws(Test262Error, function() {
''.matchAll(obj);
});
// Copyright (C) 2018 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Invocation of @@matchAll property of user-supplied RegExp objects
info: |
[...]
4. Let matcher be ? GetMethod(R, @@matchAll).
5. If matcher is not undefined, then
a. Return ? Call(matcher, R, « O »).
features: [Symbol.match, Symbol.matchAll]
---*/
var obj = {};
var returnVal = {};
var callCount = 0;
var thisVal, args;
obj[Symbol.match] = true; // https://tc39.github.io/ecma262/#sec-isregexp steps 1-3
obj[Symbol.matchAll] = function () {
callCount += 1;
thisVal = this;
args = arguments;
return returnVal;
};
var str = '';
assert.sameValue(str.matchAll(obj), returnVal);
assert.sameValue(callCount, 1, 'Invokes the method exactly once');
assert.sameValue(thisVal, obj);
assert.notSameValue(args, undefined);
assert.sameValue(args.length, 1);
assert.sameValue(args[0], str);
// Copyright (C) 2018 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Invocation of @@matchAll property of internally-created RegExps
info: |
[...]
3. Else,
a. Let R be ? RegExpCreate(regexp, "g").
4. Let matcher be ? GetMethod(R, @@matchAll).
5. If matcher is not undefined, then
a. Return ? Call(matcher, R, « O »).
6. Return ? MatchAllIterator(R, O).
features: [Symbol.match, Symbol.matchAll]
---*/
var originalMatchAll = RegExp.prototype[Symbol.matchAll];
var returnVal = {};
var result, thisVal, args;
RegExp.prototype[Symbol.matchAll] = function() {
thisVal = this;
args = arguments;
return returnVal;
};
var str = 'target';
var stringToMatch = 'string source';
try {
result = str.matchAll(stringToMatch);
assert(thisVal instanceof RegExp);
assert.sameValue(!!thisVal[Symbol.match], true);
assert.sameValue(thisVal.source, stringToMatch);
assert.sameValue(thisVal.flags, 'g');
assert.sameValue(thisVal.lastIndex, 0);
assert.sameValue(args.length, 1);
assert.sameValue(args[0], str);
assert.sameValue(result, returnVal);
} finally {
RegExp.prototype[Symbol.matchAll] = originalMatchAll;
}
// Copyright (C) 2018 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Length of String.prototype.matchAll
info: |
ES6 Section 17:
Every built-in Function object, including constructors, has a length
property whose value is an integer. Unless otherwise specified, this value
is equal to the largest number of named arguments shown in the subclause
headings for the function description, including optional parameters.
[...]
Unless otherwise specified, the length property of a built-in Function
object has the attributes { [[Writable]]: false, [[Enumerable]]: false,
[[Configurable]]: true }.
includes: [propertyHelper.js]
---*/
assert.sameValue(String.prototype.matchAll.length, 1);
verifyNotEnumerable(String.prototype.matchAll, 'length');
verifyNotWritable(String.prototype.matchAll, 'length');
verifyConfigurable(String.prototype.matchAll, 'length');
// Copyright (C) 2018 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Descriptor for `name` property
info: |
The value of the name property of this function is "matchAll".
ES6 Section 17: ECMAScript Standard Built-in Objects
Every built-in Function object, including constructors, that is not
identified as an anonymous function has a name property whose value is a
String. Unless otherwise specified, this value is the name that is given to
the function in this specification.
[...]
Unless otherwise specified, the name property of a built-in Function
object, if it exists, has the attributes { [[Writable]]: false,
[[Enumerable]]: false, [[Configurable]]: true }.
includes: [propertyHelper.js]
---*/
assert.sameValue(String.prototype[Symbol.iterator].name, 'matchAll');
verifyNotEnumerable(String.prototype[Symbol.iterator], 'name');
verifyNotWritable(String.prototype[Symbol.iterator], 'name');
verifyConfigurable(String.prototype[Symbol.iterator], 'name');
// Copyright (C) 2018 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Property descriptor
info: |
ES6 Section 17
Every other data property described in clauses 18 through 26 and in Annex
B.2 has the attributes { [[Writable]]: true, [[Enumerable]]: false,
[[Configurable]]: true } unless otherwise specified.
features: [Symbol.iterator]
includes: [propertyHelper.js]
---*/
assert.sameValue(typeof String.prototype.matchAll, 'function');
verifyNotEnumerable(String.prototype, 'matchAll');
verifyWritable(String.prototype, 'matchAll');
verifyConfigurable(String.prototype, 'matchAll');
// Copyright (C) 2018 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: The `this` value cannot be coerced into an object
info: |
1. Let O be RequireObjectCoercible(this value).
2. Let S be ToString(O).
3. ReturnIfAbrupt(S).
features: [Symbol.iterator]
---*/
var matchAll = String.prototype.matchAll;
assert.sameValue(typeof matchAll, 'function');
assert.throws(TypeError, function() {
matchAll.call(undefined);
}, 'undefined');
assert.throws(TypeError, function() {
matchAll.call(null);
}, 'null');
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