forked from AlecAivazis/survey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slider_test.go
264 lines (243 loc) · 6.32 KB
/
slider_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
package survey
import (
"bytes"
"fmt"
"io"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/Iilun/survey/v2/core"
"github.com/Iilun/survey/v2/terminal"
)
func init() {
// disable color output for all prompts to simplify testing
core.DisableColor = true
}
func TestSliderRender(t *testing.T) {
prompt := Slider{
Message: "Pick your number:",
Max: 50,
Default: 40,
}
helpfulPrompt := prompt
helpfulPrompt.Help = "This is helpful"
tests := []struct {
title string
prompt Slider
promptOption AskOpt
data SliderTemplateData
expected string
}{
{
"Test Slider question output",
prompt,
nil,
SliderTemplateData{SelectedValue: 3},
strings.Join(
[]string{
fmt.Sprintf("%s Pick your number: [Use side arrows to alter value by 1, vertical arrows to alter value by 10]", defaultIcons().Question.Text),
fmt.Sprintf(" %s%s%s", defaultIcons().SliderFiller.Text, defaultIcons().SliderCursor.Text, strings.Repeat(defaultIcons().SliderFiller.Text, 23)),
},
"\n",
),
},
{
"Test Slider answer output",
prompt,
nil,
SliderTemplateData{SelectedValue: 15, ShowAnswer: true},
fmt.Sprintf("%s Pick your number: 15\n", defaultIcons().Question.Text),
},
{
"Test Slider question output with help hidden",
helpfulPrompt,
nil,
SliderTemplateData{SelectedValue: 3},
strings.Join(
[]string{
fmt.Sprintf("%s Pick your number: [Use side arrows to alter value by 1, vertical arrows to alter value by 10, %s for more help]", defaultIcons().Question.Text, string(defaultPromptConfig().HelpInput)),
fmt.Sprintf(" %s%s%s", defaultIcons().SliderFiller.Text, defaultIcons().SliderCursor.Text, strings.Repeat(defaultIcons().SliderFiller.Text, 23)),
},
"\n",
),
},
{
"Test Slider question output with help shown",
helpfulPrompt,
nil,
SliderTemplateData{SelectedValue: 3, ShowHelp: true},
strings.Join(
[]string{
fmt.Sprintf("%s This is helpful", defaultIcons().Help.Text),
fmt.Sprintf("%s Pick your number: [Use side arrows to alter value by 1, vertical arrows to alter value by 10]", defaultIcons().Question.Text),
fmt.Sprintf(" %s%s%s", defaultIcons().SliderFiller.Text, defaultIcons().SliderCursor.Text, strings.Repeat(defaultIcons().SliderFiller.Text, 23)),
},
"\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.Slider = test.prompt
options := defaultAskOptions()
if test.promptOption != nil {
err = test.promptOption(options)
assert.NoError(t, err)
}
// set the icon set
test.data.Config = &options.PromptConfig
// Compute the state
test.prompt.MaxSize = 25
test.prompt.selectedValue = test.data.SelectedValue
test.data.ChangeInterval = 10
test.data.SliderContent = test.prompt.computeSliderContent(test.data.Config)
err = test.prompt.Render(
SliderQuestionTemplate,
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 TestSliderPrompt(t *testing.T) {
//Overflow, min max custom, up arrows, help
tests := []PromptTest{
{
"basic interaction: 1",
&Slider{
Message: "Choose a number:",
Max: 25,
},
nil,
func(c expectConsole) {
c.ExpectString("*------------------------- 0")
// Increase the value by one
c.Send(string(terminal.KeyArrowRight))
c.ExpectString("-*------------------------ 1")
// Validate
c.SendLine("")
c.ExpectEOF()
},
1,
},
{
"basic interaction: default",
&Slider{
Message: "Choose a number:",
Default: 20,
Max: 25,
},
nil,
func(c expectConsole) {
c.ExpectString("--------------------*----- 20")
// Decrease the value by one
c.Send(string(terminal.KeyArrowLeft))
c.ExpectString("-------------------*------ 19")
// Validate
c.SendLine("")
c.ExpectEOF()
},
19,
},
{
"basic interaction: custom min and max",
&Slider{
Message: "Choose a number:",
Min: 20,
Max: 70,
},
nil,
func(c expectConsole) {
c.ExpectString("*------------------------- 20")
// Decrease the value by one - does nothing
c.Send(string(terminal.KeyArrowLeft))
c.ExpectString("*------------------------- 20")
// Validate
c.SendLine("")
c.ExpectEOF()
},
20,
},
{
"basic interaction: custom min and max",
&Slider{
Message: "Choose a number:",
Min: -70,
Max: -20,
},
nil,
func(c expectConsole) {
c.ExpectString("*------------------------- -70")
// Decrease the value by 10 - does nothing
c.Send(string(terminal.KeyArrowDown))
c.ExpectString("*------------------------- -70")
// Validate
c.SendLine("")
c.ExpectEOF()
},
-70,
},
{
"basic interaction: overflow max",
&Slider{
Message: "Choose a number:",
Max: 10,
},
nil,
func(c expectConsole) {
c.ExpectString("*---------- 0")
// Increase the value by 10
c.Send(string(terminal.KeyArrowUp))
c.ExpectString("----------* 10")
// Increase the value by 10 - does nothing
c.Send(string(terminal.KeyArrowUp))
c.ExpectString("----------* 10")
// Increase the value by 1 - does nothing
c.Send(string(terminal.KeyArrowRight))
c.ExpectString("----------* 10")
// Validate
c.SendLine("")
c.ExpectEOF()
},
10,
},
}
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)
})
}
}
func TestSliderError(t *testing.T) {
// Error should be sent on:
// - max < min
// - max == min
// - default outside min max interval
invalidSliders := []Slider{
{Max: 10, Min: 50},
{Max: 10, Min: 10},
{Max: 15, Min: 10, Default: -40},
}
for _, slider := range invalidSliders {
var output int
err := Ask([]*Question{{Prompt: &slider}}, &output)
// if we didn't get an error
if err == nil {
// the test failed
t.Errorf("Did not encounter error when asking whith invalid slider: %s", fmt.Sprint(slider))
}
}
}