-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_test.go
202 lines (171 loc) · 5.16 KB
/
map_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
package rflutil
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_MapLen(t *testing.T) {
t.Run("#1: nil map", func(t *testing.T) {
var m map[int]int
v, err := MapLen(valOf(m))
assert.Nil(t, err)
assert.Equal(t, 0, v)
})
t.Run("#2: empty map", func(t *testing.T) {
v, err := MapLen(valOf(map[int]int{}))
assert.Nil(t, err)
assert.Equal(t, 0, v)
})
t.Run("#3: success", func(t *testing.T) {
v, err := MapLen(valOf(map[int]int{1: 1, 2: 2, 3: 3}))
assert.Nil(t, err)
assert.Equal(t, 3, v)
})
}
func Test_MapLen_failure(t *testing.T) {
t.Run("#1: input is not map", func(t *testing.T) {
_, err := MapLen(valOf("abc"))
assert.ErrorIs(t, err, ErrTypeInvalid)
})
}
func Test_MapGet(t *testing.T) {
t.Run("#1: success", func(t *testing.T) {
v, err := MapGet[uint](valOf(map[int]uint{1: 1, 2: 2, 3: 3}), 1)
assert.Nil(t, err)
assert.Equal(t, uint(1), v)
})
}
func Test_MapGet_failure(t *testing.T) {
t.Run("#1: input is not a map", func(t *testing.T) {
_, err := MapGet[int](valOf("abc"), 1)
assert.ErrorIs(t, err, ErrTypeInvalid)
})
t.Run("#2: value type unmatched", func(t *testing.T) {
_, err := MapGet[int](valOf(map[int]uint{1: 1, 2: 2, 3: 3}), 1)
assert.ErrorIs(t, err, ErrTypeUnmatched)
type T uint
_, err = MapGet[T](valOf(map[int]uint{1: 1, 2: 2, 3: 3}), 2)
assert.ErrorIs(t, err, ErrTypeUnmatched)
})
t.Run("#3: key type unmatched", func(t *testing.T) {
_, err := MapGet[int](valOf(map[int]uint{1: 1, 2: 2, 3: 3}), int64(1))
assert.ErrorIs(t, err, ErrTypeUnmatched)
})
t.Run("#4: key not exist", func(t *testing.T) {
_, err := MapGet[uint](valOf(map[int]uint{1: 1, 2: 2, 3: 3}), 4)
assert.ErrorIs(t, err, ErrNotFound)
})
}
func Test_MapSet(t *testing.T) {
t.Run("#1: success", func(t *testing.T) {
m := map[int]uint{1: 1, 2: 2, 3: 3}
err := MapSet(valOf(m), 1, uint(11))
assert.Nil(t, err)
assert.Equal(t, uint(11), m[1])
})
t.Run("#2: success with new key", func(t *testing.T) {
m := map[int]uint{1: 1, 2: 2, 3: 3}
err := MapSet(valOf(m), 4, uint(44))
assert.Nil(t, err)
assert.Equal(t, uint(44), m[4])
})
}
func Test_MapSet_failure(t *testing.T) {
t.Run("#1: input is not a map", func(t *testing.T) {
err := MapSet(valOf("abc"), 1, 11)
assert.ErrorIs(t, err, ErrTypeInvalid)
})
t.Run("#2: value type unmatched", func(t *testing.T) {
err := MapSet(valOf(map[int]uint{1: 1, 2: 2, 3: 3}), 1, 11)
assert.ErrorIs(t, err, ErrTypeUnmatched)
})
t.Run("#3: key type unmatched", func(t *testing.T) {
err := MapSet(valOf(map[int]uint{1: 1, 2: 2, 3: 3}), int64(1), uint(11))
assert.ErrorIs(t, err, ErrTypeUnmatched)
})
}
func Test_MapDelete(t *testing.T) {
t.Run("#1: success", func(t *testing.T) {
m := map[int]uint{1: 1, 2: 2, 3: 3}
err := MapDelete(valOf(m), 1)
assert.Nil(t, err)
assert.Equal(t, map[int]uint{2: 2, 3: 3}, m)
})
t.Run("#2: success with key not exist, nothing change", func(t *testing.T) {
m := map[int]uint{1: 1, 2: 2, 3: 3}
err := MapDelete(valOf(m), 4)
assert.Nil(t, err)
assert.Equal(t, map[int]uint{1: 1, 2: 2, 3: 3}, m)
})
}
func Test_MapDelete_failure(t *testing.T) {
t.Run("#1: input is not a map", func(t *testing.T) {
err := MapDelete(valOf("abc"), 1)
assert.ErrorIs(t, err, ErrTypeInvalid)
})
t.Run("#2: key type unmatched", func(t *testing.T) {
err := MapDelete(valOf(map[uint]uint{1: 1, 2: 2, 3: 3}), 1)
assert.ErrorIs(t, err, ErrTypeUnmatched)
})
}
func Test_MapKeys(t *testing.T) {
t.Run("#1: nil map", func(t *testing.T) {
var m map[int]uint
keys, err := MapKeys(valOf(m))
assert.Nil(t, err)
assert.Equal(t, []reflect.Value{}, keys)
})
t.Run("#2: empty map", func(t *testing.T) {
keys, err := MapKeys(valOf(map[int]uint{}))
assert.Nil(t, err)
assert.Equal(t, []reflect.Value{}, keys)
})
t.Run("#3: success", func(t *testing.T) {
keys, err := MapKeys(valOf(map[int]uint{1: 1, 2: 2, 3: 3}))
assert.Nil(t, err)
assert.Equal(t, 3, len(keys))
mapChk := map[int]struct{}{1: {}, 2: {}, 3: {}}
for _, k := range keys {
_, ok := mapChk[k.Interface().(int)]
assert.True(t, ok)
}
})
}
func Test_MapKeys_failure(t *testing.T) {
t.Run("#1: input is not a map", func(t *testing.T) {
keys, err := MapKeys(valOf("abc"))
assert.ErrorIs(t, err, ErrTypeInvalid)
assert.Nil(t, keys)
})
}
func Test_MapEntries(t *testing.T) {
t.Run("#1: nil map", func(t *testing.T) {
var m map[int]uint
entries, err := MapEntries(valOf(m))
assert.Nil(t, err)
assert.Equal(t, []MapEntry{}, entries)
})
t.Run("#2: empty map", func(t *testing.T) {
entries, err := MapEntries(valOf(map[int]uint{}))
assert.Nil(t, err)
assert.Equal(t, []MapEntry{}, entries)
})
t.Run("#3: success", func(t *testing.T) {
entries, err := MapEntries(valOf(map[int]uint{1: 1, 2: 2, 3: 3}))
assert.Nil(t, err)
assert.Equal(t, 3, len(entries))
mapChk := map[int]uint{1: 1, 2: 2, 3: 3}
for _, e := range entries {
v, ok := mapChk[e.Key.Interface().(int)]
assert.True(t, ok)
assert.Equal(t, e.Value.Interface(), v)
}
})
}
func Test_MapEntries_failure(t *testing.T) {
t.Run("#1: input is not a map", func(t *testing.T) {
entries, err := MapEntries(valOf("abc"))
assert.ErrorIs(t, err, ErrTypeInvalid)
assert.Nil(t, entries)
})
}