File: buffer_test.go

package info (click to toggle)
elvish 0.21.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,372 kB
  • sloc: javascript: 236; sh: 130; python: 104; makefile: 88; xml: 9
file content (349 lines) | stat: -rw-r--r-- 8,141 bytes parent folder | download | duplicates (2)
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
package term

import (
	"reflect"
	"testing"
)

var cellsWidthTests = []struct {
	cells     []Cell
	wantWidth int
}{
	{[]Cell{}, 0},
	{[]Cell{{"a", ""}, {"好", ""}}, 3},
}

func TestCellsWidth(t *testing.T) {
	for _, test := range cellsWidthTests {
		if width := CellsWidth(test.cells); width != test.wantWidth {
			t.Errorf("cellsWidth(%v) = %v, want %v",
				test.cells, width, test.wantWidth)
		}
	}
}

var makeSpacingTests = []struct {
	n    int
	want []Cell
}{
	{0, []Cell{}},
	{1, []Cell{{" ", ""}}},
	{4, []Cell{{" ", ""}, {" ", ""}, {" ", ""}, {" ", ""}}},
}

func TestMakeSpacing(t *testing.T) {
	for _, test := range makeSpacingTests {
		if got := makeSpacing(test.n); !reflect.DeepEqual(got, test.want) {
			t.Errorf("makeSpacing(%v) = %v, want %v", test.n, got, test.want)
		}
	}
}

var compareCellsTests = []struct {
	cells1    []Cell
	cells2    []Cell
	wantEqual bool
	wantIndex int
}{
	{[]Cell{}, []Cell{}, true, 0},
	{[]Cell{}, []Cell{{"a", ""}}, false, 0},
	{
		[]Cell{{"a", ""}, {"好", ""}, {"b", ""}},
		[]Cell{{"a", ""}, {"好", ""}, {"c", ""}},
		false, 2,
	},
	{
		[]Cell{{"a", ""}, {"好", ""}, {"b", ""}},
		[]Cell{{"a", ""}, {"好", "1"}, {"c", ""}},
		false, 1,
	},
}

func TestCompareCells(t *testing.T) {
	for _, test := range compareCellsTests {
		equal, index := CompareCells(test.cells1, test.cells2)
		if equal != test.wantEqual || index != test.wantIndex {
			t.Errorf("compareCells(%v, %v) = (%v, %v), want (%v, %v)",
				test.cells1, test.cells2,
				equal, index, test.wantEqual, test.wantIndex)
		}
	}
}

var bufferCursorTests = []struct {
	buf  *Buffer
	want Pos
}{
	{
		&Buffer{Width: 10, Lines: Lines{Line{}}},
		Pos{0, 0},
	},
	{
		&Buffer{Width: 10, Lines: Lines{Line{Cell{"a", ""}}, Line{Cell{"好", ""}}}},
		Pos{1, 2},
	},
}

func TestBufferCursor(t *testing.T) {
	for _, test := range bufferCursorTests {
		if p := test.buf.Cursor(); p != test.want {
			t.Errorf("(%v).cursor() = %v, want %v", test.buf, p, test.want)
		}
	}
}

var buffersHeighTests = []struct {
	buffers []*Buffer
	want    int
}{
	{nil, 0},
	{[]*Buffer{NewBuffer(10)}, 1},
	{
		[]*Buffer{
			{Width: 10, Lines: Lines{Line{}, Line{}}},
			{Width: 10, Lines: Lines{Line{}}},
			{Width: 10, Lines: Lines{Line{}, Line{}}},
		},
		5,
	},
}

func TestBuffersHeight(t *testing.T) {
	for _, test := range buffersHeighTests {
		if h := BuffersHeight(test.buffers...); h != test.want {
			t.Errorf("buffersHeight(%v...) = %v, want %v",
				test.buffers, h, test.want)
		}
	}
}

