Skip to content

Commit 4ada0f3

Browse files
committed
Create pagination_test.go
1 parent 8110dd8 commit 4ada0f3

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

src/pagination_test.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package src
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestPaginate(t *testing.T) {
9+
tests := []struct {
10+
name string
11+
totalItems int
12+
currentPage int
13+
pageSize int
14+
expectedStart int
15+
expectedEnd int
16+
expectedBtns []string
17+
}{
18+
{
19+
name: "Page 1 of 1 (Total 5, Size 7)",
20+
totalItems: 5,
21+
currentPage: 1,
22+
pageSize: 7,
23+
expectedStart: 0,
24+
expectedEnd: 5,
25+
expectedBtns: []string{"· 1 ·"},
26+
},
27+
{
28+
name: "Page 1 of 2 (Total 10, Size 7)",
29+
totalItems: 10,
30+
currentPage: 1,
31+
pageSize: 7,
32+
expectedStart: 0,
33+
expectedEnd: 7,
34+
expectedBtns: []string{"· 1 ·", "2", "Next >"},
35+
},
36+
{
37+
name: "Page 2 of 2 (Total 10, Size 7)",
38+
totalItems: 10,
39+
currentPage: 2,
40+
pageSize: 7,
41+
expectedStart: 7,
42+
expectedEnd: 10,
43+
expectedBtns: []string{"< Prev", "1", "· 2 ·"},
44+
},
45+
{
46+
name: "Page 1 of 3 (Total 20, Size 7)",
47+
totalItems: 20,
48+
currentPage: 1,
49+
pageSize: 7,
50+
expectedStart: 0,
51+
expectedEnd: 7,
52+
expectedBtns: []string{"· 1 ·", "2", "3", "Next >"},
53+
},
54+
{
55+
name: "Page 2 of 3 (Total 20, Size 7)",
56+
totalItems: 20,
57+
currentPage: 2,
58+
pageSize: 7,
59+
expectedStart: 7,
60+
expectedEnd: 14,
61+
expectedBtns: []string{"< Prev", "1", "· 2 ·", "3", "Next >"},
62+
},
63+
{
64+
name: "Page 3 of 3 (Total 20, Size 7)",
65+
totalItems: 20,
66+
currentPage: 3,
67+
pageSize: 7,
68+
expectedStart: 14,
69+
expectedEnd: 20,
70+
expectedBtns: []string{"< Prev", "1", "2", "· 3 ·"},
71+
},
72+
{
73+
name: "Page 5 of 10 (Total 70, Size 7)",
74+
totalItems: 70,
75+
currentPage: 5,
76+
pageSize: 7,
77+
expectedStart: 28,
78+
expectedEnd: 35,
79+
expectedBtns: []string{"< Prev", "4", "· 5 ·", "6", "Next >"},
80+
},
81+
{
82+
name: "Page 10 of 10 (Total 70, Size 7)",
83+
totalItems: 70,
84+
currentPage: 10,
85+
pageSize: 7,
86+
expectedStart: 63,
87+
expectedEnd: 70,
88+
expectedBtns: []string{"< Prev", "8", "9", "· 10 ·"},
89+
},
90+
}
91+
92+
for _, tt := range tests {
93+
t.Run(tt.name, func(t *testing.T) {
94+
start, end, buttons := Paginate(tt.totalItems, tt.currentPage, tt.pageSize, "cb:")
95+
if start != tt.expectedStart {
96+
t.Errorf("start = %d, want %d", start, tt.expectedStart)
97+
}
98+
if end != tt.expectedEnd {
99+
t.Errorf("end = %d, want %d", end, tt.expectedEnd)
100+
}
101+
102+
if len(buttons) != len(tt.expectedBtns) {
103+
t.Errorf("got %d buttons, want %d", len(buttons), len(tt.expectedBtns))
104+
for _, b := range buttons {
105+
fmt.Printf("Got button: %s\n", b.Text)
106+
}
107+
return
108+
}
109+
for i, btn := range buttons {
110+
if btn.Text != tt.expectedBtns[i] {
111+
t.Errorf("button[%d] text = %s, want %s", i, btn.Text, tt.expectedBtns[i])
112+
}
113+
}
114+
})
115+
}
116+
}

0 commit comments

Comments
 (0)