-
Notifications
You must be signed in to change notification settings - Fork 38
/
escape_test.go
336 lines (296 loc) · 10.4 KB
/
escape_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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package terminal
import (
"strconv"
"strings"
"testing"
"fyne.io/fyne/v2"
"github.com/stretchr/testify/assert"
)
func TestClearScreen(t *testing.T) {
term := New()
term.config.Columns = 5
term.config.Rows = 2
term.handleOutput([]byte("Hello"))
assert.Equal(t, "Hello", term.content.Text())
term.handleEscape("2J")
assert.Equal(t, "", term.content.Text())
}
// test clearing the screen by using "scrollback"
// this is a method tmux uses to "clear the screen"
func TestScrollBack_Tmux(t *testing.T) {
// Step 1: Setup a new terminal instance
term := New()
term.debug = true
term.config.Columns = 80 // 80 columns (standard terminal width)
term.config.Rows = 5 // Doesn't matter
// Step 2: Populate the entire screen with lines using cursor movement
for i := 1; i <= 40; i++ {
lineText := "Line " + strconv.Itoa(i)
// Move the cursor to the beginning of each line using the escape sequence \x1b[{row};{col}H
escapeMoveCursor := "\x1b[" + strconv.Itoa(i) + ";1H"
term.handleOutput([]byte(escapeMoveCursor + lineText))
}
// Step 3: Set up the scroll region and scroll content away
term.handleOutput([]byte("\x1b[1;47r")) // Set scroll region from lines 1 to 47
term.handleOutput([]byte("\x1b[2;47r")) // Set scroll region again (redundant in most cases)
term.handleOutput([]byte("\x1b[46S")) // Scroll up by 46 lines (this should move almost all content out of view)
// Step 4: Additional escape sequences to clear the screen
term.handleOutput([]byte("\x1b[1;1H")) // Move cursor to the top-left corner
term.handleOutput([]byte("\x1b[K")) // Clear the current line
term.handleOutput([]byte("\x1b[1;48r")) // Restore scroll region to the full screen
term.handleOutput([]byte("\x1b[1;1H")) // Move cursor to top-left again
term.handleOutput([]byte("\x1b(B")) // Reset character set
term.handleOutput([]byte("\x1b[m")) // Reset all attributes
// Step 5: Check the final content of the terminal
expectedContent := "" // After scrolling and clearing, the visible area should be empty
for i := 0; i < 46; i++ {
expectedContent += "\n" // Each row should be an empty line
}
assert.Equal(t, 0, term.cursorRow)
assert.Equal(t, 0, term.cursorCol)
assert.Equal(t, expectedContent, term.content.Text())
}
func TestScrollBack_With_Zero_Back_Buffer(t *testing.T) {
// Define the test cases using a map
tests := map[string]struct {
linesToAdd int
scrollLines int
expectedOutput string
expectedCursorRow int
expectedCursorCol int
}{
"when 5 lines added and scrolled up 4 lines, should show line 5": {
linesToAdd: 5,
scrollLines: 4,
expectedOutput: "Line 5",
expectedCursorRow: 0, // Adjust as needed
expectedCursorCol: 6, // Assuming cursor is at end of visible text
},
// Add more test cases here as needed
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
// Setup: Create a new terminal instance with a zero back buffer
term := New()
term.debug = true
term.config.Columns = 80 // 80 columns (standard terminal width)
term.config.Rows = 5 // Doesn't matter
// Step 1: Populate the entire screen with lines using cursor movement
for i := 1; i <= tt.linesToAdd; i++ {
lineText := "Line " + strconv.Itoa(i)
// Move the cursor to the beginning of each line using the escape sequence \x1b[{row};{col}H
escapeMoveCursor := "\x1b[" + strconv.Itoa(i) + ";1H" // Move cursor to row i, column 1
term.handleOutput([]byte(escapeMoveCursor + lineText))
}
term.handleOutput([]byte("\x1b[1;" + strconv.Itoa(tt.linesToAdd) + "r")) // Set scroll region from lines 1 to linesToAdd
term.handleOutput([]byte("\x1b[" + strconv.Itoa(tt.scrollLines) + "S")) // Scroll up
// Step 3: Get the current output after scrolling
currentOutput := strings.TrimRight(term.Text(), "\n")
// Step 4: Assert that the output matches
assert.Equal(t, tt.expectedOutput, currentOutput)
// Step 5: Check the final content of the terminal
assert.Equal(t, tt.expectedCursorRow, term.cursorRow)
assert.Equal(t, tt.expectedCursorCol, term.cursorCol)
})
}
}
func TestInsertDeleteChars(t *testing.T) {
term := New()
term.config.Columns = 5
term.config.Rows = 2
term.handleOutput([]byte("Hello"))
assert.Equal(t, "Hello", term.content.Text())
term.moveCursor(0, 2)
term.handleEscape("2@")
assert.Equal(t, "He llo", term.content.Text())
term.handleEscape("3P")
assert.Equal(t, "Helo", term.content.Text())
}
func TestEraseLine(t *testing.T) {
term := New()
term.config.Columns = 5
term.config.Rows = 2
term.handleOutput([]byte("Hello"))
assert.Equal(t, "Hello", term.content.Text())
term.moveCursor(0, 2)
term.handleEscape("K")
assert.Equal(t, "He", term.content.Text())
}
func TestCursorMove(t *testing.T) {
term := New()
term.config.Columns = 5
term.config.Rows = 2
term.handleOutput([]byte("Hello"))
assert.Equal(t, 0, term.cursorRow)
assert.Equal(t, 5, term.cursorCol)
term.handleEscape("1;4H")
assert.Equal(t, 0, term.cursorRow)
assert.Equal(t, 3, term.cursorCol)
term.handleEscape("2D")
assert.Equal(t, 0, term.cursorRow)
assert.Equal(t, 1, term.cursorCol)
term.handleEscape("2C")
assert.Equal(t, 0, term.cursorRow)
assert.Equal(t, 3, term.cursorCol)
term.handleEscape("1B")
assert.Equal(t, 1, term.cursorRow)
assert.Equal(t, 3, term.cursorCol)
term.handleEscape("1A")
assert.Equal(t, 0, term.cursorRow)
assert.Equal(t, 3, term.cursorCol)
}
func TestCursorMove_Overflow(t *testing.T) {
term := New()
term.config.Columns = 2
term.config.Rows = 2
term.handleEscape("2;2H")
assert.Equal(t, 1, term.cursorRow)
assert.Equal(t, 1, term.cursorCol)
term.handleEscape("2D")
assert.Equal(t, 1, term.cursorRow)
assert.Equal(t, 0, term.cursorCol)
term.handleEscape("5C")
assert.Equal(t, 1, term.cursorRow)
assert.Equal(t, 1, term.cursorCol)
term.handleEscape("5A")
assert.Equal(t, 0, term.cursorRow)
assert.Equal(t, 1, term.cursorCol)
term.handleEscape("4B")
assert.Equal(t, 1, term.cursorRow)
assert.Equal(t, 1, term.cursorCol)
}
func TestTrimLeftZeros(t *testing.T) {
assert.Equal(t, "1", trimLeftZeros(string([]byte{0, 0, '1'})))
}
func TestHandleOutput_NewLineMode(t *testing.T) {
tests := []struct {
name string
input string // Escape codes to feed into handleOutput
expectedCursorRow int // Expected cursorRow after processing escape codes
expectedCursorCol int // Expected cursorCol after processing escape codes
expectedNewLineMode bool // Expected value of newLineMode after processing escape codes
expectedContentText string // Expected content text after processing escape codes
expectedContentRowCount int // Expected number of rows in content after processing escape codes
}{
{
name: "single line",
input: "hello",
expectedCursorRow: 0,
expectedCursorCol: 5,
expectedNewLineMode: false,
expectedContentText: "hello",
expectedContentRowCount: 1,
},
{
name: "Default - carriage return new line",
input: "hello\r\nworld",
expectedCursorRow: 1,
expectedCursorCol: 5,
expectedNewLineMode: false,
expectedContentText: "hello\nworld",
expectedContentRowCount: 2,
},
{
name: "Default - new line",
input: "hello\nworld",
expectedCursorRow: 1,
expectedCursorCol: 10,
expectedNewLineMode: false,
expectedContentText: "hello\n world",
expectedContentRowCount: 2,
},
{
name: "Enable New Line Mode",
input: "\x1b[?20hhello\nworld",
expectedCursorRow: 1,
expectedCursorCol: 5,
expectedNewLineMode: true,
expectedContentText: "hello\nworld",
expectedContentRowCount: 2,
},
{
name: "Enable then disable New Line Mode",
input: "\x1b[?20h\x1b[?20lhello\nworld",
expectedCursorRow: 1,
expectedCursorCol: 10,
expectedNewLineMode: false,
expectedContentText: "hello\n world",
expectedContentRowCount: 2,
},
{
name: "Enable new line mode - lf vt ff",
input: "\x1b[?20hhello\n\v\fworld",
expectedCursorRow: 3,
expectedCursorCol: 5,
expectedNewLineMode: true,
expectedContentText: "hello\n\n\nworld",
expectedContentRowCount: 4,
},
{
name: "Default new line mode - lf vt ff",
input: "hello\n\v\fworld",
expectedCursorRow: 3,
expectedCursorCol: 10,
expectedNewLineMode: false,
expectedContentText: "hello\n\n\n world",
expectedContentRowCount: 4,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
term := New()
term.Resize(fyne.NewSize(500, 500))
term.handleOutput([]byte(tt.input))
assert.Equal(t, tt.expectedCursorRow, term.cursorRow)
assert.Equal(t, tt.expectedCursorCol, term.cursorCol)
assert.Equal(t, tt.expectedNewLineMode, term.newLineMode)
assert.Equal(t, tt.expectedContentText, term.content.Text())
assert.Equal(t, tt.expectedContentRowCount, len(term.content.Rows))
})
}
}
func TestTerminalEscapeSequences(t *testing.T) {
testCases := []struct {
input string
expected string
description string
}{
{
input: string([]byte{asciiEscape}) + "(BHello",
expected: "Hello",
description: "Test set G0 to ASCII charset",
},
{
input: string([]byte{asciiEscape}) + ")BHola",
expected: "Hola",
description: "Test set G1 to ASCII charset",
},
{
input: string([]byte{asciiEscape}) + "(0oooo",
expected: "⎺⎺⎺⎺", // Using decSpecialGraphics map
description: "Test set G0 to DEC charset",
},
{
input: string([]byte{asciiEscape, ')', '0', 0x0e}) + "oooo",
expected: "⎺⎺⎺⎺",
description: "Test set G1 to DEC charset and 'SO' to switch to G1",
},
{
input: string([]byte{asciiEscape, ')', '0', 0x0e}) + "oooo" + string([]byte{0x0f, 'o'}),
expected: "⎺⎺⎺⎺o",
description: "Test set G1 to DEC charset and 'SO' to switch to G1, then 'SI' to G0",
},
}
for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
term := New()
term.config.Columns = 10
term.config.Rows = 1
term.handleOutput([]byte(testCase.input))
actual := term.content.Text()
if actual != testCase.expected {
t.Errorf("Expected: %s, Got: %s", testCase.expected, actual)
}
})
}
}