-
Notifications
You must be signed in to change notification settings - Fork 3
/
table_test.go
93 lines (74 loc) · 1.82 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
package table
import (
"bytes"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestingTable() Table {
return Table{
Headers: []string{"something", "another"},
Rows: [][]string{
{"1", "2"},
{"3", "4"},
{"3", "a longer piece of text that should stretch"},
{"but this one is longer", "shorter now"},
},
}
}
func TestTable_WriteTable(t *testing.T) {
r := require.New(t)
var buf bytes.Buffer
// write the table
tab := TestingTable()
err := tab.WriteTable(&buf, nil)
r.NoError(err)
lines := strings.Split(buf.String(), "\n")
r.Equal(6, len(lines), "the output should have %v lines", 6)
for _, header := range tab.Headers {
r.Contains(lines[0], header, "header column should contain %v", header)
}
for i, row := range tab.Rows {
line := lines[i+1]
for _, v := range row {
r.Contains(line, v, "line %v should contain %v", i+1, v)
}
}
}
func TestTable_WriteLargeTable(t *testing.T) {
r := require.New(t)
var buf bytes.Buffer
// write the table
tab := TestingTable()
for i := 0; i < 200; i++ {
tab.Rows = append(tab.Rows, []string{"x"})
}
err := tab.WriteTable(&buf, nil)
r.NoError(err)
r.Contains(buf.String(), "[ ID]", "id header should have a space in it")
}
func TestTable_WriteEmptyTable(t *testing.T) {
r := require.New(t)
var buf bytes.Buffer
// write the table
tab := TestingTable()
err := tab.WriteTable(&buf, nil)
r.NoError(err)
r.Equal(6, len(strings.Split(buf.String(), "\n")))
}
func TestTable_WriteColorNoAlts(t *testing.T) {
r := require.New(t)
var buf bytes.Buffer
// write the table
tab := TestingTable()
c := Config{
ShowIndex: true,
Color: true,
AlternateColors: true,
AltColorCodes: nil,
TitleColorCode: "",
}
err := tab.WriteTable(&buf, &c)
r.NoError(err)
r.Equal(6, len(strings.Split(buf.String(), "\n")))
}