Replace testArraysAreSame with assert.compareArray

This commit is contained in:
André Bargull 2017-12-21 12:08:10 -08:00 committed by Rick Waldron
parent e65c65c293
commit 123259733b
13 changed files with 34 additions and 70 deletions

View File

@ -1181,23 +1181,3 @@ function isCanonicalizedStructurallyValidTimeZoneName(timeZone) {
}
return zoneNamePattern.test(timeZone);
}
/**
* Verifies that the actual array matches the expected one in length, elements,
* and element order.
* @param {Array} expected the expected array.
* @param {Array} actual the actual array.
* @return {boolean} true if the test succeeds.
* @exception if the test fails.
*/
function testArraysAreSame(expected, actual) {
var i;
for (i = 0; i < Math.max(actual.length, expected.length); i++) {
if (actual[i] !== expected[i]) {
$ERROR("Result array element at index " + i + " should be \"" +
expected[i] + "\" but is \"" + actual[i] + "\".");
}
}
return true;
}

View File

@ -6,7 +6,7 @@
es5id: 10.1.2_a
description: Tests that Intl.Collator can be subclassed.
author: Norbert Lindenberg
includes: [testIntl.js]
includes: [testIntl.js, compareArray.js]
---*/
// get a collator and have it sort an array for comparison with the subclass
@ -25,4 +25,4 @@ class MyCollator extends Intl.Collator {
var collator = new MyCollator(locales);
a.sort(collator.compare);
testArraysAreSame(referenceSorted, a);
assert.compareArray(a, referenceSorted);

View File

@ -7,7 +7,7 @@ description: >
Tests that Intl.Collator does not accept Unicode locale extension
keys and values that are not allowed.
author: Norbert Lindenberg
includes: [testIntl.js]
includes: [testIntl.js, compareArray.js]
---*/
var testArray = [
@ -41,6 +41,6 @@ Object.getOwnPropertyNames(keyValues).forEach(function (key) {
var options = collator.resolvedOptions();
assert.sameValue(options.locale, defaultLocale, "Locale " + options.locale + " is affected by key " + key + "; value " + value + ".");
assert.sameValue(JSON.stringify(options), defaultOptionsJSON, "Resolved options " + JSON.stringify(options) + " are affected by key " + key + "; value " + value + ".");
testArraysAreSame(defaultSortedArray, testArray.sort(collator.compare));
assert.compareArray(testArray.sort(collator.compare), defaultSortedArray);
});
});

View File

@ -5,7 +5,7 @@
es5id: 10.3.2_1_c
description: Tests that compare function is bound to its Intl.Collator.
author: Norbert Lindenberg
includes: [testIntl.js]
includes: [testIntl.js, compareArray.js]
---*/
var strings = ["d", "O", "od", "oe", "of", "ö", "o\u0308", "X", "y", "Z", "Z.", "𠮷野家", "吉野家", "!A", "A", "b", "C"];
@ -24,12 +24,8 @@ locales.forEach(function (locales) {
referenceSorted.sort(function (a, b) { return collatorObj.compare(a, b); });
var sorted = strings;
sorted.sort(compareFunc);
try {
testArraysAreSame(referenceSorted, sorted);
} catch (e) {
e.message += " (Testing with locales " + locales + "; options " +
(options ? JSON.stringify(options) : options) + ".)";
throw e;
}
assert.compareArray(sorted, referenceSorted,
"(Testing with locales " + locales + "; options " +
(options ? JSON.stringify(options) : options) + ".)");
});
});

View File

@ -7,7 +7,7 @@ description: >
Tests that the compare function isn't entirely unreasonable. This
test is not normative.
author: Norbert Lindenberg
includes: [testIntl.js]
includes: [testIntl.js, compareArray.js]
---*/
// this test should be valid at least for the following locales
@ -18,5 +18,5 @@ locales.forEach(function (locale) {
var a = ["L", "X", "C", "k", "Z", "H", "d", "m", "w", "A", "i", "f", "y", "E", "N", "V", "g", "J", "b"];
a.sort(collator.compare);
var expected = ["A", "b", "C", "d", "E", "f", "g", "H", "i", "J", "k", "L", "m", "N", "V", "w", "X", "y", "Z"];
testArraysAreSame(expected, a);
assert.compareArray(a, expected);
});

View File

@ -8,7 +8,7 @@ description: >
Tests that the compare function supports phonebook sorting if it
says it does. This test is not normative.
author: Norbert Lindenberg
includes: [testIntl.js]
includes: [testIntl.js, compareArray.js]
---*/
// this test should be valid at least for the following locales
@ -18,5 +18,5 @@ if (locales.indexOf(collator.resolvedOptions().locale) !== -1) {
var a = ["A", "b", "Af", "Ab", "od", "off", "Ä", "ö"];
a.sort(collator.compare);
var expected = ["A", "Ab", "Ä", "Af", "b", "od", "ö", "off"];
testArraysAreSame(expected, a);
assert.compareArray(a, expected);
}

View File

@ -8,7 +8,7 @@ description: >
Tests that the compare function supports different sensitivity
settings. This test is not normative.
author: Norbert Lindenberg
includes: [testIntl.js]
includes: [testIntl.js, compareArray.js]
---*/
// this test should be valid at least for the following locales
@ -29,6 +29,6 @@ locales.forEach(function (locale) {
var matches = input.filter(function (v) {
return collator.compare(v, target) === 0;
});
testArraysAreSame(expected[sensitivity], matches);
assert.compareArray(matches, expected[sensitivity]);
});
});

