forked from olekukonko/tablewriter
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtable_test.go
464 lines (417 loc) · 13.7 KB
/
table_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
// Copyright 2014 Oleku Konko All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
// This module is a Table Writer API for the Go Programming Language.
// The protocols were written in pure Go and works on windows and unix systems
package tablewriter
import (
"bytes"
"fmt"
"io"
"os"
"strings"
"testing"
"github.com/mattn/go-runewidth"
)
func ExampleShort() {
data := [][]string{
[]string{"A", "The Good", "500"},
[]string{"B", "The Very very Bad Man", "288"},
[]string{"C", "The Ugly", "120"},
[]string{"D", "The Gopher", "800"},
}
table := NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Sign", "Rating"})
for _, v := range data {
table.Append(v)
}
table.Render()
}
func ExampleLong() {
data := [][]string{
[]string{"Learn East has computers with adapted keyboards with enlarged print etc", " Some Data ", " Another Data"},
[]string{"Instead of lining up the letters all ", "the way across, he splits the keyboard in two", "Like most ergonomic keyboards", "See Data"},
}
table := NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Sign", "Rating"})
table.SetCenterSeparator("*")
table.SetRowSeparator("=")
for _, v := range data {
table.Append(v)
}
table.Render()
}
func ExampleCSV() {
table, _ := NewCSV(os.Stdout, "test.csv", true)
table.SetCenterSeparator("*")
table.SetRowSeparator("=")
table.Render()
}
func TestCSVInfo(t *testing.T) {
table, err := NewCSV(os.Stdout, "test_info.csv", true)
if err != nil {
t.Error(err)
return
}
table.SetAlignment(ALIGN_LEFT)
table.SetBorder(false)
table.Render()
}
func TestCSVSeparator(t *testing.T) {
table, err := NewCSV(os.Stdout, "test.csv", true)
if err != nil {
t.Error(err)
return
}
table.SetRowLine(true)
if runewidth.IsEastAsian() {
table.SetCenterSeparator("*")
table.SetColumnSeparator("‡")
} else {
table.SetCenterSeparator("*")
table.SetColumnSeparator("‡")
}
table.SetRowSeparator("-")
table.SetAlignment(ALIGN_LEFT)
table.Render()
}
func TestNoBorder(t *testing.T) {
data := [][]string{
[]string{"1/1/2014", "Domain name", "2233", "$10.98"},
[]string{"1/1/2014", "January Hosting", "2233", "$54.95"},
[]string{"", " (empty)\n (empty)", "", ""},
[]string{"1/4/2014", "February Hosting", "2233", "$51.00"},
[]string{"1/4/2014", "February Extra Bandwidth", "2233", "$30.00"},
[]string{"1/4/2014", " (Discount)", "2233", "-$1.00"},
}
var buf bytes.Buffer
table := NewWriter(&buf)
table.SetAutoWrapText(false)
table.SetHeader([]string{"Date", "Description", "CV2", "Amount"})
table.SetFooter([]string{"", "", "Total", "$145.93"}) // Add Footer
table.SetBorder(false) // Set Border to false
table.AppendBulk(data) // Add Bulk Data
table.Render()
want := ` DATE | DESCRIPTION | CV2 | AMOUNT
+----------+--------------------------+-------+---------+
1/1/2014 | Domain name | 2233 | $10.98
1/1/2014 | January Hosting | 2233 | $54.95
| (empty) | |
| (empty) | |
1/4/2014 | February Hosting | 2233 | $51.00
1/4/2014 | February Extra Bandwidth | 2233 | $30.00
1/4/2014 | (Discount) | 2233 | -$1.00
+----------+--------------------------+-------+---------+
TOTAL | $145 93
+-------+---------+
`
got := buf.String()
if got != want {
t.Errorf("border table rendering failed\ngot:\n%s\nwant:\n%s\n", got, want)
}
}
func TestWithBorder(t *testing.T) {
data := [][]string{
[]string{"1/1/2014", "Domain name", "2233", "$10.98"},
[]string{"1/1/2014", "January Hosting", "2233", "$54.95"},
[]string{"", " (empty)\n (empty)", "", ""},
[]string{"1/4/2014", "February Hosting", "2233", "$51.00"},
[]string{"1/4/2014", "February Extra Bandwidth", "2233", "$30.00"},
[]string{"1/4/2014", " (Discount)", "2233", "-$1.00"},
}
var buf bytes.Buffer
table := NewWriter(&buf)
table.SetAutoWrapText(false)
table.SetHeader([]string{"Date", "Description", "CV2", "Amount"})
table.SetFooter([]string{"", "", "Total", "$145.93"}) // Add Footer
table.AppendBulk(data) // Add Bulk Data
table.Render()
want := `+----------+--------------------------+-------+---------+
| DATE | DESCRIPTION | CV2 | AMOUNT |
+----------+--------------------------+-------+---------+
| 1/1/2014 | Domain name | 2233 | $10.98 |
| 1/1/2014 | January Hosting | 2233 | $54.95 |
| | (empty) | | |
| | (empty) | | |
| 1/4/2014 | February Hosting | 2233 | $51.00 |
| 1/4/2014 | February Extra Bandwidth | 2233 | $30.00 |
| 1/4/2014 | (Discount) | 2233 | -$1.00 |
+----------+--------------------------+-------+---------+
| TOTAL | $145 93 |
+----------+--------------------------+-------+---------+
`
got := buf.String()
if got != want {
t.Errorf("border table rendering failed\ngot:\n%s\nwant:\n%s\n", got, want)
}
}
func TestPrintingInMarkdown(t *testing.T) {
fmt.Println("TESTING")
data := [][]string{
[]string{"1/1/2014", "Domain name", "2233", "$10.98"},
[]string{"1/1/2014", "January Hosting", "2233", "$54.95"},
[]string{"1/4/2014", "February Hosting", "2233", "$51.00"},
[]string{"1/4/2014", "February Extra Bandwidth", "2233", "$30.00"},
}
var buf bytes.Buffer
table := NewWriter(&buf)
table.SetHeader([]string{"Date", "Description", "CV2", "Amount"})
table.AppendBulk(data) // Add Bulk Data
table.SetBorders(Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
table.Render()
want := `| DATE | DESCRIPTION | CV2 | AMOUNT |
|----------|--------------------------|------|--------|
| 1/1/2014 | Domain name | 2233 | $10.98 |
| 1/1/2014 | January Hosting | 2233 | $54.95 |
| 1/4/2014 | February Hosting | 2233 | $51.00 |
| 1/4/2014 | February Extra Bandwidth | 2233 | $30.00 |
`
got := buf.String()
if got != want {
t.Errorf("border table rendering failed\ngot:\n%s\nwant:\n%s\n", got, want)
}
}
func TestPrintHeading(t *testing.T) {
var buf bytes.Buffer
table := NewWriter(&buf)
table.SetHeader([]string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c"})
table.printHeading()
want := `| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C |
+---+---+---+---+---+---+---+---+---+---+---+---+
`
got := buf.String()
if got != want {
t.Errorf("header rendering failed\ngot:\n%s\nwant:\n%s\n", got, want)
}
}
func TestPrintHeadingWithoutAutoFormat(t *testing.T) {
var buf bytes.Buffer
table := NewWriter(&buf)
table.SetHeader([]string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c"})
table.SetAutoFormatHeaders(false)
table.printHeading()
want := `| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | a | b | c |
+---+---+---+---+---+---+---+---+---+---+---+---+
`
got := buf.String()
if got != want {
t.Errorf("header rendering failed\ngot:\n%s\nwant:\n%s\n", got, want)
}
}
func TestPrintFooter(t *testing.T) {
var buf bytes.Buffer
table := NewWriter(&buf)
table.SetHeader([]string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c"})
table.SetFooter([]string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c"})
table.printFooter()
want := `| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C |
+---+---+---+---+---+---+---+---+---+---+---+---+
`
got := buf.String()
if got != want {
t.Errorf("footer rendering failed\ngot:\n%s\nwant:\n%s\n", got, want)
}
}
func TestPrintFooterWithoutAutoFormat(t *testing.T) {
var buf bytes.Buffer
table := NewWriter(&buf)
table.SetAutoFormatHeaders(false)
table.SetHeader([]string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c"})
table.SetFooter([]string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c"})
table.printFooter()
want := `| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | a | b | c |
+---+---+---+---+---+---+---+---+---+---+---+---+
`
got := buf.String()
if got != want {
t.Errorf("footer rendering failed\ngot:\n%s\nwant:\n%s\n", got, want)
}
}
func TestPrintTableWithAndWithoutAutoWrap(t *testing.T) {
var buf bytes.Buffer
var multiline = `A multiline
string with some lines being really long.`
with := NewWriter(&buf)
with.Append([]string{multiline})
with.Render()
want := `+--------------------------------+
| A multiline string with some |
| lines being really long. |
+--------------------------------+
`
got := buf.String()
if got != want {
t.Errorf("multiline text rendering with wrapping failed\ngot:\n%s\nwant:\n%s\n", got, want)
}
buf.Truncate(0)
without := NewWriter(&buf)
without.SetAutoWrapText(false)
without.Append([]string{multiline})
without.Render()
want = `+-------------------------------------------+
| A multiline |
| string with some lines being really long. |
+-------------------------------------------+
`
got = buf.String()
if got != want {
t.Errorf("multiline text rendering without wrapping rendering failed\ngot:\n%s\nwant:\n%s\n", got, want)
}
}
func TestPrintLine(t *testing.T) {
header := make([]string, 12)
val := " "
want := ""
for i := range header {
header[i] = val
want = fmt.Sprintf("%s+-%s-", want, strings.Replace(val, " ", "-", -1))
val = val + " "
}
want = want + "+"
var buf bytes.Buffer
table := NewWriter(&buf)
table.SetHeader(header)
table.printLine(false)
got := buf.String()
if got != want {
t.Errorf("line rendering failed\ngot:\n%s\nwant:\n%s\n", got, want)
}
}
func TestAnsiStrip(t *testing.T) {
header := make([]string, 12)
val := " "
want := ""
for i := range header {
header[i] = "\033[43;30m" + val + "\033[00m"
want = fmt.Sprintf("%s+-%s-", want, strings.Replace(val, " ", "-", -1))
val = val + " "
}
want = want + "+"
var buf bytes.Buffer
table := NewWriter(&buf)
table.SetHeader(header)
table.printLine(false)
got := buf.String()
if got != want {
t.Errorf("line rendering failed\ngot:\n%s\nwant:\n%s\n", got, want)
}
}
func NewCustomizedTable(out io.Writer) *Table {
table := NewWriter(out)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetBorder(false)
table.SetAlignment(ALIGN_LEFT)
table.SetHeader([]string{})
return table
}
func TestSubclass(t *testing.T) {
buf := new(bytes.Buffer)
table := NewCustomizedTable(buf)
data := [][]string{
[]string{"A", "The Good", "500"},
[]string{"B", "The Very very Bad Man", "288"},
[]string{"C", "The Ugly", "120"},
[]string{"D", "The Gopher", "800"},
}
for _, v := range data {
table.Append(v)
}
table.Render()
output := string(buf.Bytes())
want := ` A The Good 500
B The Very very Bad Man 288
C The Ugly 120
D The Gopher 800
`
if output != want {
t.Error(fmt.Sprintf("Unexpected output '%v' != '%v'", output, want))
}
}
func TestAutoMergeRows(t *testing.T) {
data := [][]string{
[]string{"A", "The Good", "500"},
[]string{"A", "The Very very Bad Man", "288"},
[]string{"B", "The Very very Bad Man", "120"},
[]string{"B", "The Very very Bad Man", "200"},
}
var buf bytes.Buffer
table := NewWriter(&buf)
table.SetHeader([]string{"Name", "Sign", "Rating"})
for _, v := range data {
table.Append(v)
}
table.SetAutoMergeCells(true)
table.Render()
want := `+------+-----------------------+--------+
| NAME | SIGN | RATING |
+------+-----------------------+--------+
| A | The Good | 500 |
| | The Very very Bad Man | 288 |
| B | | 120 |
| | | 200 |
+------+-----------------------+--------+
`
got := buf.String()
if got != want {
t.Errorf("\ngot:\n%s\nwant:\n%s\n", got, want)
}
buf.Reset()
table = NewWriter(&buf)
table.SetHeader([]string{"Name", "Sign", "Rating"})
for _, v := range data {
table.Append(v)
}
table.SetAutoMergeCells(true)
table.SetRowLine(true)
table.Render()
want = `+------+-----------------------+--------+
| NAME | SIGN | RATING |
+------+-----------------------+--------+
| A | The Good | 500 |
+ +-----------------------+--------+
| | The Very very Bad Man | 288 |
+------+ +--------+
| B | | 120 |
+ + +--------+
| | | 200 |
+------+-----------------------+--------+
`
got = buf.String()
if got != want {
t.Errorf("\ngot:\n%s\nwant:\n%s\n", got, want)
}
buf.Reset()
table = NewWriter(&buf)
table.SetHeader([]string{"Name", "Sign", "Rating"})
dataWithlongText := [][]string{
[]string{"A", "The Good", "500"},
[]string{"A", "The Very very very very very Bad Man", "288"},
[]string{"B", "The Very very very very very Bad Man", "120"},
[]string{"C", "The Very very Bad Man", "200"},
}
table.AppendBulk(dataWithlongText)
table.SetAutoMergeCells(true)
table.SetRowLine(true)
table.Render()
want = `+------+--------------------------------+--------+
| NAME | SIGN | RATING |
+------+--------------------------------+--------+
| A | The Good | 500 |
+------+--------------------------------+--------+
| A | The Very very very very very | 288 |
| | Bad Man | |
+------+ +--------+
| B | | 120 |
| | | |
+------+--------------------------------+--------+
| C | The Very very Bad Man | 200 |
+------+--------------------------------+--------+
`
got = buf.String()
if got != want {
t.Errorf("\ngot:\n%s\nwant:\n%s\n", got, want)
}
}