Repository navigation
Expand file tree
/
Copy pathtextwrapper_test.go
More file actions
373 lines (359 loc) · 9.08 KB
/
Copy pathtextwrapper_test.go
File metadata and controls
373 lines (359 loc) · 9.08 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
package vaxis
import (
"testing"
)
func TestWrapSegments(t *testing.T) {
tests := []struct {
name string
segments []Segment
width int
expected [][]Segment
}{
{
name: "simple text fits on one line",
segments: []Segment{
{Text: "Hello world", Style: Style{}},
},
width: 20,
expected: [][]Segment{
{{Text: "Hello world", Style: Style{}}},
},
},
{
name: "text wraps at word boundary",
segments: []Segment{
{Text: "Hello world this is a test", Style: Style{}},
},
width: 15,
// Should break at word boundaries, not mid-word.
expected: [][]Segment{
{{Text: "Hello world ", Style: Style{}}},
{{Text: "this is a test", Style: Style{}}},
},
},
{
name: "CJK characters double width",
segments: []Segment{
// "Hello " = 6 chars, "世界" = 4 cells (2 chars × 2 width each).
{Text: "Hello 世界", Style: Style{}},
},
width: 10,
expected: [][]Segment{
{{Text: "Hello 世界", Style: Style{}}},
},
},
{
name: "CJK characters wrap correctly",
segments: []Segment{
// Each character is 2 cells wide.
{Text: "我是雨果", Style: Style{}},
},
width: 4,
expected: [][]Segment{
{{Text: "我是", Style: Style{}}},
{{Text: "雨果", Style: Style{}}},
},
},
{
name: "emoji sequences handled correctly",
segments: []Segment{
// "Hello " = 6, "🏳️🌈" = 2 cells, " world" = 6
{Text: "Hello 🏳️🌈 world", Style: Style{}},
},
width: 15,
expected: [][]Segment{
{{Text: "Hello 🏳️🌈 world", Style: Style{}}},
},
// TODO: test with this emoji at index == width - 1.
},
{
name: "URL too long to fit - splits with unique ID",
segments: []Segment{
{
Text: "https://example.com/very/long/path/that/does/not/fit",
Style: Style{
Hyperlink: "https://example.com/very/long/path/that/does/not/fit",
},
},
},
width: 20,
expected: [][]Segment{
{{
Text: "https://example.com/",
Style: Style{
Hyperlink: "https://example.com/very/long/path/that/does/not/fit",
HyperlinkParams: "id=1",
},
}},
{{
Text: "very/long/path/that/",
Style: Style{
Hyperlink: "https://example.com/very/long/path/that/does/not/fit",
HyperlinkParams: "id=1",
},
}},
{{
Text: "does/not/fit",
Style: Style{
Hyperlink: "https://example.com/very/long/path/that/does/not/fit",
HyperlinkParams: "id=1",
},
}},
},
},
{
name: "multiple segments with different styles",
segments: []Segment{
{Text: "Normal ", Style: Style{}},
{Text: "bold", Style: Style{Attribute: AttrBold}},
{Text: " text", Style: Style{}},
},
width: 20,
expected: [][]Segment{
{
{Text: "Normal ", Style: Style{}},
{Text: "bold", Style: Style{Attribute: AttrBold}},
{Text: " text", Style: Style{}},
},
},
},
{
name: "URL with text wraps preserving segments",
segments: []Segment{
{Text: "Check out ", Style: Style{}},
{
Text: "https://example.com/path",
Style: Style{Hyperlink: "https://example.com/path"},
},
{Text: " for more", Style: Style{}},
},
width: 20,
expected: [][]Segment{
// FIXME: we shouldn't break URLs like this?
{{Text: "Check out ", Style: Style{}}},
{{
Text: "https://example.com/",
Style: Style{
Hyperlink: "https://example.com/path",
HyperlinkParams: "id=1",
},
}},
{
{
Text: "path",
Style: Style{
Hyperlink: "https://example.com/path",
HyperlinkParams: "id=1",
},
},
{Text: " for more", Style: Style{}},
},
},
},
{
name: "empty segments",
segments: []Segment{
{Text: "", Style: Style{}},
},
width: 10,
expected: [][]Segment{
{{Text: "", Style: Style{}}},
},
},
{
name: "zero width",
segments: []Segment{
{Text: "text", Style: Style{}},
},
width: 0,
expected: [][]Segment{},
},
{
name: "combining characters don't add width",
segments: []Segment{
// Hint: these are five bytes.
{Text: "café", Style: Style{}},
}, // e + ´ = é
width: 4,
expected: [][]Segment{
{{Text: "café", Style: Style{}}},
},
},
{
// Breaking after a hyphen is technically correct 🤷
name: "break at hyphen",
segments: []Segment{
{Text: "foo-bar-baz", Style: Style{}},
},
width: 8,
expected: [][]Segment{
{{Text: "foo-bar-", Style: Style{}}},
{{Text: "baz", Style: Style{}}},
},
},
{
name: "multiple URLs each get unique ID when split",
segments: []Segment{
{
Text: "https://first.example.com/very/long/path",
Style: Style{Hyperlink: "https://first.example.com/very/long/path"},
},
{Text: " and ", Style: Style{}},
{
Text: "https://second.example.com/also/very/long",
Style: Style{Hyperlink: "https://second.example.com/also/very/long"},
},
},
width: 20,
expected: [][]Segment{
{{
Text: "https://",
Style: Style{
Hyperlink: "https://first.example.com/very/long/path",
HyperlinkParams: "id=1",
},
}},
{{
Text: "first.example.com/",
Style: Style{
Hyperlink: "https://first.example.com/very/long/path",
HyperlinkParams: "id=1",
},
}},
{
{
Text: "very/long/path",
Style: Style{
Hyperlink: "https://first.example.com/very/long/path",
HyperlinkParams: "id=1",
},
},
{Text: " and ", Style: Style{}},
},
{{
Text: "https://",
Style: Style{
Hyperlink: "https://second.example.com/also/very/long",
HyperlinkParams: "id=2",
},
}},
{{
Text: "second.example.com/",
Style: Style{
Hyperlink: "https://second.example.com/also/very/long",
HyperlinkParams: "id=2",
},
}},
{{
Text: "also/very/long",
Style: Style{
Hyperlink: "https://second.example.com/also/very/long",
HyperlinkParams: "id=2",
},
}},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
wrapper := NewSegmentWrapper(tt.width)
for _, seg := range tt.segments {
if err := wrapper.AddSegment(seg); err != nil {
t.Fatalf("AddSegment failed: %v", err)
}
}
result := wrapper.Lines()
if len(result) != len(tt.expected) {
t.Errorf("expected %d lines, got %d", len(tt.expected), len(result))
for i, line := range result {
t.Logf(" line %d: %d segments", i, len(line))
for j, seg := range line {
t.Logf(" seg %d: %q (link=%q, params=%q, attr=%v)",
j, seg.Text, seg.Style.Hyperlink, seg.Style.HyperlinkParams, seg.Style.Attribute)
}
}
return
}
for lineIdx, expected := range tt.expected {
got := result[lineIdx]
if len(got) != len(expected) {
t.Errorf("line %d: expected %d segments, got %d",
lineIdx, len(expected), len(got))
for j, seg := range got {
t.Logf(" actual seg %d: %q (link=%q, params=%q)",
j, seg.Text, seg.Style.Hyperlink, seg.Style.HyperlinkParams)
}
for j, seg := range expected {
t.Logf(" expected seg %d: %q (link=%q, params=%q)",
j, seg.Text, seg.Style.Hyperlink, seg.Style.HyperlinkParams)
}
continue
}
for segIdx, expectedSeg := range expected {
actualSeg := got[segIdx]
if actualSeg.Text != expectedSeg.Text {
t.Errorf("line %d, segment %d: expected text %q, got %q",
lineIdx, segIdx, expectedSeg.Text, actualSeg.Text)
}
if actualSeg.Style.Hyperlink != expectedSeg.Style.Hyperlink {
t.Errorf("line %d, segment %d: expected hyperlink %q, got %q",
lineIdx, segIdx, expectedSeg.Style.Hyperlink, actualSeg.Style.Hyperlink)
}
if actualSeg.Style.HyperlinkParams != expectedSeg.Style.HyperlinkParams {
t.Errorf("line %d, segment %d: expected params %q, got %q",
lineIdx, segIdx, expectedSeg.Style.HyperlinkParams, actualSeg.Style.HyperlinkParams)
}
if actualSeg.Style.Attribute != expectedSeg.Style.Attribute {
t.Errorf("line %d, segment %d: expected attribute %v, got %v",
lineIdx, segIdx, expectedSeg.Style.Attribute, actualSeg.Style.Attribute)
}
}
}
})
}
}
func TestSegmentWidth(t *testing.T) {
tests := []struct {
text string
expectedWidth int
}{
{
text: "Hello",
expectedWidth: 5,
},
{
text: "世界", // Each chinese character is width=2
expectedWidth: 4,
},
{
text: "Hello世界", // Each chinese character is width=2
expectedWidth: 9,
},
{
text: "🏳️🌈",
expectedWidth: 2,
},
{
text: "👨👩👧👦",
expectedWidth: 2,
},
{
text: "é", // e + ´
expectedWidth: 1,
},
{
text: "a\u200Bb", // a + zero-width space + b
expectedWidth: 2,
},
}
for _, tt := range tests {
t.Run(tt.text, func(t *testing.T) {
seg := Segment{Text: tt.text}
width := segmentWidth(seg)
if width != tt.expectedWidth {
t.Errorf("expected width %d, got %d for text %q",
tt.expectedWidth, width, tt.text)
}
})
}
}