View File

@ -7,7 +7,7 @@ description: >
Tests that Date.prototype.toLocaleString & Co. produces the same
results as Intl.DateTimeFormat.
author: Norbert Lindenberg
includes: [testIntl.js]
includes: [testIntl.js, compareArray.js]
---*/
var functions = {
@ -45,15 +45,11 @@ Object.getOwnPropertyNames(functions).forEach(function (p) {
}
var referenceDateTimeFormat = new Intl.DateTimeFormat(locales, constructorOptions);
var referenceFormatted = dates.map(referenceDateTimeFormat.format);
var formatted = dates.map(function (a) { return f.call(a, locales, options); });
try {
testArraysAreSame(referenceFormatted, formatted);
} catch (e) {
e.message += " (Testing with locales " + locales + "; options " +
(options ? JSON.stringify(options) : options) + ".)";
throw e;
}
assert.compareArray(formatted, referenceFormatted,
"(Testing with locales " + locales + "; options " +
(options ? JSON.stringify(options) : options) + ".)");
});
});
});

View File

@ -6,7 +6,7 @@
es5id: 12.1.2
description: Tests that Intl.DateTimeFormat can be subclassed.
author: Norbert Lindenberg
includes: [testIntl.js]
includes: [testIntl.js, compareArray.js]
---*/
// get a date-time format and have it format an array of dates for comparison with the subclass
@ -25,4 +25,4 @@ class MyDateTimeFormat extends Intl.DateTimeFormat {
var format = new MyDateTimeFormat(locales);
var actual = a.map(format.format);
testArraysAreSame(referenceFormatted, actual);
assert.compareArray(actual, referenceFormatted);

View File

@ -7,7 +7,7 @@ description: >
Tests that Number.prototype.toLocaleString produces the same
results as Intl.NumberFormat.
author: Norbert Lindenberg
includes: [testIntl.js]
includes: [testIntl.js, compareArray.js]
---*/
var numbers = [0, -0, 1, -1, 5.5, 123, -123, -123.45, 123.44501, 0.001234,
@ -29,14 +29,10 @@ locales.forEach(function (locales) {
options.forEach(function (options) {
var referenceNumberFormat = new Intl.NumberFormat(locales, options);
var referenceFormatted = numbers.map(referenceNumberFormat.format);
var formatted = numbers.map(function (a) { return a.toLocaleString(locales, options); });
try {
testArraysAreSame(referenceFormatted, formatted);
} catch (e) {
e.message += " (Testing with locales " + locales + "; options " +
(options ? JSON.stringify(options) : options) + ".)";
throw e;
}
assert.compareArray(formatted, referenceFormatted,
"(Testing with locales " + locales + "; options " +
(options ? JSON.stringify(options) : options) + ".)");
});
});

View File

@ -6,7 +6,7 @@
es5id: 11.1.2
description: Tests that Intl.NumberFormat can be subclassed.
author: Norbert Lindenberg
includes: [testIntl.js]
includes: [testIntl.js, compareArray.js]
---*/
// get a number format and have it format an array of numbers for comparison with the subclass
@ -25,4 +25,4 @@ class MyNumberFormat extends Intl.NumberFormat {
var format = new MyNumberFormat(locales);
var actual = a.map(format.format);
testArraysAreSame(referenceFormatted, actual);
assert.compareArray(actual, referenceFormatted);

View File

@ -5,7 +5,7 @@
esid: sec-intl-pluralrules-constructor
description: Tests that Intl.PluralRules can be subclassed.
author: Zibi Braniecki
includes: [testIntl.js]
includes: [testIntl.js, compareArray.js]
---*/
// get a plural-rules and have it format an array of dates for comparison with the subclass
@ -25,4 +25,4 @@ class MyPluralRules extends Intl.PluralRules {
var pr = new MyPluralRules(locales);
var actual = a.map(pr.select.bind(pr));
testArraysAreSame(referenceSelected, actual);
assert.compareArray(actual, referenceSelected);

View File

@ -7,7 +7,7 @@ description: >
Tests that localeCompare produces the same results as
Intl.Collator.
author: Norbert Lindenberg
includes: [testIntl.js]
includes: [testIntl.js, compareArray.js]
---*/
var strings = ["d", "O", "od", "oe", "of", "ö", "o\u0308", "X", "y", "Z", "Z.", "𠮷野家", "吉野家", "!A", "A", "b", "C"];
@ -22,13 +22,9 @@ locales.forEach(function (locales) {
options.forEach(function (options) {
var referenceCollator = new Intl.Collator(locales, options);
var referenceSorted = strings.slice().sort(referenceCollator.compare);
strings.sort(function (a, b) { return a.localeCompare(b, locales, options); });
try {
testArraysAreSame(referenceSorted, strings);
} catch (e) {
e.message += " (Testing with locales " + locales + "; options " + JSON.stringify(options) + ".)";
throw e;
}
assert.compareArray(strings, referenceSorted,
"(Testing with locales " + locales + "; options " + JSON.stringify(options) + ".)");
});
});