forked from AlecAivazis/survey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiselect_test.go
241 lines (228 loc) · 6.05 KB
/
multiselect_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
package survey
import (
"bytes"
"io"
"os"
"testing"
expect "github.com/Netflix/go-expect"
"github.com/stretchr/testify/assert"
"gopkg.in/AlecAivazis/survey.v1/core"
"gopkg.in/AlecAivazis/survey.v1/terminal"
)
func init() {
// disable color output for all prompts to simplify testing
core.DisableColor = true
}
func TestMultiSelectRender(t *testing.T) {
prompt := MultiSelect{
Message: "Pick your words:",
Options: []string{"foo", "bar", "baz", "buz"},
Default: []string{"bar", "buz"},
}
helpfulPrompt := prompt
helpfulPrompt.Help = "This is helpful"
tests := []struct {
title string
prompt MultiSelect
data MultiSelectTemplateData
expected string
}{
{
"Test MultiSelect question output",
prompt,
MultiSelectTemplateData{
SelectedIndex: 2,
PageEntries: prompt.Options,
Checked: map[string]bool{"bar": true, "buz": true},
},
`? Pick your words: [Use arrows to move, type to filter]
◯ foo
◉ bar
❯ ◯ baz
◉ buz
`,
},
{
"Test MultiSelect answer output",
prompt,
MultiSelectTemplateData{
Answer: "foo, buz",
ShowAnswer: true,
},
"? Pick your words: foo, buz\n",
},
{
"Test MultiSelect question output with help hidden",
helpfulPrompt,
MultiSelectTemplateData{
SelectedIndex: 2,
PageEntries: prompt.Options,
Checked: map[string]bool{"bar": true, "buz": true},
},
`? Pick your words: [Use arrows to move, type to filter, ? for more help]
◯ foo
◉ bar
❯ ◯ baz
◉ buz
`,
},
{
"Test MultiSelect question output with help shown",
helpfulPrompt,
MultiSelectTemplateData{
SelectedIndex: 2,
PageEntries: prompt.Options,
Checked: map[string]bool{"bar": true, "buz": true},
ShowHelp: true,
},
`ⓘ This is helpful
? Pick your words: [Use arrows to move, type to filter]
◯ foo
◉ bar
❯ ◯ baz
◉ buz
`,
},
}
for _, test := range tests {
r, w, err := os.Pipe()
assert.Nil(t, err, test.title)
test.prompt.WithStdio(terminal.Stdio{Out: w})
test.data.MultiSelect = test.prompt
err = test.prompt.Render(
MultiSelectQuestionTemplate,
test.data,
)
assert.Nil(t, err, test.title)
w.Close()
var buf bytes.Buffer
io.Copy(&buf, r)
assert.Contains(t, buf.String(), test.expected, test.title)
}
}
func TestMultiSelectPrompt(t *testing.T) {
tests := []PromptTest{
{
"Test MultiSelect prompt interaction",
&MultiSelect{
Message: "What days do you prefer:",
Options: []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"},
},
func(c *expect.Console) {
c.ExpectString("What days do you prefer: [Use arrows to move, type to filter]")
// Select Monday.
c.Send(string(terminal.KeyArrowDown))
c.SendLine(" ")
c.ExpectEOF()
},
[]string{"Monday"},
},
{
"Test MultiSelect prompt interaction with default",
&MultiSelect{
Message: "What days do you prefer:",
Options: []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"},
Default: []string{"Tuesday", "Thursday"},
},
func(c *expect.Console) {
c.ExpectString("What days do you prefer: [Use arrows to move, type to filter]")
c.SendLine("")
c.ExpectEOF()
},
[]string{"Tuesday", "Thursday"},
},
{
"Test MultiSelect prompt interaction overriding default",
&MultiSelect{
Message: "What days do you prefer:",
Options: []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"},
Default: []string{"Tuesday", "Thursday"},
},
func(c *expect.Console) {
c.ExpectString("What days do you prefer: [Use arrows to move, type to filter]")
// Deselect Tuesday.
c.Send(string(terminal.KeyArrowDown))
c.Send(string(terminal.KeyArrowDown))
c.SendLine(" ")
c.ExpectEOF()
},
[]string{"Thursday"},
},
{
"Test MultiSelect prompt interaction and prompt for help",
&MultiSelect{
Message: "What days do you prefer:",
Options: []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"},
Help: "Saturday is best",
},
func(c *expect.Console) {
c.ExpectString("What days do you prefer: [Use arrows to move, type to filter, ? for more help]")
c.Send("?")
c.ExpectString("Saturday is best")
// Select Saturday
c.Send(string(terminal.KeyArrowUp))
c.SendLine(" ")
c.ExpectEOF()
},
[]string{"Saturday"},
},
{
"Test MultiSelect prompt interaction with page size",
&MultiSelect{
Message: "What days do you prefer:",
Options: []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"},
PageSize: 1,
},
func(c *expect.Console) {
c.ExpectString("What days do you prefer: [Use arrows to move, type to filter]")
// Select Monday.
c.Send(string(terminal.KeyArrowDown))
c.SendLine(" ")
c.ExpectEOF()
},
[]string{"Monday"},
},
{
"Test MultiSelect prompt interaction with vim mode",
&MultiSelect{
Message: "What days do you prefer:",
Options: []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"},
VimMode: true,
},
func(c *expect.Console) {
c.ExpectString("What days do you prefer: [Use arrows to move, type to filter]")
// Select Tuesday.
c.Send("jj ")
// Select Thursday.
c.Send("jj ")
// Select Saturday.
c.Send("jj ")
c.SendLine("")
c.ExpectEOF()
},
[]string{"Tuesday", "Thursday", "Saturday"},
},
{
"Test MultiSelect prompt interaction with filter",
&MultiSelect{
Message: "What days do you prefer:",
Options: []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"},
},
func(c *expect.Console) {
c.ExpectString("What days do you prefer: [Use arrows to move, type to filter]")
// Filter down to Tuesday.
c.Send("Tues")
// Select Tuesday.
c.Send(" ")
c.SendLine("")
c.ExpectEOF()
},
[]string{"Tuesday"},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
RunPromptTest(t, test)
})
}
}