-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_test.go
More file actions
66 lines (57 loc) · 1.19 KB
/
parser_test.go
File metadata and controls
66 lines (57 loc) · 1.19 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
package GoHtml_test
import (
"fmt"
"os"
"strings"
"testing"
GoHtml "github.com/udan-jayanith/GoHTML"
)
func TestDecode(t *testing.T) {
file, err := os.Open("./test-files/3.html")
if err != nil {
t.Fatal(err)
return
}
defer file.Close()
node, err := GoHtml.Decode(file)
if err != nil {
t.Fatal(err)
return
}
var builder strings.Builder
GoHtml.Encode(&builder, node)
}
func TestDecodeWithAEmptyReader(t *testing.T) {
node, err := GoHtml.Decode(strings.NewReader(""))
if err == nil {
t.Fatal("Expected a error but got no error")
} else if node != nil {
t.Fatal("Expected node to be nil but got a Node")
}
}
func ExampleDecode() {
r := strings.NewReader(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Profile</title>
</head>
<body>
<h1 class="username">Udan</h1>
<p class="email">udanjayanith@gmail.com</p>
<p>Joined: 01/08/2024</p>
</body>
</html>
`)
rootNode, _ := GoHtml.Decode(r)
titleNode := rootNode.QuerySelector("title")
title := ""
if titleNode != nil {
title = titleNode.GetInnerText()
}
fmt.Println(title)
//Output:
//User Profile
}