Temporal: Add test for formatting Temporal objects with era formatter (#4634)

See https://github.com/tc39/proposal-temporal/issues/3049
This commit is contained in:
Tim Chevalier 2025-11-17 23:20:16 -08:00 committed by GitHub
parent 3743134508
commit 875b3ee068
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 212 additions and 0 deletions

View File

@ -0,0 +1,21 @@
// Copyright (C) 2024 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-datetime-format-functions
description: >
If era option and no other options are provided to DateTimeFormat constructor,
objects should be formatted with default options
features: [Temporal]
locale: [en]
---*/
const formatter = new Intl.DateTimeFormat(["en"], { era: "narrow" });
assert(formatter.format(new Temporal.PlainDate(2025, 11, 4)).startsWith("11"), "formatting a PlainDate should work");
assert(formatter.format(new Temporal.PlainYearMonth(2025, 11, "gregory")).startsWith("11"), "formatting a PlainYearMonth should work");
assert(formatter.format(new Temporal.PlainMonthDay(11, 4, "gregory")).startsWith("11"), "formatting a PlainMonthDay should work");
assert(formatter.format(new Temporal.PlainTime(14, 46)).startsWith("2"), "formatting a PlainTime should work");
assert(formatter.format(new Temporal.PlainDateTime(2025, 11, 4, 14, 46)).startsWith("11"), "formatting a PlainDateTime should work");
assert.sameValue(formatter.format(new Temporal.Instant(0n)),
new Date(0).toLocaleString(["en"], { era: "narrow" }), "toLocaleString on an Instant with era option should return the same results as toLocaleString on the same Date with the same 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-datetime-format-functions
description: >
If era option and no other options are provided to DateTimeFormat constructor,
objects should be formatted with default options
features: [Temporal]
locale: [en]
---*/
const formatter = new Intl.DateTimeFormat(["en"], { era: "narrow", timeZone: "UTC" });
assert(formatter.formatRange(new Temporal.PlainDate(2025, 11, 4), new Temporal.PlainDate(2025, 11, 5)).startsWith("11"), "formatting a PlainDate should work");
assert(formatter.formatRange(new Temporal.PlainYearMonth(2025, 11, "gregory"), new Temporal.PlainYearMonth(2025, 12, "gregory")).startsWith("11"), "formatting a PlainYearMonth should work");
assert(formatter.formatRange(new Temporal.PlainMonthDay(11, 4, "gregory"), new Temporal.PlainMonthDay(11, 14, "gregory")).startsWith("11"), "formatting a PlainMonthDay should work");
assert(formatter.formatRange(new Temporal.PlainTime(14, 46), new Temporal.PlainTime(17, 46)).startsWith("2"), "formatting a PlainTime should work");
assert(formatter.formatRange(new Temporal.PlainDateTime(2025, 11, 4, 14, 16, 0, 0, 0, 0, "gregory"), new Temporal.PlainDateTime(2025, 11, 15, 14, 47, 0, 0, 0, 0, "gregory")).startsWith("11"), "formatting a PlainDateTime should work");
// For instants, check the output of formatRange on the equivalent Dates and then check that this is a prefix
// of the output of formatRange on the Instants, because the time zone of the host might affect the output
const dateResult = formatter.formatRange(new Date(0), new Date(1));
assert(formatter.formatRange(new Temporal.Instant(0n), new Temporal.Instant(1000000000n)).startsWith(dateResult),
"formatting an Instant should work");

View File

@ -0,0 +1,28 @@
// Copyright (C) 2024 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-datetime-format-functions
description: >
If era option and no other options are provided to DateTimeFormat constructor,
objects should be formatted with default options
features: [Temporal]
locale: [en]
---*/
const formatter = new Intl.DateTimeFormat(["en"], { era: "narrow" });
function checkEra(parts) {
for (let part of parts) {
if (part.type === 'era' && part.value.startsWith('A'))
return true;
}
return false;
}
assert(checkEra(formatter.formatRangeToParts(new Temporal.PlainDate(2025, 11, 4 , "gregory"), new Temporal.PlainDate(2025, 11, 5, "gregory"))), "formatting a PlainDate should work");
assert(checkEra(formatter.formatRangeToParts(new Temporal.PlainYearMonth(2025, 11, "gregory"), new Temporal.PlainYearMonth(2025, 12, "gregory"))), "formatting a PlainYearMonth should work");
assert(checkEra(formatter.formatRangeToParts(new Temporal.PlainMonthDay(11, 4, "gregory"), new Temporal.PlainMonthDay(11, 14, "gregory"))), "formatting a PlainMonthDay should work");
assert(!checkEra(formatter.formatRangeToParts(new Temporal.PlainTime(14, 46), new Temporal.PlainTime(17, 46))), "formatting a PlainTime should work");
assert(checkEra(formatter.formatRangeToParts(new Temporal.PlainDateTime(2025, 11, 4, 14, 16, 0, 0, 0, 0, "gregory"), new Temporal.PlainDateTime(2025, 11, 15, 14, 47, 0, 0, 0, 0, "gregory"))), "formatting a PlainDateTime should work");
assert(checkEra(formatter.formatRangeToParts(new Temporal.Instant(0n), new Temporal.Instant(1000000000n))), "formatting an Instant should work");

View File

@ -0,0 +1,28 @@
// Copyright (C) 2025 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-datetime-format-functions
description: >
If era option and no other options are provided to DateTimeFormat constructor,
objects should be formatted with default options
features: [Temporal]
locale: [en]
---*/
const formatter = new Intl.DateTimeFormat(["en"], { era: "narrow" });
function checkEra(parts) {
for (let part of parts) {
if (part.type === 'era' && part.value.startsWith('A'))
return true;
}
return false;
}
assert(checkEra(formatter.formatToParts(new Temporal.PlainDate(2025, 11, 4))), "formatting a PlainDate should work");
assert(checkEra(formatter.formatToParts(new Temporal.PlainYearMonth(2025, 11, "gregory"))), "formatting a PlainYearMonth should work");
assert(checkEra(formatter.formatToParts(new Temporal.PlainMonthDay(11, 4, "gregory"))), "formatting a PlainMonthDay should work");
assert(!checkEra(formatter.formatToParts(new Temporal.PlainTime(14, 46))), "formatting a PlainTime should work");
assert(checkEra(formatter.formatToParts(new Temporal.PlainDateTime(2025, 11, 4, 14, 46))), "formatting a PlainDateTime should work");
assert(checkEra(formatter.formatToParts(new Temporal.Instant(0n))), "formatting an Instant should work");

View File

@ -0,0 +1,17 @@
// Copyright (C) 2025 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: >
If era option and no other options are provided to toLocaleString,
Instant should be foramtted with default options
features: [Temporal]
locale: [en]
---*/
const instant = new Temporal.Instant(0n);
const instantResult = instant.toLocaleString("en", { era: "narrow" });
const dateResult = new Date(0).toLocaleString(["en"], { era: "narrow" });
assert.sameValue(instantResult, dateResult, "toLocaleString on an Instant with era option should return the same results as toLocaleString on the same Date with the same options");

View File

@ -0,0 +1,15 @@
// Copyright (C) 2025 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: >
If era option and no other options are provided to toLocaleString,
PlainDate should be foramtted with default options
features: [Temporal]
locale: [en]
---*/
const date = new Temporal.PlainDate(2000, 5, 2, "gregory");
assert(date.toLocaleString("en", { era: "narrow" }).startsWith("5"), "toLocaleString on a PlainDate with era option should work");

View File

@ -0,0 +1,15 @@
// Copyright (C) 2025 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: >
If era option and no other options are provided to toLocaleString,
PlainDateTime should be foramtted with default options
features: [Temporal]
locale: [en]
---*/
date = new Temporal.PlainDateTime(2000, 5, 2, 14, 46, 0, 0, 0, 0, "gregory");
assert(date.toLocaleString("en", { era: "narrow" }).startsWith("5"), "toLocaleString on a PlainDateTime with era option should work");

View File

@ -0,0 +1,15 @@
// Copyright (C) 2025 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: >
If era option and no other options are provided to toLocaleString,
PlainMonthDay should be foramtted with default options
features: [Temporal]
locale: [en]
---*/
const date = new Temporal.PlainMonthDay(5, 2, "gregory");
assert(date.toLocaleString("en", { era: "narrow" }).startsWith("5"), "toLocaleString on a PlainMonthDay with era option should work");

View File

@ -0,0 +1,18 @@
// Copyright (C) 2025 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: >
If era option and no other options are provided to toLocaleString,
PlainTime should be foramtted with default options
features: [Temporal]
locale: [en]
---*/
const date = new Temporal.PlainTime(14, 46);
const result = date.toLocaleString("en", { era: "narrow" });
assert(result.startsWith("2"), "toLocaleString on a PlainTime with era option should work");
assert(!result.includes("A"), "era should be ignored when formatting a PlainTime");

View File

@ -0,0 +1,15 @@
// Copyright (C) 2025 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: >
If era option and no other options are provided to toLocaleString,
PlainYearMonth should be foramtted with default options
features: [Temporal]
locale: [en]
---*/
const date = new Temporal.PlainYearMonth(2000, 5, "gregory");
assert(date.toLocaleString("en", { era: "narrow" }).startsWith("5"), "toLocaleString on a PlainYearMonth with era option should work");

View File

@ -0,0 +1,15 @@
// Copyright (C) 2025 Igalia, S.L.. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.zoneddatetime.prototype.tolocalestring
description: >
If era option and no other options are provided to toLocaleString,
ZonedDateTime should be foramtted with default options
features: [Temporal]
locale: [en]
---*/
const zdt = new Temporal.ZonedDateTime(0n, "UTC");
assert(zdt.toLocaleString("en", { era: "narrow" }).startsWith("1"), "toLocaleString on a ZonedDateTime with era option should work");