-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_test.go
344 lines (319 loc) · 13.6 KB
/
text_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
package text
import (
"testing"
)
func TestWrap(t *testing.T) {
testCases := []struct {
in string
sz int
out string
}{
{"Coucou", 10, "Coucou"},
{"Coucou", 6, "Coucou"},
{"Coucou ", 6, "Coucou"},
{"Coucou\n", 8, "Coucou\n"},
{"This is a long sentence", 10, "This is a\nlong\nsentence"},
{"This \x1b[34mis\x1b[0m a long sentence", 10, "This \x1b[34mis\x1b[0m a\nlong\nsentence"},
{"This \x1b[34mis a long sentence\x1b[0m", 10, "This \x1b[34mis a\nlong\nsentence\x1b[0m"},
{"Supercalifragilisticexpialidocious\nChim Chim Cher-ee", 10, "Supercalif\nragilistic\nexpialidoc\nious\nChim Chim\nCher-ee"},
{"description: This edition contains Alice's Adventures in Wonderland. Tweedledum and Tweedledee, the Mad Hatter, the Cheshire Cat, the Red Queen and the White Rabbit all make their appearances, and are now familiar figures in writing, conversation and idiom.\nauthor: Lewis Caroll", 58, "description: This edition contains Alice's Adventures in\nWonderland. Tweedledum and Tweedledee, the Mad Hatter, the\nCheshire Cat, the Red Queen and the White Rabbit all make\ntheir appearances, and are now familiar figures in\nwriting, conversation and idiom.\nauthor: Lewis Caroll"},
{
"All details can be found in [![GoDoc](https://godoc.org/github.com/pirmd/style?status.svg)](https://godoc.org/github.com/pirmd/style)",
80,
"All details can be found in [![GoDoc]\n(https://godoc.org/github.com/pirmd/style?status.svg)]\n(https://godoc.org/github.com/pirmd/style)",
},
{
"This very long and detailed sentence is here to demonstrate that list can be formatted and wrapped because it hopefully be so long that it will not fulfill the maximum number of authorized chars per lines is reached.",
36,
"This very long and detailed sentence\nis here to demonstrate that list can\nbe formatted and wrapped because it\nhopefully be so long that it will\nnot fulfill the maximum number of\nauthorized chars per lines is\nreached.",
},
}
for _, tc := range testCases {
got := Wrap(tc.in, tc.sz)
if got != tc.out {
t.Errorf("Wrap failed for %#v.\nWanted:\n%#v\nGot :\n%#v\n", tc.in, tc.out, got)
}
}
}
func TestLazyWrap(t *testing.T) {
testCases := []struct {
in string
sz int
out string
}{
{"Coucou", 10, "Coucou"},
{"Coucou", 6, "Coucou"},
{"Coucou ", 6, "Coucou"},
{"Coucou\n", 8, "Coucou\n"},
{"This is a long sentence", 10, "This is a\nlong\nsentence"},
{"This \x1b[34mis\x1b[0m a long sentence", 10, "This \x1b[34mis\x1b[0m a\nlong\nsentence"},
{"This \x1b[34mis a long sentence\x1b[0m", 10, "This \x1b[34mis a\nlong\nsentence\x1b[0m"},
{"Supercalifragilisticexpialidocious\nChim Chim Cher-ee", 10, "Supercalifragilisticexpialidocious\nChim Chim\nCher-ee"},
{"description: This edition contains Alice's Adventures in Wonderland. Tweedledum and Tweedledee, the Mad Hatter, the Cheshire Cat, the Red Queen and the White Rabbit all make their appearances, and are now familiar figures in writing, conversation and idiom.\nauthor: Lewis Caroll", 58, "description: This edition contains Alice's Adventures in\nWonderland. Tweedledum and Tweedledee, the Mad Hatter, the\nCheshire Cat, the Red Queen and the White Rabbit all make\ntheir appearances, and are now familiar figures in\nwriting, conversation and idiom.\nauthor: Lewis Caroll"},
{
"All details can be found in [![GoDoc](https://godoc.org/github.com/pirmd/style?status.svg)](https://godoc.org/github.com/pirmd/style)",
80,
"All details can be found in [![GoDoc]\n(https://godoc.org/github.com/pirmd/style?status.svg)]\n(https://godoc.org/github.com/pirmd/style)",
},
}
for _, tc := range testCases {
got := LazyWrap(tc.in, tc.sz)
if got != tc.out {
t.Errorf("Wrap failed for %#v.\nWanted:\n%#v\nGot :\n%#v\n", tc.in, tc.out, got)
}
}
}
func TestTab(t *testing.T) {
testCases := []struct {
inTxt string
inP string
out string
}{
{
"Test",
" ",
" Test",
},
{
"This very long and detailed sentence is here to demonstrate that list can be formatted and wrapped because it hopefully be so long that it will not fulfill the maximum number of authorized chars per lines is reached.",
" > ",
" > This very long and detailed sentence\n > is here to demonstrate that list can\n > be formatted and wrapped because it\n > hopefully be so long that it will\n > not fulfill the maximum number of\n > authorized chars per lines is\n > reached.",
},
{
"This very long and detailed sentence is here to demonstrate that list can be formatted and wrapped because it hopefully be so long that it will not fulfill the maximum number of authorized chars per lines is reached.",
" \x1b[31m>\x1b[0m ",
" \x1b[31m>\x1b[0m This very long and detailed sentence\n \x1b[31m>\x1b[0m is here to demonstrate that list can\n \x1b[31m>\x1b[0m be formatted and wrapped because it\n \x1b[31m>\x1b[0m hopefully be so long that it will\n \x1b[31m>\x1b[0m not fulfill the maximum number of\n \x1b[31m>\x1b[0m authorized chars per lines is\n \x1b[31m>\x1b[0m reached.",
},
}
for _, tc := range testCases {
got := Tab(tc.inTxt, "", tc.inP, 40)
if got != tc.out {
t.Errorf("Tabulation of '%#v'.\nWanted:\n%#v\nGot :\n%#v\n", tc.inTxt, tc.out, got)
}
}
}
func TestTabWithTag(t *testing.T) {
testCases := []struct {
inTxt string
inT, inP string
out string
}{
{
"Test",
" - ",
" ",
" - Test",
},
{
"This very long and detailed sentence is here to demonstrate that list can be formatted and wrapped because it hopefully be so long that it will not fulfill the maximum number of authorized chars per lines is reached.",
" \x1b[31m-\x1b[0m ",
" \x1b[31m>\x1b[0m ",
" \x1b[31m-\x1b[0m This very long and detailed sentence\n \x1b[31m>\x1b[0m is here to demonstrate that list can\n \x1b[31m>\x1b[0m be formatted and wrapped because it\n \x1b[31m>\x1b[0m hopefully be so long that it will\n \x1b[31m>\x1b[0m not fulfill the maximum number of\n \x1b[31m>\x1b[0m authorized chars per lines is\n \x1b[31m>\x1b[0m reached.",
},
{
"This very long and detailed sentence is here to demonstrate that list can be formatted and wrapped because it hopefully be so long that it will not fulfill the maximum number of authorized chars per lines is reached.",
"- ",
" > ",
" - This very long and detailed sentence\n > is here to demonstrate that list can\n > be formatted and wrapped because it\n > hopefully be so long that it will\n > not fulfill the maximum number of\n > authorized chars per lines is\n > reached.",
},
{
"This very long and detailed sentence is here to demonstrate that list can be formatted and wrapped because it hopefully be so long that it will not fulfill the maximum number of authorized chars per lines is reached.",
"\x1b[31m-\x1b[0m ",
" \x1b[31m>\x1b[0m ",
" \x1b[31m-\x1b[0m This very long and detailed sentence\n \x1b[31m>\x1b[0m is here to demonstrate that list can\n \x1b[31m>\x1b[0m be formatted and wrapped because it\n \x1b[31m>\x1b[0m hopefully be so long that it will\n \x1b[31m>\x1b[0m not fulfill the maximum number of\n \x1b[31m>\x1b[0m authorized chars per lines is\n \x1b[31m>\x1b[0m reached.",
},
{
"This very long and detailed sentence is here to demonstrate that list can be formatted and wrapped because it hopefully be so long that it will not fulfill the maximum number of authorized chars per lines is reached.",
"- ",
"",
"- This very long and detailed sentence\n is here to demonstrate that list can\n be formatted and wrapped because it\n hopefully be so long that it will not\n fulfill the maximum number of\n authorized chars per lines is reached.",
},
{
"This very long and detailed sentence is here to demonstrate that list can be formatted and wrapped because it hopefully be so long that it will not fulfill the maximum number of authorized chars per lines is reached.",
" - ",
"> ",
" - This very long and detailed sentence\n> is here to demonstrate that list can\n> be formatted and wrapped because it\n> hopefully be so long that it will\n> not fulfill the maximum number of\n> authorized chars per lines is\n> reached.",
},
}
for _, tc := range testCases {
got := Tab(tc.inTxt, tc.inT, tc.inP, 40)
if got != tc.out {
t.Errorf("Inserting tag (tag='%s', indent='%s') failed.\nWant:\n%#v\nGot :\n%#v\n", tc.inT, tc.inP, tc.out, got)
}
}
}
func TestIndent(t *testing.T) {
testCases := []struct {
inTxt string
inT, inP string
out string
}{
{
"Test",
" - ",
" ",
" - Test",
},
{
"This two-lines paragraph will be indented with a bullet on the first line.\nSecond line is only here for test with no real message or interesting content.",
" \x1b[31m-\x1b[0m ",
" \x1b[31m>\x1b[0m ",
" \x1b[31m-\x1b[0m This two-lines paragraph will be indented with a bullet on the first line.\n \x1b[31m>\x1b[0m Second line is only here for test with no real message or interesting content.",
},
{
"This two-lines paragraph will be indented with a bullet on the first line.\nSecond line is only here for test with no real message or interesting content.",
"- ",
" > ",
" - This two-lines paragraph will be indented with a bullet on the first line.\n > Second line is only here for test with no real message or interesting content.",
},
{
"This two-lines paragraph will be indented with a bullet on the first line.\nSecond line is only here for test with no real message or interesting content.",
"\x1b[31m-\x1b[0m ",
" \x1b[31m>\x1b[0m ",
" \x1b[31m-\x1b[0m This two-lines paragraph will be indented with a bullet on the first line.\n \x1b[31m>\x1b[0m Second line is only here for test with no real message or interesting content.",
},
{
"This two-lines paragraph will be indented with a bullet on the first line.\nSecond line is only here for test with no real message or interesting content.",
"- ",
"",
"- This two-lines paragraph will be indented with a bullet on the first line.\n Second line is only here for test with no real message or interesting content.",
},
{
"This two-lines paragraph will be indented with a bullet on the first line.\nSecond line is only here for test with no real message or interesting content.",
" - ",
"> ",
" - This two-lines paragraph will be indented with a bullet on the first line.\n> Second line is only here for test with no real message or interesting content.",
},
}
for _, tc := range testCases {
got := Indent(tc.inTxt, tc.inT, tc.inP)
if got != tc.out {
t.Errorf("Inserting tag (tag='%s', indent='%s') failed.\nWanted:\n%#v\nGot :\n%#v\n", tc.inT, tc.inP, tc.out, got)
}
}
}
func TestJustify(t *testing.T) {
testCases := []struct {
in string
sz int
out string
}{
{"Coucou", 10, "Coucou "},
{"Coucou", 6, "Coucou"},
{"Coucou ", 6, "Coucou\n "},
{"This is a long sentence", 10, "This is a \nlong \nsentence "},
{"This \x1b[34mis\x1b[0m a long sentence", 10, "This \x1b[34mis\x1b[0m a \nlong \nsentence "},
{"This \x1b[34mis a long sentence\x1b[0m", 10, "This \x1b[34mis a \nlong \nsentence\x1b[0m "},
}
for _, tc := range testCases {
got := Justify(tc.in, tc.sz)
if got != tc.out {
t.Errorf("Justify to %d failed for %#v.\nWant:\n'%#v'\nGot :\n'%#v'\n", tc.sz, tc.in, tc.out, got)
}
}
}
func TestLeft(t *testing.T) {
testCases := []struct {
in string
sz int
out string
}{
{"Coucou", 9, "Coucou "},
{"\x1b[34mCoucou\x1b[0m", 9, "\x1b[34mCoucou\x1b[0m "},
{"Coucou", 6, "Coucou"},
{"Coucou, c'est nous", 9, "Coucou, \nc'est \nnous "},
}
for _, tc := range testCases {
got := Left(tc.in, tc.sz)
if got != tc.out {
t.Errorf("Align Left failed for '%s' (max %d).\nWant:\n%s\nGot :\n%s\n", tc.in, tc.sz, tc.out, got)
}
}
}
func TestRigth(t *testing.T) {
testCases := []struct {
in string
sz int
out string
}{
{"Coucou", 9, " Coucou"},
{"\x1b[34mCoucou\x1b[0m", 9, " \x1b[34mCoucou\x1b[0m"},
{"Coucou", 6, "Coucou"},
{"Coucou, c'est nous", 9, " Coucou,\n c'est\n nous"},
}
for _, tc := range testCases {
got := Right(tc.in, tc.sz)
if got != tc.out {
t.Errorf("Align right failed for '%s' (max %d).\nWant:\n'%v'\nGot :\n'%v'\n", tc.in, tc.sz, tc.out, got)
}
}
}
func TestCenter(t *testing.T) {
testCases := []struct {
in string
sz int
out string
}{
{"Coucou", 9, " Coucou "},
{"\x1b[34mCoucou\x1b[0m", 9, " \x1b[34mCoucou\x1b[0m "},
{"Coucou", 6, "Coucou"},
{"Coucou, c'est nous", 9, " Coucou, \n c'est \n nous "},
}
for _, tc := range testCases {
got := Center(tc.in, tc.sz)
if got != tc.out {
t.Errorf("center failed for '%s' (max %d).\nWant:\n%#v\nGot :\n%#v\n", tc.in, tc.sz, tc.out, got)
}
}
}
func TestColumnize(t *testing.T) {
testCases := []struct {
in []string
out string
}{
{
[]string{"1\n2\n3\n4", "a\nb\nc\nd\ne"},
"1 a\n2 b\n3 c\n4 d\n e",
},
}
for _, tc := range testCases {
got := Columnize(tc.in...)
if got != tc.out {
t.Errorf("Columnize failed for %#v.\nWant:\n%#v\nGot :\n%#v\n", tc.in, tc.out, got)
}
}
}
func TestRowwise(t *testing.T) {
testCases := []struct {
in []string
out string
}{
{
[]string{"1\t2\t3\t4", "a\tb\tc\td\te"},
"1 2 3 4 \na b c d e",
},
}
for _, tc := range testCases {
got := Rowwise(tc.in...)
if got != tc.out {
t.Errorf("Rowwise failed for %#v.\nWant:\n%#v\nGot :\n%#v\n", tc.in, tc.out, got)
}
}
}
func TestTabulate(t *testing.T) {
testCases := []struct {
in string
out string
}{
{
"1\t2\t3\t4\t\na\tb\tc\td\te",
"1 2 3 4 \na b c d e",
},
}
for _, tc := range testCases {
got := Tabulate(tc.in)
if got != tc.out {
t.Errorf("Tabulate failed for %#v.\nWant:\n%#v\nGot :\n%#v\n", tc.in, tc.out, got)
}
}
}