1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-16 08:34:58 +00:00
serenity/Userland/Libraries/LibJS/Tests/builtins/Temporal/Duration/Duration.from.js
Linus Groh 5c77885873 LibJS: Implement Temporal.Duration.from()
...with ParseTemporalDurationString currently TODO()'d.
2021-07-19 00:34:28 +01:00

70 lines
2.2 KiB
JavaScript

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);
});
// Un-skip once ParseTemporalDurationString is implemented
test.skip("Duration string argument", () => {
const duration = Temporal.Duration.from("TODO");
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 NaN"
);
});
});