Skip to content

Commit

Permalink
Introduce and apply harness helpers assert.compareIterator() and matc…
Browse files Browse the repository at this point in the history
…hValidator (regExpUtils)
  • Loading branch information
peterwmwong committed Mar 20, 2018
1 parent 29906bb commit 74aac5b
Show file tree
Hide file tree
Showing 18 changed files with 135 additions and 224 deletions.
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
Expand Up @@ -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)
]);
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,28 @@ 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;
var callArgs;
var regexp = /\d/u;
regexp.constructor = {
[Symbol.species]: function(){
callCount++;callArgs
callCount++;
callArgs = arguments;
return /\w/g;
}
};
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)
]);
21 changes: 5 additions & 16 deletions test/built-ins/RegExp/prototype/Symbol.matchAll/string-tostring.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
22 changes: 5 additions & 17 deletions test/built-ins/RegExp/prototype/Symbol.matchAll/this-get-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,16 @@ 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/;
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)
]);
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
]);
Original file line number Diff line number Diff line change
Expand Up @@ -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/;
Expand All @@ -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)
]);
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,19 @@ info: |
TypeError exception.
6. Return ? RegExpBuiltinExec(R, S).
features: [Symbol.matchAll]
includes: [compareArray.js, compareIterator.js, regExpUtils.js]
---*/

function TestWithRegExpExec(exec) {
RegExp.prototype.exec = 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 74aac5b

Please sign in to comment.