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 358
/
input_test.go
171 lines (160 loc) · 4.48 KB
/
input_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
package survey
import (
"bytes"
"fmt"
"io"
"os"
"testing"
expect "github.com/Netflix/go-expect"
"github.com/stretchr/testify/assert"
"gopkg.in/AlecAivazis/survey.v1/core"
"gopkg.in/AlecAivazis/survey.v1/terminal"
)
func init() {
// disable color output for all prompts to simplify testing
core.DisableColor = true
}
func TestInputRender(t *testing.T) {
tests := []struct {
title string
prompt Input
data InputTemplateData
expected string
}{
{
"Test Input question output without default",
Input{Message: "What is your favorite month:"},
InputTemplateData{},
fmt.Sprintf("%s What is your favorite month: ", core.QuestionIcon),
},
{
"Test Input question output with default",
Input{Message: "What is your favorite month:", Default: "April"},
InputTemplateData{},
fmt.Sprintf("%s What is your favorite month: (April) ", core.QuestionIcon),
},
{
"Test Input answer output",
Input{Message: "What is your favorite month:"},
InputTemplateData{Answer: "October", ShowAnswer: true},
fmt.Sprintf("%s What is your favorite month: October\n", core.QuestionIcon),
},
{
"Test Input question output without default but with help hidden",
Input{Message: "What is your favorite month:", Help: "This is helpful"},
InputTemplateData{},
fmt.Sprintf("%s What is your favorite month: [%s for help] ", core.QuestionIcon, string(core.HelpInputRune)),
},
{
"Test Input question output with default and with help hidden",
Input{Message: "What is your favorite month:", Default: "April", Help: "This is helpful"},
InputTemplateData{},
fmt.Sprintf("%s What is your favorite month: [%s for help] (April) ", core.QuestionIcon, string(core.HelpInputRune)),
},
{
"Test Input question output without default but with help shown",
Input{Message: "What is your favorite month:", Help: "This is helpful"},
InputTemplateData{ShowHelp: true},
fmt.Sprintf("%s This is helpful\n%s What is your favorite month: ", core.HelpIcon, core.QuestionIcon),
},
{
"Test Input question output with default and with help shown",
Input{Message: "What is your favorite month:", Default: "April", Help: "This is helpful"},
InputTemplateData{ShowHelp: true},
fmt.Sprintf("%s This is helpful\n%s What is your favorite month: (April) ", core.HelpIcon, core.QuestionIcon),
},
}
for _, test := range tests {
r, w, err := os.Pipe()
assert.Nil(t, err, test.title)
test.prompt.WithStdio(terminal.Stdio{Out: w})
test.data.Input = test.prompt
err = test.prompt.Render(
InputQuestionTemplate,
test.data,
)
assert.Nil(t, err, test.title)
w.Close()
var buf bytes.Buffer
io.Copy(&buf, r)
assert.Contains(t, buf.String(), test.expected, test.title)
}
}
func TestInputPrompt(t *testing.T) {
tests := []PromptTest{
{
"Test Input prompt interaction",
&Input{
Message: "What is your name?",
},
func(c *expect.Console) {
c.ExpectString("What is your name?")
c.SendLine("Larry Bird")
c.ExpectEOF()
},
"Larry Bird",
},
{
"Test Input prompt interaction with default",
&Input{
Message: "What is your name?",
Default: "Johnny Appleseed",
},
func(c *expect.Console) {
c.ExpectString("What is your name?")
c.SendLine("")
c.ExpectEOF()
},
"Johnny Appleseed",
},
{
"Test Input prompt interaction overriding default",
&Input{
Message: "What is your name?",
Default: "Johnny Appleseed",
},
func(c *expect.Console) {
c.ExpectString("What is your name?")
c.SendLine("Larry Bird")
c.ExpectEOF()
},
"Larry Bird",
},
{
"Test Input prompt interaction and prompt for help",
&Input{
Message: "What is your name?",
Help: "It might be Satoshi Nakamoto",
},
func(c *expect.Console) {
c.ExpectString("What is your name?")
c.SendLine("?")
c.ExpectString("It might be Satoshi Nakamoto")
c.SendLine("Satoshi Nakamoto")
c.ExpectEOF()
},
"Satoshi Nakamoto",
},
{
// https://en.wikipedia.org/wiki/ANSI_escape_code
// Device Status Report - Reports the cursor position (CPR) to the
// application as (as though typed at the keyboard) ESC[n;mR, where n is the
// row and m is the column.
"Test Input prompt with R matching DSR",
&Input{
Message: "What is your name?",
},
func(c *expect.Console) {
c.ExpectString("What is your name?")
c.SendLine("R")
c.ExpectEOF()
},
"R",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
RunPromptTest(t, test)
})
}
}