LibJS: Implement the Temporal.PlainDateTime constructor

And the simple Temporal.PlainDateTime.prototype getters, so that the
constructed Temporal.PlainDateTime may actually be validated.
This commit is contained in:
Timothy Flynn 2024-11-23 18:00:05 -05:00 committed by Andreas Kling
commit 029b6ad1a8
Notes: github-actions[bot] 2024-11-24 10:45:50 +00:00
41 changed files with 1210 additions and 13 deletions

View file

@ -0,0 +1,13 @@
describe("correct behavior", () => {
test("length is 2", () => {
expect(Temporal.PlainDateTime.compare).toHaveLength(2);
});
test("basic functionality", () => {
const plainDateTime1 = new Temporal.PlainDateTime(2021, 8, 27, 16, 38, 40, 1, 2, 3);
expect(Temporal.PlainDateTime.compare(plainDateTime1, plainDateTime1)).toBe(0);
const plainDateTime2 = new Temporal.PlainDateTime(2021, 8, 27, 16, 39, 5, 0, 1, 2);
expect(Temporal.PlainDateTime.compare(plainDateTime1, plainDateTime2)).toBe(-1);
expect(Temporal.PlainDateTime.compare(plainDateTime2, plainDateTime1)).toBe(1);
});
});

View file

@ -0,0 +1,171 @@
describe("correct behavior", () => {
test("length is 1", () => {
expect(Temporal.PlainDateTime.from).toHaveLength(1);
});
test("PlainDate instance argument", () => {
const plainDate = new Temporal.PlainDate(2021, 7, 6);
const plainDateTime = Temporal.PlainDateTime.from(plainDate);
expect(plainDateTime.year).toBe(2021);
expect(plainDateTime.month).toBe(7);
expect(plainDateTime.day).toBe(6);
expect(plainDateTime.hour).toBe(0);
expect(plainDateTime.minute).toBe(0);
expect(plainDateTime.second).toBe(0);
expect(plainDateTime.millisecond).toBe(0);
expect(plainDateTime.microsecond).toBe(0);
expect(plainDateTime.nanosecond).toBe(0);
});
test("PlainDateTime instance argument", () => {
const plainDateTime_ = new Temporal.PlainDateTime(2021, 7, 6, 18, 14, 47);
const plainDateTime = Temporal.PlainDateTime.from(plainDateTime_);
expect(plainDateTime).not.toBe(plainDateTime_);
expect(plainDateTime.year).toBe(2021);
expect(plainDateTime.month).toBe(7);
expect(plainDateTime.day).toBe(6);
expect(plainDateTime.hour).toBe(18);
expect(plainDateTime.minute).toBe(14);
expect(plainDateTime.second).toBe(47);
expect(plainDateTime.millisecond).toBe(0);
expect(plainDateTime.microsecond).toBe(0);
expect(plainDateTime.nanosecond).toBe(0);
});
test("fields object argument", () => {
const object = {
year: 2021,
month: 7,
day: 6,
hour: 23,
minute: 42,
second: 1,
millisecond: 0,
microsecond: 0,
nanosecond: 0,
};
const plainDateTime = Temporal.PlainDateTime.from(object);
expect(plainDateTime.year).toBe(2021);
expect(plainDateTime.month).toBe(7);
expect(plainDateTime.day).toBe(6);
expect(plainDateTime.hour).toBe(23);
expect(plainDateTime.minute).toBe(42);
expect(plainDateTime.second).toBe(1);
expect(plainDateTime.millisecond).toBe(0);
expect(plainDateTime.microsecond).toBe(0);
expect(plainDateTime.nanosecond).toBe(0);
});
test("with 'constrain' overflow option", () => {
const object = {
year: 0,
month: 1,
day: 1,
hour: 24,
minute: 60,
second: 60,
millisecond: 1000,
microsecond: 1000,
nanosecond: 1000,
};
const plainDateTime = Temporal.PlainDateTime.from(object, { overflow: "constrain" });
expect(plainDateTime.year).toBe(0);
expect(plainDateTime.month).toBe(1);
expect(plainDateTime.day).toBe(1);
expect(plainDateTime.hour).toBe(23);
expect(plainDateTime.minute).toBe(59);
expect(plainDateTime.second).toBe(59);
expect(plainDateTime.millisecond).toBe(999);
expect(plainDateTime.microsecond).toBe(999);
expect(plainDateTime.nanosecond).toBe(999);
});
test("PlainDateTime string argument", () => {
const plainDateTime = Temporal.PlainDateTime.from("2021-07-06T23:42:01");
expect(plainDateTime.year).toBe(2021);
expect(plainDateTime.month).toBe(7);
expect(plainDateTime.day).toBe(6);
expect(plainDateTime.hour).toBe(23);
expect(plainDateTime.minute).toBe(42);
expect(plainDateTime.second).toBe(1);
expect(plainDateTime.millisecond).toBe(0);
expect(plainDateTime.microsecond).toBe(0);
expect(plainDateTime.nanosecond).toBe(0);
});
});
describe("errors", () => {
test("missing fields", () => {
expect(() => {
Temporal.PlainDateTime.from({ year: 0, month: 1 });
}).toThrowWithMessage(TypeError, "Required property day is missing or undefined");
expect(() => {
Temporal.PlainDateTime.from({ year: 0, day: 1 });
}).toThrowWithMessage(TypeError, "Required property month is missing or undefined");
expect(() => {
Temporal.PlainDateTime.from({ month: 1, day: 1 });
}).toThrowWithMessage(TypeError, "Required property year is missing or undefined");
});
test("with 'reject' overflow option", () => {
const values = [
[{ year: 1234567, month: 1, day: 1 }, "Invalid ISO date"],
[{ year: 0, month: 13, day: 1 }, "Invalid ISO date"],
[{ year: 0, month: 1, day: 32 }, "Invalid ISO date"],
[{ year: 0, month: 1, day: 1, hour: 24 }, "Invalid plain time"],
[{ year: 0, month: 1, day: 1, hour: 0, minute: 60 }, "Invalid plain time"],
[{ year: 0, month: 1, day: 1, hour: 0, minute: 0, second: 60 }, "Invalid plain time"],
[
{ year: 0, month: 1, day: 1, hour: 0, minute: 0, second: 0, millisecond: 1000 },
"Invalid plain time",
],
[
{
year: 0,
month: 1,
day: 1,
hour: 0,
minute: 0,
second: 0,
millisecond: 0,
microsecond: 1000,
},
"Invalid plain time",
],
[
{
year: 0,
month: 1,
day: 1,
hour: 0,
minute: 0,
second: 0,
millisecond: 0,
microsecond: 0,
nanosecond: 1000,
},
"Invalid plain time",
],
];
for (const [object, error] of values) {
expect(() => {
Temporal.PlainDateTime.from(object, { overflow: "reject" });
}).toThrowWithMessage(RangeError, error);
}
});
test("string must not contain a UTC designator", () => {
expect(() => {
Temporal.PlainDateTime.from("2021-07-06T23:42:01Z");
}).toThrowWithMessage(RangeError, "Invalid ISO date time");
});
test("extended year must not be negative zero", () => {
expect(() => {
Temporal.PlainDateTime.from("-000000-01-01");
}).toThrowWithMessage(RangeError, "Invalid ISO date time");
expect(() => {
Temporal.PlainDateTime.from("000000-01-01"); // U+2212
}).toThrowWithMessage(RangeError, "Invalid ISO date time");
});
});

