Use assert-lib: intl402/NumberFormat

This commit is contained in:
André Bargull 2017-05-04 21:14:46 +02:00
parent 315031c07d
commit 8bed4d442d
21 changed files with 56 additions and 197 deletions

View File

@ -19,16 +19,12 @@ testWithIntlConstructors(function (Constructor) {
// variant 1: use constructor in a "new" expression // variant 1: use constructor in a "new" expression
obj = new Constructor(); obj = new Constructor();
newObj = Intl.NumberFormat.call(obj); newObj = Intl.NumberFormat.call(obj);
if (obj === newObj) { assert.notSameValue(obj, newObj, "NumberFormat object created with \"new\" was not ignored as this-value.");
$ERROR("NumberFormat object created with \"new\" was not ignored as this-value.");
}
// variant 2: use constructor as a function // variant 2: use constructor as a function
obj = Constructor(); obj = Constructor();
newObj = Intl.NumberFormat.call(obj); newObj = Intl.NumberFormat.call(obj);
if (obj === newObj) { assert.notSameValue(obj, newObj, "NumberFormat object created with constructor as function was not ignored as this-value.");
$ERROR("NumberFormat object created with constructor as function was not ignored as this-value.");
}
return true; return true;
}); });

View File

@ -19,63 +19,37 @@ validValues.forEach(function (value) {
format = new Intl.NumberFormat([defaultLocale], {style: "currency", currency: value}); format = new Intl.NumberFormat([defaultLocale], {style: "currency", currency: value});
actual = format.resolvedOptions().currency; actual = format.resolvedOptions().currency;
expected = value.toString().toUpperCase(); expected = value.toString().toUpperCase();
if (actual !== expected) { assert.sameValue(actual, expected, "Incorrect resolved currency with currency style.");
$ERROR("Incorrect resolved currency with currency style - expected " +
expected + "; got " + actual + ".");
}
// without currency style, we shouldn't get any currency back // without currency style, we shouldn't get any currency back
format = new Intl.NumberFormat([defaultLocale], {currency: value}); format = new Intl.NumberFormat([defaultLocale], {currency: value});
actual = format.resolvedOptions().currency; actual = format.resolvedOptions().currency;
expected = undefined; expected = undefined;
if (actual !== expected) { assert.sameValue(actual, expected, "Incorrect resolved currency with non-currency style.");
$ERROR("Incorrect resolved currency with non-currency style - expected " +
expected + "; got " + actual + ".");
}
// currencies specified through the locale must be ignored // currencies specified through the locale must be ignored
format = new Intl.NumberFormat([defaultLocale + "-u-cu-krw"], {style: "currency", currency: value}); format = new Intl.NumberFormat([defaultLocale + "-u-cu-krw"], {style: "currency", currency: value});
actual = format.resolvedOptions().currency; actual = format.resolvedOptions().currency;
expected = value.toString().toUpperCase(); expected = value.toString().toUpperCase();
if (actual !== expected) { assert.sameValue(actual, expected, "Incorrect resolved currency with -u-cu- and currency style.");
$ERROR("Incorrect resolved currency with -u-cu- and currency style - expected " +
expected + "; got " + actual + ".");
}
format = new Intl.NumberFormat([defaultLocale + "-u-cu-krw"], {currency: value}); format = new Intl.NumberFormat([defaultLocale + "-u-cu-krw"], {currency: value});
actual = format.resolvedOptions().currency; actual = format.resolvedOptions().currency;
expected = undefined; expected = undefined;
if (actual !== expected) { assert.sameValue(actual, expected, "Incorrect resolved currency with -u-cu- and non-currency style.");
$ERROR("Incorrect resolved currency with -u-cu- and non-currency style - expected " +
expected + "; got " + actual + ".");
}
}); });
invalidValues.forEach(function (value) { invalidValues.forEach(function (value) {
function expectError(f) { assert.throws(RangeError, function () {
var error;
try {
f();
} catch (e) {
error = e;
}
if (error === undefined) {
$ERROR("Invalid currency value " + value + " was not rejected.");
} else if (error.name !== "RangeError") {
$ERROR("Invalid currency value " + value + " was rejected with wrong error " + error.name + ".");
}
}
expectError(function () {
return new Intl.NumberFormat([defaultLocale], {style: "currency", currency: value}); return new Intl.NumberFormat([defaultLocale], {style: "currency", currency: value});
}); }, "Invalid currency value " + value + " was not rejected.");
expectError(function () { assert.throws(RangeError, function () {
return new Intl.NumberFormat([defaultLocale], {currency: value}); return new Intl.NumberFormat([defaultLocale], {currency: value});
}); }, "Invalid currency value " + value + " was not rejected.");
expectError(function () { assert.throws(RangeError, function () {
return new Intl.NumberFormat([defaultLocale + "-u-cu-krw"], {style: "currency", currency: value}); return new Intl.NumberFormat([defaultLocale + "-u-cu-krw"], {style: "currency", currency: value});
}); }, "Invalid currency value " + value + " was not rejected.");
expectError(function () { assert.throws(RangeError, function () {
return new Intl.NumberFormat([defaultLocale + "-u-cu-krw"], {currency: value}); return new Intl.NumberFormat([defaultLocale + "-u-cu-krw"], {currency: value});
}); }, "Invalid currency value " + value + " was not rejected.");
}); });

