-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_test.go
85 lines (75 loc) · 1.3 KB
/
convert_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
package main
import (
"bytes"
"strings"
"testing"
)
const sampleConfig = `{
"columnLens": [
{
"start": 0,
"end": 7
},
{
"start": 7,
"end": 22
}
]
}
`
var convertCases = []struct {
input string
resultBuf int // fields trimmed, plus delimiter, plus newline
}{
{
"1 Jeffry ",
9,
},
{
"20 CaspŁr ",
11,
},
{
"4333333Doll ",
13,
},
}
func TestNewScanWriter(t *testing.T) {
sw := newScanWriter(
strings.NewReader("One Two Three"),
bytes.NewBufferString(""),
[]byte(sampleConfig),
jsonConfig,
)
if sw == nil {
t.Fatal("failed initializing scanWriter\n")
}
}
func TestConvert(t *testing.T) {
for _, tt := range convertCases {
sw := newScanWriter(
strings.NewReader(tt.input),
bytes.NewBufferString(""),
[]byte(sampleConfig),
jsonConfig,
)
sw.convert()
bufGot := sw.w.Buffered()
if bufGot != tt.resultBuf {
t.Fatalf("convert ( %v ) = %v; want %v", tt.input, bufGot, tt.resultBuf)
}
}
}
func BenchmarkConvert(b *testing.B) {
sw := newScanWriter(
strings.NewReader(convertCases[0].input),
bytes.NewBufferString(""),
[]byte(sampleConfig),
jsonConfig,
)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
sw.convert()
}
}