🔀 Merge branch 'release/v82'
[Sone.git] / sone / test / PaginationMatcher.kt
1 package net.pterodactylus.sone.test
2
3 import net.pterodactylus.sone.utils.Pagination
4 import org.hamcrest.Description
5 import org.hamcrest.TypeSafeDiagnosingMatcher
6
7 /**
8  * Hamcrest matcher for [Pagination]s.
9  */
10 class PaginationMatcher(private val page: Int? = null, private val pages: Int? = null):
11                 TypeSafeDiagnosingMatcher<Pagination<*>>() {
12
13         override fun matchesSafely(pagination: Pagination<*>, mismatchDescription: Description): Boolean {
14                 page?.let {
15                         if (pagination.page != page) {
16                                 mismatchDescription.appendText("page is ").appendValue(pagination.page)
17                                 return false
18                         }
19                 }
20                 pages?.let {
21                         if (pagination.pageCount != pages) {
22                                 mismatchDescription.appendText("total pages is ").appendValue(pagination.pageCount)
23                                 return false
24                         }
25                 }
26                 return true
27         }
28
29         override fun describeTo(description: Description) {
30                 page?.also {
31                         description.appendText("is on page ").appendValue(page)
32                         pages?.also {
33                                         description.appendText(" of ").appendValue(pages)
34                         }
35                 } ?: pages?.also {
36                         description.appendText("has ").appendValue(pages).appendText(" pages")
37                 }
38         }
39
40         fun isOnPage(page: Int) = PaginationMatcher(page = page, pages = pages)
41         fun hasPages(pages: Int) = PaginationMatcher(page = page, pages = pages)
42
43 }
44
45 fun isOnPage(page: Int) = PaginationMatcher(page = page)
46 fun hasPages(pages: Int) = PaginationMatcher(pages = pages)