Validate negative offsets

This commit is contained in:
Rene Saarsoo 2020-11-21 21:27:05 +02:00
parent 90e1664ed6
commit 798b05c014
2 changed files with 16 additions and 6 deletions

View File

@ -653,4 +653,15 @@ Interval: 2:00 90%
`), `),
).toThrowErrorMatchingInlineSnapshot(`"Comment offset is larger than interval length at line 5 char 5"`); ).toThrowErrorMatchingInlineSnapshot(`"Comment offset is larger than interval length at line 5 char 5"`);
}); });
it("throws error when negative comment offset is outside of interval", () => {
expect(() =>
parse(`
Name: My Workout
Interval: 2:00 90%
@ 0:00 Find your rythm.
@ -3:10 Try to settle in for the effort
`),
).toThrowErrorMatchingInlineSnapshot(`"Negative comment offset is larger than interval length at line 5 char 5"`);
});
}); });

View File

@ -1,15 +1,14 @@
import { Workout, Interval, Comment } from "../ast"; import { Workout, Interval } from "../ast";
import { ValidationError } from "./ValidationError"; import { ValidationError } from "./ValidationError";
const isCommentWithinInterval = (comment: Comment, interval: Interval): boolean => {
return comment.offset.seconds < interval.duration.seconds;
};
const validateCommentOffsets = (interval: Interval) => { const validateCommentOffsets = (interval: Interval) => {
for (const comment of interval.comments) { for (const comment of interval.comments) {
if (!isCommentWithinInterval(comment, interval)) { if (comment.offset.seconds >= interval.duration.seconds) {
throw new ValidationError(`Comment offset is larger than interval length`, comment.loc); throw new ValidationError(`Comment offset is larger than interval length`, comment.loc);
} }
if (comment.offset.seconds < 0) {
throw new ValidationError(`Negative comment offset is larger than interval length`, comment.loc);
}
} }
}; };