Consolidate missing chapters functions to domain module and add tests

This commit is contained in:
arkon
2023-04-22 11:34:51 -04:00
parent 94c94b2d88
commit 26f3995595
6 changed files with 67 additions and 65 deletions

View File

@@ -1,5 +1,8 @@
package tachiyomi.domain.chapter.service
import tachiyomi.domain.chapter.model.Chapter
import kotlin.math.floor
fun List<Float>.missingChaptersCount(): Int {
if (this.isEmpty()) {
return 0
@@ -33,3 +36,14 @@ fun List<Float>.missingChaptersCount(): Int {
return missingChaptersCount
}
fun calculateChapterGap(higherChapter: Chapter?, lowerChapter: Chapter?): Int {
if (higherChapter == null || lowerChapter == null) return 0
if (!higherChapter.isRecognizedNumber || !lowerChapter.isRecognizedNumber) return 0
return calculateChapterGap(higherChapter.chapterNumber, lowerChapter.chapterNumber)
}
fun calculateChapterGap(higherChapterNumber: Float, lowerChapterNumber: Float): Int {
if (higherChapterNumber < 0f || lowerChapterNumber < 0f) return 0
return floor(higherChapterNumber).toInt() - floor(lowerChapterNumber).toInt() - 1
}

View File

@@ -4,27 +4,54 @@ 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
import tachiyomi.domain.chapter.model.Chapter
@Execution(ExecutionMode.CONCURRENT)
class MissingChaptersTest {
@Test
fun `returns 0 when empty list`() {
fun `missingChaptersCount returns 0 when empty list`() {
emptyList<Float>().missingChaptersCount() shouldBe 0
}
@Test
fun `returns 0 when all unknown chapter numbers`() {
fun `missingChaptersCount returns 0 when all unknown chapter numbers`() {
listOf(-1f, -1f, -1f).missingChaptersCount() shouldBe 0
}
@Test
fun `handles repeated base chapter numbers`() {
fun `missingChaptersCount 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`() {
fun `missingChaptersCount returns number of missing chapters`() {
listOf(-1f, 1f, 2f, 2.2f, 4f, 6f, 10f, 11f).missingChaptersCount() shouldBe 5
}
@Test
fun `calculateChapterGap returns difference`() {
calculateChapterGap(chapter(10f), chapter(9f)) shouldBe 0f
calculateChapterGap(chapter(10f), chapter(8f)) shouldBe 1f
calculateChapterGap(chapter(10f), chapter(8.5f)) shouldBe 1f
calculateChapterGap(chapter(10f), chapter(1.1f)) shouldBe 8f
calculateChapterGap(10f, 9f) shouldBe 0f
calculateChapterGap(10f, 8f) shouldBe 1f
calculateChapterGap(10f, 8.5f) shouldBe 1f
calculateChapterGap(10f, 1.1f) shouldBe 8f
}
@Test
fun `calculateChapterGap returns 0 if either are not valid chapter numbers`() {
calculateChapterGap(chapter(-1f), chapter(10f)) shouldBe 0
calculateChapterGap(chapter(99f), chapter(-1f)) shouldBe 0
calculateChapterGap(-1f, 10f) shouldBe 0
calculateChapterGap(99f, -1f) shouldBe 0
}
private fun chapter(number: Float) = Chapter.create().copy(
chapterNumber = number,
)
}