-
Notifications
You must be signed in to change notification settings - Fork 68
/
collection_test.go
1273 lines (1014 loc) · 36.6 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
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package collection
import (
"reflect"
"strings"
"testing"
"github.com/pkg/errors"
)
func TestNewCollection(t *testing.T) {
// Test that NewCollection returns a non-nil pointer to a Collection struct
arr := []int32{1, 2, 3}
c := NewCollection[int32](arr)
if c == nil {
t.Error("NewCollection returned nil")
}
}
func TestNewEmptyCollection(t *testing.T) {
c := NewEmptyCollection[int32]()
if c == nil {
t.Error("NewEmptyCollection returned nil")
}
if len(c.value) != 0 {
t.Error("NewEmptyCollection did not return an empty collection")
}
}
func TestCopy(t *testing.T) {
// Test that Copy returns a new Collection with the same values and error as the original
arr := []int32{1, 2, 3}
c := NewCollection[int32](arr).SetErr(errors.New("test error"))
copied := c.Copy()
if !reflect.DeepEqual(copied.value, c.value) {
t.Error("Copy did not copy the values correctly")
}
if copied.err.Error() != c.err.Error() {
t.Error("Copy did not copy the error correctly")
}
}
func TestIsEmpty(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{1, 2, 3})
// check if IsEmpty() returns false
if coll.IsEmpty() {
t.Errorf("IsEmpty() returned true, expected false")
}
// remove all elements from the Collection
coll.Remove(0).Remove(0).Remove(0)
// check if IsEmpty() returns true
if !coll.IsEmpty() {
t.Errorf("IsEmpty() returned false, expected true")
}
}
func TestIsNotEmpty(t *testing.T) {
coll := NewCollection[int]([]int{1, 2, 3})
if !coll.IsNotEmpty() {
t.Errorf("Expected collection to be not empty, but it is empty")
}
coll = NewEmptyCollection[int]()
if coll.IsNotEmpty() {
t.Errorf("Expected collection to be empty, but it is not empty")
}
}
func TestCollectionAppend(t *testing.T) {
coll := NewCollection[int]([]int{1, 2, 3})
newItem := 4
coll.Append(newItem)
if len(coll.value) != 4 {
t.Errorf("Expected length of collection to be 4, but got %d", len(coll.value))
}
if coll.value[len(coll.value)-1] != newItem {
t.Errorf("Expected last item in collection to be %d, but got %d", newItem, coll.value[len(coll.value)-1])
}
}
func TestCollectionRemove(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{1, 2, 3})
// remove the first element
coll.Remove(0)
// check if the first element was removed
if coll.value[0] != 2 {
t.Errorf("Remove did not remove the element correctly")
}
// remove the last element
coll.Remove(1)
// check if the last element was removed
if coll.value[0] != 2 || len(coll.value) != 1 {
t.Errorf("Remove did not remove the element correctly")
}
}
func TestCollectionInsert(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{1, 2, 3})
// insert a new element at the beginning
coll = coll.Insert(0, 0)
// check if the element was inserted correctly
if coll.value[0] != 0 {
t.Errorf("Insert did not insert the element correctly")
}
// insert a new element at the end
coll = coll.Insert(4, 4)
// check if the element was inserted correctly
if coll.value[4] != 4 {
t.Errorf("Insert did not insert the element correctly")
}
// insert a new element in the middle
coll = coll.Insert(2, 5)
// check if the element was inserted correctly
if coll.value[2] != 5 {
t.Errorf("Insert did not insert the element correctly")
}
}
func TestCollectionSearch(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{1, 2, 3})
// search for an existing element
index := coll.Search(2)
// check if the index of the element was returned correctly
if index != 1 {
t.Errorf("Search did not return the correct index")
}
// search for a non-existing element
index = coll.Search(4)
// check if -1 was returned
if index != -1 {
t.Errorf("Search did not return -1 for a non-existing element")
}
}
func TestUnique(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{1, 2, 2, 3, 3, 3})
// get the unique elements
uniqueColl := coll.Unique()
// check if the length of the unique collection is correct
if len(uniqueColl.value) != 3 {
t.Errorf("Unique did not return the correct number of unique elements")
}
// check if the unique collection contains the correct elements
if uniqueColl.value[0] != 1 || uniqueColl.value[1] != 2 || uniqueColl.value[2] != 3 {
t.Errorf("Unique did not return the correct unique elements")
}
}
// TestFilter tests the Filter method of the Collection struct
func TestFilter(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{1, 2, 3, 4, 5})
// define a filter function that only keeps even numbers
filterFunc := func(item int, key int) bool {
return item%2 == 0
}
// apply the filter function to the collection
filteredColl := coll.Filter(filterFunc)
// check if the length of the filtered collection is correct
if len(filteredColl.value) != 2 {
t.Errorf("Filter did not return the correct number of elements")
}
// check if the filtered collection contains the correct elements
if filteredColl.value[0] != 2 || filteredColl.value[1] != 4 {
t.Errorf("Filter did not return the correct elements")
}
}
// TestReject tests the Reject method of the Collection struct
func TestReject(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{1, 2, 3, 4, 5})
// define a reject function that rejects even numbers
rejectFunc := func(item int, key int) bool {
return item%2 == 0
}
// apply the reject function to the collection
rejectedColl := coll.Reject(rejectFunc)
// check if the length of the rejected collection is correct
if len(rejectedColl.value) != 3 {
t.Errorf("Reject did not return the correct number of elements")
}
// check if the rejected collection contains the correct elements
if rejectedColl.value[0] != 1 || rejectedColl.value[1] != 3 || rejectedColl.value[2] != 5 {
t.Errorf("Reject did not return the correct elements")
}
}
// TestFirst tests the First method of the Collection struct
func TestFirst(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{1, 2, 3})
// get the first element
first := coll.First()
// check if the first element is correct
if first != 1 {
t.Errorf("First did not return the correct first element")
}
// create a new empty Collection
emptyColl := NewEmptyCollection[int]()
// get the first element of an empty Collection
first = emptyColl.First()
// check if the first element is the zero value of the type
if first != 0 {
t.Errorf("First did not return the zero value of the type for an empty Collection")
}
}
func TestLast(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{1, 2, 3})
// get the last element
last := coll.Last()
// check if the last element is correct
if last != 3 {
t.Errorf("Last did not return the correct last element")
}
// create a new empty Collection
emptyColl := NewEmptyCollection[int]()
// get the last element of an empty Collection
last = emptyColl.Last()
// check if the last element is the zero value of the type
if last != 0 {
t.Errorf("Last did not return the zero value of the type for an empty Collection")
}
}
func TestSlice(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{1, 2, 3, 4, 5})
// get a slice of the collection
slice := coll.Slice(1, 3)
// check if the length of the slice is correct
if len(slice.value) != 2 {
t.Errorf("Slice did not return the correct number of elements")
}
// check if the slice contains the correct elements
if slice.value[0] != 2 || slice.value[1] != 3 {
t.Errorf("Slice did not return the correct elements")
}
}
func TestIndex(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{1, 2, 3})
// get the element at index 1
element := coll.Index(1)
// check if the element is correct
if element != 2 {
t.Errorf("Index did not return the correct element")
}
}
func TestSetIndex(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{1, 2, 3})
// set the element at index 1 to a new value
coll.SetIndex(1, 4)
// check if the element was set correctly
if coll.value[1] != 4 {
t.Errorf("SetIndex did not set the element correctly")
}
}
func TestCount(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{1, 2, 3})
// get the count of elements
count := coll.Count()
// check if the count is correct
if count != 3 {
t.Errorf("Count did not return the correct count")
}
}
func TestMerge(t *testing.T) {
// create two new Collections with some elements
coll1 := NewCollection([]int{1, 2, 3})
coll2 := NewCollection([]int{4, 5, 6})
// merge the two collections
mergedColl := coll1.Merge(coll2)
// check if the length of the merged collection is correct
if len(mergedColl.value) != 6 {
t.Errorf("Merge did not return the correct number of elements")
}
// check if the merged collection contains the correct elements
if mergedColl.value[0] != 1 || mergedColl.value[1] != 2 || mergedColl.value[2] != 3 || mergedColl.value[3] != 4 || mergedColl.value[4] != 5 || mergedColl.value[5] != 6 {
t.Errorf("Merge did not return the correct elements")
}
}
func TestEach(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{1, 2, 3})
// define a function that adds 1 to each element
addOne := func(item int, key int) {
coll.value[key] = item + 1
}
// apply the function to each element of the collection
coll.Each(addOne)
// check if the elements were modified correctly
if coll.value[0] != 2 || coll.value[1] != 3 || coll.value[2] != 4 {
t.Errorf("Each did not modify the elements correctly")
}
}
func TestMap(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{1, 2, 3})
// define a function that multiplies each element by 2
multiplyByTwo := func(item int, key int) int {
return item * 2
}
// apply the function to each element of the collection
mappedColl := coll.Map(multiplyByTwo)
// check if the length of the mapped collection is correct
if len(mappedColl.value) != 3 {
t.Errorf("Map did not return the correct number of elements")
}
// check if the mapped collection contains the correct elements
if mappedColl.value[0] != 2 || mappedColl.value[1] != 4 || mappedColl.value[2] != 6 {
t.Errorf("Map did not return the correct elements")
}
}
// TestReduce tests the Reduce method of the Collection struct
func TestReduce(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{1, 2, 3})
// define a function that sums two integers
sum := func(carry int, item int) int {
return carry + item
}
// apply the function to the collection
result := coll.Reduce(sum)
// check if the result is correct
if result != 6 {
t.Errorf("Reduce did not return the correct result")
}
}
// TestEvery tests the Every method of the Collection struct
func TestEvery(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{2, 4, 6})
// define a function that checks if an integer is even
isEven := func(item int, key int) bool {
return item%2 == 0
}
// check if every element of the collection is even
if !coll.Every(isEven) {
t.Errorf("Every did not return true for a collection where every element satisfies the condition")
}
// create a new Collection with some elements
coll = NewCollection([]int{2, 3, 6})
// check if every element of the collection is even
if coll.Every(isEven) {
t.Errorf("Every did not return false for a collection where not every element satisfies the condition")
}
}
// TestForPage tests the ForPage method of the Collection struct
func TestForPage(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{1, 2, 3, 4, 5})
// get the first page with 2 elements per page
pageColl := coll.ForPage(1, 2)
// check if the length of the page collection is correct
if len(pageColl.value) != 2 {
t.Errorf("ForPage did not return the correct number of elements for the first page")
}
// check if the page collection contains the correct elements
if pageColl.value[0] != 1 || pageColl.value[1] != 2 {
t.Errorf("ForPage did not return the correct elements for the first page")
}
// get the second page with 2 elements per page
pageColl = coll.ForPage(2, 2)
// check if the length of the page collection is correct
if len(pageColl.value) != 2 {
t.Errorf("ForPage did not return the correct number of elements for the second page")
}
// check if the page collection contains the correct elements
if pageColl.value[0] != 3 || pageColl.value[1] != 4 {
t.Errorf("ForPage did not return the correct elements for the second page")
}
// get the third page with 2 elements per page
pageColl = coll.ForPage(3, 2)
// check if the length of the page collection is correct
if len(pageColl.value) != 1 {
t.Errorf("ForPage did not return the correct number of elements for the third page")
}
// check if the page collection contains the correct elements
if pageColl.value[0] != 5 {
t.Errorf("ForPage did not return the correct elements for the third page")
}
}
// TestNth tests the Nth method of the Collection struct
func TestNth(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{1, 2, 3, 4, 5})
// get every second element starting from the first
nthColl := coll.Nth(2, 0)
// check if the length of the nth collection is correct
if len(nthColl.value) != 3 {
t.Errorf("Nth did not return the correct number of elements")
}
// check if the nth collection contains the correct elements
if nthColl.value[0] != 1 || nthColl.value[1] != 3 || nthColl.value[2] != 5 {
t.Errorf("Nth did not return the correct elements")
}
// get every second element starting from the second
nthColl = coll.Nth(2, 1)
// check if the length of the nth collection is correct
if len(nthColl.value) != 2 {
t.Errorf("Nth did not return the correct number of elements")
}
// check if the nth collection contains the correct elements
if nthColl.value[0] != 2 || nthColl.value[1] != 4 {
t.Errorf("Nth did not return the correct elements")
}
}
// TestPad tests the Pad method of the Collection struct
func TestPad(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{1, 2, 3})
// pad the collection with 2 elements
paddedColl := coll.Pad(5, 0)
// check if the length of the padded collection is correct
if len(paddedColl.value) != 5 {
t.Errorf("Pad did not return the correct number of elements")
}
// check if the padded collection contains the correct elements
if paddedColl.value[0] != 1 || paddedColl.value[1] != 2 || paddedColl.value[2] != 3 || paddedColl.value[3] != 0 || paddedColl.value[4] != 0 {
t.Errorf("Pad did not return the correct elements")
}
}
// TestPop tests the Pop method of the Collection struct
func TestPop(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{1, 2, 3})
// pop the last element
popped := coll.Pop()
// check if the popped element is correct
if popped != 3 {
t.Errorf("Pop did not return the correct popped element")
}
// check if the length of the collection is correct
if len(coll.value) != 2 {
t.Errorf("Pop did not modify the length of the collection correctly")
}
// check if the collection contains the correct elements
if coll.value[0] != 1 || coll.value[1] != 2 {
t.Errorf("Pop did not modify the elements of the collection correctly")
}
// create a new empty Collection
emptyColl := NewEmptyCollection[int]()
// try to pop an element from an empty Collection
popped = emptyColl.Pop()
// check if the popped element is the zero value of the type
if popped != 0 {
t.Errorf("Pop did not return the zero value of the type for an empty Collection")
}
}
// TestPush tests the Push method of the Collection struct
func TestPush(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{1, 2, 3})
// push a new element to the collection
coll.Push(4)
// check if the length of the collection is correct
if len(coll.value) != 4 {
t.Errorf("Push did not modify the length of the collection correctly")
}
// check if the collection contains the correct elements
if coll.value[0] != 1 || coll.value[1] != 2 || coll.value[2] != 3 || coll.value[3] != 4 {
t.Errorf("Push did not modify the elements of the collection correctly")
}
}
// TestPrepend tests the Prepend method of the Collection struct
func TestPrepend(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{1, 2, 3})
// prepend a new element to the collection
coll = coll.Prepend(0)
// check if the length of the collection is correct
if len(coll.value) != 4 {
t.Errorf("Prepend did not modify the length of the collection correctly")
}
// check if the collection contains the correct elements
if coll.value[0] != 0 || coll.value[1] != 1 || coll.value[2] != 2 || coll.value[3] != 3 {
t.Errorf("Prepend did not modify the elements of the collection correctly")
}
}
// TestRandom tests the Random method of the Collection struct
func TestRandom(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{1, 2, 3})
// get a random element from the collection
random := coll.Random()
// check if the random element is in the collection
if random != 1 && random != 2 && random != 3 {
t.Errorf("Random did not return an element from the collection")
}
}
// TestReverse tests the Reverse method of the Collection struct
func TestReverse(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{1, 2, 3})
// reverse the collection
reversedColl := coll.Reverse()
// check if the length of the reversed collection is correct
if len(reversedColl.value) != 3 {
t.Errorf("Reverse did not return the correct number of elements")
}
// check if the reversed collection contains the correct elements
if reversedColl.value[0] != 3 || reversedColl.value[1] != 2 || reversedColl.value[2] != 1 {
t.Errorf("Reverse did not return the correct elements")
}
}
// TestShuffle tests the Shuffle method of the Collection struct
func TestShuffle(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{1, 2, 3})
// shuffle the collection
shuffledColl := coll.Shuffle()
// check if the length of the shuffled collection is correct
if len(shuffledColl.value) != 3 {
t.Errorf("Shuffle did not return the correct number of elements")
}
// check if the shuffled collection contains the same elements as the original collection
if !coll.Contains(shuffledColl.value[0]) || !coll.Contains(shuffledColl.value[1]) || !coll.Contains(shuffledColl.value[2]) {
t.Errorf("Shuffle did not return the correct elements")
}
}
// TestGroupBy tests the GroupBy method of the Collection struct
func TestGroupBy(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{1, 2, 3, 4, 5})
// define a function that groups even and odd numbers
groupBy := func(item int, key int) interface{} {
if item%2 == 0 {
return "even"
} else {
return "odd"
}
}
// group the collection by even and odd numbers
groupedColl := coll.GroupBy(groupBy)
// check if the length of the grouped collection is correct
if len(groupedColl) != 2 {
t.Errorf("GroupBy did not return the correct number of groups")
}
// check if the grouped collection contains the correct elements
if len(groupedColl["even"].value) != 2 || len(groupedColl["odd"].value) != 3 {
t.Errorf("GroupBy did not return the correct elements")
}
}
// TestSplit tests the Split method of the Collection struct
func TestSplit(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{1, 2, 3, 4, 5})
// split the collection into two parts
splitColl := coll.Split(2)
// check if the length of the split collection is correct
if len(splitColl) != 3 {
t.Errorf("Split did not return the correct number of parts")
}
// check if the split collection contains the correct elements
if len(splitColl[0].value) != 2 || len(splitColl[1].value) != 2 || len(splitColl[2].value) != 1 {
t.Errorf("Split did not return the correct elements")
}
}
// TestDD tests the DD method of the Collection struct
func TestDD(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{1, 2, 3})
// call the DD method
coll.DD()
// the DD method should not return anything, so there is nothing to check
}
// TestPluckString tests the PluckString method of the Collection struct
type Person struct {
Name string
Age string
}
func TestPluckString(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]Person{
{"Alice", "20"},
{"Bob", "30"},
{"Charlie", "40"},
})
// pluck the "name" field from the collection
pluckedColl := coll.PluckString("Name")
if pluckedColl.Err() != nil {
t.Errorf("PluckString return err")
}
// check if the length of the plucked collection is correct
if len(pluckedColl.value) != 3 {
t.Errorf("PluckString did not return the correct number of elements")
}
// check if the plucked collection contains the correct elements
if pluckedColl.value[0] != "Alice" || pluckedColl.value[1] != "Bob" || pluckedColl.value[2] != "Charlie" {
t.Errorf("PluckString did not return the correct elements")
}
}
func TestPluckStringPointer(t *testing.T) {
// create a new Collection with some elements
person1 := Person{"Alice", "20"}
person2 := Person{"Bob", "30"}
person3 := Person{"Charlie", "40"}
coll := NewCollection([]*Person{&person1, &person2, &person3})
// pluck the "name" field from the collection
pluckedColl := coll.PluckString("Name")
if pluckedColl.Err() != nil {
t.Errorf("PluckString return err")
}
// check if the length of the plucked collection is correct
if len(pluckedColl.value) != 3 {
t.Errorf("PluckString did not return the correct number of elements")
}
// check if the plucked collection contains the correct elements
if pluckedColl.value[0] != "Alice" || pluckedColl.value[1] != "Bob" || pluckedColl.value[2] != "Charlie" {
t.Errorf("PluckString did not return the correct elements")
}
}
func TestPluckInt64(t *testing.T) {
// create a new Collection with some elements
type Person struct {
Id int64
Age int64
}
coll := NewCollection([]Person{
{1, 20},
{2, 30},
{3, 40},
})
// pluck the "id" field from the collection
pluckedColl := coll.PluckInt64("Id")
// check if the length of the plucked collection is correct
if pluckedColl.Count() != 3 {
t.Errorf("PluckInt64 did not return the correct number of elements")
}
// check if the plucked collection contains the correct elements
if pluckedColl.Index(0) != 1 {
t.Errorf("PluckInt64 did not return the correct elements")
}
}
// TestPluckFloat64 tests the PluckFloat64 method of the Collection struct
func TestPluckFloat64(t *testing.T) {
// create a new Collection with some elements
type Product struct {
Price float64
Quantity int
}
coll := NewCollection([]Product{
{1.99, 10},
{2.99, 20},
{3.99, 30},
})
// pluck the "price" field from the collection
pluckedColl := coll.PluckFloat64("Price")
// check if the length of the plucked collection is correct
if pluckedColl.Count() != 3 {
t.Errorf("PluckFloat64 did not return the correct number of elements")
}
// check if the plucked collection contains the correct elements
if pluckedColl.Index(0) != 1.99 || pluckedColl.Index(1) != 2.99 || pluckedColl.Index(2) != 3.99 {
t.Errorf("PluckFloat64 did not return the correct elements")
}
}
// TestPluckUint64 tests the PluckUint64 method of the Collection struct
func TestPluckUint64(t *testing.T) {
// create a new Collection with some elements
type Person struct {
Id uint64
Age uint64
}
coll := NewCollection([]Person{
{1, 20},
{2, 30},
{3, 40},
})
// pluck the "id" field from the collection
pluckedColl := coll.PluckUint64("Id")
// check if the length of the plucked collection is correct
if pluckedColl.Count() != 3 {
t.Errorf("PluckUint64 did not return the correct number of elements")
}
// check if the plucked collection contains the correct elements
if pluckedColl.Index(0) != 1 || pluckedColl.Index(1) != 2 || pluckedColl.Index(2) != 3 {
t.Errorf("PluckUint64 did not return the correct elements")
}
}
// TestPluckBool tests the PluckBool method of the Collection struct
func TestPluckBool(t *testing.T) {
// create a new Collection with some elements
type User struct {
IsActive bool
IsAdmin bool
}
coll := NewCollection([]User{
{true, false},
{false, true},
{true, true},
})
// pluck the "is_active" field from the collection
pluckedColl := coll.PluckBool("IsActive")
// check if the length of the plucked collection is correct
if pluckedColl.Count() != 3 {
t.Errorf("PluckBool did not return the correct number of elements")
}
// check if the plucked collection contains the correct elements
if pluckedColl.Index(0) != true || pluckedColl.Index(1) != false || pluckedColl.Index(2) != true {
t.Errorf("PluckBool did not return the correct elements")
}
}
// TestSortBy tests the SortBy method of the Collection struct
func TestSortBy(t *testing.T) {
// create a new Collection with some elements
type Person struct {
Id int
Age int
}
coll := NewCollection([]Person{
{3, 20},
{1, 30},
{2, 40},
})
// sort the collection by the "id" field
sortedColl := coll.SortBy("Id")
// check if the length of the sorted collection is correct
if len(sortedColl.value) != 3 {
t.Errorf("SortBy did not return the correct number of elements")
}
// check if the sorted collection contains the correct elements
if sortedColl.value[0].Id != 1 || sortedColl.value[1].Id != 2 || sortedColl.value[2].Id != 3 {
t.Errorf("SortBy did not return the correct elements")
}
}
// TestSortByDesc tests the SortByDesc method of the Collection struct
func TestSortByDesc(t *testing.T) {
// create a new Collection with some elements
type Person struct {
Id int
Age int
}
coll := NewCollection([]Person{
{3, 20},
{1, 30},
{2, 40},
})
// sort the collection by the "id" field in descending order
sortedColl := coll.SortByDesc("Id")
// check if the length of the sorted collection is correct
if sortedColl.Count() != 3 {
t.Errorf("SortByDesc did not return the correct number of elements")
}
// check if the sorted collection contains the correct elements
if sortedColl.Index(0).Id != 3 || sortedColl.Index(1).Id != 2 || sortedColl.Index(2).Id != 1 {
t.Errorf("SortByDesc did not return the correct elements")
}
}
// TestKeyByStrField tests the KeyByStrField method of the Collection struct
func TestKeyByStrField(t *testing.T) {
// create a new Collection with some elements
type Person struct {
Name string
Age string
}
coll := NewCollection([]Person{
{"Alice", "20"},
{"Bob", "30"},
{"Charlie", "40"},
})
// key the collection by the "name" field
keyedColl, err := coll.KeyByStrField("Name")
// check if the error is nil
if err != nil {
t.Errorf("KeyByStrField returned an error")
}
// check if the length of the keyed collection is correct
if len(keyedColl) != 3 {
t.Errorf("KeyByStrField did not return the correct number of elements")
}
// check if the keyed collection contains the correct elements
if _, ok := keyedColl["Alice"]; !ok {
t.Errorf("KeyByStrField did not return the correct elements")
}
if _, ok := keyedColl["Bob"]; !ok {
t.Errorf("KeyByStrField did not return the correct elements")
}
}
// TestMax tests the Max method of the Collection struct
func TestMax(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{1, 2, 3, 4, 5})
// get the maximum element from the collection
max := coll.Max()
// check if the maximum element is correct
if max != 5 {
t.Errorf("Max did not return the correct element")
}
}
// TestMin tests the Min method of the Collection struct
func TestMin(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{1, 2, 3, 4, 5})
// get the minimum element from the collection
min := coll.Min()
// check if the minimum element is correct
if min != 1 {
t.Errorf("Min did not return the correct element")
}
}
// TestContains tests the Contains method of the Collection struct
func TestContains(t *testing.T) {
// create a new Collection with some elements
coll := NewCollection([]int{1, 2, 3, 4, 5})
// check if the collection contains the element 3
if !coll.Contains(3) {
t.Errorf("Contains did not return the correct result")