forked from samber/lo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_test.go
91 lines (71 loc) · 1.75 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
package lo
import (
"sort"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
func TestKeys(t *testing.T) {
is := assert.New(t)
r1 := Keys[string, int](map[string]int{"foo": 1, "bar": 2})
sort.Strings(r1)
is.Equal(r1, []string{"bar", "foo"})
}
func TestValues(t *testing.T) {
is := assert.New(t)
r1 := Values[string, int](map[string]int{"foo": 1, "bar": 2})
sort.Ints(r1)
is.Equal(r1, []int{1, 2})
}
func TestEntries(t *testing.T) {
is := assert.New(t)
r1 := Entries[string, int](map[string]int{"foo": 1, "bar": 2})
sort.Slice(r1, func(i, j int) bool {
return r1[i].Value < r1[j].Value
})
is.EqualValues(r1, []Entry[string, int]{
{
Key: "foo",
Value: 1,
},
{
Key: "bar",
Value: 2,
},
})
}
func TestFromEntries(t *testing.T) {
is := assert.New(t)
r1 := FromEntries[string, int]([]Entry[string, int]{
{
Key: "foo",
Value: 1,
},
{
Key: "bar",
Value: 2,
},
})
is.Len(r1, 2)
is.Equal(r1["foo"], 1)
is.Equal(r1["bar"], 2)
}
func TestAssign(t *testing.T) {
is := assert.New(t)
result1 := Assign[string, int](map[string]int{"a": 1, "b": 2}, map[string]int{"b": 3, "c": 4})
is.Len(result1, 3)
is.Equal(result1, map[string]int{"a": 1, "b": 3, "c": 4})
}
func TestMapValues(t *testing.T) {
is := assert.New(t)
result1 := MapValues[int, int, string](map[int]int{1: 1, 2: 2, 3: 3, 4: 4}, func(x int, _ int) string {
return "Hello"
})
result2 := MapValues[int, int64, string](map[int]int64{1: 1, 2: 2, 3: 3, 4: 4}, func(x int64, _ int) string {
return strconv.FormatInt(x, 10)
})
is.Equal(len(result1), 4)
is.Equal(len(result2), 4)
is.Equal(result1, map[int]string{1: "Hello", 2: "Hello", 3: "Hello", 4: "Hello"})
is.Equal(result2, map[int]string{1: "1", 2: "2", 3: "3", 4: "4"})
}