-
Notifications
You must be signed in to change notification settings - Fork 26
/
table_test.go
277 lines (240 loc) · 9.62 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
// Copyright 2017 modood. All rights reserved.
// license that can be found in the LICENSE file.
package table_test
import (
"testing"
"time"
. "github.com/modood/table"
. "github.com/smartystreets/goconvey/convey"
)
func TestOutput(t *testing.T) {
Convey("Output content without panic", t, func() {
type Repo struct {
Name string
Author string
URL string
}
s := []Repo{
{"table", "modood", "https://github.com/modood/table"},
}
Println("\n\nBox Drawing:")
Output(s)
Println("\nPure Ascii:")
OutputA(s)
})
}
func TestTable(t *testing.T) {
Convey("Get table content", t, func() {
Convey("non-slice, should warning", func() {
So(Table(nil), ShouldEqual, "warning: sliceconv: param \"slice\" should be on slice value")
So(AsciiTable(nil), ShouldEqual, "warning: sliceconv: param \"slice\" should be on slice value")
})
Convey("slice of int, should warning", func() {
s := []int{1, 2, 3}
So(Table(s), ShouldEqual, "warning: table: items of slice should be on struct value")
So(AsciiTable(s), ShouldEqual, "warning: table: items of slice should be on struct value")
})
Convey("slice of struct pointers, should work correctly", func() {
type S struct {
Celsius string
}
s := []*S{{"39.5℃"}, {"37.34℃"}}
var tb = []struct {
content string
expected string
}{
{Table(s), `
┌──────────┐
│ Celsius │
├──────────┤
│ 39.5℃ │
│ 37.34℃ │
└──────────┘`},
{AsciiTable(s), `
+----------+
| Celsius |
+----------+
| 39.5℃ |
| 37.34℃ |
+----------+`},
}
for _, tt := range tb {
So("\n"+tt.content, ShouldEqual, tt.expected)
}
})
Convey("special characters should work correctly", func() {
type S struct {
Celsius string
}
s := []S{{"39.5℃"}, {"37.34℃"}}
var tb = []struct {
content string
expected string
}{
{Table(s), `
┌──────────┐
│ Celsius │
├──────────┤
│ 39.5℃ │
│ 37.34℃ │
└──────────┘`},
{AsciiTable(s), `
+----------+
| Celsius |
+----------+
| 39.5℃ |
| 37.34℃ |
+----------+`},
}
for _, tt := range tb {
So("\n"+tt.content, ShouldEqual, tt.expected)
}
})
Convey("unexported field should be ignored", func() {
type Field struct {
Exported float32
unexported float32
}
s := []Field{{1.2, 2.4}, {4.8, 9.6}}
var tb = []struct {
content string
expected string
}{
{Table(s), `
┌──────────┐
│ Exported │
├──────────┤
│ 1.2 │
│ 4.8 │
└──────────┘`},
{AsciiTable(s), `
+----------+
| Exported |
+----------+
| 1.2 |
| 4.8 |
+----------+`},
}
for _, tt := range tb {
So("\n"+tt.content, ShouldEqual, tt.expected)
}
})
Convey("Field: string(Chinese)", func() {
type Poet struct {
Name string
Dynasty string
Live string
}
s := []Poet{
{"李白", "唐朝", "701年-762年"},
{"李清照", "宋朝", "1084年-1155年"},
}
var tb = []struct {
content string
expected string
}{
{Table(s), `
┌───────────┬─────────┬─────────────────┐
│ Name │ Dynasty │ Live │
├───────────┼─────────┼─────────────────┤
│ 李白 │ 唐朝 │ 701年-762年 │
│ 李清照 │ 宋朝 │ 1084年-1155年 │
└───────────┴─────────┴─────────────────┘`},
{AsciiTable(s), `
+-----------+---------+-----------------+
| Name | Dynasty | Live |
+-----------+---------+-----------------+
| 李白 | 唐朝 | 701年-762年 |
| 李清照 | 宋朝 | 1084年-1155年 |
+-----------+---------+-----------------+`},
}
for _, tt := range tb {
So("\n"+tt.content, ShouldEqual, tt.expected)
}
})
Convey("Field: string, int, bool, time.Time", func() {
type User struct {
ID string
Name string
Age int
Deleted bool
Created time.Time
}
s := []User{
{"8", "Captain Jack Sparrow", 31, false, time.Date(2017, time.November, 8, 23, 12, 43, 249437302, time.UTC)},
{"9", "William Turner", 18, false, time.Date(2009, time.February, 9, 2, 4, 25, 363779979, time.UTC)},
{"10", "Davy Jones", 120, true, time.Date(1965, time.October, 10, 31, 10, 26, 106273532, time.UTC)},
}
var tb = []struct {
content string
expected string
}{
{Table(s), `
┌────┬──────────────────────┬─────┬─────────┬─────────────────────────────────────────┐
│ ID │ Name │ Age │ Deleted │ Created │
├────┼──────────────────────┼─────┼─────────┼─────────────────────────────────────────┤
│ 8 │ Captain Jack Sparrow │ 31 │ false │ 2017-11-08 23:12:43.249437302 +0000 UTC │
│ 9 │ William Turner │ 18 │ false │ 2009-02-09 02:04:25.363779979 +0000 UTC │
│ 10 │ Davy Jones │ 120 │ true │ 1965-10-11 07:10:26.106273532 +0000 UTC │
└────┴──────────────────────┴─────┴─────────┴─────────────────────────────────────────┘`},
{AsciiTable(s), `
+----+----------------------+-----+---------+-----------------------------------------+
| ID | Name | Age | Deleted | Created |
+----+----------------------+-----+---------+-----------------------------------------+
| 8 | Captain Jack Sparrow | 31 | false | 2017-11-08 23:12:43.249437302 +0000 UTC |
| 9 | William Turner | 18 | false | 2009-02-09 02:04:25.363779979 +0000 UTC |
| 10 | Davy Jones | 120 | true | 1965-10-11 07:10:26.106273532 +0000 UTC |
+----+----------------------+-----+---------+-----------------------------------------+`},
}
for _, tt := range tb {
So("\n"+tt.content, ShouldEqual, tt.expected)
}
})
Convey("Field: float, slice, struct, map", func() {
type Mixed struct {
String string
Int int
Float float32
Bool bool
Slice []string
Struct struct {
ID string
Name string
}
Map map[string]string
}
s := []Mixed{
{"House Stark", 100210, 3.14159, false, []string{"Arya", "Bran"}, struct {
ID string
Name string
}{"10010", "Jon Snow"}, map[string]string{"10086": "Sansa Stark"}},
{"House Lannister", 2131, 1.234234, true, []string{"Tywin", "Tyrion"}, struct {
ID string
Name string
}{"23019", "Queen Cersei"}, map[string]string{"33489": "Ser Jaime"}},
}
var tb = []struct {
content string
expected string
}{
{Table(s), `
┌─────────────────┬────────┬──────────┬───────┬────────────────┬──────────────────────────────┬────────────────────────┐
│ String │ Int │ Float │ Bool │ Slice │ Struct │ Map │
├─────────────────┼────────┼──────────┼───────┼────────────────┼──────────────────────────────┼────────────────────────┤
│ House Stark │ 100210 │ 3.14159 │ false │ [Arya Bran] │ {ID:10010 Name:Jon Snow} │ map[10086:Sansa Stark] │
│ House Lannister │ 2131 │ 1.234234 │ true │ [Tywin Tyrion] │ {ID:23019 Name:Queen Cersei} │ map[33489:Ser Jaime] │
└─────────────────┴────────┴──────────┴───────┴────────────────┴──────────────────────────────┴────────────────────────┘`},
{AsciiTable(s), `
+-----------------+--------+----------+-------+----------------+------------------------------+------------------------+
| String | Int | Float | Bool | Slice | Struct | Map |
+-----------------+--------+----------+-------+----------------+------------------------------+------------------------+
| House Stark | 100210 | 3.14159 | false | [Arya Bran] | {ID:10010 Name:Jon Snow} | map[10086:Sansa Stark] |
| House Lannister | 2131 | 1.234234 | true | [Tywin Tyrion] | {ID:23019 Name:Queen Cersei} | map[33489:Ser Jaime] |
+-----------------+--------+----------+-------+----------------+------------------------------+------------------------+`},
}
for _, tt := range tb {
So("\n"+tt.content, ShouldEqual, tt.expected)
}
})
})
}