-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuffer_test.go
More file actions
384 lines (307 loc) · 8.06 KB
/
Copy pathbuffer_test.go
File metadata and controls
384 lines (307 loc) · 8.06 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
package headlessterm
import (
"testing"
)
func TestNewBuffer(t *testing.T) {
b := NewBuffer(24, 80)
if b.Rows() != 24 {
t.Errorf("expected 24 rows, got %d", b.Rows())
}
if b.Cols() != 80 {
t.Errorf("expected 80 cols, got %d", b.Cols())
}
}
func TestBufferCell(t *testing.T) {
b := NewBuffer(24, 80)
cell := b.Cell(0, 0)
if cell == nil {
t.Fatal("expected cell at (0,0)")
}
cell.Char = 'A'
retrieved := b.Cell(0, 0)
if retrieved.Char != 'A' {
t.Errorf("expected 'A', got '%c'", retrieved.Char)
}
}
func TestBufferCellOutOfBounds(t *testing.T) {
b := NewBuffer(24, 80)
if b.Cell(-1, 0) != nil {
t.Error("expected nil for negative row")
}
if b.Cell(0, -1) != nil {
t.Error("expected nil for negative col")
}
if b.Cell(24, 0) != nil {
t.Error("expected nil for row >= rows")
}
if b.Cell(0, 80) != nil {
t.Error("expected nil for col >= cols")
}
}
func TestBufferClearRow(t *testing.T) {
b := NewBuffer(24, 80)
b.Cell(0, 0).Char = 'A'
b.Cell(0, 1).Char = 'B'
b.ClearRow(0)
if b.Cell(0, 0).Char != ' ' {
t.Error("expected cell to be cleared")
}
if b.Cell(0, 1).Char != ' ' {
t.Error("expected cell to be cleared")
}
}
func TestBufferScrollUp(t *testing.T) {
b := NewBuffer(5, 10)
for row := 0; row < 5; row++ {
b.Cell(row, 0).Char = rune('0' + row)
}
b.ScrollUp(0, 5, 1)
// Row 0 should now have what was in row 1
if b.Cell(0, 0).Char != '1' {
t.Errorf("expected '1', got '%c'", b.Cell(0, 0).Char)
}
// Last row should be cleared
if b.Cell(4, 0).Char != ' ' {
t.Errorf("expected space, got '%c'", b.Cell(4, 0).Char)
}
}
func TestBufferScrollDown(t *testing.T) {
b := NewBuffer(5, 10)
for row := 0; row < 5; row++ {
b.Cell(row, 0).Char = rune('0' + row)
}
b.ScrollDown(0, 5, 1)
// Row 1 should now have what was in row 0
if b.Cell(1, 0).Char != '0' {
t.Errorf("expected '0', got '%c'", b.Cell(1, 0).Char)
}
// First row should be cleared
if b.Cell(0, 0).Char != ' ' {
t.Errorf("expected space, got '%c'", b.Cell(0, 0).Char)
}
}
func TestBufferScrollback(t *testing.T) {
storage := &testScrollbackBuffer{lines: make([][]Cell, 0), maxLines: 100}
b := NewBufferWithStorage(5, 10, storage)
for row := 0; row < 5; row++ {
b.Cell(row, 0).Char = rune('A' + row)
}
// Scroll up, line 0 should go to scrollback
b.ScrollUp(0, 5, 1)
if b.ScrollbackLen() != 1 {
t.Errorf("expected 1 scrollback line, got %d", b.ScrollbackLen())
}
line := b.ScrollbackLine(0)
if line == nil {
t.Fatal("expected scrollback line")
}
if line[0].Char != 'A' {
t.Errorf("expected 'A' in scrollback, got '%c'", line[0].Char)
}
}
// testScrollbackBuffer is a test implementation of ScrollbackProvider
type testScrollbackBuffer struct {
lines [][]Cell
maxLines int
}
func (s *testScrollbackBuffer) Push(line []Cell) {
lineCopy := make([]Cell, len(line))
copy(lineCopy, line)
s.lines = append(s.lines, lineCopy)
if s.maxLines > 0 && len(s.lines) > s.maxLines {
s.lines = s.lines[len(s.lines)-s.maxLines:]
}
}
func (s *testScrollbackBuffer) Len() int { return len(s.lines) }
func (s *testScrollbackBuffer) Line(index int) []Cell { return s.lines[index] }
func (s *testScrollbackBuffer) Clear() { s.lines = make([][]Cell, 0) }
func (s *testScrollbackBuffer) SetMaxLines(max int) { s.maxLines = max }
func (s *testScrollbackBuffer) MaxLines() int { return s.maxLines }
func (s *testScrollbackBuffer) Pop() []Cell {
if len(s.lines) == 0 {
return nil
}
line := s.lines[len(s.lines)-1]
s.lines = s.lines[:len(s.lines)-1]
return line
}
func TestBufferLineContent(t *testing.T) {
b := NewBuffer(24, 80)
b.Cell(0, 0).Char = 'H'
b.Cell(0, 1).Char = 'e'
b.Cell(0, 2).Char = 'l'
b.Cell(0, 3).Char = 'l'
b.Cell(0, 4).Char = 'o'
content := b.LineContent(0)
if content != "Hello" {
t.Errorf("expected 'Hello', got '%s'", content)
}
}
func TestBufferTabStops(t *testing.T) {
b := NewBuffer(24, 80)
// Default tab stops at 0, 8, 16, etc.
next := b.NextTabStop(0)
if next != 8 {
t.Errorf("expected next tab at 8, got %d", next)
}
next = b.NextTabStop(8)
if next != 16 {
t.Errorf("expected next tab at 16, got %d", next)
}
prev := b.PrevTabStop(16)
if prev != 8 {
t.Errorf("expected prev tab at 8, got %d", prev)
}
}
func TestBufferResize(t *testing.T) {
b := NewBuffer(10, 20)
b.Cell(0, 0).Char = 'A'
b.Cell(5, 10).Char = 'B'
b.Resize(20, 40)
if b.Rows() != 20 || b.Cols() != 40 {
t.Errorf("expected 20x40, got %dx%d", b.Rows(), b.Cols())
}
// Content should be preserved
if b.Cell(0, 0).Char != 'A' {
t.Error("expected content to be preserved")
}
if b.Cell(5, 10).Char != 'B' {
t.Error("expected content to be preserved")
}
}
func TestBufferDirtyTracking(t *testing.T) {
b := NewBuffer(24, 80)
b.ClearAllDirty()
if b.HasDirty() {
t.Error("expected no dirty cells")
}
b.MarkDirty(0, 0)
if !b.HasDirty() {
t.Error("expected dirty cells")
}
dirty := b.DirtyCells()
if len(dirty) != 1 {
t.Errorf("expected 1 dirty cell, got %d", len(dirty))
}
if dirty[0].Row != 0 || dirty[0].Col != 0 {
t.Error("expected dirty cell at (0,0)")
}
}
func TestBufferInsertBlanks(t *testing.T) {
b := NewBuffer(24, 80)
b.Cell(0, 0).Char = 'A'
b.Cell(0, 1).Char = 'B'
b.Cell(0, 2).Char = 'C'
b.InsertBlanks(0, 1, 2)
if b.Cell(0, 0).Char != 'A' {
t.Errorf("expected 'A', got '%c'", b.Cell(0, 0).Char)
}
if b.Cell(0, 1).Char != ' ' {
t.Errorf("expected space, got '%c'", b.Cell(0, 1).Char)
}
if b.Cell(0, 2).Char != ' ' {
t.Errorf("expected space, got '%c'", b.Cell(0, 2).Char)
}
if b.Cell(0, 3).Char != 'B' {
t.Errorf("expected 'B', got '%c'", b.Cell(0, 3).Char)
}
}
func TestBufferDeleteChars(t *testing.T) {
b := NewBuffer(24, 80)
b.Cell(0, 0).Char = 'A'
b.Cell(0, 1).Char = 'B'
b.Cell(0, 2).Char = 'C'
b.Cell(0, 3).Char = 'D'
b.DeleteChars(0, 1, 2)
if b.Cell(0, 0).Char != 'A' {
t.Errorf("expected 'A', got '%c'", b.Cell(0, 0).Char)
}
if b.Cell(0, 1).Char != 'D' {
t.Errorf("expected 'D', got '%c'", b.Cell(0, 1).Char)
}
}
func TestBufferWrappedLineTracking(t *testing.T) {
b := NewBuffer(5, 10)
// Initially no lines are wrapped
if b.IsWrapped(0) {
t.Error("expected line 0 not wrapped initially")
}
// Set wrapped
b.SetWrapped(0, true)
if !b.IsWrapped(0) {
t.Error("expected line 0 to be wrapped")
}
// Clear wrapped
b.SetWrapped(0, false)
if b.IsWrapped(0) {
t.Error("expected line 0 not wrapped after clear")
}
// Out of bounds should not panic
b.SetWrapped(-1, true)
b.SetWrapped(100, true)
if b.IsWrapped(-1) {
t.Error("expected false for out of bounds")
}
if b.IsWrapped(100) {
t.Error("expected false for out of bounds")
}
}
func TestBufferWrappedLineTrackingWithScroll(t *testing.T) {
b := NewBuffer(5, 10)
// Set some wrapped flags
b.SetWrapped(0, true)
b.SetWrapped(1, false)
b.SetWrapped(2, true)
// Scroll up
b.ScrollUp(0, 5, 1)
// Wrapped flags should move with lines
if b.IsWrapped(0) != false { // was line 1
t.Error("expected line 0 not wrapped after scroll")
}
if b.IsWrapped(1) != true { // was line 2
t.Error("expected line 1 wrapped after scroll")
}
if b.IsWrapped(4) { // new line should not be wrapped
t.Error("expected new line not wrapped")
}
}
func TestBufferGrowRows(t *testing.T) {
b := NewBuffer(5, 10)
b.Cell(0, 0).Char = 'A'
b.Cell(4, 0).Char = 'E'
b.GrowRows(3)
if b.Rows() != 8 {
t.Errorf("expected 8 rows, got %d", b.Rows())
}
// Content should be preserved
if b.Cell(0, 0).Char != 'A' {
t.Error("expected content preserved")
}
if b.Cell(4, 0).Char != 'E' {
t.Error("expected content preserved")
}
// New rows should be empty
if b.Cell(7, 0).Char != ' ' {
t.Error("expected new row to be empty")
}
}
func TestBufferGrowCols(t *testing.T) {
b := NewBuffer(5, 10)
b.Cell(0, 0).Char = 'A'
b.Cell(0, 9).Char = 'B'
b.GrowCols(0, 20)
if b.Cols() != 20 {
t.Errorf("expected 20 cols, got %d", b.Cols())
}
// Content should be preserved
if b.Cell(0, 0).Char != 'A' {
t.Error("expected content preserved")
}
if b.Cell(0, 9).Char != 'B' {
t.Error("expected content preserved")
}
// New cells should be empty
if b.Cell(0, 15).Char != ' ' {
t.Error("expected new cell to be empty")
}
}