-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmaps_test.go
More file actions
365 lines (337 loc) · 7.26 KB
/
maps_test.go
File metadata and controls
365 lines (337 loc) · 7.26 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
package fusion
import (
"reflect"
"sort"
"strconv"
"testing"
)
func TestGetOrDefault(t *testing.T) {
t.Parallel()
// Test cases
testCases := []struct {
name string
inputMap map[string]int
key string
defaultValue int
expected int
}{
{
name: "existing key",
inputMap: map[string]int{
"foo": 42,
"bar": 24,
},
key: "foo",
defaultValue: 0,
expected: 42,
},
{
name: "non-existing key",
inputMap: map[string]int{
"foo": 42,
"bar": 24,
},
key: "baz",
defaultValue: 10,
expected: 10,
},
{
name: "zero default value",
inputMap: map[string]int{
"foo": 42,
"bar": 24,
},
key: "baz",
defaultValue: 0,
expected: 0,
},
}
// Run test cases
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
result := GetOrDefault(testCase.inputMap, testCase.key, testCase.defaultValue)
if result != testCase.expected {
t.Errorf("Test case '%s': expected %v but got %v", testCase.name, testCase.expected, result)
}
})
}
}
func TestKeys(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
input map[string]int
expected []string
}{
{
name: "empty map",
input: map[string]int{},
expected: []string{},
},
{
name: "map with keys",
input: map[string]int{"a": 1, "b": 2, "c": 3},
expected: []string{"a", "b", "c"},
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
result := Keys(testCase.input)
sort.Strings(result)
sort.Strings(testCase.expected)
if !reflect.DeepEqual(result, testCase.expected) {
t.Errorf("expected %v but got %v", testCase.expected, result)
}
})
}
}
func TestMapKeys(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
input map[string]int
fn func(string, int) string
expected []string
}{
{
name: "empty map",
input: map[string]int{},
fn: func(k string, v int) string { return k },
expected: []string{},
},
{
name: "map with keys of different types",
input: map[string]int{
"one": 1,
"two": 2,
"three": 3,
},
fn: func(k string, v int) string { return k + "-key" },
expected: []string{"one-key", "two-key", "three-key"},
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
result := MapKeys(testCase.input, testCase.fn)
sort.Strings(result) // Sort the result for comparison
sort.Strings(testCase.expected) // Sort the expected slice for comparison
if !reflect.DeepEqual(result, testCase.expected) {
t.Errorf("expected %v but got %v", testCase.expected, result)
}
})
}
}
func TestMapValues(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
input map[string]int
fn func(string, int) string
expected []string
}{
{
name: "empty map",
input: map[string]int{},
fn: func(k string, v int) string { return strconv.Itoa(v) },
expected: []string{},
},
{
name: "map with values of different types",
input: map[string]int{
"one": 1,
"two": 2,
"three": 3,
},
fn: func(k string, v int) string { return strconv.Itoa(v) + "-value" },
expected: []string{"1-value", "2-value", "3-value"},
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
result := MapValues(testCase.input, testCase.fn)
sort.Strings(result) // Sort the result for comparison
sort.Strings(testCase.expected) // Sort the expected slice for comparison
if !reflect.DeepEqual(result, testCase.expected) {
t.Errorf("expected %v but got %v", testCase.expected, result)
}
})
}
}
func TestMerge(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
maps []map[string]int
expected map[string]int
}{
{
name: "empty maps",
maps: []map[string]int{},
expected: map[string]int{},
},
{
name: "maps with non-overlapping keys",
maps: []map[string]int{
{"a": 1, "b": 2},
{"c": 3, "d": 4},
{"e": 5, "f": 6},
},
expected: map[string]int{
"a": 1,
"b": 2,
"c": 3,
"d": 4,
"e": 5,
"f": 6,
},
},
{
name: "maps with overlapping keys",
maps: []map[string]int{
{"a": 1, "b": 2},
{"b": 3, "c": 4},
{"c": 5, "d": 6},
},
expected: map[string]int{
"a": 1,
"b": 3,
"c": 5,
"d": 6,
},
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
result := Merge(testCase.maps...)
if !reflect.DeepEqual(result, testCase.expected) {
t.Errorf("expected %v but got %v", testCase.expected, result)
}
})
}
}
func TestOmit(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
input map[string]int
keys []string
expected map[string]int
}{
{
name: "empty map",
input: map[string]int{},
keys: []string{"a", "b"},
expected: map[string]int{},
},
{
name: "non-empty map with omitted keys",
input: map[string]int{
"a": 1,
"b": 2,
"c": 3,
},
keys: []string{"a", "c"},
expected: map[string]int{
"b": 2,
},
},
{
name: "non-empty map with non-existing keys",
input: map[string]int{
"a": 1,
"b": 2,
"c": 3,
},
keys: []string{"d", "e"},
expected: map[string]int{
"a": 1,
"b": 2,
"c": 3,
},
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
result := Omit(testCase.input, testCase.keys...)
if !reflect.DeepEqual(result, testCase.expected) {
t.Errorf("expected %v but got %v", testCase.expected, result)
}
})
}
}
func TestPick(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
input map[string]int
keys []string
expected map[string]int
}{
{
name: "empty map",
input: map[string]int{},
keys: []string{"a", "b"},
expected: map[string]int{},
},
{
name: "non-empty map with selected keys",
input: map[string]int{
"a": 1,
"b": 2,
"c": 3,
},
keys: []string{"a", "c"},
expected: map[string]int{
"a": 1,
"c": 3,
},
},
{
name: "non-empty map with non-existing keys",
input: map[string]int{
"a": 1,
"b": 2,
"c": 3,
},
keys: []string{"d", "e"},
expected: map[string]int{},
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
result := Pick(testCase.input, testCase.keys...)
if !reflect.DeepEqual(result, testCase.expected) {
t.Errorf("expected %v but got %v", testCase.expected, result)
}
})
}
}
func TestValues(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
input map[string]int
expected []int
}{
{
name: "empty map",
input: map[string]int{},
expected: []int{},
},
{
name: "map with values",
input: map[string]int{"a": 1, "b": 2, "c": 3},
expected: []int{1, 2, 3},
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
result := Values(testCase.input)
sort.Ints(result)
sort.Ints(testCase.expected)
if !reflect.DeepEqual(result, testCase.expected) {
t.Errorf("expected %v but got %v", testCase.expected, result)
}
})
}
}