mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-30 06:06:48 +00:00
LibJS: Implement the Temporal.Instant constructor
And the simple Temporal.Instant.prototype getters, so that the constructed Temporal.Instant may actually be validated.
This commit is contained in:
parent
1675f40e29
commit
90820873a2
Notes:
github-actions[bot]
2024-11-25 12:34:28 +00:00
Author: https://github.com/trflynn89
Commit: 90820873a2
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2557
Reviewed-by: https://github.com/shannonbooth ✅
22 changed files with 710 additions and 14 deletions
|
@ -0,0 +1,64 @@
|
|||
describe("correct behavior", () => {
|
||||
test("length is 1", () => {
|
||||
expect(Temporal.Instant.from).toHaveLength(1);
|
||||
});
|
||||
|
||||
test("Instant instance argument", () => {
|
||||
const instant = new Temporal.Instant(123n);
|
||||
expect(Temporal.Instant.from(instant).epochNanoseconds).toBe(123n);
|
||||
});
|
||||
|
||||
test("Instant string argument", () => {
|
||||
expect(Temporal.Instant.from("1975-02-02T14:25:36.123456789Z").epochNanoseconds).toBe(
|
||||
160583136123456789n
|
||||
);
|
||||
// Time zone is not validated
|
||||
expect(
|
||||
Temporal.Instant.from("1975-02-02T14:25:36.123456789Z[Custom/TimeZone]")
|
||||
.epochNanoseconds
|
||||
).toBe(160583136123456789n);
|
||||
|
||||
// Accepts but ignores the calendar.
|
||||
let result = null;
|
||||
expect(() => {
|
||||
result = Temporal.Instant.from("1970-01-01T00:00Z[u-ca=UTC]");
|
||||
}).not.toThrow();
|
||||
expect(result).toBeInstanceOf(Temporal.Instant);
|
||||
expect(result.epochNanoseconds).toBe(0n);
|
||||
|
||||
// Does not validate calendar name, it only checks that the calendar name matches the grammar.
|
||||
result = null;
|
||||
expect(() => {
|
||||
result = Temporal.Instant.from("1970-01-01T00:00Z[u-ca=aAaAaAaA-bBbBbBb]");
|
||||
}).not.toThrow();
|
||||
expect(result).toBeInstanceOf(Temporal.Instant);
|
||||
expect(result.epochNanoseconds).toBe(0n);
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("invalid instant string", () => {
|
||||
expect(() => {
|
||||
Temporal.Instant.from("foo");
|
||||
}).toThrowWithMessage(RangeError, "Invalid ISO date time");
|
||||
});
|
||||
|
||||
test("invalid epoch nanoseconds", () => {
|
||||
// Test cases from https://github.com/tc39/proposal-temporal/commit/baead4d85bc3e9ecab1e9824c3d3fe4fdd77fc3a
|
||||
expect(() => {
|
||||
Temporal.Instant.from("-271821-04-20T00:00:00+00:01");
|
||||
}).toThrowWithMessage(RangeError, "Invalid ISO date");
|
||||
expect(() => {
|
||||
Temporal.Instant.from("+275760-09-13T00:00:00-00:01");
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
"Invalid epoch nanoseconds value, must be in range -86400 * 10^17 to 86400 * 10^17"
|
||||
);
|
||||
});
|
||||
|
||||
test("annotations must match annotation grammar even though they're ignored", () => {
|
||||
expect(() => {
|
||||
Temporal.Instant.from("1970-01-01T00:00Z[SerenityOS=cool]");
|
||||
}).toThrowWithMessage(RangeError, "Invalid ISO date time");
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue