Core

/app/util/array_test.go (19.5 KB)

  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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
package util_test

import (
"fmt"
"reflect"
"testing"

"github.com/pkg/errors"

"{{{ .Package }}}/app/util"
)

func TestStringArrayMaxLength(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input []string
expected int
}{
{name: "empty array", input: []string{}, expected: 0},
{name: "single element", input: []string{"hello"}, expected: 5},
{name: "multiple elements with same length", input: []string{"abc", "def", "ghi"}, expected: 3},
{name: "multiple elements with different lengths", input: []string{"a", "abc", "abcde"}, expected: 5},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := util.StringArrayMaxLength(tt.input)
if result != tt.expected {
t.Errorf("StringArrayMaxLength() = %v, want %v", result, tt.expected)
}
})
}
}

func TestArrayToStringArray(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input []any
expected []string
}{
{name: "empty array", input: []any{}, expected: []string{}},
{name: "integers", input: []any{1, 2, 3}, expected: []string{"1", "2", "3"}},
{name: "mixed types", input: []any{1, "hello", true, 3.14}, expected: []string{"1", "hello", "true", "3.14"}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
anySlice := util.ArrayCopy(tt.input)
result := util.ArrayToStringArray(anySlice)
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("ArrayToStringArray() = %v, want %v", result, tt.expected)
}
})
}
}

func TestStringArrayQuoted(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input []string
expected []string
}{
{name: "empty array", input: []string{}, expected: []string{}},
{name: "simple strings", input: []string{"hello", "world"}, expected: []string{`"hello"`, `"world"`}},
{name: "strings with quotes", input: []string{`a"b`, `c'd`}, expected: []string{`"a\"b"`, `"c'd"`}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := util.StringArrayQuoted(tt.input)
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("StringArrayQuoted() = %v, want %v", result, tt.expected)
}
})
}
}

func TestStringArrayFromAny(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input []any
maxLength int
expected []string
}{
{name: "empty array", input: []any{}, maxLength: 0, expected: []string{}},
{name: "mixed types no truncation", input: []any{1, "hello", true, []byte("world")}, maxLength: 0, expected: []string{"1", "hello", "true", "world"}},
{name: "with truncation", input: []any{"abcdefghij", "12345"}, maxLength: 5, expected: []string{"abcde... (truncated)", "12345"}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := util.StringArrayFromAny(tt.input, tt.maxLength)
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("StringArrayFromAny() = %v, want %v", result, tt.expected)
}
})
}
}

func TestArrayCopy(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input []int
expected []int
}{
{name: "nil array", input: nil, expected: nil},
{name: "empty array", input: []int{}, expected: []int{}},
{name: "integers", input: []int{1, 2, 3}, expected: []int{1, 2, 3}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := util.ArrayCopy(tt.input)
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("ArrayCopy() = %v, want %v", result, tt.expected)
}

if len(tt.input) > 0 {
original := make([]int, len(tt.input))
copy(original, tt.input)
tt.input[0] = 999
if len(result) > 0 && reflect.DeepEqual(result, tt.input) {
t.Errorf("ArrayCopy() did not create a true copy")
}
copy(tt.input, original)
}
})
}
}

func TestArrayRemoveDuplicates(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input []int
expected []int
}{
{name: "empty array", input: []int{}, expected: []int{}},
{name: "no duplicates", input: []int{1, 2, 3}, expected: []int{1, 2, 3}},
{name: "with duplicates", input: []int{1, 2, 2, 3, 1, 4}, expected: []int{1, 2, 3, 4}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := util.ArrayRemoveDuplicates(tt.input)
if len(result) != len(tt.expected) {
t.Errorf("ArrayRemoveDuplicates() length = %v, want %v", len(result), len(tt.expected))
}

resultMap := make(map[int]bool)
for _, v := range result {
resultMap[v] = true
}

expectedMap := make(map[int]bool)
for _, v := range tt.expected {
expectedMap[v] = true
}

for k := range expectedMap {
if !resultMap[k] {
t.Errorf("ArrayRemoveDuplicates() missing expected element %v", k)
}
}

for k := range resultMap {
if !expectedMap[k] {
t.Errorf("ArrayRemoveDuplicates() has unexpected element %v", k)
}
}
})
}
}

func TestArraySorted(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input []int
expected []int
}{
{name: "empty array", input: []int{}, expected: []int{}},
{name: "already sorted", input: []int{1, 2, 3}, expected: []int{1, 2, 3}},
{name: "unsorted", input: []int{3, 1, 2}, expected: []int{1, 2, 3}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := util.ArraySorted(tt.input)
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("ArraySorted() = %v, want %v", result, tt.expected)
}
})
}
}

func TestArrayLimit(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input []int
limit int
expectedArray []int
expectedRest int
}{
{name: "empty array", input: []int{}, limit: 5, expectedArray: []int{}, expectedRest: 0},
{name: "limit larger than array", input: []int{1, 2, 3}, limit: 5, expectedArray: []int{1, 2, 3}, expectedRest: 0},
{name: "limit smaller than array", input: []int{1, 2, 3, 4, 5}, limit: 3, expectedArray: []int{1, 2, 3}, expectedRest: 2},
{name: "limit zero", input: []int{1, 2, 3}, limit: 0, expectedArray: []int{1, 2, 3}, expectedRest: 0},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result, rest := util.ArrayLimit(tt.input, tt.limit)
if !reflect.DeepEqual(result, tt.expectedArray) {
t.Errorf("ArrayLimit() array = %v, want %v", result, tt.expectedArray)
}
if rest != tt.expectedRest {
t.Errorf("ArrayLimit() rest = %v, want %v", rest, tt.expectedRest)
}
})
}
}

func TestStringArrayOxfordComma(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input []string
separator string
expected string
}{
{name: "empty array", input: []string{}, separator: "and", expected: ""},
{name: "single element", input: []string{"apple"}, separator: "and", expected: "apple"},
{name: "two elements", input: []string{"apple", "banana"}, separator: "and", expected: "apple and banana"},
{name: "three elements", input: []string{"apple", "banana", "cherry"}, separator: "and", expected: "apple, banana, and cherry"},
{name: "four elements", input: []string{"apple", "banana", "cherry", "date"}, separator: "or", expected: "apple, banana, cherry, or date"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := util.StringArrayOxfordComma(tt.input, tt.separator)
if result != tt.expected {
t.Errorf("StringArrayOxfordComma() = %v, want %v", result, tt.expected)
}
})
}
}

func TestArrayTransform(t *testing.T) {
tx := func(i int) string {
return "x" + string(rune(i+'0'))
}
t.Parallel()
tests := []struct {
name string
input []int
fn func(int) string
expected []string
}{
{name: "empty array", input: []int{}, fn: tx, expected: []string{}},
{name: "transform integers to strings", input: []int{1, 2, 3}, fn: tx, expected: []string{"x1", "x2", "x3"}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := util.ArrayTransform(tt.input, tt.fn)
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("ArrayTransform() = %v, want %v", result, tt.expected)
}
})
}
}

func TestArraySplit(t *testing.T) {
fn := func(i int) bool {
return i%2 == 0
}
t.Parallel()
tests := []struct {
name string
input []int
fn func(int) bool
expectedTrue []int
expectedFalse []int
}{
{name: "empty array", input: []int{}, fn: fn, expectedTrue: []int{}, expectedFalse: []int{}},
{name: "split even/odd", input: []int{1, 2, 3, 4, 5}, fn: fn, expectedTrue: []int{2, 4}, expectedFalse: []int{1, 3, 5}},
{name: "all true", input: []int{2, 4, 6}, fn: fn, expectedTrue: []int{2, 4, 6}, expectedFalse: []int{}},
{name: "all false", input: []int{1, 3, 5}, fn: fn, expectedTrue: []int{}, expectedFalse: []int{1, 3, 5}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
trueResult, falseResult := util.ArraySplit(tt.input, tt.fn)

if len(trueResult) != len(tt.expectedTrue) {
t.Errorf("ArraySplit() true result length = %v, want %v", len(trueResult), len(tt.expectedTrue))
} else {
for i, v := range trueResult {
if i < len(tt.expectedTrue) && v != tt.expectedTrue[i] {
t.Errorf("ArraySplit() true result[%d] = %v, want %v", i, v, tt.expectedTrue[i])
}
}
}

if len(falseResult) != len(tt.expectedFalse) {
t.Errorf("ArraySplit() false result length = %v, want %v", len(falseResult), len(tt.expectedFalse))
} else {
for i, v := range falseResult {
if i < len(tt.expectedFalse) && v != tt.expectedFalse[i] {
t.Errorf("ArraySplit() false result[%d] = %v, want %v", i, v, tt.expectedFalse[i])
}
}
}
})
}
}

func TestArrayRemoveNil(t *testing.T) {
t.Parallel()
i1, i2, i3 := 1, 2, 3
tests := []struct {
name string
input []*int
expected []*int
}{
{name: "empty array", input: []*int{}, expected: []*int{}},
{name: "no nil elements", input: []*int{&i1, &i2, &i3}, expected: []*int{&i1, &i2, &i3}},
{name: "some nil elements", input: []*int{&i1, nil, &i2, nil, &i3}, expected: []*int{&i1, &i2, &i3}},
{name: "all nil elements", input: []*int{nil, nil, nil}, expected: []*int{}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := util.ArrayRemoveNil(tt.input)

if len(result) != len(tt.expected) {
t.Errorf("ArrayRemoveNil() length = %v, want %v", len(result), len(tt.expected))
return
}

for i, v := range result {
switch {
case v == nil:
t.Errorf("ArrayRemoveNil() contains nil at index %d", i)
case tt.expected[i] == nil:
t.Errorf("ArrayRemoveNil() expected nil at index %d, got %v", i, *v)
case *v != *tt.expected[i]:
t.Errorf("ArrayRemoveNil() at index %d = %v, want %v", i, *v, *tt.expected[i])
}
}
})
}
}

func TestArrayRemoveEmpty(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input []string
expected []string
}{
{name: "empty array", input: []string{}, expected: []string{}},
{name: "no empty elements", input: []string{"a", "b", "c"}, expected: []string{"a", "b", "c"}},
{name: "some empty elements", input: []string{"a", "", "b", "", "c"}, expected: []string{"a", "b", "c"}},
{name: "all empty elements", input: []string{"", "", ""}, expected: []string{}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := util.ArrayRemoveEmpty(tt.input)
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("ArrayRemoveEmpty() = %v, want %v", result, tt.expected)
}
})
}

intTests := []struct {
name string
input []int
expected []int
}{
{name: "empty array", input: []int{}, expected: []int{}},
{name: "no zero elements", input: []int{1, 2, 3}, expected: []int{1, 2, 3}},
{name: "some zero elements", input: []int{1, 0, 2, 0, 3}, expected: []int{1, 2, 3}},
{name: "all zero elements", input: []int{0, 0, 0}, expected: []int{}},
}

for _, tt := range intTests {
t.Run(tt.name+" (int)", func(t *testing.T) {
t.Parallel()
result := util.ArrayRemoveEmpty(tt.input)
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("ArrayRemoveEmpty() = %v, want %v", result, tt.expected)
}
})
}
}

func TestArrayDereference(t *testing.T) {
t.Parallel()
i1, i2, i3 := 1, 2, 3
tests := []struct {
name string
input []*int
expected []int
}{
{name: "empty array", input: []*int{}, expected: []int{}},
{name: "simple array", input: []*int{&i1, &i2, &i3}, expected: []int{1, 2, 3}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := util.ArrayDereference(tt.input)
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("ArrayDereference() = %v, want %v", result, tt.expected)
}
})
}
}

func TestLengthAny(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input any
expected int
}{
{name: "empty slice", input: []int{}, expected: 0},
{name: "int slice", input: []int{1, 2, 3}, expected: 3},
{name: "string slice", input: []string{"a", "b", "c"}, expected: 3},
{name: "pointer to slice", input: &[]int{1, 2, 3, 4}, expected: 4},
{name: "array", input: [3]int{1, 2, 3}, expected: 3},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := util.LengthAny(tt.input)
if result != tt.expected {
t.Errorf("LengthAny() = %v, want %v", result, tt.expected)
}
})
}
}

func TestArrayFromAny(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input any
expected []int
}{
{name: "empty slice", input: []int{}, expected: []int{}},
{name: "int slice", input: []int{1, 2, 3}, expected: []int{1, 2, 3}},
{name: "pointer to slice", input: &[]int{1, 2, 3, 4}, expected: []int{1, 2, 3, 4}},
{name: "array", input: [3]int{1, 2, 3}, expected: []int{1, 2, 3}},
{name: "single value", input: 42, expected: []int{42}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := util.ArrayFromAny[int](tt.input)
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("ArrayFromAny() = %v, want %v", result, tt.expected)
}
})
}
}

func TestArrayTest(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input any
expected bool
}{
{name: "empty slice", input: []int{}, expected: true},
{name: "non-empty slice", input: []int{1, 2, 3}, expected: true},
{name: "array", input: [3]int{1, 2, 3}, expected: true},
{name: "string", input: "not an array", expected: false},
{name: "integer", input: 42, expected: false},
{name: "struct", input: struct{ name string }{"test"}, expected: false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := util.ArrayTest(tt.input)
if result != tt.expected {
t.Errorf("ArrayTest() = %v, want %v", result, tt.expected)
}
})
}
}

