diff --git a/harness/compareIterator.js b/harness/compareIterator.js new file mode 100644 index 00000000000..69b9246e1b7 --- /dev/null +++ b/harness/compareIterator.js @@ -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); +} diff --git a/harness/regExpUtils.js b/harness/regExpUtils.js index bdb956427ac..4dd7ddf13b0 100644 --- a/harness/regExpUtils.js +++ b/harness/regExpUtils.js @@ -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'); + } +} diff --git a/test/built-ins/RegExp/prototype/Symbol.matchAll/species-constructor-is-undefined.js b/test/built-ins/RegExp/prototype/Symbol.matchAll/species-constructor-is-undefined.js index 5aae5d1f6e8..792bb1e7f8c 100644 --- a/test/built-ins/RegExp/prototype/Symbol.matchAll/species-constructor-is-undefined.js +++ b/test/built-ins/RegExp/prototype/Symbol.matchAll/species-constructor-is-undefined.js @@ -18,26 +18,14 @@ info: | 2. Let C be ? Get(O, "constructor"). 3. If C is undefined, return defaultConstructor. features: [Symbol.matchAll] -includes: [compareArray.js] +includes: [compareArray.js, compareIterator.js, regExpUtils.js] ---*/ var regexp = /\w/g; regexp.constructor = undefined; var str = 'a*b'; -var iter = regexp[Symbol.matchAll](str); -var { value, done } = iter.next(); -assert.compareArray(value, ['a']); -assert.sameValue(value.index, 0); -assert.sameValue(value.input, str); -assert(!done); - -({ value, done } = iter.next()); -assert.compareArray(value, ['b']); -assert.sameValue(value.index, 2); -assert.sameValue(value.input, str); -assert(!done); - -({ value, done } = iter.next()); -assert.sameValue(value, undefined); -assert(done); +assert.compareIterator(regexp[Symbol.matchAll](str), [ + matchValidator(['a'], 0, str), + matchValidator(['b'], 2, str) +]); diff --git a/test/built-ins/RegExp/prototype/Symbol.matchAll/species-constructor-species-is-null-or-undefined.js b/test/built-ins/RegExp/prototype/Symbol.matchAll/species-constructor-species-is-null-or-undefined.js index 97f08d4b195..564d8bb58b9 100644 --- a/test/built-ins/RegExp/prototype/Symbol.matchAll/species-constructor-species-is-null-or-undefined.js +++ b/test/built-ins/RegExp/prototype/Symbol.matchAll/species-constructor-species-is-null-or-undefined.js @@ -21,7 +21,7 @@ info: | 5. Let S be ? Get(C, @@species). 6. If S is either undefined or null, return defaultConstructor. features: [Symbol.matchAll, Symbol.species] -includes: [compareArray.js] +includes: [compareArray.js, compareIterator.js, regExpUtils.js] ---*/ function TestWithConstructor(ctor) { @@ -30,23 +30,11 @@ function TestWithConstructor(ctor) { [Symbol.species]: ctor }; var str = 'a*b'; - var iter = regexp[Symbol.matchAll](str); - var { value, done } = iter.next(); - assert.compareArray(value, ['a']); - assert.sameValue(value.index, 0); - assert.sameValue(value.input, str); - assert(!done); - - ({ value, done } = iter.next()); - assert.compareArray(value, ['b']); - assert.sameValue(value.index, 2); - assert.sameValue(value.input, str); - assert(!done); - - ({ value, done } = iter.next()); - assert.sameValue(value, undefined); - assert(done); + assert.compareIterator(regexp[Symbol.matchAll](str), [ + matchValidator(['a'], 0, str), + matchValidator(['b'], 2, str) + ]); } TestWithConstructor(undefined); diff --git a/test/built-ins/RegExp/prototype/Symbol.matchAll/species-constructor.js b/test/built-ins/RegExp/prototype/Symbol.matchAll/species-constructor.js index 0316c8daa9f..cb9e9bf03c2 100644 --- a/test/built-ins/RegExp/prototype/Symbol.matchAll/species-constructor.js +++ b/test/built-ins/RegExp/prototype/Symbol.matchAll/species-constructor.js @@ -15,7 +15,7 @@ info: | b. Let flags be ? ToString(? Get(R, "flags")) c. Let matcher be ? Construct(C, R, flags). features: [Symbol.matchAll, Symbol.species] -includes: [compareArray.js] +includes: [compareArray.js, compareIterator.js, regExpUtils.js] ---*/ var callCount = 0; @@ -23,7 +23,7 @@ var callArgs; var regexp = /\d/u; regexp.constructor = { [Symbol.species]: function(){ - callCount++;callArgs + callCount++; callArgs = arguments; return /\w/g; } @@ -31,23 +31,12 @@ regexp.constructor = { var str = 'a*b'; var iter = regexp[Symbol.matchAll](str); -assert.sameValue(callCount, 1);callArgs +assert.sameValue(callCount, 1); assert.sameValue(callArgs.length, 2); assert.sameValue(callArgs[0], regexp); assert.sameValue(callArgs[1], 'u'); -var { value, done } = iter.next(); -assert.compareArray(value, ['a']); -assert.sameValue(value.index, 0); -assert.sameValue(value.input, str); -assert(!done); - -({ value, done } = iter.next()); -assert.compareArray(value, ['b']); -assert.sameValue(value.index, 2); -assert.sameValue(value.input, str); -assert(!done); - -({ value, done } = iter.next()); -assert.sameValue(value, undefined); -assert(done); +assert.compareIterator(iter, [ + matchValidator(['a'], 0, str), + matchValidator(['b'], 2, str) +]); diff --git a/test/built-ins/RegExp/prototype/Symbol.matchAll/string-tostring.js b/test/built-ins/RegExp/prototype/Symbol.matchAll/string-tostring.js index ba53e3eef0e..bafeb125b65 100644 --- a/test/built-ins/RegExp/prototype/Symbol.matchAll/string-tostring.js +++ b/test/built-ins/RegExp/prototype/Symbol.matchAll/string-tostring.js @@ -11,7 +11,7 @@ info: | MatchAllIterator ( R, O ) 1. Let S be ? ToString(O). features: [Symbol.matchAll] -includes: [compareArray.js] +includes: [compareArray.js, compareIterator.js, regExpUtils.js] ---*/ var str = 'a*b'; @@ -21,20 +21,9 @@ var obj = { } }; var regexp = /\w/g; -var iter = regexp[Symbol.matchAll](obj); -var { value, done } = iter.next(); -assert.compareArray(value, ['a']); -assert.sameValue(value.index, 0); -assert.sameValue(value.input, str); -assert(!done); +assert.compareIterator(regexp[Symbol.matchAll](obj), [ + matchValidator(['a'], 0, str), + matchValidator(['b'], 2, str) +]); -({ value, done } = iter.next()); -assert.compareArray(value, ['b']); -assert.sameValue(value.index, 2); -assert.sameValue(value.input, str); -assert(!done); - -({ value, done } = iter.next()); -assert.sameValue(value, undefined); -assert(done); diff --git a/test/built-ins/RegExp/prototype/Symbol.matchAll/this-get-flags.js b/test/built-ins/RegExp/prototype/Symbol.matchAll/this-get-flags.js index 64f23b81051..eca4ffa4d4b 100644 --- a/test/built-ins/RegExp/prototype/Symbol.matchAll/this-get-flags.js +++ b/test/built-ins/RegExp/prototype/Symbol.matchAll/this-get-flags.js @@ -14,7 +14,7 @@ info: | [...] b. Let flags be ? ToString(? Get(R, "flags")) features: [Symbol.matchAll] -includes: [compareArray.js] +includes: [compareArray.js, compareIterator.js, regExpUtils.js] ---*/ var regexp = /\w/; @@ -22,20 +22,8 @@ Object.defineProperty(regexp, 'flags', { value: 'g' }); var str = 'a*b'; -var iter = regexp[Symbol.matchAll](str); -var { value, done } = iter.next(); -assert.compareArray(value, ['a']); -assert.sameValue(value.index, 0); -assert.sameValue(value.input, str); -assert(!done); - -({ value, done } = iter.next()); -assert.compareArray(value, ['b']); -assert.sameValue(value.index, 2); -assert.sameValue(value.input, str); -assert(!done); - -({ value, done } = iter.next()); -assert.sameValue(value, undefined); -assert(done); +assert.compareIterator(regexp[Symbol.matchAll](str), [ + matchValidator(['a'], 0, str), + matchValidator(['b'], 2, str) +]); diff --git a/test/built-ins/RegExp/prototype/Symbol.matchAll/this-lastindex-cached.js b/test/built-ins/RegExp/prototype/Symbol.matchAll/this-lastindex-cached.js index 66f93327d0a..ebefdb4e388 100644 --- a/test/built-ins/RegExp/prototype/Symbol.matchAll/this-lastindex-cached.js +++ b/test/built-ins/RegExp/prototype/Symbol.matchAll/this-lastindex-cached.js @@ -15,7 +15,7 @@ info: | f. Let lastIndex be ? ToLength(? Get(R, "lastIndex")). g. Perform ? Set(matcher, "lastIndex", lastIndex, true). features: [Symbol.matchAll] -includes: [compareArray.js] +includes: [compareArray.js, compareIterator.js, regExpUtils.js] ---*/ var regexp = /./g; @@ -30,18 +30,7 @@ var iter = regexp[Symbol.matchAll](str); // Verify lastIndex is cached at the time of calling @@matchAll regexp.lastIndex = 0; -var { value, done } = iter.next(); -assert.compareArray(value, ['c']); -assert.sameValue(value.index, 2); -assert.sameValue(value.input, str); -assert(!done); - -({ value, done } = iter.next()); -assert.compareArray(value, ['d']); -assert.sameValue(value.index, 3); -assert.sameValue(value.input, str); -assert(!done); - -({ value, done } = iter.next()); -assert.sameValue(value, undefined); -assert(done); +assert.compareIterator(iter, [ + matchValidator(['c'], 2, str), + matchValidator(['d'], 3, str) +]); diff --git a/test/built-ins/RegExp/prototype/Symbol.matchAll/this-tostring-flags.js b/test/built-ins/RegExp/prototype/Symbol.matchAll/this-tostring-flags.js index 672284d1a8a..5c7f9cb8159 100644 --- a/test/built-ins/RegExp/prototype/Symbol.matchAll/this-tostring-flags.js +++ b/test/built-ins/RegExp/prototype/Symbol.matchAll/this-tostring-flags.js @@ -14,7 +14,7 @@ info: | [...] b. Let flags be ? ToString(? Get(R, "flags")) features: [Symbol.matchAll] -includes: [compareArray.js] +includes: [compareArray.js, compareIterator.js, regExpUtils.js] ---*/ var regexp = /\w/; @@ -26,20 +26,8 @@ Object.defineProperty(regexp, 'flags', { } }); var str = 'a*b'; -var iter = regexp[Symbol.matchAll](str); -var { value, done } = iter.next(); -assert.compareArray(value, ['a']); -assert.sameValue(value.index, 0); -assert.sameValue(value.input, str); -assert(!done); - -({ value, done } = iter.next()); -assert.compareArray(value, ['b']); -assert.sameValue(value.index, 2); -assert.sameValue(value.input, str); -assert(!done); - -({ value, done } = iter.next()); -assert.sameValue(value, undefined); -assert(done); +assert.compareIterator(regexp[Symbol.matchAll](str), [ + matchValidator(['a'], 0, str), + matchValidator(['b'], 2, str) +]); diff --git a/test/built-ins/RegExpStringIteratorPrototype/next/custom-regexpexec-match-get-0-tostring.js b/test/built-ins/RegExpStringIteratorPrototype/next/custom-regexpexec-match-get-0-tostring.js index 4222586b9ce..33ac742ca2d 100644 --- a/test/built-ins/RegExpStringIteratorPrototype/next/custom-regexpexec-match-get-0-tostring.js +++ b/test/built-ins/RegExpStringIteratorPrototype/next/custom-regexpexec-match-get-0-tostring.js @@ -36,13 +36,13 @@ RegExp.prototype.exec = function () { return execResult; }; -var { value, done } = iter.next(); +var result = iter.next(); assert.sameValue(internalRegExp.lastIndex, 1); -assert.sameValue(value, execResult); -assert(!done); +assert.sameValue(result.value, execResult); +assert(!result.done); -({ value, done } = iter.next()); +result = iter.next(); assert.sameValue(internalRegExp.lastIndex, 2); -assert.sameValue(value, execResult); -assert(!done); +assert.sameValue(result.value, execResult); +assert(!result.done); diff --git a/test/built-ins/RegExpStringIteratorPrototype/next/custom-regexpexec-not-callable.js b/test/built-ins/RegExpStringIteratorPrototype/next/custom-regexpexec-not-callable.js index f349a641167..d659cd28d44 100644 --- a/test/built-ins/RegExpStringIteratorPrototype/next/custom-regexpexec-not-callable.js +++ b/test/built-ins/RegExpStringIteratorPrototype/next/custom-regexpexec-not-callable.js @@ -18,6 +18,7 @@ info: | TypeError exception. 6. Return ? RegExpBuiltinExec(R, S). features: [Symbol.matchAll] +includes: [compareArray.js, compareIterator.js, regExpUtils.js] ---*/ function TestWithRegExpExec(exec) { @@ -25,23 +26,11 @@ function TestWithRegExpExec(exec) { var regexp = /\w/g; var str = 'a*b'; - var iter = regexp[Symbol.matchAll](str); - var { value, done } = iter.next(); - assert.compareArray(value, ['a']); - assert.sameValue(value.index, 0); - assert.sameValue(value.input, str); - assert(!done); - - ({ value, done } = iter.next()); - assert.compareArray(value, ['b']); - assert.sameValue(value.index, 2); - assert.sameValue(value.input, str); - assert(!done); - - ({ value, done } = iter.next()); - assert.sameValue(value, undefined); - assert(done); + assert.compareIterator(regexp[Symbol.matchAll](str), [ + matchValidator(['a'], 0, str), + matchValidator(['b'], 2, str) + ]); } TestWithRegExpExec(undefined); diff --git a/test/built-ins/RegExpStringIteratorPrototype/next/custom-regexpexec.js b/test/built-ins/RegExpStringIteratorPrototype/next/custom-regexpexec.js index d3311030ce5..850a7a18c0e 100644 --- a/test/built-ins/RegExpStringIteratorPrototype/next/custom-regexpexec.js +++ b/test/built-ins/RegExpStringIteratorPrototype/next/custom-regexpexec.js @@ -38,17 +38,17 @@ function callNextWithExecReturnValue(returnValue) { } var firstExecReturnValue = ['ab']; -var { value, done } = callNextWithExecReturnValue(firstExecReturnValue); -assert.sameValue(value, firstExecReturnValue); -assert(!done); +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); -({ value, done } = callNextWithExecReturnValue(null)); -assert.sameValue(value, undefined); -assert(done); +result = callNextWithExecReturnValue(null); +assert.sameValue(result.value, undefined); +assert(result.done); assert.sameValue(callArgs.length, 1); assert.sameValue(callArgs[0], str); diff --git a/test/built-ins/RegExpStringIteratorPrototype/next/next-iteration-global.js b/test/built-ins/RegExpStringIteratorPrototype/next/next-iteration-global.js index 7d5c7349e23..d1b6726e585 100644 --- a/test/built-ins/RegExpStringIteratorPrototype/next/next-iteration-global.js +++ b/test/built-ins/RegExpStringIteratorPrototype/next/next-iteration-global.js @@ -22,29 +22,19 @@ info: | 3. Perform ? Set(R, "lastIndex", nextIndex, true). iii. Return ! CreateIterResultObject(match, false). features: [Symbol.matchAll] +includes: [compareArray.js, compareIterator.js, regExpUtils.js] ---*/ var regexp = /\w/g; var str = 'a*b'; var iter = regexp[Symbol.matchAll](str); -var { value, done } = iter.next(); -assert.compareArray(value, ['a']); -assert.sameValue(value.index, 0); -assert.sameValue(value.input, str); -assert(!done); - -({ value, done } = iter.next()); -assert.compareArray(value, ['b']); -assert.sameValue(value.index, 2); -assert.sameValue(value.input, str); -assert(!done); - -({ value, done } = iter.next()); -assert.sameValue(value, undefined); -assert(done); +assert.compareIterator(iter, [ + matchValidator(['a'], 0, str), + matchValidator(['b'], 2, str) +]); // Verifies %RegExpStringIteratorPrototype%.next() step 4 -({ value, done } = iter.next()); -assert.sameValue(value, undefined); -assert(done); +var result = iter.next(); +assert.sameValue(result.value, undefined); +assert(result.done); diff --git a/test/built-ins/RegExpStringIteratorPrototype/next/next-iteration.js b/test/built-ins/RegExpStringIteratorPrototype/next/next-iteration.js index 8b1679fe8b5..a6de2ad8a91 100644 --- a/test/built-ins/RegExpStringIteratorPrototype/next/next-iteration.js +++ b/test/built-ins/RegExpStringIteratorPrototype/next/next-iteration.js @@ -20,23 +20,18 @@ info: | i. Set O.[[Done]] to true. ii. Return ! CreateIterResultObject(match, false). features: [Symbol.matchAll] +includes: [compareArray.js, compareIterator.js, regExpUtils.js] ---*/ var regexp = /\w/; -var str = 'a*b'; +var str = '*a*b'; var iter = regexp[Symbol.matchAll](str); -var { value, done } = iter.next(); -assert.compareArray(value, ['a']); -assert.sameValue(value.index, 0); -assert.sameValue(value.input, str); -assert(!done); - -({ value, done } = iter.next()); -assert.sameValue(value, undefined); -assert(done); +assert.compareIterator(iter, [ + matchValidator(['a'], 1, str) +]); // Verifies %RegExpStringIteratorPrototype%.next() step 4 -({ value, done } = iter.next()); -assert.sameValue(value, undefined); -assert(done); +var result = iter.next(); +assert.sameValue(result.value, undefined); +assert(result.done); diff --git a/test/built-ins/String/prototype/matchAll/regexp-get-matchAll-throws.js b/test/built-ins/String/prototype/matchAll/regexp-get-matchAll-throws.js index cccba4e1c13..8c73480b05b 100644 --- a/test/built-ins/String/prototype/matchAll/regexp-get-matchAll-throws.js +++ b/test/built-ins/String/prototype/matchAll/regexp-get-matchAll-throws.js @@ -18,7 +18,7 @@ Object.defineProperty(regexp, Symbol.matchAll, { get() { throw new Test262Error(); } -});; +}); assert.throws(Test262Error, function() { ''.matchAll(regexp); diff --git a/test/built-ins/String/prototype/matchAll/regexp-is-null.js b/test/built-ins/String/prototype/matchAll/regexp-is-null.js index 0d30c7dc6be..9ae102e6b16 100644 --- a/test/built-ins/String/prototype/matchAll/regexp-is-null.js +++ b/test/built-ins/String/prototype/matchAll/regexp-is-null.js @@ -17,18 +17,11 @@ info: | 3. Else, a. Let R be RegExpCreate(regexp, "g"). features: [String.prototype.matchAll] -includes: [compareArray.js] +includes: [compareArray.js, compareIterator.js, regExpUtils.js] ---*/ -var str = '-null-' -var iter = str.matchAll(null); +var str = '-null-'; -var { value, done } = iter.next(); -assert.compareArray(value, ['null']); -assert.sameValue(value.index, 1); -assert.sameValue(value.input, '-null-'); -assert(!done); - -({ value, done } = iter.next()); -assert.sameValue(value, undefined); -assert(done); +assert.compareIterator(str.matchAll(null), [ + matchValidator(['null'], 1, str) +]); diff --git a/test/built-ins/String/prototype/matchAll/regexp-is-undefined.js b/test/built-ins/String/prototype/matchAll/regexp-is-undefined.js index 891499df468..bf9ac09e004 100644 --- a/test/built-ins/String/prototype/matchAll/regexp-is-undefined.js +++ b/test/built-ins/String/prototype/matchAll/regexp-is-undefined.js @@ -17,23 +17,12 @@ info: | 3. Else, a. Let R be RegExpCreate(regexp, "g"). features: [String.prototype.matchAll] -includes: [compareArray.js] +includes: [compareArray.js, compareIterator.js, regExpUtils.js] ---*/ var str = 'a'; -var iter = str.matchAll(undefined); -var { value, done } = iter.next(); -assert.compareArray(value, ['']); -assert.sameValue(value.index, 0); -assert.sameValue(value.input, str); -assert(!done); - -({ value, done } = iter.next()); -assert.compareArray(value, ['']); -assert.sameValue(value.index, 1); -assert.sameValue(value.input, str); - -({ value, done } = iter.next()); -assert.sameValue(value, undefined); -assert(done); +assert.compareIterator(str.matchAll(undefined), [ + matchValidator([''], 0, str), + matchValidator([''], 1, str) +]); diff --git a/test/built-ins/String/prototype/matchAll/regexp-prototype-has-no-matchAll.js b/test/built-ins/String/prototype/matchAll/regexp-prototype-has-no-matchAll.js index 9af5580a789..0aeb021a9e3 100644 --- a/test/built-ins/String/prototype/matchAll/regexp-prototype-has-no-matchAll.js +++ b/test/built-ins/String/prototype/matchAll/regexp-prototype-has-no-matchAll.js @@ -12,26 +12,13 @@ info: | [...] 3. Return ? MatchAllIterator(regexp, O). features: [Symbol.matchAll, String.prototype.matchAll] -includes: [compareArray.js] +includes: [compareArray.js, compareIterator.js, regExpUtils.js] ---*/ -var regexp = /\w/g; delete RegExp.prototype[Symbol.matchAll]; var str = 'a*b'; -var iter = str.matchAll(regexp); -var { value, done } = iter.next(); -assert.compareArray(value, ['a']); -assert.sameValue(value.index, 0); -assert.sameValue(value.input, str); -assert(!done); - -({ value, done } = iter.next()); -assert.compareArray(value, ['b']); -assert.sameValue(value.index, 2); -assert.sameValue(value.input, str); -assert(!done); - -({ value, done } = iter.next()); -assert.sameValue(value, undefined); -assert(done); +assert.compareIterator(str.matchAll(/\w/g), [ + matchValidator(['a'], 0, str), + matchValidator(['b'], 2, str) +]);