This repository has been archived by the owner on Apr 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 357
/
confirm_test.go
154 lines (141 loc) · 3.71 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
package survey
import (
"bytes"
"fmt"
"io"
"os"
"testing"
"github.com/AlecAivazis/survey/v2/core"
"github.com/AlecAivazis/survey/v2/terminal"
"github.com/stretchr/testify/assert"
)
func init() {
// disable color output for all prompts to simplify testing
core.DisableColor = true
}
func TestConfirmRender(t *testing.T) {
tests := []struct {
title string
prompt Confirm
data ConfirmTemplateData
expected string
}{
{
"Test Confirm question output with default true",
Confirm{Message: "Is pizza your favorite food?", Default: true},
ConfirmTemplateData{},
fmt.Sprintf("%s Is pizza your favorite food? (Y/n) ", defaultIcons().Question.Text),
},
{
"Test Confirm question output with default false",
Confirm{Message: "Is pizza your favorite food?", Default: false},
ConfirmTemplateData{},
fmt.Sprintf("%s Is pizza your favorite food? (y/N) ", defaultIcons().Question.Text),
},
{
"Test Confirm answer output",
Confirm{Message: "Is pizza your favorite food?"},
ConfirmTemplateData{Answer: "Yes"},
fmt.Sprintf("%s Is pizza your favorite food? Yes\n", defaultIcons().Question.Text),
},
{
"Test Confirm with help but help message is hidden",
Confirm{Message: "Is pizza your favorite food?", Help: "This is helpful"},
ConfirmTemplateData{},
fmt.Sprintf("%s Is pizza your favorite food? [%s for help] (y/N) ", defaultIcons().Question.Text, string(defaultPromptConfig().HelpInput)),
},
{
"Test Confirm help output with help message shown",
Confirm{Message: "Is pizza your favorite food?", Help: "This is helpful"},
ConfirmTemplateData{ShowHelp: true},
fmt.Sprintf("%s This is helpful\n%s Is pizza your favorite food? (y/N) ", defaultIcons().Help.Text, defaultIcons().Question.Text),
},
}
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.Confirm = test.prompt
// set the runtime config
test.data.Config = defaultPromptConfig()
err = test.prompt.Render(
ConfirmQuestionTemplate,
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 TestConfirmPrompt(t *testing.T) {
tests := []PromptTest{
{
"Test Confirm prompt interaction",
&Confirm{
Message: "Is pizza your favorite food?",
},
func(c expectConsole) {
c.ExpectString("Is pizza your favorite food? (y/N)")
c.SendLine("n")
c.ExpectEOF()
},
false,
},
{
"Test Confirm prompt interaction with default",
&Confirm{
Message: "Is pizza your favorite food?",
Default: true,
},
func(c expectConsole) {
c.ExpectString("Is pizza your favorite food? (Y/n)")
c.SendLine("")
c.ExpectEOF()
},
true,
},
{
"Test Confirm prompt interaction overriding default",
&Confirm{
Message: "Is pizza your favorite food?",
Default: true,
},
func(c expectConsole) {
c.ExpectString("Is pizza your favorite food? (Y/n)")
c.SendLine("n")
c.ExpectEOF()
},
false,
},
{
"Test Confirm prompt interaction and prompt for help",
&Confirm{
Message: "Is pizza your favorite food?",
Help: "It probably is",
},
func(c expectConsole) {
c.ExpectString(
fmt.Sprintf(
"Is pizza your favorite food? [%s for help] (y/N)",
string(defaultPromptConfig().HelpInput),
),
)
c.SendLine("?")
c.ExpectString("It probably is")
c.SendLine("Y")
c.ExpectEOF()
},
true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
RunPromptTest(t, test)
})
}
}