-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfirm_test.go
285 lines (251 loc) · 7.12 KB
/
confirm_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
package surveyexpect_test
import (
"testing"
"time"
"github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/terminal"
"github.com/stretchr/testify/assert"
"go.nhat.io/surveyexpect"
"go.nhat.io/surveyexpect/options"
)
func TestConfirm(t *testing.T) {
t.Parallel()
testCases := []struct {
scenario string
expectSurvey surveyexpect.Expector
defaultValue bool
help string
showHelp bool
options []survey.AskOpt
expectedAnswer bool
expectedError string
}{
{
scenario: "no answer sends an empty answer (default: false)",
expectSurvey: surveyexpect.Expect(func(s *surveyexpect.Survey) {
s.ExpectConfirm("ConfirmPrompt?")
}),
},
{
scenario: "empty answer (default: false)",
expectSurvey: surveyexpect.Expect(func(s *surveyexpect.Survey) {
s.ExpectConfirm("ConfirmPrompt?").
Answer("")
}),
},
{
scenario: "no answer sends an empty answer (default: true)",
expectSurvey: surveyexpect.Expect(func(s *surveyexpect.Survey) {
s.ExpectConfirm("ConfirmPrompt?")
}),
defaultValue: true,
expectedAnswer: true,
},
{
scenario: "empty answer (default: true)",
expectSurvey: surveyexpect.Expect(func(s *surveyexpect.Survey) {
s.ExpectConfirm("ConfirmPrompt?").
Answer("")
}),
defaultValue: true,
expectedAnswer: true,
},
{
scenario: "confirm without help (yes)",
expectSurvey: surveyexpect.Expect(func(s *surveyexpect.Survey) {
s.ExpectConfirm("ConfirmPrompt?").Yes()
}),
defaultValue: false,
expectedAnswer: true,
},
{
scenario: "confirm without help (no)",
expectSurvey: surveyexpect.Expect(func(s *surveyexpect.Survey) {
s.ExpectConfirm("ConfirmPrompt?").No()
}),
defaultValue: true,
expectedAnswer: false,
},
{
scenario: "confirm with visible help and do not ask for it",
expectSurvey: surveyexpect.Expect(func(s *surveyexpect.Survey) {
s.ExpectConfirm("ConfirmPrompt? [? for help]").Yes()
}),
help: "This is a helpful help",
showHelp: true,
expectedAnswer: true,
},
{
scenario: "confirm with visible help and ask for it",
expectSurvey: surveyexpect.Expect(func(s *surveyexpect.Survey) {
s.ExpectConfirm("ConfirmPrompt? [? for help]").
ShowHelp("This is a helpful help")
s.ExpectConfirm("ConfirmPrompt?").Yes()
}),
help: "This is a helpful help",
showHelp: true,
expectedAnswer: true,
},
{
scenario: "confirm with invisible help and do not ask for it",
expectSurvey: surveyexpect.Expect(func(s *surveyexpect.Survey) {
s.ExpectConfirm("ConfirmPrompt?").Yes()
}),
help: "This is a helpful help",
expectedAnswer: true,
},
{
scenario: "confirm with invisible help and ask for it",
expectSurvey: surveyexpect.Expect(func(s *surveyexpect.Survey) {
s.ExpectConfirm("ConfirmPrompt?").
ShowHelp("This is a helpful help")
s.ExpectConfirm("ConfirmPrompt?").Yes()
}),
help: "This is a helpful help",
expectedAnswer: true,
},
{
scenario: "input is interrupted",
expectSurvey: surveyexpect.Expect(func(s *surveyexpect.Survey) {
s.ExpectConfirm("ConfirmPrompt?").
Interrupt()
}),
expectedError: "interrupt",
},
{
scenario: "input contains invalid character",
expectSurvey: surveyexpect.Expect(func(s *surveyexpect.Survey) {
s.ExpectConfirm("ConfirmPrompt?").
Answer("\033X").
Interrupted()
}),
expectedError: `unexpected escape sequence from terminal: ['\x1b' 'X']`,
},
{
scenario: "input is invalid",
expectSurvey: surveyexpect.Expect(func(s *surveyexpect.Survey) {
s.ExpectConfirm("ConfirmPrompt?").
Answer("not a yes or no")
s.ExpectConfirm("ConfirmPrompt?").Yes()
}),
expectedAnswer: true,
},
{
scenario: "required does not affect confirm",
expectSurvey: surveyexpect.Expect(func(s *surveyexpect.Survey) {
s.ExpectConfirm("ConfirmPrompt?")
}),
options: []survey.AskOpt{
survey.WithValidator(survey.Required),
},
expectedAnswer: false,
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.scenario, func(t *testing.T) {
t.Parallel()
// Prepare the survey.
s := tc.expectSurvey(t)
p := &survey.ConfirmTemplateData{
Confirm: survey.Confirm{Message: "ConfirmPrompt?", Help: tc.help, Default: tc.defaultValue},
ShowHelp: tc.showHelp,
}
// Start the survey.
s.Start(func(stdio terminal.Stdio) {
tc.options = append(tc.options, options.WithStdio(stdio))
var answer bool
err := survey.AskOne(p, &answer, tc.options...)
assert.Equal(t, tc.expectedAnswer, answer)
if tc.expectedError == "" {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, tc.expectedError)
}
})
})
}
}
func TestConfirm_NoHelpButStillExpect(t *testing.T) {
t.Parallel()
testingT := T()
s := surveyexpect.Expect(func(s *surveyexpect.Survey) {
s.WithTimeout(50 * time.Millisecond)
s.ExpectConfirm("ConfirmPrompt?").
ShowHelp("It is your secret")
})(testingT)
expectedError := "there are remaining expectations that were not met:\n\nExpect : Confirm Prompt\nMessage: \"ConfirmPrompt?\"\nAnswer : ?\n"
p := &survey.Confirm{Message: "ConfirmPrompt?"}
// Start the survey.
s.Start(func(stdio terminal.Stdio) {
var answer bool
err := survey.AskOne(p, &answer, options.WithStdio(stdio))
assert.False(t, answer)
assert.NoError(t, err)
})
assert.EqualError(t, s.ExpectationsWereMet(), expectedError)
t.Log(testingT.LogString())
}
func TestConfirm_SurveyInterrupted(t *testing.T) {
t.Parallel()
testCases := []struct {
scenario string
expectSurvey surveyexpect.Expector
expectedError string
}{
{
scenario: "interrupt",
expectSurvey: surveyexpect.Expect(func(s *surveyexpect.Survey) {
s.ExpectConfirm("Do you want to save your password?").Interrupt()
}),
expectedError: "interrupt",
},
{
scenario: "invalid input",
expectSurvey: surveyexpect.Expect(func(s *surveyexpect.Survey) {
s.ExpectConfirm("Do you want to save your password?").
Answer("\033X").
Interrupted()
}),
expectedError: `unexpected escape sequence from terminal: ['\x1b' 'X']`,
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.scenario, func(t *testing.T) {
t.Parallel()
testingT := T()
s := tc.expectSurvey(testingT)
questions := []*survey.Question{
{
Name: "username",
Prompt: &survey.Confirm{Message: "Do you want to save your password?"},
},
{
Name: "password",
Prompt: &survey.Password{Message: "Enter your password:"},
},
}
expectedResult := map[string]interface{}{
"confirm": true,
"password": "old password",
}
// Start the survey.
s.Start(func(stdio terminal.Stdio) {
result := map[string]interface{}{
"confirm": true,
"password": "old password",
}
err := survey.Ask(questions, &result, options.WithStdio(stdio))
assert.Equal(t, expectedResult, result)
if tc.expectedError == "" {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, tc.expectedError)
}
})
assert.Nil(t, s.ExpectationsWereMet())
t.Log(testingT.LogString())
})
}
}