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
|
package tui
import (
"strings"
"testing"
)
func TestIntegration_DeepNesting(t *testing.T) {
root := New(
WithSize(100, 100),
WithPadding(2),
)
current := root
depth := 5
for range depth {
child := New(
WithFlexGrow(1),
WithPadding(2),
)
current.AddChild(child)
current = child
}
buf := NewBuffer(100, 100)
root.Render(buf, 100, 100)
leaf := current
expectedContentWidth := 100 - (depth+1)*4
if leaf.ContentRect().Width != expectedContentWidth {
t.Errorf("leaf.ContentRect().Width = %d, want %d",
leaf.ContentRect().Width, expectedContentWidth)
}
}
func TestIntegration_Centering(t *testing.T) {
type tc struct {
parentWidth, parentHeight int
childWidth, childHeight int
justify Justify
align Align
expectedX, expectedY int
}
tests := map[string]tc{
"center center": {
parentWidth: 100, parentHeight: 100,
childWidth: 20, childHeight: 10,
justify: JustifyCenter,
align: AlignCenter,
expectedX: 40, expectedY: 45,
},
"end center column": {
parentWidth: 100, parentHeight: 100,
childWidth: 20, childHeight: 10,
justify: JustifyEnd,
align: AlignCenter,
expectedX: 40, expectedY: 90,
},
"start end": {
parentWidth: 100, parentHeight: 100,
childWidth: 20, childHeight: 10,
justify: JustifyStart,
align: AlignEnd,
expectedX: 80, expectedY: 0,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
root := New(
WithSize(tt.parentWidth, tt.parentHeight),
WithDirection(Column),
WithJustify(tt.justify),
WithAlign(tt.align),
)
child := New(
WithSize(tt.childWidth, tt.childHeight),
)
root.AddChild(child)
buf := NewBuffer(tt.parentWidth, tt.parentHeight)
root.Render(buf, tt.parentWidth, tt.parentHeight)
childRect := child.Rect()
if childRect.X != tt.expectedX {
t.Errorf("child.X = %d, want %d", childRect.X, tt.expectedX)
}
if childRect.Y != tt.expectedY {
t.Errorf("child.Y = %d, want %d", childRect.Y, tt.expectedY)
}
})
}
}
func TestIntegration_RenderOutput(t *testing.T) {
panel := New(
WithSize(10, 5),
WithBorder(BorderSingle),
)
buf := NewBuffer(10, 5)
panel.Render(buf, 10, 5)
expected := []string{
"┌────────┐",
"│ │",
"│ │",
"│ │",
"└────────┘",
}
for y := range 5 {
var row strings.Builder
for x := range 10 {
cell := buf.Cell(x, y)
row.WriteString(cell.Text)
}
if row.String() != expected[y] {
t.Errorf("row %d = %q, want %q", y, row.String(), expected[y])
}
}
}
func TestIntegration_CullingOutsideBounds(t *testing.T) {
root := New(
WithSize(100, 100),
)
child := New(
WithSize(10, 10),
)
root.AddChild(child)
root.Calculate(100, 100)
buf := NewBuffer(5, 5)
RenderTree(buf, root)
}
func TestIntegration_GapBetweenChildren(t *testing.T) {
root := New(
WithSize(100, 100),
WithDisplay(DisplayFlex), WithDirection(Row),
WithGap(10),
)
child1 := New(WithWidth(20), WithHeight(100))
child2 := New(WithWidth(20), WithHeight(100))
child3 := New(WithWidth(20), WithHeight(100))
root.AddChild(child1, child2, child3)
buf := NewBuffer(100, 100)
root.Render(buf, 100, 100)
if child1.Rect().X != 0 {
t.Errorf("child1.X = %d, want 0", child1.Rect().X)
}
if child2.Rect().X != 30 {
t.Errorf("child2.X = %d, want 30", child2.Rect().X)
}
if child3.Rect().X != 60 {
t.Errorf("child3.X = %d, want 60", child3.Rect().X)
}
}
func TestIntegration_TextWrapEndToEnd(t *testing.T) {
buf := NewBuffer(20, 10)
root := New(
WithSize(20, 10),
WithDirection(Column),
)
text := New(
WithText("the quick brown fox jumps over the lazy dog"),
WithTextAlign(TextAlignLeft),
)
root.AddChild(text)
root.Calculate(20, 10)
RenderTree(buf, root)
line0 := extractBufferLine(buf, 0, 20)
line1 := extractBufferLine(buf, 1, 20)
line2 := extractBufferLine(buf, 2, 20)
if !strings.HasPrefix(strings.TrimRight(line0, " \x00"), "the quick brown fox") {
t.Errorf("line 0: got %q", line0)
}
if !strings.HasPrefix(strings.TrimRight(line1, " \x00"), "jumps over the lazy") {
t.Errorf("line 1: got %q", line1)
}
if !strings.HasPrefix(strings.TrimRight(line2, " \x00"), "dog") {
t.Errorf("line 2: got %q", line2)
}
textRect := text.Rect()
if textRect.Height != 3 {
t.Errorf("text element height = %d, want 3", textRect.Height)
}
}
func extractBufferLine(buf *Buffer, y, width int) string {
var b strings.Builder
for x := range width {
cell := buf.Cell(x, y)
if cell.Text != "" {
b.WriteString(cell.Text)
} else {
b.WriteByte(' ')
}
}
return b.String()
}
func TestIntegration_TextAlignment(t *testing.T) {
type tc struct {
align TextAlign
content string
boxWidth int
expectedStartOffset int
}
tests := map[string]tc{
"left align": {
align: TextAlignLeft,
content: "Hi",
boxWidth: 20,
expectedStartOffset: 0,
},
"center align": {
align: TextAlignCenter,
content: "Hi",
boxWidth: 20,
expectedStartOffset: 9,
},
"right align": {
align: TextAlignRight,
content: "Hi",
boxWidth: 20,
expectedStartOffset: 18,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
elem := New(
WithText(tt.content),
WithTextAlign(tt.align),
WithSize(tt.boxWidth, 1),
)
buf := NewBuffer(tt.boxWidth, 1)
elem.Calculate(tt.boxWidth, 1)
RenderTree(buf, elem)
foundX := -1
for x := 0; x < tt.boxWidth; x++ {
if buf.Cell(x, 0).Text == "H" {
foundX = x
break
}
}
contentRect := elem.ContentRect()
expectedX := contentRect.X + tt.expectedStartOffset
if foundX != expectedX {
t.Errorf("'H' found at x=%d, want %d", foundX, expectedX)
}
})
}
}
func TestIntegration_BorderTitle(t *testing.T) {
type tc struct {
title string
borderStyle BorderStyle
opts []Option
width int
height int
wantSubstr string
wantNotSubstr string
gradientCheck bool
}
tests := map[string]tc{
"title in rounded border": {
title: "Status",
borderStyle: BorderRounded,
width: 20,
height: 5,
wantSubstr: "Status",
},
"empty title draws no title text": {
title: "",
borderStyle: BorderRounded,
width: 20,
height: 5,
wantSubstr: "╭",
wantNotSubstr: "Title",
},
"title takes priority over gradient": {
title: "MyTitle",
borderStyle: BorderRounded,
width: 20,
height: 5,
opts: []Option{WithBorderGradient(Gradient{Start: Red, End: Blue})},
wantSubstr: "MyTitle",
gradientCheck: true,
},
"long title is truncated": {
title: "VeryLongTitleThatExceedsBoxWidth",
borderStyle: BorderRounded,
width: 15,
height: 3,
wantSubstr: "VeryLong",
},
"single border with title": {
title: "Info",
borderStyle: BorderSingle,
width: 12,
height: 4,
wantSubstr: "Info",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
el := New(
WithSize(tt.width, tt.height),
WithBorder(tt.borderStyle),
WithBorderTitle(tt.title),
)
el.Apply(tt.opts...)
buf := NewBuffer(tt.width, tt.height)
el.Render(buf, tt.width, tt.height)
term := NewMockTerminal(tt.width, tt.height)
Render(term, buf)
output := term.StringTrimmed()
if tt.wantSubstr != "" && !strings.Contains(output, tt.wantSubstr) {
t.Errorf("output should contain %q, got:\n%s", tt.wantSubstr, output)
}
if tt.wantNotSubstr != "" && strings.Contains(output, tt.wantNotSubstr) {
t.Errorf("output should NOT contain %q, got:\n%s", tt.wantNotSubstr, output)
}
if tt.gradientCheck {
cell := buf.Cell(tt.width-1, tt.height-1)
if cell.Style.Fg.IsDefault() {
t.Error("border lost its gradient when a title was set")
}
}
})
}
}
func TestIntegration_BorderTitleClipped(t *testing.T) {
type tc struct {
title string
innerHeight int
outerHeight int
scrollY int
wantSubstr string
wantNotSubstr string
}
tests := map[string]tc{
"title visible when top border inside clip": {
title: "ClippedTitle",
innerHeight: 8,
outerHeight: 5,
wantSubstr: "ClippedTitle",
},
"title hidden when scrolled above viewport": {
title: "HiddenTitle",
innerHeight: 8,
outerHeight: 5,
scrollY: 3,
wantSubstr: "",
wantNotSubstr: "HiddenTitle",
},
"title wider than box truncated": {
title: "VeryVeryLongTitleThatExceedsBoxWidth",
innerHeight: 8,
outerHeight: 5,
wantSubstr: "VeryVeryLong",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
inner := New(
WithSize(20, tt.innerHeight),
WithBorder(BorderRounded),
WithBorderTitle(tt.title),
)
opts := []Option{WithSize(20, tt.outerHeight)}
if tt.scrollY != 0 {
opts = append(opts, WithScrollable(ScrollVertical), WithScrollOffset(0, tt.scrollY))
} else {
opts = append(opts, WithOverflow(OverflowHidden))
}
outer := New(opts...)
outer.AddChild(inner)
buf := NewBuffer(20, tt.outerHeight)
outer.Render(buf, 20, tt.outerHeight)
term := NewMockTerminal(20, tt.outerHeight)
Render(term, buf)
output := term.StringTrimmed()
if tt.wantSubstr != "" && !strings.Contains(output, tt.wantSubstr) {
t.Errorf("output should contain %q, got:\n%s", tt.wantSubstr, output)
}
if tt.wantNotSubstr != "" && strings.Contains(output, tt.wantNotSubstr) {
t.Errorf("output should NOT contain %q, got:\n%s", tt.wantNotSubstr, output)
}
})
}
}
|