package com.pixelized.desktop.lwa.parser.dice

import com.pixelized.shared.lwa.parser.dice.Dice
import com.pixelized.shared.lwa.parser.dice.DiceParser
import org.junit.Test

class DiceParserTest {

    @Test
    fun testInvalidExpression() {
        val parser = DiceParser()
        parser.test(expression = "", expected = null)
        parser.test(expression = " ", expected = null)
        parser.test(expression = "1", expected = null)
        parser.test(expression = "d6", expected = null)
        parser.test(expression = "ad6", expected = null)
        parser.test(expression = "dd6", expected = null)
        parser.test(expression = "ed6", expected = null)
    }

    @Test
    fun testFaces() {
        val parser = DiceParser()
        parser.test(expression = "1d4", expected = Dice(modifier = null, quantity = 1, faces = 4))
        parser.test(expression = "1d6", expected = Dice(modifier = null, quantity = 1, faces = 6))
        parser.test(expression = "1d8", expected = Dice(modifier = null, quantity = 1, faces = 8))
    }

    @Test
    fun testQuantity() {
        val parser = DiceParser()
        parser.test(expression = "2d6", expected = Dice(modifier = null, quantity = 2, faces = 6))
        parser.test(expression = "3d6", expected = Dice(modifier = null, quantity = 3, faces = 6))
        parser.test(expression = "4d6", expected = Dice(modifier = null, quantity = 4, faces = 6))
    }

    @Test
    fun testWhitespace() {
        val parser = DiceParser()
        parser.test(expression = "   2d6", expected = Dice(modifier = null, quantity = 2, faces = 6))
        parser.test(expression = "2d6   ", expected = Dice(modifier = null, quantity = 2, faces = 6))
    }

    @Test
    fun testModifier() {
        val parser = DiceParser()
        parser.test(
            expression = "a1d6",
            expected = Dice(modifier = Dice.Modifier.ADVANTAGE, quantity = 1, faces = 6)
        )
        parser.test(
            expression = "d1d6",
            expected = Dice(modifier = Dice.Modifier.DISADVANTAGE, quantity = 1, faces = 6)
        )
        parser.test(
            expression = "e1d6",
            expected = Dice(modifier = Dice.Modifier.EMPHASIS, quantity = 1, faces = 6)
        )
    }

    private fun DiceParser.test(
        expression: String,
        expected: Dice?,
    ) {
        val result = parse(expression = expression)
        assert(result == expected) {
            "DiceParser.parse(expression=$expression) is expected to return:$expected, but was:$result"
        }
    }
}