mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2026-11-22 20:03:26 +00:00
small fix of pagination for lists
This commit is contained in:
1 parent
72d76ff16d
commit
3ab8123fc2
2 files changed
+56
-7
No files matched your search
+3
-7
@@ -52,16 +52,12 @@ fun <T> List<T>.paginate(with: Pagination): PaginationResult<T> {
|
||||
*
|
||||
* @param T The type of items in the list
|
||||
* @param with The pagination parameters specifying which page to retrieve
|
||||
* @param reversed If true, the list will be paginated in reverse order
|
||||
* @return A [PaginationResult] containing the items from the requested page, optionally reversed
|
||||
* @param reversed If true, traverses the list in reverse order before selecting the requested page
|
||||
* @return A [PaginationResult] containing the items from the requested page in traversal order
|
||||
*/
|
||||
fun <T> List<T>.paginate(with: Pagination, reversed: Boolean): PaginationResult<T> {
|
||||
return if (reversed) {
|
||||
val actualPagination = with.optionallyReverse(
|
||||
size,
|
||||
reversed
|
||||
)
|
||||
paginate(actualPagination).changeResultsUnchecked { results.reversed() }
|
||||
asReversed().paginate(with)
|
||||
} else {
|
||||
paginate(with)
|
||||
}
|
||||
|
||||
+53
@@ -4,6 +4,8 @@ import dev.inmo.micro_utils.pagination.*
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class PaginationPaging {
|
||||
@@ -69,4 +71,55 @@ class PaginationPaging {
|
||||
|
||||
assertTrue(paginationHappend)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testReversedPaginationWalksList() {
|
||||
assertPaginationWalk(
|
||||
listOf(1, 2, 3, 4, 5),
|
||||
reversed = true,
|
||||
expectedPages = listOf(listOf(5, 4), listOf(3, 2), listOf(1))
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testReversedPaginationBeyondLastPage() {
|
||||
val result = listOf(1, 2, 3, 4, 5).paginate(SimplePagination(3, 2), reversed = true)
|
||||
|
||||
assertEquals(PaginationResult(3, 2, emptyList<Int>(), 5L), result)
|
||||
assertNull(result.nextPageIfNotEmptyOrLastPage())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testReversedPaginationOfEmptyList() {
|
||||
assertPaginationWalk(emptyList(), reversed = true, expectedPages = listOf(emptyList()))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testReversedPaginationStopsOnFullLastPage() {
|
||||
assertPaginationWalk(
|
||||
listOf(4, 1, 3, 2),
|
||||
reversed = true,
|
||||
expectedPages = listOf(listOf(2, 3), listOf(1, 4))
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testForwardPaginationWalksList() {
|
||||
assertPaginationWalk(
|
||||
listOf(1, 2, 3, 4, 5),
|
||||
reversed = false,
|
||||
expectedPages = listOf(listOf(1, 2), listOf(3, 4), listOf(5))
|
||||
)
|
||||
}
|
||||
|
||||
private fun assertPaginationWalk(list: List<Int>, reversed: Boolean, expectedPages: List<List<Int>>) {
|
||||
val pageSize = 2
|
||||
var request: Pagination? = FirstPagePagination(pageSize)
|
||||
for ((page, expectedItems) in expectedPages.withIndex()) {
|
||||
val result = list.paginate(assertNotNull(request, "Expected a request for page $page"), reversed)
|
||||
assertEquals(PaginationResult(page, pageSize, expectedItems, list.size.toLong()), result)
|
||||
request = result.nextPageIfNotEmptyOrLastPage()
|
||||
}
|
||||
assertNull(request)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user