File: iteration_test.go

package info (click to toggle)
golang-github-puerkitobio-goquery 1.10.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 880 kB
  • sloc: sh: 27; makefile: 3
file content (143 lines) | stat: -rw-r--r-- 3,090 bytes parent folder | download
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
package goquery

import (
	"testing"

	"golang.org/x/net/html"
)

func TestEach(t *testing.T) {
	var cnt int

	sel := Doc().Find(".hero-unit .row-fluid").Each(func(i int, n *Selection) {
		cnt++
		t.Logf("At index %v, node %v", i, n.Nodes[0].Data)
	}).Find("a")

	if cnt != 4 {
		t.Errorf("Expected Each() to call function 4 times, got %v times.", cnt)
	}
	assertLength(t, sel.Nodes, 6)
}

func TestEachWithBreak(t *testing.T) {
	var cnt int

	sel := Doc().Find(".hero-unit .row-fluid").EachWithBreak(func(i int, n *Selection) bool {
		cnt++
		t.Logf("At index %v, node %v", i, n.Nodes[0].Data)
		return false
	}).Find("a")

	if cnt != 1 {
		t.Errorf("Expected Each() to call function 1 time, got %v times.", cnt)
	}
	assertLength(t, sel.Nodes, 6)
}

func TestEachEmptySelection(t *testing.T) {
	var cnt int

	sel := Doc().Find("zzzz")
	sel.Each(func(i int, n *Selection) {
		cnt++
	})
	if cnt > 0 {
		t.Error("Expected Each() to not be called on empty Selection.")
	}
	sel2 := sel.Find("div")
	assertLength(t, sel2.Nodes, 0)
}

func TestMap(t *testing.T) {
	sel := Doc().Find(".pvk-content")
	vals := sel.Map(func(i int, s *Selection) string {
		n := s.Get(0)
		if n.Type == html.ElementNode {
			return n.Data
		}
		return ""
	})
	for _, v := range vals {
		if v != "div" {
			t.Error("Expected Map array result to be all 'div's.")
		}
	}
	if len(vals) != 3 {
		t.Errorf("Expected Map array result to have a length of 3, found %v.", len(vals))
	}
}

func TestForRange(t *testing.T) {
	sel := Doc().Find(".pvk-content")
	initLen := sel.Length()
	for i := range sel.Nodes {
		single := sel.Eq(i)
		//h, err := single.Html()
		//if err != nil {
		//	t.Fatal(err)
		//}
		//fmt.Println(i, h)
		if single.Length() != 1 {
			t.Errorf("%d: expected length of 1, got %d", i, single.Length())
		}
	}
	if sel.Length() != initLen {
		t.Errorf("expected initial selection to still have length %d, got %d", initLen, sel.Length())
	}
}

func TestGenericMap(t *testing.T) {
	sel := Doc().Find(".pvk-content")
	vals := Map(sel, func(i int, s *Selection) *html.NodeType {
		n := s.Get(0)
		if n.Type == html.ElementNode {
			return &n.Type
		}
		return nil
	})
	for _, v := range vals {
		if v == nil || *v != html.ElementNode {
			t.Error("Expected Map array result to be all 'div's.")
		}
	}
	if len(vals) != 3 {
		t.Errorf("Expected Map array result to have a length of 3, found %v.", len(vals))
	}
}

func TestEachIter(t *testing.T) {
	var cnt int

	sel := Doc().Find(".hero-unit .row-fluid")

	for i, s := range sel.EachIter() {
		cnt++
		t.Logf("At index %v, node %v", i, s.Nodes[0].Data)
	}

	sel = sel.Find("a")

	if cnt != 4 {
		t.Errorf("Expected Each() to call function 4 times, got %v times.", cnt)
	}
	assertLength(t, sel.Nodes, 6)
}

func TestEachIterWithBreak(t *testing.T) {
	var cnt int

	sel := Doc().Find(".hero-unit .row-fluid")
	for i, s := range sel.EachIter() {
		cnt++
		t.Logf("At index %v, node %v", i, s.Nodes[0].Data)
		break
	}

	sel = sel.Find("a")

	if cnt != 1 {
		t.Errorf("Expected Each() to call function 1 time, got %v times.", cnt)
	}
	assertLength(t, sel.Nodes, 6)
}