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
|
package dom
import (
"fmt"
"strings"
"testing"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
func TestRenderRepresentation_NoAttributes(t *testing.T) {
input := "<a>\nText\n</a>"
expected := "├─body\n│ ├─a\n│ │ ├─#text \"\\nText\\n\""
doc, err := html.Parse(strings.NewReader(input))
if err != nil {
t.Fatal(err)
}
body := FindFirstNode(doc, func(node *html.Node) bool {
return node.DataAtom == atom.Body
})
output := RenderRepresentation(body)
if output != expected {
t.Errorf("expected %q but got %q", expected, output)
}
}
func TestRenderRepresentation_MultipleAttributes(t *testing.T) {
input := `<a href="/page.html" target="_blank" class="button primary">Text</a>`
expected := `├─body
│ ├─a (href="/page.html" target="_blank" class="button primary")
│ │ ├─#text "Text"`
doc, err := html.Parse(strings.NewReader(input))
if err != nil {
t.Fatal(err)
}
body := FindFirstNode(doc, func(node *html.Node) bool {
return node.DataAtom == atom.Body
})
output := RenderRepresentation(body)
if output != expected {
t.Errorf("expected %q but got %q", expected, output)
}
}
func TestRenderRepresentation_Root(t *testing.T) {
input := `<img src="/img.png" />`
expected := strings.TrimSpace(`
#document
├─html
│ ├─head
│ ├─body
│ │ ├─img (src="/img.png")
`)
doc, err := html.Parse(strings.NewReader(input))
if err != nil {
t.Fatal(err)
}
output := RenderRepresentation(doc)
fmt.Println(output)
if output != expected {
t.Errorf("expected %q but got %q", expected, output)
}
}
|