-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbufferline_test.go
More file actions
443 lines (422 loc) · 11.7 KB
/
bufferline_test.go
File metadata and controls
443 lines (422 loc) · 11.7 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
package xterm
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func nullCell() *CellData {
return CellDataFromCharData(NewCharData(0, NullCellChar, NullCellWidth, NullCellCode))
}
func charCell(ch rune, width int) *CellData {
return CellDataFromCharData(NewCharData(0, string(ch), width, uint32(ch)))
}
func TestBufferLineNewAndLength(t *testing.T) {
t.Parallel()
type Expectation struct {
Len int
IsWrapped bool
}
tests := []struct {
Name string
Cols int
IsWrapped bool
Expected Expectation
}{
{"10 cols not wrapped", 10, false, Expectation{10, false}},
{"5 cols wrapped", 5, true, Expectation{5, true}},
{"0 cols", 0, false, Expectation{0, false}},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
bl := NewBufferLine(tc.Cols, nil, tc.IsWrapped)
got := Expectation{bl.Len, bl.IsWrapped}
if diff := cmp.Diff(tc.Expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
})
}
}
func TestBufferLineSetCellFromCodepoint(t *testing.T) {
t.Parallel()
type Expectation struct {
Char string
Width int
Fg uint32
Bg uint32
}
tests := []struct {
Name string
CodePoint uint32
Width int
Fg uint32
Bg uint32
Expected Expectation
}{
{"ASCII char", 'A', 1, 0, 0, Expectation{"A", 1, 0, 0}},
{"wide char", 0x4E16, 2, 1, 2, Expectation{"世", 2, 1, 2}}, //nolint:gosmopolitan
{"null codepoint", 0, 1, 0, 0, Expectation{"", 1, 0, 0}},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
bl := NewBufferLine(5, nil, false)
attrs := &AttributeData{Fg: tc.Fg, Bg: tc.Bg, Extended: &ExtendedAttrs{}}
bl.SetCellFromCodepoint(0, tc.CodePoint, tc.Width, attrs)
got := Expectation{
Char: bl.GetString(0),
Width: bl.GetWidth(0),
Fg: bl.GetFg(0),
Bg: bl.GetBg(0),
}
if diff := cmp.Diff(tc.Expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
})
}
}
func TestBufferLineAddCodepointToCell(t *testing.T) {
t.Parallel()
type Expectation struct {
Char string
IsCombined bool
}
tests := []struct {
Name string
Base rune
Combine rune
Expected Expectation
}{
{"combining accent", 'e', 0x0301, Expectation{"e\u0301", true}},
{"add to empty cell", 0, 'A', Expectation{"A", false}},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
bl := NewBufferLine(5, nil, false)
if tc.Base != 0 {
attrs := &AttributeData{Extended: &ExtendedAttrs{}}
bl.SetCellFromCodepoint(0, uint32(tc.Base), 1, attrs)
}
bl.AddCodepointToCell(0, uint32(tc.Combine), 0)
got := Expectation{
Char: bl.GetString(0),
IsCombined: bl.IsCombined(0) != 0,
}
if diff := cmp.Diff(tc.Expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
})
}
}
func TestBufferLineAddMultipleCombining(t *testing.T) {
t.Parallel()
type Expectation struct {
Char string
}
bl := NewBufferLine(5, nil, false)
attrs := &AttributeData{Extended: &ExtendedAttrs{}}
bl.SetCellFromCodepoint(0, 'e', 1, attrs)
bl.AddCodepointToCell(0, 0x0301, 0) // combining acute
bl.AddCodepointToCell(0, 0x0327, 0) // combining cedilla
got := Expectation{Char: bl.GetString(0)}
expected := Expectation{Char: "e\u0301\u0327"}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestBufferLineLoadCell(t *testing.T) {
t.Parallel()
type Expectation struct {
Content uint32
Fg uint32
Bg uint32
}
bl := NewBufferLine(5, nil, false)
attrs := &AttributeData{Fg: 100, Bg: 200, Extended: &ExtendedAttrs{}}
bl.SetCellFromCodepoint(2, 'X', 1, attrs)
cell := NewCellData()
bl.LoadCell(2, cell)
got := Expectation{Content: cell.Content, Fg: cell.Fg, Bg: cell.Bg}
expected := Expectation{
Content: uint32('X') | (1 << ContentWidthShift),
Fg: 100,
Bg: 200,
}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestBufferLineInsertCells(t *testing.T) {
t.Parallel()
type Expectation struct {
Chars string
}
bl := NewBufferLine(5, nil, false)
attrs := &AttributeData{Extended: &ExtendedAttrs{}}
for i, ch := range []rune{'A', 'B', 'C', 'D', 'E'} {
bl.SetCellFromCodepoint(i, uint32(ch), 1, attrs)
}
fill := nullCell()
bl.InsertCells(1, 2, fill)
result := bl.TranslateToString(true, 0, -1)
got := Expectation{Chars: result}
expected := Expectation{Chars: "A BC"}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestBufferLineDeleteCells(t *testing.T) {
t.Parallel()
type Expectation struct {
Chars string
}
bl := NewBufferLine(5, nil, false)
attrs := &AttributeData{Extended: &ExtendedAttrs{}}
for i, ch := range []rune{'A', 'B', 'C', 'D', 'E'} {
bl.SetCellFromCodepoint(i, uint32(ch), 1, attrs)
}
fill := nullCell()
bl.DeleteCells(1, 2, fill)
result := bl.TranslateToString(false, 0, -1)
got := Expectation{Chars: result}
expected := Expectation{Chars: "ADE "}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestBufferLineReplaceCells(t *testing.T) {
t.Parallel()
type Expectation struct {
Chars string
}
bl := NewBufferLine(5, nil, false)
attrs := &AttributeData{Extended: &ExtendedAttrs{}}
for i, ch := range []rune{'A', 'B', 'C', 'D', 'E'} {
bl.SetCellFromCodepoint(i, uint32(ch), 1, attrs)
}
fill := charCell(' ', 1)
bl.ReplaceCells(1, 4, fill, false)
result := bl.TranslateToString(false, 0, -1)
got := Expectation{Chars: result}
expected := Expectation{Chars: "A E"}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestBufferLineResize(t *testing.T) {
t.Parallel()
type Expectation struct {
Len int
Chars string
}
tests := []struct {
Name string
InitCols int
NewCols int
Expected Expectation
}{
{"grow", 3, 5, Expectation{5, "ABC "}},
{"shrink", 5, 3, Expectation{3, "ABC"}},
{"same", 5, 5, Expectation{5, "ABCDE"}},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
bl := NewBufferLine(tc.InitCols, nil, false)
attrs := &AttributeData{Extended: &ExtendedAttrs{}}
chars := []rune("ABCDE")
for i := 0; i < tc.InitCols && i < len(chars); i++ {
bl.SetCellFromCodepoint(i, uint32(chars[i]), 1, attrs)
}
fill := charCell(' ', 1)
bl.Resize(tc.NewCols, fill)
result := bl.TranslateToString(false, 0, -1)
got := Expectation{Len: bl.Len, Chars: result}
if diff := cmp.Diff(tc.Expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
})
}
}
func TestBufferLineFill(t *testing.T) {
t.Parallel()
type Expectation struct {
Chars string
}
bl := NewBufferLine(5, nil, false)
attrs := &AttributeData{Extended: &ExtendedAttrs{}}
for i, ch := range []rune{'A', 'B', 'C', 'D', 'E'} {
bl.SetCellFromCodepoint(i, uint32(ch), 1, attrs)
}
fill := charCell('X', 1)
bl.Fill(fill, false)
result := bl.TranslateToString(false, 0, -1)
got := Expectation{Chars: result}
expected := Expectation{Chars: "XXXXX"}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestBufferLineCopyFrom(t *testing.T) {
t.Parallel()
type Expectation struct {
Chars string
IsWrapped bool
}
src := NewBufferLine(5, nil, true)
attrs := &AttributeData{Extended: &ExtendedAttrs{}}
for i, ch := range []rune{'H', 'E', 'L', 'L', 'O'} {
src.SetCellFromCodepoint(i, uint32(ch), 1, attrs)
}
dst := NewBufferLine(5, nil, false)
dst.CopyFrom(src)
got := Expectation{
Chars: dst.TranslateToString(false, 0, -1),
IsWrapped: dst.IsWrapped,
}
expected := Expectation{Chars: "HELLO", IsWrapped: true}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestBufferLineClone(t *testing.T) {
t.Parallel()
type Expectation struct {
Chars string
IsWrapped bool
Independent bool
}
orig := NewBufferLine(3, nil, true)
attrs := &AttributeData{Extended: &ExtendedAttrs{}}
orig.SetCellFromCodepoint(0, 'A', 1, attrs)
orig.SetCellFromCodepoint(1, 'B', 1, attrs)
orig.SetCellFromCodepoint(2, 'C', 1, attrs)
clone := orig.Clone()
// Modify original to verify independence
orig.SetCellFromCodepoint(0, 'Z', 1, attrs)
got := Expectation{
Chars: clone.TranslateToString(false, 0, -1),
IsWrapped: clone.IsWrapped,
Independent: clone.GetString(0) != orig.GetString(0),
}
expected := Expectation{Chars: "ABC", IsWrapped: true, Independent: true}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestBufferLineGetTrimmedLength(t *testing.T) {
t.Parallel()
type Expectation struct {
TrimmedLen int
}
tests := []struct {
Name string
Content string
Expected Expectation
}{
{"full line", "ABCDE", Expectation{5}},
{"trailing empty", "AB", Expectation{2}},
{"all empty", "", Expectation{0}},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
bl := NewBufferLine(5, nil, false)
attrs := &AttributeData{Extended: &ExtendedAttrs{}}
for i, ch := range tc.Content {
bl.SetCellFromCodepoint(i, uint32(ch), 1, attrs)
}
got := Expectation{TrimmedLen: bl.GetTrimmedLength()}
if diff := cmp.Diff(tc.Expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
})
}
}
func TestBufferLineTranslateToString(t *testing.T) {
t.Parallel()
type Expectation struct {
Result string
}
tests := []struct {
Name string
Content string
TrimRight bool
StartCol int
EndCol int
Expected Expectation
}{
{"full line", "HELLO", false, 0, -1, Expectation{"HELLO"}},
{"substring", "HELLO", false, 1, 4, Expectation{"ELL"}},
{"trim right", "HI", true, 0, -1, Expectation{"HI"}},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
bl := NewBufferLine(5, nil, false)
attrs := &AttributeData{Extended: &ExtendedAttrs{}}
for i, ch := range tc.Content {
bl.SetCellFromCodepoint(i, uint32(ch), 1, attrs)
}
result := bl.TranslateToString(tc.TrimRight, tc.StartCol, tc.EndCol)
got := Expectation{Result: result}
if diff := cmp.Diff(tc.Expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
})
}
}
func TestBufferLineGetSet(t *testing.T) {
t.Parallel()
type Expectation struct {
Char string
Width int
Code uint32
}
bl := NewBufferLine(5, nil, false)
cd := NewCharData(42, "A", 1, 65)
bl.Set(0, cd)
result := bl.Get(0)
got := Expectation{
Char: CharDataChar(result),
Width: CharDataWidth(result),
Code: CharDataCode(result),
}
expected := Expectation{Char: "A", Width: 1, Code: 65}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestBufferLineCopyCellsFrom(t *testing.T) {
t.Parallel()
type Expectation struct {
Chars string
}
src := NewBufferLine(5, nil, false)
dst := NewBufferLine(5, nil, false)
attrs := &AttributeData{Extended: &ExtendedAttrs{}}
for i, ch := range []rune{'A', 'B', 'C', 'D', 'E'} {
src.SetCellFromCodepoint(i, uint32(ch), 1, attrs)
}
dst.CopyCellsFrom(src, 1, 2, 2, false)
got := Expectation{Chars: dst.GetString(2) + dst.GetString(3)}
expected := Expectation{Chars: "BC"}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestBufferLineWideCharTrimmedLength(t *testing.T) {
t.Parallel()
type Expectation struct {
TrimmedLen int
}
bl := NewBufferLine(5, nil, false)
attrs := &AttributeData{Extended: &ExtendedAttrs{}}
bl.SetCellFromCodepoint(0, 0x4E16, 2, attrs) // '世' width=2
bl.SetCellFromCodepoint(1, 0, 0, attrs) // second cell of wide char
got := Expectation{TrimmedLen: bl.GetTrimmedLength()}
expected := Expectation{TrimmedLen: 2}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}