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 ".";
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."`
);
});
});

View File

@ -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),