-
Notifications
You must be signed in to change notification settings - Fork 1
/
godash_test.go
383 lines (307 loc) · 8.28 KB
/
godash_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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package godash
import (
"reflect"
"sort"
"testing"
)
func TestIsPointer(t *testing.T) {
x := &[]string{}
y := []string{}
if r := IsPointer(x); !r {
t.Errorf("expected 'true' (got %v)", r)
}
if r := IsPointer(y); r {
t.Errorf("expected 'false' (got %v)", r)
}
}
func TestIsFunction(t *testing.T) {
x := func() {}
y := []string{}
if r := IsFunction(x); !r {
t.Errorf("expected 'true' (got %v)", r)
}
if r := IsPointer(y); r {
t.Errorf("expected 'false' (got %v)", r)
}
}
func TestIsSlice(t *testing.T) {
x := []string{}
y := func() {}
if r := IsSlice(x); !r {
t.Errorf("expected 'true' (got %v)", r)
}
if r := IsSlice(y); r {
t.Errorf("expected 'false' (got %v)", r)
}
}
func TestIsMap(t *testing.T) {
x := make(map[string]interface{})
y := 4
z := make([]string, 0)
if r := IsMap(x); !r {
t.Errorf("expected 'true' (got %v)", r)
}
if r := IsMap(y); r {
t.Errorf("expected 'false' (got %v)", r)
}
if r := IsMap(z); r {
t.Errorf("expected 'false' (got %v)", r)
}
}
func TestUnique(t *testing.T) {
in := []string{"a", "a", "c", "d", "c"}
out := []string{}
b1 := []int{}
// Validity tests
if err := Unique(in, b1); err == nil {
t.Errorf("expected an error (got %v)", err)
}
if err := Unique(in, &b1); err == nil {
t.Errorf("expected an error (got %v)", err)
}
// Functionality tests
if err := Unique(in, &out); err != nil {
t.Errorf("expected nil error (got %v)", err)
}
if len(out) != 3 {
t.Errorf("expected output slice length of 3 (got %v)", len(out))
}
}
func TestSliceEqual(t *testing.T) {
s1 := []string{"a", "b", "c"}
s2 := []string{"c", "b", "a"}
s3 := []string{"a", "b", "d", "c"}
s4 := []int{1, 2, 3}
x := 0
// Validity tests
if _, err := SliceEqual(s1, x); err == nil {
t.Errorf("expected an error (got %v)", err)
}
if _, err := SliceEqual(s1, s4); err == nil {
t.Errorf("expected an error (got %v)", err)
}
// Functionality tests
if r, _ := SliceEqual(s1, x); r {
t.Errorf("expected 'false' (got %v)", r)
}
if r, _ := SliceEqual(s1, s2); !r {
t.Errorf("expected 'true' (got %v)", r)
}
if _, err := SliceEqual(s1, s2); err != nil {
t.Errorf("expected nil error (got %v)", err)
}
if r, _ := SliceEqual(s1, s3); r {
t.Errorf("expected 'false' (got %v)", r)
}
if _, err := SliceEqual(s1, s3); err != nil {
t.Errorf("expected nil error (got %v)", err)
}
if r, _ := SliceEqual(s1, s4); r {
t.Errorf("expected 'false' (got %v)", r)
}
}
func TestIncludes(t *testing.T) {
s1 := []string{"a", "b", "c"}
// Validity tests
if _, err := Includes(s1, 1); err == nil {
t.Errorf("expected an error (got %v)", err)
}
if _, err := Includes(1, 1); err == nil {
t.Errorf("expected an error (got %v)", err)
}
// Functionality tests
if r, _ := Includes(s1, "a"); !r {
t.Errorf("expected 'true' (got %v)", r)
}
if r, _ := Includes(s1, "d"); r {
t.Errorf("expected 'false' (got %v)", r)
}
}
func TestAppendUniq(t *testing.T) {
x := 1
s1 := []string{"a", "b", "c"}
// Validity tests
if err := AppendUniq(&x, 1); err == nil {
t.Errorf("expected an error (got %v)", err)
}
if err := AppendUniq(x, 1); err == nil {
t.Errorf("expected an error (got %v)", err)
}
if err := AppendUniq(&s1, 1); err == nil {
t.Errorf("expected an error (got %v)", err)
}
if err := AppendUniq(&s1, "d", 1); err == nil {
t.Errorf("expected an error (got %v)", err)
}
// Functionality tests
s2 := []string{"a", "b", "c"}
expected := []string{"a", "b", "c", "d"}
if err := AppendUniq(&s2, "a", "d", "a", "d"); err != nil {
t.Errorf("expected nil error (got %v)", err)
}
sort.Strings(s2)
sort.Strings(expected)
if r := reflect.DeepEqual(s2, expected); !r {
t.Errorf("expected correct slice (got %v)", s2)
}
}
func TestKeys(t *testing.T) {
m := map[string]interface{}{"a": 3, "b": false}
a := []int{}
b := 3
c := []string{}
// Validity tests
// Test the input variable is a map
if err := MapKeys(&m, a); err == nil {
t.Errorf("expected an error (got %v)", err)
}
// Test the input variable is a map
if err := MapKeys(a, a); err == nil {
t.Errorf("expected an error (got %v)", err)
}
// Test the output variable is a pointer
if err := MapKeys(m, a); err == nil {
t.Errorf("expected an error (got %v)", err)
}
// Test the output variable is a pointer of the valid type
if err := MapKeys(m, &a); err == nil {
t.Errorf("expected an error (got %v)", err)
}
// Test the output variable is a pointer
if err := MapKeys(m, b); err == nil {
t.Errorf("expected an error (got %v)", err)
}
// Test the output variable is a pointer
if err := MapKeys(m, &b); err == nil {
t.Errorf("expected an error (got %v)", err)
}
// Functionality tests
expected := []string{"a", "b"}
if err := MapKeys(m, &c); err != nil {
t.Errorf("expected nil error (got %v)", err)
}
sort.Strings(expected)
sort.Strings(c)
if ok := reflect.DeepEqual(c, expected); !ok {
t.Errorf("expected (%v) (got %v)", expected, c)
}
}
func TestMapValues(t *testing.T) {
m := map[string]int{"foo": 3, "bar": 6}
a := []string{}
b := 3
c := []int{}
// Validity tests
// Test the input variable is a map
if err := MapValues(&m, a); err == nil {
t.Errorf("expected an error (got %v)", err)
}
// Test the input variable is a map
if err := MapValues(a, a); err == nil {
t.Errorf("expected an error (got %v)", err)
}
// Test the output variable is a pointer
if err := MapValues(m, a); err == nil {
t.Errorf("expected an error (got %v)", err)
}
// Test the output variable is a pointer of the valid type
if err := MapValues(m, &a); err == nil {
t.Errorf("expected an error (got %v)", err)
}
// Test the output variable is a pointer
if err := MapValues(m, b); err == nil {
t.Errorf("expected an error (got %v)", err)
}
// Test the output variable is a pointer
if err := MapValues(m, &b); err == nil {
t.Errorf("expected an error (got %v)", err)
}
// Functionality tests
expected := []int{3, 6}
// Test when no error should be returned
if err := MapValues(m, &c); err != nil {
t.Errorf("expected nil error (got %v)", err)
}
sort.Ints(expected)
sort.Ints(c)
// Test the correct values were returned
if ok := reflect.DeepEqual(c, expected); !ok {
t.Errorf("expected (%v) (got %v)", expected, c)
}
}
func TestIntersect(t *testing.T) {
s1 := []int{1, 2, 3, 4}
s2 := []int{3, 4, 5, 1}
s3 := []int{7, 8, 9, 0}
s4 := []int{3, 4, 4, 3}
x := 3
y := []string{}
out := []int{}
// Validity tests
// Test if the first slice is actually a slice
if err := Intersect(x, s2, &out); err == nil {
t.Errorf("expected an error (got %v)", err)
}
// Test if the second slice is actually a slice
if err := Intersect(s1, x, &out); err == nil {
t.Errorf("expected an error (got %v)", err)
}
// Test if the output argument is a pointer
if err := Intersect(x, s2, out); err == nil {
t.Errorf("expected an error (got %v)", err)
}
// Test if the output argument is a pointer to a slice
if err := Intersect(s1, s2, &x); err == nil {
t.Errorf("expected an error (got %v)", err)
}
// Test if the two slices are of the same type
if err := Intersect(s1, y, &out); err == nil {
t.Errorf("expected an error (got %v)", err)
}
if err := Intersect(y, s2, &out); err == nil {
t.Errorf("expected an error (got %v)", err)
}
// Test if the output slice reference is of the same type
if err := Intersect(s1, s2, &y); err == nil {
t.Errorf("expected an error (got %v)", err)
}
// Functionality tests
var (
expected []int
err error
)
// Test when there is an intersection
out = []int{}
expected = []int{3, 4, 1}
err = Intersect(s1, s2, &out)
sort.Ints(expected)
sort.Ints(out)
if err != nil {
t.Errorf("expected nil error (got %v)", err)
}
if ok := reflect.DeepEqual(out, expected); !ok {
t.Errorf("expected (%v) (got %v)", expected, out)
}
// Test when there is no intersection
out = []int{}
expected = []int{}
err = Intersect(s1, s3, &out)
if err != nil {
t.Errorf("expected nil error (got %v)", err)
}
if ok := reflect.DeepEqual(out, expected); !ok {
t.Errorf("expected (%v) (got %v)", expected, out)
}
// Test intersection with duplicates
out = []int{}
expected = []int{3, 4}
err = Intersect(s1, s4, &out)
sort.Ints(expected)
sort.Ints(out)
if err != nil {
t.Errorf("expected nil error (got %v)", err)
}
if ok := reflect.DeepEqual(out, expected); !ok {
t.Errorf("expected (%v) (got %v)", expected, out)
}
}