This repository has been archived by the owner on Apr 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 357
/
select_test.go
376 lines (358 loc) · 8.98 KB
/
select_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
package survey
import (
"bytes"
"fmt"
"io"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/AlecAivazis/survey/v2/core"
"github.com/AlecAivazis/survey/v2/terminal"
)
func init() {
// disable color output for all prompts to simplify testing
core.DisableColor = true
}
func TestSelectRender(t *testing.T) {
prompt := Select{
Message: "Pick your word:",
Options: []string{"foo", "bar", "baz", "buz"},
Default: "baz",
}
helpfulPrompt := prompt
helpfulPrompt.Help = "This is helpful"
tests := []struct {
title string
prompt Select
data SelectTemplateData
expected string
}{
{
"Test Select question output",
prompt,
SelectTemplateData{SelectedIndex: 2, PageEntries: core.OptionAnswerList(prompt.Options)},
strings.Join(
[]string{
fmt.Sprintf("%s Pick your word: [Use arrows to move, type to filter]", defaultIcons().Question.Text),
" foo",
" bar",
fmt.Sprintf("%s baz", defaultIcons().SelectFocus.Text),
" buz\n",
},
"\n",
),
},
{
"Test Select answer output",
prompt,
SelectTemplateData{Answer: "buz", ShowAnswer: true, PageEntries: core.OptionAnswerList(prompt.Options)},
fmt.Sprintf("%s Pick your word: buz\n", defaultIcons().Question.Text),
},
{
"Test Select question output with help hidden",
helpfulPrompt,
SelectTemplateData{SelectedIndex: 2, PageEntries: core.OptionAnswerList(prompt.Options)},
strings.Join(
[]string{
fmt.Sprintf("%s Pick your word: [Use arrows to move, type to filter, %s for more help]", defaultIcons().Question.Text, string(defaultPromptConfig().HelpInput)),
" foo",
" bar",
fmt.Sprintf("%s baz", defaultIcons().SelectFocus.Text),
" buz\n",
},
"\n",
),
},
{
"Test Select question output with help shown",
helpfulPrompt,
SelectTemplateData{SelectedIndex: 2, ShowHelp: true, PageEntries: core.OptionAnswerList(prompt.Options)},
strings.Join(
[]string{
fmt.Sprintf("%s This is helpful", defaultIcons().Help.Text),
fmt.Sprintf("%s Pick your word: [Use arrows to move, type to filter]", defaultIcons().Question.Text),
" foo",
" bar",
fmt.Sprintf("%s baz", defaultIcons().SelectFocus.Text),
" buz\n",
},
"\n",
),
},
}
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.Select = test.prompt
// set the icon set
test.data.Config = defaultPromptConfig()
err = test.prompt.Render(
SelectQuestionTemplate,
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 TestSelectPrompt(t *testing.T) {
tests := []PromptTest{
{
"basic interaction: blue",
&Select{
Message: "Choose a color:",
Options: []string{"red", "blue", "green"},
},
func(c expectConsole) {
c.ExpectString("Choose a color:")
// Select blue.
c.SendLine(string(terminal.KeyArrowDown))
c.ExpectEOF()
},
core.OptionAnswer{Index: 1, Value: "blue"},
},
{
"basic interaction: green",
&Select{
Message: "Choose a color:",
Options: []string{"red", "blue", "green"},
},
func(c expectConsole) {
c.ExpectString("Choose a color:")
// Select blue.
c.Send(string(terminal.KeyArrowDown))
// Select green.
c.SendLine(string(terminal.KeyTab))
c.ExpectEOF()
},
core.OptionAnswer{Index: 2, Value: "green"},
},
{
"default value",
&Select{
Message: "Choose a color:",
Options: []string{"red", "blue", "green"},
Default: "green",
},
func(c expectConsole) {
c.ExpectString("Choose a color:")
// Select green.
c.SendLine("")
c.ExpectEOF()
},
core.OptionAnswer{Index: 2, Value: "green"},
},
{
"default index",
&Select{
Message: "Choose a color:",
Options: []string{"red", "blue", "green"},
Default: 2,
},
func(c expectConsole) {
c.ExpectString("Choose a color:")
// Select green.
c.SendLine("")
c.ExpectEOF()
},
core.OptionAnswer{Index: 2, Value: "green"},
},
{
"overriding default",
&Select{
Message: "Choose a color:",
Options: []string{"red", "blue", "green"},
Default: "blue",
},
func(c expectConsole) {
c.ExpectString("Choose a color:")
// Select red.
c.SendLine(string(terminal.KeyArrowUp))
c.ExpectEOF()
},
core.OptionAnswer{Index: 0, Value: "red"},
},
{
"SKIP: prompt for help",
&Select{
Message: "Choose a color:",
Options: []string{"red", "blue", "green"},
Help: "My favourite color is red",
},
func(c expectConsole) {
c.ExpectString("Choose a color:")
c.SendLine("?")
c.ExpectString("My favourite color is red")
// Select red.
c.SendLine("")
c.ExpectEOF()
},
core.OptionAnswer{Index: 0, Value: "red"},
},
{
"PageSize",
&Select{
Message: "Choose a color:",
Options: []string{"red", "blue", "green"},
PageSize: 1,
},
func(c expectConsole) {
c.ExpectString("Choose a color:")
// Select green.
c.SendLine(string(terminal.KeyArrowUp))
c.ExpectEOF()
},
core.OptionAnswer{Index: 2, Value: "green"},
},
{
"vim mode",
&Select{
Message: "Choose a color:",
Options: []string{"red", "blue", "green"},
VimMode: true,
},
func(c expectConsole) {
c.ExpectString("Choose a color:")
// Select blue.
c.SendLine("j")
c.ExpectEOF()
},
core.OptionAnswer{Index: 1, Value: "blue"},
},
{
"filter",
&Select{
Message: "Choose a color:",
Options: []string{"red", "blue", "green"},
},
func(c expectConsole) {
c.ExpectString("Choose a color:")
// Filter down to red and green.
c.Send("re")
// Select green.
c.SendLine(string(terminal.KeyArrowDown))
c.ExpectEOF()
},
core.OptionAnswer{Index: 2, Value: "green"},
},
{
"filter is case-insensitive",
&Select{
Message: "Choose a color:",
Options: []string{"red", "blue", "green"},
},
func(c expectConsole) {
c.ExpectString("Choose a color:")
// Filter down to red and green.
c.Send("RE")
// Select green.
c.SendLine(string(terminal.KeyArrowDown))
c.ExpectEOF()
},
core.OptionAnswer{Index: 2, Value: "green"},
},
{
"Can select the first result in a filtered list if there is a default",
&Select{
Message: "Choose a color:",
Options: []string{"red", "blue", "green"},
Default: "blue",
},
func(c expectConsole) {
c.ExpectString("Choose a color:")
// Make sure only red is showing
c.SendLine("red")
c.ExpectEOF()
},
core.OptionAnswer{Index: 0, Value: "red"},
},
{
"custom filter",
&Select{
Message: "Choose a color:",
Options: []string{"red", "blue", "green"},
Filter: func(filter string, optValue string, optIndex int) (filtered bool) {
return len(optValue) >= 5
},
},
func(c expectConsole) {
c.ExpectString("Choose a color:")
// Filter down to only green since custom filter only keeps options that are longer than 5 runes
c.SendLine("re")
c.ExpectEOF()
},
core.OptionAnswer{Index: 2, Value: "green"},
},
{
"answers filtered out",
&Select{
Message: "Choose a color:",
Options: []string{"red", "blue", "green"},
},
func(c expectConsole) {
c.ExpectString("Choose a color:")
// filter away everything
c.SendLine("z")
// send enter (should get ignored since there are no answers)
c.SendLine(string(terminal.KeyEnter))
// remove the filter we just applied
c.SendLine(string(terminal.KeyBackspace))
// press enter
c.SendLine(string(terminal.KeyEnter))
},
core.OptionAnswer{Index: 0, Value: "red"},
},
{
"delete filter word",
&Select{
Message: "Choose a color:",
Options: []string{"red", "blue", "black"},
},
func(c expectConsole) {
c.ExpectString("Choose a color:")
// Filter down to blue.
c.Send("blu")
// Filter down to blue and black.
c.Send(string(terminal.KeyDelete))
// Select black.
c.SendLine(string(terminal.KeyArrowDown))
c.ExpectEOF()
},
core.OptionAnswer{Index: 2, Value: "black"},
},
{
"delete filter word in rune",
&Select{
Message: "今天中午吃什么?",
Options: []string{"青椒牛肉丝", "小炒肉", "小煎鸡"},
},
func(c expectConsole) {
c.ExpectString("今天中午吃什么?")
// Filter down to 小炒肉.
c.Send("小炒")
// Filter down to 小炒肉 and 小煎鸡.
c.Send(string(terminal.KeyBackspace))
// Select 小煎鸡.
c.SendLine(string(terminal.KeyArrowDown))
c.ExpectEOF()
},
core.OptionAnswer{Index: 2, Value: "小煎鸡"},
},
}
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)
})
}
}