File: lines_test.go

package info (click to toggle)
lazygit 0.57.0%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,748 kB
  • sloc: sh: 153; makefile: 76
file content (449 lines) | stat: -rw-r--r-- 9,408 bytes parent folder | download
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
444
445
446
447
448
449
package utils

import (
	"bufio"
	"strings"
	"testing"

	"github.com/jesseduffield/gocui"
	"github.com/stretchr/testify/assert"
)

// TestSplitLines is a function.
func TestSplitLines(t *testing.T) {
	type scenario struct {
		multilineString string
		expected        []string
	}

	scenarios := []scenario{
		{
			"",
			[]string{},
		},
		{
			"\n",
			[]string{},
		},
		{
			"hello world !\nhello universe !\n",
			[]string{
				"hello world !",
				"hello universe !",
			},
		},
	}

	for _, s := range scenarios {
		assert.EqualValues(t, s.expected, SplitLines(s.multilineString))
	}
}

func TestSplitNul(t *testing.T) {
	type scenario struct {
		multilineString string
		expected        []string
	}

	scenarios := []scenario{
		{
			"",
			[]string{},
		},
		{
			"\x00",
			[]string{
				"",
			},
		},
		{
			"hello world !\x00hello universe !\x00",
			[]string{
				"hello world !",
				"hello universe !",
			},
		},
	}

	for _, s := range scenarios {
		assert.EqualValues(t, s.expected, SplitNul(s.multilineString))
	}
}

// TestNormalizeLinefeeds is a function.
func TestNormalizeLinefeeds(t *testing.T) {
	type scenario struct {
		byteArray []byte
		expected  []byte
	}
	scenarios := []scenario{
		{
			// \r\n
			[]byte{97, 115, 100, 102, 13, 10},
			[]byte{97, 115, 100, 102, 10},
		},
		{
			// bash\r\nblah
			[]byte{97, 115, 100, 102, 13, 10, 97, 115, 100, 102},
			[]byte{97, 115, 100, 102, 10, 97, 115, 100, 102},
		},
		{
			// \r
			[]byte{97, 115, 100, 102, 13},
			[]byte{97, 115, 100, 102},
		},
		{
			// \n
			[]byte{97, 115, 100, 102, 10},
			[]byte{97, 115, 100, 102, 10},
		},
	}

	for _, s := range scenarios {
		assert.EqualValues(t, string(s.expected), NormalizeLinefeeds(string(s.byteArray)))
	}
}

func TestScanLinesAndTruncateWhenLongerThanBuffer(t *testing.T) {
	type scenario struct {
		input         string
		expectedLines []string
	}

	scenarios := []scenario{
		{
			"",
			[]string{},
		},
		{
			"\n",
			[]string{""},
		},
		{
			"abc",
			[]string{"abc"},
		},
		{
			"abc\ndef",
			[]string{"abc", "def"},
		},
		{
			"abc\n\ndef",
			[]string{"abc", "", "def"},
		},
		{
			"abc\r\ndef\r",
			[]string{"abc", "def"},
		},
		{
			"abcdef",
			[]string{"abcde"},
		},
		{
			"abcdef\n",
			[]string{"abcde"},
		},
		{
			"abcdef\nghijkl\nx",
			[]string{"abcde", "ghijk", "x"},
		},
		{
			"abc\ndefghijklmnopqrstuvw\nx",
			[]string{"abc", "defgh", "x"},
		},
	}

	for _, s := range scenarios {
		scanner := bufio.NewScanner(strings.NewReader(s.input))
		scanner.Buffer(make([]byte, 5), 5)
		scanner.Split(ScanLinesAndTruncateWhenLongerThanBuffer(5))
		result := []string{}
		for scanner.Scan() {
			result = append(result, scanner.Text())
		}
		assert.NoError(t, scanner.Err())
		assert.EqualValues(t, s.expectedLines, result)
	}
}

