Skip to content

Commit

Permalink
test: Add test about shift constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Distractic committed Dec 20, 2023
1 parent 58e4ec9 commit 5b83371
Showing 1 changed file with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
package com.github.rushyverse.api.gui.load

import io.kotest.assertions.throwables.shouldNotThrow
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.matchers.collections.shouldContainExactly
import io.kotest.matchers.shouldBe
import io.mockk.every
import io.mockk.mockk
import java.util.*
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.nanoseconds
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.bukkit.inventory.Inventory
import org.bukkit.inventory.ItemStack
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource

class ShiftInventoryLoadingAnimationTest {

Expand All @@ -33,6 +40,30 @@ class ShiftInventoryLoadingAnimationTest {
}
}

@ParameterizedTest
@ValueSource(ints = [0, -1, -2, -3, -4, -5, -6, -7, -8])
fun `should throw if duration is not positive`(duration: Int) {
shouldThrow<IllegalArgumentException> {
ShiftInventoryLoadingAnimation<Unit>(
initialize = { emptySequence() },
shift = 1,
delay = duration.milliseconds
)
}
}

@ParameterizedTest
@ValueSource(ints = [1, 2, 3, 4, 5, 6, 7, 8])
fun `should not throw if duration is positive`(duration: Int) {
shouldNotThrow<IllegalArgumentException> {
ShiftInventoryLoadingAnimation<Unit>(
initialize = { emptySequence() },
shift = 1,
delay = duration.nanoseconds
)
}
}

@Test
fun `should not change inventory if initialize is empty`() {
val delay = 10.milliseconds
Expand All @@ -53,12 +84,21 @@ class ShiftInventoryLoadingAnimationTest {

@Test
fun `should just initialize if shift is zero`() = runBlocking {
shouldJustInitializeWithoutShift(0)
}

@Test
fun `should just initialize if shift is inventory size`() = runBlocking {
shouldJustInitializeWithoutShift(inventory.size)
}

private suspend fun shouldJustInitializeWithoutShift(shift: Int) = coroutineScope {
val delay = 10.milliseconds
val items = Array(inventory.size) { mockk<ItemStack>() }

val animation = ShiftInventoryLoadingAnimation<Unit>(
initialize = { items.asSequence() },
shift = 0,
shift = shift,
delay = delay
)

Expand All @@ -68,4 +108,30 @@ class ShiftInventoryLoadingAnimationTest {

inventory.contents.toList() shouldContainExactly items.toList()
}

@ParameterizedTest
@ValueSource(ints = [1, 2, 3, 4, 5, 6, 7, 8])
fun `should shift initialized items`(shift: Int) = runBlocking {
val delay = 100.milliseconds
val items = Array(inventory.size) { mockk<ItemStack>() }
val itemList = items.toList()

val animation = ShiftInventoryLoadingAnimation<Unit>(
initialize = { items.asSequence() },
shift = shift,
delay = delay
)

val job = launch { animation.loading(Unit, inventory) }

delay(delay / 2)
repeat(5) {
inventory.contents.toList() shouldContainExactly itemList
delay(delay)
Collections.rotate(itemList, shift)
job.isActive shouldBe true
}

job.cancelAndJoin()
}
}

0 comments on commit 5b83371

Please sign in to comment.