mirror of https://github.com/tc39/test262.git
fix: corrections to assert.compareArray and assert.compareArray.format (#3226)
This commit is contained in:
parent
d9ddf80479
commit
b3158bce51
|
@ -26,8 +26,8 @@ compareArray.isSameValue = function(a, b) {
|
|||
return a === b;
|
||||
};
|
||||
|
||||
compareArray.format = function(array) {
|
||||
return `[${array.map(String).join(', ')}]`;
|
||||
compareArray.format = function(arrayLike) {
|
||||
return `[${[].map.call(arrayLike, String).join(', ')}]`;
|
||||
};
|
||||
|
||||
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(expected != null, `Second argument shouldn't be nullish. ${message}`);
|
||||
var format = compareArray.format;
|
||||
assert(
|
||||
compareArray(actual, expected),
|
||||
`Expected ${format(actual)} and ${format(expected)} to have the same contents. ${message}`
|
||||
);
|
||||
var result = compareArray(actual, expected);
|
||||
|
||||
// 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}`);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -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);
|
|
@ -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');
|
|
@ -10,6 +10,6 @@ includes: [compareArray.js]
|
|||
var first = [0, 'a', undefined];
|
||||
var second = [0, 'b', undefined];
|
||||
|
||||
if (compareArray(first, second) !== false) {
|
||||
throw new Error('Arrays containing different elements are not equivalent.');
|
||||
}
|
||||
assert.throws(Test262Error, () => {
|
||||
assert.compareArray(first, second);
|
||||
}, 'Arrays containing different elements are not equivalent.');
|
||||
|
|
|
@ -7,10 +7,11 @@ description: >
|
|||
includes: [compareArray.js]
|
||||
---*/
|
||||
|
||||
if (compareArray([], [undefined]) !== false) {
|
||||
throw new Error('Arrays of differing lengths are not equivalent.');
|
||||
}
|
||||
|
||||
if (compareArray([undefined], []) !== false) {
|
||||
throw new Error('Arrays of differing lengths are not equivalent.');
|
||||
}
|
||||
assert.throws(Test262Error, () => {
|
||||
assert.compareArray([], [undefined]);
|
||||
}, 'Arrays of differing lengths are not equivalent.');
|
||||
|
||||
assert.throws(Test262Error, () => {
|
||||
assert.compareArray([undefined], []);
|
||||
}, 'Arrays of differing lengths are not equivalent.');
|
||||
|
|
|
@ -7,6 +7,4 @@ description: >
|
|||
includes: [compareArray.js]
|
||||
---*/
|
||||
|
||||
if (compareArray([], []) !== true) {
|
||||
throw new Error('Empty arrays are equivalent.');
|
||||
}
|
||||
assert.compareArray([], [], 'Empty arrays are equivalent.');
|
||||
|
|
|
@ -11,6 +11,6 @@ var obj = {};
|
|||
var first = [0, 1, '', 's', null, undefined, obj];
|
||||
var second = [0, 1, '', 's', undefined, null, obj];
|
||||
|
||||
if (compareArray(first, second) !== false) {
|
||||
throw new Error('Arrays containing the same elements in different order are not equivalent.');
|
||||
}
|
||||
assert.throws(Test262Error, () => {
|
||||
assert.compareArray(first, second);
|
||||
}, 'Arrays containing the same elements in different order are not equivalent.');
|
||||
|
|
|
@ -11,6 +11,4 @@ var obj = {};
|
|||
var first = [0, 1, '', 's', undefined, null, obj];
|
||||
var second = [0, 1, '', 's', undefined, null, obj];
|
||||
|
||||
if (compareArray(first, second) !== true) {
|
||||
throw new Error('Arrays containing the same elements in the same order are equivalent.');
|
||||
}
|
||||
assert.compareArray(first, second, 'Arrays containing the same elements in the same order are equivalent.');
|
||||
|
|
|
@ -7,5 +7,7 @@ description: >
|
|||
includes: [compareArray.js]
|
||||
---*/
|
||||
|
||||
assert(compareArray([NaN], [NaN]));
|
||||
assert(!compareArray([0], [-0]));
|
||||
assert.compareArray([NaN], [NaN]);
|
||||
assert.throws(Test262Error, () => {
|
||||
assert.compareArray([0], [-0]);
|
||||
});
|
||||
|
|
|
@ -7,22 +7,17 @@ description: >
|
|||
includes: [compareArray.js]
|
||||
---*/
|
||||
|
||||
if (compareArray([,], [,]) !== true) {
|
||||
throw new Error('Sparse arrays of the same length are equivalent.');
|
||||
}
|
||||
|
||||
if (compareArray([,], [,,]) !== false) {
|
||||
throw new Error('Sparse arrays of differing lengths are not equivalent.');
|
||||
}
|
||||
|
||||
if (compareArray([,,], [,]) !== false) {
|
||||
throw new Error('Sparse arrays of differing lengths are not equivalent.');
|
||||
}
|
||||
|
||||
if (compareArray([,], []) !== false) {
|
||||
throw new Error('Sparse arrays are not equivalent to empty arrays.');
|
||||
}
|
||||
|
||||
if (compareArray([], [,]) !== false) {
|
||||
throw new Error('Sparse arrays are not equivalent to empty arrays.');
|
||||
}
|
||||
assert.compareArray([,], [,], 'Sparse arrays of the same length are equivalent.');
|
||||
assert.throws(Test262Error, () => {
|
||||
assert.compareArray([,], [,,]);
|
||||
}, 'Sparse arrays of differing lengths are not equivalent.');
|
||||
assert.throws(Test262Error, () => {
|
||||
assert.compareArray([,,], [,]);
|
||||
}, 'Sparse arrays of differing lengths are not equivalent.');
|
||||
assert.throws(Test262Error, () => {
|
||||
assert.compareArray([,], []);
|
||||
}, 'Sparse arrays are not equivalent to empty arrays.');
|
||||
assert.throws(Test262Error, () => {
|
||||
assert.compareArray([], [,]);
|
||||
}, 'Sparse arrays are not equivalent to empty arrays.');
|
||||
|
|
|
@ -11,7 +11,7 @@ features: [Symbol]
|
|||
var threw = false;
|
||||
|
||||
try {
|
||||
assert.compareArray([Symbol()], [Symbol('desc')]);
|
||||
assert.compareArray([Symbol()], [Symbol('desc')]);
|
||||
} catch (err) {
|
||||
threw = true;
|
||||
|
||||
|
|
Loading…
Reference in New Issue