LibJS: Implement the Temporal.PlainYearMonth constructor

And the simple Temporal.PlainYearMonth.prototype getters, so that the
constructed Temporal.PlainYearMonth may actually be validated.
This commit is contained in:
Timothy Flynn 2024-11-21 11:22:32 -05:00 committed by Andreas Kling
commit b68d67693e
Notes: github-actions[bot] 2024-11-22 18:56:56 +00:00
27 changed files with 926 additions and 3 deletions

View file

@ -0,0 +1,14 @@
describe("correct behavior", () => {
test("length is 2", () => {
expect(Temporal.PlainYearMonth.compare).toHaveLength(2);
});
test("basic functionality", () => {
const plainYearMonth1 = new Temporal.PlainYearMonth(2021, 8);
expect(Temporal.PlainYearMonth.compare(plainYearMonth1, plainYearMonth1)).toBe(0);
const plainYearMonth2 = new Temporal.PlainYearMonth(2021, 9);
expect(Temporal.PlainYearMonth.compare(plainYearMonth2, plainYearMonth2)).toBe(0);
expect(Temporal.PlainYearMonth.compare(plainYearMonth1, plainYearMonth2)).toBe(-1);
expect(Temporal.PlainYearMonth.compare(plainYearMonth2, plainYearMonth1)).toBe(1);
});
});

View file

@ -0,0 +1,130 @@
describe("correct behavior", () => {
test("length is 1", () => {
expect(Temporal.PlainYearMonth.from).toHaveLength(1);
});
test("PlainYearMonth instance argument", () => {
const plainYearMonth_ = new Temporal.PlainYearMonth(2021, 7);
const plainYearMonth = Temporal.PlainYearMonth.from(plainYearMonth_);
expect(plainYearMonth.year).toBe(2021);
expect(plainYearMonth.month).toBe(7);
expect(plainYearMonth.monthCode).toBe("M07");
expect(plainYearMonth.daysInYear).toBe(365);
expect(plainYearMonth.daysInMonth).toBe(31);
expect(plainYearMonth.monthsInYear).toBe(12);
expect(plainYearMonth.inLeapYear).toBeFalse();
});
test("fields object argument", () => {
const object = {
year: 2021,
month: 7,
};
const plainYearMonth = Temporal.PlainYearMonth.from(object);
expect(plainYearMonth.year).toBe(2021);
expect(plainYearMonth.month).toBe(7);
expect(plainYearMonth.monthCode).toBe("M07");
expect(plainYearMonth.daysInYear).toBe(365);
expect(plainYearMonth.daysInMonth).toBe(31);
expect(plainYearMonth.monthsInYear).toBe(12);
expect(plainYearMonth.inLeapYear).toBeFalse();
});
test("from year month string", () => {
const plainYearMonth = Temporal.PlainYearMonth.from("2021-07");
expect(plainYearMonth.year).toBe(2021);
expect(plainYearMonth.month).toBe(7);
expect(plainYearMonth.monthCode).toBe("M07");
});
test("from date time string", () => {
const plainYearMonth = Temporal.PlainYearMonth.from("2021-07-06T23:42:01");
expect(plainYearMonth.year).toBe(2021);
expect(plainYearMonth.month).toBe(7);
expect(plainYearMonth.monthCode).toBe("M07");
expect(plainYearMonth.daysInYear).toBe(365);
expect(plainYearMonth.daysInMonth).toBe(31);
expect(plainYearMonth.monthsInYear).toBe(12);
expect(plainYearMonth.inLeapYear).toBeFalse();
});
test("compares calendar name in year month string in lowercase", () => {
const values = [
"2023-02[u-ca=iso8601]",
"2023-02[u-ca=isO8601]",
"2023-02[u-ca=iSo8601]",
"2023-02[u-ca=iSO8601]",
"2023-02[u-ca=Iso8601]",
"2023-02[u-ca=IsO8601]",
"2023-02[u-ca=ISo8601]",
"2023-02[u-ca=ISO8601]",
];
for (const value of values) {
expect(() => {
Temporal.PlainYearMonth.from(value);
}).not.toThrowWithMessage(
RangeError,
"YYYY-MM string format can only be used with the iso8601 calendar"
);
}
});
});
describe("errors", () => {
test("missing fields", () => {
expect(() => {
Temporal.PlainYearMonth.from({});
}).toThrowWithMessage(TypeError, "Required property year is missing or undefined");
expect(() => {
Temporal.PlainYearMonth.from({ year: 0 });
}).toThrowWithMessage(TypeError, "Required property month is missing or undefined");
expect(() => {
Temporal.PlainYearMonth.from({ month: 1 });
}).toThrowWithMessage(TypeError, "Required property year is missing or undefined");
});
test("invalid year month string", () => {
expect(() => {
Temporal.PlainYearMonth.from("foo");
}).toThrowWithMessage(RangeError, "Invalid ISO date time");
});
test("string must not contain a UTC designator", () => {
expect(() => {
Temporal.PlainYearMonth.from("2021-07-06T23:42:01Z");
}).toThrowWithMessage(RangeError, "Invalid ISO date time");
});
test("extended year must not be negative zero", () => {
expect(() => {
Temporal.PlainYearMonth.from("-000000-01");
}).toThrowWithMessage(RangeError, "Invalid ISO date time");
expect(() => {
Temporal.PlainYearMonth.from("000000-01"); // U+2212
}).toThrowWithMessage(RangeError, "Invalid ISO date time");
});
test("can only use iso8601 calendar with year month strings", () => {
expect(() => {
Temporal.PlainYearMonth.from("2023-02[u-ca=iso8602]");
}).toThrowWithMessage(RangeError, "Invalid calendar identifier 'iso8602'");
expect(() => {
Temporal.PlainYearMonth.from("2023-02[u-ca=ladybird]");
}).toThrowWithMessage(RangeError, "Invalid calendar identifier 'ladybird'");
});
test("doesn't throw non-iso8601 calendar error when using a superset format string such as DateTime", () => {
// NOTE: This will still throw, but only because "ladybird" is not a recognised calendar, not because of the string format restriction.
try {
Temporal.PlainYearMonth.from("2023-02-10T22:57[u-ca=ladybird]");
} catch (e) {
expect(e).toBeInstanceOf(RangeError);
expect(e.message).not.toBe(
"MM-DD string format can only be used with the iso8601 calendar"
);
expect(e.message).toBe("Invalid calendar identifier 'ladybird'");
}
});
});

