generated from things-labs/cicd-go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_key_slice_test.go
123 lines (115 loc) · 2.31 KB
/
map_key_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
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
package structs
import (
"reflect"
"sort"
"testing"
"github.com/stretchr/testify/require"
)
func TestKeysOfMap(t *testing.T) {
require.Panics(t, func() { KeysOfMap("no map") })
require.Panics(t, func() { KeysOfMap(map[int]struct{}{1: {}}) })
require.Equal(t, []string{}, KeysOfMap(nil))
require.Equal(t, []string{}, KeysOfMap(map[int]struct{}{}))
want := []string{"1", "2", "3", "4"}
tests := []struct {
name string
m interface{}
want []string
}{
{
"no ptr 1",
map[string]struct{}{
"1": {},
"2": {},
"3": {},
"4": {},
},
want,
},
{
"ptr 1",
&map[string]struct{}{
"1": {},
"2": {},
"3": {},
"4": {},
},
want,
},
{
"no ptr 2",
map[string]string{
"1": "value",
"2": "value",
"3": "value",
"4": "value",
},
want,
},
{
"ptr 2",
&map[string]string{
"1": "value",
"2": "value",
"3": "value",
"4": "value",
},
want,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := KeysOfMap(tt.m)
sort.Strings(got)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("KeysIntOfMap() = %v, want %v", got, tt.want)
}
})
}
}
// Int64Slices attaches the methods of Interface to []int64, sorting a increasing order.
type Int64Slices []int64
func (p Int64Slices) Len() int { return len(p) }
func (p Int64Slices) Less(i, j int) bool { return p[i] < p[j] }
func (p Int64Slices) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func TestKeysIntOfMap(t *testing.T) {
require.Panics(t, func() { KeysIntOfMap("no map") })
require.Panics(t, func() { KeysIntOfMap(map[string]struct{}{"1": {}}) })
require.Equal(t, []int64{}, KeysIntOfMap(nil))
require.Equal(t, []int64{}, KeysIntOfMap(map[string]struct{}{}))
tests := []struct {
name string
m interface{}
want []int64
}{
{
"no ptr",
map[int]string{
1: "value",
2: "value",
3: "value",
4: "value",
},
[]int64{1, 2, 3, 4},
},
{
"ptr",
&map[int]string{
1: "value",
2: "value",
3: "value",
4: "value",
},
[]int64{1, 2, 3, 4},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := KeysIntOfMap(tt.m)
sort.Sort(Int64Slices(got))
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("KeysIntOfMap() = %v, want %v", got, tt.want)
}
})
}
}