You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
4.4 KiB
103 lines
4.4 KiB
import {
|
|
battlerAttribute,
|
|
BattlerAttributeType,
|
|
BattlerDeclaration,
|
|
battlerDeclaration,
|
|
BattlerStatement,
|
|
BattlerStatementType,
|
|
isBattlerAttribute,
|
|
isBattlerDeclaration
|
|
} from "./BattlerStatement";
|
|
import {TopLevelStatementType} from "./TopLevelStatement";
|
|
import {ScriptFile, scriptFile} from "./ScriptFile";
|
|
import {parse as parseInternal, SyntaxError} from "./NomScript.peggy";
|
|
import {numberValue} from "./ScriptValue";
|
|
|
|
function parse(text: string): ScriptFile {
|
|
return parseInternal(text, {start: "ScriptFile", grammarSource: "testData"})
|
|
}
|
|
|
|
describe("BattlerDeclaration", () => {
|
|
describe("constructor", () => {
|
|
test("sets id property from input", () => {
|
|
expect(battlerDeclaration("myId", []).id).toEqual("myId")
|
|
})
|
|
});
|
|
describe("typecheck", () => {
|
|
test("returns true on constructor output", () => {
|
|
expect(isBattlerDeclaration(battlerDeclaration("testId", []))).toBeTruthy()
|
|
})
|
|
test("returns true on hand-crafted instance", () => {
|
|
expect(isBattlerDeclaration({
|
|
type: TopLevelStatementType.BATTLER,
|
|
id: "someId",
|
|
contents: []
|
|
})).toBeTruthy()
|
|
})
|
|
});
|
|
describe("parsing", () => {
|
|
function success(name: string, text: string, ...result: BattlerDeclaration[]) {
|
|
test(`succeeds for ${name}`, () => {
|
|
expect(parse(text)).toEqual(scriptFile(result))
|
|
})
|
|
}
|
|
success("basic empty instance", "battler nomi\nend battler", battlerDeclaration("nomi", []))
|
|
success("comment after opening", "battler nomi // of the nomi crew\nend battler", battlerDeclaration("nomi", []))
|
|
success("comments and empty lines inside the block", "battler nomi\n\n// hungry gal...\n\nend battler", battlerDeclaration("nomi", []))
|
|
success("nonempty instance", "battler nomi\nhealth 20\nconfidence 90\nend battler",
|
|
battlerDeclaration("nomi", [
|
|
battlerAttribute(BattlerAttributeType.HEALTH, numberValue(20)),
|
|
battlerAttribute(BattlerAttributeType.CONFIDENCE, numberValue(90))]))
|
|
|
|
function failure(name: string, text: string) {
|
|
test(`fails for ${name}`, () => {
|
|
expect(() => parse(text)).toThrow(SyntaxError)
|
|
})
|
|
}
|
|
|
|
failure("opening statement only", "battler open")
|
|
failure("ending statement only", "end battler")
|
|
failure("multi identifier", "battler cool girl\nend battler")
|
|
failure("containing top level statement", "battler genius\nscript version 3\nend battler")
|
|
failure("containing other battler declaration", "battler coolio\nbattler even_cooler\nend battler\nend battler")
|
|
});
|
|
});
|
|
|
|
describe("BattlerAttribute", () => {
|
|
describe("constructor", () => {
|
|
test("forwards the elements to the fields", () => {
|
|
const attr = battlerAttribute(BattlerAttributeType.HEALTH, numberValue(20))
|
|
|
|
expect(attr.attribute).toEqual(BattlerAttributeType.HEALTH)
|
|
expect(attr.value).toEqual(numberValue(20))
|
|
})
|
|
})
|
|
describe("typecheck", () => {
|
|
test("returns true on constructor input", () => {
|
|
expect(isBattlerAttribute(battlerAttribute(BattlerAttributeType.HEALTH, numberValue(20)))).toBeTruthy()
|
|
})
|
|
test("returns true on hand-crafted instance", () => {
|
|
expect(isBattlerAttribute({
|
|
type: BattlerStatementType.ATTRIBUTE,
|
|
attribute: BattlerAttributeType.STAMINA,
|
|
value: numberValue(20)
|
|
})).toBeTruthy()
|
|
})
|
|
})
|
|
describe("parsing", () => {
|
|
function success(name: string, text: string, ...result: readonly BattlerStatement[]) {
|
|
test(`succeeds for ${name}`, () => {
|
|
expect(parse(`battler battler\n${text}\nend battler`)).toEqual(scriptFile([battlerDeclaration("battler", result)]))
|
|
})
|
|
}
|
|
success("basic empty instance", "health 20", battlerAttribute(BattlerAttributeType.HEALTH, numberValue(20)))
|
|
success("comment between the attribute and the number", "stamina/* 200 */99", battlerAttribute(BattlerAttributeType.STAMINA, numberValue(99)))
|
|
|
|
function failure(name: string, text: string) {
|
|
test(`fails for ${name}`, () => {
|
|
expect(() => parse(text)).toThrow(SyntaxError)
|
|
})
|
|
}
|
|
failure("no value after the attribute", "stamina // 99")
|
|
})
|
|
}); |