LibJS: Implement Temporal.PlainDate.prototype.with/withCalendar

This commit is contained in:
Timothy Flynn 2024-11-22 11:06:13 -05:00 committed by Andreas Kling
commit f280a96e35
Notes: github-actions[bot] 2024-11-23 13:48:04 +00:00
4 changed files with 127 additions and 0 deletions

View file

@ -0,0 +1,55 @@
describe("correct behavior", () => {
test("length is 1", () => {
expect(Temporal.PlainDate.prototype.with).toHaveLength(1);
});
test("basic functionality", () => {
const plainDate = new Temporal.PlainDate(1970, 1, 1);
const values = [
[{ year: 2021 }, new Temporal.PlainDate(2021, 1, 1)],
[{ year: 2021, month: 7 }, new Temporal.PlainDate(2021, 7, 1)],
[{ year: 2021, month: 7, day: 6 }, new Temporal.PlainDate(2021, 7, 6)],
[{ year: 2021, monthCode: "M07", day: 6 }, new Temporal.PlainDate(2021, 7, 6)],
];
for (const [arg, expected] of values) {
expect(plainDate.with(arg).equals(expected)).toBeTrue();
}
// Supplying the same values doesn't change the date, but still creates a new object
const plainDateLike = { year: plainDate.year, month: plainDate.month, day: plainDate.day };
expect(plainDate.with(plainDateLike)).not.toBe(plainDate);
expect(plainDate.with(plainDateLike).equals(plainDate)).toBeTrue();
});
});
describe("errors", () => {
test("this value must be a Temporal.PlainDate object", () => {
expect(() => {
Temporal.PlainDate.prototype.with.call("foo");
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainDate");
});
test("argument must be an object", () => {
expect(() => {
new Temporal.PlainDate(1970, 1, 1).with("foo");
}).toThrowWithMessage(TypeError, "Object must be a partial Temporal object");
expect(() => {
new Temporal.PlainDate(1970, 1, 1).with(42);
}).toThrowWithMessage(TypeError, "Object must be a partial Temporal object");
});
test("argument must have one of 'day', 'month', 'monthCode', 'year'", () => {
expect(() => {
new Temporal.PlainDate(1970, 1, 1).with({});
}).toThrowWithMessage(TypeError, "Object must be a partial Temporal object");
});
test("argument must not have 'calendar' or 'timeZone'", () => {
expect(() => {
new Temporal.PlainDate(1970, 1, 1).with({ calendar: {} });
}).toThrowWithMessage(TypeError, "Object must be a partial Temporal object");
expect(() => {
new Temporal.PlainDate(1970, 1, 1).with({ timeZone: {} });
}).toThrowWithMessage(TypeError, "Object must be a partial Temporal object");
});
});

View file

@ -0,0 +1,13 @@
describe("correct behavior", () => {
test("length is 1", () => {
expect(Temporal.PlainDate.prototype.withCalendar).toHaveLength(1);
});
test("basic functionality", () => {
const calendar = "gregory";
const firstPlainDate = new Temporal.PlainDate(1, 1, 1);
expect(firstPlainDate.calendarId).not.toBe(calendar);
const secondPlainDate = firstPlainDate.withCalendar(calendar);
expect(secondPlainDate.calendarId).toBe(calendar);
});
});