-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings_helpers_test.go
201 lines (173 loc) · 4.52 KB
/
settings_helpers_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
package goservices
import (
"fmt"
"testing"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
)
func Test_validateServicesAreUnique(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
serviceA := NewMockService(ctrl)
serviceA.EXPECT().String().Return("A").AnyTimes()
serviceA2 := NewMockService(ctrl)
serviceA2.EXPECT().String().Return("A").AnyTimes()
serviceB := NewMockService(ctrl)
serviceB.EXPECT().String().Return("B").AnyTimes()
testCases := map[string]struct {
services []Service
errMessage string
}{
"no service": {},
"single service": {
services: []Service{serviceA},
},
"two same services": {
services: []Service{serviceA, serviceA},
errMessage: "service A is duplicated twice",
},
"two same string services": {
services: []Service{serviceA, serviceA2},
errMessage: "service name A is duplicated twice",
},
"two distinct services": {
services: []Service{serviceA, serviceB},
},
}
for name, testCase := range testCases {
testCase := testCase
t.Run(name, func(t *testing.T) {
t.Parallel()
errMessage := validateServicesAreUnique(testCase.services)
assert.Equal(t, testCase.errMessage, errMessage)
})
}
}
func Test_findDuplicateServices(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
serviceA := NewMockService(ctrl)
serviceA.EXPECT().String().Return("A").AnyTimes()
serviceA2 := NewMockService(ctrl)
serviceA2.EXPECT().String().Return("A").AnyTimes()
serviceB := NewMockService(ctrl)
serviceB.EXPECT().String().Return("B").AnyTimes()
testCases := map[string]struct {
services []Service
duplicateServices map[fmt.Stringer]uint
duplicatedNames map[string]uint
}{
"no service": {
duplicateServices: map[fmt.Stringer]uint{},
duplicatedNames: map[string]uint{},
},
"single service": {
services: []Service{serviceA},
duplicateServices: map[fmt.Stringer]uint{},
duplicatedNames: map[string]uint{},
},
"two same services": {
services: []Service{serviceA, serviceA},
duplicateServices: map[fmt.Stringer]uint{
serviceA: 2,
},
duplicatedNames: map[string]uint{
"A": 2,
},
},
"two same string services": {
services: []Service{serviceA, serviceA2},
duplicateServices: map[fmt.Stringer]uint{},
duplicatedNames: map[string]uint{
"A": 2,
},
},
"two distinct services": {
services: []Service{serviceA, serviceB},
duplicateServices: map[fmt.Stringer]uint{},
duplicatedNames: map[string]uint{},
},
}
for name, testCase := range testCases {
testCase := testCase
t.Run(name, func(t *testing.T) {
t.Parallel()
duplicateServices, duplicatedNames := findDuplicatedServices(testCase.services)
assert.Equal(t, testCase.duplicateServices, duplicateServices)
assert.Equal(t, testCase.duplicatedNames, duplicatedNames)
})
}
}
func Test_makeDuplicatedServicesErrMessage(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
serviceA := NewMockService(ctrl)
serviceA.EXPECT().String().Return("A").AnyTimes()
serviceB := NewMockService(ctrl)
serviceB.EXPECT().String().Return("B").AnyTimes()
testCases := map[string]struct {
duplicateServices map[fmt.Stringer]uint
messagePrefix string
errMessage string
}{
"no duplicated service": {
duplicateServices: map[fmt.Stringer]uint{},
},
"single service duplicated 3 times": {
duplicateServices: map[fmt.Stringer]uint{
serviceA: 3,
},
messagePrefix: "service",
errMessage: "service A is duplicated 3 times",
},
"two services duplicated twice": {
duplicateServices: map[fmt.Stringer]uint{
serviceA: 2,
serviceB: 2,
},
messagePrefix: "service",
errMessage: "services A is duplicated twice and " +
"B is duplicated twice",
},
}
for name, testCase := range testCases {
testCase := testCase
t.Run(name, func(t *testing.T) {
t.Parallel()
errMessage := makeDuplicatedServicesErrMessage(
testCase.duplicateServices, testCase.messagePrefix)
assert.Equal(t, testCase.errMessage, errMessage)
})
}
}
func Test_countToString(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
count uint
s string
}{
"zero": {
s: "0 time",
},
"one": {
count: 1,
s: "once",
},
"two": {
count: 2,
s: "twice",
},
"three": {
count: 3,
s: "3 times",
},
}
for name, testCase := range testCases {
testCase := testCase
t.Run(name, func(t *testing.T) {
t.Parallel()
s := countToString(testCase.count)
assert.Equal(t, testCase.s, s)
})
}
}