-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathcommentHandler_test.go
399 lines (364 loc) · 9.1 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
package main
import (
"testing"
"github.com/alexellis/derek/types"
)
var actionOptions = []struct {
title string
body string
expectedAction string
}{
{
title: "Correct reopen command",
body: "Derek reopen",
expectedAction: "reopen",
},
{ //this case replaces Test_Parsing_Close
title: "Correct close command",
body: "Derek close",
expectedAction: "close",
},
{
title: "invalid command",
body: "Derek dance",
expectedAction: "",
},
{
title: "Longer reopen command",
body: "Derek reopen: ",
expectedAction: "reopen",
},
{
title: "Longer close command",
body: "Derek close: ",
expectedAction: "close",
},
}
func Test_Parsing_OpenClose(t *testing.T) {
for _, test := range actionOptions {
t.Run(test.title, func(t *testing.T) {
action := parse(test.body)
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: "Derek add label: demo",
expectedType: "AddLabel",
expectedVal: "demo",
},
{
title: "Remove label of demo",
body: "Derek remove label: demo",
expectedType: "RemoveLabel",
expectedVal: "demo",
},
{
title: "Invalid label action",
body: "Derek peel label: demo",
expectedType: "",
expectedVal: "",
},
}
for _, test := range labelOptions {
t.Run(test.title, func(t *testing.T) {
action := parse(test.body)
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: "Derek assign: burt",
expectedType: assignConstant,
expectedVal: "burt",
},
{
title: "Unassign burt",
body: "Derek unassign: burt",
expectedType: unassignConstant,
expectedVal: "burt",
},
{
title: "Assign to me",
body: "Derek assign: me",
expectedType: assignConstant,
expectedVal: "me",
},
{
title: "Unassign me",
body: "Derek unassign: me",
expectedType: unassignConstant,
expectedVal: "me",
},
{
title: "Invalid assignment action",
body: "Derek consign: burt",
expectedType: "",
expectedVal: "",
},
{
title: "Unassign blank",
body: "Derek unassign: ",
expectedType: "",
expectedVal: "",
},
}
for _, test := range assignmentOptions {
t.Run(test.title, func(t *testing.T) {
action := parse(test.body)
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: "Derek set title: This is a really great Title!",
expectedType: setTitleConstant,
expectedVal: "This is a really great Title!",
},
{
title: "Mis-spelling of title",
body: "Derek set titel: This is a really great Title!",
expectedType: "",
expectedVal: "",
},
{
title: "Empty Title",
body: "Derek set title: ",
expectedType: "", //blank because it should fail isValidCommand
expectedVal: "",
},
{
title: "Empty Title (Double Space)",
body: "Derek set title: ",
expectedType: setTitleConstant,
expectedVal: "",
},
}
for _, test := range titleOptions {
t.Run(test.title, func(t *testing.T) {
action := parse(test.body)
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)
}
})
}
}