Use assert-lib: intl402/DateTimeFormat

This commit is contained in:
André Bargull 2017-05-04 21:12:42 +02:00
parent 94e9ad831e
commit 1579764e37
20 changed files with 44 additions and 133 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.DateTimeFormat.call(obj); newObj = Intl.DateTimeFormat.call(obj);
if (obj === newObj) { assert.notSameValue(obj, newObj, "DateTimeFormat object created with \"new\" was not ignored as this-value.");
$ERROR("DateTimeFormat 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.DateTimeFormat.call(obj); newObj = Intl.DateTimeFormat.call(obj);
if (obj === newObj) { assert.notSameValue(obj, newObj, "DateTimeFormat object created with constructor as function was not ignored as this-value.");
$ERROR("DateTimeFormat object created with constructor as function was not ignored as this-value.");
}
return true; return true;
}); });

View File

@ -13,6 +13,4 @@ includes: [testIntl.js]
taintProperties(["weekday", "era", "year", "month", "day", "hour", "minute", "second", "timeZone"]); taintProperties(["weekday", "era", "year", "month", "day", "hour", "minute", "second", "timeZone"]);
var locale = new Intl.DateTimeFormat(undefined, {localeMatcher: "lookup"}).resolvedOptions().locale; var locale = new Intl.DateTimeFormat(undefined, {localeMatcher: "lookup"}).resolvedOptions().locale;
if (!isCanonicalizedStructurallyValidLanguageTag(locale)) { assert(isCanonicalizedStructurallyValidLanguageTag(locale), "DateTimeFormat returns invalid locale " + locale + ".");
$ERROR("DateTimeFormat returns invalid locale " + locale + ".");
}

View File

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

View File

@ -19,19 +19,17 @@ function testWithDateTimeFormat(options, expected) {
var resolvedOptions = format.resolvedOptions(); var resolvedOptions = format.resolvedOptions();
getDateTimeComponents().forEach(function (component) { getDateTimeComponents().forEach(function (component) {
if (resolvedOptions.hasOwnProperty(component)) { if (resolvedOptions.hasOwnProperty(component)) {
if (!expected.hasOwnProperty(component)) { assert(expected.hasOwnProperty(component),
$ERROR("Unrequested component " + component + "Unrequested component " + component +
" added to expected subset " + JSON.stringify(expected) + " added to expected subset " + JSON.stringify(expected) +
"; locales " + locales + ", options " + "; locales " + locales + ", options " +
(options ? JSON.stringify(options) : options) + "."); (options ? JSON.stringify(options) : options) + ".");
}
} else { } else {
if (expected.hasOwnProperty(component)) { assert.sameValue(expected.hasOwnProperty(component), false,
$ERROR("Missing component " + component + "Missing component " + component +
" from expected subset " + JSON.stringify(expected) + " from expected subset " + JSON.stringify(expected) +
"; locales " + locales + ", options " + "; locales " + locales + ", options " +
(options ? JSON.stringify(options) : options) + "."); (options ? JSON.stringify(options) : options) + ".");
}
} }
}); });
}); });
@ -50,13 +48,12 @@ function testWithToLocale(f, options, expected) {
var referenceFormat = new Intl.DateTimeFormat(locales, expected); var referenceFormat = new Intl.DateTimeFormat(locales, expected);
expectedStrings.push(referenceFormat.format(date)); expectedStrings.push(referenceFormat.format(date));
}); });
if (expectedStrings.indexOf(formatted) === -1) { assert.notSameValue(expectedStrings.indexOf(formatted), -1,
$ERROR("Function " + f + " did not return expected string for locales " + "Function " + f + " did not return expected string for locales " +
locales + ", options " + (options? JSON.stringify(options) : options) + locales + ", options " + (options? JSON.stringify(options) : options) +
"; expected " + "; expected " +
(expectedStrings.length === 1 ? expectedStrings[0] : "one of " + expectedStrings) + (expectedStrings.length === 1 ? expectedStrings[0] : "one of " + expectedStrings) +
", got " + formatted + "."); ", got " + formatted + ".");
}
}); });
}); });
} }

View File

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

View File

@ -30,18 +30,9 @@ locales.forEach(function (locale) {
keyValues[key].forEach(function (value) { keyValues[key].forEach(function (value) {
var dateTimeFormat = new Intl.DateTimeFormat([locale + "-u-" + key + "-" + value]); var dateTimeFormat = new Intl.DateTimeFormat([locale + "-u-" + key + "-" + value]);
var options = dateTimeFormat.resolvedOptions(); var options = dateTimeFormat.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(dateTimeFormat.format(input), defaultFormatted, "Formatted value " + dateTimeFormat.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 !== dateTimeFormat.format(input)) {
$ERROR("Formatted value " + dateTimeFormat.format(input) + " is affected by key " +
key + "; value " + value + ".");
}
}); });
}); });
}); });

