-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraverser_test.go
More file actions
62 lines (52 loc) · 1.74 KB
/
traverser_test.go
File metadata and controls
62 lines (52 loc) · 1.74 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
package GoHtml_test
import (
"fmt"
"testing"
GoHtml "github.com/udan-jayanith/GoHTML"
)
func TestWalkthrough(t *testing.T) {
body := GoHtml.CreateNode("body")
h1 := GoHtml.CreateNode("h1")
h1.AppendText("This is a heading")
body.AppendChild(h1)
p := GoHtml.CreateNode("p")
p.AppendText("The HTML <p>tag is a fundamental element used for creating paragraphs in web development. It helps structure content, separating text into distinct blocks. When you wrap text within <p>... </p>tags, you tell browsers to treat the enclosed content as a paragraph.")
body.AppendChild(p)
traverser := GoHtml.NewTraverser(body)
resList := make([]*GoHtml.Node, 0)
traverser.Walkthrough(func(node *GoHtml.Node) bool {
resList = append(resList, node)
return GoHtml.ContinueWalkthrough
})
testList := []*GoHtml.Node{
body,
h1,
h1.GetChildNode(),
p,
p.GetChildNode(),
}
for i := range testList {
if testList[i] != resList[i] {
t.Fatal("Expected ", testList[i], "but got ", resList[i], "in index ", i)
}
}
}
func ExampleTraverser_Walkthrough() {
//Creation of the node tree.
body := GoHtml.CreateNode("body")
h1 := GoHtml.CreateNode("h1")
h1.AppendText("This is a heading")
body.AppendChild(h1)
p := GoHtml.CreateNode("p")
p.AppendText("The HTML <p>tag is a fundamental element used for creating paragraphs in web development. It helps structure content, separating text into distinct blocks. When you wrap text within <p>... </p>tags, you tell browsers to treat the enclosed content as a paragraph.")
body.AppendChild(p)
traverser := GoHtml.NewTraverser(body)
for node := range traverser.Walkthrough {
fmt.Println(node)
}
//or
traverser.Walkthrough(func(node *GoHtml.Node) GoHtml.TraverseCondition {
fmt.Println(node)
return true
})
}