generated from bradfordwagner/gh-template-go-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slice_test.go
81 lines (73 loc) · 2.17 KB
/
slice_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
package bwutil_test
import (
"strconv"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
bwutil "github.com/bradfordwagner/go-util"
)
var _ = Describe("Slice", func() {
It("maintains order", func() {
a, expected := []string{"1", "2", "3"}, []int{1, 2, 3}
res := bwutil.SliceMap(a, func(a string) (b int) {
b, _ = strconv.Atoi(a)
return
})
Expect(res).To(Equal(expected))
})
It("takes a struct and returns a map", func() {
type testStruct struct {
A string
B int
}
s := []testStruct{{"1", 1}, {"2", 2}, {"3", 3}}
expected := map[string]int{"1": 1, "2": 2, "3": 3}
res := bwutil.SliceToMap(s, func(a testStruct) (k string, v int) {
return a.A, a.B
})
Expect(res).To(Equal(expected))
})
Context("SliceRemove", func() {
It("is an empty list", func() {
input, remove, expected := []string{}, "2", []string{}
res := bwutil.SliceRemove(input, remove)
Expect(res).To(Equal(expected))
})
It("contains the element and removes it", func() {
input, remove, expected := []string{"1", "2", "3"}, "2", []string{"1", "3"}
res := bwutil.SliceRemove(input, remove)
Expect(res).To(Equal(expected))
})
It("does not contains the element and does not remove anything", func() {
input, remove, expected := []string{"1", "3"}, "2", []string{"1", "3"}
res := bwutil.SliceRemove(input, remove)
Expect(res).To(Equal(expected))
})
It("removes first item", func() {
input, remove, expected := []string{"1", "2", "3"}, "1", []string{"2", "3"}
res := bwutil.SliceRemove(input, remove)
Expect(res).To(Equal(expected))
})
It("removes last item", func() {
input, remove, expected := []string{"1", "2", "3"}, "3", []string{"1", "2"}
res := bwutil.SliceRemove(input, remove)
Expect(res).To(Equal(expected))
})
})
It("IsSlice", func() {
type args struct {
a interface{}
pass bool
}
var test = func(args args) {
Expect(bwutil.IsSlice(args.a)).To(Equal(args.pass))
}
test(args{[]string{"a", "b"}, true})
test(args{[]int{1, 2, 3}, true})
test(args{[]int{}, true})
test(args{1, false})
test(args{"a", false})
type invalid struct{}
test(args{invalid{}, false})
test(args{[]invalid{}, true})
})
})