View File

@ -28,11 +28,10 @@ locales.forEach(function (locale) {
var actual = format.resolvedOptions(); var actual = format.resolvedOptions();
getDateTimeComponents().forEach(function (component) { getDateTimeComponents().forEach(function (component) {
if (actual.hasOwnProperty(component)) { if (actual.hasOwnProperty(component)) {
if (!subset.hasOwnProperty(component)) { assert(subset.hasOwnProperty(component),
$ERROR("Unrequested component " + component + "Unrequested component " + component +
" added to requested subset " + JSON.stringify(subset) + " added to requested subset " + JSON.stringify(subset) +
"; locale " + locale + "."); "; locale " + locale + ".");
}
try { try {
testValidDateTimeComponentValue(component, actual[component]); testValidDateTimeComponentValue(component, actual[component]);
} catch (e) { } catch (e) {
@ -41,11 +40,10 @@ locales.forEach(function (locale) {
throw e; throw e;
} }
} else { } else {
if (subset.hasOwnProperty(component)) { assert.sameValue(subset.hasOwnProperty(component), false,
$ERROR("Missing component " + component + "Missing component " + component +
" from requested subset " + JSON.stringify(subset) + " from requested subset " + JSON.stringify(subset) +
"; locale " + locale + "."); "; locale " + locale + ".");
}
} }
}); });
}); });

View File

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

View File

@ -15,8 +15,5 @@ var validTimeZoneNames = [
validTimeZoneNames.forEach(function (name) { validTimeZoneNames.forEach(function (name) {
// this must not throw an exception for a valid time zone name // this must not throw an exception for a valid time zone name
var format = new Intl.DateTimeFormat(["de-de"], {timeZone: name}); var format = new Intl.DateTimeFormat(["de-de"], {timeZone: name});
if (format.resolvedOptions().timeZone !== name.toUpperCase()) { assert.sameValue(format.resolvedOptions().timeZone, name.toUpperCase(), "Time zone name " + name + " was not correctly accepted.");
$ERROR("Time zone name " + name + " was not correctly accepted; turned into " +
format.resolvedOptions().timeZone + ".");
}
}); });

View File

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

View File

@ -28,11 +28,8 @@ Object.getOwnPropertyNames(additionalTimeZoneNames).forEach(function (name) {
if (error === undefined) { if (error === undefined) {
var actual = format.resolvedOptions().timeZone; var actual = format.resolvedOptions().timeZone;
var expected = additionalTimeZoneNames[name]; var expected = additionalTimeZoneNames[name];
if (actual !== expected) { assert.sameValue(actual, expected, "Time zone name " + name + " was accepted, but incorrectly canonicalized.");
$ERROR("Time zone name " + name + " was accepted, but incorrectly canonicalized to " + } else {
actual + "; expected " + expected + "."); assert(error instanceof RangeError, "Time zone name " + name + " was rejected with wrong error " + error.name + ".");
}
} else if (error.name !== "RangeError") {
$ERROR("Time zone name " + name + " was rejected with wrong error " + error.name + ".");
} }
}); });

View File

