From 9012a389179c37691635793cbf4997709bb7150c Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Sat, 26 Dec 2020 17:48:29 +0200 Subject: [PATCH] Check that comments don't extend past interval end --- src/parser/parser.test.ts | 66 +++++++++++++++++++++++++++++++++++++++ src/parser/validate.ts | 3 ++ 2 files changed, 69 insertions(+) diff --git a/src/parser/parser.test.ts b/src/parser/parser.test.ts index 61c81e9..49ee07b 100644 --- a/src/parser/parser.test.ts +++ b/src/parser/parser.test.ts @@ -762,4 +762,70 @@ Interval: 2:00 90% } `); }); + + it("throws error when comment does not finish before end of interval", () => { + expect(() => + parse(` +Name: My Workout +Interval: 1:00 80% + @ 0:51 First comment +Interval: 1:00 90% +`), + ).toThrowErrorMatchingInlineSnapshot( + `"Less than 10 seconds between comment start and interval end at line 4 char 5"`, + ); + }); + + it("triggers no error when comment finishes right at interval end", () => { + expect( + parse(` +Name: My Workout +Interval: 1:00 80% + @ 0:50 First comment +Interval: 1:00 90% +`), + ).toMatchInlineSnapshot(` + Object { + "author": "", + "description": "", + "intervals": Array [ + Object { + "cadence": undefined, + "comments": Array [ + Object { + "loc": Object { + "col": 4, + "row": 3, + }, + "offset": Duration { + "seconds": 50, + }, + "text": "First comment", + }, + ], + "duration": Duration { + "seconds": 60, + }, + "intensity": ConstantIntensity { + "_value": 0.8, + }, + "type": "Interval", + }, + Object { + "cadence": undefined, + "comments": Array [], + "duration": Duration { + "seconds": 60, + }, + "intensity": ConstantIntensity { + "_value": 0.9, + }, + "type": "Interval", + }, + ], + "name": "My Workout", + "tags": Array [], + } + `); + }); }); diff --git a/src/parser/validate.ts b/src/parser/validate.ts index bf4a543..57bb8b9 100644 --- a/src/parser/validate.ts +++ b/src/parser/validate.ts @@ -16,6 +16,9 @@ const validateCommentOffsets = ({ comments, duration }: Interval) => { if (i > 0 && comment.offset.seconds < comments[i - 1].offset.seconds + 10) { throw new ValidationError(`Less than 10 seconds between comments`, comment.loc); } + if (comment.offset.seconds + 10 > duration.seconds) { + throw new ValidationError(`Less than 10 seconds between comment start and interval end`, comment.loc); + } } };