mirror of
https://github.com/tc39/test262.git
synced 2025-07-30 09:24:41 +02:00
Test all combinations in temporal-plainyearmonth-formatting-datetime-style
This commit is contained in:
parent
a55bf7da9e
commit
96695862e2
@ -8,70 +8,78 @@ 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",
|
||||
];
|
||||
|
||||
// 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 yearmonthResultShort = dateFormatterShort.format(yearmonth);
|
||||
// assert.sameValue(yearmonthResultShort, "1/22", "plain yearmonth, dateStyle=short");
|
||||
assert.sameValue(yearmonthResultShort.search("30"), -1, "plain yearmonth, dateStyle=short: day should not appear");
|
||||
assert.notSameValue(yearmonthResultShort.search("1"), -1, "plain yearmonth, dateStyle=short: numeric month should appear");
|
||||
assert.notSameValue(yearmonthResultShort.search("22"), -1, "plain yearmonth, dateStyle=short: 2-digit year should appear");
|
||||
const expected = {
|
||||
// "January 2222"
|
||||
full: {
|
||||
year: ["2222", true],
|
||||
month: ["1", false],
|
||||
day: ["30", false],
|
||||
},
|
||||
|
||||
const yearmonthResultMedium = dateFormatterMedium.format(yearmonth);
|
||||
// assert.sameValue(yearmonthResultMedium, "Jan 2222", "plain yearmonth, dateStyle=medium");
|
||||
assert.sameValue(yearmonthResultMedium.search("30"), -1, "plain yearmonth, dateStyle=medium: day should not appear");
|
||||
assert.sameValue(yearmonthResultMedium.search("1"), -1, "plain yearmonth, dateStyle=medium: numeric month should not appear");
|
||||
assert.notSameValue(yearmonthResultMedium.search("2222"), -1, "plain yearmonth, dateStyle=medium: 4-digit year should appear");
|
||||
// "January 2222"
|
||||
long: {
|
||||
year: ["2222", true],
|
||||
month: ["1", false],
|
||||
day: ["30", false],
|
||||
},
|
||||
|
||||
const yearmonthResultLong = dateFormatterLong.format(yearmonth);
|
||||
// assert.sameValue(yearmonthResultLong, "January 2222", "plain yearmonth, dateStyle=long");
|
||||
assert.sameValue(yearmonthResultLong.search("30"), -1, "plain yearmonth, dateStyle=long: day should not appear");
|
||||
assert.sameValue(yearmonthResultLong.search("1"), -1, "plain yearmonth, dateStyle=long: numeric month should not appear");
|
||||
assert.notSameValue(yearmonthResultLong.search("2222"), -1, "plain yearmonth, dateStyle=long: 4-digit year should appear");
|
||||
// "Jan 2222"
|
||||
medium: {
|
||||
year: ["2222", true],
|
||||
month: ["1", false],
|
||||
day: ["30", false],
|
||||
},
|
||||
|
||||
const yearmonthResultFull = dateFormatterFull.format(yearmonth);
|
||||
// assert.sameValue(yearmonthResultFull, "January 2222", "plain yearmonth, dateStyle=full");
|
||||
assert.sameValue(yearmonthResultFull.search("30"), -1, "plain yearmonth, dateStyle=full: day should not appear");
|
||||
assert.sameValue(yearmonthResultFull.search("1"), -1, "plain yearmonth, dateStyle=full: numeric month should not appear");
|
||||
assert.notSameValue(yearmonthResultFull.search("2222"), -1, "plain yearmonth, dateStyle=full: 4-digit year should appear");
|
||||
// "1/22"
|
||||
short: {
|
||||
year: ["22", true],
|
||||
month: ["1", true],
|
||||
day: ["30", false],
|
||||
},
|
||||
};
|
||||
|
||||
assert.throws(TypeError, () => timeFormatterShort.format(yearmonth), "plain yearmonth, timeStyle=short");
|
||||
assert.throws(TypeError, () => timeFormatterMedium.format(yearmonth), "plain yearmonth, timeStyle=medium");
|
||||
assert.throws(TypeError, () => timeFormatterLong.format(yearmonth), "plain yearmonth, timeStyle=long");
|
||||
assert.throws(TypeError, () => timeFormatterFull.format(yearmonth), "plain yearmonth, timeStyle=full");
|
||||
function ensureDateField(result, field, dateStyle) {
|
||||
let [searchValue, present] = expected[dateStyle][field];
|
||||
let verb = present ? "should" : "should not";
|
||||
|
||||
var yearmonthResult = dateTimeFormatterShort.format(yearmonth);
|
||||
assert.sameValue(yearmonthResult, yearmonthResultShort, "plain yearmonth, dateStyle = timeStyle = short");
|
||||
yearmonthResult = dateTimeFormatterMedium.format(yearmonth);
|
||||
assert.sameValue(yearmonthResult, yearmonthResultMedium, "plain yearmonth, dateStyle = timeStyle = medium");
|
||||
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");
|
||||
yearmonthResult = dateTimeFormatterShortLong.format(yearmonth);
|
||||
assert.sameValue(yearmonthResult, yearmonthResultShort, "plain yearmonth, dateStyle = short, timeStyle = long");
|
||||
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(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}`);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user