From 6d024c475ac1e6919b60ab660a62a21809077f8f Mon Sep 17 00:00:00 2001 From: Paul Woitaschek Date: Sat, 13 May 2023 13:23:04 +0200 Subject: [PATCH] Write additional tests and report errors. --- data/src/main/kotlin/voice/data/Chapter.kt | 62 ++++++++------ .../src/test/kotlin/voice/data/ChapterTest.kt | 80 +++++++++++++++++-- 2 files changed, 112 insertions(+), 30 deletions(-) diff --git a/data/src/main/kotlin/voice/data/Chapter.kt b/data/src/main/kotlin/voice/data/Chapter.kt index 6d660d3528..add5ee505a 100644 --- a/data/src/main/kotlin/voice/data/Chapter.kt +++ b/data/src/main/kotlin/voice/data/Chapter.kt @@ -11,6 +11,7 @@ import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor import kotlinx.serialization.descriptors.SerialDescriptor import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Encoder +import voice.logging.core.Logger import java.time.Instant @Entity(tableName = "chapters2") @@ -25,32 +26,47 @@ data class Chapter( @Ignore val chapterMarks: List = if (markData.isEmpty()) { - listOf(ChapterMark(name, 0L, duration)) + listOf(ChapterMark(name, 0L, duration - 1)) } else { - val sorted = markData.distinctBy { it.startMs } - .filter { it.startMs in 0..() - for ((index, markData) in sorted.withIndex()) { - val name = markData.name - val previous = result.lastOrNull() - val next = sorted.getOrNull(index + 1) - val startMs = if (previous == null) 0L else previous.endMs + 1 - val endMs = if (next != null && next.startMs + 2 < duration && startMs < next.startMs - 1) { - next.startMs - 1 - } else { - duration - 1 - } - result += ChapterMark( - name = name, - startMs = startMs, - endMs = endMs, - ) - if (endMs == duration - 1) { - break + try { + val result = mutableListOf() + val sorted = markData.distinctBy { it.startMs } + .filter { it.startMs in 0.., expected: List, duration: Long = 20L) { val positions = Chapter( - duration = 20L, + duration = duration, fileLastModified = Instant.now(), id = ChapterId(""), - markData = listOf(11, 5, 5, 10).mapIndexed { index, i -> + markData = chapterStarts.sorted().mapIndexed { index, i -> MarkData( startMs = i.toLong(), name = "Mark $index", @@ -21,10 +90,7 @@ class ChapterTest { name = "Chapter", ).chapterMarks.map { MarkPosition(it.startMs, it.endMs) } - positions.shouldContainInOrder( - MarkPosition(0L, 9L), - MarkPosition(10L, 19L), - ) + positions.shouldContainInOrder(expected) } data class MarkPosition(val start: Long, val end: Long)