Skip to content

Commit

Permalink
Added deepEqual helper, PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuckton committed Aug 30, 2019
1 parent 9dc5f7a commit 0b040c7
Show file tree
Hide file tree
Showing 24 changed files with 781 additions and 171 deletions.
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 @@ -91,3 +94,55 @@ assert.throws = function (expectedErrorConstructor, func, message) {
message += 'Expected a ' + expectedErrorConstructor.name + ' to be thrown but no exception was thrown at all';
$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;
}
};
39 changes: 13 additions & 26 deletions harness/compareArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,34 @@ description: |
Compare the contents of two arrays
---*/

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

/**
* @template T
* @param {T[]} a
* @param {T[]} b
* @param {(a: T, b: T) => boolean} [equaler]
*/
function compareArray(a, b, equaler) {
if (b === undefined) return a === undefined;
if (a === undefined) return false;
function compareArray(a, b) {
if (b.length !== a.length) {
return false;
}
if (typeof equaler !== "function") {
equaler = undefined;
}

for (var i = 0; i < a.length; i++) {
if (equaler ? !equaler(b[i], a[i]) : b[i] !== a[i]) {
if (b[i] !== a[i]) {
return false;
}
}
return true;
}

/**
* @type {{
* <T>(actual: T[], expected: T[], equaler?: (a: T, b: T) => boolean, message?: string): void;
* <T>(actual: T[], expected: T[], message?: string): void;
* }}
* @template T
* @param {T[]} actual
* @param {T[]} expected
* @param {string} [message]
*/
assert.compareArray = function(actual, expected, equaler, message) {
if (typeof equaler === "string") message = equaler, equaler = undefined;
if (expected === undefined) {
assert(actual === undefined, 'Expected ' + formatArray(actual) + ' to be undefined. ' + (message || ''));
return;
}

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

function formatArray(array) {
return array ? '[' + array.join(', ') + ']' : 'undefined';
}
assert.compareArray = function(actual, expected, message) {
assert(compareArray(actual, expected),
'Expected ' + assert._formatValue(actual) + ' and ' + assert._formatValue(expected) + ' to have the same contents. ' + (message || ''));
};

Loading

0 comments on commit 0b040c7

Please sign in to comment.