-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser_test.go
147 lines (110 loc) · 2.59 KB
/
parser_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
package hype
import (
"context"
"errors"
"fmt"
"os"
"strings"
"testing"
"github.com/stretchr/testify/require"
"golang.org/x/net/html"
)
func Test_Parser_ParseFolder(t *testing.T) {
t.Parallel()
r := require.New(t)
root := "testdata/parser/folder"
cab := os.DirFS(root)
p := NewParser(cab)
p.Root = root
docs, err := p.ParseExecuteFolder(context.Background(), root)
r.NoError(err)
r.Len(docs, 3)
exp := `var Canceled = errors.New`
titles := []string{"ONE", "TWO", "THREE"}
for i, doc := range docs {
r.Equal(titles[i], doc.Title)
act := doc.String()
r.Contains(act, exp)
}
}
func Test_Parser_ParseHTMLNode_Error(t *testing.T) {
t.Parallel()
tcs := []struct {
name string
node *html.Node
}{
{
name: "error node",
node: &html.Node{
Type: html.ErrorNode,
Data: "boom",
},
},
{
name: "unknown node",
node: &html.Node{
Type: 42,
Data: "boom",
},
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
r := require.New(t)
p := testParser(t, "testdata/auto/parser/hello")
_, err := p.ParseHTMLNode(tc.node, nil)
r.Error(err)
var pe ParseError
r.True(errors.As(err, &pe), err)
pe = ParseError{}
r.True(errors.Is(err, pe), err)
})
}
}
func Test_Parser_ParseFragment_Error(t *testing.T) {
t.Parallel()
r := require.New(t)
p := testParser(t, "testdata/auto/parser/hello")
_, err := p.ParseFragment(strings.NewReader(`<include`))
r.Error(err)
var pe ParseError
r.True(errors.As(err, &pe), err)
pe = ParseError{}
r.True(errors.Is(err, pe), err)
}
func Test_Parser_ParseExecuteFragment_Error(t *testing.T) {
t.Parallel()
r := require.New(t)
p := testParser(t, "testdata/auto/parser/hello")
p.NodeParsers["foo"] = func(p *Parser, el *Element) (Nodes, error) {
n := newExecuteNode(t, func(ctx context.Context, d *Document) error {
return fmt.Errorf("boom")
})
return Nodes{n}, nil
}
ctx := context.Background()
_, err := p.ParseExecuteFragment(ctx, strings.NewReader(`<include`))
r.Error(err)
var pe ParseError
r.True(errors.As(err, &pe), err)
pe = ParseError{}
r.True(errors.Is(err, pe), err)
_, err = p.ParseExecuteFragment(ctx, strings.NewReader(`<foo></foo>`))
r.Error(err)
var ee ExecuteError
r.True(errors.As(err, &ee), err)
ee = ExecuteError{}
r.True(errors.Is(err, ee), err)
}
func Test_Parser_MarshalJSON(t *testing.T) {
t.Parallel()
r := require.New(t)
p := testParser(t, "testdata/auto/snippets/simple")
p.DisablePages = true
p.Section = 42
err := p.Vars.Set("foo", "bar")
r.NoError(err)
_, err = p.ParseFile("hype.md")
r.NoError(err)
testJSON(t, "parser", p)
}