View file

@ -0,0 +1,60 @@
describe("errors", () => {
test("called without new", () => {
expect(() => {
Temporal.PlainYearMonth();
}).toThrowWithMessage(
TypeError,
"Temporal.PlainYearMonth constructor must be called with 'new'"
);
});
test("cannot pass Infinity", () => {
expect(() => {
new Temporal.PlainYearMonth(Infinity);
}).toThrowWithMessage(RangeError, "Invalid plain year month");
expect(() => {
new Temporal.PlainYearMonth(0, Infinity);
}).toThrowWithMessage(RangeError, "Invalid plain year month");
expect(() => {
new Temporal.PlainYearMonth(0, 1, undefined, Infinity);
}).toThrowWithMessage(RangeError, "Invalid plain year month");
expect(() => {
new Temporal.PlainYearMonth(-Infinity);
}).toThrowWithMessage(RangeError, "Invalid plain year month");
expect(() => {
new Temporal.PlainYearMonth(0, -Infinity);
}).toThrowWithMessage(RangeError, "Invalid plain year month");
expect(() => {
new Temporal.PlainYearMonth(0, 1, undefined, -Infinity);
}).toThrowWithMessage(RangeError, "Invalid plain year month");
});
test("cannot pass invalid ISO month/day", () => {
expect(() => {
new Temporal.PlainYearMonth(0, 0);
}).toThrowWithMessage(RangeError, "Invalid plain year month");
expect(() => {
new Temporal.PlainYearMonth(0, 1, undefined, 0);
}).toThrowWithMessage(RangeError, "Invalid plain year month");
});
});
describe("normal behavior", () => {
test("length is 2", () => {
expect(Temporal.PlainYearMonth).toHaveLength(2);
});
test("basic functionality", () => {
const plainYearMonth = new Temporal.PlainYearMonth(2021, 7);
expect(typeof plainYearMonth).toBe("object");
expect(plainYearMonth).toBeInstanceOf(Temporal.PlainYearMonth);
expect(Object.getPrototypeOf(plainYearMonth)).toBe(Temporal.PlainYearMonth.prototype);
});
// FIXME: Re-implement this test with Temporal.PlainYearMonth.prototype.toString({ calendarName: "always" }).
// test("default reference day is 1", () => {
// const plainYearMonth = new Temporal.PlainYearMonth(2021, 7);
// const fields = plainYearMonth.getISOFields();
// expect(fields.isoDay).toBe(1);
// });
});

View file

