Add tests for the datetime-style proposal. (#2125)

This commit is contained in:
Ms2ger 2019-04-10 17:09:40 +02:00 committed by Leo Balter
parent 611919174f
commit c03e14263e
9 changed files with 420 additions and 0 deletions

View File

@ -88,6 +88,10 @@ Intl.Segmenter
# https://github.com/sffc/proposal-unified-intl-numberformat
Intl.NumberFormat-unified
# Intl.DateTimeFormat: dateStyle and timeStyle options
# https://github.com/tc39/proposal-intl-datetime-style
Intl.DateTimeFormat-datetimestyle
# Global
# https://github.com/tc39/proposal-global
globalThis

View File

@ -0,0 +1,29 @@
// Copyright 2019 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-initializedatetimeformat
description: >
Checks error cases for the options argument to the DateTimeFormat constructor.
info: |
InitializeDateTimeFormat ( dateTimeFormat, locales, options )
...
28. Let dateStyle be ? GetOption(options, "dateStyle", "string", « "full", "long", "medium", "short" », undefined).
features: [Intl.DateTimeFormat-datetimestyle]
---*/
const invalidOptions = [
"",
"FULL",
" long",
"short ",
"narrow",
"numeric",
];
for (const dateStyle of invalidOptions) {
assert.throws(RangeError, function() {
new Intl.DateTimeFormat("en", { dateStyle });
}, `new Intl.DateTimeFormat("en", { dateStyle: "${dateStyle}" }) throws RangeError`);
}

View File

@ -0,0 +1,37 @@
// Copyright 2019 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-initializedatetimeformat
description: >
Checks handling of the options argument to the DateTimeFormat constructor.
info: |
InitializeDateTimeFormat ( dateTimeFormat, locales, options )
...
28. Let dateStyle be ? GetOption(options, "dateStyle", "string", « "full", "long", "medium", "short" », undefined).
29. If dateStyle is not undefined, set dateTimeFormat.[[DateStyle]] to dateStyle.
features: [Intl.DateTimeFormat-datetimestyle]
---*/
const validOptions = [
[undefined, undefined],
["full", "full"],
["long", "long"],
["medium", "medium"],
["short", "short"],
[{ toString() { return "full"; } }, "full"],
[{ valueOf() { return "long"; }, toString: undefined }, "long"],
];
for (const [dateStyle, expected] of validOptions) {
const dtf = new Intl.DateTimeFormat("en", { dateStyle });
const options = dtf.resolvedOptions();
assert.sameValue(options.dateStyle, expected);
const propdesc = Object.getOwnPropertyDescriptor(options, "dateStyle");
if (expected === undefined) {
assert.sameValue(propdesc, undefined);
} else {
assert.sameValue(propdesc.value, expected);
}
}

View File

@ -0,0 +1,130 @@
// Copyright 2018 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-initializedatetimeformat
description: Checks the order of getting options for the DateTimeFormat constructor.
includes: [compareArray.js]
features: [Intl.DateTimeFormat-datetimestyle]
---*/
// To be merged into constructor-options-order.js when the feature is removed.
const expected = [
// ToDateTimeOptions step 4.
"weekday", "year", "month", "day",
// ToDateTimeOptions step 5.
"hour", "minute", "second",
// InitializeDateTimeFormat step 4.
"localeMatcher",
// InitializeDateTimeFormat step 6.
"hour12",
// InitializeDateTimeFormat step 7.
"hourCycle",
// InitializeDateTimeFormat step 22.
"timeZone",
// InitializeDateTimeFormat step 28.
"dateStyle",
// InitializeDateTimeFormat step 30.
"timeStyle",
// InitializeDateTimeFormat step 33.
"weekday",
"era",
"year",
"month",
"day",
"hour",
"minute",
"second",
"timeZoneName",
"formatMatcher",
];
const actual = [];
const options = {
get dateStyle() {
actual.push("dateStyle");
return undefined;
},
get day() {
actual.push("day");
return "numeric";
},
get era() {
actual.push("era");
return "long";
},
get formatMatcher() {
actual.push("formatMatcher");
return "best fit";
},
get hour() {
actual.push("hour");
return "numeric";
},
get hour12() {
actual.push("hour12");
return true;
},
get hourCycle() {
actual.push("hourCycle");
return "h24";
},
get localeMatcher() {
actual.push("localeMatcher");
return "best fit";
},
get minute() {
actual.push("minute");
return "numeric";
},
get month() {
actual.push("month");
return "numeric";
},
get second() {
actual.push("second");
return "numeric";
},
get timeStyle() {
actual.push("timeStyle");
return undefined;
},
get timeZone() {
actual.push("timeZone");
return "UTC";
},
get timeZoneName() {
actual.push("timeZoneName");
return "long";
},
get weekday() {
actual.push("weekday");
return "long";
},
get year() {
actual.push("year");
return "numeric";
},
};
new Intl.DateTimeFormat("en", options);
assert.compareArray(actual, expected);

View File

@ -0,0 +1,29 @@
// Copyright 2018 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-initializedatetimeformat
description: Checks the propagation of exceptions from the options for the DateTimeFormat constructor.
features: [Intl.DateTimeFormat-datetimestyle]
---*/
// To be merged into constructor-options-throwing-getters.js when the feature is removed.
function CustomError() {}
const options = [
// InitializeDateTimeFormat step 28
"dateStyle",
// InitializeDateTimeFormat step 30
"timeStyle",
];
for (const option of options) {
assert.throws(CustomError, () => {
new Intl.DateTimeFormat("en", {
get [option]() {
throw new CustomError();
}
});
}, `Exception from ${option} getter should be propagated`);
}

View File

@ -0,0 +1,29 @@
// Copyright 2019 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-initializedatetimeformat
description: >
Checks error cases for the options argument to the DateTimeFormat constructor.
info: |
InitializeDateTimeFormat ( dateTimeFormat, locales, options )
...
30. Let timeStyle be ? GetOption(options, "timeStyle", "string", « "full", "long", "medium", "short" », undefined).
features: [Intl.DateTimeFormat-datetimestyle]
---*/
const invalidOptions = [
"",
"FULL",
" long",
"short ",
"narrow",
"numeric",
];
for (const timeStyle of invalidOptions) {
assert.throws(RangeError, function() {
new Intl.DateTimeFormat("en", { timeStyle });
}, `new Intl.DateTimeFormat("en", { timeStyle: "${timeStyle}" }) throws RangeError`);
}

View File

@ -0,0 +1,36 @@
// Copyright 2019 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-initializedatetimeformat
description: >
Checks handling of the options argument to the DateTimeFormat constructor.
info: |
InitializeDateTimeFormat ( dateTimeFormat, locales, options )
...
30. Let timeStyle be ? GetOption(options, "timeStyle", "string", « "full", "long", "medium", "short" », undefined).
31. If timeStyle is not undefined, set dateTimeFormat.[[TimeStyle]] to timeStyle.
features: [Intl.DateTimeFormat-datetimestyle]
---*/
const validOptions = [
[undefined, undefined],
["full", "full"],
["long", "long"],
["medium", "medium"],
["short", "short"],
[{ toString() { return "full"; } }, "full"],
[{ valueOf() { return "long"; }, toString: undefined }, "long"],
];
for (const [timeStyle, expected] of validOptions) {
const dtf = new Intl.DateTimeFormat("en", { timeStyle });
const options = dtf.resolvedOptions();
assert.sameValue(options.timeStyle, expected);
const propdesc = Object.getOwnPropertyDescriptor(options, "timeStyle");
if (expected === undefined) {
assert.sameValue(propdesc, undefined);
} else {
assert.sameValue(propdesc.value, expected);
}
}

View File

@ -0,0 +1,91 @@
// Copyright 2019 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-date-time-style-pattern
description: Checks basic handling of timeStyle and dateStyle.
features: [Intl.DateTimeFormat-datetimestyle]
locale: [en-US]
---*/
const date = new Date("1886-05-01T14:12:47Z");
const dateOptions = [
["full", "Saturday, May 1, 1886", " at "],
["long", "May 1, 1886", " at "],
["medium", "May 1, 1886", ", "],
["short", "5/1/86", ", "],
];
const timeOptions = [
["full", "2:12:47 PM Coordinated Universal Time", "14:12:47 Coordinated Universal Time"],
["long", "2:12:47 PM UTC", "14:12:47 UTC"],
["medium", "2:12:47 PM", "14:12:47"],
["short", "2:12 PM", "14:12"],
];
const options12 = [
{ "hour12": true },
{ "hourCycle": "h11" },
{ "hourCycle": "h12" },
{ "hourCycle": "h23", "hour12": true },
{ "hourCycle": "h24", "hour12": true },
];
const options24 = [
{ "hour12": false },
{ "hourCycle": "h23" },
{ "hourCycle": "h24" },
{ "hourCycle": "h11", "hour12": false },
{ "hourCycle": "h12", "hour12": false },
];
for (const [dateStyle, expected] of dateOptions) {
const dtf = new Intl.DateTimeFormat("en-US", {
dateStyle,
timeZone: "UTC",
});
const dateString = dtf.format(date);
assert.sameValue(dateString, expected, `Result for ${dateStyle}`);
}
for (const [timeStyle, expected12, expected24] of timeOptions) {
for (const hourOptions of options12) {
const dtf = new Intl.DateTimeFormat("en-US", {
timeStyle,
timeZone: "UTC",
...hourOptions
});
const dateString = dtf.format(date);
assert.sameValue(dateString, expected12, `Result for ${timeStyle} with ${JSON.stringify(hourOptions)}`);
}
for (const hourOptions of options24) {
const dtf = new Intl.DateTimeFormat("en-US", {
timeStyle,
timeZone: "UTC",
...hourOptions
});
const dateString = dtf.format(date);
assert.sameValue(dateString, expected24, `Result for ${timeStyle} with ${JSON.stringify(hourOptions)}`);
}
}
for (const [dateStyle, expectedDate, connector] of dateOptions) {
for (const [timeStyle, expectedTime] of timeOptions) {
const dtf = new Intl.DateTimeFormat("en-US", {
dateStyle,
timeStyle,
timeZone: "UTC",
});
const dateString = dtf.format(date);
assert.sameValue(
dateString,
[expectedDate, connector, expectedTime].join(""),
`Result for date=${dateStyle} and time=${timeStyle}`
);
}
}

View File

@ -0,0 +1,35 @@
// Copyright 2018 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-intl.datetimeformat.prototype.resolvedoptions
description: Verifies the property order for the object returned by resolvedOptions().
includes: [compareArray.js]
features: [Intl.DateTimeFormat-datetimestyle]
---*/
const options = new Intl.DateTimeFormat([], {
"timeStyle": "full",
"hourCycle": "h24",
"weekday": "short",
"era": "short",
"year": "numeric",
"month": "numeric",
"day": "numeric",
"hour": "numeric",
"minute": "numeric",
"second": "numeric",
"timeZoneName": "short",
}).resolvedOptions();
const expected = [
"locale",
"calendar",
"numberingSystem",
"timeZone",
"hourCycle",
"hour12",
"timeStyle",
];
assert.compareArray(Object.getOwnPropertyNames(options), expected);