func TestArrayFlatten(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input [][]int
expected []int
}{
{name: "empty arrays", input: [][]int{}, expected: []int{}},
{name: "single empty array", input: [][]int{{}}, expected: []int{}},
{name: "multiple arrays", input: [][]int{{1, 2}, {3, 4}, {5, 6}}, expected: []int{1, 2, 3, 4, 5, 6}},
{name: "mixed empty and non-empty arrays", input: [][]int{{}, {1, 2}, {}, {3, 4}}, expected: []int{1, 2, 3, 4}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := util.ArrayFlatten(tt.input...)
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("ArrayFlatten() = %v, want %v", result, tt.expected)
}
})
}
}

func TestArrayFirstN(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input []int
n int
expected []int
}{
{name: "empty array", input: []int{}, n: 3, expected: []int{}},
{name: "n larger than array", input: []int{1, 2, 3}, n: 5, expected: []int{1, 2, 3}},
{name: "n smaller than array", input: []int{1, 2, 3, 4, 5}, n: 3, expected: []int{1, 2, 3}},
{name: "n equal to array length", input: []int{1, 2, 3}, n: 3, expected: []int{1, 2, 3}},
{name: "n is zero", input: []int{1, 2, 3}, n: 0, expected: []int{}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := util.ArrayFirstN(tt.input, tt.n)
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("ArrayFirstN() = %v, want %v", result, tt.expected)
}
})
}
}

func TestArrayLastN(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input []int
n int
expected []int
}{
{name: "empty array", input: []int{}, n: 3, expected: []int{}},
{name: "n larger than array", input: []int{1, 2, 3}, n: 5, expected: []int{1, 2, 3}},
{name: "n smaller than array", input: []int{1, 2, 3, 4, 5}, n: 3, expected: []int{3, 4, 5}},
{name: "n equal to array length", input: []int{1, 2, 3}, n: 3, expected: []int{1, 2, 3}},
{name: "n is zero", input: []int{1, 2, 3}, n: 0, expected: []int{}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := util.ArrayLastN(tt.input, tt.n)
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("ArrayLastN() = %v, want %v", result, tt.expected)
}
})
}
}

func TestArrayReplaceOrAdd(t *testing.T) {
chk := func(i int) bool {
return i == 2
}
t.Parallel()
tests := []struct {
name string
input []int
fn func(int) bool
replace int
expected []int
}{
{name: "empty array", input: []int{}, fn: chk, replace: 42, expected: []int{42}},
{name: "element found", input: []int{1, 2, 3}, fn: chk, replace: 42, expected: []int{1, 42, 3}},
{name: "element not found", input: []int{1, 3, 5}, fn: chk, replace: 42, expected: []int{1, 3, 5, 42}},
{name: "multiple matches (should replace first)", input: []int{1, 2, 3, 2, 4}, fn: chk, replace: 42, expected: []int{1, 42, 3, 2, 4}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := util.ArrayReplaceOrAdd(tt.input, tt.fn, tt.replace)
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("ArrayReplaceOrAdd() = %v, want %v", result, tt.expected)
}
})
}
}

func TestMapError(t *testing.T) {
chk := func(i, idx int) (string, error) {
return fmt.Sprintf("x%d", i), nil
}
chk3 := func(i, idx int) (string, error) {
if i == 3 {
return "", errors.Errorf("error at value %d", i)
}
return fmt.Sprintf("x%d", i), nil
}
t.Parallel()
tests := []struct {
name string
input []int
fn func(int, int) (string, error)
expected []string
expectError bool
}{
{name: "empty array", input: []int{}, fn: chk, expected: []string{}},
{name: "no errors", input: []int{1, 2, 3}, fn: chk, expected: []string{"x1", "x2", "x3"}},
{name: "with error", input: []int{1, 2, 3, 4, 5}, fn: chk3, expected: nil, expectError: true},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result, err := util.MapError(tt.input, tt.fn)

if tt.expectError {
if err == nil {
t.Errorf("MapError() expected error but got nil")
}
} else {
if err != nil {
t.Errorf("MapError() unexpected error: %v", err)
}
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("MapError() = %v, want %v", result, tt.expected)
}
}
})
}
}