-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelems_test.go
More file actions
94 lines (84 loc) · 2.73 KB
/
Copy pathelems_test.go
File metadata and controls
94 lines (84 loc) · 2.73 KB
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
package rx
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
func TestHTML(t *testing.T) {
cases := []struct {
tpl string
want *Node
}{
{`<div>`, getNode("div")},
{`<p role="label">`, getNode("p").AddRole("label")},
{`<div class="flex">I can be < here<button>Click me</button></div>`,
getNode("div").AddClasses("flex").
SetText("I can be < here").
AddChildren(getNode("button").SetText("Click me"))},
{`<div class="flex">I can be <tag> here<button>Click me</button></div>`,
getNode("div").AddClasses("flex").
SetText("I can be <tag> here").
AddChildren(getNode("button").SetText("Click me"))},
{`<div><div></div><div></div></div>`, getNode("div").AddChildren(getNode("div"), getNode("div"))},
{`<svg><path/></svg>`, getNode("svg").AddChildren(getNode("path"))},
}
pubfields := cmpopts.IgnoreUnexported(Node{})
for _, c := range cases {
got := Get(c.tpl)
if !cmp.Equal(got, c.want, pubfields) {
t.Errorf("in %s: %s", c.tpl, cmp.Diff(got, c.want, pubfields))
}
// run checks in serialize
serialize(got, new(etree), new(Counter), make(XAS, 0))
}
}
func TestUnescape(t *testing.T) {
cases := []struct {
input string
output string
}{
{"<p>This is a paragraph.</p>", "<p>This is a paragraph.</p>"},
{"<a href="https://www.example.com">Link</a>", `<a href="https://www.example.com">Link</a>`},
{"<script>alert("Hello!");</script>", `<script>alert("Hello!");</script>`},
{"<style>body { color: red; }</style>", `<style>body { color: red; }</style>`},
{"This & that.", "This & that."},
{""Quoted text"", `"Quoted text"`},
{"It's a test.", "It's a test."},
{"Nested <b>bold</b> and <i>italic</i> text.", "Nested <b>bold</b> and <i>italic</i> text."},
{"This <em>is not fully </em> a test.", "This <em>is not fully </em> a test."},
}
for _, tc := range cases {
got := unescape(tc.input)
if got != tc.output {
t.Errorf("unescape(%s) = %s; want %s", tc.input, got, tc.output)
}
}
}
func BenchmarkHTMLSimpleDiv(b *testing.B) {
b.Run("quick-get", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
freePool()
for i := 0; i < 3000; i++ {
n := Get(`<div class="w-2 bg-zinc-200">`)
// prevent compiler to skip the work
if n == nil {
b.Fatal("invalid node returned")
}
}
}
})
b.Run("getnode", func(b *testing.B) {
for i := 0; i < b.N; i++ {
b.ReportAllocs()
freePool()
for i := 0; i < 3000; i++ {
n := getNode("div").AddClasses("w-2 bg-zinc-200")
// prevent compiler to skip the work
if n == nil {
b.Fatal("invalid node returned")
}
}
}
})
}