func TestWrapViewLinesToWidth(t *testing.T) {
	tests := []struct {
		name                         string
		wrap                         bool
		editable                     bool
		text                         string
		width                        int
		tabWidth                     int
		expectedWrappedLines         []string
		expectedWrappedLinesIndices  []int
		expectedOriginalLinesIndices []int
	}{
		{
			name:  "Wrap off",
			wrap:  false,
			text:  "1st line\n2nd line\n3rd line",
			width: 5,
			expectedWrappedLines: []string{
				"1st line",
				"2nd line",
				"3rd line",
			},
			expectedWrappedLinesIndices:  []int{0, 1, 2},
			expectedOriginalLinesIndices: []int{0, 1, 2},
		},
		{
			name:  "Wrap on space",
			wrap:  true,
			text:  "Hello World",
			width: 5,
			expectedWrappedLines: []string{
				"Hello",
				"World",
			},
			expectedWrappedLinesIndices:  []int{0},
			expectedOriginalLinesIndices: []int{0, 0},
		},
		{
			name:  "Wrap on hyphen",
			wrap:  true,
			text:  "Hello-World",
			width: 6,
			expectedWrappedLines: []string{
				"Hello-",
				"World",
			},
		},
		{
			name:  "Wrap on hyphen 2",
			wrap:  true,
			text:  "Blah Hello-World",
			width: 12,
			expectedWrappedLines: []string{
				"Blah Hello-",
				"World",
			},
		},
		{
			name:  "Wrap on hyphen 3",
			wrap:  true,
			text:  "Blah Hello-World",
			width: 11,
			expectedWrappedLines: []string{
				"Blah Hello-",
				"World",
			},
		},
		{
			name:  "Wrap on hyphen 4",
			wrap:  true,
			text:  "Blah Hello-World",
			width: 10,
			expectedWrappedLines: []string{
				"Blah Hello",
				"-World",
			},
		},
		{
			name:  "Wrap on space 2",
			wrap:  true,
			text:  "Blah Hello World",
			width: 10,
			expectedWrappedLines: []string{
				"Blah Hello",
				"World",
			},
		},
		{
			name:  "Wrap on space with more words",
			wrap:  true,
			text:  "Longer word here",
			width: 10,
			expectedWrappedLines: []string{
				"Longer",
				"word here",
			},
		},
		{
			name:  "Split word that's too long",
			wrap:  true,
			text:  "ThisWordIsWayTooLong",
			width: 10,
			expectedWrappedLines: []string{
				"ThisWordIs",
				"WayTooLong",
			},
		},
		{
			name:  "Split word that's too long over multiple lines",
			wrap:  true,
			text:  "ThisWordIsWayTooLong",
			width: 5,
			expectedWrappedLines: []string{
				"ThisW",
				"ordIs",
				"WayTo",
				"oLong",
			},
		},
		{
			name:  "Lots of hyphens",
			wrap:  true,
			text:  "one-two-three-four-five",
			width: 8,
			expectedWrappedLines: []string{
				"one-two-",
				"three-",
				"four-",
				"five",
			},
		},
		{
			name:  "Several lines using all the available width",
			wrap:  true,
			text:  "aaa bb cc ddd-ee ff",
			width: 5,
			expectedWrappedLines: []string{
				"aaa",
				"bb cc",
				"ddd-",
				"ee ff",
			},
		},
		{
			name:  "Several lines using all the available width, with multi-cell runes",
			wrap:  true,
			text:  "ðŸĪðŸĪðŸĪ 🐝🐝 🙉🙉 ðŸĶŠðŸĶŠðŸĶŠ-🐎🐎 ðŸĶĒðŸĶĒ",
			width: 9,
			expectedWrappedLines: []string{
				"ðŸĪðŸĪðŸĪ",
				"🐝🐝 🙉🙉",
				"ðŸĶŠðŸĶŠðŸĶŠ-",
				"🐎🐎 ðŸĶĒðŸĶĒ",
			},
		},
		{
			name:  "Space in last column",
			wrap:  true,
			text:  "hello world",
			width: 6,
			expectedWrappedLines: []string{
				"hello",
				"world",
			},
		},
		{
			name:  "Hyphen in last column",
			wrap:  true,
			text:  "hello-world",
			width: 6,
			expectedWrappedLines: []string{
				"hello-",
				"world",
			},
		},
		{
			name:  "English text",
			wrap:  true,
			text:  "+The sea reach of the Thames stretched before us like the bedinnind of an interminable waterway. In the offind the sea and the sky were welded todether without a joint, and in the luminous space the tanned sails of the bardes drifting blah blah",
			width: 81,
			expectedWrappedLines: []string{
				"+The sea reach of the Thames stretched before us like the bedinnind of an",
				"interminable waterway. In the offind the sea and the sky were welded todether",
				"without a joint, and in the luminous space the tanned sails of the bardes",
				"drifting blah blah",
			},
		},
		{
			name:     "Tabs, width 4",
			wrap:     true,
			text:     "\ta\tbb\tccc\tdddd\teeeee",
			width:    50,
			tabWidth: 4,
			expectedWrappedLines: []string{
				"    a   bb  ccc dddd    eeeee",
			},
		},
		{
			name:     "Tabs, width 8",
			wrap:     true,
			text:     "\ta\tbb\tccc\tdddddddd\teeeee",
			width:    100,
			tabWidth: 8,
			expectedWrappedLines: []string{
				"        a       bb      ccc     dddddddd        eeeee",
			},
		},
		{
			name:  "Multiple lines",
			wrap:  true,
			text:  "First paragraph\nThe second paragraph is a bit longer.\nThird paragraph\n",
			width: 10,
			expectedWrappedLines: []string{
				"First",
				"paragraph",
				"The second",
				"paragraph",
				"is a bit",
				"longer.",
				"Third",
				"paragraph",
			},
			expectedWrappedLinesIndices:  []int{0, 2, 6},
			expectedOriginalLinesIndices: []int{0, 0, 1, 1, 1, 1, 2, 2},
		},
		{
			name:     "Avoid blank line at end if not editable",
			wrap:     true,
			editable: false,
			text:     "First\nSecond\nThird\n",
			width:    10,
			expectedWrappedLines: []string{
				"First",
				"Second",
				"Third",
			},
			expectedWrappedLinesIndices:  []int{0, 1, 2},
			expectedOriginalLinesIndices: []int{0, 1, 2},
		},
		{
			name:     "Keep blank line at end if editable",
			wrap:     true,
			editable: true,
			text:     "First\nSecond\nThird\n",
			width:    10,
			expectedWrappedLines: []string{
				"First",
				"Second",
				"Third",
				"",
			},
			expectedWrappedLinesIndices:  []int{0, 1, 2, 3},
			expectedOriginalLinesIndices: []int{0, 1, 2, 3},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			tabWidth := tt.tabWidth
			if tabWidth == 0 {
				tabWidth = 4
			}
			wrappedLines, wrappedLinesIndices, originalLinesIndices := WrapViewLinesToWidth(tt.wrap, tt.editable, tt.text, tt.width, tabWidth)
			assert.Equal(t, tt.expectedWrappedLines, wrappedLines)
			if tt.expectedWrappedLinesIndices != nil {
				assert.Equal(t, tt.expectedWrappedLinesIndices, wrappedLinesIndices)
			}
			if tt.expectedOriginalLinesIndices != nil {
				assert.Equal(t, tt.expectedOriginalLinesIndices, originalLinesIndices)
			}

			// As a sanity check, also test that gocui's line wrapping behaves the same way
			view := gocui.NewView("", 0, 0, tt.width+1, 1000, gocui.OutputNormal)
			view.TabWidth = tabWidth
			assert.Equal(t, tt.width, view.InnerWidth())
			view.Wrap = tt.wrap
			view.Editable = tt.editable
			view.SetContent(tt.text)
			assert.Equal(t, wrappedLines, view.ViewBufferLines())
		})
	}
}