Merge pull request #2311 from shvaikalesh/harness-compare-array

Improve harness/compareArray
This commit is contained in:
Leo Balter 2019-08-28 14:12:12 -03:00 committed by GitHub
commit 2835648540
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 2 deletions

View File

@ -5,13 +5,20 @@ description: |
Compare the contents of two arrays
---*/
function isSameValue(a, b) {
if (a === 0 && b === 0) return 1 / a === 1 / b;
if (a !== a && b !== b) return true;
return a === b;
}
function compareArray(a, b) {
if (b.length !== a.length) {
return false;
}
for (var i = 0; i < a.length; i++) {
if (b[i] !== a[i]) {
if (!isSameValue(b[i], a[i])) {
return false;
}
}
@ -19,6 +26,10 @@ function compareArray(a, b) {
}
assert.compareArray = function(actual, expected, message) {
function formatArray(array) {
return '[' + array.map(String).join(', ') + ']';
}
assert(compareArray(actual, expected),
'Expected [' + actual.join(', ') + '] and [' + expected.join(', ') + '] to have the same contents. ' + message);
'Expected ' + formatArray(actual) + ' and ' + formatArray(expected) + ' to have the same contents. ' + message);
};

View File

@ -0,0 +1,11 @@
// Copyright (C) 2019 Alexey Shvayka. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
compareArray uses SameValue for value comparison.
includes: [compareArray.js]
---*/
assert(compareArray([NaN], [NaN]));
assert(!compareArray([0], [-0]));

View File

@ -0,0 +1,23 @@
// Copyright (C) 2019 Alexey Shvayka. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
compareArray correctly formats Symbols in error message.
includes: [compareArray.js]
features: [Symbol]
---*/
var threw = false;
try {
assert.compareArray([Symbol()], [Symbol('desc')]);
} catch (err) {
threw = true;
assert.sameValue(err.constructor, Test262Error);
assert(err.message.indexOf('[Symbol()]') !== -1);
assert(err.message.indexOf('[Symbol(desc)]') !== -1);
}
assert(threw);