Skip to content

Commit

Permalink
Add tests for regexp-match-indices (#2309)
Browse files Browse the repository at this point in the history
* Add tests for regexp-match-indices

* Add tests for unicode matches based on presence of /u flag

* Added deepEqual helper, PR feedback
  • Loading branch information
rbuckton authored and leobalter committed Sep 18, 2019
1 parent c41a8ac commit 1056d8f
Show file tree
Hide file tree
Showing 25 changed files with 1,156 additions and 5 deletions.
4 changes: 4 additions & 0 deletions features.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ optional-chaining
# https://github.com/tc39/proposal-top-level-await
top-level-await

# RegExp Match Array Indices
# https://github.com/tc39/proposal-regexp-match-indices
regexp-match-indices

## Standard language features
#
# Language features that have been included in a published version of the
Expand Down
55 changes: 55 additions & 0 deletions harness/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ description: |
Collection of assertion functions used throughout test262
---*/

/// <reference lib="esnext" />
/// <reference path="./types.d.ts" />

function assert(mustBeTrue, message) {
if (mustBeTrue === true) {
return;
Expand Down Expand Up @@ -92,6 +95,58 @@ assert.throws = function (expectedErrorConstructor, func, message) {
$ERROR(message);
};

assert._formatValue = function(value, seen) {
switch (typeof value) {
case 'string':
return typeof JSON !== "undefined" ? JSON.stringify(value) : '"' + value + '"';
case 'number':
case 'boolean':
case 'symbol':
case 'bigint':
return value.toString();
case 'undefined':
return 'undefined';
case 'function':
return '[Function' + (value.name ? ': ' + value.name : '') + ']';
case 'object':
if (value === null) return 'null';
if (value instanceof Date) return 'Date "' + value.toISOString() + '"';
if (value instanceof RegExp) return value.toString();
if (!seen) {
seen = {
counter: 0,
map: new Map()
};
}

var usage = seen.map.get(value);
if (usage) {
usage.used = true;
return '[Ref: #' + usage.id + ']';
}

usage = { id: ++seen.counter, used: false };
seen.map.set(value, usage);

if (typeof Set !== "undefined" && value instanceof Set) {
return 'Set {' + Array.from(value).map(function (value) { return assert._formatValue(value, seen); }).join(', ') + '}' + (usage.used ? ' as #' + usage.id : '');
}
if (typeof Map !== "undefined" && value instanceof Map) {
return 'Map {' + Array.from(value).map(function (pair) { return assert._formatValue(pair[0], seen) + ' => ' + assert._formatValue(pair[1], seen) + '}'; }).join(', ') + '}' + (usage.used ? ' as #' + usage.id : '');
}
if (Array.isArray ? Array.isArray(value) : value instanceof Array) {
return '[' + value.map(function (value) { return assert._formatValue(value, seen); }).join(', ') + ']' + (usage.used ? ' as #' + usage.id : '');
}
var tag = Symbol.toStringTag in value ? value[Symbol.toStringTag] : 'Object';
if (tag === 'Object' && Object.getPrototypeOf(value) === null) {
tag = '[Object: null prototype]';
}
return (tag ? tag + ' ' : '') + '{ ' + Object.keys(value).map(function (key) { return key.toString() + ': ' + assert._formatValue(value[key], seen); }).join(', ') + ' }' + (usage.used ? ' as #' + usage.id : '');
default:
return typeof value;
}
};

assert._toString = function (value) {
try {
return String(value);
Expand Down
20 changes: 15 additions & 5 deletions harness/compareArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@ description: |
Compare the contents of two arrays
---*/

// @ts-check
/// <reference path="./assert.js" />

function isSameValue(a, b) {
if (a === 0 && b === 0) return 1 / a === 1 / b;
if (a !== a && b !== b) return true;

return a === b;
}

/**
* @template T
* @param {T[]} a
* @param {T[]} b
*/
function compareArray(a, b) {
if (b.length !== a.length) {
return false;
Expand All @@ -25,11 +33,13 @@ function compareArray(a, b) {
return true;
}

/**
* @template T
* @param {T[]} actual
* @param {T[]} expected
* @param {string} [message]
*/
assert.compareArray = function(actual, expected, message) {
function formatArray(array) {
return '[' + array.map(String).join(', ') + ']';
}

assert(compareArray(actual, expected),
'Expected ' + formatArray(actual) + ' and ' + formatArray(expected) + ' to have the same contents. ' + message);
'Expected ' + assert._formatValue(actual) + ' and ' + assert._formatValue(expected) + ' to have the same contents. ' + message);
};
Loading

0 comments on commit 1056d8f

Please sign in to comment.