-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
element_test.go
125 lines (98 loc) · 2.12 KB
/
element_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
package hype
import (
"testing"
"github.com/stretchr/testify/require"
"golang.org/x/net/html"
)
func Test_Element_StartTag(t *testing.T) {
t.Parallel()
r := require.New(t)
hn := &html.Node{
Data: "div",
}
attrs := &Attributes{}
r.NoError(attrs.Set("class", "foo"))
r.NoError(attrs.Set("id", "bar"))
table := []struct {
name string
e *Element
exp string
}{
{name: "empty", e: &Element{}, exp: ""},
{name: "with atom", e: &Element{
HTMLNode: hn,
}, exp: "<div>"},
{name: "with attrs", e: &Element{
HTMLNode: hn,
Attributes: attrs,
}, exp: `<div class="foo" id="bar">`},
}
for _, tc := range table {
t.Run(tc.name, func(t *testing.T) {
r := require.New(t)
r.Equal(tc.exp, tc.e.StartTag())
})
}
}
func Test_Element_EndTag(t *testing.T) {
t.Parallel()
table := []struct {
name string
e *Element
exp string
}{
{name: "empty", e: &Element{}, exp: ""},
{name: "with atom", e: &Element{
HTMLNode: &html.Node{Data: "div"},
}, exp: "</div>"},
}
for _, tc := range table {
t.Run(tc.name, func(t *testing.T) {
r := require.New(t)
r.Equal(tc.exp, tc.e.EndTag())
})
}
}
func Test_Element_String(t *testing.T) {
t.Parallel()
r := require.New(t)
hn := &html.Node{
Data: "div",
}
attrs := &Attributes{}
r.NoError(attrs.Set("class", "foo"))
r.NoError(attrs.Set("id", "bar"))
table := []struct {
name string
e *Element
exp string
}{
{name: "empty", e: &Element{}, exp: ""},
{name: "with atom", e: &Element{
HTMLNode: hn,
}, exp: "<div></div>"},
{name: "with attrs", e: &Element{
HTMLNode: hn,
Attributes: attrs,
}, exp: `<div class="foo" id="bar"></div>`},
{name: "with kids", e: &Element{
HTMLNode: hn,
Nodes: Nodes{Text("hello")},
}, exp: "<div>hello</div>"},
}
for _, tc := range table {
t.Run(tc.name, func(t *testing.T) {
r := require.New(t)
r.Equal(tc.exp, tc.e.String())
})
}
}
func Test_Element_MarshalJSON(t *testing.T) {
t.Parallel()
r := require.New(t)
parent := NewEl("body", nil)
el := NewEl("div", parent)
err := el.Set("class", "foo")
r.NoError(err)
testJSON(t, "element", el)
}