View File

@ -11,23 +11,10 @@ author: Norbert Lindenberg
var defaultLocale = new Intl.NumberFormat().resolvedOptions().locale; var defaultLocale = new Intl.NumberFormat().resolvedOptions().locale;
function expectError(f) { assert.throws(TypeError, function () {
var error;
try {
f();
} catch (e) {
error = e;
}
if (error === undefined) {
$ERROR("Invalid currency value " + value + " was not rejected.");
} else if (error.name !== "TypeError") {
$ERROR("Invalid currency value " + value + " was rejected with wrong error " + error.name + ".");
}
}
expectError(function () {
return new Intl.NumberFormat([defaultLocale], {style: "currency"}); return new Intl.NumberFormat([defaultLocale], {style: "currency"});
}); }, "Throws TypeError when currency code is not specified.");
expectError(function () {
assert.throws(TypeError, function () {
return new Intl.NumberFormat([defaultLocale + "-u-cu-krw"], {style: "currency"}); return new Intl.NumberFormat([defaultLocale + "-u-cu-krw"], {style: "currency"});
}); }, "Throws TypeError when currency code is not specified; Currenty code from Unicode locale extension sequence is ignored.");

View File

@ -183,12 +183,6 @@ Object.getOwnPropertyNames(currencyDigits).forEach(function (currency) {
var format = Intl.NumberFormat([], {style: "currency", currency: currency}); var format = Intl.NumberFormat([], {style: "currency", currency: currency});
var min = format.resolvedOptions().minimumFractionDigits; var min = format.resolvedOptions().minimumFractionDigits;
var max = format.resolvedOptions().maximumFractionDigits; var max = format.resolvedOptions().maximumFractionDigits;
if (min !== digits) { assert.sameValue(min, digits, "Didn't get correct minimumFractionDigits for currency " + currency + ".");
$ERROR("Didn't get correct minimumFractionDigits for currency " + assert.sameValue(max, digits, "Didn't get correct maximumFractionDigits for currency " + currency + ".");
currency + "; expected " + digits + ", got " + min + ".");
}
if (max !== digits) {
$ERROR("Didn't get correct maximumFractionDigits for currency " +
currency + "; expected " + digits + ", got " + max + ".");
}
}); });

View File

@ -13,6 +13,4 @@ includes: [testIntl.js]
taintProperties(["localeMatcher"]); taintProperties(["localeMatcher"]);
var locale = new Intl.NumberFormat(undefined, {localeMatcher: "lookup"}).resolvedOptions().locale; var locale = new Intl.NumberFormat(undefined, {localeMatcher: "lookup"}).resolvedOptions().locale;
if (!isCanonicalizedStructurallyValidLanguageTag(locale)) { assert(isCanonicalizedStructurallyValidLanguageTag(locale), "NumberFormat returns invalid locale " + locale + ".");
$ERROR("NumberFormat returns invalid locale " + locale + ".");
}

View File

@ -12,10 +12,6 @@ author: Norbert Lindenberg
var obj = new Intl.NumberFormat(); var obj = new Intl.NumberFormat();
var actualPrototype = Object.getPrototypeOf(obj); var actualPrototype = Object.getPrototypeOf(obj);
if (actualPrototype !== Intl.NumberFormat.prototype) { assert.sameValue(actualPrototype, Intl.NumberFormat.prototype, "Prototype of object constructed by Intl.NumberFormat isn't Intl.NumberFormat.prototype.");
$ERROR("Prototype of object constructed by Intl.NumberFormat isn't Intl.NumberFormat.prototype; got " + actualPrototype);
}
if (!Object.isExtensible(obj)) { assert(Object.isExtensible(obj), "Object constructed by Intl.NumberFormat must be extensible.");
$ERROR("Object constructed by Intl.NumberFormat must be extensible.");
}

View File

