-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgodeline_test.go
166 lines (147 loc) · 5.22 KB
/
godeline_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package godeline_test
import (
"testing"
"github.com/DavidEsdrs/godeline"
editnode "github.com/DavidEsdrs/godeline/edit-node"
)
func TestTokenize(t *testing.T) {
var input string = `
<html>
<head>
<title>Página Sem Atributos</title>
</head>
<body>
<header>
<h1>Exemplo de Página HTML</h1>
<nav>
<ul>
<li><a>Início</a></li>
<li><a>Sobre</a></li>
<li><a>Contato</a></li>
</ul>
</nav>
</header>
<main>
<section>
<h2>Seção 1</h2>
<p>Esta é a primeira seção do conteúdo.</p>
</section>
<section>
<h2>Seção 2</h2>
<p>Esta é a segunda seção do conteúdo.</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</section>
</main>
<footer>
<p>Rodapé da página.</p>
</footer>
</body>
</html>
`
tree := editnode.NewEditTree()
tree.AddDelimiterType("<html>", "</html>")
tree.AddDelimiterType("<head>", "</head>")
tree.AddDelimiterType("<body>", "</body>")
tree.AddDelimiterType("<header>", "</header>")
tree.AddDelimiterType("<h1>", "</h1>")
tree.AddDelimiterType("<nav>", "</nav>")
tree.AddDelimiterType("<ul>", "</ul>")
tree.AddDelimiterType("<li>", "</li>")
tree.AddDelimiterType("<a>", "</a>")
tree.AddDelimiterType("<main>", "</main>")
tree.AddDelimiterType("<section>", "</section>")
tree.AddDelimiterType("<h2>", "</h2>")
tree.AddDelimiterType("<p>", "</p>")
tree.AddDelimiterType("<footer>", "</footer>")
tree.AddDelimiterType("<footer>", "</footer>")
proc := godeline.NewProcessor(&tree, 1<<12)
_, err := proc.Tokenize(input)
if err != nil {
t.Errorf(err.Error())
}
}
func TestTokenizeAndSanitization(t *testing.T) {
var input string = `
<html>
<head>
<title>Página Sem Atributos</title>
</head>
<body>
<header>
<h1>Exemplo de Página HTML</h1>
<nav>
<ul>
<li><a>Início</a></li>
<li><a>Sobre</a></li>
<li><a>Contato</a></li>
</ul>
</nav>
</header>
<main>
<section>
<h2>Seção 1</h2>
<p>Esta é a primeira seção do conteúdo.</p>
</section>
<section>
<h2>Seção 2</h2>
<p>Esta é a segunda seção do conteúdo.</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</section>
</main>
<footer>
<p>Rodapé da página.</p>
</footer>
</body>
</html>
`
tree := editnode.NewEditTree()
tree.AddDelimiterType("<html>", "</html>")
tree.AddDelimiterType("<head>", "</head>")
tree.AddDelimiterType("<body>", "</body>")
tree.AddDelimiterType("<header>", "</header>")
tree.AddDelimiterType("<h1>", "</h1>")
tree.AddDelimiterType("<nav>", "</nav>")
tree.AddDelimiterType("<ul>", "</ul>")
tree.AddDelimiterType("<li>", "</li>")
tree.AddDelimiterType("<a>", "</a>")
tree.AddDelimiterType("<main>", "</main>")
tree.AddDelimiterType("<section>", "</section>")
tree.AddDelimiterType("<h2>", "</h2>")
tree.AddDelimiterType("<p>", "</p>")
tree.AddDelimiterType("<footer>", "</footer>")
tree.AddDelimiterType("<footer>", "</footer>")
proc := godeline.NewProcessor(&tree, 1<<12)
proc.Sanitize()
result, err := proc.Tokenize(input)
result.Tokens() // array de tokens
if err != nil {
t.Errorf(err.Error())
}
}
func TestNested(t *testing.T) {
tree := editnode.NewEditTree()
tree.AddDelimiterType("[[", "]]")
proc := godeline.NewProcessor(&tree, 1<<12)
_, err := proc.Tokenize("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec iaculis mauris in fringilla ornare. In hac habitasse platea dictumst. Mauris et nisi id turpis vulputate fermentum. Nulla ac aliquet lorem, sed placerat est. Nam lacinia est ac velit malesuada, non lacinia erat laoreet. Praesent purus diam, consequat quis libero at, [[vu[[putate]] idawdawd ]]mperdiet purus. In ut luctus ex. Aenean semper non orci blandit varius. Vestibulum lectus est, cursus a orci sed, gravida pretium massa. Nam id elit quis massa aliquet tristique. Ut porttitor aliquam semper. Nam arcu ipsum, aliquam quis aliquam a, varius sed nulla. Cras at ante eu libero dapibus iaculis et id urna. Donec ac velit tellus. Duis vestibulum nec mi et vehicula. Pellentesque ornare volutpat rhoncus.")
if err != nil {
t.Error(err)
}
}
func TestErrorClosingNotFound(t *testing.T) {
tree := editnode.NewEditTree()
tree.AddDelimiterType("[[", "]]")
proc := godeline.NewProcessor(&tree, 1<<12)
proc.StopOnError()
_, err := proc.Tokenize("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec iaculis mauris in fringilla ornare. In hac habitasse platea dictumst. Mauris et nisi id turpis vulputate fermentum. Nulla ac aliquet lorem, sed placerat est. Nam lacinia est ac velit malesuada, non lacinia erat laoreet. Praesent purus diam, consequat quis libero at, [[vuputate]] idawdawd [[ mperdiet purus. In ut luctus ex. Aenean semper non orci blandit varius. Vestibulum lectus est, cursus a orci sed, gravida pretium massa. Nam id elit quis massa aliquet tristique. Ut porttitor aliquam semper. Nam arcu ipsum, aliquam quis aliquam a, varius sed nulla. Cras at ante eu libero dapibus iaculis et id urna. Donec ac velit tellus. Duis vestibulum nec mi et vehicula. Pellentesque ornare volutpat rhoncus.")
if err == nil || err != godeline.ErrClosingTagNotFound {
t.Errorf("failed - error didn't happen as expected")
}
}