var bufferTrimToLinesTests = []struct {
	buf  *Buffer
	low  int
	high int
	want *Buffer
}{
	{
		&Buffer{Width: 10, Lines: Lines{
			Line{Cell{"a", ""}}, Line{Cell{"b", ""}}, Line{Cell{"c", ""}}, Line{Cell{"d", ""}},
		}},
		0, 2,
		&Buffer{Width: 10, Lines: Lines{
			Line{Cell{"a", ""}}, Line{Cell{"b", ""}},
		}},
	},
	// Negative low is treated as 0.

	{
		&Buffer{Width: 10, Lines: Lines{
			Line{Cell{"a", ""}}, Line{Cell{"b", ""}}, Line{Cell{"c", ""}}, Line{Cell{"d", ""}},
		}},
		-1, 2,
		&Buffer{Width: 10, Lines: Lines{
			Line{Cell{"a", ""}}, Line{Cell{"b", ""}},
		}},
	},
	// With dot.
	{
		&Buffer{Width: 10, Lines: Lines{
			Line{Cell{"a", ""}}, Line{Cell{"b", ""}}, Line{Cell{"c", ""}}, Line{Cell{"d", ""}},
		}, Dot: Pos{1, 1}},
		1, 3,
		&Buffer{Width: 10, Lines: Lines{
			Line{Cell{"b", ""}}, Line{Cell{"c", ""}},
		}, Dot: Pos{0, 1}},
	},
	// With dot that is going to be trimmed away.
	{
		&Buffer{Width: 10, Lines: Lines{
			Line{Cell{"a", ""}}, Line{Cell{"b", ""}}, Line{Cell{"c", ""}}, Line{Cell{"d", ""}},
		}, Dot: Pos{0, 1}},
		1, 3,
		&Buffer{Width: 10, Lines: Lines{
			Line{Cell{"b", ""}}, Line{Cell{"c", ""}},
		}, Dot: Pos{0, 1}},
	},
}

func TestBufferTrimToLines(t *testing.T) {
	for _, test := range bufferTrimToLinesTests {
		b := cloneBuffer(test.buf)
		b.TrimToLines(test.low, test.high)
		if !reflect.DeepEqual(b, test.want) {
			t.Errorf("buf.trimToLines(%v, %v) makes it %v, want %v",
				test.low, test.high, b, test.want)
		}
	}
}

var bufferExtendTests = []struct {
	buf     *Buffer
	buf2    *Buffer
	moveDot bool
	want    *Buffer
}{
	{
		&Buffer{Width: 10, Lines: Lines{
			Line{Cell{"a", ""}}, Line{Cell{"b", ""}}}},
		&Buffer{Width: 11, Lines: Lines{
			Line{Cell{"c", ""}}, Line{Cell{"d", ""}}}},
		false,
		&Buffer{Width: 10, Lines: Lines{
			Line{Cell{"a", ""}}, Line{Cell{"b", ""}},
			Line{Cell{"c", ""}}, Line{Cell{"d", ""}}}},
	},
	// Moving dot.
	{
		&Buffer{Width: 10, Lines: Lines{
			Line{Cell{"a", ""}}, Line{Cell{"b", ""}}}},
		&Buffer{
			Width: 11,
			Lines: Lines{Line{Cell{"c", ""}}, Line{Cell{"d", ""}}},
			Dot:   Pos{1, 1},
		},
		true,
		&Buffer{
			Width: 10,
			Lines: Lines{
				Line{Cell{"a", ""}}, Line{Cell{"b", ""}},
				Line{Cell{"c", ""}}, Line{Cell{"d", ""}}},
			Dot: Pos{3, 1},
		},
	},
}

func TestBufferExtend(t *testing.T) {
	for _, test := range bufferExtendTests {
		buf := cloneBuffer(test.buf)
		buf.Extend(test.buf2, test.moveDot)
		if !reflect.DeepEqual(buf, test.want) {
			t.Errorf("buf.extend(%v, %v) makes it %v, want %v",
				test.buf2, test.moveDot, buf, test.want)
		}
	}
}

