import {parse as parseInternal, SyntaxError} from "./NomScript.peggy" import {isVersionStatement, TopLevelStatementTypes, VersionStatement, versionStatement} from "./TopLevelStatement"; import {scriptFile, ScriptFile} from "./ScriptFile"; function parse(text: string): ScriptFile { return parseInternal(text, {start: "ScriptFile", grammarSource: "testData"}) } describe("VersionStatement", () => { describe("constructor", () => { test("forwards parameter to the version property", () => { expect(versionStatement(5)).toEqual({ type: TopLevelStatementTypes.VERSION, version: 5 }) }) }) describe("typecheck", () => { test("passes on the output of the constructor", () => { expect(isVersionStatement(versionStatement(2))).toBeTruthy() }) test("passes on a hand-constructed instance", () => { expect(isVersionStatement({ type: TopLevelStatementTypes.VERSION, version: 5 })).toBeTruthy() }) }) describe("parsing", () => { function success(name: string, text: string, ...result: VersionStatement[]) { test(`succeeds for ${name}`, () => { expect(parse(text)).toEqual(scriptFile(result)) }) } success("basic example", "script version 1", versionStatement(1)) success("followed by line comment", "script version 3 // and nine tenths", versionStatement(3)) success("preceded by block comment", "/* just because it's a */ script version 2", versionStatement(2)) success("preceded by line comment", "// line comment\nscript version 9999", versionStatement(9999)) success("preceded by spaces", "\n\n\n\t\t\tscript version 2", versionStatement(2)) success("separated by tabs instead of spaces", "script\tversion\t4", versionStatement(4)) success("separated by comments instead of spaces", "script/* or candy */version/* if it can be called that */4", versionStatement(4)) success("separated by multiline comments instead of spaces", "script/* ripped\n */version/* mergin\n */4", versionStatement(4)) success("doubled on separate lines", "script version 1\nscript version 2", versionStatement(1), versionStatement(2)) function failure(name: string, text: string) { test(`fails for ${name}`, () => { expect(() => parse(text)).toThrow(SyntaxError) }) } failure("separated by newlines", "script\nversion\n1") failure("not separated", "scriptversion1") failure("wrong order", "version script 1") failure("decimal version", "version script 1.5") failure("followed by garbage", "script version 1 and a half") failure("doubled on same line", "script version 1 script version 1") }) })