@ -10,15 +10,7 @@ author: Norbert Lindenberg
---*/ ---*/
var desc = Object.getOwnPropertyDescriptor(Intl.DateTimeFormat, "prototype"); var desc = Object.getOwnPropertyDescriptor(Intl.DateTimeFormat, "prototype");
if (desc === undefined) { assert.notSameValue(desc, undefined, "Intl.DateTimeFormat.prototype is not defined.");
$ERROR("Intl.DateTimeFormat.prototype is not defined."); assert.sameValue(desc.writable, false, "Intl.DateTimeFormat.prototype must not be writable.");
} assert.sameValue(desc.enumerable, false, "Intl.DateTimeFormat.prototype must not be enumerable.");
if (desc.writable) { assert.sameValue(desc.configurable, false, "Intl.DateTimeFormat.prototype must not be configurable.");
$ERROR("Intl.DateTimeFormat.prototype must not be writable.");
}
if (desc.enumerable) {
$ERROR("Intl.DateTimeFormat.prototype must not be enumerable.");
}
if (desc.configurable) {
$ERROR("Intl.DateTimeFormat.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.DateTimeFormat // initialized as an Intl.DateTimeFormat
if (typeof Intl.DateTimeFormat.prototype.format(0) !== "string") { assert.sameValue(typeof Intl.DateTimeFormat.prototype.format(0), "string", "Intl.DateTimeFormat's prototype is not an object that has been initialized as an Intl.DateTimeFormat");
$ERROR("Intl.DateTimeFormat's prototype is not an object that has been " +
"initialized as an Intl.DateTimeFormat");
}

View File

@ -19,16 +19,8 @@ var invalidTargets = [undefined, null, true, 0, "DateTimeFormat", [], {}];
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.DateTimeFormat.prototype.constructor !== Intl.DateTimeFormat) { assert.sameValue(Intl.DateTimeFormat.prototype.constructor, Intl.DateTimeFormat, "Intl.DateTimeFormat.prototype.constructor is not the same as Intl.DateTimeFormat");
$ERROR("Intl.DateTimeFormat.prototype.constructor is not the same as " +
"Intl.DateTimeFormat");
}

View File

@ -23,11 +23,7 @@ locales.forEach(function (locales) {
dates.forEach(function (date) { dates.forEach(function (date) {
var referenceFormatted = formatObj.format(date); var referenceFormatted = formatObj.format(date);
var formatted = formatFunc(date); var formatted = formatFunc(date);
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

@ -12,15 +12,7 @@ var invalidValues = [NaN, Infinity, -Infinity];
var format = new Intl.DateTimeFormat(); var format = new Intl.DateTimeFormat();
invalidValues.forEach(function (value) { invalidValues.forEach(function (value) {
var error; assert.throws(RangeError, function() {
try {
var result = format.format(value); var result = format.format(value);
} catch (e) { }, "Invalid value " + value + " was not rejected.");
error = e;
}
if (error === undefined) {
$ERROR("Invalid value " + value + " was not rejected.");
} else if (error.name !== "RangeError") {
$ERROR("Invalid value " + value + " was rejected with wrong error " + error.name + ".");
}
}); });

View File

@ -18,17 +18,12 @@ var dates = [
var format = new Intl.DateTimeFormat(["en-US"], {year: "numeric", month: "long", timeZone: "UTC"}); var format = new Intl.DateTimeFormat(["en-US"], {year: "numeric", month: "long", timeZone: "UTC"});
// this test requires a Gregorian calendar, which we usually find in the US // this test requires a Gregorian calendar, which we usually find in the US
if (format.resolvedOptions().calendar !== "gregory") { assert.sameValue(format.resolvedOptions().calendar, "gregory", "Internal error: Didn't find Gregorian calendar");
$ERROR("Internal error: Didn't find Gregorian calendar");
}
dates.forEach(function (date) { dates.forEach(function (date) {
var year = new Date(date).getUTCFullYear(); var year = new Date(date).getUTCFullYear();
var expectedYear = year <= 0 ? 1 - year : year; var expectedYear = year <= 0 ? 1 - year : year;
var expectedYearString = expectedYear.toLocaleString(["en-US"], {useGrouping: false}); var expectedYearString = expectedYear.toLocaleString(["en-US"], {useGrouping: false});
var dateString = format.format(date); var dateString = format.format(date);
if (dateString.indexOf(expectedYearString) === -1) { assert.notSameValue(dateString.indexOf(expectedYearString), -1, "Formatted year doesn't contain expected year expected " + expectedYearString + ", got " + dateString + ".");
$ERROR("Formatted year doesn't contain expected year expected " +
expectedYearString + ", got " + dateString + ".");
}
}); });

View File

@ -14,9 +14,7 @@ includes: [testIntl.js]
var actual = new Intl.DateTimeFormat().resolvedOptions(); var actual = new Intl.DateTimeFormat().resolvedOptions();
var actual2 = new Intl.DateTimeFormat().resolvedOptions(); var actual2 = new Intl.DateTimeFormat().resolvedOptions();
if (actual2 === actual) { assert.notSameValue(actual2, actual, "resolvedOptions returned the same object twice.");
$ERROR("resolvedOptions returned the same object twice.");
}
// source: CLDR file common/bcp47/calendar.xml; version CLDR 21. // source: CLDR file common/bcp47/calendar.xml; version CLDR 21.
var calendars = [ var calendars = [

View File

@ -15,15 +15,9 @@ var requestedLocales = [defaultLocale, notSupported];
var supportedLocales; var supportedLocales;
if (!Intl.DateTimeFormat.hasOwnProperty('supportedLocalesOf')) { assert(Intl.DateTimeFormat.hasOwnProperty('supportedLocalesOf'), "Intl.DateTimeFormat doesn't have a supportedLocalesOf property.");
$ERROR("Intl.DateTimeFormat doesn't have a supportedLocalesOf property.");
}
supportedLocales = Intl.DateTimeFormat.supportedLocalesOf(requestedLocales); supportedLocales = Intl.DateTimeFormat.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.');
}