Refactor the roll mechanism to allow broader instructions set.

This commit is contained in:
Thomas Andres Gomez 2024-11-27 14:09:26 +01:00
parent d2ae180cf7
commit 8d93d46cce
10 changed files with 207 additions and 146 deletions

View file

@ -1,12 +1,11 @@
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 com.pixelized.desktop.lwa.parser.arithmetic.ArithmeticParser
import kotlin.test.Test
import kotlin.test.assertFails
class ArithmeticParserTest {
class InstructionParserTest {
@Test
fun testDiceInstructionParse() {
@ -14,16 +13,16 @@ class ArithmeticParserTest {
fun test(
instruction: String,
expectedModifier: Instruction.Dice.Modifier?,
expectedModifier: Instruction.Dice.Modifier.Dice.Modifier?,
expectedQuantity: Int,
expectedFaces: Int,
) {
val dice = parser.parseInstruction(instruction = instruction)
assert(dice is Instruction.Dice) {
assert(dice is Instruction.Dice.Dice) {
"Instruction should be ArithmeticInstruction.Dice but was: ${dice::class.java.simpleName}"
}
(dice as? Instruction.Dice)?.let {
(dice as? Instruction.Dice.Dice)?.let {
assert(dice.modifier == expectedModifier) {
"$instruction modifier should be:\"$expectedModifier\", but was: ${dice.modifier}"
}
@ -69,10 +68,10 @@ class ArithmeticParserTest {
ArithmeticParser.words.map { instruction ->
val word = parser.parseInstruction(instruction = instruction)
assert(word is Instruction.Word) {
assert(word is Instruction.Word.Word) {
"Instruction should be ArithmeticInstruction.Word but was: ${word::class.java.simpleName}"
}
(word as? Instruction.Word)?.let {
(word as? Instruction.Word.Word)?.let {
assert(it.name == instruction) {
"Instruction should be $instruction, but was ${it.name}"
}
@ -87,10 +86,10 @@ class ArithmeticParserTest {
"100".let { instruction ->
val flat = parser.parseInstruction(instruction = instruction)
assert(flat is Instruction.Flat) {
assert(flat is Instruction.Flat.Flat) {
"Instruction should be ArithmeticInstruction.Flat but was: ${flat::class.java.simpleName}"
}
(flat as? Instruction.Flat)?.let {
(flat as? Instruction.Flat.Flat)?.let {
assert("${it.value}" == instruction) {
"Instruction should be $instruction, but was ${it.value}"
}
@ -113,7 +112,7 @@ class ArithmeticParserTest {
val parser = ArithmeticParser()
fun test(
arithmetics: Arithmetic,
arithmetics: Instruction,
expectedSign: Int,
expectedInstruction: Instruction,
) {