mirror of
https://github.com/tc39/test262.git
synced 2025-07-26 15:34:29 +02:00
Cleanup compareArray and deepEqual assertions, remove assert._formatValue
Fixes #2372
This commit is contained in:
parent
4182972a49
commit
6b66b82d88
@ -94,58 +94,6 @@ assert.throws = function (expectedErrorConstructor, func, message) {
|
|||||||
$ERROR(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) {
|
assert._toString = function (value) {
|
||||||
try {
|
try {
|
||||||
return String(value);
|
return String(value);
|
||||||
|
@ -6,40 +6,34 @@ description: |
|
|||||||
defines: [compareArray]
|
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) {
|
function compareArray(a, b) {
|
||||||
if (b.length !== a.length) {
|
if (b.length !== a.length) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var i = 0; i < a.length; i++) {
|
for (var i = 0; i < a.length; i++) {
|
||||||
if (!isSameValue(b[i], a[i])) {
|
if (!compareArray.isSameValue(b[i], a[i])) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
compareArray.isSameValue = function(a, b) {
|
||||||
* @template T
|
if (a === 0 && b === 0) return 1 / a === 1 / b;
|
||||||
* @param {T[]} actual
|
if (a !== a && b !== b) return true;
|
||||||
* @param {T[]} expected
|
|
||||||
* @param {string} [message]
|
return a === b;
|
||||||
*/
|
};
|
||||||
assert.compareArray = function(actual, expected, message) {
|
|
||||||
assert(compareArray(actual, expected),
|
compareArray.format = function(array) {
|
||||||
'Expected ' + assert._formatValue(actual) + ' and ' + assert._formatValue(expected) + ' to have the same contents. ' + message);
|
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 || '')}`
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
@ -6,7 +6,67 @@ description: >
|
|||||||
defines: [assert.deepEqual]
|
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 EQUAL = 1;
|
||||||
var NOT_EQUAL = -1;
|
var NOT_EQUAL = -1;
|
||||||
var UNKNOWN = 0;
|
var UNKNOWN = 0;
|
||||||
@ -250,7 +310,6 @@ var deepEqual = (function () {
|
|||||||
|
|
||||||
function getCache(cache, left, right) {
|
function getCache(cache, left, right) {
|
||||||
var otherCache;
|
var otherCache;
|
||||||
/** @type {EQUAL | NOT_EQUAL | UNKNOWN | undefined} */
|
|
||||||
var result;
|
var result;
|
||||||
|
|
||||||
otherCache = cache.get(left);
|
otherCache = cache.get(left);
|
||||||
@ -266,8 +325,3 @@ var deepEqual = (function () {
|
|||||||
|
|
||||||
return deepEqual;
|
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 || ''));
|
|
||||||
};
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user