Debian Codegraph @

ansi/truncate_test.go

raw
1 package ansi
2
3 import (
4 "testing"
5 )
6
7 // nolint
8 var tcases = []struct {
9 name string
10 input string
11 extra string
12 width int
13 expectRight string
14 expectLeft string
15 }{
16 {
17 "empty",
18 "",
19 "",
20 0,
21 "",
22 "",
23 },
24 {
25 "truncate_length_0",
26 "foo",
27 "",
28 0,
29 "",
30 "foo",
31 },
32 {
33 "equalascii",
34 "one",
35 ".",
36 3,
37 "one",
38 "",
39 },
40 {
41 "equalemoji",
42 "on👋",
43 ".",
44 3,
45 "on.",
46 ".👋",
47 },
48 {
49 "simple multiple words",
50 "a couple of words",
51 "",
52 6,
53 "a coup",
54 "le of words",
55 },
56 {
57 "equalcontrolemoji",
58 "one\x1b[0m",
59 ".",
60 3,
61 "one\x1b[0m",
62 "\x1b[0m",
63 },
64 {
65 "truncate_tail_greater",
66 "foo",
67 "...",
68 5,
69 "foo",
70 "",
71 },
72 {
73 "simple",
74 "foobar",
75 "",
76 3,
77 "foo",
78 "bar",
79 },
80 {
81 "passthrough",
82 "foobar",
83 "",
84 10,
85 "foobar",
86 "",
87 },
88 {
89 "ascii",
90 "hello",
91 "",
92 3,
93 "hel",
94 "lo",
95 },
96 {
97 "emoji",
98 "👋",
99 "",
100 2,
101 "👋",
102 "",
103 },
104 {
105 "wideemoji",
106 "🫧",
107 "",
108 2,
109 "🫧",
110 "",
111 },
112 {
113 "controlemoji",
114 "\x1b[31mhello 👋abc\x1b[0m",
115 "",
116 8,
117 "\x1b[31mhello 👋\x1b[0m",
118 "\x1b[31mabc\x1b[0m",
119 },
120 {
121 "osc8",
122 "\x1b]8;;https://charm.sh\x1b\\Charmbracelet 🫧\x1b]8;;\x1b\\",
123 "",
124 5,
125 "\x1b]8;;https://charm.sh\x1b\\Charm\x1b]8;;\x1b\\",
126 "\x1b]8;;https://charm.sh\x1b\\bracelet 🫧\x1b]8;;\x1b\\",
127 },
128 {
129 "osc8_8bit",
130 "\x9d8;;https://charm.sh\x9cCharmbracelet 🫧\x9d8;;\x9c",
131 "",
132 5,
133 "\x9d8;;https://charm.sh\x9cCharm\x9d8;;\x9c",
134 "\x9d8;;https://charm.sh\x9cbracelet 🫧\x9d8;;\x9c",
135 },
136 {
137 "style_tail",
138 "\x1B[38;5;219mHiya!",
139 "…",
140 3,
141 "\x1B[38;5;219mHi…",
142 "\x1B[38;5;219m…a!",
143 },
144 {
145 "double_style_tail",
146 "\x1B[38;5;219mHiya!\x1B[38;5;219mHello",
147 "…",
148 7,
149 "\x1B[38;5;219mHiya!\x1B[38;5;219mH…",
150 "\x1B[38;5;219m\x1B[38;5;219m…llo",
151 },
152 {
153 "noop",
154 "\x1B[7m--",
155 "",
156 2,
157 "\x1B[7m--",
158 "\x1b[7m",
159 },
160 {
161 "double_width",
162 "\x1B[38;2;249;38;114m你好\x1B[0m",
163 "",
164 3,
165 "\x1B[38;2;249;38;114m你\x1B[0m",
166 "\x1B[38;2;249;38;114m好\x1B[0m",
167 },
168 {
169 "double_width_rune",
170 "你",
171 "",
172 1,
173 "",
174 "你",
175 },
176 {
177 "double_width_runes",
178 "你好",
179 "",
180 2,
181 "你",
182 "好",
183 },
184 {
185 "spaces_only",
186 " ",
187 "…",
188 2,
189 " …",
190 "… ",
191 },
192 {
193 "longer_tail",
194 "foo",
195 "...",
196 2,
197 "",
198 "...o",
199 },
200 {
201 "same_tail_width",
202 "foo",
203 "...",
204 3,
205 "foo",
206 "",
207 },
208 {
209 "same_tail_width_control",
210 "\x1b[31mfoo\x1b[0m",
211 "...",
212 3,
213 "\x1b[31mfoo\x1b[0m",
214 "\x1b[31m\x1b[0m",
215 },
216 {
217 "same_width",
218 "foo",
219 "",
220 3,
221 "foo",
222 "",
223 },
224 {
225 "truncate_with_tail",
226 "foobar",
227 ".",
228 4,
229 "foo.",
230 ".ar",
231 },
232 {
233 "style",
234 "I really \x1B[38;2;249;38;114mlove\x1B[0m Go!",
235 "",
236 8,
237 "I really\x1B[38;2;249;38;114m\x1B[0m",
238 " \x1B[38;2;249;38;114mlove\x1B[0m Go!",
239 },
240 {
241 "dcs",
242 "\x1BPq#0;2;0;0;0#1;2;100;100;0#2;2;0;100;0#1~~@@vv@@~~@@~~$#2??}}GG}}??}}??-#1!14@\x1B\\foobar",
243 "…",
244 4,
245 "\x1BPq#0;2;0;0;0#1;2;100;100;0#2;2;0;100;0#1~~@@vv@@~~@@~~$#2??}}GG}}??}}??-#1!14@\x1B\\foo…",
246 "\x1BPq#0;2;0;0;0#1;2;100;100;0#2;2;0;100;0#1~~@@vv@@~~@@~~$#2??}}GG}}??}}??-#1!14@\x1B\\…ar",
247 },
248 {
249 "emoji_tail",
250 "\x1b[36mHello there!\x1b[m",
251 "😃",
252 8,
253 "\x1b[36mHello 😃\x1b[m",
254 "\x1b[36m😃ere!\x1b[m",
255 },
256 {
257 "unicode",
258 "\x1b[35mClaire‘s Boutique\x1b[0m",
259 "",
260 8,
261 "\x1b[35mClaire‘s\x1b[0m",
262 "\x1b[35m Boutique\x1b[0m",
263 },
264 {
265 "wide_chars",
266 "こんにちは",
267 "…",
268 7,
269 "こんに…",
270 "…ちは",
271 },
272 {
273 "style_wide_chars",
274 "\x1b[35mこんにちは\x1b[m",
275 "…",
276 7,
277 "\x1b[35mこんに…\x1b[m",
278 "\x1b[35m…ちは\x1b[m",
279 },
280 {
281 "osc8_lf",
282 "สวัสดีสวัสดี\x1b]8;;https://example.com\x1b\\\nสวัสดีสวัสดี\x1b]8;;\x1b\\",
283 "…",
284 9,
285 "สวัสดีสวัสดี\x1b]8;;https://example.com\x1b\\\n…\x1b]8;;\x1b\\",
286 "\x1b]8;;https://example.com\x1b\\…วัสดีสวัสดี\x1b]8;;\x1b\\",
287 },
288 {
289 "simple japanese text prefix/suffix",
290 "耐許ヱヨカハ調出あゆ監",
291 "…",
292 13,
293 "耐許ヱヨカハ…",
294 "…調出あゆ監",
295 },
296 {
297 "simple japanese text",
298 "耐許ヱヨカハ調出あゆ監",
299 "",
300 14,
301 "耐許ヱヨカハ調",
302 "出あゆ監",
303 },
304 {
305 "new line inside and outside range",
306 "\n\nsomething\nin\nthe\nway\n\n",
307 "-",
308 10,
309 "\n\nsomething\n-",
310 "-n\nthe\nway\n\n",
311 },
312 {
313 "multi-width graphemes with newlines - japanese text",
314 `耐許ヱヨカハ調出あゆ監件び理別よン國給災レホチ権輝モエフ会割もフ響3現エツ文時しだびほ経機ムイメフ敗文ヨク現義なさド請情ゆじょて憶主管州けでふく。排ゃわつげ美刊ヱミ出見ツ南者オ抜豆ハトロネ論索モネニイ任償スヲ話破リヤヨ秒止口イセソス止央のさ食周健でてつだ官送ト読聴遊容ひるべ。際ぐドらづ市居ネムヤ研校35岩6繹ごわク報拐イ革深52球ゃレスご究東スラ衝3間ラ録占たス。
315
316 禁にンご忘康ざほぎル騰般ねど事超スんいう真表何カモ自浩ヲシミ図客線るふ静王ぱーま写村月掛焼詐面ぞゃ。昇強ごントほ価保キ族85岡モテ恋困ひりこな刊並せご出来ぼぎむう点目ヲウ止環公ニレ事応タス必書タメムノ当84無信升ちひょ。価ーぐ中客テサ告覧ヨトハ極整
317 ラ得95稿はかラせ江利ス宏丸霊ミ考整ス静将ず業巨職ノラホ収嗅ざな。`,
318 "",
319 14,
320 "耐許ヱヨカハ調",
321 `出あゆ監件び理別よン國給災レホチ権輝モエフ会割もフ響3現エツ文時しだびほ経機ムイメフ敗文ヨク現義なさド請情ゆじょて憶主管州けでふく。排ゃわつげ美刊ヱミ出見ツ南者オ抜豆ハトロネ論索モネニイ任償スヲ話破リヤヨ秒止口イセソス止央のさ食周健でてつだ官送ト読聴遊容ひるべ。際ぐドらづ市居ネムヤ研校35岩6繹ごわク報拐イ革深52球ゃレスご究東スラ衝3間ラ録占たス。
322
323 禁にンご忘康ざほぎル騰般ねど事超スんいう真表何カモ自浩ヲシミ図客線るふ静王ぱーま写村月掛焼詐面ぞゃ。昇強ごントほ価保キ族85岡モテ恋困ひりこな刊並せご出来ぼぎむう点目ヲウ止環公ニレ事応タス必書タメムノ当84無信升ちひょ。価ーぐ中客テサ告覧ヨトハ極整
324 ラ得95稿はかラせ江利ス宏丸霊ミ考整ス静将ず業巨職ノラホ収嗅ざな。`,
325 },
326 }
327
328 func TestTruncate(t *testing.T) {
329 for i, c := range tcases {
330 t.Run(c.name, func(t *testing.T) {
331 if result := Truncate(c.input, c.width, c.extra); result != c.expectRight {
332 t.Errorf("test case %d failed:\nexpected: %q\n got: %q", i+1, c.expectRight, result)
333 }
334 })
335 }
336 }
337
338 func BenchmarkTruncateString(b *testing.B) {
339 b.RunParallel(func(pb *testing.PB) {
340 b.ReportAllocs()
341 b.ResetTimer()
342 for pb.Next() {
343 Truncate("foo", 2, "")
344 }
345 })
346 }
347
348 func TestTruncateLeft(t *testing.T) {
349 for i, c := range tcases {
350 t.Run(c.name, func(t *testing.T) {
351 if result := TruncateLeft(c.input, c.width, c.extra); result != c.expectLeft {
352 t.Errorf("test case %d failed:\nexpected: %q\n got: %q", i+1, c.expectLeft, result)
353 }
354 })
355 }
356 }
357
358 func TestCut(t *testing.T) {
359 for i, c := range []struct {
360 desc string
361 input string
362 left int
363 right int
364 expect string
365 }{
366 {
367 "simple string",
368 "This is a long string", 2, 6,
369 "is i",
370 },
371 {
372 "with ansi",
373 "I really \x1B[38;2;249;38;114mlove\x1B[0m Go!", 4, 25,
374 "ally \x1b[38;2;249;38;114mlove\x1b[0m Go!",
375 },
376 {
377 "left is 0",
378 "Foo \x1B[38;2;249;38;114mbar\x1B[0mbaz", 0, 5,
379 "Foo \x1B[38;2;249;38;114mb\x1B[0m",
380 },
381 {
382 "right is 0",
383 "\x1b[7mHello\x1b[m", 3, 0,
384 "",
385 },
386 {
387 "right is less than left",
388 "\x1b[7mHello\x1b[m", 3, 2,
389 "",
390 },
391 {
392 "cut size is 0",
393 "\x1b[7mHello\x1b[m", 2, 2,
394 "",
395 },
396 {
397 "maintains open ansi",
398 "\x1b[38;5;212;48;5;63mHello, Artichoke!\x1b[m", 7, 16,
399 "\x1b[38;5;212;48;5;63mArtichoke\x1b[m",
400 },
401 {
402 "multiline",
403 "\n\x1b[38;2;98;98;98m\nif [ -f RE\nADME.md ]; then\x1b[m\n\x1b[38;2;98;98;98m echo oi\x1b[m\n\x1b[38;2;98;98;98mfi\x1b[m\n", 8, 13,
404 "\x1b[38;2;98;98;98mRE\nADM\x1b[m\x1b[38;2;98;98;98m\x1b[m\x1b[38;2;98;98;98m\x1b[m",
405 },
406 } {
407 t.Run(c.desc, func(t *testing.T) {
408 got := Cut(c.input, c.left, c.right)
409 if got != c.expect {
410 t.Errorf("%s (#%d):\nexpected: %q\ngot: %q", c.desc, i+1, c.expect, got)
411 }
412 })
413 }
414 }
415
416 func TestByteToGraphemeRange(t *testing.T) {
417 cases := []struct {
418 name string
419 feed [2]int
420 expect [2]int
421 input string
422 }{
423 {
424 name: "simple",
425 input: "hello world from x/ansi",
426 feed: [2]int{2, 9},
427 expect: [2]int{2, 9},
428 },
429 {
430 name: "with emoji",
431 input: " Downloads",
432 feed: [2]int{4, 7},
433 expect: [2]int{2, 5},
434 },
435 {
436 name: "start out of bounds",
437 input: "some text",
438 feed: [2]int{-1, 5},
439 expect: [2]int{0, 5},
440 },
441 {
442 name: "end out of bounds",
443 input: "some text",
444 feed: [2]int{1, 50},
445 expect: [2]int{1, 9},
446 },
447 }
448
449 for _, tt := range cases {
450 t.Run(tt.name, func(t *testing.T) {
451 charStart, charStop := ByteToGraphemeRange(tt.input, tt.feed[0], tt.feed[1])
452 if expect := tt.expect[0]; expect != charStart {
453 t.Errorf("expected start to be %d, got %d", expect, charStart)
454 }
455 if expect := tt.expect[1]; expect != charStop {
456 t.Errorf("expected stop to be %d, got %d", expect, charStop)
457 }
458 })
459 }
460 }