Debian Codegraph @

comparison/behavior_test.go

raw symbols ◀ ▶ SCIP
2 Debian patches touch this file
1 package comparison
2
3 import (
4 "testing"
5
6 "github.com/clipperhouse/displaywidth"
7 "github.com/mattn/go-runewidth"
8 "github.com/rivo/uniseg"
9 )
10
11 func TestLibraryBehaviorComparison(t *testing.T) {
12 testCases := []struct {
13 name string
14 input string
15 expected map[string]int // library -> expected width
16 }{
17 // Basic ASCII
18 {
19 name: "ASCII text",
20 input: "Hello World",
21 expected: map[string]int{
22 "displaywidth_default": 11,
23 "displaywidth_options{}": 11,
24 "go-runewidth_default": 11,
25 "uniseg_default": 11,
26 },
27 },
28
29 // East Asian characters
30 {
31 name: "CJK characters",
32 input: "中文",
33 expected: map[string]int{
34 "displaywidth_default": 4,
35 "displaywidth_options{}": 4,
36 "go-runewidth_default": 4,
37 "uniseg_default": 4,
38 },
39 },
40
41 // Ambiguous characters (width depends on EastAsianWidth)
42 {
43 name: "Ambiguous characters",
44 input: "★°±",
45 expected: map[string]int{
46 "displaywidth_default": 3,
47 "displaywidth_options{}": 3,
48 "displaywidth_EAW": 6,
49 "go-runewidth_default": 3,
50 "go-runewidth_EAW": 6,
51 "uniseg_default": 3,
52 "uniseg_EAW": 5, // uniseg behavior is different
53 },
54 },
55
56 // Emoji
57 {
58 name: "Basic emoji",
59 input: "😀🚀🎉",
60 expected: map[string]int{
61 "displaywidth_default": 6,
62 "displaywidth_options{}": 6,
63 "go-runewidth_default": 6,
64 "uniseg_default": 6,
65 },
66 },
67
68 // Unicode 16.0 new emojis
69 {
70 name: "Unicode 16.0 emojis",
71 input: "🫩🫆🪾🫜🪉🪏🫟", // Face with Bags Under Eyes, Fingerprint, Leafless Tree, Root Vegetable, Harp, Shovel, Splatter
72 expected: map[string]int{
73 "displaywidth_default": 14, // 2 per emoji (properly handles Unicode 16.0)
74 "displaywidth_options{}": 14,
•75 "go-runewidth_default": 14, // go-runewidth may not fully support Unicode 16.0 yet (treats as width 1)
76 "uniseg_default": 7, // uniseg may not fully support Unicode 16.0 yet (treats as width 1)
77 },
78 },
79
80 // Regional Indicator Pairs (flags) - the key difference
81 // TODO: 2 is the correct width, that's what Ghostty and iTerm do.
82 // Sadly, Mac Terminal displays width 1. Perhaps we should special-case
83 // it, not sure.
84 {
85 name: "Flags",
86 input: "🇺🇸🇯🇵🇬🇧",
87 expected: map[string]int{
88 "displaywidth_default": 6, // flags are always width 2 (modern standard)
89 "displaywidth_options{}": 6, // same as default
•90 "go-runewidth_default": 6, // go-runewidth treats flags as width 2
91 "go-runewidth_strict_false": 6,
92 "go-runewidth_strict_true": 6, // go-runewidth always returns 2 for flags
93 "uniseg_default": 6, // uniseg treats flags as width 2
94 },
95 },
96
97 // Single Regional Indicator
98 {
99 name: "Single Regional Indicator",
100 input: "🇺",
101 expected: map[string]int{
102 "displaywidth_default": 2,
103 "displaywidth_options{}": 2,
104 "go-runewidth_default": 1,
105 "uniseg_default": 2,
106 },
107 },
108
109 // Variation selectors
110 // ☺️ (U+263A + VS16) = width 2, ⌛︎ (U+231B + VS15) = width 2 (VS15 is no-op), ❤️ (U+2764 + VS16) = width 2
111 {
112 name: "Variation selectors",
113 input: "☺️⌛︎❤️",
114 expected: map[string]int{
115 "displaywidth_default": 6, // 2 + 2 + 2 (VS15 is no-op per Unicode TR51)
116 "displaywidth_options{}": 6,
117 "go-runewidth_default": 4,
118 "uniseg_default": 5, // uniseg still treats VS15 as width 1
119 },
120 },
121
122 // Keycap sequences
123 {
124 name: "Keycap sequences",
125 input: "1️⃣#️⃣",
126 expected: map[string]int{
127 "displaywidth_default": 4,
128 "displaywidth_options{}": 4,
129 "go-runewidth_default": 2,
130 "uniseg_default": 2,
131 },
132 },
133
134 // Mixed content
135 {
136 name: "Mixed content",
137 input: "Hello 世界! 😀🇺🇸",
138 expected: map[string]int{
139 "displaywidth_default": 16, // 6 + 4 + 2 + 2 + 2
140 "displaywidth_options{}": 16, // same as default
•141 "go-runewidth_default": 16, // 6 + 4 + 2 + 2 + 2 (flags are width 2)
142 "uniseg_default": 16, // 6 + 4 + 2 + 2 + 2
143 },
144 },
145
146 // Control characters
147 {
148 name: "Control characters",
149 input: "hello\nworld\t",
150 expected: map[string]int{
151 "displaywidth_default": 10, // newline and tab are width 0
152 "displaywidth_options{}": 10,
153 "go-runewidth_default": 10,
154 "uniseg_default": 10,
155 },
156 },
157 }
158
159 for _, tc := range testCases {
160 t.Run(tc.name, func(t *testing.T) {
161 // Test displaywidth with default options
162 displaywidthDefault := displaywidth.String(tc.input)
163 if expected, ok := tc.expected["displaywidth_default"]; ok {
164 if displaywidthDefault != expected {
165 t.Errorf("displaywidth.String() = %d, want %d", displaywidthDefault, expected)
166 }
167 }
168
169 // Test displaywidth with zero-value options (should behave same as default)
170 displaywidthZero := displaywidth.String(tc.input)
171 if expected, ok := tc.expected["displaywidth_options{}"]; ok {
172 if displaywidthZero != expected {
173 t.Errorf("displaywidth.String() = %d, want %d", displaywidthZero, expected)
174 }
175 }
176
177 // Test displaywidth with EastAsianWidth=true
178 displaywidthEAW := displaywidth.Options{EastAsianWidth: true}.String(tc.input)
179 if expected, ok := tc.expected["displaywidth_EAW"]; ok {
180 if displaywidthEAW != expected {
181 t.Errorf("displaywidth.Options{EastAsianWidth: true}.String() = %d, want %d", displaywidthEAW, expected)
182 }
183 }
184
185 // Test go-runewidth default
186 goRunewidthDefault := runewidth.StringWidth(tc.input)
187 if expected, ok := tc.expected["go-runewidth_default"]; ok {
188 if goRunewidthDefault != expected {
189 t.Errorf("runewidth.StringWidth() = %d, want %d", goRunewidthDefault, expected)
190 }
191 }
192
193 // Test go-runewidth with StrictEmojiNeutral=false
194 goRunewidthStrictFalse := (&runewidth.Condition{StrictEmojiNeutral: false}).StringWidth(tc.input)
195 if expected, ok := tc.expected["go-runewidth_strict_false"]; ok {
196 if goRunewidthStrictFalse != expected {
197 t.Errorf("runewidth.Condition{StrictEmojiNeutral: false}.StringWidth() = %d, want %d", goRunewidthStrictFalse, expected)
198 }
199 }
200
201 // Test go-runewidth with StrictEmojiNeutral=true
202 goRunewidthStrictTrue := (&runewidth.Condition{StrictEmojiNeutral: true}).StringWidth(tc.input)
203 if expected, ok := tc.expected["go-runewidth_strict_true"]; ok {
204 if goRunewidthStrictTrue != expected {
205 t.Errorf("runewidth.Condition{StrictEmojiNeutral: true}.StringWidth() = %d, want %d", goRunewidthStrictTrue, expected)
206 }
207 }
208
209 // Test go-runewidth with EastAsianWidth=true
210 goRunewidthEAW := (&runewidth.Condition{EastAsianWidth: true}).StringWidth(tc.input)
211 if expected, ok := tc.expected["go-runewidth_EAW"]; ok {
212 if goRunewidthEAW != expected {
213 t.Errorf("runewidth.Condition{EastAsianWidth: true}.StringWidth() = %d, want %d", goRunewidthEAW, expected)
214 }
215 }
216
217 // Test uniseg default
218 unisegDefault := uniseg.StringWidth(tc.input)
219 if expected, ok := tc.expected["uniseg_default"]; ok {
220 if unisegDefault != expected {
221 t.Errorf("uniseg.StringWidth() = %d, want %d", unisegDefault, expected)
222 }
223 }
224
225 // Test uniseg with EastAsianWidth=true
226 originalEAW := uniseg.EastAsianAmbiguousWidth
227 uniseg.EastAsianAmbiguousWidth = 2
228 unisegEAW := uniseg.StringWidth(tc.input)
229 uniseg.EastAsianAmbiguousWidth = originalEAW
230 if expected, ok := tc.expected["uniseg_EAW"]; ok {
231 if unisegEAW != expected {
232 t.Errorf("uniseg.StringWidth() with EastAsianAmbiguousWidth=2 = %d, want %d", unisegEAW, expected)
233 }
234 }
235 })
236 }
237 }
238
239 func TestFlagBehaviorDetailed(t *testing.T) {
240 flags := []string{"🇺🇸", "🇯🇵", "🇬🇧", "🇫🇷", "🇩🇪"}
241
242 t.Log("Flag behavior comparison:")
243 t.Log("Library | displaywidth | go-runewidth (default) | go-runewidth (strict=false) | go-runewidth (strict=true) | uniseg")
244 t.Log("--------|--------------|------------------------|----------------------------|----------------------------|-------")
245
246 for _, flag := range flags {
247 // displaywidth (always width 2, no StrictEmojiNeutral option)
248 displaywidthDefault := displaywidth.String(flag)
249
250 // go-runewidth
251 goRunewidthDefault := runewidth.StringWidth(flag)
252 goRunewidthStrictFalse := (&runewidth.Condition{StrictEmojiNeutral: false}).StringWidth(flag)
253 goRunewidthStrictTrue := (&runewidth.Condition{StrictEmojiNeutral: true}).StringWidth(flag)
254
255 // uniseg
256 unisegDefault := uniseg.StringWidth(flag)
257
258 t.Logf("%s | %d | %d | %d | %d | %d", flag, displaywidthDefault, goRunewidthDefault, goRunewidthStrictFalse, goRunewidthStrictTrue, unisegDefault)
259 }
260 }
261
262 func TestTruncateComparison(t *testing.T) {
263 testCases := []struct {
264 name string
265 input string
266 maxWidth int
267 tail string
268 }{
269 {
270 name: "ASCII truncation",
271 input: "Hello World",
272 maxWidth: 5,
273 tail: "...",
274 },
275 {
276 name: "CJK truncation",
277 input: "中文测试",
278 maxWidth: 4,
279 tail: "...",
280 },
281 {
282 name: "Emoji truncation",
283 input: "😀🚀🎉",
284 maxWidth: 4,
285 tail: "...",
286 },
287 {
288 name: "Flags truncation",
289 input: "🇺🇸🇯🇵🇬🇧", // known difference
290 maxWidth: 4,
291 tail: "...",
292 },
293 {
294 name: "Mixed content truncation",
295 input: "Hello 世界! 😀🇺🇸",
296 maxWidth: 10,
297 tail: "...",
298 },
299 {
300 name: "No truncation needed",
301 input: "Hi",
302 maxWidth: 10,
303 tail: "...",
304 },
305 {
306 name: "Empty tail",
307 input: "Hello World",
308 maxWidth: 5,
309 tail: "",
310 },
311 {
312 name: "Width exactly equal to string width",
313 input: "Hello",
314 maxWidth: 5,
315 tail: "...",
316 },
317 {
318 name: "Width exactly equal to string width with emoji",
319 input: "😀🚀",
320 maxWidth: 4,
321 tail: "...",
322 },
323 {
324 name: "Width exactly equal to string width with CJK",
325 input: "中文",
326 maxWidth: 4,
327 tail: "...",
328 },
329 {
330 name: "MaxWidth is 0",
331 input: "Hello",
332 maxWidth: 0,
333 tail: "...",
334 },
335 {
336 name: "MaxWidth is 1",
337 input: "Hello",
338 maxWidth: 1,
339 tail: "...",
340 },
341 {
342 name: "MaxWidth is 2",
343 input: "Hello",
344 maxWidth: 2,
345 tail: "...",
346 },
347 {
348 name: "Empty string input",
349 input: "",
350 maxWidth: 5,
351 tail: "...",
352 },
353 {
354 name: "Tail wider than maxWidth",
355 input: "Hello",
356 maxWidth: 2,
357 tail: "中文", // width 4, wider than maxWidth
358 },
359 {
360 name: "Tail with emoji",
361 input: "Hello",
362 maxWidth: 5,
363 tail: "😀",
364 },
365 {
366 name: "MaxWidth exactly equal to tail width",
367 input: "Hello World",
368 maxWidth: 3, // exactly width of "..."
369 tail: "...",
370 },
371 {
372 name: "Input with control characters",
373 input: "hello\nworld",
374 maxWidth: 8,
375 tail: "...",
376 },
377 {
378 name: "Single wide character truncation",
379 input: "中",
380 maxWidth: 1,
381 tail: "...",
382 },
383 {
384 name: "Single emoji truncation",
385 input: "😀",
386 maxWidth: 1,
387 tail: "...",
388 },
389 }
390
391 for _, tc := range testCases {
392 t.Run(tc.name, func(t *testing.T) {
393 // displaywidth
394 displaywidthResult := displaywidth.TruncateString(tc.input, tc.maxWidth, tc.tail)
395 displaywidthWidth := displaywidth.String(displaywidthResult)
396
397 // go-runewidth
398 goRunewidthResult := runewidth.Truncate(tc.input, tc.maxWidth, tc.tail)
399 goRunewidthWidth := runewidth.StringWidth(goRunewidthResult)
400
401 if displaywidthWidth != goRunewidthWidth {
402 t.Logf("displaywidth and go-runewidth results differ for %q: %d != %d", tc.input, displaywidthWidth, goRunewidthWidth)
403 }
404
405 if displaywidthResult != goRunewidthResult {
406 t.Logf("displaywidth and go-runewidth results differ for %s : %s != %s", tc.input, displaywidthResult, goRunewidthResult)
407 }
408 })
409 }
410 }