forked from AlecAivazis/survey
-
Notifications
You must be signed in to change notification settings - Fork 9
/
input_test.go
433 lines (418 loc) · 12.6 KB
/
input_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
package survey
import (
"bytes"
"fmt"
"io"
"os"
"strings"
"testing"
"github.com/plandex-ai/survey/v2/core"
"github.com/plandex-ai/survey/v2/terminal"
"github.com/stretchr/testify/assert"
)
func init() {
// disable color output for all prompts to simplify testing
core.DisableColor = true
}
func TestInputRender(t *testing.T) {
suggestFn := func(string) (s []string) { return s }
tests := []struct {
title string
prompt Input
data InputTemplateData
expected string
}{
{
"Test Input question output without default",
Input{Message: "What is your favorite month:"},
InputTemplateData{},
fmt.Sprintf("%s What is your favorite month: ", defaultIcons().Question.Text),
},
{
"Test Input question output with default",
Input{Message: "What is your favorite month:", Default: "April"},
InputTemplateData{},
fmt.Sprintf("%s What is your favorite month: (April) ", defaultIcons().Question.Text),
},
{
"Test Input answer output",
Input{Message: "What is your favorite month:"},
InputTemplateData{ShowAnswer: true, Answer: "October"},
fmt.Sprintf("%s What is your favorite month: October\n", defaultIcons().Question.Text),
},
{
"Test Input question output without default but with help hidden",
Input{Message: "What is your favorite month:", Help: "This is helpful"},
InputTemplateData{},
fmt.Sprintf("%s What is your favorite month: [%s for help] ", defaultIcons().Question.Text, string(defaultPromptConfig().HelpInput)),
},
{
"Test Input question output with default and with help hidden",
Input{Message: "What is your favorite month:", Default: "April", Help: "This is helpful"},
InputTemplateData{},
fmt.Sprintf("%s What is your favorite month: [%s for help] (April) ", defaultIcons().Question.Text, string(defaultPromptConfig().HelpInput)),
},
{
"Test Input question output without default but with help shown",
Input{Message: "What is your favorite month:", Help: "This is helpful"},
InputTemplateData{ShowHelp: true},
fmt.Sprintf("%s This is helpful\n%s What is your favorite month: ", defaultIcons().Help.Text, defaultIcons().Question.Text),
},
{
"Test Input question output with default and with help shown",
Input{Message: "What is your favorite month:", Default: "April", Help: "This is helpful"},
InputTemplateData{ShowHelp: true},
fmt.Sprintf("%s This is helpful\n%s What is your favorite month: (April) ", defaultIcons().Help.Text, defaultIcons().Question.Text),
},
{
"Test Input question output with completion",
Input{Message: "What is your favorite month:", Suggest: suggestFn},
InputTemplateData{},
fmt.Sprintf("%s What is your favorite month: [%s for suggestions] ", defaultIcons().Question.Text, string(defaultPromptConfig().SuggestInput)),
},
{
"Test Input question output with suggestions and help hidden",
Input{Message: "What is your favorite month:", Suggest: suggestFn, Help: "This is helpful"},
InputTemplateData{},
fmt.Sprintf("%s What is your favorite month: [%s for help, %s for suggestions] ", defaultIcons().Question.Text, string(defaultPromptConfig().HelpInput), string(defaultPromptConfig().SuggestInput)),
},
{
"Test Input question output with suggestions and default and help hidden",
Input{Message: "What is your favorite month:", Suggest: suggestFn, Help: "This is helpful", Default: "April"},
InputTemplateData{},
fmt.Sprintf("%s What is your favorite month: [%s for help, %s for suggestions] (April) ", defaultIcons().Question.Text, string(defaultPromptConfig().HelpInput), string(defaultPromptConfig().SuggestInput)),
},
{
"Test Input question output with suggestions shown",
Input{Message: "What is your favorite month:", Suggest: suggestFn},
InputTemplateData{
Answer: "February",
PageEntries: core.OptionAnswerList([]string{"January", "February", "March", "etc..."}),
SelectedIndex: 1,
},
fmt.Sprintf(
"%s What is your favorite month: February [Use arrows to move, enter to select, type to continue]\n"+
" January\n%s February\n March\n etc...\n",
defaultIcons().Question.Text, defaultPromptConfig().Icons.SelectFocus.Text,
),
},
}
for _, test := range tests {
t.Run(test.title, func(t *testing.T) {
r, w, err := os.Pipe()
assert.NoError(t, err)
test.prompt.WithStdio(terminal.Stdio{Out: w})
test.data.Input = test.prompt
// set the runtime config
test.data.Config = defaultPromptConfig()
err = test.prompt.Render(
InputQuestionTemplate,
test.data,
)
assert.NoError(t, err)
assert.NoError(t, w.Close())
var buf bytes.Buffer
_, err = io.Copy(&buf, r)
assert.NoError(t, err)
assert.Contains(t, buf.String(), test.expected)
})
}
}
func TestInputPrompt(t *testing.T) {
tests := []PromptTest{
{
"Test Input prompt interaction",
&Input{
Message: "What is your name?",
},
func(c expectConsole) {
c.ExpectString("What is your name?")
c.SendLine("Larry Bird")
c.ExpectEOF()
},
"Larry Bird",
},
{
"Test Input prompt interaction with default",
&Input{
Message: "What is your name?",
Default: "Johnny Appleseed",
},
func(c expectConsole) {
c.ExpectString("What is your name?")
c.SendLine("")
c.ExpectEOF()
},
"Johnny Appleseed",
},
{
"Test Input prompt interaction overriding default",
&Input{
Message: "What is your name?",
Default: "Johnny Appleseed",
},
func(c expectConsole) {
c.ExpectString("What is your name?")
c.SendLine("Larry Bird")
c.ExpectEOF()
},
"Larry Bird",
},
{
"Test Input prompt interaction and prompt for help",
&Input{
Message: "What is your name?",
Help: "It might be Satoshi Nakamoto",
},
func(c expectConsole) {
c.ExpectString("What is your name?")
c.SendLine("?")
c.ExpectString("It might be Satoshi Nakamoto")
c.SendLine("Satoshi Nakamoto")
c.ExpectEOF()
},
"Satoshi Nakamoto",
},
{
// https://en.wikipedia.org/wiki/ANSI_escape_code
// Device Status Report - Reports the cursor position (CPR) to the
// application as (as though typed at the keyboard) ESC[n;mR, where n is the
// row and m is the column.
"SKIP: Test Input prompt with R matching DSR",
&Input{
Message: "What is your name?",
},
func(c expectConsole) {
c.ExpectString("What is your name?")
c.SendLine("R")
c.ExpectEOF()
},
"R",
},
{
"Test Input prompt interaction when delete",
&Input{
Message: "What is your name?",
},
func(c expectConsole) {
c.ExpectString("What is your name?")
c.Send("Johnny ")
c.Send(string(terminal.KeyDelete))
c.SendLine("")
c.ExpectEOF()
},
"Johnny",
},
{
"Test Input prompt interaction when delete rune",
&Input{
Message: "What is your name?",
},
func(c expectConsole) {
c.ExpectString("What is your name?")
c.Send("小明")
c.Send(string(terminal.KeyBackspace))
c.SendLine("")
c.ExpectEOF()
},
"小",
},
{
"Test Input prompt interaction when ask for suggestion with empty value",
&Input{
Message: "What is your favorite month?",
Suggest: func(string) []string {
return []string{"January", "February"}
},
},
func(c expectConsole) {
c.ExpectString("What is your favorite month?")
c.Send(string(terminal.KeyTab))
c.ExpectString("January")
c.ExpectString("February")
c.SendLine("")
c.ExpectEOF()
},
"January",
},
{
"Test Input prompt interaction when ask for suggestion with some value",
&Input{
Message: "What is your favorite month?",
Suggest: func(string) []string {
return []string{"February"}
},
},
func(c expectConsole) {
c.ExpectString("What is your favorite month?")
c.Send("feb")
c.Send(string(terminal.KeyTab))
c.SendLine("")
c.ExpectEOF()
},
"February",
},
{
"Test Input prompt interaction when ask for suggestion with some value, choosing the second one",
&Input{
Message: "What is your favorite month?",
Suggest: func(string) []string {
return []string{"January", "February", "March"}
},
},
func(c expectConsole) {
c.ExpectString("What is your favorite month?")
c.Send(string(terminal.KeyTab))
c.Send(string(terminal.KeyArrowDown))
c.Send(string(terminal.KeyArrowDown))
c.SendLine("")
c.ExpectEOF()
},
"March",
},
{
"Test Input prompt interaction when ask for suggestion with some value, choosing the second one",
&Input{
Message: "What is your favorite month?",
Suggest: func(string) []string {
return []string{"January", "February", "March"}
},
},
func(c expectConsole) {
c.ExpectString("What is your favorite month?")
c.Send(string(terminal.KeyTab))
c.Send(string(terminal.KeyArrowDown))
c.Send(string(terminal.KeyArrowDown))
c.Send(string(terminal.KeyArrowUp))
c.SendLine("")
c.ExpectEOF()
},
"February",
},
{
"Test Input prompt interaction when ask for suggestion, complementing it and get new suggestions",
&Input{
Message: "Where to save it?",
Suggest: func(complete string) []string {
if complete == "" {
return []string{"folder1/", "folder2/", "folder3/"}
}
return []string{"folder3/file1.txt", "folder3/file2.txt"}
},
},
func(c expectConsole) {
c.ExpectString("Where to save it?")
c.Send(string(terminal.KeyTab))
c.ExpectString("folder1/")
c.Send(string(terminal.KeyArrowDown))
c.Send(string(terminal.KeyArrowDown))
c.Send("f")
c.Send(string(terminal.KeyTab))
c.ExpectString("folder3/file2.txt")
c.Send(string(terminal.KeyArrowDown))
c.SendLine("")
c.ExpectEOF()
},
"folder3/file2.txt",
},
{
"Test Input prompt interaction when asked suggestions, but abort suggestions",
&Input{
Message: "Wanna a suggestion?",
Suggest: func(string) []string {
return []string{"suggest1", "suggest2"}
},
},
func(c expectConsole) {
c.ExpectString("Wanna a suggestion?")
c.Send("typed answer")
c.Send(string(terminal.KeyTab))
c.ExpectString("suggest1")
c.Send(string(terminal.KeyEscape))
c.ExpectString("typed answer")
c.SendLine("")
c.ExpectEOF()
},
"typed answer",
},
{
"Test Input prompt interaction with suggestions, when tabbed with list being shown, should select next suggestion",
&Input{
Message: "Choose the special one:",
Suggest: func(string) []string {
return []string{"suggest1", "suggest2", "special answer"}
},
},
func(c expectConsole) {
c.ExpectString("Choose the special one:")
c.Send("s")
c.Send(string(terminal.KeyTab))
c.ExpectString("suggest1")
c.ExpectString("suggest2")
c.ExpectString("special answer")
c.Send(string(terminal.KeyTab))
c.Send(string(terminal.KeyTab))
c.SendLine("")
c.ExpectEOF()
},
"special answer",
},
{
"Test Input prompt must allow moving cursor using right and left arrows",
&Input{Message: "Filename to save:"},
func(c expectConsole) {
c.ExpectString("Filename to save:")
c.Send("essay.txt")
c.Send(string(terminal.KeyArrowLeft))
c.Send(string(terminal.KeyArrowLeft))
c.Send(string(terminal.KeyArrowLeft))
c.Send(string(terminal.KeyArrowLeft))
c.Send("_final")
c.Send(string(terminal.KeyArrowRight))
c.Send(string(terminal.KeyArrowRight))
c.Send(string(terminal.KeyArrowRight))
c.Send(string(terminal.KeyArrowRight))
c.Send(string(terminal.KeyBackspace))
c.Send(string(terminal.KeyBackspace))
c.Send(string(terminal.KeyBackspace))
c.Send("md")
c.Send(string(terminal.KeyArrowLeft))
c.Send(string(terminal.KeyArrowLeft))
c.Send(string(terminal.KeyArrowLeft))
c.SendLine("2")
c.ExpectEOF()
},
"essay_final2.md",
},
{
"Test Input prompt must allow moving cursor using right and left arrows, even after suggestions",
&Input{Message: "Filename to save:", Suggest: func(string) []string { return []string{".txt", ".csv", ".go"} }},
func(c expectConsole) {
c.ExpectString("Filename to save:")
c.Send(string(terminal.KeyTab))
c.ExpectString(".txt")
c.ExpectString(".csv")
c.ExpectString(".go")
c.Send(string(terminal.KeyTab))
c.Send(string(terminal.KeyArrowLeft))
c.Send(string(terminal.KeyArrowLeft))
c.Send(string(terminal.KeyArrowLeft))
c.Send(string(terminal.KeyArrowLeft))
c.Send(string(terminal.KeyArrowLeft))
c.Send("newtable")
c.SendLine("")
c.ExpectEOF()
},
"newtable.csv",
},
}
for _, test := range tests {
testName := strings.TrimPrefix(test.name, "SKIP: ")
t.Run(testName, func(t *testing.T) {
if testName != test.name {
t.Skipf("warning: flakey test %q", testName)
}
RunPromptTest(t, test)
})
}
}