@ -29,18 +29,9 @@ locales.forEach(function (locale) {
keyValues[key].forEach(function (value) { keyValues[key].forEach(function (value) {
var numberFormat = new Intl.NumberFormat([locale + "-u-" + key + "-" + value]); var numberFormat = new Intl.NumberFormat([locale + "-u-" + key + "-" + value]);
var options = numberFormat.resolvedOptions(); var options = numberFormat.resolvedOptions();
if (options.locale !== defaultLocale) { assert.sameValue(options.locale, defaultLocale, "Locale " + options.locale + " is affected by key " + key + "; value " + value + ".");
$ERROR("Locale " + options.locale + " is affected by key " + assert.sameValue(JSON.stringify(options), defaultOptionsJSON, "Resolved options " + JSON.stringify(options) + " are affected by key " + key + "; value " + value + ".");
key + "; value " + value + "."); assert.sameValue(numberFormat.format(input), defaultFormatted, "Formatted value " + numberFormat.format(input) + " is affected by key " + key + "; value " + value + ".");
}
if (JSON.stringify(options) !== defaultOptionsJSON) {
$ERROR("Resolved options " + JSON.stringify(options) + " are affected by key " +
key + "; value " + value + ".");
}
if (defaultFormatted !== numberFormat.format(input)) {
$ERROR("Formatted value " + numberFormat.format(input) + " is affected by key " +
key + "; value " + value + ".");
}
}); });
}); });
}); });

View File

@ -12,6 +12,4 @@ author: Norbert Lindenberg
var obj = new Intl.NumberFormat(); var obj = new Intl.NumberFormat();
var toStringValue = Object.prototype.toString.call(obj); var toStringValue = Object.prototype.toString.call(obj);
if (toStringValue !== "[object Object]") { assert.sameValue(toStringValue, "[object Object]", "Intl.NumberFormat instance produces wrong [[Class]] - toString returns " + toStringValue + ".");
$ERROR("Intl.NumberFormat instance produces wrong [[Class]] - toString returns " + toStringValue + ".");
}

View File

@ -18,8 +18,5 @@ var wellFormedCurrencyCodes = [
wellFormedCurrencyCodes.forEach(function (code) { wellFormedCurrencyCodes.forEach(function (code) {
// this must not throw an exception for a valid currency code // this must not throw an exception for a valid currency code
var format = new Intl.NumberFormat(["de-de"], {style: "currency", currency: code}); var format = new Intl.NumberFormat(["de-de"], {style: "currency", currency: code});
if (format.resolvedOptions().currency !== code.toUpperCase()) { assert.sameValue(format.resolvedOptions().currency, code.toUpperCase(), "Currency " + code + " was not correctly accepted.");
$ERROR("Currency " + code + " was not correctly accepted; turned into " +
format.resolvedOptions().currency + ".");
}
}); });

View File

@ -20,16 +20,8 @@ var invalidCurrencyCodes = [
]; ];
invalidCurrencyCodes.forEach(function (code) { invalidCurrencyCodes.forEach(function (code) {
var error;
try {
// this must throw an exception for an invalid currency code // this must throw an exception for an invalid currency code
assert.throws(RangeError, function() {
var format = new Intl.NumberFormat(["de-de"], {style: "currency", currency: code}); var format = new Intl.NumberFormat(["de-de"], {style: "currency", currency: code});
} catch (e) { }, "Invalid currency code '" + code + "' was not rejected.");
error = e;
}
if (error === undefined) {
$ERROR("Invalid currency code '" + code + "' was not rejected.");
} else if (error.name !== "RangeError") {
$ERROR("Invalid currency code '" + code + "' was rejected with wrong error " + error.name + ".");
}
}); });

View File

@ -8,15 +8,7 @@ author: Norbert Lindenberg
---*/ ---*/
var desc = Object.getOwnPropertyDescriptor(Intl.NumberFormat, "prototype"); var desc = Object.getOwnPropertyDescriptor(Intl.NumberFormat, "prototype");
if (desc === undefined) { assert.notSameValue(desc, undefined, "Intl.NumberFormat.prototype is not defined.");
$ERROR("Intl.NumberFormat.prototype is not defined."); assert.sameValue(desc.writable, false, "Intl.NumberFormat.prototype must not be writable.");
} assert.sameValue(desc.enumerable, false, "Intl.NumberFormat.prototype must not be enumerable.");
if (desc.writable) { assert.sameValue(desc.configurable, false, "Intl.NumberFormat.prototype must not be configurable.");
$ERROR("Intl.NumberFormat.prototype must not be writable.");
}
if (desc.enumerable) {
$ERROR("Intl.NumberFormat.prototype must not be enumerable.");
}
if (desc.configurable) {
$ERROR("Intl.NumberFormat.prototype must not be configurable.");
}

