/
query_test.go
76 lines (55 loc) · 1.71 KB
/
query_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
package domquery_test
import (
"strings"
"testing"
"github.com/hschaeidt/domquery"
)
func TestFind(t *testing.T) {
dom := `<html><head></head><body><div class="myClass"><a class="myClass">myLink</a></div><a class="myClass">otherLink</a><a class="otherClass">yetAnotherLink</a></body></html>`
q := new(domquery.Query)
q.Load(strings.NewReader(dom))
result := q.Find("{class}myClass")
res := result.All()
if len(res) != 3 {
t.Errorf("Expected: Result length of 3 - Got: Result length of %d", len(res))
}
res1 := res[0]
res2 := res[1]
res3 := res[2]
value, chain := res1.Value()
if value != "" {
t.Errorf("Expected: No value - Got: %s", value)
}
if chain == nil {
t.Error("Expected: Next chain - Got: No next chain")
}
value, chain = res2.Value()
if value != "myLink" {
t.Errorf("Expected: myLink as value - Got: %s as value", value)
}
if chain != nil {
t.Error("Expected: No next chain - Got: Next chain")
}
value, chain = res3.Value()
if value != "otherLink" {
t.Errorf("Expected: otherLink as value - Got: %s as value", value)
}
if chain != nil {
t.Error("Expected: No next chain - Got: Next chain")
}
}
func TestFindFindMultipleClasses(t *testing.T) {
dom := `<html><head></head><body><div class="myClass myClass2"><a class="myClass">myLink</a></div><a class="myClass">otherLink</a><a class="otherClass">yetAnotherLink</a></body></html>`
q := new(domquery.Query)
q.Load(strings.NewReader(dom))
result := q.Find("{class}myClass{class}myClass2")
res := result.All()
if len(res) != 1 {
t.Errorf("Expected: Result length of 1 - Got: Result length of %d", len(res))
}
res1 := res[0]
value, _ := res1.Value()
if value != "" {
t.Errorf("Expected: No value - Got: %s", value)
}
}