-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
template_test.go
49 lines (40 loc) · 1.05 KB
/
template_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
package main
import (
"bytes"
rice "github.com/GeertJohan/go.rice"
"html/template"
"strings"
"testing"
)
func TestTemplateParsing(t *testing.T) {
templateBox := rice.MustFindBox("templates")
templateString := templateBox.MustString("index.html")
tmpl, err := template.New("index").Parse(templateString)
if err != nil {
t.Error(err)
}
output := Output{
Source: Source{
Source: "Example Source",
Paragraphs: []string{"Test Paragraph", "Test Paragraph 2"},
},
ShowsParagraphs: false,
}
// Using a byte buffer to capture the output of the template and save it to a string afterwards
var tmplBuffer bytes.Buffer
err = tmpl.Execute(&tmplBuffer, output)
html := tmplBuffer.String()
if err != nil {
t.Error(err)
}
if len(html) == 0 {
t.Log("Output of the template is empty, should contain HTML output")
t.FailNow()
}
if !strings.Contains(html, "Example Source") {
t.Error("Source is missing in the template output")
}
if !strings.Contains(html, "Test Paragraph") {
t.Error("Paragraph is missing in the template output")
}
}