Temporal: Add coverage for formatting each Temporal object with only one component

Adds a test for each Temporal object's toLocaleString() method, formatting
them with only one option e.g. { year: 'numeric' } and comparing it with
the corresponding output for legacy Date.

See tc39/proposal-temporal#2796.
This commit is contained in:
Philip Chimento 2024-10-03 15:42:29 -07:00 committed by Ms2ger
parent 459cef7185
commit 9e6ab9bb02
7 changed files with 191 additions and 0 deletions

View File

@ -0,0 +1,32 @@
// Copyright (C) 2024 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.tolocalestring
description: Each date and time component can be formatted individually
locale: [en]
features: [Temporal]
---*/
const epochMs = 1735213600_321; // 2024-12-26T11:46:40.321Z
const epochNs = BigInt(epochMs) * 1_000_000n;
const legacyDate = new Date(epochMs);
const instant = new Temporal.Instant(epochNs);
for (const options of [
{ "year": "numeric" },
{ "month": "long" },
{ "day": "numeric" },
{ "weekday": "long" },
{ "hour": "numeric" },
{ "minute": "numeric" },
{ "second": "numeric" },
{ "fractionalSecondDigits": 3 },
{ "dayPeriod": "short" },
{ "timeZoneName": "short" },
// "era" is skipped; it implies year, month, and day in "en" locale
]) {
const instantResult = instant.toLocaleString("en", options);
const legacyDateResult = legacyDate.toLocaleString("en", options);
assert.sameValue(instantResult, legacyDateResult, `Instant.toLocaleString should format lone option ${JSON.stringify(options)}`);
}

View File

@ -0,0 +1,25 @@
// Copyright (C) 2024 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaindate.prototype.tolocalestring
description: Each date component can be formatted individually
locale: [en]
features: [Temporal]
---*/
const epochMs = 1735213600_321; // 2024-12-26T11:46:40.321Z
const legacyDate = new Date(epochMs);
const plainDate = new Temporal.PlainDate(2024, 12, 26);
for (const options of [
{ "year": "numeric" },
{ "month": "long" },
{ "day": "numeric" },
{ "weekday": "long" },
// "era" is skipped; it implies year, month, and day in "en" locale
]) {
const plainDateResult = plainDate.toLocaleString("en", options);
const legacyDateResult = legacyDate.toLocaleString("en", options);
assert.sameValue(plainDateResult, legacyDateResult, `Instant.toLocaleString should format lone option ${JSON.stringify(options)}`);
}

View File

@ -0,0 +1,31 @@
// Copyright (C) 2024 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaindatetime.prototype.tolocalestring
description: Each date and time component can be formatted individually
locale: [en]
features: [Temporal]
---*/
const epochMs = 1735213600_321; // 2024-12-26T11:46:40.321Z
const legacyDateLocal = new Date(epochMs);
const legacyDate = new Date(epochMs + legacyDateLocal.getTimezoneOffset() * 60 * 1000);
const plainDateTime = new Temporal.PlainDateTime(2024, 12, 26, 11, 46, 40, 321);
for (const options of [
{ "year": "numeric" },
{ "month": "long" },
{ "day": "numeric" },
{ "weekday": "long" },
{ "hour": "numeric" },
{ "minute": "numeric" },
{ "second": "numeric" },
{ "fractionalSecondDigits": 3 },
{ "dayPeriod": "short" },
// "era" is skipped; it implies year, month, and day in "en" locale
]) {
const plainDateTimeResult = plainDateTime.toLocaleString("en", options);
const legacyDateResult = legacyDate.toLocaleString("en", options);
assert.sameValue(plainDateTimeResult, legacyDateResult, `PlainDateTime.toLocaleString should format lone option ${JSON.stringify(options)}`);
}

View File

