forked from kungfusheep/glyph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionlist_test.go
More file actions
289 lines (245 loc) · 6.8 KB
/
selectionlist_test.go
File metadata and controls
289 lines (245 loc) · 6.8 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
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 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)
}
}