-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbufferservice_test.go
More file actions
434 lines (351 loc) · 9.62 KB
/
bufferservice_test.go
File metadata and controls
434 lines (351 loc) · 9.62 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
package xterm
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func newTestBufferService(cols, rows, scrollback int) *BufferService {
opts := NewOptionsService(&TerminalOptions{
Cols: cols,
Rows: rows,
Scrollback: scrollback,
})
return NewBufferService(opts)
}
func TestBufferServiceNew(t *testing.T) {
t.Parallel()
type Expectation struct {
Cols int
Rows int
BufferNotNil bool
BuffersNotNil bool
IsUserScrolling bool
}
bs := newTestBufferService(80, 24, 1000)
got := Expectation{
Cols: bs.Cols,
Rows: bs.Rows,
BufferNotNil: bs.Buffer() != nil,
BuffersNotNil: bs.Buffers != nil,
IsUserScrolling: bs.IsUserScrolling,
}
expected := Expectation{
Cols: 80,
Rows: 24,
BufferNotNil: true,
BuffersNotNil: true,
IsUserScrolling: false,
}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestBufferServiceMinimumDimensions(t *testing.T) {
t.Parallel()
type Expectation struct {
Cols int
Rows int
}
// Create options with cols/rows below minimum, bypassing the override logic
opts := NewOptionsService(nil)
opts.Options.Cols = 1
opts.Options.Rows = 0
bs := NewBufferService(opts)
got := Expectation{Cols: bs.Cols, Rows: bs.Rows}
expected := Expectation{Cols: MinimumCols, Rows: MinimumRows}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestBufferServiceResize(t *testing.T) {
t.Parallel()
type Expectation struct {
Cols int
Rows int
ColsChanged bool
RowsChanged bool
}
bs := newTestBufferService(80, 24, 1000)
var event BufferResizeEvent
bs.OnResizeEmitter.Event(func(e BufferResizeEvent) { event = e })
bs.Resize(120, 40)
got := Expectation{
Cols: bs.Cols,
Rows: bs.Rows,
ColsChanged: event.ColsChanged,
RowsChanged: event.RowsChanged,
}
expected := Expectation{Cols: 120, Rows: 40, ColsChanged: true, RowsChanged: true}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestBufferServiceResizeNoChange(t *testing.T) {
t.Parallel()
type Expectation struct {
ColsChanged bool
RowsChanged bool
}
bs := newTestBufferService(80, 24, 1000)
var event BufferResizeEvent
bs.OnResizeEmitter.Event(func(e BufferResizeEvent) { event = e })
bs.Resize(80, 24)
got := Expectation{ColsChanged: event.ColsChanged, RowsChanged: event.RowsChanged}
expected := Expectation{ColsChanged: false, RowsChanged: false}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestBufferServiceReset(t *testing.T) {
t.Parallel()
type Expectation struct {
IsUserScrolling bool
CursorX int
CursorY int
}
bs := newTestBufferService(80, 24, 1000)
bs.IsUserScrolling = true
bs.Buffer().X = 10
bs.Buffer().Y = 5
bs.Reset()
got := Expectation{
IsUserScrolling: bs.IsUserScrolling,
CursorX: bs.Buffer().X,
CursorY: bs.Buffer().Y,
}
expected := Expectation{IsUserScrolling: false, CursorX: 0, CursorY: 0}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestBufferServiceScroll(t *testing.T) {
t.Parallel()
type Expectation struct {
YBase int
YDisp int
LinesLength int
ScrollEvent int
}
bs := newTestBufferService(80, 5, 100)
da := DefaultAttrData()
var lastScroll int
bs.OnScrollEmitter.Event(func(v int) { lastScroll = v })
// Scroll once — should increase ybase since buffer isn't full
bs.Scroll(&da, false)
got := Expectation{
YBase: bs.Buffer().YBase,
YDisp: bs.Buffer().YDisp,
LinesLength: bs.Buffer().Lines.Length(),
ScrollEvent: lastScroll,
}
expected := Expectation{
YBase: 1,
YDisp: 1,
LinesLength: 6,
ScrollEvent: 1,
}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestBufferServiceScrollWrapped(t *testing.T) {
t.Parallel()
type Expectation struct {
IsWrapped bool
}
bs := newTestBufferService(80, 5, 100)
da := DefaultAttrData()
bs.Scroll(&da, true)
// The new line at the bottom should be wrapped
lastLine := bs.Buffer().Lines.Get(bs.Buffer().Lines.Length() - 1)
got := Expectation{IsWrapped: lastLine.IsWrapped}
expected := Expectation{IsWrapped: true}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestBufferServiceScrollUserScrolling(t *testing.T) {
t.Parallel()
type Expectation struct {
YDisp int
}
bs := newTestBufferService(80, 5, 100)
da := DefaultAttrData()
// Scroll a few times to build up ybase
for range 3 {
bs.Scroll(&da, false)
}
// Simulate user scrolling up
bs.IsUserScrolling = true
bs.Buffer().YDisp = 0
// Scroll again — ydisp should stay at user's position
bs.Scroll(&da, false)
got := Expectation{YDisp: bs.Buffer().YDisp}
// ydisp stays where user put it (not following ybase)
expected := Expectation{YDisp: 0}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestBufferServiceScrollWithScrollRegion(t *testing.T) {
t.Parallel()
type Expectation struct {
YBase int
LinesLength int
}
bs := newTestBufferService(80, 5, 100)
da := DefaultAttrData()
// Set a scroll region (not starting at 0)
bs.Buffer().ScrollTop = 1
bs.Buffer().ScrollBottom = 3
bs.Scroll(&da, false)
// With non-zero scrollTop, ybase should not change
got := Expectation{
YBase: bs.Buffer().YBase,
LinesLength: bs.Buffer().Lines.Length(),
}
expected := Expectation{YBase: 0, LinesLength: 5}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestBufferServiceScrollLines(t *testing.T) {
t.Parallel()
type Expectation struct {
YDisp int
IsUserScrolling bool
ScrollEvents int
}
bs := newTestBufferService(80, 5, 100)
da := DefaultAttrData()
// Build up scrollback
for range 10 {
bs.Scroll(&da, false)
}
scrollEvents := 0
bs.OnScrollEmitter.Event(func(int) { scrollEvents++ })
// Scroll up
bs.ScrollLines(-3, false)
got := Expectation{
YDisp: bs.Buffer().YDisp,
IsUserScrolling: bs.IsUserScrolling,
ScrollEvents: scrollEvents,
}
expected := Expectation{YDisp: 7, IsUserScrolling: true, ScrollEvents: 1}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestBufferServiceScrollLinesDown(t *testing.T) {
t.Parallel()
type Expectation struct {
YDisp int
IsUserScrolling bool
}
bs := newTestBufferService(80, 5, 100)
da := DefaultAttrData()
for range 10 {
bs.Scroll(&da, false)
}
// Scroll up first
bs.ScrollLines(-5, false)
// Then scroll back down to bottom
bs.ScrollLines(5, false)
got := Expectation{
YDisp: bs.Buffer().YDisp,
IsUserScrolling: bs.IsUserScrolling,
}
expected := Expectation{YDisp: 10, IsUserScrolling: false}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestBufferServiceScrollLinesAtTop(t *testing.T) {
t.Parallel()
type Expectation struct {
YDisp int
ScrollEvents int
}
bs := newTestBufferService(80, 5, 100)
// No scrollback, ydisp is already 0
scrollEvents := 0
bs.OnScrollEmitter.Event(func(int) { scrollEvents++ })
bs.ScrollLines(-1, false)
got := Expectation{YDisp: bs.Buffer().YDisp, ScrollEvents: scrollEvents}
expected := Expectation{YDisp: 0, ScrollEvents: 0}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestBufferServiceScrollLinesSuppressEvent(t *testing.T) {
t.Parallel()
type Expectation struct {
ScrollEvents int
YDisp int
}
bs := newTestBufferService(80, 5, 100)
da := DefaultAttrData()
for range 10 {
bs.Scroll(&da, false)
}
scrollEvents := 0
bs.OnScrollEmitter.Event(func(int) { scrollEvents++ })
bs.ScrollLines(-2, true)
got := Expectation{ScrollEvents: scrollEvents, YDisp: bs.Buffer().YDisp}
expected := Expectation{ScrollEvents: 0, YDisp: 8}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestBufferServiceScrollLinesClampToZero(t *testing.T) {
t.Parallel()
type Expectation struct {
YDisp int
}
bs := newTestBufferService(80, 5, 100)
da := DefaultAttrData()
for range 3 {
bs.Scroll(&da, false)
}
// Try to scroll way past the top
bs.ScrollLines(-100, false)
got := Expectation{YDisp: bs.Buffer().YDisp}
expected := Expectation{YDisp: 0}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestBufferServiceDispose(t *testing.T) {
t.Parallel()
type Expectation struct {
ResizeEvents int
ScrollEvents int
}
bs := newTestBufferService(80, 24, 1000)
resizeCount := 0
scrollCount := 0
bs.OnResizeEmitter.Event(func(BufferResizeEvent) { resizeCount++ })
bs.OnScrollEmitter.Event(func(int) { scrollCount++ })
bs.Dispose()
bs.Resize(120, 40)
got := Expectation{ResizeEvents: resizeCount, ScrollEvents: scrollCount}
expected := Expectation{ResizeEvents: 0, ScrollEvents: 0}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestBufferServiceBufferActivateFiresScroll(t *testing.T) {
t.Parallel()
type Expectation struct {
ScrollFired bool
}
bs := newTestBufferService(80, 24, 1000)
scrollFired := false
bs.OnScrollEmitter.Event(func(int) { scrollFired = true })
// Switching to alt buffer should fire scroll event
bs.Buffers.ActivateAltBuffer(nil)
got := Expectation{ScrollFired: scrollFired}
expected := Expectation{ScrollFired: true}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}