Update tests

Koitharu 2 years ago
parent 05d4db00b3
commit a03ab0e0c0
Signed by: Koitharu
GPG Key ID: 676DEE768C17A9D7

@ -1,6 +1,7 @@
package org.koitharu.kotatsu.parsers package org.koitharu.kotatsu.parsers
import androidx.annotation.CallSuper import androidx.annotation.CallSuper
import androidx.annotation.VisibleForTesting
import okhttp3.Headers import okhttp3.Headers
import org.koitharu.kotatsu.parsers.config.ConfigKey import org.koitharu.kotatsu.parsers.config.ConfigKey
import org.koitharu.kotatsu.parsers.model.* import org.koitharu.kotatsu.parsers.model.*
@ -81,7 +82,8 @@ abstract class MangaParser @InternalParsersApi constructor(
/** /**
* Used as fallback if value of `sortOrder` passed to [getList] is null * Used as fallback if value of `sortOrder` passed to [getList] is null
*/ */
protected open val defaultSortOrder: SortOrder @VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
open val defaultSortOrder: SortOrder
get() { get() {
val supported = availableSortOrders val supported = availableSortOrders
return SortOrder.entries.first { it in supported } return SortOrder.entries.first { it in supported }

@ -11,10 +11,7 @@ import org.koitharu.kotatsu.parsers.model.SortOrder
import org.koitharu.kotatsu.parsers.util.domain import org.koitharu.kotatsu.parsers.util.domain
import org.koitharu.kotatsu.parsers.util.medianOrNull import org.koitharu.kotatsu.parsers.util.medianOrNull
import org.koitharu.kotatsu.parsers.util.mimeType import org.koitharu.kotatsu.parsers.util.mimeType
import org.koitharu.kotatsu.test_util.isDistinct import org.koitharu.kotatsu.test_util.*
import org.koitharu.kotatsu.test_util.isDistinctBy
import org.koitharu.kotatsu.test_util.isUrlAbsolute
import org.koitharu.kotatsu.test_util.maxDuplicates
import kotlin.time.Duration.Companion.minutes import kotlin.time.Duration.Companion.minutes
@ -28,7 +25,7 @@ internal class MangaParserTest {
@MangaSources @MangaSources
fun list(source: MangaSource) = runTest(timeout = timeout) { fun list(source: MangaSource) = runTest(timeout = timeout) {
val parser = context.newParserInstance(source) val parser = context.newParserInstance(source)
val list = parser.getList(0, sortOrder = SortOrder.POPULARITY, tags = null, tagsExclude = null) val list = parser.getList(0, null)
checkMangaList(list, "list") checkMangaList(list, "list")
assert(list.all { it.source == source }) assert(list.all { it.source == source })
} }
@ -93,10 +90,16 @@ internal class MangaParserTest {
val titles = tags.map { it.title } val titles = tags.map { it.title }
assert(titles.isDistinct()) assert(titles.isDistinct())
assert("" !in titles) assert("" !in titles)
assert(titles.all { it.first().isUpperCase() }) { "Not all tags are capitalized" }
assert(tags.all { it.source == source }) assert(tags.all { it.source == source })
val tag = tags.last() val tag = tags.last()
val list = parser.getList(offset = 0, tags = setOf(tag), null, sortOrder = null) val list = parser.getList(
offset = 0,
MangaListFilter.Advanced.Builder(parser.defaultSortOrder)
.tags(setOf(tag))
.build(),
)
checkMangaList(list, "${tag.title} (${tag.key})") checkMangaList(list, "${tag.title} (${tag.key})")
assert(list.all { it.source == source }) assert(list.all { it.source == source })
} }
@ -142,7 +145,7 @@ internal class MangaParserTest {
@MangaSources @MangaSources
fun details(source: MangaSource) = runTest(timeout = timeout) { fun details(source: MangaSource) = runTest(timeout = timeout) {
val parser = context.newParserInstance(source) val parser = context.newParserInstance(source)
val list = parser.getList(0, sortOrder = SortOrder.POPULARITY, tags = null, tagsExclude = null) val list = parser.getList(0, null)
val manga = list[3] val manga = list[3]
parser.getDetails(manga).apply { parser.getDetails(manga).apply {
assert(!chapters.isNullOrEmpty()) { "Chapters are null or empty" } assert(!chapters.isNullOrEmpty()) { "Chapters are null or empty" }
@ -156,7 +159,7 @@ internal class MangaParserTest {
assert(c.isDistinctBy { it.id }) { assert(c.isDistinctBy { it.id }) {
"Chapters are not distinct by id: ${c.maxDuplicates { it.id }} for $publicUrl" "Chapters are not distinct by id: ${c.maxDuplicates { it.id }} for $publicUrl"
} }
assert(c.isDistinctBy { it.number to it.branch }) { assert(c.isDistinctByNotNull { if (it.number > 0f) it.number to it.branch else null }) {
"Chapters are not distinct by number: ${c.maxDuplicates { it.number to it.branch }} for $publicUrl" "Chapters are not distinct by number: ${c.maxDuplicates { it.number to it.branch }} for $publicUrl"
} }
assert(c.all { it.source == source }) assert(c.all { it.source == source })
@ -171,7 +174,7 @@ internal class MangaParserTest {
@MangaSources @MangaSources
fun pages(source: MangaSource) = runTest(timeout = timeout) { fun pages(source: MangaSource) = runTest(timeout = timeout) {
val parser = context.newParserInstance(source) val parser = context.newParserInstance(source)
val list = parser.getList(0, sortOrder = SortOrder.UPDATED, tags = null, tagsExclude = null) val list = parser.getList(0, null)
val manga = list.first() val manga = list.first()
val chapter = parser.getDetails(manga).chapters?.firstOrNull() ?: error("Chapter is null at ${manga.publicUrl}") val chapter = parser.getDetails(manga).chapters?.firstOrNull() ?: error("Chapter is null at ${manga.publicUrl}")
val pages = parser.getPages(chapter) val pages = parser.getPages(chapter)

@ -30,6 +30,16 @@ internal fun <T, K> Collection<T>.isDistinctBy(selector: (T) -> K): Boolean {
return set.size == size return set.size == size
} }
internal fun <T, K> Collection<T>.isDistinctByNotNull(selector: (T) -> K?): Boolean {
val set = ArraySet<K>(size)
for (item in this) {
if (!set.add(selector(item) ?: continue)) {
return false
}
}
return set.size == size
}
internal fun String.isUrlRelative() = matches(PATTERN_URL_RELATIVE) internal fun String.isUrlRelative() = matches(PATTERN_URL_RELATIVE)
internal fun String.isUrlAbsolute() = matches(PATTERN_URL_ABSOLUTE) internal fun String.isUrlAbsolute() = matches(PATTERN_URL_ABSOLUTE)

Loading…
Cancel
Save