forked from AlecAivazis/survey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiline_test.go
159 lines (147 loc) · 4.51 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
package survey
import (
"bytes"
"fmt"
"io"
"os"
"testing"
"github.com/anhcuongdev/survey/v2/core"
"github.com/anhcuongdev/survey/v2/terminal"
"github.com/stretchr/testify/assert"
)
func init() {
// disable color output for all prompts to simplify testing
core.DisableColor = true
}
func TestMultilineRender(t *testing.T) {
tests := []struct {
title string
prompt Multiline
data MultilineTemplateData
expected string
}{
{
"Test Multiline question output without default",
Multiline{Message: "What is your favorite month:"},
MultilineTemplateData{},
fmt.Sprintf("%s What is your favorite month: [Enter 2 empty lines to finish]", defaultIcons().Question.Text),
},
{
"Test Multiline question output with default",
Multiline{Message: "What is your favorite month:", Default: "April"},
MultilineTemplateData{},
fmt.Sprintf("%s What is your favorite month: (April) [Enter 2 empty lines to finish]", defaultIcons().Question.Text),
},
{
"Test Multiline answer output",
Multiline{Message: "What is your favorite month:"},
MultilineTemplateData{Answer: "October", ShowAnswer: true},
fmt.Sprintf("%s What is your favorite month: \nOctober", defaultIcons().Question.Text),
},
{
"Test Multiline question output without default but with help hidden",
Multiline{Message: "What is your favorite month:", Help: "This is helpful"},
MultilineTemplateData{},
fmt.Sprintf("%s What is your favorite month: [Enter 2 empty lines to finish]", string(defaultPromptConfig().HelpInput)),
},
{
"Test Multiline question output with default and with help hidden",
Multiline{Message: "What is your favorite month:", Default: "April", Help: "This is helpful"},
MultilineTemplateData{},
fmt.Sprintf("%s What is your favorite month: (April) [Enter 2 empty lines to finish]", string(defaultPromptConfig().HelpInput)),
},
{
"Test Multiline question output without default but with help shown",
Multiline{Message: "What is your favorite month:", Help: "This is helpful"},
MultilineTemplateData{ShowHelp: true},
fmt.Sprintf("%s This is helpful\n%s What is your favorite month: [Enter 2 empty lines to finish]", defaultIcons().Help.Text, defaultIcons().Question.Text),
},
{
"Test Multiline question output with default and with help shown",
Multiline{Message: "What is your favorite month:", Default: "April", Help: "This is helpful"},
MultilineTemplateData{ShowHelp: true},
fmt.Sprintf("%s This is helpful\n%s What is your favorite month: (April) [Enter 2 empty lines to finish]", 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.Multiline = test.prompt
// set the icon set
test.data.Config = defaultPromptConfig()
err = test.prompt.Render(
MultilineQuestionTemplate,
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, test.title)
})
}
}
func TestMultilinePrompt(t *testing.T) {
tests := []PromptTest{
{
"Test Multiline prompt interaction",
&Multiline{
Message: "What is your name?",
},
func(c expectConsole) {
c.ExpectString("What is your name?")
c.SendLine("Larry Bird\nI guess...\nnot sure\n\n")
c.ExpectEOF()
},
"Larry Bird\nI guess...\nnot sure",
},
{
"Test Multiline prompt interaction with default",
&Multiline{
Message: "What is your name?",
Default: "Johnny Appleseed",
},
func(c expectConsole) {
c.ExpectString("What is your name?")
c.SendLine("\n\n")
c.ExpectEOF()
},
"Johnny Appleseed",
},
{
"Test Multiline prompt interaction overriding default",
&Multiline{
Message: "What is your name?",
Default: "Johnny Appleseed",
},
func(c expectConsole) {
c.ExpectString("What is your name?")
c.SendLine("Larry Bird\n\n")
c.ExpectEOF()
},
"Larry Bird",
},
{
"Test Multiline does not implement help interaction",
&Multiline{
Message: "What is your name?",
Help: "It might be Satoshi Nakamoto",
},
func(c expectConsole) {
c.ExpectString("What is your name?")
c.SendLine("?")
c.SendLine("Satoshi Nakamoto\n\n")
c.ExpectEOF()
},
"?\nSatoshi Nakamoto",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
RunPromptTest(t, test)
})
}
}