LibJS: Implement stringification Temporal.PlainMonthDay prototypes

This commit is contained in:
Timothy Flynn 2024-11-20 14:57:18 -05:00 committed by Tim Flynn
commit 5bccb36a6f
Notes: github-actions[bot] 2024-11-22 00:25:37 +00:00
14 changed files with 238 additions and 7 deletions

View file

@ -57,10 +57,10 @@ describe("normal behavior", () => {
expect(Object.getPrototypeOf(plainMonthDay)).toBe(Temporal.PlainMonthDay.prototype);
});
// FIXME: Re-implement this test with Temporal.PlainMonthDay.prototype.toString({ calendarName: "always" }).
// test("default reference year is 1972", () => {
// const plainMonthDay = new Temporal.PlainMonthDay(7, 6);
// const fields = plainMonthDay.getISOFields();
// expect(fields.isoYear).toBe(1972);
// });
test("default reference year is 1972", () => {
const plainMonthDay = new Temporal.PlainMonthDay(7, 6);
const fields = plainMonthDay.toString({ calendarName: "always" });
const year = fields.split("-")[0];
expect(year).toBe("1972");
});
});

View file

@ -0,0 +1,23 @@
describe("correct behavior", () => {
test("length is 0", () => {
expect(Temporal.PlainMonthDay.prototype.toJSON).toHaveLength(0);
});
test("basic functionality", () => {
let plainMonthDay;
plainMonthDay = new Temporal.PlainMonthDay(7, 6);
expect(plainMonthDay.toJSON()).toBe("07-06");
plainMonthDay = new Temporal.PlainMonthDay(7, 6, "gregory", 2021);
expect(plainMonthDay.toJSON()).toBe("2021-07-06[u-ca=gregory]");
});
});
describe("errors", () => {
test("this value must be a Temporal.PlainMonthDay object", () => {
expect(() => {
Temporal.PlainMonthDay.prototype.toJSON.call("foo");
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainMonthDay");
});
});

View file

@ -0,0 +1,23 @@
describe("correct behavior", () => {
test("length is 0", () => {
expect(Temporal.PlainMonthDay.prototype.toLocaleString).toHaveLength(0);
});
test("basic functionality", () => {
let plainMonthDay;
plainMonthDay = new Temporal.PlainMonthDay(7, 6);
expect(plainMonthDay.toLocaleString()).toBe("07-06");
plainMonthDay = new Temporal.PlainMonthDay(7, 6, "gregory", 2021);
expect(plainMonthDay.toLocaleString()).toBe("2021-07-06[u-ca=gregory]");
});
});
describe("errors", () => {
test("this value must be a Temporal.PlainMonthDay object", () => {
expect(() => {
Temporal.PlainMonthDay.prototype.toLocaleString.call("foo");
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainMonthDay");
});
});

View file

@ -0,0 +1,42 @@
describe("correct behavior", () => {
test("length is 0", () => {
expect(Temporal.PlainMonthDay.prototype.toString).toHaveLength(0);
});
test("basic functionality", () => {
let plainMonthDay;
plainMonthDay = new Temporal.PlainMonthDay(7, 6);
expect(plainMonthDay.toString()).toBe("07-06");
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, "gregory", 2021);
expect(plainMonthDay.toString()).toBe("2021-07-06[u-ca=gregory]");
expect(plainMonthDay.toString({ calendarName: "auto" })).toBe("2021-07-06[u-ca=gregory]");
expect(plainMonthDay.toString({ calendarName: "always" })).toBe("2021-07-06[u-ca=gregory]");
expect(plainMonthDay.toString({ calendarName: "never" })).toBe("2021-07-06");
expect(plainMonthDay.toString({ calendarName: "critical" })).toBe(
"2021-07-06[!u-ca=gregory]"
);
});
});
describe("errors", () => {
test("this value must be a Temporal.PlainMonthDay object", () => {
expect(() => {
Temporal.PlainMonthDay.prototype.toString.call("foo");
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainMonthDay");
});
test("calendarName option must be one of 'auto', 'always', 'never', 'critical'", () => {
const plainMonthDay = new Temporal.PlainMonthDay(7, 6);
expect(() => {
plainMonthDay.toString({ calendarName: "foo" });
}).toThrowWithMessage(RangeError, "foo is not a valid value for option calendarName");
});
});