-
Notifications
You must be signed in to change notification settings - Fork 21
/
map_test.go
182 lines (175 loc) · 4.25 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
package devstatscode
import (
"fmt"
"strings"
"testing"
lib "github.com/cncf/devstatscode"
testlib "github.com/cncf/devstatscode/test"
)
func TestMapFromString(t *testing.T) {
// Test cases
var testCases = []struct {
value string
expected map[string]string
}{
{value: "", expected: nil},
{value: "map[]", expected: nil},
{value: "map[:]", expected: map[string]string{"": ""}},
{value: "map[a:]", expected: map[string]string{"a": ""}},
{value: "map[:b]", expected: map[string]string{"": "b"}},
{value: "map[a:b]", expected: map[string]string{"a": "b"}},
{value: "map[a:b c:d", expected: nil},
{value: "map a:b c:d]", expected: nil},
{value: "map[a:b c:d]", expected: map[string]string{"a": "b", "c": "d"}},
}
// Execute test cases
for index, test := range testCases {
got := lib.MapFromString(test.value)
expected := test.expected
testlib.MakeComparableMapStr(&expected)
oriGot := got
testlib.MakeComparableMapStr(&got)
gotS := fmt.Sprintf("%+v", got)
expectedS := fmt.Sprintf("%+v", expected)
if gotS != expectedS {
t.Errorf(
"test number %d, expected %v got %v, test: %v",
index+1, test.expected, oriGot, test,
)
}
}
}
func TestSkipEmpty(t *testing.T) {
// Test cases
var testCases = []struct {
values []string
expected []string
}{
{values: []string{}, expected: []string{}},
{values: []string{""}, expected: []string{}},
{values: []string{" "}, expected: []string{" "}},
{values: []string{"a"}, expected: []string{"a"}},
{values: []string{"", ""}, expected: []string{"", ""}},
{values: []string{"", "a"}, expected: []string{"", "a"}},
{values: []string{"a", "b"}, expected: []string{"a", "b"}},
}
// Execute test cases
for index, test := range testCases {
got := lib.SkipEmpty(test.values)
if !testlib.CompareStringSlices(got, test.expected) {
t.Errorf(
"test number %d, expected %v length %d, got %v length %d",
index+1, test.expected, len(test.expected), got, len(got),
)
}
}
}
func TestStringsMapToArray(t *testing.T) {
// Test cases
toLower := func(in string) string {
return strings.ToLower(in)
}
var testCases = []struct {
values []string
function func(string) string
expected []string
}{
{
values: []string{},
function: toLower,
expected: []string{},
},
{
values: []string{"A"},
function: toLower,
expected: []string{"a"},
},
{
values: []string{"A", "b", "Cd"},
function: toLower,
expected: []string{"a", "b", "cd"},
},
}
// Execute test cases
for index, test := range testCases {
got := lib.StringsMapToArray(test.function, test.values)
if !testlib.CompareStringSlices(got, test.expected) {
t.Errorf(
"test number %d, expected %v, got %v",
index+1, test.expected, got,
)
}
}
}
func TestStringsMapToSet(t *testing.T) {
// Test cases
stripFunc := func(x string) string {
return strings.TrimSpace(x)
}
var testCases = []struct {
values []string
function func(string) string
expected map[string]struct{}
}{
{
values: []string{},
function: stripFunc,
expected: map[string]struct{}{},
},
{
values: []string{" a\n\t"},
function: stripFunc,
expected: map[string]struct{}{"a": {}},
},
{
values: []string{"a ", " b", "\tc\t", "d e"},
function: stripFunc,
expected: map[string]struct{}{
"a": {},
"b": {},
"c": {},
"d e": {},
},
},
}
// Execute test cases
for index, test := range testCases {
got := lib.StringsMapToSet(test.function, test.values)
if !testlib.CompareSets(got, test.expected) {
t.Errorf(
"test number %d, expected %v, got %v",
index+1, test.expected, got,
)
}
}
}
func TestStringsSetKeys(t *testing.T) {
// Test cases
var testCases = []struct {
set map[string]struct{}
expected []string
}{
{
set: map[string]struct{}{},
expected: []string{},
},
{
set: map[string]struct{}{"xyz": {}},
expected: []string{"xyz"},
},
{
set: map[string]struct{}{"b": {}, "a": {}, "c": {}},
expected: []string{"a", "b", "c"},
},
}
// Execute test cases
for index, test := range testCases {
got := lib.StringsSetKeys(test.set)
if !testlib.CompareStringSlices(got, test.expected) {
t.Errorf(
"test number %d, expected %v, got %v",
index+1, test.expected, got,
)
}
}
}