-
Notifications
You must be signed in to change notification settings - Fork 1
/
pager_test.go
125 lines (114 loc) · 2.1 KB
/
pager_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package gopager
import (
"testing"
)
type TestStruct struct {
ID uint
Name string
Age uint
}
type TestStructs []TestStruct
func (t TestStructs) Len() int {
return len(t)
}
func TestPaginater(t *testing.T) {
obj := TestStructs{
{
ID: 1,
Name: "Mr.A",
Age: 20,
},
{
ID: 2,
Name: "Mr.B",
Age: 30,
},
{
ID: 3,
Name: "Mr.C",
Age: 25,
},
{
ID: 4,
Name: "Mr.D",
Age: 33,
},
{
ID: 5,
Name: "Mr.E",
Age: 34,
},
}
expectMaxPageNum := 3
// TEST: NewPaginater()
p := NewPaginater(TestStructs{}, 2)
if p.MaxPage() != 0 {
t.Fatalf("expect: 0, but actual: %v\n", p.MaxPage())
}
// TEST: MaxPage()
p = NewPaginater(obj, 2)
if p.MaxPage() != expectMaxPageNum {
t.Fatalf("expect: %v, but actual: %v\n", expectMaxPageNum, p.MaxPage())
}
// TEST: Current()
actual := TestStructs{}
p.Current(&actual)
if len(actual) != 0 {
t.Fatalf("expect: 0, but actual: %v\n", actual)
}
var currentTests = []struct {
in int
out int
}{
{
in: 1,
out: 2,
},
{
in: 2,
out: 2,
},
{
in: 3,
out: 1,
},
}
for _, ct := range currentTests {
actual := TestStructs{}
p.Page(ct.in).Current(&actual)
t.Logf("idx: %v actual: %+v\n", ct.in, actual)
if len(actual) != ct.out {
t.Fatalf("expect: %v, but actual: %v\n", ct.out, len(actual))
}
}
// TEST: Page() & CurrentPage()
p.Page(-1)
if p.CurrentPage() != 0 {
t.Fatalf("expect: 0, but actual: %v\n", p.CurrentPage())
}
p.Page(0)
// TEST: Next()
var nextTests = []int{2, 2, 1}
idx := 0
for p.HasNext() {
actual := TestStructs{}
p.Next(&actual)
t.Logf("curPage: %v actual: %+v\n", p.CurrentPage(), actual)
if len(actual) != nextTests[idx] {
t.Fatalf("expect: %v, but actual: %v\n", nextTests[idx], len(actual))
}
idx++
}
// TEST: Previous()
var previousTests = []int{2, 2}
idx = 0
for p.HasPrevious() {
actual := TestStructs{}
p.Previous(&actual)
t.Logf("curPage: %v actual: %+v\n", p.CurrentPage(), actual)
if len(actual) != previousTests[idx] {
t.Fatalf("expect: %v, but actual: %v\n", previousTests[idx], len(actual))
}
idx++
}
}