1 package net.pterodactylus.sone.utils
3 import net.pterodactylus.sone.test.hasPages
4 import org.hamcrest.MatcherAssert.assertThat
5 import org.hamcrest.Matchers.contains
6 import org.hamcrest.Matchers.equalTo
10 * Unit test for [Pagination].
12 class PaginationTest {
14 private val items = listOf(1, 2, 3, 4, 5)
15 private val pagination = Pagination<Int>(items, 2)
18 fun `pagination can be created from iterable`() {
19 val pagination = listOf(1, 2, 3, 4, 5).asIterable().paginate(2)
20 assertThat(pagination, hasPages(3).isOnPage(0))
24 fun `new pagination is at page 0`() {
25 assertThat(pagination.page, equalTo(0))
29 fun `new pagination is at page number 1`() {
30 assertThat(pagination.pageNumber, equalTo(1))
34 fun `setting a page to less than 0 sets page to 0`() {
36 assertThat(pagination.page, equalTo(0))
40 fun `setting page to a valid page sets page`() {
42 assertThat(pagination.page, equalTo(1))
46 fun `setting a too large page will cap the page`() {
48 assertThat(pagination.page, equalTo(2))
52 fun `the page count is returned correctly`() {
53 assertThat(pagination.pageCount, equalTo(3))
57 fun `page size is returned correctly`() {
58 assertThat(pagination.pageSize, equalTo(2))
62 fun `a page size of less than 1 is set to 1`() {
63 pagination.pageSize = 0
64 assertThat(pagination.pageSize, equalTo(1))
68 fun `changing page size sets new page correctly`() {
70 pagination.pageSize = 1
71 assertThat(pagination.page, equalTo(2))
75 fun `changing page size to very large returns to page 0`() {
77 pagination.pageSize = 20
78 assertThat(pagination.page, equalTo(0))
82 fun `item count for current page is page size of first page`() {
83 assertThat(pagination.itemCount, equalTo(2))
87 fun `item count for last page is 1`() {
89 assertThat(pagination.itemCount, equalTo(1))
93 fun `items on first page are returned correctly`() {
94 assertThat(pagination.items, contains(1, 2))
98 fun `items on last page are returned correctly`() {
100 assertThat(pagination.items, contains(5))
104 fun `pagination is first on first page`() {
105 assertThat(pagination.isFirst, equalTo(true))
109 fun `pagination is not first on second page`() {
111 assertThat(pagination.isFirst, equalTo(false))
115 fun `pagination is not first on last page`() {
117 assertThat(pagination.isFirst, equalTo(false))
121 fun `pagination is not last on first page`() {
122 assertThat(pagination.isLast, equalTo(false))
126 fun `pagination is not last on second page`() {
128 assertThat(pagination.isLast, equalTo(false))
132 fun `pagination is last on last page`() {
134 assertThat(pagination.isLast, equalTo(true))
138 fun `pagination is necessary for three pages`() {
139 assertThat(pagination.isNecessary, equalTo(true))
143 fun `pagination is necessary for two pages`() {
144 pagination.pageSize = 4
145 assertThat(pagination.isNecessary, equalTo(true))
149 fun `pagination is not necessary for one page`() {
150 pagination.pageSize = 20
151 assertThat(pagination.isNecessary, equalTo(false))
155 fun `previous page is returned correctly for second page`() {
157 assertThat(pagination.previousPage, equalTo(0))
161 fun `previous page is returned correctly for last page`() {
163 assertThat(pagination.previousPage, equalTo(1))
167 fun `next page is returned correctly for first page`() {
168 assertThat(pagination.nextPage, equalTo(1))
172 fun `next page is returned correctly for second page`() {
174 assertThat(pagination.nextPage, equalTo(2))
178 fun `last page is returned correctly`() {
179 assertThat(pagination.lastPage, equalTo(2))
183 fun `iterator returns items on the current page`() {
184 assertThat(pagination.iterator().asSequence().toList(), contains(1, 2))