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
|
package tui
import (
"bytes"
"strings"
"testing"
)
func TestSprint(t *testing.T) {
type tc struct {
el *Element
opts []PrintOption
contains []string
empty bool
}
tests := map[string]tc{
"basic text element": {
el: New(WithText("hello world")),
opts: []PrintOption{WithPrintWidth(40)},
contains: []string{"hello world"},
},
"explicit width": {
el: func() *Element {
parent := New(WithDirection(Row))
parent.AddChild(New(WithText("left")))
return parent
}(),
opts: []PrintOption{WithPrintWidth(20)},
contains: []string{"left"},
},
"nested with borders": {
el: New(WithText("boxed"), WithBorder(BorderSingle)),
opts: []PrintOption{WithPrintWidth(20)},
contains: []string{"boxed", "┌", "└"},
},
"styled text": {
el: New(WithText("red"), WithTextStyle(NewStyle().Foreground(Red))),
opts: []PrintOption{WithPrintWidth(20)},
contains: []string{"\x1b[", "red", "\x1b[0m"},
},
"empty element": {
el: New(),
opts: []PrintOption{WithPrintWidth(20)},
empty: true,
},
"multiline content": {
el: func() *Element {
parent := New(WithDirection(Column))
parent.AddChild(
New(WithText("line 1")),
New(WithText("line 2")),
)
return parent
}(),
opts: []PrintOption{WithPrintWidth(30)},
contains: []string{"line 1", "line 2"},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
result := Sprint(tt.el, tt.opts...)
if tt.empty {
if result != "" {
t.Fatalf("expected empty result, got %q", result)
}
return
}
if result == "" {
t.Fatal("expected non-empty result, got empty")
}
for _, sub := range tt.contains {
if !strings.Contains(result, sub) {
t.Errorf("result does not contain %q\ngot: %q", sub, result)
}
}
})
}
}
func TestFprint(t *testing.T) {
type tc struct {
el *Element
contains []string
empty bool
}
tests := map[string]tc{
"writes to buffer": {
el: New(WithText("hello")),
contains: []string{"hello"},
},
"empty element writes nothing": {
el: New(),
empty: true,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
var buf bytes.Buffer
Fprint(&buf, tt.el, WithPrintWidth(30))
result := buf.String()
if tt.empty {
if result != "" {
t.Fatalf("expected empty output, got %q", result)
}
return
}
if result == "" {
t.Fatal("expected non-empty output, got empty")
}
for _, sub := range tt.contains {
if !strings.Contains(result, sub) {
t.Errorf("output does not contain %q\ngot: %q", sub, result)
}
}
})
}
}
func TestFprintTrailingNewline(t *testing.T) {
var buf bytes.Buffer
Fprint(&buf, New(WithText("hi")), WithPrintWidth(10))
result := buf.String()
if !strings.HasSuffix(result, "\n") {
t.Errorf("Fprint output should end with newline, got %q", result)
}
}
func TestSprintNoTrailingNewline(t *testing.T) {
result := Sprint(New(WithText("hi")), WithPrintWidth(10))
if strings.HasSuffix(result, "\n") {
t.Errorf("Sprint output should NOT end with newline, got %q", result)
}
}
|