Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for String.prototype.matchAll #1500

Merged
merged 3 commits into from
Apr 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions features.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ String.prototype.trimStart
# https://github.com/tc39/proposal-numeric-separator
numeric-separator-literal

# String.prototype.matchAll
# https://github.com/tc39/proposal-string-matchall
String.prototype.matchAll
Symbol.matchAll
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


# Standard language features
#
# Language features that have been included in a published version of the
Expand Down
34 changes: 34 additions & 0 deletions harness/compareIterator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (C) 2018 Peter Wong. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Compare the values of an iterator with an array of expected values
---*/

// Example:
//
// function* numbers() {
// yield 1;
// yield 2;
// yield 3;
// }
//
// compareIterator(numbers(), [
// v => assert.sameValue(v, 1),
// v => assert.sameValue(v, 2),
// v => assert.sameValue(v, 3),
// ]);
//
assert.compareIterator = function(iter, validators, message) {
message = message || '';

var i, result;
for (i = 0; i < validators.length; i++) {
result = iter.next();
assert(!result.done, 'Expected ' + i + ' values(s). Instead iterator only produced ' + (i - 1) + ' value(s). ' + message);
validators[i](result.value);
}

result = iter.next();
assert(result.done, 'Expected only ' + i + ' values(s). Instead iterator produced more. ' + message);
assert.sameValue(result.value, undefined, 'Expected value of `undefined` when iterator completes. ' + message);
}
15 changes: 15 additions & 0 deletions harness/regExpUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,18 @@ function testPropertyEscapes(regex, string, expression) {
}
}
}

