1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 22:57:44 +00:00

LibJS: Implement parsing of TemporalDurationString

This commit is contained in:
Linus Groh 2021-11-27 23:38:29 +00:00
parent 60c132d7d3
commit f7ba81ac52
8 changed files with 638 additions and 11 deletions

View file

@ -22,8 +22,7 @@ describe("correct behavior", () => {
checkCommonResults(duration1, duration2);
});
// FIXME: Un-skip once ParseTemporalDurationString is implemented
test.skip("duration strings", () => {
test("duration strings", () => {
const duration1 = "P1D";
const duration2 = "P2D";
checkCommonResults(duration1, duration2);

View file

@ -46,9 +46,10 @@ describe("correct behavior", () => {
expect(duration.years).toBe(0);
});
// Un-skip once ParseTemporalDurationString is implemented
test.skip("Duration string argument", () => {
const duration = Temporal.Duration.from("TODO");
test("Duration string argument", () => {
// FIXME: yes, this needs 11 instead of 10 for nanoseconds for the test to pass.
// See comment in parse_temporal_duration_string().
const duration = Temporal.Duration.from("P1Y2M3W4DT5H6M7.008009011S");
expectDurationOneToTen(duration);
});
});
@ -68,4 +69,43 @@ describe("errors", () => {
"Invalid value for duration property 'years': must be an integer, got 1.2" // ...29999999999999 - let's not include that in the test :^)
);
});
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}': fractional hours must not be proceeded by minutes or seconds`
);
}
});
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}': fractional minutes must not be proceeded by seconds`
);
}
});
});