LibJS: Implement the Temporal.Duration constructor

This also includes a stubbed Temporal.Duration.prototype.

Until we have re-implemented Temporal.PlainDate/ZonedDateTime, some of
Temporal.Duration.compare (and its invoked AOs) are left unimplemented.
This commit is contained in:
Timothy Flynn 2024-11-18 11:58:51 -05:00 committed by Tim Flynn
commit 5fe0d3352d
Notes: github-actions[bot] 2024-11-21 00:06:22 +00:00
30 changed files with 2143 additions and 26 deletions

View file

@ -0,0 +1,95 @@
describe("correct behavior", () => {
test("length is 2", () => {
expect(Temporal.Duration.compare).toHaveLength(2);
});
function checkCommonResults(duration1, duration2) {
expect(Temporal.Duration.compare(duration1, duration1)).toBe(0);
expect(Temporal.Duration.compare(duration2, duration2)).toBe(0);
expect(Temporal.Duration.compare(duration1, duration2)).toBe(-1);
expect(Temporal.Duration.compare(duration2, duration1)).toBe(1);
}
test("basic functionality", () => {
const duration1 = new Temporal.Duration(0, 0, 0, 1);
const duration2 = new Temporal.Duration(0, 0, 0, 2);
checkCommonResults(duration1, duration2);
});
test("duration-like objects", () => {
const duration1 = { years: 0, months: 0, weeks: 0, days: 1 };
const duration2 = { years: 0, months: 0, weeks: 0, days: 2 };
checkCommonResults(duration1, duration2);
});
test("duration strings", () => {
const duration1 = "P1D";
const duration2 = "P2D";
checkCommonResults(duration1, duration2);
});
});
describe("errors", () => {
test("invalid duration-like object", () => {
expect(() => {
Temporal.Duration.compare({});
}).toThrowWithMessage(TypeError, "Invalid duration-like object");
expect(() => {
Temporal.Duration.compare({ years: 0, months: 0, weeks: 0, days: 1 }, {});
}).toThrowWithMessage(TypeError, "Invalid duration-like object");
});
test("relativeTo is required for comparing calendar units (year, month, week)", () => {
const duration1 = new Temporal.Duration(1);
const duration2 = new Temporal.Duration(2);
expect(() => {
Temporal.Duration.compare(duration1, duration2);
}).toThrowWithMessage(
RangeError,
"A starting point is required for comparing calendar units"
);
const duration3 = new Temporal.Duration(0, 3);
const duration4 = new Temporal.Duration(0, 4);
expect(() => {
Temporal.Duration.compare(duration3, duration4);
}).toThrowWithMessage(
RangeError,
"A starting point is required for comparing calendar units"
);
const duration5 = new Temporal.Duration(0, 0, 5);
const duration6 = new Temporal.Duration(0, 0, 6);
expect(() => {
Temporal.Duration.compare(duration5, duration6);
}).toThrowWithMessage(
RangeError,
"A starting point is required for comparing calendar units"
);
// Still throws if year/month/week of one the duration objects is non-zero.
const duration7 = new Temporal.Duration(0, 0, 0, 7);
const duration8 = new Temporal.Duration(0, 0, 8);
expect(() => {
Temporal.Duration.compare(duration7, duration8);
}).toThrowWithMessage(
RangeError,
"A starting point is required for comparing calendar units"
);
const duration9 = new Temporal.Duration(0, 0, 9);
const duration10 = new Temporal.Duration(0, 0, 0, 10);
expect(() => {
Temporal.Duration.compare(duration9, duration10);
}).toThrowWithMessage(
RangeError,
"A starting point is required for comparing calendar units"
);
});
});

View file

