-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollect_test.go
148 lines (113 loc) · 2.53 KB
/
collect_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
package collect
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestToMapOfBool(t *testing.T) {
t.Run("string slice", func(t *testing.T) {
s := []string{"a", "b", "c"}
m := ToMapOfBool(s)
assert.Equal(t, map[string]bool{
"a": true,
"b": true,
"c": true,
}, m)
})
t.Run("empty string slice", func(t *testing.T) {
var s []string
m := ToMapOfBool(s)
assert.Equal(t, map[string]bool{}, m)
})
t.Run("pointer slice", func(t *testing.T) {
a, b, c := 1, 2, 3
s := []*int{&a, &b, &c}
m := ToMapOfBool(s)
assert.Equal(t, map[*int]bool{
&a: true,
&b: true,
&c: true,
}, m)
})
}
func TestToMapOfEmptyStruct(t *testing.T) {
t.Run("string slice", func(t *testing.T) {
s := []string{"a", "b", "c"}
m := ToMapOfEmptyStruct(s)
assert.Equal(t, map[string]struct{}{
"a": {},
"b": {},
"c": {},
}, m)
})
t.Run("empty string slice", func(t *testing.T) {
var s []string
m := ToMapOfEmptyStruct(s)
assert.Equal(t, map[string]struct{}{}, m)
})
}
func TestToMapOfBoolFunc(t *testing.T) {
type outer struct {
s string
}
t.Run("string slice", func(t *testing.T) {
s := []outer{
{s: "a"},
{s: "b"},
{s: "c"},
}
m := ToMapOfBoolFunc(s, func(t outer) string { return t.s })
assert.Equal(t, map[string]bool{
"a": true,
"b": true,
"c": true,
}, m)
})
t.Run("empty string slice", func(t *testing.T) {
var s []outer
m := ToMapOfBoolFunc(s, func(t outer) string { return t.s })
assert.Equal(t, map[string]bool{}, m)
})
}
func TestToMapOfEmptyStructFunc(t *testing.T) {
type outer struct {
s string
}
t.Run("string slice", func(t *testing.T) {
s := []outer{
{s: "a"},
{s: "b"},
{s: "c"},
}
m := ToMapOfEmptyStructFunc(s, func(t outer) string { return t.s })
assert.Equal(t, map[string]struct{}{
"a": {},
"b": {},
"c": {},
}, m)
})
t.Run("empty string slice", func(t *testing.T) {
var s []outer
m := ToMapOfEmptyStructFunc(s, func(t outer) string { return t.s })
assert.Equal(t, map[string]struct{}{}, m)
})
}
func TestToMap(t *testing.T) {
type entity struct {
id int
text string
}
s := []entity{
{id: 1, text: "a"},
{id: 2, text: "b"},
{id: 3, text: "c"},
}
// Using a single kvFunc is chosen over two separate functions because anonymous
// function declaration in Go is very verbose. The goal was to make it fit in one
// line while keeping it readable.
m := ToMap(s, func(e entity) (int, string) { return e.id, e.text })
assert.Equal(t, map[int]string{
1: "a",
2: "b",
3: "c",
}, m)
}