-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
commentHandler_test.go
517 lines (473 loc) · 12 KB
/
commentHandler_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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
// Copyright (c) Derek Author(s) 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package main
import (
"os"
"testing"
"github.com/alexellis/derek/types"
)
var commandTriggers = []string{commandTriggerDefault, commandTriggerSlash}
func Test_getCommandTrigger(t *testing.T) {
const (
envVar = "use_slash_trigger"
errorMessage = "expected trigger to be: %s, got: %s"
)
var trigger string
// test default trigger
os.Unsetenv(envVar)
trigger = getCommandTrigger()
if trigger != commandTriggerDefault {
t.Errorf(errorMessage, commandTriggerDefault, trigger)
}
// test slash trigger
os.Setenv(envVar, "true")
trigger = getCommandTrigger()
if trigger != commandTriggerSlash {
t.Errorf(errorMessage, commandTriggerSlash, trigger)
}
}
func Test_Parsing_OpenClose(t *testing.T) {
var actionOptions = []struct {
title string
body string
expectedAction string
}{
{
title: "Correct reopen command",
body: "reopen",
expectedAction: "reopen",
},
{ //this case replaces Test_Parsing_Close
title: "Correct close command",
body: "close",
expectedAction: "close",
},
{
title: "invalid command",
body: "dance",
expectedAction: "",
},
{
title: "Longer reopen command",
body: "reopen: ",
expectedAction: "reopen",
},
{
title: "Longer close command",
body: "close: ",
expectedAction: "close",
},
}
for _, test := range actionOptions {
t.Run(test.title, func(t *testing.T) {
for _, trigger := range commandTriggers {
action := parse(trigger+test.body, trigger)
if action.Type != test.expectedAction {
t.Errorf("Action - want: %s, got %s", test.expectedAction, action.Type)
}
}
})
}
}
func Test_Parsing_Labels(t *testing.T) {
var labelOptions = []struct {
title string
body string
expectedType string
expectedVal string
}{
{ //this case replaces Test_Parsing_AddLabel
title: "Add label of demo",
body: "add label: demo",
expectedType: "AddLabel",
expectedVal: "demo",
},
{
title: "Remove label of demo",
body: "remove label: demo",
expectedType: "RemoveLabel",
expectedVal: "demo",
},
{
title: "Invalid label action",
body: "peel label: demo",
expectedType: "",
expectedVal: "",
},
}
for _, test := range labelOptions {
t.Run(test.title, func(t *testing.T) {
for _, trigger := range commandTriggers {
action := parse(trigger+test.body, trigger)
if action.Type != test.expectedType || action.Value != test.expectedVal {
t.Errorf("Action - wanted: %s, got %s\nLabel - wanted: %s, got %s", test.expectedType, action.Type, test.expectedVal, action.Value)
}
}
})
}
}
func Test_Parsing_Assignments(t *testing.T) {
var assignmentOptions = []struct {
title string
body string
expectedType string
expectedVal string
}{
{
title: "Assign to burt",
body: "assign: burt",
expectedType: assignConstant,
expectedVal: "burt",
},
{
title: "Unassign burt",
body: "unassign: burt",
expectedType: unassignConstant,
expectedVal: "burt",
},
{
title: "Assign to me",
body: "assign: me",
expectedType: assignConstant,
expectedVal: "me",
},
{
title: "Unassign me",
body: "unassign: me",
expectedType: unassignConstant,
expectedVal: "me",
},
{
title: "Invalid assignment action",
body: "consign: burt",
expectedType: "",
expectedVal: "",
},
{
title: "Unassign blank",
body: "unassign: ",
expectedType: "",
expectedVal: "",
},
}
for _, test := range assignmentOptions {
t.Run(test.title, func(t *testing.T) {
for _, trigger := range commandTriggers {
action := parse(trigger+test.body, trigger)
if action.Type != test.expectedType || action.Value != test.expectedVal {
t.Errorf("Action - wanted: %s, got %s\nMaintainer - wanted: %s, got %s", test.expectedType, action.Type, test.expectedVal, action.Value)
}
}
})
}
}
func Test_Parsing_Titles(t *testing.T) {
var titleOptions = []struct {
title string
body string
expectedType string
expectedVal string
}{
{
title: "Set Title",
body: "set title: This is a really great Title!",
expectedType: setTitleConstant,
expectedVal: "This is a really great Title!",
},
{
title: "Mis-spelling of title",
body: "set titel: This is a really great Title!",
expectedType: "",
expectedVal: "",
},
{
title: "Empty Title",
body: "set title: ",
expectedType: "", //blank because it should fail isValidCommand
expectedVal: "",
},
{
title: "Empty Title (Double Space)",
body: "set title: ",
expectedType: setTitleConstant,
expectedVal: "",
},
}
for _, test := range titleOptions {
t.Run(test.title, func(t *testing.T) {
for _, trigger := range commandTriggers {
action := parse(trigger+test.body, trigger)
if action.Type != test.expectedType || action.Value != test.expectedVal {
t.Errorf("\nAction - wanted: %s, got %s\nValue - wanted: %s, got %s", test.expectedType, action.Type, test.expectedVal, action.Value)
}
}
})
}
}
func Test_assessState(t *testing.T) {
var stateOptions = []struct {
title string
requestedAction string
currentState string
expectedNewState string
expectedBool bool
}{
{
title: "Currently Closed and trying to close",
requestedAction: closeConstant,
currentState: closedConstant,
expectedNewState: "",
expectedBool: false,
},
{
title: "Currently Open and trying to reopen",
requestedAction: reopenConstant,
currentState: openConstant,
expectedNewState: "",
expectedBool: false,
},
{
title: "Currently Closed and trying to open",
requestedAction: reopenConstant,
currentState: closedConstant,
expectedNewState: openConstant,
expectedBool: true,
},
{
title: "Currently Open and trying to close",
requestedAction: closeConstant,
currentState: openConstant,
expectedNewState: closedConstant,
expectedBool: true,
},
}
for _, test := range stateOptions {
t.Run(test.title, func(t *testing.T) {
newState, validTransition := checkTransition(test.requestedAction, test.currentState)
if newState != test.expectedNewState || validTransition != test.expectedBool {
t.Errorf("\nStates - wanted: %s, got %s\nValidity - wanted: %t, got %t\n", test.expectedNewState, newState, test.expectedBool, validTransition)
}
})
}
}
func Test_validAction(t *testing.T) {
var stateOptions = []struct {
title string
running bool
requestedAction string
start string
stop string
expectedBool bool
}{
{
title: "Currently unlocked and trying to lock",
running: false,
requestedAction: lockConstant,
start: lockConstant,
stop: unlockConstant,
expectedBool: true,
},
{
title: "Currently unlocked and trying to unlock",
running: false,
requestedAction: unlockConstant,
start: lockConstant,
stop: unlockConstant,
expectedBool: false,
},
{
title: "Currently locked and trying to lock",
running: true,
requestedAction: lockConstant,
start: lockConstant,
stop: unlockConstant,
expectedBool: false,
},
{
title: "Currently locked and trying to unlock",
running: true,
requestedAction: unlockConstant,
start: lockConstant,
stop: unlockConstant,
expectedBool: true,
},
}
for _, test := range stateOptions {
t.Run(test.title, func(t *testing.T) {
isValid := validAction(test.running, test.requestedAction, test.start, test.stop)
if isValid != test.expectedBool {
t.Errorf("\nActions - wanted: %t, got %t\n", test.expectedBool, isValid)
}
})
}
}
func Test_findLabel(t *testing.T) {
var stateOptions = []struct {
title string
currentLabels []types.IssueLabel
cmdLabel string
expectedFound bool
}{
{
title: "Label exists lowercase",
currentLabels: []types.IssueLabel{
types.IssueLabel{
Name: "rod",
},
types.IssueLabel{
Name: "jane",
},
types.IssueLabel{
Name: "freddie",
},
},
cmdLabel: "rod",
expectedFound: true,
},
{
title: "Label exists case insensitive",
currentLabels: []types.IssueLabel{
types.IssueLabel{
Name: "rod",
},
types.IssueLabel{
Name: "jane",
},
types.IssueLabel{
Name: "freddie",
},
},
cmdLabel: "Rod",
expectedFound: true,
},
{
title: "Label doesnt exist lowercase",
currentLabels: []types.IssueLabel{
types.IssueLabel{
Name: "rod",
},
types.IssueLabel{
Name: "jane",
},
types.IssueLabel{
Name: "freddie",
},
},
cmdLabel: "derek",
expectedFound: false,
},
{
title: "Label doesnt exist case insensitive",
currentLabels: []types.IssueLabel{
types.IssueLabel{
Name: "rod",
},
types.IssueLabel{
Name: "jane",
},
types.IssueLabel{
Name: "freddie",
},
},
cmdLabel: "Derek",
expectedFound: false,
},
{
title: "no existing labels lowercase",
currentLabels: nil,
cmdLabel: "derek",
expectedFound: false,
},
{title: "Label doesnt exist case insensitive",
currentLabels: nil,
cmdLabel: "Derek",
expectedFound: false,
},
}
for _, test := range stateOptions {
t.Run(test.title, func(t *testing.T) {
labelFound := findLabel(test.currentLabels, test.cmdLabel)
if labelFound != test.expectedFound {
t.Errorf("Find Labels(%s) - wanted: %t, got %t\n", test.title, test.expectedFound, labelFound)
}
})
}
}
func Test_Parsing_Milestones(t *testing.T) {
var milestonesOptions = []struct {
title string
body string
expectedType string
expectedVal string
}{
{
title: "Right set milestone",
body: "set milestone: demo",
expectedType: "SetMilestone",
expectedVal: "demo",
},
{
title: "Right remove milestone",
body: "remove milestone: demo",
expectedType: "RemoveMilestone",
expectedVal: "demo",
},
{
title: "Wrong set milestone",
body: "you ok label: demo",
expectedType: "",
expectedVal: "",
},
}
for _, test := range milestonesOptions {
t.Run(test.title, func(t *testing.T) {
for _, trigger := range commandTriggers {
action := parse(trigger+test.body, trigger)
if action.Type != test.expectedType || action.Value != test.expectedVal {
t.Errorf("Action - wanted: %s, got %s\nLabel - wanted: %s, got %s", test.expectedType, action.Type, test.expectedVal, action.Value)
}
}
})
}
}
func Test_isDcoLabel(t *testing.T) {
dcoLabel := []struct {
title string
label string
expectedBool bool
}{
{
title: "Counts as no-dco - case insensitivity",
label: "NO-DCO",
expectedBool: true,
},
{
title: "Normal no-dco case",
label: "no-dco",
expectedBool: true,
},
{
title: "Counts as no-dco - case insensitivity",
label: "No-Dco",
expectedBool: true,
},
{
title: "Does not follow no-dco so it counts as normal label",
label: "nodco",
expectedBool: false,
},
{
title: "Normal label",
label: "randomlabel",
expectedBool: false,
},
}
for _, test := range dcoLabel {
t.Run(test.label, func(t *testing.T) {
itsDco := isDcoLabel(test.label)
if itsDco != test.expectedBool {
t.Errorf("Wanted `%s` to return: %t but it returned: %t.", test.label, test.expectedBool, itsDco)
}
})
}
}