-
Notifications
You must be signed in to change notification settings - Fork 2
/
arr_test.go
267 lines (258 loc) · 5.35 KB
/
arr_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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package m2obj
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestArrs(t *testing.T) {
obj := New(Group{
"arr": Array{
"hello",
2,
Group{
"1": 1,
},
},
})
// errors
t.Run("Errors Test", func(t *testing.T) {
assert.Panics(t, func() {
obj.ArrPush(100)
})
assert.Panics(t, func() {
obj.ArrPop()
})
assert.Panics(t, func() {
obj.ArrSet(0, 100)
})
assert.Panics(t, func() {
obj.ArrGet(0)
})
assert.Panics(t, func() {
obj.ArrInsert(0, 100)
})
assert.Panics(t, func() {
obj.ArrRemove(0)
})
assert.Panics(t, func() {
obj.ArrForeach(func(i int, obj *Object) error { return nil })
})
assert.Panics(t, func() {
obj.ArrLen()
})
})
var arr *Object
assert.NotPanics(t, func() {
arr = obj.MustGet("arr")
})
// push/len
assert.NotPanics(t, func() {
arr.ArrPush(100)
})
assert.Equal(t, 4, arr.ArrLen())
assert.Equal(t, map[string]interface{}{
"arr": []interface{}{
"hello",
2,
map[string]interface{}{
"1": 1,
},
100,
},
}, obj.Staticize())
// pop
assert.NotPanics(t, func() {
arr.ArrPop()
})
assert.Equal(t, 3, arr.ArrLen())
assert.Equal(t, map[string]interface{}{
"arr": []interface{}{
"hello",
2,
map[string]interface{}{
"1": 1,
},
},
}, obj.Staticize())
// set/get
assert.NotPanics(t, func() {
assert.Equal(t, map[string]interface{}{
"1": 1,
}, arr.MustGet("[2]").Staticize())
arr.ArrSet(2, 100)
assert.Equal(t, 100, arr.MustGet("[2]").ValInt())
assert.Equal(t, 100, arr.ArrGet(2).ValInt())
})
// insert
assert.Panics(t, func() {
arr.ArrInsert(arr.ArrLen()+1, "awd")
})
assert.Panics(t, func() {
arr.ArrInsert(-1, "awd")
})
assert.NotPanics(t, func() {
arr.ArrInsert(arr.ArrLen(), "awd")
arr.ArrInsert(0, "awd")
arr.ArrInsert(2, "awd")
assert.Equal(t, map[string]interface{}{
"arr": []interface{}{
"awd",
"hello",
"awd",
2,
100,
"awd",
},
}, obj.Staticize())
})
// remove
assert.Panics(t, func() {
arr.ArrRemove(arr.ArrLen())
})
assert.Panics(t, func() {
arr.ArrRemove(-1)
})
assert.NotPanics(t, func() {
arr.ArrRemove(0)
arr.ArrRemove(0)
arr.ArrRemove(3)
arr.ArrRemove(1)
assert.Equal(t, map[string]interface{}{
"arr": []interface{}{
"awd",
100,
},
}, obj.Staticize())
})
// foreach
assert.NotPanics(t, func() {
assert.NoError(t, arr.ArrForeach(func(index int, obj *Object) error {
obj.SetVal(false)
return nil
}))
assert.Equal(t, map[string]interface{}{
"arr": []interface{}{
false,
false,
},
}, obj.Staticize())
})
}
func TestArrs2(t *testing.T) {
arr := New(Array{
"1",
2,
true,
})
assert.NotPanics(t, func() {
arr.ArrShift()
})
assert.Equal(t, New(Array{
2,
true,
}).Staticize(), arr.Staticize())
assert.NotPanics(t, func() {
arr.ArrUnshift("1")
})
assert.Equal(t, New(Array{
"1",
2,
true,
}).Staticize(), arr.Staticize())
}
func TestObject_ArrPushArr(t *testing.T) {
arr1 := New(Array{1, 2, 3})
arr2 := New(Array{4, 5, "6"})
obj := New(Group{"1": 1})
assert.Panics(t, func() {
arr1.ArrMerge(obj)
})
assert.Panics(t, func() {
obj.ArrMerge(arr1)
})
assert.NotPanics(t, func() {
arr1.ArrMerge(arr2)
})
assert.Equal(t, New(Array{1, 2, 3, 4, 5, "6"}).Staticize(), arr1.Staticize())
assert.NotPanics(t, func() {
arr1.ArrMerge(New([]interface{}{7, 8, 9}))
})
assert.Equal(t, New(Array{1, 2, 3, 4, 5, "6", 7, 8, 9}).Staticize(), arr1.Staticize())
}
func TestObject_ArrPushAll(t *testing.T) {
arr1 := New(Array{1, 2, 3})
arr2 := []int{4, 5, 6}
obj := New(Group{"1": 1})
// type error
assert.Panics(t, func() {
obj.ArrPushAll(4, 5, 6)
})
// ... add
assert.NotPanics(t, func() {
arr1.ArrPushAll(4, 5, 6)
})
assert.Equal(t, New(Array{1, 2, 3, 4, 5, 6}).Staticize(), arr1.Staticize())
// slice to []interface{} then ... add
arr1 = New(Array{1, 2, 3})
arrInterface2 := make([]interface{}, len(arr2))
for i, v := range arr2 {
arrInterface2[i] = v
}
assert.NotPanics(t, func() {
arr1.ArrPushAll(arrInterface2...)
})
assert.Equal(t, New(Array{1, 2, 3, 4, 5, 6}).Staticize(), arr1.Staticize())
// slice for each add
arr1 = New(Array{1, 2, 3})
for _, v := range arr2 {
assert.NotPanics(t, func() {
arr1.ArrPush(v)
})
}
assert.Equal(t, New(Array{1, 2, 3, 4, 5, 6}).Staticize(), arr1.Staticize())
}
func TestObject_ArrForeach(t *testing.T) {
var (
arr1 *Object
arr2 *Object
arr3 []int
)
arr1 = New(Array{1, 2, 3})
arr2 = New(Array{1, 2, "3"})
arr3 = make([]int, 0)
// no error and no panic
assert.NotPanics(t, func() {
assert.NoError(t, arr1.ArrForeach(func(index int, obj *Object) error {
arr3 = append(arr3, obj.ValInt())
return nil
}))
})
assert.Equal(t, []int{1, 2, 3}, arr3)
arr3 = make([]int, 0)
// transform the panic to error
assert.NotPanics(t, func() {
assert.Error(t, arr2.ArrForeach(func(index int, obj *Object) error {
switch obj.Val().(type) {
case int:
arr3 = append(arr3, obj.ValInt())
return nil
default:
return invalidTypeErr("")
}
}))
})
assert.Equal(t, []int{1, 2}, arr3)
arr3 = make([]int, 0)
// throw the panic directly
assert.Panics(t, func() {
assert.NoError(t, arr2.ArrForeach(func(index int, obj *Object) error {
arr3 = append(arr3, obj.ValInt())
return nil
}))
})
assert.Equal(t, []int{1, 2}, arr3)
assert.Panics(t, func() {
New(arr3).ArrForeach(func(index int, obj *Object) error {
return nil
})
})
}