@ -0,0 +1,15 @@
describe("correct behavior", () => {
test("calendarId basic functionality", () => {
const calendar = "iso8601";
const plainYearMonth = new Temporal.PlainYearMonth(2000, 5, calendar);
expect(plainYearMonth.calendarId).toBe("iso8601");
});
});
describe("errors", () => {
test("this value must be a Temporal.PlainYearMonth object", () => {
expect(() => {
Reflect.get(Temporal.PlainYearMonth.prototype, "calendarId", "foo");
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainYearMonth");
});
});

View file

@ -0,0 +1,14 @@
describe("correct behavior", () => {
test("basic functionality", () => {
const plainYearMonth = new Temporal.PlainYearMonth(2021, 7);
expect(plainYearMonth.daysInMonth).toBe(31);
});
});
describe("errors", () => {
test("this value must be a Temporal.PlainYearMonth object", () => {
expect(() => {
Reflect.get(Temporal.PlainYearMonth.prototype, "daysInMonth", "foo");
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainYearMonth");
});
});

View file

@ -0,0 +1,14 @@
describe("correct behavior", () => {
test("basic functionality", () => {
const plainYearMonth = new Temporal.PlainYearMonth(2021, 7);
expect(plainYearMonth.daysInYear).toBe(365);
});
});
describe("errors", () => {
test("this value must be a Temporal.PlainYearMonth object", () => {
expect(() => {
Reflect.get(Temporal.PlainYearMonth.prototype, "daysInYear", "foo");
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainYearMonth");
});
});

View file

@ -0,0 +1,14 @@
describe("correct behavior", () => {
test("basic functionality", () => {
const plainYearMonth = new Temporal.PlainYearMonth(2021, 7);
expect(plainYearMonth.era).toBeUndefined();
});
});
describe("errors", () => {
test("this value must be a Temporal.PlainYearMonth object", () => {
expect(() => {
Reflect.get(Temporal.PlainYearMonth.prototype, "era", "foo");
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainYearMonth");
});
});

View file

@ -0,0 +1,14 @@
describe("correct behavior", () => {
test("basic functionality", () => {
const plainYearMonth = new Temporal.PlainYearMonth(2021, 7);
expect(plainYearMonth.eraYear).toBeUndefined();
});
});
describe("errors", () => {
test("this value must be a Temporal.PlainYearMonth object", () => {
expect(() => {
Reflect.get(Temporal.PlainYearMonth.prototype, "eraYear", "foo");
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainYearMonth");
});
});

View file

@ -0,0 +1,14 @@
describe("correct behavior", () => {
test("basic functionality", () => {
const plainYearMonth = new Temporal.PlainYearMonth(2021, 7);
expect(plainYearMonth.inLeapYear).toBe(false);
});
});
describe("errors", () => {
test("this value must be a Temporal.PlainYearMonth object", () => {
expect(() => {
Reflect.get(Temporal.PlainYearMonth.prototype, "inLeapYear", "foo");
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainYearMonth");
});
});

View file

@ -0,0 +1,14 @@
describe("correct behavior", () => {
test("basic functionality", () => {
const plainYearMonth = new Temporal.PlainYearMonth(2021, 7);
expect(plainYearMonth.month).toBe(7);
});
});
describe("errors", () => {
test("this value must be a Temporal.PlainYearMonth object", () => {
expect(() => {
Reflect.get(Temporal.PlainYearMonth.prototype, "month", "foo");
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainYearMonth");
});
});

View file

@ -0,0 +1,14 @@
describe("correct behavior", () => {
test("basic functionality", () => {
const plainYearMonth = new Temporal.PlainYearMonth(2021, 7);
expect(plainYearMonth.monthCode).toBe("M07");
});
});
describe("errors", () => {
test("this value must be a Temporal.PlainYearMonth object", () => {
expect(() => {
Reflect.get(Temporal.PlainYearMonth.prototype, "monthCode", "foo");
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainYearMonth");
});
});

View file

@ -0,0 +1,14 @@
describe("correct behavior", () => {
test("basic functionality", () => {
const plainYearMonth = new Temporal.PlainYearMonth(2021, 7);
expect(plainYearMonth.monthsInYear).toBe(12);
});
});
describe("errors", () => {
test("this value must be a Temporal.PlainYearMonth object", () => {
expect(() => {
Reflect.get(Temporal.PlainYearMonth.prototype, "monthsInYear", "foo");
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainYearMonth");
});
});

View file

@ -0,0 +1,14 @@
describe("correct behavior", () => {
test("basic functionality", () => {
const plainYearMonth = new Temporal.PlainYearMonth(2021, 7);
expect(plainYearMonth.year).toBe(2021);
});
});
describe("errors", () => {
test("this value must be a Temporal.PlainYearMonth object", () => {
expect(() => {
Reflect.get(Temporal.PlainYearMonth.prototype, "year", "foo");
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainYearMonth");
});
});