-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbufferreflow_test.go
More file actions
287 lines (277 loc) · 7.16 KB
/
bufferreflow_test.go
File metadata and controls
287 lines (277 loc) · 7.16 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
package xterm
import (
"testing"
"github.com/google/go-cmp/cmp"
)
// helper: create a CircularList of BufferLines from strings, marking continuations as wrapped.
func makeLines(cols int, contents []string, wrappedIndices map[int]bool) *CircularList[*BufferLine] {
cl := NewCircularList[*BufferLine](len(contents) + 10)
attrs := &AttributeData{Extended: &ExtendedAttrs{}}
for idx, s := range contents {
bl := NewBufferLine(cols, nil, wrappedIndices[idx])
for i, ch := range s {
if i < cols {
bl.SetCellFromCodepoint(i, uint32(ch), 1, attrs)
}
}
cl.Push(bl)
}
return cl
}
func TestGetWrappedLineTrimmedLength(t *testing.T) {
t.Parallel()
type Expectation struct {
Length int
}
tests := []struct {
Name string
Lines []string
Index int
Cols int
Expected Expectation
}{
{
Name: "last line trimmed",
Lines: []string{"ABCDE", "FG"},
Index: 1,
Cols: 5,
Expected: Expectation{Length: 2},
},
{
Name: "non-last line full",
Lines: []string{"ABCDE", "FG"},
Index: 0,
Cols: 5,
Expected: Expectation{Length: 5},
},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
lines := make([]*BufferLine, len(tc.Lines))
attrs := &AttributeData{Extended: &ExtendedAttrs{}}
for i, s := range tc.Lines {
bl := NewBufferLine(tc.Cols, nil, i > 0)
for j, ch := range s {
if j < tc.Cols {
bl.SetCellFromCodepoint(j, uint32(ch), 1, attrs)
}
}
lines[i] = bl
}
got := Expectation{Length: getWrappedLineTrimmedLength(lines, tc.Index, tc.Cols)}
if diff := cmp.Diff(tc.Expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
})
}
}
func TestReflowSmallerGetNewLineLengths(t *testing.T) {
t.Parallel()
type Expectation struct {
Lengths []int
}
tests := []struct {
Name string
Content []string
OldCols int
NewCols int
Expected Expectation
}{
{
Name: "simple split",
Content: []string{"ABCDEFGH"},
OldCols: 10,
NewCols: 5,
Expected: Expectation{Lengths: []int{5, 3}},
},
{
Name: "exact fit",
Content: []string{"ABCDE"},
OldCols: 10,
NewCols: 5,
Expected: Expectation{Lengths: []int{5}},
},
{
Name: "three lines",
Content: []string{"ABCDEFGHIJKLM"},
OldCols: 15,
NewCols: 5,
Expected: Expectation{Lengths: []int{5, 5, 3}},
},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
attrs := &AttributeData{Extended: &ExtendedAttrs{}}
lines := make([]*BufferLine, len(tc.Content))
for i, s := range tc.Content {
bl := NewBufferLine(tc.OldCols, nil, i > 0)
for j, ch := range s {
if j < tc.OldCols {
bl.SetCellFromCodepoint(j, uint32(ch), 1, attrs)
}
}
lines[i] = bl
}
result := reflowSmallerGetNewLineLengths(lines, tc.OldCols, tc.NewCols)
got := Expectation{Lengths: result}
if diff := cmp.Diff(tc.Expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
})
}
}
func TestReflowLargerGetLinesToRemove(t *testing.T) {
t.Parallel()
type Expectation struct {
ToRemoveLen int
}
tests := []struct {
Name string
Contents []string
Wrapped map[int]bool
OldCols int
NewCols int
Expected Expectation
}{
{
Name: "no wrapped lines",
Contents: []string{"ABC", "DEF", "GHI"},
Wrapped: map[int]bool{},
OldCols: 5,
NewCols: 10,
Expected: Expectation{ToRemoveLen: 0},
},
{
Name: "wrapped line can merge",
Contents: []string{"ABCDE", "FG", "XYZ"},
Wrapped: map[int]bool{1: true},
OldCols: 5,
NewCols: 10,
Expected: Expectation{ToRemoveLen: 2}, // [startIdx, count] pair
},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
// Lines must be pre-resized to newCols (as Buffer.Resize does before calling reflow)
cl := makeLines(tc.NewCols, tc.Contents, tc.Wrapped)
nc := nullCell()
// Place cursor on last line so it doesn't interfere with reflow
result := reflowLargerGetLinesToRemove(cl, tc.OldCols, tc.NewCols, cl.Length()-1, nc, false)
got := Expectation{ToRemoveLen: len(result)}
if diff := cmp.Diff(tc.Expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
})
}
}
func TestReflowLargerCreateNewLayout(t *testing.T) {
t.Parallel()
type Expectation struct {
LayoutLen int
CountRemoved int
}
cl := makeLines(5, []string{"ABCDE", "FG", "HIJKL"}, map[int]bool{1: true})
// toRemove: [startIndex=0, count=1] means remove 1 line starting at index 0
toRemove := []int{1, 1}
result := reflowLargerCreateNewLayout(cl, toRemove)
got := Expectation{
LayoutLen: len(result.layout),
CountRemoved: result.countRemoved,
}
expected := Expectation{LayoutLen: 2, CountRemoved: 1}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestReflowLargerApplyNewLayout(t *testing.T) {
t.Parallel()
type Expectation struct {
Length int
}
cl := makeLines(5, []string{"ABCDE", "FG", "HIJKL"}, map[int]bool{1: true})
layout := []int{0, 2} // keep lines 0 and 2, remove line 1
reflowLargerApplyNewLayout(cl, layout)
got := Expectation{Length: cl.Length()}
expected := Expectation{Length: 2}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestReflowIntegrationLarger(t *testing.T) {
t.Parallel()
type Expectation struct {
LineCount int
Line0 string
}
// Create a buffer with wrapped content, then resize larger
b := NewBuffer(BufferOptions{
Cols: 5,
Rows: 5,
Scrollback: 100,
TabStopWidth: 8,
HasScrollback: true,
})
b.FillViewportRows(nil)
// Write "ABCDEFGH" across two lines (wrapped)
attrs := &AttributeData{Extended: &ExtendedAttrs{}}
line0 := b.Lines.Get(0)
for i, ch := range []rune("ABCDE") {
line0.SetCellFromCodepoint(i, uint32(ch), 1, attrs)
}
line1 := b.Lines.Get(1)
line1.IsWrapped = true
for i, ch := range []rune("FGH") {
line1.SetCellFromCodepoint(i, uint32(ch), 1, attrs)
}
// Move cursor below the wrapped group so reflow can process it
b.Y = 4
b.Resize(10, 5)
got := Expectation{
LineCount: b.Lines.Length(),
Line0: b.Lines.Get(0).TranslateToString(true, 0, -1),
}
expected := Expectation{
LineCount: 5,
Line0: "ABCDEFGH",
}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestReflowIntegrationSmaller(t *testing.T) {
t.Parallel()
type Expectation struct {
Line0Content string
Line1Wrapped bool
}
b := NewBuffer(BufferOptions{
Cols: 10,
Rows: 5,
Scrollback: 100,
TabStopWidth: 8,
HasScrollback: true,
})
b.FillViewportRows(nil)
attrs := &AttributeData{Extended: &ExtendedAttrs{}}
line0 := b.Lines.Get(0)
for i, ch := range []rune("ABCDEFGH") {
line0.SetCellFromCodepoint(i, uint32(ch), 1, attrs)
}
// Move cursor below the content line so reflow can process it
b.Y = 4
b.Resize(5, 5)
got := Expectation{
Line0Content: b.Lines.Get(0).TranslateToString(true, 0, -1),
Line1Wrapped: b.Lines.Get(1).IsWrapped,
}
expected := Expectation{
Line0Content: "ABCDE",
Line1Wrapped: true,
}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}