-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection_test.go
178 lines (158 loc) · 3.98 KB
/
collection_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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package collection
import (
"testing"
)
type User struct {
Id int `json:"id"`
Age int `json:"age"`
Name string `json:"name"`
}
func TestIntSliceError(t *testing.T) {
type UserTmp struct {
Id int `json:"id"`
Age int `json:"age"`
Name []string `json:"name"`
}
data := []UserTmp{
UserTmp{1, 32, []string{"hello1"}},
UserTmp{2, 32, []string{"hello2"}},
UserTmp{3, 32, []string{"hello2"}},
UserTmp{4, 32, []string{"hello3"}},
}
ret := New().Value(data).Field("Name").StringSlice() // []string{"[hello1]", "[hello2]", "[hello2]", "[hello3]"}
if len(ret) != 4 {
t.Fatalf("should be == 4")
}
}
func TestIntUniqueSlice(t *testing.T) {
data := []User{
User{1, 32, "hello1"},
User{2, 32, "hello2"},
User{3, 32, "hello2"},
User{4, 32, "hello3"},
}
ret := New().Value(data).Field("Name").StringSlice() // [hello1 hello2 hello2 hello3]
if len(ret) != 4 {
t.Fatalf("should be == 4")
}
ret = New().Value(data).Field("Name").Unique().StringSlice() // [hello1 hello2 hello3]
if len(ret) != 3 {
t.Fatalf("should be == 3")
}
}
func TestStringMap(t *testing.T) {
data := []User{
User{1, 32, "hello1"},
User{2, 32, "hello2"},
User{3, 32, "hello2"},
User{4, 32, "hello3"},
}
ret := New().Value(data).Field("Name").StringMap()
/*
expect: map[string]interface{} {
"hello1": interface{User{1, 32, "hello1"}},
"hello2": interface{User{3, 32, "hello2"} }, // 其中User{2, 32, "hello2"}会被覆盖掉
"hello3": interface{User{4, 32, "hello3"}},
}
*/
if len(ret) != 3 {
t.Fatalf("should be == 3")
}
if ret["hello2"].(User).Id != 3 {
t.Fatalf("id should be == 3")
}
}
func TestStringMapSlice(t *testing.T) {
data := []User{
User{1, 32, "hello1"},
User{2, 32, "hello2"},
User{3, 32, "hello2"},
User{4, 32, "hello3"},
}
ret := New().Value(data).Field("Name").StringMapSlice()
/*
expect: map[string][]interface{} {
"hello1": []interface{User{1, 32, "hello1"}},
"hello2": []interface{User{2, 32, "hello2"} , User{3, 32, "hello2"} },
"hello3": []interface{User{4, 32, "hello3"}},
}
*/
if len(ret) != 3 {
t.Fatalf("should be == 3")
}
if ret["hello2"][0].(User).Id != 2 {
t.Fatalf("id should be == 2")
}
if ret["hello2"][1].(User).Id != 3 {
t.Fatalf("id should be == 2")
}
}
func TestMapValue(t *testing.T) {
data := map[string]User{
"hello1": User{1, 32, "hello1"},
"hello2": User{2, 32, "hello2"},
}
ret := New().Value(data).Field("Id").IntSlice() // []int{1,2}
if len(ret) != 2 {
t.Fatalf("should be == 1")
}
//if ret[0] != 1 || ret[1] != 2 { // 存在 []int{2,1} 情况
//t.Fatalf("should be == []int{1,2} , but is :%v", ret)
//}
}
func TestInputNotSlice(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Fatalf("err can not be nil , should be panic ")
}
}()
data := "abc"
New().Value(data).Field("Id").IntSlice() // 'id' is not exists
}
func TestIsNotStruct(t *testing.T) {
data := []string{"a", "b"}
ret := New().Value(data).Field("Id").IntSlice() // 'id' is not exists
if len(ret) != 0 {
t.Fatalf("should be == 0")
}
}
func TestPtrIsNil(t *testing.T) {
data := []*User{
nil,
&User{2, 32, "hello2"},
}
ret := New().Value(data).Field("Id").IntSlice() // 'id' is not exists
if len(ret) != 1 {
t.Fatalf("should be == 1")
}
if ret[0] != 2 {
t.Fatalf("should be == 2")
}
}
func TestFieldNotExists(t *testing.T) {
data := getData()
ret := New().Value(data).Field("id").IntSlice() // 'id' is not exists
if len(ret) != 0 {
t.Fatalf("length should be == 0")
}
}
func getData() []User {
return []User{
User{1, 32, "hello1"},
User{2, 33, "hello2"},
}
}
func TestIntSlice(t *testing.T) {
data := getData()
ret := New().Value(data).Field("Id").IntSlice() // []int{1,2}
if ret[0] != 1 || ret[1] != 2 {
t.Fatalf("should be == []int{1,2}")
}
}
func TestStringSlice(t *testing.T) {
data := getData()
ret := New().Value(data).Field("Name").StringSlice() // []int{1,2}
if ret[0] != "hello1" || ret[1] != "hello2" {
t.Fatalf("should be == []int{'hello1','hello2'}")
}
}