Allow untitled workouts

This commit is contained in:
Rene Saarsoo 2020-10-04 13:20:04 +03:00
parent adebc3d460
commit 0d6f61913c
2 changed files with 18 additions and 12 deletions

View File

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

View File

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