Add paginate() extension method and pagination matcher
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 29 May 2017 04:53:37 +0000 (06:53 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 4 Jun 2017 09:55:27 +0000 (11:55 +0200)
src/main/kotlin/net/pterodactylus/sone/utils/Pagination.kt
src/test/kotlin/net/pterodactylus/sone/test/PaginationMatcher.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/utils/PaginationTest.kt

index 130fe2e..6c436e2 100644 (file)
@@ -40,3 +40,5 @@ class Pagination<out T>(private val originalItems: List<T>, pageSize: Int): Iter
        override fun iterator() = items.iterator()
 
 }
+
+fun <T> Iterable<T>.paginate(pageSize: Int) = Pagination<T>(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 (file)
index 0000000..5a47ca7
--- /dev/null
@@ -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<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)
index 9a1153c..dce14fe 100644 (file)
@@ -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<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))
        }