var bufferExtendRightTests = []struct {
	buf  *Buffer
	buf2 *Buffer
	want *Buffer
}{
	// No padding, equal height.
	{
		&Buffer{Width: 1, Lines: Lines{Line{Cell{"a", ""}}, Line{Cell{"b", ""}}}},
		&Buffer{Width: 1, Lines: Lines{Line{Cell{"c", ""}}, Line{Cell{"d", ""}}}},
		&Buffer{Width: 2, Lines: Lines{
			Line{Cell{"a", ""}, Cell{"c", ""}},
			Line{Cell{"b", ""}, Cell{"d", ""}}}},
	},
	// With padding, equal height.
	{
		&Buffer{Width: 2, Lines: Lines{Line{Cell{"a", ""}}, Line{Cell{"b", ""}}}},
		&Buffer{Width: 1, Lines: Lines{Line{Cell{"c", ""}}, Line{Cell{"d", ""}}}},
		&Buffer{Width: 3, Lines: Lines{
			Line{Cell{"a", ""}, Cell{" ", ""}, Cell{"c", ""}},
			Line{Cell{"b", ""}, Cell{" ", ""}, Cell{"d", ""}}}},
	},
	// buf is higher.
	{
		&Buffer{Width: 1, Lines: Lines{
			Line{Cell{"a", ""}}, Line{Cell{"b", ""}}, Line{Cell{"x", ""}}}},
		&Buffer{Width: 1, Lines: Lines{
			Line{Cell{"c", ""}}, Line{Cell{"d", ""}},
		}},
		&Buffer{Width: 2, Lines: Lines{
			Line{Cell{"a", ""}, Cell{"c", ""}},
			Line{Cell{"b", ""}, Cell{"d", ""}},
			Line{Cell{"x", ""}}}},
	},
	// buf2 is higher.
	{
		&Buffer{Width: 1, Lines: Lines{Line{Cell{"a", ""}}, Line{Cell{"b", ""}}}},
		&Buffer{Width: 1, Lines: Lines{
			Line{Cell{"c", ""}}, Line{Cell{"d", ""}}, Line{Cell{"e", ""}},
		}},
		&Buffer{Width: 2, Lines: Lines{
			Line{Cell{"a", ""}, Cell{"c", ""}},
			Line{Cell{"b", ""}, Cell{"d", ""}},
			Line{Cell{" ", ""}, Cell{"e", ""}}}},
	},
}

func TestBufferExtendRight(t *testing.T) {
	for _, test := range bufferExtendRightTests {
		buf := cloneBuffer(test.buf)
		buf.ExtendRight(test.buf2)
		if !reflect.DeepEqual(buf, test.want) {
			t.Errorf("buf.extendRight(%v) makes it %v, want %v",
				test.buf2, buf, test.want)
		}
	}
}

func TestBufferBuffer(t *testing.T) {
	b := NewBufferBuilder(4).Write("text").Buffer()
	if b.Buffer() != b {
		t.Errorf("Buffer did not return itself")
	}
}

var bufferTTYStringTests = []struct {
	buf  *Buffer
	want string
}{
	{
		nil,
		"nil",
	},
	{
		NewBufferBuilder(4).
			Write("ABCD").
			Newline().
			Write("XY").
			Buffer(),
		"Width = 4, Dot = (0, 0)\n" +
			"┌────┐\n" +
			"│ABCD│\n" +
			"│XY$ │\n" +
			"└────┘\n",
	},
	{
		NewBufferBuilder(4).
			Write("A").SetDotHere().
			WriteStringSGR("B", "1").
			WriteStringSGR("C", "7").
			Write("D").
			Newline().
			WriteStringSGR("XY", "7").
			Buffer(),
		"Width = 4, Dot = (0, 1)\n" +
			"┌────┐\n" +
			"│A\033[1mB\033[;7mC\033[mD│\n" +
			"│\033[7mXY\033[m$ │\n" +
			"└────┘\n",
	},
}

func TestBufferTTYString(t *testing.T) {
	for _, test := range bufferTTYStringTests {
		ttyString := test.buf.TTYString()
		if ttyString != test.want {
			t.Errorf("TTYString -> %q, want %q", ttyString, test.want)
		}
	}
}

func cloneBuffer(b *Buffer) *Buffer {
	return &Buffer{b.Width, cloneLines(b.Lines), b.Dot}
}

func cloneLines(lines Lines) Lines {
	newLines := make(Lines, len(lines))
	for i, line := range lines {
		if line != nil {
			newLines[i] = make(Line, len(line))
			copy(newLines[i], line)
		}
	}
	return newLines
}