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
|
package tui
import (
"strings"
"testing"
"unicode/utf8"
)
func clusterRuneStarts(s string) []int {
starts := []int{0}
runeIdx := 0
for len(s) > 0 {
_, _, size := nextCluster(s)
if size == 0 {
break
}
for i := 0; i < size; {
_, rs := utf8.DecodeRuneInString(s[i:])
if rs == 0 {
break
}
i += rs
runeIdx++
}
starts = append(starts, runeIdx)
s = s[size:]
}
return starts
}
const (
emojiRocket = "\U0001F680"
emojiFlagUS = "\U0001F1FA\U0001F1F8"
emojiThumbs = "\U0001F44D\U0001F3FD"
emojiFamily = "\U0001F468\u200d\U0001F469\u200d\U0001F467\u200d\U0001F466"
emojiHeart = "\u2764\U0000FE0F"
emojiKeycap1 = "1\U0000FE0F\u20E3"
accentE = "e\u0301"
)
func TestNextCluster_SegmentationAndWidth(t *testing.T) {
type tc struct {
in string
clusters []string
width int
}
tests := map[string]tc{
"ascii": {in: "ab", clusters: []string{"a", "b"}, width: 2},
"cjk": {in: "你好", clusters: []string{"你", "好"}, width: 4},
"single emoji": {in: emojiRocket, clusters: []string{emojiRocket}, width: 2},
"flag": {in: emojiFlagUS, clusters: []string{emojiFlagUS}, width: 2},
"skin tone": {in: emojiThumbs, clusters: []string{emojiThumbs}, width: 2},
"zwj family": {in: emojiFamily, clusters: []string{emojiFamily}, width: 2},
"heart vs16": {in: emojiHeart, clusters: []string{emojiHeart}, width: 2},
"keycap": {in: emojiKeycap1, clusters: []string{emojiKeycap1}, width: 2},
"decomposed accent": {in: accentE, clusters: []string{accentE}, width: 1},
"decomposed in word": {in: "caf" + accentE, clusters: []string{"c", "a", "f", accentE}, width: 4},
"zwsp does not glue": {in: "a\u200bb", clusters: []string{"a", "\u200b", "b"}, width: 3},
"three regional indicators": {
in: emojiFlagUS + "\U0001F1EB",
clusters: []string{emojiFlagUS, "\U0001F1EB"},
width: 4,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
var got []string
total := 0
for rest := tt.in; len(rest) > 0; {
cl, w, size := nextCluster(rest)
if size == 0 {
t.Fatalf("nextCluster(%q) returned size 0", rest)
}
got = append(got, cl)
total += w
rest = rest[size:]
}
if len(got) != len(tt.clusters) {
t.Fatalf("clusters = %q, want %q", got, tt.clusters)
}
for i := range got {
if got[i] != tt.clusters[i] {
t.Errorf("cluster[%d] = %q, want %q", i, got[i], tt.clusters[i])
}
}
if strings.Join(got, "") != tt.in {
t.Errorf("clusters %q do not reconstruct input %q", got, tt.in)
}
if total != tt.width {
t.Errorf("summed cluster width = %d, want %d", total, tt.width)
}
if sw := stringWidth(tt.in); sw != tt.width {
t.Errorf("stringWidth(%q) = %d, want %d", tt.in, sw, tt.width)
}
})
}
}
func TestStringWidth_Clusters(t *testing.T) {
type tc struct {
in string
width int
}
tests := map[string]tc{
"flag": {in: emojiFlagUS, width: 2},
"skin tone": {in: emojiThumbs, width: 2},
"zwj family": {in: emojiFamily, width: 2},
"decomposed": {in: "caf" + accentE, width: 4},
"flag in text": {in: "x" + emojiFlagUS + "y", width: 4},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
if got := stringWidth(tt.in); got != tt.width {
t.Errorf("stringWidth(%q) = %d, want %d", tt.in, got, tt.width)
}
})
}
}
func TestNextClusterBytes_MatchesNextCluster(t *testing.T) {
inputs := map[string]string{
"ascii": "ab",
"cjk": "你好",
"rocket": emojiRocket,
"flag": emojiFlagUS,
"skin tone": emojiThumbs,
"zwj family": emojiFamily,
"heart vs16": emojiHeart,
"keycap": emojiKeycap1,
"accent": accentE,
"zwsp": "a\u200bb",
}
for name, in := range inputs {
t.Run(name, func(t *testing.T) {
for rest := in; len(rest) > 0; {
cl, w, size := nextCluster(rest)
bw, bsize, base := nextClusterBytes([]byte(rest))
if bw != w || bsize != size {
t.Fatalf("nextClusterBytes = (w=%d size=%d), nextCluster = (w=%d size=%d)", bw, bsize, w, size)
}
wantBase, _ := utf8.DecodeRuneInString(cl)
if base != wantBase {
t.Errorf("base rune = %q, want %q", base, wantBase)
}
rest = rest[size:]
}
})
}
}
func TestClusterRuneStarts(t *testing.T) {
type tc struct {
in string
want []int
}
tests := map[string]tc{
"ascii": {in: "ab", want: []int{0, 1, 2}},
"family is one cluster": {in: emojiFamily, want: []int{0, 7}},
"decomposed in word": {in: "caf" + accentE, want: []int{0, 1, 2, 3, 5}},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got := clusterRuneStarts(tt.in)
if len(got) != len(tt.want) {
t.Fatalf("clusterRuneStarts(%q) = %v, want %v", tt.in, got, tt.want)
}
for i := range got {
if got[i] != tt.want[i] {
t.Fatalf("clusterRuneStarts(%q) = %v, want %v", tt.in, got, tt.want)
}
}
})
}
}
|