diff --git a/src/parser/parser.test.ts b/src/parser/parser.test.ts index 81cad6a..9366816 100644 --- a/src/parser/parser.test.ts +++ b/src/parser/parser.test.ts @@ -1,14 +1,13 @@ import { parse } from "."; describe("Parser", () => { - it("parses empty workout file", () => { - expect(parse("")).toMatchInlineSnapshot(` - Object { - "author": "", - "description": "", - "intervals": Array [], - "name": "", - } - `); + it("throws error for empty file", () => { + expect(() => parse("")).toThrowErrorMatchingInlineSnapshot( + `"Workout is missing a name. Use \`Name:\` to declare one."` + ); + + expect(() => parse(" \n \n \t")).toThrowErrorMatchingInlineSnapshot( + `"Workout is missing a name. Use \`Name:\` to declare one."` + ); }); }); diff --git a/src/parser/parser.ts b/src/parser/parser.ts index 356c13a..465df76 100644 --- a/src/parser/parser.ts +++ b/src/parser/parser.ts @@ -107,8 +107,13 @@ const parseIntervals = (tokens: Token[]): Interval[] => { export const parseTokens = (tokens: Token[]): Workout => { const [header, intervalTokens] = parseHeader(tokens); + + if (header.name === undefined) { + throw new Error("Workout is missing a name. Use `Name:` to declare one."); + } + return { - name: header.name || "", + name: header.name, author: header.author || "", description: header.description || "", intervals: parseIntervals(intervalTokens),