-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathgotable_test.go
204 lines (171 loc) · 5.03 KB
/
gotable_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
// Package gotable_test used to test package gotable
package gotable_test
import (
"github.com/liushuochen/gotable/exception"
"strings"
"testing"
"github.com/liushuochen/gotable"
)
// Check version string whether start with "gotable".
func TestVersionPrefix(t *testing.T) {
version := gotable.Version()
if !strings.HasPrefix(version, "gotable") {
t.Errorf("expected version start switch gotable, but %s got", version)
}
}
// Test the value of gotable.TerminalDefault, gotable.Highlight, gotable.Underline and gotable.Flash.
func TestValueOfColorDisplay(t *testing.T) {
if gotable.TerminalDefault != 0 {
t.Errorf("expected gotable.TerminalDefault is 0, but %d got", gotable.TerminalDefault)
}
if gotable.Highlight != 1 {
t.Errorf("expected gotable.Highlight is 1, but %d got", gotable.Highlight)
}
if gotable.Underline != 4 {
t.Errorf("expected gotable.Underline is 4, but %d got", gotable.Underline)
}
if gotable.Flash != 5 {
t.Errorf("expected gotable.Flash is 5, but %d got", gotable.Flash)
}
}
// The value of color control
func TestValueOfColorControllers(t *testing.T) {
if gotable.Black != 30 {
t.Errorf("expected gotable.Black is 30, but %d got", gotable.Black)
}
if gotable.Red != 31 {
t.Errorf("expected gotable.Red is 31, but %d got", gotable.Red)
}
if gotable.Green != 32 {
t.Errorf("expected gotable.Green is 32, but %d got", gotable.Green)
}
if gotable.Yellow != 33 {
t.Errorf("expected gotable.Yellow is 33, but %d got", gotable.Yellow)
}
if gotable.Blue != 34 {
t.Errorf("expected gotable.Blue is 34, but %d got", gotable.Blue)
}
if gotable.Purple != 35 {
t.Errorf("expected gotable.Purple is 35, but %d got", gotable.Purple)
}
if gotable.Cyan != 36 {
t.Errorf("expected gotable.Cyan is 36, but %d got", gotable.Cyan)
}
if gotable.Write != 37 {
t.Errorf("expected gotable.Write is 37, but %d got", gotable.Write)
}
if gotable.NoneBackground != 0 {
t.Errorf("expected gotable.NoneBackground is 0, but %d got", gotable.NoneBackground)
}
}
// Create a simple table.
func TestCreateSimpleTable(t *testing.T) {
columns := []string{"country", "capital"}
_, err := gotable.Create(columns...)
if err != nil {
t.Errorf("expected err is nil, but %s got", err.Error())
return
}
}
// Create a simple table with duplicate columns.
func TestCreateSimpleTableWithDuplicateColumn(t *testing.T) {
columns := []string{"name", "name"}
_, err := gotable.Create(columns...)
if err == nil {
t.Errorf("expected err is not nil, but nil got")
}
}
// Create a simple table without column.
func TestCreateSimpleTableWithoutColumn(t *testing.T) {
_, err := gotable.Create()
switch err.(type) {
case *exception.ColumnsLengthError:
default:
t.Errorf("expected err is ColumnsLengthError, but %T got", err)
}
}
// Create a safe table.
func TestCreateSafeTable(t *testing.T) {
columns := []string{"country", "capital"}
_, err := gotable.CreateSafeTable(columns...)
if err != nil {
t.Errorf("expected err is nil, but %s got", err.Error())
return
}
}
// Create a safe table with duplicate columns.
func TestCreateSafeTableWithDuplicateColumn(t *testing.T) {
columns := []string{"name", "name"}
_, err := gotable.CreateSafeTable(columns...)
if err == nil {
t.Errorf("expected err is not nil, but nil got")
}
}
// Create a safe table without column.
func TestCreateSafeTableWithoutColumn(t *testing.T) {
_, err := gotable.CreateSafeTable()
switch err.(type) {
case *exception.ColumnsLengthError:
default:
t.Errorf("expected err is ColumnsLengthError, but %T got", err)
}
}
// Create table using struct.
func TestCreateTableByStruct(t *testing.T) {
type Student struct {
Name string `gotable:"name"`
Age string `gotable:"age"`
}
stu := &Student{
Name: "Bob",
Age: "12",
}
_, err := gotable.CreateByStruct(stu)
if err != nil {
t.Errorf("expected err is nil, but %s got.", err.Error())
}
}
// Create table using empty struct.
func TestCreateTableByStruct2(t *testing.T) {
type Student struct{}
stu := new(Student)
_, err := gotable.CreateByStruct(stu)
switch err.(type) {
case *exception.ColumnsLengthError:
default:
t.Errorf("expected err is ColumnsLengthError, but %T got", err)
}
}
// Create table using struct which contains duplicate tags.
func TestCreateTableByStruct3(t *testing.T) {
type Student struct {
Name string `gotable:"name"`
Age string `gotable:"name"`
}
stu := &Student{
Name: "Bob",
Age: "12",
}
_, err := gotable.CreateByStruct(stu)
if err == nil {
t.Error("expected got an error, but nil got.")
}
}
// Check the returned value for Versions function whether empty.
func TestVersions(t *testing.T) {
result := gotable.Versions()
if len(result) == 0 {
t.Error("Versions return an empty slice.")
}
}
// Check create table from a CSV file.
// - Check table length (expected is 3, defined in test_csv.csv).
func TestReadCSVFile(t *testing.T) {
table, err := gotable.Read("test_csv.csv")
if err != nil {
t.Errorf("expected err is nil, but %s got.", err.Error())
}
if table.Length() != 3 {
t.Errorf("expected table length is 3, but %d got.", table.Length())
}
}