-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex_template_test.go
More file actions
82 lines (69 loc) · 1.93 KB
/
Copy pathindex_template_test.go
File metadata and controls
82 lines (69 loc) · 1.93 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package web
import (
"bytes"
"encoding/xml"
"github.com/PuerkitoBio/goquery"
"github.com/stretchr/testify/assert"
"golang.org/x/net/html"
"html/template"
"io"
"strings"
"tdd-html-templates/todo"
"testing"
)
func Test_wellFormedHtml(t *testing.T) {
model := todo.NewList()
buf := renderTemplate("index.tmpl", model)
assertWellFormedHtml(t, buf)
}
func Test_todoItemsAreShown(t *testing.T) {
model := todo.NewList()
model.Add("Foo")
model.Add("Bar")
buf := renderTemplate("index.tmpl", model)
// parse the HTML with goquery
document, err := goquery.NewDocumentFromReader(bytes.NewReader(buf.Bytes()))
if err != nil {
// if parsing fails, we stop the test here with t.FatalF
t.Fatalf("Error rendering template %s", err)
}
// assert there are two <li> elements inside the <ul class="todo-list">
selection := document.Find("ul.todo-list li")
assert.Equal(t, 2, selection.Length())
// assert the first <li> text is "Foo"
assert.Equal(t, "Foo", text(selection.Nodes[0]))
// assert the second <li> text is "Bar"
assert.Equal(t, "Bar", text(selection.Nodes[1]))
}
func text(node *html.Node) string {
// A little mess due to the fact that goquery has
// a .Text() method on Selection but not on html.Node
sel := goquery.Selection{Nodes: []*html.Node{node}}
return strings.TrimSpace(sel.Text())
}
func assertWellFormedHtml(t *testing.T, buf bytes.Buffer) {
decoder := xml.NewDecoder(bytes.NewReader(buf.Bytes()))
decoder.Strict = false
decoder.AutoClose = xml.HTMLAutoClose
decoder.Entity = xml.HTMLEntity
for {
_, err := decoder.Token()
switch err {
case io.EOF:
return // We're done, it's valid!
case nil:
// do nothing
default:
t.Fatalf("Error parsing html: %s", err)
}
}
}
func renderTemplate(templateName string, model any) bytes.Buffer {
templ := template.Must(template.ParseFiles(templateName))
var buf bytes.Buffer
err := templ.Execute(&buf, model)
if err != nil {
panic(err)
}
return buf
}