-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_test.go
306 lines (245 loc) · 7.22 KB
/
list_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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package geko_test
import (
"encoding/json"
"math/rand"
"reflect"
"testing"
"github.com/7sDream/geko"
)
func TestList_New(t *testing.T) {
l := geko.NewList[int]()
if l.List != nil {
t.Fatalf("NewList inner slice is not nil")
}
l2 := geko.NewListFrom([]int{1, 2, 3})
if !reflect.DeepEqual(l2.List, []int{1, 2, 3}) {
t.Fatalf("NewListFrom doesn't store origin slice")
}
}
func TestList_NewWithCapacity(t *testing.T) {
l := geko.NewListWithCapacity[int](12)
if cap(l.List) != 12 {
t.Fatalf("NewListWithCapacity inner slice does not have correct capacity")
}
}
func TestList_Get(t *testing.T) {
l := geko.NewListFrom([]int{1, 2, 3})
if !willPanic(func() {
l.Get(-1)
}) {
t.Fatalf("Get doesn't panic with negative index")
}
if !willPanic(func() {
l.Get(4)
}) {
t.Fatalf("Get doesn't panic with out-of-bound index")
}
if l.Get(1) != 2 {
t.Fatalf("Get doesn't return correct value")
}
}
func TestList_Set(t *testing.T) {
l := geko.NewListFrom([]int{1, 2, 3})
if !willPanic(func() {
l.Set(-1, 0)
}) {
t.Fatalf("Set doesn't panic with negative index")
}
if !willPanic(func() {
l.Set(6, 0)
}) {
t.Fatalf("Set doesn't panic with out-of-bound index")
}
l.Set(1, 10)
if l.Get(1) != 10 {
t.Fatalf("Set not effect")
}
}
func TestList_Append(t *testing.T) {
l := geko.NewList[int]()
l.Append(1, 2, 3)
if !reflect.DeepEqual(l.List, []int{1, 2, 3}) {
t.Fatalf("Append not correct")
}
}
func TestList_Delete(t *testing.T) {
l := geko.NewListFrom([]int{1, 2, 3})
if !willPanic(func() {
l.Delete(-1)
}) {
t.Fatalf("Delete doesn't panic with negative index")
}
if !willPanic(func() {
l.Delete(3)
}) {
t.Fatalf("Delete doesn't panic with out-of-bound index")
}
l.Delete(1)
if !reflect.DeepEqual(l.List, []int{1, 3}) {
t.Fatalf("Delete not correct")
}
}
func TestList_Len(t *testing.T) {
for times := 0; times < 20; times++ {
l := geko.NewList[int]()
exceptedLength := rand.Int() % 100
for i := 0; i < exceptedLength; i++ {
l.Append(i)
}
length := l.Len()
if length != exceptedLength {
t.Fatalf("Len excepted %d, got %d", exceptedLength, length)
}
}
}
func TestList_MarshalJSON_Nil(t *testing.T) {
var l *geko.List[int]
output, err := json.Marshal(l)
if err != nil {
t.Fatalf("Marshal nil list with error: %s", err.Error())
}
if string(output) != `null` {
t.Fatalf("Marshal result %s not correct", string(output))
}
}
func TestList_MarshalJSON_InternalNilList(t *testing.T) {
l := geko.NewList[int]()
output, err := json.Marshal(l)
if err != nil {
t.Fatalf("Marshal empty list with error: %s", err.Error())
}
if string(output) != `[]` {
t.Fatalf("Marshal result %s not correct", string(output))
}
}
func TestList_MarshalJSON_ConcreteType(t *testing.T) {
l := geko.NewListFrom[int]([]int{1, 2, 3})
output, err := json.Marshal(l)
if err != nil {
t.Fatalf("Marshal empty list with error: %s", err.Error())
}
if string(output) != `[1,2,3]` {
t.Fatalf("Marshal result %s not correct", string(output))
}
}
func TestList_MarshalJSON_AnyType(t *testing.T) {
l := geko.NewListFrom[any]([]any{
1, 2.5, true, nil,
map[string]int{"a": 1},
geko.NewMap[string, any](),
})
output, err := json.Marshal(l)
if err != nil {
t.Fatalf("Marshal empty list with error: %s", err.Error())
}
if string(output) != `[1,2.5,true,null,{"a":1},{}]` {
t.Fatalf("Marshal result %s not correct", string(output))
}
}
func TestList_UnmarshalJSON_DirectlyCallWithInvalidData(t *testing.T) {
l := geko.NewList[any]()
if err := l.UnmarshalJSON([]byte("")); err == nil {
t.Fatalf("Should report error with empty input")
}
if err := l.UnmarshalJSON([]byte(`x`)); err == nil {
t.Fatalf("Should report error with invalid input")
}
}
func TestList_UnmarshalJSON_NilList(t *testing.T) {
var l geko.Array
if err := json.Unmarshal([]byte(`[1]`), l); err == nil {
t.Fatalf("Unmarshal into nil list should report error")
}
// *List = std slice, so this format is better, it supports null
if err := json.Unmarshal([]byte(`[1]`), &l); err != nil {
t.Fatalf("Unmarshal into pointer to nil list with error: %s", err.Error())
}
var m2 = geko.NewList[any]()
if err := json.Unmarshal([]byte(`null`), &m2); err != nil {
t.Fatalf("Unmarshal into pointer to nil list with error: %s", err.Error())
}
if m2 != nil {
t.Fatalf("Unmarshal null into Map do not get nil")
}
}
func TestList_UnmarshalJSON_UnmatchedType(t *testing.T) {
unmarshalWillReportError[*json.UnmarshalTypeError](t, "{}", geko.NewList[any]())
unmarshalWillReportError[*json.UnmarshalTypeError](t, "4", geko.NewList[any]())
unmarshalWillReportError[*json.UnmarshalTypeError](t, `"string"`, geko.NewList[any]())
unmarshalWillReportError[*json.UnmarshalTypeError](t, "true", geko.NewList[any]())
}
func TestList_UnmarshalJSON_UnmatchedValueType(t *testing.T) {
unmarshalWillReportError[*json.UnmarshalTypeError](t, `["str"]`, geko.NewList[int]())
unmarshalWillReportError[*json.UnmarshalTypeError](t, `["str"]`, geko.NewList[s]())
}
func TestList_UnmarshalJSON_ConcreteType(t *testing.T) {
l := geko.NewList[int]()
if err := json.Unmarshal([]byte(`[1]`), &l); err != nil {
t.Fatalf("Unmarshal with error: %s", err.Error())
}
l2 := geko.NewList[s]()
if err := json.Unmarshal([]byte(`[{"s": "good"}]`), &l2); err != nil {
t.Fatalf("Unmarshal with error: %s", err.Error())
}
if l2.Get(0).S != "good" {
t.Fatalf("Unmarshal into concrete struct type failed: %#v", l2)
}
l3 := geko.NewList[*s]()
if err := json.Unmarshal([]byte(`[{"s": "good"}]`), &l3); err != nil {
t.Fatalf("Unmarshal with error: %s", err.Error())
}
if l3.Get(0).S != "good" {
t.Fatalf("Unmarshal into concrete struct pointer type failed: %#v", l3)
}
}
func TestList_UnmarshalJSON_InitializedList(t *testing.T) {
l := geko.NewListFrom[int]([]int{7})
if err := json.Unmarshal([]byte(`[1]`), &l); err != nil {
t.Fatalf("Unmarshal into initialized map with error: %s", err.Error())
}
excepted := []int{1}
if !reflect.DeepEqual(l.List, excepted) {
t.Fatalf("Excepted %#v, got %#v", excepted, l.List)
}
l2 := geko.NewListFrom[any]([]any{"old"})
if err := json.Unmarshal([]byte(`[1]`), &l2); err != nil {
t.Fatalf("Unmarshal into initialized map with error: %s", err.Error())
}
excepted2 := []any{1.0}
if !reflect.DeepEqual(l2.List, excepted2) {
t.Fatalf("Excepted %#v, got %#v", excepted2, l2.List)
}
}
func TestList_UnmarshalJSON_InnerValueUseOurType(t *testing.T) {
l := geko.NewList[any]()
if err := json.Unmarshal([]byte(`[1,["2",{"llm":true}],{"a":1,"arr":["lml"]}]`), &l); err != nil {
t.Fatalf("Unmarshal error: %s", err.Error())
}
arr := l.Get(1)
ll, ok := arr.(geko.Array)
if !ok {
t.Fatalf("Inner array is not Array type")
}
arrObj := ll.Get(1)
llm, ok := arrObj.(geko.ObjectItems)
if !ok {
t.Fatalf("Inner array -> object is not ObjectItems type")
}
llmItem := llm.GetByIndex(0)
if llmItem.Key != "llm" && llmItem.Value != true {
t.Fatalf("Inner array -> object item not correct: %#v", llm)
}
obj := l.Get(2)
lm, ok := obj.(geko.ObjectItems)
if !ok {
t.Fatalf("Inner object is not ObjectItems type")
}
objArr := lm.GetValueByIndex(1)
lml, ok := objArr.(geko.Array)
if !ok {
t.Fatalf("Inner object -> array is not Array type")
}
if lml.Get(0) != "lml" {
t.Fatalf("Inner object -> array item not correct: %#v", lml)
}
}