mirror of
https://github.com/mihonapp/mihon.git
synced 2025-10-22 02:58:55 +02:00
Add tests for MissingChapters function
This commit is contained in:
@@ -1,23 +1,21 @@
|
||||
package tachiyomi.domain.chapter.service
|
||||
|
||||
import kotlin.math.floor
|
||||
|
||||
fun countMissingChapters(chaptersInput: List<Float>): Int? {
|
||||
if (chaptersInput.isEmpty()) {
|
||||
fun List<Float>.missingChaptersCount(): Int {
|
||||
if (this.isEmpty()) {
|
||||
return 0
|
||||
}
|
||||
|
||||
val chapters = chaptersInput
|
||||
// Remove any invalid chapters
|
||||
.filter { it != -1f }
|
||||
val chapters = this
|
||||
// Ignore unknown chapter numbers
|
||||
.filterNot { it == -1f }
|
||||
// Convert to integers, as we cannot check if 16.5 is missing
|
||||
.map { floor(it.toDouble()).toInt() }
|
||||
.map(Float::toInt)
|
||||
// Only keep unique chapters so that -1 or 16 are not counted multiple times
|
||||
.distinct()
|
||||
.sorted()
|
||||
|
||||
if (chapters.isEmpty()) {
|
||||
return null
|
||||
return 0
|
||||
}
|
||||
|
||||
var missingChaptersCount = 0
|
||||
|
@@ -0,0 +1,30 @@
|
||||
package tachiyomi.domain.chapter.service
|
||||
|
||||
import io.kotest.matchers.shouldBe
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.parallel.Execution
|
||||
import org.junit.jupiter.api.parallel.ExecutionMode
|
||||
|
||||
@Execution(ExecutionMode.CONCURRENT)
|
||||
class MissingChaptersTest {
|
||||
|
||||
@Test
|
||||
fun `returns 0 when empty list`() {
|
||||
emptyList<Float>().missingChaptersCount() shouldBe 0
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `returns 0 when all unknown chapter numbers`() {
|
||||
listOf(-1f, -1f, -1f).missingChaptersCount() shouldBe 0
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `handles repeated base chapter numbers`() {
|
||||
listOf(1f, 1.0f, 1.1f, 1.5f, 1.6f, 1.99f).missingChaptersCount() shouldBe 0
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `returns number of missing chapters`() {
|
||||
listOf(-1f, 1f, 2f, 2.2f, 4f, 6f, 10f, 11f).missingChaptersCount() shouldBe 5
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user