-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathbox_render_test.go
More file actions
executable file
·550 lines (478 loc) · 15.9 KB
/
box_render_test.go
File metadata and controls
executable file
·550 lines (478 loc) · 15.9 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
package box
import (
"strings"
"testing"
"github.com/charmbracelet/x/ansi"
"github.com/mattn/go-runewidth"
)
func TestRenderBasicBox(t *testing.T) {
b := NewBox().Padding(2, 1).Style(Single)
out, err := b.Render("Box CLI Maker", "Highly Customizable Terminal Box Maker")
if err != nil {
t.Fatalf("Render returned error: %v", err)
}
if out == "" {
t.Fatalf("expected non-empty render output")
}
if !strings.Contains(out, "Box CLI Maker") || !strings.Contains(out, "Highly Customizable Terminal Box Maker") {
t.Fatalf("rendered output does not contain title/content: %q", out)
}
// Basic structural checks: top and bottom lines should use the Single style corners.
lines := strings.Split(out, "\n")
if len(lines) < 3 {
t.Fatalf("expected at least 3 lines in rendered box, got %d", len(lines))
}
// Last element is empty due to trailing newline; bottom bar is at len(lines)-2.
top := lines[0]
bottom := lines[len(lines)-2]
if !strings.HasPrefix(top, "┌") || !strings.HasSuffix(top, "┐") {
t.Errorf("top bar does not use Single style corners: %q", top)
}
if !strings.HasPrefix(bottom, "└") || !strings.HasSuffix(bottom, "┘") {
t.Errorf("bottom bar does not use Single style corners: %q", bottom)
}
}
func TestRenderInbuiltStylesPorts(t *testing.T) {
tests := []BoxStyle{
Single,
SingleDouble,
Double,
DoubleSingle,
Bold,
Round,
Hidden,
Classic,
Block,
}
for _, style := range tests {
preset, ok := boxes[style]
if !ok {
t.Fatalf("no preset found for style %q", style)
}
b := NewBox().Padding(2, 5).Style(style)
out, err := b.Render("Box CLI Maker", "Highly Customized Terminal Box Maker")
if err != nil {
t.Fatalf("Render returned error for style %q: %v", style, err)
}
lines := strings.Split(out, "\n")
if len(lines) < 3 {
t.Fatalf("style %q: expected at least 3 lines, got %d", style, len(lines))
}
// Last element is empty due to trailing newline; bottom bar is at len-2.
top := lines[0]
bottom := lines[len(lines)-2]
if !strings.HasPrefix(top, preset.topLeft) || !strings.HasSuffix(top, preset.topRight) {
t.Errorf("style %q: unexpected top corners: %q", style, top)
}
if !strings.HasPrefix(bottom, preset.bottomLeft) || !strings.HasSuffix(bottom, preset.bottomRight) {
t.Errorf("style %q: unexpected bottom corners: %q", style, bottom)
}
// Check that interior lines use the expected vertical glyphs (including
// the Hidden style, where vertical is a space).
if len(lines) > 3 {
interior := lines[1 : len(lines)-2]
mid := interior[len(interior)/2]
if len(mid) == 0 {
t.Errorf("style %q: mid interior line unexpectedly empty", style)
} else {
if !strings.HasPrefix(mid, preset.vertical) || !strings.HasSuffix(mid, preset.vertical) {
t.Errorf("style %q: unexpected vertical borders in interior line: %q", style, mid)
}
}
}
}
}
func TestRenderDefaultStyleWithoutExplicitStyle(t *testing.T) {
b := NewBox().Padding(1, 1)
out, err := b.Render("Default", "Content")
if err != nil {
t.Fatalf("Render returned error: %v", err)
}
lines := strings.Split(out, "\n")
if len(lines) < 3 {
t.Fatalf("expected at least 3 lines in rendered box, got %d", len(lines))
}
top := lines[0]
bottom := lines[len(lines)-2]
if !strings.HasPrefix(top, "┌") || !strings.HasSuffix(top, "┐") {
t.Errorf("expected Single style corners by default; top=%q", top)
}
if !strings.HasPrefix(bottom, "└") || !strings.HasSuffix(bottom, "┘") {
t.Errorf("expected Single style corners by default; bottom=%q", bottom)
}
}
func TestManualBorderOverridesAfterStyle(t *testing.T) {
b := NewBox().
Style(Double).
TopLeft("*").
TopRight("*").
BottomLeft("*").
BottomRight("*")
out, err := b.Render("Title", "Content")
if err != nil {
t.Fatalf("Render returned error: %v", err)
}
lines := strings.Split(out, "\n")
if len(lines) < 3 {
t.Fatalf("expected at least 3 lines in rendered box, got %d", len(lines))
}
top := lines[0]
bottom := lines[len(lines)-2]
if !strings.HasPrefix(top, "*") || !strings.HasSuffix(top, "*") {
t.Errorf("expected custom top corners '*', got %q", top)
}
if !strings.HasPrefix(bottom, "*") || !strings.HasSuffix(bottom, "*") {
t.Errorf("expected custom bottom corners '*', got %q", bottom)
}
if !strings.Contains(top, "═") || !strings.Contains(bottom, "═") {
t.Errorf("expected Double style horizontal borders '═', got top=%q bottom=%q", top, bottom)
}
}
func TestBoxCopy(t *testing.T) {
t.Run("independent copies", func(t *testing.T) {
original := NewBox().
Padding(1, 2).
Color(Red).
TitleColor(Blue).
ContentColor(Yellow).
TitlePosition(Top).
Style(Double).
WrapContent(true).
WrapLimit(30)
original.TopLeft("[").TopRight("]").BottomLeft("{").BottomRight("}").Horizontal("-").Vertical("|")
clone := original.Copy()
if clone == nil {
t.Fatalf("expected non-nil copy")
}
if clone == original {
t.Fatalf("Copy should return a distinct pointer")
}
clone.Color(Green).Padding(5, 6).TitlePosition(Bottom).TopLeft("*")
if original.color != Red {
t.Fatalf("expected original color to remain Red, got %q", original.color)
}
if original.px != 1 || original.py != 2 {
t.Fatalf("expected original padding (1,2), got (%d,%d)", original.px, original.py)
}
if original.titlePos != Top {
t.Fatalf("expected original title position to stay Top, got %v", original.titlePos)
}
if original.topLeft != "[" {
t.Fatalf("expected original topLeft to stay '[', got %q", original.topLeft)
}
original.Color(Magenta)
if clone.color != Green {
t.Fatalf("expected clone color to remain Green after mutating original, got %q", clone.color)
}
})
t.Run("nil receiver", func(t *testing.T) {
var b *Box
if copy := b.Copy(); copy != nil {
t.Fatalf("expected nil copy from nil receiver, got %#v", copy)
}
})
}
func TestHPaddingAndVPadding(t *testing.T) {
b := NewBox().Padding(1, 2)
if b.px != 1 || b.py != 2 {
t.Fatalf("expected initial padding (1,2), got (%d,%d)", b.px, b.py)
}
b.HPadding(5)
if b.px != 5 {
t.Errorf("expected HPadding to set horizontal padding to 5, got %d", b.px)
}
if b.py != 2 {
t.Errorf("expected HPadding to leave vertical padding unchanged at 2, got %d", b.py)
}
b.VPadding(7)
if b.py != 7 {
t.Errorf("expected VPadding to set vertical padding to 7, got %d", b.py)
}
if b.px != 5 {
t.Errorf("expected VPadding to leave horizontal padding unchanged at 5, got %d", b.px)
}
}
func TestRenderTitlePositions(t *testing.T) {
title := "My Title"
content := "Some content"
cases := []struct {
name string
pos TitlePosition
}{
{"inside", Inside},
{"top", Top},
{"bottom", Bottom},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
b := NewBox().Padding(2, 1).Style(Single).TitlePosition(tc.pos)
out, err := b.Render(title, content)
if err != nil {
t.Fatalf("Render returned error for position %v: %v", tc.pos, err)
}
lines := strings.Split(out, "\n")
if len(lines) < 3 {
t.Fatalf("expected at least 3 lines, got %d", len(lines))
}
top := lines[0]
bottom := lines[len(lines)-2]
interior := lines[1 : len(lines)-2]
hasTitleInside := false
for _, l := range interior {
if strings.Contains(l, title) {
hasTitleInside = true
break
}
}
switch tc.pos {
case Inside:
if !hasTitleInside {
t.Errorf("expected title to appear inside box for Inside position; output: %q", out)
}
case Top:
if !strings.Contains(top, title) {
t.Errorf("expected title to appear in top bar for Top position; top: %q", top)
}
case Bottom:
if !strings.Contains(bottom, title) {
t.Errorf("expected title to appear in bottom bar for Bottom position; bottom: %q", bottom)
}
}
})
}
}
func TestRenderInvalidBoxStyle(t *testing.T) {
b := NewBox().Padding(2, 1).Style(BoxStyle("InvalidStyle"))
_, err := b.Render("Title", "Content")
if err == nil {
t.Fatalf("expected error for invalid Box style, got nil")
}
if !strings.Contains(err.Error(), "invalid Box style") {
t.Errorf("unexpected error message: %v", err)
}
}
func TestRenderInvalidTitlePosition(t *testing.T) {
b := NewBox().Padding(2, 1).Style(Single).TitlePosition(TitlePosition("Weird"))
_, err := b.Render("Title", "Content")
if err == nil {
t.Fatalf("expected error for invalid TitlePosition, got nil")
}
if !strings.Contains(err.Error(), "invalid TitlePosition") {
t.Errorf("unexpected error message: %v", err)
}
}
func TestRenderMultilineTitleNonInside(t *testing.T) {
b := NewBox().Padding(2, 1).Style(Single).TitlePosition(Top)
_, err := b.Render("Line1\nLine2", "Content")
if err == nil {
t.Fatalf("expected error for multiline title at non-Inside position, got nil")
}
if !strings.Contains(err.Error(), "multiline titles are only supported Inside title position only") {
t.Errorf("unexpected error message: %v", err)
}
}
func TestRenderNegativeWrapLimit(t *testing.T) {
b := NewBox().Padding(1, 1).Style(Single).WrapContent(true).WrapLimit(-1)
if _, err := b.Render("Title", "Content"); err == nil {
t.Fatalf("expected error for negative wrap limit, got nil")
} else if !strings.Contains(err.Error(), "wrapping limit cannot be negative") {
t.Errorf("unexpected error message for negative wrap limit: %v", err)
}
}
func TestRenderNonTTYLWrapContent(t *testing.T) {
oldIsTTY := isTTY
defer func() { isTTY = oldIsTTY }() // restore after test
isTTY = func(fd uintptr) bool { return false } // mock as non-TTY
b := NewBox().Padding(1, 1).Style(Single).WrapContent(true)
if _, err := b.Render("Title", "Content"); err == nil {
t.Fatalf("expected error for non TTY output with wrapping enabled, got nil")
} else if !strings.Contains(err.Error(), "cannot determine terminal width") {
t.Errorf("unexpected error message for non TTY wrap content: %v", err)
}
}
func TestRenderNegativePadding(t *testing.T) {
// Horizontal padding < 0.
b := NewBox().Padding(-1, 1).Style(Single)
if _, err := b.Render("Title", "Content"); err == nil {
t.Fatalf("expected error for negative horizontal padding, got nil")
} else if !strings.Contains(err.Error(), "horizontal padding cannot be negative") {
t.Errorf("unexpected error for negative horizontal padding: %v", err)
}
// Vertical padding < 0.
b = NewBox().Padding(1, -1).Style(Single)
if _, err := b.Render("Title", "Content"); err == nil {
t.Fatalf("expected error for negative vertical padding, got nil")
} else if !strings.Contains(err.Error(), "vertical padding cannot be negative") {
t.Errorf("unexpected error for negative vertical padding: %v", err)
}
}
func TestRenderInvalidContentAlign(t *testing.T) {
b := NewBox().Padding(1, 1).Style(Single).ContentAlign(AlignType("Weird"))
if _, err := b.Render("Title", "Content"); err == nil {
t.Fatalf("expected error for invalid content alignment, got nil")
} else if !strings.Contains(err.Error(), "invalid Content Alignment") {
t.Errorf("unexpected error message for invalid content alignment: %v", err)
}
}
func TestRenderWithWrapLimit(t *testing.T) {
longContent := strings.Repeat("word ", 20)
b := NewBox().Padding(2, 0).Style(Single).Color(Green).WrapContent(true).WrapLimit(10)
out, err := b.Render("Wrapped", longContent)
if err != nil {
t.Fatalf("Render with wrapping returned error: %v", err)
}
if !strings.Contains(out, "Wrapped") {
t.Errorf("expected title to appear in wrapped box output")
}
if !strings.Contains(out, "word") {
t.Errorf("expected content to appear in wrapped box output")
}
}
func TestRenderWithVariousColorFormats(t *testing.T) {
title := "Color Formats"
content := "content"
tests := []struct {
name string
configure func(*Box)
}{
{
name: "short hex #RGB",
configure: func(b *Box) {
b.TitleColor("#0F0")
},
},
{
name: "full hex #RRGGBB",
configure: func(b *Box) {
b.ContentColor("#00FF00")
},
},
{
name: "rgb:RRRR/GGGG/BBBB",
configure: func(b *Box) {
b.Color("rgb:0000/ffff/0000")
},
},
{
name: "rgba:RRRR/GGGG/BBBB/AAAA",
configure: func(b *Box) {
b.Color("rgba:ffff/0000/0000/ffff")
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
b := NewBox().Padding(1, 1).Style(Single)
// Apply specific color configuration.
ct := *b
box := &ct
// Configure colors on the copy so one test's colors don't bleed into another.
tc.configure(box)
out, err := box.Render(title, content)
if err != nil {
t.Fatalf("Render returned error for %s: %v", tc.name, err)
}
if out == "" {
t.Fatalf("expected non-empty output for %s", tc.name)
}
if !strings.Contains(out, title) || !strings.Contains(out, content) {
t.Fatalf("rendered output for %s missing title or content: %q", tc.name, out)
}
})
}
}
func TestRenderInvalidColors(t *testing.T) {
title := "Title"
content := "Content"
tests := []struct {
name string
mut func(*Box)
}{
{
name: "invalid title color",
mut: func(b *Box) { b.TitleColor("NotAColor") },
},
{
name: "invalid content color",
mut: func(b *Box) { b.ContentColor("NotAColor") },
},
{
name: "invalid border color",
mut: func(b *Box) { b.Color("NotAColor") },
},
}
for _, tc := range tests {
b := NewBox().Padding(1, 1).Style(Single)
// Apply the specific invalid color configuration.
tc.mut(b)
if _, err := b.Render(title, content); err == nil {
t.Fatalf("%s: expected error for invalid color, got nil", tc.name)
} else if !strings.Contains(err.Error(), "unable to parse color") {
t.Errorf("%s: unexpected error message: %v", tc.name, err)
}
}
}
func TestMustRenderSuccessAndPanic(t *testing.T) {
// Success case: MustRender should not panic when Render succeeds.
t.Run("success", func(t *testing.T) {
b := NewBox().Padding(1, 1).Style(Single)
_ = b.MustRender("Title", "Content")
})
// Panic case: invalid style causes Render to error, hence MustRender panics.
t.Run("panic", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fatalf("expected MustRender to panic for invalid style, but it did not")
}
}()
b := NewBox().Padding(1, 1).Style(BoxStyle("InvalidStyle"))
_ = b.MustRender("Title", "Content")
})
}
func TestRenderEmojiBordersHaveConsistentWidth(t *testing.T) {
b := NewBox().Padding(2, 1)
b.TopLeft("📦").TopRight("📦").BottomLeft("📦").BottomRight("📦").Horizontal("📦").Vertical("📦")
out, err := b.Render("Emoji Box", "With emoji borders")
if err != nil {
t.Fatalf("Render with emoji borders returned error: %v", err)
}
lines := strings.Split(strings.TrimRight(out, "\n"), "\n")
if len(lines) < 3 {
t.Fatalf("expected at least 3 lines in rendered box, got %d", len(lines))
}
top := ansi.Strip(lines[0])
interior := ansi.Strip(lines[1])
bottom := ansi.Strip(lines[len(lines)-1])
topW := runewidth.StringWidth(top)
interiorW := runewidth.StringWidth(interior)
bottomW := runewidth.StringWidth(bottom)
if topW != interiorW || interiorW != bottomW {
t.Fatalf("expected equal visual widths for emoji box borders, got top=%d interior=%d bottom=%d", topW, interiorW, bottomW)
}
}
func TestRenderBoxCustomGlyphsWithoutNewBoxMethod(t *testing.T) {
b := new(Box)
b = b.TopLeft("+").TopRight("+").BottomLeft("+").BottomRight("+").Horizontal("-").Vertical("|")
out, err := b.Render("Custom Glyphs", "Using custom border glyphs")
if err != nil {
t.Fatalf("Render with custom glyphs returned error: %v", err)
}
lines := strings.Split(out, "\n")
if len(lines) < 3 {
t.Fatalf("expected at least 3 lines in rendered box, got %d", len(lines))
}
top := lines[0]
bottom := lines[len(lines)-2]
interior := lines[1 : len(lines)-2]
if !strings.HasPrefix(top, "+") || !strings.HasSuffix(top, "+") {
t.Errorf("top border does not use custom corners: %q", top)
}
if !strings.HasPrefix(bottom, "+") || !strings.HasSuffix(bottom, "+") {
t.Errorf("bottom border does not use custom corners: %q", bottom)
}
for _, line := range interior {
if !strings.HasPrefix(line, "|") || !strings.HasSuffix(line, "|") {
t.Errorf("interior line does not use custom vertical borders: %q", line)
}
}
}