-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
backscanner_test.go
121 lines (104 loc) · 2.29 KB
/
backscanner_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
package backscanner
import (
"io"
"strings"
"testing"
"github.com/icza/mighty"
)
func TestDefaults(t *testing.T) {
eq := mighty.Eq(t)
scanner := New(nil, 0)
eq(DefaultChunkSize, scanner.o.ChunkSize)
eq(DefaultMaxBufferSize, scanner.o.MaxBufferSize)
scanner = NewOptions(nil, 0, &Options{
ChunkSize: -1,
MaxBufferSize: -1,
})
eq(DefaultChunkSize, scanner.o.ChunkSize)
eq(DefaultMaxBufferSize, scanner.o.MaxBufferSize)
}
func TestScanner(t *testing.T) {
eq := mighty.Eq(t)
type result struct {
line string
pos int
err error
}
cases := []struct {
input string
exps []result
}{
// Empty input
{input: "", exps: []result{{"", 0, io.EOF}}},
// Normal input with \n line endings
{
input: "Start\nLine1\nLine2\nLine3\nEnd",
exps: []result{
{"End", 24, nil},
{"Line3", 18, nil},
{"Line2", 12, nil},
{"Line1", 6, nil},
{"Start", 0, nil},
{"", 0, io.EOF},
},
},
// Normal input with \r\n line endings
{
input: "Line1\r\nLine2\r\n",
exps: []result{
{"", 14, nil},
{"Line2", 7, nil},
{"Line1", 0, nil},
{"", 0, io.EOF},
},
},
}
for _, c := range cases {
// Test with different chunk sizes:
for _, chunkSize := range []int{-1, 0, 1, 2, 10, 100} {
scanner := NewOptions(strings.NewReader(c.input), len(c.input), &Options{ChunkSize: chunkSize})
i := 0
for {
line, pos, err := scanner.Line()
exp := c.exps[i]
eq(exp.line, line)
eq(exp.pos, pos)
eq(exp.err, err)
if err == io.EOF {
eq(len(c.exps)-1, i)
break
}
i++
}
}
}
}
func TestLongLine(t *testing.T) {
eq := mighty.Eq(t)
scanner := NewOptions(strings.NewReader("123456789"), 10, &Options{
MaxBufferSize: 5,
})
_, _, err := scanner.Line()
eq(ErrLongLine, err)
}
type fullBufferAndEOFReaderAt struct {
content string
}
func (r fullBufferAndEOFReaderAt) ReadAt(p []byte, off int64) (n int, err error) {
if len(p) == len(r.content) && off == 0 {
copy(p, r.content)
return len(p), io.EOF
}
return strings.NewReader(r.content).ReadAt(p, off)
}
func TestFullBufferAndEOF(t *testing.T) {
eq := mighty.Eq(t)
in := "1234567890"
scanner := NewOptions(fullBufferAndEOFReaderAt{in}, len(in), &Options{
MaxBufferSize: len(in),
})
line, pos, err := scanner.Line()
eq(nil, err)
eq(in, line)
eq(0, pos)
}