@ -0,0 +1,22 @@
// Copyright (C) 2024 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plainmonthday.prototype.tolocalestring
description: Each date component can be formatted individually
locale: [en-u-ca-iso8601]
features: [Temporal]
---*/
const epochMs = 1735213600_321; // 2024-12-26T11:46:40.321Z
const legacyDate = new Date(epochMs);
const plainMonthDay = new Temporal.PlainMonthDay(12, 26);
for (const options of [
{ "month": "long" },
{ "day": "numeric" },
]) {
const plainMonthDayResult = plainMonthDay.toLocaleString("en-u-ca-iso8601", options);
const legacyDateResult = legacyDate.toLocaleString("en-u-ca-iso8601", options);
assert.sameValue(plainMonthDayResult, legacyDateResult, `PlainMonthDay.toLocaleString should format lone option ${JSON.stringify(options)}`);
}

View File

@ -0,0 +1,26 @@
// Copyright (C) 2024 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaintime.prototype.tolocalestring
description: Each time component can be formatted individually
locale: [en]
features: [Temporal]
---*/
const epochMs = 1735213600_321; // 2024-12-26T11:46:40.321Z
const legacyDateLocal = new Date(epochMs);
const legacyDate = new Date(epochMs + legacyDateLocal.getTimezoneOffset() * 60 * 1000);
const plainTime = new Temporal.PlainTime(11, 46, 40, 321);
for (const options of [
{ "hour": "numeric" },
{ "minute": "numeric" },
{ "second": "numeric" },
{ "fractionalSecondDigits": 3 },
{ "dayPeriod": "short" },
]) {
const plainTimeResult = plainTime.toLocaleString("en", options);
const legacyDateResult = legacyDate.toLocaleString("en", options);
assert.sameValue(plainTimeResult, legacyDateResult, `PlainTime.toLocaleString should format lone option ${JSON.stringify(options)}`);
}

View File

@ -0,0 +1,23 @@
// Copyright (C) 2024 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plainyearmonth.prototype.tolocalestring
description: Each date and time component can be formatted individually
locale: [en-u-ca-iso8601]
features: [Temporal]
---*/
const epochMs = 1735213600_321; // 2024-12-26T11:46:40.321Z
const legacyDate = new Date(epochMs);
const plainYearMonth = new Temporal.PlainYearMonth(2024, 12);
for (const options of [
{ "year": "numeric" },
{ "month": "long" },
// "era" is skipped; it implies year, month, and day in "en" locale
]) {
const plainYearMonthResult = plainYearMonth.toLocaleString("en-u-ca-iso8601", options);
const legacyDateResult = legacyDate.toLocaleString("en-u-ca-iso8601", options);
assert.sameValue(plainYearMonthResult, legacyDateResult, `Instant.toLocaleString should format lone option ${JSON.stringify(options)}`);
}

View File

@ -0,0 +1,32 @@
// Copyright (C) 2024 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.tolocalestring
description: Each date and time component can be formatted individually
locale: [en, en-u-tz-utc]
features: [Temporal]
---*/
const epochMs = 1735213600_321; // 2024-12-26T11:46:40.321Z
const epochNs = BigInt(epochMs) * 1_000_000n;
const legacyDate = new Date(epochMs);
const zonedDateTime = new Temporal.ZonedDateTime(epochNs, "UTC");
for (const options of [
{ "year": "numeric" },
{ "month": "long" },
{ "day": "numeric" },
{ "weekday": "long" },
{ "hour": "numeric" },
{ "minute": "numeric" },
{ "second": "numeric" },
{ "fractionalSecondDigits": 3 },
{ "dayPeriod": "short" },
{ "timeZoneName": "short" },
// "era" is skipped; it implies year, month, and day in "en" locale
]) {
const zonedDateTimeResult = zonedDateTime.toLocaleString("en", options);
const legacyDateResult = legacyDate.toLocaleString("en", { ...options, timeZone: "UTC" });
assert.sameValue(zonedDateTimeResult, legacyDateResult, `ZonedDateTime.toLocaleString should format lone option ${JSON.stringify(options)}`);
}