Refactor the character sheet.

This commit is contained in:
Thomas Andres Gomez 2024-11-26 11:50:53 +01:00
parent 52f7f8333b
commit 51021d41d5
37 changed files with 1996 additions and 529 deletions

View file

@ -0,0 +1,156 @@
package com.pixelized.desktop.lwa.parser
import com.pixelized.desktop.lwa.parser.arithmetic.Arithmetic
import com.pixelized.desktop.lwa.parser.arithmetic.ArithmeticParser
import com.pixelized.desktop.lwa.parser.arithmetic.Instruction
import kotlin.test.Test
import kotlin.test.assertFails
class ArithmeticParserTest {
@Test
fun testDiceInstructionParse() {
val parser = ArithmeticParser()
fun test(
instruction: String,
expectedModifier: Instruction.Dice.Modifier?,
expectedQuantity: Int,
expectedFaces: Int,
) {
val dice = parser.parseInstruction(instruction = instruction)
assert(dice is Instruction.Dice) {
"Instruction should be ArithmeticInstruction.Dice but was: ${dice::class.java.simpleName}"
}
(dice as? Instruction.Dice)?.let {
assert(dice.modifier == expectedModifier) {
"$instruction modifier should be:\"$expectedModifier\", but was: ${dice.modifier}"
}
assert(dice.quantity == expectedQuantity) {
"$instruction quantity should be \"$expectedQuantity\" but was ${dice.quantity}"
}
assert(dice.faces == expectedFaces) {
"$instruction faces should be \"$expectedFaces\" but was ${dice.faces}"
}
}
}
test(
instruction = "1d100",
expectedModifier = null,
expectedQuantity = 1,
expectedFaces = 100,
)
test(
instruction = "a2d6",
expectedModifier = Instruction.Dice.Modifier.ADVANTAGE,
expectedQuantity = 2,
expectedFaces = 6,
)
test(
instruction = "d1d2",
expectedModifier = Instruction.Dice.Modifier.DISADVANTAGE,
expectedQuantity = 1,
expectedFaces = 2,
)
test(
instruction = "e6d6",
expectedModifier = Instruction.Dice.Modifier.EMPHASIS,
expectedQuantity = 6,
expectedFaces = 6,
)
}
@Test
fun testWordInstructionParse() {
val parser = ArithmeticParser()
ArithmeticParser.words.map { instruction ->
val word = parser.parseInstruction(instruction = instruction)
assert(word is Instruction.Word) {
"Instruction should be ArithmeticInstruction.Word but was: ${word::class.java.simpleName}"
}
(word as? Instruction.Word)?.let {
assert(it.name == instruction) {
"Instruction should be $instruction, but was ${it.name}"
}
}
}
}
@Test
fun testFlatInstructionParse() {
val parser = ArithmeticParser()
"100".let { instruction ->
val flat = parser.parseInstruction(instruction = instruction)
assert(flat is Instruction.Flat) {
"Instruction should be ArithmeticInstruction.Flat but was: ${flat::class.java.simpleName}"
}
(flat as? Instruction.Flat)?.let {
assert("${it.value}" == instruction) {
"Instruction should be $instruction, but was ${it.value}"
}
}
}
}
@Test
fun testFailedInstructionParse() {
val parser = ArithmeticParser()
assertFails(
message = "Instruction parse should failed with UnknownInstruction",
) {
parser.parseInstruction(instruction = "a110")
}
}
@Test
fun testRollParse() {
val parser = ArithmeticParser()
fun test(
arithmetics: Arithmetic,
expectedSign: Int,
expectedInstruction: Instruction,
) {
assert(arithmetics.sign == expectedSign) {
"Arithmetic sign should be $expectedSign but was: ${arithmetics.sign}"
}
assert(arithmetics.instruction == expectedInstruction) {
"Arithmetic instruction should be $expectedInstruction but was: ${arithmetics.instruction}"
}
}
val instructions = parser.parse(value = "1+1d6+2-BDC+BDD")
test(
arithmetics = instructions[0],
expectedSign = 1,
expectedInstruction = Instruction.Flat(value = 1),
)
test(
arithmetics = instructions[1],
expectedSign = 1,
expectedInstruction = Instruction.Dice(quantity = 1, faces = 6, modifier = null),
)
test(
arithmetics = instructions[2],
expectedSign = 1,
expectedInstruction = Instruction.Flat(value = 2),
)
test(
arithmetics = instructions[3],
expectedSign = -1,
expectedInstruction = Instruction.Word.BDC,
)
test(
arithmetics = instructions[4],
expectedSign = 1,
expectedInstruction = Instruction.Word.BDD,
)
}
}