-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.go
352 lines (282 loc) · 8.83 KB
/
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
// Package cpick is an interactive color picker in the terminal using cview
package cpick
import (
"fmt"
color "github.com/ethanbaker/colors"
"github.com/ethanbaker/cpick/cview"
"github.com/gdamore/tcell/v2"
)
// Default keys, runes, and mod masks for simplicity
var dk tcell.Key = tcell.KeyF64
var dr rune = '_'
var dm tcell.ModMask
// Easy way to simulate a tcell event
func simEvent(key tcell.Key, r rune, m tcell.ModMask) *tcell.EventKey {
return tcell.NewEventKey(key, r, m)
}
// Keys that will be used commonly
var enter tcell.Key = tcell.KeyEnter
var escape tcell.Key = tcell.KeyEscape
var tab tcell.Key = tcell.KeyTab
var movementRunes = []rune{'l', 'h', 'j', 'k', 'G'}
var movementKeys = []tcell.Key{tcell.KeyLeft, tcell.KeyRight, tcell.KeyUp, tcell.KeyDown}
// Tester funcion used to test functions used in cpick without having
// to export them or use them in an interactive application.
func tester() error {
// Test screen setups
hScreenSetup()
svScreenSetup()
// Test non-error returning functions
testHelp()
// Test error returning functions
var errFuncs = [...]func() error{testColorPages, testHTable, testSVTable, testSearch, testInputCapture}
for _, v := range errFuncs {
err := v()
if err != nil {
return err
}
}
_, err := Start(false)
return err
}
func testColorPages() error {
app.SetFocus(colorPages)
// Test setup
colorPageSetup()
// Test done function
colorPageDoneFunc(escape)
colorPageDoneFunc(tab)
// Test selected function
colorPageSelectedFunc(0, 0)
colorPageSelectedFunc(0, 3)
// Test selection changed function
for i := 0; i < len(colorInfo); i++ {
colorInfo[i].table.Select(1, 1)
}
// Test capture handler
err := testColorPageCaptureHandler()
if err != nil {
return err
}
// Test color name getter function
var hsvs = [...]color.HSV{{H: 0, S: 100, V: 100}, {H: 0, S: 100, V: 99}}
var altHsvs = [...]color.HSV{{H: 0, S: 100, V: 99}, {H: 0, S: 100, V: 98}}
for i := 0; i < len(hsvs); i++ {
name := getColorName(hsvs[i], altHsvs[i])
switch i {
case 0:
if name != "red" {
return fmt.Errorf(fmt.Sprintf("Error! getColorName(%v, %v) is not properly returning red!\nOutput: %v\n", hsvs[i], altHsvs[i], name))
}
case 1:
if name != "custom color" {
return fmt.Errorf(fmt.Sprintf("Error! getColorName(%v, %v) is not properly returning custom color!\nOutput: %v\n", hsvs[i], altHsvs[i], name))
}
}
}
// Test path getter function
_, err = getPath()
if err != nil {
return err
}
// Test colors getter function
var paths = [...]string{"", "./testing/colors.json"}
for _, v := range paths {
data := getCustomColors(v)
if data.COLORLIST[0].NAME != "css" {
return fmt.Errorf(fmt.Sprintf("Error! getCustomColors(%v) is not properly returning presetData!\nOutput: %v\n", v, data))
}
}
return nil
}
// Test capture handler
func testColorPageCaptureHandler() error {
var eventRunes = [...]rune{'C', 'c', 'C', 'c', ' ', 'n', 'N'}
for i, v := range eventRunes {
setEvent := simEvent(dk, v, dm)
returnEvent := colorPageCaptureHandler(setEvent)
switch i {
case 0:
colorInfo[colorPageIndex].table.Select(3, 1)
case 1:
colorInfo[colorPageIndex].table.Select(0, 0)
}
if setEvent != returnEvent {
return fmt.Errorf(fmt.Sprintf("Error! colorPageCaptureHandler(%v) is not properly returning event!\nOutput: %v\n", setEvent, returnEvent))
}
}
for _, v := range movementRunes {
setEvent := simEvent(dk, v, dm)
returnEvent := colorPageCaptureHandler(setEvent)
if v == 'G' {
if returnEvent != nil {
return fmt.Errorf(fmt.Sprintf("Error! colorPageCaptureHandler(%v) is not properly returning nil for movement rune 'G'!\nOutput: %v\n", setEvent, returnEvent))
}
} else {
if returnEvent != setEvent {
return fmt.Errorf(fmt.Sprintf("Error! colorPageCaptureHandler(%v) is not properly returning nil for movement runes!\nOutput: %v\n", setEvent, returnEvent))
}
}
}
colorInfo[0].table.Select(1, 1)
for _, v := range movementKeys {
setEvent := simEvent(v, dr, dm)
returnEvent := colorPageCaptureHandler(setEvent)
if v == 'G' {
if returnEvent != nil {
return fmt.Errorf(fmt.Sprintf("Error! colorPageCaptureHandler(%v) is not properly returning nil for movement rune 'G'!\nOutput: %v\n", setEvent, returnEvent))
}
} else {
if returnEvent != setEvent {
return fmt.Errorf(fmt.Sprintf("Error! colorPageCaptureHandler(%v) is not properly returning nil for movement runes!\nOutput: %v\n", setEvent, returnEvent))
}
}
}
return nil
}
func testHTable() error {
app.SetFocus(hTable)
// Test setup function
hTableSetup()
// Test done function
hTableDoneFunc(escape)
hTableDoneFunc(tab)
// Test selected function
hTableSelectedFunc(0, 0)
// Test selection changed function
hTableSelectionChangedFunc(0, 0)
// Test capture handler
for i := 0; i < 2; i++ {
setEvent := simEvent(dk, ' ', dm)
returnEvent := hCaptureHandler(setEvent)
if setEvent != returnEvent {
return fmt.Errorf(fmt.Sprintf("Error! hCaptureHandler(%v) is not properly returning event!\nOutput: %v\n", setEvent, returnEvent))
}
colorInfo[0].table.Select(0, 1)
}
return nil
}
func testSVTable() error {
app.SetFocus(svTable)
// Test setup function
svTableSetup()
// Test done function
svTableDoneFunc(escape)
svTableDoneFunc(tab)
// Test selected function
svTableSelectedFunc(0, 0)
// Test selection changed function
svTableSelectionChangedFunc(0, 0)
svTableSelectionChangedFunc(50, 0)
// Test capture handler
setEvent := simEvent(dk, dr, dm)
returnEvent := svCaptureHandler(setEvent)
if setEvent != returnEvent {
return fmt.Errorf(fmt.Sprintf("Error! svCaptureHandler(%v) is not properly returning event!\nOutput: %v\n", setEvent, returnEvent))
}
// Test draw function
drawSVTable()
// Test block drawing
darkHSV1 := color.HSV{H: 0, S: 101, V: 101}
darkHSV2 := color.HSV{H: -1, S: -1, V: -1}
lightHSV1 := color.HSV{H: 0, S: 101, V: 101}
lightHSV2 := color.HSV{H: -1, S: -1, V: -1}
setColorValues(darkHSV1, darkSVBlock, darkSVText, lightHSV1, lightSVBlock, lightSVText)
setColorValues(darkHSV2, darkSVBlock, darkSVText, lightHSV2, lightSVBlock, lightSVText)
return nil
}
func testHelp() {
app.SetFocus(helpModal)
// Test setup function
helpPageSetup()
var primitives = [...]cview.Primitive{colorPages, hTable, svTable}
// Test done function and show help function
for _, v := range primitives {
helpFocus = v
helpModalDoneFunc(0, "Exit help")
app.SetFocus(v)
showHelp()
}
}
func testSearch() error {
app.SetFocus(searchInput)
// Test setup function
searchInputSetup()
// Test done function
searchInputDoneFunc(escape)
searchInput.SetText("red")
searchInputDoneFunc(enter)
// Test autocomplete function
searchInputAutocompleteFunc("")
searchInputAutocompleteFunc("red")
searchInputAutocompleteFunc("lightgoldenrodyellow")
searchInputAutocompleteFunc("?")
// Test parsing function
parseSearchText("#ffffff")
parseSearchText("#fffffff")
parseSearchText("rgb:a")
parseSearchText("rgb:0 0 0")
parseSearchText("rgb:0 0 -1")
parseSearchText("rgb:0 0 0 0")
parseSearchText("hsv:0 0 0")
parseSearchText("hsv:-1 0 0")
parseSearchText("hsv:0 0 -1")
parseSearchText("hsv:0 0 0 0")
parseSearchText("hsl:0 0 0")
parseSearchText("hsl:-1 0 0")
parseSearchText("hsl:0 0 -1")
parseSearchText("hsl:0 0 0 0")
parseSearchText("cmyk: 0 0 0 0")
parseSearchText("cmyk: 0 0 0 -1")
parseSearchText("cmyk: 0 0 0 0 0")
parseSearchText("decimal: 0")
parseSearchText("decimal: -1")
parseSearchText("decimal: 0 0")
parseSearchText("ansi:a")
// Test capture handler
var eventRunes = [...]rune{'n', 'N'}
for i, v := range eventRunes {
searchIndexes = [][]int{{0, 0, 0}, {1, 1, 1}}
switch i {
case 0:
searchIndex = 0
case 1:
searchIndex = len(searchIndexes) - 1
}
setEvent := simEvent(dk, v, dm)
returnEvent := searchInputCaptureHandler(setEvent)
if setEvent != returnEvent {
return fmt.Errorf(fmt.Sprintf("Error! searchInputCaptureHandler(%v) is not properly returning event!\nOutput: %v\n", setEvent, returnEvent))
}
}
return nil
}
func testInputCapture() error {
var eventKeys = [...]rune{'q', 'q', '`', '?'}
for i, v := range eventKeys {
switch i {
case 1:
app.SetFocus(searchInput)
case 3:
app.SetFocus(svTable)
}
setEvent := simEvent(dk, v, dm)
returnEvent := inputCaptureHandler(setEvent)
if i == 3 {
setEvent = nil
}
if setEvent != returnEvent {
return fmt.Errorf(fmt.Sprintf("Error! inputCaptureHandler(%v) is not properly returning event!\nOutput: %v\n", simEvent(dk, v, dm), returnEvent))
}
}
var primitives = [...]cview.Primitive{hTable, colorPages, svTable}
for _, v := range primitives {
app.SetFocus(v)
setEvent := simEvent(dk, dr, dm)
returnEvent := inputCaptureHandler(setEvent)
if setEvent != returnEvent {
return fmt.Errorf(fmt.Sprintf("Error! inputCaptureHandler(%v) is not properly returning event!\nOutput: %v\n", simEvent(dk, dr, dm), returnEvent))
}
}
return nil
}