github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/paginator/paginator_test.go (about) 1 // Copyright 2023 The GitBundle Inc. All rights reserved. 2 // Copyright 2017 The Gitea Authors. All rights reserved. 3 // Use of this source code is governed by a MIT-style 4 // license that can be found in the LICENSE file. 5 6 // Copyright 2022 The GitBundle Authors. 7 // Copyright 2015 Unknwon. Licensed under the Apache License, Version 2.0 8 9 package paginator 10 11 import ( 12 "testing" 13 14 "github.com/stretchr/testify/assert" 15 ) 16 17 func TestPaginator(t *testing.T) { 18 t.Run("Basic logics", func(t *testing.T) { 19 p := New(0, -1, -1, 0) 20 assert.Equal(t, 1, p.PagingNum()) 21 assert.True(t, p.IsFirst()) 22 assert.False(t, p.HasPrevious()) 23 assert.Equal(t, 1, p.Previous()) 24 assert.False(t, p.HasNext()) 25 assert.Equal(t, 1, p.Next()) 26 assert.True(t, p.IsLast()) 27 assert.Equal(t, 0, p.Total()) 28 29 p = New(1, 10, 2, 0) 30 assert.Equal(t, 10, p.PagingNum()) 31 assert.True(t, p.IsFirst()) 32 assert.False(t, p.HasPrevious()) 33 assert.False(t, p.HasNext()) 34 assert.True(t, p.IsLast()) 35 36 p = New(10, 10, 1, 0) 37 assert.Equal(t, 10, p.PagingNum()) 38 assert.True(t, p.IsFirst()) 39 assert.False(t, p.HasPrevious()) 40 assert.False(t, p.HasNext()) 41 assert.True(t, p.IsLast()) 42 43 p = New(11, 10, 1, 0) 44 assert.Equal(t, 10, p.PagingNum()) 45 assert.True(t, p.IsFirst()) 46 assert.False(t, p.HasPrevious()) 47 assert.True(t, p.HasNext()) 48 assert.Equal(t, 2, p.Next()) 49 assert.False(t, p.IsLast()) 50 51 p = New(11, 10, 2, 0) 52 assert.Equal(t, 10, p.PagingNum()) 53 assert.False(t, p.IsFirst()) 54 assert.True(t, p.HasPrevious()) 55 assert.Equal(t, 1, p.Previous()) 56 assert.False(t, p.HasNext()) 57 assert.True(t, p.IsLast()) 58 59 p = New(20, 10, 2, 0) 60 assert.Equal(t, 10, p.PagingNum()) 61 assert.False(t, p.IsFirst()) 62 assert.True(t, p.HasPrevious()) 63 assert.False(t, p.HasNext()) 64 assert.True(t, p.IsLast()) 65 66 p = New(25, 10, 2, 0) 67 assert.Equal(t, 10, p.PagingNum()) 68 assert.False(t, p.IsFirst()) 69 assert.True(t, p.HasPrevious()) 70 assert.True(t, p.HasNext()) 71 assert.False(t, p.IsLast()) 72 }) 73 74 t.Run("Generate pages", func(t *testing.T) { 75 p := New(0, 10, 1, 0) 76 pages := p.Pages() 77 assert.Equal(t, 0, len(pages)) 78 }) 79 80 t.Run("Only current page", func(t *testing.T) { 81 p := New(0, 10, 1, 1) 82 pages := p.Pages() 83 assert.Equal(t, 1, len(pages)) 84 assert.Equal(t, 1, pages[0].Num()) 85 assert.True(t, pages[0].IsCurrent()) 86 87 p = New(1, 10, 1, 1) 88 pages = p.Pages() 89 assert.Equal(t, 1, len(pages)) 90 assert.Equal(t, 1, pages[0].Num()) 91 assert.True(t, pages[0].IsCurrent()) 92 }) 93 94 t.Run("Total page number is less or equal", func(t *testing.T) { 95 p := New(1, 10, 1, 2) 96 pages := p.Pages() 97 assert.Equal(t, 1, len(pages)) 98 assert.Equal(t, 1, pages[0].Num()) 99 assert.True(t, pages[0].IsCurrent()) 100 101 p = New(11, 10, 1, 2) 102 pages = p.Pages() 103 assert.Equal(t, 2, len(pages)) 104 assert.Equal(t, 1, pages[0].Num()) 105 assert.True(t, pages[0].IsCurrent()) 106 assert.Equal(t, 2, pages[1].Num()) 107 assert.False(t, pages[1].IsCurrent()) 108 109 p = New(11, 10, 2, 2) 110 pages = p.Pages() 111 assert.Equal(t, 2, len(pages)) 112 assert.Equal(t, 1, pages[0].Num()) 113 assert.False(t, pages[0].IsCurrent()) 114 assert.Equal(t, 2, pages[1].Num()) 115 assert.True(t, pages[1].IsCurrent()) 116 117 p = New(25, 10, 2, 3) 118 pages = p.Pages() 119 assert.Equal(t, 3, len(pages)) 120 assert.Equal(t, 1, pages[0].Num()) 121 assert.False(t, pages[0].IsCurrent()) 122 assert.Equal(t, 2, pages[1].Num()) 123 assert.True(t, pages[1].IsCurrent()) 124 assert.Equal(t, 3, pages[2].Num()) 125 assert.False(t, pages[2].IsCurrent()) 126 }) 127 128 t.Run("Has more previous pages ", func(t *testing.T) { 129 // ... 2 130 p := New(11, 10, 2, 1) 131 pages := p.Pages() 132 assert.Equal(t, 2, len(pages)) 133 assert.Equal(t, -1, pages[0].Num()) 134 assert.False(t, pages[0].IsCurrent()) 135 assert.Equal(t, 2, pages[1].Num()) 136 assert.True(t, pages[1].IsCurrent()) 137 138 // ... 2 3 139 p = New(21, 10, 2, 2) 140 pages = p.Pages() 141 assert.Equal(t, 3, len(pages)) 142 assert.Equal(t, -1, pages[0].Num()) 143 assert.False(t, pages[0].IsCurrent()) 144 assert.Equal(t, 2, pages[1].Num()) 145 assert.True(t, pages[1].IsCurrent()) 146 assert.Equal(t, 3, pages[2].Num()) 147 assert.False(t, pages[2].IsCurrent()) 148 149 // ... 2 3 4 150 p = New(31, 10, 3, 3) 151 pages = p.Pages() 152 assert.Equal(t, 4, len(pages)) 153 assert.Equal(t, -1, pages[0].Num()) 154 assert.False(t, pages[0].IsCurrent()) 155 assert.Equal(t, 2, pages[1].Num()) 156 assert.False(t, pages[1].IsCurrent()) 157 assert.Equal(t, 3, pages[2].Num()) 158 assert.True(t, pages[2].IsCurrent()) 159 assert.Equal(t, 4, pages[3].Num()) 160 assert.False(t, pages[3].IsCurrent()) 161 162 // ... 3 4 5 163 p = New(41, 10, 4, 3) 164 pages = p.Pages() 165 assert.Equal(t, 4, len(pages)) 166 assert.Equal(t, -1, pages[0].Num()) 167 assert.False(t, pages[0].IsCurrent()) 168 assert.Equal(t, 3, pages[1].Num()) 169 assert.False(t, pages[1].IsCurrent()) 170 assert.Equal(t, 4, pages[2].Num()) 171 assert.True(t, pages[2].IsCurrent()) 172 assert.Equal(t, 5, pages[3].Num()) 173 assert.False(t, pages[3].IsCurrent()) 174 175 // ... 4 5 6 7 8 9 10 176 p = New(100, 10, 9, 7) 177 pages = p.Pages() 178 assert.Equal(t, 8, len(pages)) 179 assert.Equal(t, -1, pages[0].Num()) 180 assert.False(t, pages[0].IsCurrent()) 181 assert.Equal(t, 4, pages[1].Num()) 182 assert.False(t, pages[1].IsCurrent()) 183 assert.Equal(t, 5, pages[2].Num()) 184 assert.False(t, pages[2].IsCurrent()) 185 assert.Equal(t, 6, pages[3].Num()) 186 assert.False(t, pages[3].IsCurrent()) 187 assert.Equal(t, 7, pages[4].Num()) 188 assert.False(t, pages[4].IsCurrent()) 189 assert.Equal(t, 8, pages[5].Num()) 190 assert.False(t, pages[5].IsCurrent()) 191 assert.Equal(t, 9, pages[6].Num()) 192 assert.True(t, pages[6].IsCurrent()) 193 assert.Equal(t, 10, pages[7].Num()) 194 assert.False(t, pages[7].IsCurrent()) 195 }) 196 197 t.Run("Has more next pages", func(t *testing.T) { 198 // 1 ... 199 p := New(21, 10, 1, 1) 200 pages := p.Pages() 201 assert.Equal(t, 2, len(pages)) 202 assert.Equal(t, 1, pages[0].Num()) 203 assert.True(t, pages[0].IsCurrent()) 204 assert.Equal(t, -1, pages[1].Num()) 205 assert.False(t, pages[1].IsCurrent()) 206 207 // 1 2 ... 208 p = New(21, 10, 1, 2) 209 pages = p.Pages() 210 assert.Equal(t, 3, len(pages)) 211 assert.Equal(t, 1, pages[0].Num()) 212 assert.True(t, pages[0].IsCurrent()) 213 assert.Equal(t, 2, pages[1].Num()) 214 assert.False(t, pages[1].IsCurrent()) 215 assert.Equal(t, -1, pages[2].Num()) 216 assert.False(t, pages[2].IsCurrent()) 217 218 // 1 2 3 ... 219 p = New(31, 10, 2, 3) 220 pages = p.Pages() 221 assert.Equal(t, 4, len(pages)) 222 assert.Equal(t, 1, pages[0].Num()) 223 assert.False(t, pages[0].IsCurrent()) 224 assert.Equal(t, 2, pages[1].Num()) 225 assert.True(t, pages[1].IsCurrent()) 226 assert.Equal(t, 3, pages[2].Num()) 227 assert.False(t, pages[2].IsCurrent()) 228 assert.Equal(t, -1, pages[3].Num()) 229 assert.False(t, pages[3].IsCurrent()) 230 231 // 1 2 3 ... 232 p = New(41, 10, 2, 3) 233 pages = p.Pages() 234 assert.Equal(t, 4, len(pages)) 235 assert.Equal(t, 1, pages[0].Num()) 236 assert.False(t, pages[0].IsCurrent()) 237 assert.Equal(t, 2, pages[1].Num()) 238 assert.True(t, pages[1].IsCurrent()) 239 assert.Equal(t, 3, pages[2].Num()) 240 assert.False(t, pages[2].IsCurrent()) 241 assert.Equal(t, -1, pages[3].Num()) 242 assert.False(t, pages[3].IsCurrent()) 243 244 // 1 2 3 4 5 6 7 ... 245 p = New(100, 10, 1, 7) 246 pages = p.Pages() 247 assert.Equal(t, 8, len(pages)) 248 assert.Equal(t, 1, pages[0].Num()) 249 assert.True(t, pages[0].IsCurrent()) 250 assert.Equal(t, 2, pages[1].Num()) 251 assert.False(t, pages[1].IsCurrent()) 252 assert.Equal(t, 3, pages[2].Num()) 253 assert.False(t, pages[2].IsCurrent()) 254 assert.Equal(t, 4, pages[3].Num()) 255 assert.False(t, pages[3].IsCurrent()) 256 assert.Equal(t, 5, pages[4].Num()) 257 assert.False(t, pages[4].IsCurrent()) 258 assert.Equal(t, 6, pages[5].Num()) 259 assert.False(t, pages[5].IsCurrent()) 260 assert.Equal(t, 7, pages[6].Num()) 261 assert.False(t, pages[6].IsCurrent()) 262 assert.Equal(t, -1, pages[7].Num()) 263 assert.False(t, pages[7].IsCurrent()) 264 265 // 1 2 3 4 5 6 7 ... 266 p = New(100, 10, 2, 7) 267 pages = p.Pages() 268 assert.Equal(t, 8, len(pages)) 269 assert.Equal(t, 1, pages[0].Num()) 270 assert.False(t, pages[0].IsCurrent()) 271 assert.Equal(t, 2, pages[1].Num()) 272 assert.True(t, pages[1].IsCurrent()) 273 assert.Equal(t, 3, pages[2].Num()) 274 assert.False(t, pages[2].IsCurrent()) 275 assert.Equal(t, 4, pages[3].Num()) 276 assert.False(t, pages[3].IsCurrent()) 277 assert.Equal(t, 5, pages[4].Num()) 278 assert.False(t, pages[4].IsCurrent()) 279 assert.Equal(t, 6, pages[5].Num()) 280 assert.False(t, pages[5].IsCurrent()) 281 assert.Equal(t, 7, pages[6].Num()) 282 assert.False(t, pages[6].IsCurrent()) 283 assert.Equal(t, -1, pages[7].Num()) 284 assert.False(t, pages[7].IsCurrent()) 285 }) 286 287 t.Run("Has both more previous and next pages", func(t *testing.T) { 288 // ... 2 3 ... 289 p := New(35, 10, 2, 2) 290 pages := p.Pages() 291 assert.Equal(t, 4, len(pages)) 292 assert.Equal(t, -1, pages[0].Num()) 293 assert.False(t, pages[0].IsCurrent()) 294 assert.Equal(t, 2, pages[1].Num()) 295 assert.True(t, pages[1].IsCurrent()) 296 assert.Equal(t, 3, pages[2].Num()) 297 assert.False(t, pages[2].IsCurrent()) 298 assert.Equal(t, -1, pages[3].Num()) 299 assert.False(t, pages[3].IsCurrent()) 300 301 // ... 2 3 4 ... 302 p = New(49, 10, 3, 3) 303 pages = p.Pages() 304 assert.Equal(t, 5, len(pages)) 305 assert.Equal(t, -1, pages[0].Num()) 306 assert.False(t, pages[0].IsCurrent()) 307 assert.Equal(t, 2, pages[1].Num()) 308 assert.False(t, pages[1].IsCurrent()) 309 assert.Equal(t, 3, pages[2].Num()) 310 assert.True(t, pages[2].IsCurrent()) 311 assert.Equal(t, 4, pages[3].Num()) 312 assert.False(t, pages[3].IsCurrent()) 313 assert.Equal(t, -1, pages[4].Num()) 314 assert.False(t, pages[4].IsCurrent()) 315 }) 316 }