@ -0,0 +1,132 @@
const expectDurationOneToTen = duration => {
expect(duration.years).toBe(1);
expect(duration.months).toBe(2);
expect(duration.weeks).toBe(3);
expect(duration.days).toBe(4);
expect(duration.hours).toBe(5);
expect(duration.minutes).toBe(6);
expect(duration.seconds).toBe(7);
expect(duration.milliseconds).toBe(8);
expect(duration.microseconds).toBe(9);
expect(duration.nanoseconds).toBe(10);
};
describe("correct behavior", () => {
test("length is 1", () => {
expect(Temporal.Duration.from).toHaveLength(1);
});
test("Duration instance argument", () => {
const duration = Temporal.Duration.from(
new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
);
expectDurationOneToTen(duration);
});
test("Duration-like object argument", () => {
const duration = Temporal.Duration.from({
years: 1,
months: 2,
weeks: 3,
days: 4,
hours: 5,
minutes: 6,
seconds: 7,
milliseconds: 8,
microseconds: 9,
nanoseconds: 10,
});
expectDurationOneToTen(duration);
});
test("Duration string argument", () => {
const duration = Temporal.Duration.from("P1Y2M3W4DT5H6M7.008009010S");
expectDurationOneToTen(duration);
});
});
describe("errors", () => {
test("Invalid duration-like object", () => {
expect(() => {
Temporal.Duration.from({});
}).toThrowWithMessage(TypeError, "Invalid duration-like object");
});
test("Invalid duration property value", () => {
expect(() => {
Temporal.Duration.from({ years: 1.23 });
}).toThrowWithMessage(
RangeError,
"Invalid value for duration property 'years': must be an integer, got 1.2" // ...29999999999999 - let's not include that in the test :^)
);
expect(() => {
Temporal.Duration.from({ years: "foo" });
}).toThrowWithMessage(
RangeError,
"Invalid value for duration property 'years': must be an integer, got foo"
);
expect(() => {
Temporal.Duration.from({ years: NaN });
}).toThrowWithMessage(
RangeError,
"Invalid value for duration property 'years': must be an integer, got NaN"
);
});
test("invalid duration string", () => {
expect(() => {
Temporal.Duration.from("foo");
}).toThrowWithMessage(RangeError, "Invalid duration string 'foo'");
});
test("invalid duration string: fractional hours proceeded by minutes or seconds", () => {
const values = [
"PT1.23H1M",
"PT1.23H1.23M",
"PT1.23H1S",
"PT1.23H1.23S",
"PT1.23H1M1S",
"PT1.23H1M1.23S",
"PT1.23H1.23M1S",
"PT1.23H1.23M1.23S",
];
for (const value of values) {
expect(() => {
Temporal.Duration.from(value);
}).toThrowWithMessage(RangeError, `Invalid duration string '${value}'`);
}
});
test("invalid duration string: fractional minutes proceeded by seconds", () => {
const values = ["PT1.23M1S", "PT1.23M1.23S"];
for (const value of values) {
expect(() => {
Temporal.Duration.from(value);
}).toThrowWithMessage(RangeError, `Invalid duration string '${value}'`);
}
});
test("invalid duration string: exceed duration limits", () => {
const values = [
"P4294967296Y", // abs(years) >= 2**32
"P4294967296M", // abs(months) >= 2**32
"P4294967296W", // abs(weeks) >= 2**32
"P104249991375D", // days >= 2*53 seconds
"PT2501999792984H", // hours >= 2*53 seconds
"PT150119987579017M", // minutes >= 2*53 seconds
"PT9007199254740992S", // seconds >= 2*53 seconds
];
for (const value of values) {
expect(() => {
Temporal.Duration.from(value);
}).toThrowWithMessage(RangeError, `Invalid duration`);
expect(() => {
Temporal.Duration.from("-" + value);
}).toThrowWithMessage(RangeError, `Invalid duration`);
}
});
});

View file

@ -0,0 +1,35 @@
describe("errors", () => {
test("called without new", () => {
expect(() => {
Temporal.Duration();
}).toThrowWithMessage(TypeError, "Temporal.Duration constructor must be called with 'new'");
});
test("cannot mix arguments with different signs", () => {
expect(() => {
new Temporal.Duration(-1, 1);
}).toThrowWithMessage(RangeError, "Invalid duration");
expect(() => {
new Temporal.Duration(1, -1);
}).toThrowWithMessage(RangeError, "Invalid duration");
});
test("cannot pass Infinity", () => {
expect(() => {
new Temporal.Duration(Infinity);
}).toThrowWithMessage(RangeError, "Invalid duration");
});
});
describe("normal behavior", () => {
test("length is 0", () => {
expect(Temporal.Duration).toHaveLength(0);
});
test("basic functionality", () => {
const duration = new Temporal.Duration();
expect(typeof duration).toBe("object");
expect(duration).toBeInstanceOf(Temporal.Duration);
expect(Object.getPrototypeOf(duration)).toBe(Temporal.Duration.prototype);
});
});

View file

@ -0,0 +1,3 @@
test("basic functionality", () => {
expect(Temporal.Duration.prototype[Symbol.toStringTag]).toBe("Temporal.Duration");
});

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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