-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathlexer_test.go
85 lines (79 loc) · 1.44 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
package main
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func TestOpenTagLexer(t *testing.T) {
tests := []struct {
input string
want []*attr
}{
{
"<div>",
[]*attr{},
},
{
"<div disabled>",
[]*attr{{name: stringPos{"disabled", pos(5)}}},
},
{
`<div class="foo">`,
[]*attr{{name: stringPos{"class", pos(5)}, value: stringPos{"foo", pos(12)}}},
},
{
`<p data-^name="/foo/bar/^value" thing="^asd" >`,
[]*attr{
{
name: stringPos{
"data-^name",
pos(5),
},
value: stringPos{
"/foo/bar/^value",
pos(17),
},
},
{
name: stringPos{
"thing",
pos(36),
},
value: stringPos{
"^asd",
pos(43),
},
},
},
},
}
opts := cmp.AllowUnexported(attr{}, stringPos{})
for _, tt := range tests {
t.Run("", func(t *testing.T) {
got, err := scanAttrs(tt.input)
if err != nil {
t.Fatalf("scanAttrs: %v", err)
}
if diff := cmp.Diff(tt.want, got, opts); diff != "" {
t.Errorf("(-want, +got)\n%s", diff)
}
})
}
}
func FuzzOpenTagLexer(f *testing.F) {
seeds := []string{
"<a href=\"https://adhoc.team/\">",
"<b>",
"<input checked>",
}
for _, seed := range seeds {
f.Add([]byte(seed))
}
f.Fuzz(func(t *testing.T, in []byte) {
_, err := scanAttrs(string(in))
if err != nil {
if _, ok := err.(openTagScanError); !ok {
t.Errorf("expected scan error, got %T %v", err, err)
}
}
})
}