--- /dev/null
+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<Pagination<*>>() {
+
+ 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)
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
private val pagination = Pagination<Int>(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))
}