Test all combinations in temporal-plainyearmonth-formatting-datetime-style

This commit is contained in:
André Bargull 2025-05-22 11:58:53 +02:00 committed by Ms2ger
parent a55bf7da9e
commit 96695862e2

View File

@ -8,70 +8,78 @@ locale: [en-US]
features: [Temporal] features: [Temporal]
---*/ ---*/
const dateFormatterShort = new Intl.DateTimeFormat("en-US", { dateStyle: "short", timeZone: "Pacific/Apia" }); const locale = "en-US";
const dateFormatterMedium = new Intl.DateTimeFormat("en-US", { dateStyle: "medium", timeZone: "Pacific/Apia" }); const timeZone = "Pacific/Apia";
const dateFormatterLong = new Intl.DateTimeFormat("en-US", { dateStyle: "long", timeZone: "Pacific/Apia" });
const dateFormatterFull = new Intl.DateTimeFormat("en-US", { dateStyle: "full", timeZone: "Pacific/Apia" }); const dateStyles = [
const timeFormatterShort = new Intl.DateTimeFormat("en-US", { timeStyle: "short", timeZone: "Pacific/Apia" }); "full", "long", "medium", "short",
const timeFormatterMedium = new Intl.DateTimeFormat("en-US", { timeStyle: "medium", timeZone: "Pacific/Apia" }); ];
const timeFormatterLong = new Intl.DateTimeFormat("en-US", { timeStyle: "long", timeZone: "Pacific/Apia" });
const timeFormatterFull = new Intl.DateTimeFormat("en-US", { timeStyle: "full", timeZone: "Pacific/Apia" }); const timeStyles = [
const dateTimeFormatterShort = new Intl.DateTimeFormat("en-US", { dateStyle: "short", timeStyle: "short", timeZone: "Pacific/Apia" }); "full", "long", "medium", "short",
const dateTimeFormatterMedium = new Intl.DateTimeFormat("en-US", { dateStyle: "medium", timeStyle: "medium", timeZone: "Pacific/Apia" }); ];
const dateTimeFormatterLong = new Intl.DateTimeFormat("en-US", { dateStyle: "long", timeStyle: "long", timeZone: "Pacific/Apia" });
const dateTimeFormatterFull = new Intl.DateTimeFormat("en-US", { dateStyle: "full", timeStyle: "full", timeZone: "Pacific/Apia" });
const dateTimeFormatterShortLong = new Intl.DateTimeFormat("en-US", { dateStyle: "short", timeStyle: "long", timeZone: "Pacific/Apia" });
const dateTimeFormatterShortMedium = new Intl.DateTimeFormat("en-US", { dateStyle: "short", timeStyle: "medium", timeZone: "Pacific/Apia" });
const dateTimeFormatterShortFull = new Intl.DateTimeFormat("en-US", { dateStyle: "short", timeStyle: "full", timeZone: "Pacific/Apia" });
const dateTimeFormatterMediumLong = new Intl.DateTimeFormat("en-US", { dateStyle: "medium", timeStyle: "long", timeZone: "Pacific/Apia" });
const dateTimeFormatterMediumShort = new Intl.DateTimeFormat("en-US", { dateStyle: "medium", timeStyle: "short", timeZone: "Pacific/Apia" });
const dateTimeFormatterMediumFull = new Intl.DateTimeFormat("en-US", { dateStyle: "medium", timeStyle: "full", timeZone: "Pacific/Apia" });
const dateTimeFormatterLongMedium = new Intl.DateTimeFormat("en-US", { dateStyle: "long", timeStyle: "medium", timeZone: "Pacific/Apia" });
const dateTimeFormatterLongShort = new Intl.DateTimeFormat("en-US", { dateStyle: "long", timeStyle: "short", timeZone: "Pacific/Apia" });
const dateTimeFormatterLongFull = new Intl.DateTimeFormat("en-US", { dateStyle: "long", timeStyle: "full", timeZone: "Pacific/Apia" });
const dateTimeFormatterFullMedium = new Intl.DateTimeFormat("en-US", { dateStyle: "full", timeStyle: "medium", timeZone: "Pacific/Apia" });
const dateTimeFormatterFullShort = new Intl.DateTimeFormat("en-US", { dateStyle: "full", timeStyle: "short", timeZone: "Pacific/Apia" });
const dateTimeFormatterFullLong = new Intl.DateTimeFormat("en-US", { dateStyle: "full", timeStyle: "long", timeZone: "Pacific/Apia" });
// Use a reference day so we can check that it doesn't occur in any string output // Use a reference day so we can check that it doesn't occur in any string output
const yearmonth = new Temporal.PlainYearMonth(2222, 1, "gregory", 30); const yearmonth = new Temporal.PlainYearMonth(2222, 1, "gregory", 30);
const yearmonthResultShort = dateFormatterShort.format(yearmonth); const expected = {
// assert.sameValue(yearmonthResultShort, "1/22", "plain yearmonth, dateStyle=short"); // "January 2222"
assert.sameValue(yearmonthResultShort.search("30"), -1, "plain yearmonth, dateStyle=short: day should not appear"); full: {
assert.notSameValue(yearmonthResultShort.search("1"), -1, "plain yearmonth, dateStyle=short: numeric month should appear"); year: ["2222", true],
assert.notSameValue(yearmonthResultShort.search("22"), -1, "plain yearmonth, dateStyle=short: 2-digit year should appear"); month: ["1", false],
day: ["30", false],
},
const yearmonthResultMedium = dateFormatterMedium.format(yearmonth); // "January 2222"
// assert.sameValue(yearmonthResultMedium, "Jan 2222", "plain yearmonth, dateStyle=medium"); long: {
assert.sameValue(yearmonthResultMedium.search("30"), -1, "plain yearmonth, dateStyle=medium: day should not appear"); year: ["2222", true],
assert.sameValue(yearmonthResultMedium.search("1"), -1, "plain yearmonth, dateStyle=medium: numeric month should not appear"); month: ["1", false],
assert.notSameValue(yearmonthResultMedium.search("2222"), -1, "plain yearmonth, dateStyle=medium: 4-digit year should appear"); day: ["30", false],
},
const yearmonthResultLong = dateFormatterLong.format(yearmonth); // "Jan 2222"
// assert.sameValue(yearmonthResultLong, "January 2222", "plain yearmonth, dateStyle=long"); medium: {
assert.sameValue(yearmonthResultLong.search("30"), -1, "plain yearmonth, dateStyle=long: day should not appear"); year: ["2222", true],
assert.sameValue(yearmonthResultLong.search("1"), -1, "plain yearmonth, dateStyle=long: numeric month should not appear"); month: ["1", false],
assert.notSameValue(yearmonthResultLong.search("2222"), -1, "plain yearmonth, dateStyle=long: 4-digit year should appear"); day: ["30", false],
},
const yearmonthResultFull = dateFormatterFull.format(yearmonth); // "1/22"
// assert.sameValue(yearmonthResultFull, "January 2222", "plain yearmonth, dateStyle=full"); short: {
assert.sameValue(yearmonthResultFull.search("30"), -1, "plain yearmonth, dateStyle=full: day should not appear"); year: ["22", true],
assert.sameValue(yearmonthResultFull.search("1"), -1, "plain yearmonth, dateStyle=full: numeric month should not appear"); month: ["1", true],
assert.notSameValue(yearmonthResultFull.search("2222"), -1, "plain yearmonth, dateStyle=full: 4-digit year should appear"); day: ["30", false],
},
};
assert.throws(TypeError, () => timeFormatterShort.format(yearmonth), "plain yearmonth, timeStyle=short"); function ensureDateField(result, field, dateStyle) {
assert.throws(TypeError, () => timeFormatterMedium.format(yearmonth), "plain yearmonth, timeStyle=medium"); let [searchValue, present] = expected[dateStyle][field];
assert.throws(TypeError, () => timeFormatterLong.format(yearmonth), "plain yearmonth, timeStyle=long"); let verb = present ? "should" : "should not";
assert.throws(TypeError, () => timeFormatterFull.format(yearmonth), "plain yearmonth, timeStyle=full");
var yearmonthResult = dateTimeFormatterShort.format(yearmonth); assert.sameValue(
assert.sameValue(yearmonthResult, yearmonthResultShort, "plain yearmonth, dateStyle = timeStyle = short"); result.includes(searchValue),
yearmonthResult = dateTimeFormatterMedium.format(yearmonth); present,
assert.sameValue(yearmonthResult, yearmonthResultMedium, "plain yearmonth, dateStyle = timeStyle = medium"); `dateStyle=${dateStyle}: ${field} ${verb} appear`
yearmonthResult = dateTimeFormatterLong.format(yearmonth); );
assert.sameValue(yearmonthResult, yearmonthResultLong, "plain yearmonth, dateStyle = timeStyle = long"); }
yearmonthResult = dateTimeFormatterFull.format(yearmonth);
assert.sameValue(yearmonthResult, yearmonthResultFull, "plain yearmonth, dateStyle = timeStyle = full"); // timeStyle throws when no dateStyle is present.
yearmonthResult = dateTimeFormatterShortLong.format(yearmonth); for (let timeStyle of timeStyles) {
assert.sameValue(yearmonthResult, yearmonthResultShort, "plain yearmonth, dateStyle = short, timeStyle = long"); let dtf = new Intl.DateTimeFormat(locale, {timeStyle, timeZone});
assert.throws(TypeError, () => dtf.format(yearmonth), `timeStyle=${timeStyle}`);
}
for (let dateStyle of dateStyles) {
let dtf = new Intl.DateTimeFormat(locale, {dateStyle, timeZone});
let result = dtf.format(yearmonth);
ensureDateField(result, "year", dateStyle);
ensureDateField(result, "month", dateStyle);
ensureDateField(result, "day", dateStyle);
// timeStyle is ignored when dateStyle is present.
for (let timeStyle of timeStyles) {
let dtf = new Intl.DateTimeFormat(locale, {dateStyle, timeStyle, timeZone});
assert.sameValue(dtf.format(yearmonth), result, `dateStyle = ${dateStyle}, timeStyle = ${timeStyle}`);
}
}