forked from wagoodman/bashful
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreen_test.go
260 lines (209 loc) · 6.88 KB
/
screen_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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package main
import (
"bytes"
"io"
"os"
"testing"
"github.com/alecthomas/repr"
)
func captureBoolStdout(f func(bool), x bool) string {
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
f(x)
w.Close()
os.Stdout = old
var buf bytes.Buffer
io.Copy(&buf, r)
return buf.String()
}
func captureStrStdout(f func(string), x string) string {
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
f(x)
w.Close()
os.Stdout = old
var buf bytes.Buffer
io.Copy(&buf, r)
return buf.String()
}
func captureIntStdout(f func(int), x int) string {
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
f(x)
w.Close()
os.Stdout = old
var buf bytes.Buffer
io.Copy(&buf, r)
return buf.String()
}
func captureStrIntStdout(f func(string, int), x string, y int) string {
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
f(x, y)
w.Close()
os.Stdout = old
var buf bytes.Buffer
io.Copy(&buf, r)
return buf.String()
}
func TestVisualLength(t *testing.T) {
ansiString := "\x1b[2mHello, World!\x1b[0m"
normalLen := len(ansiString)
if normalLen != 21 {
t.Error("TestVisualLength: Test harness not working! Expected 15 got", normalLen)
}
visualLen := visualLength(ansiString)
if visualLen != 13 {
t.Error("Expected 13 got", visualLen)
}
}
func TestTrimToVisualLength(t *testing.T) {
normalString := "Hello, World!"
ansiString := "\x1b[2mHel\x1b[3mlo, Wor\x1b[0mld!\x1b[0m"
for idx := 0; idx < len(normalString); idx++ {
trimString := trimToVisualLength(ansiString, idx)
if visualLength(trimString) != idx {
t.Error("TestTrimToVisualLength: Expected", idx, "got", visualLength(trimString), ". Trim:", trimString)
}
}
}
func TestMoveCursor(t *testing.T) {
var expectedOutput, testOutput string
scr := Screen()
scr.ResetFrame(5, false, false)
// stay in place
scr.curLine = 1
expectedOutput = ""
testOutput = captureIntStdout(scr.MoveCursor, 1)
if expectedOutput != testOutput {
t.Error("TestMoveCursor (Stay in place): Expected", repr.String(expectedOutput), "got", repr.String(testOutput))
}
// move to the next line
scr.curLine = 1
expectedOutput = "\x1b[1B"
testOutput = captureIntStdout(scr.MoveCursor, 2)
if expectedOutput != testOutput {
t.Error("TestMoveCursor (Move to the next line): Expected", repr.String(expectedOutput), "got", repr.String(testOutput))
}
// move a few lines away
scr.curLine = 1
expectedOutput = "\x1b[2B"
testOutput = captureIntStdout(scr.MoveCursor, 3)
if expectedOutput != testOutput {
t.Error("TestMoveCursor (Move a few lines away): Expected", repr.String(expectedOutput), "got", repr.String(testOutput))
}
// move past the frame (no footer, to last line)
scr.curLine = 1
expectedOutput = "\x1b[3B"
testOutput = captureIntStdout(scr.MoveCursor, 10)
if expectedOutput != testOutput {
t.Error("TestMoveCursor (Move past the frame, to last line): Expected", repr.String(expectedOutput), "got", repr.String(testOutput))
}
// move past the frame (no header, to first line)
scr.curLine = 2
expectedOutput = "\x1b[2A"
testOutput = captureIntStdout(scr.MoveCursor, -10)
if expectedOutput != testOutput {
t.Error("TestMoveCursor (Move past the frame, to first line): Expected", repr.String(expectedOutput), "got", repr.String(testOutput))
}
scr.ResetFrame(5, true, true)
// move past the frame (to footer)
scr.curLine = 1
expectedOutput = "\x1b[4B"
testOutput = captureIntStdout(scr.MoveCursor, 10)
if expectedOutput != testOutput {
t.Error("TestMoveCursor (Move past the frame, to footer): Expected", repr.String(expectedOutput), "got", repr.String(testOutput))
}
// move past the frame (to header)
scr.curLine = 2
expectedOutput = "\x1b[3A"
testOutput = captureIntStdout(scr.MoveCursor, -10)
if expectedOutput != testOutput {
t.Error("TestMoveCursor (Move past the frame, to header): Expected", repr.String(expectedOutput), "got", repr.String(testOutput))
}
}
func TestMovePastFrame(t *testing.T) {
var testOutput string
scr := Screen()
scr.ResetFrame(5, false, false)
var testData = []struct {
hasHeader bool
hasFooter bool
keepHeader bool
expectedOutput string
}{
{false, false, false, "\x1b[3B\x1b[1B"},
{false, true, false, "\x1b[4B"},
{true, false, false, "\x1b[3B\x1b[1B"},
{true, true, false, "\x1b[4B"},
{false, false, true, "\x1b[3B\x1b[1B"},
{false, true, true, "\x1b[4B\x1b[1B"},
{true, false, true, "\x1b[3B\x1b[1B"},
{true, true, true, "\x1b[4B\x1b[1B"},
}
for _, testObj := range testData {
scr.ResetFrame(5, testObj.hasHeader, testObj.hasFooter)
scr.curLine = 1
testOutput = captureBoolStdout(scr.MovePastFrame, testObj.keepHeader)
if testObj.expectedOutput != testOutput {
t.Error("MovePastFrame (header:", testObj.hasHeader, ", footer:", testObj.hasFooter, ", keepheader:", testObj.keepHeader, "): Expected", repr.String(testObj.expectedOutput), "got", repr.String(testOutput))
}
}
}
func TestDisplayFooter(t *testing.T) {
var expectedOutput, testOutput string
scr := Screen()
scr.ResetFrame(5, false, false)
scr.curLine = 1
expectedOutput = "\x1b[3B\x1b[2K\x1b[0GThis is the footer.\n"
testOutput = captureStrStdout(scr.DisplayFooter, "This is the footer.")
if expectedOutput != testOutput {
t.Error("TestDisplayFooter (Stay in place): Expected", repr.String(expectedOutput), "got", repr.String(testOutput))
}
}
func TestDisplayHeader(t *testing.T) {
var expectedOutput, testOutput string
scr := Screen()
scr.ResetFrame(5, false, false)
scr.curLine = 1
expectedOutput = "\x1b[1A\x1b[2K\x1b[0GThis is the header.\n"
testOutput = captureStrStdout(scr.DisplayHeader, "This is the header.")
if expectedOutput != testOutput {
t.Error("TestDisplayFooter (Stay in place): Expected", repr.String(expectedOutput), "got", repr.String(testOutput))
}
}
func TestPrintLn(t *testing.T) {
var expectedOutput, testOutput string
scr := Screen()
scr.ResetFrame(5, false, false)
scr.curLine = 1
expectedOutput = "\x1b[2K\x1b[0GHello, World!\n"
testOutput = captureStrStdout(scr.printLn, "Hello, World!")
if expectedOutput != testOutput {
t.Error("TestPrintLn (Stay in place): Expected", repr.String(expectedOutput), "got", repr.String(testOutput))
}
}
func TestDisplay(t *testing.T) {
var expectedOutput, testOutput string
scr := Screen()
scr.ResetFrame(5, false, false)
terminalWidth = func() (uint, error) {
return uint(20), nil
}
scr.curLine = 1
expectedOutput = "\x1b[2B\x1b[2K\x1b[0GHello, World!\n"
testOutput = captureStrIntStdout(scr.Display, "Hello, World!", 3)
if expectedOutput != testOutput {
t.Error("TestDisplay (simply display): Expected", repr.String(expectedOutput), "got", repr.String(testOutput))
}
scr.curLine = 1
expectedOutput = "\x1b[2B\x1b[2K\x1b[0GHello, World!!!!!...\n"
testOutput = captureStrIntStdout(scr.Display, "Hello, World!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", 3)
if expectedOutput != testOutput {
t.Error("TestDisplay (trim long lines): Expected", repr.String(expectedOutput), "got", repr.String(testOutput))
}
}