search_node.go

This commit is contained in:
2026-03-02 22:26:28 +03:00
parent 33b08bdd00
commit 7db719e3ff
9 changed files with 134 additions and 17 deletions

76
search_test.go Normal file
View File

@@ -0,0 +1,76 @@
package scraper
import (
"fmt"
"strings"
"testing"
"golang.org/x/net/html"
)
const htmlStr = `<div><span id="test">Hello</span><span id="test">World</span></div>`
/*
func Must[T any](v T, err error) T {
if err != nil {
panic(err)
}
return v
}
*/
func TestSearchElem(t *testing.T) {
doc := Must(
html.Parse(strings.NewReader(htmlStr)),
)
ch := SearchElem(doc, "span")
count := 0
for range ch {
count++
}
if count != 2 {
t.Errorf("Expected: 2 span elements, got: %d", count)
}
}
func TestSearchAttr(t *testing.T) {
doc := Must(
html.Parse(strings.NewReader(htmlStr)),
)
ch := SearchAttr(doc, "id", "test")
count := 0
for range ch {
count++
}
if count != 2 {
t.Errorf("Expected: 2 span elements with id 'test', got: %d", count)
}
}
func TestSearchElemAttr(t *testing.T) {
doc := Must(
html.Parse(strings.NewReader(htmlStr)),
)
ch := SearchElemAttr(doc, "span", "id", "test")
count := 0
for range ch {
count++
}
if count != 2 {
t.Errorf("Expected: 2 span elements with id 'test', got: %d", count)
}
}
func TestCrawlText(t *testing.T) {
fmt.Println(
CrawlText(Must(
html.Parse(strings.NewReader(htmlStr)),
)),
)
}