Require workouts to have a name

This commit is contained in:
Rene Saarsoo 2020-09-21 16:24:56 +03:00
parent 07bb22bcf2
commit 8e813276ad
2 changed files with 14 additions and 10 deletions

View File

@ -1,14 +1,13 @@
import { parse } from "."; import { parse } from ".";
describe("Parser", () => { describe("Parser", () => {
it("parses empty workout file", () => { it("throws error for empty file", () => {
expect(parse("")).toMatchInlineSnapshot(` expect(() => parse("")).toThrowErrorMatchingInlineSnapshot(
Object { `"Workout is missing a name. Use \`Name:\` to declare one."`
"author": "", );
"description": "",
"intervals": Array [], expect(() => parse(" \n \n \t")).toThrowErrorMatchingInlineSnapshot(
"name": "", `"Workout is missing a name. Use \`Name:\` to declare one."`
} );
`);
}); });
}); });

View File

@ -107,8 +107,13 @@ const parseIntervals = (tokens: Token[]): Interval[] => {
export const parseTokens = (tokens: Token[]): Workout => { export const parseTokens = (tokens: Token[]): Workout => {
const [header, intervalTokens] = parseHeader(tokens); const [header, intervalTokens] = parseHeader(tokens);
if (header.name === undefined) {
throw new Error("Workout is missing a name. Use `Name:` to declare one.");
}
return { return {
name: header.name || "", name: header.name,
author: header.author || "", author: header.author || "",
description: header.description || "", description: header.description || "",
intervals: parseIntervals(intervalTokens), intervals: parseIntervals(intervalTokens),