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
|
package tui
import (
"strings"
"testing"
)
func TestNextCluster_EdgeCases(t *testing.T) {
type tc struct {
name string
input string
wantCl string
wantW int
wantSz int
}
cases := []tc{
{name: "empty", input: "", wantCl: "", wantW: 0, wantSz: 0},
{name: "single ascii", input: "a", wantCl: "a", wantW: 1, wantSz: 1},
{name: "two ascii", input: "ab", wantCl: "a", wantW: 1, wantSz: 1},
{name: "ascii then emoji", input: "a\U0001F680", wantCl: "a", wantW: 1, wantSz: 1},
{name: "single cjk", input: "世", wantCl: "世", wantW: 2, wantSz: 3},
{name: "cjk then ascii", input: "世a", wantCl: "世", wantW: 2, wantSz: 3},
{name: "zwj two emoji", input: "\U0001F468\U0001F469", wantCl: "\U0001F468\U0001F469", wantW: 2, wantSz: 11},
{name: "lone RI", input: "\U0001F1FAx", wantCl: "\U0001F1FA", wantW: 2, wantSz: 4},
{name: "leading combining", input: "́a", wantCl: "́", wantW: 1, wantSz: 2},
{name: "zwnj between ascii", input: "ab", wantCl: "a", wantW: 1, wantSz: 4},
{name: "skin tone", input: "\U0001F44D\U0001F3FD", wantCl: "\U0001F44D\U0001F3FD", wantW: 2, wantSz: 8},
{name: "vs15", input: "☝\U0000FE0Ex", wantCl: "☝\U0000FE0E", wantW: 1, wantSz: 6},
{name: "vs15 narrows wide emoji", input: "\U0001F600\U0000FE0E", wantCl: "\U0001F600\U0000FE0E", wantW: 1, wantSz: 7},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
cl, w, sz := nextCluster(tt.input)
if cl != tt.wantCl || w != tt.wantW || sz != tt.wantSz {
t.Errorf("nextCluster(%q) = (%q, %d, %d), want (%q, %d, %d)",
tt.input, cl, w, sz, tt.wantCl, tt.wantW, tt.wantSz)
}
})
}
}
func TestNextClusterBytes_EdgeCases(t *testing.T) {
w, sz, base := nextClusterBytes(nil)
if w != 0 || sz != 0 || base != 0 {
t.Errorf("nextClusterBytes(nil) = (%d, %d, %d), want (0, 0, 0)", w, sz, base)
}
w, sz, base = nextClusterBytes([]byte{'a'})
if w != 1 || sz != 1 || base != 'a' {
t.Errorf("nextClusterBytes('a') = (%d, %d, %c), want (1, 1, a)", w, sz, base)
}
w, sz, base = nextClusterBytes([]byte("ab"))
if w != 1 || sz != 1 || base != 'a' {
t.Errorf("nextClusterBytes('ab') = (%d, %d, %c), want (1, 1, a)", w, sz, base)
}
w, sz, base = nextClusterBytes([]byte(emojiFlagUS))
if w != 2 || sz != 8 || base != 0x1F1FA {
t.Errorf("nextClusterBytes(flag) = (%d, %d, %x), want (2, 8, 1F1FA)", w, sz, base)
}
}
func TestClusterCount_EdgeCases(t *testing.T) {
type tc struct {
name string
s string
want int
}
cases := []tc{
{name: "empty", s: "", want: 0},
{name: "ascii", s: "hello", want: 5},
{name: "flag", s: emojiFlagUS, want: 1},
{name: "flag x flag", s: emojiFlagUS + "x" + emojiFlagUS, want: 3},
{name: "family", s: emojiFamily, want: 1},
{name: "mixed", s: "a" + emojiFamily + "b" + emojiFlagUS, want: 4},
{name: "accent", s: accentE, want: 1},
{name: "cjk", s: "你好世界", want: 4},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
if got := clusterCount(tt.s); got != tt.want {
t.Errorf("clusterCount(%q) = %d, want %d", tt.s, got, tt.want)
}
})
}
}
func TestBuffer_SetString_Overlap(t *testing.T) {
b := NewBuffer(10, 1)
b.SetString(0, 0, "你好", NewStyle())
b.SetString(1, 0, "x", NewStyle())
if got := b.Cell(1, 0).Rune; got != 'x' {
t.Errorf("cell(1,0).Rune = %q, want 'x'", got)
}
}
func TestBuffer_SetStringGradient_ClusterMulti(t *testing.T) {
b := NewBuffer(20, 1)
g := NewGradient(Red, Blue)
text := "a" + emojiFlagUS + "b" + accentE + "c" + emojiFamily + "d"
total := b.SetStringGradient(0, 0, text, g, NewStyle())
if total != 9 {
t.Errorf("SetStringGradient returned width %d, want 9", total)
}
for x := range total {
c := b.Cell(x, 0)
if c.IsContinuation() || c.Rune == 0 {
continue
}
if c.Style.Fg.IsDefault() {
t.Errorf("cell(%d,0).Fg is default for rune %q", x, c.Rune)
}
}
}
func TestInput_CombiningInsert(t *testing.T) {
inp := newTestInput(WithInputWidth(10))
inp.text.Set("a")
inp.cursorPos.Set(1)
inp.focused.Set(true)
inp.blink.Set(true)
inp.HandleEvent(KeyEvent{Key: KeyRune, Rune: '́'})
if text := inp.Text(); text != "á" {
t.Errorf("text = %q, want %q", text, "á")
}
if got := inp.cursorPos.Get(); got != 2 {
t.Errorf("cursorPos = %d, want 2", got)
}
}
func TestInput_ZWJInsert(t *testing.T) {
inp := newTestInput(WithInputWidth(10))
inp.SetText("a")
inp.focused.Set(true)
inp.blink.Set(true)
inp.HandleEvent(KeyEvent{Key: KeyRune, Rune: '\U0001F468'})
inp.HandleEvent(KeyEvent{Key: KeyRune, Rune: ''})
inp.HandleEvent(KeyEvent{Key: KeyRune, Rune: '\U0001F469'})
want := "a\U0001F468\U0001F469"
if text := inp.Text(); text != want {
t.Errorf("text = %q, want %q", text, want)
}
}
func TestBuffer_String_Combining(t *testing.T) {
inputs := []struct {
name string
text string
}{
{"flag", emojiFlagUS},
{"family", emojiFamily},
{"accent", accentE},
{"mixed", "a" + emojiFlagUS + "b" + accentE + "c" + emojiFamily + "d"},
{"cjk", "你好世界"},
{"keycap", emojiKeycap1},
{"heart+vs16", emojiHeart},
}
for _, tt := range inputs {
t.Run(tt.name, func(t *testing.T) {
b := NewBuffer(30, 1)
b.SetString(0, 0, tt.text, NewStyle())
if s := b.String(); !strings.Contains(s, tt.text) {
t.Errorf("String() = %q, should contain %q", s, tt.text)
}
if st := b.StringTrimmed(); !strings.Contains(st, tt.text) {
t.Errorf("StringTrimmed() = %q, should contain %q", st, tt.text)
}
})
}
}
func TestSegmentStyledRunes_MultiRune(t *testing.T) {
styleA := NewStyle().Bold()
styleB := NewStyle().Italic()
rs := []styledRune{{r: 'a', st: styleA}, {r: 0x0301, st: styleB}}
clusters := segmentStyledRunes(rs)
if len(clusters) != 1 {
t.Fatalf("segmentStyledRunes returned %d clusters, want 1", len(clusters))
}
if clusters[0].st != styleA {
t.Errorf("cluster style should be base rune's style")
}
if clusters[0].text != "á" {
t.Errorf("cluster text = %q, want %q", clusters[0].text, "á")
}
if clusters[0].width != 1 {
t.Errorf("cluster width = %d, want 1", clusters[0].width)
}
}
func TestSegmentStyledRunes_Flag(t *testing.T) {
styleA := NewStyle().Bold()
rs := []styledRune{{r: 0x1F1FA, st: styleA}, {r: 0x1F1F8, st: styleA}}
clusters := segmentStyledRunes(rs)
if len(clusters) != 1 {
t.Fatalf("segmentStyledRunes returned %d clusters, want 1", len(clusters))
}
if clusters[0].text != emojiFlagUS {
t.Errorf("cluster text = %q, want US flag", clusters[0].text)
}
if clusters[0].width != 2 {
t.Errorf("cluster width = %d, want 2", clusters[0].width)
}
}
func TestNewClusterCell_EdgeCases(t *testing.T) {
c := newClusterCell("", 1, NewStyle(), "")
if c.Rune != 0 || c.Combining != "" {
t.Errorf("empty cell: Rune=%d Combining=%q", c.Rune, c.Combining)
}
c = newClusterCell("a", 0, NewStyle(), "")
if c.Rune != 0 || c.Combining != "" {
t.Errorf("width-0 cell should be empty")
}
c = newClusterCell("你", 2, NewStyle(), "")
if c.Rune != '你' || c.Combining != "" {
t.Errorf("single CJK: Rune=%c Combining=%q", c.Rune, c.Combining)
}
}
func TestSnapRuneToClusterStart_AllCases(t *testing.T) {
type tc struct {
name string
s string
idx int
want int
}
cases := []tc{
{name: "ascii 0", s: "abc", idx: 0, want: 0},
{name: "ascii 1", s: "abc", idx: 1, want: 1},
{name: "ascii 2", s: "abc", idx: 2, want: 2},
{name: "ascii 3", s: "abc", idx: 3, want: 3},
{name: "inside flag", s: "x" + emojiFlagUS + "y", idx: 2, want: 1},
{name: "at flag start", s: "x" + emojiFlagUS + "y", idx: 1, want: 1},
{name: "at flag end", s: "x" + emojiFlagUS + "y", idx: 3, want: 3},
{name: "inside accent", s: "caf" + accentE, idx: 4, want: 3},
{name: "negative", s: "abc", idx: -1, want: 0},
{name: "past end", s: "abc", idx: 99, want: 3},
{name: "empty", s: "", idx: 0, want: 0},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
if got := snapRuneToClusterStart(tt.s, tt.idx); got != tt.want {
t.Errorf("snapRuneToClusterStart(%q, %d) = %d, want %d", tt.s, tt.idx, got, tt.want)
}
})
}
}
func TestRuneIndexToDisplayCol_AllCases(t *testing.T) {
type tc struct {
name string
s string
idx int
want int
}
cases := []tc{
{name: "ascii 0", s: "abc", idx: 0, want: 0},
{name: "ascii 1", s: "abc", idx: 1, want: 1},
{name: "ascii 3", s: "abc", idx: 3, want: 3},
{name: "cjk 0", s: "你好", idx: 0, want: 0},
{name: "cjk 1", s: "你好", idx: 1, want: 2},
{name: "cjk 2", s: "你好", idx: 2, want: 4},
{name: "flag 0", s: emojiFlagUS + "x", idx: 0, want: 0},
{name: "flag inside", s: emojiFlagUS + "x", idx: 1, want: 0},
{name: "flag after", s: emojiFlagUS + "x", idx: 2, want: 2},
{name: "flag past", s: emojiFlagUS + "x", idx: 3, want: 3},
{name: "zwj 0", s: emojiFamily + "x", idx: 0, want: 0},
{name: "zwj inside", s: emojiFamily + "x", idx: 3, want: 0},
{name: "zwj at boundary", s: emojiFamily + "x", idx: 7, want: 2},
{name: "zwj after", s: emojiFamily + "x", idx: 8, want: 3},
{name: "accent inside", s: "caf" + accentE, idx: 4, want: 3},
{name: "accent at end", s: "caf" + accentE, idx: 5, want: 4},
{name: "empty", s: "", idx: 0, want: 0},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
if got := runeIndexToDisplayCol(tt.s, tt.idx); got != tt.want {
t.Errorf("runeIndexToDisplayCol(%q, %d) = %d, want %d", tt.s, tt.idx, got, tt.want)
}
})
}
}
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])
}
}
})
}
}
func TestInput_WideGlyphAtRightEdge(t *testing.T) {
type tc struct {
name string
focused bool
}
cases := []tc{
{name: "unfocused", focused: false},
{name: "focused", focused: true},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
inp := newTestInput(WithInputWidth(5))
inp.SetText("abcd你ef")
inp.cursorPos.Set(0)
inp.scrollPos.Set(0)
inp.focused.Set(tt.focused)
window := inp.visibleWidth()
if tt.focused {
window++
}
got := inp.displayText()
if w := stringWidth(got); w > window {
t.Errorf("displayText() = %q is %d cols, exceeds viewport %d", got, w, window)
}
if strings.ContainsRune(got, '你') {
t.Errorf("displayText() = %q should omit 你 (does not fit at the edge)", got)
}
})
}
}
|