diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/MangaParser.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/MangaParser.kt index 8de5048c..82848fb2 100644 --- a/src/main/kotlin/org/koitharu/kotatsu/parsers/MangaParser.kt +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/MangaParser.kt @@ -1,6 +1,7 @@ package org.koitharu.kotatsu.parsers import androidx.annotation.CallSuper +import androidx.annotation.VisibleForTesting import okhttp3.Headers import org.koitharu.kotatsu.parsers.config.ConfigKey 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 */ - protected open val defaultSortOrder: SortOrder + @VisibleForTesting(otherwise = VisibleForTesting.PROTECTED) + open val defaultSortOrder: SortOrder get() { val supported = availableSortOrders return SortOrder.entries.first { it in supported } diff --git a/src/test/kotlin/org/koitharu/kotatsu/parsers/MangaParserTest.kt b/src/test/kotlin/org/koitharu/kotatsu/parsers/MangaParserTest.kt index b1c2912c..d117081e 100644 --- a/src/test/kotlin/org/koitharu/kotatsu/parsers/MangaParserTest.kt +++ b/src/test/kotlin/org/koitharu/kotatsu/parsers/MangaParserTest.kt @@ -11,10 +11,7 @@ import org.koitharu.kotatsu.parsers.model.SortOrder import org.koitharu.kotatsu.parsers.util.domain import org.koitharu.kotatsu.parsers.util.medianOrNull import org.koitharu.kotatsu.parsers.util.mimeType -import org.koitharu.kotatsu.test_util.isDistinct -import org.koitharu.kotatsu.test_util.isDistinctBy -import org.koitharu.kotatsu.test_util.isUrlAbsolute -import org.koitharu.kotatsu.test_util.maxDuplicates +import org.koitharu.kotatsu.test_util.* import kotlin.time.Duration.Companion.minutes @@ -28,7 +25,7 @@ internal class MangaParserTest { @MangaSources fun list(source: MangaSource) = runTest(timeout = timeout) { 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") assert(list.all { it.source == source }) } @@ -93,10 +90,16 @@ internal class MangaParserTest { val titles = tags.map { it.title } assert(titles.isDistinct()) assert("" !in titles) + assert(titles.all { it.first().isUpperCase() }) { "Not all tags are capitalized" } assert(tags.all { it.source == source }) 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})") assert(list.all { it.source == source }) } @@ -142,7 +145,7 @@ internal class MangaParserTest { @MangaSources fun details(source: MangaSource) = runTest(timeout = timeout) { 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] parser.getDetails(manga).apply { assert(!chapters.isNullOrEmpty()) { "Chapters are null or empty" } @@ -156,7 +159,7 @@ internal class MangaParserTest { assert(c.isDistinctBy { it.id }) { "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" } assert(c.all { it.source == source }) @@ -171,7 +174,7 @@ internal class MangaParserTest { @MangaSources fun pages(source: MangaSource) = runTest(timeout = timeout) { 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 chapter = parser.getDetails(manga).chapters?.firstOrNull() ?: error("Chapter is null at ${manga.publicUrl}") val pages = parser.getPages(chapter) diff --git a/src/test/kotlin/org/koitharu/kotatsu/test_util/Util.kt b/src/test/kotlin/org/koitharu/kotatsu/test_util/Util.kt index af2c4d5d..6240da72 100644 --- a/src/test/kotlin/org/koitharu/kotatsu/test_util/Util.kt +++ b/src/test/kotlin/org/koitharu/kotatsu/test_util/Util.kt @@ -30,6 +30,16 @@ internal fun Collection.isDistinctBy(selector: (T) -> K): Boolean { return set.size == size } +internal fun Collection.isDistinctByNotNull(selector: (T) -> K?): Boolean { + val set = ArraySet(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.isUrlAbsolute() = matches(PATTERN_URL_ABSOLUTE)