-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiline_test.go
175 lines (152 loc) · 4.11 KB
/
multiline_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
package surveyexpect_test
import (
"testing"
"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 TestMultilinePrompt(t *testing.T) {
t.Parallel()
testCases := []struct {
scenario string
expectSurvey surveyexpect.Expector
help string
showHelp bool
options []survey.AskOpt
expectedAnswer string
expectedError string
}{
{
scenario: "no answer sends an empty line",
expectSurvey: surveyexpect.Expect(func(s *surveyexpect.Survey) {
s.ExpectMultiline("Enter your comment")
}),
},
{
scenario: "empty answer",
expectSurvey: surveyexpect.Expect(func(s *surveyexpect.Survey) {
s.ExpectMultiline("Enter your comment").
Answer("")
}),
},
{
scenario: "input is interrupted",
expectSurvey: surveyexpect.Expect(func(s *surveyexpect.Survey) {
s.ExpectMultiline("Enter your comment").
Times(10). // Times will be discarded due to the interruption.
Interrupt()
}),
expectedError: "interrupt",
},
{
scenario: "input is invalid",
expectSurvey: surveyexpect.Expect(func(s *surveyexpect.Survey) {
s.ExpectMultiline("Enter your comment").
Answer("\033X").
Interrupted()
}),
expectedError: `unexpected escape sequence from terminal: ['\x1b' 'X']`,
},
{
scenario: "answer is required",
expectSurvey: surveyexpect.Expect(func(s *surveyexpect.Survey) {
// Be asked for 5 times without giving up the password.
s.ExpectMultiline("Enter your comment").
Times(5)
// Finally, input the password.
s.ExpectMultiline("Enter your comment").
Answer("this is a multiline\ncomment\n\nend")
}),
options: []survey.AskOpt{
survey.WithValidator(survey.Required),
},
expectedAnswer: "this is a multiline\ncomment\n\nend",
},
}
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.Multiline{Message: "Enter your comment"}
// Start the survey.
s.Start(func(stdio terminal.Stdio) {
tc.options = append(tc.options, options.WithStdio(stdio))
var answer string
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 TestMultilinePrompt_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.ExpectMultiline("Enter your message:").Interrupt()
}),
expectedError: "interrupt",
},
{
scenario: "invalid input",
expectSurvey: surveyexpect.Expect(func(s *surveyexpect.Survey) {
s.ExpectMultiline("Enter your message:").
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: "message",
Prompt: &survey.Multiline{Message: "Enter your message:"},
},
{
Name: "status",
Prompt: &survey.Multiline{Message: "Enter your status:"},
},
}
expectedResult := map[string]interface{}{
"message": "old message",
"status": "old status",
}
// Start the survey.
s.Start(func(stdio terminal.Stdio) {
result := map[string]interface{}{
"message": "old message",
"status": "old status",
}
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())
})
}
}