forked from pterm/pterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interactive_textinput_printer_test.go
243 lines (215 loc) · 7.41 KB
/
interactive_textinput_printer_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
package pterm_test
import (
"os"
"reflect"
"testing"
"time"
"atomicgo.dev/keyboard"
"atomicgo.dev/keyboard/keys"
"github.com/MarvinJWendt/testza"
"github.com/overmindtech/pterm"
"github.com/overmindtech/pterm/internal"
)
func TestInteractiveTextInputPrinter_WithDefaultText(t *testing.T) {
p := pterm.DefaultInteractiveTextInput.WithDefaultText("default")
testza.AssertEqual(t, p.DefaultText, "default")
}
func TestInteractiveTextInputPrinter_WithDefaultValue(t *testing.T) {
p := pterm.DefaultInteractiveTextInput.WithDefaultValue("default")
testza.AssertEqual(t, p.DefaultValue, "default")
}
func TestInteractiveTextInputPrinter_WithDelimiter(t *testing.T) {
p := pterm.DefaultInteractiveTextInput.WithDelimiter(">>")
testza.AssertEqual(t, p.Delimiter, ">>")
}
func TestInteractiveTextInputPrinter_WithMultiLine_true(t *testing.T) {
p := pterm.DefaultInteractiveTextInput.WithMultiLine()
testza.AssertTrue(t, p.MultiLine)
}
func TestInteractiveTextInputPrinter_WithMultiLine_false(t *testing.T) {
p := pterm.DefaultInteractiveTextInput.WithMultiLine(false)
testza.AssertFalse(t, p.MultiLine)
}
func TestInteractiveTextInputPrinter_WithTextStyle(t *testing.T) {
style := pterm.NewStyle(pterm.FgRed)
p := pterm.DefaultInteractiveTextInput.WithTextStyle(style)
testza.AssertEqual(t, p.TextStyle, style)
}
func TestInteractiveTextInputPrinter_WithMask(t *testing.T) {
go func() {
time.Sleep(1 * time.Millisecond)
keyboard.SimulateKeyPress('a')
keyboard.SimulateKeyPress('b')
keyboard.SimulateKeyPress('c')
keyboard.SimulateKeyPress(keys.Enter)
}()
result, _ := pterm.DefaultInteractiveTextInput.WithMask("*").Show()
testza.AssertEqual(t, "abc", result)
}
func TestInteractiveTextInputPrinter_WithCancel(t *testing.T) {
exitCalled := false
internal.DefaultExitFunc = func(code int) {
exitCalled = true
}
defer func() { internal.DefaultExitFunc = os.Exit }()
go func() {
time.Sleep(1 * time.Millisecond)
keyboard.SimulateKeyPress(keys.CtrlC)
}()
result, _ := pterm.DefaultInteractiveTextInput.WithMask("*").Show()
testza.AssertEqual(t, "", result)
if !exitCalled {
t.Errorf("Expected exit to be called on Ctrl+C")
}
}
func TestInteractiveTextInputPrinter_OnEnter(t *testing.T) {
go func() {
keyboard.SimulateKeyPress(keys.Enter)
}()
result, _ := pterm.DefaultInteractiveTextInput.WithDefaultValue("default").Show()
testza.AssertEqual(t, "default", result)
}
func TestInteractiveTextInputPrinter_Editable(t *testing.T) {
go func() {
// change `default` to `deffaultt` by simulating cursor moves `left`, `right` and inserting
// keys on current cursor positions.
keyboard.SimulateKeyPress(keys.Left)
keyboard.SimulateKeyPress(keys.Left)
keyboard.SimulateKeyPress(keys.Left)
keyboard.SimulateKeyPress(keys.Left)
keyboard.SimulateKeyPress(keys.Key{Code: keys.RuneKey, Runes: []rune{'f'}})
keyboard.SimulateKeyPress(keys.Right)
keyboard.SimulateKeyPress(keys.Right)
keyboard.SimulateKeyPress(keys.Right)
keyboard.SimulateKeyPress(keys.Key{Code: keys.RuneKey, Runes: []rune{'t'}})
keyboard.SimulateKeyPress(keys.Enter)
}()
result, _ := pterm.DefaultInteractiveTextInput.WithDefaultValue("default").Show()
testza.AssertEqual(t, "deffaultt", result)
}
func TestInteractiveTextInputPrinter_WithMultiLineOnTab(t *testing.T) {
go func() {
keyboard.SimulateKeyPress(keys.Tab)
}()
result, _ := pterm.DefaultInteractiveTextInput.
WithMultiLine(true).
WithDefaultValue("default").Show()
testza.AssertEqual(t, "default", result)
}
func TestInteractiveTextInputPrinter_WithMultiLineOnUp(t *testing.T) {
go func() {
time.Sleep(1 * time.Millisecond)
keyboard.SimulateKeyPress(keys.Backspace)
keyboard.SimulateKeyPress(keys.Enter)
keyboard.SimulateKeyPress("second line")
keyboard.SimulateKeyPress(keys.Up)
keyboard.SimulateKeyPress("first line")
keyboard.SimulateKeyPress(keys.Tab)
}()
result, _ := pterm.DefaultInteractiveTextInput.
WithMultiLine(true).Show()
testza.AssertEqual(t, "first line\nsecond line", result)
}
func TestInteractiveTextInputPrinter_WithMultiLineOnDown(t *testing.T) {
go func() {
time.Sleep(1 * time.Millisecond)
keyboard.SimulateKeyPress("a")
keyboard.SimulateKeyPress(keys.Enter)
keyboard.SimulateKeyPress(keys.Enter)
keyboard.SimulateKeyPress(keys.Up)
keyboard.SimulateKeyPress("b")
keyboard.SimulateKeyPress(keys.Down)
keyboard.SimulateKeyPress("c")
keyboard.SimulateKeyPress(keys.Tab)
}()
result, _ := pterm.DefaultInteractiveTextInput.
WithMultiLine(true).Show()
testza.AssertEqual(t, "a\nb\nc", result)
}
func TestInteractiveTextInputPrinter_WithMultiLineOnLeft(t *testing.T) {
go func() {
time.Sleep(1 * time.Millisecond)
keyboard.SimulateKeyPress(keys.Backspace)
keyboard.SimulateKeyPress(keys.Enter)
keyboard.SimulateKeyPress("a")
keyboard.SimulateKeyPress(keys.Left)
keyboard.SimulateKeyPress(keys.Left)
keyboard.SimulateKeyPress("b")
keyboard.SimulateKeyPress(keys.Tab)
}()
result, _ := pterm.DefaultInteractiveTextInput.
WithMultiLine(true).Show()
testza.AssertEqual(t, "b\na", result)
}
func TestInteractiveTextInputPrinter_WithMultiLineOnRight(t *testing.T) {
go func() {
time.Sleep(1 * time.Millisecond)
keyboard.SimulateKeyPress('a')
keyboard.SimulateKeyPress(keys.Enter)
keyboard.SimulateKeyPress(keys.Up)
keyboard.SimulateKeyPress(keys.Right)
keyboard.SimulateKeyPress(keys.Right)
keyboard.SimulateKeyPress("b")
keyboard.SimulateKeyPress(keys.Tab)
}()
result, _ := pterm.DefaultInteractiveTextInput.
WithMultiLine(true).Show()
testza.AssertEqual(t, "a\nb", result)
}
func TestInteractiveTextInputPrinter_OnBackspace(t *testing.T) {
go func() {
keyboard.SimulateKeyPress(keys.Backspace)
keyboard.SimulateKeyPress(keys.Enter)
}()
result, _ := pterm.DefaultInteractiveTextInput.
WithDefaultValue("a").Show()
testza.AssertEqual(t, "", result)
}
func TestInteractiveTextInputPrinter_WithMultiLineOnDelete(t *testing.T) {
go func() {
keyboard.SimulateKeyPress(keys.Backspace)
keyboard.SimulateKeyPress(keys.Enter)
keyboard.SimulateKeyPress('a')
keyboard.SimulateKeyPress(keys.Up)
keyboard.SimulateKeyPress(keys.Delete)
keyboard.SimulateKeyPress(keys.Tab)
}()
result, _ := pterm.DefaultInteractiveTextInput.
WithMultiLine(true).
WithDefaultValue("a").Show()
testza.AssertEqual(t, "a", result)
}
func TestInteractiveTextInputPrinter_WithMultiLineOnBackspace(t *testing.T) {
go func() {
keyboard.SimulateKeyPress(keys.Backspace)
keyboard.SimulateKeyPress(keys.Enter)
keyboard.SimulateKeyPress(keys.Backspace)
keyboard.SimulateKeyPress(keys.Tab)
}()
result, _ := pterm.DefaultInteractiveTextInput.
WithMultiLine(true).
WithDefaultValue("a").Show()
testza.AssertEqual(t, "", result)
}
func TestInteractiveTextInputPrinter_WithMultiLineOnLeftRight(t *testing.T) {
go func() {
time.Sleep(1 * time.Millisecond)
keyboard.SimulateKeyPress("a")
keyboard.SimulateKeyPress(keys.Enter)
keyboard.SimulateKeyPress("b")
keyboard.SimulateKeyPress(keys.Tab)
}()
result, _ := pterm.DefaultInteractiveTextInput.
WithMultiLine(true).
Show("Enter")
testza.AssertEqual(t, "a\nb", result)
}
func TestInteractiveTextInputPrinter_WithOnInterruptFunc(t *testing.T) {
// OnInterrupt function defaults to nil
pd := pterm.InteractiveTextInputPrinter{}
testza.AssertNil(t, pd.OnInterruptFunc)
// Verify OnInterrupt is set
exitfunc := func() {}
p := pterm.DefaultInteractiveTextInput.WithOnInterruptFunc(exitfunc)
testza.AssertEqual(t, reflect.ValueOf(p.OnInterruptFunc).Pointer(), reflect.ValueOf(exitfunc).Pointer())
}