1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 23:07:35 +00:00

LibJS: Add calendarName: "critical" option to toString() methods

This is a normative change in the Temporal spec.

See: e715a50
This commit is contained in:
Luke Wilde 2022-11-02 19:24:47 +00:00 committed by Linus Groh
parent 192aa75279
commit 4a167cfbec
9 changed files with 73 additions and 15 deletions

View file

@ -11,12 +11,14 @@ describe("correct behavior", () => {
expect(plainDate.toString({ calendarName: "auto" })).toBe("2021-07-06");
expect(plainDate.toString({ calendarName: "always" })).toBe("2021-07-06[u-ca=iso8601]");
expect(plainDate.toString({ calendarName: "never" })).toBe("2021-07-06");
expect(plainDate.toString({ calendarName: "critical" })).toBe("2021-07-06[!u-ca=iso8601]");
plainDate = new Temporal.PlainDate(2021, 7, 6, { toString: () => "foo" });
expect(plainDate.toString()).toBe("2021-07-06[u-ca=foo]");
expect(plainDate.toString({ calendarName: "auto" })).toBe("2021-07-06[u-ca=foo]");
expect(plainDate.toString({ calendarName: "always" })).toBe("2021-07-06[u-ca=foo]");
expect(plainDate.toString({ calendarName: "never" })).toBe("2021-07-06");
expect(plainDate.toString({ calendarName: "critical" })).toBe("2021-07-06[!u-ca=foo]");
plainDate = new Temporal.PlainDate(0, 1, 1);
expect(plainDate.toString()).toBe("0000-01-01");
@ -62,7 +64,7 @@ describe("errors", () => {
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainDate");
});
test("calendarName option must be one of 'auto', 'always', 'never'", () => {
test("calendarName option must be one of 'auto', 'always', 'never', 'critical'", () => {
const plainDate = new Temporal.PlainDate(2021, 7, 6);
expect(() => {
plainDate.toString({ calendarName: "foo" });

View file

@ -80,6 +80,21 @@ describe("correct behavior", () => {
expect(plainDateTime.toString(options)).toBe("2022-08-08T14:38:40.1002003");
expect(calledToString).toBeFalse();
});
test("calendarName option", () => {
const plainDateTime = new Temporal.PlainDateTime(2022, 11, 2, 19, 4, 35, 100, 200, 300);
const values = [
["auto", "2022-11-02T19:04:35.1002003"],
["always", "2022-11-02T19:04:35.1002003[u-ca=iso8601]"],
["never", "2022-11-02T19:04:35.1002003"],
["critical", "2022-11-02T19:04:35.1002003[!u-ca=iso8601]"],
];
for (const [calendarName, expected] of values) {
const options = { calendarName };
expect(plainDateTime.toString(options)).toBe(expected);
}
});
});
describe("errors", () => {
@ -88,4 +103,11 @@ describe("errors", () => {
Temporal.PlainDateTime.prototype.toString.call("foo");
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainDateTime");
});
test("calendarName option must be one of 'auto', 'always', 'never', 'critical'", () => {
const plainDateTime = new Temporal.PlainDateTime(2022, 11, 2, 19, 5, 40, 100, 200, 300);
expect(() => {
plainDateTime.toString({ calendarName: "foo" });
}).toThrowWithMessage(RangeError, "foo is not a valid value for option calendarName");
});
});

View file

@ -11,12 +11,16 @@ describe("correct behavior", () => {
expect(plainMonthDay.toString({ calendarName: "auto" })).toBe("07-06");
expect(plainMonthDay.toString({ calendarName: "always" })).toBe("1972-07-06[u-ca=iso8601]");
expect(plainMonthDay.toString({ calendarName: "never" })).toBe("07-06");
expect(plainMonthDay.toString({ calendarName: "critical" })).toBe(
"1972-07-06[!u-ca=iso8601]"
);
plainMonthDay = new Temporal.PlainMonthDay(7, 6, { toString: () => "foo" }, 2021);
expect(plainMonthDay.toString()).toBe("2021-07-06[u-ca=foo]");
expect(plainMonthDay.toString({ calendarName: "auto" })).toBe("2021-07-06[u-ca=foo]");
expect(plainMonthDay.toString({ calendarName: "always" })).toBe("2021-07-06[u-ca=foo]");
expect(plainMonthDay.toString({ calendarName: "never" })).toBe("2021-07-06");
expect(plainMonthDay.toString({ calendarName: "critical" })).toBe("2021-07-06[!u-ca=foo]");
});
});
@ -27,7 +31,7 @@ describe("errors", () => {
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainMonthDay");
});
test("calendarName option must be one of 'auto', 'always', 'never'", () => {
test("calendarName option must be one of 'auto', 'always', 'never', 'critical'", () => {
const plainMonthDay = new Temporal.PlainMonthDay(7, 6);
expect(() => {
plainMonthDay.toString({ calendarName: "foo" });

View file

@ -13,12 +13,16 @@ describe("correct behavior", () => {
"2021-07-01[u-ca=iso8601]"
);
expect(plainYearMonth.toString({ calendarName: "never" })).toBe("2021-07");
expect(plainYearMonth.toString({ calendarName: "critical" })).toBe(
"2021-07-01[!u-ca=iso8601]"
);
plainYearMonth = new Temporal.PlainYearMonth(2021, 7, { toString: () => "foo" }, 6);
expect(plainYearMonth.toString()).toBe("2021-07-06[u-ca=foo]");
expect(plainYearMonth.toString({ calendarName: "auto" })).toBe("2021-07-06[u-ca=foo]");
expect(plainYearMonth.toString({ calendarName: "always" })).toBe("2021-07-06[u-ca=foo]");
expect(plainYearMonth.toString({ calendarName: "never" })).toBe("2021-07-06");
expect(plainYearMonth.toString({ calendarName: "critical" })).toBe("2021-07-06[!u-ca=foo]");
plainYearMonth = new Temporal.PlainYearMonth(0, 1);
expect(plainYearMonth.toString()).toBe("0000-01");
@ -47,7 +51,7 @@ describe("errors", () => {
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainYearMonth");
});
test("calendarName option must be one of 'auto', 'always', 'never'", () => {
test("calendarName option must be one of 'auto', 'always', 'never', 'critical'", () => {
const plainYearMonth = new Temporal.PlainYearMonth(2021, 7);
expect(() => {
plainYearMonth.toString({ calendarName: "foo" });

View file

@ -125,6 +125,23 @@ describe("correct behavior", () => {
expect(zonedDateTime.toString(options)).toBe("2022-08-08T14:38:40.1002003+00:00[UTC]");
expect(calledToString).toBeFalse();
});
test("calendarName option", () => {
const plainDateTime = new Temporal.PlainDateTime(2022, 11, 2, 19, 4, 35, 100, 200, 300);
const timeZone = new Temporal.TimeZone("UTC");
const zonedDateTime = plainDateTime.toZonedDateTime(timeZone);
const values = [
["auto", "2022-11-02T19:04:35.1002003+00:00[UTC]"],
["always", "2022-11-02T19:04:35.1002003+00:00[UTC][u-ca=iso8601]"],
["never", "2022-11-02T19:04:35.1002003+00:00[UTC]"],
["critical", "2022-11-02T19:04:35.1002003+00:00[UTC][!u-ca=iso8601]"],
];
for (const [calendarName, expected] of values) {
const options = { calendarName };
expect(zonedDateTime.toString(options)).toBe(expected);
}
});
});
describe("errors", () => {
@ -140,4 +157,11 @@ describe("errors", () => {
zonedDateTime.toString();
}).toThrowWithMessage(TypeError, "null is not a function");
});
test("calendarName option must be one of 'auto', 'always', 'never', 'critical'", () => {
const zonedDateTime = new Temporal.ZonedDateTime(0n, "UTC");
expect(() => {
zonedDateTime.toString({ calendarName: "foo" });
}).toThrowWithMessage(RangeError, "foo is not a valid value for option calendarName");
});
});