View File

@ -11,7 +11,4 @@ author: Roozbeh Pournader
// test by calling a function that would fail if "this" were not an object // test by calling a function that would fail if "this" were not an object
// initialized as an Intl.NumberFormat // initialized as an Intl.NumberFormat
if (typeof Intl.NumberFormat.prototype.format(0) !== "string") { assert.sameValue(typeof Intl.NumberFormat.prototype.format(0), "string", "Intl.NumberFormat's prototype is not an object that has been initialized as an Intl.NumberFormat");
$ERROR("Intl.NumberFormat's prototype is not an object that has been " +
"initialized as an Intl.NumberFormat");
}

View File

@ -19,16 +19,8 @@ var invalidTargets = [undefined, null, true, 0, "NumberFormat", [], {}];
Object.getOwnPropertyNames(functions).forEach(function (functionName) { Object.getOwnPropertyNames(functions).forEach(function (functionName) {
var f = functions[functionName]; var f = functions[functionName];
invalidTargets.forEach(function (target) { invalidTargets.forEach(function (target) {
var error; assert.throws(TypeError, function() {
try {
f.call(target); f.call(target);
} catch (e) { }, "Calling " + functionName + " on " + target + " was not rejected.");
error = e;
}
if (error === undefined) {
$ERROR("Calling " + functionName + " on " + target + " was not rejected.");
} else if (error.name !== "TypeError") {
$ERROR("Calling " + functionName + " on " + target + " was rejected with wrong error " + error.name + ".");
}
}); });
}); });

View File

@ -9,7 +9,4 @@ description: >
author: Roozbeh Pournader author: Roozbeh Pournader
---*/ ---*/
if (Intl.NumberFormat.prototype.constructor !== Intl.NumberFormat) { assert.sameValue(Intl.NumberFormat.prototype.constructor, Intl.NumberFormat, "Intl.NumberFormat.prototype.constructor is not the same as Intl.NumberFormat");
$ERROR("Intl.NumberFormat.prototype.constructor is not the same as " +
"Intl.NumberFormat");
}

View File

@ -20,9 +20,5 @@ for (i in testData) {
correctResult = formatter.format(number); correctResult = formatter.format(number);
result = formatter.format(input); result = formatter.format(input);
if (result !== correctResult) { assert.sameValue(result, correctResult, 'Intl.NumberFormat does not convert other types to numbers. Input: "' + input + '".');
$ERROR('Intl.NumberFormat does not convert other ' +
'types to numbers. Input: "'+input+'" Output: "'+result+'" '+
'Expected output: "'+correctResult+'"');
}
} }

View File

@ -30,11 +30,7 @@ locales.forEach(function (locales) {
numbers.forEach(function (number) { numbers.forEach(function (number) {
var referenceFormatted = formatObj.format(number); var referenceFormatted = formatObj.format(number);
var formatted = formatFunc(number); var formatted = formatFunc(number);
if (referenceFormatted !== formatted) { assert.sameValue(referenceFormatted, formatted, "format function produces different result than format method for locales " + locales + "; options: " + (options ? JSON.stringify(options) : options) + ".");
$ERROR("format function produces different result than format method for locales " +
locales + "; options: " + (options ? JSON.stringify(options) : options) +
" : " + formatted + " vs. " + referenceFormatted + ".");
}
}); });
}); });
}); });

View File

@ -11,10 +11,6 @@ author: Roozbeh Pournader
var formatter = new Intl.NumberFormat(); var formatter = new Intl.NumberFormat();
if (formatter.format(1) === formatter.format(-1)) { assert.notSameValue(formatter.format(1), formatter.format(-1), 'Intl.NumberFormat is formatting 1 and -1 the same way.');
$ERROR('Intl.NumberFormat is formatting 1 and -1 the same way.');
}
if (formatter.format(-0) !== formatter.format(0)) { assert.sameValue(formatter.format(-0), formatter.format(0), 'Intl.NumberFormat is formatting signed zeros differently.');
$ERROR('Intl.NumberFormat is formatting signed zeros differently.');
}

View File

