-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration_test.go
168 lines (152 loc) · 4.08 KB
/
integration_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
package huhtest
import (
"testing"
"github.com/charmbracelet/huh"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestHuhTest_RespondsCorrectlyToQuestions(t *testing.T) {
t.Parallel()
type answers struct {
input string
groupInputA string
groupInputB string
groupInputC string
confirmTrue bool
confirmFalse bool
singleSelect1 string
singleSelect2 string
multiSelect []string
consecutiveQuestion1 string
consecutiveQuestion2 string
}
var actual answers
myForm := huh.NewForm(
// Simple question
huh.NewGroup(
huh.NewInput().
Title("How Are You Feeling?").
Value(&actual.input),
),
// Questions in a group
huh.NewGroup(
huh.NewInput().
Title("Group Question A?").
Value(&actual.groupInputA),
huh.NewInput().
Title("Group Question B?").
Value(&actual.groupInputB),
huh.NewInput().
Title("Group Question C?").
Value(&actual.groupInputC),
),
// Confirm question with a true answer
huh.NewGroup(
huh.NewConfirm().
Title("Would you like a drink?").
Value(&actual.confirmTrue),
),
// Confirm question with a false answer
huh.NewGroup(
huh.NewConfirm().
Title("Would you like a meal?").
Value(&actual.confirmFalse),
),
// Select with first option
huh.NewGroup(
huh.NewSelect[string]().
Title("Make a choice").
Options(
huh.NewOption("a", "a"),
huh.NewOption("b", "b"),
huh.NewOption("c", "c"),
huh.NewOption("d", "d"),
huh.NewOption("e", "e"),
huh.NewOption("f", "f"),
).
Value(&actual.singleSelect1),
),
// Select with third option
huh.NewGroup(
huh.NewSelect[string]().
Title("Make a second choice").
Options(
huh.NewOption("a", "a"),
huh.NewOption("b", "b"),
huh.NewOption("c", "c"),
huh.NewOption("d", "d"),
huh.NewOption("e", "e"),
huh.NewOption("f", "f"),
).
Value(&actual.singleSelect2),
),
// Multi select
huh.NewGroup(
huh.NewMultiSelect[string]().
Title("Please pick all options that apply").
Options(
huh.NewOption("a", "a"),
huh.NewOption("b", "b"),
huh.NewOption("c", "c"),
huh.NewOption("d", "d"),
huh.NewOption("e", "e"),
huh.NewOption("f", "f"),
).
Value(&actual.multiSelect),
),
// Simple question that gets asked twice (1)
huh.NewGroup(
huh.NewInput().
Title("Are You OK? (gonna ask you twice)").
Value(&actual.consecutiveQuestion1),
),
// Simple question that gets asked twice (2)
huh.NewGroup(
huh.NewInput().
Title("Are You OK? (gonna ask you twice)").
Value(&actual.consecutiveQuestion2),
),
)
formInput, formOutput, closeResponder := NewResponder().
// Simple question
AddResponse("How Are You Feeling?", "Amazing Thanks!").
// Questions in a group
AddResponse("Group Question A?", "Foo").
AddResponse("Group Question B?", "Bar").
AddResponse("Group Question C?", "Baz").
// Confirm question with a true answer
AddConfirm("Would you like a drink?", ConfirmAffirm).
// Confirm question with a false answer
AddConfirm("meal?", ConfirmNegative).
// Select with first option
AddSelect("Make a choice", 0).
// Select with third option
AddSelect("Make a second choice", 2).
// Multi select
AddMultiSelect("Please pick all options that apply", []int{2, 3, 5}).
// Simple question that gets asked twice (2)
AddResponse("Are You OK? (gonna ask you twice)", "yes").
AddResponse("Are You OK? (gonna ask you twice)", "yes for sure").
// Better for debugging
Debug().
Start(t, defaultTimeout)
defer closeResponder()
// Act
err := myForm.WithInput(formInput).WithOutput(formOutput).Run()
// Assert
require.NoError(t, err)
expected := answers{
input: "Amazing Thanks!",
groupInputA: "Foo",
groupInputB: "Bar",
groupInputC: "Baz",
confirmTrue: true,
confirmFalse: false,
singleSelect1: "a",
singleSelect2: "c",
multiSelect: []string{"c", "d", "f"},
consecutiveQuestion1: "yes",
consecutiveQuestion2: "yes for sure",
}
assert.Equal(t, expected, actual)
}