forked from syohex/go-texttable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtexttable_test.go
184 lines (148 loc) · 3.86 KB
/
texttable_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
package texttable
import (
"testing"
)
//
// Public Method
//
func TestDraw(t *testing.T) {
tbl := &TextTable{}
expected := `+------+----------+
| 名前 | ふりがな |
+------+----------+
| foo | ふう |
| hoge | ほげ |
+------+----------+`
tbl.SetHeader("名前", "ふりがな")
tbl.AddRow("foo", "ふう")
tbl.AddRow("hoge", "ほげ")
got := tbl.Draw()
if got != expected {
t.Errorf("[got]\n%s\n\n[expected]\n%s\n", got, expected)
}
}
func TestSetHeader(t *testing.T) {
tbl := &TextTable{}
err := tbl.SetHeader()
if err == nil {
t.Errorf("SetHeader should take one argument at least")
}
}
func TestAddRow(t *testing.T) {
tbl := &TextTable{}
tbl.SetHeader("name", "age")
err := tbl.AddRow("bob", "30", "182")
if err == nil {
t.Errorf("row length should be smaller than equal header length")
}
err = tbl.AddRow()
if err == nil {
t.Errorf("AddRow should take one argument at least")
}
}
//
// Private Function/Methods
//
func Test_calcMaxHeight(t *testing.T) {
input := []string{
"hello", "apple\nmelon\norange", "1\n2",
}
got := calcMaxHeight(input)
if got != 3 {
t.Errorf("calcMaxHeight(%s) != 3(got=%d)", input, got)
}
}
func Test_decideAlignment(t *testing.T) {
got := decideAlignment("102948")
if got != ALIGN_RIGHT {
t.Errorf("decimal string of integer alighment is 'right'")
}
got = decideAlignment("01234")
if got != ALIGN_RIGHT {
t.Errorf("octal string of integer alighment is 'right'")
}
got = decideAlignment("ff")
if got != ALIGN_RIGHT {
t.Errorf("hex string without '0x' of integer alighment is 'right'")
}
got = decideAlignment("0xaabbccdd")
if got != ALIGN_RIGHT {
t.Errorf("hex string of integer alighment is 'right'")
}
got = decideAlignment("1.245")
if got != ALIGN_RIGHT {
t.Errorf("string of float alighment is 'right'")
}
got = decideAlignment("foo")
if got != ALIGN_LEFT {
t.Errorf("string alighment is 'left'")
}
}
func Test_stringsToTableRow(t *testing.T) {
input := []string{
"apple", "orange\nmelon\ngrape\nnuts", "peach\nbanana",
}
tableRows := stringsToTableRow(input)
if len(tableRows) != 4 {
t.Errorf("returned table height=%d(Expected 4)", len(tableRows))
}
for i, row := range tableRows {
if len(row.cellUnits) != len(input) {
t.Errorf("width of tableRows[%d]=%d(Expected %d)",
i, len(row.cellUnits), len(input))
}
}
}
func Test_borderString(t *testing.T) {
tbl := new(TextTable)
tbl.maxWidths = []int{4, 5, 3, 2}
expected := "+------+-------+-----+----+"
border := tbl.borderString()
if border != expected {
t.Errorf("got %s(Expected %s)", border, expected)
}
tbl.maxWidths = []int{0}
expected = "+--+"
border = tbl.borderString()
if border != expected {
t.Errorf("got %s(Expected %s)", border, expected)
}
}
func Test_formatCellUnit(t *testing.T) {
cell := cellUnit{content: "apple", alignment: ALIGN_RIGHT}
expected := " apple "
got := formatCellUnit(&cell, 5)
if got != expected {
t.Errorf("got '%s'(Expected '%s')", got, expected)
}
expected = " apple "
got = formatCellUnit(&cell, 10)
if got != expected {
t.Errorf("got '%s'(Expected '%s')", got, expected)
}
cellLeft := cellUnit{content: "orange", alignment: ALIGN_LEFT}
expected = " orange "
got = formatCellUnit(&cellLeft, 6)
if got != expected {
t.Errorf("got '%s'(Expected '%s')", got, expected)
}
expected = " orange "
got = formatCellUnit(&cellLeft, 10)
if got != expected {
t.Errorf("got '%s'(Expected '%s')", got, expected)
}
}
func Test_generateRowString(t *testing.T) {
tbl := TextTable{}
tbl.maxWidths = []int{8, 5}
cells := []*cellUnit{
&cellUnit{content: "apple", alignment: ALIGN_RIGHT},
&cellUnit{content: "melon", alignment: ALIGN_RIGHT},
}
row := tableRow{cellUnits: cells, kind: ROW_CELLS}
got := tbl.generateRowString(&row)
expected := "| apple | melon |"
if got != expected {
t.Errorf("got '%s'(Expected '%s')", got, expected)
}
}