-
Notifications
You must be signed in to change notification settings - Fork 15
/
table_style_test.go
137 lines (128 loc) · 3.67 KB
/
table_style_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
package clif
import (
"fmt"
. "github.com/smartystreets/goconvey/convey"
"io/ioutil"
"regexp"
"testing"
)
var _testTableRenderHeader = func(str string) string {
//fmt.Printf("\n>>> USING HEADER RENDERER ON \"%s\"\n", strings.Replace(str, "\n", " ** ", -1))
rxRight := regexp.MustCompile(`^(\s*)\**(\S.+?)\**(\s*)$`)
if rxRight.MatchString(str) {
match := rxRight.FindStringSubmatch(str)
prefix := match[1]
word := match[2]
suffix := match[3]
return fmt.Sprintf("%s*%s*%s", prefix, word, suffix)
}
return str
}
var testsTableStyle = []struct {
data [][]string
expectedWidths []int
renderedTable string
}{
{
data: [][]string{
{"foo", "bar", "baz"},
},
expectedWidths: []int{17, 17, 19},
renderedTable: `fixtures/table_0`,
},
{
data: [][]string{
{"foofoofoofoofoo", "bar", "baz"},
},
expectedWidths: []int{26, 12, 15},
renderedTable: `fixtures/table_1`,
},
{
data: [][]string{
{"foo\nfoo\nfoo\nfoo\nfoo", "bar", "baz"},
},
expectedWidths: []int{17, 17, 19},
renderedTable: `fixtures/table_2`,
},
{
data: [][]string{
{"foofoofoofoofoo", "bar", "baz"},
{"foo", "barbarbarbarbar", "baz"},
},
expectedWidths: []int{21, 21, 11},
renderedTable: `fixtures/table_3`,
},
{
data: [][]string{
{"foofoofoofoofoo", "bar", "baz"},
{"foo", "barbarbarbarbar", "baz"},
{"foo", "bar", "bazbazbazbazbaz"},
},
expectedWidths: []int{17, 17, 19},
renderedTable: `fixtures/table_4`,
},
{
data: [][]string{
{"foofoofoofoofoofoofoofoofoofoofoofoofoofoofoo", "bar", "baz"},
{"foo", "barbarbarbarbarbarbarbarbarbarbarbarbarbarbar", "baz"},
{"foo", "bar", "bazbazbazbazbazbazbazbazbazbazbazbazbazbazbaz"},
},
expectedWidths: []int{17, 17, 19},
renderedTable: `fixtures/table_5`,
},
{
data: [][]string{
{
"foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo",
"bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar",
"baz baz baz baz baz baz baz baz baz baz baz baz baz baz baz baz baz baz baz",
},
},
expectedWidths: []int{17, 17, 19},
renderedTable: `fixtures/table_6`,
},
}
func TestTableStyle(t *testing.T) {
headers := []string{"H1", "H2", "H3"}
Convey("Create new style from data", t, func() {
for idx, test := range testsTableStyle {
Convey(fmt.Sprintf("%d)", idx), func() {
table := NewTable(headers)
style := NewDefaultTableStyle()
table.style = style
for _, row := range test.data {
table.AddRow(row)
}
Convey("Total width with waste + col sizes must be equal to total render size", func() {
widths := style.CalculateColWidths(table, 60)
waste := style.Waste(table.colAmount)
total := waste
for _, w := range widths {
total += w
}
So(waste, ShouldEqual, 7)
So(total, ShouldEqual, 60)
//fmt.Printf("EXP WIDTH: %v -- IS WIDTH: %v\n", test.expectedWidths, widths)
So(test.expectedWidths, ShouldResemble, widths)
})
Convey("Render table", func() {
style.HeaderRenderer = _testTableRenderHeader
out := style.Render(table, 60)
expect, _ := ioutil.ReadFile(test.renderedTable)
//fmt.Printf("\n--IS: \n%s\n---\n-- EXPECT: \n%s\n--\n", out, expect)
So(out, ShouldResemble, string(expect))
})
if test.renderedTable == "fixtures/table_5" {
Convey("Render table with open style", func() {
style = CopyTableStyle(OpenTableStyle)
style.HeaderRenderer = _testTableRenderHeader
out := style.Render(table, 60)
expect, _ := ioutil.ReadFile(test.renderedTable + "_open")
//fmt.Printf("\n--IS: \n%s\n---\n-- EXPECT: \n%s\n--\n", out, expect)
So(out, ShouldResemble, string(expect))
})
}
})
}
})
}