import {identifier, isIdentifier, isNumberValue, numberValue, ScriptValueType} from "./ScriptValue"; import { parse, SyntaxError } from "./NomScript.peggy"; import {ScriptExpression} from "./ScriptExpression"; describe("NumberValue", () => { describe("constructor", () => { test("forwards number parameter to the value property", () => { expect(numberValue(5)).toEqual({ type: ScriptValueType.NUMBER, value: 5 }) }) test("parses string parameter to the value property", () => { expect(numberValue("99")).toEqual({ type: ScriptValueType.NUMBER, value: 99 }) }) }) describe("typecheck", () => { test("passes on the output of the constructor", () => { expect(isNumberValue(numberValue(2))).toBeTruthy() }) test("passes on a hand-constructed instance", () => { expect(isNumberValue({ type: ScriptValueType.NUMBER, value: 21 })).toBeTruthy() }) }) describe("parsing", () => { function success(name: string, text: string, result: ScriptExpression) { test(`succeeds for ${name}`, () => { expect(parse(text, {startRule: "TopLevelExpression", grammarSource: "testdata"})).toEqual(result) }) } success("zero", "0", numberValue(0)) success("leading zeroes on a zero", "00000000", numberValue(0)) success("leading zeroes on a positive number", "0000000050", numberValue(50)) success("simple positive number", "1", numberValue(1)) success("long number", "99999999", numberValue(99999999)) function failure(name: string, text: string) { test(`fails for ${name}`, () => { expect(() => parse(text, {startRule: "TopLevelExpression", grammarSource: "testdata"})).toThrow(SyntaxError) }) } failure("number containing non-digit characters", "100a0") failure("number with decimal point", "100.0") failure("number with negative sign", "-100") }) }) describe("ScriptIdentifier", () => { describe("constructor", () => { test("transfers the value parameter to the attribute", () => { expect(identifier("naps").value).toEqual("naps") }) }) describe("typecheck", () => { test("passes on the output of the constructor", () => { expect(isIdentifier(identifier("baps"))).toBeTruthy() }) test("passes on a hand-constructed instance", () => { expect(isIdentifier({ type: ScriptValueType.IDENTIFIER, value: "laps" })).toBeTruthy() }) }) describe("parsing", () => { function success(name: string, text: string, result: ScriptExpression) { test(`succeeds for ${name}`, () => { expect(parse(text, {startRule: "TopLevelExpression", grammarSource: "testdata"})).toEqual(result) }) } success("basic identifier", "basic", identifier("basic")) success("identifier starting with underscore", "_boop", identifier("_boop")) success("identifier starting with dollar sign", "$tring", identifier("$tring")) success("single character identifier", "a", identifier("a")) success("identifier ending in numbers", "$100", identifier("$100")) function failure(name: string, text: string) { test(`fails for ${name}`, () => { expect(() => parse(text, {startRule: "TopLevelExpression", grammarSource: "testdata"})).toThrow(SyntaxError) }) } failure("identifier starting in numbers", "100$") }) });