// Returns a function that will validate RegExp match result
//
// Example:
//
// var validate = matchValidator(['b'], 1, 'abc');
// validate(/b/.exec('abc'));
//
function matchValidator(expectedEntries, expectedIndex, expectedInput) {
return function(match) {
assert.compareArray(match, expectedEntries, 'Match entries');
assert.sameValue(match.index, expectedIndex, 'Match index');
assert.sameValue(match.input, expectedInput, 'Match input');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (C) 2018 Peter Wong. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: pending
description: |
Throws TypeError when internally created RegExp's lastIndex is not 0
info: |
RegExp.prototype [ @@matchAll ] ( string )
[...]
3. Return ? MatchAllIterator(R, string).

MatchAllIterator ( R, O )
[...]
2. If ? IsRegExp(R) is true, then
[...]
3. Else,
a. Let matcher be RegExpCreate(R, "g").
b. If ? IsRegExp(matcher) is not true, throw a TypeError exception.
[...]
3. If Get(matcher, "lastIndex") is not 0, throw a TypeError exception.
features: [Symbol.match, Symbol.matchAll]
---*/

Object.defineProperty(RegExp.prototype, Symbol.match, {
get() {
this.lastIndex = 1;
return true;
}
});

assert.throws(TypeError, function() {
RegExp.prototype[Symbol.matchAll].call({}, '');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (C) 2018 Peter Wong. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: pending
description: Throws TypeError when internally created RegExp's @@match is false
info: |
RegExp.prototype [ @@matchAll ] ( string )
[...]
3. Return ? MatchAllIterator(R, string).

MatchAllIterator ( R, O )
[...]
2. If ? IsRegExp(R) is true, then
[...]
3. Else,
a. Let matcher be RegExpCreate(R, "g").
b. If ? IsRegExp(matcher) is not true, throw a TypeError exception.
features: [Symbol.match, Symbol.matchAll]
---*/

Object.defineProperty(RegExp.prototype, Symbol.match, {
get() {
return false;
}
});

assert.throws(TypeError, function() {
RegExp.prototype[Symbol.matchAll].call({}, '');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (C) 2018 Peter Wong. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: pending
description: Re-throws errors thrown while accessing @@match property
info: |
RegExp.prototype [ @@matchAll ] ( string )
[...]
3. Return ? MatchAllIterator(R, string).

MatchAllIterator ( R, O )
[...]
2. If ? IsRegExp(R) is true, then
[...]
3. Else,
a. Let matcher be RegExpCreate(R, "g").
b. If ? IsRegExp(matcher) is not true, throw a TypeError exception.
features: [Symbol.match, Symbol.matchAll]
---*/

Object.defineProperty(RegExp.prototype, Symbol.match, {
get() {
throw new Test262Error();
}
});

assert.throws(Test262Error, function() {
RegExp.prototype[Symbol.matchAll].call({}, '');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (C) 2018 Peter Wong. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: pending
description: Re-throws errors thrown while accessing RegExp's @@match property
info: |
RegExp.prototype [ @@matchAll ] ( string )
[...]
3. Return ? MatchAllIterator(R, string).

MatchAllIterator ( R, O )
[...]
2. If ? IsRegExp(R) is true, then
[...]
features: [Symbol.match, Symbol.matchAll]
---*/

var obj = {
get [Symbol.match]() {
throw new Test262Error();
}
};

assert.throws(Test262Error, function() {
RegExp.prototype[Symbol.matchAll].call(obj, '');
});
30 changes: 30 additions & 0 deletions test/built-ins/RegExp/prototype/Symbol.matchAll/length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (C) 2018 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: pending
description: RegExp.prototype[Symbol.matchAll] `length` property
info: |
17 ECMAScript Standard Built-in Objects:

[...]

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. Optional parameters
(which are indicated with brackets: [ ]) or rest parameters (which
are shown using the form «...name») are not included in the default
argument count.

Unless otherwise specified, the length property of a built-in function
object has the attributes { [[Writable]]: false, [[Enumerable]]: false,
[[Configurable]]: true }.
includes: [propertyHelper.js]
features: [Symbol.matchAll]
---*/

assert.sameValue(RegExp.prototype[Symbol.matchAll].length, 1);

verifyNotEnumerable(RegExp.prototype[Symbol.matchAll], 'length');
verifyNotWritable(RegExp.prototype[Symbol.matchAll], 'length');
verifyConfigurable(RegExp.prototype[Symbol.matchAll], 'length');
28 changes: 28 additions & 0 deletions test/built-ins/RegExp/prototype/Symbol.matchAll/name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (C) 2018 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: pending
description: RegExp.prototype[Symbol.matchAll] `name` property
info: |
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, the name property of a built-in function
object, if it exists, has the attributes { [[Writable]]: false,
[[Enumerable]]: false, [[Configurable]]: true }.
includes: [propertyHelper.js]
features: [Symbol.matchAll]
---*/

assert.sameValue(RegExp.prototype[Symbol.matchAll].name, '[Symbol.matchAll]');

verifyNotEnumerable(RegExp.prototype[Symbol.matchAll], 'name');
verifyNotWritable(RegExp.prototype[Symbol.matchAll], 'name');
verifyConfigurable(RegExp.prototype[Symbol.matchAll], 'name');
22 changes: 22 additions & 0 deletions test/built-ins/RegExp/prototype/Symbol.matchAll/prop-desc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (C) 2018 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: pending
description: RegExp.prototype[Symbol.matchAll] property descriptor
info: |
17 ECMAScript Standard Built-in Objects:

[...]

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.
includes: [propertyHelper.js]
features: [Symbol.matchAll]
---*/

assert.sameValue(typeof RegExp.prototype[Symbol.matchAll], 'function');

verifyNotEnumerable(RegExp.prototype, Symbol.matchAll);
verifyWritable(RegExp.prototype, Symbol.matchAll);
verifyConfigurable(RegExp.prototype, Symbol.matchAll);
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (C) 2018 Peter Wong. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: pending
description: Re-throws errors while creating an internal RegExp
info: |
RegExp.prototype [ @@matchAll ] ( string )
[...]
3. Return ? MatchAllIterator(R, string).

MatchAllIterator ( R, O )
[...]
2. If ? IsRegExp(R) is true, then
[...]
3. Else,
a. Let R be RegExpCreate(R, "g").
features: [Symbol.matchAll]
---*/

var obj = {
toString() {
throw new Test262Error();
}
};

assert.throws(Test262Error, function() {
RegExp.prototype[Symbol.matchAll].call(obj, '');
});

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (C) 2018 Peter Wong. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: pending
description: |
Re-throws errors thrown while accessing RegExp's constructor property
info: |
RegExp.prototype [ @@matchAll ] ( string )
[...]
3. Return ? MatchAllIterator(R, string).

MatchAllIterator ( R, O )
[...]
2. If ? IsRegExp(R) is true, then
a. Let C be ? SpeciesConstructor(R, RegExp).

SpeciesConstructor ( O, defaultConstructor )
[...]
2. Let C be ? Get(O, "constructor").
features: [Symbol.matchAll]
---*/

var regexp = /./;
Object.defineProperty(regexp, 'constructor', {
get(){
throw new Test262Error();
}
});

assert.throws(Test262Error, function() {
regexp[Symbol.matchAll]('');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (C) 2018 Peter Wong. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: pending
description: Re-throws errors thrown while accessing of @@species property
info: |
RegExp.prototype [ @@matchAll ] ( string )
[...]
3. Return ? MatchAllIterator(R, string).

MatchAllIterator ( R, O )
[...]
2. If ? IsRegExp(R) is true, then
a. Let C be ? SpeciesConstructor(R, RegExp).

SpeciesConstructor ( O, defaultConstructor )
[...]
2. Let C be ? Get(O, "constructor").
features: [Symbol.matchAll, Symbol.species]
---*/

var regexp = /./;
regexp.constructor = {
get [Symbol.species]() {
throw new Test262Error();
}
};

assert.throws(Test262Error, function() {
regexp[Symbol.matchAll]('');
});
Loading