-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsection_test.go
More file actions
381 lines (354 loc) · 7.67 KB
/
section_test.go
File metadata and controls
381 lines (354 loc) · 7.67 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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package gitlabcodeowners
import (
"testing"
"github.com/chefe/gitlabcodeowners/testhelper"
)
func TestSection_parseSectionHeader(t *testing.T) {
t.Parallel()
tests := []struct {
name string
header string
want section
wantErr bool
}{
{
name: "required section with no approval count and no default owners",
header: "[Section name]",
want: section{
name: "Section name",
approvals: 1,
owners: []string{},
rules: []rule{},
},
wantErr: false,
},
{
name: "optional section with no approval count and no default owners",
header: "^[Section name]",
want: section{
name: "Section name",
approvals: 0,
owners: []string{},
rules: []rule{},
},
wantErr: false,
},
{
name: "required section with approval count and no default owners",
header: "[Section name][5]",
want: section{
name: "Section name",
approvals: 5,
owners: []string{},
rules: []rule{},
},
wantErr: false,
},
{
name: "optional section with approval count and no default owners",
header: "^[Section name][5]",
want: section{
name: "Section name",
approvals: 0,
owners: []string{},
rules: []rule{},
},
wantErr: false,
},
{
name: "required section with no approval count and one default owner",
header: "[Section name] @username",
want: section{
name: "Section name",
approvals: 1,
owners: []string{"@username"},
rules: []rule{},
},
wantErr: false,
},
{
name: "optional section with no approval count and one default owner",
header: "^[Section name] @username",
want: section{
name: "Section name",
approvals: 0,
owners: []string{"@username"},
rules: []rule{},
},
wantErr: false,
},
{
name: "required section with approval count and multiple default owners",
header: "[Docs][2] @group @subgroup",
want: section{
name: "Docs",
approvals: 2,
owners: []string{"@group", "@subgroup"},
rules: []rule{},
},
wantErr: false,
},
{
name: "optional section with approval count and multiple default owners",
header: "^[Docs][2] @group @subgroup",
want: section{
name: "Docs",
approvals: 0,
owners: []string{"@group", "@subgroup"},
rules: []rule{},
},
wantErr: false,
},
{
name: "required section with zero as approval count",
header: "[Testing][0]",
want: section{
name: "Testing",
approvals: 1,
owners: []string{},
rules: []rule{},
},
wantErr: false,
},
{
name: "required section with negative approval count",
header: "[Testing][-42]",
want: section{
name: "Testing",
approvals: 1,
owners: []string{},
rules: []rule{},
},
wantErr: false,
},
{
name: "required section where approval count is not a number",
header: "[Legal][abc]",
want: section{
name: "Legal",
approvals: 1,
owners: []string{},
rules: []rule{},
},
wantErr: false,
},
{
name: "missing square closing bracket for section",
header: "[Section name",
want: section{}, //nolint:exhaustruct // default is returned on error
wantErr: true,
},
{
name: "missing square closing bracket for approval count",
header: "[Section name][1 @username",
want: section{}, //nolint:exhaustruct // default is returned on error
wantErr: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := parseSectionHeader(tt.header)
testhelper.DeepEqual(t, got, tt.want)
if (err != nil) != tt.wantErr {
t.Errorf("wantErr=%t but got error %v", tt.wantErr, err)
}
})
}
}
func TestSection_checkBracketCountInSectionHeader(t *testing.T) {
t.Parallel()
tests := []struct {
name string
header string
wantErr bool
}{
{
name: "33 - too much brackets",
header: "[One][Two][Three]",
wantErr: true,
},
{
name: "22 - name with approval count",
header: "[Documentation][4] @docs-team",
wantErr: false,
},
{
name: "11 - only name without approval count",
header: "[Documentation] @docs-team",
wantErr: false,
},
{
name: "21 - missing closing brackets for approval count",
header: "[Documentation][4 @docs-team",
wantErr: true,
},
{
name: "10 - missing closing brackets for name",
header: "[Documentation @docs-team",
wantErr: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
err := checkBracketCountInSectionHeader(tt.header)
if (err != nil) != tt.wantErr {
t.Errorf("wantErr=%t but got error %v", tt.wantErr, err)
}
})
}
}
func TestSection_extractPartsFromSectionHeader(t *testing.T) {
t.Parallel()
type parts struct {
name string
approvals string
owners string
}
tests := []struct {
name string
header string
want parts
}{
{
name: "name with approval count and default owners",
header: "[Documentation][4] @docs-team @specialuser",
want: parts{
name: "Documentation",
approvals: "4",
owners: " @docs-team @specialuser",
},
},
{
name: "name with approval count but no default owners",
header: "[Documentation][4]",
want: parts{
name: "Documentation",
approvals: "4",
owners: "",
},
},
{
name: "name with default owners but no approval count",
header: "[Documentation] @docs-team @specialuser",
want: parts{
name: "Documentation",
approvals: "",
owners: " @docs-team @specialuser",
},
},
{
name: "only name",
header: "[Documentation]",
want: parts{
name: "Documentation",
approvals: "",
owners: "",
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
name, approvals, owners := extractPartsFromSectionHeader(tt.header)
got := parts{name: name, approvals: approvals, owners: owners}
testhelper.DeepEqual(t, got, tt.want)
})
}
}
func TestSection_parseApprovalCount(t *testing.T) {
t.Parallel()
tests := []struct {
name string
count string
optional bool
want int
}{
{
name: "required - valid number",
count: "5",
optional: false,
want: 5,
},
{
name: "required - number with whitespace",
count: " 42\t",
optional: false,
want: 42,
},
{
name: "required - zero",
count: "0",
optional: false,
want: 1,
},
{
name: "required - negative number",
count: "-10",
optional: false,
want: 1,
},
{
name: "required - empty string",
count: "",
optional: false,
want: 1,
},
{
name: "required - not a number",
count: "abc",
optional: false,
want: 1,
},
{
name: "optional - valid number",
count: "5",
optional: true,
want: 0,
},
{
name: "optional - number with whitespace",
count: " 42\t",
optional: true,
want: 0,
},
{
name: "optional - zero",
count: "0",
optional: true,
want: 0,
},
{
name: "optional - negative number",
count: "-10",
optional: true,
want: 0,
},
{
name: "optional - empty string",
count: "",
optional: true,
want: 0,
},
{
name: "optional - not a number",
count: "abc",
optional: true,
want: 0,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := parseApprovalCount(tt.count, tt.optional)
if got != tt.want {
t.Errorf("got %d, wanted %d", got, tt.want)
}
})
}
}