LibJS: Implement the Temporal.PlainTime constructor

And the simple Temporal.PlainTime.prototype getters, so that the
constructed Temporal.PlainTime may actually be validated.
This commit is contained in:
Timothy Flynn 2024-11-23 10:21:21 -05:00 committed by Tim Flynn
commit 66365fef57
Notes: github-actions[bot] 2024-11-24 00:38:03 +00:00
22 changed files with 693 additions and 1 deletions

View file

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

View file

@ -0,0 +1,67 @@
describe("correct behavior", () => {
test("length is 1", () => {
expect(Temporal.PlainTime.from).toHaveLength(1);
});
test("PlainTime instance argument", () => {
const plainTime = new Temporal.PlainTime(18, 45, 37, 1, 2, 3);
const createdPlainTime = Temporal.PlainTime.from(plainTime);
expect(createdPlainTime.hour).toBe(18);
expect(createdPlainTime.minute).toBe(45);
expect(createdPlainTime.second).toBe(37);
expect(createdPlainTime.millisecond).toBe(1);
expect(createdPlainTime.microsecond).toBe(2);
expect(createdPlainTime.nanosecond).toBe(3);
});
test("PlainTime string argument", () => {
const createdPlainTime = Temporal.PlainTime.from("2021-08-27T18:44:11");
expect(createdPlainTime.hour).toBe(18);
expect(createdPlainTime.minute).toBe(44);
expect(createdPlainTime.second).toBe(11);
expect(createdPlainTime.millisecond).toBe(0);
expect(createdPlainTime.microsecond).toBe(0);
expect(createdPlainTime.nanosecond).toBe(0);
});
});
describe("errors", () => {
test("string must not contain a UTC designator", () => {
expect(() => {
Temporal.PlainTime.from("2021-07-06T23:42:01Z");
}).toThrowWithMessage(RangeError, "Invalid ISO date time");
});
test("extended year must not be negative zero", () => {
expect(() => {
Temporal.PlainTime.from("-000000-01-01T00:00:00");
}).toThrowWithMessage(RangeError, "Invalid ISO date time");
expect(() => {
Temporal.PlainTime.from("000000-01-01T00:00:00"); // U+2212
}).toThrowWithMessage(RangeError, "Invalid ISO date time");
});
test("ambiguous string must contain a time designator", () => {
const values = [
// YYYY-MM or HHMM-UU
"2021-12",
// MMDD or HHMM
"1214",
"0229",
"1130",
// MM-DD or HH-UU
"12-14",
// YYYYMM or HHMMSS
"202112",
];
for (const value of values) {
expect(() => {
Temporal.PlainTime.from(value);
}).toThrowWithMessage(RangeError, "Invalid ISO date time");
expect(() => {
Temporal.PlainTime.from(`T${value}`);
}).not.toThrow();
}
});
});

View file

@ -0,0 +1,50 @@
describe("errors", () => {
test("called without new", () => {
expect(() => {
Temporal.PlainTime();
}).toThrowWithMessage(
TypeError,
"Temporal.PlainTime constructor must be called with 'new'"
);
});
test("cannot pass Infinity", () => {
for (let i = 0; i < 6; ++i) {
const args = Array(6).fill(0);
args[i] = Infinity;
expect(() => {
new Temporal.PlainTime(...args);
}).toThrowWithMessage(RangeError, "Invalid plain time");
args[i] = -Infinity;
expect(() => {
new Temporal.PlainTime(...args);
}).toThrowWithMessage(RangeError, "Invalid plain time");
}
});
test("cannot pass invalid ISO time", () => {
const badValues = [24, 60, 60, 1000, 1000, 1000];
for (let i = 0; i < 6; ++i) {
const args = [0, 0, 0, 0, 0, 0];
args[i] = badValues[i];
expect(() => {
new Temporal.PlainTime(...args);
}).toThrowWithMessage(RangeError, "Invalid plain time");
}
});
});
describe("normal behavior", () => {
test("length is 0", () => {
expect(Temporal.PlainTime).toHaveLength(0);
});
test("basic functionality", () => {
const plainTime = new Temporal.PlainTime(19, 46, 32, 123, 456, 789);
expect(typeof plainTime).toBe("object");
expect(plainTime).toBeInstanceOf(Temporal.PlainTime);
expect(Object.getPrototypeOf(plainTime)).toBe(Temporal.PlainTime.prototype);
});
});

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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