-
Notifications
You must be signed in to change notification settings - Fork 3
/
ltsv_test.go
318 lines (295 loc) · 7.71 KB
/
ltsv_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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package ltsv
import (
"bytes"
"fmt"
"io"
"testing"
)
type readerTest struct {
value string
records []map[string]string
}
var readerTests = []readerTest{
{
`host:127.0.0.1 ident:- user:frank time:[10/Oct/2000:13:55:36 -0700] req:GET /apache_pb.gif
HTTP/1.0 status:200 size:2326 referer:http://www.example.com/start.html ua:Mozilla/4.08 [en] (Win98; I ;Nav)
`,
[]map[string]string{
{"host": "127.0.0.1", "ident": "-", "user": "frank", "time": "[10/Oct/2000:13:55:36 -0700]", "req": "GET /apache_pb.gif"},
{"status": "200", "size": "2326", "referer": "http://www.example.com/start.html", "ua": "Mozilla/4.08 [en] (Win98; I ;Nav)"},
},
},
{
` trimspace :こんにちは
trim space :こんばんは
日本語:ラベル
nolabelnofield
ha,s.p-un_ct: おはよう `,
[]map[string]string{
{"trimspace": "こんにちは"},
{"trim space": "こんばんは"},
{"日本語": "ラベル"},
{"ha,s.p-un_ct": " おはよう "},
},
},
{
`label:こんにちは こんばんは
label2:こんばんは
こんにちは`,
[]map[string]string{
{"label": "こんにちは"},
{"label2": "こんばんは"},
},
},
{
`
hoge:foo bar:baz
perl:5.17.8 ruby:2.0 python:2.6
sushi:寿司 tennpura:天ぷら ramen:ラーメン gyoza:餃子
`,
[]map[string]string{
{"hoge": "foo", "bar": "baz"},
{"perl": "5.17.8", "ruby": "2.0", "python": "2.6"},
{"sushi": "寿司", "tennpura": "天ぷら", "ramen": "ラーメン", "gyoza": "餃子"},
},
},
{
`
hoge: bar:
perl: ruby:
`,
[]map[string]string{
{"hoge": "", "bar": ""},
{"perl": "", "ruby": ""},
},
},
}
type structRecordA struct {
host string
ident string
user string
time string
req string
}
type structRecordB struct {
status int
size int
referer string
ua string
}
type structTest struct {
value string
records []interface{}
}
var structTests = []structTest{
{
`host:127.0.0.1 ident:- user:frank time:[10/Oct/2000:13:55:36 -0700] req:GET /apache_pb.gif
status:200 size:2326 referer:http://www.example.com/start.html ua:Mozilla/4.08 [en] (Win98; I ;Nav)
`,
[]interface{}{
&structRecordA{host: "127.0.0.1", ident: "-", user: "frank", time: "[10/Oct/2000:13:55:36 -0700]", req: "GET /apache_pb.gif"},
&structRecordB{status: 200, size: 2326, referer: "http://www.example.com/start.html", ua: "Mozilla/4.08 [en] (Win98; I ;Nav)"},
},
},
}
type structRecordC struct {
Host string `ltsv:"host"`
Ident string `ltsv:"ident"`
User string `ltsv:"user"`
Time string `ltsv:"time"`
Req string `ltsv:"req"`
}
func (s *structRecordC) String() string {
return fmt.Sprintf("host:%v ident:%v user:%v time:%v req:%v",
s.Host, s.Ident, s.User, s.Time, s.Req)
}
type structLoadTest struct {
value string
records []structRecordC
}
var structLoadTests = []structLoadTest{
{
`host:127.0.0.1 ident:- user:frank time:[10/Oct/2000:13:55:36 -0700] req:GET /apache_pb.gif
`,
[]structRecordC{
structRecordC{Host: "127.0.0.1", Ident: "-", User: "frank", Time: "[10/Oct/2000:13:55:36 -0700]", Req: "GET /apache_pb.gif"},
},
},
}
func TestReaderRead(t *testing.T) {
for n, test := range readerTests {
reader := NewReader(bytes.NewBufferString(test.value))
for i, result := range test.records {
record, err := reader.Read()
if err != nil {
t.Errorf("error %v at test %d, line %d", err, n, i)
}
for label, field := range result {
if record[label] != field {
t.Errorf("wrong field %v at test %d, line %d, label %s, field %s", record[label], n, i, label, field)
}
}
if len(result) != len(record) {
t.Errorf("wrong size of record %d at test %d, line %d", len(record), n, i)
}
}
_, err := reader.Read()
if err == nil || err != io.EOF {
t.Errorf("expected EOF got %v at test %d", err, n)
}
}
}
func TestReaderLoad(t *testing.T) {
for n, test := range structLoadTests {
reader := NewReader(bytes.NewBufferString(test.value))
for i, result := range test.records {
st := structRecordC{}
err := reader.Load(&st)
if err != nil {
t.Errorf("error %v at test %d, line %d", err, n, i)
}
if st.String() != result.String() {
t.Errorf("got: %s", st.String())
}
}
st := structRecordC{}
err := reader.Load(&st)
if err == nil || err != io.EOF {
t.Errorf("expected EOF got %v at test %d", err, n)
}
}
}
func TestReaderReadAll(t *testing.T) {
for n, test := range readerTests {
reader := NewReader(bytes.NewBufferString(test.value))
records, err := reader.ReadAll()
if err != nil {
t.Errorf("error %v at test %d", err, n)
}
if len(test.records) != len(records) {
t.Errorf("wrong size of records %d at test %d", len(records), n)
} else {
for i, result := range test.records {
record := records[i]
for label, field := range result {
if record[label] != field {
t.Errorf("wrong field %v at test %d, line %d, label %s, field %s", record[label], n, i, label, field)
}
}
if len(result) != len(record) {
t.Errorf("wrong size of record %d at test %d, line %d", len(record), n, i)
}
}
}
}
}
func TestWriterWrite(t *testing.T) {
var buf bytes.Buffer
for n, test := range readerTests {
buf.Reset()
writer := NewWriter(&buf)
for i, record := range test.records {
err := writer.Write(record)
if err != nil {
t.Errorf("error %v at test %d, line %d", err, n, i)
}
}
writer.Flush()
reader := NewReader(&buf)
records, err := reader.ReadAll()
if err != nil {
t.Errorf("error %v at test %d", err, n)
continue
}
if len(records) != len(test.records) {
t.Errorf("wrong size of records %d at test %d", len(records), n)
} else {
for i := 0; i < len(test.records); i++ {
record := records[i]
result := test.records[i]
for label, field := range result {
if field != record[label] {
t.Errorf("wrong field %s at test %d, line %d, label %s, field %s", record[label], n, i, label, field)
}
}
}
}
}
}
func TestWriterWriteStruct(t *testing.T) {
var buf bytes.Buffer
for n, test := range structTests {
buf.Reset()
writer := NewWriter(&buf)
for i, record := range test.records {
err := writer.Write(record)
if err != nil {
t.Errorf("error %v at test %d, line %d", err, n, i)
}
}
writer.Flush()
if buf.String() != test.value {
t.Errorf("expect:\n%s\ngot:\n%v\n", test.value, buf.String())
}
}
}
func TestWriterWriteAll(t *testing.T) {
var buf bytes.Buffer
for n, test := range readerTests {
buf.Reset()
writer := NewWriter(&buf)
writer.WriteAll(test.records)
reader := NewReader(&buf)
records, err := reader.ReadAll()
if err != nil {
t.Errorf("error %v at test %d", err, n)
continue
}
if len(records) != len(test.records) {
t.Errorf("wrong size of records %d at test %d", len(records), n)
} else {
for i := 0; i < len(test.records); i++ {
record := records[i]
result := test.records[i]
for label, field := range result {
if field != record[label] {
t.Errorf("wrong field %s at test %d, line %d, label %s, field %s", record[label], n, i, label, field)
}
}
}
}
}
}
func TestWriterWriteAllStruct(t *testing.T) {
var buf bytes.Buffer
for _, test := range structTests {
buf.Reset()
writer := NewWriter(&buf)
writer.WriteAll(test.records)
if buf.String() != test.value {
t.Errorf("expect:\n%s\ngot:\n%v\n", test.value, buf.String())
}
}
}
func BenchmarkReaderRead(b *testing.B) {
for i := 0; i < 10000; i++ {
reader := NewReader(bytes.NewBufferString(readerTests[3].value))
_, e := reader.Read()
if e != nil {
break
}
}
}
func BenchmarkWriterWrite(b *testing.B) {
var buf bytes.Buffer
for i := 0; i < 10000; i++ {
for _, test := range readerTests {
buf.Reset()
writer := NewWriter(&buf)
for _, record := range test.records {
writer.Write(record)
}
writer.Flush()
}
}
}