fix: corrections to assert.compareArray and assert.compareArray.format (#3226)

This commit is contained in:
Rick Waldron 2021-10-01 12:52:15 -04:00 committed by GitHub
parent d9ddf80479
commit b3158bce51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 114 additions and 45 deletions

View File

@ -26,8 +26,8 @@ compareArray.isSameValue = function(a, b) {
return a === b; return a === b;
}; };
compareArray.format = function(array) { compareArray.format = function(arrayLike) {
return `[${array.map(String).join(', ')}]`; return `[${[].map.call(arrayLike, String).join(', ')}]`;
}; };
assert.compareArray = function(actual, expected, message) { assert.compareArray = function(actual, expected, message) {
@ -35,8 +35,11 @@ assert.compareArray = function(actual, expected, message) {
assert(actual != null, `First argument shouldn't be nullish. ${message}`); assert(actual != null, `First argument shouldn't be nullish. ${message}`);
assert(expected != null, `Second argument shouldn't be nullish. ${message}`); assert(expected != null, `Second argument shouldn't be nullish. ${message}`);
var format = compareArray.format; var format = compareArray.format;
assert( var result = compareArray(actual, expected);
compareArray(actual, expected),
`Expected ${format(actual)} and ${format(expected)} to have the same contents. ${message}` // The following prevents actual and expected from being iterated and evaluated
); // more than once unless absolutely necessary.
if (!result) {
assert(false, `Expected ${format(actual)} and ${format(expected)} to have the same contents. ${message}`);
}
}; };

View File

@ -0,0 +1,38 @@
// Copyright (C) 2021 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Accepts spreadable arguments
includes: [compareArray.js]
---*/
const fixture = [0, 'a', undefined];
function checkFormatOfAssertionMessage(func, errorMessage) {
var caught = false;
try {
func();
} catch (error) {
caught = true;
assert.sameValue(error.constructor, Test262Error);
assert.sameValue(error.message, errorMessage);
}
assert(caught, `Expected ${func} to throw, but it didn't.`);
}
function f() {
assert.compareArray(arguments, fixture);
assert.compareArray(fixture, arguments);
checkFormatOfAssertionMessage(() => {
assert.compareArray(arguments, [], 'arguments and []');
}, 'Expected [0, a, undefined] and [] to have the same contents. arguments and []');
checkFormatOfAssertionMessage(() => {
assert.compareArray([], arguments, '[] and arguments');
}, 'Expected [] and [0, a, undefined] to have the same contents. [] and arguments');
}
f(...fixture);

View File

@ -0,0 +1,34 @@
// Copyright (C) 2021 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Accepts spreadable arguments
includes: [compareArray.js]
---*/
function checkFormatOfAssertionMessage(func, errorMessage) {
var caught = false;
try {
func();
} catch (error) {
caught = true;
assert.sameValue(error.constructor, Test262Error);
assert.sameValue(error.message, errorMessage);
}
assert(caught, `Expected ${func} to throw, but it didn't.`);
}
const fixture = { length: 3, 0: 0, 1: 'a', 2: undefined};
assert.compareArray(fixture, [0, 'a', undefined]);
assert.compareArray([0, 'a', undefined], fixture);
checkFormatOfAssertionMessage(() => {
assert.compareArray(fixture, [], 'fixture and []');
}, 'Expected [0, a, undefined] and [] to have the same contents. fixture and []');
checkFormatOfAssertionMessage(() => {
assert.compareArray([], fixture, '[] and fixture');
}, 'Expected [] and [0, a, undefined] to have the same contents. [] and fixture');

View File

@ -10,6 +10,6 @@ includes: [compareArray.js]
var first = [0, 'a', undefined]; var first = [0, 'a', undefined];
var second = [0, 'b', undefined]; var second = [0, 'b', undefined];
if (compareArray(first, second) !== false) { assert.throws(Test262Error, () => {
throw new Error('Arrays containing different elements are not equivalent.'); assert.compareArray(first, second);
} }, 'Arrays containing different elements are not equivalent.');

View File

@ -7,10 +7,11 @@ description: >
includes: [compareArray.js] includes: [compareArray.js]
---*/ ---*/
if (compareArray([], [undefined]) !== false) {
throw new Error('Arrays of differing lengths are not equivalent.');
}
if (compareArray([undefined], []) !== false) { assert.throws(Test262Error, () => {
throw new Error('Arrays of differing lengths are not equivalent.'); assert.compareArray([], [undefined]);
} }, 'Arrays of differing lengths are not equivalent.');
assert.throws(Test262Error, () => {
assert.compareArray([undefined], []);
}, 'Arrays of differing lengths are not equivalent.');

View File

@ -7,6 +7,4 @@ description: >
includes: [compareArray.js] includes: [compareArray.js]
---*/ ---*/
if (compareArray([], []) !== true) { assert.compareArray([], [], 'Empty arrays are equivalent.');
throw new Error('Empty arrays are equivalent.');
}

View File

@ -11,6 +11,6 @@ var obj = {};
var first = [0, 1, '', 's', null, undefined, obj]; var first = [0, 1, '', 's', null, undefined, obj];
var second = [0, 1, '', 's', undefined, null, obj]; var second = [0, 1, '', 's', undefined, null, obj];
if (compareArray(first, second) !== false) { assert.throws(Test262Error, () => {
throw new Error('Arrays containing the same elements in different order are not equivalent.'); assert.compareArray(first, second);
} }, 'Arrays containing the same elements in different order are not equivalent.');

View File

@ -11,6 +11,4 @@ var obj = {};
var first = [0, 1, '', 's', undefined, null, obj]; var first = [0, 1, '', 's', undefined, null, obj];
var second = [0, 1, '', 's', undefined, null, obj]; var second = [0, 1, '', 's', undefined, null, obj];
if (compareArray(first, second) !== true) { assert.compareArray(first, second, 'Arrays containing the same elements in the same order are equivalent.');
throw new Error('Arrays containing the same elements in the same order are equivalent.');
}

View File

@ -7,5 +7,7 @@ description: >
includes: [compareArray.js] includes: [compareArray.js]
---*/ ---*/
assert(compareArray([NaN], [NaN])); assert.compareArray([NaN], [NaN]);
assert(!compareArray([0], [-0])); assert.throws(Test262Error, () => {
assert.compareArray([0], [-0]);
});

View File

@ -7,22 +7,17 @@ description: >
includes: [compareArray.js] includes: [compareArray.js]
---*/ ---*/
if (compareArray([,], [,]) !== true) {
throw new Error('Sparse arrays of the same length are equivalent.');
}
if (compareArray([,], [,,]) !== false) { assert.compareArray([,], [,], 'Sparse arrays of the same length are equivalent.');
throw new Error('Sparse arrays of differing lengths are not equivalent.'); assert.throws(Test262Error, () => {
} assert.compareArray([,], [,,]);
}, 'Sparse arrays of differing lengths are not equivalent.');
if (compareArray([,,], [,]) !== false) { assert.throws(Test262Error, () => {
throw new Error('Sparse arrays of differing lengths are not equivalent.'); assert.compareArray([,,], [,]);
} }, 'Sparse arrays of differing lengths are not equivalent.');
assert.throws(Test262Error, () => {
if (compareArray([,], []) !== false) { assert.compareArray([,], []);
throw new Error('Sparse arrays are not equivalent to empty arrays.'); }, 'Sparse arrays are not equivalent to empty arrays.');
} assert.throws(Test262Error, () => {
assert.compareArray([], [,]);
if (compareArray([], [,]) !== false) { }, 'Sparse arrays are not equivalent to empty arrays.');
throw new Error('Sparse arrays are not equivalent to empty arrays.');
}

View File

@ -11,7 +11,7 @@ features: [Symbol]
var threw = false; var threw = false;
try { try {
assert.compareArray([Symbol()], [Symbol('desc')]); assert.compareArray([Symbol()], [Symbol('desc')]);
} catch (err) { } catch (err) {
threw = true; threw = true;