Cleanup compareArray and deepEqual assertions, remove assert._formatValue

Fixes #2372
This commit is contained in:
Leo Balter 2019-10-09 10:57:20 -04:00 committed by Rick Waldron
parent 4182972a49
commit 6b66b82d88
3 changed files with 336 additions and 340 deletions

View File

@ -94,58 +94,6 @@ assert.throws = function (expectedErrorConstructor, func, message) {
$ERROR(message);
};
assert._formatValue = (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()
};
}
let 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(value => assert._formatValue(value, seen)).join(', ')}}${usage.used ? ` as #${usage.id}` : ''}`;
}
if (typeof Map !== "undefined" && value instanceof Map) {
return `Map {${Array.from(value).map(pair => `${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(value => assert._formatValue(value, seen)).join(', ')}]${usage.used ? ` as #${usage.id}` : ''}`;
}
let 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(key => `${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);

View File

@ -6,40 +6,34 @@ description: |
defines: [compareArray]
---*/
// @ts-check
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;
}
for (var i = 0; i < a.length; i++) {
if (!isSameValue(b[i], a[i])) {
if (!compareArray.isSameValue(b[i], a[i])) {
return false;
}
}
return true;
}
/**
* @template T
* @param {T[]} actual
* @param {T[]} expected
* @param {string} [message]
*/
assert.compareArray = function(actual, expected, message) {
assert(compareArray(actual, expected),
'Expected ' + assert._formatValue(actual) + ' and ' + assert._formatValue(expected) + ' to have the same contents. ' + message);
compareArray.isSameValue = function(a, b) {
if (a === 0 && b === 0) return 1 / a === 1 / b;
if (a !== a && b !== b) return true;
return a === b;
};
compareArray.format = function(array) {
return `[${array.map(String).join(', ')}]`;
};
assert.compareArray = function(actual, expected, message) {
var format = compareArray.format;
assert(
compareArray(actual, expected),
`Expected ${format(actual)} and ${format(expected)} to have the same contents. ${(message || '')}`
);
};

View File

@ -6,7 +6,67 @@ description: >
defines: [assert.deepEqual]
---*/
var deepEqual = (function () {
assert.deepEqual = function(actual, expected, message) {
var format = assert.deepEqual.format;
assert(
assert.deepEqual._compare(actual, expected),
`Expected ${format(actual)} to be structurally equal to ${format(expected)}. ${(message || '')}`
);
};
assert.deepEqual.format = 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()
};
}
let 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(value => assert.deepEqual.format(value, seen)).join(', ')}}${usage.used ? ` as #${usage.id}` : ''}`;
}
if (typeof Map !== "undefined" && value instanceof Map) {
return `Map {${Array.from(value).map(pair => `${assert.deepEqual.format(pair[0], seen)} => ${assert.deepEqual.format(pair[1], seen)}}`).join(', ')}}${usage.used ? ` as #${usage.id}` : ''}`;
}
if (Array.isArray ? Array.isArray(value) : value instanceof Array) {
return `[${value.map(value => assert.deepEqual.format(value, seen)).join(', ')}]${usage.used ? ` as #${usage.id}` : ''}`;
}
let 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(key => `${key.toString()}: ${assert.deepEqual.format(value[key], seen)}`).join(', ')} }${usage.used ? ` as #${usage.id}` : ''}`;
default:
return typeof value;
}
};
assert.deepEqual._compare = (function () {
var EQUAL = 1;
var NOT_EQUAL = -1;
var UNKNOWN = 0;
@ -250,7 +310,6 @@ var deepEqual = (function () {
function getCache(cache, left, right) {
var otherCache;
/** @type {EQUAL | NOT_EQUAL | UNKNOWN | undefined} */
var result;
otherCache = cache.get(left);
@ -266,8 +325,3 @@ var deepEqual = (function () {
return deepEqual;
})();
assert.deepEqual = function (actual, expected, message) {
assert(deepEqual(actual, expected),
'Expected ' + assert._formatValue(actual) + ' to be structurally equal to ' + assert._formatValue(expected) + '. ' + (message || ''));
};