From 24f6f7a0149d78e27fc989e46911d32a19079b92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Bargull?= Date: Thu, 22 May 2025 11:58:30 +0200 Subject: [PATCH] Test all combinations in temporal-plaindate-formatting-datetime-style --- ...ral-plaindate-formatting-datetime-style.js | 122 ++++++++++-------- 1 file changed, 71 insertions(+), 51 deletions(-) diff --git a/test/intl402/DateTimeFormat/prototype/format/temporal-plaindate-formatting-datetime-style.js b/test/intl402/DateTimeFormat/prototype/format/temporal-plaindate-formatting-datetime-style.js index f29dc98a34..7d6c497c05 100644 --- a/test/intl402/DateTimeFormat/prototype/format/temporal-plaindate-formatting-datetime-style.js +++ b/test/intl402/DateTimeFormat/prototype/format/temporal-plaindate-formatting-datetime-style.js @@ -8,57 +8,77 @@ locale: [en-US] features: [Temporal] ---*/ -const dateFormatterShort = new Intl.DateTimeFormat("en-US", { dateStyle: "short", timeZone: "Pacific/Apia" }); -const dateFormatterMedium = new Intl.DateTimeFormat("en-US", { dateStyle: "medium", 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 timeFormatterShort = new Intl.DateTimeFormat("en-US", { timeStyle: "short", timeZone: "Pacific/Apia" }); -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 dateTimeFormatterShort = new Intl.DateTimeFormat("en-US", { dateStyle: "short", timeStyle: "short", timeZone: "Pacific/Apia" }); -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" }); +const locale = "en-US"; +const timeZone = "Pacific/Apia"; + +const dateStyles = [ + "full", "long", "medium", "short", +]; + +const timeStyles = [ + "full", "long", "medium", "short", +]; const date = new Temporal.PlainDate(2021, 8, 4); -const dateShort = dateFormatterShort.format(date); -/* - To avoid requiring an exact format for the output of locale-dependent functions, - don't run these tests, but they're left here as documentation for what the output - should roughly look like. -*/ -// assert.sameValue(dateShort, "8/4/21", "plain date, dateStyle=short"); -const dateMedium = dateFormatterMedium.format(date); -// assert.sameValue(dateMedium, "Aug 4, 2021", "plain date, dateStyle=medium"); -const dateLong = dateFormatterLong.format(date); -// assert.sameValue(dateLong, "August 4, 2021", "plain date, dateStyle=long"); -const dateFull = dateFormatterFull.format(date); -// assert.sameValue(dateFull, "Wednesday, August 4, 2021", "plain date, dateStyle=full"); -assert.throws(TypeError, () => timeFormatterShort.format(date), "plain date, timeStyle=short"); -assert.throws(TypeError, () => timeFormatterMedium.format(date), "plain date, timeStyle=medium"); -assert.throws(TypeError, () => timeFormatterLong.format(date), "plain date, timeStyle=long"); -assert.throws(TypeError, () => timeFormatterFull.format(date), "plain date, timeStyle=full"); -var result = dateTimeFormatterShort.format(date); -assert.sameValue(result, dateShort, "plain date, dateStyle = timeStyle = short"); -result = dateTimeFormatterMedium.format(date); -assert.sameValue(result, dateMedium, "plain date, dateStyle = timeStyle = medium"); -result = dateTimeFormatterLong.format(date); -assert.sameValue(result, dateLong, "plain date, dateStyle = timeStyle = long"); -result = dateTimeFormatterFull.format(date); -assert.sameValue(result, dateFull, "plain date, dateStyle = timeStyle = full"); -result = dateTimeFormatterShortLong.format(date); -assert.sameValue(result, dateShort, "plain date, dateStyle = short, timeStyle = long"); +const expected = { + // "Wednesday, August 4, 2021" + full: { + year: ["2021", true], + month: ["8", false], + day: ["4", true], + }, + + // "August 4, 2021" + long: { + year: ["2021", true], + month: ["8", false], + day: ["4", true], + }, + + // "Aug 4, 2021" + medium: { + year: ["2021", true], + month: ["8", false], + day: ["4", true], + }, + + // "8/4/21" + short: { + year: ["21", true], + month: ["8", true], + day: ["4", true], + }, +}; + +function ensureDateField(result, field, dateStyle) { + let [searchValue, present] = expected[dateStyle][field]; + let verb = present ? "should" : "should not"; + + assert.sameValue( + result.includes(searchValue), + present, + `dateStyle=${dateStyle}: ${field} ${verb} appear` + ); +} + +// timeStyle throws when no dateStyle is present. +for (let timeStyle of timeStyles) { + let dtf = new Intl.DateTimeFormat(locale, {timeStyle, timeZone}); + assert.throws(TypeError, () => dtf.format(date), `timeStyle=${timeStyle}`); +} + +for (let dateStyle of dateStyles) { + let dtf = new Intl.DateTimeFormat(locale, {dateStyle, timeZone}); + let result = dtf.format(date); + + 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(date), result, `dateStyle = ${dateStyle}, timeStyle = ${timeStyle}`); + } +}