diff --git a/harness/compareArray.js b/harness/compareArray.js index adf294b5e8..f5d3169d62 100644 --- a/harness/compareArray.js +++ b/harness/compareArray.js @@ -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}`); + } }; diff --git a/test/harness/compare-array-arguments.js b/test/harness/compare-array-arguments.js new file mode 100644 index 0000000000..597efa0564 --- /dev/null +++ b/test/harness/compare-array-arguments.js @@ -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); diff --git a/test/harness/compare-array-arraylike.js b/test/harness/compare-array-arraylike.js new file mode 100644 index 0000000000..8318e5ff51 --- /dev/null +++ b/test/harness/compare-array-arraylike.js @@ -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'); diff --git a/test/harness/compare-array-different-elements.js b/test/harness/compare-array-different-elements.js index 7519d96a9d..ef72d10d4a 100644 --- a/test/harness/compare-array-different-elements.js +++ b/test/harness/compare-array-different-elements.js @@ -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.'); diff --git a/test/harness/compare-array-different-length.js b/test/harness/compare-array-different-length.js index a188fc2628..05debda822 100644 --- a/test/harness/compare-array-different-length.js +++ b/test/harness/compare-array-different-length.js @@ -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.'); diff --git a/test/harness/compare-array-empty.js b/test/harness/compare-array-empty.js index af701fd418..8a441f9bd0 100644 --- a/test/harness/compare-array-empty.js +++ b/test/harness/compare-array-empty.js @@ -7,6 +7,4 @@ description: > includes: [compareArray.js] ---*/ -if (compareArray([], []) !== true) { - throw new Error('Empty arrays are equivalent.'); -} +assert.compareArray([], [], 'Empty arrays are equivalent.'); diff --git a/test/harness/compare-array-same-elements-different-order.js b/test/harness/compare-array-same-elements-different-order.js index aa384c7cb7..369a32c456 100644 --- a/test/harness/compare-array-same-elements-different-order.js +++ b/test/harness/compare-array-same-elements-different-order.js @@ -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.'); diff --git a/test/harness/compare-array-same-elements-same-order.js b/test/harness/compare-array-same-elements-same-order.js index e363528ff7..a7fbc67807 100644 --- a/test/harness/compare-array-same-elements-same-order.js +++ b/test/harness/compare-array-same-elements-same-order.js @@ -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.'); diff --git a/test/harness/compare-array-samevalue.js b/test/harness/compare-array-samevalue.js index 826840e219..440dca3756 100644 --- a/test/harness/compare-array-samevalue.js +++ b/test/harness/compare-array-samevalue.js @@ -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]); +}); diff --git a/test/harness/compare-array-sparse.js b/test/harness/compare-array-sparse.js index ce2f4ae4f2..448a00e11a 100644 --- a/test/harness/compare-array-sparse.js +++ b/test/harness/compare-array-sparse.js @@ -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.'); diff --git a/test/harness/compare-array-symbol.js b/test/harness/compare-array-symbol.js index bbcc8177f7..ca5369655f 100644 --- a/test/harness/compare-array-symbol.js +++ b/test/harness/compare-array-symbol.js @@ -11,7 +11,7 @@ features: [Symbol] var threw = false; try { - assert.compareArray([Symbol()], [Symbol('desc')]); + assert.compareArray([Symbol()], [Symbol('desc')]); } catch (err) { threw = true;