-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathlexer_test.go
155 lines (144 loc) · 2.69 KB
/
lexer_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 httphead
import (
"bytes"
"testing"
)
func TestScannerSkipEscaped(t *testing.T) {
for _, test := range []struct {
in []byte
c byte
pos int
}{
{
in: []byte(`foo,bar`),
c: ',',
pos: 4,
},
{
in: []byte(`foo\,bar,baz`),
c: ',',
pos: 9,
},
} {
s := NewScanner(test.in)
s.SkipEscaped(test.c)
if act, exp := s.pos, test.pos; act != exp {
t.Errorf("unexpected scanner pos: %v; want %v", act, exp)
}
}
}
type readCase struct {
label string
in []byte
out []byte
err bool
}
var quotedStringCases = []readCase{
{
label: "nonterm",
in: []byte(`"`),
out: []byte(``),
err: true,
},
{
label: "empty",
in: []byte(`""`),
out: []byte(``),
},
{
label: "simple",
in: []byte(`"hello, world!"`),
out: []byte(`hello, world!`),
},
{
label: "quoted",
in: []byte(`"hello, \"world\"!"`),
out: []byte(`hello, "world"!`),
},
{
label: "quoted",
in: []byte(`"\"hello\", \"world\"!"`),
out: []byte(`"hello", "world"!`),
},
}
var commentCases = []readCase{
{
label: "nonterm",
in: []byte(`(hello`),
out: []byte(``),
err: true,
},
{
label: "empty",
in: []byte(`()`),
out: []byte(``),
},
{
label: "simple",
in: []byte(`(hello)`),
out: []byte(`hello`),
},
{
label: "quoted",
in: []byte(`(hello\)\(world)`),
out: []byte(`hello)(world`),
},
{
label: "nested",
in: []byte(`(hello(world))`),
out: []byte(`hello(world)`),
},
}
type readTest struct {
label string
cases []readCase
fn func(*Scanner) bool
}
var readTests = []readTest{
{
"ReadString",
quotedStringCases,
(*Scanner).fetchQuotedString,
},
{
"ReadComment",
commentCases,
(*Scanner).fetchComment,
},
}
func TestScannerRead(t *testing.T) {
for _, bunch := range readTests {
for _, test := range bunch.cases {
t.Run(bunch.label+" "+test.label, func(t *testing.T) {
l := &Scanner{data: []byte(test.in)}
if ok := bunch.fn(l); ok != !test.err {
t.Errorf("l.%s() = %v; want %v", bunch.label, ok, !test.err)
return
}
if !bytes.Equal(test.out, l.itemBytes) {
t.Errorf("l.%s() = %s; want %s", bunch.label, string(l.itemBytes), string(test.out))
}
})
}
}
}
func BenchmarkScannerReadString(b *testing.B) {
for _, bench := range quotedStringCases {
b.Run(bench.label, func(b *testing.B) {
for i := 0; i < b.N; i++ {
l := &Scanner{data: []byte(bench.in)}
_ = l.fetchQuotedString()
}
})
}
}
func BenchmarkScannerReadComment(b *testing.B) {
for _, bench := range commentCases {
b.Run(bench.label, func(b *testing.B) {
for i := 0; i < b.N; i++ {
l := &Scanner{data: []byte(bench.in)}
_ = l.fetchComment()
}
})
}
}