-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_test.go
64 lines (55 loc) · 1.43 KB
/
node_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
package dom
import (
"errors"
"strings"
"testing"
"github.com/andybalholm/cascadia"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/net/html"
"github.com/crhntr/dom/spec"
)
func Test_nodeType(t *testing.T) {
for _, tt := range []struct {
HTMLNodeType html.NodeType
DOMNodeType spec.NodeType
}{
{html.ElementNode, spec.NodeTypeElement},
{html.CommentNode, spec.NodeTypeComment},
{html.DocumentNode, spec.NodeTypeDocument},
{html.DoctypeNode, spec.NodeTypeDocumentType},
{html.NodeType(100000), spec.NodeTypeUnknown},
{html.ErrorNode, spec.NodeTypeUnknown},
{html.RawNode, spec.NodeTypeUnknown},
} {
assert.Equal(t, tt.DOMNodeType, nodeType(tt.HTMLNodeType))
}
}
func parseDocument(t *testing.T, document, selector string) (*Document, *Element) {
t.Helper()
parsedDocument, err := html.Parse(strings.NewReader(document))
require.NoError(t, err)
var result *Element
if selector != "" {
result = &Element{
node: cascadia.Query(parsedDocument, cascadia.MustCompile(selector)),
}
}
return &Document{
node: parsedDocument,
}, result
}
func Test_recursiveTextContent_write_failure(t *testing.T) {
w := &writeError{}
text := &html.Node{
Type: html.TextNode,
Data: "failure",
}
require.Panics(t, func() {
recursiveTextContent(w, text)
})
}
type writeError struct{}
func (w writeError) WriteString(string) (n int, err error) {
return 0, errors.New("lemon")
}