forked from dogenzaka/tsv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
256 lines (235 loc) · 4.69 KB
/
parser_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
package tsv
import (
"fmt"
"golang.org/x/text/unicode/norm"
"os"
"testing"
)
type TestRow struct {
Name string
Age int
Gender string
Active bool
MiddleNames []string
BigNumber float64
SmallNumber float32
}
type TestTaggedRow struct {
Name string `tsv:"name"`
Age int `tsv:"age"`
Active bool `tsv:"active"`
Gender string `tsv:"gender"`
MiddleNames []string `tsv:"middleNames"`
}
func TestParserWithoutHeader(t *testing.T) {
file, err := os.Open("example_simple.tsv")
if err != nil {
t.Error(err)
return
}
defer file.Close()
data := TestRow{}
parser := NewParserWithoutHeader(file, &data, ",", "\\N")
i := 0
for {
eof, err := parser.Next()
if eof {
return
}
if i == 0 {
if data.Name != "alex" ||
data.Age != 10 ||
data.Gender != "male" ||
data.Active != true ||
len(data.MiddleNames) != 3 {
fmt.Println(data)
t.Error("Record does not match index:0")
if err != nil {
t.Error(err)
}
}
}
if i == 1 {
if data.Name != "john" ||
data.Age != 24 ||
data.Gender != "male" ||
data.Active != false ||
len(data.MiddleNames) != 2 {
fmt.Println(data)
t.Error("Record does not match index:1")
if err != nil {
t.Error(err)
}
}
}
if i == 2 {
if data.Name != "sara" ||
data.Age != 30 ||
data.Gender != "female" ||
data.Active != true ||
len(data.MiddleNames) != 1 {
t.Error("Record does not match index:2")
if err != nil {
t.Error(err)
}
}
}
if i == 3 {
if err == nil {
t.Error("Error should be caused")
return
}
}
if i == 4 {
if err == nil {
t.Error("Error should be caused")
return
}
}
if i == 5 {
if data.Name != "mike" ||
data.Age != 55 ||
data.Gender != "male" ||
data.Active != false ||
len(data.MiddleNames) != 0 {
fmt.Println(data)
t.Error("Record does not match index:5")
if err != nil {
t.Error(err)
}
}
}
if i == 6 {
if data.Name != "" ||
data.Age != 55 ||
data.Gender != "male" ||
data.Active != false ||
len(data.MiddleNames) != 0 {
t.Error("Record does not match index:6")
if err != nil {
t.Error(err)
}
}
}
if i == 7 {
if data.Name != "big" ||
data.Age != 69 ||
data.Gender != "female" ||
data.Active != true ||
len(data.MiddleNames) != 0 ||
data.BigNumber != 1231231231231212312321354123312312312 ||
data.SmallNumber != 12312312312541234123 {
fmt.Println(data)
t.Error("Record does not match index:7")
if err != nil {
t.Error(err)
}
}
}
i++
}
}
func TestParserTaggedStructure(t *testing.T) {
file, err := os.Open("example.tsv")
if err != nil {
t.Error(err)
return
}
defer file.Close()
data := TestTaggedRow{}
parser, err := NewParser(file, &data, ",", "\\N")
if err != nil {
t.Error(err)
return
}
i := 0
for {
eof, err := parser.Next()
if eof {
return
}
if i == 0 {
if err != nil {
t.Error(err)
}
if data.Name != "alex" ||
data.Age != 10 ||
data.Gender != "male" ||
data.Active != true ||
len(data.MiddleNames) != 2 {
fmt.Println(data)
fmt.Println(data.Name)
t.Error("Record does not match index:0")
}
}
if i == 1 {
if err != nil {
t.Error(err)
}
if data.Name != "john" ||
data.Age != 24 ||
data.Gender != "male" ||
data.Active != false ||
len(data.MiddleNames) != 1 {
fmt.Println(data)
t.Error("Record does not match index:1")
}
}
if i == 2 {
if err != nil {
t.Error(err)
}
if data.Name != "sara" ||
data.Age != 30 ||
data.Gender != "female" ||
data.Active != true ||
len(data.MiddleNames) != 0 {
fmt.Println(data)
t.Error("Record does not match index:2")
}
}
i++
}
}
func TestParserNormalize(t *testing.T) {
file, err := os.Open("example_norm.tsv")
if err != nil {
t.Error(err)
return
}
defer file.Close()
data := TestRow{}
parser, err := NewParser(file, &data, ",", "\\N")
if err != nil {
t.Error(err)
return
}
// Use NFC as normalization
parser.normalize = norm.NFKC
i := 0
for {
eof, err := parser.Next()
if eof {
return
}
if err != nil {
t.Error(err)
}
if i == 0 && data.Name != "アレックス" {
t.Errorf("name is not normalized %v", data.Name)
}
if i == 1 && data.Name != "デボラ" {
t.Errorf("name is not normalized %v", data.Name)
}
if i == 2 && data.Name != "デボラ" {
t.Errorf("name is not normalized %v", data.Name)
}
if i == 3 && data.Name != "(テスト)" {
t.Errorf("name is not normalized %v", data.Name)
}
if i == 4 && data.Name != "/" {
t.Errorf("name is not normalized %v", data.Name)
}
i++
}
}