-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
wrap_test.go
491 lines (468 loc) · 17.3 KB
/
wrap_test.go
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
package text
import (
"reflect"
"strings"
"testing"
)
func TestWrap(t *testing.T) {
cases := []struct {
Input, Output string
Lim int
}{
// A simple word passes through.
{
"foo",
"foo",
4,
},
// Word breaking
{
"foobarbaz",
"foob\narba\nz",
4,
},
// Lines are broken at whitespace.
{
"foo bar baz",
"foo\nbar\nbaz",
4,
},
// Word breaking
{
"foo bars bazzes",
"foo\nbars\nbazz\nes",
4,
},
// A word that would run beyond the width is wrapped.
{
"fo sop",
"fo\nsop",
4,
},
// A tab counts as 4 characters.
{
"foo\nb\t r\n baz",
"foo\nb\nr\n baz",
4,
},
// Trailing whitespace is removed after used for wrapping.
// Runs of whitespace on which a line is broken are removed.
{
"foo \nb ar ",
"foo\n\nb\nar\n",
4,
},
// An explicit line break at the end of the input is preserved.
{
"foo bar baz\n",
"foo\nbar\nbaz\n",
4,
},
// Explicit break are always preserved.
{
"\nfoo bar\n\n\nbaz\n",
"\nfoo\nbar\n\n\nbaz\n",
4,
},
// Ignore complete words with terminal color sequence
{
"foo \x1b[31mbar\x1b[0m baz",
"foo\n\x1b[31mbar\x1b[0m\nbaz",
4,
},
// Handle words with colors sequence inside the word
{
"foo b\x1b[31mbar\x1b[0mr baz",
"foo\nb\x1b[31mbar\n\x1b[0mr\nbaz",
4,
},
// Break words with colors sequence inside the word
{
"foo bb\x1b[31mbar\x1b[0mr baz",
"foo\nbb\x1b[31mba\nr\x1b[0mr\nbaz",
4,
},
// Complete example:
{
" This is a list: \n\n\t* foo\n\t* bar\n\n\n\t* baz \nBAM ",
" This\nis a\nlist:\n\n *\nfoo\n *\nbar\n\n\n *\nbaz\nBAM\n",
6,
},
// Handle chinese (wide characters)
{
"一只敏捷的狐狸跳过了一只懒狗。",
"一只敏捷的狐\n狸跳过了一只\n懒狗。",
12,
},
// Handle chinese with colors
{
"一只敏捷的\x1b[31m狐狸跳过\x1b[0m了一只懒狗。",
"一只敏捷的\x1b[31m狐\n狸跳过\x1b[0m了一只\n懒狗。",
12,
},
// Handle mixed wide and short characters
{
"敏捷 A quick 的狐狸 fox 跳过 jumps over a lazy 了一只懒狗 dog。",
"敏捷 A quick\n的狐狸 fox\n跳过 jumps\nover a lazy\n了一只懒狗\ndog。",
12,
},
// Handle mixed wide and short characters with color
{
"敏捷 A \x1b31mquick 的狐狸 fox 跳\x1b0m过 jumps over a lazy 了一只懒狗 dog。",
"敏捷 A \x1b31mquick\n的狐狸 fox\n跳\x1b0m过 jumps\nover a lazy\n了一只懒狗\ndog。",
12,
},
// Handle mixed wide and short characters with color at both ends
{
"\x1b31m敏捷 A quick 的狐狸 fox 跳过 jumps over a lazy 了一只懒狗 dog。\x1b0m",
"\x1b31m敏捷 A quick\n的狐狸 fox\n跳过 jumps\nover a lazy\n了一只懒狗\ndog。\x1b0m",
12,
},
}
for i, tc := range cases {
actual, lines := Wrap(tc.Input, tc.Lim)
if actual != tc.Output {
t.Fatalf("Case %d Input:\n\n`%s`\n\nExpected Output:\n\n`%s`\n\nActual Output:\n\n`%s`",
i, tc.Input, tc.Output, actual)
}
expected := len(strings.Split(tc.Output, "\n"))
if expected != lines {
t.Fatalf("Case %d Nb lines mismatch\nExpected:%d\nActual:%d",
i, expected, lines)
}
}
}
func BenchmarkWrap(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
Wrap("敏捷 A \x1b31mquick 的狐狸 fox 跳\x1b0m过 jumps over a lazy 了一只懒狗 dog。", 12)
}
}
func TestWrapLeftPadded(t *testing.T) {
cases := []struct {
input, output string
lim, pad int
}{
{
"The Lorem ipsum text is typically composed of pseudo-Latin words. It is commonly used as placeholder text to examine or demonstrate the visual effects of various graphic design.",
` The Lorem ipsum text is typically composed of
pseudo-Latin words. It is commonly used as placeholder
text to examine or demonstrate the visual effects of
various graphic design.`,
59, 4,
},
// Handle Chinese
{
"婞一枳郲逴靲屮蜧曀殳,掫乇峔掮傎溒兀緉冘仜。郼牪艽螗媷錵朸一詅掜豗怙刉笀丌,楀棶乇矹迡搦囷圣亍昄漚粁仈祂。覂一洳袶揙楱亍滻瘯毌,掗屮柅軡菵腩乜榵毌夯。勼哻怌婇怤灟葠雺奷朾恦扰衪岨坋誁乇芚誙腞。冇笉妺悆浂鱦賌廌灱灱觓坋佫呬耴跣兀枔蓔輈。嵅咍犴膰痭瘰机一靬涽捊矷尒玶乇,煚塈丌岰陊鉖怞戉兀甿跾觓夬侄。棩岧汌橩僁螗玎一逭舴圂衪扐衲兀,嵲媕亍衩衿溽昃夯丌侄蒰扂丱呤。毰侘妅錣廇螉仴一暀淖蚗佶庂咺丌,輀鈁乇彽洢溦洰氶乇构碨洐巿阹。",
` 婞一枳郲逴靲屮蜧曀殳,掫乇峔掮傎溒兀緉冘仜。郼牪艽螗媷
錵朸一詅掜豗怙刉笀丌,楀棶乇矹迡搦囷圣亍昄漚粁仈祂。覂
一洳袶揙楱亍滻瘯毌,掗屮柅軡菵腩乜榵毌夯。勼哻怌婇怤灟
葠雺奷朾恦扰衪岨坋誁乇芚誙腞。冇笉妺悆浂鱦賌廌灱灱觓坋
佫呬耴跣兀枔蓔輈。嵅咍犴膰痭瘰机一靬涽捊矷尒玶乇,煚塈
丌岰陊鉖怞戉兀甿跾觓夬侄。棩岧汌橩僁螗玎一逭舴圂衪扐衲
兀,嵲媕亍衩衿溽昃夯丌侄蒰扂丱呤。毰侘妅錣廇螉仴一暀淖
蚗佶庂咺丌,輀鈁乇彽洢溦洰氶乇构碨洐巿阹。`,
59, 4,
},
// Handle long unbreakable words in a full sentence
{
"OT: there are alternatives to maintainer-/user-set priority, e.g. \"[user pain](http://www.lostgarden.com/2008/05/improving-bug-triage-with-user-pain.html)\".",
` OT: there are alternatives to maintainer-/user-set
priority, e.g. "[user pain](http://www.lostgarden.com/
2008/05/improving-bug-triage-with-user-pain.html)".`,
58, 4,
},
}
for i, tc := range cases {
actual, lines := WrapLeftPadded(tc.input, tc.lim, tc.pad)
if actual != tc.output {
t.Fatalf("Case %d Input:\n\n`%s`\n\nExpected Output:\n`\n%s`\n\nActual Output:\n`\n%s\n%s`",
i, tc.input, tc.output,
"|"+strings.Repeat("-", tc.lim-2)+"|",
actual)
}
expected := len(strings.Split(tc.output, "\n"))
if expected != lines {
t.Fatalf("Case %d Nb lines mismatch\nExpected:%d\nActual:%d",
i, expected, lines)
}
}
}
func BenchmarkWrapLeftPadded(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
WrapLeftPadded("The Lorem ipsum text is typically composed of pseudo-Latin words. It is commonly used as placeholder text to examine or demonstrate the visual effects of various graphic design.", 59, 4)
}
}
func TestWrapWithPadIndent(t *testing.T) {
cases := []struct {
input, output string
lim int
indent, pad string
}{
{
"The Lorem ipsum text is typically composed of pseudo-Latin words. It is commonly used as placeholder text to examine or demonstrate the visual effects of various graphic design.",
` The Lorem ipsum text is typically composed of
pseudo-Latin words. It is commonly used as
placeholder text to examine or demonstrate the visual
effects of various graphic design.`,
59, " ", " ",
},
// Handle Chinese
{
"婞一枳郲逴靲屮蜧曀殳,掫乇峔掮傎溒兀緉冘仜。郼牪艽螗媷錵朸一詅掜豗怙刉笀丌,楀棶乇矹迡搦囷圣亍昄漚粁仈祂。覂一洳袶揙楱亍滻瘯毌,掗屮柅軡菵腩乜榵毌夯。勼哻怌婇怤灟葠雺奷朾恦扰衪岨坋誁乇芚誙腞。冇笉妺悆浂鱦賌廌灱灱觓坋佫呬耴跣兀枔蓔輈。嵅咍犴膰痭瘰机一靬涽捊矷尒玶乇,煚塈丌岰陊鉖怞戉兀甿跾觓夬侄。棩岧汌橩僁螗玎一逭舴圂衪扐衲兀,嵲媕亍衩衿溽昃夯丌侄蒰扂丱呤。毰侘妅錣廇螉仴一暀淖蚗佶庂咺丌,輀鈁乇彽洢溦洰氶乇构碨洐巿阹。",
` 婞一枳郲逴靲屮蜧曀殳,掫乇峔掮傎溒兀緉冘仜。郼牪艽螗媷錵
朸一詅掜豗怙刉笀丌,楀棶乇矹迡搦囷圣亍昄漚粁仈祂。覂
一洳袶揙楱亍滻瘯毌,掗屮柅軡菵腩乜榵毌夯。勼哻怌婇怤
灟葠雺奷朾恦扰衪岨坋誁乇芚誙腞。冇笉妺悆浂鱦賌廌灱灱
觓坋佫呬耴跣兀枔蓔輈。嵅咍犴膰痭瘰机一靬涽捊矷尒玶乇
,煚塈丌岰陊鉖怞戉兀甿跾觓夬侄。棩岧汌橩僁螗玎一逭舴
圂衪扐衲兀,嵲媕亍衩衿溽昃夯丌侄蒰扂丱呤。毰侘妅錣廇
螉仴一暀淖蚗佶庂咺丌,輀鈁乇彽洢溦洰氶乇构碨洐巿阹。`,
59, " ", " ",
},
// Handle long unbreakable words in a full sentence
{
"OT: there are alternatives to maintainer-/user-set priority, e.g. \"[user pain](http://www.lostgarden.com/2008/05/improving-bug-triage-with-user-pain.html)\".",
` OT: there are alternatives to maintainer-/user-set
priority, e.g. "[user pain](http://www.lostgarden.co
m/2008/05/improving-bug-triage-with-user-pain.html)"
.`,
58, " ", " ",
},
}
for i, tc := range cases {
actual, lines := WrapWithPadIndent(tc.input, tc.lim, tc.indent, tc.pad)
if actual != tc.output {
t.Fatalf("Case %d Input:\n\n`%s`\n\nExpected Output:\n`\n%s`\n\nActual Output:\n`\n%s\n%s`",
i, tc.input, tc.output,
"|"+strings.Repeat("-", tc.lim-2)+"|",
actual)
}
expected := len(strings.Split(tc.output, "\n"))
if expected != lines {
t.Fatalf("Case %d Nb lines mismatch\nExpected:%d\nActual:%d",
i, expected, lines)
}
}
}
func BenchmarkWrapWithPadIndent(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
WrapWithPadIndent("The Lorem ipsum text is typically composed of pseudo-Latin words. It is commonly used as placeholder text to examine or demonstrate the visual effects of various graphic design.", 59, " ", " ")
}
}
func TestWrapWithPadIndentAlign(t *testing.T) {
cases := []struct {
input, output string
lim int
indent, pad string
align Alignment
}{
{
"The Lorem ipsum text is typically composed of pseudo-Latin words. It is commonly used as placeholder text to examine or demonstrate the visual effects of various graphic design.",
`<indent> The Lorem ipsum text is typically composed of
<pad> pseudo-Latin words. It is commonly used as placeholder
<pad> text to examine or demonstrate the visual effects of
<pad> various graphic design.`,
63, "<indent>", "<pad>",
AlignCenter,
},
// Handle Chinese
{
"婞一枳郲逴靲屮蜧曀殳,掫乇峔掮傎溒兀緉冘仜。郼牪艽螗媷錵朸一詅掜豗怙刉笀丌,楀棶乇矹迡搦囷圣亍昄漚粁仈祂。覂一洳袶揙楱亍滻瘯毌,掗屮柅軡菵腩乜榵毌夯。勼哻怌婇怤灟葠雺奷朾恦扰衪岨坋誁乇芚誙腞。冇笉妺悆浂鱦賌廌灱灱觓坋佫呬耴跣兀枔蓔輈。嵅咍犴膰痭瘰机一靬涽捊矷尒玶乇,煚塈丌岰陊鉖怞戉兀甿跾觓夬侄。棩岧汌橩僁螗玎一逭舴圂衪扐衲兀,嵲媕亍衩衿溽昃夯丌侄蒰扂丱呤。毰侘妅錣廇螉仴一暀淖蚗佶庂咺丌,輀鈁乇彽洢溦洰氶乇构碨洐巿阹。",
`<indent> 婞一枳郲逴靲屮蜧曀殳,掫乇峔掮傎溒兀緉冘仜。郼牪艽
<pad>螗媷錵朸一詅掜豗怙刉笀丌,楀棶乇矹迡搦囷圣亍昄漚粁仈祂
<pad>。覂一洳袶揙楱亍滻瘯毌,掗屮柅軡菵腩乜榵毌夯。勼哻怌婇
<pad>怤灟葠雺奷朾恦扰衪岨坋誁乇芚誙腞。冇笉妺悆浂鱦賌廌灱灱
<pad>觓坋佫呬耴跣兀枔蓔輈。嵅咍犴膰痭瘰机一靬涽捊矷尒玶乇,
<pad>煚塈丌岰陊鉖怞戉兀甿跾觓夬侄。棩岧汌橩僁螗玎一逭舴圂衪
<pad>扐衲兀,嵲媕亍衩衿溽昃夯丌侄蒰扂丱呤。毰侘妅錣廇螉仴一
<pad> 暀淖蚗佶庂咺丌,輀鈁乇彽洢溦洰氶乇构碨洐巿阹。`,
59, "<indent>", "<pad>",
AlignRight,
},
// Handle long unbreakable words in a full sentence
{
"OT: there are alternatives to maintainer-/user-set priority, e.g. \"[user pain](http://www.lostgarden.com/2008/05/improving-bug-triage-with-user-pain.html)\".",
`<indent> OT: there are alternatives to
<pad>maintainer-/user-set priority, e.g. "[user pain](
<pad>http://www.lostgarden.com/2008/05/improving-bug-t
<pad> riage-with-user-pain.html)".`,
54, "<indent>", "<pad>",
AlignCenter,
},
// Handle indent and padding with wide characters and ANSI escape codes
{
"The Lorem ipsum text is typically composed of pseudo-Latin words. It is commonly used as placeholder text to examine or demonstrate the visual effects of various graphic design.",
"\x1b[31m狐狸跳过\x1b[0m The Lorem ipsum text is typically composed of\n" +
"\x1b[31m狐\x1b[0m pseudo-Latin words. It is commonly used as placeholder text\n" +
"\x1b[31m狐\x1b[0m to examine or demonstrate the visual effects of various\n" +
"\x1b[31m狐\x1b[0m graphic design.",
63, "\x1b[31m狐狸跳过\x1b[0m", "\x1b[31m狐\x1b[0m",
AlignCenter,
},
// handle too long indentation
{
"The Lorem ipsum text is typically composed of pseudo-Latin words. It is commonly used as placeholder text to examine or demonstrate the visual effects of various graphic design.",
"⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬\n" +
"<pad>The Lorem ipsum\n"+
"<pad>text is\n"+
"<pad>typically\n"+
"<pad>composed of\n"+
"<pad>pseudo-Latin\n"+
"<pad>words. It is\n"+
"<pad>commonly used\n"+
"<pad>as placeholder\n"+
"<pad>text to examine\n"+
"<pad>or demonstrate\n"+
"<pad>the visual\n"+
"<pad>effects of\n"+
"<pad>various graphic\n"+
"<pad>design.",
20, "<indentindentindent>", "<pad>",
AlignLeft,
},
// handle too long padding
{
"The Lorem ipsum text is typically composed of pseudo-Latin words. It is commonly used as placeholder text to examine or demonstrate the visual effects of various graphic design.",
"⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬\n⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬\n" +
"⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬\n⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬\n" +
"⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬⭬\n",
20, "<indentindentindent>", "<padpadpadpadpadpad>",
AlignLeft,
},
}
for i, tc := range cases {
actual, lines := WrapWithPadIndentAlign(tc.input, tc.lim, tc.indent, tc.pad, tc.align)
if actual != tc.output {
t.Fatalf("Case %d Input:\n\n`%s`\n\nExpected Output:\n`\n%s`\n\nActual Output:\n`\n%s\n%s`",
i, tc.input, tc.output,
"|"+strings.Repeat("-", tc.lim-2)+"|",
actual)
}
expected := len(strings.Split(tc.output, "\n"))
if expected != lines {
t.Fatalf("Case %d Nb lines mismatch\nExpected:%d\nActual:%d",
i, expected, lines)
}
}
}
func BenchmarkWrapWithPadIndentAlign(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
WrapWithPadIndentAlign("The Lorem ipsum text is typically composed of pseudo-Latin words. It is commonly used as placeholder text to examine or demonstrate the visual effects of various graphic design.", 59, " ", " ", AlignCenter)
}
}
func TestSplitWord(t *testing.T) {
cases := []struct {
Input string
Length int
Result, Leftover string
}{
// A simple word passes through.
{
"foo",
4,
"foo", "",
},
// Cut at the right place
{
"foobarHoy",
4,
"foob", "arHoy",
},
// A simple word passes through with colors
{
"\x1b[31mbar\x1b[0m",
4,
"\x1b[31mbar\x1b[0m", "",
},
// Cut at the right place with colors
{
"\x1b[31mfoobarHoy\x1b[0m",
4,
"\x1b[31mfoob", "arHoy\x1b[0m",
},
// Handle prefix and suffix properly
{
"foo\x1b[31mfoobarHoy\x1b[0mbaaar",
4,
"foo\x1b[31mf", "oobarHoy\x1b[0mbaaar",
},
// Cut properly with length = 0
{
"foo",
0,
"", "foo",
},
// Handle chinese
{
"快檢什麼望對",
4,
"快檢", "什麼望對",
},
{
"快檢什麼望對",
5,
"快檢", "什麼望對",
},
// Handle chinese with colors
{
"快\x1b[31m檢什麼\x1b[0m望對",
4,
"快\x1b[31m檢", "什麼\x1b[0m望對",
},
}
for i, tc := range cases {
result, leftover := splitWord(tc.Input, tc.Length)
if result != tc.Result || leftover != tc.Leftover {
t.Fatalf("Case %d Input:\n\n`%s`\n\nExpected Output:\n\n`%s` - `%s`\n\nActual Output:\n\n`%s` - `%s`",
i, tc.Input, tc.Result, tc.Leftover, result, leftover)
}
}
}
func BenchmarkSplitWord(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
splitWord("快\x1b[31m檢什麼\x1b[0m望對", 4)
}
}
func TestSegmentLines(t *testing.T) {
cases := []struct {
Input string
Output []string
}{
// A plain ascii line with escapes.
{
"This is an example.",
[]string{"This", " ", "is", " ", "an", " ", "example."},
},
// A plain wide line with escapes.
{
"一只敏捷的狐狸跳过了一只懒狗。",
[]string{"一", "只", "敏", "捷", "的", "狐", "狸", "跳", "过",
"了", "一", "只", "懒", "狗", "。"},
},
// A complex sentence.
{
"This is a 'complex' example, where 一只 and English 混合了。",
[]string{"This", " ", "is", " ", "a", " ", "'complex'", " ", "example,",
" ", "where", " ", "一", "只", " ", "and", " ", "English", " ", "混",
"合", "了", "。"},
},
}
for i, tc := range cases {
chunks := segmentLine(tc.Input)
if !reflect.DeepEqual(chunks, tc.Output) {
t.Fatalf("Case %d Input:\n\n`%s`\n\nExpected Output:\n\n`[%s]`\n\nActual Output:\n\n`[%s]`\n\n",
i, tc.Input, strings.Join(tc.Output, ", "), strings.Join(chunks, ", "))
}
}
}
func BenchmarkSegmentLines(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
segmentLine("This is a 'complex' example, where 一只 and English 混合了。")
}
}