-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinputhandler_esc_test.go
More file actions
473 lines (404 loc) · 10.5 KB
/
inputhandler_esc_test.go
File metadata and controls
473 lines (404 loc) · 10.5 KB
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
package xterm
import (
"testing"
"github.com/google/go-cmp/cmp"
)
// newTestHandler creates an InputHandler with default 80x24 terminal for testing.
func newTestHandler() *InputHandler {
opts := NewOptionsService(nil)
bs := NewBufferService(opts)
cs := NewCharsetService()
core := NewCoreService(opts)
osc := NewOscLinkService(bs)
uni := NewUnicodeService()
bs.Buffer().FillViewportRows(nil)
return NewInputHandler(bs, cs, core, opts, osc, uni)
}
// --- Print handler tests ---
func TestPrint_ASCII(t *testing.T) {
t.Parallel()
h := newTestHandler()
h.ParseString("Hello")
type Expectation struct {
X int
Y int
Text string
}
buf := h.activeBuffer()
got := Expectation{
X: buf.X,
Y: buf.Y,
Text: buf.Lines.Get(buf.YBase+0).TranslateToString(true, 0, -1),
}
want := Expectation{X: 5, Y: 0, Text: "Hello"}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
}
func TestPrint_WideChars(t *testing.T) {
t.Parallel()
h := newTestHandler()
// 世 is a wide character (width 2)
h.ParseString("世") //nolint:gosmopolitan
type Expectation struct {
X int
Width int
}
buf := h.activeBuffer()
got := Expectation{
X: buf.X,
Width: buf.Lines.Get(buf.YBase).GetWidth(0),
}
want := Expectation{X: 2, Width: 2}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
}
func TestPrint_Wrapping(t *testing.T) {
t.Parallel()
opts := NewOptionsService(&TerminalOptions{Cols: 5, Rows: 5})
bs := NewBufferService(opts)
cs := NewCharsetService()
core := NewCoreService(opts)
osc := NewOscLinkService(bs)
uni := NewUnicodeService()
bs.Buffer().FillViewportRows(nil)
h := NewInputHandler(bs, cs, core, opts, osc, uni)
h.ParseString("ABCDEFGH")
type Expectation struct {
X int
Y int
Line0 string
Line1 string
IsWrapped bool
}
buf := h.activeBuffer()
got := Expectation{
X: buf.X,
Y: buf.Y,
Line0: buf.Lines.Get(buf.YBase+0).TranslateToString(true, 0, -1),
Line1: buf.Lines.Get(buf.YBase+1).TranslateToString(true, 0, -1),
IsWrapped: buf.Lines.Get(buf.YBase + 1).IsWrapped,
}
want := Expectation{X: 3, Y: 1, Line0: "ABCDE", Line1: "FGH", IsWrapped: true}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
}
// --- C0 control tests ---
func TestC0_LineFeed(t *testing.T) {
t.Parallel()
h := newTestHandler()
h.ParseString("A\nB")
type Expectation struct {
X int
Y int
}
buf := h.activeBuffer()
got := Expectation{X: buf.X, Y: buf.Y}
want := Expectation{X: 2, Y: 1}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
}
func TestC0_CarriageReturn(t *testing.T) {
t.Parallel()
h := newTestHandler()
h.ParseString("Hello\r")
type Expectation struct {
X int
Y int
}
buf := h.activeBuffer()
got := Expectation{X: buf.X, Y: buf.Y}
want := Expectation{X: 0, Y: 0}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
}
func TestC0_CRLF(t *testing.T) {
t.Parallel()
h := newTestHandler()
h.ParseString("AB\r\nCD")
type Expectation struct {
X int
Y int
Line0 string
Line1 string
}
buf := h.activeBuffer()
got := Expectation{
X: buf.X,
Y: buf.Y,
Line0: buf.Lines.Get(buf.YBase+0).TranslateToString(true, 0, -1),
Line1: buf.Lines.Get(buf.YBase+1).TranslateToString(true, 0, -1),
}
want := Expectation{X: 2, Y: 1, Line0: "AB", Line1: "CD"}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
}
func TestC0_Backspace(t *testing.T) {
t.Parallel()
h := newTestHandler()
h.ParseString("AB\b")
type Expectation struct {
X int
Y int
}
buf := h.activeBuffer()
got := Expectation{X: buf.X, Y: buf.Y}
want := Expectation{X: 1, Y: 0}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
}
func TestC0_BackspaceAtCol0(t *testing.T) {
t.Parallel()
h := newTestHandler()
h.ParseString("\b")
buf := h.activeBuffer()
if buf.X != 0 {
t.Errorf("expected X=0, got %d", buf.X)
}
}
func TestC0_Tab(t *testing.T) {
t.Parallel()
h := newTestHandler()
h.ParseString("\t")
buf := h.activeBuffer()
if buf.X != 8 {
t.Errorf("expected X=8 (default tab stop), got %d", buf.X)
}
}
func TestC0_TabFromMidColumn(t *testing.T) {
t.Parallel()
h := newTestHandler()
h.ParseString("AB\t")
buf := h.activeBuffer()
if buf.X != 8 {
t.Errorf("expected X=8, got %d", buf.X)
}
}
// --- ESC sequence tests ---
func TestESC_SaveRestoreCursor(t *testing.T) {
t.Parallel()
h := newTestHandler()
h.ParseString("ABCDE")
// ESC 7 = save cursor
h.ParseString("\x1b7")
savedX := h.activeBuffer().X
savedY := h.activeBuffer().Y
h.ParseString("\r\nXYZ")
// ESC 8 = restore cursor
h.ParseString("\x1b8")
type Expectation struct {
SavedX int
SavedY int
RestoredX int
RestoredY int
}
buf := h.activeBuffer()
got := Expectation{
SavedX: savedX,
SavedY: savedY,
RestoredX: buf.X,
RestoredY: buf.Y,
}
want := Expectation{SavedX: 5, SavedY: 0, RestoredX: 5, RestoredY: 0}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
}
func TestESC_Index(t *testing.T) {
t.Parallel()
h := newTestHandler()
// ESC D = Index (move cursor down)
h.ParseString("A\x1bD")
type Expectation struct {
X int
Y int
}
buf := h.activeBuffer()
got := Expectation{X: buf.X, Y: buf.Y}
want := Expectation{X: 1, Y: 1}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
}
func TestESC_IndexScrollsAtBottom(t *testing.T) {
t.Parallel()
opts := NewOptionsService(&TerminalOptions{Cols: 10, Rows: 3})
bs := NewBufferService(opts)
cs := NewCharsetService()
core := NewCoreService(opts)
osc := NewOscLinkService(bs)
uni := NewUnicodeService()
bs.Buffer().FillViewportRows(nil)
h := NewInputHandler(bs, cs, core, opts, osc, uni)
// Move to last row, then index should scroll
h.ParseString("\x1bD\x1bD") // two index commands: row 0→1→2
h.ParseString("\x1bD") // at row 2 (scrollBottom), should scroll
buf := h.activeBuffer()
if buf.Y != 2 {
t.Errorf("expected Y=2 (bottom), got %d", buf.Y)
}
}
func TestESC_ReverseIndex(t *testing.T) {
t.Parallel()
h := newTestHandler()
// Move down, then reverse index
h.ParseString("\n\n\x1bM")
type Expectation struct {
X int
Y int
}
buf := h.activeBuffer()
got := Expectation{X: buf.X, Y: buf.Y}
want := Expectation{X: 0, Y: 1}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
}
func TestESC_ReverseIndexScrollsAtTop(t *testing.T) {
t.Parallel()
h := newTestHandler()
// At row 0, reverse index should scroll down (insert blank line at top)
h.ParseString("TopLine")
h.ParseString("\r") // back to col 0
h.ParseString("\x1bM")
buf := h.activeBuffer()
// Cursor should still be at row 0
if buf.Y != 0 {
t.Errorf("expected Y=0, got %d", buf.Y)
}
// The old top line should now be at row 1
line1 := buf.Lines.Get(buf.YBase+1).TranslateToString(true, 0, -1)
if line1 != "TopLine" {
t.Errorf("expected 'TopLine' at row 1, got %q", line1)
}
}
func TestESC_NextLine(t *testing.T) {
t.Parallel()
h := newTestHandler()
h.ParseString("ABC")
// ESC E = NEL (CR + LF)
h.ParseString("\x1bE")
type Expectation struct {
X int
Y int
}
buf := h.activeBuffer()
got := Expectation{X: buf.X, Y: buf.Y}
want := Expectation{X: 0, Y: 1}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
}
func TestESC_TabSet(t *testing.T) {
t.Parallel()
h := newTestHandler()
buf := h.activeBuffer()
// Move to column 5 and set a tab stop
h.ParseString("12345")
h.ParseString("\x1bH") // ESC H = HTS
if !buf.Tabs[5] {
t.Error("expected tab stop at column 5")
}
}
func TestESC_FullReset(t *testing.T) {
t.Parallel()
h := newTestHandler()
var resetFired bool
h.OnRequestResetEmitter.Event(func(struct{}) {
resetFired = true
})
h.ParseString("Hello\n\n")
// ESC c = RIS (full reset)
h.ParseString("\x1bc")
if !resetFired {
t.Error("expected OnRequestReset to fire")
}
}
func TestESC_KeypadModes(t *testing.T) {
t.Parallel()
h := newTestHandler()
// ESC = → application keypad
h.ParseString("\x1b=")
if !h.coreService.DecPrivateModes.ApplicationKeypad {
t.Error("expected application keypad mode")
}
// ESC > → numeric keypad
h.ParseString("\x1b>")
if h.coreService.DecPrivateModes.ApplicationKeypad {
t.Error("expected numeric keypad mode")
}
}
func TestESC_SetgLevel(t *testing.T) {
t.Parallel()
h := newTestHandler()
// ESC n → GL = G2
h.ParseString("\x1bn")
if h.charsetService.GLevel != 2 {
t.Errorf("expected GLevel=2, got %d", h.charsetService.GLevel)
}
// ESC o → GL = G3
h.ParseString("\x1bo")
if h.charsetService.GLevel != 3 {
t.Errorf("expected GLevel=3, got %d", h.charsetService.GLevel)
}
}
func TestESC_SelectDefaultCharset(t *testing.T) {
t.Parallel()
h := newTestHandler()
// Set a non-default charset first
h.charsetService.SetgCharset(0, CharsetDECSpecialGraphics)
h.charsetService.SetgLevel(0)
// ESC % G → select default charset
h.ParseString("\x1b%G")
if h.charsetService.GLevel != 0 {
t.Errorf("expected GLevel=0, got %d", h.charsetService.GLevel)
}
if h.charsetService.Charset != nil {
t.Error("expected nil (default) charset")
}
}
func TestESC_ScreenAlignmentPattern(t *testing.T) {
t.Parallel()
opts := NewOptionsService(&TerminalOptions{Cols: 5, Rows: 3})
bs := NewBufferService(opts)
cs := NewCharsetService()
core := NewCoreService(opts)
osc := NewOscLinkService(bs)
uni := NewUnicodeService()
bs.Buffer().FillViewportRows(nil)
h := NewInputHandler(bs, cs, core, opts, osc, uni)
// ESC # 8 = DECALN
h.ParseString("\x1b#8")
buf := h.activeBuffer()
for row := range 3 {
line := buf.Lines.Get(buf.YBase + row)
text := line.TranslateToString(true, 0, -1)
if text != "EEEEE" {
t.Errorf("row %d: expected 'EEEEE', got %q", row, text)
}
}
// Cursor should be at 0,0
if buf.X != 0 || buf.Y != 0 {
t.Errorf("expected cursor at (0,0), got (%d,%d)", buf.X, buf.Y)
}
}
func TestESC_SelectCharset_DECSpecialGraphics(t *testing.T) {
t.Parallel()
h := newTestHandler()
// ESC ( 0 → designate DEC Special Graphics to G0
h.ParseString("\x1b(0")
// Now printing 'q' should produce box-drawing horizontal line (U+2500)
h.ParseString("q")
buf := h.activeBuffer()
line := buf.Lines.Get(buf.YBase)
cp := line.GetCodePoint(0)
if cp != 0x2500 {
t.Errorf("expected codepoint 0x2500 (─), got 0x%X", cp)
}
}