View file

@ -0,0 +1,53 @@
describe("errors", () => {
test("called without new", () => {
expect(() => {
Temporal.PlainDateTime();
}).toThrowWithMessage(
TypeError,
"Temporal.PlainDateTime constructor must be called with 'new'"
);
});
test("cannot pass Infinity", () => {
for (let i = 0; i < 9; ++i) {
const args = Array(9).fill(0);
args[i] = Infinity;
expect(() => {
new Temporal.PlainDateTime(...args);
}).toThrowWithMessage(RangeError, "Invalid plain date time");
args[i] = -Infinity;
expect(() => {
new Temporal.PlainDateTime(...args);
}).toThrowWithMessage(RangeError, "Invalid plain date time");
}
});
test("cannot pass invalid ISO date or time", () => {
// NOTE: The year max value is 3 more than in the polyfill, but they incorrectly seem to
// use ISOYearMonthWithinLimits, which AFAICT isn't used for PlainDate{,Time} in the spec.
// ¯\_(ツ)_/¯
const badValues = [275764, 0, 0, 24, 60, 60, 1000, 1000, 1000];
for (let i = 0; i < 9; ++i) {
const args = [0, 1, 1, 0, 0, 0, 0, 0, 0];
args[i] = badValues[i];
expect(() => {
new Temporal.PlainDateTime(...args);
}).toThrowWithMessage(RangeError, "Invalid plain date time");
}
});
});
describe("normal behavior", () => {
test("length is 3", () => {
expect(Temporal.PlainDateTime).toHaveLength(3);
});
test("basic functionality", () => {
const plainDateTime = new Temporal.PlainDateTime(2021, 7, 22, 19, 46, 32, 123, 456, 789);
expect(typeof plainDateTime).toBe("object");
expect(plainDateTime).toBeInstanceOf(Temporal.PlainDateTime);
expect(Object.getPrototypeOf(plainDateTime)).toBe(Temporal.PlainDateTime.prototype);
});
});

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -0,0 +1,14 @@
describe("correct behavior", () => {
test("basic functionality", () => {
const plainDateTime = new Temporal.PlainDateTime(2021, 7, 30, 1, 4, 32, 111, 420, 963);
expect(plainDateTime.nanosecond).toBe(963);
});
});
describe("errors", () => {
test("this value must be a Temporal.PlainDateTime object", () => {
expect(() => {
Reflect.get(Temporal.PlainDateTime.prototype, "nanosecond", "foo");
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainDateTime");
});
});

View file

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

View file

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

View file

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

View file

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

View file

@ -9,7 +9,7 @@ const PLAIN_TIME_PROPERTIES = [
const REJECTED_CALENDAR_TYPES_THREE_ARGUMENTS = [
Temporal.PlainDate,
// Temporal.PlainDateTime,
Temporal.PlainDateTime,
Temporal.PlainTime,
];