Deck tests

This commit is contained in:
2025-03-05 18:20:49 +01:00
parent 729e7f6adc
commit 7043e1c6e7
6 changed files with 167 additions and 45 deletions

View File

@@ -0,0 +1,8 @@
package eventDemo
import eventDemo.app.entity.Card
import eventDemo.app.entity.Deck
fun Deck.allCardCount(): Int = stack.size + discard.size + playersHands.values.flatten().size
fun Deck.allCards(): Set<Card> = stack + discard + playersHands.values.flatten()

View File

@@ -0,0 +1,105 @@
package eventDemo.app.entity
import eventDemo.allCardCount
import eventDemo.allCards
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.collections.shouldBeUnique
import io.kotest.matchers.ints.shouldBeExactly
import kotlin.test.assertNotNull
class DeckTest :
FunSpec({
val totalCardsNumber = 104
test("newWithoutPlayers") {
// When
val deck = Deck.newWithoutPlayers()
// Then
deck.stack.size shouldBeExactly totalCardsNumber
deck.discard.size shouldBeExactly 0
deck.playersHands.size shouldBeExactly 0
deck.allCardCount() shouldBeExactly totalCardsNumber
deck.allCards().shouldBeUnique()
deck.allCards().map { it.id }.shouldBeUnique()
}
test("initHands") {
// Given
val playerNumbers = 4
val players = (1..playerNumbers).map { Player(name = "name $it") }.toSet()
val deck = Deck.newWithoutPlayers()
// When
val initDeck = deck.initHands(players)
// Then
initDeck.stack.size shouldBeExactly totalCardsNumber - (playerNumbers * 7)
initDeck.discard.size shouldBeExactly 0
initDeck.playersHands.size shouldBeExactly playerNumbers
initDeck.playersHands.forEach { (_, cards) -> cards.size shouldBeExactly 7 }
initDeck.allCardCount() shouldBeExactly totalCardsNumber
}
test("takeOneCardFromStackTo") {
// Given
val playerNumbers = 4
val players = (1..playerNumbers).map { Player(name = "name $it") }.toSet()
val deck = Deck.newWithoutPlayers().initHands(players)
val firstPlayer = players.first()
// When
val modifiedDeck = deck.takeOneCardFromStackTo(firstPlayer)
// Then
modifiedDeck.discard.size shouldBeExactly 0
modifiedDeck.stack.size shouldBeExactly totalCardsNumber - (playerNumbers * 7) - 1
modifiedDeck.playersHands.size shouldBeExactly playerNumbers
assertNotNull(modifiedDeck.playersHands[firstPlayer]).size shouldBeExactly 7 + 1
modifiedDeck.playersHands
.filterKeys { it != firstPlayer }
.forEach { (_, cards) -> cards.size shouldBeExactly 7 }
modifiedDeck.allCardCount() shouldBeExactly totalCardsNumber
}
test("putOneCardFromHand") {
// Given
val playerNumbers = 4
val players = (1..playerNumbers).map { Player(name = "name $it") }.toSet()
val deck = Deck.newWithoutPlayers().initHands(players)
val firstPlayer = players.first()
// When
val card = deck.playersHands[firstPlayer]!!.first()
val modifiedDeck = deck.putOneCardFromHand(firstPlayer, card)
// Then
modifiedDeck.discard.size shouldBeExactly 1
modifiedDeck.stack.size shouldBeExactly totalCardsNumber - (playerNumbers * 7)
modifiedDeck.playersHands.size shouldBeExactly playerNumbers
assertNotNull(modifiedDeck.playersHands[firstPlayer]).size shouldBeExactly 6
modifiedDeck.playersHands
.filterKeys { it != firstPlayer }
.forEach { (_, cards) -> cards.size shouldBeExactly 7 }
modifiedDeck.allCardCount() shouldBeExactly totalCardsNumber
}
test("placeFirstCardOnDiscard") {
// Given
val playerNumbers = 4
val players = (1..playerNumbers).map { Player(name = "name $it") }.toSet()
val deck = Deck.newWithoutPlayers().initHands(players)
val firstPlayer = players.first()
// When
val modifiedDeck = deck.placeFirstCardOnDiscard()
// Then
modifiedDeck.discard.size shouldBeExactly 1
modifiedDeck.stack.size shouldBeExactly totalCardsNumber - (playerNumbers * 7) - 1
modifiedDeck.playersHands.size shouldBeExactly playerNumbers
modifiedDeck.playersHands
.forEach { (_, cards) -> cards.size shouldBeExactly 7 }
modifiedDeck.allCardCount() shouldBeExactly totalCardsNumber
}
})