-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathselectionlist_test.go
More file actions
596 lines (511 loc) · 13.4 KB
/
selectionlist_test.go
File metadata and controls
596 lines (511 loc) · 13.4 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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
package glyph
import (
"fmt"
"testing"
)
func TestV2SelectionListBasic(t *testing.T) {
items := []string{"Apple", "Banana", "Cherry"}
selected := 1
view := VBox(
&SelectionList{
Items: &items,
Selected: &selected,
Marker: "> ",
Render: func(s *string) any {
return Text(s)
},
},
)
tmpl := Build(view)
buf := NewBuffer(40, 10)
tmpl.Execute(buf, 40, 10)
// Check output
line0 := buf.GetLine(0)
line1 := buf.GetLine(1)
line2 := buf.GetLine(2)
t.Logf("Line 0: %q", line0)
t.Logf("Line 1: %q", line1)
t.Logf("Line 2: %q", line2)
// Apple should not have marker (selected = 1)
if !contains(line0, " Apple") {
t.Errorf("Line 0 should have spaces before Apple: %q", line0)
}
// Banana should have marker (selected = 1)
if !contains(line1, "> Banana") {
t.Errorf("Line 1 should have marker before Banana: %q", line1)
}
// Cherry should not have marker
if !contains(line2, " Cherry") {
t.Errorf("Line 2 should have spaces before Cherry: %q", line2)
}
}
func TestV2SelectionListWithRender(t *testing.T) {
items := []string{"First", "Second", "Third"}
selected := 0
view := VBox(
&SelectionList{
Items: &items,
Selected: &selected,
Marker: "* ",
Render: func(s *string) any {
return Text(s)
},
},
)
tmpl := Build(view)
buf := NewBuffer(40, 10)
tmpl.Execute(buf, 40, 10)
line0 := buf.GetLine(0)
t.Logf("Line 0: %q", line0)
// First should have marker
if !contains(line0, "* First") {
t.Errorf("Line 0 should have marker before First: %q", line0)
}
}
func TestV2SelectionListMaxVisible(t *testing.T) {
items := []string{"One", "Two", "Three", "Four", "Five"}
selected := 0
list := &SelectionList{
Items: &items,
Selected: &selected,
Marker: "> ",
MaxVisible: 3,
Render: func(s *string) any {
return Text(s)
},
}
view := VBox(list)
tmpl := Build(view)
buf := NewBuffer(40, 10)
tmpl.Execute(buf, 40, 10)
// Should only show 3 items
line0 := buf.GetLine(0)
line1 := buf.GetLine(1)
line2 := buf.GetLine(2)
line3 := buf.GetLine(3)
t.Logf("Line 0: %q", line0)
t.Logf("Line 1: %q", line1)
t.Logf("Line 2: %q", line2)
t.Logf("Line 3: %q", line3)
if !contains(line0, "One") {
t.Errorf("Line 0 should contain One: %q", line0)
}
if !contains(line2, "Three") {
t.Errorf("Line 2 should contain Three: %q", line2)
}
// Line 3 should be empty (only showing 3 items)
if contains(line3, "Four") {
t.Errorf("Line 3 should NOT contain Four (MaxVisible=3): %q", line3)
}
}
func TestV2SelectionListScrolling(t *testing.T) {
items := []string{"One", "Two", "Three", "Four", "Five"}
selected := 3 // Select "Four"
list := &SelectionList{
Items: &items,
Selected: &selected,
Marker: "> ",
MaxVisible: 3,
Render: func(s *string) any {
return Text(s)
},
}
view := VBox(list)
tmpl := Build(view)
buf := NewBuffer(40, 10)
tmpl.Execute(buf, 40, 10)
// After ensureVisible, offset should be 1 so we see [Two, Three, Four]
line0 := buf.GetLine(0)
line1 := buf.GetLine(1)
line2 := buf.GetLine(2)
t.Logf("Line 0: %q", line0)
t.Logf("Line 1: %q", line1)
t.Logf("Line 2: %q", line2)
// "Four" should be visible and selected
found := false
for y := 0; y < 3; y++ {
line := buf.GetLine(y)
if contains(line, "> Four") {
found = true
break
}
}
if !found {
t.Error("Selected item 'Four' with marker not found in visible window")
}
}
func TestSelectionListOverflowClipped(t *testing.T) {
// Reproduces the pprofin bug: a bordered VBox with header, Grow(1) list,
// and footer. The list has MaxVisible(20) but the terminal is only 15 lines
// tall. Without the fix, the list renders past the border and pushes
// the footer off-screen.
items := make([]string, 25)
for i := range items {
items[i] = fmt.Sprintf("Item %02d", i+1)
}
selected := 0
list := &SelectionList{
Items: &items,
Selected: &selected,
Marker: "> ",
MaxVisible: 20,
Render: func(s *string) any {
return Text(s)
},
}
// Layout mirrors pprofin: bordered root, header, grow list, footer
view := VBox.Border(BorderSingle).Title("test")(
Text("header"), // 1 line
HRule(), // 1 line
VBox.Grow(1)(list), // flex fill
HRule(), // 1 line
Text("footer"), // 1 line
)
screenH := int16(15)
tmpl := Build(view)
buf := NewBuffer(40, int(screenH))
tmpl.Execute(buf, 40, screenH)
// Dump for debugging
for y := 0; y < int(screenH); y++ {
t.Logf("Line %2d: %q", y, buf.GetLine(y))
}
// The bottom border must be on the last line
bottomLeft := buf.Get(0, int(screenH)-1)
if bottomLeft.Rune != '└' {
t.Errorf("Bottom-left corner should be └, got %c (border overwritten by list overflow)", bottomLeft.Rune)
}
// The footer text must be on the second-to-last line (inside border)
footerLine := buf.GetLine(int(screenH) - 2)
if !contains(footerLine, "footer") {
t.Errorf("Footer should be visible on line %d, got: %q", screenH-2, footerLine)
}
// The header must be on line 1 (inside top border)
headerLine := buf.GetLine(1)
if !contains(headerLine, "header") {
t.Errorf("Header should be on line 1, got: %q", headerLine)
}
// List items should NOT appear on the footer or border lines
footerArea := buf.GetLine(int(screenH) - 2)
if contains(footerArea, "Item") {
t.Errorf("List items should not overflow into footer area: %q", footerArea)
}
borderLine := buf.GetLine(int(screenH) - 1)
if contains(borderLine, "Item") {
t.Errorf("List items should not overflow into border: %q", borderLine)
}
}
func TestSelectionListVariableHeight(t *testing.T) {
type item struct {
Title string
Desc string
}
items := []item{
{"Open", "Opens a file from disk"},
{"Save", "Saves the current buffer"},
{"Quit", "Exits the application"},
}
selected := 1
list := &SelectionList{
Items: &items,
Selected: &selected,
Marker: "> ",
Render: func(it *item) any {
// 2-row item: title on first line, description on second
return VBox(
Text(&it.Title),
Text(&it.Desc),
)
},
}
view := VBox(list)
tmpl := Build(view)
buf := NewBuffer(40, 20)
tmpl.Execute(buf, 40, 20)
for y := 0; y < 10; y++ {
t.Logf("Line %2d: %q", y, buf.GetLine(y))
}
// Item 0 ("Open") occupies lines 0-1
line0 := buf.GetLine(0)
line1 := buf.GetLine(1)
if !contains(line0, "Open") {
t.Errorf("Line 0 should contain 'Open': %q", line0)
}
if !contains(line1, "Opens a file") {
t.Errorf("Line 1 should contain description: %q", line1)
}
// non-selected: no marker
if contains(line0, ">") {
t.Errorf("Line 0 should NOT have marker (item 0 not selected): %q", line0)
}
// Item 1 ("Save") is selected, occupies lines 2-3
line2 := buf.GetLine(2)
line3 := buf.GetLine(3)
if !contains(line2, "> Save") {
t.Errorf("Line 2 should have marker + 'Save': %q", line2)
}
if !contains(line3, "Saves") {
t.Errorf("Line 3 should contain description: %q", line3)
}
// Item 2 ("Quit") occupies lines 4-5
line4 := buf.GetLine(4)
line5 := buf.GetLine(5)
if !contains(line4, "Quit") {
t.Errorf("Line 4 should contain 'Quit': %q", line4)
}
if !contains(line5, "Exits") {
t.Errorf("Line 5 should contain description: %q", line5)
}
// Line 6 should be empty (no more items)
line6 := buf.GetLine(6)
if contains(line6, "Open") || contains(line6, "Save") || contains(line6, "Quit") {
t.Errorf("Line 6 should be empty, got: %q", line6)
}
}
func TestSelectionListVariableHeightMaxVisible(t *testing.T) {
type item struct {
Title string
Desc string
}
items := []item{
{"Alpha", "First item"},
{"Beta", "Second item"},
{"Gamma", "Third item"},
{"Delta", "Fourth item"},
{"Epsilon", "Fifth item"},
}
selected := 0
list := &SelectionList{
Items: &items,
Selected: &selected,
Marker: "> ",
MaxVisible: 3,
Render: func(it *item) any {
return VBox(
Text(&it.Title),
Text(&it.Desc),
)
},
}
view := VBox(list)
tmpl := Build(view)
buf := NewBuffer(40, 20)
tmpl.Execute(buf, 40, 20)
for y := 0; y < 12; y++ {
t.Logf("Line %2d: %q", y, buf.GetLine(y))
}
// 3 items visible x 2 rows each = 6 rows total
line0 := buf.GetLine(0)
line5 := buf.GetLine(5)
line6 := buf.GetLine(6)
if !contains(line0, "> Alpha") {
t.Errorf("Line 0 should have marker + 'Alpha': %q", line0)
}
if !contains(line5, "Third item") {
t.Errorf("Line 5 should contain 'Third item' (last visible item desc): %q", line5)
}
// Item 4 ("Delta") should NOT be visible
if contains(line6, "Delta") {
t.Errorf("Line 6 should NOT contain 'Delta' (MaxVisible=3): %q", line6)
}
}
func TestSelectionListVariableHeightScrolling(t *testing.T) {
type item struct {
Title string
Desc string
}
items := []item{
{"Alpha", "First"},
{"Beta", "Second"},
{"Gamma", "Third"},
{"Delta", "Fourth"},
{"Epsilon", "Fifth"},
}
selected := 4 // select last item
list := &SelectionList{
Items: &items,
Selected: &selected,
Marker: "> ",
MaxVisible: 3,
Render: func(it *item) any {
return VBox(
Text(&it.Title),
Text(&it.Desc),
)
},
}
view := VBox(list)
tmpl := Build(view)
buf := NewBuffer(40, 20)
tmpl.Execute(buf, 40, 20)
for y := 0; y < 12; y++ {
t.Logf("Line %2d: %q", y, buf.GetLine(y))
}
// With scrolling, the last 3 items should be visible: Gamma, Delta, Epsilon
// Epsilon (selected) should have marker
found := false
for y := 0; y < 12; y++ {
line := buf.GetLine(y)
if contains(line, "> Epsilon") {
found = true
break
}
}
if !found {
t.Error("Selected item 'Epsilon' with marker not found in rendered output")
}
// Alpha should NOT be visible (scrolled past)
for y := 0; y < 12; y++ {
line := buf.GetLine(y)
if contains(line, "Alpha") {
t.Errorf("'Alpha' should not be visible (scrolled past), found on line %d: %q", y, line)
break
}
}
}
func TestSelectionListVariableHeightSelectedRef(t *testing.T) {
type item struct {
Title string
Desc string
}
items := []item{
{"Open", "Opens file"},
{"Save", "Saves file"},
{"Quit", "Exits app"},
}
selected := 1
ref := &NodeRef{}
list := &SelectionList{
Items: &items,
Selected: &selected,
Marker: "> ",
SelectedRef: ref,
Render: func(it *item) any {
return VBox(
Text(&it.Title),
Text(&it.Desc),
)
},
}
view := VBox(list)
tmpl := Build(view)
buf := NewBuffer(40, 20)
tmpl.Execute(buf, 40, 20)
// Item 0 takes rows 0-1, so item 1 starts at Y=2 with height 2
if ref.Y != 2 {
t.Errorf("SelectedRef.Y should be 2, got %d", ref.Y)
}
if ref.H != 2 {
t.Errorf("SelectedRef.H should be 2, got %d", ref.H)
}
}
func TestSelectionListVariableHeightClipped(t *testing.T) {
// variable-height items inside a bordered box with fixed screen height;
// items must not overflow beyond the border
type item struct {
Title string
Desc string
}
items := []item{
{"Alpha", "First"},
{"Beta", "Second"},
{"Gamma", "Third"},
{"Delta", "Fourth"},
{"Epsilon", "Fifth"},
{"Zeta", "Sixth"},
{"Eta", "Seventh"},
{"Theta", "Eighth"},
}
selected := 0
list := &SelectionList{
Items: &items,
Selected: &selected,
Marker: "> ",
MaxVisible: 20,
Render: func(it *item) any {
return VBox(
Text(&it.Title),
Text(&it.Desc),
)
},
}
view := VBox.Border(BorderSingle).Title("test")(
Text("header"),
HRule(),
VBox.Grow(1)(list),
HRule(),
Text("footer"),
)
screenH := int16(15)
tmpl := Build(view)
buf := NewBuffer(40, int(screenH))
tmpl.Execute(buf, 40, screenH)
for y := 0; y < int(screenH); y++ {
t.Logf("Line %2d: %q", y, buf.GetLine(y))
}
// bottom border must be on last line
bottomLeft := buf.Get(0, int(screenH)-1)
if bottomLeft.Rune != '└' {
t.Errorf("Bottom-left corner should be └, got %c", bottomLeft.Rune)
}
// footer must be visible
footerLine := buf.GetLine(int(screenH) - 2)
if !contains(footerLine, "footer") {
t.Errorf("Footer should be visible: %q", footerLine)
}
// no item content on footer or border lines
for _, y := range []int{int(screenH) - 2, int(screenH) - 1} {
line := buf.GetLine(y)
if contains(line, "Alpha") || contains(line, "Beta") || contains(line, "Gamma") {
t.Errorf("List content should not overflow into footer/border on line %d: %q", y, line)
}
}
}
func TestSelectionListOverflowScrolling(t *testing.T) {
// When the list is clipped to fewer rows than MaxVisible, scrolling
// to a selected item near the bottom should still work correctly.
items := make([]string, 25)
for i := range items {
items[i] = fmt.Sprintf("Item %02d", i+1)
}
selected := 24 // last item
list := &SelectionList{
Items: &items,
Selected: &selected,
Marker: "> ",
MaxVisible: 20,
Render: func(s *string) any {
return Text(s)
},
}
view := VBox.Border(BorderSingle).Title("test")(
Text("header"),
HRule(),
VBox.Grow(1)(list),
HRule(),
Text("footer"),
)
screenH := int16(15)
tmpl := Build(view)
buf := NewBuffer(40, int(screenH))
tmpl.Execute(buf, 40, screenH)
for y := 0; y < int(screenH); y++ {
t.Logf("Line %2d: %q", y, buf.GetLine(y))
}
// The selected item (Item 25) should be visible somewhere in the list area
found := false
for y := 3; y < int(screenH)-3; y++ { // list area is between header+hrule and hrule+footer
line := buf.GetLine(y)
if contains(line, "> Item 25") {
found = true
break
}
}
if !found {
t.Error("Selected item 'Item 25' should be visible in the clipped list area")
}
// Footer must still be visible
footerLine := buf.GetLine(int(screenH) - 2)
if !contains(footerLine, "footer") {
t.Errorf("Footer should still be visible: %q", footerLine)
}
}