-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager_test.go
122 lines (103 loc) · 2.46 KB
/
manager_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
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
package views
import (
"bytes"
"html/template"
"io/ioutil"
"os"
"strings"
"testing"
"time"
)
// loadTemplateCount gets incremented every time loadTemplate() is called
var loadTemplateCount int
// tplData used to test the templates
var tplData = struct {
Today time.Time
}{
Today: time.Date(1985, 10, 31, 10, 34, 0, 0, time.UTC),
}
// manager holds the Manager instance we'll be using fot this tests.
var mngr = Manager{
BasePath: "_test_assets/templates",
Loader: loadTemplate,
Funcs: template.FuncMap{
"formatTime": func(t time.Time) string {
return t.Format(time.RFC1123)
},
},
}
func TestRender(t *testing.T) {
b := new(bytes.Buffer)
if err := mngr.Render(b, "pages/hello.html", tplData); err != nil {
t.Fatal(err)
}
s := b.String()
e := `<p>Hello!</p>`
if s != e {
t.Errorf("unexpected render result:\n%s\n\nexpected:\n%s", s, e)
}
}
func TestRenderCache(t *testing.T) {
mngr.EnableCaching()
b := new(bytes.Buffer)
c := loadTemplateCount + 1
if err := mngr.Render(b, "pages/hello.html", tplData); err != nil {
t.Fatal(err)
}
if loadTemplateCount != c {
t.Error("template not loaded on first call")
}
if err := mngr.Render(b, "pages/hello.html", tplData); err != nil {
t.Fatal(err)
}
if loadTemplateCount != c {
t.Error("template not cached on subsequent calls")
}
}
func TestRenderInLayout(t *testing.T) {
b := new(bytes.Buffer)
if err := mngr.RenderInLayout(b, "pages/hello.html", "layouts/application.html", tplData); err != nil {
t.Fatal(err)
}
s := strings.TrimSpace(b.String())
e := strings.TrimSpace(`
<!DOCTYPE html>
<html lang="en">
<head>
<title>Views Example</title>
</head>
<body>
<div>Thu, 31 Oct 1985 10:34:00 UTC</div>
<div class="container">
<p>Hello!</p>
</div>
</body>
</html>
`)
if s != e {
t.Errorf("unexpected render result:\n%s\n\nexpected:\n\n%s", s, e)
}
}
func loadTemplate(path string) ([]byte, error) {
loadTemplateCount += 1
f, err := os.Open(path)
if err != nil {
return nil, err
}
return ioutil.ReadAll(f)
}
func BenchmarkRenderNoCache(b *testing.B) {
for i := 0; i < b.N; i++ {
if err := mngr.RenderInLayout(ioutil.Discard, "pages/hello.html", "layouts/application.html", tplData); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkRenderWithCache(b *testing.B) {
mngr.EnableCaching()
for i := 0; i < b.N; i++ {
if err := mngr.RenderInLayout(ioutil.Discard, "pages/hello.html", "layouts/application.html", tplData); err != nil {
b.Fatal(err)
}
}
}