@ -30,31 +30,14 @@ var formattedNaN = formatter.format(NaN);
var formattedInfinity = formatter.format(Infinity); var formattedInfinity = formatter.format(Infinity);
var formattedNegativeInfinity = formatter.format(-Infinity); var formattedNegativeInfinity = formatter.format(-Infinity);
if (formattedNaN === formattedInfinity) { assert.notSameValue(formattedNaN, formattedInfinity, 'Intl.NumberFormat formats NaN and Infinity the same way.');
$ERROR('Intl.NumberFormat formats NaN and Infinity the ' +
'same way.');
}
if (formattedNaN === formattedNegativeInfinity) { assert.notSameValue(formattedNaN, formattedNegativeInfinity, 'Intl.NumberFormat formats NaN and negative Infinity the same way.');
$ERROR('Intl.NumberFormat formats NaN and negative ' +
'Infinity the same way.');
}
if (formattedInfinity === formattedNegativeInfinity) { assert.notSameValue(formattedInfinity, formattedNegativeInfinity, 'Intl.NumberFormat formats Infinity and negative Infinity the same way.');
$ERROR('Intl.NumberFormat formats Infinity and ' +
'negative Infinity the same way.');
}
if (hasUnicodeDigits.test(formattedNaN)) { assert.sameValue(hasUnicodeDigits.test(formattedNaN), false, 'Intl.NumberFormat formats NaN using a digit.');
$ERROR('Intl.NumberFormat formats NaN using a digit.');
}
if (hasUnicodeDigits.test(formattedInfinity)) { assert.sameValue(hasUnicodeDigits.test(formattedInfinity), false, 'Intl.NumberFormat formats Infinity using a digit.');
$ERROR('Intl.NumberFormat formats Infinity using a ' +
'digit.');
}
if (hasUnicodeDigits.test(formattedNegativeInfinity)) { assert.sameValue(hasUnicodeDigits.test(formattedNegativeInfinity), false, 'Intl.NumberFormat formats negative Infinity using a digit.');
$ERROR('Intl.NumberFormat formats negative Infinity ' +
'using a digit.');
}

View File

@ -17,12 +17,7 @@ var formattedTwentyPercent = percentFormatter.format(0.20);
// FIXME: May not work for some theoretical locales where percents and // FIXME: May not work for some theoretical locales where percents and
// normal numbers are formatted using different numbering systems. // normal numbers are formatted using different numbering systems.
if (formattedTwentyPercent.indexOf(formattedTwenty) === -1) { assert.notSameValue(formattedTwentyPercent.indexOf(formattedTwenty), -1, "Intl.NumberFormat's formatting of 20% does not include a formatting of 20 as a substring.");
$ERROR("Intl.NumberFormat's formatting of 20% does not include a " +
"formatting of 20 as a substring.");
}
// FIXME: Move this to somewhere appropriate // FIXME: Move this to somewhere appropriate
if (percentFormatter.format(0.011) === percentFormatter.format(0.02)) { assert.notSameValue(percentFormatter.format(0.011), percentFormatter.format(0.02), 'Intl.NumberFormat is formatting 1.1% and 2% the same way.');
$ERROR('Intl.NumberFormat is formatting 1.1% and 2% the same way.');
}

View File

@ -14,9 +14,7 @@ includes: [testIntl.js]
var actual = new Intl.NumberFormat().resolvedOptions(); var actual = new Intl.NumberFormat().resolvedOptions();
var actual2 = new Intl.NumberFormat().resolvedOptions(); var actual2 = new Intl.NumberFormat().resolvedOptions();
if (actual2 === actual) { assert.notSameValue(actual2, actual, "resolvedOptions returned the same object twice.");
$ERROR("resolvedOptions returned the same object twice.");
}
// this assumes the default values where the specification provides them // this assumes the default values where the specification provides them
mustHaveProperty(actual, "locale", isCanonicalizedStructurallyValidLanguageTag); mustHaveProperty(actual, "locale", isCanonicalizedStructurallyValidLanguageTag);

View File

@ -15,15 +15,9 @@ var requestedLocales = [defaultLocale, notSupported];
var supportedLocales; var supportedLocales;
if (!Intl.NumberFormat.hasOwnProperty('supportedLocalesOf')) { assert(Intl.NumberFormat.hasOwnProperty('supportedLocalesOf'), "Intl.NumberFormat doesn't have a supportedLocalesOf property.");
$ERROR("Intl.NumberFormat doesn't have a supportedLocalesOf property.");
}
supportedLocales = Intl.NumberFormat.supportedLocalesOf(requestedLocales); supportedLocales = Intl.NumberFormat.supportedLocalesOf(requestedLocales);
if (supportedLocales.length !== 1) { assert.sameValue(supportedLocales.length, 1, 'The length of supported locales list is not 1.');
$ERROR('The length of supported locales list is not 1.');
}
if (supportedLocales[0] !== defaultLocale) { assert.sameValue(supportedLocales[0], defaultLocale, 'The default locale is not returned in the supported list.');
$ERROR('The default locale is not returned in the supported list.');
}