more fixes to god of fixes in paginations

This commit is contained in:
InsanusMokrassar 2024-05-26 23:51:46 +06:00
parent 77f56c5dda
commit 758a92410b
3 changed files with 30 additions and 6 deletions

View File

@ -50,15 +50,19 @@ val PaginationResult<*>.lastPage
val PaginationResult<*>.isLastPage val PaginationResult<*>.isLastPage
get() = page.toLong() == lastPageLong get() = page.toLong() == lastPageLong
fun <T> emptyPaginationResult() = PaginationResult<T>(0, 0, emptyList(), 0L)
fun <T> emptyPaginationResult( fun <T> emptyPaginationResult(
basePagination: Pagination basePagination: Pagination,
objectsNumber: Number
) = PaginationResult<T>( ) = PaginationResult<T>(
basePagination.page, basePagination.page,
basePagination.size, basePagination.size,
emptyList(), emptyList(),
0L objectsNumber.toLong()
) )
fun <T> emptyPaginationResult(
basePagination: Pagination,
) = emptyPaginationResult<T>(basePagination, 0)
fun <T> emptyPaginationResult() = emptyPaginationResult<T>(FirstPagePagination(0))
/** /**
* @return New [PaginationResult] with [data] without checking of data sizes equality * @return New [PaginationResult] with [data] without checking of data sizes equality

View File

@ -21,8 +21,8 @@ fun <T> Iterable<T>.paginate(with: Pagination): PaginationResult<T> {
} }
fun <T> List<T>.paginate(with: Pagination): PaginationResult<T> { fun <T> List<T>.paginate(with: Pagination): PaginationResult<T> {
if (with.firstIndex !in indices || with.lastIndex !in indices) { if (with.firstIndex >= size || with.lastIndex < 0) {
return emptyPaginationResult(with) return emptyPaginationResult(with, size.toLong())
} }
return asSequence().drop(with.firstIndex).take(with.size).toList().createPaginationResult( return asSequence().drop(with.firstIndex).take(with.size).toList().createPaginationResult(
with, with,

View File

@ -38,7 +38,27 @@ class PaginationPaging {
doForAllWithNextPaging(startPagination) { doForAllWithNextPaging(startPagination) {
val resultPagination = list.paginate(it) val resultPagination = list.paginate(it)
assertEquals(resultPagination, emptyPaginationResult(it)) assertEquals(resultPagination, emptyPaginationResult(it, list.size))
assertFalse(paginationHappend)
paginationHappend = true
resultPagination
}
assertTrue(paginationHappend)
}
@Test
fun testRightOutPaginateOnList() {
val list = (0 until 7).toList()
val startPagination = SimplePagination(page = 4, size = 2)
var paginationHappend = false
doForAllWithNextPaging(startPagination) {
val resultPagination = list.paginate(it)
assertEquals(resultPagination, emptyPaginationResult(it, list.size))
assertFalse(paginationHappend) assertFalse(paginationHappend)