From: David ‘Bombe’ Roden Date: Mon, 29 May 2017 04:53:37 +0000 (+0200) Subject: Add paginate() extension method and pagination matcher X-Git-Tag: 0.9.7^2~189 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=059a9194badec4e9c36e8fcb7fea458eea243bb0;p=Sone.git Add paginate() extension method and pagination matcher --- diff --git a/src/main/kotlin/net/pterodactylus/sone/utils/Pagination.kt b/src/main/kotlin/net/pterodactylus/sone/utils/Pagination.kt index 130fe2e..6c436e2 100644 --- a/src/main/kotlin/net/pterodactylus/sone/utils/Pagination.kt +++ b/src/main/kotlin/net/pterodactylus/sone/utils/Pagination.kt @@ -40,3 +40,5 @@ class Pagination(private val originalItems: List, pageSize: Int): Iter override fun iterator() = items.iterator() } + +fun Iterable.paginate(pageSize: Int) = Pagination(toList(), pageSize) diff --git a/src/test/kotlin/net/pterodactylus/sone/test/PaginationMatcher.kt b/src/test/kotlin/net/pterodactylus/sone/test/PaginationMatcher.kt new file mode 100644 index 0000000..5a47ca7 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/test/PaginationMatcher.kt @@ -0,0 +1,46 @@ +package net.pterodactylus.sone.test + +import net.pterodactylus.sone.utils.Pagination +import org.hamcrest.Description +import org.hamcrest.TypeSafeDiagnosingMatcher + +/** + * Hamcrest matcher for [Pagination]s. + */ +class PaginationMatcher(private val page: Int? = null, private val pages: Int? = null): + TypeSafeDiagnosingMatcher>() { + + override fun matchesSafely(pagination: Pagination<*>, mismatchDescription: Description): Boolean { + page?.let { + if (pagination.page != page) { + mismatchDescription.appendText("page is ").appendValue(pagination.page) + return false + } + } + pages?.let { + if (pagination.pageCount != pages) { + mismatchDescription.appendText("total pages is ").appendValue(pagination.pageCount) + return false + } + } + return true + } + + override fun describeTo(description: Description) { + page?.also { + description.appendText("is on page ").appendValue(page) + pages?.also { + description.appendText(" of ").appendValue(pages) + } + } ?: pages?.also { + description.appendText("has ").appendValue(pages).appendText(" pages") + } + } + + fun isOnPage(page: Int) = PaginationMatcher(page = page, pages = pages) + fun hasPages(pages: Int) = PaginationMatcher(page = page, pages = pages) + +} + +fun isOnPage(page: Int) = PaginationMatcher(page = page) +fun hasPages(pages: Int) = PaginationMatcher(pages = pages) diff --git a/src/test/kotlin/net/pterodactylus/sone/utils/PaginationTest.kt b/src/test/kotlin/net/pterodactylus/sone/utils/PaginationTest.kt index 9a1153c..dce14fe 100644 --- a/src/test/kotlin/net/pterodactylus/sone/utils/PaginationTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/utils/PaginationTest.kt @@ -1,5 +1,6 @@ package net.pterodactylus.sone.utils +import net.pterodactylus.sone.test.hasPages import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.contains import org.hamcrest.Matchers.equalTo @@ -14,6 +15,12 @@ class PaginationTest { private val pagination = Pagination(items, 2) @Test + fun `pagination can be created from iterable`() { + val pagination = listOf(1, 2, 3, 4, 5).asIterable().paginate(2) + assertThat(pagination, hasPages(3).isOnPage(0)) + } + + @Test fun `new pagination is at page 0`() { assertThat(pagination.page, equalTo(0)) }