-
Notifications
You must be signed in to change notification settings - Fork 0
/
huhtest_test.go
291 lines (231 loc) · 7.85 KB
/
huhtest_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
package huhtest
import (
"bufio"
"io"
"strings"
"testing"
"time"
testingi "github.com/mitchellh/go-testing-interface"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Functions
// simulateCLI is a test helper that serves as a stand-in for an actual huh form. Questions are sent
// to the io.PipeWriter in order and answers are captured line-by-line from the io.PipeReader.
//
// Returns the captured answers
func simulateCLI(t *testing.T, questions []string, stdout *io.PipeWriter, stdin *io.PipeReader) []string {
t.Helper()
actualAnswers := make([]string, len(questions))
reader := bufio.NewReader(stdin)
for index, question := range questions {
t.Logf("Posing question: %s", question)
_, err := stdout.Write([]byte(question + defaultSubmit))
require.NoError(t, err)
line, err := reader.ReadString('\r')
require.NoError(t, err)
// Strip trailing submit character
line = strings.TrimSuffix(line, "\r")
// Strip leading submit character, this works in the integration tests so I assume it's valid
line = strings.TrimPrefix(line, "\n")
actualAnswers[index] = readableReplacer.Replace(line)
}
return actualAnswers
}
// Tests
const defaultTimeout = 1 * time.Second
// Tests both the public API and the unexported methods that might one day be made public
func TestResponder_Start_ReturnsExpectedAnswers(t *testing.T) {
t.Parallel()
tests := map[string]struct {
responder *Responder
questions []string
expectedAnswers []string
}{
"one simple question": {
responder: NewResponder().
AddResponse("alright?", "Yes, for sure!"),
questions: []string{"You doing alright?"},
expectedAnswers: []string{"Yes, for sure!"},
},
"three simple questions": {
responder: NewResponder().
AddResponse("alright?", "Yes, for sure!").
AddResponse("dinner?", "Let's find out!").
AddResponse("fair?", "Don't think so"),
questions: []string{"You doing alright?", "What's for dinner?", "Are you going to the fair?"},
expectedAnswers: []string{"Yes, for sure!", "Let's find out!", "Don't think so"},
},
"one question that gets asked 3 times with different answers": {
responder: NewResponder().
addResponses("alright?", "Yes, for sure!", "Positive!", "Yes sir!"),
questions: []string{"You doing alright?", "You doing alright?", "You doing alright?"},
expectedAnswers: []string{"Yes, for sure!", "Positive!", "Yes sir!"},
},
"one question that gets asked 3 times gets repeated answers": {
responder: NewResponder().
addResponses("alright?", "Yes, for sure!"),
questions: []string{"You doing alright?", "You doing alright?", "You doing alright?"},
expectedAnswers: []string{"Yes, for sure!", "Yes, for sure!", "Yes, for sure!"},
},
"one affirmative confirm question": {
responder: NewResponder().
addConfirms("alright?", ConfirmAffirm),
questions: []string{"You doing alright?"},
expectedAnswers: []string{"<right> "},
},
"one negative confirm question": {
responder: NewResponder().
addConfirms("alright?", ConfirmNegative),
questions: []string{"You doing alright?"},
expectedAnswers: []string{" "},
},
"multiple confirm questions": {
responder: NewResponder().
addConfirms("right?", ConfirmAffirm, ConfirmNegative, ConfirmNegative),
questions: []string{"You doing alright?", "is it alright?", "right?"},
expectedAnswers: []string{"<right> ", " ", " "},
},
"one select question": {
responder: NewResponder().
AddSelect("how?", 5),
questions: []string{"how?"},
expectedAnswers: []string{"<down><down><down><down><down>"},
},
"multiple select questions": {
responder: NewResponder().
addSelects("how?", 0, 1, 3),
questions: []string{"how?", "how?", "how?"},
expectedAnswers: []string{
"",
"<down>",
"<down><down><down>",
},
},
"one multiselect question": {
responder: NewResponder().
AddMultiSelect("how?", []int{2, 3}),
questions: []string{"how?"},
expectedAnswers: []string{"<down><down> <down> "},
},
"multiple multiselect questions": {
responder: NewResponder().
addMultiSelects("how?", []int{2, 3}, []int{3, 4}),
questions: []string{"how?", "how?"},
expectedAnswers: []string{
"<down><down> <down> ",
"<down><down><down> <down> ",
},
},
"one exact match": {
responder: NewResponder().
AddResponse("You doing alright?", "Splendid").
MatchExact().
AddResponse("alright?", "Yes"),
questions: []string{"You doing alright?"},
expectedAnswers: []string{"Splendid"},
},
"one regexp match": {
responder: NewResponder().
AddResponse(`Y[ou]{2} doin. alr.ght\?a?`, "Splendid").
MatchRegexp().
AddResponse("alright?", "Yes, for sure!").
MatchExact(),
questions: []string{"You doing alright?"},
expectedAnswers: []string{"Splendid"},
},
}
for name, testData := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()
// Act
stdin, stdout, closer := testData.responder.Start(t, defaultTimeout)
// Assert
defer closer()
actualAnswers := simulateCLI(t, testData.questions, stdout, stdin)
assert.Equal(t, testData.expectedAnswers, actualAnswers)
})
}
}
func TestResponder_Start_FailsTestIfCalledNotOnceOnOnce(t *testing.T) {
t.Parallel()
// Arrange
responder := NewResponder().
AddResponse("right?", "left!").
RespondOnce()
questions := []string{"right?", "right?"}
expectedAnswers := []string{"left!", "left!"}
dummyT := new(testingi.RuntimeT)
// Act
stdin, stdout, closer := responder.Start(dummyT, defaultTimeout)
// Assert
defer closer()
actualAnswers := simulateCLI(t, questions, stdout, stdin)
// It should still return the answers
assert.Equal(t, expectedAnswers, actualAnswers)
assert.True(t, dummyT.Failed(), "Test should have failed")
}
func TestResponder_Start_FailsTestIfCalledMoreThanTimes(t *testing.T) {
t.Parallel()
// Arrange
responder := NewResponder().
AddResponse("right?", "left!").
RespondTimes(3)
questions := []string{"right?", "right?", "right?", "right?", "right?"}
expectedAnswers := []string{"left!", "left!", "left!", "left!", "left!"}
dummyT := new(testingi.RuntimeT)
// Act
stdin, stdout, closer := responder.Start(dummyT, defaultTimeout)
// Assert
defer closer()
actualAnswers := simulateCLI(t, questions, stdout, stdin)
// It should still return the answers
assert.Equal(t, expectedAnswers, actualAnswers)
assert.True(t, dummyT.Failed(), "Test should have failed")
}
func TestResponder_Start_TimeoutClosesPipesAndFailsTest(t *testing.T) {
t.Parallel()
// Arrange
responder := NewResponder()
dummyT := new(testingi.RuntimeT)
// Act
formInput, formOutput, cancel := responder.Start(dummyT, 0)
// Assert
defer cancel()
_, readErr := formInput.Read([]byte{})
require.ErrorIs(t, readErr, io.ErrClosedPipe)
_, writeErr := formOutput.Write([]byte{})
require.ErrorIs(t, writeErr, io.ErrClosedPipe)
assert.True(t, dummyT.Failed(), "Test should have failed")
}
func TestResponder_Start_CancelFunctionClosesPipes(t *testing.T) {
t.Parallel()
// Arrange
responder := NewResponder()
// Act
formInput, formOutput, cancel := responder.Start(new(testing.T), defaultTimeout)
// Assert
cancel()
_, readErr := formInput.Read([]byte{})
require.ErrorIs(t, readErr, io.ErrClosedPipe)
_, writeErr := formOutput.Write([]byte{})
require.ErrorIs(t, writeErr, io.ErrClosedPipe)
}
func TestNewResponderWith_SetsExpectedRespones(t *testing.T) {
t.Parallel()
// Arrange
responses := map[string][]string{
"a": {"b", "c"},
"1": {"2", "4"},
}
questions := []string{"a", "1", "a", "1"}
expectedAnswers := []string{"b", "2", "c", "4"}
// Act
responder := NewResponderWith(responses)
// Assert
stdin, stdout, closer := responder.Start(t, defaultTimeout)
// Assert
defer closer()
actualAnswers := simulateCLI(t, questions, stdout, stdin)
assert.Equal(t, expectedAnswers, actualAnswers)
}