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
|
package tui
import (
"strings"
"testing"
)
func TestBuffer_SetString_Cluster(t *testing.T) {
b := NewBuffer(10, 1)
b.SetString(0, 0, emojiFlagUS+"x", NewStyle())
lead := b.Cell(0, 0)
if cellGlyph(lead) != emojiFlagUS {
t.Errorf("cell(0,0) glyph = %q, want the whole flag %q", cellGlyph(lead), emojiFlagUS)
}
if lead.Width != 2 {
t.Errorf("cell(0,0).Width = %d, want 2", lead.Width)
}
if !b.Cell(1, 0).IsContinuation() {
t.Errorf("cell(1,0) should be a continuation cell")
}
if got := b.Cell(2, 0).Rune; got != 'x' {
t.Errorf("cell(2,0).Rune = %q, want 'x' (flag is 2 cols wide, not 4)", got)
}
if line := b.String(); !strings.Contains(line, emojiFlagUS) {
t.Errorf("buffer text %q does not contain the flag cluster", line)
}
}
func TestBuffer_SetStringGradient_Cluster(t *testing.T) {
b := NewBuffer(10, 1)
g := NewGradient(Red, Blue)
w := b.SetStringGradient(0, 0, emojiFlagUS+"x", g, NewStyle())
if w != 3 {
t.Errorf("SetStringGradient returned width %d, want 3 (flag 2 + x 1)", w)
}
lead := b.Cell(0, 0)
if cellGlyph(lead) != emojiFlagUS || lead.Width != 2 {
t.Errorf("cell(0,0) = {%q, w%d}, want the flag at width 2", cellGlyph(lead), lead.Width)
}
if !b.Cell(1, 0).IsContinuation() {
t.Errorf("cell(1,0) should be a continuation cell")
}
if got := b.Cell(2, 0).Rune; got != 'x' {
t.Errorf("cell(2,0).Rune = %q, want 'x'", got)
}
if lead.Style.Fg.IsDefault() {
t.Errorf("gradient color was not applied to the flag cell")
}
}
func TestWrapText_KeepsClustersWhole(t *testing.T) {
type tc struct {
text string
width int
want []string
}
tests := map[string]tc{
"flag plus text fits one line": {text: emojiFlagUS + "abcd", width: 6, want: []string{emojiFlagUS + "abcd"}},
"break between clusters": {text: "a" + emojiFlagUS + "b", width: 2, want: []string{"a", emojiFlagUS, "b"}},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got := wrapText(tt.text, tt.width)
if len(got) != len(tt.want) {
t.Fatalf("wrapText(%q, %d) = %q, want %q", tt.text, tt.width, got, tt.want)
}
for i := range got {
if got[i] != tt.want[i] {
t.Errorf("line[%d] = %q, want %q", i, got[i], tt.want[i])
}
}
})
}
}
func TestRenderTree_ClusterBoxNoWrap(t *testing.T) {
buf := NewBuffer(8, 5)
box := New(
WithWidth(8),
WithBorder(BorderSingle),
WithText(emojiFlagUS+"abcd"),
WithWrap(false),
)
Calculate(box, 8, 3)
RenderTree(buf, box)
line := buf.StringTrimmed()
if !strings.Contains(line, emojiFlagUS+"abcd") {
t.Errorf("box content %q should contain the full text on one line", line)
}
}
func TestCellGlyph(t *testing.T) {
c1 := NewCell('a', NewStyle())
if g := cellGlyph(c1); g != "a" {
t.Errorf("cellGlyph(single rune) = %q, want %q", g, "a")
}
c2 := newClusterCell(emojiFlagUS, 2, NewStyle(), "")
g2 := cellGlyph(c2)
if g2 != emojiFlagUS {
t.Errorf("cellGlyph(cluster) = %q, want %q", g2, emojiFlagUS)
}
c3 := Cell{}
if g3 := cellGlyph(c3); g3 != "" {
t.Errorf("cellGlyph(empty) = %q, want empty string", g3)
}
}
func TestWrapInlineStyledRows_ASCII(t *testing.T) {
type tc struct {
name string
text string
width int
want []string
}
tests := []tc{
{name: "empty", text: "", width: 10, want: []string{""}},
{name: "fits on one line", text: "hello", width: 10, want: []string{"hello"}},
{name: "wraps at boundary", text: "hello world", width: 5, want: []string{"hello", " worl", "d"}},
{name: "newline breaks", text: "ab\ncd", width: 10, want: []string{"ab", "cd"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := wrapInlineStyledRows(tt.text, tt.width)
if len(got) != len(tt.want) {
t.Fatalf("wrapInlineStyledRows = %q, want %q", got, tt.want)
}
for i := range got {
if got[i] != tt.want[i] {
t.Errorf("row[%d] = %q, want %q", i, got[i], tt.want[i])
}
}
})
}
}
func TestWrapInlineStyledRows_ANSI(t *testing.T) {
type tc struct {
name string
text string
width int
want []string
}
tests := []tc{
{name: "ansi bold", text: "\x1b[1mhello\x1b[0m", width: 10, want: []string{"\x1b[1mhello\x1b[0m"}},
{name: "ansi wraps", text: "\x1b[31mhello world\x1b[0m", width: 5, want: []string{"\x1b[31mhello", " worl", "d\x1b[0m"}},
{name: "ansi at boundary", text: "ab\x1b[1mcdef\x1b[0m", width: 4, want: []string{"ab\x1b[1mcd", "ef\x1b[0m"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := wrapInlineStyledRows(tt.text, tt.width)
if len(got) != len(tt.want) {
t.Fatalf("wrapInlineStyledRows = %q, want %q", got, tt.want)
}
for i := range got {
if got[i] != tt.want[i] {
t.Errorf("row[%d] = %q, want %q", i, got[i], tt.want[i])
}
}
})
}
}
func TestWrapInlineStyledRows_Cluster(t *testing.T) {
type tc struct {
name string
text string
width int
want []string
}
tests := []tc{
{name: "flag plus ascii", text: emojiFlagUS + "abcd", width: 6, want: []string{emojiFlagUS + "abcd"}},
{name: "flag wraps atomic", text: "a" + emojiFlagUS + "b", width: 2, want: []string{"a", emojiFlagUS, "b"}},
{name: "cjk wraps", text: "你好世界", width: 4, want: []string{"你好", "世界"}},
{name: "ansi flag", text: "\x1b[33m" + emojiFlagUS + "\x1b[0m", width: 4, want: []string{"\x1b[33m" + emojiFlagUS + "\x1b[0m"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := wrapInlineStyledRows(tt.text, tt.width)
if len(got) != len(tt.want) {
t.Fatalf("wrapInlineStyledRows = %q, want %q", got, tt.want)
}
for i := range got {
if got[i] != tt.want[i] {
t.Errorf("row[%d] = %q, want %q", i, got[i], tt.want[i])
}
}
})
}
}
func TestWrapInlineStyledRows_Oversized(t *testing.T) {
type tc struct {
name string
text string
width int
want []string
}
tests := []tc{
{name: "cjk at width 1", text: "你", width: 1, want: []string{"?"}},
{name: "cjk at width 2", text: "你", width: 2, want: []string{"你"}},
{name: "flag at width 1", text: emojiFlagUS, width: 1, want: []string{"?"}},
{name: "ascii then cjk at width 1", text: "a你", width: 1, want: []string{"a", "?"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := wrapInlineStyledRows(tt.text, tt.width)
if len(got) != len(tt.want) {
t.Fatalf("wrapInlineStyledRows = %q, want %q", got, tt.want)
}
for i := range got {
if got[i] != tt.want[i] {
t.Errorf("row[%d] = %q, want %q", i, got[i], tt.want[i])
}
}
})
}
}
|