-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
tokenizer_internal_test.go
155 lines (138 loc) · 3.36 KB
/
tokenizer_internal_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
package xmltokenizer
import (
"errors"
"io"
"os"
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestOptions(t *testing.T) {
tt := []struct {
name string
options []Option
expectedOptions options
}{
{
name: "defaultOptions",
expectedOptions: defaultOptions(),
},
{
name: "less than 0",
options: []Option{
WithReadBufferSize(-1),
WithAttrBufferSize(-1),
WithAutoGrowBufferMaxLimitSize(-1),
},
expectedOptions: options{
readBufferSize: defaultReadBufferSize,
autoGrowBufferMaxLimitSize: autoGrowBufferMaxLimitSize,
attrsBufferSize: defaultAttrsBufferSize,
},
},
{
name: "readBufferSize > maxLimitGrowBufferSize",
options: []Option{
WithReadBufferSize(4 << 10),
WithAutoGrowBufferMaxLimitSize(1 << 10),
},
expectedOptions: options{
readBufferSize: 4 << 10,
autoGrowBufferMaxLimitSize: 4 << 10,
attrsBufferSize: defaultAttrsBufferSize,
},
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
tok := New(nil, tc.options...)
if diff := cmp.Diff(tok.options, tc.expectedOptions,
cmp.AllowUnexported(options{}),
); diff != "" {
t.Fatal(diff)
}
})
}
}
func TestAutoGrowBuffer(t *testing.T) {
tt := []struct {
name string
filename string
opts []Option
err error
}{
{
name: "grow buffer with alloc",
filename: "long_comment_token.xml",
opts: []Option{
WithReadBufferSize(5),
},
err: nil,
},
{
name: "grow buffer exceed max limit",
filename: "long_comment_token.xml",
opts: []Option{
WithReadBufferSize(5),
WithAutoGrowBufferMaxLimitSize(5),
},
err: errAutoGrowBufferExceedMaxLimit,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
f, err := os.Open(filepath.Join("testdata", tc.filename))
if err != nil {
panic(err)
}
defer f.Close()
tok := New(f, tc.opts...)
for {
_, err = tok.Token()
if err == io.EOF {
err = nil
break
}
if err != nil {
break
}
}
if !errors.Is(err, tc.err) {
t.Fatalf("expected error: %v, got: %v", tc.err, err)
}
})
}
}
type fnReader func(b []byte) (n int, err error)
func (f fnReader) Read(b []byte) (n int, err error) { return f(b) }
func TestReset(t *testing.T) {
r := fnReader(func(b []byte) (n int, err error) { return len(b), nil })
tok := New(r)
tok.Token() // Trigger make buffer init, cause grow buffer by alloc up to max limit: 1MB
tok.Reset(r,
WithReadBufferSize(1024),
WithAutoGrowBufferMaxLimitSize(4),
)
if expected := 1024; len(tok.buf) != expected {
t.Fatalf("expected len(t.buf): %d, got: %d", expected, len(tok.buf))
}
if expected := 1000 << 10; cap(tok.buf) != expected {
t.Fatalf("expected cap(t.buf): %d, got: %d", expected, cap(tok.buf))
}
if tok.cur != 0 {
t.Fatalf("expected cur: %d, got: cur: %d",
0, tok.cur)
}
newBufferSize := 2000 << 10
tok.Reset(r,
WithReadBufferSize(newBufferSize),
WithAutoGrowBufferMaxLimitSize(4),
)
tok.Token() // Trigger manageBuffer
if expected := newBufferSize; len(tok.buf) != expected {
t.Fatalf("expected len(t.buf): %d, got: %d", expected, len(tok.buf))
}
if expected := newBufferSize + defaultReadBufferSize; cap(tok.buf) != expected {
t.Fatalf("expected len(t.buf): %d, got: %d", expected, len(tok.buf))
}
}